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
24namespace OHOS {
25namespace Telephony {
26static constexpr int32_t DEFAULT_SIM_SLOT_ID = 0;
27
28Ring::Ring() : audioPlayer_(new (std::nothrow) AudioPlayer())
29{
30    Init();
31}
32
33Ring::~Ring()
34{
35    if (audioPlayer_ != nullptr) {
36        delete audioPlayer_;
37        audioPlayer_ = nullptr;
38    }
39}
40
41void Ring::Init()
42{
43    SystemSoundManager_ = Media::SystemSoundManagerFactory::CreateSystemSoundManager();
44    if (SystemSoundManager_ == nullptr) {
45        TELEPHONY_LOGE("get systemSoundManager failed");
46        return;
47    }
48}
49
50int32_t Ring::Play(int32_t slotId)
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    RingtonePlayer_ = SystemSoundManager_->GetRingtonePlayer(context, type);
60    if (RingtonePlayer_ == nullptr) {
61        TELEPHONY_LOGE("get RingtonePlayer failed");
62        return TELEPHONY_ERR_LOCAL_PTR_NULL;
63    }
64    audioPlayer_->RegisterRingCallback(RingtonePlayer_);
65    int32_t result = RingtonePlayer_->Configure(defaultVolume_, true);
66    if (result != TELEPHONY_SUCCESS) {
67        TELEPHONY_LOGE("configure failed");
68    }
69    audioPlayer_->SetStop(PlayerType::TYPE_RING, false);
70    return RingtonePlayer_->Start();
71}
72
73int32_t Ring::Stop()
74{
75    std::lock_guard<std::mutex> lock(mutex_);
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    audioPlayer_->SetStop(PlayerType::TYPE_RING, true);
82    if (RingtonePlayer_ == nullptr) {
83        TELEPHONY_LOGE("RingtonePlayer_ is nullptr");
84        return TELEPHONY_ERR_LOCAL_PTR_NULL;
85    }
86    result = RingtonePlayer_->Stop();
87    return result;
88}
89
90void Ring::ReleaseRenderer()
91{
92    if (RingtonePlayer_ == nullptr) {
93        TELEPHONY_LOGE("RingtonePlayer_ is nullptr");
94        return;
95    }
96    RingtonePlayer_->Release();
97}
98
99int32_t Ring::SetMute()
100{
101    if (RingtonePlayer_ == nullptr) {
102        TELEPHONY_LOGE("RingtonePlayer_ is nullptr");
103        return TELEPHONY_ERR_LOCAL_PTR_NULL;
104    }
105    return RingtonePlayer_->Configure(0, true);
106}
107} // namespace Telephony
108} // namespace OHOS