• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef HISTREAMER_AUDIO_SINK_H
17 #define HISTREAMER_AUDIO_SINK_H
18 #include <mutex>
19 #include "common/status.h"
20 #include "meta/meta.h"
21 #include "sink/media_synchronous_sink.h"
22 #include "media_sync_manager.h"
23 #include "buffer/avbuffer_queue.h"
24 #include "buffer/avbuffer_queue_define.h"
25 #include "plugin/audio_sink_plugin.h"
26 #include "filter/filter.h"
27 #include "plugin/plugin_time.h"
28 #include "performance_utils.h"
29 
30 namespace OHOS {
31 namespace Media {
32 using namespace OHOS::Media::Plugins;
33 class AudioSink : public std::enable_shared_from_this<AudioSink>, public Pipeline::MediaSynchronousSink {
34 public:
35     AudioSink();
36     ~AudioSink();
37     Status Init(std::shared_ptr<Meta>& meta, const std::shared_ptr<Pipeline::EventReceiver>& receiver);
38     sptr<AVBufferQueueProducer> GetBufferQueueProducer();
39     sptr<AVBufferQueueConsumer> GetBufferQueueConsumer();
40     Status SetParameter(const std::shared_ptr<Meta>& meta);
41     Status GetParameter(std::shared_ptr<Meta>& meta);
42     Status Prepare();
43     Status Start();
44     Status Stop();
45     Status Pause();
46     Status Resume();
47     Status Flush();
48     Status Release();
49     Status SetPlayRange(int64_t start, int64_t end);
50     Status SetVolume(float volume);
51     void DrainOutputBuffer(bool flushed);
52     void SetEventReceiver(const std::shared_ptr<Pipeline::EventReceiver>& receiver);
53     Status GetLatency(uint64_t& nanoSec);
54     void SetSyncCenter(std::shared_ptr<Pipeline::MediaSyncManager> syncCenter);
55     int64_t DoSyncWrite(const std::shared_ptr<OHOS::Media::AVBuffer>& buffer) override;
56     void ResetSyncInfo() override;
57     Status SetSpeed(float speed);
58     Status SetAudioEffectMode(int32_t effectMode);
59     Status GetAudioEffectMode(int32_t &effectMode);
60     int32_t SetVolumeWithRamp(float targetVolume, int32_t duration);
61     void SetThreadGroupId(const std::string& groupId);
62     Status SetIsTransitent(bool isTransitent);
63     Status ChangeTrack(std::shared_ptr<Meta>& meta, const std::shared_ptr<Pipeline::EventReceiver>& receiver);
64     Status SetMuted(bool isMuted);
65     Status SetSeekTime(int64_t seekTime);
66     bool GetSyncCenterClockTime(int64_t &clockTime);
67     Status SetIsCalledBySystemApp(bool isCalledBySystemApp);
68     Status SetLooping(bool loop);
69 
70     float GetMaxAmplitude();
71     int32_t SetMaxAmplitudeCbStatus(bool status);
72     Status SetPerfRecEnabled(bool isPerfRecEnabled);
73 
74     static const int64_t kMinAudioClockUpdatePeriodUs = 20 * HST_USECOND;
75 
76     static const int64_t kMaxAllowedAudioSinkDelayUs = 1500 * HST_MSECOND;
77 protected:
78     std::atomic<OHOS::Media::Pipeline::FilterState> state_;
79 private:
80     Status PrepareInputBufferQueue();
81     std::shared_ptr<Plugins::AudioSinkPlugin> CreatePlugin();
82     int64_t getPendingAudioPlayoutDurationUs(int64_t nowUs);
83     int64_t getDurationUsPlayedAtSampleRate(uint32_t numFrames);
84     void UpdateAudioWriteTimeMayWait();
85     bool UpdateTimeAnchorIfNeeded(const std::shared_ptr<OHOS::Media::AVBuffer>& buffer);
86     void DrainAndReportEosEvent();
87     void HandleEosInner(bool drain);
88     bool DropApeBuffer(std::shared_ptr<AVBuffer> filledOutputBuffer);
89     void CalcMaxAmplitude(std::shared_ptr<AVBuffer> filledOutputBuffer);
90     void CheckUpdateState(char *frame, uint64_t replyBytes, int32_t format);
91     int64_t CalcBufferDuration(const std::shared_ptr<OHOS::Media::AVBuffer>& buffer);
92     void PerfRecord(int64_t audioWriteMs);
93 
94     class UnderrunDetector {
95     public:
96         void DetectAudioUnderrun(int64_t clkTime, int64_t latency);
97         void SetEventReceiver(std::weak_ptr<Pipeline::EventReceiver> eventReceiver);
98         void UpdateBufferTimeNoLock(int64_t clkTime, int64_t latency);
99         void SetLastAudioBufferDuration(int64_t durationUs);
100         void Reset();
101     private:
102         std::weak_ptr<Pipeline::EventReceiver> eventReceiver_;
103         Mutex mutex_ {};
104         int64_t lastClkTime_ {HST_TIME_NONE};
105         int64_t lastLatency_ {HST_TIME_NONE};
106         int64_t lastBufferDuration_ {HST_TIME_NONE};
107     };
108 
109     class AudioLagDetector : public Pipeline::LagDetector {
110     public:
111         void Reset() override;
112 
113         bool CalcLag(std::shared_ptr<AVBuffer> buffer) override;
114 
SetLatency(int64_t latency)115         void SetLatency(int64_t latency)
116         {
117             latency_ = latency;
118         }
119 
120         struct AudioDrainTimeGroup {
121             int64_t lastAnchorPts = 0;
122             int64_t anchorDuration = 0;
123             int64_t writeDuration = 0;
124             int64_t nowClockTime = 0;
125 
126             AudioDrainTimeGroup() = default;
AudioDrainTimeGroupAudioDrainTimeGroup127             AudioDrainTimeGroup(int64_t anchorPts, int64_t duration, int64_t writeDuration, int64_t clockTime)
128                 : lastAnchorPts(anchorPts),
129                   anchorDuration(duration),
130                   writeDuration(writeDuration),
131                   nowClockTime(clockTime) {}
132         };
133 
134         void UpdateDrainTimeGroup(AudioDrainTimeGroup group);
135     private:
136         int64_t latency_ = 0;
137         int64_t lastDrainTimeMs_ = 0;
138         AudioDrainTimeGroup lastDrainTimeGroup_ {};
139     };
140 
141     std::shared_ptr<Plugins::AudioSinkPlugin> plugin_ {};
142     std::shared_ptr<Pipeline::EventReceiver> playerEventReceiver_;
143     int32_t appUid_{0};
144     int32_t appPid_{0};
145     int64_t numFramesWritten_ {0};
146     int64_t firstAudioAnchorTimeMediaUs_ {HST_TIME_NONE};
147     int64_t nextAudioClockUpdateTimeUs_ {HST_TIME_NONE};
148     int64_t lastAnchorClockTime_  {HST_TIME_NONE};
149     int64_t latestBufferPts_ {HST_TIME_NONE};
150     int64_t latestBufferDuration_ {0};
151     int64_t bufferDurationSinceLastAnchor_ {0};
152     std::atomic<bool> forceUpdateTimeAnchorNextTime_ {true};
153     const std::string INPUT_BUFFER_QUEUE_NAME = "AudioSinkInputBufferQueue";
154     std::shared_ptr<AVBufferQueue> inputBufferQueue_;
155     sptr<AVBufferQueueProducer> inputBufferQueueProducer_;
156     sptr<AVBufferQueueConsumer> inputBufferQueueConsumer_;
157     int64_t firstPts_ {HST_TIME_NONE};
158     int32_t sampleRate_ {0};
159     int32_t samplePerFrame_ {0};
160     int32_t audioChannelCount_ = 0;
161     int64_t fixDelay_ {0};
162     bool isTransitent_ {false};
163     bool isEos_ {false};
164     std::unique_ptr<Task> eosTask_ {nullptr};
165     enum class EosInterruptState : int {
166         NONE,
167         INITIAL,
168         PAUSE,
169         RESUME,
170         STOP,
171     };
172     Mutex eosMutex_ {};
173     std::atomic<bool> eosDraining_ {false};
174     std::atomic<EosInterruptState> eosInterruptType_ {EosInterruptState::NONE};
175     bool isApe_ {false};
176     int64_t playRangeStartTime_ = -1;
177     int64_t playRangeEndTime_ = -1;
178     // vars for audio progress optimization
179     int64_t playingBufferDurationUs_ {0};
180     int64_t lastBufferWriteTime_ {0};
181     bool lastBufferWriteSuccess_ {true};
182     float speed_ {1.0f};
183     std::mutex pluginMutex_;
184     float volume_ {-1.0f};
185     int32_t effectMode_ {-1};
186     bool isMuted_ = false;
187     Mutex amplitudeMutex_ {};
188     float maxAmplitude_ = 0;
189 
190     bool calMaxAmplitudeCbStatus_ = false;
191     UnderrunDetector underrunDetector_;
192     AudioLagDetector lagDetector_;
193     std::atomic<int64_t> seekTimeUs_ {HST_TIME_NONE};
194     PerfRecorder perfRecorder_ {};
195     bool isPerfRecEnabled_ { false };
196     bool isCalledBySystemApp_ { false };
197     bool isLoop_ { false };
198 };
199 }
200 }
201 
202 #endif // HISTREAMER_AUDIO_SINK_H
203