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 "common/status.h"
19 #include "native_avbuffer.h"
20 #include "native_averrors.h"
21 #include "surface_buffer.h"
22 #include "unittest_log.h"
23 
24 namespace OHOS {
25 namespace Media {
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, static_cast<int32_t>(Status::ERROR_UNKNOWN),
64                                       "buffer_ is nullptr!");
65     return OH_AVBuffer_SetParameter(buffer_, std::static_pointer_cast<AVFormatCapiMock>(format)->GetFormat());
66 }
67 
GetNativeBuffer()68 sptr<SurfaceBuffer> AVBufferCapiMock::GetNativeBuffer()
69 {
70     UNITTEST_CHECK_AND_RETURN_RET_LOG(buffer_ != nullptr, nullptr, "buffer_ is nullptr!");
71     OH_NativeBuffer *surfaceBuffer = OH_AVBuffer_GetNativeBuffer(buffer_);
72     UNITTEST_CHECK_AND_RETURN_RET_LOG(surfaceBuffer != nullptr, nullptr, "surfaceBuffer is nullptr!");
73     return sptr<SurfaceBuffer>(SurfaceBuffer::NativeBufferToSurfaceBuffer(surfaceBuffer));
74 }
75 
Destroy()76 int32_t AVBufferCapiMock::Destroy()
77 {
78     int32_t ret = OH_AVBuffer_Destroy(buffer_);
79     UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == static_cast<int32_t>(Status::OK), ret, "OH_AVBuffer_Destroy failed!");
80     buffer_ = nullptr;
81     return static_cast<int32_t>(Status::OK);
82 }
83 
GetAVBuffer()84 OH_AVBuffer *AVBufferCapiMock::GetAVBuffer()
85 {
86     return buffer_;
87 }
88 } // namespace Media
89 } // namespace OHOS