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