• 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 AUDIO_HAPTIC_SOUND_LOW_LATENCY_IMPL_H
17 #define AUDIO_HAPTIC_SOUND_LOW_LATENCY_IMPL_H
18 
19 #include "audio_haptic_sound.h"
20 #include "isoundpool.h"
21 
22 namespace OHOS {
23 namespace Media {
24 class AudioHapticSoundLowLatencyImpl : public AudioHapticSound,
25     public std::enable_shared_from_this<AudioHapticSoundLowLatencyImpl> {
26 public:
27     AudioHapticSoundLowLatencyImpl(const AudioSource& audioSource, const bool &muteAudio,
28         const AudioStandard::StreamUsage &streamUsage, const bool &parallelPlayFlag = false);
29     ~AudioHapticSoundLowLatencyImpl();
30 
31     // AudioHapticSound override
32     int32_t PrepareSound() override;
33     int32_t StartSound(const int32_t &audioHapticSyncId = 0) override;
34     int32_t StopSound() override;
35     int32_t ReleaseSound() override;
36     int32_t SetVolume(float volume) override;
37     int32_t SetLoop(bool loop) override;
38     int32_t GetAudioCurrentTime() override;
39     int32_t SetAudioHapticSoundCallback(const std::shared_ptr<AudioHapticSoundCallback> &callback) override;
40 
41     void NotifyPreparedEvent();
42     void NotifyErrorEvent(int32_t errorCode);
43     void NotifyFirstFrameEvent(uint64_t latency);
44     void NotifyEndOfStreamEvent();
45 
46 private:
47     int32_t LoadSoundPoolPlayer();
48     int32_t ReleaseSoundInternal();
49     void ReleaseSoundPoolPlayer();
50     int32_t OpenAudioSource();
51 
52     AudioSource audioSource_;
53     bool muteAudio_ = false;
54     bool parallelPlayFlag_ = false;
55     AudioStandard::StreamUsage streamUsage_ = AudioStandard::STREAM_USAGE_UNKNOWN;
56     float volume_ = 1.0f;
57     bool loop_ = false;
58     AudioSource configuredAudioSource_;
59     std::atomic<AudioHapticPlayerState> playerState_ = AudioHapticPlayerState::STATE_NEW;
60 
61     std::weak_ptr<AudioHapticSoundCallback> audioHapticPlayerCallback_;
62 
63     std::mutex audioHapticPlayerLock_;
64 
65     // var for sound pool
66     std::shared_ptr<Media::ISoundPool> soundPoolPlayer_ = nullptr;
67     std::shared_ptr<ISoundPoolCallback> soundPoolCallback_ = nullptr;
68     std::shared_ptr<ISoundPoolFrameWriteCallback> firstFrameCallback_ = nullptr;
69     int32_t soundID_ = -1;
70     int32_t streamID_ = -1;
71     int32_t fileDes_ = -1;
72     bool isPrepared_ = false;
73     bool isReleased_ = false;
74     bool isUnsupportedFile_ = false;
75     std::mutex prepareMutex_;
76     std::condition_variable prepareCond_;
77     int32_t audioHapticSyncId_ = 0;
78 };
79 
80 class AHSoundLowLatencyCallback : public ISoundPoolCallback {
81 public:
82     explicit AHSoundLowLatencyCallback(std::shared_ptr<AudioHapticSoundLowLatencyImpl> soundLowLatencyImpl);
83     virtual ~AHSoundLowLatencyCallback() = default;
84 
85     // ISoundPoolCallback override
86     void OnLoadCompleted(int32_t soundId) override;
87     void OnPlayFinished(int32_t streamID) override;
88     void OnError(int32_t errorCode) override;
89 
90 private:
91     std::weak_ptr<AudioHapticSoundLowLatencyImpl> soundLowLatencyImpl_;
92 };
93 
94 class AHSoundFirstFrameCallback : public ISoundPoolFrameWriteCallback {
95 public:
96     explicit AHSoundFirstFrameCallback(std::shared_ptr<AudioHapticSoundLowLatencyImpl> soundLowLatencyImpl);
97     virtual ~AHSoundFirstFrameCallback() = default;
98 
99     void OnFirstAudioFrameWritingCallback(uint64_t &latency) override;
100 private:
101     std::weak_ptr<AudioHapticSoundLowLatencyImpl> soundLowLatencyImpl_;
102 };
103 } // namespace Media
104 } // namespace OHOS
105 #endif // AUDIO_HAPTIC_SOUND_LOW_LATENCY_IMPL_H