• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "native_avformat.h"
17 
18 #include "common/log.h"
19 #include "common/native_mfmagic.h"
20 #include "common/status.h"
21 #include "meta/meta_key.h"
22 #include "securec.h"
23 
24 namespace {
25 constexpr uint32_t MAX_STRING_LENGTH = 256;
26 constexpr uint32_t MAX_DUMP_LENGTH = 1024;
27 } // namespace
28 
29 using namespace OHOS::Media;
30 
OH_AVFormat_Create(void)31 struct OH_AVFormat *OH_AVFormat_Create(void)
32 {
33     return new (std::nothrow) OH_AVFormat();
34 }
35 
OH_AVFormat_CreateAudioFormat(const char * mimeType,int32_t sampleRate,int32_t channelCount)36 struct OH_AVFormat *OH_AVFormat_CreateAudioFormat(const char *mimeType, int32_t sampleRate, int32_t channelCount)
37 {
38     FALSE_RETURN_V_MSG_E(mimeType != nullptr, nullptr, "mimeType is nullptr!");
39     OH_AVFormat *audioFormat = new (std::nothrow) OH_AVFormat();
40     FALSE_RETURN_V_MSG_E(audioFormat != nullptr, nullptr, "new format is nullptr!");
41     audioFormat->format_.PutStringValue(Tag::MIME_TYPE, mimeType);
42     audioFormat->format_.PutIntValue(Tag::AUDIO_SAMPLE_RATE, sampleRate);
43     audioFormat->format_.PutIntValue(Tag::AUDIO_CHANNEL_COUNT, channelCount);
44     return audioFormat;
45 }
46 
OH_AVFormat_CreateVideoFormat(const char * mimeType,int32_t width,int32_t height)47 struct OH_AVFormat *OH_AVFormat_CreateVideoFormat(const char *mimeType, int32_t width, int32_t height)
48 {
49     FALSE_RETURN_V_MSG_E(mimeType != nullptr, nullptr, "mimeType is nullptr!");
50     OH_AVFormat *videoFormat = new (std::nothrow) OH_AVFormat();
51     FALSE_RETURN_V_MSG_E(videoFormat != nullptr, nullptr, "new format is nullptr!");
52     videoFormat->format_.PutStringValue(Tag::MIME_TYPE, mimeType);
53     videoFormat->format_.PutIntValue(Tag::VIDEO_WIDTH, width);
54     videoFormat->format_.PutIntValue(Tag::VIDEO_HEIGHT, height);
55     return videoFormat;
56 }
57 
OH_AVFormat_Destroy(struct OH_AVFormat * format)58 void OH_AVFormat_Destroy(struct OH_AVFormat *format)
59 {
60     delete format;
61 }
62 
OH_AVFormat_Copy(struct OH_AVFormat * to,struct OH_AVFormat * from)63 bool OH_AVFormat_Copy(struct OH_AVFormat *to, struct OH_AVFormat *from)
64 {
65     FALSE_RETURN_V_MSG_E(to != nullptr, false, "to format is nullptr!");
66     FALSE_RETURN_V_MSG_E(to->magic_ == MFMagic::MFMAGIC_FORMAT, false, "magic error!");
67     FALSE_RETURN_V_MSG_E(from != nullptr, false, "from format is nullptr!");
68     FALSE_RETURN_V_MSG_E(from->magic_ == MFMagic::MFMAGIC_FORMAT, false, "magic error!");
69 
70     to->format_ = from->format_;
71     return true;
72 }
73 
OH_AVFormat_SetIntValue(struct OH_AVFormat * format,const char * key,int32_t value)74 bool OH_AVFormat_SetIntValue(struct OH_AVFormat *format, const char *key, int32_t value)
75 {
76     FALSE_RETURN_V_MSG_E(format != nullptr, false, "input format is nullptr!");
77     FALSE_RETURN_V_MSG_E(format->magic_ == MFMagic::MFMAGIC_FORMAT, false, "magic error!");
78     FALSE_RETURN_V_MSG_E(key != nullptr, false, "key is nullptr!");
79 
80     return format->format_.PutIntValue(key, value);
81 }
82 
OH_AVFormat_SetLongValue(struct OH_AVFormat * format,const char * key,int64_t value)83 bool OH_AVFormat_SetLongValue(struct OH_AVFormat *format, const char *key, int64_t value)
84 {
85     FALSE_RETURN_V_MSG_E(format != nullptr, false, "input format is nullptr!");
86     FALSE_RETURN_V_MSG_E(format->magic_ == MFMagic::MFMAGIC_FORMAT, false, "magic error!");
87     FALSE_RETURN_V_MSG_E(key != nullptr, false, "key is nullptr!");
88 
89     return format->format_.PutLongValue(key, value);
90 }
91 
OH_AVFormat_SetFloatValue(struct OH_AVFormat * format,const char * key,float value)92 bool OH_AVFormat_SetFloatValue(struct OH_AVFormat *format, const char *key, float value)
93 {
94     FALSE_RETURN_V_MSG_E(format != nullptr, false, "input format is nullptr!");
95     FALSE_RETURN_V_MSG_E(format->magic_ == MFMagic::MFMAGIC_FORMAT, false, "magic error!");
96     FALSE_RETURN_V_MSG_E(key != nullptr, false, "key is nullptr!");
97 
98     return format->format_.PutFloatValue(key, value);
99 }
100 
OH_AVFormat_SetDoubleValue(struct OH_AVFormat * format,const char * key,double value)101 bool OH_AVFormat_SetDoubleValue(struct OH_AVFormat *format, const char *key, double value)
102 {
103     FALSE_RETURN_V_MSG_E(format != nullptr, false, "input format is nullptr!");
104     FALSE_RETURN_V_MSG_E(format->magic_ == MFMagic::MFMAGIC_FORMAT, false, "magic error!");
105     FALSE_RETURN_V_MSG_E(key != nullptr, false, "key is nullptr!");
106 
107     return format->format_.PutDoubleValue(key, value);
108 }
109 
OH_AVFormat_SetStringValue(struct OH_AVFormat * format,const char * key,const char * value)110 bool OH_AVFormat_SetStringValue(struct OH_AVFormat *format, const char *key, const char *value)
111 {
112     FALSE_RETURN_V_MSG_E(format != nullptr, false, "input format is nullptr!");
113     FALSE_RETURN_V_MSG_E(format->magic_ == MFMagic::MFMAGIC_FORMAT, false, "magic error!");
114     FALSE_RETURN_V_MSG_E(key != nullptr, false, "key is nullptr!");
115     FALSE_RETURN_V_MSG_E(value != nullptr, false, "value is nullptr!");
116 
117     return format->format_.PutStringValue(key, value);
118 }
119 
OH_AVFormat_SetBuffer(struct OH_AVFormat * format,const char * key,const uint8_t * addr,size_t size)120 bool OH_AVFormat_SetBuffer(struct OH_AVFormat *format, const char *key, const uint8_t *addr, size_t size)
121 {
122     FALSE_RETURN_V_MSG_E(format != nullptr, false, "input format is nullptr!");
123     FALSE_RETURN_V_MSG_E(format->magic_ == MFMagic::MFMAGIC_FORMAT, false, "magic error!");
124     FALSE_RETURN_V_MSG_E(key != nullptr, false, "key is nullptr!");
125     FALSE_RETURN_V_MSG_E(addr != nullptr, false, "addr is nullptr!");
126     FALSE_RETURN_V_MSG_E(size != 0, false, "size is zero!");
127 
128     return format->format_.PutBuffer(key, addr, size);
129 }
130 
OH_AVFormat_GetIntValue(struct OH_AVFormat * format,const char * key,int32_t * out)131 bool OH_AVFormat_GetIntValue(struct OH_AVFormat *format, const char *key, int32_t *out)
132 {
133     FALSE_RETURN_V_MSG_E(format != nullptr, false, "input format is nullptr!");
134     FALSE_RETURN_V_MSG_E(format->magic_ == MFMagic::MFMAGIC_FORMAT, false, "magic error!");
135     FALSE_RETURN_V_MSG_E(key != nullptr, false, "key is nullptr!");
136     FALSE_RETURN_V_MSG_E(out != nullptr, false, "out is nullptr!");
137 
138     return format->format_.GetIntValue(key, *out);
139 }
140 
OH_AVFormat_GetLongValue(struct OH_AVFormat * format,const char * key,int64_t * out)141 bool OH_AVFormat_GetLongValue(struct OH_AVFormat *format, const char *key, int64_t *out)
142 {
143     FALSE_RETURN_V_MSG_E(format != nullptr, false, "input format is nullptr!");
144     FALSE_RETURN_V_MSG_E(format->magic_ == MFMagic::MFMAGIC_FORMAT, false, "magic error!");
145     FALSE_RETURN_V_MSG_E(key != nullptr, false, "key is nullptr!");
146     FALSE_RETURN_V_MSG_E(out != nullptr, false, "out is nullptr!");
147 
148     return format->format_.GetLongValue(key, *out);
149 }
150 
OH_AVFormat_GetFloatValue(struct OH_AVFormat * format,const char * key,float * out)151 bool OH_AVFormat_GetFloatValue(struct OH_AVFormat *format, const char *key, float *out)
152 {
153     FALSE_RETURN_V_MSG_E(format != nullptr, false, "input format is nullptr!");
154     FALSE_RETURN_V_MSG_E(format->magic_ == MFMagic::MFMAGIC_FORMAT, false, "magic error!");
155     FALSE_RETURN_V_MSG_E(key != nullptr, false, "key is nullptr!");
156     FALSE_RETURN_V_MSG_E(out != nullptr, false, "out is nullptr!");
157 
158     return format->format_.GetFloatValue(key, *out);
159 }
160 
OH_AVFormat_GetDoubleValue(struct OH_AVFormat * format,const char * key,double * out)161 bool OH_AVFormat_GetDoubleValue(struct OH_AVFormat *format, const char *key, double *out)
162 {
163     FALSE_RETURN_V_MSG_E(format != nullptr, false, "input format is nullptr!");
164     FALSE_RETURN_V_MSG_E(format->magic_ == MFMagic::MFMAGIC_FORMAT, false, "magic error!");
165     FALSE_RETURN_V_MSG_E(key != nullptr, false, "key is nullptr!");
166     FALSE_RETURN_V_MSG_E(out != nullptr, false, "out is nullptr!");
167 
168     return format->format_.GetDoubleValue(key, *out);
169 }
170 
OH_AVFormat_GetStringValue(struct OH_AVFormat * format,const char * key,const char ** out)171 bool OH_AVFormat_GetStringValue(struct OH_AVFormat *format, const char *key, const char **out)
172 {
173     FALSE_RETURN_V_MSG_E(format != nullptr, false, "input format is nullptr!");
174     FALSE_RETURN_V_MSG_E(format->magic_ == MFMagic::MFMAGIC_FORMAT, false, "magic error!");
175     FALSE_RETURN_V_MSG_E(key != nullptr, false, "key is nullptr!");
176     FALSE_RETURN_V_MSG_E(out != nullptr, false, "out is nullptr!");
177 
178     if (format->outString_ != nullptr) {
179         free(format->outString_);
180         format->outString_ = nullptr;
181     }
182 
183     std::string str;
184     bool ret = format->format_.GetStringValue(key, str);
185     if (!ret) {
186         return false;
187     }
188     uint32_t bufLength = str.size() > MAX_STRING_LENGTH ? MAX_STRING_LENGTH : str.size();
189 
190     format->outString_ = static_cast<char *>(malloc((bufLength + 1) * sizeof(char)));
191     FALSE_RETURN_V_MSG_E(format->outString_ != nullptr, false, "malloc out string nullptr!");
192 
193     if (strcpy_s(format->outString_, bufLength + 1, str.c_str()) != EOK) {
194         MEDIA_LOG_E("Failed to strcpy_s");
195         free(format->outString_);
196         format->outString_ = nullptr;
197         return false;
198     }
199 
200     *out = format->outString_;
201     return true;
202 }
203 
OH_AVFormat_GetBuffer(struct OH_AVFormat * format,const char * key,uint8_t ** addr,size_t * size)204 bool OH_AVFormat_GetBuffer(struct OH_AVFormat *format, const char *key, uint8_t **addr, size_t *size)
205 {
206     FALSE_RETURN_V_MSG_E(format != nullptr, false, "input format is nullptr!");
207     FALSE_RETURN_V_MSG_E(format->magic_ == MFMagic::MFMAGIC_FORMAT, false, "magic error!");
208     FALSE_RETURN_V_MSG_E(key != nullptr, false, "key is nullptr!");
209     FALSE_RETURN_V_MSG_E(addr != nullptr, false, "addr is nullptr!");
210     FALSE_RETURN_V_MSG_E(size != nullptr, false, "size is nullptr!");
211 
212     return format->format_.GetBuffer(key, addr, *size);
213 }
214 
OH_AVFormat_DumpInfo(struct OH_AVFormat * format)215 const char *OH_AVFormat_DumpInfo(struct OH_AVFormat *format)
216 {
217     FALSE_RETURN_V_MSG_E(format != nullptr, nullptr, "input format is nullptr!");
218     if (format->dumpInfo_ != nullptr) {
219         free(format->dumpInfo_);
220         format->dumpInfo_ = nullptr;
221     }
222     std::string info = format->format_.Stringify();
223     if (info.empty()) {
224         return nullptr;
225     }
226     uint32_t bufLength = info.size() > MAX_DUMP_LENGTH ? MAX_DUMP_LENGTH : info.size();
227     format->dumpInfo_ = static_cast<char *>(malloc((bufLength + 1) * sizeof(char)));
228     FALSE_RETURN_V_MSG_E(format->dumpInfo_ != nullptr, nullptr, "malloc dump info nullptr!");
229     if (strcpy_s(format->dumpInfo_, bufLength + 1, info.c_str()) != EOK) {
230         MEDIA_LOG_E("Failed to strcpy_s");
231         free(format->dumpInfo_);
232         format->dumpInfo_ = nullptr;
233     }
234     return format->dumpInfo_;
235 }
236