1 /*
2 **
3 ** Copyright 2017, 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 #pragma once
19 
20 #include "TrackBase.h"
21 
22 #include <android/content/AttributionSourceState.h>
23 
24 namespace android {
25 
26 // playback track
27 class MmapTrack : public TrackBase, public IAfMmapTrack {
28 public:
29     MmapTrack(IAfThreadBase* thread,
30                             const audio_attributes_t& attr,
31                             uint32_t sampleRate,
32                             audio_format_t format,
33                             audio_channel_mask_t channelMask,
34                             audio_session_t sessionId,
35                             bool isOut,
36                             const android::content::AttributionSourceState& attributionSource,
37                             pid_t creatorPid,
38                             audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE);
39     ~MmapTrack() override;
40 
41     status_t initCheck() const final;
42     status_t start(
43             AudioSystem::sync_event_t event, audio_session_t triggerSession) final;
44     void stop() final;
isFastTrack()45     bool isFastTrack() const final { return false; }
isDirect()46     bool isDirect() const final { return true; }
47 
48     void appendDumpHeader(String8& result) const final;
49     void appendDump(String8& result, bool active) const final;
50 
51                         // protected by MMapThread::mLock
setSilenced_l(bool silenced)52     void setSilenced_l(bool silenced) final { mSilenced = silenced;
53                                                        mSilencedNotified = false;}
54                         // protected by MMapThread::mLock
isSilenced_l()55     bool isSilenced_l() const final { return mSilenced; }
56                         // protected by MMapThread::mLock
getAndSetSilencedNotified_l()57     bool getAndSetSilencedNotified_l() final { bool silencedNotified = mSilencedNotified;
58                                                         mSilencedNotified = true;
59                                                         return silencedNotified; }
60 
61     /**
62      * Updates the mute state and notifies the audio service. Call this only when holding player
63      * thread lock.
64      */
65     void processMuteEvent_l(const sp<IAudioManager>& audioManager,
66                             mute_state_t muteState)
67                             /* REQUIRES(MmapPlaybackThread::mLock) */ final;
68 private:
69     DISALLOW_COPY_AND_ASSIGN(MmapTrack);
70 
71     // AudioBufferProvider interface
72     virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer);
73     // releaseBuffer() not overridden
74 
75     // ExtendedAudioBufferProvider interface
76     size_t framesReady() const final;
77     int64_t framesReleased() const final;
78     void onTimestamp(const ExtendedTimestamp &timestamp) final;
79 
80     const pid_t mPid;
81     bool  mSilenced;            // protected by MMapThread::mLock
82     bool  mSilencedNotified;    // protected by MMapThread::mLock
83 
84     // TODO: replace PersistableBundle with own struct
85     // access these two variables only when holding player thread lock.
86     std::unique_ptr<os::PersistableBundle> mMuteEventExtras
87             /* GUARDED_BY(MmapPlaybackThread::mLock) */;
88     mute_state_t mMuteState
89             /* GUARDED_BY(MmapPlaybackThread::mLock) */;
90 };  // end of Track
91 
92 } // namespace android