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 AUDIO_HAPTIC_PLAYER_IMPL_H 17 #define AUDIO_HAPTIC_PLAYER_IMPL_H 18 19 #include "audio_haptic_player.h" 20 #include "audio_haptic_sound.h" 21 #include "audio_haptic_vibrator.h" 22 23 namespace OHOS { 24 namespace Media { 25 class AudioHapticSoundCallbackImpl; 26 27 class AudioHapticPlayerImpl : public AudioHapticPlayer, public std::enable_shared_from_this<AudioHapticPlayerImpl> { 28 public: 29 AudioHapticPlayerImpl(); 30 ~AudioHapticPlayerImpl(); 31 32 // AudioHapticPlayer override 33 bool IsMuted(const AudioHapticType &audioHapticType) const override; 34 int32_t Prepare() override; 35 int32_t Start() override; 36 int32_t Stop() override; 37 int32_t Release() override; 38 int32_t SetVolume(float volume) override; 39 int32_t SetHapticIntensity(float intensity) override; 40 int32_t SetLoop(bool loop) override; 41 int32_t SetAudioHapticPlayerCallback(const std::shared_ptr<AudioHapticPlayerCallback> &playerCallback) override; 42 int32_t GetAudioCurrentTime() override; 43 HapticsMode GetHapticsMode() const override; 44 void SetHapticsMode(HapticsMode hapticsMode) override; 45 46 void SetPlayerParam(const AudioHapticPlayerParam ¶m); 47 void LoadPlayer(); 48 void NotifyStartVibrate(const uint64_t &latency); 49 void NotifyInterruptEvent(const AudioStandard::InterruptEvent &interruptEvent); 50 void NotifyEndOfStreamEvent(); 51 void NotifyErrorEvent(int32_t errCode); 52 53 private: 54 // func for sound 55 void ReleaseSound(); 56 static void HandleEndOfStreamEventThreadFunc(std::weak_ptr<AudioHapticPlayerImpl> player); 57 void HandleEndOfStreamEvent(); 58 // func for vibration 59 int32_t StartVibrate(); 60 void StopVibrate(); 61 void ResetVibrateState(); 62 void ReleaseVibrator(); 63 64 // var for all 65 AudioLatencyMode latencyMode_; 66 AudioStandard::StreamUsage streamUsage_ = AudioStandard::STREAM_USAGE_UNKNOWN; 67 bool muteAudio_; 68 bool muteHaptic_; 69 bool parallelPlayFlag_ = false; 70 std::string audioUri_; 71 HapticSource hapticSource_; 72 float volume_ = 1.0f; 73 bool loop_ = false; 74 AudioHapticPlayerState playerState_ = AudioHapticPlayerState::STATE_INVALID; 75 std::mutex audioHapticPlayerLock_; 76 HapticsMode hapticsMode_ = HapticsMode::HAPTICS_MODE_INVALID; 77 78 // var for callback 79 std::weak_ptr<AudioHapticPlayerCallback> audioHapticPlayerCallback_; 80 uint64_t audioLatency_ = 0; 81 std::shared_ptr<AudioHapticSoundCallback> soundCallback_ = nullptr; 82 83 // var for vibrate 84 std::shared_ptr<AudioHapticVibrator> audioHapticVibrator_ = nullptr; 85 std::shared_ptr<std::thread> vibrateThread_; 86 std::mutex waitStartVibrateMutex_; 87 std::condition_variable condStartVibrate_; 88 bool isAudioPlayFirstFrame_ = false; 89 bool isVibrationStopped_ = false; 90 91 // var for audio 92 std::shared_ptr<AudioHapticSound> audioHapticSound_ = nullptr; 93 }; 94 95 class AudioHapticSoundCallbackImpl : public AudioHapticSoundCallback { 96 public: 97 explicit AudioHapticSoundCallbackImpl(std::shared_ptr<AudioHapticPlayerImpl> audioHapticPlayerImpl); 98 virtual ~AudioHapticSoundCallbackImpl() = default; 99 100 void OnEndOfStream() override; 101 void OnError(int32_t errorCode) override; 102 void OnFirstFrameWriting(uint64_t latency) override; 103 void OnInterrupt(const AudioStandard::InterruptEvent &interruptEvent) override; 104 105 private: 106 std::weak_ptr<AudioHapticPlayerImpl> audioHapticPlayerImpl_; 107 }; 108 } // namespace Media 109 } // namespace OHOS 110 #endif // AUDIO_HAPTIC_PLAYER_IMPL_H 111