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 "dfx_report.h"
17 #include "photo_asset_adapter.h"
18 #include "photo_proxy.h"
19 #include "camera_log.h"
20 #include "iservice_registry.h"
21 #include <cstdint>
22 #include "media_photo_asset_proxy.h"
23 #include "system_ability_definition.h"
24
25 namespace OHOS {
26 namespace CameraStandard {
27
28 Media::MediaLibraryManager *g_mediaLibraryManager = nullptr;
PhotoAssetAdapter(int32_t cameraShotType,int32_t uid,uint32_t callingTokenID)29 PhotoAssetAdapter::PhotoAssetAdapter(int32_t cameraShotType, int32_t uid, uint32_t callingTokenID)
30 {
31 CAMERA_SYNC_TRACE;
32 MEDIA_INFO_LOG("PhotoAssetAdapter ctor");
33 if (g_mediaLibraryManager == nullptr) {
34 g_mediaLibraryManager = Media::MediaLibraryManager::GetMediaLibraryManager();
35 CHECK_EXECUTE(g_mediaLibraryManager == nullptr, CameraReportUtils::GetInstance().ReportCameraCreateNullptr(
36 "PhotoAssetAdapter::PhotoAssetAdapter", "Media::MediaLibraryManager::GetMediaLibraryManager"));
37 CHECK_RETURN_ELOG(g_mediaLibraryManager == nullptr, "GetMediaLibraryManager failed!");
38 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
39 CHECK_RETURN_ELOG(samgr == nullptr, "Failed to get System ability manager!");
40 sptr<IRemoteObject> object = samgr->GetSystemAbility(CAMERA_SERVICE_ID);
41 CHECK_RETURN_ELOG(object == nullptr, "object is null!");
42 g_mediaLibraryManager->InitMediaLibraryManager(object);
43 }
44 const static int32_t INVALID_UID = -1;
45 const static int32_t BASE_USER_RANGE = 200000;
46 CHECK_PRINT_ELOG(uid <= INVALID_UID, "Get INVALID_UID UID %{public}d", uid);
47 userId_ = uid / BASE_USER_RANGE;
48 MEDIA_DEBUG_LOG("get uid:%{public}d, userId:%{public}d.", uid, userId_);
49 photoAssetProxy_ = g_mediaLibraryManager->CreatePhotoAssetProxy(
50 static_cast<Media::CameraShotType>(cameraShotType), uid, userId_, callingTokenID);
51 CHECK_EXECUTE(!photoAssetProxy_, CameraReportUtils::GetInstance().ReportCameraCreateNullptr(
52 "PhotoAssetAdapter::PhotoAssetAdapter", "Media::MediaLibraryManager::CreatePhotoAssetProxy"));
53 }
54 // LCOV_EXCL_START
AddPhotoProxy(sptr<Media::PhotoProxy> photoProxy)55 void PhotoAssetAdapter::AddPhotoProxy(sptr<Media::PhotoProxy> photoProxy)
56 {
57 if (photoProxy == nullptr) {
58 return;
59 }
60 CHECK_EXECUTE(photoAssetProxy_, photoAssetProxy_->AddPhotoProxy(photoProxy));
61 }
62
GetPhotoAssetUri()63 std::string PhotoAssetAdapter::GetPhotoAssetUri()
64 {
65 std::string res = "";
66 CHECK_RETURN_RET(!photoAssetProxy_, res);
67 res = photoAssetProxy_->GetPhotoAssetUri();
68 CHECK_EXECUTE(res == "", CameraReportUtils::GetInstance().ReportCameraGetNullStr(
69 "PhotoAssetAdapter::GetPhotoAssetUri", "Media::PhotoAssetProxy::GetPhotoAssetUri"));
70 return res;
71 }
72
GetVideoFd()73 int32_t PhotoAssetAdapter::GetVideoFd()
74 {
75 int32_t res = -1;
76 CHECK_RETURN_RET(!photoAssetProxy_, res);
77 res = photoAssetProxy_->GetVideoFd();
78 CHECK_EXECUTE(res == -1, CameraReportUtils::GetInstance().ReportCameraFail(
79 "PhotoAssetAdapter::GetVideoFd", "Media::PhotoAssetProxy::GetVideoFd"));
80 return res;
81 }
82
GetUserId()83 int32_t PhotoAssetAdapter::GetUserId()
84 {
85 return photoAssetProxy_ ? userId_ : -1;
86 }
87
NotifyVideoSaveFinished()88 void PhotoAssetAdapter::NotifyVideoSaveFinished()
89 {
90 CHECK_EXECUTE(photoAssetProxy_, photoAssetProxy_->NotifyVideoSaveFinished());
91 }
92 // LCOV_EXCL_STOP
createPhotoAssetIntf(int32_t cameraShotType,int32_t uid,uint32_t callingTokenID)93 extern "C" PhotoAssetIntf *createPhotoAssetIntf(int32_t cameraShotType, int32_t uid, uint32_t callingTokenID)
94 {
95 return new PhotoAssetAdapter(cameraShotType, uid, callingTokenID);
96 }
97
98 } // namespace AVSession
99 } // namespace OHOS