Phil Burk | 204a163 | 2017-01-03 17:23:43 -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_AUDIOSTREAMINTERNAL_H |
| 18 | #define AAUDIO_AUDIOSTREAMINTERNAL_H |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 19 | |
| 20 | #include <stdint.h> |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 21 | #include <aaudio/AAudio.h> |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 22 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 23 | #include "binding/IAAudioService.h" |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 24 | #include "binding/AudioEndpointParcelable.h" |
| 25 | #include "client/IsochronousClockModel.h" |
| 26 | #include "client/AudioEndpoint.h" |
| 27 | #include "core/AudioStream.h" |
| 28 | |
| 29 | using android::sp; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 30 | using android::IAAudioService; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 31 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 32 | namespace aaudio { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 33 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 34 | // A stream that talks to the AAudioService or directly to a HAL. |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 35 | class AudioStreamInternal : public AudioStream { |
| 36 | |
| 37 | public: |
| 38 | AudioStreamInternal(); |
| 39 | virtual ~AudioStreamInternal(); |
| 40 | |
| 41 | // =========== Begin ABSTRACT methods =========================== |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 42 | virtual aaudio_result_t requestStart() override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 43 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 44 | virtual aaudio_result_t requestPause() override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 45 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 46 | virtual aaudio_result_t requestFlush() override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 47 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 48 | virtual aaudio_result_t requestStop() override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 49 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 50 | // TODO use aaudio_clockid_t all the way down to AudioClock |
| 51 | virtual aaudio_result_t getTimestamp(clockid_t clockId, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 52 | int64_t *framePosition, |
| 53 | int64_t *timeNanoseconds) override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 54 | |
| 55 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 56 | virtual aaudio_result_t updateState() override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 57 | // =========== End ABSTRACT methods =========================== |
| 58 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 59 | virtual aaudio_result_t open(const AudioStreamBuilder &builder) override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 60 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 61 | virtual aaudio_result_t close() override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 62 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 63 | virtual aaudio_result_t write(const void *buffer, |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 64 | int32_t numFrames, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 65 | int64_t timeoutNanoseconds) override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 66 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 67 | virtual aaudio_result_t waitForStateChange(aaudio_stream_state_t currentState, |
| 68 | aaudio_stream_state_t *nextState, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 69 | int64_t timeoutNanoseconds) override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 70 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 71 | virtual aaudio_result_t setBufferSize(int32_t requestedFrames) override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 72 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 73 | virtual int32_t getBufferSize() const override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 74 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 75 | virtual int32_t getBufferCapacity() const override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 76 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 77 | virtual int32_t getFramesPerBurst() const override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 78 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 79 | virtual int64_t getFramesRead() override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 80 | |
| 81 | virtual int32_t getXRunCount() const override { |
| 82 | return mXRunCount; |
| 83 | } |
| 84 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 85 | virtual aaudio_result_t registerThread() override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 86 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 87 | virtual aaudio_result_t unregisterThread() override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 88 | |
| 89 | protected: |
| 90 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 91 | aaudio_result_t processCommands(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * Low level write that will not block. It will just write as much as it can. |
| 95 | * |
| 96 | * It passed back a recommended time to wake up if wakeTimePtr is not NULL. |
| 97 | * |
| 98 | * @return the number of frames written or a negative error code. |
| 99 | */ |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 100 | virtual aaudio_result_t writeNow(const void *buffer, |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 101 | int32_t numFrames, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 102 | int64_t currentTimeNanos, |
| 103 | int64_t *wakeTimePtr); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 104 | |
| 105 | void onFlushFromServer(); |
| 106 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 107 | aaudio_result_t onEventFromServer(AAudioServiceMessage *message); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 108 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 109 | aaudio_result_t onTimestampFromServer(AAudioServiceMessage *message); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 110 | |
| 111 | private: |
| 112 | IsochronousClockModel mClockModel; |
| 113 | AudioEndpoint mAudioEndpoint; |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 114 | aaudio_handle_t mServiceStreamHandle; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 115 | EndpointDescriptor mEndpointDescriptor; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 116 | // Offset from underlying frame position. |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 117 | int64_t mFramesOffsetFromService = 0; |
| 118 | int64_t mLastFramesRead = 0; |
| 119 | int32_t mFramesPerBurst; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 120 | int32_t mXRunCount = 0; |
| 121 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 122 | void processTimestamp(uint64_t position, int64_t time); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 123 | }; |
| 124 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 125 | } /* namespace aaudio */ |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 126 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 127 | #endif //AAUDIO_AUDIOSTREAMINTERNAL_H |