blob: e91ea8207eb5e75f8ff92df7ba127cfab572d8ba [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 Burk4501b352017-06-29 18:12:36 -070057 std::string dump() const;
58
Phil Burkc0c70e32017-02-09 13:18:38 -080059 // -------------------------------------------------------------------
Phil Burk2355edb2016-12-26 13:54:02 -080060 /**
61 * Open the device.
62 */
Phil Burkc0c70e32017-02-09 13:18:38 -080063 virtual aaudio_result_t open(const aaudio::AAudioStreamRequest &request,
64 aaudio::AAudioStreamConfiguration &configurationOutput) = 0;
65
66 virtual aaudio_result_t close();
Phil Burk2355edb2016-12-26 13:54:02 -080067
68 /**
69 * Start the flow of data.
70 */
Phil Burk71f35bb2017-04-13 16:05:07 -070071 virtual aaudio_result_t start();
Phil Burk2355edb2016-12-26 13:54:02 -080072
73 /**
74 * Stop the flow of data such that start() can resume with loss of data.
75 */
Phil Burk71f35bb2017-04-13 16:05:07 -070076 virtual aaudio_result_t pause();
77
78 /**
79 * Stop the flow of data after data in buffer has played.
80 */
81 virtual aaudio_result_t stop();
Phil Burk2355edb2016-12-26 13:54:02 -080082
Phil Burk98d6d922017-07-06 11:52:45 -070083 aaudio_result_t stopTimestampThread();
84
Phil Burk2355edb2016-12-26 13:54:02 -080085 /**
86 * Discard any data held by the underlying HAL or Service.
87 */
Phil Burk71f35bb2017-04-13 16:05:07 -070088 virtual aaudio_result_t flush();
Phil Burk2355edb2016-12-26 13:54:02 -080089
Eric Laurentcb4dae22017-07-01 19:39:32 -070090 virtual aaudio_result_t startClient(const android::AudioClient& client __unused,
91 audio_port_handle_t *clientHandle __unused) {
92 return AAUDIO_ERROR_UNAVAILABLE;
93 }
94
95 virtual aaudio_result_t stopClient(audio_port_handle_t clientHandle __unused) {
96 return AAUDIO_ERROR_UNAVAILABLE;
97 }
98
Phil Burk11e8d332017-05-24 09:59:02 -070099 bool isRunning() const {
100 return mState == AAUDIO_STREAM_STATE_STARTED;
101 }
Eric Laurentcb4dae22017-07-01 19:39:32 -0700102
Phil Burkc0c70e32017-02-09 13:18:38 -0800103 // -------------------------------------------------------------------
Phil Burk2355edb2016-12-26 13:54:02 -0800104
Phil Burkc0c70e32017-02-09 13:18:38 -0800105 /**
106 * Send a message to the client.
107 */
108 aaudio_result_t sendServiceEvent(aaudio_service_event_t event,
109 double dataDouble = 0.0,
110 int64_t dataLong = 0);
Phil Burkdec33ab2017-01-17 14:48:16 -0800111
Phil Burkc0c70e32017-02-09 13:18:38 -0800112 /**
113 * Fill in a parcelable description of stream.
114 */
115 aaudio_result_t getDescription(AudioEndpointParcelable &parcelable);
Phil Burk2355edb2016-12-26 13:54:02 -0800116
Phil Burk2355edb2016-12-26 13:54:02 -0800117
Phil Burkc0c70e32017-02-09 13:18:38 -0800118 void setRegisteredThread(pid_t pid) {
Phil Burk2355edb2016-12-26 13:54:02 -0800119 mRegisteredClientThread = pid;
120 }
Phil Burkdec33ab2017-01-17 14:48:16 -0800121
Phil Burkc0c70e32017-02-09 13:18:38 -0800122 pid_t getRegisteredThread() const {
Phil Burk2355edb2016-12-26 13:54:02 -0800123 return mRegisteredClientThread;
124 }
125
Phil Burkc0c70e32017-02-09 13:18:38 -0800126 int32_t getFramesPerBurst() const {
127 return mFramesPerBurst;
128 }
129
130 int32_t calculateBytesPerFrame() const {
131 return mSamplesPerFrame * AAudioConvert_formatToSizeInBytes(mAudioFormat);
132 }
133
134 void run() override; // to implement Runnable
135
Phil Burk5ef003b2017-06-30 11:43:37 -0700136 void disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800137
Phil Burk2ac035f2017-06-23 14:51:14 -0700138 uid_t getOwnerUserId() const {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700139 return mMmapClient.clientUid;
Phil Burk2ac035f2017-06-23 14:51:14 -0700140 }
141
Phil Burkb63320a2017-06-30 10:28:20 -0700142 pid_t getOwnerProcessId() const {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700143 return mMmapClient.clientPid;
Phil Burkb63320a2017-06-30 10:28:20 -0700144 }
145
Phil Burk11e8d332017-05-24 09:59:02 -0700146 aaudio_handle_t getHandle() const {
147 return mHandle;
148 }
149 void setHandle(aaudio_handle_t handle) {
150 mHandle = handle;
151 }
152
Phil Burk5a26e662017-07-07 12:44:48 -0700153 aaudio_stream_state_t getState() const {
154 return mState;
155 }
156
Phil Burk2355edb2016-12-26 13:54:02 -0800157protected:
Phil Burk98d6d922017-07-06 11:52:45 -0700158
Phil Burk5a26e662017-07-07 12:44:48 -0700159 void setState(aaudio_stream_state_t state) {
160 mState = state;
161 }
162
Phil Burkc0c70e32017-02-09 13:18:38 -0800163 aaudio_result_t writeUpMessageQueue(AAudioServiceMessage *command);
164
165 aaudio_result_t sendCurrentTimestamp();
166
Phil Burk940083c2017-07-17 17:00:02 -0700167 /**
168 * @param positionFrames
169 * @param timeNanos
170 * @return AAUDIO_OK or AAUDIO_ERROR_UNAVAILABLE or other negative error
171 */
Phil Burkc0c70e32017-02-09 13:18:38 -0800172 virtual aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) = 0;
173
Phil Burk97350f92017-07-21 15:59:44 -0700174 virtual aaudio_result_t getHardwareTimestamp(int64_t *positionFrames, int64_t *timeNanos) = 0;
175
Phil Burkc0c70e32017-02-09 13:18:38 -0800176 virtual aaudio_result_t getDownDataDescription(AudioEndpointParcelable &parcelable) = 0;
177
Phil Burkec89b2e2017-06-20 15:05:06 -0700178 aaudio_stream_state_t mState = AAUDIO_STREAM_STATE_UNINITIALIZED;
Phil Burk2355edb2016-12-26 13:54:02 -0800179
Eric Laurentcb4dae22017-07-01 19:39:32 -0700180 pid_t mRegisteredClientThread = ILLEGAL_THREAD_ID;
Phil Burk2355edb2016-12-26 13:54:02 -0800181
Eric Laurentcb4dae22017-07-01 19:39:32 -0700182 SharedRingBuffer* mUpMessageQueue;
183 std::mutex mLockUpMessageQueue;
Phil Burk2355edb2016-12-26 13:54:02 -0800184
Eric Laurentcb4dae22017-07-01 19:39:32 -0700185 AAudioThread mAAudioThread;
Phil Burkc0c70e32017-02-09 13:18:38 -0800186 // This is used by one thread to tell another thread to exit. So it must be atomic.
Eric Laurentcb4dae22017-07-01 19:39:32 -0700187 std::atomic<bool> mThreadEnabled;
Phil Burkc0c70e32017-02-09 13:18:38 -0800188
Eric Laurentcb4dae22017-07-01 19:39:32 -0700189 aaudio_format_t mAudioFormat = AAUDIO_FORMAT_UNSPECIFIED;
190 int32_t mFramesPerBurst = 0;
191 int32_t mSamplesPerFrame = AAUDIO_UNSPECIFIED;
192 int32_t mSampleRate = AAUDIO_UNSPECIFIED;
193 int32_t mCapacityInFrames = AAUDIO_UNSPECIFIED;
194 android::AudioClient mMmapClient;
195 audio_port_handle_t mClientHandle = AUDIO_PORT_HANDLE_NONE;
196
Phil Burk97350f92017-07-21 15:59:44 -0700197 SimpleDoubleBuffer<Timestamp> mAtomicTimestamp;
198
Phil Burk11e8d332017-05-24 09:59:02 -0700199private:
Eric Laurentcb4dae22017-07-01 19:39:32 -0700200 aaudio_handle_t mHandle = -1;
Phil Burk2355edb2016-12-26 13:54:02 -0800201};
202
Phil Burk5ed503c2017-02-01 09:38:15 -0800203} /* namespace aaudio */
Phil Burk2355edb2016-12-26 13:54:02 -0800204
Phil Burk5ed503c2017-02-01 09:38:15 -0800205#endif //AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H