2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H
18 #define AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H
23 #include <media/AudioClient.h>
24 #include <utils/RefBase.h>
26 #include "fifo/FifoBuffer.h"
27 #include "binding/IAAudioService.h"
28 #include "binding/AudioEndpointParcelable.h"
29 #include "binding/AAudioServiceMessage.h"
30 #include "utility/AAudioUtilities.h"
31 #include "utility/AudioClock.h"
33 #include "SharedRingBuffer.h"
34 #include "AAudioThread.h"
42 class AAudioServiceEndpoint;
44 // 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)
49 * Each instance of AAudioServiceStreamBase corresponds to a client stream.
50 * It uses a subclass of AAudioServiceEndpoint to communicate with the underlying device or port.
52 class AAudioServiceStreamBase
53 : public virtual android::RefBase
54 , public AAudioStreamParameters
58 AAudioServiceStreamBase(android::AAudioService &aAudioService);
60 virtual ~AAudioServiceStreamBase();
66 static std::string dumpHeader();
68 // does not include EOL
69 virtual std::string dump() const;
71 // -------------------------------------------------------------------
75 virtual aaudio_result_t open(const aaudio::AAudioStreamRequest &request) = 0;
77 virtual aaudio_result_t close();
80 * Start the flow of audio data.
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.
85 virtual aaudio_result_t start();
88 * Stop the flow of data so that start() can resume without loss of data.
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.
93 virtual aaudio_result_t pause();
96 * Stop the flow of data after the currently queued data has finished playing.
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.
102 virtual aaudio_result_t stop();
104 aaudio_result_t stopTimestampThread();
107 * Discard any data held by the underlying HAL or Service.
109 * An AAUDIO_SERVICE_EVENT_FLUSHED will be sent to the client when complete.
111 virtual aaudio_result_t flush();
114 virtual aaudio_result_t startClient(const android::AudioClient& client __unused,
115 audio_port_handle_t *clientHandle __unused) {
116 ALOGD("AAudioServiceStreamBase::startClient(%p, ...) AAUDIO_ERROR_UNAVAILABLE", &client);
117 return AAUDIO_ERROR_UNAVAILABLE;
120 virtual aaudio_result_t stopClient(audio_port_handle_t clientHandle __unused) {
121 ALOGD("AAudioServiceStreamBase::stopClient(%d) AAUDIO_ERROR_UNAVAILABLE", clientHandle);
122 return AAUDIO_ERROR_UNAVAILABLE;
125 bool isRunning() const {
126 return mState == AAUDIO_STREAM_STATE_STARTED;
129 // -------------------------------------------------------------------
132 * Send a message to the client.
134 aaudio_result_t sendServiceEvent(aaudio_service_event_t event,
135 double dataDouble = 0.0,
136 int64_t dataLong = 0);
139 * Fill in a parcelable description of stream.
141 aaudio_result_t getDescription(AudioEndpointParcelable &parcelable);
144 void setRegisteredThread(pid_t pid) {
145 mRegisteredClientThread = pid;
148 pid_t getRegisteredThread() const {
149 return mRegisteredClientThread;
152 int32_t getFramesPerBurst() const {
153 return mFramesPerBurst;
156 void run() override; // to implement Runnable
160 const android::AudioClient &getAudioClient() {
164 uid_t getOwnerUserId() const {
165 return mMmapClient.clientUid;
168 pid_t getOwnerProcessId() const {
169 return mMmapClient.clientPid;
172 aaudio_handle_t getHandle() const {
175 void setHandle(aaudio_handle_t handle) {
179 aaudio_stream_state_t getState() const {
183 void onVolumeChanged(float volume);
190 aaudio_result_t open(const aaudio::AAudioStreamRequest &request,
191 aaudio_sharing_mode_t sharingMode);
193 void setState(aaudio_stream_state_t state) {
198 * Device specific startup.
199 * @return AAUDIO_OK or negative error.
201 virtual aaudio_result_t startDevice();
203 aaudio_result_t writeUpMessageQueue(AAudioServiceMessage *command);
205 aaudio_result_t sendCurrentTimestamp();
208 * @param positionFrames
210 * @return AAUDIO_OK or AAUDIO_ERROR_UNAVAILABLE or other negative error
212 virtual aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) = 0;
214 virtual aaudio_result_t getHardwareTimestamp(int64_t *positionFrames, int64_t *timeNanos) = 0;
216 virtual aaudio_result_t getAudioDataDescription(AudioEndpointParcelable &parcelable) = 0;
218 aaudio_stream_state_t mState = AAUDIO_STREAM_STATE_UNINITIALIZED;
220 pid_t mRegisteredClientThread = ILLEGAL_THREAD_ID;
222 SharedRingBuffer* mUpMessageQueue;
223 std::mutex mUpMessageQueueLock;
225 AAudioThread mTimestampThread;
226 // This is used by one thread to tell another thread to exit. So it must be atomic.
227 std::atomic<bool> mThreadEnabled{false};
229 int32_t mFramesPerBurst = 0;
230 android::AudioClient mMmapClient; // set in open, used in MMAP start()
231 audio_port_handle_t mClientHandle = AUDIO_PORT_HANDLE_NONE;
233 SimpleDoubleBuffer<Timestamp> mAtomicTimestamp;
235 android::AAudioService &mAudioService;
237 // The mServiceEndpoint variable can be accessed by multiple threads.
238 // So we access it by locally promoting a weak pointer to a smart pointer,
239 // which is thread-safe.
240 android::sp<AAudioServiceEndpoint> mServiceEndpoint;
241 android::wp<AAudioServiceEndpoint> mServiceEndpointWeak;
244 aaudio_handle_t mHandle = -1;
247 } /* namespace aaudio */
249 #endif //AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H