• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef IMSMEDIA_AUDIO_PLAYER_INCLUDED
18 #define IMSMEDIA_AUDIO_PLAYER_INCLUDED
19 
20 #include <ImsMediaAudioDefine.h>
21 #include <aaudio/AAudio.h>
22 #include <media/NdkMediaCodec.h>
23 #include <media/NdkMediaFormat.h>
24 #include <mutex>
25 
26 using android::sp;
27 
28 enum FrameType : uint8_t
29 {
30     SPEECH = 0,
31     SID,
32     LOST,
33     NO_DATA
34 };
35 
36 class ImsMediaAudioPlayer
37 {
38 public:
39     ImsMediaAudioPlayer();
40     virtual ~ImsMediaAudioPlayer();
41 
42     /**
43      * @brief Sets the codec type
44      *
45      * @param type kAudioCodecType defined in ImsMediaDefine.h
46      */
47     void SetCodec(int32_t type);
48 
49     /**
50      * @brief Sets the evs bitrate converted from codec mode
51      *
52      * @param mode
53      */
54     void SetEvsBitRate(int32_t bitRate);
55 
56     /**
57      * @brief Sets the Sampling rate of the audio player
58      *
59      * @param samplingRate
60      */
61     void SetSamplingRate(int32_t samplingRate);
62 
63     /**
64      * @brief sets the Evs Codec mode.
65      *
66      * @param mode used to define evs codec mode.
67      */
68     void SetCodecMode(uint32_t mode);
69 
70     /**
71      * @brief Sets the EVS codec offset of the channel aware mode
72      *
73      * @param offset Permissible values are -1, 0, 2, 3, 5, and 7. If ch-aw-recv is -1,
74      * channel-aware mode is disabled
75      */
76     void SetEvsChAwOffset(int32_t offset);
77 
78     /**
79      * @brief Sets the bandwidth of the EVS codec.
80      *
81      * @param evsBandwidth kEvsBandwidth defined in ImsMediaDefine.h
82      */
83     void SetEvsBandwidth(int32_t evsBandwidth);
84 
85     /**
86      * @brief Sets the payload header mode of the EVS codec.
87      *
88      * @param EvsPayloadHeaderMode kRtpPayloadHeaderMode defined in ImsMediaDefine.h
89      */
90     void SetEvsPayloadHeaderMode(int32_t EvsPayloadHeaderMode);
91 
92     /**
93      * @brief Set Whether discontinuous transmission is enabled or not
94      *
95      * @params isDtxEnabled, if set to true then enable discontinuous transmission
96      */
97     void SetDtxEnabled(bool isDtxEnabled);
98 
99     /**
100      * @brief Setting octet-align for AMR/AMR-WB
101      *
102      * @params isOctetAligned, If it's set to true then all fields in the AMR/AMR-WB header
103      * shall be aligned to octet boundaries by adding padding bits.
104      */
105     void SetOctetAligned(bool isOctetAligned);
106 
107     /**
108      * @brief Set the cmr value to change the audio mode
109      *
110      * @param cmr The 4 bit of cmr define code for EVS modes and not used for AMR/AMR-WB codec
111      */
112     void ProcessCmr(const uint32_t cmr);
113 
114     /**
115      * @brief Starts audio player to play the decoded audio frame and ndk audio decoder to decode
116      * the given data
117      *
118      * @return true Returns when the audio codec and aaudio runs without error
119      * @return false Returns when the audio codec configuration is invalid gets error during the
120      * launch
121      */
122     bool Start();
123 
124     /**
125      * @brief Stops audio player to stop the aaudio and ndk audio decoder
126      *
127      */
128     void Stop();
129 
130     /**
131      * @brief decodeFrame sends encoded audio frame for decoding.
132      *
133      * @param decoderInput compressed audio frame passed to AoC for decode and playback.
134      * @param nDataSize compressed frame size as per negotiated bitrate.
135      * @param frameType
136      *          0 - SPEECH frame
137      *          1 - SID
138      *              size > 0 : SID packet.
139      *              size = 0 : SID interval.
140      *          2 - LOST frame.
141      *              size > 0 if Partial Frame is present and buffer contains partial frame (EVS
142      * only). size = 0 iF Partial Frame is not present. 3 - NO_DATA : SID interval
143      * @param hasNextFrame true when next frame is available in JitterBuffer (EVS only). false
144      * otherwise.
145      * @param nextFrameFirstByte first byte of next frame if available in JitterBuffer (EVS only). 0
146      * otherwise.
147      *
148      * @return true on audio frame decoded successfully.
149      * @return false on audio frame decode failure.
150      */
151     virtual bool onDataFrame(uint8_t* buffer, uint32_t size, FrameType frameType, bool hasNextFrame,
152             uint8_t nextFrameByte);
153 
154 private:
155     void openAudioStream();
156     void restartAudioStream();
157     static void audioErrorCallback(AAudioStream* stream, void* userData, aaudio_result_t error);
158     bool decodeAmr(uint8_t* buffer, uint32_t size);
159     bool decodeEvs(uint8_t* buffer, uint32_t size);
160 
161     AAudioStream* mAudioStream;
162     AMediaCodec* mCodec;
163     AMediaFormat* mFormat;
164     int32_t mCodecType;
165     uint32_t mCodecMode;
166     int32_t mSamplingRate;
167     int32_t mEvsChAwOffset;
168     kEvsBandwidth mEvsBandwidth;
169     uint16_t mBuffer[PCM_BUFFER_SIZE];
170     std::mutex mMutex;
171     int32_t mEvsBitRate;
172     kRtpPayloadHeaderMode mEvsCodecHeaderMode;
173     bool mIsFirstFrame;
174     bool mIsEvsInitialized;
175     bool mIsDtxEnabled;
176     bool mIsOctetAligned;
177 };
178 
179 #endif
180