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 handlerClear");
34 std::lock_guard<std::mutex> lockGuard(handlerLock_);
35 handler_ = nullptr;
36 }
37
AVSessionPostTask(const Callback & callback,const std::string & name,int64_t delayTime)38 bool AVSessionEventHandler::AVSessionPostTask(const Callback &callback, const std::string &name, int64_t delayTime)
39 {
40 SLOGD("AVSessionEventHandler ProxyPostTask: %{public}s", name.c_str());
41 std::lock_guard<std::mutex> lockGuard(handlerLock_);
42 if (!handler_) {
43 SLOGI("AVSessionEventHandler create new: %{public}s", name.c_str());
44 auto runner = AppExecFwk::EventRunner::Create("OS_AVSessionHdl");
45 handler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
46 }
47 return handler_->PostTask(callback, name, delayTime);
48 }
49
AVSessionRemoveTask(const std::string & name)50 void AVSessionEventHandler::AVSessionRemoveTask(const std::string &name)
51 {
52 SLOGI("AVSessionEventHandlerRemove:%{public}s", name.c_str());
53 std::lock_guard<std::mutex> lockGuard(handlerLock_);
54 if (handler_) {
55 handler_->RemoveTask(name);
56 }
57 }
58
AVSessionRemoveHandler()59 void AVSessionEventHandler::AVSessionRemoveHandler()
60 {
61 SLOGI("AVSessionEventHandler RemoveEventHandler");
62 std::lock_guard<std::mutex> lockGuard(handlerLock_);
63 handler_ = nullptr;
64 }
65 }