blob: af435b4b8f51d1c5fabaa2c094d686cc4db6bf40 [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 Burk97350f92017-07-21 15:59:44 -070023#include <media/AudioClient.h>
Phil Burk11e8d332017-05-24 09:59:02 -070024#include <utils/RefBase.h>
25
Phil Burk2355edb2016-12-26 13:54:02 -080026#include "fifo/FifoBuffer.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080027#include "binding/IAAudioService.h"
28#include "binding/AudioEndpointParcelable.h"
29#include "binding/AAudioServiceMessage.h"
30#include "utility/AAudioUtilities.h"
Phil Burk97350f92017-07-21 15:59:44 -070031#include "utility/AudioClock.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080032
Phil Burk2355edb2016-12-26 13:54:02 -080033#include "SharedRingBuffer.h"
Phil Burk5ed503c2017-02-01 09:38:15 -080034#include "AAudioThread.h"
Phil Burk523b3042017-09-13 13:03:08 -070035
36namespace android {
37 class AAudioService;
38}
Phil Burk2355edb2016-12-26 13:54:02 -080039
Phil Burk5ed503c2017-02-01 09:38:15 -080040namespace aaudio {
Phil Burk2355edb2016-12-26 13:54:02 -080041
Phil Burk39f02dd2017-08-04 09:13:31 -070042class AAudioServiceEndpoint;
43
Phil Burk2355edb2016-12-26 13:54:02 -080044// We expect the queue to only have a few commands.
45// This should be way more than we need.
46#define QUEUE_UP_CAPACITY_COMMANDS (128)
47
Phil Burkc0c70e32017-02-09 13:18:38 -080048/**
Phil Burk39f02dd2017-08-04 09:13:31 -070049 * Each instance of AAudioServiceStreamBase corresponds to a client stream.
50 * It uses a subclass of AAudioServiceEndpoint to communicate with the underlying device or port.
Phil Burkc0c70e32017-02-09 13:18:38 -080051 */
52class AAudioServiceStreamBase
Phil Burk11e8d332017-05-24 09:59:02 -070053 : public virtual android::RefBase
Phil Burk39f02dd2017-08-04 09:13:31 -070054 , public AAudioStreamParameters
Phil Burk11e8d332017-05-24 09:59:02 -070055 , public Runnable {
Phil Burk2355edb2016-12-26 13:54:02 -080056
57public:
Phil Burk39f02dd2017-08-04 09:13:31 -070058 AAudioServiceStreamBase(android::AAudioService &aAudioService);
59
Phil Burk5ed503c2017-02-01 09:38:15 -080060 virtual ~AAudioServiceStreamBase();
Phil Burk2355edb2016-12-26 13:54:02 -080061
62 enum {
63 ILLEGAL_THREAD_ID = 0
64 };
65
Phil Burka5222e22017-07-28 13:31:14 -070066 static std::string dumpHeader();
67
68 // does not include EOL
69 virtual std::string dump() const;
Phil Burk4501b352017-06-29 18:12:36 -070070
Phil Burkc0c70e32017-02-09 13:18:38 -080071 // -------------------------------------------------------------------
Phil Burk2355edb2016-12-26 13:54:02 -080072 /**
73 * Open the device.
74 */
Phil Burk39f02dd2017-08-04 09:13:31 -070075 virtual aaudio_result_t open(const aaudio::AAudioStreamRequest &request) = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -080076
77 virtual aaudio_result_t close();
Phil Burk2355edb2016-12-26 13:54:02 -080078
79 /**
Phil Burk39f02dd2017-08-04 09:13:31 -070080 * Start the flow of audio data.
81 *
82 * This is not guaranteed to be synchronous but it currently is.
83 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
Phil Burk2355edb2016-12-26 13:54:02 -080084 */
Phil Burk71f35bb2017-04-13 16:05:07 -070085 virtual aaudio_result_t start();
Phil Burk2355edb2016-12-26 13:54:02 -080086
87 /**
Phil Burk39f02dd2017-08-04 09:13:31 -070088 * Stop the flow of data so that start() can resume without loss of data.
89 *
90 * This is not guaranteed to be synchronous but it currently is.
91 * An AAUDIO_SERVICE_EVENT_PAUSED will be sent to the client when complete.
92 */
Phil Burk71f35bb2017-04-13 16:05:07 -070093 virtual aaudio_result_t pause();
94
95 /**
Phil Burk39f02dd2017-08-04 09:13:31 -070096 * Stop the flow of data after the currently queued data has finished playing.
97 *
98 * This is not guaranteed to be synchronous but it currently is.
99 * An AAUDIO_SERVICE_EVENT_STOPPED will be sent to the client when complete.
100 *
Phil Burk71f35bb2017-04-13 16:05:07 -0700101 */
102 virtual aaudio_result_t stop();
Phil Burk2355edb2016-12-26 13:54:02 -0800103
Phil Burk98d6d922017-07-06 11:52:45 -0700104 aaudio_result_t stopTimestampThread();
105
Phil Burk2355edb2016-12-26 13:54:02 -0800106 /**
Phil Burk39f02dd2017-08-04 09:13:31 -0700107 * Discard any data held by the underlying HAL or Service.
108 *
109 * An AAUDIO_SERVICE_EVENT_FLUSHED will be sent to the client when complete.
Phil Burk2355edb2016-12-26 13:54:02 -0800110 */
Phil Burk71f35bb2017-04-13 16:05:07 -0700111 virtual aaudio_result_t flush();
Phil Burk2355edb2016-12-26 13:54:02 -0800112
Phil Burk39f02dd2017-08-04 09:13:31 -0700113
Eric Laurentcb4dae22017-07-01 19:39:32 -0700114 virtual aaudio_result_t startClient(const android::AudioClient& client __unused,
115 audio_port_handle_t *clientHandle __unused) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700116 ALOGD("AAudioServiceStreamBase::startClient(%p, ...) AAUDIO_ERROR_UNAVAILABLE", &client);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700117 return AAUDIO_ERROR_UNAVAILABLE;
118 }
119
120 virtual aaudio_result_t stopClient(audio_port_handle_t clientHandle __unused) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700121 ALOGD("AAudioServiceStreamBase::stopClient(%d) AAUDIO_ERROR_UNAVAILABLE", clientHandle);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700122 return AAUDIO_ERROR_UNAVAILABLE;
123 }
124
Phil Burk11e8d332017-05-24 09:59:02 -0700125 bool isRunning() const {
126 return mState == AAUDIO_STREAM_STATE_STARTED;
127 }
Eric Laurentcb4dae22017-07-01 19:39:32 -0700128
Phil Burkc0c70e32017-02-09 13:18:38 -0800129 // -------------------------------------------------------------------
Phil Burk2355edb2016-12-26 13:54:02 -0800130
Phil Burkc0c70e32017-02-09 13:18:38 -0800131 /**
132 * Send a message to the client.
133 */
134 aaudio_result_t sendServiceEvent(aaudio_service_event_t event,
135 double dataDouble = 0.0,
136 int64_t dataLong = 0);
Phil Burkdec33ab2017-01-17 14:48:16 -0800137
Phil Burkc0c70e32017-02-09 13:18:38 -0800138 /**
139 * Fill in a parcelable description of stream.
140 */
141 aaudio_result_t getDescription(AudioEndpointParcelable &parcelable);
Phil Burk2355edb2016-12-26 13:54:02 -0800142
Phil Burk2355edb2016-12-26 13:54:02 -0800143
Phil Burkc0c70e32017-02-09 13:18:38 -0800144 void setRegisteredThread(pid_t pid) {
Phil Burk2355edb2016-12-26 13:54:02 -0800145 mRegisteredClientThread = pid;
146 }
Phil Burkdec33ab2017-01-17 14:48:16 -0800147
Phil Burkc0c70e32017-02-09 13:18:38 -0800148 pid_t getRegisteredThread() const {
Phil Burk2355edb2016-12-26 13:54:02 -0800149 return mRegisteredClientThread;
150 }
151
Phil Burkc0c70e32017-02-09 13:18:38 -0800152 int32_t getFramesPerBurst() const {
153 return mFramesPerBurst;
154 }
155
Phil Burkc0c70e32017-02-09 13:18:38 -0800156 void run() override; // to implement Runnable
157
Phil Burk5ef003b2017-06-30 11:43:37 -0700158 void disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800159
Phil Burk39f02dd2017-08-04 09:13:31 -0700160 const android::AudioClient &getAudioClient() {
161 return mMmapClient;
162 }
163
Phil Burk2ac035f2017-06-23 14:51:14 -0700164 uid_t getOwnerUserId() const {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700165 return mMmapClient.clientUid;
Phil Burk2ac035f2017-06-23 14:51:14 -0700166 }
167
Phil Burkb63320a2017-06-30 10:28:20 -0700168 pid_t getOwnerProcessId() const {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700169 return mMmapClient.clientPid;
Phil Burkb63320a2017-06-30 10:28:20 -0700170 }
171
Phil Burk11e8d332017-05-24 09:59:02 -0700172 aaudio_handle_t getHandle() const {
173 return mHandle;
174 }
175 void setHandle(aaudio_handle_t handle) {
176 mHandle = handle;
177 }
178
Phil Burk5a26e662017-07-07 12:44:48 -0700179 aaudio_stream_state_t getState() const {
180 return mState;
181 }
182
Phil Burk39f02dd2017-08-04 09:13:31 -0700183 void onVolumeChanged(float volume);
184
Phil Burk2355edb2016-12-26 13:54:02 -0800185protected:
Phil Burk98d6d922017-07-06 11:52:45 -0700186
Phil Burk39f02dd2017-08-04 09:13:31 -0700187 /**
188 * Open the device.
189 */
190 aaudio_result_t open(const aaudio::AAudioStreamRequest &request,
191 aaudio_sharing_mode_t sharingMode);
192
Phil Burk5a26e662017-07-07 12:44:48 -0700193 void setState(aaudio_stream_state_t state) {
194 mState = state;
195 }
196
Phil Burkbcc36742017-08-31 17:24:51 -0700197 /**
198 * Device specific startup.
199 * @return AAUDIO_OK or negative error.
200 */
201 virtual aaudio_result_t startDevice();
202
Phil Burkc0c70e32017-02-09 13:18:38 -0800203 aaudio_result_t writeUpMessageQueue(AAudioServiceMessage *command);
204
205 aaudio_result_t sendCurrentTimestamp();
206
Phil Burk940083c2017-07-17 17:00:02 -0700207 /**
208 * @param positionFrames
209 * @param timeNanos
210 * @return AAUDIO_OK or AAUDIO_ERROR_UNAVAILABLE or other negative error
211 */
Phil Burkc0c70e32017-02-09 13:18:38 -0800212 virtual aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) = 0;
213
Phil Burk97350f92017-07-21 15:59:44 -0700214 virtual aaudio_result_t getHardwareTimestamp(int64_t *positionFrames, int64_t *timeNanos) = 0;
215
Phil Burk523b3042017-09-13 13:03:08 -0700216 virtual aaudio_result_t getAudioDataDescription(AudioEndpointParcelable &parcelable) = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -0800217
Phil Burkec89b2e2017-06-20 15:05:06 -0700218 aaudio_stream_state_t mState = AAUDIO_STREAM_STATE_UNINITIALIZED;
Phil Burk2355edb2016-12-26 13:54:02 -0800219
Eric Laurentcb4dae22017-07-01 19:39:32 -0700220 pid_t mRegisteredClientThread = ILLEGAL_THREAD_ID;
Phil Burk2355edb2016-12-26 13:54:02 -0800221
Eric Laurentcb4dae22017-07-01 19:39:32 -0700222 SharedRingBuffer* mUpMessageQueue;
Phil Burk39f02dd2017-08-04 09:13:31 -0700223 std::mutex mUpMessageQueueLock;
Phil Burk2355edb2016-12-26 13:54:02 -0800224
Phil Burkbcc36742017-08-31 17:24:51 -0700225 AAudioThread mTimestampThread;
Phil Burkc0c70e32017-02-09 13:18:38 -0800226 // This is used by one thread to tell another thread to exit. So it must be atomic.
Phil Burk39f02dd2017-08-04 09:13:31 -0700227 std::atomic<bool> mThreadEnabled{false};
Phil Burkc0c70e32017-02-09 13:18:38 -0800228
Eric Laurentcb4dae22017-07-01 19:39:32 -0700229 int32_t mFramesPerBurst = 0;
Phil Burk39f02dd2017-08-04 09:13:31 -0700230 android::AudioClient mMmapClient; // set in open, used in MMAP start()
Eric Laurentcb4dae22017-07-01 19:39:32 -0700231 audio_port_handle_t mClientHandle = AUDIO_PORT_HANDLE_NONE;
232
Phil Burk97350f92017-07-21 15:59:44 -0700233 SimpleDoubleBuffer<Timestamp> mAtomicTimestamp;
234
Phil Burk39f02dd2017-08-04 09:13:31 -0700235 android::AAudioService &mAudioService;
236 android::sp<AAudioServiceEndpoint> mServiceEndpoint;
237
Phil Burk11e8d332017-05-24 09:59:02 -0700238private:
Eric Laurentcb4dae22017-07-01 19:39:32 -0700239 aaudio_handle_t mHandle = -1;
Phil Burk2355edb2016-12-26 13:54:02 -0800240};
241
Phil Burk5ed503c2017-02-01 09:38:15 -0800242} /* namespace aaudio */
Phil Burk2355edb2016-12-26 13:54:02 -0800243
Phil Burk5ed503c2017-02-01 09:38:15 -0800244#endif //AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H