blob: d83d5ab6d2f0d2453fa51b58fa682e8f2fe73dc9 [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 Burkc0c70e32017-02-09 13:18:38 -080055 // -------------------------------------------------------------------
Phil Burk2355edb2016-12-26 13:54:02 -080056 /**
57 * Open the device.
58 */
Phil Burkc0c70e32017-02-09 13:18:38 -080059 virtual aaudio_result_t open(const aaudio::AAudioStreamRequest &request,
60 aaudio::AAudioStreamConfiguration &configurationOutput) = 0;
61
62 virtual aaudio_result_t close();
Phil Burk2355edb2016-12-26 13:54:02 -080063
64 /**
65 * Start the flow of data.
66 */
Phil Burk71f35bb2017-04-13 16:05:07 -070067 virtual aaudio_result_t start();
Phil Burk2355edb2016-12-26 13:54:02 -080068
69 /**
70 * Stop the flow of data such that start() can resume with loss of data.
71 */
Phil Burk71f35bb2017-04-13 16:05:07 -070072 virtual aaudio_result_t pause();
73
74 /**
75 * Stop the flow of data after data in buffer has played.
76 */
77 virtual aaudio_result_t stop();
Phil Burk2355edb2016-12-26 13:54:02 -080078
79 /**
80 * Discard any data held by the underlying HAL or Service.
81 */
Phil Burk71f35bb2017-04-13 16:05:07 -070082 virtual aaudio_result_t flush();
Phil Burk2355edb2016-12-26 13:54:02 -080083
Phil Burk11e8d332017-05-24 09:59:02 -070084 bool isRunning() const {
85 return mState == AAUDIO_STREAM_STATE_STARTED;
86 }
Phil Burkc0c70e32017-02-09 13:18:38 -080087 // -------------------------------------------------------------------
Phil Burk2355edb2016-12-26 13:54:02 -080088
Phil Burkc0c70e32017-02-09 13:18:38 -080089 /**
90 * Send a message to the client.
91 */
92 aaudio_result_t sendServiceEvent(aaudio_service_event_t event,
93 double dataDouble = 0.0,
94 int64_t dataLong = 0);
Phil Burkdec33ab2017-01-17 14:48:16 -080095
Phil Burkc0c70e32017-02-09 13:18:38 -080096 /**
97 * Fill in a parcelable description of stream.
98 */
99 aaudio_result_t getDescription(AudioEndpointParcelable &parcelable);
Phil Burk2355edb2016-12-26 13:54:02 -0800100
Phil Burk2355edb2016-12-26 13:54:02 -0800101
Phil Burkc0c70e32017-02-09 13:18:38 -0800102 void setRegisteredThread(pid_t pid) {
Phil Burk2355edb2016-12-26 13:54:02 -0800103 mRegisteredClientThread = pid;
104 }
Phil Burkdec33ab2017-01-17 14:48:16 -0800105
Phil Burkc0c70e32017-02-09 13:18:38 -0800106 pid_t getRegisteredThread() const {
Phil Burk2355edb2016-12-26 13:54:02 -0800107 return mRegisteredClientThread;
108 }
109
Phil Burkc0c70e32017-02-09 13:18:38 -0800110 int32_t getFramesPerBurst() const {
111 return mFramesPerBurst;
112 }
113
114 int32_t calculateBytesPerFrame() const {
115 return mSamplesPerFrame * AAudioConvert_formatToSizeInBytes(mAudioFormat);
116 }
117
118 void run() override; // to implement Runnable
119
Phil Burkec89b2e2017-06-20 15:05:06 -0700120 void processFatalError();
Phil Burkc0c70e32017-02-09 13:18:38 -0800121
Phil Burk2ac035f2017-06-23 14:51:14 -0700122 uid_t getOwnerUserId() const {
123 return mOwnerUserId;
124 }
125 void setOwnerUserId(uid_t uid) {
126 mOwnerUserId = uid;
127 }
128
Phil Burk11e8d332017-05-24 09:59:02 -0700129 aaudio_handle_t getHandle() const {
130 return mHandle;
131 }
132 void setHandle(aaudio_handle_t handle) {
133 mHandle = handle;
134 }
135
Phil Burk2355edb2016-12-26 13:54:02 -0800136protected:
Phil Burkc0c70e32017-02-09 13:18:38 -0800137 aaudio_result_t writeUpMessageQueue(AAudioServiceMessage *command);
138
139 aaudio_result_t sendCurrentTimestamp();
140
141 virtual aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) = 0;
142
143 virtual aaudio_result_t getDownDataDescription(AudioEndpointParcelable &parcelable) = 0;
144
Phil Burkec89b2e2017-06-20 15:05:06 -0700145 aaudio_stream_state_t mState = AAUDIO_STREAM_STATE_UNINITIALIZED;
Phil Burk2355edb2016-12-26 13:54:02 -0800146
Phil Burk3316d5e2017-02-15 11:23:01 -0800147 pid_t mRegisteredClientThread = ILLEGAL_THREAD_ID;
Phil Burk2355edb2016-12-26 13:54:02 -0800148
Phil Burk3316d5e2017-02-15 11:23:01 -0800149 SharedRingBuffer* mUpMessageQueue;
Phil Burkc0c70e32017-02-09 13:18:38 -0800150 std::mutex mLockUpMessageQueue;
Phil Burk2355edb2016-12-26 13:54:02 -0800151
Phil Burkec89b2e2017-06-20 15:05:06 -0700152 AAudioThread mAAudioThread;
Phil Burkc0c70e32017-02-09 13:18:38 -0800153 // This is used by one thread to tell another thread to exit. So it must be atomic.
Phil Burkec89b2e2017-06-20 15:05:06 -0700154 std::atomic<bool> mThreadEnabled;
Phil Burkc0c70e32017-02-09 13:18:38 -0800155
Phil Burk9dca9822017-05-26 14:27:43 -0700156 aaudio_format_t mAudioFormat = AAUDIO_FORMAT_UNSPECIFIED;
Phil Burk3316d5e2017-02-15 11:23:01 -0800157 int32_t mFramesPerBurst = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -0800158 int32_t mSamplesPerFrame = AAUDIO_UNSPECIFIED;
159 int32_t mSampleRate = AAUDIO_UNSPECIFIED;
160 int32_t mCapacityInFrames = AAUDIO_UNSPECIFIED;
Phil Burk2ac035f2017-06-23 14:51:14 -0700161 uid_t mOwnerUserId = -1;
Phil Burk11e8d332017-05-24 09:59:02 -0700162private:
163 aaudio_handle_t mHandle = -1;
Phil Burk2355edb2016-12-26 13:54:02 -0800164};
165
Phil Burk5ed503c2017-02-01 09:38:15 -0800166} /* namespace aaudio */
Phil Burk2355edb2016-12-26 13:54:02 -0800167
Phil Burk5ed503c2017-02-01 09:38:15 -0800168#endif //AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H