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 
45     void NotifyEndofStreamEvent(const int32_t &streamId);
46     void NotifyInterruptEvent(const int32_t &streamId, const AudioStandard::InterruptEvent &interruptEvent);
47 
48 private:
49     int32_t InitPlayer(const std::string &audioUri);
50     int32_t CreatePlayerWithOptions(const AudioHapticPlayerOptions &options);
51     void DeletePlayer(const int32_t &streamId);
52     void DeleteAllPlayer();
53     std::string GetNewHapticUriForAudioUri(const std::string &audioUri, const std::string &ringtonePath,
54         const std::string& hapticsPath);
55     void GetNewHapticUriForAudioUri(const std::string &audioUri,
56         std::map<ToneHapticsFeature, std::string> &hapticsUriMap);
57     void GetHapticUriForAudioUri(const std::string &audioUri, std::map<ToneHapticsFeature, std::string> &hapticsUris);
58     std::string GetDefaultNonSyncHapticsPath();
59     SystemToneOptions GetOptionsFromRingerMode();
60     std::string ChangeUri(const std::string &uri);
61     void InitHapticsSourceIds();
62     void ReleaseHapticsSourceIds();
63     ToneHapticsType ConvertToToneHapticsType(SystemToneType type);
64     HapticsMode ConvertToHapticsMode(ToneHapticsMode toneHapticsMode);
65     void GetNewHapticSettings(const std::string &audioUri, std::map<ToneHapticsFeature, std::string> &hapticsUris);
66     std::string ChangeHapticsUri(const std::string &hapticsUri);
67     void GetCurrentHapticSettings(const std::string &audioUri, std::map<ToneHapticsFeature, std::string> &hapticUriMap);
68     bool IsSameHapticMaps(const std::map<ToneHapticsFeature, std::string> &hapticUriMap);
69     void UpdateStreamId();
70     bool InitDataShareHelper();
71     void ReleaseDataShareHelper();
72     int32_t RegisterSource(const std::string &audioUri, const std::string &hapticUri);
73 
74     std::shared_ptr<AudioHapticManager> audioHapticManager_ = nullptr;
75     std::unordered_map<int32_t, std::shared_ptr<AudioHapticPlayer>> playerMap_;
76     std::unordered_map<int32_t, std::shared_ptr<SystemTonePlayerCallback>> callbackMap_;
77     int32_t streamId_ = 0;
78     std::string configuredUri_ = "";
79     std::string defaultNonSyncHapticUri_ = "";
80     std::shared_ptr<AbilityRuntime::Context> context_;
81     SystemSoundManagerImpl &systemSoundMgr_;
82     SystemToneType systemToneType_;
83     SystemToneState systemToneState_ = SystemToneState::STATE_INVALID;
84     float volume_ = SYS_TONE_PLAYER_MAX_VOLUME;
85     ToneHapticsFeature hapticsFeature_ = ToneHapticsFeature::STANDARD;
86     std::map<ToneHapticsFeature, int32_t> sourceIds_;
87     std::vector<ToneHapticsFeature> supportedHapticsFeatures_;
88     HapticsMode hapticsMode_ = HapticsMode::HAPTICS_MODE_INVALID;
89     std::map<ToneHapticsFeature, std::string> hapticUriMap_;
90     bool isHapticUriEmpty_ = false;
91     std::shared_ptr<DataShare::DataShareHelper> dataShareHelper_ = nullptr;
92 
93     std::mutex systemTonePlayerMutex_;
94 };
95 
96 class SystemTonePlayerCallback : public AudioHapticPlayerCallback {
97 public:
98     explicit SystemTonePlayerCallback(int32_t streamId, std::shared_ptr<SystemTonePlayerImpl> systemTonePlayerImpl);
99     virtual ~SystemTonePlayerCallback() = default;
100 
101     void OnInterrupt(const AudioStandard::InterruptEvent &interruptEvent) override;
102     void OnEndOfStream(void) override;
103     void OnError(int32_t errorCode) override;
104 
105 private:
106     std::weak_ptr<SystemTonePlayerImpl> systemTonePlayerImpl_;
107     int32_t streamId_;
108 };
109 } // namespace Media
110 } // namespace OHOS
111 #endif // SYSTEM_TONE_PLAYER_IMPL_H
112