1 /*
2 * Copyright (C) 2023 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 #include "avcodec_video_decoder_impl.h"
17 #include "avcodec_trace.h"
18 #include "avcodec_errors.h"
19 #include "avcodec_log.h"
20 #include "i_avcodec_service.h"
21
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVCodecVideoDecoderImpl"};
24 }
25
26 namespace OHOS {
27 namespace MediaAVCodec {
CreateByMime(const std::string & mime)28 std::shared_ptr<AVCodecVideoDecoder> VideoDecoderFactory::CreateByMime(const std::string &mime)
29 {
30 AVCODEC_SYNC_TRACE;
31
32 std::shared_ptr<AVCodecVideoDecoderImpl> impl = std::make_shared<AVCodecVideoDecoderImpl>();
33
34 int32_t ret = impl->Init(AVCODEC_TYPE_VIDEO_DECODER, true, mime);
35 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "AVCodec video decoder impl init failed, %{public}s",
36 AVCSErrorToString(static_cast<AVCodecServiceErrCode>(ret)).c_str());
37
38 return impl;
39 }
40
CreateByName(const std::string & name)41 std::shared_ptr<AVCodecVideoDecoder> VideoDecoderFactory::CreateByName(const std::string &name)
42 {
43 AVCODEC_SYNC_TRACE;
44
45 std::shared_ptr<AVCodecVideoDecoderImpl> impl = std::make_shared<AVCodecVideoDecoderImpl>();
46
47 int32_t ret = impl->Init(AVCODEC_TYPE_VIDEO_DECODER, false, name);
48 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "AVCodec video decoder impl init failed, %{public}s",
49 AVCSErrorToString(static_cast<AVCodecServiceErrCode>(ret)).c_str());
50
51 return impl;
52 }
53
Init(AVCodecType type,bool isMimeType,const std::string & name)54 int32_t AVCodecVideoDecoderImpl::Init(AVCodecType type, bool isMimeType, const std::string &name)
55 {
56 AVCODEC_SYNC_TRACE;
57 codecService_ = AVCodecServiceFactory::GetInstance().CreateCodecService();
58 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "Codec service create failed");
59
60 return codecService_->Init(type, isMimeType, name);
61 }
62
AVCodecVideoDecoderImpl()63 AVCodecVideoDecoderImpl::AVCodecVideoDecoderImpl()
64 {
65 AVCODEC_LOGD("AVCodecVideoDecoderImpl:0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
66 }
67
~AVCodecVideoDecoderImpl()68 AVCodecVideoDecoderImpl::~AVCodecVideoDecoderImpl()
69 {
70 if (codecService_ != nullptr) {
71 (void)AVCodecServiceFactory::GetInstance().DestroyCodecService(codecService_);
72 codecService_ = nullptr;
73 }
74 AVCODEC_LOGD("AVCodecVideoDecoderImpl:0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
75 }
76
Configure(const Format & format)77 int32_t AVCodecVideoDecoderImpl::Configure(const Format &format)
78 {
79 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
80
81 AVCODEC_SYNC_TRACE;
82 return codecService_->Configure(format);
83 }
84
Prepare()85 int32_t AVCodecVideoDecoderImpl::Prepare()
86 {
87 return AVCS_ERR_OK;
88 }
89
Start()90 int32_t AVCodecVideoDecoderImpl::Start()
91 {
92 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
93
94 AVCODEC_SYNC_TRACE;
95 return codecService_->Start();
96 }
97
Stop()98 int32_t AVCodecVideoDecoderImpl::Stop()
99 {
100 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
101
102 AVCODEC_SYNC_TRACE;
103 return codecService_->Stop();
104 }
105
Flush()106 int32_t AVCodecVideoDecoderImpl::Flush()
107 {
108 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
109
110 AVCODEC_SYNC_TRACE;
111 return codecService_->Flush();
112 }
113
Reset()114 int32_t AVCodecVideoDecoderImpl::Reset()
115 {
116 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
117
118 AVCODEC_SYNC_TRACE;
119 return codecService_->Reset();
120 }
121
Release()122 int32_t AVCodecVideoDecoderImpl::Release()
123 {
124 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
125
126 AVCODEC_SYNC_TRACE;
127 return codecService_->Release();
128 }
129
SetOutputSurface(sptr<Surface> surface)130 int32_t AVCodecVideoDecoderImpl::SetOutputSurface(sptr<Surface> surface)
131 {
132 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
133
134 AVCODEC_SYNC_TRACE;
135 return codecService_->SetOutputSurface(surface);
136 }
137
QueueInputBuffer(uint32_t index,AVCodecBufferInfo info,AVCodecBufferFlag flag)138 int32_t AVCodecVideoDecoderImpl::QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag)
139 {
140 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
141 CHECK_AND_RETURN_RET_LOG(cbFlag == CallbackFlag::MEMORY_CALLBACK, AVCS_ERR_INVALID_STATE,
142 "The callback of AVSharedMemory is invalid!");
143
144 AVCODEC_SYNC_TRACE;
145 return codecService_->QueueInputBuffer(index, info, flag);
146 }
147
QueueInputBuffer(uint32_t index)148 int32_t AVCodecVideoDecoderImpl::QueueInputBuffer(uint32_t index)
149 {
150 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
151 CHECK_AND_RETURN_RET_LOG(cbFlag == CallbackFlag::BUFFER_CALLBACK, AVCS_ERR_INVALID_STATE,
152 "The callback of AVBuffer is invalid!");
153
154 AVCODEC_SYNC_TRACE;
155 return codecService_->QueueInputBuffer(index);
156 }
157
GetOutputFormat(Format & format)158 int32_t AVCodecVideoDecoderImpl::GetOutputFormat(Format &format)
159 {
160 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
161
162 AVCODEC_SYNC_TRACE;
163 return codecService_->GetOutputFormat(format);
164 }
165
ReleaseOutputBuffer(uint32_t index,bool render)166 int32_t AVCodecVideoDecoderImpl::ReleaseOutputBuffer(uint32_t index, bool render)
167 {
168 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
169 CHECK_AND_RETURN_RET_LOG(cbFlag != CallbackFlag::INVALID_CALLBACK, AVCS_ERR_INVALID_STATE,
170 "The callback is invalid!");
171 AVCODEC_SYNC_TRACE;
172 return codecService_->ReleaseOutputBuffer(index, render);
173 }
174
SetParameter(const Format & format)175 int32_t AVCodecVideoDecoderImpl::SetParameter(const Format &format)
176 {
177 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
178
179 AVCODEC_SYNC_TRACE;
180 return codecService_->SetParameter(format);
181 }
182
SetCallback(const std::shared_ptr<AVCodecCallback> & callback)183 int32_t AVCodecVideoDecoderImpl::SetCallback(const std::shared_ptr<AVCodecCallback> &callback)
184 {
185 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
186 CHECK_AND_RETURN_RET_LOG(callback != nullptr, AVCS_ERR_INVALID_VAL, "Callback is nullptr");
187 CHECK_AND_RETURN_RET_LOG(cbFlag == CallbackFlag::MEMORY_CALLBACK || cbFlag == CallbackFlag::INVALID_CALLBACK,
188 AVCS_ERR_INVALID_STATE, "The callback of AVBuffer is already set!");
189 cbFlag = CallbackFlag::MEMORY_CALLBACK;
190
191 AVCODEC_SYNC_TRACE;
192 return codecService_->SetCallback(callback);
193 }
194
SetCallback(const std::shared_ptr<MediaCodecCallback> & callback)195 int32_t AVCodecVideoDecoderImpl::SetCallback(const std::shared_ptr<MediaCodecCallback> &callback)
196 {
197 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr, AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
198 CHECK_AND_RETURN_RET_LOG(callback != nullptr, AVCS_ERR_INVALID_VAL, "Callback is nullptr");
199 CHECK_AND_RETURN_RET_LOG(cbFlag == CallbackFlag::BUFFER_CALLBACK || cbFlag == CallbackFlag::INVALID_CALLBACK,
200 AVCS_ERR_INVALID_STATE, "The callback of AVSharedMemory is already set!");
201 cbFlag = CallbackFlag::BUFFER_CALLBACK;
202
203 AVCODEC_SYNC_TRACE;
204 return codecService_->SetCallback(callback);
205 }
206
207 #ifdef SUPPORT_DRM
SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> & keySessionProxy,const bool svpFlag)208 int32_t AVCodecVideoDecoderImpl::SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> &keySessionProxy,
209 const bool svpFlag)
210 {
211 AVCODEC_LOGI("AVCodecVideoDecoderImpl SetDecryptConfig proxy");
212 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
213 AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
214 CHECK_AND_RETURN_RET_LOG(keySessionProxy != nullptr,
215 AVCS_ERR_INVALID_OPERATION, "keySessionProxy is nullptr");
216
217 AVCODEC_SYNC_TRACE;
218 return codecService_->SetDecryptConfig(keySessionProxy, svpFlag);
219 }
220 #endif
221 } // namespace MediaAVCodec
222 } // namespace OHOS
223