1 /* 2 ** 3 ** Copyright 2012, The Android Open Source Project 4 ** 5 ** Licensed under the Apache License, Version 2.0 (the "License"); 6 ** you may not use this file except in compliance with the License. 7 ** You may obtain a copy of the License at 8 ** 9 ** http://www.apache.org/licenses/LICENSE-2.0 10 ** 11 ** Unless required by applicable law or agreed to in writing, software 12 ** distributed under the License is distributed on an "AS IS" BASIS, 13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 ** See the License for the specific language governing permissions and 15 ** limitations under the License. 16 */ 17 18 #pragma once 19 20 #include "TrackBase.h" 21 22 #include <android/content/AttributionSourceState.h> 23 #include <audio_utils/mutex.h> 24 #include <datapath/AudioStreamIn.h> // struct Source 25 26 namespace android { 27 28 // record track 29 class RecordTrack : public TrackBase, public virtual IAfRecordTrack { 30 public: 31 RecordTrack(IAfRecordThread* thread, 32 const sp<Client>& client, 33 const audio_attributes_t& attr, 34 uint32_t sampleRate, 35 audio_format_t format, 36 audio_channel_mask_t channelMask, 37 size_t frameCount, 38 void *buffer, 39 size_t bufferSize, 40 audio_session_t sessionId, 41 pid_t creatorPid, 42 const AttributionSourceState& attributionSource, 43 audio_input_flags_t flags, 44 track_type type, 45 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE, 46 int32_t startFrames = -1); 47 ~RecordTrack() override; 48 status_t initCheck() const final; 49 50 status_t start(AudioSystem::sync_event_t event, audio_session_t triggerSession) final; 51 void stop() final; 52 void destroy() final; 53 void invalidate() final; 54 55 // clear the buffer overflow flag clearOverflow()56 void clearOverflow() final { mOverflow = false; } 57 // set the buffer overflow flag and return previous value setOverflow()58 bool setOverflow() final { bool tmp = mOverflow; mOverflow = true; 59 return tmp; } 60 61 void appendDumpHeader(String8& result) const final; 62 void appendDump(String8& result, bool active) const final; 63 64 void handleSyncStartEvent(const sp<audioflinger::SyncEvent>& event) final; 65 void clearSyncStartEvent() final; 66 67 void updateTrackFrameInfo(int64_t trackFramesReleased, 68 int64_t sourceFramesRead, 69 uint32_t halSampleRate, 70 const ExtendedTimestamp ×tamp) final; 71 isFastTrack()72 bool isFastTrack() const final { return (mFlags & AUDIO_INPUT_FLAG_FAST) != 0; } isDirect()73 bool isDirect() const final 74 { return (mFlags & AUDIO_INPUT_FLAG_DIRECT) != 0; } 75 setSilenced(bool silenced)76 void setSilenced(bool silenced) final { if (!isPatchTrack()) mSilenced = silenced; } isSilenced()77 bool isSilenced() const final { return mSilenced; } 78 79 status_t getActiveMicrophones( 80 std::vector<media::MicrophoneInfoFw>* activeMicrophones) const final; 81 82 status_t setPreferredMicrophoneDirection(audio_microphone_direction_t direction) final; 83 status_t setPreferredMicrophoneFieldDimension(float zoom) final; 84 status_t shareAudioHistory(const std::string& sharedAudioPackageName, 85 int64_t sharedAudioStartMs) final; startFrames()86 int32_t startFrames() const final { return mStartFrames; } 87 88 using SinkMetadatas = std::vector<record_track_metadata_v7_t>; 89 using MetadataInserter = std::back_insert_iterator<SinkMetadatas>; 90 void copyMetadataTo(MetadataInserter& backInserter) const final; 91 sinkBuffer()92 AudioBufferProvider::Buffer& sinkBuffer() final { return mSink; } synchronizedRecordState()93 audioflinger::SynchronizedRecordState& synchronizedRecordState() final { 94 return mSynchronizedRecordState; 95 } recordBufferConverter()96 RecordBufferConverter* recordBufferConverter() const final { return mRecordBufferConverter; } resamplerBufferProvider()97 ResamplerBufferProvider* resamplerBufferProvider() const final { 98 return mResamplerBufferProvider; 99 } 100 101 private: 102 DISALLOW_COPY_AND_ASSIGN(RecordTrack); 103 104 protected: 105 // AudioBufferProvider interface 106 status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) override; 107 // releaseBuffer() not overridden 108 109 private: 110 111 bool mOverflow; // overflow on most recent attempt to fill client buffer 112 113 AudioBufferProvider::Buffer mSink; // references client's buffer sink in shared memory 114 115 // sync event triggering actual audio capture. Frames read before this event will 116 // be dropped and therefore not read by the application. 117 sp<audioflinger::SyncEvent> mSyncStartEvent; 118 119 audioflinger::SynchronizedRecordState 120 mSynchronizedRecordState{mSampleRate}; // sampleRate defined in base 121 122 // used by resampler to find source frames 123 ResamplerBufferProvider* mResamplerBufferProvider; 124 125 // used by the record thread to convert frames to proper destination format 126 RecordBufferConverter *mRecordBufferConverter; 127 audio_input_flags_t mFlags; 128 129 bool mSilenced; 130 131 std::string mSharedAudioPackageName = {}; 132 int32_t mStartFrames = -1; 133 }; 134 135 // playback track, used by PatchPanel 136 class PatchRecord : public RecordTrack, public PatchTrackBase, public IAfPatchRecord { 137 public: 138 PatchRecord(IAfRecordThread* recordThread, 139 uint32_t sampleRate, 140 audio_channel_mask_t channelMask, 141 audio_format_t format, 142 size_t frameCount, 143 void *buffer, 144 size_t bufferSize, 145 audio_input_flags_t flags, 146 const Timeout& timeout = {}, 147 audio_source_t source = AUDIO_SOURCE_DEFAULT); 148 ~PatchRecord() override; 149 getSource()150 Source* getSource() override { return nullptr; } 151 152 // AudioBufferProvider interface 153 status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) override; 154 void releaseBuffer(AudioBufferProvider::Buffer* buffer) override; 155 156 // PatchProxyBufferProvider interface 157 status_t obtainBuffer(Proxy::Buffer* buffer, 158 const struct timespec* timeOut = nullptr) override; 159 void releaseBuffer(Proxy::Buffer* buffer) override; 160 writeFrames(const void * src,size_t frameCount,size_t frameSize)161 size_t writeFrames(const void* src, size_t frameCount, size_t frameSize) final { 162 return writeFrames(this, src, frameCount, frameSize); 163 } 164 165 protected: 166 /** Write the source data into the buffer provider. @return written frame count. */ 167 static size_t writeFrames(AudioBufferProvider* dest, const void* src, 168 size_t frameCount, size_t frameSize); 169 170 }; // end of PatchRecord 171 172 class PassthruPatchRecord : public PatchRecord, public Source { 173 public: 174 PassthruPatchRecord(IAfRecordThread* recordThread, 175 uint32_t sampleRate, 176 audio_channel_mask_t channelMask, 177 audio_format_t format, 178 size_t frameCount, 179 audio_input_flags_t flags, 180 audio_source_t source = AUDIO_SOURCE_DEFAULT); 181 getSource()182 Source* getSource() final { return static_cast<Source*>(this); } 183 184 // Source interface 185 status_t read(void* buffer, size_t bytes, size_t* read) final; 186 status_t getCapturePosition(int64_t* frames, int64_t* time) final; 187 status_t standby() final; 188 189 // AudioBufferProvider interface 190 // This interface is used by RecordThread to pass the data obtained 191 // from HAL or other source to the client. PassthruPatchRecord receives 192 // the data in 'obtainBuffer' so these calls are stubbed out. 193 status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) final; 194 void releaseBuffer(AudioBufferProvider::Buffer* buffer) final; 195 196 // PatchProxyBufferProvider interface 197 // This interface is used from DirectOutputThread to acquire data from HAL. producesBufferOnDemand()198 bool producesBufferOnDemand() const final { return true; } 199 status_t obtainBuffer(Proxy::Buffer* buffer, const struct timespec* timeOut = nullptr) final; 200 void releaseBuffer(Proxy::Buffer* buffer) final; 201 202 private: 203 // This is to use with PatchRecord::writeFrames 204 struct PatchRecordAudioBufferProvider : public AudioBufferProvider { PatchRecordAudioBufferProviderPatchRecordAudioBufferProvider205 explicit PatchRecordAudioBufferProvider(PassthruPatchRecord& passthru) : 206 mPassthru(passthru) {} getNextBufferPatchRecordAudioBufferProvider207 status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) override { 208 return mPassthru.PatchRecord::getNextBuffer(buffer); 209 } releaseBufferPatchRecordAudioBufferProvider210 void releaseBuffer(AudioBufferProvider::Buffer* buffer) override { 211 return mPassthru.PatchRecord::releaseBuffer(buffer); 212 } 213 private: 214 PassthruPatchRecord& mPassthru; 215 }; 216 217 sp<StreamInHalInterface> obtainStream(sp<IAfThreadBase>* thread); readMutex()218 audio_utils::mutex& readMutex() const { return mReadMutex; } 219 220 PatchRecordAudioBufferProvider mPatchRecordAudioBufferProvider; 221 std::unique_ptr<void, decltype(free)*> mSinkBuffer; // frame size aligned continuous buffer 222 std::unique_ptr<void, decltype(free)*> mStubBuffer; // buffer used for AudioBufferProvider 223 size_t mUnconsumedFrames = 0; 224 mutable audio_utils::mutex mReadMutex{ 225 audio_utils::MutexOrder::kPassthruPatchRecord_ReadMutex}; 226 audio_utils::condition_variable mReadCV; 227 size_t mReadBytes = 0; // GUARDED_BY(readMutex()) 228 status_t mReadError = NO_ERROR; // GUARDED_BY(readMutex()) 229 int64_t mLastReadFrames = 0; // accessed on RecordThread only 230 }; 231 232 } // namespace android 233