1 /*
2  * Copyright (c) 2021-2021 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 MEDIA_PIPELINE_AUDIO_SINK_FILTER_H
17 #define MEDIA_PIPELINE_AUDIO_SINK_FILTER_H
18 
19 #include <atomic>
20 
21 #include "foundation/osal/thread/condition_variable.h"
22 #include "foundation/osal/thread/mutex.h"
23 #include "pipeline/core/error_code.h"
24 #include "pipeline/core/filter_base.h"
25 #include "pipeline/filters/sink/media_synchronous_sink.h"
26 #include "plugin/core/audio_sink.h"
27 #include "plugin/core/plugin_info.h"
28 
29 namespace OHOS {
30 namespace Media {
31 namespace Pipeline {
32 class AudioSinkFilter : public std::enable_shared_from_this<AudioSinkFilter>, public MediaSynchronousSink {
33 public:
34     explicit AudioSinkFilter(const std::string& name);
35     ~AudioSinkFilter() override;
36 
37     void Init(EventReceiver* receiver, FilterCallback* callback) override;
38 
39     ErrorCode SetParameter(int32_t key, const Plugin::Any& value) override;
40 
41     ErrorCode GetParameter(int32_t key, Plugin::Any& value) override;
42 
43     bool Negotiate(const std::string& inPort,
44                    const std::shared_ptr<const Plugin::Capability>& upstreamCap,
45                    Plugin::Capability& negotiatedCap,
46                    const Plugin::Meta& upstreamParams,
47                    Plugin::Meta& downstreamParams) override;
48 
49     bool Configure(const std::string& inPort, const std::shared_ptr<const Plugin::Meta>& upstreamMeta,
50                    Plugin::Meta& upstreamParams, Plugin::Meta& downstreamParams) override;
51 
52     /**
53      *
54      * @param inPort
55      * @param buffer
56      * @param offset always ignore this parameter
57      * @return
58      */
59     ErrorCode PushData(const std::string& inPort, const AVBufferPtr& buffer, int64_t offset) override;
60 
61     ErrorCode Start() override;
62     ErrorCode Stop() override;
63 
64     ErrorCode Pause() override;
65     ErrorCode Resume() override;
66 
67     void FlushStart() override;
68     void FlushEnd() override;
69     ErrorCode SetVolume(float volume);
70 
71 protected:
72     ErrorCode DoSyncWrite(const AVBufferPtr& buffer) override;
73 
74     void ResetSyncInfo() override;
75 
76 private:
77     ErrorCode SetPluginParameter(Tag tag, const Plugin::ValueType& value);
78     ErrorCode ConfigureToPreparePlugin(const std::shared_ptr<const Plugin::Meta>& meta);
79     ErrorCode SetVolumeToPlugin();
80     void OnEvent(const Plugin::PluginEvent& event) override;
81 
82     int64_t latestBufferPts_ {HST_TIME_NONE};
83     int64_t latestBufferDuration_ {0};
84     int64_t frameCnt_ {0};
85     int32_t appPid_ {};
86     int32_t appUid_ {};
87     Plugin::AudioRenderInfo audioRenderInfo_ {};
88     Plugin::AudioInterruptMode audioInterruptMode_ {Plugin::AudioInterruptMode::SHARE_MODE};
89     Plugin::Seekable seekable_ {Plugin::Seekable::INVALID};
90 
91     std::atomic<bool> pushThreadIsBlocking {false};
92     bool isFlushing {false};
93     OSAL::ConditionVariable startWorkingCondition_ {};
94     OSAL::Mutex mutex_ {};
95 
96     std::shared_ptr<Plugin::AudioSink> plugin_ {};
97     float volume_ {-1.0f}; // default invalid value
98     int64_t reportAnchorDuration_ {0};
99     int64_t lastReportedClockTime_ {HST_TIME_NONE};
100 
101     bool forceUpdateTimeAnchorNextTime_ {false};
102 };
103 } // namespace Pipeline
104 } // namespace Media
105 } // namespace OHOS
106 #endif