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