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_NORMAL_IMPL_H 17 #define AUDIO_HAPTIC_SOUND_NORMAL_IMPL_H 18 19 #include "audio_haptic_sound.h" 20 #include "player.h" 21 22 namespace OHOS { 23 namespace Media { 24 class AudioHapticSoundNormalImpl : public AudioHapticSound, 25 public std::enable_shared_from_this<AudioHapticSoundNormalImpl> { 26 public: 27 AudioHapticSoundNormalImpl(const std::string &audioUri, const bool &muteAudio, 28 const AudioStandard::StreamUsage &streamUsage); 29 ~AudioHapticSoundNormalImpl(); 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 SetAVPlayerState(AudioHapticPlayerState playerState); 42 void NotifyPreparedEvent(); 43 void NotifyErrorEvent(int32_t errorCode); 44 void NotifyFirstFrameEvent(uint64_t latency); 45 void NotifyInterruptEvent(AudioStandard::InterruptEvent &interruptEvent); 46 void NotifyEndOfStreamEvent(); 47 48 private: 49 int32_t LoadAVPlayer(); 50 int32_t ResetAVPlayer(); 51 void ReleaseAVPlayer(); 52 53 std::string audioUri_ = ""; 54 bool muteAudio_ = false; 55 AudioStandard::StreamUsage streamUsage_ = AudioStandard::STREAM_USAGE_UNKNOWN; 56 float volume_ = 1.0f; 57 bool loop_ = false; 58 std::string configuredAudioUri_ = ""; 59 AudioHapticPlayerState playerState_ = AudioHapticPlayerState::STATE_NEW; 60 61 std::weak_ptr<AudioHapticSoundCallback> audioHapticPlayerCallback_; 62 63 std::mutex audioHapticPlayerLock_; 64 65 // var for avplayer 66 std::shared_ptr<Media::Player> avPlayer_ = nullptr; 67 std::shared_ptr<PlayerCallback> avPlayerCallback_ = nullptr; 68 int32_t fileDes_ = -1; 69 bool isPrepared_ = false; 70 bool isReleased_ = false; 71 bool isUnsupportedFile_ = false; 72 std::mutex prepareMutex_; 73 std::condition_variable prepareCond_; 74 }; 75 76 class AHSoundNormalCallback : public PlayerCallback { 77 public: 78 explicit AHSoundNormalCallback(std::shared_ptr<AudioHapticSoundNormalImpl> soundNormalImpl); 79 virtual ~AHSoundNormalCallback() = default; 80 81 // PlayerCallback override 82 void OnError(int32_t errorCode, const std::string &errorMsg) override; 83 void OnInfo(Media::PlayerOnInfoType type, int32_t extra, const Media::Format &infoBody) override; 84 85 private: 86 void HandleStateChangeEvent(int32_t extra, const Format &infoBody); 87 void HandleAudioInterruptEvent(int32_t extra, const Format &infoBody); 88 void HandleAudioFirstFrameEvent(int32_t extra, const Format &infoBody); 89 90 std::weak_ptr<AudioHapticSoundNormalImpl> soundNormalImpl_; 91 AudioHapticPlayerState playerState_ = AudioHapticPlayerState::STATE_NEW; 92 }; 93 } // namespace Media 94 } // namespace OHOS 95 #endif // AUDIO_HAPTIC_SOUND_NORMAL_IMPL_H 96