blob: c7df6f340e8ebe7bd872c6998757e5353a7a8e37 [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 Burk11e8d332017-05-24 09:59:02 -070023#include <utils/RefBase.h>
24
Phil Burk2355edb2016-12-26 13:54:02 -080025#include "fifo/FifoBuffer.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080026#include "binding/IAAudioService.h"
27#include "binding/AudioEndpointParcelable.h"
28#include "binding/AAudioServiceMessage.h"
29#include "utility/AAudioUtilities.h"
30
Phil Burk2355edb2016-12-26 13:54:02 -080031#include "SharedRingBuffer.h"
Phil Burk5ed503c2017-02-01 09:38:15 -080032#include "AAudioThread.h"
Phil Burk2355edb2016-12-26 13:54:02 -080033
Phil Burk5ed503c2017-02-01 09:38:15 -080034namespace aaudio {
Phil Burk2355edb2016-12-26 13:54:02 -080035
36// We expect the queue to only have a few commands.
37// This should be way more than we need.
38#define QUEUE_UP_CAPACITY_COMMANDS (128)
39
Phil Burkc0c70e32017-02-09 13:18:38 -080040/**
41 * Base class for a stream in the AAudio service.
42 */
43class AAudioServiceStreamBase
Phil Burk11e8d332017-05-24 09:59:02 -070044 : public virtual android::RefBase
45 , public Runnable {
Phil Burk2355edb2016-12-26 13:54:02 -080046
47public:
Phil Burk5ed503c2017-02-01 09:38:15 -080048 AAudioServiceStreamBase();
49 virtual ~AAudioServiceStreamBase();
Phil Burk2355edb2016-12-26 13:54:02 -080050
51 enum {
52 ILLEGAL_THREAD_ID = 0
53 };
54
Phil Burk4501b352017-06-29 18:12:36 -070055 std::string dump() const;
56
Phil Burkc0c70e32017-02-09 13:18:38 -080057 // -------------------------------------------------------------------
Phil Burk2355edb2016-12-26 13:54:02 -080058 /**
59 * Open the device.
60 */
Phil Burkc0c70e32017-02-09 13:18:38 -080061 virtual aaudio_result_t open(const aaudio::AAudioStreamRequest &request,
62 aaudio::AAudioStreamConfiguration &configurationOutput) = 0;
63
64 virtual aaudio_result_t close();
Phil Burk2355edb2016-12-26 13:54:02 -080065
66 /**
67 * Start the flow of data.
68 */
Phil Burk71f35bb2017-04-13 16:05:07 -070069 virtual aaudio_result_t start();
Phil Burk2355edb2016-12-26 13:54:02 -080070
71 /**
72 * Stop the flow of data such that start() can resume with loss of data.
73 */
Phil Burk71f35bb2017-04-13 16:05:07 -070074 virtual aaudio_result_t pause();
75
76 /**
77 * Stop the flow of data after data in buffer has played.
78 */
79 virtual aaudio_result_t stop();
Phil Burk2355edb2016-12-26 13:54:02 -080080
Phil Burk98d6d922017-07-06 11:52:45 -070081 aaudio_result_t stopTimestampThread();
82
Phil Burk2355edb2016-12-26 13:54:02 -080083 /**
84 * Discard any data held by the underlying HAL or Service.
85 */
Phil Burk71f35bb2017-04-13 16:05:07 -070086 virtual aaudio_result_t flush();
Phil Burk2355edb2016-12-26 13:54:02 -080087
Phil Burk11e8d332017-05-24 09:59:02 -070088 bool isRunning() const {
89 return mState == AAUDIO_STREAM_STATE_STARTED;
90 }
Phil Burkc0c70e32017-02-09 13:18:38 -080091 // -------------------------------------------------------------------
Phil Burk2355edb2016-12-26 13:54:02 -080092
Phil Burkc0c70e32017-02-09 13:18:38 -080093 /**
94 * Send a message to the client.
95 */
96 aaudio_result_t sendServiceEvent(aaudio_service_event_t event,
97 double dataDouble = 0.0,
98 int64_t dataLong = 0);
Phil Burkdec33ab2017-01-17 14:48:16 -080099
Phil Burkc0c70e32017-02-09 13:18:38 -0800100 /**
101 * Fill in a parcelable description of stream.
102 */
103 aaudio_result_t getDescription(AudioEndpointParcelable &parcelable);
Phil Burk2355edb2016-12-26 13:54:02 -0800104
Phil Burk2355edb2016-12-26 13:54:02 -0800105
Phil Burkc0c70e32017-02-09 13:18:38 -0800106 void setRegisteredThread(pid_t pid) {
Phil Burk2355edb2016-12-26 13:54:02 -0800107 mRegisteredClientThread = pid;
108 }
Phil Burkdec33ab2017-01-17 14:48:16 -0800109
Phil Burkc0c70e32017-02-09 13:18:38 -0800110 pid_t getRegisteredThread() const {
Phil Burk2355edb2016-12-26 13:54:02 -0800111 return mRegisteredClientThread;
112 }
113
Phil Burkc0c70e32017-02-09 13:18:38 -0800114 int32_t getFramesPerBurst() const {
115 return mFramesPerBurst;
116 }
117
118 int32_t calculateBytesPerFrame() const {
119 return mSamplesPerFrame * AAudioConvert_formatToSizeInBytes(mAudioFormat);
120 }
121
122 void run() override; // to implement Runnable
123
Phil Burk5ef003b2017-06-30 11:43:37 -0700124 void disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800125
Phil Burk2ac035f2017-06-23 14:51:14 -0700126 uid_t getOwnerUserId() const {
127 return mOwnerUserId;
128 }
129 void setOwnerUserId(uid_t uid) {
130 mOwnerUserId = uid;
131 }
132
Phil Burkb63320a2017-06-30 10:28:20 -0700133 pid_t getOwnerProcessId() const {
134 return mOwnerProcessId;
135 }
136 void setOwnerProcessId(pid_t pid) {
137 mOwnerProcessId = pid;
138 }
139
Phil Burk11e8d332017-05-24 09:59:02 -0700140 aaudio_handle_t getHandle() const {
141 return mHandle;
142 }
143 void setHandle(aaudio_handle_t handle) {
144 mHandle = handle;
145 }
146
Phil Burk5a26e662017-07-07 12:44:48 -0700147 aaudio_stream_state_t getState() const {
148 return mState;
149 }
150
Phil Burk2355edb2016-12-26 13:54:02 -0800151protected:
Phil Burk98d6d922017-07-06 11:52:45 -0700152
Phil Burk5a26e662017-07-07 12:44:48 -0700153 void setState(aaudio_stream_state_t state) {
154 mState = state;
155 }
156
Phil Burkc0c70e32017-02-09 13:18:38 -0800157 aaudio_result_t writeUpMessageQueue(AAudioServiceMessage *command);
158
159 aaudio_result_t sendCurrentTimestamp();
160
161 virtual aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) = 0;
162
163 virtual aaudio_result_t getDownDataDescription(AudioEndpointParcelable &parcelable) = 0;
164
Phil Burkec89b2e2017-06-20 15:05:06 -0700165 aaudio_stream_state_t mState = AAUDIO_STREAM_STATE_UNINITIALIZED;
Phil Burk2355edb2016-12-26 13:54:02 -0800166
Phil Burk3316d5e2017-02-15 11:23:01 -0800167 pid_t mRegisteredClientThread = ILLEGAL_THREAD_ID;
Phil Burk2355edb2016-12-26 13:54:02 -0800168
Phil Burk3316d5e2017-02-15 11:23:01 -0800169 SharedRingBuffer* mUpMessageQueue;
Phil Burkc0c70e32017-02-09 13:18:38 -0800170 std::mutex mLockUpMessageQueue;
Phil Burk2355edb2016-12-26 13:54:02 -0800171
Phil Burkec89b2e2017-06-20 15:05:06 -0700172 AAudioThread mAAudioThread;
Phil Burkc0c70e32017-02-09 13:18:38 -0800173 // This is used by one thread to tell another thread to exit. So it must be atomic.
Phil Burkec89b2e2017-06-20 15:05:06 -0700174 std::atomic<bool> mThreadEnabled;
Phil Burkc0c70e32017-02-09 13:18:38 -0800175
Phil Burk9dca9822017-05-26 14:27:43 -0700176 aaudio_format_t mAudioFormat = AAUDIO_FORMAT_UNSPECIFIED;
Phil Burk3316d5e2017-02-15 11:23:01 -0800177 int32_t mFramesPerBurst = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -0800178 int32_t mSamplesPerFrame = AAUDIO_UNSPECIFIED;
179 int32_t mSampleRate = AAUDIO_UNSPECIFIED;
180 int32_t mCapacityInFrames = AAUDIO_UNSPECIFIED;
Phil Burk2ac035f2017-06-23 14:51:14 -0700181 uid_t mOwnerUserId = -1;
Phil Burkb63320a2017-06-30 10:28:20 -0700182 pid_t mOwnerProcessId = -1;
Phil Burk11e8d332017-05-24 09:59:02 -0700183private:
184 aaudio_handle_t mHandle = -1;
Phil Burk2355edb2016-12-26 13:54:02 -0800185};
186
Phil Burk5ed503c2017-02-01 09:38:15 -0800187} /* namespace aaudio */
Phil Burk2355edb2016-12-26 13:54:02 -0800188
Phil Burk5ed503c2017-02-01 09:38:15 -0800189#endif //AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H