1 /* 2 * Copyright (C) 2021 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 TELEPHONY_AUDIO_PLAYER_H 17 #define TELEPHONY_AUDIO_PLAYER_H 18 #include <cstdint> 19 #include <string> 20 21 #include "audio_capturer.h" 22 #include "audio_renderer.h" 23 24 namespace OHOS { 25 namespace Telephony { 26 enum PlayerType { 27 TYPE_RING = 0, 28 TYPE_TONE, 29 TYPE_DTMF, 30 TYPE_SOUND, 31 }; 32 33 struct wav_hdr { 34 /* RIFF Chunk Descriptor */ 35 uint8_t RIFF[4] = {'R', 'I', 'F', 'F'}; // RIFF Header Magic header 36 uint32_t ChunkSize = 0; // RIFF Chunk Size 37 uint8_t WAVE[4] = {'W', 'A', 'V', 'E'}; // WAVE Header 38 /* "fmt" sub-chunk */ 39 uint8_t fmt[4] = {'f', 'm', 't', ' '}; // FMT header 40 uint32_t Subchunk1Size = 16; // Size of the fmt chunk 41 uint16_t AudioFormat = 1; // Audio format 1=PCM 42 uint16_t NumOfChan = 2; // Number of channels 1=Mono 2=Stereo 43 uint32_t SamplesPerSec = 44100; // Sampling Frequency in Hz 44 uint32_t bytesPerSec = 176400; // bytes per second 45 uint16_t blockAlign = 4; // 2=16-bit mono, 4=16-bit stereo 46 /* "INFO" sub-chunk */ 47 uint16_t SubchunkInfo1 = 16; 48 uint8_t LISTB[5] = {'L', 'I', 'S', 'T', 'B'}; 49 uint16_t SubchunkInfo2 = 0; 50 uint8_t SubchunkInfo3 = 0; 51 uint8_t INFOICRD[8] = {'I', 'N', 'F', 'O', 'I', 'C', 'R', 'D'}; 52 uint32_t SubchunkInfo4 = 11; 53 uint8_t TIME[10] = {'2', '0', '2', '0', '-', '0', '9', '-', '0', '4'}; 54 uint16_t SubchunkInfo5 = 0; 55 uint8_t ISFT[4] = {'I', 'S', 'F', 'T'}; 56 uint32_t SubchunkInfo6 = 14; 57 uint8_t Lavf[18] = {'L', 'a', 'v', 'f', '5', '8', ' ', '7', '6', ' ', '1', '0', '0', ' ', 'I', 'T', 'C', 'H'}; 58 uint32_t SubchunkInfo7 = 12; 59 uint8_t Logic[12] = {'L', 'o', 'g', 'i', 'c', ' ', 'P', 'r', 'o', ' ', 'X', ' '}; 60 /* "data" sub-chunk */ 61 uint8_t Subchunk2ID[4] = {'d', 'a', 't', 'a'}; // "data" string 62 uint32_t Subchunk2Size = 0; // Sampled data length 63 }; 64 65 class AudioPlayer { 66 public: 67 AudioPlayer() = default; 68 ~AudioPlayer() = default; 69 bool InitRenderer(const wav_hdr &wavHeader, AudioStandard::AudioStreamType streamType); 70 bool InitRenderer(); 71 bool InitCapturer(); 72 int32_t Play(const std::string &path, AudioStandard::AudioStreamType streamType, PlayerType playerType); 73 int32_t Play(PlayerType playerType); 74 void ReleaseRenderer(); 75 void ReleaseCapturer(); 76 void SetStop(PlayerType playerType, bool state); 77 78 private: 79 class CallAudioRendererCallback : public AudioStandard::AudioRendererCallback { 80 public: 81 void OnInterrupt(const AudioStandard::InterruptEvent &interruptEvent) override; OnStateChange(const AudioStandard::RendererState state,const AudioStandard::StateChangeCmdType cmdType)82 void OnStateChange(const AudioStandard::RendererState state, 83 const AudioStandard::StateChangeCmdType cmdType) override {} 84 }; 85 86 private: 87 static constexpr uint16_t READ_SIZE = 1; 88 static constexpr uint16_t MIN_BYTES = 4; 89 static constexpr uint16_t RENDERER_FLAG = 0; 90 static constexpr uint16_t CAPTURER_FLAG = 0; 91 size_t bufferLen = 0; 92 bool isStop_ = false; 93 bool isRingStop_ = false; 94 bool isRenderInitialized_ = false; 95 bool isCapturerInitialized_ = false; 96 bool isToneStop_ = false; 97 bool isSoundStop_ = false; 98 std::shared_ptr<AudioStandard::AudioRendererCallback> callback_ = nullptr; 99 bool IsStop(PlayerType playerType); 100 std::unique_ptr<AudioStandard::AudioRenderer> audioRenderer_ = nullptr; 101 std::unique_ptr<AudioStandard::AudioCapturer> audioCapturer_ = nullptr; 102 bool GetRealPath(const std::string &profilePath, std::string &realPath); 103 }; 104 } // namespace Telephony 105 } // namespace OHOS 106 107 #endif // TELEPHONY_AUDIO_PLAYER_H 108