1 /* 2 * Copyright (C) 2017 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 17 #ifndef AAUDIO_SERVICE_ENDPOINT_MMAP_H 18 #define AAUDIO_SERVICE_ENDPOINT_MMAP_H 19 20 #include <atomic> 21 #include <functional> 22 #include <vector> 23 24 #include "client/AudioStreamInternal.h" 25 #include "client/AudioStreamInternalPlay.h" 26 #include "binding/AAudioServiceMessage.h" 27 #include "AAudioServiceEndpointShared.h" 28 #include "AAudioServiceStreamShared.h" 29 #include "AAudioServiceStreamMMAP.h" 30 #include "AAudioMixer.h" 31 #include "AAudioService.h" 32 #include "SharedMemoryWrapper.h" 33 34 namespace aaudio { 35 36 /** 37 * This is used by AAudioServiceStreamMMAP to access the MMAP devices 38 * through AudioFlinger. 39 */ 40 class AAudioServiceEndpointMMAP 41 : public AAudioServiceEndpoint 42 , public android::MmapStreamCallback { 43 44 public: 45 explicit AAudioServiceEndpointMMAP(android::AAudioService &audioService); 46 47 ~AAudioServiceEndpointMMAP() override = default; 48 49 std::string dump() const override; 50 51 aaudio_result_t open(const aaudio::AAudioStreamRequest &request) override; 52 53 void close() override EXCLUDES(mMmapStreamLock); 54 55 aaudio_result_t startStream(android::sp<AAudioServiceStreamBase> stream, 56 audio_port_handle_t *clientHandle) override; 57 58 aaudio_result_t stopStream(android::sp<AAudioServiceStreamBase> stream, 59 audio_port_handle_t clientHandle) override; 60 61 aaudio_result_t startClient(const android::AudioClient& client, 62 const audio_attributes_t *attr, 63 audio_port_handle_t *clientHandle) override 64 EXCLUDES(mMmapStreamLock); 65 66 aaudio_result_t stopClient(audio_port_handle_t clientHandle) override 67 EXCLUDES(mMmapStreamLock); 68 69 aaudio_result_t standby() override EXCLUDES(mMmapStreamLock); 70 71 aaudio_result_t exitStandby(AudioEndpointParcelable* parcelable) override 72 EXCLUDES(mMmapStreamLock); 73 74 aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) override 75 EXCLUDES(mMmapStreamLock); 76 77 aaudio_result_t getTimestamp(int64_t *positionFrames, int64_t *timeNanos) override; 78 79 void handleTearDownAsync(audio_port_handle_t portHandle); 80 81 // -------------- Callback functions for MmapStreamCallback --------------------- 82 void onTearDown(audio_port_handle_t portHandle) override; 83 84 void onVolumeChanged(float volume) override; 85 86 void onRoutingChanged(audio_port_handle_t portHandle) override; 87 // ------------------------------------------------------------------------------ 88 89 aaudio_result_t getDownDataDescription(AudioEndpointParcelable* parcelable); 90 getHardwareTimeOffsetNanos()91 int64_t getHardwareTimeOffsetNanos() const { 92 return mHardwareTimeOffsetNanos; 93 } 94 95 aaudio_result_t getExternalPosition(uint64_t *positionFrames, int64_t *timeNanos) 96 EXCLUDES(mMmapStreamLock); 97 98 int64_t nextDataReportTime() EXCLUDES(mMmapStreamLock); 99 100 void reportData() EXCLUDES(mMmapStreamLock); 101 102 private: 103 104 /** 105 * 106 * @return true if mMapStream was cleared 107 */ 108 bool close_l() REQUIRES(mMmapStreamLock); 109 110 aaudio_result_t openWithConfig(audio_config_base_t* config) EXCLUDES(mMmapStreamLock); 111 112 aaudio_result_t createMmapBuffer_l() REQUIRES(mMmapStreamLock); 113 114 MonotonicCounter mFramesTransferred; 115 116 // Interface to the AudioFlinger MMAP support. 117 mutable std::mutex mMmapStreamLock; 118 android::sp<android::MmapStreamInterface> mMmapStream GUARDED_BY(mMmapStreamLock); 119 120 struct audio_mmap_buffer_info mMmapBufferinfo; 121 122 // There is only one port associated with an MMAP endpoint. 123 audio_port_handle_t mPortHandle = AUDIO_PORT_HANDLE_NONE; 124 125 android::AAudioService &mAAudioService; 126 127 std::unique_ptr<SharedMemoryWrapper> mAudioDataWrapper; 128 129 int64_t mHardwareTimeOffsetNanos = 0; // TODO get from HAL 130 131 aaudio_result_t mHalExternalPositionStatus = AAUDIO_OK; 132 uint64_t mLastPositionFrames = 0; 133 int64_t mTimestampNanosForLastPosition = 0; 134 int32_t mTimestampGracePeriodMs; 135 int32_t mFrozenPositionCount = 0; 136 int32_t mFrozenTimestampCount = 0; 137 int64_t mDataReportOffsetNanos = 0; 138 139 }; 140 141 } /* namespace aaudio */ 142 143 #endif //AAUDIO_SERVICE_ENDPOINT_MMAP_H 144 145