1 /* 2 * Copyright (c) 2022 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_TONEPLAYER_PRIVATE_H_ 17 #define AUDIO_TONEPLAYER_PRIVATE_H_ 18 19 #include <map> 20 #include <thread> 21 #include "tone_player.h" 22 #include "audio_renderer.h" 23 24 namespace OHOS { 25 namespace AudioStandard { 26 class TonePlayerPrivate : public AudioRendererWriteCallback, public AudioRendererCallback, public TonePlayer, 27 public std::enable_shared_from_this<TonePlayerPrivate> { 28 public: 29 TonePlayerPrivate(const std::string cachePath, const AudioRendererInfo &rendererInfo); 30 ~TonePlayerPrivate(); 31 bool LoadTone(ToneType toneType) override; 32 bool StartTone() override; 33 bool StopTone() override; 34 bool Release() override; 35 void OnInterrupt(const InterruptEvent &interruptEvent) override; 36 void OnStateChange(const RendererState state, const StateChangeCmdType __attribute__((unused)) cmdType) override; 37 void OnWriteData(size_t length) override; 38 39 private: 40 static constexpr float TRACK_VOLUME = 0.5f; 41 42 enum tone_player_state { 43 TONE_PLAYER_IDLE, // TonePlayer is being initialized or initialization failed 44 TONE_PLAYER_INIT, // TonePlayer has been successfully initialized and is not playing 45 TONE_PLAYER_STARTING, // TonePlayer is starting playing 46 TONE_PLAYER_PLAYING, // TonePlayer is playing tone 47 TONE_PLAYER_STOPPING, // TonePlayer is the last End phase 48 TONE_PLAYER_STOPPED, // The Audiorenerer will be stopped 49 }; 50 51 enum tone_player_event { 52 PLAYER_EVENT_LOAD, // Event used to load the tone 53 PLAYER_EVENT_PLAY, // TonePlayer is starting playing 54 PLAYER_EVENT_STOP, // Event used to playing tone 55 }; 56 57 enum tone_data_state { 58 TONE_DATA_LOADING, // STATE when tone data is preparing 59 TONE_DATA_LOADED, // STATE when thread filled the data gone for waiting 60 TONE_DATA_REQUESTED, // STATE when AudioRender waiting for the data 61 }; 62 typedef std::cv_status status_t; 63 AudioRendererOptions rendererOptions = {}; 64 uint32_t currSegment_; // Current segment index in ToneDescriptor segments[] 65 uint32_t currCount_; // Current sequence repeat count 66 std::shared_ptr<ToneInfo> toneInfo_; // pointer to active tone Info 67 std::shared_ptr<ToneInfo> initialToneInfo_; // pointer to new active tone Info 68 std::vector<int32_t> supportedTones_; 69 volatile uint16_t tonePlayerState_; // TonePlayer state (tone_state) 70 std::string cachePath_; // NAPI interface to create AudioRenderer 71 uint32_t loopCounter_; // Current tone loopback count 72 uint32_t totalSample_; // Total no. of tone samples played 73 uint32_t nextSegSample_; // Position of next segment transition expressed in samples 74 uint32_t maxSample_; // Maximum number of audio samples played (maximun tone duration) 75 uint32_t samplingRate_; // AudioFlinger Sampling rate 76 uint32_t sampleCount_; // Initial value should be zero before any new Tone renderering 77 std::unique_ptr<AudioRenderer> audioRenderer_; // Pointer to AudioRenderer used for playback 78 std::mutex mutexLock_; // Mutex to control concurent access 79 std::mutex cbkCondLock_; // Mutex associated to waitAudioCbkCond_ 80 std::condition_variable waitAudioCbkCond_; // condition enabling interface 81 std::mutex dataCondLock_; // Mutex associated to waitToneDataCond_ 82 std::condition_variable waitToneDataCond_; // condition enabling interface 83 tone_data_state toneDataState_; 84 // to wait for audio rendere callback completion after a change is requested 85 float volume_; // Volume applied to audio Renderer 86 volatile bool playWait_ = true; // wait_for state 87 #ifdef DUMPFILE 88 FILE *pfd_; 89 #endif // DUMPFILE 90 uint32_t processSize_; // In audioRenderer, Size of audio blocks generated at a time 91 bool InitAudioRenderer(); 92 void AudioToneRendererCallback(); 93 void AudioToneDataThreadFunc(); 94 bool InitToneWaveInfo(); 95 bool TonePlayerStateHandler(int16_t event); 96 int32_t GetSamples(uint16_t *freqs, int8_t *buffer, uint32_t samples); 97 bool LoadEventStateHandler(); 98 bool PlayEventStateHandler(); 99 void StopEventStateHandler(); 100 bool ContinueToneplay(uint32_t sampleCnt, int8_t *audioBuf); 101 bool CheckToneStarted(uint32_t sampleCnt, int8_t *audioBuf); 102 bool CheckToneStopped(); 103 void GetCurrentSegmentUpdated(std::shared_ptr<ToneInfo> toneDesc); 104 bool CheckToneContinuity (); 105 bool AudioToneSequenceGen(BufferDesc &bufDesc); 106 std::unique_ptr<std::thread> toneDataGenLoop_ = nullptr; 107 }; 108 } // namespace AudioStandard 109 } // namespace OHOS 110 #endif /* AUDIO_TONEPLAYER_H_ */ 111