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 "native_avbuffer.h"
17 #include <shared_mutex>
18 #include "common/log.h"
19 #include "common/native_mfmagic.h"
20 #include "meta/meta.h"
21 #include "native_window.h"
22
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_FOUNDATION, "NaiveAVBuffer" };
25 }
26
27 using namespace OHOS;
28 using namespace OHOS::Media;
29
OH_AVBuffer_Create(int32_t capacity)30 OH_AVBuffer *OH_AVBuffer_Create(int32_t capacity)
31 {
32 FALSE_RETURN_V_MSG_E(capacity > 0, nullptr, "capacity %{public}d is error!", capacity);
33 auto allocator = AVAllocatorFactory::CreateSharedAllocator(MemoryFlag::MEMORY_READ_WRITE);
34 FALSE_RETURN_V_MSG_E(allocator != nullptr, nullptr, "create allocator failed");
35
36 std::shared_ptr<AVBuffer> buffer = AVBuffer::CreateAVBuffer(allocator, capacity);
37 FALSE_RETURN_V_MSG_E(buffer != nullptr, nullptr, "create OH_AVBuffer failed");
38 FALSE_RETURN_V_MSG_E(buffer->memory_ != nullptr, nullptr, "memory is nullptr");
39 FALSE_RETURN_V_MSG_E(buffer->memory_->GetAddr() != nullptr, nullptr, "memory's addr is nullptr");
40
41 struct OH_AVBuffer *buf = new (std::nothrow) OH_AVBuffer(buffer);
42 FALSE_RETURN_V_MSG_E(buffer != nullptr, nullptr, "failed to new OH_AVBuffer");
43 buf->isUserCreated = true;
44 return buf;
45 }
46
OH_AVBuffer_Destroy(struct OH_AVBuffer * buffer)47 OH_AVErrCode OH_AVBuffer_Destroy(struct OH_AVBuffer *buffer)
48 {
49 FALSE_RETURN_V_MSG_E(buffer != nullptr, AV_ERR_INVALID_VAL, "input buffer is nullptr!");
50 FALSE_RETURN_V_MSG_E(buffer->magic_ == MFMagic::MFMAGIC_AVBUFFER, AV_ERR_INVALID_VAL, "magic error!");
51 FALSE_RETURN_V_MSG_E(buffer->isUserCreated, AV_ERR_OPERATE_NOT_PERMIT, "input buffer is not user created!");
52 delete buffer;
53 return AV_ERR_OK;
54 }
55
OH_AVBuffer_GetBufferAttr(OH_AVBuffer * buffer,OH_AVCodecBufferAttr * attr)56 OH_AVErrCode OH_AVBuffer_GetBufferAttr(OH_AVBuffer *buffer, OH_AVCodecBufferAttr *attr)
57 {
58 FALSE_RETURN_V_MSG_E(buffer != nullptr, AV_ERR_INVALID_VAL, "input buffer is nullptr!");
59 FALSE_RETURN_V_MSG_E(buffer->magic_ == MFMagic::MFMAGIC_AVBUFFER, AV_ERR_INVALID_VAL, "magic error!");
60 FALSE_RETURN_V_MSG_E(buffer->buffer_ != nullptr, AV_ERR_INVALID_VAL, "buffer is nullptr!");
61 FALSE_RETURN_V_MSG_E(attr != nullptr, AV_ERR_INVALID_VAL, "attr is nullptr!");
62 attr->pts = buffer->buffer_->pts_;
63 attr->flags = static_cast<uint32_t>(buffer->buffer_->flag_);
64 if (buffer->buffer_->memory_ != nullptr) {
65 attr->offset = buffer->buffer_->memory_->GetOffset();
66 attr->size = buffer->buffer_->memory_->GetSize();
67 } else {
68 attr->offset = 0;
69 attr->size = 0;
70 }
71 return AV_ERR_OK;
72 }
73
OH_AVBuffer_SetBufferAttr(OH_AVBuffer * buffer,const OH_AVCodecBufferAttr * attr)74 OH_AVErrCode OH_AVBuffer_SetBufferAttr(OH_AVBuffer *buffer, const OH_AVCodecBufferAttr *attr)
75 {
76 FALSE_RETURN_V_MSG_E(buffer != nullptr, AV_ERR_INVALID_VAL, "input buffer is nullptr!");
77 FALSE_RETURN_V_MSG_E(buffer->magic_ == MFMagic::MFMAGIC_AVBUFFER, AV_ERR_INVALID_VAL, "magic error!");
78 FALSE_RETURN_V_MSG_E(buffer->buffer_ != nullptr, AV_ERR_INVALID_VAL, "buffer is nullptr!");
79 FALSE_RETURN_V_MSG_E(attr != nullptr, AV_ERR_INVALID_VAL, "attr is nullptr!");
80 buffer->buffer_->pts_ = attr->pts;
81 buffer->buffer_->flag_ = attr->flags;
82
83 if (buffer->buffer_->memory_ != nullptr) {
84 Status ret = buffer->buffer_->memory_->SetSize(attr->size);
85 FALSE_RETURN_V_MSG_E(ret == Status::OK, AV_ERR_INVALID_VAL, "size is invalid!");
86
87 ret = buffer->buffer_->memory_->SetOffset(attr->offset);
88 FALSE_RETURN_V_MSG_E(ret == Status::OK, AV_ERR_INVALID_VAL, "offset is invalid!");
89 }
90 return AV_ERR_OK;
91 }
92
OH_AVBuffer_GetParameter(OH_AVBuffer * buffer)93 OH_AVFormat *OH_AVBuffer_GetParameter(OH_AVBuffer *buffer)
94 {
95 FALSE_RETURN_V_MSG_E(buffer != nullptr, nullptr, "input buffer is nullptr!");
96 FALSE_RETURN_V_MSG_E(buffer->magic_ == MFMagic::MFMAGIC_AVBUFFER, nullptr, "magic error!");
97 FALSE_RETURN_V_MSG_E(buffer->buffer_ != nullptr, nullptr, "buffer is nullptr!");
98 FALSE_RETURN_V_MSG_E(buffer->buffer_->meta_ != nullptr, nullptr, "buffer's meta is nullptr!");
99
100 OH_AVFormat *avFormat = OH_AVFormat_Create();
101 if (!avFormat->format_.SetMeta(buffer->buffer_->meta_)) {
102 MEDIA_LOG_E("set meta failed!");
103 OH_AVFormat_Destroy(avFormat);
104 avFormat = nullptr;
105 }
106 return avFormat;
107 }
108
OH_AVBuffer_SetParameter(OH_AVBuffer * buffer,const OH_AVFormat * format)109 OH_AVErrCode OH_AVBuffer_SetParameter(OH_AVBuffer *buffer, const OH_AVFormat *format)
110 {
111 FALSE_RETURN_V_MSG_E(buffer != nullptr, AV_ERR_INVALID_VAL, "input buffer is nullptr!");
112 FALSE_RETURN_V_MSG_E(buffer->magic_ == MFMagic::MFMAGIC_AVBUFFER, AV_ERR_INVALID_VAL, "magic error!");
113 FALSE_RETURN_V_MSG_E(buffer->buffer_ != nullptr, AV_ERR_INVALID_VAL, "buffer is nullptr!");
114 FALSE_RETURN_V_MSG_E(format != nullptr, AV_ERR_INVALID_VAL, "input format is nullptr!");
115 FALSE_RETURN_V_MSG_E(format->magic_ == MFMagic::MFMAGIC_FORMAT, AV_ERR_INVALID_VAL, "magic error!");
116
117 auto formatRef = const_cast<OH_AVFormat *>(format);
118 std::shared_ptr<Meta> meta = formatRef->format_.GetMeta();
119 FALSE_RETURN_V_MSG_E(meta != nullptr, AV_ERR_INVALID_VAL, "input meta is nullptr!!");
120
121 *(buffer->buffer_->meta_) = *(meta);
122 return AV_ERR_OK;
123 }
124
OH_AVBuffer_GetAddr(OH_AVBuffer * buffer)125 uint8_t *OH_AVBuffer_GetAddr(OH_AVBuffer *buffer)
126 {
127 FALSE_RETURN_V_MSG_E(buffer != nullptr, nullptr, "input buffer is nullptr!");
128 FALSE_RETURN_V_MSG_E(buffer->magic_ == MFMagic::MFMAGIC_AVBUFFER, nullptr, "magic error!");
129 FALSE_RETURN_V_MSG_E(buffer->buffer_ != nullptr, nullptr, "buffer is nullptr!");
130 FALSE_RETURN_V_MSG_E(buffer->buffer_->memory_ != nullptr, nullptr, "buffer's memory is nullptr!");
131 return buffer->buffer_->memory_->GetAddr();
132 }
133
OH_AVBuffer_GetCapacity(OH_AVBuffer * buffer)134 int32_t OH_AVBuffer_GetCapacity(OH_AVBuffer *buffer)
135 {
136 FALSE_RETURN_V_MSG_E(buffer != nullptr, -1, "input buffer is nullptr!");
137 FALSE_RETURN_V_MSG_E(buffer->magic_ == MFMagic::MFMAGIC_AVBUFFER, -1, "magic error!");
138 FALSE_RETURN_V_MSG_E(buffer->buffer_ != nullptr, -1, "buffer is nullptr!");
139 FALSE_RETURN_V_MSG_E(buffer->buffer_->memory_ != nullptr, -1, "buffer's memory is nullptr!");
140 return buffer->buffer_->memory_->GetCapacity();
141 }
142
OH_AVBuffer_GetNativeBuffer(OH_AVBuffer * buffer)143 OH_NativeBuffer *OH_AVBuffer_GetNativeBuffer(OH_AVBuffer *buffer)
144 {
145 FALSE_RETURN_V_MSG_E(buffer != nullptr, nullptr, "input buffer is nullptr!");
146 FALSE_RETURN_V_MSG_E(buffer->magic_ == MFMagic::MFMAGIC_AVBUFFER, nullptr, "magic error!");
147 FALSE_RETURN_V_MSG_E(buffer->buffer_ != nullptr, nullptr, "buffer is nullptr!");
148 FALSE_RETURN_V_MSG_E(buffer->buffer_->memory_ != nullptr, nullptr, "buffer's memory is nullptr!");
149 sptr<SurfaceBuffer> surfaceBuffer = buffer->buffer_->memory_->GetSurfaceBuffer();
150 FALSE_RETURN_V_MSG_E(surfaceBuffer != nullptr, nullptr, "surfaceBuffer is nullptr!");
151 surfaceBuffer->IncStrongRef(surfaceBuffer.GetRefPtr());
152 return surfaceBuffer->SurfaceBufferToNativeBuffer();
153 }