180922886Sopenharmony_ci/*
280922886Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
380922886Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
480922886Sopenharmony_ci * you may not use this file except in compliance with the License.
580922886Sopenharmony_ci * You may obtain a copy of the License at
680922886Sopenharmony_ci *
780922886Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
880922886Sopenharmony_ci *
980922886Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1080922886Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1180922886Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1280922886Sopenharmony_ci * See the License for the specific language governing permissions and
1380922886Sopenharmony_ci * limitations under the License.
1480922886Sopenharmony_ci */
1580922886Sopenharmony_ci
1680922886Sopenharmony_ci#include "ability_manager_adapter.h"
1780922886Sopenharmony_ci
1880922886Sopenharmony_ci#include "avsession_dynamic_loader.h"
1980922886Sopenharmony_ci#include "avsession_errors.h"
2080922886Sopenharmony_ci#include "avsession_log.h"
2180922886Sopenharmony_ci#include "bundle_status_adapter.h"
2280922886Sopenharmony_ci#include "ability_connect_helper.h"
2380922886Sopenharmony_ci
2480922886Sopenharmony_cinamespace OHOS::AVSession {
2580922886Sopenharmony_cistatic const std::string AVSESSION_DYNAMIC_INSIGHT_LIBRARY_PATH = std::string("libavsession_dynamic_insight.z.so");
2680922886Sopenharmony_ci
2780922886Sopenharmony_ciAbilityManagerAdapter::AbilityManagerAdapter(const std::string& bundleName, const std::string& abilityName)
2880922886Sopenharmony_ci{
2980922886Sopenharmony_ci    SLOGI("construct bundleName=%{public}s abilityName=%{public}s", bundleName.c_str(), abilityName.c_str());
3080922886Sopenharmony_ci    bundleName_ = bundleName;
3180922886Sopenharmony_ci    abilityName_ = abilityName;
3280922886Sopenharmony_ci}
3380922886Sopenharmony_ci
3480922886Sopenharmony_ciAbilityManagerAdapter::~AbilityManagerAdapter()
3580922886Sopenharmony_ci{}
3680922886Sopenharmony_ci
3780922886Sopenharmony_ci__attribute__((no_sanitize("cfi"))) int32_t AbilityManagerAdapter::StartAbilityByCall(std::string& sessionId)
3880922886Sopenharmony_ci{
3980922886Sopenharmony_ci    if (status_.load() != Status::ABILITY_STATUS_INIT) {
4080922886Sopenharmony_ci        SLOGE("Start Ability is running");
4180922886Sopenharmony_ci        return ERR_START_ABILITY_IS_RUNNING;
4280922886Sopenharmony_ci    }
4380922886Sopenharmony_ci    status_.store(Status::ABILITY_STATUS_RUNNING);
4480922886Sopenharmony_ci    int32_t ret = AVSESSION_ERROR;
4580922886Sopenharmony_ci    bool isSupport = false;
4680922886Sopenharmony_ci
4780922886Sopenharmony_ci    std::unique_ptr<AVSessionDynamicLoader> dynamicLoader = std::make_unique<AVSessionDynamicLoader>();
4880922886Sopenharmony_ci    typedef bool (*IsSupportPlayIntentFunc)(const std::string& bundleName, const std::string& assetId);
4980922886Sopenharmony_ci    IsSupportPlayIntentFunc isSupportPlayIntent =
5080922886Sopenharmony_ci        reinterpret_cast<IsSupportPlayIntentFunc>(dynamicLoader->GetFuntion(
5180922886Sopenharmony_ci            AVSESSION_DYNAMIC_INSIGHT_LIBRARY_PATH, "IsSupportPlayIntent"));
5280922886Sopenharmony_ci    if (isSupportPlayIntent) {
5380922886Sopenharmony_ci        isSupport = (*isSupportPlayIntent)(bundleName_, "");
5480922886Sopenharmony_ci    }
5580922886Sopenharmony_ci
5680922886Sopenharmony_ci    if (isSupport) {
5780922886Sopenharmony_ci        SLOGI("Start Ability mediaintent");
5880922886Sopenharmony_ci        typedef int32_t (*StartAVPlaybackFunc)(const std::string& bundleName, const std::string& assetId);
5980922886Sopenharmony_ci        StartAVPlaybackFunc startAVPlayback =
6080922886Sopenharmony_ci            reinterpret_cast<StartAVPlaybackFunc>(dynamicLoader->GetFuntion(
6180922886Sopenharmony_ci                AVSESSION_DYNAMIC_INSIGHT_LIBRARY_PATH, "StartAVPlayback"));
6280922886Sopenharmony_ci        if (startAVPlayback) {
6380922886Sopenharmony_ci            ret = (*startAVPlayback)(bundleName_, "");
6480922886Sopenharmony_ci        }
6580922886Sopenharmony_ci    } else {
6680922886Sopenharmony_ci        ret = AbilityConnectHelper::GetInstance().StartAbilityByCall(bundleName_, abilityName_);
6780922886Sopenharmony_ci    }
6880922886Sopenharmony_ci    if (ret != AVSESSION_SUCCESS) {
6980922886Sopenharmony_ci        SLOGE("Start Ability failed: %{public}d", ret);
7080922886Sopenharmony_ci        status_.store(Status::ABILITY_STATUS_INIT);
7180922886Sopenharmony_ci        return ret;
7280922886Sopenharmony_ci    }
7380922886Sopenharmony_ci
7480922886Sopenharmony_ci    WaitForTimeout(ABILITY_START_TIMEOUT_MS);
7580922886Sopenharmony_ci    ret = ERR_START_ABILITY_TIMEOUT;
7680922886Sopenharmony_ci    if (status_.load() == Status::ABILITY_STATUS_SUCCESS) {
7780922886Sopenharmony_ci        ret = AVSESSION_SUCCESS;
7880922886Sopenharmony_ci        sessionId = sessionId_;
7980922886Sopenharmony_ci    }
8080922886Sopenharmony_ci    status_.store(Status::ABILITY_STATUS_INIT);
8180922886Sopenharmony_ci    return ret;
8280922886Sopenharmony_ci}
8380922886Sopenharmony_ci
8480922886Sopenharmony_civoid AbilityManagerAdapter::StartAbilityByCallDone(const std::string& sessionId)
8580922886Sopenharmony_ci{
8680922886Sopenharmony_ci    if (status_.load() != Status::ABILITY_STATUS_RUNNING) {
8780922886Sopenharmony_ci        SLOGI("no need to notify");
8880922886Sopenharmony_ci        return;
8980922886Sopenharmony_ci    }
9080922886Sopenharmony_ci    sessionId_ = sessionId;
9180922886Sopenharmony_ci    syncCon_.notify_one();
9280922886Sopenharmony_ci}
9380922886Sopenharmony_ci
9480922886Sopenharmony_ci// LCOV_EXCL_START
9580922886Sopenharmony_civoid AbilityManagerAdapter::WaitForTimeout(uint32_t timeout)
9680922886Sopenharmony_ci{
9780922886Sopenharmony_ci    std::unique_lock<std::mutex> lock(syncMutex_);
9880922886Sopenharmony_ci    auto waitStatus = syncCon_.wait_for(lock, std::chrono::milliseconds(timeout));
9980922886Sopenharmony_ci    if (waitStatus == std::cv_status::timeout) {
10080922886Sopenharmony_ci        SLOGE("StartAbilityByCall timeout");
10180922886Sopenharmony_ci        status_.store(Status::ABILITY_STATUS_FAILED);
10280922886Sopenharmony_ci        return;
10380922886Sopenharmony_ci    }
10480922886Sopenharmony_ci    status_.store(Status::ABILITY_STATUS_SUCCESS);
10580922886Sopenharmony_ci}
10680922886Sopenharmony_ci// LCOV_EXCL_STOP
10780922886Sopenharmony_ci} // namespace OHOS::AVSession
108