blob: 0bd2276f20328322c0e6965b44bdbfca27b2a666 [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
81 /**
82 * Discard any data held by the underlying HAL or Service.
83 */
Phil Burk71f35bb2017-04-13 16:05:07 -070084 virtual aaudio_result_t flush();
Phil Burk2355edb2016-12-26 13:54:02 -080085
Phil Burk11e8d332017-05-24 09:59:02 -070086 bool isRunning() const {
87 return mState == AAUDIO_STREAM_STATE_STARTED;
88 }
Phil Burkc0c70e32017-02-09 13:18:38 -080089 // -------------------------------------------------------------------
Phil Burk2355edb2016-12-26 13:54:02 -080090
Phil Burkc0c70e32017-02-09 13:18:38 -080091 /**
92 * Send a message to the client.
93 */
94 aaudio_result_t sendServiceEvent(aaudio_service_event_t event,
95 double dataDouble = 0.0,
96 int64_t dataLong = 0);
Phil Burkdec33ab2017-01-17 14:48:16 -080097
Phil Burkc0c70e32017-02-09 13:18:38 -080098 /**
99 * Fill in a parcelable description of stream.
100 */
101 aaudio_result_t getDescription(AudioEndpointParcelable &parcelable);
Phil Burk2355edb2016-12-26 13:54:02 -0800102
Phil Burk2355edb2016-12-26 13:54:02 -0800103
Phil Burkc0c70e32017-02-09 13:18:38 -0800104 void setRegisteredThread(pid_t pid) {
Phil Burk2355edb2016-12-26 13:54:02 -0800105 mRegisteredClientThread = pid;
106 }
Phil Burkdec33ab2017-01-17 14:48:16 -0800107
Phil Burkc0c70e32017-02-09 13:18:38 -0800108 pid_t getRegisteredThread() const {
Phil Burk2355edb2016-12-26 13:54:02 -0800109 return mRegisteredClientThread;
110 }
111
Phil Burkc0c70e32017-02-09 13:18:38 -0800112 int32_t getFramesPerBurst() const {
113 return mFramesPerBurst;
114 }
115
116 int32_t calculateBytesPerFrame() const {
117 return mSamplesPerFrame * AAudioConvert_formatToSizeInBytes(mAudioFormat);
118 }
119
120 void run() override; // to implement Runnable
121
Phil Burk5ef003b2017-06-30 11:43:37 -0700122 void disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800123
Phil Burk2ac035f2017-06-23 14:51:14 -0700124 uid_t getOwnerUserId() const {
125 return mOwnerUserId;
126 }
127 void setOwnerUserId(uid_t uid) {
128 mOwnerUserId = uid;
129 }
130
Phil Burkb63320a2017-06-30 10:28:20 -0700131 pid_t getOwnerProcessId() const {
132 return mOwnerProcessId;
133 }
134 void setOwnerProcessId(pid_t pid) {
135 mOwnerProcessId = pid;
136 }
137
Phil Burk11e8d332017-05-24 09:59:02 -0700138 aaudio_handle_t getHandle() const {
139 return mHandle;
140 }
141 void setHandle(aaudio_handle_t handle) {
142 mHandle = handle;
143 }
144
Phil Burk2355edb2016-12-26 13:54:02 -0800145protected:
Phil Burkc0c70e32017-02-09 13:18:38 -0800146 aaudio_result_t writeUpMessageQueue(AAudioServiceMessage *command);
147
148 aaudio_result_t sendCurrentTimestamp();
149
150 virtual aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) = 0;
151
152 virtual aaudio_result_t getDownDataDescription(AudioEndpointParcelable &parcelable) = 0;
153
Phil Burkec89b2e2017-06-20 15:05:06 -0700154 aaudio_stream_state_t mState = AAUDIO_STREAM_STATE_UNINITIALIZED;
Phil Burk2355edb2016-12-26 13:54:02 -0800155
Phil Burk3316d5e2017-02-15 11:23:01 -0800156 pid_t mRegisteredClientThread = ILLEGAL_THREAD_ID;
Phil Burk2355edb2016-12-26 13:54:02 -0800157
Phil Burk3316d5e2017-02-15 11:23:01 -0800158 SharedRingBuffer* mUpMessageQueue;
Phil Burkc0c70e32017-02-09 13:18:38 -0800159 std::mutex mLockUpMessageQueue;
Phil Burk2355edb2016-12-26 13:54:02 -0800160
Phil Burkec89b2e2017-06-20 15:05:06 -0700161 AAudioThread mAAudioThread;
Phil Burkc0c70e32017-02-09 13:18:38 -0800162 // This is used by one thread to tell another thread to exit. So it must be atomic.
Phil Burkec89b2e2017-06-20 15:05:06 -0700163 std::atomic<bool> mThreadEnabled;
Phil Burkc0c70e32017-02-09 13:18:38 -0800164
Phil Burk9dca9822017-05-26 14:27:43 -0700165 aaudio_format_t mAudioFormat = AAUDIO_FORMAT_UNSPECIFIED;
Phil Burk3316d5e2017-02-15 11:23:01 -0800166 int32_t mFramesPerBurst = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -0800167 int32_t mSamplesPerFrame = AAUDIO_UNSPECIFIED;
168 int32_t mSampleRate = AAUDIO_UNSPECIFIED;
169 int32_t mCapacityInFrames = AAUDIO_UNSPECIFIED;
Phil Burk2ac035f2017-06-23 14:51:14 -0700170 uid_t mOwnerUserId = -1;
Phil Burkb63320a2017-06-30 10:28:20 -0700171 pid_t mOwnerProcessId = -1;
Phil Burk11e8d332017-05-24 09:59:02 -0700172private:
173 aaudio_handle_t mHandle = -1;
Phil Burk2355edb2016-12-26 13:54:02 -0800174};
175
Phil Burk5ed503c2017-02-01 09:38:15 -0800176} /* namespace aaudio */
Phil Burk2355edb2016-12-26 13:54:02 -0800177
Phil Burk5ed503c2017-02-01 09:38:15 -0800178#endif //AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H