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