Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 1 | /* |
| 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 Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 17 | #ifndef AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H |
| 18 | #define AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 19 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 20 | #include <mutex> |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 21 | |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 22 | #include "fifo/FifoBuffer.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 23 | #include "binding/IAAudioService.h" |
| 24 | #include "binding/AudioEndpointParcelable.h" |
| 25 | #include "binding/AAudioServiceMessage.h" |
| 26 | #include "utility/AAudioUtilities.h" |
| 27 | |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 28 | #include "SharedRingBuffer.h" |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 29 | #include "AAudioThread.h" |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 30 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 31 | namespace aaudio { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 32 | |
| 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 Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 37 | /** |
| 38 | * Base class for a stream in the AAudio service. |
| 39 | */ |
| 40 | class AAudioServiceStreamBase |
| 41 | : public Runnable { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 42 | |
| 43 | public: |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 44 | AAudioServiceStreamBase(); |
| 45 | virtual ~AAudioServiceStreamBase(); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 46 | |
| 47 | enum { |
| 48 | ILLEGAL_THREAD_ID = 0 |
| 49 | }; |
| 50 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 51 | // ------------------------------------------------------------------- |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 52 | /** |
| 53 | * Open the device. |
| 54 | */ |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 55 | virtual aaudio_result_t open(const aaudio::AAudioStreamRequest &request, |
| 56 | aaudio::AAudioStreamConfiguration &configurationOutput) = 0; |
| 57 | |
| 58 | virtual aaudio_result_t close(); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Start the flow of data. |
| 62 | */ |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 63 | virtual aaudio_result_t start() = 0; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Stop the flow of data such that start() can resume with loss of data. |
| 67 | */ |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 68 | virtual aaudio_result_t pause() = 0; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Discard any data held by the underlying HAL or Service. |
| 72 | */ |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 73 | virtual aaudio_result_t flush() = 0; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 74 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 75 | // ------------------------------------------------------------------- |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 76 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 77 | /** |
| 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 Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 83 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 84 | /** |
| 85 | * Fill in a parcelable description of stream. |
| 86 | */ |
| 87 | aaudio_result_t getDescription(AudioEndpointParcelable &parcelable); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 88 | |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 89 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 90 | void setRegisteredThread(pid_t pid) { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 91 | mRegisteredClientThread = pid; |
| 92 | } |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 93 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 94 | pid_t getRegisteredThread() const { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 95 | return mRegisteredClientThread; |
| 96 | } |
| 97 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 98 | 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 Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 110 | protected: |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 111 | 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 Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 120 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 121 | pid_t mRegisteredClientThread = ILLEGAL_THREAD_ID; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 122 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 123 | SharedRingBuffer* mUpMessageQueue; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 124 | std::mutex mLockUpMessageQueue; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 125 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 126 | 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 Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 134 | int32_t mFramesPerBurst = 0; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 135 | int32_t mSamplesPerFrame = AAUDIO_UNSPECIFIED; |
| 136 | int32_t mSampleRate = AAUDIO_UNSPECIFIED; |
| 137 | int32_t mCapacityInFrames = AAUDIO_UNSPECIFIED; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 138 | }; |
| 139 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 140 | } /* namespace aaudio */ |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 141 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 142 | #endif //AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H |