• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "media_codec_decoder_adapter_impl.h"
17 
18 #include <unordered_map>
19 
20 #include "native_avcapability.h"
21 #include "native_buffer.h"
22 #include "external_window.h"
23 #include "native_drm_err.h"
24 #include "native_mediakeysession.h"
25 #include "native_avbuffer.h"
26 #include "native_cencinfo.h"
27 #include "native_avcodec_audiocodec.h"
28 #include <multimedia/native_audio_channel_layout.h>
29 
30 #include "nweb_log.h"
31 #include "buffer_info_adapter_impl.h"
32 #include "ohos_buffer_adapter_impl.h"
33 
34 using namespace OHOS::NWeb;
35 
36 namespace {
37 
38 const std::unordered_map<OH_AVCodecBufferFlags, BufferFlag> BUFFER_FLAG_MAP = {
39     { OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_NONE, BufferFlag::CODEC_BUFFER_FLAG_NONE },
40     { OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS, BufferFlag::CODEC_BUFFER_FLAG_EOS },
41     { OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_SYNC_FRAME, BufferFlag::CODEC_BUFFER_FLAG_SYNC_FRAME },
42     { OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_INCOMPLETE_FRAME,
43         BufferFlag::CODEC_BUFFER_FLAG_PARTIAL_FRAME },
44     { OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_CODEC_DATA, BufferFlag::CODEC_BUFFER_FLAG_CODEC_DATA }
45 };
46 
47 const std::unordered_map<BufferFlag, OH_AVCodecBufferFlags> AV_BUFFER_FLAG_MAP = {
48     { BufferFlag::CODEC_BUFFER_FLAG_NONE, OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_NONE },
49     { BufferFlag::CODEC_BUFFER_FLAG_EOS, OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS },
50     { BufferFlag::CODEC_BUFFER_FLAG_SYNC_FRAME, OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_SYNC_FRAME },
51     { BufferFlag::CODEC_BUFFER_FLAG_PARTIAL_FRAME,
52         OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_INCOMPLETE_FRAME },
53     { BufferFlag::CODEC_BUFFER_FLAG_CODEC_DATA, OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_CODEC_DATA }
54 };
55 } // namespace
56 
~MediaCodecDecoderAdapterImpl()57 MediaCodecDecoderAdapterImpl::~MediaCodecDecoderAdapterImpl()
58 {
59     WVLOG_D("MediaCodecDecoder ~MediaCodecDecoderAdapterImpl");
60     if (decoder_ != nullptr) {
61         ReleaseDecoder();
62     }
63 }
64 
CreateVideoDecoderByMime(const std::string & mimetype)65 DecoderAdapterCode MediaCodecDecoderAdapterImpl::CreateVideoDecoderByMime(const std::string& mimetype)
66 {
67     WVLOG_I("MediaCodecDecoder create decoder by mime[%{public}s], isSecure_ = %{public}d",
68         mimetype.c_str(), static_cast<int32_t>(isSecure_));
69     avCap_ = OH_AVCodec_GetCapability(mimetype.c_str(), false);
70     if (avCap_ == nullptr) {
71         WVLOG_E("MediaCodecDecoder create failed, get capability error.");
72         return DecoderAdapterCode::DECODER_ERROR;
73     }
74     if (isSecure_) {
75         return CreateVideoDecoderByName(mimetype);
76     }
77 
78     decoder_ = OH_VideoDecoder_CreateByMime(mimetype.c_str());
79     if (decoder_ == nullptr) {
80         WVLOG_E("MediaCodecDecoder create decoder failed.");
81         return DecoderAdapterCode::DECODER_ERROR;
82     }
83 
84     return DecoderAdapterCode::DECODER_OK;
85 }
86 
CreateVideoDecoderByName(const std::string & name)87 DecoderAdapterCode MediaCodecDecoderAdapterImpl::CreateVideoDecoderByName(const std::string& name)
88 {
89     WVLOG_I("MediaCodecDecoder create decoder by name[%{public}s], isSecure_ = %{public}d",
90         name.c_str(), static_cast<int32_t>(isSecure_));
91     std::string decoderName = name;
92     avCap_ = OH_AVCodec_GetCapabilityByCategory(name.c_str(), false, HARDWARE);
93     if (avCap_ != nullptr) {
94         decoderName = std::string(OH_AVCapability_GetName(avCap_));
95         if (isSecure_) {
96             decoderName += ".secure";
97         }
98         decoder_ = OH_VideoDecoder_CreateByName(decoderName.c_str());
99         if (decoder_ != nullptr) {
100             isHardwareDecode_ = true;
101             WVLOG_I("MediaCodecDecoder create hardware decoder.");
102             return DecoderAdapterCode::DECODER_OK;
103         }
104     }
105 
106     avCap_ = OH_AVCodec_GetCapabilityByCategory(name.c_str(), false, SOFTWARE);
107     if (avCap_ != nullptr) {
108         decoderName = std::string(OH_AVCapability_GetName(avCap_));
109         if (isSecure_) {
110             decoderName += ".secure";
111         }
112         decoder_ = OH_VideoDecoder_CreateByName(decoderName.c_str());
113         if (decoder_ != nullptr) {
114             isHardwareDecode_ = false;
115             WVLOG_I("MediaCodecDecoder create software decoder.");
116             return DecoderAdapterCode::DECODER_OK;
117         }
118     }
119 
120     WVLOG_E("MediaCodecDecoder create decoder failed.");
121     return DecoderAdapterCode::DECODER_ERROR;
122 }
123 
ConfigureDecoder(const std::shared_ptr<DecoderFormatAdapter> format)124 DecoderAdapterCode MediaCodecDecoderAdapterImpl::ConfigureDecoder(const std::shared_ptr<DecoderFormatAdapter> format)
125 {
126     WVLOG_D("MediaCodecDecoder ConfigureDecoder");
127     if (decoder_ == nullptr) {
128         WVLOG_E("MediaCodecDecoder decoder_ is nullptr.");
129         return DecoderAdapterCode::DECODER_ERROR;
130     }
131 
132     if (format == nullptr) {
133         WVLOG_E("format is nullptr.");
134         return DecoderAdapterCode::DECODER_ERROR;
135     }
136 
137     if (avCap_ != nullptr && !OH_AVCapability_IsVideoSizeSupported(avCap_, format->GetWidth(), format->GetHeight())) {
138         WVLOG_E("video size not support.");
139         return DecoderAdapterCode::DECODER_ERROR;
140     }
141 
142     OH_AVFormat* codecFormat = OH_AVFormat_Create();
143     if (codecFormat == nullptr) {
144         return DecoderAdapterCode::DECODER_ERROR;
145     }
146 
147     OH_AVFormat_SetIntValue(codecFormat, OH_MD_KEY_WIDTH, format->GetWidth());
148     OH_AVFormat_SetIntValue(codecFormat, OH_MD_KEY_HEIGHT, format->GetHeight());
149 
150     OH_AVErrCode ret = OH_VideoDecoder_Configure(decoder_, codecFormat);
151     OH_AVFormat_Destroy(codecFormat);
152     if (ret != OH_AVErrCode::AV_ERR_OK) {
153         WVLOG_E("MediaCodecDecoder OH_VideoDecoder_Configure fail, ret=%{public}u.", static_cast<uint32_t>(ret));
154         return DecoderAdapterCode::DECODER_ERROR;
155     }
156     return DecoderAdapterCode::DECODER_OK;
157 }
158 
SetParameterDecoder(const std::shared_ptr<DecoderFormatAdapter> format)159 DecoderAdapterCode MediaCodecDecoderAdapterImpl::SetParameterDecoder(const std::shared_ptr<DecoderFormatAdapter> format)
160 {
161     WVLOG_D("MediaCodecDecoder SetParameterDecoder");
162     if (decoder_ == nullptr) {
163         WVLOG_E("MediaCodecDecoder decoder_ is nullptr.");
164         return DecoderAdapterCode::DECODER_ERROR;
165     }
166 
167     if (format == nullptr) {
168         WVLOG_E("format is nullptr.");
169         return DecoderAdapterCode::DECODER_ERROR;
170     }
171 
172     OH_AVFormat* codecFormat = OH_AVFormat_Create();
173     if (codecFormat == nullptr) {
174         return DecoderAdapterCode::DECODER_ERROR;
175     }
176 
177     OH_AVFormat_SetIntValue(codecFormat, OH_MD_KEY_WIDTH, format->GetWidth());
178     OH_AVFormat_SetIntValue(codecFormat, OH_MD_KEY_HEIGHT, format->GetHeight());
179 
180     OH_AVErrCode ret = OH_VideoDecoder_SetParameter(decoder_, codecFormat);
181     OH_AVFormat_Destroy(codecFormat);
182     if (ret != OH_AVErrCode::AV_ERR_OK) {
183         WVLOG_E("MediaCodecDecoder OH_VideoDecoder_SetParameter fail, ret=%{public}u.", static_cast<uint32_t>(ret));
184         return DecoderAdapterCode::DECODER_ERROR;
185     }
186     return DecoderAdapterCode::DECODER_OK;
187 }
188 
SetOutputSurface(void * window)189 DecoderAdapterCode MediaCodecDecoderAdapterImpl::SetOutputSurface(void* window)
190 {
191     WVLOG_D("MediaCodecDecoder SetOutputSurface");
192     if (decoder_ == nullptr) {
193         WVLOG_E("MediaCodecDecoder decoder_ is nullptr.");
194         return DecoderAdapterCode::DECODER_ERROR;
195     }
196 
197     OHNativeWindow* window_ = reinterpret_cast<OHNativeWindow*>(window);
198     if (window_ == nullptr) {
199         WVLOG_E("Window is nullptr.");
200         return DecoderAdapterCode::DECODER_ERROR;
201     }
202 
203     if (isHardwareDecode_) {
204         uint64_t usage = NATIVEBUFFER_USAGE_MEM_DMA;
205         OH_NativeWindow_NativeWindowHandleOpt(window_, SET_USAGE, usage);
206         WVLOG_I("MediaCodecDecoder default to opening Hebc.");
207     }
208     OH_AVErrCode ret = OH_VideoDecoder_SetSurface(decoder_, window_);
209     if (ret != OH_AVErrCode::AV_ERR_OK) {
210         WVLOG_E("MediaCodecDecoder OH_VideoDecoder_SetSurface fail, ret=%{public}u.", static_cast<uint32_t>(ret));
211         return DecoderAdapterCode::DECODER_ERROR;
212     }
213     return DecoderAdapterCode::DECODER_OK;
214 }
215 
PrepareDecoder()216 DecoderAdapterCode MediaCodecDecoderAdapterImpl::PrepareDecoder()
217 {
218     WVLOG_D("MediaCodecDecoder PrepareDecoder");
219     if (decoder_ == nullptr) {
220         WVLOG_E("MediaCodecDecoder decoder_ is nullptr.");
221         return DecoderAdapterCode::DECODER_ERROR;
222     }
223 
224     OH_AVErrCode ret = OH_VideoDecoder_Prepare(decoder_);
225     if (ret != OH_AVErrCode::AV_ERR_OK) {
226         WVLOG_E("MediaCodecDecoder OH_VideoDecoder_Prepare fail, ret=%{public}u.", static_cast<uint32_t>(ret));
227         return DecoderAdapterCode::DECODER_ERROR;
228     }
229     return DecoderAdapterCode::DECODER_OK;
230 }
231 
StartDecoder()232 DecoderAdapterCode MediaCodecDecoderAdapterImpl::StartDecoder()
233 {
234     WVLOG_D("MediaCodecDecoder StartDecoder");
235     if (decoder_ == nullptr) {
236         WVLOG_E("MediaCodecDecoder decoder_ is nullptr.");
237         return DecoderAdapterCode::DECODER_ERROR;
238     }
239     OH_AVErrCode ret = OH_VideoDecoder_Start(decoder_);
240     if (ret != OH_AVErrCode::AV_ERR_OK) {
241         WVLOG_E("MediaCodecDecoder OH_VideoDecoder_Start fail, ret=%{public}u.", static_cast<uint32_t>(ret));
242         return DecoderAdapterCode::DECODER_ERROR;
243     }
244     return DecoderAdapterCode::DECODER_OK;
245 }
246 
StopDecoder()247 DecoderAdapterCode MediaCodecDecoderAdapterImpl::StopDecoder()
248 {
249     WVLOG_D("MediaCodecDecoder StopDecoder");
250     if (decoder_ == nullptr) {
251         WVLOG_E("MediaCodecDecoder decoder_ is nullptr.");
252         return DecoderAdapterCode::DECODER_ERROR;
253     }
254     OH_AVErrCode ret = OH_VideoDecoder_Stop(decoder_);
255     if (ret != OH_AVErrCode::AV_ERR_OK) {
256         WVLOG_E("MediaCodecDecoder OH_VideoDecoder_Stop fail, ret=%{public}u.", static_cast<uint32_t>(ret));
257         return DecoderAdapterCode::DECODER_ERROR;
258     }
259 
260     std::unique_lock<std::mutex> lock(bufferMutex_);
261     bufferMap_.clear();
262     return DecoderAdapterCode::DECODER_OK;
263 }
264 
FlushDecoder()265 DecoderAdapterCode MediaCodecDecoderAdapterImpl::FlushDecoder()
266 {
267     WVLOG_D("MediaCodecDecoder FlushDecoder");
268     if (decoder_ == nullptr) {
269         WVLOG_E("MediaCodecDecoder decoder_ is nullptr.");
270         return DecoderAdapterCode::DECODER_ERROR;
271     }
272     OH_AVErrCode ret = OH_VideoDecoder_Flush(decoder_);
273     if (ret != OH_AVErrCode::AV_ERR_OK) {
274         WVLOG_E("MediaCodecDecoder OH_VideoDecoder_Flush fail, ret=%{public}u.", static_cast<uint32_t>(ret));
275         return DecoderAdapterCode::DECODER_ERROR;
276     }
277     std::unique_lock<std::mutex> lock(bufferMutex_);
278     bufferMap_.clear();
279     return DecoderAdapterCode::DECODER_OK;
280 }
281 
ResetDecoder()282 DecoderAdapterCode MediaCodecDecoderAdapterImpl::ResetDecoder()
283 {
284     WVLOG_D("MediaCodecDecoder ResetDecoder");
285     if (decoder_ == nullptr) {
286         WVLOG_E("MediaCodecDecoder decoder_ is nullptr.");
287         return DecoderAdapterCode::DECODER_ERROR;
288     }
289     OH_AVErrCode ret = OH_VideoDecoder_Reset(decoder_);
290     if (ret != OH_AVErrCode::AV_ERR_OK) {
291         WVLOG_E("OH_VideoDecoder_Reset fail, ret=%{public}u.", static_cast<uint32_t>(ret));
292         return DecoderAdapterCode::DECODER_ERROR;
293     }
294     std::unique_lock<std::mutex> lock(bufferMutex_);
295     bufferMap_.clear();
296     return DecoderAdapterCode::DECODER_OK;
297 }
298 
ReleaseDecoder()299 DecoderAdapterCode MediaCodecDecoderAdapterImpl::ReleaseDecoder()
300 {
301     WVLOG_D("MediaCodecDecoder ReleaseDecoder");
302     if (decoder_ == nullptr) {
303         WVLOG_E("MediaCodecDecoder decoder_ is nullptr.");
304         return DecoderAdapterCode::DECODER_ERROR;
305     }
306     OH_AVErrCode ret = OH_VideoDecoder_Destroy(decoder_);
307     decoder_ = nullptr;
308     if (ret != OH_AVErrCode::AV_ERR_OK) {
309         WVLOG_E("MediaCodecDecoder OH_VideoDecoder_Destroy fail, ret=%{public}u.", static_cast<uint32_t>(ret));
310         return DecoderAdapterCode::DECODER_ERROR;
311     }
312     std::unique_lock<std::mutex> lock(bufferMutex_);
313     bufferMap_.clear();
314     return DecoderAdapterCode::DECODER_OK;
315 }
316 
QueueInputBufferDec(uint32_t index,int64_t presentationTimeUs,int32_t size,int32_t offset,BufferFlag flag)317 DecoderAdapterCode MediaCodecDecoderAdapterImpl::QueueInputBufferDec(
318     uint32_t index, int64_t presentationTimeUs, int32_t size, int32_t offset, BufferFlag flag)
319 {
320     WVLOG_D("MediaCodecDecoder QueueInputBufferDec");
321     if (decoder_ == nullptr) {
322         WVLOG_E("MediaCodecDecoder decoder_ is nullptr.");
323         return DecoderAdapterCode::DECODER_ERROR;
324     }
325 
326     OH_AVErrCode ret;
327     OH_AVCodecBufferAttr attr;
328     attr.pts = presentationTimeUs;
329     attr.size = size;
330     attr.offset = offset;
331     attr.flags = MediaCodecDecoderAdapterImpl::GetAVBufferFlag(flag);
332 
333     {
334         std::unique_lock<std::mutex> lock(bufferMutex_);
335         if (bufferMap_.find(index) == bufferMap_.end()) {
336             WVLOG_E("MediaCodecDecoder QueueInputBufferDec not find index.");
337             return DecoderAdapterCode::DECODER_ERROR;
338         }
339         ret = OH_AVBuffer_SetBufferAttr(bufferMap_[index], &attr);
340     }
341 
342     if (ret != OH_AVErrCode::AV_ERR_OK) {
343         WVLOG_E("MediaCodecDecoder OH_AVBuffer_SetBufferAttr fail, ret=%{public}u.", static_cast<uint32_t>(ret));
344         return DecoderAdapterCode::DECODER_ERROR;
345     }
346     ret = OH_VideoDecoder_PushInputBuffer(decoder_, index);
347     if (ret != OH_AVErrCode::AV_ERR_OK) {
348         WVLOG_E("MediaCodecDecoder OH_VideoDecoder_PushInputBuffer fail, ret=%{public}u.", static_cast<uint32_t>(ret));
349         return DecoderAdapterCode::DECODER_ERROR;
350     }
351     return DecoderAdapterCode::DECODER_OK;
352 }
353 
GetOutputFormatDec(std::shared_ptr<DecoderFormatAdapter> format)354 DecoderAdapterCode MediaCodecDecoderAdapterImpl::GetOutputFormatDec(std::shared_ptr<DecoderFormatAdapter> format)
355 {
356     WVLOG_D("MediaCodecDecoder GetOutputFormatDec");
357     if (decoder_ == nullptr) {
358         WVLOG_E("MediaCodecDecoder decoder_ is nullptr.");
359         return DecoderAdapterCode::DECODER_ERROR;
360     }
361 
362     if (format == nullptr) {
363         WVLOG_E("format is nullptr.");
364         return DecoderAdapterCode::DECODER_ERROR;
365     }
366 
367     OH_AVFormat* codecFormat = OH_VideoDecoder_GetOutputDescription(decoder_);
368     if (codecFormat == nullptr) {
369         return DecoderAdapterCode::DECODER_ERROR;
370     }
371 
372     int32_t width = 0;
373     int32_t height = 0;
374     OH_AVFormat_GetIntValue(codecFormat, OH_MD_KEY_VIDEO_PIC_WIDTH, &width);
375     OH_AVFormat_GetIntValue(codecFormat, OH_MD_KEY_VIDEO_PIC_HEIGHT, &height);
376     format->SetWidth(width);
377     format->SetHeight(height);
378     OH_AVFormat_Destroy(codecFormat);
379 
380     return DecoderAdapterCode::DECODER_OK;
381 }
382 
ReleaseOutputBufferDec(uint32_t index,bool isRender)383 DecoderAdapterCode MediaCodecDecoderAdapterImpl::ReleaseOutputBufferDec(uint32_t index, bool isRender)
384 {
385     WVLOG_D("MediaCodecDecoder ReleaseOutputBufferDec, isRender = %{public}d", isRender);
386     if (decoder_ == nullptr) {
387         WVLOG_E("MediaCodecDecoder decoder_ is nullptr.");
388         return DecoderAdapterCode::DECODER_ERROR;
389     }
390 
391     OH_AVErrCode ret;
392     if (isRender) {
393         // Display the buffer data and release the buffer.
394         ret = OH_VideoDecoder_RenderOutputBuffer(decoder_, index);
395     } else {
396         // Only release the buffer.
397         ret = OH_VideoDecoder_FreeOutputBuffer(decoder_, index);
398     }
399     if (ret != OH_AVErrCode::AV_ERR_OK) {
400         WVLOG_E("MediaCodecDecoder release buffer[%{public}u] fail.", index);
401         return DecoderAdapterCode::DECODER_ERROR;
402     }
403     std::unique_lock<std::mutex> lock(bufferMutex_);
404     bufferMap_.erase(index);
405     return DecoderAdapterCode::DECODER_OK;
406 }
407 
SetCallbackDec(const std::shared_ptr<DecoderCallbackAdapter> callback)408 DecoderAdapterCode MediaCodecDecoderAdapterImpl::SetCallbackDec(const std::shared_ptr<DecoderCallbackAdapter> callback)
409 {
410     WVLOG_D("MediaCodecDecoder SetCallbackDec");
411     if (decoder_ == nullptr) {
412         WVLOG_E("MediaCodecDecoder decoder_ is nullptr.");
413         return DecoderAdapterCode::DECODER_ERROR;
414     }
415 
416     if (callback == nullptr) {
417         WVLOG_E("Media Callback is NULL.");
418         return DecoderAdapterCode::DECODER_ERROR;
419     }
420 
421     callback_ = callback;
422     struct OH_AVCodecCallback cb;
423     cb.onError = [] (OH_AVCodec *codec, int32_t errorCode, void *userData) {
424         (void)codec;
425         static_cast<MediaCodecDecoderAdapterImpl*>(userData)->OnError(errorCode);
426     };
427     cb.onStreamChanged = [] (OH_AVCodec *codec, OH_AVFormat *format, void *userData) {
428         (void)codec;
429         static_cast<MediaCodecDecoderAdapterImpl*>(userData)->OnOutputFormatChanged(format);
430     };
431     cb.onNeedInputBuffer = [] (OH_AVCodec *codec, uint32_t index, OH_AVBuffer *buffer, void *userData) {
432         (void)codec;
433         static_cast<MediaCodecDecoderAdapterImpl*>(userData)->OnInputBufferAvailable(index, buffer);
434     };
435     cb.onNewOutputBuffer = [] (OH_AVCodec *codec, uint32_t index, OH_AVBuffer *buffer, void *userData) {
436         (void)codec;
437         static_cast<MediaCodecDecoderAdapterImpl*>(userData)->OnOutputBufferAvailable(index, buffer);
438     };
439 
440     OH_AVErrCode ret = OH_VideoDecoder_RegisterCallback(decoder_, cb, this);
441     if (ret != OH_AVErrCode::AV_ERR_OK) {
442         return DecoderAdapterCode::DECODER_ERROR;
443     }
444 
445     return DecoderAdapterCode::DECODER_OK;
446 }
447 
GetBufferFlag(OH_AVCodecBufferFlags codecBufferFlag)448 BufferFlag MediaCodecDecoderAdapterImpl::GetBufferFlag(OH_AVCodecBufferFlags codecBufferFlag)
449 {
450     auto flag = BUFFER_FLAG_MAP.find(codecBufferFlag);
451     if (flag == BUFFER_FLAG_MAP.end()) {
452         WVLOG_E("buffer flag not found.");
453         return BufferFlag::CODEC_BUFFER_FLAG_NONE;
454     }
455     return flag->second;
456 }
457 
GetAVBufferFlag(BufferFlag bufferFlag)458 OH_AVCodecBufferFlags MediaCodecDecoderAdapterImpl::GetAVBufferFlag(BufferFlag bufferFlag)
459 {
460     auto flag = AV_BUFFER_FLAG_MAP.find(bufferFlag);
461     if (flag == AV_BUFFER_FLAG_MAP.end()) {
462         WVLOG_E("buffer flag not found.");
463         return OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_NONE;
464     }
465     return flag->second;
466 }
467 
OnError(int32_t errorCode)468 void MediaCodecDecoderAdapterImpl::OnError(int32_t errorCode)
469 {
470     WVLOG_D("MediaCodecDecoder OnError");
471     if (!callback_) {
472         WVLOG_E("callback is NULL.");
473         return;
474     }
475 
476     callback_->OnError(ErrorType::CODEC_ERROR_INTERNAL, errorCode);
477 }
478 
OnOutputFormatChanged(OH_AVFormat * format)479 void MediaCodecDecoderAdapterImpl::OnOutputFormatChanged(OH_AVFormat* format)
480 {
481     WVLOG_D("MediaCodecDecoder OnOutputFormatChanged");
482     if (!callback_) {
483         WVLOG_E("callback is NULL.");
484         return;
485     }
486 
487     int32_t width = 0;
488     int32_t height = 0;
489     OH_AVFormat_GetIntValue(format, OH_MD_KEY_WIDTH, &width);
490     OH_AVFormat_GetIntValue(format, OH_MD_KEY_HEIGHT, &height);
491     callback_->OnStreamChanged(width, height, 0);
492 }
493 
OnInputBufferAvailable(uint32_t index,OH_AVBuffer * buffer)494 void MediaCodecDecoderAdapterImpl::OnInputBufferAvailable(uint32_t index, OH_AVBuffer* buffer)
495 {
496     WVLOG_D("MediaCodecDecoder OnInputBufferAvailable");
497     if (!callback_) {
498         WVLOG_E("callback is NULL.");
499         return;
500     }
501 
502     if (buffer == nullptr || OH_AVBuffer_GetAddr(buffer) == nullptr) {
503         return;
504     }
505 
506     std::shared_ptr<OhosBufferAdapterImpl> ohosBuffer = std::make_shared<OhosBufferAdapterImpl>();
507     if (ohosBuffer == nullptr) {
508         WVLOG_E("new OhosBufferAdapterImpl failed");
509         return;
510     }
511 
512     {
513         std::unique_lock<std::mutex> lock(bufferMutex_);
514         bufferMap_[index] = buffer;
515     }
516 
517     ohosBuffer->SetAddr(OH_AVBuffer_GetAddr(buffer));
518     ohosBuffer->SetBufferSize(OH_AVBuffer_GetCapacity(buffer));
519     callback_->OnNeedInputData(index, ohosBuffer);
520 }
521 
OnOutputBufferAvailable(uint32_t index,OH_AVBuffer * buffer)522 void MediaCodecDecoderAdapterImpl::OnOutputBufferAvailable(uint32_t index, OH_AVBuffer* buffer)
523 {
524     WVLOG_D("MediaCodecDecoder OnOutputBufferAvailable");
525     if (!callback_) {
526         WVLOG_E("callback is NULL.");
527         return;
528     }
529 
530     OH_AVCodecBufferAttr attr;
531     OH_AVErrCode ret = OH_AVBuffer_GetBufferAttr(buffer, &attr);
532     if (ret != OH_AVErrCode::AV_ERR_OK) {
533         return;
534     }
535 
536     std::shared_ptr<BufferInfoAdapterImpl> info_ = std::make_shared<BufferInfoAdapterImpl>();
537     if (!info_) {
538         WVLOG_E("new BufferInfoAdapterImpl failed");
539         return;
540     }
541 
542     info_->SetPresentationTimeUs(attr.pts);
543     info_->SetSize(attr.size);
544     info_->SetOffset(attr.offset);
545 
546     BufferFlag bufferFlag_;
547 
548     bufferFlag_ = MediaCodecDecoderAdapterImpl::GetBufferFlag(static_cast<OH_AVCodecBufferFlags>(attr.flags));
549     callback_->OnNeedOutputData(index, info_, bufferFlag_);
550 }
551 
SetAVCencInfoStruct(OH_AVCencInfo * avCencInfo,const std::shared_ptr<AudioCencInfoAdapter> cencInfo)552 DecoderAdapterCode MediaCodecDecoderAdapterImpl::SetAVCencInfoStruct(
553     OH_AVCencInfo *avCencInfo, const std::shared_ptr<AudioCencInfoAdapter> cencInfo)
554 {
555     OH_AVErrCode errNo = OH_AVCencInfo_SetAlgorithm(avCencInfo, static_cast<DrmCencAlgorithm>(cencInfo->GetAlgo()));
556     if (errNo != AV_ERR_OK) {
557         WVLOG_E("MediaCodecDecoder set AVCencInfo Algorithm fail, errNo = %{public}u", static_cast<uint32_t>(errNo));
558         return DecoderAdapterCode::DECODER_ERROR;
559     }
560 
561     errNo = OH_AVCencInfo_SetKeyIdAndIv(
562         avCencInfo, cencInfo->GetKeyId(), cencInfo->GetKeyIdLen(), cencInfo->GetIv(), cencInfo->GetIvLen());
563     if (errNo != AV_ERR_OK) {
564         WVLOG_E("MediaCodecDecoder set AVCencInfo keyid and iv fail, errNo = %{public}u",
565             static_cast<uint32_t>(errNo));
566         return DecoderAdapterCode::DECODER_ERROR;
567     }
568 
569     DrmSubsample subSamples[cencInfo->GetClearHeaderLens().size()];
570     for (uint32_t i = 0; i < cencInfo->GetClearHeaderLens().size(); i++) {
571         subSamples[i].clearHeaderLen = cencInfo->GetClearHeaderLens()[i];
572         subSamples[i].payLoadLen = cencInfo->GetPayLoadLens()[i];
573     }
574     errNo = OH_AVCencInfo_SetSubsampleInfo(
575         avCencInfo, cencInfo->GetEncryptedBlockCount(), cencInfo->GetSkippedBlockCount(),
576         cencInfo->GetFirstEncryptedOffset(), cencInfo->GetClearHeaderLens().size(), subSamples);
577     if (errNo != AV_ERR_OK) {
578         WVLOG_E("MediaCodecDecoder set AVCencInfo subsampleInfo fail, errNo = %{public}u",
579             static_cast<uint32_t>(errNo));
580         return DecoderAdapterCode::DECODER_ERROR;
581     }
582 
583     errNo = OH_AVCencInfo_SetMode(avCencInfo, DRM_CENC_INFO_KEY_IV_SUBSAMPLES_SET);
584     if (errNo != AV_ERR_OK) {
585         WVLOG_E("MediaCodecDecoder set AVCencInfo mode fail, errNo = %{public}u", static_cast<uint32_t>(errNo));
586         return DecoderAdapterCode::DECODER_ERROR;
587     }
588     return DecoderAdapterCode::DECODER_OK;
589 }
590 
SetDecryptionConfig(void * session,bool isSecure)591 DecoderAdapterCode MediaCodecDecoderAdapterImpl::SetDecryptionConfig(void *session, bool isSecure)
592 {
593     WVLOG_I("%{public}s, isSecure = %{public}u, session = %{public}d.",
594         __FUNCTION__, static_cast<uint32_t>(isSecure), session ? 1 : 0);
595     isSecure_ = isSecure;
596     if (session == nullptr) {
597         return DecoderAdapterCode::DECODER_OK;
598     }
599     OH_AVErrCode errCode = OH_VideoDecoder_SetDecryptionConfig(
600         decoder_, static_cast<MediaKeySession *>(session), isSecure_);
601     if (errCode != AV_ERR_OK) {
602         WVLOG_E("MediaCodecDecoder OH_VideoDecoder_SetDecryptionConfig fail, errCode = %{public}u.", uint32_t(errCode));
603         return DecoderAdapterCode::DECODER_ERROR;
604     }
605     return DecoderAdapterCode::DECODER_OK;
606 }
607 
SetAVCencInfo(uint32_t index,const std::shared_ptr<AudioCencInfoAdapter> cencInfo)608 DecoderAdapterCode MediaCodecDecoderAdapterImpl::SetAVCencInfo(
609     uint32_t index, const std::shared_ptr<AudioCencInfoAdapter> cencInfo)
610 {
611     if (cencInfo == nullptr) {
612         WVLOG_E("cencInfo is nullptr.");
613         return DecoderAdapterCode::DECODER_ERROR;
614     }
615     OH_AVErrCode ret;
616     OH_AVCencInfo *avCencInfo = OH_AVCencInfo_Create();
617     if (avCencInfo == nullptr) {
618         WVLOG_E("creat avCencInfo fail.");
619         return DecoderAdapterCode::DECODER_ERROR;
620     }
621 
622     if (SetAVCencInfoStruct(avCencInfo, cencInfo) != DecoderAdapterCode::DECODER_OK) {
623         return DecoderAdapterCode::DECODER_ERROR;
624     }
625 
626     {
627         std::unique_lock<std::mutex> lock(bufferMutex_);
628         if (bufferMap_.find(index) == bufferMap_.end()) {
629             WVLOG_E("MediaCodecDecoder QueueInputBufferDec not find index.");
630             return DecoderAdapterCode::DECODER_ERROR;
631         }
632         ret = OH_AVCencInfo_SetAVBuffer(avCencInfo, bufferMap_[index]);
633     }
634 
635     if (ret != AV_ERR_OK) {
636         WVLOG_E("OH_AVCencInfo_SetAVBuffer fail, ret=%{public}u.", static_cast<uint32_t>(ret));
637         return DecoderAdapterCode::DECODER_ERROR;
638     }
639     ret = OH_AVCencInfo_Destroy(avCencInfo);
640     if (ret != AV_ERR_OK) {
641         WVLOG_E("OH_AVCencInfo_Destroy fail, ret=%{public}u.", static_cast<uint32_t>(ret));
642         return DecoderAdapterCode::DECODER_ERROR;
643     }
644 
645     return DecoderAdapterCode::DECODER_OK;
646 }
647