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 #include <android/content/AttributionSourceState.h> 19 20 #ifndef INCLUDING_FROM_AUDIOFLINGER_H 21 #error This header file should only be included from AudioFlinger.h 22 #endif 23 24 // record track 25 class RecordTrack : public TrackBase { 26 public: 27 RecordTrack(RecordThread *thread, 28 const sp<Client>& client, 29 const audio_attributes_t& attr, 30 uint32_t sampleRate, 31 audio_format_t format, 32 audio_channel_mask_t channelMask, 33 size_t frameCount, 34 void *buffer, 35 size_t bufferSize, 36 audio_session_t sessionId, 37 pid_t creatorPid, 38 const AttributionSourceState& attributionSource, 39 audio_input_flags_t flags, 40 track_type type, 41 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE, 42 int32_t startFrames = -1); 43 virtual ~RecordTrack(); 44 virtual status_t initCheck() const; 45 46 virtual status_t start(AudioSystem::sync_event_t event, audio_session_t triggerSession); 47 virtual void stop(); 48 49 void destroy(); 50 51 virtual void invalidate(); 52 // clear the buffer overflow flag clearOverflow()53 void clearOverflow() { mOverflow = false; } 54 // set the buffer overflow flag and return previous value setOverflow()55 bool setOverflow() { bool tmp = mOverflow; mOverflow = true; 56 return tmp; } 57 58 void appendDumpHeader(String8& result); 59 void appendDump(String8& result, bool active); 60 61 void handleSyncStartEvent(const sp<SyncEvent>& event); 62 void clearSyncStartEvent(); 63 64 void updateTrackFrameInfo(int64_t trackFramesReleased, 65 int64_t sourceFramesRead, 66 uint32_t halSampleRate, 67 const ExtendedTimestamp ×tamp); 68 isFastTrack()69 virtual bool isFastTrack() const { return (mFlags & AUDIO_INPUT_FLAG_FAST) != 0; } isDirect()70 bool isDirect() const override 71 { return (mFlags & AUDIO_INPUT_FLAG_DIRECT) != 0; } 72 setSilenced(bool silenced)73 void setSilenced(bool silenced) { if (!isPatchTrack()) mSilenced = silenced; } isSilenced()74 bool isSilenced() const { return mSilenced; } 75 76 status_t getActiveMicrophones( 77 std::vector<media::MicrophoneInfoFw>* activeMicrophones); 78 79 status_t setPreferredMicrophoneDirection(audio_microphone_direction_t direction); 80 status_t setPreferredMicrophoneFieldDimension(float zoom); 81 status_t shareAudioHistory(const std::string& sharedAudioPackageName, 82 int64_t sharedAudioStartMs); startFrames()83 int32_t startFrames() { return mStartFrames; } 84 checkServerLatencySupported(audio_format_t format,audio_input_flags_t flags)85 static bool checkServerLatencySupported( 86 audio_format_t format, audio_input_flags_t flags) { 87 return audio_is_linear_pcm(format) 88 && (flags & AUDIO_INPUT_FLAG_HW_AV_SYNC) == 0; 89 } 90 91 using SinkMetadatas = std::vector<record_track_metadata_v7_t>; 92 using MetadataInserter = std::back_insert_iterator<SinkMetadatas>; 93 virtual void copyMetadataTo(MetadataInserter& backInserter) const; 94 95 private: 96 friend class AudioFlinger; // for mState 97 98 DISALLOW_COPY_AND_ASSIGN(RecordTrack); 99 100 // AudioBufferProvider interface 101 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer); 102 // releaseBuffer() not overridden 103 104 bool mOverflow; // overflow on most recent attempt to fill client buffer 105 106 AudioBufferProvider::Buffer mSink; // references client's buffer sink in shared memory 107 108 // sync event triggering actual audio capture. Frames read before this event will 109 // be dropped and therefore not read by the application. 110 sp<SyncEvent> mSyncStartEvent; 111 112 // number of captured frames to drop after the start sync event has been received. 113 // when < 0, maximum frames to drop before starting capture even if sync event is 114 // not received 115 ssize_t mFramesToDrop; 116 117 // used by resampler to find source frames 118 ResamplerBufferProvider *mResamplerBufferProvider; 119 120 // used by the record thread to convert frames to proper destination format 121 RecordBufferConverter *mRecordBufferConverter; 122 audio_input_flags_t mFlags; 123 124 bool mSilenced; 125 126 std::string mSharedAudioPackageName = {}; 127 int32_t mStartFrames = -1; 128 }; 129 130 // playback track, used by PatchPanel 131 class PatchRecord : public RecordTrack, public PatchTrackBase { 132 public: 133 134 PatchRecord(RecordThread *recordThread, 135 uint32_t sampleRate, 136 audio_channel_mask_t channelMask, 137 audio_format_t format, 138 size_t frameCount, 139 void *buffer, 140 size_t bufferSize, 141 audio_input_flags_t flags, 142 const Timeout& timeout = {}, 143 audio_source_t source = AUDIO_SOURCE_DEFAULT); 144 virtual ~PatchRecord(); 145 getSource()146 virtual Source* getSource() { return nullptr; } 147 148 // AudioBufferProvider interface 149 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer); 150 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer); 151 152 // PatchProxyBufferProvider interface 153 virtual status_t obtainBuffer(Proxy::Buffer *buffer, 154 const struct timespec *timeOut = NULL); 155 virtual void releaseBuffer(Proxy::Buffer *buffer); 156 writeFrames(const void * src,size_t frameCount,size_t frameSize)157 size_t writeFrames(const void* src, size_t frameCount, size_t frameSize) { 158 return writeFrames(this, src, frameCount, frameSize); 159 } 160 161 protected: 162 /** Write the source data into the buffer provider. @return written frame count. */ 163 static size_t writeFrames(AudioBufferProvider* dest, const void* src, 164 size_t frameCount, size_t frameSize); 165 166 }; // end of PatchRecord 167 168 class PassthruPatchRecord : public PatchRecord, public Source { 169 public: 170 PassthruPatchRecord(RecordThread *recordThread, 171 uint32_t sampleRate, 172 audio_channel_mask_t channelMask, 173 audio_format_t format, 174 size_t frameCount, 175 audio_input_flags_t flags, 176 audio_source_t source = AUDIO_SOURCE_DEFAULT); 177 getSource()178 Source* getSource() override { return static_cast<Source*>(this); } 179 180 // Source interface 181 status_t read(void *buffer, size_t bytes, size_t *read) override; 182 status_t getCapturePosition(int64_t *frames, int64_t *time) override; 183 status_t standby() override; 184 185 // AudioBufferProvider interface 186 // This interface is used by RecordThread to pass the data obtained 187 // from HAL or other source to the client. PassthruPatchRecord receives 188 // the data in 'obtainBuffer' so these calls are stubbed out. 189 status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) override; 190 void releaseBuffer(AudioBufferProvider::Buffer* buffer) override; 191 192 // PatchProxyBufferProvider interface 193 // This interface is used from DirectOutputThread to acquire data from HAL. producesBufferOnDemand()194 bool producesBufferOnDemand() const override { return true; } 195 status_t obtainBuffer(Proxy::Buffer *buffer, const struct timespec *timeOut = nullptr) override; 196 void releaseBuffer(Proxy::Buffer *buffer) override; 197 198 private: 199 // This is to use with PatchRecord::writeFrames 200 struct PatchRecordAudioBufferProvider : public AudioBufferProvider { PatchRecordAudioBufferProviderPatchRecordAudioBufferProvider201 explicit PatchRecordAudioBufferProvider(PassthruPatchRecord& passthru) : 202 mPassthru(passthru) {} getNextBufferPatchRecordAudioBufferProvider203 status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) override { 204 return mPassthru.PatchRecord::getNextBuffer(buffer); 205 } releaseBufferPatchRecordAudioBufferProvider206 void releaseBuffer(AudioBufferProvider::Buffer* buffer) override { 207 return mPassthru.PatchRecord::releaseBuffer(buffer); 208 } 209 private: 210 PassthruPatchRecord& mPassthru; 211 }; 212 213 sp<StreamInHalInterface> obtainStream(sp<ThreadBase>* thread); 214 215 PatchRecordAudioBufferProvider mPatchRecordAudioBufferProvider; 216 std::unique_ptr<void, decltype(free)*> mSinkBuffer; // frame size aligned continuous buffer 217 std::unique_ptr<void, decltype(free)*> mStubBuffer; // buffer used for AudioBufferProvider 218 size_t mUnconsumedFrames = 0; 219 std::mutex mReadLock; 220 std::condition_variable mReadCV; 221 size_t mReadBytes = 0; // GUARDED_BY(mReadLock) 222 status_t mReadError = NO_ERROR; // GUARDED_BY(mReadLock) 223 int64_t mLastReadFrames = 0; // accessed on RecordThread only 224 }; 225