• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (audioPlayer_ != nullptr) {
36         delete audioPlayer_;
37         audioPlayer_ = nullptr;
38     }
39 }
40 
Init()41 void Ring::Init()
42 {
43     SystemSoundManager_ = Media::SystemSoundManagerFactory::CreateSystemSoundManager();
44     if (SystemSoundManager_ == nullptr) {
45         TELEPHONY_LOGE("get systemSoundManager failed");
46         return;
47     }
48 }
49 
Play(int32_t slotId,std::string ringtonePath)50 int32_t Ring::Play(int32_t slotId, std::string ringtonePath)
51 {
52     if (SystemSoundManager_ == nullptr || audioPlayer_ == nullptr) {
53         TELEPHONY_LOGE("SystemSoundManager_ or audioPlayer_ is nullptr");
54         return TELEPHONY_ERR_LOCAL_PTR_NULL;
55     }
56     const std::shared_ptr<AbilityRuntime::Context> context;
57     Media::RingtoneType type = slotId == DEFAULT_SIM_SLOT_ID ? Media::RingtoneType::RINGTONE_TYPE_SIM_CARD_0 :
58         Media::RingtoneType::RINGTONE_TYPE_SIM_CARD_1;
59     TELEPHONY_LOGI("ringtonePath: %{public}s", ringtonePath.c_str());
60     RingtonePlayer_ = SystemSoundManager_->GetSpecificRingTonePlayer(context, type, ringtonePath);
61     if (RingtonePlayer_ == nullptr) {
62         TELEPHONY_LOGE("get RingtonePlayer failed");
63         return TELEPHONY_ERR_LOCAL_PTR_NULL;
64     }
65     audioPlayer_->RegisterRingCallback(RingtonePlayer_);
66     int32_t result = RingtonePlayer_->Configure(defaultVolume_, true);
67     if (result != TELEPHONY_SUCCESS) {
68         TELEPHONY_LOGE("configure failed");
69     }
70     audioPlayer_->SetStop(PlayerType::TYPE_RING, false);
71     return RingtonePlayer_->Start();
72 }
73 
Stop()74 int32_t Ring::Stop()
75 {
76     std::lock_guard<std::mutex> lock(mutex_);
77     if (audioPlayer_ == nullptr) {
78         TELEPHONY_LOGE("audioPlayer_ is nullptr");
79         return TELEPHONY_ERR_LOCAL_PTR_NULL;
80     }
81     int32_t result = TELEPHONY_SUCCESS;
82     audioPlayer_->SetStop(PlayerType::TYPE_RING, true);
83     if (RingtonePlayer_ == nullptr) {
84         TELEPHONY_LOGE("RingtonePlayer_ is nullptr");
85         return TELEPHONY_ERR_LOCAL_PTR_NULL;
86     }
87     result = RingtonePlayer_->Stop();
88     isMutedRing_ = false;
89     return result;
90 }
91 
ReleaseRenderer()92 void Ring::ReleaseRenderer()
93 {
94     if (RingtonePlayer_ == nullptr) {
95         TELEPHONY_LOGE("RingtonePlayer_ is nullptr");
96         return;
97     }
98     RingtonePlayer_->Release();
99 }
100 
SetMute()101 int32_t Ring::SetMute()
102 {
103     if (RingtonePlayer_ == nullptr) {
104         TELEPHONY_LOGE("RingtonePlayer_ is nullptr");
105         return TELEPHONY_ERR_LOCAL_PTR_NULL;
106     }
107     isMutedRing_ = true;
108     return RingtonePlayer_->Configure(0, true);
109 }
110 
SetRingToneVolume(float volume)111 int32_t Ring::SetRingToneVolume(float volume)
112 {
113     TELEPHONY_LOGI("SetRingToneVolume volume = %{public}f", volume);
114     if (isMutedRing_) {
115         TELEPHONY_LOGI("ringTone is already muted");
116         return TELEPHONY_SUCCESS;
117     }
118     if (RingtonePlayer_ == nullptr) {
119         return TELEPHONY_ERR_LOCAL_PTR_NULL;
120     }
121     if (volume >= 0.0f && volume <= 1.0f) {
122         return RingtonePlayer_->Configure(volume, true);
123     } else {
124         TELEPHONY_LOGE("volume is valid");
125         return TELEPHONY_ERR_FAIL;
126     }
127 }
128 } // namespace Telephony
129 } // namespace OHOS