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
16 #include "codec_handle_buffer.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 {
CodecHandleBuffer(struct OmxCodecBuffer & codecBuffer,BufferHandle * bufferHandle)26 CodecHandleBuffer::CodecHandleBuffer(struct OmxCodecBuffer &codecBuffer, BufferHandle *bufferHandle)
27 : ICodecBuffer(codecBuffer), bufferHandle_(bufferHandle)
28 {}
29
~CodecHandleBuffer()30 CodecHandleBuffer::~CodecHandleBuffer()
31 {
32 if (bufferHandle_ != nullptr) {
33 FreeBufferHandle(bufferHandle_);
34 bufferHandle_ = nullptr;
35 }
36 }
37
Create(struct OmxCodecBuffer & codecBuffer)38 sptr<ICodecBuffer> CodecHandleBuffer::Create(struct OmxCodecBuffer &codecBuffer)
39 {
40 if (codecBuffer.bufferhandle == nullptr) {
41 CODEC_LOGE("bufferHandle is null");
42 return nullptr;
43 }
44 BufferHandle *bufferHandle = codecBuffer.bufferhandle->Move();
45 codecBuffer.bufferhandle = nullptr;
46 CodecHandleBuffer *buffer = new CodecHandleBuffer(codecBuffer, bufferHandle);
47 return sptr<ICodecBuffer>(buffer);
48 }
49
FillOmxBuffer(struct OmxCodecBuffer & codecBuffer,OMX_BUFFERHEADERTYPE & omxBuffer)50 int32_t CodecHandleBuffer::FillOmxBuffer(struct OmxCodecBuffer &codecBuffer, OMX_BUFFERHEADERTYPE &omxBuffer)
51 {
52 if (!CheckInvalid(codecBuffer)) {
53 CODEC_LOGE("CheckInvalid return false or mem has no right to write ");
54 return HDF_ERR_INVALID_PARAM;
55 }
56 ResetBuffer(codecBuffer, omxBuffer);
57
58 int fenceFd = codecBuffer.fenceFd;
59 if (fenceFd >= 0) {
60 auto ret = SyncWait(fenceFd, TIME_WAIT_MS);
61 if (ret != EOK) {
62 CODEC_LOGE("SyncWait ret err [%{public}d]", ret);
63 }
64 close(codecBuffer.fenceFd);
65 codecBuffer.fenceFd = -1;
66 }
67 return ICodecBuffer::FillOmxBuffer(codecBuffer, omxBuffer);
68 }
69
EmptyOmxBuffer(struct OmxCodecBuffer & codecBuffer,OMX_BUFFERHEADERTYPE & omxBuffer)70 int32_t CodecHandleBuffer::EmptyOmxBuffer(struct OmxCodecBuffer &codecBuffer, OMX_BUFFERHEADERTYPE &omxBuffer)
71 {
72 CODEC_LOGE("bufferHandle is not support in EmptyThisBuffer");
73 (void)codecBuffer;
74 (void)omxBuffer;
75 return HDF_ERR_INVALID_PARAM;
76 }
77
FreeBuffer(struct OmxCodecBuffer & codecBuffer)78 int32_t CodecHandleBuffer::FreeBuffer(struct OmxCodecBuffer &codecBuffer)
79 {
80 if (!CheckInvalid(codecBuffer)) {
81 CODEC_LOGE("shMem_ is null or CheckInvalid return false");
82 return HDF_ERR_INVALID_PARAM;
83 }
84 codecBuffer.bufferhandle = nullptr;
85 if (bufferHandle_ != nullptr) {
86 FreeBufferHandle(bufferHandle_);
87 bufferHandle_ = nullptr;
88 }
89
90 return HDF_SUCCESS;
91 }
92
EmptyOmxBufferDone(OMX_BUFFERHEADERTYPE & omxBuffer)93 int32_t CodecHandleBuffer::EmptyOmxBufferDone(OMX_BUFFERHEADERTYPE &omxBuffer)
94 {
95 CODEC_LOGE("bufferHandle is not support in EmptyThisBuffer");
96 (void)omxBuffer;
97 return HDF_ERR_INVALID_PARAM;
98 }
99
FillOmxBufferDone(OMX_BUFFERHEADERTYPE & omxBuffer)100 int32_t CodecHandleBuffer::FillOmxBufferDone(OMX_BUFFERHEADERTYPE &omxBuffer)
101 {
102 return ICodecBuffer::FillOmxBufferDone(omxBuffer);
103 }
104
GetBuffer()105 uint8_t *CodecHandleBuffer::GetBuffer()
106 {
107 return reinterpret_cast<uint8_t *>(bufferHandle_);
108 }
109
CheckInvalid(struct OmxCodecBuffer & codecBuffer)110 bool CodecHandleBuffer::CheckInvalid(struct OmxCodecBuffer &codecBuffer)
111 {
112 if (!ICodecBuffer::CheckInvalid(codecBuffer) || bufferHandle_ == nullptr) {
113 CODEC_LOGE("bufferHandle_ is null or CheckInvalid return false");
114 return false;
115 }
116 return true;
117 }
118
ResetBuffer(struct OmxCodecBuffer & codecBuffer,OMX_BUFFERHEADERTYPE & omxBuffer)119 void CodecHandleBuffer::ResetBuffer(struct OmxCodecBuffer &codecBuffer, OMX_BUFFERHEADERTYPE &omxBuffer)
120 {
121 if (codecBuffer.bufferhandle == nullptr) {
122 return;
123 }
124 auto bufferHandle = codecBuffer.bufferhandle->Move();
125 codecBuffer.bufferhandle = nullptr;
126 // if recv new BufferHandle, save it, and save the new bufferhandle to omxbuffer
127 if (bufferHandle == nullptr) {
128 return;
129 }
130 if (bufferHandle_ != nullptr) {
131 FreeBufferHandle(bufferHandle_);
132 }
133 bufferHandle_ = bufferHandle;
134 omxBuffer.pBuffer = reinterpret_cast<uint8_t *>(bufferHandle_);
135 }
136 } // namespace Omx
137 } // namespace Codec
138 } // namespace OHOS