180922886Sopenharmony_ci/*
280922886Sopenharmony_ci * Copyright (c) 2022-2023 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#ifndef OHOS_AVSESSION_TRACE_H
1780922886Sopenharmony_ci#define OHOS_AVSESSION_TRACE_H
1880922886Sopenharmony_ci
1980922886Sopenharmony_ci#include <string>
2080922886Sopenharmony_ci
2180922886Sopenharmony_ci#ifdef ENBABLE_AVSESSION_TRACE_CONTROL
2280922886Sopenharmony_ci#include "hitrace_meter.h"
2380922886Sopenharmony_ci#endif
2480922886Sopenharmony_ci
2580922886Sopenharmony_ci#ifdef ENBABLE_AVSESSION_TRACE_CONTROL
2680922886Sopenharmony_ci#define AVSESSION_TRACE_SYNC_START(traceName) AVSessionTrace trace(traceName)
2780922886Sopenharmony_ci#define AVSESSION_TRACE_ASYNC_START(traceName, taskId) AVSessionTrace::TraceBegin(traceName, taskId)
2880922886Sopenharmony_ci#define AVSESSION_TRACE_ASYNC_END(traceName, taskId) AVSessionTrace::TraceEnd(traceName, taskId)
2980922886Sopenharmony_ci#else
3080922886Sopenharmony_ci#define AVSESSION_TRACE_SYNC_START(...)
3180922886Sopenharmony_ci#define AVSESSION_TRACE_ASYNC_START(...)
3280922886Sopenharmony_ci#define AVSESSION_TRACE_ASYNC_END(...)
3380922886Sopenharmony_ci
3480922886Sopenharmony_ci#endif
3580922886Sopenharmony_cinamespace OHOS::AVSession {
3680922886Sopenharmony_cienum AVSessionTraceTaskId : int32_t {
3780922886Sopenharmony_ci    INVALID_TASK_ID = -1,
3880922886Sopenharmony_ci    NAPI_SET_AV_META_DATA_TASK_ID = 0,
3980922886Sopenharmony_ci    NAPI_SET_AV_PLAYBACK_STATE_TASK_ID = 1,
4080922886Sopenharmony_ci    NAPI_SEND_AV_KEY_EVENT_TASK_ID = 2,
4180922886Sopenharmony_ci    NAPI_SEND_CONTROL_COMMAND_TASK_ID = 3,
4280922886Sopenharmony_ci    NAPI_CREATE_AVSESSION_TASK_ID = 4,
4380922886Sopenharmony_ci    NAPI_CREATE_CONTROLLER_TASK_ID = 5,
4480922886Sopenharmony_ci    NAPI_SEND_SYSTEM_AV_KEY_EVENT_TASK_ID = 6,
4580922886Sopenharmony_ci    NAPI_SEND_SYSTEM_CONTROL_COMMAND_TASK_ID = 7,
4680922886Sopenharmony_ci    NAPI_CAST_AUDIO_TASK_ID = 8,
4780922886Sopenharmony_ci    NAPI_SET_SESSION_EVENT_TASK_ID = 9,
4880922886Sopenharmony_ci    NAPI_SEND_COMMON_COMMAND_TASK_ID = 10,
4980922886Sopenharmony_ci    NAPI_SKIP_TO_QUEUE_ITEM_TASK_ID = 11,
5080922886Sopenharmony_ci    NAPI_SET_AV_QUEUE_TITLE_TASK_ID = 12,
5180922886Sopenharmony_ci    NAPI_SET_AV_QUEUE_ITEMS_TASK_ID = 13,
5280922886Sopenharmony_ci    NAPI_SET_EXTRAS_TASK_ID = 14,
5380922886Sopenharmony_ci    NAPI_START_CAST_DISCOVERY_TASK_ID = 15,
5480922886Sopenharmony_ci    NAPI_STOP_CAST_DISCOVERY_TASK_ID = 16,
5580922886Sopenharmony_ci    NAPI_SET_DISCOVERABLE_TASK_ID = 17,
5680922886Sopenharmony_ci    NAPI_START_CAST_TASK_ID = 18,
5780922886Sopenharmony_ci    NAPI_STOP_CAST_TASK_ID = 19,
5880922886Sopenharmony_ci    NAPI_CREATE_CAST_CONTROLLER_TASK_ID = 20,
5980922886Sopenharmony_ci    NAPI_CAST_CONTROLLER_SEND_CONTROL_COMMAND_TASK_ID = 21,
6080922886Sopenharmony_ci    NAPI_CAST_CONTROLLER_START_TASK_ID = 22,
6180922886Sopenharmony_ci    NAPI_CAST_CONTROLLER_PREPARE_TASK_ID = 23,
6280922886Sopenharmony_ci    NAPI_CAST_CONTROLLER_UPDATE_MEDIA_INFO_TASK_ID = 24,
6380922886Sopenharmony_ci    NAPI_CAST_CONTROLLER_GET_DURATION_TASK_ID = 25,
6480922886Sopenharmony_ci    NAPI_CAST_CONTROLLER_GET_POSITION_TASK_ID = 26,
6580922886Sopenharmony_ci    NAPI_CAST_CONTROLLER_GET_VOLUME_TASK_ID = 27,
6680922886Sopenharmony_ci    NAPI_CAST_CONTROLLER_GET_LOOPMODE_TASK_ID = 28,
6780922886Sopenharmony_ci    NAPI_CAST_CONTROLLER_GET_PLAY_SPEED_TASK_ID = 29,
6880922886Sopenharmony_ci    NAPI_CAST_CONTROLLER_GET_PLAY_STATE_TASK_ID = 30,
6980922886Sopenharmony_ci    NAPI_CAST_CONTROLLER_GET_CURRENT_ITEM_TASK_ID = 31,
7080922886Sopenharmony_ci    NAPI_CAST_CONTROLLER_SET_DISPLAY_SURFACE_TASK_ID = 32,
7180922886Sopenharmony_ci    NAPI_SET_AVCALL_META_DATA_TASK_ID = 33,
7280922886Sopenharmony_ci    NAPI_SET_AV_CALL_STATE_TASK_ID = 34,
7380922886Sopenharmony_ci    NAPI_PROVIDE_KEY_RESPONSE_TASK_ID = 35,
7480922886Sopenharmony_ci    NAPI_START_DEVICE_LOGGING_TASK_ID = 36,
7580922886Sopenharmony_ci    NAPI_STOP_DEVICE_LOGGING_TASK_ID = 37,
7680922886Sopenharmony_ci};
7780922886Sopenharmony_ci#ifdef ENBABLE_AVSESSION_TRACE_CONTROL
7880922886Sopenharmony_ciclass __attribute__((visibility("default"))) AVSessionTrace {
7980922886Sopenharmony_cipublic:
8080922886Sopenharmony_ci    explicit AVSessionTrace(const std::string& traceName)
8180922886Sopenharmony_ci    {
8280922886Sopenharmony_ci        std::string avSessionTrace("AVSession:");
8380922886Sopenharmony_ci        avSessionTrace += traceName;
8480922886Sopenharmony_ci        StartTrace(HITRACE_TAG_ZMEDIA, avSessionTrace);
8580922886Sopenharmony_ci        isSync_ = true;
8680922886Sopenharmony_ci    }
8780922886Sopenharmony_ci
8880922886Sopenharmony_ci    static void TraceBegin(const std::string& traceName, int32_t taskId)
8980922886Sopenharmony_ci    {
9080922886Sopenharmony_ci        if (taskId <= INVALID_TASK_ID) {
9180922886Sopenharmony_ci            return;
9280922886Sopenharmony_ci        }
9380922886Sopenharmony_ci        std::string traceBegin("AVSession:");
9480922886Sopenharmony_ci        traceBegin += traceName;
9580922886Sopenharmony_ci        StartAsyncTrace(HITRACE_TAG_ZMEDIA, traceBegin, taskId);
9680922886Sopenharmony_ci    }
9780922886Sopenharmony_ci
9880922886Sopenharmony_ci    static void TraceEnd(const std::string& traceName, int32_t taskId)
9980922886Sopenharmony_ci    {
10080922886Sopenharmony_ci        if (taskId <= INVALID_TASK_ID) {
10180922886Sopenharmony_ci            return;
10280922886Sopenharmony_ci        }
10380922886Sopenharmony_ci        std::string traceEnd("AVSession:");
10480922886Sopenharmony_ci        traceEnd += traceName;
10580922886Sopenharmony_ci        FinishAsyncTrace(HITRACE_TAG_ZMEDIA, traceEnd, taskId);
10680922886Sopenharmony_ci    }
10780922886Sopenharmony_ci
10880922886Sopenharmony_ci    ~AVSessionTrace()
10980922886Sopenharmony_ci    {
11080922886Sopenharmony_ci        if (isSync_) {
11180922886Sopenharmony_ci            FinishTrace(HITRACE_TAG_ZMEDIA);
11280922886Sopenharmony_ci        }
11380922886Sopenharmony_ci    }
11480922886Sopenharmony_ci
11580922886Sopenharmony_ci    AVSessionTrace(const AVSessionTrace&) = delete;
11680922886Sopenharmony_ci    AVSessionTrace& operator = (const AVSessionTrace&) = delete;
11780922886Sopenharmony_ci
11880922886Sopenharmony_ci    AVSessionTrace(AVSessionTrace&&) = delete;
11980922886Sopenharmony_ci    AVSessionTrace& operator = (AVSessionTrace&&) = delete;
12080922886Sopenharmony_ciprivate:
12180922886Sopenharmony_ci    bool isSync_ = false;
12280922886Sopenharmony_ci};
12380922886Sopenharmony_ci#endif
12480922886Sopenharmony_ci} // namespace OHOS::AVSession
12580922886Sopenharmony_ci#endif // OHOS_AVSESSION_TRACE_H