1 /*
2  * Copyright (c) 2022-2022 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 #define HST_LOG_TAG "CodecMode"
17 
18 #include "pipeline/filters/codec/codec_mode.h"
19 #include "foundation/cpp_ext/memory_ext.h"
20 #include "foundation/log.h"
21 #include "foundation/utils/steady_clock.h"
22 #include "pipeline/filters/common/plugin_utils.h"
23 
24 namespace OHOS {
25 namespace Media {
26 namespace Pipeline {
Init(std::shared_ptr<Plugin::Codec> & plugin,std::vector<POutPort> & outPorts)27 bool CodecMode::Init(std::shared_ptr<Plugin::Codec>& plugin, std::vector<POutPort>& outPorts)
28 {
29     if (plugin == nullptr) {
30         MEDIA_LOG_I("plugin is null");
31         return false;
32     }
33     plugin_ = plugin;
34 
35     if (outPorts.empty()) {
36         MEDIA_LOG_I("outPorts is empty");
37         return false;
38     }
39     outPorts_ = outPorts;
40     return true;
41 }
42 
Configure()43 ErrorCode CodecMode::Configure()
44 {
45     FAIL_RETURN_MSG(TranslatePluginStatus(plugin_->Prepare()), "Prepare plugin fail");
46     FAIL_RETURN_MSG(TranslatePluginStatus(plugin_->Start()), "Start plugin fail");
47     return ErrorCode::SUCCESS;
48 }
49 
Prepare()50 ErrorCode CodecMode::Prepare()
51 {
52     return ErrorCode::SUCCESS;
53 }
54 
Release()55 ErrorCode CodecMode::Release()
56 {
57     return ErrorCode::SUCCESS;
58 }
59 
SetBufferPoolSize(uint32_t inBufPoolSize,uint32_t outBufPoolSize)60 void CodecMode::SetBufferPoolSize(uint32_t inBufPoolSize, uint32_t outBufPoolSize)
61 {
62     inBufPoolSize_ = inBufPoolSize;
63     outBufPoolSize_ = outBufPoolSize;
64 }
65 
GetInBufferPoolSize() const66 uint32_t CodecMode::GetInBufferPoolSize() const
67 {
68     return inBufPoolSize_;
69 }
70 
GetOutBufferPoolSize() const71 uint32_t CodecMode::GetOutBufferPoolSize() const
72 {
73     return outBufPoolSize_;
74 }
75 
CreateOutBufferPool(std::shared_ptr<Allocator> & outAllocator,uint32_t bufferCnt,uint32_t bufferSize,Plugin::BufferMetaType bufferMetaType)76 void CodecMode::CreateOutBufferPool(std::shared_ptr<Allocator>& outAllocator,
77                                     uint32_t bufferCnt, uint32_t bufferSize, Plugin::BufferMetaType bufferMetaType)
78 {
79     // 每次重新创建bufferPool
80     outBufPool_ = std::make_shared<BufferPool<AVBuffer>>(bufferCnt);
81     if (outAllocator == nullptr) {
82         MEDIA_LOG_I("Plugin doest not support out allocator, and no allocator negotiated from sink, "
83                     "using framework allocator.");
84         plugin_->SetParameter(Tag::OUTPUT_MEMORY_TYPE, MemoryType::VIRTUAL_ADDR);
85         outBufPool_->Init(bufferSize, bufferMetaType);
86     } else {
87         MEDIA_LOG_I("Using plugin output allocator, outputMemoryType: " PUBLIC_LOG_U8, outAllocator->GetMemoryType());
88         plugin_->SetParameter(Tag::OUTPUT_MEMORY_TYPE, outAllocator->GetMemoryType());
89         for (size_t cnt = 0; cnt < bufferCnt; cnt++) {
90             auto buf = CppExt::make_unique<AVBuffer>(bufferMetaType);
91             if (buf == nullptr || buf->AllocMemory(outAllocator, bufferSize) == nullptr) {
92                 MEDIA_LOG_I("Alloc buffer " PUBLIC_LOG_U32 " failed.", static_cast<uint32_t>(cnt));
93                 continue;
94             }
95             outBufPool_->Append(std::move(buf));
96         }
97     }
98 }
99 } // namespace Pipeline
100 } // namespace Media
101 } // namespace OHOS