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_VIDEO_SINK_FILTER_H
17 #define MEDIA_PIPELINE_VIDEO_SINK_FILTER_H
18 
19 #ifdef VIDEO_SUPPORT
20 
21 #include <atomic>
22 #if !defined(OHOS_LITE) && defined(VIDEO_SUPPORT)
23 #include "refbase.h"
24 #include "surface/surface.h"
25 #endif
26 #include "foundation/osal/thread/condition_variable.h"
27 #include "foundation/osal/thread/mutex.h"
28 #include "foundation/osal/thread/task.h"
29 #include "foundation/utils/blocking_queue.h"
30 #include "pipeline/core/error_code.h"
31 #include "pipeline/core/filter_base.h"
32 #include "pipeline/filters/sink/media_synchronous_sink.h"
33 #include "plugin/core/plugin_info.h"
34 #include "plugin/core/video_sink.h"
35 
36 namespace OHOS {
37 namespace Media {
38 namespace Pipeline {
39 class VideoSinkFilter : public MediaSynchronousSink {
40 public:
41     explicit VideoSinkFilter(const std::string& name);
42     ~VideoSinkFilter() override;
43 
44     void Init(EventReceiver* receiver, FilterCallback* callback) override;
45 
46     ErrorCode SetParameter(int32_t key, const Plugin::Any& value) override;
47 
48     ErrorCode GetParameter(int32_t key, Plugin::Any& value) override;
49 
50     bool Negotiate(const std::string& inPort,
51                    const std::shared_ptr<const Plugin::Capability>& upstreamCap,
52                    Plugin::Capability& negotiatedCap,
53                    const Plugin::Meta& upstreamParams,
54                    Plugin::Meta& downstreamParams) override;
55 
56     bool Configure(const std::string& inPort, const std::shared_ptr<const Plugin::Meta>& upstreamMeta,
57                    Plugin::Meta& upstreamParams, Plugin::Meta& downstreamParams) override;
58 
59     /**
60      *
61      * @param inPort
62      * @param buffer
63      * @param offset always ignore this parameter
64      * @return
65      */
66     ErrorCode PushData(const std::string& inPort, const AVBufferPtr& buffer, int64_t offset) override;
67 
68     ErrorCode Start() override;
69     ErrorCode Stop() override;
70 
71     ErrorCode Pause() override;
72     ErrorCode Resume() override;
73 
74     void FlushStart() override;
75     void FlushEnd() override;
76 
77 #if !defined(OHOS_LITE) && defined(VIDEO_SUPPORT)
78     ErrorCode SetVideoSurface(sptr<Surface> surface);
79 #endif
80 
81 protected:
82     ErrorCode DoSyncWrite(const AVBufferPtr &buffer) override;
83 
84     void ResetSyncInfo() override;
85 
86 private:
87     ErrorCode ConfigurePluginParams(const std::shared_ptr<const Plugin::Meta>& meta);
88     ErrorCode ConfigurePluginToStartNoLocked(const std::shared_ptr<const Plugin::Meta>& meta);
89     bool CreateVideoSinkPlugin(const std::shared_ptr<Plugin::PluginInfo>& selectedPluginInfo);
90     void HandleNegotiateParams(const Plugin::Meta& upstreamParams, Plugin::Meta& downstreamParams);
91     void RenderFrame();
92     bool CheckBufferLatenessMayWait(AVBufferPtr buffer);
93     std::shared_ptr<OHOS::Media::BlockingQueue<AVBufferPtr>> inBufQueue_ {nullptr};
94     std::shared_ptr<OHOS::Media::OSAL::Task> renderTask_ {nullptr};
95     std::atomic<bool> pushThreadIsBlocking_ {false};
96     std::atomic<bool> isFlushing_ {false};
97     OSAL::ConditionVariable startWorkingCondition_ {};
98     OSAL::Mutex mutex_;
99 #ifndef OHOS_LITE
100     sptr<Surface> surface_;
101 #endif
102     std::shared_ptr<Plugin::VideoSink> plugin_ {nullptr};
103     int64_t refreshTime_ {0};
104     bool isFirstFrame_ {true};
105     uint32_t frameRate_ {0};
106     bool forceRenderNextFrame_ {false};
107     Plugin::VideoScaleType videoScaleType_ {Plugin::VideoScaleType::VIDEO_SCALE_TYPE_FIT};
108 
109     void CalcFrameRate();
110     std::shared_ptr<OHOS::Media::OSAL::Task> frameRateTask_ {nullptr};
111     std::atomic<uint64_t> renderFrameCnt_ {0};
112     std::atomic<uint64_t> discardFrameCnt_ {0};
113 };
114 } // namespace Pipeline
115 } // namespace Media
116 } // namespace OHOS
117 
118 #endif
119 #endif // MEDIA_PIPELINE_VIDEO_SINK_FILTER_H