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(std::vector<media::MicrophoneInfo>* activeMicrophones); 77 78 status_t setPreferredMicrophoneDirection(audio_microphone_direction_t direction); 79 status_t setPreferredMicrophoneFieldDimension(float zoom); 80 status_t shareAudioHistory(const std::string& sharedAudioPackageName, 81 int64_t sharedAudioStartMs); startFrames()82 int32_t startFrames() { return mStartFrames; } 83 checkServerLatencySupported(audio_format_t format,audio_input_flags_t flags)84 static bool checkServerLatencySupported( 85 audio_format_t format, audio_input_flags_t flags) { 86 return audio_is_linear_pcm(format) 87 && (flags & AUDIO_INPUT_FLAG_HW_AV_SYNC) == 0; 88 } 89 90 private: 91 friend class AudioFlinger; // for mState 92 93 DISALLOW_COPY_AND_ASSIGN(RecordTrack); 94 95 // AudioBufferProvider interface 96 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer); 97 // releaseBuffer() not overridden 98 99 bool mOverflow; // overflow on most recent attempt to fill client buffer 100 101 AudioBufferProvider::Buffer mSink; // references client's buffer sink in shared memory 102 103 // sync event triggering actual audio capture. Frames read before this event will 104 // be dropped and therefore not read by the application. 105 sp<SyncEvent> mSyncStartEvent; 106 107 // number of captured frames to drop after the start sync event has been received. 108 // when < 0, maximum frames to drop before starting capture even if sync event is 109 // not received 110 ssize_t mFramesToDrop; 111 112 // used by resampler to find source frames 113 ResamplerBufferProvider *mResamplerBufferProvider; 114 115 // used by the record thread to convert frames to proper destination format 116 RecordBufferConverter *mRecordBufferConverter; 117 audio_input_flags_t mFlags; 118 119 bool mSilenced; 120 121 std::string mSharedAudioPackageName = {}; 122 int32_t mStartFrames = -1; 123 }; 124 125 // playback track, used by PatchPanel 126 class PatchRecord : public RecordTrack, public PatchTrackBase { 127 public: 128 129 PatchRecord(RecordThread *recordThread, 130 uint32_t sampleRate, 131 audio_channel_mask_t channelMask, 132 audio_format_t format, 133 size_t frameCount, 134 void *buffer, 135 size_t bufferSize, 136 audio_input_flags_t flags, 137 const Timeout& timeout = {}); 138 virtual ~PatchRecord(); 139 getSource()140 virtual Source* getSource() { return nullptr; } 141 142 // AudioBufferProvider interface 143 virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer); 144 virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer); 145 146 // PatchProxyBufferProvider interface 147 virtual status_t obtainBuffer(Proxy::Buffer *buffer, 148 const struct timespec *timeOut = NULL); 149 virtual void releaseBuffer(Proxy::Buffer *buffer); 150 writeFrames(const void * src,size_t frameCount,size_t frameSize)151 size_t writeFrames(const void* src, size_t frameCount, size_t frameSize) { 152 return writeFrames(this, src, frameCount, frameSize); 153 } 154 155 protected: 156 /** Write the source data into the buffer provider. @return written frame count. */ 157 static size_t writeFrames(AudioBufferProvider* dest, const void* src, 158 size_t frameCount, size_t frameSize); 159 160 }; // end of PatchRecord 161 162 class PassthruPatchRecord : public PatchRecord, public Source { 163 public: 164 PassthruPatchRecord(RecordThread *recordThread, 165 uint32_t sampleRate, 166 audio_channel_mask_t channelMask, 167 audio_format_t format, 168 size_t frameCount, 169 audio_input_flags_t flags); 170 getSource()171 Source* getSource() override { return static_cast<Source*>(this); } 172 173 // Source interface 174 status_t read(void *buffer, size_t bytes, size_t *read) override; 175 status_t getCapturePosition(int64_t *frames, int64_t *time) override; 176 status_t standby() override; 177 178 // AudioBufferProvider interface 179 // This interface is used by RecordThread to pass the data obtained 180 // from HAL or other source to the client. PassthruPatchRecord receives 181 // the data in 'obtainBuffer' so these calls are stubbed out. 182 status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) override; 183 void releaseBuffer(AudioBufferProvider::Buffer* buffer) override; 184 185 // PatchProxyBufferProvider interface 186 // This interface is used from DirectOutputThread to acquire data from HAL. producesBufferOnDemand()187 bool producesBufferOnDemand() const override { return true; } 188 status_t obtainBuffer(Proxy::Buffer *buffer, const struct timespec *timeOut = nullptr) override; 189 void releaseBuffer(Proxy::Buffer *buffer) override; 190 191 private: 192 // This is to use with PatchRecord::writeFrames 193 struct PatchRecordAudioBufferProvider : public AudioBufferProvider { PatchRecordAudioBufferProviderPatchRecordAudioBufferProvider194 explicit PatchRecordAudioBufferProvider(PassthruPatchRecord& passthru) : 195 mPassthru(passthru) {} getNextBufferPatchRecordAudioBufferProvider196 status_t getNextBuffer(AudioBufferProvider::Buffer* buffer) override { 197 return mPassthru.PatchRecord::getNextBuffer(buffer); 198 } releaseBufferPatchRecordAudioBufferProvider199 void releaseBuffer(AudioBufferProvider::Buffer* buffer) override { 200 return mPassthru.PatchRecord::releaseBuffer(buffer); 201 } 202 private: 203 PassthruPatchRecord& mPassthru; 204 }; 205 206 sp<StreamInHalInterface> obtainStream(sp<ThreadBase>* thread); 207 208 PatchRecordAudioBufferProvider mPatchRecordAudioBufferProvider; 209 std::unique_ptr<void, decltype(free)*> mSinkBuffer; // frame size aligned continuous buffer 210 std::unique_ptr<void, decltype(free)*> mStubBuffer; // buffer used for AudioBufferProvider 211 size_t mUnconsumedFrames = 0; 212 std::mutex mReadLock; 213 std::condition_variable mReadCV; 214 size_t mReadBytes = 0; // GUARDED_BY(mReadLock) 215 status_t mReadError = NO_ERROR; // GUARDED_BY(mReadLock) 216 int64_t mLastReadFrames = 0; // accessed on RecordThread only 217 }; 218