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