• 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 
40     void NotifyEndofStreamEvent(const int32_t &streamId);
41     void NotifyInterruptEvent(const int32_t &streamId, const AudioStandard::InterruptEvent &interruptEvent);
42 
43 private:
44     int32_t InitPlayer(const std::string &audioUri);
45     int32_t CreatePlayerWithOptions(const AudioHapticPlayerOptions &options);
46     void DeletePlayer(const int32_t &streamId);
47     void DeleteAllPlayer();
48     std::string GetHapticUriForAudioUri(const std::string &audioUri);
49     bool IsFileExisting(const std::string &fileUri);
50     std::string GetDefaultNonSyncHapticsPath();
51     SystemToneOptions GetOptionsFromRingerMode();
52     void UpdateStreamId();
53     std::string ChangeUri(const std::string &uri);
54 
55     std::shared_ptr<AudioHapticManager> audioHapticManager_ = nullptr;
56     std::unordered_map<int32_t, std::shared_ptr<AudioHapticPlayer>> playerMap_;
57     std::unordered_map<int32_t, std::shared_ptr<SystemTonePlayerCallback>> callbackMap_;
58     int32_t sourceId_ = -1;
59     int32_t streamId_ = 0;
60     std::string configuredUri_ = "";
61     std::string defaultNonSyncHapticUri_ = "";
62     std::shared_ptr<AbilityRuntime::Context> context_;
63     SystemSoundManagerImpl &systemSoundMgr_;
64     SystemToneType systemToneType_;
65     SystemToneState systemToneState_ = SystemToneState::STATE_INVALID;
66     std::string hapticUri_ = "";
67     bool isHapticUriEmpty_ = false;
68 
69     std::mutex systemTonePlayerMutex_;
70 };
71 
72 class SystemTonePlayerCallback : public AudioHapticPlayerCallback {
73 public:
74     explicit SystemTonePlayerCallback(int32_t streamId, std::shared_ptr<SystemTonePlayerImpl> systemTonePlayerImpl);
75     virtual ~SystemTonePlayerCallback() = default;
76 
77     void OnInterrupt(const AudioStandard::InterruptEvent &interruptEvent) override;
78     void OnEndOfStream(void) override;
79     void OnError(int32_t errorCode) override;
80 
81 private:
82     std::weak_ptr<SystemTonePlayerImpl> systemTonePlayerImpl_;
83     int32_t streamId_;
84 };
85 } // namespace Media
86 } // namespace OHOS
87 #endif // SYSTEM_TONE_PLAYER_IMPL_H
88