• 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 
16 #include "media_asset_impl.h"
17 
18 #include "media_log.h"
19 #include <securec.h>
20 #include "nocopyable.h"
21 #include "userfile_manager_types.h"
22 
23 using namespace OHOS::Media;
24 
25 const static int64_t MILLI_TO_SECOND = 1000;
26 const static int32_t MAX_URI_LENGTH = 256;
27 const static int32_t MAX_DISPLAY_NAME_LENGTH = MAX_URI_LENGTH + 6;
28 const static int32_t MAX_TITLE_LENGTH = 256;
29 
30 const std::unordered_map<MediaType, MediaLibrary_MediaType> g_mediaTypeMapping = {
31     {MediaType::MEDIA_TYPE_IMAGE, MediaLibrary_MediaType::MEDIA_LIBRARY_IMAGE},
32     {MediaType::MEDIA_TYPE_VIDEO, MediaLibrary_MediaType::MEDIA_LIBRARY_VIDEO},
33 };
34 
35 const std::unordered_map<PhotoSubType, MediaLibrary_MediaSubType> g_photoSubTypeMapping = {
36     {PhotoSubType::DEFAULT, MediaLibrary_MediaSubType::MEDIA_LIBRARY_DEFAULT},
37     {PhotoSubType::CAMERA, MediaLibrary_MediaSubType::MEDIA_LIBRARY_DEFAULT},
38     {PhotoSubType::MOVING_PHOTO, MediaLibrary_MediaSubType::MEDIA_LIBRARY_MOVING_PHOTO},
39     {PhotoSubType::BURST, MediaLibrary_MediaSubType::MEDIA_LIBRARY_BURST},
40 };
41 
CreateMediaAsset(std::shared_ptr<FileAsset> fileAsset)42 std::shared_ptr<MediaAsset> MediaAssetFactory::CreateMediaAsset(
43     std::shared_ptr<FileAsset> fileAsset)
44 {
45     std::shared_ptr<MediaAssetImpl> impl = std::make_shared<MediaAssetImpl>(fileAsset);
46     CHECK_AND_PRINT_LOG(impl != nullptr, "Failed to create MediaAssetManagerImpl instance.");
47 
48     return impl;
49 }
50 
MediaAssetImpl(std::shared_ptr<FileAsset> fileAsset)51 MediaAssetImpl::MediaAssetImpl(std::shared_ptr<FileAsset> fileAsset)
52 {
53     MEDIA_DEBUG_LOG("MediaAssetImpl Constructor is called.");
54     fileAsset_ = fileAsset;
55     uri_ = new char[MAX_URI_LENGTH];
56     displayName_ = new char[MAX_DISPLAY_NAME_LENGTH];
57     title_ = new char[MAX_TITLE_LENGTH];
58 }
59 
~MediaAssetImpl()60 MediaAssetImpl::~MediaAssetImpl()
61 {
62     if (fileAsset_ != nullptr) {
63         fileAsset_ = nullptr;
64     }
65 
66     if (uri_ != nullptr) {
67         delete[] uri_;
68         uri_ = nullptr;
69     }
70 
71     if (displayName_ != nullptr) {
72         delete[] displayName_;
73         displayName_ = nullptr;
74     }
75 
76     if (title_ != nullptr) {
77         delete[] title_;
78         title_ = nullptr;
79     }
80 }
81 
GetUri(const char ** uri)82 MediaLibrary_ErrorCode MediaAssetImpl::GetUri(const char** uri)
83 {
84     if (uri_ == nullptr) {
85         uri_ = new(std::nothrow) char[MAX_URI_LENGTH];
86         CHECK_AND_RETURN_RET_LOG(uri_ != nullptr, MEDIA_LIBRARY_INTERNAL_SYSTEM_ERROR, "alloc memory failed!");
87     }
88     CHECK_AND_RETURN_RET_LOG(fileAsset_ != nullptr, MEDIA_LIBRARY_INTERNAL_SYSTEM_ERROR, "fileAsset is nullptr");
89     const std::string fileUri = fileAsset_->GetUri();
90     int32_t uriLen = static_cast<int32_t>(fileUri.length());
91     int32_t len = uriLen < MAX_URI_LENGTH ? uriLen : MAX_URI_LENGTH - 1;
92     strncpy_s(uri_, MAX_URI_LENGTH, fileUri.c_str(), len);
93     MEDIA_INFO_LOG("MediaAssetImpl::GetUri, uri: %{public}s, return uri: %{public}s",
94         fileUri.c_str(), uri_);
95     *uri = uri_;
96     return MEDIA_LIBRARY_OK;
97 }
98 
GetMediaType(MediaLibrary_MediaType * mediaType)99 MediaLibrary_ErrorCode MediaAssetImpl::GetMediaType(MediaLibrary_MediaType* mediaType)
100 {
101     CHECK_AND_RETURN_RET_LOG(fileAsset_ != nullptr, MEDIA_LIBRARY_INTERNAL_SYSTEM_ERROR, "fileAsset is nullptr");
102     MediaType type = fileAsset_->GetMediaType();
103     MEDIA_INFO_LOG("GetMediaType type: %{public}d", static_cast<int32_t>(type));
104     auto itr = g_mediaTypeMapping.find(type);
105     if (itr != g_mediaTypeMapping.end()) {
106         *mediaType = itr->second;
107         return MEDIA_LIBRARY_OK;
108     }
109     return MEDIA_LIBRARY_INTERNAL_SYSTEM_ERROR;
110 }
111 
GetMediaSubType(MediaLibrary_MediaSubType * mediaSubType)112 MediaLibrary_ErrorCode MediaAssetImpl::GetMediaSubType(MediaLibrary_MediaSubType* mediaSubType)
113 {
114     CHECK_AND_RETURN_RET_LOG(fileAsset_ != nullptr, MEDIA_LIBRARY_INTERNAL_SYSTEM_ERROR, "fileAsset is nullptr");
115     PhotoSubType subType = static_cast<PhotoSubType>(fileAsset_->GetPhotoSubType());
116     MEDIA_INFO_LOG("GetMediaSubType subType: %{public}d", static_cast<int32_t>(subType));
117     auto itr = g_photoSubTypeMapping.find(subType);
118     if (itr != g_photoSubTypeMapping.end()) {
119         *mediaSubType = itr->second;
120         return MEDIA_LIBRARY_OK;
121     }
122     return MEDIA_LIBRARY_INTERNAL_SYSTEM_ERROR;
123 }
124 
GetDisplayName(const char ** displayName)125 MediaLibrary_ErrorCode MediaAssetImpl::GetDisplayName(const char** displayName)
126 {
127     if (displayName_ == nullptr) {
128         displayName_ = new(std::nothrow) char[MAX_DISPLAY_NAME_LENGTH];
129         CHECK_AND_RETURN_RET_LOG(displayName_ != nullptr, MEDIA_LIBRARY_INTERNAL_SYSTEM_ERROR, "alloc memory failed!");
130     }
131     CHECK_AND_RETURN_RET_LOG(fileAsset_ != nullptr, MEDIA_LIBRARY_INTERNAL_SYSTEM_ERROR, "fileAsset is nullptr");
132     const std::string display = fileAsset_->GetDisplayName();
133     int32_t displayNameLen = static_cast<int32_t>(display.length());
134     int32_t len = displayNameLen < MAX_DISPLAY_NAME_LENGTH ? displayNameLen : MAX_DISPLAY_NAME_LENGTH - 1;
135     strncpy_s(displayName_, MAX_DISPLAY_NAME_LENGTH, display.c_str(), len);
136     MEDIA_INFO_LOG("MediaAssetImpl::GetDisplayName, display name: %{public}s, return display name: %{public}s",
137         display.c_str(), displayName_);
138     *displayName = displayName_;
139     return MEDIA_LIBRARY_OK;
140 }
141 
GetSize(uint32_t * size)142 MediaLibrary_ErrorCode MediaAssetImpl::GetSize(uint32_t* size)
143 {
144     CHECK_AND_RETURN_RET_LOG(fileAsset_ != nullptr, MEDIA_LIBRARY_INTERNAL_SYSTEM_ERROR, "fileAsset is nullptr");
145     *size = static_cast<uint32_t>(fileAsset_->GetSize());
146     return MEDIA_LIBRARY_OK;
147 }
148 
GetDateAdded(uint32_t * dateAdded)149 MediaLibrary_ErrorCode MediaAssetImpl::GetDateAdded(uint32_t* dateAdded)
150 {
151     CHECK_AND_RETURN_RET_LOG(fileAsset_ != nullptr, MEDIA_LIBRARY_INTERNAL_SYSTEM_ERROR, "fileAsset is nullptr");
152     *dateAdded = static_cast<uint32_t>(fileAsset_->GetDateAdded() / MILLI_TO_SECOND);
153     return MEDIA_LIBRARY_OK;
154 }
155 
GetDateModified(uint32_t * dateModified)156 MediaLibrary_ErrorCode MediaAssetImpl::GetDateModified(uint32_t* dateModified)
157 {
158     CHECK_AND_RETURN_RET_LOG(fileAsset_ != nullptr, MEDIA_LIBRARY_INTERNAL_SYSTEM_ERROR, "fileAsset is nullptr");
159     *dateModified = static_cast<uint32_t>(fileAsset_->GetDateModified() / MILLI_TO_SECOND);
160     return MEDIA_LIBRARY_OK;
161 }
162 
GetDateAddedMs(uint32_t * dateAddedMs)163 MediaLibrary_ErrorCode MediaAssetImpl::GetDateAddedMs(uint32_t* dateAddedMs)
164 {
165     CHECK_AND_RETURN_RET_LOG(fileAsset_ != nullptr, MEDIA_LIBRARY_INTERNAL_SYSTEM_ERROR, "fileAsset is nullptr");
166     *dateAddedMs = static_cast<uint32_t>(fileAsset_->GetDateModified());
167     return MEDIA_LIBRARY_OK;
168 }
169 
GetDateModifiedMs(uint32_t * dateModifiedMs)170 MediaLibrary_ErrorCode MediaAssetImpl::GetDateModifiedMs(uint32_t* dateModifiedMs)
171 {
172     CHECK_AND_RETURN_RET_LOG(fileAsset_ != nullptr, MEDIA_LIBRARY_INTERNAL_SYSTEM_ERROR, "fileAsset is nullptr");
173     *dateModifiedMs = static_cast<uint32_t>(fileAsset_->GetDateModified());
174     return MEDIA_LIBRARY_OK;
175 }
176 
GetDateTaken(uint32_t * dateTaken)177 MediaLibrary_ErrorCode MediaAssetImpl::GetDateTaken(uint32_t* dateTaken)
178 {
179     CHECK_AND_RETURN_RET_LOG(fileAsset_ != nullptr, MEDIA_LIBRARY_INTERNAL_SYSTEM_ERROR, "fileAsset is nullptr");
180     *dateTaken = static_cast<uint32_t>(fileAsset_->GetDateTaken());
181     return MEDIA_LIBRARY_OK;
182 }
183 
GetFileAssetInstance() const184 std::shared_ptr<FileAsset> MediaAssetImpl::GetFileAssetInstance() const
185 {
186     return fileAsset_;
187 }
188 
GetDuration(uint32_t * duration)189 MediaLibrary_ErrorCode MediaAssetImpl::GetDuration(uint32_t* duration)
190 {
191     CHECK_AND_RETURN_RET_LOG(fileAsset_ != nullptr, MEDIA_LIBRARY_INTERNAL_SYSTEM_ERROR, "fileAsset is nullptr");
192     *duration  = static_cast<uint32_t>(fileAsset_->GetDuration());
193     return MEDIA_LIBRARY_OK;
194 }
195 
GetWidth(uint32_t * width)196 MediaLibrary_ErrorCode MediaAssetImpl::GetWidth(uint32_t* width)
197 {
198     CHECK_AND_RETURN_RET_LOG(fileAsset_ != nullptr, MEDIA_LIBRARY_INTERNAL_SYSTEM_ERROR, "fileAsset is nullptr");
199     *width = static_cast<uint32_t>(fileAsset_->GetWidth());
200     return MEDIA_LIBRARY_OK;
201 }
202 
GetHeight(uint32_t * height)203 MediaLibrary_ErrorCode MediaAssetImpl::GetHeight(uint32_t* height)
204 {
205     CHECK_AND_RETURN_RET_LOG(fileAsset_ != nullptr, MEDIA_LIBRARY_INTERNAL_SYSTEM_ERROR, "fileAsset is nullptr");
206     *height = static_cast<uint32_t>(fileAsset_->GetHeight());
207     return MEDIA_LIBRARY_OK;
208 }
209 
GetOrientation(uint32_t * orientation)210 MediaLibrary_ErrorCode MediaAssetImpl::GetOrientation(uint32_t* orientation)
211 {
212     CHECK_AND_RETURN_RET_LOG(fileAsset_ != nullptr, MEDIA_LIBRARY_INTERNAL_SYSTEM_ERROR, "fileAsset is nullptr");
213     *orientation = static_cast<uint32_t>(fileAsset_->GetOrientation());
214     return MEDIA_LIBRARY_OK;
215 }
216 
IsFavorite(uint32_t * favorite)217 MediaLibrary_ErrorCode MediaAssetImpl::IsFavorite(uint32_t* favorite)
218 {
219     CHECK_AND_RETURN_RET_LOG(fileAsset_ != nullptr, MEDIA_LIBRARY_INTERNAL_SYSTEM_ERROR, "fileAsset is nullptr");
220     *favorite = static_cast<uint32_t>(fileAsset_->IsFavorite());
221     return MEDIA_LIBRARY_OK;
222 }
223 
GetTitle(const char ** title)224 MediaLibrary_ErrorCode MediaAssetImpl::GetTitle(const char** title)
225 {
226     if (title_ == nullptr) {
227         title_ = new(std::nothrow) char[MAX_TITLE_LENGTH];
228         CHECK_AND_RETURN_RET_LOG(title_ != nullptr, MEDIA_LIBRARY_INTERNAL_SYSTEM_ERROR, "alloc memory failed!");
229     }
230     CHECK_AND_RETURN_RET_LOG(fileAsset_ != nullptr, MEDIA_LIBRARY_INTERNAL_SYSTEM_ERROR, "fileAsset is nullptr");
231     const std::string titleStr = fileAsset_->GetTitle();
232     int32_t titleLen = static_cast<int32_t>(titleStr.length());
233     int32_t len = titleLen < MAX_TITLE_LENGTH ? titleLen : MAX_TITLE_LENGTH - 1;
234     strncpy_s(title_, MAX_TITLE_LENGTH, titleStr.c_str(), len);
235     MEDIA_INFO_LOG("GetTitle, title: %{public}s, return title: %{public}s", titleStr.c_str(), title_);
236     *title = title_;
237     return MEDIA_LIBRARY_OK;
238 }
239