• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "cj_avplayer_utils.h"
17 #include "media_log.h"
18 
19 namespace OHOS {
20 namespace Media {
21 namespace {
22     constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_PLAYER, "CommonUtils"};
23     constexpr int32_t DECIMAL = 10;
24 } //namespace
25 
StrToULL(const std::string & str,uint64_t & value)26 bool __attribute__((visibility("default"))) StrToULL(const std::string &str, uint64_t &value)
27 {
28     CHECK_AND_RETURN_RET(!str.empty() && (isdigit(str.front())), false);
29     std::string valStr(str);
30     char* end = nullptr;
31     errno = 0;
32     unsigned long long result = strtoull(valStr.c_str(), &end, DECIMAL);
33     // end will not be nullptr here
34     CHECK_AND_RETURN_RET_LOG(result <= ULLONG_MAX, false,
35         "call StrToULL func false,  input str is: %{public}s!", valStr.c_str());
36     CHECK_AND_RETURN_RET_LOG(end != valStr.c_str() && end[0] == '\0' && errno != ERANGE, false,
37         "call StrToULL func false,  input str is: %{public}s!", valStr.c_str());
38     value = result;
39     return true;
40 }
41 
MallocCString(const std::string & origin)42 char *MallocCString(const std::string &origin)
43 {
44     auto len = origin.length() + 1;
45     char *res = static_cast<char *>(malloc(sizeof(char) * len));
46     if (res == nullptr) {
47         return nullptr;
48     }
49     return std::char_traits<char>::copy(res, origin.c_str(), len);
50 }
51 
Convert2CArrI32(const std::vector<int32_t> & arr)52 CArrI32 Convert2CArrI32(const std::vector<int32_t> &arr)
53 {
54     if (arr.size() == 0) {
55         return CArrI32{0};
56     }
57     int32_t *head = static_cast<int32_t *>(malloc(sizeof(int32_t) * arr.size()));
58     if (head == nullptr) {
59         return CArrI32{0};
60     }
61     for (size_t i = 0; i < arr.size(); i++) {
62         head[i] = arr[i];
63     }
64     return CArrI32{.head = head, .size = arr.size()};
65 }
66 
Convert2CArrFloat(const std::vector<float> & arr)67 CArrFloat Convert2CArrFloat(const std::vector<float> &arr)
68 {
69     if (arr.size() == 0) {
70         return CArrFloat{0};
71     }
72     float *head = static_cast<float *>(malloc(sizeof(float) * arr.size()));
73     if (head == nullptr) {
74         return CArrFloat{0};
75     }
76     for (size_t i = 0; i < arr.size(); i++) {
77         head[i] = arr[i];
78     }
79     return CArrFloat{.head = head, .size = arr.size()};
80 }
81 
Convert2CSubtitleInfo(std::string text,int32_t pts,int32_t duration)82 CSubtitleInfo Convert2CSubtitleInfo(std::string text, int32_t pts, int32_t duration)
83 {
84     CSubtitleInfo info = CSubtitleInfo{0};
85     info.text = MallocCString(text);
86     info.startTime = pts;
87     info.duration = duration;
88     return info;
89 }
90 
GetPlayerTrackIndex(char ** key,CValueType * value,const Format trackInfo,int64_t & count)91 void GetPlayerTrackIndex(char **key, CValueType *value, const Format trackInfo, int64_t &count)
92 {
93     int32_t index = -1;
94     if (trackInfo.GetIntValue(std::string(PlayerKeys::PLAYER_TRACK_INDEX), index)) {
95         key[count] = MallocCString(std::string(PlayerKeys::PLAYER_TRACK_INDEX));
96         value[count] = CValueType{.number = index, .dou = 0.0, .str = nullptr};
97         count++;
98     }
99 }
100 
GetPlayerTrackType(char ** key,CValueType * value,const Format trackInfo,int64_t & count)101 void GetPlayerTrackType(char **key, CValueType *value, const Format trackInfo, int64_t &count)
102 {
103     int32_t type = -1;
104     if (trackInfo.GetIntValue(std::string(PlayerKeys::PLAYER_TRACK_TYPE), type)) {
105         key[count] = MallocCString(std::string(PlayerKeys::PLAYER_TRACK_TYPE));
106         value[count] = CValueType{.number = type, .dou = 0.0, .str = nullptr};
107         count++;
108     }
109 }
110 
GetPlayerMime(char ** key,CValueType * value,const Format trackInfo,int64_t & count)111 void GetPlayerMime(char **key, CValueType *value, const Format trackInfo, int64_t &count)
112 {
113     std::string mime;
114     if (trackInfo.GetStringValue(std::string(PlayerKeys::PLAYER_MIME), mime)) {
115         key[count] = MallocCString(std::string(PlayerKeys::PLAYER_MIME));
116         value[count] = CValueType{.number = 0, .dou = 0.0, .str = MallocCString(mime)};
117         count++;
118     }
119 }
120 
GetPlayerDuration(char ** key,CValueType * value,const Format trackInfo,int64_t & count)121 void GetPlayerDuration(char **key, CValueType *value, const Format trackInfo, int64_t &count)
122 {
123     int32_t duration = -1;
124     if (trackInfo.GetIntValue(std::string(PlayerKeys::PLAYER_DURATION), duration)) {
125         key[count] = MallocCString(std::string(PlayerKeys::PLAYER_DURATION));
126         value[count] = CValueType{.number = duration, .dou = 0.0, .str = nullptr};
127         count++;
128     }
129 }
130 
GetPlayerBitrate(char ** key,CValueType * value,const Format trackInfo,int64_t & count)131 void GetPlayerBitrate(char **key, CValueType *value, const Format trackInfo, int64_t &count)
132 {
133     int32_t bitrate = -1;
134     if (trackInfo.GetIntValue(std::string(PlayerKeys::PLAYER_BITRATE), bitrate)) {
135         key[count] = MallocCString(std::string(PlayerKeys::PLAYER_BITRATE));
136         value[count] = CValueType{.number = bitrate, .dou = 0.0, .str = nullptr};
137         count++;
138     }
139 }
140 
GetPlayerWidth(char ** key,CValueType * value,const Format trackInfo,int64_t & count)141 void GetPlayerWidth(char **key, CValueType *value, const Format trackInfo, int64_t &count)
142 {
143     int32_t width = -1;
144     if (trackInfo.GetIntValue(std::string(PlayerKeys::PLAYER_WIDTH), width)) {
145         key[count] = MallocCString(std::string(PlayerKeys::PLAYER_WIDTH));
146         value[count] = CValueType{.number = width, .dou = 0.0, .str = nullptr};
147         count++;
148     }
149 }
150 
GetPlayerHeight(char ** key,CValueType * value,const Format trackInfo,int64_t & count)151 void GetPlayerHeight(char **key, CValueType *value, const Format trackInfo, int64_t &count)
152 {
153     int32_t height = -1;
154     if (trackInfo.GetIntValue(std::string(PlayerKeys::PLAYER_HEIGHT), height)) {
155         key[count] = MallocCString(std::string(PlayerKeys::PLAYER_HEIGHT));
156         value[count] = CValueType{.number = height, .dou = 0.0, .str = nullptr};
157         count++;
158     }
159 }
160 
GetPlayerFramerate(char ** key,CValueType * value,const Format trackInfo,int64_t & count)161 void GetPlayerFramerate(char **key, CValueType *value, const Format trackInfo, int64_t &count)
162 {
163     double frameRate = -1;
164     if (trackInfo.GetDoubleValue(std::string(PlayerKeys::PLAYER_FRAMERATE), frameRate)) {
165         key[count] = MallocCString(std::string(PlayerKeys::PLAYER_FRAMERATE));
166         value[count] = CValueType{.number = 0, .dou = frameRate, .str = nullptr};
167         count++;
168     }
169 }
170 
GetPlayerChannels(char ** key,CValueType * value,const Format trackInfo,int64_t & count)171 void GetPlayerChannels(char **key, CValueType *value, const Format trackInfo, int64_t &count)
172 {
173     int32_t channelCount = -1;
174     if (trackInfo.GetIntValue(std::string(PlayerKeys::PLAYER_CHANNELS), channelCount)) {
175         key[count] = MallocCString(std::string(PlayerKeys::PLAYER_CHANNELS));
176         value[count] = CValueType{.number = channelCount, .dou = 0.0, .str = nullptr};
177         count++;
178     }
179 }
180 
GetPlayerSampleRate(char ** key,CValueType * value,const Format trackInfo,int64_t & count)181 void GetPlayerSampleRate(char **key, CValueType *value, const Format trackInfo, int64_t &count)
182 {
183     int32_t sampleRate = -1;
184     if (trackInfo.GetIntValue(std::string(PlayerKeys::PLAYER_SAMPLE_RATE), sampleRate)) {
185         key[count] = MallocCString(std::string(PlayerKeys::PLAYER_SAMPLE_RATE));
186         value[count] = CValueType{.number = sampleRate, .dou = 0.0, .str = nullptr};
187         count++;
188     }
189 }
190 
Convert2CMediaDescription(const Format trackInfo)191 CMediaDescription Convert2CMediaDescription(const Format trackInfo)
192 {
193     CMediaDescription result = CMediaDescription{0};
194     char **key = static_cast<char **>(malloc(sizeof(char *) * MAX_SIZE_OF_MEDIADESCRIPTIONKEY));
195     CValueType *value = static_cast<CValueType *>(malloc(sizeof(CValueType) * MAX_SIZE_OF_MEDIADESCRIPTIONKEY));
196     if (key == nullptr || value == nullptr) {
197         free(key);
198         free(value);
199         return result;
200     }
201     int64_t count = 0;
202     GetPlayerTrackIndex(key, value, trackInfo, count);
203     GetPlayerTrackType(key, value, trackInfo, count);
204     GetPlayerMime(key, value, trackInfo, count);
205     GetPlayerDuration(key, value, trackInfo, count);
206     GetPlayerBitrate(key, value, trackInfo, count);
207     GetPlayerWidth(key, value, trackInfo, count);
208     GetPlayerHeight(key, value, trackInfo, count);
209     GetPlayerFramerate(key, value, trackInfo, count);
210     GetPlayerChannels(key, value, trackInfo, count);
211     GetPlayerSampleRate(key, value, trackInfo, count);
212     int32_t sampleDepth = -1;
213     if (trackInfo.GetIntValue("sample_depth", sampleDepth)) {
214         key[count] = MallocCString("sample_depth");
215         value[count] = CValueType{.number = sampleDepth, .dou = 0.0, .str = nullptr};
216         count++;
217     }
218     std::string language;
219     if (trackInfo.GetStringValue("language", language)) {
220         key[count] = MallocCString("language");
221         value[count] = CValueType{.number = 0, .dou = 0.0, .str = MallocCString(language)};
222         count++;
223     }
224     std::string trackName;
225     if (trackInfo.GetStringValue("track_name", trackName)) {
226         key[count] = MallocCString("track_name");
227         value[count] = CValueType{.number = 0, .dou = 0.0, .str = MallocCString(trackName)};
228         count++;
229     }
230     std::string hdrType;
231     if (trackInfo.GetStringValue("hdr_type", hdrType)) {
232         key[count] = MallocCString("hdr_type");
233         value[count] = CValueType{.number = 0, .dou = 0.0, .str = MallocCString(hdrType)};
234         count++;
235     }
236     result.key = key;
237     result.value = value;
238     result.size = count;
239     return result;
240 }
241 
Convert2CArrCMediaDescription(const std::vector<Format> trackInfo)242 CArrCMediaDescription Convert2CArrCMediaDescription(const std::vector<Format> trackInfo)
243 {
244     if (trackInfo.size() == 0) {
245         return CArrCMediaDescription{0};
246     }
247     CMediaDescription *head = static_cast<CMediaDescription *>(malloc(sizeof(CMediaDescription) * trackInfo.size()));
248     if (head == nullptr) {
249         return CArrCMediaDescription{0};
250     }
251     for (size_t i = 0; i < trackInfo.size(); i++) {
252         head[i] = Convert2CMediaDescription(trackInfo[i]);
253     }
254     return CArrCMediaDescription{.head = head, .size = trackInfo.size()};
255 }
256 } // namespace Media
257 } // namespace OHOS