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 "avbuffer_inner_mock.h"
17 #include "avformat_inner_mock.h"
18 #include "media_description.h"
19 #include "meta/meta.h"
20 #include "native_averrors.h"
21 #include "securec.h"
22 #include "unittest_log.h"
23
24
25 namespace OHOS {
26 namespace MediaAVCodec {
27 using namespace Media;
GetAddr()28 uint8_t *AVBufferInnerMock::GetAddr()
29 {
30 UNITTEST_CHECK_AND_RETURN_RET_LOG(buffer_ != nullptr, nullptr, "buffer_ is nullptr!");
31 UNITTEST_CHECK_AND_RETURN_RET_LOG(buffer_->memory_ != nullptr, nullptr, "buffer_->memory_ is nullptr!");
32 return buffer_->memory_->GetAddr();
33 }
34
GetCapacity()35 int32_t AVBufferInnerMock::GetCapacity()
36 {
37 UNITTEST_CHECK_AND_RETURN_RET_LOG(buffer_ != nullptr, -1, "buffer_ is nullptr!");
38 UNITTEST_CHECK_AND_RETURN_RET_LOG(buffer_->memory_ != nullptr, -1, "buffer_->memory_ is nullptr!");
39 return buffer_->memory_->GetCapacity();
40 }
41
GetBufferAttr(OH_AVCodecBufferAttr & attr)42 int32_t AVBufferInnerMock::GetBufferAttr(OH_AVCodecBufferAttr &attr)
43 {
44 UNITTEST_CHECK_AND_RETURN_RET_LOG(buffer_ != nullptr, static_cast<int32_t>(Status::ERROR_UNKNOWN),
45 "buffer_ is nullptr!");
46 attr.pts = buffer_->pts_;
47 attr.flags = static_cast<uint32_t>(buffer_->flag_);
48 if (buffer_->memory_ != nullptr) {
49 attr.offset = buffer_->memory_->GetOffset();
50 attr.size = buffer_->memory_->GetSize();
51 } else {
52 attr.offset = 0;
53 attr.size = 0;
54 }
55 return static_cast<int32_t>(Status::OK);
56 }
57
SetBufferAttr(const OH_AVCodecBufferAttr & attr)58 int32_t AVBufferInnerMock::SetBufferAttr(const OH_AVCodecBufferAttr &attr)
59 {
60 UNITTEST_CHECK_AND_RETURN_RET_LOG(buffer_ != nullptr, static_cast<int32_t>(Status::ERROR_UNKNOWN),
61 "buffer_ is nullptr!");
62 buffer_->pts_ = attr.pts;
63 buffer_->flag_ = attr.flags;
64 if (buffer_->memory_ != nullptr) {
65 (void)buffer_->memory_->SetOffset(attr.offset);
66 (void)buffer_->memory_->SetSize(attr.size);
67 }
68 return static_cast<int32_t>(Status::OK);
69 }
70
GetParameter()71 std::shared_ptr<FormatMock> AVBufferInnerMock::GetParameter()
72 {
73 UNITTEST_CHECK_AND_RETURN_RET_LOG(buffer_ != nullptr, nullptr, "buffer_ is nullptr!");
74 Format format;
75 format.SetMeta(buffer_->meta_);
76 auto formatMock = std::make_shared<AVFormatInnerMock>(format);
77 return formatMock;
78 }
79
SetParameter(const std::shared_ptr<FormatMock> & format)80 int32_t AVBufferInnerMock::SetParameter(const std::shared_ptr<FormatMock> &format)
81 {
82 UNITTEST_CHECK_AND_RETURN_RET_LOG(buffer_ != nullptr, AV_ERR_UNKNOWN, "buffer_ is nullptr!");
83 auto &formatAVCodec = std::static_pointer_cast<AVFormatInnerMock>(format)->GetFormat();
84 *(buffer_->meta_) = *(formatAVCodec.GetMeta());
85 return static_cast<int32_t>(Status::OK);
86 }
87
Destroy()88 int32_t AVBufferInnerMock::Destroy()
89 {
90 buffer_ = nullptr;
91 return AV_ERR_OK;
92 }
93
GetNativeBuffer()94 sptr<SurfaceBuffer> AVBufferInnerMock::GetNativeBuffer()
95 {
96 UNITTEST_CHECK_AND_RETURN_RET_LOG(buffer_ != nullptr, nullptr, "buffer_ is nullptr!");
97 UNITTEST_CHECK_AND_RETURN_RET_LOG(buffer_->memory_ != nullptr, nullptr, "buffer_->memory_ is nullptr!");
98 return buffer_->memory_->GetSurfaceBuffer();
99 }
100
GetAVBuffer()101 std::shared_ptr<AVBuffer> &AVBufferInnerMock::GetAVBuffer()
102 {
103 return buffer_;
104 }
105 } // namespace MediaAVCodec
106 } // namespace OHOS