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); 52 std::string GetHapticUriForAudioUri(const std::string &audioUri); 53 bool IsFileExisting(const std::string &fileUri); 54 std::string ChangeUri(const std::string &audioUri); 55 56 float volume_ = 1.0f; 57 bool loop_ = false; 58 std::string configuredUri_ = ""; 59 std::shared_ptr<AudioHapticManager> audioHapticManager_ = nullptr; 60 int32_t sourceId_ = -1; 61 std::shared_ptr<AudioHapticPlayer> player_ = nullptr; 62 std::shared_ptr<AbilityRuntime::Context> context_; 63 std::shared_ptr<AudioHapticPlayerCallback> callback_ = nullptr; 64 std::shared_ptr<RingtonePlayerInterruptCallback> interruptCallback_ = nullptr; 65 SystemSoundManagerImpl &systemSoundMgr_; 66 RingtoneType type_ = RINGTONE_TYPE_SIM_CARD_0; 67 RingtoneState ringtoneState_ = STATE_NEW; 68 69 std::mutex playerMutex_; 70 }; 71 72 class RingtonePlayerCallback : public AudioHapticPlayerCallback { 73 public: 74 explicit RingtonePlayerCallback(RingtonePlayerImpl &ringtonePlayerImpl); 75 virtual ~RingtonePlayerCallback() = default; 76 77 void OnInterrupt(const AudioStandard::InterruptEvent &interruptEvent) override; 78 void OnEndOfStream(void) override; 79 void OnError(int32_t errorCode) override; 80 81 private: 82 RingtonePlayerImpl &ringtonePlayerImpl_; 83 }; 84 } // namespace Media 85 } // namespace OHOS 86 #endif // RINGTONE_PLAYER_IMPL_H 87