blob: 93a522ef0947fcae65c32b674a364f280e30fe48 [file] [log] [blame]
Phil Burk2355edb2016-12-26 13:54:02 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Phil Burk5ed503c2017-02-01 09:38:15 -080017#ifndef AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H
18#define AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H
Phil Burk2355edb2016-12-26 13:54:02 -080019
Phil Burk71f35bb2017-04-13 16:05:07 -070020#include <assert.h>
Phil Burkc0c70e32017-02-09 13:18:38 -080021#include <mutex>
Phil Burkdec33ab2017-01-17 14:48:16 -080022
Phil Burk11e8d332017-05-24 09:59:02 -070023#include <utils/RefBase.h>
24
Phil Burk2355edb2016-12-26 13:54:02 -080025#include "fifo/FifoBuffer.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080026#include "binding/IAAudioService.h"
27#include "binding/AudioEndpointParcelable.h"
28#include "binding/AAudioServiceMessage.h"
29#include "utility/AAudioUtilities.h"
Eric Laurentcb4dae22017-07-01 19:39:32 -070030#include <media/AudioClient.h>
Phil Burkc0c70e32017-02-09 13:18:38 -080031
Phil Burk2355edb2016-12-26 13:54:02 -080032#include "SharedRingBuffer.h"
Phil Burk5ed503c2017-02-01 09:38:15 -080033#include "AAudioThread.h"
Phil Burk2355edb2016-12-26 13:54:02 -080034
Phil Burk5ed503c2017-02-01 09:38:15 -080035namespace aaudio {
Phil Burk2355edb2016-12-26 13:54:02 -080036
37// We expect the queue to only have a few commands.
38// This should be way more than we need.
39#define QUEUE_UP_CAPACITY_COMMANDS (128)
40
Phil Burkc0c70e32017-02-09 13:18:38 -080041/**
42 * Base class for a stream in the AAudio service.
43 */
44class AAudioServiceStreamBase
Phil Burk11e8d332017-05-24 09:59:02 -070045 : public virtual android::RefBase
46 , public Runnable {
Phil Burk2355edb2016-12-26 13:54:02 -080047
48public:
Phil Burk5ed503c2017-02-01 09:38:15 -080049 AAudioServiceStreamBase();
50 virtual ~AAudioServiceStreamBase();
Phil Burk2355edb2016-12-26 13:54:02 -080051
52 enum {
53 ILLEGAL_THREAD_ID = 0
54 };
55
Phil Burk4501b352017-06-29 18:12:36 -070056 std::string dump() const;
57
Phil Burkc0c70e32017-02-09 13:18:38 -080058 // -------------------------------------------------------------------
Phil Burk2355edb2016-12-26 13:54:02 -080059 /**
60 * Open the device.
61 */
Phil Burkc0c70e32017-02-09 13:18:38 -080062 virtual aaudio_result_t open(const aaudio::AAudioStreamRequest &request,
63 aaudio::AAudioStreamConfiguration &configurationOutput) = 0;
64
65 virtual aaudio_result_t close();
Phil Burk2355edb2016-12-26 13:54:02 -080066
67 /**
68 * Start the flow of data.
69 */
Phil Burk71f35bb2017-04-13 16:05:07 -070070 virtual aaudio_result_t start();
Phil Burk2355edb2016-12-26 13:54:02 -080071
72 /**
73 * Stop the flow of data such that start() can resume with loss of data.
74 */
Phil Burk71f35bb2017-04-13 16:05:07 -070075 virtual aaudio_result_t pause();
76
77 /**
78 * Stop the flow of data after data in buffer has played.
79 */
80 virtual aaudio_result_t stop();
Phil Burk2355edb2016-12-26 13:54:02 -080081
Phil Burk98d6d922017-07-06 11:52:45 -070082 aaudio_result_t stopTimestampThread();
83
Phil Burk2355edb2016-12-26 13:54:02 -080084 /**
85 * Discard any data held by the underlying HAL or Service.
86 */
Phil Burk71f35bb2017-04-13 16:05:07 -070087 virtual aaudio_result_t flush();
Phil Burk2355edb2016-12-26 13:54:02 -080088
Eric Laurentcb4dae22017-07-01 19:39:32 -070089 virtual aaudio_result_t startClient(const android::AudioClient& client __unused,
90 audio_port_handle_t *clientHandle __unused) {
91 return AAUDIO_ERROR_UNAVAILABLE;
92 }
93
94 virtual aaudio_result_t stopClient(audio_port_handle_t clientHandle __unused) {
95 return AAUDIO_ERROR_UNAVAILABLE;
96 }
97
Phil Burk11e8d332017-05-24 09:59:02 -070098 bool isRunning() const {
99 return mState == AAUDIO_STREAM_STATE_STARTED;
100 }
Eric Laurentcb4dae22017-07-01 19:39:32 -0700101
Phil Burkc0c70e32017-02-09 13:18:38 -0800102 // -------------------------------------------------------------------
Phil Burk2355edb2016-12-26 13:54:02 -0800103
Phil Burkc0c70e32017-02-09 13:18:38 -0800104 /**
105 * Send a message to the client.
106 */
107 aaudio_result_t sendServiceEvent(aaudio_service_event_t event,
108 double dataDouble = 0.0,
109 int64_t dataLong = 0);
Phil Burkdec33ab2017-01-17 14:48:16 -0800110
Phil Burkc0c70e32017-02-09 13:18:38 -0800111 /**
112 * Fill in a parcelable description of stream.
113 */
114 aaudio_result_t getDescription(AudioEndpointParcelable &parcelable);
Phil Burk2355edb2016-12-26 13:54:02 -0800115
Phil Burk2355edb2016-12-26 13:54:02 -0800116
Phil Burkc0c70e32017-02-09 13:18:38 -0800117 void setRegisteredThread(pid_t pid) {
Phil Burk2355edb2016-12-26 13:54:02 -0800118 mRegisteredClientThread = pid;
119 }
Phil Burkdec33ab2017-01-17 14:48:16 -0800120
Phil Burkc0c70e32017-02-09 13:18:38 -0800121 pid_t getRegisteredThread() const {
Phil Burk2355edb2016-12-26 13:54:02 -0800122 return mRegisteredClientThread;
123 }
124
Phil Burkc0c70e32017-02-09 13:18:38 -0800125 int32_t getFramesPerBurst() const {
126 return mFramesPerBurst;
127 }
128
129 int32_t calculateBytesPerFrame() const {
130 return mSamplesPerFrame * AAudioConvert_formatToSizeInBytes(mAudioFormat);
131 }
132
133 void run() override; // to implement Runnable
134
Phil Burk5ef003b2017-06-30 11:43:37 -0700135 void disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800136
Phil Burk2ac035f2017-06-23 14:51:14 -0700137 uid_t getOwnerUserId() const {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700138 return mMmapClient.clientUid;
Phil Burk2ac035f2017-06-23 14:51:14 -0700139 }
140
Phil Burkb63320a2017-06-30 10:28:20 -0700141 pid_t getOwnerProcessId() const {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700142 return mMmapClient.clientPid;
Phil Burkb63320a2017-06-30 10:28:20 -0700143 }
144
Phil Burk11e8d332017-05-24 09:59:02 -0700145 aaudio_handle_t getHandle() const {
146 return mHandle;
147 }
148 void setHandle(aaudio_handle_t handle) {
149 mHandle = handle;
150 }
151
Phil Burk5a26e662017-07-07 12:44:48 -0700152 aaudio_stream_state_t getState() const {
153 return mState;
154 }
155
Phil Burk2355edb2016-12-26 13:54:02 -0800156protected:
Phil Burk98d6d922017-07-06 11:52:45 -0700157
Phil Burk5a26e662017-07-07 12:44:48 -0700158 void setState(aaudio_stream_state_t state) {
159 mState = state;
160 }
161
Phil Burkc0c70e32017-02-09 13:18:38 -0800162 aaudio_result_t writeUpMessageQueue(AAudioServiceMessage *command);
163
164 aaudio_result_t sendCurrentTimestamp();
165
166 virtual aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) = 0;
167
168 virtual aaudio_result_t getDownDataDescription(AudioEndpointParcelable &parcelable) = 0;
169
Phil Burkec89b2e2017-06-20 15:05:06 -0700170 aaudio_stream_state_t mState = AAUDIO_STREAM_STATE_UNINITIALIZED;
Phil Burk2355edb2016-12-26 13:54:02 -0800171
Eric Laurentcb4dae22017-07-01 19:39:32 -0700172 pid_t mRegisteredClientThread = ILLEGAL_THREAD_ID;
Phil Burk2355edb2016-12-26 13:54:02 -0800173
Eric Laurentcb4dae22017-07-01 19:39:32 -0700174 SharedRingBuffer* mUpMessageQueue;
175 std::mutex mLockUpMessageQueue;
Phil Burk2355edb2016-12-26 13:54:02 -0800176
Eric Laurentcb4dae22017-07-01 19:39:32 -0700177 AAudioThread mAAudioThread;
Phil Burkc0c70e32017-02-09 13:18:38 -0800178 // This is used by one thread to tell another thread to exit. So it must be atomic.
Eric Laurentcb4dae22017-07-01 19:39:32 -0700179 std::atomic<bool> mThreadEnabled;
Phil Burkc0c70e32017-02-09 13:18:38 -0800180
Eric Laurentcb4dae22017-07-01 19:39:32 -0700181 aaudio_format_t mAudioFormat = AAUDIO_FORMAT_UNSPECIFIED;
182 int32_t mFramesPerBurst = 0;
183 int32_t mSamplesPerFrame = AAUDIO_UNSPECIFIED;
184 int32_t mSampleRate = AAUDIO_UNSPECIFIED;
185 int32_t mCapacityInFrames = AAUDIO_UNSPECIFIED;
186 android::AudioClient mMmapClient;
187 audio_port_handle_t mClientHandle = AUDIO_PORT_HANDLE_NONE;
188
Phil Burk11e8d332017-05-24 09:59:02 -0700189private:
Eric Laurentcb4dae22017-07-01 19:39:32 -0700190 aaudio_handle_t mHandle = -1;
Phil Burk2355edb2016-12-26 13:54:02 -0800191};
192
Phil Burk5ed503c2017-02-01 09:38:15 -0800193} /* namespace aaudio */
Phil Burk2355edb2016-12-26 13:54:02 -0800194
Phil Burk5ed503c2017-02-01 09:38:15 -0800195#endif //AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H