blob: 91eec35c3f8e222de21202a49712c4bb0d737d52 [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 Burkc0c70e32017-02-09 13:18:38 -080020#include <mutex>
Phil Burkdec33ab2017-01-17 14:48:16 -080021
Phil Burk2355edb2016-12-26 13:54:02 -080022#include "fifo/FifoBuffer.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080023#include "binding/IAAudioService.h"
24#include "binding/AudioEndpointParcelable.h"
25#include "binding/AAudioServiceMessage.h"
26#include "utility/AAudioUtilities.h"
27
Phil Burk2355edb2016-12-26 13:54:02 -080028#include "SharedRingBuffer.h"
Phil Burk5ed503c2017-02-01 09:38:15 -080029#include "AAudioThread.h"
Phil Burk2355edb2016-12-26 13:54:02 -080030
Phil Burk5ed503c2017-02-01 09:38:15 -080031namespace aaudio {
Phil Burk2355edb2016-12-26 13:54:02 -080032
33// We expect the queue to only have a few commands.
34// This should be way more than we need.
35#define QUEUE_UP_CAPACITY_COMMANDS (128)
36
Phil Burkc0c70e32017-02-09 13:18:38 -080037/**
38 * Base class for a stream in the AAudio service.
39 */
40class AAudioServiceStreamBase
41 : public Runnable {
Phil Burk2355edb2016-12-26 13:54:02 -080042
43public:
Phil Burk5ed503c2017-02-01 09:38:15 -080044 AAudioServiceStreamBase();
45 virtual ~AAudioServiceStreamBase();
Phil Burk2355edb2016-12-26 13:54:02 -080046
47 enum {
48 ILLEGAL_THREAD_ID = 0
49 };
50
Phil Burkc0c70e32017-02-09 13:18:38 -080051 // -------------------------------------------------------------------
Phil Burk2355edb2016-12-26 13:54:02 -080052 /**
53 * Open the device.
54 */
Phil Burkc0c70e32017-02-09 13:18:38 -080055 virtual aaudio_result_t open(const aaudio::AAudioStreamRequest &request,
56 aaudio::AAudioStreamConfiguration &configurationOutput) = 0;
57
58 virtual aaudio_result_t close();
Phil Burk2355edb2016-12-26 13:54:02 -080059
60 /**
61 * Start the flow of data.
62 */
Phil Burk5ed503c2017-02-01 09:38:15 -080063 virtual aaudio_result_t start() = 0;
Phil Burk2355edb2016-12-26 13:54:02 -080064
65 /**
66 * Stop the flow of data such that start() can resume with loss of data.
67 */
Phil Burk5ed503c2017-02-01 09:38:15 -080068 virtual aaudio_result_t pause() = 0;
Phil Burk2355edb2016-12-26 13:54:02 -080069
70 /**
71 * Discard any data held by the underlying HAL or Service.
72 */
Phil Burk5ed503c2017-02-01 09:38:15 -080073 virtual aaudio_result_t flush() = 0;
Phil Burk2355edb2016-12-26 13:54:02 -080074
Phil Burkc0c70e32017-02-09 13:18:38 -080075 // -------------------------------------------------------------------
Phil Burk2355edb2016-12-26 13:54:02 -080076
Phil Burkc0c70e32017-02-09 13:18:38 -080077 /**
78 * Send a message to the client.
79 */
80 aaudio_result_t sendServiceEvent(aaudio_service_event_t event,
81 double dataDouble = 0.0,
82 int64_t dataLong = 0);
Phil Burkdec33ab2017-01-17 14:48:16 -080083
Phil Burkc0c70e32017-02-09 13:18:38 -080084 /**
85 * Fill in a parcelable description of stream.
86 */
87 aaudio_result_t getDescription(AudioEndpointParcelable &parcelable);
Phil Burk2355edb2016-12-26 13:54:02 -080088
Phil Burk2355edb2016-12-26 13:54:02 -080089
Phil Burkc0c70e32017-02-09 13:18:38 -080090 void setRegisteredThread(pid_t pid) {
Phil Burk2355edb2016-12-26 13:54:02 -080091 mRegisteredClientThread = pid;
92 }
Phil Burkdec33ab2017-01-17 14:48:16 -080093
Phil Burkc0c70e32017-02-09 13:18:38 -080094 pid_t getRegisteredThread() const {
Phil Burk2355edb2016-12-26 13:54:02 -080095 return mRegisteredClientThread;
96 }
97
Phil Burkc0c70e32017-02-09 13:18:38 -080098 int32_t getFramesPerBurst() const {
99 return mFramesPerBurst;
100 }
101
102 int32_t calculateBytesPerFrame() const {
103 return mSamplesPerFrame * AAudioConvert_formatToSizeInBytes(mAudioFormat);
104 }
105
106 void run() override; // to implement Runnable
107
108 void processError();
109
Phil Burk2355edb2016-12-26 13:54:02 -0800110protected:
Phil Burkc0c70e32017-02-09 13:18:38 -0800111 aaudio_result_t writeUpMessageQueue(AAudioServiceMessage *command);
112
113 aaudio_result_t sendCurrentTimestamp();
114
115 virtual aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) = 0;
116
117 virtual aaudio_result_t getDownDataDescription(AudioEndpointParcelable &parcelable) = 0;
118
119 aaudio_stream_state_t mState = AAUDIO_STREAM_STATE_UNINITIALIZED;
Phil Burk2355edb2016-12-26 13:54:02 -0800120
Phil Burk3316d5e2017-02-15 11:23:01 -0800121 pid_t mRegisteredClientThread = ILLEGAL_THREAD_ID;
Phil Burk2355edb2016-12-26 13:54:02 -0800122
Phil Burk3316d5e2017-02-15 11:23:01 -0800123 SharedRingBuffer* mUpMessageQueue;
Phil Burkc0c70e32017-02-09 13:18:38 -0800124 std::mutex mLockUpMessageQueue;
Phil Burk2355edb2016-12-26 13:54:02 -0800125
Phil Burkc0c70e32017-02-09 13:18:38 -0800126 AAudioThread mAAudioThread;
127 // This is used by one thread to tell another thread to exit. So it must be atomic.
128 std::atomic<bool> mThreadEnabled;
129
130
131 int mAudioDataFileDescriptor = -1;
132
133 aaudio_audio_format_t mAudioFormat = AAUDIO_FORMAT_UNSPECIFIED;
Phil Burk3316d5e2017-02-15 11:23:01 -0800134 int32_t mFramesPerBurst = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -0800135 int32_t mSamplesPerFrame = AAUDIO_UNSPECIFIED;
136 int32_t mSampleRate = AAUDIO_UNSPECIFIED;
137 int32_t mCapacityInFrames = AAUDIO_UNSPECIFIED;
Phil Burk2355edb2016-12-26 13:54:02 -0800138};
139
Phil Burk5ed503c2017-02-01 09:38:15 -0800140} /* namespace aaudio */
Phil Burk2355edb2016-12-26 13:54:02 -0800141
Phil Burk5ed503c2017-02-01 09:38:15 -0800142#endif //AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H