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 std::string &audioUri, const bool &muteAudio, 28 const AudioStandard::StreamUsage &streamUsage, const bool ¶llelPlayFlag = false); 29 ~AudioHapticSoundLowLatencyImpl(); 30 31 // AudioHapticSound override 32 int32_t PrepareSound() override; 33 int32_t StartSound() 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 void ReleaseSoundPoolPlayer(); 49 int32_t OpenAudioUri(const std::string &audioUri); 50 51 std::string audioUri_ = ""; 52 bool muteAudio_ = false; 53 bool parallelPlayFlag_ = false; 54 AudioStandard::StreamUsage streamUsage_ = AudioStandard::STREAM_USAGE_UNKNOWN; 55 float volume_ = 1.0f; 56 bool loop_ = false; 57 std::string configuredAudioUri_ = ""; 58 AudioHapticPlayerState playerState_ = AudioHapticPlayerState::STATE_NEW; 59 60 std::weak_ptr<AudioHapticSoundCallback> audioHapticPlayerCallback_; 61 62 std::mutex audioHapticPlayerLock_; 63 64 // var for sound pool 65 std::shared_ptr<Media::ISoundPool> soundPoolPlayer_ = nullptr; 66 std::shared_ptr<ISoundPoolCallback> soundPoolCallback_ = nullptr; 67 std::shared_ptr<ISoundPoolFrameWriteCallback> firstFrameCallback_ = nullptr; 68 int32_t soundID_ = -1; 69 int32_t streamID_ = -1; 70 int32_t fileDes_ = -1; 71 bool isPrepared_ = false; 72 bool isReleased_ = false; 73 bool isUnsupportedFile_ = false; 74 std::mutex prepareMutex_; 75 std::condition_variable prepareCond_; 76 }; 77 78 class AHSoundLowLatencyCallback : public ISoundPoolCallback { 79 public: 80 explicit AHSoundLowLatencyCallback(std::shared_ptr<AudioHapticSoundLowLatencyImpl> soundLowLatencyImpl); 81 virtual ~AHSoundLowLatencyCallback() = default; 82 83 // ISoundPoolCallback override 84 void OnLoadCompleted(int32_t soundId) override; 85 void OnPlayFinished() override; 86 void OnError(int32_t errorCode) override; 87 88 private: 89 std::weak_ptr<AudioHapticSoundLowLatencyImpl> soundLowLatencyImpl_; 90 }; 91 92 class AHSoundFirstFrameCallback : public ISoundPoolFrameWriteCallback { 93 public: 94 explicit AHSoundFirstFrameCallback(std::shared_ptr<AudioHapticSoundLowLatencyImpl> soundLowLatencyImpl); 95 virtual ~AHSoundFirstFrameCallback() = default; 96 97 void OnFirstAudioFrameWritingCallback(uint64_t &latency) override; 98 private: 99 std::weak_ptr<AudioHapticSoundLowLatencyImpl> soundLowLatencyImpl_; 100 }; 101 } // namespace Media 102 } // namespace OHOS 103 #endif // AUDIO_HAPTIC_SOUND_LOW_LATENCY_IMPL_H