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