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