• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "codec_mode.h"
19 #include "common/plugin_utils.h"
20 #include "foundation/log.h"
21 #include "foundation/cpp_ext/memory_ext.h"
22 #include "utils/steady_clock.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, using framework allocator");
83         outBufPool_->Init(bufferSize, bufferMetaType);
84     } else {
85         MEDIA_LOG_I("using plugin output allocator");
86         for (size_t cnt = 0; cnt < bufferCnt; cnt++) {
87             auto buf = CppExt::make_unique<AVBuffer>(bufferMetaType);
88             if (buf == nullptr || buf->AllocMemory(outAllocator, bufferSize) == nullptr) {
89                 MEDIA_LOG_I("alloc buffer " PUBLIC_LOG_U32 " fail", static_cast<uint32_t>(cnt));
90                 continue;
91             }
92             outBufPool_->Append(std::move(buf));
93         }
94     }
95 }
96 } // namespace Pipeline
97 } // namespace Media
98 } // namespace OHOS