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 "MtpService"
16 #include "mtp_service.h"
17 #include "media_log.h"
18 #include "mtp_media_library.h"
19
20 using namespace std;
21 namespace OHOS {
22 namespace Media {
MtpService(void)23 MtpService::MtpService(void) : monitorPtr_(nullptr), isMonitorRun_(false)
24 {
25 }
26
Init()27 void MtpService::Init()
28 {
29 if (monitorPtr_ == nullptr) {
30 monitorPtr_ = make_shared<MtpMonitor>();
31 }
32 }
33
StartService()34 void MtpService::StartService()
35 {
36 MEDIA_INFO_LOG("MtpService::StartService");
37 {
38 std::unique_lock lock(mutex_);
39 Init();
40 CHECK_AND_RETURN_LOG(!isMonitorRun_, "MtpService::StartService -- monitor is already running, return");
41 CHECK_AND_RETURN_LOG(monitorPtr_ != nullptr, "MtpService::StartService monitorPtr_ is nullptr");
42 monitorPtr_->Start();
43 isMonitorRun_ = true;
44 }
45 }
46
StopService()47 void MtpService::StopService()
48 {
49 MEDIA_INFO_LOG("MtpService::StopService");
50 {
51 std::unique_lock lock(mutex_);
52 CHECK_AND_RETURN_LOG(isMonitorRun_, "MtpService::StopService -- monitor is not running, return");
53 CHECK_AND_RETURN_LOG(monitorPtr_ != nullptr, "MtpService::StopService monitorPtr_ is nullptr");
54 monitorPtr_->Stop();
55 isMonitorRun_ = false;
56 // after stop mtp service, clear the unordered_map memory of the MtpMediaLibrary
57 MtpMediaLibrary::GetInstance()->Clear();
58 }
59 }
60 } // namespace Media
61 } // namespace OHOS