1 /*
2  * Copyright (c) 2022 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 applicable 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 "avsession_event_handler.h"
17 #include "avsession_log.h"
18 
19 namespace OHOS::AVSession {
GetInstance()20 AVSessionEventHandler &AVSessionEventHandler::GetInstance()
21 {
22     static AVSessionEventHandler avSessionEventHandler;
23     return avSessionEventHandler;
24 }
25 
AVSessionEventHandler()26 AVSessionEventHandler::AVSessionEventHandler()
27 {
28     SLOGI("construct");
29 }
30 
~AVSessionEventHandler()31 AVSessionEventHandler::~AVSessionEventHandler()
32 {
33     SLOGI("destroy");
34 }
35 
AVSessionPostTask(const Callback & callback,const std::string & name,int64_t delayTime)36 bool AVSessionEventHandler::AVSessionPostTask(const Callback &callback, const std::string &name, int64_t delayTime)
37 {
38     SLOGI("AVSessionEventHandler ProxyPostTask: %{public}s", name.c_str());
39     std::lock_guard<std::mutex> lockGuard(handlerLock_);
40     if (!handler_) {
41         SLOGI("AVSessionEventHandler create new: %{public}s", name.c_str());
42         auto runner = AppExecFwk::EventRunner::Create("OS_AVSessionHdl");
43         handler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
44     }
45     return handler_->PostTask(callback, name, delayTime);
46 }
47 
AVSessionRemoveTask(const std::string & name)48 void AVSessionEventHandler::AVSessionRemoveTask(const std::string &name)
49 {
50     SLOGI("AVSessionEventHandler ProxyRemoveTask");
51     std::lock_guard<std::mutex> lockGuard(handlerLock_);
52     if (handler_) {
53         handler_->RemoveTask(name);
54     }
55 }
56 
AVSessionRemoveHandler()57 void AVSessionEventHandler::AVSessionRemoveHandler()
58 {
59     SLOGI("AVSessionEventHandler RemoveEventHandler");
60     std::lock_guard<std::mutex> lockGuard(handlerLock_);
61     handler_ = nullptr;
62 }
63 }