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 "telephony_log_wrapper.h"
21
22 #include "audio_player.h"
23
24 namespace OHOS {
25 namespace Telephony {
26 using AudioPlay = int32_t (AudioPlayer::*)(const std::string &, AudioStandard::AudioStreamType, PlayerType);
27
Ring()28 Ring::Ring() : isVibrating_(false), shouldRing_(false), shouldVibrate_(false), ringtonePath_(""),
29 audioPlayer_(new (std::nothrow) AudioPlayer())
30 {
31 Init(DelayedSingleton<AudioProxy>::GetInstance()->GetDefaultRingPath());
32 }
33
Ring(const std::string & path)34 Ring::Ring(const std::string &path)
35 : isVibrating_(false), shouldRing_(false), shouldVibrate_(false), ringtonePath_("")
36 {
37 Init(path);
38 }
39
~Ring()40 Ring::~Ring()
41 {
42 if (audioPlayer_ != nullptr) {
43 delete audioPlayer_;
44 audioPlayer_ = nullptr;
45 }
46 }
47
Init(const std::string & ringtonePath)48 void Ring::Init(const std::string &ringtonePath)
49 {
50 if (ringtonePath.empty()) {
51 TELEPHONY_LOGE("ringtone path empty");
52 return;
53 }
54 if (AudioStandard::AudioRingerMode::RINGER_MODE_NORMAL ==
55 DelayedSingleton<AudioProxy>::GetInstance()->GetRingerMode()) {
56 shouldRing_ = true;
57 shouldVibrate_ = true;
58 } else if (AudioStandard::AudioRingerMode::RINGER_MODE_VIBRATE ==
59 DelayedSingleton<AudioProxy>::GetInstance()->GetRingerMode()) {
60 shouldRing_ = false;
61 shouldVibrate_ = true;
62 } else if (AudioStandard::AudioRingerMode::RINGER_MODE_SILENT ==
63 DelayedSingleton<AudioProxy>::GetInstance()->GetRingerMode()) {
64 shouldRing_ = false;
65 shouldVibrate_ = false;
66 }
67 ringtonePath_ = ringtonePath;
68 }
69
Play()70 int32_t Ring::Play()
71 {
72 if (!shouldRing_ || ringtonePath_.empty()) {
73 TELEPHONY_LOGE("should not ring or ringtone path empty");
74 return CALL_ERR_INVALID_PATH;
75 }
76 if (audioPlayer_ == nullptr) {
77 TELEPHONY_LOGE("audioPlayer_ is nullptr");
78 return TELEPHONY_ERR_LOCAL_PTR_NULL;
79 }
80 int32_t result = TELEPHONY_SUCCESS;
81 AudioPlay audioPlay = &AudioPlayer::Play;
82 std::thread play(audioPlay, audioPlayer_, ringtonePath_, AudioStandard::AudioStreamType::STREAM_RING,
83 PlayerType::TYPE_RING);
84 pthread_setname_np(play.native_handle(), RING_PLAY_THREAD);
85 play.detach();
86 if (shouldVibrate_) {
87 result = StartVibrate();
88 }
89 return result;
90 }
91
Stop()92 int32_t Ring::Stop()
93 {
94 std::lock_guard<std::mutex> lock(mutex_);
95 if (!shouldRing_ || ringtonePath_.empty()) {
96 TELEPHONY_LOGE("should not ring or ringtone path empty");
97 return CALL_ERR_INVALID_PATH;
98 }
99 if (audioPlayer_ == nullptr) {
100 TELEPHONY_LOGE("audioPlayer_ is nullptr");
101 return TELEPHONY_ERR_LOCAL_PTR_NULL;
102 }
103 int32_t result = TELEPHONY_SUCCESS;
104 audioPlayer_->SetStop(PlayerType::TYPE_RING, true);
105 if (isVibrating_) {
106 result = CancelVibrate();
107 }
108 return result;
109 }
110
StartVibrate()111 int32_t Ring::StartVibrate()
112 {
113 if (DelayedSingleton<AudioProxy>::GetInstance()->StartVibrate() == TELEPHONY_SUCCESS) {
114 isVibrating_ = true;
115 return TELEPHONY_SUCCESS;
116 }
117 TELEPHONY_LOGE("start vibrate failed");
118 return CALL_ERR_AUDIO_START_VIBRATE_FAILED;
119 }
120
CancelVibrate()121 int32_t Ring::CancelVibrate()
122 {
123 if (DelayedSingleton<AudioProxy>::GetInstance()->CancelVibrate() == TELEPHONY_SUCCESS) {
124 isVibrating_ = false;
125 return TELEPHONY_SUCCESS;
126 }
127 TELEPHONY_LOGE("cancel vibrate failed");
128 return CALL_ERR_AUDIO_CANCEL_VIBRATE_FAILED;
129 }
130
ShouldVibrate()131 bool Ring::ShouldVibrate()
132 {
133 return DelayedSingleton<AudioProxy>::GetInstance()->GetRingerMode() !=
134 AudioStandard::AudioRingerMode::RINGER_MODE_SILENT;
135 }
136
ReleaseRenderer()137 void Ring::ReleaseRenderer()
138 {
139 if (audioPlayer_ == nullptr) {
140 TELEPHONY_LOGE("audioPlayer_ is nullptr");
141 return;
142 }
143 audioPlayer_->ReleaseRenderer();
144 }
145 } // namespace Telephony
146 } // namespace OHOS