1 /*
2 * Copyright (C) 2023-2025 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 #define MEDIA_PIPELINE
16
17 #include <malloc.h>
18 #include <map>
19 #include <unistd.h>
20 #include <vector>
21 #include "avcodec_video_decoder.h"
22 #include "avcodec_errors.h"
23 #include "avcodec_trace.h"
24 #include "common/log.h"
25 #include "media_description.h"
26 #include "surface_type.h"
27 #include "buffer/avbuffer_queue_consumer.h"
28 #include "meta/meta_key.h"
29 #include "meta/meta.h"
30 #include "video_decoder_adapter.h"
31 #include "avcodec_sysevent.h"
32 #include "media_core.h"
33
34 namespace {
35 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_SYSTEM_PLAYER, "VideoDecoderAdapter" };
36 }
37
38 namespace OHOS {
39 namespace Media {
40 using namespace MediaAVCodec;
41 using FileType = OHOS::Media::Plugins::FileType;
42 const std::string VIDEO_INPUT_BUFFER_QUEUE_NAME = "VideoDecoderInputBufferQueue";
43
VideoDecoderCallback(std::shared_ptr<VideoDecoderAdapter> videoDecoder)44 VideoDecoderCallback::VideoDecoderCallback(std::shared_ptr<VideoDecoderAdapter> videoDecoder)
45 {
46 MEDIA_LOG_D_SHORT("VideoDecoderCallback instances create.");
47 videoDecoderAdapter_ = videoDecoder;
48 }
49
~VideoDecoderCallback()50 VideoDecoderCallback::~VideoDecoderCallback()
51 {
52 MEDIA_LOG_D_SHORT("~VideoDecoderCallback()");
53 }
54
OnError(MediaAVCodec::AVCodecErrorType errorType,int32_t errorCode)55 void VideoDecoderCallback::OnError(MediaAVCodec::AVCodecErrorType errorType, int32_t errorCode)
56 {
57 if (auto videoDecoderAdapter = videoDecoderAdapter_.lock()) {
58 videoDecoderAdapter->OnError(errorType, errorCode);
59 } else {
60 MEDIA_LOG_I_SHORT("invalid videoDecoderAdapter");
61 }
62 }
63
OnOutputFormatChanged(const MediaAVCodec::Format & format)64 void VideoDecoderCallback::OnOutputFormatChanged(const MediaAVCodec::Format &format)
65 {
66 if (auto videoDecoderAdapter = videoDecoderAdapter_.lock()) {
67 videoDecoderAdapter->OnOutputFormatChanged(format);
68 } else {
69 MEDIA_LOG_I_SHORT("invalid videoDecoderAdapter");
70 }
71 }
72
OnInputBufferAvailable(uint32_t index,std::shared_ptr<AVBuffer> buffer)73 void VideoDecoderCallback::OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer)
74 {
75 if (auto videoDecoderAdapter = videoDecoderAdapter_.lock()) {
76 videoDecoderAdapter->OnInputBufferAvailable(index, buffer);
77 } else {
78 MEDIA_LOG_I_SHORT("invalid videoDecoderAdapter");
79 }
80 }
81
OnOutputBufferAvailable(uint32_t index,std::shared_ptr<AVBuffer> buffer)82 void VideoDecoderCallback::OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer)
83 {
84 if (auto videoDecoderAdapter = videoDecoderAdapter_.lock()) {
85 videoDecoderAdapter->OnOutputBufferAvailable(index, buffer);
86 } else {
87 MEDIA_LOG_I_SHORT("invalid videoDecoderAdapter");
88 }
89 }
90
VideoDecoderAdapter()91 VideoDecoderAdapter::VideoDecoderAdapter()
92 {
93 MEDIA_LOG_D_SHORT("VideoDecoderAdapter instances create.");
94 }
95
~VideoDecoderAdapter()96 VideoDecoderAdapter::~VideoDecoderAdapter()
97 {
98 MEDIA_LOG_I_SHORT("~VideoDecoderAdapter()");
99 FALSE_RETURN_MSG(mediaCodec_ != nullptr, "mediaCodec_ is nullptr");
100 MEDIA_LOG_D("VideoDecoderAdapter::QueueInputBuffer");
101 mediaCodec_->Release();
102 std::unique_lock<std::mutex> lock(dtsQueMutex_);
103 if (!inputBufferDtsQue_.empty()) {
104 MEDIA_LOG_I("Clear dtsQue_, currrent size: " PUBLIC_LOG_U64, static_cast<uint64_t>(inputBufferDtsQue_.size()));
105 inputBufferDtsQue_.clear();
106 }
107 }
108
Init(MediaAVCodec::AVCodecType type,bool isMimeType,const std::string & name)109 Status VideoDecoderAdapter::Init(MediaAVCodec::AVCodecType type, bool isMimeType, const std::string &name)
110 {
111 MEDIA_LOG_I_SHORT("mediaCodec_->Init.");
112
113 Format format;
114 int32_t ret;
115 std::shared_ptr<Media::Meta> callerInfo = std::make_shared<Media::Meta>();
116 callerInfo->SetData(Media::Tag::AV_CODEC_FORWARD_CALLER_PID, appPid_);
117 callerInfo->SetData(Media::Tag::AV_CODEC_FORWARD_CALLER_UID, appUid_);
118 callerInfo->SetData(Media::Tag::AV_CODEC_FORWARD_CALLER_PROCESS_NAME, bundleName_);
119 format.SetMeta(callerInfo);
120 mediaCodecName_ = "";
121 if (isMimeType) {
122 ret = MediaAVCodec::VideoDecoderFactory::CreateByMime(name, format, mediaCodec_);
123 MEDIA_LOG_I_SHORT("VideoDecoderAdapter::Init CreateByMime errorCode %{public}d", ret);
124 } else {
125 ret = MediaAVCodec::VideoDecoderFactory::CreateByName(name, format, mediaCodec_);
126 MEDIA_LOG_I_SHORT("VideoDecoderAdapter::Init CreateByName errorCode %{public}d", ret);
127 }
128
129 FALSE_RETURN_V_MSG(mediaCodec_ != nullptr, Status::ERROR_INVALID_STATE, "mediaCodec_ is nullptr");
130 mediaCodecName_ = name;
131 return Status::OK;
132 }
133
Configure(const Format & format)134 Status VideoDecoderAdapter::Configure(const Format &format)
135 {
136 MEDIA_LOG_I_SHORT("VideoDecoderAdapter->Configure.");
137 std::shared_ptr<Media::Meta> metaInfo = const_cast<Format &>(format).GetMeta();
138 FileType currentFileType = FileType::UNKNOW;
139 if (metaInfo != nullptr && metaInfo->GetData(Media::Tag::MEDIA_FILE_TYPE, currentFileType)) {
140 fileType_ = static_cast<int32_t>(currentFileType);
141 MEDIA_LOG_I("Media file type " PUBLIC_LOG_D32, fileType_);
142 }
143 FALSE_RETURN_V_MSG(mediaCodec_ != nullptr, Status::ERROR_INVALID_STATE, "mediaCodec_ is nullptr");
144 int32_t ret = mediaCodec_->Configure(format);
145 isConfigured_ = ret == AVCodecServiceErrCode::AVCS_ERR_OK;
146 return isConfigured_ ? Status::OK : Status::ERROR_INVALID_DATA;
147 }
148
SetParameter(const Format & format)149 int32_t VideoDecoderAdapter::SetParameter(const Format &format)
150 {
151 MEDIA_LOG_D_SHORT("SetParameter enter.");
152 FALSE_RETURN_V_MSG(mediaCodec_ != nullptr, AVCodecServiceErrCode::AVCS_ERR_INVALID_VAL, "mediaCodec_ is nullptr");
153 return mediaCodec_->SetParameter(format);
154 }
155
Start()156 Status VideoDecoderAdapter::Start()
157 {
158 MEDIA_LOG_I_SHORT("Start enter.");
159 FALSE_RETURN_V_MSG(mediaCodec_ != nullptr, Status::ERROR_INVALID_STATE, "mediaCodec_ is nullptr");
160 FALSE_RETURN_V_MSG(isConfigured_, Status::ERROR_INVALID_STATE, "mediaCodec_ is not configured");
161 int32_t ret = mediaCodec_->Start();
162 if (ret != AVCodecServiceErrCode::AVCS_ERR_OK) {
163 std::string instanceId = std::to_string(instanceId_);
164 struct VideoCodecFaultInfo videoCodecFaultInfo;
165 videoCodecFaultInfo.appName = bundleName_;
166 videoCodecFaultInfo.instanceId = instanceId;
167 videoCodecFaultInfo.callerType = "player_framework";
168 videoCodecFaultInfo.videoCodec = mediaCodecName_;
169 videoCodecFaultInfo.errMsg = "mediaCodec_ start failed";
170 FaultVideoCodecEventWrite(videoCodecFaultInfo);
171 }
172 return ret == AVCodecServiceErrCode::AVCS_ERR_OK ? Status::OK : Status::ERROR_INVALID_STATE;
173 }
174
Stop()175 Status VideoDecoderAdapter::Stop()
176 {
177 MEDIA_LOG_I_SHORT("Stop enter.");
178 FALSE_RETURN_V_MSG(mediaCodec_ != nullptr, Status::ERROR_INVALID_STATE, "mediaCodec_ is nullptr");
179 FALSE_RETURN_V_MSG(isConfigured_, Status::ERROR_INVALID_STATE, "mediaCodec_ is not configured");
180 mediaCodec_->Stop();
181 return Status::OK;
182 }
183
Flush()184 Status VideoDecoderAdapter::Flush()
185 {
186 MEDIA_LOG_I_SHORT("Flush enter.");
187 FALSE_RETURN_V_MSG(mediaCodec_ != nullptr, Status::ERROR_INVALID_STATE, "mediaCodec_ is nullptr");
188 FALSE_RETURN_V_MSG(isConfigured_, Status::ERROR_INVALID_STATE, "mediaCodec_ is not configured");
189 int32_t ret = mediaCodec_->Flush();
190 {
191 std::unique_lock<std::mutex> lock(mutex_);
192 if (inputBufferQueueConsumer_ != nullptr) {
193 for (auto &buffer : bufferVector_) {
194 inputBufferQueueConsumer_->DetachBuffer(buffer);
195 }
196 bufferVector_.clear();
197 inputBufferQueueConsumer_->SetQueueSize(0);
198 }
199 }
200 {
201 std::unique_lock<std::mutex> lock(dtsQueMutex_);
202 if (!inputBufferDtsQue_.empty()) {
203 MEDIA_LOG_I("Clear dtsQue_, currrent size: " PUBLIC_LOG_U64,
204 static_cast<uint64_t>(inputBufferDtsQue_.size()));
205 inputBufferDtsQue_.clear();
206 }
207 }
208 return ret == AVCodecServiceErrCode::AVCS_ERR_OK ? Status::OK : Status::ERROR_INVALID_STATE;
209 }
210
Reset()211 Status VideoDecoderAdapter::Reset()
212 {
213 MEDIA_LOG_I_SHORT("Reset enter.");
214 FALSE_RETURN_V_MSG(mediaCodec_ != nullptr, Status::ERROR_INVALID_STATE, "mediaCodec_ is nullptr");
215 mediaCodec_->Reset();
216 isConfigured_ = false;
217 std::unique_lock<std::mutex> lock(mutex_);
218 if (inputBufferQueueConsumer_ != nullptr) {
219 for (auto &buffer : bufferVector_) {
220 inputBufferQueueConsumer_->DetachBuffer(buffer);
221 }
222 bufferVector_.clear();
223 inputBufferQueueConsumer_->SetQueueSize(0);
224 }
225 return Status::OK;
226 }
227
Release()228 Status VideoDecoderAdapter::Release()
229 {
230 MEDIA_LOG_I_SHORT("Release enter.");
231 FALSE_RETURN_V_MSG(mediaCodec_ != nullptr, Status::ERROR_INVALID_STATE, "mediaCodec_ is nullptr");
232 int32_t ret = mediaCodec_->Release();
233 return ret == AVCodecServiceErrCode::AVCS_ERR_OK ? Status::OK : Status::ERROR_INVALID_STATE;
234 }
235
ResetRenderTime()236 void VideoDecoderAdapter::ResetRenderTime()
237 {
238 currentTime_ = -1;
239 }
240
SetCallback(const std::shared_ptr<MediaAVCodec::MediaCodecCallback> & callback)241 int32_t VideoDecoderAdapter::SetCallback(const std::shared_ptr<MediaAVCodec::MediaCodecCallback> &callback)
242 {
243 MEDIA_LOG_D_SHORT("SetCallback enter.");
244 callback_ = callback;
245 FALSE_RETURN_V_MSG(mediaCodec_ != nullptr, AVCodecServiceErrCode::AVCS_ERR_INVALID_VAL, "mediaCodec_ is nullptr");
246 std::shared_ptr<MediaAVCodec::MediaCodecCallback> mediaCodecCallback
247 = std::make_shared<VideoDecoderCallback>(shared_from_this());
248 return mediaCodec_->SetCallback(mediaCodecCallback);
249 }
250
PrepareInputBufferQueue()251 void VideoDecoderAdapter::PrepareInputBufferQueue()
252 {
253 if (inputBufferQueue_ != nullptr && inputBufferQueue_-> GetQueueSize() > 0) {
254 MEDIA_LOG_W_SHORT("InputBufferQueue already create");
255 return;
256 }
257 inputBufferQueue_ = AVBufferQueue::Create(0,
258 MemoryType::UNKNOWN_MEMORY, VIDEO_INPUT_BUFFER_QUEUE_NAME, true);
259 inputBufferQueueProducer_ = inputBufferQueue_->GetProducer();
260 inputBufferQueueConsumer_ = inputBufferQueue_->GetConsumer();
261 }
262
GetBufferQueueProducer()263 sptr<AVBufferQueueProducer> VideoDecoderAdapter::GetBufferQueueProducer()
264 {
265 return inputBufferQueueProducer_;
266 }
267
GetBufferQueueConsumer()268 sptr<AVBufferQueueConsumer> VideoDecoderAdapter::GetBufferQueueConsumer()
269 {
270 return inputBufferQueueConsumer_;
271 }
272
AquireAvailableInputBuffer()273 void VideoDecoderAdapter::AquireAvailableInputBuffer()
274 {
275 AVCodecTrace trace("VideoDecoderAdapter::AquireAvailableInputBuffer");
276 if (inputBufferQueueConsumer_ == nullptr) {
277 MEDIA_LOG_E_SHORT("inputBufferQueueConsumer_ is null");
278 return;
279 }
280 std::unique_lock<std::mutex> lock(mutex_);
281 std::shared_ptr<AVBuffer> tmpBuffer;
282 if (inputBufferQueueConsumer_->AcquireBuffer(tmpBuffer) == Status::OK) {
283 if (fileType_ == static_cast<int32_t>(FileType::AVI)) {
284 GetInputBufferDts(tmpBuffer);
285 }
286 FALSE_RETURN_MSG(tmpBuffer->meta_ != nullptr, "tmpBuffer is nullptr.");
287 uint32_t index;
288 FALSE_RETURN_MSG(tmpBuffer->meta_->GetData(Tag::REGULAR_TRACK_ID, index), "get index failed.");
289 if (tmpBuffer->flag_ & (uint32_t)(Plugins::AVBufferFlag::EOS)) {
290 tmpBuffer->memory_->SetSize(0);
291 }
292 FALSE_RETURN_MSG(mediaCodec_ != nullptr, "mediaCodec_ is nullptr.");
293 int32_t ret = mediaCodec_->QueueInputBuffer(index);
294 if (ret != ERR_OK) {
295 MEDIA_LOG_E_SHORT("QueueInputBuffer failed, index: %{public}u, bufferid: %{public}" PRIu64
296 ", pts: %{public}" PRIu64", flag: %{public}u, errCode: %{public}d", index, tmpBuffer->GetUniqueId(),
297 tmpBuffer->pts_, tmpBuffer->flag_, ret);
298 if (ret == AVCS_ERR_DECRYPT_FAILED) {
299 eventReceiver_->OnEvent({"video_decoder_adapter", EventType::EVENT_ERROR,
300 MSERR_DRM_VERIFICATION_FAILED});
301 }
302 } else {
303 MEDIA_LOG_D_SHORT("QueueInputBuffer success, index: %{public}u, bufferid: %{public}" PRIu64
304 ", pts: %{public}" PRIu64", flag: %{public}u", index, tmpBuffer->GetUniqueId(),
305 tmpBuffer->pts_, tmpBuffer->flag_);
306 }
307 } else {
308 MEDIA_LOG_E_SHORT("AcquireBuffer failed.");
309 }
310 }
311
GetInputBufferDts(std::shared_ptr<AVBuffer> & inputBuffer)312 void VideoDecoderAdapter::GetInputBufferDts(std::shared_ptr<AVBuffer> &inputBuffer)
313 {
314 FALSE_RETURN_MSG(inputBuffer != nullptr, "inputBuffer is nullptr.");
315 std::unique_lock<std::mutex> lock(dtsQueMutex_);
316 inputBufferDtsQue_.push_back(inputBuffer->dts_);
317 MEDIA_LOG_D("Inputbuffer DTS: " PUBLIC_LOG_D64 " dtsQue_ size: " PUBLIC_LOG_U64,
318 inputBuffer->dts_, static_cast<uint64_t>(inputBufferDtsQue_.size()));
319 }
320
SetOutputBufferPts(std::shared_ptr<AVBuffer> & outputBuffer)321 void VideoDecoderAdapter::SetOutputBufferPts(std::shared_ptr<AVBuffer> &outputBuffer)
322 {
323 FALSE_RETURN_MSG(outputBuffer != nullptr, "outputBuffer is nullptr.");
324 std::unique_lock<std::mutex> lock(dtsQueMutex_);
325 if (!inputBufferDtsQue_.empty()) {
326 outputBuffer->pts_ = inputBufferDtsQue_.front();
327 inputBufferDtsQue_.pop_front();
328 MEDIA_LOG_D("Outputbuffer PTS: " PUBLIC_LOG_D64 " dtsQue_ size: " PUBLIC_LOG_U64,
329 outputBuffer->pts_, static_cast<uint64_t>(inputBufferDtsQue_.size()));
330 } else {
331 MEDIA_LOG_W("DtsQue_ is empty.");
332 outputBuffer->pts_ = outputBuffer->dts_;
333 }
334 }
335
OnInputBufferAvailable(uint32_t index,std::shared_ptr<AVBuffer> buffer)336 void VideoDecoderAdapter::OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer)
337 {
338 AVCodecTrace trace("VideoDecoderAdapter::OnInputBufferAvailable");
339 FALSE_RETURN_MSG(buffer != nullptr && buffer->meta_ != nullptr, "meta_ is nullptr.");
340 buffer->meta_->SetData(Tag::REGULAR_TRACK_ID, index);
341 if (inputBufferQueueConsumer_ == nullptr) {
342 MEDIA_LOG_E_SHORT("inputBufferQueueConsumer_ is null");
343 return;
344 }
345 std::unique_lock<std::mutex> lock(mutex_);
346 if (inputBufferQueueConsumer_->IsBufferInQueue(buffer)) {
347 if (inputBufferQueueConsumer_->ReleaseBuffer(buffer) != Status::OK) {
348 MEDIA_LOG_E_SHORT("IsBufferInQueue ReleaseBuffer failed. index: %{public}u, bufferid: %{public}" PRIu64
349 ", pts: %{public}" PRIu64", flag: %{public}u", index, buffer->GetUniqueId(),
350 buffer->pts_, buffer->flag_);
351 } else {
352 MEDIA_LOG_D_SHORT("IsBufferInQueue ReleaseBuffer success. index: %{public}u, bufferid: %{public}" PRIu64
353 ", pts: %{public}" PRIu64", flag: %{public}u", index, buffer->GetUniqueId(),
354 buffer->pts_, buffer->flag_);
355 }
356 } else {
357 uint32_t size = inputBufferQueueConsumer_->GetQueueSize() + 1;
358 MEDIA_LOG_D_SHORT("AttachBuffer enter. index: %{public}u, size: %{public}u , bufferid: %{public}" PRIu64,
359 index, size, buffer->GetUniqueId());
360 inputBufferQueueConsumer_->SetQueueSizeAndAttachBuffer(size, buffer, false);
361 bufferVector_.push_back(buffer);
362 }
363 }
364
OnError(MediaAVCodec::AVCodecErrorType errorType,int32_t errorCode)365 void VideoDecoderAdapter::OnError(MediaAVCodec::AVCodecErrorType errorType, int32_t errorCode)
366 {
367 FALSE_RETURN_MSG(callback_ != nullptr, "OnError callback_ is nullptr");
368 callback_->OnError(errorType, errorCode);
369 }
370
OnOutputFormatChanged(const MediaAVCodec::Format & format)371 void VideoDecoderAdapter::OnOutputFormatChanged(const MediaAVCodec::Format &format)
372 {
373 FALSE_RETURN_MSG(callback_ != nullptr, "OnOutputFormatChanged callback_ is nullptr");
374 callback_->OnOutputFormatChanged(format);
375 }
376
OnOutputBufferAvailable(uint32_t index,std::shared_ptr<AVBuffer> buffer)377 void VideoDecoderAdapter::OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer)
378 {
379 AVCodecTrace trace("VideoDecoderAdapter::OnOutputBufferAvailable");
380 if (buffer != nullptr) {
381 MEDIA_LOG_D_SHORT("OnOutputBufferAvailable start. index: %{public}u, bufferid: %{public}" PRIu64
382 ", pts: %{public}" PRIu64 ", flag: %{public}u", index, buffer->GetUniqueId(), buffer->pts_, buffer->flag_);
383 } else {
384 MEDIA_LOG_D_SHORT("OnOutputBufferAvailable start. buffer is nullptr, index: %{public}u", index);
385 }
386 FALSE_RETURN_MSG(buffer != nullptr, "buffer is nullptr");
387 if (fileType_ == static_cast<int32_t>(FileType::AVI)) {
388 SetOutputBufferPts(buffer);
389 }
390 FALSE_RETURN_MSG(callback_ != nullptr, "callback_ is nullptr");
391 callback_->OnOutputBufferAvailable(index, buffer);
392 }
393
GetOutputFormat(Format & format)394 int32_t VideoDecoderAdapter::GetOutputFormat(Format &format)
395 {
396 FALSE_RETURN_V_MSG(mediaCodec_ != nullptr, AVCodecServiceErrCode::AVCS_ERR_INVALID_VAL,
397 "GetOutputFormat mediaCodec_ is nullptr");
398 return mediaCodec_->GetOutputFormat(format);
399 }
400
ReleaseOutputBuffer(uint32_t index,bool render)401 int32_t VideoDecoderAdapter::ReleaseOutputBuffer(uint32_t index, bool render)
402 {
403 AVCodecTrace trace("VideoDecoderAdapter::ReleaseOutputBuffer");
404 FALSE_RETURN_V_NOLOG(!isPerfRecEnabled_, ReleaseOutputBufferWithPerfRecord(index, render));
405 mediaCodec_->ReleaseOutputBuffer(index, render);
406 return 0;
407 }
408
SetPerfRecEnabled(bool isPerfRecEnabled)409 Status VideoDecoderAdapter::SetPerfRecEnabled(bool isPerfRecEnabled)
410 {
411 isPerfRecEnabled_ = isPerfRecEnabled;
412 return Status::OK;
413 }
414
ReleaseOutputBufferWithPerfRecord(uint32_t index,bool render)415 int32_t VideoDecoderAdapter::ReleaseOutputBufferWithPerfRecord(uint32_t index, bool render)
416 {
417 int64_t renderTime = CALC_EXPR_TIME_MS(mediaCodec_->ReleaseOutputBuffer(index, render));
418 FALSE_RETURN_V_MSG(eventReceiver_ != nullptr, 0, "Report perf failed, callback is nullptr");
419 FALSE_RETURN_V_NOLOG(perfRecorder_.Record(renderTime) == PerfRecorder::FULL, 0);
420 eventReceiver_->
421 OnDfxEvent({ "VRNDR", DfxEventType::DFX_INFO_PERF_REPORT, perfRecorder_.GetMainPerfData() });
422 perfRecorder_.Reset();
423 return 0;
424 }
425
RenderOutputBufferAtTime(uint32_t index,int64_t renderTimestampNs)426 int32_t VideoDecoderAdapter::RenderOutputBufferAtTime(uint32_t index, int64_t renderTimestampNs)
427 {
428 AVCodecTrace trace("VideoDecoderAdapter::RenderOutputBufferAtTime");
429 MEDIA_LOG_D_SHORT("VideoDecoderAdapter::RenderOutputBufferAtTime");
430 mediaCodec_->RenderOutputBufferAtTime(index, renderTimestampNs);
431 return 0;
432 }
433
SetOutputSurface(sptr<Surface> videoSurface)434 int32_t VideoDecoderAdapter::SetOutputSurface(sptr<Surface> videoSurface)
435 {
436 MEDIA_LOG_I_SHORT("VideoDecoderAdapter::SetOutputSurface");
437 FALSE_RETURN_V_MSG(mediaCodec_ != nullptr, AVCodecServiceErrCode::AVCS_ERR_INVALID_VAL, "mediaCodec_ is nullptr");
438 return mediaCodec_->SetOutputSurface(videoSurface);
439 }
440
SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> & keySession,const bool svpFlag)441 int32_t VideoDecoderAdapter::SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> &keySession,
442 const bool svpFlag)
443 {
444 #ifdef SUPPORT_DRM
445 if (mediaCodec_ == nullptr) {
446 MEDIA_LOG_E_SHORT("mediaCodec_ is nullptr");
447 return AVCodecServiceErrCode::AVCS_ERR_INVALID_VAL;
448 }
449 if (keySession == nullptr) {
450 MEDIA_LOG_E_SHORT("keySession is nullptr");
451 return AVCodecServiceErrCode::AVCS_ERR_INVALID_VAL;
452 }
453 return mediaCodec_->SetDecryptConfig(keySession, svpFlag);
454 #else
455 return 0;
456 #endif
457 }
458
SetEventReceiver(const std::shared_ptr<Pipeline::EventReceiver> & receiver)459 void VideoDecoderAdapter::SetEventReceiver(const std::shared_ptr<Pipeline::EventReceiver> &receiver)
460 {
461 eventReceiver_ = receiver;
462 }
463
SetCallingInfo(int32_t appUid,int32_t appPid,std::string bundleName,uint64_t instanceId)464 void VideoDecoderAdapter::SetCallingInfo(int32_t appUid, int32_t appPid, std::string bundleName, uint64_t instanceId)
465 {
466 appUid_ = appUid;
467 appPid_ = appPid;
468 bundleName_ = bundleName;
469 instanceId_ = instanceId;
470 }
471
OnDumpInfo(int32_t fd)472 void VideoDecoderAdapter::OnDumpInfo(int32_t fd)
473 {
474 MEDIA_LOG_D_SHORT("VideoDecoderAdapter::OnDumpInfo called.");
475 std::string dumpString;
476 dumpString += "VideoDecoderAdapter media codec name is:" + mediaCodecName_ + "\n";
477 if (inputBufferQueue_ != nullptr) {
478 dumpString += "VideoDecoderAdapter buffer size is:" + std::to_string(inputBufferQueue_->GetQueueSize()) + "\n";
479 }
480 if (fd < 0) {
481 MEDIA_LOG_E_SHORT("VideoDecoderAdapter::OnDumpInfo fd is invalid.");
482 return;
483 }
484 int ret = write(fd, dumpString.c_str(), dumpString.size());
485 if (ret < 0) {
486 MEDIA_LOG_E_SHORT("VideoDecoderAdapter::OnDumpInfo write failed.");
487 return;
488 }
489 }
490 } // namespace Media
491 } // namespace OHOS
492