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