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_capi_mock.h"
17 #include "avformat_capi_mock.h"
18 #include "native_avbuffer.h"
19 #include "native_averrors.h"
20 #include "surface_buffer.h"
21 #include "unittest_log.h"
22 
23 
24 namespace OHOS {
25 namespace MediaAVCodec {
GetAddr()26 uint8_t *AVBufferCapiMock::GetAddr()
27 {
28     UNITTEST_CHECK_AND_RETURN_RET_LOG(buffer_ != nullptr, nullptr, "buffer_ is nullptr!");
29     return OH_AVBuffer_GetAddr(buffer_);
30 }
31 
GetCapacity()32 int32_t AVBufferCapiMock::GetCapacity()
33 {
34     UNITTEST_CHECK_AND_RETURN_RET_LOG(buffer_ != nullptr, 0, "buffer_ is nullptr!");
35     return OH_AVBuffer_GetCapacity(buffer_);
36 }
37 
GetBufferAttr(OH_AVCodecBufferAttr & attr)38 int32_t AVBufferCapiMock::GetBufferAttr(OH_AVCodecBufferAttr &attr)
39 {
40     UNITTEST_CHECK_AND_RETURN_RET_LOG(buffer_ != nullptr, static_cast<int32_t>(Status::ERROR_UNKNOWN),
41                                       "buffer_ is nullptr!");
42     return static_cast<int32_t>(OH_AVBuffer_GetBufferAttr(buffer_, &attr));
43 }
44 
SetBufferAttr(const OH_AVCodecBufferAttr & attr)45 int32_t AVBufferCapiMock::SetBufferAttr(const OH_AVCodecBufferAttr &attr)
46 {
47     UNITTEST_CHECK_AND_RETURN_RET_LOG(buffer_ != nullptr, static_cast<int32_t>(Status::ERROR_UNKNOWN),
48                                       "buffer_ is nullptr!");
49     return OH_AVBuffer_SetBufferAttr(buffer_, &attr);
50 }
51 
GetParameter()52 std::shared_ptr<FormatMock> AVBufferCapiMock::GetParameter()
53 {
54     UNITTEST_CHECK_AND_RETURN_RET_LOG(buffer_ != nullptr, nullptr, "buffer_ is nullptr!");
55     OH_AVFormat *format = OH_AVBuffer_GetParameter(buffer_);
56     UNITTEST_CHECK_AND_RETURN_RET_LOG(format != nullptr, nullptr, "format is nullptr!");
57     auto formatMock = std::make_shared<AVFormatCapiMock>(format);
58     return formatMock;
59 }
60 
SetParameter(const std::shared_ptr<FormatMock> & format)61 int32_t AVBufferCapiMock::SetParameter(const std::shared_ptr<FormatMock> &format)
62 {
63     UNITTEST_CHECK_AND_RETURN_RET_LOG(buffer_ != nullptr, AV_ERR_UNKNOWN, "buffer_ is nullptr!");
64     return OH_AVBuffer_SetParameter(buffer_, std::static_pointer_cast<AVFormatCapiMock>(format)->GetFormat());
65 }
66 
Destroy()67 int32_t AVBufferCapiMock::Destroy()
68 {
69     int32_t ret = OH_AVBuffer_Destroy(buffer_);
70     UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == AV_ERR_OK, ret, "OH_AVBuffer_Destroy failed!");
71     buffer_ = nullptr;
72     return AV_ERR_OK;
73 }
74 
GetNativeBuffer()75 sptr<SurfaceBuffer> AVBufferCapiMock::GetNativeBuffer()
76 {
77     UNITTEST_CHECK_AND_RETURN_RET_LOG(buffer_ != nullptr, nullptr, "buffer_ is nullptr!");
78     OH_NativeBuffer *surfaceBuffer = OH_AVBuffer_GetNativeBuffer(buffer_);
79     UNITTEST_CHECK_AND_RETURN_RET_LOG(surfaceBuffer != nullptr, nullptr, "surfaceBuffer is nullptr!");
80     auto ref = sptr<SurfaceBuffer>(SurfaceBuffer::NativeBufferToSurfaceBuffer(surfaceBuffer));
81     ref->DecStrongRef(ref.GetRefPtr());
82     return ref;
83 }
84 
GetAVBuffer()85 OH_AVBuffer *AVBufferCapiMock::GetAVBuffer()
86 {
87     return buffer_;
88 }
89 } // namespace MediaAVCodec
90 } // namespace OHOS