• 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_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 AudioSource& audioSource, const bool &muteAudio,
28         const AudioStandard::StreamUsage &streamUsage);
29     ~AudioHapticSoundNormalImpl();
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 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(const bool &isLoop);
47 
48 private:
49     int32_t LoadAVPlayer();
50     int32_t ResetAVPlayer();
51     int32_t ReleaseSoundInternal();
52     void ReleaseAVPlayer();
53     int32_t OpenAudioSource();
54 
55     AudioSource audioSource_;
56     bool muteAudio_ = false;
57     AudioStandard::StreamUsage streamUsage_ = AudioStandard::STREAM_USAGE_UNKNOWN;
58     float volume_ = 1.0f;
59     bool loop_ = false;
60     AudioSource configuredAudioSource_;
61     std::atomic<AudioHapticPlayerState> playerState_ = AudioHapticPlayerState::STATE_NEW;
62 
63     std::weak_ptr<AudioHapticSoundCallback> audioHapticPlayerCallback_;
64 
65     std::mutex audioHapticPlayerLock_;
66 
67     // var for avplayer
68     std::shared_ptr<Media::Player> avPlayer_ = nullptr;
69     std::shared_ptr<PlayerCallback> avPlayerCallback_ = nullptr;
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     int32_t audioHapticSyncId_ = 0;
77 };
78 
79 class AHSoundNormalCallback : public PlayerCallback {
80 public:
81     explicit AHSoundNormalCallback(std::shared_ptr<AudioHapticSoundNormalImpl> soundNormalImpl);
82     virtual ~AHSoundNormalCallback() = default;
83 
84     // PlayerCallback override
85     void OnError(int32_t errorCode, const std::string &errorMsg) override;
86     void OnInfo(Media::PlayerOnInfoType type, int32_t extra, const Media::Format &infoBody) override;
87 
88 private:
89     void HandleStateChangeEvent(int32_t extra, const Format &infoBody);
90     void HandleAudioInterruptEvent(int32_t extra, const Format &infoBody);
91     void HandleAudioFirstFrameEvent(int32_t extra, const Format &infoBody);
92     void HandleEOSEvent(int32_t extra, const Format &infoBody);
93 
94     std::weak_ptr<AudioHapticSoundNormalImpl> soundNormalImpl_;
95     AudioHapticPlayerState playerState_ = AudioHapticPlayerState::STATE_NEW;
96 };
97 } // namespace Media
98 } // namespace OHOS
99 #endif // AUDIO_HAPTIC_SOUND_NORMAL_IMPL_H
100