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#ifndef OHOS_AVSESSION_SYSEVENT_H
1780922886Sopenharmony_ci#define OHOS_AVSESSION_SYSEVENT_H
1880922886Sopenharmony_ci
1980922886Sopenharmony_ci#ifdef ENABLE_AVSESSION_SYSEVENT_CONTROL
2080922886Sopenharmony_ci#include <mutex>
2180922886Sopenharmony_ci#include "hisysevent.h"
2280922886Sopenharmony_ci#include "timer.h"
2380922886Sopenharmony_ci#endif
2480922886Sopenharmony_ci
2580922886Sopenharmony_cinamespace OHOS::AVSession {
2680922886Sopenharmony_cienum class Operation {
2780922886Sopenharmony_ci    OPT_PLAY = 0,
2880922886Sopenharmony_ci    OPT_PAUSE,
2980922886Sopenharmony_ci    OPT_STOP,
3080922886Sopenharmony_ci    OPT_PLAY_NEXT,
3180922886Sopenharmony_ci    OPT_PLAY_PREVIOUS,
3280922886Sopenharmony_ci    OPT_FAST_FORWARD,
3380922886Sopenharmony_ci    OPT_REWIND,
3480922886Sopenharmony_ci    OPT_SEEK,
3580922886Sopenharmony_ci    OPT_SET_SPEED,
3680922886Sopenharmony_ci    OPT_SET_LOOP_MODE,
3780922886Sopenharmony_ci    OPT_TOGGLE_FAVORITE,
3880922886Sopenharmony_ci    OPT_MEDIA_KEY_EVENT,
3980922886Sopenharmony_ci    OPT_OUTPUT_DEVICE_CHANGED,
4080922886Sopenharmony_ci    OPT_COMMAND_MAX,
4180922886Sopenharmony_ci    OPT_CREATE_SESSION,
4280922886Sopenharmony_ci    OPT_DELETE_SESSION,
4380922886Sopenharmony_ci    OPT_ALL_CTRL_COMMAND,
4480922886Sopenharmony_ci    OPT_SUCCESS_CTRL_COMMAND,
4580922886Sopenharmony_ci};
4680922886Sopenharmony_ci
4780922886Sopenharmony_ci#ifdef ENABLE_AVSESSION_SYSEVENT_CONTROL
4880922886Sopenharmony_ciclass AVSessionSysEvent {
4980922886Sopenharmony_cipublic:
5080922886Sopenharmony_ci    struct LifeCycleInfo {
5180922886Sopenharmony_ci        std::string bundleName_;
5280922886Sopenharmony_ci        bool appStatus_;
5380922886Sopenharmony_ci        int32_t sessionType_;
5480922886Sopenharmony_ci        bool isCreateSession_;
5580922886Sopenharmony_ci    };
5680922886Sopenharmony_ci
5780922886Sopenharmony_ci    struct ControllerCommandInfo {
5880922886Sopenharmony_ci        std::string bundleName_;
5980922886Sopenharmony_ci        pid_t controllerPid_;
6080922886Sopenharmony_ci        int32_t controllerCmd_;
6180922886Sopenharmony_ci    };
6280922886Sopenharmony_ci
6380922886Sopenharmony_ci    static AVSessionSysEvent& GetInstance();
6480922886Sopenharmony_ci
6580922886Sopenharmony_ci    template<typename... Types>
6680922886Sopenharmony_ci    static int HiSysWriteFault(const std::string& eventName, Types... keyValues)
6780922886Sopenharmony_ci    {
6880922886Sopenharmony_ci        return HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::AV_SESSION, eventName,
6980922886Sopenharmony_ci            OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, keyValues...);
7080922886Sopenharmony_ci    }
7180922886Sopenharmony_ci
7280922886Sopenharmony_ci    template<typename... Types>
7380922886Sopenharmony_ci    static int HiSysWriteStatistic(const std::string& eventName, Types... keyValues)
7480922886Sopenharmony_ci    {
7580922886Sopenharmony_ci        return HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::AV_SESSION, eventName,
7680922886Sopenharmony_ci            OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC, keyValues...);
7780922886Sopenharmony_ci    }
7880922886Sopenharmony_ci
7980922886Sopenharmony_ci    template<typename... Types>
8080922886Sopenharmony_ci    static int HiSysWriteSecurity(const std::string& eventName, Types... keyValues)
8180922886Sopenharmony_ci    {
8280922886Sopenharmony_ci        return HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::AV_SESSION, eventName,
8380922886Sopenharmony_ci            OHOS::HiviewDFX::HiSysEvent::EventType::SECURITY, keyValues...);
8480922886Sopenharmony_ci    }
8580922886Sopenharmony_ci
8680922886Sopenharmony_ci    template<typename... Types>
8780922886Sopenharmony_ci    static int HiSysWriteBehavior(const std::string& eventName, Types... keyValues)
8880922886Sopenharmony_ci    {
8980922886Sopenharmony_ci        return HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::AV_SESSION, eventName,
9080922886Sopenharmony_ci            OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, keyValues...);
9180922886Sopenharmony_ci    }
9280922886Sopenharmony_ci
9380922886Sopenharmony_ci    void Reset();
9480922886Sopenharmony_ci    void Regiter();
9580922886Sopenharmony_ci    void Unregister();
9680922886Sopenharmony_ci    void AddOperationCount(Operation operation);
9780922886Sopenharmony_ci    void AddLifeCycleInfo(const std::string& bundleName, bool appStatus,
9880922886Sopenharmony_ci        const int32_t& sessionType, bool isCreateSession);
9980922886Sopenharmony_ci    void AddControllerCommandInfo(const std::string& bundleName, const pid_t& controllerPid,
10080922886Sopenharmony_ci        const int32_t& controllerCmd, const int32_t& sessionType);
10180922886Sopenharmony_ci    void SetAudioStatus(pid_t uid, int32_t rendererState);
10280922886Sopenharmony_ci    int32_t GetAudioStatus(pid_t uid);
10380922886Sopenharmony_ci
10480922886Sopenharmony_ciprivate:
10580922886Sopenharmony_ci    std::map<pid_t, int32_t> audioStatuses_;
10680922886Sopenharmony_ci    AVSessionSysEvent();
10780922886Sopenharmony_ci    std::map<Operation, uint32_t> optCounts_;
10880922886Sopenharmony_ci    std::unique_ptr<Utils::Timer> timer_;
10980922886Sopenharmony_ci    uint32_t timerId_;
11080922886Sopenharmony_ci    std::recursive_mutex lock_;
11180922886Sopenharmony_ci    static constexpr uint32_t NOTIFY_TIME_INTERVAL = 1 * 60 * 60 * 1000; // retry after 1 hours
11280922886Sopenharmony_ci    std::list<AVSessionSysEvent::LifeCycleInfo> lifeCycleInfos_;
11380922886Sopenharmony_ci    std::list<AVSessionSysEvent::ControllerCommandInfo> controllerCommandInfos_;
11480922886Sopenharmony_ci    static constexpr float MULTIPLE = 1.0f;
11580922886Sopenharmony_ci};
11680922886Sopenharmony_ci#endif
11780922886Sopenharmony_ci
11880922886Sopenharmony_ci#ifdef ENABLE_AVSESSION_SYSEVENT_CONTROL
11980922886Sopenharmony_ci#define HISYSEVENT_FAULT(eventName, ...) ((void)AVSessionSysEvent::HiSysWriteFault(eventName, __VA_ARGS__))
12080922886Sopenharmony_ci#define HISYSEVENT_STATISTIC(eventName, ...) ((void)AVSessionSysEvent::HiSysWriteStatistic(eventName, __VA_ARGS__))
12180922886Sopenharmony_ci#define HISYSEVENT_SECURITY(eventName, ...) ((void)AVSessionSysEvent::HiSysWriteSecurity(eventName, __VA_ARGS__))
12280922886Sopenharmony_ci#define HISYSEVENT_BEHAVIOR(eventName, ...) ((void)AVSessionSysEvent::HiSysWriteBehavior(eventName, __VA_ARGS__))
12380922886Sopenharmony_ci#define HISYSEVENT_RESET AVSessionSysEvent::GetInstance().Reset()
12480922886Sopenharmony_ci#define HISYSEVENT_REGITER AVSessionSysEvent::GetInstance().Regiter()
12580922886Sopenharmony_ci#define HISYSEVENT_UNREGISTER AVSessionSysEvent::GetInstance().Unregister()
12680922886Sopenharmony_ci#define HISYSEVENT_ADD_OPERATION_COUNT(operation) AVSessionSysEvent::GetInstance().AddOperationCount(operation)
12780922886Sopenharmony_ci#define HISYSEVENT_ADD_CONTROLLER_COMMAND_INFO(bundleName, controllerPid, controllerCmd, sessionType)           \
12880922886Sopenharmony_ci    do {                                                                                                        \
12980922886Sopenharmony_ci        AVSessionSysEvent::GetInstance().AddControllerCommandInfo(bundleName, controllerPid,                    \
13080922886Sopenharmony_ci                                                                  controllerCmd, sessionType);                  \
13180922886Sopenharmony_ci    } while (0)
13280922886Sopenharmony_ci
13380922886Sopenharmony_ci#define HISYSEVENT_ADD_LIFE_CYCLE_INFO(bundleName, appStatus, sessionType, isCreateSession)                     \
13480922886Sopenharmony_ci    do {                                                                                                        \
13580922886Sopenharmony_ci        AVSessionSysEvent::GetInstance().AddLifeCycleInfo(bundleName, appStatus, sessionType, isCreateSession); \
13680922886Sopenharmony_ci    } while (0)
13780922886Sopenharmony_ci#define HISYSEVENT_GET_AUDIO_STATUS(uid) AVSessionSysEvent::GetInstance().GetAudioStatus(uid)
13880922886Sopenharmony_ci#define HISYSEVENT_SET_AUDIO_STATUS(uid, rendererState)                                                         \
13980922886Sopenharmony_ci    do {                                                                                                        \
14080922886Sopenharmony_ci        AVSessionSysEvent::GetInstance().SetAudioStatus(uid, rendererState);                                    \
14180922886Sopenharmony_ci    } while (0)
14280922886Sopenharmony_ci
14380922886Sopenharmony_ci#else
14480922886Sopenharmony_ci#define HISYSEVENT_FAULT(...)
14580922886Sopenharmony_ci#define HISYSEVENT_STATISTIC(...)
14680922886Sopenharmony_ci#define HISYSEVENT_SECURITY(...)
14780922886Sopenharmony_ci#define HISYSEVENT_BEHAVIOR(...)
14880922886Sopenharmony_ci#define HISYSEVENT_RESET
14980922886Sopenharmony_ci#define HISYSEVENT_REGITER
15080922886Sopenharmony_ci#define HISYSEVENT_UNREGISTER
15180922886Sopenharmony_ci#define HISYSEVENT_ADD_OPERATION_COUNT(...)
15280922886Sopenharmony_ci#define HISYSEVENT_ADD_CONTROLLER_COMMAND_INFO(...)
15380922886Sopenharmony_ci#define HISYSEVENT_ADD_LIFE_CYCLE_INFO(...)
15480922886Sopenharmony_ci#define HISYSEVENT_GET_AUDIO_STATUS(...)
15580922886Sopenharmony_ci#define HISYSEVENT_SET_AUDIO_STATUS(...)
15680922886Sopenharmony_ci#endif
15780922886Sopenharmony_ci} // namespace OHOS::AVSession
15880922886Sopenharmony_ci#endif // OHOS_AVSESSION_SYSEVENT_H