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 "avformat_inner_mock.h"
17 #include "meta/meta_key.h"
18 #include "securec.h"
19 namespace OHOS {
20 namespace Media {
PutIntValue(const std::string_view & key,int32_t value)21 bool AVFormatInnerMock::PutIntValue(const std::string_view &key, int32_t value)
22 {
23 return format_.PutIntValue(key, value);
24 }
25
GetIntValue(const std::string_view & key,int32_t & value)26 bool AVFormatInnerMock::GetIntValue(const std::string_view &key, int32_t &value)
27 {
28 return format_.GetIntValue(key, value);
29 }
30
PutStringValue(const std::string_view & key,const std::string_view & value)31 bool AVFormatInnerMock::PutStringValue(const std::string_view &key, const std::string_view &value)
32 {
33 return format_.PutStringValue(key, value);
34 }
35
GetStringValue(const std::string_view & key,std::string & value)36 bool AVFormatInnerMock::GetStringValue(const std::string_view &key, std::string &value)
37 {
38 return format_.GetStringValue(key, value);
39 }
40
Destroy()41 void AVFormatInnerMock::Destroy()
42 {
43 if (dumpInfo_ != nullptr) {
44 free(dumpInfo_);
45 dumpInfo_ = nullptr;
46 }
47 return;
48 }
49
GetFormat()50 Format &AVFormatInnerMock::GetFormat()
51 {
52 return format_;
53 }
54
PutLongValue(const std::string_view & key,int64_t value)55 bool AVFormatInnerMock::PutLongValue(const std::string_view &key, int64_t value)
56 {
57 return format_.PutLongValue(key, value);
58 }
59
GetLongValue(const std::string_view & key,int64_t & value)60 bool AVFormatInnerMock::GetLongValue(const std::string_view &key, int64_t &value)
61 {
62 return format_.GetLongValue(key, value);
63 }
64
PutFloatValue(const std::string_view & key,float value)65 bool AVFormatInnerMock::PutFloatValue(const std::string_view &key, float value)
66 {
67 return format_.PutFloatValue(key, value);
68 }
69
GetFloatValue(const std::string_view & key,float & value)70 bool AVFormatInnerMock::GetFloatValue(const std::string_view &key, float &value)
71 {
72 return format_.GetFloatValue(key, value);
73 }
74
PutDoubleValue(const std::string_view & key,double value)75 bool AVFormatInnerMock::PutDoubleValue(const std::string_view &key, double value)
76 {
77 return format_.PutDoubleValue(key, value);
78 }
79
GetDoubleValue(const std::string_view & key,double & value)80 bool AVFormatInnerMock::GetDoubleValue(const std::string_view &key, double &value)
81 {
82 return format_.GetDoubleValue(key, value);
83 }
84
GetBuffer(const std::string_view & key,uint8_t ** addr,size_t & size)85 bool AVFormatInnerMock::GetBuffer(const std::string_view &key, uint8_t **addr, size_t &size)
86 {
87 return format_.GetBuffer(key, addr, size);
88 }
89
PutBuffer(const std::string_view & key,const uint8_t * addr,size_t size)90 bool AVFormatInnerMock::PutBuffer(const std::string_view &key, const uint8_t *addr, size_t size)
91 {
92 return format_.PutBuffer(key, addr, size);
93 }
94
InitAudioTrackFormat(const std::string_view & mimeType,int32_t sampleRate,int32_t channelCount)95 void AVFormatInnerMock::InitAudioTrackFormat(const std::string_view &mimeType, int32_t sampleRate, int32_t channelCount)
96 {
97 format_.PutStringValue(Tag::MIME_TYPE, mimeType);
98 format_.PutIntValue(Tag::AUDIO_SAMPLE_RATE, sampleRate);
99 format_.PutIntValue(Tag::AUDIO_CHANNEL_COUNT, channelCount);
100 }
101
InitVideoTrackFormat(const std::string_view & mimeType,int32_t width,int32_t height)102 void AVFormatInnerMock::InitVideoTrackFormat(const std::string_view &mimeType, int32_t width, int32_t height)
103 {
104 format_.PutStringValue(Tag::MIME_TYPE, mimeType);
105 format_.PutIntValue(Tag::VIDEO_WIDTH, width);
106 format_.PutIntValue(Tag::VIDEO_HEIGHT, height);
107 }
108
DumpInfo()109 const char *AVFormatInnerMock::DumpInfo()
110 {
111 if (dumpInfo_ != nullptr) {
112 free(dumpInfo_);
113 dumpInfo_ = nullptr;
114 }
115 std::string info = format_.Stringify();
116 if (info.empty()) {
117 return nullptr;
118 }
119 constexpr uint32_t maxDumpLength = 1024;
120 uint32_t bufLength = info.size() > maxDumpLength ? maxDumpLength : info.size();
121 dumpInfo_ = static_cast<char *>(malloc((bufLength + 1) * sizeof(char)));
122 if (dumpInfo_ == nullptr) {
123 return nullptr;
124 }
125 if (strcpy_s(dumpInfo_, bufLength + 1, info.c_str()) != 0) {
126 free(dumpInfo_);
127 dumpInfo_ = nullptr;
128 }
129 return dumpInfo_;
130 }
131 } // namespace Media
132 } // namespace OHOS