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 "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)23MtpMonitor::MtpMonitor(void) 24 { 25 Init(); 26 } 27 Init()28void MtpMonitor::Init() 29 { 30 interruptFlag = false; 31 } 32 Start()33void MtpMonitor::Start() 34 { 35 std::thread([this] { this->Run(); }).detach(); 36 } 37 Stop()38void MtpMonitor::Stop() 39 { 40 interruptFlag = true; 41 } 42 Run()43void MtpMonitor::Run() 44 { 45 string name("MtpMonitor::Run"); 46 pthread_setname_np(pthread_self(), name.c_str()); 47 while (!interruptFlag) { 48 if (operationPtr_ == nullptr) { 49 operationPtr_ = make_shared<MtpOperation>(); 50 } 51 operationPtr_->Execute(); 52 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME)); 53 } 54 } 55 } // namespace Media 56 } // namespace OHOS 57