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