1 /*
2  * Copyright (c) 2023 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 #ifndef RINGTONE_PLAYER_IMPL_H
17 #define RINGTONE_PLAYER_IMPL_H
18 
19 #include "audio_haptic_manager.h"
20 #include "player.h"
21 #include "system_sound_manager_impl.h"
22 #include "system_sound_vibrator.h"
23 
24 
25 namespace OHOS {
26 namespace Media {
27 class RingtonePlayerCallback;
28 
29 class RingtonePlayerInterruptCallback;
30 
31 class RingtonePlayerImpl : public RingtonePlayer {
32 public:
33     RingtonePlayerImpl(const std::shared_ptr<AbilityRuntime::Context> &context,
34         SystemSoundManagerImpl &sysSoundMgr, RingtoneType type);
35     ~RingtonePlayerImpl();
36     void NotifyEndofStreamEvent();
37     void NotifyInterruptEvent(const AudioStandard::InterruptEvent &interruptEvent);
38 
39     // RingtonePlayer override
40     RingtoneState GetRingtoneState() override;
41     int32_t Configure(const float &volume, const bool &loop) override;
42     int32_t Start() override;
43     int32_t Stop() override;
44     int32_t Release() override;
45     int32_t GetAudioRendererInfo(AudioStandard::AudioRendererInfo &rendererInfo) const override;
46     std::string GetTitle() override;
47     int32_t SetRingtonePlayerInterruptCallback(
48         const std::shared_ptr<RingtonePlayerInterruptCallback> &interruptCallback) override;
49 
50 private:
51     void InitPlayer(std::string &audioUri, ToneHapticsSettings &settings, AudioHapticPlayerOptions options);
52     std::string GetNewHapticUriForAudioUri(const std::string &audioUri, const std::string &ringtonePath,
53         const std::string& hapticsPath);
54     std::string GetNewHapticUriForAudioUri(const std::string &audioUri);
55     std::string GetHapticUriForAudioUri(const std::string &audioUri);
56     bool IsFileExisting(const std::string &fileUri);
57     std::string ChangeUri(const std::string &audioUri);
58     ToneHapticsType ConvertToToneHapticsType(RingtoneType type);
59     HapticsMode ConvertToHapticsMode(ToneHapticsMode toneHapticsMode);
60     ToneHapticsSettings GetHapticSettings(std::string &audioUri, bool &muteHaptics);
61     std::string ChangeHapticsUri(const std::string &hapticsUri);
62     bool InitDataShareHelper();
63     void ReleaseDataShareHelper();
64     int32_t RegisterSource(const std::string &audioUri, const std::string &hapticUri);
65 
66     float volume_ = 1.0f;
67     bool loop_ = false;
68     std::string configuredUri_ = "";
69     ToneHapticsSettings configuredHaptcisSettings_;
70     std::shared_ptr<AudioHapticManager> audioHapticManager_ = nullptr;
71     int32_t sourceId_ = -1;
72     std::shared_ptr<AudioHapticPlayer> player_ = nullptr;
73     std::shared_ptr<AbilityRuntime::Context> context_;
74     std::shared_ptr<AudioHapticPlayerCallback> callback_ = nullptr;
75     std::shared_ptr<RingtonePlayerInterruptCallback> interruptCallback_ = nullptr;
76     SystemSoundManagerImpl &systemSoundMgr_;
77     RingtoneType type_ = RINGTONE_TYPE_SIM_CARD_0;
78     RingtoneState ringtoneState_ = STATE_NEW;
79     std::shared_ptr<DataShare::DataShareHelper> dataShareHelper_ = nullptr;
80 
81     std::mutex playerMutex_;
82 };
83 
84 class RingtonePlayerCallback : public AudioHapticPlayerCallback {
85 public:
86     explicit RingtonePlayerCallback(RingtonePlayerImpl &ringtonePlayerImpl);
87     virtual ~RingtonePlayerCallback() = default;
88 
89     void OnInterrupt(const AudioStandard::InterruptEvent &interruptEvent) override;
90     void OnEndOfStream(void) override;
91     void OnError(int32_t errorCode) override;
92 
93 private:
94     RingtonePlayerImpl &ringtonePlayerImpl_;
95 };
96 } // namespace Media
97 } // namespace OHOS
98 #endif // RINGTONE_PLAYER_IMPL_H
99