1 /*
2 * Copyright (c) 2023 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 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 <dlfcn.h>
17 #include "lpp_log.h"
18 #include "low_power_player_factory.h"
19 #include "lpp_sync_manager_adapter.h"
20 namespace OHOS {
21 namespace HDI {
22 namespace LowPowerPlayer {
23 namespace V1_0 {
24
25 static std::mutex mutex_;
26 static std::shared_ptr<void> libHandle_ = nullptr;
27 static GetAVCapabilityFunc capVdi_ = nullptr;
28
LowPowerPlayerFactoryImplGetInstance(void)29 extern "C" ILowPowerPlayerFactory* LowPowerPlayerFactoryImplGetInstance(void)
30 {
31 return new (std::nothrow) LowPowerPlayerFactory();
32 }
33
LowPowerPlayerFactoryImplRelease(void * ptr)34 extern "C" void LowPowerPlayerFactoryImplRelease(void* ptr)
35 {
36 delete (LowPowerPlayerFactory*)ptr;
37 }
38
CreateSyncMgr(sptr<ILppSyncManagerAdapter> & lppAdapter)39 int32_t LowPowerPlayerFactory::CreateSyncMgr(sptr<ILppSyncManagerAdapter> &lppAdapter)
40 {
41 sptr<LppSyncManagerAdapter> lppInstance = sptr<LppSyncManagerAdapter>::MakeSptr();
42 int32_t ret = lppInstance->Init();
43 CHECK_TRUE_RETURN_RET_LOG(ret != HDF_SUCCESS, HDF_FAILURE, "create vdi failed");
44 lppAdapter = lppInstance;
45 return HDF_SUCCESS;
46 }
47
CreateAudioSink(sptr<ILppAudioSinkAdapter> & audioSinkAdapter)48 int32_t LowPowerPlayerFactory::CreateAudioSink(sptr<ILppAudioSinkAdapter>& audioSinkAdapter)
49 {
50 return HDF_SUCCESS;
51 }
52
GetAVCapability(LppAVCap & avCap)53 int32_t LowPowerPlayerFactory::GetAVCapability(LppAVCap& avCap)
54 {
55 std::lock_guard<std::mutex> lock(mutex_);
56 if (libHandle_ == nullptr) {
57 void *handle = dlopen(LOW_POWER_PLAYER_VDI_LIBRARY, RTLD_LAZY);
58 CHECK_TRUE_RETURN_RET_LOG(handle == NULL, HDF_FAILURE, "dlopen failed, %{public}s", dlerror());
59 libHandle_ = std::shared_ptr<void>(handle, dlclose); // use smart pointer to manage library handle
60 }
61 if (capVdi_ == nullptr) {
62 capVdi_ = reinterpret_cast<GetAVCapabilityFunc>(dlsym(libHandle_.get(), "GetAVCapabilityVdi"));
63 CHECK_TRUE_RETURN_RET_LOG(capVdi_ == NULL, HDF_FAILURE, "createVdi_ dlsym failed, %{public}s", dlerror());
64 }
65 int32_t ret = capVdi_(avCap);
66 CHECK_TRUE_RETURN_RET_LOG(ret != HDF_SUCCESS, HDF_FAILURE, "GetAVCapability failed, %{public}s", strerror(errno));
67 return ret;
68 }
69
70 } // namespace V1_0
71 } // namespace LowPowerPlayer
72 } // namespace HDI
73 } // namespace OHOS