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 // record track
23 class RecordTrack : public TrackBase {
24 public:
25                         RecordTrack(RecordThread *thread,
26                                 const sp<Client>& client,
27                                 const audio_attributes_t& attr,
28                                 uint32_t sampleRate,
29                                 audio_format_t format,
30                                 audio_channel_mask_t channelMask,
31                                 size_t frameCount,
32                                 void *buffer,
33                                 size_t bufferSize,
34                                 audio_session_t sessionId,
35                                 uid_t uid,
36                                 audio_input_flags_t flags,
37                                 track_type type,
38                                 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE);
39     virtual             ~RecordTrack();
40     virtual status_t    initCheck() const;
41 
42     virtual status_t    start(AudioSystem::sync_event_t event, audio_session_t triggerSession);
43     virtual void        stop();
44 
45             void        destroy();
46 
47     virtual void        invalidate();
48             // clear the buffer overflow flag
clearOverflow()49             void        clearOverflow() { mOverflow = false; }
50             // set the buffer overflow flag and return previous value
setOverflow()51             bool        setOverflow() { bool tmp = mOverflow; mOverflow = true;
52                                                 return tmp; }
53 
54     static  void        appendDumpHeader(String8& result);
55             void        appendDump(String8& result, bool active);
56 
57             void        handleSyncStartEvent(const sp<SyncEvent>& event);
58             void        clearSyncStartEvent();
59 
60             void        updateTrackFrameInfo(int64_t trackFramesReleased,
61                                              int64_t sourceFramesRead,
62                                              uint32_t halSampleRate,
63                                              const ExtendedTimestamp &timestamp);
64 
isFastTrack()65     virtual bool        isFastTrack() const { return (mFlags & AUDIO_INPUT_FLAG_FAST) != 0; }
66 
setSilenced(bool silenced)67             void        setSilenced(bool silenced) { if (!isPatchTrack()) mSilenced = silenced; }
isSilenced()68             bool        isSilenced() const { return mSilenced; }
69 
70             status_t    getActiveMicrophones(std::vector<media::MicrophoneInfo>* activeMicrophones);
71 
72 private:
73     friend class AudioFlinger;  // for mState
74 
75     DISALLOW_COPY_AND_ASSIGN(RecordTrack);
76 
77     // AudioBufferProvider interface
78     virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
79     // releaseBuffer() not overridden
80 
81     bool                mOverflow;  // overflow on most recent attempt to fill client buffer
82 
83             AudioBufferProvider::Buffer mSink;  // references client's buffer sink in shared memory
84 
85             // sync event triggering actual audio capture. Frames read before this event will
86             // be dropped and therefore not read by the application.
87             sp<SyncEvent>                       mSyncStartEvent;
88 
89             // number of captured frames to drop after the start sync event has been received.
90             // when < 0, maximum frames to drop before starting capture even if sync event is
91             // not received
92             ssize_t                             mFramesToDrop;
93 
94             // used by resampler to find source frames
95             ResamplerBufferProvider            *mResamplerBufferProvider;
96 
97             // used by the record thread to convert frames to proper destination format
98             RecordBufferConverter              *mRecordBufferConverter;
99             audio_input_flags_t                mFlags;
100 
101             bool                               mSilenced;
102 };
103 
104 // playback track, used by PatchPanel
105 class PatchRecord : virtual public RecordTrack, public PatchProxyBufferProvider {
106 public:
107 
108     PatchRecord(RecordThread *recordThread,
109                 uint32_t sampleRate,
110                 audio_channel_mask_t channelMask,
111                 audio_format_t format,
112                 size_t frameCount,
113                 void *buffer,
114                 size_t bufferSize,
115                 audio_input_flags_t flags);
116     virtual             ~PatchRecord();
117 
118     // AudioBufferProvider interface
119     virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
120     virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
121 
122     // PatchProxyBufferProvider interface
123     virtual status_t    obtainBuffer(Proxy::Buffer *buffer,
124                                      const struct timespec *timeOut = NULL);
125     virtual void        releaseBuffer(Proxy::Buffer *buffer);
126 
setPeerProxy(PatchProxyBufferProvider * proxy)127     void setPeerProxy(PatchProxyBufferProvider *proxy) { mPeerProxy = proxy; }
128 
129 private:
130     sp<ClientProxy>             mProxy;
131     PatchProxyBufferProvider*   mPeerProxy;
132     struct timespec             mPeerTimeout;
133 };  // end of PatchRecord
134