1 /*
2 * Copyright (C) 2021 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 "ring.h"
17
18 #include <thread>
19
20 #include "audio_player.h"
21 #include "call_base.h"
22 #include "telephony_log_wrapper.h"
23
24 namespace OHOS {
25 namespace Telephony {
26 static constexpr int32_t DEFAULT_SIM_SLOT_ID = 0;
27
Ring()28 Ring::Ring() : audioPlayer_(new (std::nothrow) AudioPlayer())
29 {
30 Init();
31 }
32
~Ring()33 Ring::~Ring()
34 {
35 std::lock_guard<ffrt::mutex> lock(mutex_);
36 if (audioPlayer_ != nullptr) {
37 delete audioPlayer_;
38 audioPlayer_ = nullptr;
39 }
40 }
41
Init()42 void Ring::Init()
43 {
44 SystemSoundManager_ = Media::SystemSoundManagerFactory::CreateSystemSoundManager();
45 if (SystemSoundManager_ == nullptr) {
46 TELEPHONY_LOGE("get systemSoundManager failed");
47 return;
48 }
49 }
50
Play(int32_t slotId,std::string ringtonePath,Media::HapticStartupMode mode)51 int32_t Ring::Play(int32_t slotId, std::string ringtonePath, Media::HapticStartupMode mode)
52 {
53 std::lock_guard<ffrt::mutex> lock(mutex_);
54 if (SystemSoundManager_ == nullptr || audioPlayer_ == nullptr) {
55 TELEPHONY_LOGE("SystemSoundManager_ or audioPlayer_ is nullptr");
56 return TELEPHONY_ERR_LOCAL_PTR_NULL;
57 }
58 const std::shared_ptr<AbilityRuntime::Context> context;
59 Media::RingtoneType type = slotId == DEFAULT_SIM_SLOT_ID ? Media::RingtoneType::RINGTONE_TYPE_SIM_CARD_0 :
60 Media::RingtoneType::RINGTONE_TYPE_SIM_CARD_1;
61 TELEPHONY_LOGI("ringtonePath: %{public}s", ringtonePath.c_str());
62 RingtonePlayer_ = SystemSoundManager_->GetSpecificRingTonePlayer(context, type, ringtonePath);
63 if (RingtonePlayer_ == nullptr) {
64 TELEPHONY_LOGE("get RingtonePlayer failed");
65 return TELEPHONY_ERR_LOCAL_PTR_NULL;
66 }
67 audioPlayer_->RegisterRingCallback(RingtonePlayer_);
68 int32_t result = RingtonePlayer_->Configure(defaultVolume_, true);
69 if (result != TELEPHONY_SUCCESS) {
70 TELEPHONY_LOGE("configure failed");
71 }
72 audioPlayer_->SetStop(PlayerType::TYPE_RING, false);
73 return RingtonePlayer_->Start(mode);
74 }
75
Stop()76 int32_t Ring::Stop()
77 {
78 std::lock_guard<ffrt::mutex> lock(mutex_);
79 if (audioPlayer_ == nullptr) {
80 TELEPHONY_LOGE("audioPlayer_ is nullptr");
81 return TELEPHONY_ERR_LOCAL_PTR_NULL;
82 }
83 int32_t result = TELEPHONY_SUCCESS;
84 audioPlayer_->SetStop(PlayerType::TYPE_RING, true);
85 if (RingtonePlayer_ == nullptr) {
86 TELEPHONY_LOGE("RingtonePlayer_ is nullptr");
87 return TELEPHONY_ERR_LOCAL_PTR_NULL;
88 }
89 result = RingtonePlayer_->Stop();
90 isMutedRing_ = false;
91 return result;
92 }
93
ReleaseRenderer()94 void Ring::ReleaseRenderer()
95 {
96 if (RingtonePlayer_ == nullptr) {
97 TELEPHONY_LOGE("RingtonePlayer_ is nullptr");
98 return;
99 }
100 RingtonePlayer_->Release();
101 }
102
SetMute()103 int32_t Ring::SetMute()
104 {
105 if (RingtonePlayer_ == nullptr) {
106 TELEPHONY_LOGE("RingtonePlayer_ is nullptr");
107 return TELEPHONY_ERR_LOCAL_PTR_NULL;
108 }
109 isMutedRing_ = true;
110 return RingtonePlayer_->Configure(0, true);
111 }
112
SetRingToneVolume(float volume)113 int32_t Ring::SetRingToneVolume(float volume)
114 {
115 TELEPHONY_LOGI("SetRingToneVolume volume = %{public}f", volume);
116 if (isMutedRing_) {
117 TELEPHONY_LOGI("ringTone is already muted");
118 return TELEPHONY_SUCCESS;
119 }
120 if (RingtonePlayer_ == nullptr) {
121 return TELEPHONY_ERR_LOCAL_PTR_NULL;
122 }
123 if (volume >= 0.0f && volume <= 1.0f) {
124 return RingtonePlayer_->Configure(volume, true);
125 } else {
126 TELEPHONY_LOGE("volume is valid");
127 return TELEPHONY_ERR_FAIL;
128 }
129 }
130 } // namespace Telephony
131 } // namespace OHOS