blob: 2f94614b8f64bfe616113cfe90e49b7291134651 [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 Burk2355edb2016-12-26 13:54:02 -080035
Phil Burk5ed503c2017-02-01 09:38:15 -080036namespace aaudio {
Phil Burk2355edb2016-12-26 13:54:02 -080037
38// We expect the queue to only have a few commands.
39// This should be way more than we need.
40#define QUEUE_UP_CAPACITY_COMMANDS (128)
41
Phil Burkc0c70e32017-02-09 13:18:38 -080042/**
43 * Base class for a stream in the AAudio service.
44 */
45class AAudioServiceStreamBase
Phil Burk11e8d332017-05-24 09:59:02 -070046 : public virtual android::RefBase
47 , public Runnable {
Phil Burk2355edb2016-12-26 13:54:02 -080048
49public:
Phil Burk5ed503c2017-02-01 09:38:15 -080050 AAudioServiceStreamBase();
51 virtual ~AAudioServiceStreamBase();
Phil Burk2355edb2016-12-26 13:54:02 -080052
53 enum {
54 ILLEGAL_THREAD_ID = 0
55 };
56
Phil Burka5222e22017-07-28 13:31:14 -070057 static std::string dumpHeader();
58
59 // does not include EOL
60 virtual std::string dump() const;
Phil Burk4501b352017-06-29 18:12:36 -070061
Phil Burkc0c70e32017-02-09 13:18:38 -080062 // -------------------------------------------------------------------
Phil Burk2355edb2016-12-26 13:54:02 -080063 /**
64 * Open the device.
65 */
Phil Burkc0c70e32017-02-09 13:18:38 -080066 virtual aaudio_result_t open(const aaudio::AAudioStreamRequest &request,
67 aaudio::AAudioStreamConfiguration &configurationOutput) = 0;
68
69 virtual aaudio_result_t close();
Phil Burk2355edb2016-12-26 13:54:02 -080070
71 /**
72 * Start the flow of data.
73 */
Phil Burk71f35bb2017-04-13 16:05:07 -070074 virtual aaudio_result_t start();
Phil Burk2355edb2016-12-26 13:54:02 -080075
76 /**
77 * Stop the flow of data such that start() can resume with loss of data.
78 */
Phil Burk71f35bb2017-04-13 16:05:07 -070079 virtual aaudio_result_t pause();
80
81 /**
82 * Stop the flow of data after data in buffer has played.
83 */
84 virtual aaudio_result_t stop();
Phil Burk2355edb2016-12-26 13:54:02 -080085
Phil Burk98d6d922017-07-06 11:52:45 -070086 aaudio_result_t stopTimestampThread();
87
Phil Burk2355edb2016-12-26 13:54:02 -080088 /**
89 * Discard any data held by the underlying HAL or Service.
90 */
Phil Burk71f35bb2017-04-13 16:05:07 -070091 virtual aaudio_result_t flush();
Phil Burk2355edb2016-12-26 13:54:02 -080092
Eric Laurentcb4dae22017-07-01 19:39:32 -070093 virtual aaudio_result_t startClient(const android::AudioClient& client __unused,
94 audio_port_handle_t *clientHandle __unused) {
95 return AAUDIO_ERROR_UNAVAILABLE;
96 }
97
98 virtual aaudio_result_t stopClient(audio_port_handle_t clientHandle __unused) {
99 return AAUDIO_ERROR_UNAVAILABLE;
100 }
101
Phil Burk11e8d332017-05-24 09:59:02 -0700102 bool isRunning() const {
103 return mState == AAUDIO_STREAM_STATE_STARTED;
104 }
Eric Laurentcb4dae22017-07-01 19:39:32 -0700105
Phil Burkc0c70e32017-02-09 13:18:38 -0800106 // -------------------------------------------------------------------
Phil Burk2355edb2016-12-26 13:54:02 -0800107
Phil Burkc0c70e32017-02-09 13:18:38 -0800108 /**
109 * Send a message to the client.
110 */
111 aaudio_result_t sendServiceEvent(aaudio_service_event_t event,
112 double dataDouble = 0.0,
113 int64_t dataLong = 0);
Phil Burkdec33ab2017-01-17 14:48:16 -0800114
Phil Burkc0c70e32017-02-09 13:18:38 -0800115 /**
116 * Fill in a parcelable description of stream.
117 */
118 aaudio_result_t getDescription(AudioEndpointParcelable &parcelable);
Phil Burk2355edb2016-12-26 13:54:02 -0800119
Phil Burk2355edb2016-12-26 13:54:02 -0800120
Phil Burkc0c70e32017-02-09 13:18:38 -0800121 void setRegisteredThread(pid_t pid) {
Phil Burk2355edb2016-12-26 13:54:02 -0800122 mRegisteredClientThread = pid;
123 }
Phil Burkdec33ab2017-01-17 14:48:16 -0800124
Phil Burkc0c70e32017-02-09 13:18:38 -0800125 pid_t getRegisteredThread() const {
Phil Burk2355edb2016-12-26 13:54:02 -0800126 return mRegisteredClientThread;
127 }
128
Phil Burkc0c70e32017-02-09 13:18:38 -0800129 int32_t getFramesPerBurst() const {
130 return mFramesPerBurst;
131 }
132
133 int32_t calculateBytesPerFrame() const {
134 return mSamplesPerFrame * AAudioConvert_formatToSizeInBytes(mAudioFormat);
135 }
136
137 void run() override; // to implement Runnable
138
Phil Burk5ef003b2017-06-30 11:43:37 -0700139 void disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800140
Phil Burk2ac035f2017-06-23 14:51:14 -0700141 uid_t getOwnerUserId() const {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700142 return mMmapClient.clientUid;
Phil Burk2ac035f2017-06-23 14:51:14 -0700143 }
144
Phil Burkb63320a2017-06-30 10:28:20 -0700145 pid_t getOwnerProcessId() const {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700146 return mMmapClient.clientPid;
Phil Burkb63320a2017-06-30 10:28:20 -0700147 }
148
Phil Burk11e8d332017-05-24 09:59:02 -0700149 aaudio_handle_t getHandle() const {
150 return mHandle;
151 }
152 void setHandle(aaudio_handle_t handle) {
153 mHandle = handle;
154 }
155
Phil Burk5a26e662017-07-07 12:44:48 -0700156 aaudio_stream_state_t getState() const {
157 return mState;
158 }
159
Phil Burk2355edb2016-12-26 13:54:02 -0800160protected:
Phil Burk98d6d922017-07-06 11:52:45 -0700161
Phil Burk5a26e662017-07-07 12:44:48 -0700162 void setState(aaudio_stream_state_t state) {
163 mState = state;
164 }
165
Phil Burkc0c70e32017-02-09 13:18:38 -0800166 aaudio_result_t writeUpMessageQueue(AAudioServiceMessage *command);
167
168 aaudio_result_t sendCurrentTimestamp();
169
Phil Burk940083c2017-07-17 17:00:02 -0700170 /**
171 * @param positionFrames
172 * @param timeNanos
173 * @return AAUDIO_OK or AAUDIO_ERROR_UNAVAILABLE or other negative error
174 */
Phil Burkc0c70e32017-02-09 13:18:38 -0800175 virtual aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) = 0;
176
Phil Burk97350f92017-07-21 15:59:44 -0700177 virtual aaudio_result_t getHardwareTimestamp(int64_t *positionFrames, int64_t *timeNanos) = 0;
178
Phil Burkc0c70e32017-02-09 13:18:38 -0800179 virtual aaudio_result_t getDownDataDescription(AudioEndpointParcelable &parcelable) = 0;
180
Phil Burkec89b2e2017-06-20 15:05:06 -0700181 aaudio_stream_state_t mState = AAUDIO_STREAM_STATE_UNINITIALIZED;
Phil Burk2355edb2016-12-26 13:54:02 -0800182
Eric Laurentcb4dae22017-07-01 19:39:32 -0700183 pid_t mRegisteredClientThread = ILLEGAL_THREAD_ID;
Phil Burk2355edb2016-12-26 13:54:02 -0800184
Eric Laurentcb4dae22017-07-01 19:39:32 -0700185 SharedRingBuffer* mUpMessageQueue;
186 std::mutex mLockUpMessageQueue;
Phil Burk2355edb2016-12-26 13:54:02 -0800187
Eric Laurentcb4dae22017-07-01 19:39:32 -0700188 AAudioThread mAAudioThread;
Phil Burkc0c70e32017-02-09 13:18:38 -0800189 // This is used by one thread to tell another thread to exit. So it must be atomic.
Eric Laurentcb4dae22017-07-01 19:39:32 -0700190 std::atomic<bool> mThreadEnabled;
Phil Burkc0c70e32017-02-09 13:18:38 -0800191
Eric Laurentcb4dae22017-07-01 19:39:32 -0700192 aaudio_format_t mAudioFormat = AAUDIO_FORMAT_UNSPECIFIED;
193 int32_t mFramesPerBurst = 0;
194 int32_t mSamplesPerFrame = AAUDIO_UNSPECIFIED;
195 int32_t mSampleRate = AAUDIO_UNSPECIFIED;
196 int32_t mCapacityInFrames = AAUDIO_UNSPECIFIED;
197 android::AudioClient mMmapClient;
198 audio_port_handle_t mClientHandle = AUDIO_PORT_HANDLE_NONE;
199
Phil Burk97350f92017-07-21 15:59:44 -0700200 SimpleDoubleBuffer<Timestamp> mAtomicTimestamp;
201
Phil Burk11e8d332017-05-24 09:59:02 -0700202private:
Eric Laurentcb4dae22017-07-01 19:39:32 -0700203 aaudio_handle_t mHandle = -1;
Phil Burk2355edb2016-12-26 13:54:02 -0800204};
205
Phil Burk5ed503c2017-02-01 09:38:15 -0800206} /* namespace aaudio */
Phil Burk2355edb2016-12-26 13:54:02 -0800207
Phil Burk5ed503c2017-02-01 09:38:15 -0800208#endif //AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H