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=Sterio 41 uint32_t SamplesPerSec = 44100; // Sampling Frequency in Hz 42 uint32_t bytesPerSec = 176400; // bytes per second 43 uint16_t blockAlign = 2; // 2=16-bit mono, 4=16-bit stereo 44 uint16_t bitsPerSample = 16; // Number of bits per sample 45 /* "data" sub-chunk */ 46 uint8_t Subchunk2ID[4] = {'d', 'a', 't', 'a'}; // "data" string 47 uint32_t Subchunk2Size = 0; // Sampled data length 48 }; 49 50 class AudioPlayer { 51 public: 52 static bool InitRenderer(const wav_hdr &wavHeader, AudioStandard::AudioStreamType streamType); 53 static int32_t Play(const std::string &path, AudioStandard::AudioStreamType streamType, PlayerType playerType); 54 static void SetStop(PlayerType playerType, bool state); 55 56 private: 57 static constexpr uint16_t READ_SIZE = 1; 58 static constexpr uint16_t MIN_BYTES = 4; 59 static size_t bufferLen; 60 static bool isStop_; 61 static bool isRingStop_; 62 static bool isToneStop_; 63 static bool IsStop(PlayerType playerType); 64 static void ReleaseRenderer(); 65 static std::unique_ptr<AudioStandard::AudioRenderer> audioRenderer_; 66 }; 67 } // namespace Telephony 68 } // namespace OHOS 69 70 #endif // TELEPHONY_AUDIO_PLAYER_H