1 /*
2 * Copyright (c) 2022-2023 Shenzhen Kaihong DID 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 #include "codec_dyna_buffer.h"
16 #include <buffer_handle_utils.h>
17 #include <hdf_base.h>
18 #include <securec.h>
19 #include <unistd.h>
20 #include "codec_log_wrapper.h"
21 #include "v1_0/codec_types.h"
22 using namespace OHOS::HDI::Codec::V1_0;
23 namespace OHOS {
24 namespace Codec {
25 namespace Omx {
CodecDynaBuffer(struct OmxCodecBuffer & codecBuffer,BufferHandle * bufferHandle)26 CodecDynaBuffer::CodecDynaBuffer(struct OmxCodecBuffer &codecBuffer, BufferHandle *bufferHandle)
27 : ICodecBuffer(codecBuffer)
28 {
29 dynaBuffer_ = std::make_shared<DynamicBuffer>();
30 dynaBuffer_->bufferHandle = bufferHandle;
31 }
32
~CodecDynaBuffer()33 CodecDynaBuffer::~CodecDynaBuffer()
34 {
35 dynaBuffer_ = nullptr;
36 }
37
Create(struct OmxCodecBuffer & codecBuffer)38 sptr<ICodecBuffer> CodecDynaBuffer::Create(struct OmxCodecBuffer &codecBuffer)
39 {
40 BufferHandle *bufferHandle = nullptr;
41 if (codecBuffer.bufferhandle) {
42 bufferHandle = codecBuffer.bufferhandle->Move();
43 codecBuffer.bufferhandle = nullptr;
44 }
45 codecBuffer.allocLen = sizeof(DynamicBuffer);
46 CodecDynaBuffer *buffer = new CodecDynaBuffer(codecBuffer, bufferHandle);
47 return sptr<ICodecBuffer>(buffer);
48 }
49
FillOmxBuffer(struct OmxCodecBuffer & codecBuffer,OMX_BUFFERHEADERTYPE & omxBuffer)50 int32_t CodecDynaBuffer::FillOmxBuffer(struct OmxCodecBuffer &codecBuffer, OMX_BUFFERHEADERTYPE &omxBuffer)
51 {
52 CODEC_LOGE("dyna buffer handle is not supported in FillThisBuffer");
53 (void)codecBuffer;
54 (void)omxBuffer;
55 return HDF_ERR_INVALID_PARAM;
56 }
57
EmptyOmxBuffer(struct OmxCodecBuffer & codecBuffer,OMX_BUFFERHEADERTYPE & omxBuffer)58 int32_t CodecDynaBuffer::EmptyOmxBuffer(struct OmxCodecBuffer &codecBuffer, OMX_BUFFERHEADERTYPE &omxBuffer)
59 {
60 if (!CheckInvalid(codecBuffer)) {
61 CODEC_LOGE("CheckInvalid return false");
62 return HDF_ERR_INVALID_PARAM;
63 }
64 ResetBuffer(codecBuffer, omxBuffer);
65
66 int fence = codecBuffer.fenceFd;
67 if (fence >= 0) {
68 auto ret = SyncWait(fence, TIME_WAIT_MS);
69 if (ret != EOK) {
70 CODEC_LOGW("SyncWait ret err");
71 }
72 close(codecBuffer.fenceFd);
73 codecBuffer.fenceFd = -1;
74 }
75
76 return ICodecBuffer::EmptyOmxBuffer(codecBuffer, omxBuffer);
77 }
78
FreeBuffer(struct OmxCodecBuffer & codecBuffer)79 int32_t CodecDynaBuffer::FreeBuffer(struct OmxCodecBuffer &codecBuffer)
80 {
81 if (!CheckInvalid(codecBuffer)) {
82 CODEC_LOGE("shMem_ is null or CheckInvalid return false");
83 return HDF_ERR_INVALID_PARAM;
84 }
85
86 codecBuffer.bufferhandle = nullptr;
87 dynaBuffer_ = nullptr;
88
89 return HDF_SUCCESS;
90 }
91
EmptyOmxBufferDone(OMX_BUFFERHEADERTYPE & omxBuffer)92 int32_t CodecDynaBuffer::EmptyOmxBufferDone(OMX_BUFFERHEADERTYPE &omxBuffer)
93 {
94 return ICodecBuffer::EmptyOmxBufferDone(omxBuffer);
95 }
96
FillOmxBufferDone(OMX_BUFFERHEADERTYPE & omxBuffer)97 int32_t CodecDynaBuffer::FillOmxBufferDone(OMX_BUFFERHEADERTYPE &omxBuffer)
98 {
99 CODEC_LOGE("dyna buffer handle is not supported in FillThisBuffer");
100 (void)omxBuffer;
101 return HDF_ERR_INVALID_PARAM;
102 }
103
GetBuffer()104 uint8_t *CodecDynaBuffer::GetBuffer()
105 {
106 return reinterpret_cast<uint8_t *>(dynaBuffer_.get());
107 }
108
CheckInvalid(struct OmxCodecBuffer & codecBuffer)109 bool CodecDynaBuffer::CheckInvalid(struct OmxCodecBuffer &codecBuffer)
110 {
111 if (!ICodecBuffer::CheckInvalid(codecBuffer) || dynaBuffer_ == nullptr) {
112 CODEC_LOGE("dynaBuffer_ is null or CheckInvalid return false");
113 return false;
114 }
115 return true;
116 }
117
ResetBuffer(struct OmxCodecBuffer & codecBuffer,OMX_BUFFERHEADERTYPE & omxBuffer)118 void CodecDynaBuffer::ResetBuffer(struct OmxCodecBuffer &codecBuffer, OMX_BUFFERHEADERTYPE &omxBuffer)
119 {
120 (void)omxBuffer;
121 if (codecBuffer.bufferhandle == nullptr) {
122 return;
123 }
124 auto bufferHandle = codecBuffer.bufferhandle->Move();
125 codecBuffer.bufferhandle = nullptr;
126
127 // if recv new BufferHandle, save it, but do not need to save to omxBuffer
128 if (dynaBuffer_->bufferHandle != nullptr) {
129 FreeBufferHandle(dynaBuffer_->bufferHandle);
130 }
131 dynaBuffer_->bufferHandle = bufferHandle;
132 codecBuffer.filledLen = sizeof(DynamicBuffer);
133 }
134 } // namespace Omx
135 } // namespace Codec
136 } // namespace OHOS