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 #include <iomanip>
16 #include <ctime>
17 #include "media_library_adapter.h"
18 #include "media_library_manager.h"
19 #include "media_log.h"
20 #include "ipc_skeleton.h"
21 #include "media_dfx.h"
22 #include "hitrace/tracechain.h"
23 #include "system_ability_definition.h"
24 #include "iservice_registry.h"
25
26 namespace {
27 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_RECORDER, "RecorderServer"};
28 }
29
30 namespace OHOS {
31 namespace Media {
32 namespace MeidaLibraryAdapter {
33 constexpr std::string_view prefix = "VID_";
34 constexpr std::string_view connector = "_";
35 class RecorderPhotoProxy : public PhotoProxy {
36 public:
RecorderPhotoProxy()37 RecorderPhotoProxy() { }
38
GetDisplayName()39 std::string GetDisplayName()
40 {
41 return GetTitle() + '.' + GetExtension();
42 }
43
SetDisplayName(const std::string & displayName)44 void SetDisplayName(const std::string &displayName)
45 {
46 displayName_ = displayName;
47 }
48
GetExtension()49 std::string GetExtension() override
50 {
51 return "mp4";
52 }
53
GetPhotoId()54 std::string GetPhotoId() override
55 {
56 return "";
57 }
58
GetDeferredProcType()59 DeferredProcType GetDeferredProcType() override
60 {
61 return DeferredProcType::BACKGROUND;
62 }
63
GetWidth()64 int32_t GetWidth() override
65 {
66 return 0;
67 }
68
GetHeight()69 int32_t GetHeight() override
70 {
71 return 0;
72 }
73
GetFileDataAddr()74 void* GetFileDataAddr() override
75 {
76 return fileDataAddr_;
77 }
78
GetFileSize()79 size_t GetFileSize() override
80 {
81 return 0;
82 }
83
Release()84 void Release() override
85 {
86 }
87
GetFormat()88 PhotoFormat GetFormat() override
89 {
90 return photoFormat_;
91 }
92
SetFormat(PhotoFormat format)93 void SetFormat(PhotoFormat format)
94 {
95 photoFormat_ = format;
96 }
97
GetPhotoQuality()98 PhotoQuality GetPhotoQuality() override
99 {
100 return PhotoQuality::HIGH;
101 }
102
GetLatitude()103 double GetLatitude() override
104 {
105 return 0.0;
106 }
107
GetLongitude()108 double GetLongitude() override
109 {
110 return 0.0;
111 }
GetTitle()112 std::string GetTitle() override
113 {
114 return displayName_;
115 }
116
GetShootingMode()117 int32_t GetShootingMode() override
118 {
119 return 0;
120 }
121
GetBurstKey()122 std::string GetBurstKey() override
123 {
124 return "";
125 }
126
IsCoverPhoto()127 bool IsCoverPhoto() override
128 {
129 return false;
130 }
131
GetCloudImageEnhanceFlag()132 uint32_t GetCloudImageEnhanceFlag() override
133 {
134 return 0;
135 };
136 private:
137 void *fileDataAddr_ = nullptr;
138 std::string displayName_;
139 PhotoFormat photoFormat_ = PhotoFormat::RGBA;
140 };
141
GetLocaltime(const time_t * clock,struct tm * result)142 struct tm *GetLocaltime(const time_t* clock, struct tm *result)
143 {
144 struct tm *ptr = localtime(clock);
145 if (!ptr) {
146 return nullptr;
147 }
148 *result = *ptr;
149 return result;
150 }
151
CreateDisplayName()152 std::string CreateDisplayName()
153 {
154 struct tm currentTime;
155 std::string formattedTime = "";
156
157 auto clock = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
158 struct tm* timeResult = GetLocaltime(&clock, ¤tTime);
159 int32_t yearWidth = 4;
160 int32_t otherWidth = 2;
161 int32_t startYear = 1900;
162 char placeholder = '0';
163 if (timeResult != nullptr) {
164 std::stringstream ss;
165 ss << prefix << std::setw(yearWidth) << std::setfill(placeholder) << currentTime.tm_year + startYear
166 << std::setw(otherWidth) << std::setfill(placeholder) << (currentTime.tm_mon + 1)
167 << std::setw(otherWidth) << std::setfill(placeholder) << currentTime.tm_mday
168 << connector << std::setw(otherWidth) << std::setfill(placeholder) << currentTime.tm_hour
169 << std::setw(otherWidth) << std::setfill(placeholder) << currentTime.tm_min
170 << std::setw(otherWidth) << std::setfill(placeholder) << currentTime.tm_sec;
171 formattedTime = ss.str();
172 } else {
173 MEDIA_LOGE("Failed to get current time.");
174 }
175 return formattedTime;
176 }
177
CreateMediaLibrary(int32_t & fd,std::string & uri)178 bool CreateMediaLibrary(int32_t &fd, std::string &uri)
179 {
180 MediaTrace trace("RecorderServer::CreateMediaLibrary");
181 MEDIA_LOGI("RecorderServer::CreateMediaLibrary");
182 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
183 CHECK_AND_RETURN_RET_LOG(samgr != nullptr, false, "Failed to get System ability manager");
184 auto object = samgr->GetSystemAbility(PLAYER_DISTRIBUTED_SERVICE_ID);
185 CHECK_AND_RETURN_RET_LOG(object != nullptr, false, "object is null");
186 auto mediaLibraryManager = Media::MediaLibraryManager::GetMediaLibraryManager();
187 CHECK_AND_RETURN_RET_LOG(mediaLibraryManager != nullptr, false,
188 "Error to init mediaLibraryManager");
189 mediaLibraryManager->InitMediaLibraryManager(object);
190 const static int32_t INVALID_UID = -1;
191 const static int32_t BASE_USER_RANGE = 200000;
192 int32_t uid = IPCSkeleton::GetCallingUid();
193 if (uid <= INVALID_UID) {
194 MEDIA_LOGD("Get INVALID_UID UID %{public}d", uid);
195 }
196 int32_t userId = uid / BASE_USER_RANGE;
197 MEDIA_LOGD("get uid:%{public}d, userId:%{public}d, tokenId:%{public}d", uid, userId,
198 IPCSkeleton::GetCallingTokenID());
199
200 auto photoAssetProxy = mediaLibraryManager->CreatePhotoAssetProxy(CameraShotType::VIDEO, uid, userId);
201 sptr<RecorderPhotoProxy> recorderPhotoProxy = new(std::nothrow) RecorderPhotoProxy();
202 CHECK_AND_RETURN_RET_LOG(recorderPhotoProxy != nullptr, false,
203 "Error to create recorderPhotoProxy");
204 recorderPhotoProxy->SetDisplayName(CreateDisplayName());
205 photoAssetProxy->AddPhotoProxy((sptr<PhotoProxy>&)recorderPhotoProxy);
206 uri = photoAssetProxy->GetPhotoAssetUri();
207 MEDIA_LOGD("video uri:%{public}s", uri.c_str());
208 fd = mediaLibraryManager->OpenAsset(uri, "rw");
209 return true;
210 }
211 } // namespace MeidaLibraryAdapter
212 } // namespace Media
213 } // namespace OHOS