blob: 301795d29ab5c094e56f7caf5416f2531e6950a2 [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 Burk39f02dd2017-08-04 09:13:31 -070035#include "AAudioService.h"
Phil Burk2355edb2016-12-26 13:54:02 -080036
Phil Burk5ed503c2017-02-01 09:38:15 -080037namespace aaudio {
Phil Burk2355edb2016-12-26 13:54:02 -080038
Phil Burk39f02dd2017-08-04 09:13:31 -070039class AAudioServiceEndpoint;
40
Phil Burk2355edb2016-12-26 13:54:02 -080041// We expect the queue to only have a few commands.
42// This should be way more than we need.
43#define QUEUE_UP_CAPACITY_COMMANDS (128)
44
Phil Burkc0c70e32017-02-09 13:18:38 -080045/**
Phil Burk39f02dd2017-08-04 09:13:31 -070046 * Each instance of AAudioServiceStreamBase corresponds to a client stream.
47 * It uses a subclass of AAudioServiceEndpoint to communicate with the underlying device or port.
Phil Burkc0c70e32017-02-09 13:18:38 -080048 */
49class AAudioServiceStreamBase
Phil Burk11e8d332017-05-24 09:59:02 -070050 : public virtual android::RefBase
Phil Burk39f02dd2017-08-04 09:13:31 -070051 , public AAudioStreamParameters
Phil Burk11e8d332017-05-24 09:59:02 -070052 , public Runnable {
Phil Burk2355edb2016-12-26 13:54:02 -080053
54public:
Phil Burk39f02dd2017-08-04 09:13:31 -070055 AAudioServiceStreamBase(android::AAudioService &aAudioService);
56
Phil Burk5ed503c2017-02-01 09:38:15 -080057 virtual ~AAudioServiceStreamBase();
Phil Burk2355edb2016-12-26 13:54:02 -080058
59 enum {
60 ILLEGAL_THREAD_ID = 0
61 };
62
Phil Burka5222e22017-07-28 13:31:14 -070063 static std::string dumpHeader();
64
65 // does not include EOL
66 virtual std::string dump() const;
Phil Burk4501b352017-06-29 18:12:36 -070067
Phil Burkc0c70e32017-02-09 13:18:38 -080068 // -------------------------------------------------------------------
Phil Burk2355edb2016-12-26 13:54:02 -080069 /**
70 * Open the device.
71 */
Phil Burk39f02dd2017-08-04 09:13:31 -070072 virtual aaudio_result_t open(const aaudio::AAudioStreamRequest &request) = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -080073
74 virtual aaudio_result_t close();
Phil Burk2355edb2016-12-26 13:54:02 -080075
76 /**
Phil Burk39f02dd2017-08-04 09:13:31 -070077 * Start the flow of audio data.
78 *
79 * This is not guaranteed to be synchronous but it currently is.
80 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
Phil Burk2355edb2016-12-26 13:54:02 -080081 */
Phil Burk71f35bb2017-04-13 16:05:07 -070082 virtual aaudio_result_t start();
Phil Burk2355edb2016-12-26 13:54:02 -080083
84 /**
Phil Burk39f02dd2017-08-04 09:13:31 -070085 * Stop the flow of data so that start() can resume without loss of data.
86 *
87 * This is not guaranteed to be synchronous but it currently is.
88 * An AAUDIO_SERVICE_EVENT_PAUSED will be sent to the client when complete.
89 */
Phil Burk71f35bb2017-04-13 16:05:07 -070090 virtual aaudio_result_t pause();
91
92 /**
Phil Burk39f02dd2017-08-04 09:13:31 -070093 * Stop the flow of data after the currently queued data has finished playing.
94 *
95 * This is not guaranteed to be synchronous but it currently is.
96 * An AAUDIO_SERVICE_EVENT_STOPPED will be sent to the client when complete.
97 *
Phil Burk71f35bb2017-04-13 16:05:07 -070098 */
99 virtual aaudio_result_t stop();
Phil Burk2355edb2016-12-26 13:54:02 -0800100
Phil Burk98d6d922017-07-06 11:52:45 -0700101 aaudio_result_t stopTimestampThread();
102
Phil Burk2355edb2016-12-26 13:54:02 -0800103 /**
Phil Burk39f02dd2017-08-04 09:13:31 -0700104 * Discard any data held by the underlying HAL or Service.
105 *
106 * An AAUDIO_SERVICE_EVENT_FLUSHED will be sent to the client when complete.
Phil Burk2355edb2016-12-26 13:54:02 -0800107 */
Phil Burk71f35bb2017-04-13 16:05:07 -0700108 virtual aaudio_result_t flush();
Phil Burk2355edb2016-12-26 13:54:02 -0800109
Phil Burk39f02dd2017-08-04 09:13:31 -0700110
Eric Laurentcb4dae22017-07-01 19:39:32 -0700111 virtual aaudio_result_t startClient(const android::AudioClient& client __unused,
112 audio_port_handle_t *clientHandle __unused) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700113 ALOGD("AAudioServiceStreamBase::startClient(%p, ...) AAUDIO_ERROR_UNAVAILABLE", &client);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700114 return AAUDIO_ERROR_UNAVAILABLE;
115 }
116
117 virtual aaudio_result_t stopClient(audio_port_handle_t clientHandle __unused) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700118 ALOGD("AAudioServiceStreamBase::stopClient(%d) AAUDIO_ERROR_UNAVAILABLE", clientHandle);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700119 return AAUDIO_ERROR_UNAVAILABLE;
120 }
121
Phil Burk11e8d332017-05-24 09:59:02 -0700122 bool isRunning() const {
123 return mState == AAUDIO_STREAM_STATE_STARTED;
124 }
Eric Laurentcb4dae22017-07-01 19:39:32 -0700125
Phil Burkc0c70e32017-02-09 13:18:38 -0800126 // -------------------------------------------------------------------
Phil Burk2355edb2016-12-26 13:54:02 -0800127
Phil Burkc0c70e32017-02-09 13:18:38 -0800128 /**
129 * Send a message to the client.
130 */
131 aaudio_result_t sendServiceEvent(aaudio_service_event_t event,
132 double dataDouble = 0.0,
133 int64_t dataLong = 0);
Phil Burkdec33ab2017-01-17 14:48:16 -0800134
Phil Burkc0c70e32017-02-09 13:18:38 -0800135 /**
136 * Fill in a parcelable description of stream.
137 */
138 aaudio_result_t getDescription(AudioEndpointParcelable &parcelable);
Phil Burk2355edb2016-12-26 13:54:02 -0800139
Phil Burk2355edb2016-12-26 13:54:02 -0800140
Phil Burkc0c70e32017-02-09 13:18:38 -0800141 void setRegisteredThread(pid_t pid) {
Phil Burk2355edb2016-12-26 13:54:02 -0800142 mRegisteredClientThread = pid;
143 }
Phil Burkdec33ab2017-01-17 14:48:16 -0800144
Phil Burkc0c70e32017-02-09 13:18:38 -0800145 pid_t getRegisteredThread() const {
Phil Burk2355edb2016-12-26 13:54:02 -0800146 return mRegisteredClientThread;
147 }
148
Phil Burkc0c70e32017-02-09 13:18:38 -0800149 int32_t getFramesPerBurst() const {
150 return mFramesPerBurst;
151 }
152
Phil Burkc0c70e32017-02-09 13:18:38 -0800153 void run() override; // to implement Runnable
154
Phil Burk5ef003b2017-06-30 11:43:37 -0700155 void disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800156
Phil Burk39f02dd2017-08-04 09:13:31 -0700157 const android::AudioClient &getAudioClient() {
158 return mMmapClient;
159 }
160
Phil Burk2ac035f2017-06-23 14:51:14 -0700161 uid_t getOwnerUserId() const {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700162 return mMmapClient.clientUid;
Phil Burk2ac035f2017-06-23 14:51:14 -0700163 }
164
Phil Burkb63320a2017-06-30 10:28:20 -0700165 pid_t getOwnerProcessId() const {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700166 return mMmapClient.clientPid;
Phil Burkb63320a2017-06-30 10:28:20 -0700167 }
168
Phil Burk11e8d332017-05-24 09:59:02 -0700169 aaudio_handle_t getHandle() const {
170 return mHandle;
171 }
172 void setHandle(aaudio_handle_t handle) {
173 mHandle = handle;
174 }
175
Phil Burk5a26e662017-07-07 12:44:48 -0700176 aaudio_stream_state_t getState() const {
177 return mState;
178 }
179
Phil Burk39f02dd2017-08-04 09:13:31 -0700180 void onVolumeChanged(float volume);
181
Phil Burk2355edb2016-12-26 13:54:02 -0800182protected:
Phil Burk98d6d922017-07-06 11:52:45 -0700183
Phil Burk39f02dd2017-08-04 09:13:31 -0700184 /**
185 * Open the device.
186 */
187 aaudio_result_t open(const aaudio::AAudioStreamRequest &request,
188 aaudio_sharing_mode_t sharingMode);
189
Phil Burk5a26e662017-07-07 12:44:48 -0700190 void setState(aaudio_stream_state_t state) {
191 mState = state;
192 }
193
Phil Burkc0c70e32017-02-09 13:18:38 -0800194 aaudio_result_t writeUpMessageQueue(AAudioServiceMessage *command);
195
196 aaudio_result_t sendCurrentTimestamp();
197
Phil Burk940083c2017-07-17 17:00:02 -0700198 /**
199 * @param positionFrames
200 * @param timeNanos
201 * @return AAUDIO_OK or AAUDIO_ERROR_UNAVAILABLE or other negative error
202 */
Phil Burkc0c70e32017-02-09 13:18:38 -0800203 virtual aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) = 0;
204
Phil Burk97350f92017-07-21 15:59:44 -0700205 virtual aaudio_result_t getHardwareTimestamp(int64_t *positionFrames, int64_t *timeNanos) = 0;
206
Phil Burkc0c70e32017-02-09 13:18:38 -0800207 virtual aaudio_result_t getDownDataDescription(AudioEndpointParcelable &parcelable) = 0;
208
Phil Burkec89b2e2017-06-20 15:05:06 -0700209 aaudio_stream_state_t mState = AAUDIO_STREAM_STATE_UNINITIALIZED;
Phil Burk2355edb2016-12-26 13:54:02 -0800210
Eric Laurentcb4dae22017-07-01 19:39:32 -0700211 pid_t mRegisteredClientThread = ILLEGAL_THREAD_ID;
Phil Burk2355edb2016-12-26 13:54:02 -0800212
Eric Laurentcb4dae22017-07-01 19:39:32 -0700213 SharedRingBuffer* mUpMessageQueue;
Phil Burk39f02dd2017-08-04 09:13:31 -0700214 std::mutex mUpMessageQueueLock;
Phil Burk2355edb2016-12-26 13:54:02 -0800215
Eric Laurentcb4dae22017-07-01 19:39:32 -0700216 AAudioThread mAAudioThread;
Phil Burkc0c70e32017-02-09 13:18:38 -0800217 // This is used by one thread to tell another thread to exit. So it must be atomic.
Phil Burk39f02dd2017-08-04 09:13:31 -0700218 std::atomic<bool> mThreadEnabled{false};
Phil Burkc0c70e32017-02-09 13:18:38 -0800219
Eric Laurentcb4dae22017-07-01 19:39:32 -0700220 int32_t mFramesPerBurst = 0;
Phil Burk39f02dd2017-08-04 09:13:31 -0700221 android::AudioClient mMmapClient; // set in open, used in MMAP start()
Eric Laurentcb4dae22017-07-01 19:39:32 -0700222 audio_port_handle_t mClientHandle = AUDIO_PORT_HANDLE_NONE;
223
Phil Burk97350f92017-07-21 15:59:44 -0700224 SimpleDoubleBuffer<Timestamp> mAtomicTimestamp;
225
Phil Burk39f02dd2017-08-04 09:13:31 -0700226 android::AAudioService &mAudioService;
227 android::sp<AAudioServiceEndpoint> mServiceEndpoint;
228
Phil Burk11e8d332017-05-24 09:59:02 -0700229private:
Eric Laurentcb4dae22017-07-01 19:39:32 -0700230 aaudio_handle_t mHandle = -1;
Phil Burk2355edb2016-12-26 13:54:02 -0800231};
232
Phil Burk5ed503c2017-02-01 09:38:15 -0800233} /* namespace aaudio */
Phil Burk2355edb2016-12-26 13:54:02 -0800234
Phil Burk5ed503c2017-02-01 09:38:15 -0800235#endif //AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H