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                                 uint32_t sampleRate,
28                                 audio_format_t format,
29                                 audio_channel_mask_t channelMask,
30                                 size_t frameCount,
31                                 void *buffer,
32                                 int sessionId,
33                                 int uid,
34                                 IAudioFlinger::track_flags_t flags,
35                                 track_type type);
36     virtual             ~RecordTrack();
37 
38     virtual status_t    start(AudioSystem::sync_event_t event, int triggerSession);
39     virtual void        stop();
40 
41             void        destroy();
42 
43             void        invalidate();
44             // clear the buffer overflow flag
clearOverflow()45             void        clearOverflow() { mOverflow = false; }
46             // set the buffer overflow flag and return previous value
setOverflow()47             bool        setOverflow() { bool tmp = mOverflow; mOverflow = true;
48                                                 return tmp; }
49 
50     static  void        appendDumpHeader(String8& result);
51             void        dump(char* buffer, size_t size, bool active);
52 
53             void        handleSyncStartEvent(const sp<SyncEvent>& event);
54             void        clearSyncStartEvent();
55 
56 private:
57     friend class AudioFlinger;  // for mState
58 
59                         RecordTrack(const RecordTrack&);
60                         RecordTrack& operator = (const RecordTrack&);
61 
62     // AudioBufferProvider interface
63     virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer,
64                                    int64_t pts = kInvalidPTS);
65     // releaseBuffer() not overridden
66 
67     bool                mOverflow;  // overflow on most recent attempt to fill client buffer
68 
69            // updated by RecordThread::readInputParameters_l()
70             AudioResampler                      *mResampler;
71 
72             // interleaved stereo pairs of fixed-point Q4.27
73             int32_t                             *mRsmpOutBuffer;
74             // current allocated frame count for the above, which may be larger than needed
75             size_t                              mRsmpOutFrameCount;
76 
77             size_t                              mRsmpInUnrel;   // unreleased frames remaining from
78                                                                 // most recent getNextBuffer
79                                                                 // for debug only
80 
81             // rolling counter that is never cleared
82             int32_t                             mRsmpInFront;   // next available frame
83 
84             AudioBufferProvider::Buffer mSink;  // references client's buffer sink in shared memory
85 
86             // sync event triggering actual audio capture. Frames read before this event will
87             // be dropped and therefore not read by the application.
88             sp<SyncEvent>                       mSyncStartEvent;
89 
90             // number of captured frames to drop after the start sync event has been received.
91             // when < 0, maximum frames to drop before starting capture even if sync event is
92             // not received
93             ssize_t                             mFramesToDrop;
94 
95             // used by resampler to find source frames
96             ResamplerBufferProvider *mResamplerBufferProvider;
97 };
98 
99 // playback track, used by PatchPanel
100 class PatchRecord : virtual public RecordTrack, public PatchProxyBufferProvider {
101 public:
102 
103     PatchRecord(RecordThread *recordThread,
104                 uint32_t sampleRate,
105                 audio_channel_mask_t channelMask,
106                 audio_format_t format,
107                 size_t frameCount,
108                 void *buffer,
109                 IAudioFlinger::track_flags_t flags);
110     virtual             ~PatchRecord();
111 
112     // AudioBufferProvider interface
113     virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer,
114                                    int64_t pts);
115     virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
116 
117     // PatchProxyBufferProvider interface
118     virtual status_t    obtainBuffer(Proxy::Buffer *buffer,
119                                      const struct timespec *timeOut = NULL);
120     virtual void        releaseBuffer(Proxy::Buffer *buffer);
121 
setPeerProxy(PatchProxyBufferProvider * proxy)122     void setPeerProxy(PatchProxyBufferProvider *proxy) { mPeerProxy = proxy; }
123 
124 private:
125     sp<ClientProxy>             mProxy;
126     PatchProxyBufferProvider*   mPeerProxy;
127     struct timespec             mPeerTimeout;
128 };  // end of PatchRecord
129