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 "avmuxer_capi_mock.h"
17 #include "securec.h"
18 
19 namespace OHOS {
20 namespace MediaAVCodec {
Destroy()21 int32_t AVMuxerCapiMock::Destroy()
22 {
23     int ret = OH_AVMuxer_Destroy(muxer_);
24     if (ret != AV_ERR_OK) {
25         return ret;
26     }
27     muxer_ = nullptr;
28     return AV_ERR_OK;
29 }
30 
Start()31 int32_t AVMuxerCapiMock::Start()
32 {
33     return OH_AVMuxer_Start(muxer_);
34 }
35 
Stop()36 int32_t AVMuxerCapiMock::Stop()
37 {
38     return OH_AVMuxer_Stop(muxer_);
39 }
40 
AddTrack(int32_t & trackIndex,std::shared_ptr<FormatMock> & trackFormat)41 int32_t AVMuxerCapiMock::AddTrack(int32_t &trackIndex, std::shared_ptr<FormatMock> &trackFormat)
42 {
43     auto formatMock = std::static_pointer_cast<AVFormatCapiMock>(trackFormat);
44     return OH_AVMuxer_AddTrack(muxer_, &trackIndex, formatMock->GetFormat());
45 }
46 
WriteSample(uint32_t trackIndex,const uint8_t * sample,const OH_AVCodecBufferAttr & info)47 int32_t AVMuxerCapiMock::WriteSample(uint32_t trackIndex,
48     const uint8_t *sample, const OH_AVCodecBufferAttr &info)
49 {
50     int32_t ret = AV_ERR_NO_MEMORY;
51     OH_AVMemory *avSample = OH_AVMemory_Create(info.size);
52     uint8_t *data = OH_AVMemory_GetAddr(avSample);
53     int32_t size = OH_AVMemory_GetSize(avSample);
54     if (memcpy_s(data, size, sample, info.size) == EOK) {
55         OH_AVCodecBufferAttr bufferAttr;
56         bufferAttr.pts = info.pts;
57         bufferAttr.size = info.size;
58         bufferAttr.offset = info.offset;
59         bufferAttr.flags = info.flags;
60         ret = OH_AVMuxer_WriteSample(muxer_, trackIndex, avSample, bufferAttr);
61     }
62     (void)OH_AVMemory_Destroy(avSample);
63     return ret;
64 }
65 
WriteSampleBuffer(uint32_t trackIndex,const OH_AVBuffer * sample)66 int32_t AVMuxerCapiMock::WriteSampleBuffer(uint32_t trackIndex, const OH_AVBuffer *sample)
67 {
68     if (muxer_ != nullptr) {
69         return OH_AVMuxer_WriteSampleBuffer(muxer_, trackIndex, sample);
70     }
71     return AV_ERR_UNKNOWN;
72 }
73 
SetTimedMetadata()74 int32_t AVMuxerCapiMock::SetTimedMetadata()
75 {
76     return AV_ERR_UNKNOWN;
77 }
78 
SetRotation(int32_t rotation)79 int32_t AVMuxerCapiMock::SetRotation(int32_t rotation)
80 {
81     return OH_AVMuxer_SetRotation(muxer_, rotation);
82 }
83 } // namespace MediaAVCodec
84 } // namespace OHOS