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 HISTREAMER_PIPELINE_VIDEO_ENCODER_FILTER_H
17 #define HISTREAMER_PIPELINE_VIDEO_ENCODER_FILTER_H
18 
19 #if defined(RECORDER_SUPPORT) && defined(VIDEO_SUPPORT)
20 
21 #include "pipeline/core/type_define.h"
22 #include "pipeline/filters/codec/codec_filter_base.h"
23 #include "plugin/common/plugin_tags.h"
24 
25 namespace OHOS {
26 namespace Media {
27 namespace Pipeline {
28 class VideoEncoderFilter : public CodecFilterBase {
29 public:
30     explicit VideoEncoderFilter(const std::string& name);
31     ~VideoEncoderFilter() override;
32 
33     virtual ErrorCode SetVideoEncoder(int32_t sourceId, std::shared_ptr<Plugin::Meta> encoderMeta);
34 
35     ErrorCode Prepare() override;
36 
37     ErrorCode Start() override;
38 
39     ErrorCode Stop() override;
40 
41     void FlushStart() override;
42 
43     void FlushEnd() override;
44 
45     bool Negotiate(const std::string& inPort,
46                    const std::shared_ptr<const Plugin::Capability>& upstreamCap,
47                    Plugin::Capability& negotiatedCap,
48                    const Plugin::Meta& upstreamParams,
49                    Plugin::Meta& downstreamParams) override;
50 
51     bool Configure(const std::string& inPort, const std::shared_ptr<const Plugin::Meta>& upstreamMeta,
52                    Plugin::Meta& upstreamParams, Plugin::Meta& downstreamParams) override;
53 
54     /**
55      *
56      * @param inPort
57      * @param buffer
58      * @param offset always ignore this parameter
59      * @return
60      */
61     ErrorCode PushData(const std::string& inPort, const AVBufferPtr& buffer, int64_t offset) override;
62 
63     void OnInputBufferDone(const std::shared_ptr<Plugin::Buffer>& buffer) override;
64 
65     void OnOutputBufferDone(const std::shared_ptr<Plugin::Buffer>& buffer) override;
66 
67 private:
68     class DataCallbackImpl;
69 
70     struct VideoEncoderFormat {
71         std::string mime;
72         uint32_t width;
73         uint32_t height;
74         int64_t bitRate;
75         uint32_t frameRate;
76         Plugin::VideoPixelFormat format;
77         Plugin::VideoH264Profile profile;
78         uint32_t level;
79         Plugin::CodecConfig codecConfig;
80     };
81 
82     ErrorCode SetVideoEncoderFormat(const std::shared_ptr<const Plugin::Meta>& meta);
83 
84     ErrorCode AllocateOutputBuffers();
85 
86     ErrorCode ConfigurePluginOutputBuffers();
87 
88     ErrorCode ConfigurePluginParams();
89 
90     ErrorCode ConfigurePlugin();
91 
92     ErrorCode ConfigureNoLocked(const std::shared_ptr<const Plugin::Meta>& meta);
93 
94     void HandleFrame();
95 
96     void HandleOneFrame(const std::shared_ptr<AVBuffer>& data);
97 
98     void FinishFrame();
99 
100     uint32_t CalculateBufferSize(const std::shared_ptr<const Plugin::Meta>& meta) override;
101 
102     bool isFlushing_ {false};
103     bool isStop_ {false};
104     Capability capNegWithDownstream_;
105     Capability capNegWithUpstream_;
106     VideoEncoderFormat vencFormat_;
107     std::shared_ptr<Plugin::Meta> codecMeta_ {nullptr};
108     DataCallbackImpl* dataCallback_ {nullptr};
109 
110     std::shared_ptr<OHOS::Media::OSAL::Task> handleFrameTask_ {nullptr};
111     std::shared_ptr<OHOS::Media::OSAL::Task> pushTask_ {nullptr};
112     std::shared_ptr<BufferPool<AVBuffer>> outBufPool_ {nullptr};
113     std::shared_ptr<OHOS::Media::BlockingQueue<AVBufferPtr>> inBufQue_ {nullptr};
114     std::shared_ptr<OHOS::Media::BlockingQueue<AVBufferPtr>> outBufQue_ {nullptr};
115 };
116 }
117 }
118 }
119 #endif
120 #endif // HISTREAMER_PIPELINE_VIDEO_ENCODER_FILTER_H
121