1 /*
2  * portaudio.cpp, port class for audio
3  *
4  * Copyright (c) 2009-2010 Wind River Systems, Inc.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include <stdlib.h>
20 #include <string.h>
21 
22 #include <OMX_Core.h>
23 #include <OMX_Component.h>
24 
25 #include <componentbase.h>
26 #include <portaudio.h>
27 
28 #define LOG_TAG "portaudio"
29 #include <log.h>
30 
PortAudio()31 PortAudio::PortAudio()
32 {
33     memset(&audioparam, 0, sizeof(audioparam));
34     ComponentBase::SetTypeHeader(&audioparam, sizeof(audioparam));
35 }
36 
SetPortAudioParam(const OMX_AUDIO_PARAM_PORTFORMATTYPE * p,bool overwrite_readonly)37 OMX_ERRORTYPE PortAudio::SetPortAudioParam(
38     const OMX_AUDIO_PARAM_PORTFORMATTYPE *p, bool overwrite_readonly)
39 {
40     if (!overwrite_readonly) {
41         OMX_ERRORTYPE ret;
42 
43         ret = ComponentBase::CheckTypeHeader((void *)p, sizeof(*p));
44         if (ret != OMX_ErrorNone)
45             return ret;
46         if (audioparam.nPortIndex != p->nPortIndex)
47             return OMX_ErrorBadPortIndex;
48     }
49     else
50         audioparam.nPortIndex = p->nPortIndex;
51 
52     audioparam.nIndex = p->nIndex;
53     audioparam.eEncoding = p->eEncoding;
54 
55     return OMX_ErrorNone;
56 }
57 
GetPortAudioParam(void)58 const OMX_AUDIO_PARAM_PORTFORMATTYPE *PortAudio::GetPortAudioParam(void)
59 {
60     return &audioparam;
61 }
62 
63 /* end of PortAudio */
64 
PortMp3()65 PortMp3::PortMp3()
66 {
67     OMX_AUDIO_PARAM_PORTFORMATTYPE audioparam;
68 
69     memcpy(&audioparam, GetPortAudioParam(), sizeof(audioparam));
70     audioparam.eEncoding = OMX_AUDIO_CodingMP3;
71     SetPortAudioParam(&audioparam, false);
72 
73     memset(&mp3param, 0, sizeof(mp3param));
74     ComponentBase::SetTypeHeader(&mp3param, sizeof(mp3param));
75 }
76 
SetPortMp3Param(const OMX_AUDIO_PARAM_MP3TYPE * p,bool overwrite_readonly)77 OMX_ERRORTYPE PortMp3::SetPortMp3Param(const OMX_AUDIO_PARAM_MP3TYPE *p,
78                                        bool overwrite_readonly)
79 {
80     if (!overwrite_readonly) {
81         OMX_ERRORTYPE ret;
82 
83         ret = ComponentBase::CheckTypeHeader((void *)p, sizeof(*p));
84         if (ret != OMX_ErrorNone)
85             return ret;
86         if (mp3param.nPortIndex != p->nPortIndex)
87             return OMX_ErrorBadPortIndex;
88     }
89     else {
90         OMX_AUDIO_PARAM_PORTFORMATTYPE audioparam;
91 
92         memcpy(&audioparam, GetPortAudioParam(), sizeof(audioparam));
93         audioparam.nPortIndex = p->nPortIndex;
94         SetPortAudioParam(&audioparam, true);
95 
96         mp3param.nPortIndex = p->nPortIndex;
97     }
98 
99     mp3param.nChannels = p->nChannels;
100     mp3param.nBitRate = p->nBitRate;
101     mp3param.nSampleRate = p->nSampleRate;
102     mp3param.nAudioBandWidth = p->nAudioBandWidth;
103     mp3param.eChannelMode = p->eChannelMode;
104     mp3param.eFormat = p->eFormat;
105 
106     return OMX_ErrorNone;
107 }
108 
GetPortMp3Param(void)109 const OMX_AUDIO_PARAM_MP3TYPE *PortMp3::GetPortMp3Param(void)
110 {
111     return &mp3param;
112 }
113 
114 /* end of PortMp3 */
115 
PortAac()116 PortAac::PortAac()
117 {
118     OMX_AUDIO_PARAM_PORTFORMATTYPE audioparam;
119 
120     memcpy(&audioparam, GetPortAudioParam(), sizeof(audioparam));
121     audioparam.eEncoding = OMX_AUDIO_CodingAAC;
122     SetPortAudioParam(&audioparam, false);
123 
124     memset(&aacparam, 0, sizeof(aacparam));
125     ComponentBase::SetTypeHeader(&aacparam, sizeof(aacparam));
126 }
127 
SetPortAacParam(const OMX_AUDIO_PARAM_AACPROFILETYPE * p,bool overwrite_readonly)128 OMX_ERRORTYPE PortAac::SetPortAacParam(const OMX_AUDIO_PARAM_AACPROFILETYPE *p,
129                                        bool overwrite_readonly)
130 {
131     if (!overwrite_readonly) {
132         OMX_ERRORTYPE ret;
133 
134         ret = ComponentBase::CheckTypeHeader((void *)p, sizeof(*p));
135         if (ret != OMX_ErrorNone)
136             return ret;
137         if (aacparam.nPortIndex != p->nPortIndex)
138             return OMX_ErrorBadPortIndex;
139     }
140     else {
141         OMX_AUDIO_PARAM_PORTFORMATTYPE audioparam;
142 
143         memcpy(&audioparam, GetPortAudioParam(), sizeof(audioparam));
144         audioparam.nPortIndex = p->nPortIndex;
145         SetPortAudioParam(&audioparam, true);
146 
147         aacparam.nPortIndex = p->nPortIndex;
148     }
149 
150     aacparam.nChannels = p->nChannels;
151     aacparam.nBitRate = p->nBitRate;
152     aacparam.nSampleRate = p->nSampleRate;
153     aacparam.nAudioBandWidth = p->nAudioBandWidth;
154     aacparam.nFrameLength = p->nFrameLength;
155     aacparam.nAACtools = p->nAACtools;
156     aacparam.nAACERtools = p->nAACERtools;
157     aacparam.eAACProfile = p->eAACProfile;
158     aacparam.eAACStreamFormat = p->eAACStreamFormat;
159     aacparam.eChannelMode = p->eChannelMode;
160 
161     return OMX_ErrorNone;
162 }
163 
GetPortAacParam(void)164 const OMX_AUDIO_PARAM_AACPROFILETYPE *PortAac::GetPortAacParam(void)
165 {
166     return &aacparam;
167 }
168 
169 /* end of PortAac */
170 
PortWma()171 PortWma::PortWma()
172 {
173     OMX_AUDIO_PARAM_PORTFORMATTYPE audioparam;
174 
175     memcpy(&audioparam, GetPortAudioParam(), sizeof(audioparam));
176     audioparam.eEncoding = OMX_AUDIO_CodingWMA;
177     SetPortAudioParam(&audioparam, false);
178 
179     memset(&wmaparam, 0, sizeof(wmaparam));
180     ComponentBase::SetTypeHeader(&wmaparam, sizeof(wmaparam));
181 }
182 
SetPortWmaParam(const OMX_AUDIO_PARAM_WMATYPE * p,bool overwrite_readonly)183 OMX_ERRORTYPE PortWma::SetPortWmaParam(const OMX_AUDIO_PARAM_WMATYPE *p,
184                                        bool overwrite_readonly)
185 {
186     if (!overwrite_readonly) {
187         OMX_ERRORTYPE ret;
188 
189         ret = ComponentBase::CheckTypeHeader((void *)p, sizeof(*p));
190         if (ret != OMX_ErrorNone)
191             return ret;
192         if (wmaparam.nPortIndex != p->nPortIndex)
193             return OMX_ErrorBadPortIndex;
194     }
195     else {
196         OMX_AUDIO_PARAM_PORTFORMATTYPE audioparam;
197 
198         memcpy(&audioparam, GetPortAudioParam(), sizeof(audioparam));
199         audioparam.nPortIndex = p->nPortIndex;
200         SetPortAudioParam(&audioparam, true);
201 
202         wmaparam.nPortIndex = p->nPortIndex;
203     }
204 
205     wmaparam.nChannels = p->nChannels;
206     wmaparam.nBitRate = p->nBitRate;
207     wmaparam.eFormat = p->eFormat;
208     wmaparam.eProfile = p->eProfile;
209     wmaparam.nSamplingRate = p->nSamplingRate;
210     wmaparam.nBlockAlign = p->nBlockAlign;
211     wmaparam.nEncodeOptions = p->nEncodeOptions;
212     wmaparam.nSuperBlockAlign = p->nSuperBlockAlign;
213 
214     return OMX_ErrorNone;
215 }
216 
GetPortWmaParam(void)217 const OMX_AUDIO_PARAM_WMATYPE *PortWma::GetPortWmaParam(void)
218 {
219     return &wmaparam;
220 }
221 
222 /* end of PortWma */
223 
PortPcm()224 PortPcm::PortPcm()
225 {
226     OMX_AUDIO_PARAM_PORTFORMATTYPE audioparam;
227 
228     memcpy(&audioparam, GetPortAudioParam(), sizeof(audioparam));
229     audioparam.eEncoding = OMX_AUDIO_CodingPCM;
230     SetPortAudioParam(&audioparam, false);
231 
232     memset(&pcmparam, 0, sizeof(pcmparam));
233     ComponentBase::SetTypeHeader(&pcmparam, sizeof(pcmparam));
234 }
235 
SetPortPcmParam(const OMX_AUDIO_PARAM_PCMMODETYPE * p,bool overwrite_readonly)236 OMX_ERRORTYPE PortPcm::SetPortPcmParam(const OMX_AUDIO_PARAM_PCMMODETYPE *p,
237                                        bool overwrite_readonly)
238 {
239     if (!overwrite_readonly) {
240         OMX_ERRORTYPE ret;
241 
242         ret = ComponentBase::CheckTypeHeader((void *)p, sizeof(*p));
243         if (ret != OMX_ErrorNone)
244             return ret;
245         if (pcmparam.nPortIndex != p->nPortIndex)
246             return OMX_ErrorBadPortIndex;
247     }
248     else {
249         OMX_AUDIO_PARAM_PORTFORMATTYPE audioparam;
250 
251         memcpy(&audioparam, GetPortAudioParam(), sizeof(audioparam));
252         audioparam.nPortIndex = p->nPortIndex;
253         SetPortAudioParam(&audioparam, true);
254 
255         pcmparam.nPortIndex = p->nPortIndex;
256     }
257 
258     pcmparam.nChannels = p->nChannels;
259     pcmparam.eNumData = p->eNumData;
260     pcmparam.eEndian = p->eEndian;
261     pcmparam.bInterleaved = p->bInterleaved;
262     pcmparam.nBitPerSample = p->nBitPerSample;
263     pcmparam.nSamplingRate = p->nSamplingRate;
264     pcmparam.ePCMMode = p->ePCMMode;
265     memcpy(&pcmparam.eChannelMapping[0], &p->eChannelMapping[0],
266            sizeof(OMX_U32) * p->nChannels);
267 
268     return OMX_ErrorNone;
269 }
270 
GetPortPcmParam(void)271 const OMX_AUDIO_PARAM_PCMMODETYPE *PortPcm::GetPortPcmParam(void)
272 {
273     return &pcmparam;
274 }
275 
276 /* end of PortPcm */
277 
PortAmr()278 PortAmr::PortAmr()
279 {
280     OMX_AUDIO_PARAM_PORTFORMATTYPE audioparam;
281 
282     memcpy(&audioparam, GetPortAudioParam(), sizeof(audioparam));
283     audioparam.eEncoding = OMX_AUDIO_CodingAMR;
284     SetPortAudioParam(&audioparam, false);
285 
286     memset(&amrparam, 0, sizeof(amrparam));
287     ComponentBase::SetTypeHeader(&amrparam, sizeof(amrparam));
288 }
289 
SetPortAmrParam(const OMX_AUDIO_PARAM_AMRTYPE * p,bool overwrite_readonly)290 OMX_ERRORTYPE PortAmr::SetPortAmrParam(const OMX_AUDIO_PARAM_AMRTYPE *p,
291                                        bool overwrite_readonly)
292 {
293     if (!overwrite_readonly) {
294         OMX_ERRORTYPE ret;
295 
296         ret = ComponentBase::CheckTypeHeader((void *)p, sizeof(*p));
297         if (ret != OMX_ErrorNone)
298             return ret;
299         if (amrparam.nPortIndex != p->nPortIndex)
300             return OMX_ErrorBadPortIndex;
301     }
302     else {
303         OMX_AUDIO_PARAM_PORTFORMATTYPE audioparam;
304 
305         memcpy(&audioparam, GetPortAudioParam(), sizeof(audioparam));
306         audioparam.nPortIndex = p->nPortIndex;
307         SetPortAudioParam(&audioparam, true);
308 
309         amrparam.nPortIndex = p->nPortIndex;
310     }
311 
312     amrparam.nChannels       = p->nChannels;
313     amrparam.nBitRate        = p->nBitRate;
314     amrparam.eAMRBandMode    = p->eAMRBandMode;
315     amrparam.eAMRDTXMode     = p->eAMRDTXMode;
316     amrparam.eAMRFrameFormat = p->eAMRFrameFormat;
317 
318     return OMX_ErrorNone;
319 }
320 
GetPortAmrParam(void)321 const OMX_AUDIO_PARAM_AMRTYPE *PortAmr::GetPortAmrParam(void)
322 {
323     return &amrparam;
324 }
325 
326 /* end of PortAmr */
327