• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 #define MLOG_TAG "Scanner"
16 
17 #include "ringtone_metadata_extractor.h"
18 
19 #include <fcntl.h>
20 #include <sys/stat.h>
21 
22 #include "directory_ex.h"
23 #include "ringtone_file_utils.h"
24 #include "ringtone_log.h"
25 #include "ringtone_errno.h"
26 
27 namespace OHOS {
28 namespace Media {
29 using namespace std;
30 
31 template <class Type>
stringToNum(const string & str)32 static Type stringToNum(const string &str)
33 {
34     std::istringstream iss(str);
35     Type num;
36     iss >> num;
37     return num;
38 }
39 
convertTimeStr2TimeStamp(string & timeStr)40 static time_t convertTimeStr2TimeStamp(string &timeStr)
41 {
42     struct tm timeinfo;
43     time_t timeStamp = time(NULL);
44     if (strptime(timeStr.c_str(), "%Y-%m-%d %H:%M:%S",  &timeinfo) != nullptr) {
45         timeStamp = mktime(&timeinfo);
46     }
47     return timeStamp;
48 }
49 
PopulateExtractedAVMetadataOne(const std::unordered_map<int32_t,std::string> & resultMap,std::unique_ptr<RingtoneMetadata> & data)50 void PopulateExtractedAVMetadataOne(const std::unordered_map<int32_t, std::string> &resultMap,
51     std::unique_ptr<RingtoneMetadata> &data)
52 {
53     int32_t intTempMeta;
54 
55     string strTemp = resultMap.at(Media::AV_KEY_DURATION);
56     if (strTemp != "") {
57         intTempMeta = stringToNum<int32_t>(strTemp);
58         data->SetDuration(intTempMeta);
59     }
60 
61     strTemp = resultMap.at(Media::AV_KEY_MIME_TYPE);
62     if (strTemp != "") {
63         data->SetMimeType(strTemp);
64     }
65 }
66 
PopulateExtractedAVMetadataTwo(const std::unordered_map<int32_t,std::string> & resultMap,std::unique_ptr<RingtoneMetadata> & data)67 void PopulateExtractedAVMetadataTwo(const std::unordered_map<int32_t, std::string> &resultMap,
68     std::unique_ptr<RingtoneMetadata> &data)
69 {
70     string strTemp = resultMap.at(Media::AV_KEY_DATE_TIME_FORMAT);
71     if (strTemp != "") {
72         int64_t int64TempMeta = convertTimeStr2TimeStamp(strTemp);
73         if (int64TempMeta < 0) {
74             data->SetDateTaken(data->GetDateModified() / MSEC_TO_SEC);
75         } else {
76             data->SetDateTaken(int64TempMeta);
77         }
78     } else {
79         // use modified time as date taken time when date taken not set
80         data->SetDateTaken(data->GetDateModified() / MSEC_TO_SEC);
81     }
82 
83     strTemp = resultMap.at(Media::AV_KEY_TITLE);
84     if (!strTemp.empty()) {
85         data->SetTitle(strTemp);
86     }
87 }
88 
FillExtractedMetadata(const std::unordered_map<int32_t,std::string> & resultMap,std::unique_ptr<RingtoneMetadata> & data)89 void RingtoneMetadataExtractor::FillExtractedMetadata(const std::unordered_map<int32_t, std::string> &resultMap,
90     std::unique_ptr<RingtoneMetadata> &data)
91 {
92     PopulateExtractedAVMetadataOne(resultMap, data);
93     PopulateExtractedAVMetadataTwo(resultMap, data);
94 }
95 
ExtractAudioMetadata(std::unique_ptr<RingtoneMetadata> & data)96 int32_t RingtoneMetadataExtractor::ExtractAudioMetadata(std::unique_ptr<RingtoneMetadata> &data)
97 {
98     std::shared_ptr<Media::AVMetadataHelper> avMetadataHelper =
99         Media::AVMetadataHelperFactory::CreateAVMetadataHelper();
100     if (avMetadataHelper == nullptr) {
101         RINGTONE_ERR_LOG("AV RingtoneMetadata helper is null");
102         return E_AVMETADATA;
103     }
104 
105     string absFilePath;
106     if (!PathToRealPath(data->GetData(), absFilePath)) {
107         RINGTONE_ERR_LOG("AV RingtoneMetadata is not real path, file path: %{public}s", data->GetData().c_str());
108         return E_AVMETADATA;
109     }
110     if (absFilePath.empty()) {
111         RINGTONE_ERR_LOG("Failed to obtain the canonical path for source path: %{public}s %{public}d",
112             absFilePath.c_str(), errno);
113         return E_AVMETADATA;
114     }
115 
116     int32_t fd = open(absFilePath.c_str(), O_RDONLY);
117     if (fd <= 0) {
118         RINGTONE_ERR_LOG("Open file descriptor failed, errno = %{public}d", errno);
119         return E_SYSCALL;
120     }
121     struct stat64 st;
122     if (fstat64(fd, &st) != 0) {
123         RINGTONE_ERR_LOG("Get file state failed for the given fd");
124         (void)close(fd);
125         return E_SYSCALL;
126     }
127 
128     int32_t err = avMetadataHelper->SetSource(fd, 0, static_cast<int64_t>(st.st_size), Media::AV_META_USAGE_META_ONLY);
129     if (err != 0) {
130         RINGTONE_ERR_LOG("SetSource failed for the given file descriptor, err = %{public}d", err);
131         (void)close(fd);
132         return E_AVMETADATA;
133     } else {
134         std::unordered_map<int32_t, std::string> resultMap = avMetadataHelper->ResolveMetadata();
135         if (!resultMap.empty()) {
136             FillExtractedMetadata(resultMap, data);
137         }
138     }
139 
140     (void)close(fd);
141 
142     return E_OK;
143 }
144 
Extract(std::unique_ptr<RingtoneMetadata> & data)145 int32_t RingtoneMetadataExtractor::Extract(std::unique_ptr<RingtoneMetadata> &data)
146 {
147     return ExtractAudioMetadata(data);
148 }
149 } // namespace Media
150 } // namespace OHOS
151