• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_VIBRATOR_IMPL_H
17 #define AUDIO_HAPTIC_VIBRATOR_IMPL_H
18 
19 #include "audio_haptic_vibrator.h"
20 
21 #include <cstdint>
22 #include <string>
23 
24 #ifdef SUPPORT_VIBRATOR
25 #include "vibrator_agent.h"
26 #endif
27 
28 namespace OHOS {
29 namespace Media {
30 
31 class AudioHapticVibratorImpl : public AudioHapticVibrator {
32 public:
33     explicit AudioHapticVibratorImpl(AudioHapticPlayer &audioHapticPlayer);
34     ~AudioHapticVibratorImpl();
35 
36     int32_t PreLoad(const HapticSource &hapticSource, const AudioStandard::StreamUsage &streamUsage) override;
37     int32_t SetHapticIntensity(float intensity) override;
38     int32_t Release() override;
39     void ResetStopState() override;
40     int32_t StartVibrate(const AudioLatencyMode &latencyMode) override;
41     int32_t StopVibrate() override;
42     int32_t GetDelayTime() override;
43     void SetIsSupportEffectId(bool isSupport);
44     bool IsHapticsCustomSupported() override;
EnableHapticsInSilentMode(bool enable)45     void EnableHapticsInSilentMode(bool enable) override
46     {
47         enableInSilentMode_.store(enable);
48     }
49     int32_t SetHapticsRamp(int32_t duration, float startIntensity, float endIntensity) override;
50     int32_t SetHapticsFeature(const HapticsFeature &feature) override;
SetAudioHapticSyncId(const int32_t & audioHapticSyncId)51     void SetAudioHapticSyncId(const int32_t &audioHapticSyncId) override
52     {
53         audioHapticSyncId_ = audioHapticSyncId;
54     }
55 private:
56     int32_t StartVibrateForSoundPool();
57     int32_t StartVibrateWithEffect();
58     int32_t StartVibrateForAVPlayer();
59     int32_t StartNonSyncVibration();
60     int32_t StartNonSyncOnceVibration();
61     int32_t RunVibrationPatterns(const std::shared_ptr<VibratorPackage>& vibratorPkg,
62                                  std::unique_lock<std::mutex> &lock);
63     int32_t OpenHapticSource(const HapticSource& hapticSource, int32_t& fd);
64     int32_t OpenHapticFile(const HapticSource& hapticSource);
65     int32_t ExtractFd(const std::string& hapticsUri);
66     int32_t PlayVibrationPattern(const std::shared_ptr<VibratorPackage>& vibratorPkg,
67                                  int32_t patternIndex,
68                                  int32_t& vibrateTime,
69                                  std::unique_lock<std::mutex>& lock);
70     int32_t PlayVibrateForAVPlayer(const std::shared_ptr<VibratorPackage>& vibratorPkg,
71                                    std::unique_lock<std::mutex>& lock);
72     int32_t PlayVibrateForSoundPool(const std::shared_ptr<VibratorPackage>& vibratorPkg,
73                                     std::unique_lock<std::mutex>& lock);
74     int32_t SeekAndRestart();
75     void ResumeModulePackge();
76     bool IsNonSync();
77 
78     AudioHapticPlayer &audioHapticPlayer_;
79 
80 #ifdef SUPPORT_VIBRATOR
81     VibratorUsage vibratorUsage_ = VibratorUsage::USAGE_UNKNOWN;
82     std::shared_ptr<VibratorFileDescription> vibratorFD_ = nullptr;
83     std::shared_ptr<VibratorPackage> vibratorPkg_ = nullptr;
84     std::shared_ptr<VibratorPackage> seekVibratorPkg_ = nullptr;
85     std::shared_ptr<VibratorPackage> modulatePkg_ = nullptr;
86     std::condition_variable vibrateCV_;
87     float vibrateIntensity_ = 1.0f;
88     bool isSupportEffectId_ = false;
89     HapticSource hapticSource_;
90     std::atomic<bool> enableInSilentMode_ = false;
91     float rampEndIntensity_ = -1.0f;
92     int32_t audioHapticSyncId_ = 0;
93 #endif
94     std::mutex vibrateMutex_;
95     AudioStandard::StreamUsage streamUsage_ = AudioStandard::StreamUsage::STREAM_USAGE_UNKNOWN;
96     bool isStopped_ = false;
97     std::atomic<int32_t> vibrationTimeElapsed_ = 0;
98     std::atomic<int64_t> patternStartTime_ = 0;
99     std::atomic<int32_t> vibratorTime_ = 0;
100     VibratorParameter vibratorParameter_;
101     std::atomic<bool> isRunning_ = false;
102     std::atomic<bool> isIntensityChanged_ = false;
103     std::atomic<bool> isNeedRestart_ = false;
104 };
105 } // namespace Media
106 } // namespace OHOS
107 #endif // AUDIO_HAPTIC_VIBRATOR_IMPL_H
108