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 int32_t EnableHapticsInSilentMode(bool enable) override; 46 bool IsHapticsIntensityAdjustmentSupported() override; 47 bool IsHapticsRampSupported() override; 48 int32_t SetHapticsRamp(int32_t duration, float startIntensity, float endIntensity) override; 49 int32_t SetHapticsFeature(const HapticsFeature &feature) override; 50 51 void SetPlayerParam(const AudioHapticPlayerParam ¶m); 52 void LoadPlayer(); 53 void NotifyStartVibrate(const uint64_t &latency); 54 void NotifyInterruptEvent(const AudioStandard::InterruptEvent &interruptEvent); 55 void NotifyEndOfStreamEvent(); 56 void NotifyErrorEvent(int32_t errCode); 57 static void SendHapticPlayerEvent(const int32_t &errorCode, const std::string &strEvent); 58 void NotifyFirstFrame(const uint64_t &latency); 59 60 private: 61 // func for sound 62 void ReleaseSound(); 63 static void HandleEndOfStreamEventThreadFunc(std::weak_ptr<AudioHapticPlayerImpl> player); 64 void HandleEndOfStreamEvent(); 65 // func for vibration 66 int32_t StartVibrate(); 67 void StopVibrate(); 68 void ResetVibrateState(); 69 void ReleaseVibrator(); 70 int32_t GetDelayTime(int32_t playedTimes); 71 72 // var for all 73 AudioLatencyMode latencyMode_; 74 AudioStandard::StreamUsage streamUsage_ = AudioStandard::STREAM_USAGE_UNKNOWN; 75 bool muteAudio_; 76 bool muteHaptic_; 77 bool parallelPlayFlag_ = false; 78 AudioSource audioSource_; 79 HapticSource hapticSource_; 80 float volume_ = 1.0f; 81 std::atomic<bool> loop_ = false; 82 AudioHapticPlayerState playerState_ = AudioHapticPlayerState::STATE_INVALID; 83 std::mutex audioHapticPlayerLock_; 84 HapticsMode hapticsMode_ = HapticsMode::HAPTICS_MODE_INVALID; 85 std::atomic<int32_t> audioHapticSyncId_ = 0; 86 bool isSupportDSPSync_ = false; 87 88 // var for callback 89 std::weak_ptr<AudioHapticPlayerCallback> audioHapticPlayerCallback_; 90 uint64_t audioLatency_ = 0; 91 std::shared_ptr<AudioHapticSoundCallback> soundCallback_ = nullptr; 92 93 // var for vibrate 94 std::shared_ptr<AudioHapticVibrator> audioHapticVibrator_ = nullptr; 95 std::shared_ptr<std::thread> vibrateThread_; 96 std::mutex waitStartVibrateMutex_; 97 std::condition_variable condStartVibrate_; 98 bool canStartVibrate_ = false; 99 std::atomic<bool> isVibrationStopped_ = false; 100 std::atomic_bool isVibrationRunning_{false}; 101 std::atomic<bool> isGentle_ = false; 102 std::atomic<bool> isRamp_ = false; 103 104 // var for audio 105 std::shared_ptr<AudioHapticSound> audioHapticSound_ = nullptr; 106 }; 107 108 class AudioHapticSoundCallbackImpl : public AudioHapticSoundCallback { 109 public: 110 explicit AudioHapticSoundCallbackImpl(std::shared_ptr<AudioHapticPlayerImpl> audioHapticPlayerImpl); 111 virtual ~AudioHapticSoundCallbackImpl() = default; 112 113 void OnEndOfStream() override; 114 void OnError(int32_t errorCode) override; 115 void OnFirstFrameWriting(uint64_t latency) override; 116 void OnInterrupt(const AudioStandard::InterruptEvent &interruptEvent) override; 117 118 private: 119 std::weak_ptr<AudioHapticPlayerImpl> audioHapticPlayerImpl_; 120 }; 121 } // namespace Media 122 } // namespace OHOS 123 #endif // AUDIO_HAPTIC_PLAYER_IMPL_H 124