Yyylx_hw_lijin!25 代码整改
a6185370Add on Jan 13History
/*

 * Copyright (c) 2025 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */



#include "PlayerNative.h"

#include "AVCodecSampleLog.h"

#include "dfx/error/AVCodecSampleError.h"

#include <cstdint>

#include <hilog/log.h>

#include <uv.h>

#undef LOG_DOMAIN

#undef LOG_TAG

#define LOG_DOMAIN 0xFF00

#define LOG_TAG "Player"



napi_value PlayerNative::PlayHdr(napi_env env, napi_callback_info info) {

    SampleInfo sampleInfo;

    size_t argc = 3;

    napi_value args[3] = {nullptr};

    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);



    napi_get_value_int32(env, args[0], &sampleInfo.inputFd);

    napi_get_value_int64(env, args[1], &sampleInfo.inputFileOffset);

    napi_get_value_int64(env, args[2], &sampleInfo.inputFileSize);



    sampleInfo.window = NativeXComponentSample::PluginManager::GetInstance()->pluginWindow_;

    int32_t ret = Player::GetInstance().Init(sampleInfo);

    if (ret == AVCODEC_SAMPLE_OK) {

        Player::GetInstance().Start();

    }

    return nullptr;

}



napi_value PlayerNative::Pause(napi_env env, napi_callback_info info) {

    CHECK_AND_RETURN_RET_LOG(env != nullptr && info != nullptr, nullptr, "env or info is null");

    int32_t ret = Player::GetInstance().Pause();

    if (ret != AVCODEC_SAMPLE_OK) {

        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_DOMAIN, "Pause", "Player pause failed.");

    }

    return nullptr;

}



napi_value PlayerNative::Resume(napi_env env, napi_callback_info info) {

    CHECK_AND_RETURN_RET_LOG(env != nullptr && info != nullptr, nullptr, "env or info is null");

    int32_t ret = Player::GetInstance().Resume();

    if (ret != AVCODEC_SAMPLE_OK) {

        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_DOMAIN, "Resume", "Player resume failed.");

    }

    return nullptr;

}



napi_value PlayerNative::SwitchVideo(napi_env env, napi_callback_info info) {

    CHECK_AND_RETURN_RET_LOG(env != nullptr && info != nullptr, nullptr, "env or info is null");

    // delete all resource

    Player::GetInstance().StartRelease();

    return PlayHdr(env, info);

}



napi_value PlayerNative::StartRelease(napi_env env, napi_callback_info info) {

    OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "playerNative", "StartRelease123");

    // delete all resource

    Player::GetInstance().StartRelease();

    return nullptr;

}



EXTERN_C_START

static napi_value Init(napi_env env, napi_value exports) {

    napi_property_descriptor classProp[] = {

        {"playHdr", nullptr, PlayerNative::PlayHdr, nullptr, nullptr, nullptr, napi_default, nullptr},

        {"pause", nullptr, PlayerNative::Pause, nullptr, nullptr, nullptr, napi_default, nullptr},

        {"resume", nullptr, PlayerNative::Resume, nullptr, nullptr, nullptr, napi_default, nullptr},

        {"switchVideo", nullptr, PlayerNative::SwitchVideo, nullptr, nullptr, nullptr, napi_default, nullptr},

        {"startRelease", nullptr, PlayerNative::StartRelease, nullptr, nullptr, nullptr, napi_default, nullptr}};

    napi_define_properties(env, exports, sizeof(classProp) / sizeof(classProp[0]), classProp);

    NativeXComponentSample::PluginManager::GetInstance()->Export(env, exports);

    return exports;

}

EXTERN_C_END



static napi_module PlayerModule = {.nm_version = 1,

                                   .nm_flags = 0,

                                   .nm_filename = nullptr,

                                   .nm_register_func = Init,

                                   .nm_modname = "player",

                                   .nm_priv = ((void *)0),

                                   .reserved = {0}};



extern "C" __attribute__((constructor)) void RegisterPlayerModule(void) { napi_module_register(&PlayerModule); }