• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 FOUNDATION_ACE_ENGINE_ADAPTER_OHOS_ENTRANCE_PICKER_PICKER_AUDIO_HAPTIC_CONTROLLER_H
17 #define FOUNDATION_ACE_ENGINE_ADAPTER_OHOS_ENTRANCE_PICKER_PICKER_AUDIO_HAPTIC_CONTROLLER_H
18 
19 #include <condition_variable>
20 #include <cmath>
21 #include <mutex>
22 #include <thread>
23 #include <deque>
24 
25 #include "adapter/ohos/entrance/picker/picker_haptic_interface.h"
26 #include "core/components/common/layout/screen_system_manager.h"
27 #include "core/gestures/velocity_tracker.h"
28 #include "frameworks/base/memory/ace_type.h"
29 #include "audio_haptic_manager.h"
30 #include "audio_haptic_player.h"
31 #include "audio_group_manager.h"
32 #include "audio_system_manager.h"
33 
34 namespace OHOS::Ace::NG {
35 
36 class PickerHapticController : public IPickerAudioHaptic {
37 public:
38     enum class ThreadStatus {
39         NONE,
40         START,
41         READY,
42         PLAYING,
43         PLAY_ONCE,
44     };
45 
46     PickerHapticController(const std::string& uri, const std::string& effectId) noexcept;
47     ~PickerHapticController() noexcept;
48     void Play(size_t speed) override;
49     void PlayOnce() override;
50     void Stop() override;
51     void HandleDelta(double dy) override;
52 
53 private:
54     void ThreadLoop();
55     void ThreadRelease();
56     void InitPlayThread();
57     bool IsThreadReady();
58     bool IsThreadPlaying();
59     bool IsThreadPlayOnce();
60     bool IsThreadNone();
61     double ConvertPxToMillimeters(double px) const;
62     size_t GetCurrentSpeedInMm();
63     int8_t GetPlayStatus();
64 
65 #ifndef SUPPORT_DIGITAL_CROWN
66     bool isHapticCanLoopPlay_ = false;
67     bool isInHapticLoop_ = false;
68     bool isLoopReadyToStop_ = false;
69 #endif
70     ThreadStatus playThreadStatus_ = ThreadStatus::NONE;
71     std::recursive_mutex threadMutex_;
72     std::condition_variable_any threadCv_;
73     VelocityTracker velocityTracker_;
74     int32_t effectSourceId_ = -1;
75     size_t absSpeedInMm_ = 0;
76     uint64_t  lastHandleDeltaTime_ = 0;
77     std::deque<double> recentSpeeds_;
78     std::shared_ptr<Media::AudioHapticPlayer> effectAudioHapticPlayer_ = nullptr;
79     std::shared_ptr<Media::AudioHapticManager> audioHapticManager_ = nullptr;
80     std::unique_ptr<std::thread> playThread_ = nullptr;
81     std::shared_ptr<AudioStandard::AudioGroupManager> audioGroupMngr_ = nullptr;
82     ACE_DISALLOW_COPY_AND_MOVE(PickerHapticController);
83 };
84 } // namespace OHOS::Ace::NG
85 
86 #endif // FOUNDATION_ACE_ENGINE_ADAPTER_OHOS_ENTRANCE_PICKER_PICKER_AUDIO_HAPTIC_CONTROLLER_H
87