181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent/*
281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent**
381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** Copyright 2012, The Android Open Source Project
481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent**
581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** Licensed under the Apache License, Version 2.0 (the "License");
681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** you may not use this file except in compliance with the License.
781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** You may obtain a copy of the License at
881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent**
981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent**     http://www.apache.org/licenses/LICENSE-2.0
1081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent**
1181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** Unless required by applicable law or agreed to in writing, software
1281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** distributed under the License is distributed on an "AS IS" BASIS,
1381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** See the License for the specific language governing permissions and
1581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** limitations under the License.
1681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent*/
1781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifndef INCLUDING_FROM_AUDIOFLINGER_H
1981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    #error This header file should only be included from AudioFlinger.h
2081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
2181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// record track
2381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass RecordTrack : public TrackBase {
2481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
2581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        RecordTrack(RecordThread *thread,
2681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                const sp<Client>& client,
271f564acca544826daaf7dca44e39cec6016b82fdKevin Rocard                                const audio_attributes_t& attr,
2881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                uint32_t sampleRate,
2981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                audio_format_t format,
3081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                audio_channel_mask_t channelMask,
3181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                size_t frameCount,
3283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                                void *buffer,
338fe68036c2a670abb9eb0cc67ab9830c2c23de81Andy Hung                                size_t bufferSize,
34d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                                audio_session_t sessionId,
351f12a8ad958344c50733b948628ffa06db9c5bc6Andy Hung                                uid_t uid,
36050677873c10d4da308ac222f8533c96cca3207eEric Laurent                                audio_input_flags_t flags,
3720b9ef0b55c9150ae11057ab997ae61be2d496efEric Laurent                                track_type type,
3820b9ef0b55c9150ae11057ab997ae61be2d496efEric Laurent                                audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE);
3981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual             ~RecordTrack();
4097a893eb34f8687485c88eaf15917974a203f20bAndy Hung    virtual status_t    initCheck() const;
4181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
42d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten    virtual status_t    start(AudioSystem::sync_event_t event, audio_session_t triggerSession);
4381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual void        stop();
4481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            void        destroy();
4681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
476acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual void        invalidate();
4881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // clear the buffer overflow flag
4981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            void        clearOverflow() { mOverflow = false; }
5081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // set the buffer overflow flag and return previous value
5181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            bool        setOverflow() { bool tmp = mOverflow; mOverflow = true;
5281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                return tmp; }
5381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    static  void        appendDumpHeader(String8& result);
552c6c3bb76b2fa18ed3536331a1700250bd04c7ceAndy Hung            void        appendDump(String8& result, bool active);
5681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5725f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten            void        handleSyncStartEvent(const sp<SyncEvent>& event);
5825f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten            void        clearSyncStartEvent();
5925f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten
603f0c902beb53a245c9db35e871607dba05b8d391Andy Hung            void        updateTrackFrameInfo(int64_t trackFramesReleased,
613f0c902beb53a245c9db35e871607dba05b8d391Andy Hung                                             int64_t sourceFramesRead,
623f0c902beb53a245c9db35e871607dba05b8d391Andy Hung                                             uint32_t halSampleRate,
633f0c902beb53a245c9db35e871607dba05b8d391Andy Hung                                             const ExtendedTimestamp &timestamp);
64050677873c10d4da308ac222f8533c96cca3207eEric Laurent
65050677873c10d4da308ac222f8533c96cca3207eEric Laurent    virtual bool        isFastTrack() const { return (mFlags & AUDIO_INPUT_FLAG_FAST) != 0; }
66050677873c10d4da308ac222f8533c96cca3207eEric Laurent
679378eb9634d07010bebf8a2d4bda84320ec80e57jiabin            void        setSilenced(bool silenced) { if (!isPatchTrack()) mSilenced = silenced; }
68f4ddfefc8ba59a8486d91826154cc9447821409eSvet Ganov            bool        isSilenced() const { return mSilenced; }
69f4ddfefc8ba59a8486d91826154cc9447821409eSvet Ganov
70653cc0ab6d5b714d1f26adcd08f9bc3269b62fa0jiabin            status_t    getActiveMicrophones(std::vector<media::MicrophoneInfo>* activeMicrophones);
71653cc0ab6d5b714d1f26adcd08f9bc3269b62fa0jiabin
7281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
7381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    friend class AudioFlinger;  // for mState
7481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
75bf49308851509d38dbf3f57d9682a819d9f983c9Mikhail Naganov    DISALLOW_COPY_AND_ASSIGN(RecordTrack);
7681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
7781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // AudioBufferProvider interface
78d79072e9dff59f767cce2cda1caab80ce5a0815bGlenn Kasten    virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
7981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // releaseBuffer() not overridden
8081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
8181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool                mOverflow;  // overflow on most recent attempt to fill client buffer
826dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
836dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            AudioBufferProvider::Buffer mSink;  // references client's buffer sink in shared memory
846dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
856dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            // sync event triggering actual audio capture. Frames read before this event will
866dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            // be dropped and therefore not read by the application.
876dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            sp<SyncEvent>                       mSyncStartEvent;
886dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
896dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            // number of captured frames to drop after the start sync event has been received.
906dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            // when < 0, maximum frames to drop before starting capture even if sync event is
916dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            // not received
926dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            ssize_t                             mFramesToDrop;
936dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
946dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            // used by resampler to find source frames
9597a893eb34f8687485c88eaf15917974a203f20bAndy Hung            ResamplerBufferProvider            *mResamplerBufferProvider;
9697a893eb34f8687485c88eaf15917974a203f20bAndy Hung
9797a893eb34f8687485c88eaf15917974a203f20bAndy Hung            // used by the record thread to convert frames to proper destination format
9897a893eb34f8687485c88eaf15917974a203f20bAndy Hung            RecordBufferConverter              *mRecordBufferConverter;
99050677873c10d4da308ac222f8533c96cca3207eEric Laurent            audio_input_flags_t                mFlags;
100f4ddfefc8ba59a8486d91826154cc9447821409eSvet Ganov
101f4ddfefc8ba59a8486d91826154cc9447821409eSvet Ganov            bool                               mSilenced;
10281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
10383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
10483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent// playback track, used by PatchPanel
10583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentclass PatchRecord : virtual public RecordTrack, public PatchProxyBufferProvider {
10683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentpublic:
10783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
10883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    PatchRecord(RecordThread *recordThread,
10983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                uint32_t sampleRate,
11083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                audio_channel_mask_t channelMask,
11183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                audio_format_t format,
11283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                size_t frameCount,
11383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                void *buffer,
1148fe68036c2a670abb9eb0cc67ab9830c2c23de81Andy Hung                size_t bufferSize,
115050677873c10d4da308ac222f8533c96cca3207eEric Laurent                audio_input_flags_t flags);
11683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    virtual             ~PatchRecord();
11783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
11883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    // AudioBufferProvider interface
119d79072e9dff59f767cce2cda1caab80ce5a0815bGlenn Kasten    virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
12083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer);
12183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
12283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    // PatchProxyBufferProvider interface
12383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    virtual status_t    obtainBuffer(Proxy::Buffer *buffer,
12483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                                     const struct timespec *timeOut = NULL);
12583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    virtual void        releaseBuffer(Proxy::Buffer *buffer);
12683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
12783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    void setPeerProxy(PatchProxyBufferProvider *proxy) { mPeerProxy = proxy; }
12883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
12983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentprivate:
13083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    sp<ClientProxy>             mProxy;
13183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    PatchProxyBufferProvider*   mPeerProxy;
13283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    struct timespec             mPeerTimeout;
13383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent};  // end of PatchRecord
134