blob: d6b6ee3d41fd0b7c39cb502fd9104fac93eb6941 [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 Burk2355edb2016-12-26 13:54:02 -080023#include "fifo/FifoBuffer.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080024#include "binding/IAAudioService.h"
25#include "binding/AudioEndpointParcelable.h"
26#include "binding/AAudioServiceMessage.h"
27#include "utility/AAudioUtilities.h"
28
Phil Burk2355edb2016-12-26 13:54:02 -080029#include "SharedRingBuffer.h"
Phil Burk5ed503c2017-02-01 09:38:15 -080030#include "AAudioThread.h"
Phil Burk2355edb2016-12-26 13:54:02 -080031
Phil Burk5ed503c2017-02-01 09:38:15 -080032namespace aaudio {
Phil Burk2355edb2016-12-26 13:54:02 -080033
34// We expect the queue to only have a few commands.
35// This should be way more than we need.
36#define QUEUE_UP_CAPACITY_COMMANDS (128)
37
Phil Burkc0c70e32017-02-09 13:18:38 -080038/**
39 * Base class for a stream in the AAudio service.
40 */
41class AAudioServiceStreamBase
42 : public Runnable {
Phil Burk2355edb2016-12-26 13:54:02 -080043
44public:
Phil Burk5ed503c2017-02-01 09:38:15 -080045 AAudioServiceStreamBase();
46 virtual ~AAudioServiceStreamBase();
Phil Burk2355edb2016-12-26 13:54:02 -080047
48 enum {
49 ILLEGAL_THREAD_ID = 0
50 };
51
Phil Burkc0c70e32017-02-09 13:18:38 -080052 // -------------------------------------------------------------------
Phil Burk2355edb2016-12-26 13:54:02 -080053 /**
54 * Open the device.
55 */
Phil Burkc0c70e32017-02-09 13:18:38 -080056 virtual aaudio_result_t open(const aaudio::AAudioStreamRequest &request,
57 aaudio::AAudioStreamConfiguration &configurationOutput) = 0;
58
59 virtual aaudio_result_t close();
Phil Burk2355edb2016-12-26 13:54:02 -080060
61 /**
62 * Start the flow of data.
63 */
Phil Burk71f35bb2017-04-13 16:05:07 -070064 virtual aaudio_result_t start();
Phil Burk2355edb2016-12-26 13:54:02 -080065
66 /**
67 * Stop the flow of data such that start() can resume with loss of data.
68 */
Phil Burk71f35bb2017-04-13 16:05:07 -070069 virtual aaudio_result_t pause();
70
71 /**
72 * Stop the flow of data after data in buffer has played.
73 */
74 virtual aaudio_result_t stop();
Phil Burk2355edb2016-12-26 13:54:02 -080075
76 /**
77 * Discard any data held by the underlying HAL or Service.
78 */
Phil Burk71f35bb2017-04-13 16:05:07 -070079 virtual aaudio_result_t flush();
Phil Burk2355edb2016-12-26 13:54:02 -080080
Phil Burkc0c70e32017-02-09 13:18:38 -080081 // -------------------------------------------------------------------
Phil Burk2355edb2016-12-26 13:54:02 -080082
Phil Burkc0c70e32017-02-09 13:18:38 -080083 /**
84 * Send a message to the client.
85 */
86 aaudio_result_t sendServiceEvent(aaudio_service_event_t event,
87 double dataDouble = 0.0,
88 int64_t dataLong = 0);
Phil Burkdec33ab2017-01-17 14:48:16 -080089
Phil Burkc0c70e32017-02-09 13:18:38 -080090 /**
91 * Fill in a parcelable description of stream.
92 */
93 aaudio_result_t getDescription(AudioEndpointParcelable &parcelable);
Phil Burk2355edb2016-12-26 13:54:02 -080094
Phil Burk2355edb2016-12-26 13:54:02 -080095
Phil Burkc0c70e32017-02-09 13:18:38 -080096 void setRegisteredThread(pid_t pid) {
Phil Burk2355edb2016-12-26 13:54:02 -080097 mRegisteredClientThread = pid;
98 }
Phil Burkdec33ab2017-01-17 14:48:16 -080099
Phil Burkc0c70e32017-02-09 13:18:38 -0800100 pid_t getRegisteredThread() const {
Phil Burk2355edb2016-12-26 13:54:02 -0800101 return mRegisteredClientThread;
102 }
103
Phil Burkc0c70e32017-02-09 13:18:38 -0800104 int32_t getFramesPerBurst() const {
105 return mFramesPerBurst;
106 }
107
108 int32_t calculateBytesPerFrame() const {
109 return mSamplesPerFrame * AAudioConvert_formatToSizeInBytes(mAudioFormat);
110 }
111
112 void run() override; // to implement Runnable
113
114 void processError();
115
Phil Burk2355edb2016-12-26 13:54:02 -0800116protected:
Phil Burkc0c70e32017-02-09 13:18:38 -0800117 aaudio_result_t writeUpMessageQueue(AAudioServiceMessage *command);
118
119 aaudio_result_t sendCurrentTimestamp();
120
121 virtual aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) = 0;
122
123 virtual aaudio_result_t getDownDataDescription(AudioEndpointParcelable &parcelable) = 0;
124
125 aaudio_stream_state_t mState = AAUDIO_STREAM_STATE_UNINITIALIZED;
Phil Burk2355edb2016-12-26 13:54:02 -0800126
Phil Burk3316d5e2017-02-15 11:23:01 -0800127 pid_t mRegisteredClientThread = ILLEGAL_THREAD_ID;
Phil Burk2355edb2016-12-26 13:54:02 -0800128
Phil Burk3316d5e2017-02-15 11:23:01 -0800129 SharedRingBuffer* mUpMessageQueue;
Phil Burkc0c70e32017-02-09 13:18:38 -0800130 std::mutex mLockUpMessageQueue;
Phil Burk2355edb2016-12-26 13:54:02 -0800131
Phil Burkc0c70e32017-02-09 13:18:38 -0800132 AAudioThread mAAudioThread;
133 // This is used by one thread to tell another thread to exit. So it must be atomic.
134 std::atomic<bool> mThreadEnabled;
135
136
137 int mAudioDataFileDescriptor = -1;
138
139 aaudio_audio_format_t mAudioFormat = AAUDIO_FORMAT_UNSPECIFIED;
Phil Burk3316d5e2017-02-15 11:23:01 -0800140 int32_t mFramesPerBurst = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -0800141 int32_t mSamplesPerFrame = AAUDIO_UNSPECIFIED;
142 int32_t mSampleRate = AAUDIO_UNSPECIFIED;
143 int32_t mCapacityInFrames = AAUDIO_UNSPECIFIED;
Phil Burk2355edb2016-12-26 13:54:02 -0800144};
145
Phil Burk5ed503c2017-02-01 09:38:15 -0800146} /* namespace aaudio */
Phil Burk2355edb2016-12-26 13:54:02 -0800147
Phil Burk5ed503c2017-02-01 09:38:15 -0800148#endif //AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H