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_inner_mock.h"
17 #include "securec.h"
18 #include "common/native_mfmagic.h"
19
20 namespace OHOS {
21 namespace MediaAVCodec {
22 using namespace OHOS::Media;
Destroy()23 int32_t AVMuxerInnerMock::Destroy()
24 {
25 if (muxer_ != nullptr) {
26 muxer_ = nullptr;
27 return AV_ERR_OK;
28 }
29 return AV_ERR_UNKNOWN;
30 }
31
Start()32 int32_t AVMuxerInnerMock::Start()
33 {
34 if (muxer_ != nullptr) {
35 return muxer_->Start();
36 }
37 return AV_ERR_UNKNOWN;
38 }
39
Stop()40 int32_t AVMuxerInnerMock::Stop()
41 {
42 if (muxer_ != nullptr) {
43 return muxer_->Stop();
44 }
45 return AV_ERR_UNKNOWN;
46 }
47
AddTrack(int32_t & trackIndex,std::shared_ptr<FormatMock> & trackFormat)48 int32_t AVMuxerInnerMock::AddTrack(int32_t &trackIndex, std::shared_ptr<FormatMock> &trackFormat)
49 {
50 if (muxer_ != nullptr) {
51 auto formatMock = std::static_pointer_cast<AVFormatInnerMock>(trackFormat);
52 return muxer_->AddTrack(trackIndex, formatMock->GetFormat().GetMeta());
53 }
54 return AV_ERR_UNKNOWN;
55 }
56
WriteSample(uint32_t trackIndex,const uint8_t * sample,const OH_AVCodecBufferAttr & info)57 int32_t AVMuxerInnerMock::WriteSample(uint32_t trackIndex,
58 const uint8_t *sample, const OH_AVCodecBufferAttr &info)
59 {
60 if (muxer_ != nullptr) {
61 auto alloc = AVAllocatorFactory::CreateSharedAllocator(MemoryFlag::MEMORY_READ_WRITE);
62 std::shared_ptr<AVBuffer> avSample = AVBuffer::CreateAVBuffer(alloc, info.size);
63 avSample->memory_->Write(sample + info.offset, info.size);
64 avSample->pts_ = info.pts;
65 avSample->flag_ = info.flags;
66 return muxer_->WriteSample(trackIndex, avSample);
67 }
68 return AV_ERR_UNKNOWN;
69 }
70
WriteSampleBuffer(uint32_t trackIndex,const OH_AVBuffer * sample)71 int32_t AVMuxerInnerMock::WriteSampleBuffer(uint32_t trackIndex, const OH_AVBuffer *sample)
72 {
73 if (muxer_ != nullptr && sample != nullptr) {
74 return muxer_->WriteSample(trackIndex, sample->buffer_);
75 }
76 return AV_ERR_UNKNOWN;
77 }
78
SetTimedMetadata()79 int32_t AVMuxerInnerMock::SetTimedMetadata()
80 {
81 if (muxer_ != nullptr) {
82 std::shared_ptr<Meta> param = std::make_shared<Meta>();
83 param->SetData("use_timed_meta_track", 1);
84 return muxer_->SetParameter(param);
85 }
86 return AV_ERR_UNKNOWN;
87 }
88
SetRotation(int32_t rotation)89 int32_t AVMuxerInnerMock::SetRotation(int32_t rotation)
90 {
91 if (muxer_ != nullptr) {
92 std::shared_ptr<Meta> param = std::make_shared<Meta>();
93 param->Set<Tag::VIDEO_ROTATION>(static_cast<Plugins::VideoRotation>(rotation));
94 return muxer_->SetParameter(param);
95 }
96 return AV_ERR_UNKNOWN;
97 }
98 } // namespace MediaAVCodec
99 } // namespace OHOS