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, |
| 52 | aaudio_position_frames_t *framePosition, |
| 53 | aaudio_nanoseconds_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 | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 65 | aaudio_nanoseconds_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, |
| 69 | aaudio_nanoseconds_t timeoutNanoseconds) override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 70 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 71 | virtual aaudio_result_t setBufferSize(aaudio_size_frames_t requestedFrames, |
| 72 | aaudio_size_frames_t *actualFrames) override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 73 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 74 | virtual aaudio_size_frames_t getBufferSize() const override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 75 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 76 | virtual aaudio_size_frames_t getBufferCapacity() const override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 77 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 78 | virtual aaudio_size_frames_t getFramesPerBurst() const override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 79 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 80 | virtual aaudio_position_frames_t getFramesRead() override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 81 | |
| 82 | virtual int32_t getXRunCount() const override { |
| 83 | return mXRunCount; |
| 84 | } |
| 85 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 86 | virtual aaudio_result_t registerThread() override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 87 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 88 | virtual aaudio_result_t unregisterThread() override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 89 | |
| 90 | protected: |
| 91 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 92 | aaudio_result_t processCommands(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * Low level write that will not block. It will just write as much as it can. |
| 96 | * |
| 97 | * It passed back a recommended time to wake up if wakeTimePtr is not NULL. |
| 98 | * |
| 99 | * @return the number of frames written or a negative error code. |
| 100 | */ |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 101 | virtual aaudio_result_t writeNow(const void *buffer, |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 102 | int32_t numFrames, |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 103 | aaudio_nanoseconds_t currentTimeNanos, |
| 104 | aaudio_nanoseconds_t *wakeTimePtr); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 105 | |
| 106 | void onFlushFromServer(); |
| 107 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 108 | aaudio_result_t onEventFromServer(AAudioServiceMessage *message); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 109 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 110 | aaudio_result_t onTimestampFromServer(AAudioServiceMessage *message); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 111 | |
| 112 | private: |
| 113 | IsochronousClockModel mClockModel; |
| 114 | AudioEndpoint mAudioEndpoint; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 115 | aaudio_handle_t mServiceStreamHandle; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 116 | EndpointDescriptor mEndpointDescriptor; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 117 | // Offset from underlying frame position. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 118 | aaudio_position_frames_t mFramesOffsetFromService = 0; |
| 119 | aaudio_position_frames_t mLastFramesRead = 0; |
| 120 | aaudio_size_frames_t mFramesPerBurst; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 121 | int32_t mXRunCount = 0; |
| 122 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 123 | void processTimestamp(uint64_t position, aaudio_nanoseconds_t time); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 124 | }; |
| 125 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 126 | } /* namespace aaudio */ |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 127 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 128 | #endif //AAUDIO_AUDIOSTREAMINTERNAL_H |