• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 "common/status.h"
19 #include "meta/meta.h"
20 #include "sink/media_synchronous_sink.h"
21 #include "media_sync_manager.h"
22 #include "buffer/avbuffer_queue.h"
23 #include "buffer/avbuffer_queue_define.h"
24 #include "plugin/audio_sink_plugin.h"
25 #include "filter/filter.h"
26 #include "common/log.h"
27 #include "plugin/plugin_manager.h"
28 #include "plugin/plugin_time.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> GetInputBufferQueue();
39     Status SetParameter(const std::shared_ptr<Meta>& meta);
40     Status GetParameter(std::shared_ptr<Meta>& meta);
41     Status Prepare();
42     Status Start();
43     Status Stop();
44     Status Pause();
45     Status Resume();
46     Status Flush();
47     Status Release();
48     Status SetVolume(float volume);
49     void DrainOutputBuffer();
50     void SetEventReceiver(const std::shared_ptr<Pipeline::EventReceiver>& receiver);
51     Status GetLatency(uint64_t& nanoSec);
52     void SetSyncCenter(std::shared_ptr<Pipeline::MediaSyncManager> syncCenter);
53     bool DoSyncWrite(const std::shared_ptr<OHOS::Media::AVBuffer>& buffer) override;
54     void ResetSyncInfo() override;
55     Status SetSpeed(float speed);
56     Status SetAudioEffectMode(int32_t effectMode);
57     Status GetAudioEffectMode(int32_t &effectMode);
58     int32_t SetVolumeWithRamp(float targetVolume, int32_t duration);
59     Status SetIsTransitent(bool isTransitent);
60     class AVBufferAvailableListener : public IConsumerListener {
61     public:
AVBufferAvailableListener(std::shared_ptr<AudioSink> audioSink)62         AVBufferAvailableListener(std::shared_ptr<AudioSink> audioSink)
63         {
64             audioSink_ = audioSink;
65         }
66 
OnBufferAvailable()67         void OnBufferAvailable() override
68         {
69             if (auto sink = audioSink_.lock()) {
70                 sink->DrainOutputBuffer();
71             } else {
72                 MEDIA_LOG_I("invalid audioSink");
73             }
74         }
75     private:
76         std::weak_ptr<AudioSink> audioSink_;
77     };
78     static const int64_t kMinAudioClockUpdatePeriodUs = 20 * HST_USECOND;
79 
80     static const int64_t kMaxAllowedAudioSinkDelayUs = 1500 * HST_MSECOND;
81 protected:
82     std::atomic<OHOS::Media::Pipeline::FilterState> state_;
83 private:
84     Status PrepareInputBufferQueue();
85     std::shared_ptr<Plugins::AudioSinkPlugin> CreatePlugin(std::shared_ptr<Meta> meta);
86     bool OnNewAudioMediaTime(int64_t mediaTimeUs);
87     int64_t getPendingAudioPlayoutDurationUs(int64_t nowUs);
88     int64_t getDurationUsPlayedAtSampleRate(uint32_t numFrames);
89     std::shared_ptr<Plugins::AudioSinkPlugin> plugin_ {};
90     std::shared_ptr<Pipeline::EventReceiver> playerEventReceiver_;
91     int32_t appUid_{0};
92     int32_t appPid_{0};
93     int64_t numFramesWritten_ {0};
94     int64_t firstAudioAnchorTimeMediaUs_ {HST_TIME_NONE};
95     int64_t nextAudioClockUpdateTimeUs_ {HST_TIME_NONE};
96     int64_t lastReportedClockTime_ {HST_TIME_NONE};
97     int64_t latestBufferPts_ {HST_TIME_NONE};
98     int64_t latestBufferDuration_ {0};
99     bool forceUpdateTimeAnchorNextTime_ {false};
100     const std::string INPUT_BUFFER_QUEUE_NAME = "AudioSinkInputBufferQueue";
101     std::shared_ptr<AVBufferQueue> inputBufferQueue_;
102     sptr<AVBufferQueueProducer> inputBufferQueueProducer_;
103     sptr<AVBufferQueueConsumer> inputBufferQueueConsumer_;
104     int64_t firstPts_ {HST_TIME_NONE};
105     int32_t sampleRate_ {0};
106     int32_t samplePerFrame_ {0};
107     int64_t fixDelay_ {0};
108     bool isTransitent_ {false};
109     bool isEos_ {false};
110 };
111 }
112 }
113 
114 #endif // HISTREAMER_AUDIO_SINK_H
115