• 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 SYSTEM_TONE_PLAYER_IMPL_H
17 #define SYSTEM_TONE_PLAYER_IMPL_H
18 
19 #include "audio_haptic_manager.h"
20 #include "system_sound_manager_impl.h"
21 
22 namespace OHOS {
23 namespace Media {
24 class SystemTonePlayerCallback;
25 
26 class SystemTonePlayerImpl : public SystemTonePlayer, public std::enable_shared_from_this<SystemTonePlayerImpl> {
27 public:
28     SystemTonePlayerImpl(const std::shared_ptr<AbilityRuntime::Context> &context,
29         SystemSoundManagerImpl &systemSoundMgr, SystemToneType systemToneType);
30     ~SystemTonePlayerImpl();
31 
32     // SystemTonePlayer override
33     std::string GetTitle() const override;
34     int32_t Prepare() override;
35     int32_t Start() override;
36     int32_t Start(const SystemToneOptions &systemToneOptions) override;
37     int32_t Stop(const int32_t &streamID) override;
38     int32_t Release() override;
39     int32_t SetAudioVolume(float volume) override;
40     int32_t GetAudioVolume(float &recvValue) override;
41     int32_t GetSupportHapticsFeatures(std::vector<ToneHapticsFeature> &recvFeatures) override;
42     int32_t SetHapticsFeature(ToneHapticsFeature feature) override;
43     int32_t GetHapticsFeature(ToneHapticsFeature &feature) override;
44     bool IsStreamIdExist(int32_t streamId) override;
45     int32_t SetSystemTonePlayerFinishedAndErrorCallback(
46         const std::shared_ptr<SystemTonePlayerFinishedAndErrorCallback> &finishedAndErrorCallback) override;
47 
48     void NotifyEndofStreamEvent(const int32_t &streamId);
49     void NotifyInterruptEvent(const int32_t &streamId, const AudioStandard::InterruptEvent &interruptEvent);
50     void NotifyErrorEvent(int32_t errCode);
51 
52 private:
53     int32_t InitPlayer(const std::string &audioUri);
54     int32_t CreatePlayerWithOptions(const AudioHapticPlayerOptions &options);
55     void DeletePlayer(const int32_t &streamId);
56     void DeleteAllPlayer();
57     std::string GetNewHapticUriForAudioUri(const std::string &audioUri, const std::string &ringtonePath,
58         const std::string& hapticsPath);
59     void GetNewHapticUriForAudioUri(const std::string &audioUri,
60         std::map<ToneHapticsFeature, std::string> &hapticsUriMap);
61     void GetHapticUriForAudioUri(const std::string &audioUri, std::map<ToneHapticsFeature, std::string> &hapticsUris);
62     std::string GetDefaultNonSyncHapticsPath();
63     SystemToneOptions GetOptionsFromRingerMode();
64     std::string ChangeUri(const std::string &uri);
65     void InitHapticsSourceIds();
66     void ReleaseHapticsSourceIds();
67     ToneHapticsType ConvertToToneHapticsType(SystemToneType type);
68     HapticsMode ConvertToHapticsMode(ToneHapticsMode toneHapticsMode);
69     void GetNewHapticSettings(const std::string &audioUri, std::map<ToneHapticsFeature, std::string> &hapticsUris);
70     std::string ChangeHapticsUri(const std::string &hapticsUri);
71     void GetCurrentHapticSettings(const std::string &audioUri, std::map<ToneHapticsFeature, std::string> &hapticUriMap);
72     bool IsSameHapticMaps(const std::map<ToneHapticsFeature, std::string> &hapticUriMap);
73     void UpdateStreamId();
74     bool InitDataShareHelper();
75     void ReleaseDataShareHelper();
76     int32_t RegisterSource(const std::string &audioUri, const std::string &hapticUri);
77     void CreateCallbackThread(int32_t delayTime);
78     void DeleteCallbackThreadId(int32_t streamId);
79     void DeleteAllCallbackThreadId();
80     bool IsExitCallbackThreadId(int32_t streamId);
81 
82     std::shared_ptr<AudioHapticManager> audioHapticManager_ = nullptr;
83     std::unordered_map<int32_t, std::shared_ptr<AudioHapticPlayer>> playerMap_;
84     std::unordered_map<int32_t, std::shared_ptr<SystemTonePlayerCallback>> callbackMap_;
85     int32_t streamId_ = 0;
86     std::string configuredUri_ = "";
87     std::string defaultNonSyncHapticUri_ = "";
88     std::shared_ptr<AbilityRuntime::Context> context_;
89     SystemSoundManagerImpl &systemSoundMgr_;
90     SystemToneType systemToneType_;
91     SystemToneState systemToneState_ = SystemToneState::STATE_INVALID;
92     float volume_ = SYS_TONE_PLAYER_MAX_VOLUME;
93     ToneHapticsFeature hapticsFeature_ = ToneHapticsFeature::STANDARD;
94     std::map<ToneHapticsFeature, int32_t> sourceIds_;
95     std::vector<ToneHapticsFeature> supportedHapticsFeatures_;
96     HapticsMode hapticsMode_ = HapticsMode::HAPTICS_MODE_INVALID;
97     std::map<ToneHapticsFeature, std::string> hapticUriMap_;
98     bool isHapticUriEmpty_ = false;
99     bool isNoneHaptics_ = false;
100     std::shared_ptr<DataShare::DataShareHelper> dataShareHelper_ = nullptr;
101     std::shared_ptr<SystemTonePlayerFinishedAndErrorCallback> finishedAndErrorCallback_ = nullptr;
102     std::unordered_map<int32_t, std::thread::id> callbackThreadIdMap_;
103 
104     std::mutex systemTonePlayerMutex_;
105 };
106 
107 class SystemTonePlayerCallback : public AudioHapticPlayerCallback {
108 public:
109     explicit SystemTonePlayerCallback(int32_t streamId, std::shared_ptr<SystemTonePlayerImpl> systemTonePlayerImpl);
110     virtual ~SystemTonePlayerCallback() = default;
111 
112     void OnInterrupt(const AudioStandard::InterruptEvent &interruptEvent) override;
113     void OnEndOfStream(void) override;
114     void OnError(int32_t errorCode) override;
115 
116 private:
117     std::weak_ptr<SystemTonePlayerImpl> systemTonePlayerImpl_;
118     int32_t streamId_;
119 };
120 } // namespace Media
121 } // namespace OHOS
122 #endif // SYSTEM_TONE_PLAYER_IMPL_H
123