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 
45 private:
46     int32_t StartVibrateForSoundPool();
47     int32_t StartVibrateWithEffect();
48     int32_t StartVibrateForAVPlayer();
49     int32_t StartNonSyncVibration();
50     int32_t RunVibrationPatterns(std::unique_lock<std::mutex> &lock);
51     int32_t OpenHapticFile(const std::string &hapticUri);
52     int32_t ExtractFd(const std::string& hapticsUri);
53 
54     AudioHapticPlayer &audioHapticPlayer_;
55 
56 #ifdef SUPPORT_VIBRATOR
57     VibratorUsage vibratorUsage_ = VibratorUsage::USAGE_UNKNOWN;
58     std::shared_ptr<VibratorFileDescription> vibratorFD_ = nullptr;
59     std::shared_ptr<VibratorPackage> vibratorPkg_ = nullptr;
60     std::condition_variable vibrateCV_;
61     float vibrateIntensity_ = 1.0f;
62     bool isSupportEffectId_ = false;
63     HapticSource hapticSource_;
64 #endif
65     std::mutex vibrateMutex_;
66     AudioStandard::StreamUsage streamUsage_ = AudioStandard::StreamUsage::STREAM_USAGE_UNKNOWN;
67     bool isStopped_ = false;
68 };
69 } // namespace Media
70 } // namespace OHOS
71 #endif // AUDIO_HAPTIC_VIBRATOR_IMPL_H
72