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