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 #define MLOG_TAG "MtpMonitor"
16 #include "mtp_monitor.h"
17 #include <thread>
18 #include "media_log.h"
19 using namespace std;
20 namespace OHOS {
21 namespace Media {
22 constexpr int32_t SLEEP_TIME = 10;
MtpMonitor(void)23 MtpMonitor::MtpMonitor(void)
24 {
25     Init();
26 }
27 
Init()28 void MtpMonitor::Init()
29 {
30     interruptFlag = false;
31 }
32 
Start()33 void MtpMonitor::Start()
34 {
35     std::thread([this] { this->Run(); }).detach();
36 }
37 
Stop()38 void MtpMonitor::Stop()
39 {
40     interruptFlag = true;
41     // make sure stop done after other operations.
42     std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME));
43 }
44 
Run()45 void MtpMonitor::Run()
46 {
47     string name("MtpMonitor::Run");
48     pthread_setname_np(pthread_self(), name.c_str());
49     while (!interruptFlag) {
50         if (operationPtr_ == nullptr) {
51             operationPtr_ = make_shared<MtpOperation>();
52         }
53         if (operationPtr_ != nullptr) {
54             operationPtr_->Execute();
55         }
56     }
57     if (operationPtr_ != nullptr) {
58         operationPtr_.reset();
59     }
60 }
61 } // namespace Media
62 } // namespace OHOS
63