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_FILTER_CODEC_BASE_H
17 #define HISTREAMER_PIPELINE_FILTER_CODEC_BASE_H
18 
19 #include <string>
20 #include "foundation/osal/thread/task.h"
21 #include "foundation/utils/blocking_queue.h"
22 #include "foundation/utils/buffer_pool.h"
23 #include "pipeline/core/filter_base.h"
24 #include "pipeline/core/type_define.h"
25 #include "pipeline/filters/codec/codec_mode.h"
26 #include "pipeline/filters/common/plugin_utils.h"
27 #include "plugin/common/plugin_tags.h"
28 #include "plugin/core/codec.h"
29 #include "plugin/core/plugin_info.h"
30 
31 namespace OHOS {
32 namespace Media {
33 namespace Pipeline {
34 class CodecFilterBase : public FilterBase, public Plugin::DataCallbackHelper {
35 public:
36     explicit CodecFilterBase(const std::string &name);
37     ~CodecFilterBase() override;
38 
39     ErrorCode Start() override;
40 
41     ErrorCode Stop() override;
42 
43     ErrorCode Prepare() override;
44 
45     ErrorCode SetParameter(int32_t key, const Plugin::Any& value) override;
46 
47     ErrorCode GetParameter(int32_t key, Plugin::Any& outVal) override;
48 
49     bool Negotiate(const std::string& inPort,
50                    const std::shared_ptr<const Plugin::Capability>& upstreamCap,
51                    Plugin::Capability& negotiatedCap,
52                    const Plugin::Meta& upstreamParams,
53                    Plugin::Meta& downstreamParams) override;
54 
55     bool Configure(const std::string& inPort, const std::shared_ptr<const Plugin::Meta>& upstreamMeta,
56                    Plugin::Meta& upstreamParams, Plugin::Meta& downstreamParams) override;
57 
58     void FlushStart() override;
59 
60     void FlushEnd() override;
61 
62     ErrorCode PushData(const std::string& inPort, const AVBufferPtr& buffer, int64_t offset) override;
63 
64 protected:
65     virtual uint32_t GetOutBufferPoolSize();
66 
67     virtual uint32_t CalculateBufferSize(const std::shared_ptr<const Plugin::Meta>& meta);
68 
69     virtual Plugin::Meta GetNegotiateParams(const Plugin::Meta& upstreamParams);
70 
71     bool CheckRequiredOutCapKeys(const Capability& capability);
72 
73     virtual std::vector<Capability::Key> GetRequiredOutCapKeys();
74 
75     virtual ErrorCode ConfigureToStartPluginLocked(const std::shared_ptr<const Plugin::Meta>& meta);
76 
77     ErrorCode UpdateMetaFromPlugin(Plugin::Meta& meta);
78 
79     ErrorCode SetPluginParameterLocked(Tag tag, const Plugin::ValueType& value);
80 
81     virtual void UpdateParams(const std::shared_ptr<const Plugin::Meta>& upMeta,
82                               std::shared_ptr<Plugin::Meta>& meta);
83 
84     template<typename T>
GetPluginParameterLocked(Tag tag,T & value)85     ErrorCode GetPluginParameterLocked(Tag tag, T& value)
86     {
87         Plugin::Any tmp;
88         auto err = TranslatePluginStatus(plugin_->GetParameter(tag, tmp));
89         if (err == ErrorCode::SUCCESS && Plugin::Any::IsSameTypeWith<T>(tmp)) {
90             value = Plugin::AnyCast<T>(tmp);
91         }
92         return err;
93     }
94 
95     Plugin::Meta sinkParams_ {};
96     std::shared_ptr<Plugin::Codec> plugin_ {};
97     Plugin::BufferMetaType bufferMetaType_ = {};
98     std::shared_ptr<CodecMode> codecMode_ {nullptr};
99     Plugin::PluginType pluginType_ {};
100     Plugin::CodecMode preferredCodecMode_ {Plugin::CodecMode::HARDWARE};
101 private:
102     std::atomic<bool> isFlushing_ {false};
103     virtual std::shared_ptr<Allocator> GetAllocator();
104     Capability capNegWithDownstream_ {};
105 };
106 } // namespace Pipeline
107 } // namespace Media
108 } // namespace OHOS
109 #endif // HISTREAMER_PIPELINE_FILTER_CODEC_BASE_H
110