Debian Codegraph @

Xpjproject/pjmedia/src/pjmedia-codec/and_aud_mediacodec.cpp

raw
1 /*
2 * Copyright (C)2020 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #include <pjmedia-codec/and_aud_mediacodec.h>
19 #include <pjmedia-codec/amr_sdp_match.h>
20 #include <pjmedia/codec.h>
21 #include <pjmedia/errno.h>
22 #include <pjmedia/endpoint.h>
23 #include <pjmedia/plc.h>
24 #include <pjmedia/port.h>
25 #include <pjmedia/silencedet.h>
26 #include <pj/assert.h>
27 #include <pj/atomic_queue.h>
28 #include <pj/log.h>
29 #include <pj/math.h>
30 #include <pj/pool.h>
31 #include <pj/string.h>
32 #include <pj/os.h>
33
34 /*
35 * Only build this file if PJMEDIA_HAS_ANDROID_MEDIACODEC != 0
36 */
37 #if defined(PJMEDIA_HAS_ANDROID_MEDIACODEC) && \
38 PJMEDIA_HAS_ANDROID_MEDIACODEC != 0
39
40 #include <android/log.h>
41 /* Android AMediaCodec: */
42 #include "media/NdkMediaCodec.h"
43
44 #define THIS_FILE "and_aud_mediacodec.cpp"
45
46 #define AND_MEDIA_KEY_PCM_ENCODING "pcm-encoding"
47 #define AND_MEDIA_KEY_CHANNEL_COUNT "channel-count"
48 #define AND_MEDIA_KEY_SAMPLE_RATE "sample-rate"
49 #define AND_MEDIA_KEY_BITRATE "bitrate"
50 #define AND_MEDIA_KEY_MIME "mime"
51
52 #define BUFFER_MAX_ITEM 16
53 #define API_AT_LEAST(x) __builtin_available(android x, *)
54 typedef struct and_med_buf_info {
55 pj_int32_t index;
56 pj_int32_t size;
57 pj_uint32_t flags;
58 } and_med_buf_info;
59
60 /* Prototypes for Android MediaCodec codecs factory */
61 static pj_status_t and_media_test_alloc(pjmedia_codec_factory *factory,
62 const pjmedia_codec_info *id );
63 static pj_status_t and_media_default_attr(pjmedia_codec_factory *factory,
64 const pjmedia_codec_info *id,
65 pjmedia_codec_param *attr );
66 static pj_status_t and_media_enum_codecs(pjmedia_codec_factory *factory,
67 unsigned *count,
68 pjmedia_codec_info codecs[]);
69 static pj_status_t and_media_alloc_codec(pjmedia_codec_factory *factory,
70 const pjmedia_codec_info *id,
71 pjmedia_codec **p_codec);
72 static pj_status_t and_media_dealloc_codec(pjmedia_codec_factory *factory,
73 pjmedia_codec *codec );
74
75 /* Prototypes for Android MediaCodec codecs implementation. */
76 static pj_status_t and_media_codec_init(pjmedia_codec *codec,
77 pj_pool_t *pool );
78 static pj_status_t and_media_codec_open(pjmedia_codec *codec,
79 pjmedia_codec_param *attr );
80 static pj_status_t and_media_codec_close(pjmedia_codec *codec );
81 static pj_status_t and_media_codec_modify(pjmedia_codec *codec,
82 const pjmedia_codec_param *attr );
83 static pj_status_t and_media_codec_parse(pjmedia_codec *codec,
84 void *pkt,
85 pj_size_t pkt_size,
86 const pj_timestamp *ts,
87 unsigned *frame_cnt,
88 pjmedia_frame frames[]);
89 static pj_status_t and_media_codec_encode(pjmedia_codec *codec,
90 const struct pjmedia_frame *input,
91 unsigned output_buf_len,
92 struct pjmedia_frame *output);
93 static pj_status_t and_media_codec_decode(pjmedia_codec *codec,
94 const struct pjmedia_frame *input,
95 unsigned output_buf_len,
96 struct pjmedia_frame *output);
97 static pj_status_t and_media_codec_recover(pjmedia_codec *codec,
98 unsigned output_buf_len,
99 struct pjmedia_frame *output);
100
101 /* Definition for Android MediaCodec codecs operations. */
102 static pjmedia_codec_op and_media_op =
103 {
104 &and_media_codec_init,
105 &and_media_codec_open,
106 &and_media_codec_close,
107 &and_media_codec_modify,
108 &and_media_codec_parse,
109 &and_media_codec_encode,
110 &and_media_codec_decode,
111 &and_media_codec_recover
112 };
113
114 /* Definition for Android MediaCodec codecs factory operations. */
115 static pjmedia_codec_factory_op and_media_factory_op =
116 {
117 &and_media_test_alloc,
118 &and_media_default_attr,
119 &and_media_enum_codecs,
120 &and_media_alloc_codec,
121 &and_media_dealloc_codec,
122 &pjmedia_codec_and_media_aud_deinit
123 };
124
125 /* Android MediaCodec codecs factory */
126 static struct and_media_factory {
127 pjmedia_codec_factory base;
128 pjmedia_endpt *endpt;
129 pj_pool_t *pool;
130 pj_mutex_t *mutex;
131 } and_media_factory;
132
133 typedef enum and_aud_codec_id {
134 /* AMRNB codec. */
135 AND_AUD_CODEC_AMRNB,
136
137 /* AMRWB codec. */
138 AND_AUD_CODEC_AMRWB
139 } and_aud_codec_id;
140
141 /* Android MediaCodec codecs private data. */
142 typedef struct and_media_private {
143 int codec_idx; /**< Codec index. */
144 void *codec_setting; /**< Specific codec setting. */
145 pj_pool_t *pool; /**< Pool for each instance. */
146 AMediaCodec *enc; /**< Encoder state. */
147 AMediaCodec *dec; /**< Decoder state. */
148
149 pj_uint16_t frame_size; /**< Bitstream frame size. */
150
151 pj_bool_t plc_enabled; /**< PLC enabled flag. */
152 pjmedia_plc *plc; /**< PJMEDIA PLC engine, NULL if
153 codec has internal PLC. */
154
155 pj_bool_t vad_enabled; /**< VAD enabled flag. */
156 pjmedia_silence_det *vad; /**< PJMEDIA VAD engine, NULL if
157 codec has internal VAD. */
158 pj_timestamp last_tx; /**< Timestamp of last transmit.*/
159
160 pj_atomic_queue_t *enc_avail_input_buf; /**< Encoder available input
161 buffer */
162 pj_atomic_queue_t *enc_avail_output_buf; /**< Encoder available ouput
163 buffer */
164 pj_atomic_queue_t *dec_avail_input_buf; /**< Decoder available input
165 buffer */
166 pj_atomic_queue_t *dec_avail_output_buf; /**< Decoder available output
167 buffer */
168 } and_media_private_t;
169
170 /* CUSTOM CALLBACKS */
171
172 /* Parse frames from a packet. Default behaviour of frame parsing is
173 * just separating frames based on calculating frame length derived
174 * from bitrate. Implement this callback when the default behaviour is
175 * unapplicable.
176 */
177 typedef pj_status_t (*parse_cb)(and_media_private_t *codec_data, void *pkt,
178 pj_size_t pkt_size, const pj_timestamp *ts,
179 unsigned *frame_cnt, pjmedia_frame frames[]);
180
181 /* Pack frames into a packet. Default behaviour of packing frames is
182 * just stacking the frames with octet aligned without adding any
183 * payload header. Implement this callback when the default behaviour is
184 * unapplicable.
185 */
186 typedef pj_status_t (*pack_cb)(and_media_private_t *codec_data,
187 unsigned nframes, void *pkt, pj_size_t *pkt_size,
188 pj_size_t max_pkt_size);
189
190 /* This callback is useful for preparing a frame before pass it to decoder.
191 */
192 typedef void (*predecode_cb)(and_media_private_t *codec_data,
193 const pjmedia_frame *rtp_frame,
194 pjmedia_frame *out);
195
196 #if PJMEDIA_HAS_AND_MEDIA_AMRNB || PJMEDIA_HAS_AND_MEDIA_AMRWB
197 /* Custom callback implementations. */
198 static pj_status_t parse_amr(and_media_private_t *codec_data, void *pkt,
199 pj_size_t pkt_size, const pj_timestamp *ts,
200 unsigned *frame_cnt, pjmedia_frame frames[]);
201 static pj_status_t pack_amr(and_media_private_t *codec_data, unsigned nframes,
202 void *pkt, pj_size_t *pkt_size,
203 pj_size_t max_pkt_size);
204 static void predecode_amr(and_media_private_t *codec_data,
205 const pjmedia_frame *input,
206 pjmedia_frame *out);
207 #endif
208
209 #if PJMEDIA_HAS_AND_MEDIA_AMRNB
210
211 static pj_str_t AMRNB_encoder[] = {{(char *)"OMX.google.amrnb.encoder\0", 24},
212 {(char *)"c2.android.amrnb.encoder\0", 24}};
213
214 static pj_str_t AMRNB_decoder[] = {{(char *)"OMX.google.amrnb.decoder\0", 24},
215 {(char *)"c2.android.amrnb.decoder\0", 24}};
216 #endif
217
218 #if PJMEDIA_HAS_AND_MEDIA_AMRWB
219
220 static pj_str_t AMRWB_encoder[] = {{(char *)"OMX.google.amrwb.encoder\0", 24},
221 {(char *)"c2.android.amrwb.encoder\0", 24}};
222
223 static pj_str_t AMRWB_decoder[] = {{(char *)"OMX.google.amrwb.decoder\0", 24},
224 {(char *)"c2.android.amrwb.decoder\0", 24}};
225 #endif
226
227 /**
228 * Called when an input buffer becomes available.
229 * The specified index is the index of the available input buffer.
230 */
231 static void and_med_on_input_avail(AMediaCodec *codec,
232 void *userdata,
233 int32_t index)
234 {
235 and_media_private_t *and_media_data = (and_media_private_t *) userdata;
236 and_med_buf_info buf_info;
237 pj_atomic_queue_t *buf_queue;
238 pj_bzero(&buf_info, sizeof(buf_info));
239
240 if (codec == and_media_data->enc) {
241 buf_queue = and_media_data->enc_avail_input_buf;
242 } else {
243 buf_queue = and_media_data->dec_avail_input_buf;
244 }
245 buf_info.index = index;
246 pj_atomic_queue_put(buf_queue, &buf_info);
247 }
248 /**
249 * Called when an output buffer becomes available.
250 * The specified index is the index of the available output buffer.
251 */
252 static void and_med_on_output_avail(AMediaCodec *codec,
253 void *userdata,
254 int32_t index,
255 AMediaCodecBufferInfo *bufferInfo)
256 {
257 and_media_private_t *and_media_data = (and_media_private_t *) userdata;
258 and_med_buf_info buf_info;
259 pj_atomic_queue_t *buf_queue;
260 pj_bzero(&buf_info, sizeof(buf_info));
261 if (codec == and_media_data->enc) {
262 buf_queue = and_media_data->enc_avail_output_buf;
263 } else {
264 buf_queue = and_media_data->dec_avail_output_buf;
265 }
266 buf_info.index = index;
267 buf_info.size = bufferInfo->size;
268 buf_info.flags = bufferInfo->flags;
269 pj_atomic_queue_put(buf_queue, &buf_info);
270 }
271
272 /**
273 * Called when the output format has changed.
274 * The specified format contains the new output format.
275 */
276 static void and_med_on_format_changed(AMediaCodec *codec,
277 void *userdata,
278 AMediaFormat *format)
279 {
280 and_media_private_t *and_media_data = (and_media_private_t *) userdata;
281 __android_log_print(ANDROID_LOG_INFO, THIS_FILE,
282 "[%s] On format changed\r\n",
283 (codec==and_media_data->enc)?"encoder":"decoder");
284 }
285
286 /**
287 * Called when the MediaCodec encountered an error.
288 */
289 static void and_med_on_error(AMediaCodec *codec,
290 void *userdata,
291 media_status_t error,
292 int32_t actionCode,
293 const char *detail)
294 {
295 and_media_private_t *and_media_data = (and_media_private_t *) userdata;
296 __android_log_print(ANDROID_LOG_INFO, THIS_FILE,
297 "[%s] On Media error : err[%d] code[%d] msg[%s]\r\n",
298 (codec==and_media_data->enc)?"encoder":"decoder", error,
299 actionCode, detail);
300 }
301
302 /* Android MediaCodec codec implementation descriptions. */
303 static struct and_media_codec {
304 int enabled; /* Is this codec enabled? */
305 const char *name; /* Codec name. */
306 const char *mime_type; /* Mime type. */
307 pj_str_t *encoder_name; /* Encoder name. */
308 pj_str_t *decoder_name; /* Decoder name. */
309
310 pj_uint8_t pt; /* Payload type. */
311 and_aud_codec_id codec_id; /* Codec id. */
312 unsigned clock_rate; /* Codec's clock rate. */
313 unsigned channel_count; /* Codec's channel count. */
314 unsigned samples_per_frame; /* Codec's samples count. */
315 unsigned def_bitrate; /* Default bitrate of this codec. */
316 unsigned max_bitrate; /* Maximum bitrate of this codec. */
317 pj_uint8_t frm_per_pkt; /* Default num of frames per packet.*/
318 int has_native_vad; /* Codec has internal VAD? */
319 int has_native_plc; /* Codec has internal PLC? */
320
321 parse_cb parse; /* Callback to parse bitstream. */
322 pack_cb pack; /* Callback to pack bitstream. */
323 predecode_cb predecode; /* Callback to prepare bitstream
324 before passing it to decoder. */
325
326 pjmedia_codec_fmtp dec_fmtp; /* Decoder's fmtp params. */
327 }
328
329 and_media_codec[] =
330 {
331 # if PJMEDIA_HAS_AND_MEDIA_AMRNB
332 {0, "AMR", "audio/3gpp", NULL, NULL,
333 PJMEDIA_RTP_PT_AMR, AND_AUD_CODEC_AMRNB, 8000, 1, 160, 7400, 12200,
334 2, 0, 0, &parse_amr, &pack_amr, &predecode_amr,
335 {1, {{{(char *)"octet-align", 11}, {(char *)"1", 1}}}}
336 },
337 # endif
338
339 # if PJMEDIA_HAS_AND_MEDIA_AMRWB
340 {0, "AMR-WB", "audio/amr-wb", NULL, NULL,
341 PJMEDIA_RTP_PT_AMRWB, AND_AUD_CODEC_AMRWB, 16000, 1, 320, 15850, 23850,
342 2, 0, 0, &parse_amr, &pack_amr, &predecode_amr,
343 {1, {{{(char *)"octet-align", 11}, {(char *)"1", 1}}}}
344 },
345 # endif
346 };
347
348 #if PJMEDIA_HAS_AND_MEDIA_AMRNB || PJMEDIA_HAS_AND_MEDIA_AMRWB
349
350 #include <pjmedia-codec/amr_helper.h>
351
352 typedef struct amr_settings_t {
353 pjmedia_codec_amr_pack_setting enc_setting;
354 pjmedia_codec_amr_pack_setting dec_setting;
355 pj_int8_t enc_mode;
356 } amr_settings_t;
357
358 /* Pack AMR payload */
359 static pj_status_t pack_amr(and_media_private_t *codec_data, unsigned nframes,
360 void *pkt, pj_size_t *pkt_size,
361 pj_size_t max_pkt_size)
362 {
363 enum {MAX_FRAMES_PER_PACKET = PJMEDIA_MAX_FRAME_DURATION_MS / 20};
364
365 pjmedia_frame frames[MAX_FRAMES_PER_PACKET];
366 pj_uint8_t *p; /* Read cursor */
367 pjmedia_codec_amr_pack_setting *setting;
368 unsigned i;
369 pj_status_t status;
370
371 setting = &((amr_settings_t*)codec_data->codec_setting)->enc_setting;
372
373 /* Align pkt buf right */
374 p = (pj_uint8_t*)pkt + max_pkt_size - *pkt_size;
375 pj_memmove(p, pkt, *pkt_size);
376
377 /* Get frames */
378 for (i = 0; i < nframes; ++i) {
379 pjmedia_codec_amr_bit_info *info = (pjmedia_codec_amr_bit_info*)
380 &frames[i].bit_info;
381 pj_bzero(info, sizeof(*info));
382 info->frame_type = (pj_uint8_t)((*p >> 3) & 0x0F);
383 info->good_quality = (pj_uint8_t)((*p >> 2) & 0x01);
384 info->mode = ((amr_settings_t*)codec_data->codec_setting)->enc_mode;
385 info->start_bit = 0;
386 frames[i].buf = p + 1;
387 if (setting->amr_nb) {
388 frames[i].size = (info->frame_type <= 8)?
389 pjmedia_codec_amrnb_framelen[info->frame_type] : 0;
390 } else {
391 frames[i].size = (info->frame_type <= 9)?
392 pjmedia_codec_amrwb_framelen[info->frame_type] : 0;
393 }
394 p += frames[i].size + 1;
395 }
396 /* Pack */
397 *pkt_size = max_pkt_size;
398 status = pjmedia_codec_amr_pack(frames, nframes, setting, pkt, pkt_size);
399
400 return status;
401 }
402
403 /* Parse AMR payload into frames. */
404 static pj_status_t parse_amr(and_media_private_t *codec_data, void *pkt,
405 pj_size_t pkt_size, const pj_timestamp *ts,
406 unsigned *frame_cnt, pjmedia_frame frames[])
407 {
408 amr_settings_t* s = (amr_settings_t*)codec_data->codec_setting;
409 pjmedia_codec_amr_pack_setting *setting;
410 pj_status_t status;
411 pj_uint8_t cmr;
412
413 setting = &s->dec_setting;
414 status = pjmedia_codec_amr_parse(pkt, pkt_size, ts, setting, frames,
415 frame_cnt, &cmr);
416 if (status != PJ_SUCCESS)
417 return status;
418
419 /* Check Change Mode Request. */
420 if (((setting->amr_nb && cmr <= 7) || (!setting->amr_nb && cmr <= 8)) &&
421 s->enc_mode != cmr)
422 {
423 s->enc_mode = cmr;
424 }
425 return PJ_SUCCESS;
426 }
427
428 static void predecode_amr(and_media_private_t *codec_data,
429 const pjmedia_frame *input,
430 pjmedia_frame *out)
431 {
432 pjmedia_codec_amr_bit_info *info;
433 pj_uint8_t *bitstream = (pj_uint8_t *)out->buf;
434 pjmedia_codec_amr_pack_setting *setting;
435
436 out->buf = &bitstream[1];
437 setting = &((amr_settings_t*)codec_data->codec_setting)->dec_setting;
438 pjmedia_codec_amr_predecode(input, setting, out);
439 info = (pjmedia_codec_amr_bit_info*)&out->bit_info;
440 bitstream[0] = (info->frame_type << 3) | (info->good_quality << 2);
441 out->buf = &bitstream[0];
442 ++out->size;
443 }
444
445 #endif /* PJMEDIA_HAS_AND_MEDIA_AMRNB || PJMEDIA_HAS_AND_MEDIA_AMRWB */
446
447 static pj_status_t configure_codec(and_media_private_t *and_media_data,
448 pj_bool_t is_encoder)
449 {
450 media_status_t am_status;
451 AMediaFormat *aud_fmt;
452 int idx = and_media_data->codec_idx;
453 AMediaCodec *codec = (is_encoder?and_media_data->enc:and_media_data->dec);
454
455 aud_fmt = AMediaFormat_new();
456 if (!aud_fmt) {
457 return PJ_ENOMEM;
458 }
459 AMediaFormat_setString(aud_fmt, AND_MEDIA_KEY_MIME,
460 and_media_codec[idx].mime_type);
461 AMediaFormat_setInt32(aud_fmt, AND_MEDIA_KEY_PCM_ENCODING, 2);
462 AMediaFormat_setInt32(aud_fmt, AND_MEDIA_KEY_CHANNEL_COUNT,
463 and_media_codec[idx].channel_count);
464 AMediaFormat_setInt32(aud_fmt, AND_MEDIA_KEY_SAMPLE_RATE,
465 and_media_codec[idx].clock_rate);
466 AMediaFormat_setInt32(aud_fmt, AND_MEDIA_KEY_BITRATE,
467 and_media_codec[idx].def_bitrate);
468
469 /* Configure and start encoder. */
470 am_status = AMediaCodec_configure(codec, aud_fmt, NULL, NULL, is_encoder);
471 AMediaFormat_delete(aud_fmt);
472 if (am_status != AMEDIA_OK) {
473 PJ_LOG(4, (THIS_FILE, "%s [0x%p] configure failed, status=%d",
474 is_encoder?"Encoder":"Decoder", codec, am_status));
475 return PJMEDIA_CODEC_EFAILED;
476 }
477 am_status = AMediaCodec_start(codec);
478 if (am_status != AMEDIA_OK) {
479 PJ_LOG(4, (THIS_FILE, "%s [0x%p] start failed, status=%d",
480 is_encoder?"Encoder":"Decoder", codec, am_status));
481 return PJMEDIA_CODEC_EFAILED;
482 }
483 PJ_LOG(4, (THIS_FILE, "%s [0x%p] started", is_encoder?"Encoder":"Decoder",
484 codec));
485 return PJ_SUCCESS;
486 }
487
488 /*
489 * Initialize and register Android MediaCodec codec factory to pjmedia endpoint.
490 */
491 PJ_DEF(pj_status_t) pjmedia_codec_and_media_aud_init( pjmedia_endpt *endpt )
492 {
493 pjmedia_codec_mgr *codec_mgr;
494 pj_str_t codec_name;
495 pj_status_t status;
496 int api_level = android_get_device_api_level();
497
498 if (api_level < 28) {
499 PJ_LOG(4,(THIS_FILE, "Minimum API level 28,"
500 "Android MediaCodec cannot work with API level %d",
501 api_level));
502
503 return PJ_SUCCESS;
504 }
505
506 if (and_media_factory.pool != NULL) {
507 /* Already initialized. */
508 return PJ_SUCCESS;
509 }
510
511 PJ_LOG(4, (THIS_FILE, "Initing codec"));
512
513 /* Create Android MediaCodec codec factory. */
514 and_media_factory.base.op = &and_media_factory_op;
515 and_media_factory.base.factory_data = NULL;
516 and_media_factory.endpt = endpt;
517
518 and_media_factory.pool = pjmedia_endpt_create_pool(endpt,
519 "Android MediaCodec codecs",
520 4000, 4000);
521 if (!and_media_factory.pool)
522 return PJ_ENOMEM;
523
524 /* Create mutex. */
525 status = pj_mutex_create_simple(and_media_factory.pool,
526 "Android MediaCodec codecs",
527 &and_media_factory.mutex);
528 if (status != PJ_SUCCESS)
529 goto on_error;
530
531 /* Get the codec manager. */
532 codec_mgr = pjmedia_endpt_get_codec_mgr(endpt);
533 if (!codec_mgr) {
534 status = PJ_EINVALIDOP;
535 goto on_error;
536 }
537
538 #if PJMEDIA_HAS_AND_MEDIA_AMRNB
539 PJ_LOG(4, (THIS_FILE, "Registering AMRNB codec"));
540
541 pj_cstr(&codec_name, "AMR");
542 status = pjmedia_sdp_neg_register_fmt_match_cb(
543 &codec_name,
544 &pjmedia_codec_amr_match_sdp);
545 if (status != PJ_SUCCESS)
546 goto on_error;
547 #endif
548
549 #if PJMEDIA_HAS_AND_MEDIA_AMRWB
550 PJ_LOG(4, (THIS_FILE, "Registering AMRWB codec"));
551
552 pj_cstr(&codec_name, "AMR-WB");
553 status = pjmedia_sdp_neg_register_fmt_match_cb(
554 &codec_name,
555 &pjmedia_codec_amr_match_sdp);
556 if (status != PJ_SUCCESS)
557 goto on_error;
558 #endif
559
560 /* Suppress compile warning */
561 PJ_UNUSED_ARG(codec_name);
562
563 /* Register codec factory to endpoint. */
564 status = pjmedia_codec_mgr_register_factory(codec_mgr,
565 &and_media_factory.base);
566 if (status != PJ_SUCCESS)
567 goto on_error;
568
569 /* Done. */
570 return PJ_SUCCESS;
571
572 on_error:
573 pj_pool_release(and_media_factory.pool);
574 and_media_factory.pool = NULL;
575 return status;
576 }
577
578 /*
579 * Unregister Android MediaCodec codecs factory from pjmedia endpoint.
580 */
581 PJ_DEF(pj_status_t) pjmedia_codec_and_media_aud_deinit(void)
582 {
583 pjmedia_codec_mgr *codec_mgr;
584 pj_status_t status;
585
586 if (and_media_factory.pool == NULL) {
587 /* Already deinitialized */
588 return PJ_SUCCESS;
589 }
590
591 pj_mutex_lock(and_media_factory.mutex);
592
593 /* Get the codec manager. */
594 codec_mgr = pjmedia_endpt_get_codec_mgr(and_media_factory.endpt);
595 if (!codec_mgr) {
596 pj_pool_release(and_media_factory.pool);
597 and_media_factory.pool = NULL;
598 pj_mutex_unlock(and_media_factory.mutex);
599 return PJ_EINVALIDOP;
600 }
601
602 /* Unregister Android MediaCodec codecs factory. */
603 status = pjmedia_codec_mgr_unregister_factory(codec_mgr,
604 &and_media_factory.base);
605
606 /* Destroy mutex. */
607 pj_mutex_unlock(and_media_factory.mutex);
608 pj_mutex_destroy(and_media_factory.mutex);
609 and_media_factory.mutex = NULL;
610
611 /* Destroy pool. */
612 pj_pool_release(and_media_factory.pool);
613 and_media_factory.pool = NULL;
614
615 return status;
616 }
617
618 /*
619 * Check if factory can allocate the specified codec.
620 */
621 static pj_status_t and_media_test_alloc(pjmedia_codec_factory *factory,
622 const pjmedia_codec_info *info )
623 {
624 unsigned i;
625
626 PJ_UNUSED_ARG(factory);
627
628 /* Type MUST be audio. */
629 if (info->type != PJMEDIA_TYPE_AUDIO)
630 return PJMEDIA_CODEC_EUNSUP;
631
632 for (i = 0; i < PJ_ARRAY_SIZE(and_media_codec); ++i) {
633 pj_str_t name = pj_str((char*)and_media_codec[i].name);
634 if ((pj_stricmp(&info->encoding_name, &name) == 0) &&
635 (info->clock_rate == (unsigned)and_media_codec[i].clock_rate) &&
636 (info->channel_cnt == (unsigned)and_media_codec[i].channel_count) &&
637 (and_media_codec[i].enabled))
638 {
639 return PJ_SUCCESS;
640 }
641 }
642
643 /* Unsupported, or mode is disabled. */
644 return PJMEDIA_CODEC_EUNSUP;
645 }
646
647 /*
648 * Generate default attribute.
649 */
650 static pj_status_t and_media_default_attr (pjmedia_codec_factory *factory,
651 const pjmedia_codec_info *id,
652 pjmedia_codec_param *attr)
653 {
654 unsigned i;
655
656 PJ_ASSERT_RETURN(factory==&and_media_factory.base, PJ_EINVAL);
657
658 pj_bzero(attr, sizeof(pjmedia_codec_param));
659
660 for (i = 0; i < PJ_ARRAY_SIZE(and_media_codec); ++i) {
661 pj_str_t name = pj_str((char*)and_media_codec[i].name);
662 if ((and_media_codec[i].enabled) &&
663 (pj_stricmp(&id->encoding_name, &name) == 0) &&
664 (id->clock_rate == (unsigned)and_media_codec[i].clock_rate) &&
665 (id->channel_cnt == (unsigned)and_media_codec[i].channel_count) &&
666 (id->pt == (unsigned)and_media_codec[i].pt))
667 {
668 attr->info.pt = (pj_uint8_t)id->pt;
669 attr->info.channel_cnt = and_media_codec[i].channel_count;
670 attr->info.clock_rate = and_media_codec[i].clock_rate;
671 attr->info.avg_bps = and_media_codec[i].def_bitrate;
672 attr->info.max_bps = and_media_codec[i].max_bitrate;
673 attr->info.pcm_bits_per_sample = 16;
674 attr->info.frm_ptime = (pj_uint16_t)
675 (and_media_codec[i].samples_per_frame * 1000 /
676 and_media_codec[i].channel_count /
677 and_media_codec[i].clock_rate);
678 attr->setting.frm_per_pkt = and_media_codec[i].frm_per_pkt;
679
680 /* Default flags. */
681 attr->setting.plc = 1;
682 attr->setting.penh= 0;
683 attr->setting.vad = 1;
684 attr->setting.cng = attr->setting.vad;
685 attr->setting.dec_fmtp = and_media_codec[i].dec_fmtp;
686
687 return PJ_SUCCESS;
688 }
689 }
690
691 return PJMEDIA_CODEC_EUNSUP;
692 }
693
694 static pj_bool_t codec_exists(const pj_str_t *codec_name)
695 {
696 AMediaCodec *codec;
697 char *codec_txt;
698
699 codec_txt = codec_name->ptr;
700
701 codec = AMediaCodec_createCodecByName(codec_txt);
702 if (!codec) {
703 PJ_LOG(4, (THIS_FILE, "Failed creating codec : %.*s",
704 (int)codec_name->slen, codec_name->ptr));
705 return PJ_FALSE;
706 }
707 AMediaCodec_delete(codec);
708
709 return PJ_TRUE;
710 }
711
712 /*
713 * Enum codecs supported by this factory.
714 */
715 static pj_status_t and_media_enum_codecs(pjmedia_codec_factory *factory,
716 unsigned *count,
717 pjmedia_codec_info codecs[])
718 {
719 unsigned max;
720 unsigned i;
721
722 PJ_UNUSED_ARG(factory);
723 PJ_ASSERT_RETURN(codecs && *count > 0, PJ_EINVAL);
724
725 max = *count;
726
727 for (i = 0, *count = 0; i < PJ_ARRAY_SIZE(and_media_codec) &&
728 *count < max; ++i)
729 {
730 unsigned enc_idx, dec_idx;
731 pj_str_t *enc_name = NULL;
732 unsigned num_enc = 0;
733 pj_str_t *dec_name = NULL;
734 unsigned num_dec = 0;
735
736 switch (and_media_codec[i].codec_id) {
737
738 case AND_AUD_CODEC_AMRNB:
739 #if PJMEDIA_HAS_AND_MEDIA_AMRNB
740 enc_name = &AMRNB_encoder[0];
741 dec_name = &AMRNB_decoder[0];
742 num_enc = PJ_ARRAY_SIZE(AMRNB_encoder);
743 num_dec = PJ_ARRAY_SIZE(AMRNB_decoder);
744 #endif
745 break;
746 case AND_AUD_CODEC_AMRWB:
747 #if PJMEDIA_HAS_AND_MEDIA_AMRWB
748 enc_name = &AMRWB_encoder[0];
749 dec_name = &AMRWB_decoder[0];
750 num_enc = PJ_ARRAY_SIZE(AMRWB_encoder);
751 num_dec = PJ_ARRAY_SIZE(AMRWB_decoder);
752 #endif
753
754 break;
755 default:
756 continue;
757 };
758 if (!enc_name || !dec_name) {
759 continue;
760 }
761
762 for (enc_idx = 0; enc_idx < num_enc ;++enc_idx, ++enc_name) {
763 if (codec_exists(enc_name)) {
764 break;
765 }
766 }
767 if (enc_idx == num_enc)
768 continue;
769
770 for (dec_idx = 0; dec_idx < num_dec ;++dec_idx, ++dec_name) {
771 if (codec_exists(dec_name)) {
772 break;
773 }
774 }
775 if (dec_idx == num_dec)
776 continue;
777
778 and_media_codec[i].encoder_name = enc_name;
779 and_media_codec[i].decoder_name = dec_name;
780 pj_bzero(&codecs[*count], sizeof(pjmedia_codec_info));
781 codecs[*count].encoding_name = pj_str((char*)and_media_codec[i].name);
782 codecs[*count].pt = and_media_codec[i].pt;
783 codecs[*count].type = PJMEDIA_TYPE_AUDIO;
784 codecs[*count].clock_rate = and_media_codec[i].clock_rate;
785 codecs[*count].channel_cnt = and_media_codec[i].channel_count;
786 and_media_codec[i].enabled = PJ_TRUE;
787 PJ_LOG(4, (THIS_FILE, "Found encoder [%d]: %.*s and decoder: %.*s ",
788 *count, (int)enc_name->slen, enc_name->ptr,
789 (int)dec_name->slen, dec_name->ptr));
790 ++*count;
791 }
792
793 return PJ_SUCCESS;
794 }
795
796 static void create_codec(and_media_private_t *and_media_data)
797 {
798 char const *enc_name =
799 and_media_codec[and_media_data->codec_idx].encoder_name->ptr;
800 char const *dec_name =
801 and_media_codec[and_media_data->codec_idx].decoder_name->ptr;
802
803 if (!and_media_data->enc) {
804 and_media_data->enc = AMediaCodec_createCodecByName(enc_name);
805 if (!and_media_data->enc) {
806 PJ_LOG(4, (THIS_FILE, "Failed creating encoder: %s", enc_name));
807 }
808 pj_atomic_queue_create(and_media_data->pool,
809 BUFFER_MAX_ITEM,
810 sizeof(and_med_buf_info),
811 "enc_input_buf",
812 &and_media_data->enc_avail_input_buf);
813 pj_atomic_queue_create(and_media_data->pool,
814 BUFFER_MAX_ITEM,
815 sizeof(and_med_buf_info),
816 "enc_output_buf",
817 &and_media_data->enc_avail_output_buf);
818
819 PJ_LOG(4, (THIS_FILE, "Done creating encoder: %s [0x%p]", enc_name,
820 and_media_data->enc));
821 }
822
823 if (!and_media_data->dec) {
824 and_media_data->dec = AMediaCodec_createCodecByName(dec_name);
825 if (!and_media_data->dec) {
826 PJ_LOG(4, (THIS_FILE, "Failed creating decoder: %s", dec_name));
827 }
828 pj_atomic_queue_create(and_media_data->pool,
829 BUFFER_MAX_ITEM,
830 sizeof(and_med_buf_info),
831 "dec_input_buf",
832 &and_media_data->dec_avail_input_buf);
833 pj_atomic_queue_create(and_media_data->pool,
834 BUFFER_MAX_ITEM,
835 sizeof(and_med_buf_info),
836 "dec_output_buf",
837 &and_media_data->dec_avail_output_buf);
838
839 PJ_LOG(4, (THIS_FILE, "Done creating decoder: %s [0x%p]", dec_name,
840 and_media_data->dec));
841 }
842 }
843
844 /*
845 * Allocate a new codec instance.
846 */
847 static pj_status_t and_media_alloc_codec(pjmedia_codec_factory *factory,
848 const pjmedia_codec_info *id,
849 pjmedia_codec **p_codec)
850 {
851 and_media_private_t *codec_data;
852 pjmedia_codec *codec;
853 int idx;
854 pj_pool_t *pool;
855 unsigned i;
856
857 PJ_ASSERT_RETURN(factory && id && p_codec, PJ_EINVAL);
858 PJ_ASSERT_RETURN(factory == &and_media_factory.base, PJ_EINVAL);
859
860 pj_mutex_lock(and_media_factory.mutex);
861
862 /* Find codec's index */
863 idx = -1;
864 for (i = 0; i < PJ_ARRAY_SIZE(and_media_codec); ++i) {
865 pj_str_t name = pj_str((char*)and_media_codec[i].name);
866 if ((pj_stricmp(&id->encoding_name, &name) == 0) &&
867 (id->clock_rate == (unsigned)and_media_codec[i].clock_rate) &&
868 (id->channel_cnt == (unsigned)and_media_codec[i].channel_count) &&
869 (and_media_codec[i].enabled))
870 {
871 idx = i;
872 break;
873 }
874 }
875 if (idx == -1) {
876 *p_codec = NULL;
877 pj_mutex_unlock(and_media_factory.mutex);
878 return PJMEDIA_CODEC_EFAILED;
879 }
880
881 /* Create pool for codec instance */
882 pool = pjmedia_endpt_create_pool(and_media_factory.endpt, "andmedaud%p",
883 512, 512);
884 codec = PJ_POOL_ZALLOC_T(pool, pjmedia_codec);
885 PJ_ASSERT_RETURN(codec != NULL, PJ_ENOMEM);
886 codec->op = &and_media_op;
887 codec->factory = factory;
888 codec->codec_data = PJ_POOL_ZALLOC_T(pool, and_media_private_t);
889 codec_data = (and_media_private_t*) codec->codec_data;
890
891 /* Create PLC if codec has no internal PLC */
892 if (!and_media_codec[idx].has_native_plc) {
893 pj_status_t status;
894 status = pjmedia_plc_create(pool, and_media_codec[idx].clock_rate,
895 and_media_codec[idx].samples_per_frame, 0,
896 &codec_data->plc);
897 if (status != PJ_SUCCESS) {
898 goto on_error;
899 }
900 }
901
902 /* Create silence detector if codec has no internal VAD */
903 if (!and_media_codec[idx].has_native_vad) {
904 pj_status_t status;
905 status = pjmedia_silence_det_create(pool,
906 and_media_codec[idx].clock_rate,
907 and_media_codec[idx].samples_per_frame,
908 &codec_data->vad);
909 if (status != PJ_SUCCESS) {
910 goto on_error;
911 }
912 }
913
914 codec_data->pool = pool;
915 codec_data->codec_idx = idx;
916
917 create_codec(codec_data);
918 if (!codec_data->enc || !codec_data->dec) {
919 goto on_error;
920 }
921 pj_mutex_unlock(and_media_factory.mutex);
922
923 *p_codec = codec;
924 return PJ_SUCCESS;
925
926 on_error:
927 pj_mutex_unlock(and_media_factory.mutex);
928 and_media_dealloc_codec(factory, codec);
929 return PJMEDIA_CODEC_EFAILED;
930 }
931
932 /*
933 * Free codec.
934 */
935 static pj_status_t and_media_dealloc_codec(pjmedia_codec_factory *factory,
936 pjmedia_codec *codec )
937 {
938 and_media_private_t *codec_data;
939
940 PJ_ASSERT_RETURN(factory && codec, PJ_EINVAL);
941 PJ_ASSERT_RETURN(factory == &and_media_factory.base, PJ_EINVAL);
942
943 /* Close codec, if it's not closed. */
944 codec_data = (and_media_private_t*) codec->codec_data;
945 if (codec_data->enc) {
946 AMediaCodec_stop(codec_data->enc);
947 AMediaCodec_delete(codec_data->enc);
948 codec_data->enc = NULL;
949 pj_atomic_queue_destroy(codec_data->enc_avail_input_buf);
950 codec_data->enc_avail_input_buf = NULL;
951 pj_atomic_queue_destroy(codec_data->enc_avail_output_buf);
952 codec_data->enc_avail_output_buf = NULL;
953 }
954
955 if (codec_data->dec) {
956 AMediaCodec_stop(codec_data->dec);
957 AMediaCodec_delete(codec_data->dec);
958 codec_data->dec = NULL;
959 pj_atomic_queue_destroy(codec_data->dec_avail_input_buf);
960 codec_data->dec_avail_input_buf = NULL;
961 pj_atomic_queue_destroy(codec_data->dec_avail_output_buf);
962 codec_data->dec_avail_output_buf = NULL;
963 }
964 pj_pool_release(codec_data->pool);
965
966 return PJ_SUCCESS;
967 }
968
969 /*
970 * Init codec.
971 */
972 static pj_status_t and_media_codec_init(pjmedia_codec *codec,
973 pj_pool_t *pool )
974 {
975 PJ_UNUSED_ARG(codec);
976 PJ_UNUSED_ARG(pool);
977 return PJ_SUCCESS;
978 }
979
980 /*
981 * Open codec.
982 */
983 static pj_status_t and_media_codec_open(pjmedia_codec *codec,
984 pjmedia_codec_param *attr)
985 {
986 and_media_private_t *codec_data = (and_media_private_t*) codec->codec_data;
987 struct and_media_codec *and_media_data =
988 &and_media_codec[codec_data->codec_idx];
989 pj_status_t status;
990
991 PJ_ASSERT_RETURN(codec && attr, PJ_EINVAL);
992 PJ_ASSERT_RETURN(codec_data != NULL, PJ_EINVALIDOP);
993
994 PJ_LOG(5,(THIS_FILE, "Opening codec.."));
995
996 codec_data->vad_enabled = (attr->setting.vad != 0);
997 codec_data->plc_enabled = (attr->setting.plc != 0);
998 and_media_data->clock_rate = attr->info.clock_rate;
999
1000 #if PJMEDIA_HAS_AND_MEDIA_AMRNB
1001 if (and_media_data->codec_id == AND_AUD_CODEC_AMRNB ||
1002 and_media_data->codec_id == AND_AUD_CODEC_AMRWB)
1003 {
1004 amr_settings_t *s;
1005 pj_uint8_t octet_align = 0;
1006 pj_int8_t enc_mode;
1007 unsigned i;
1008
1009 enc_mode = pjmedia_codec_amr_get_mode(attr->info.avg_bps);
1010
1011 pj_assert(enc_mode >= 0 && enc_mode <= 8);
1012
1013 /* Check AMR specific attributes */
1014 for (i = 0; i < attr->setting.dec_fmtp.cnt; ++i) {
1015 /* octet-align, one of the parameters that must have same value
1016 * in offer & answer (RFC 4867 Section 8.3.1). Just check fmtp
1017 * in the decoder side, since it's value is guaranteed to fulfil
1018 * above requirement (by SDP negotiator).
1019 */
1020 const pj_str_t STR_FMTP_OCTET_ALIGN = {(char *)"octet-align", 11};
1021
1022 if (pj_stricmp(&attr->setting.dec_fmtp.param[i].name,
1023 &STR_FMTP_OCTET_ALIGN) == 0)
1024 {
1025 octet_align=(pj_uint8_t)
1026 pj_strtoul(&attr->setting.dec_fmtp.param[i].val);
1027 break;
1028 }
1029 }
1030 for (i = 0; i < attr->setting.enc_fmtp.cnt; ++i) {
1031 /* mode-set, encoding mode is chosen based on local default mode
1032 * setting:
1033 * - if local default mode is included in the mode-set, use it
1034 * - otherwise, find the closest mode to local default mode;
1035 * if there are two closest modes, prefer to use the higher
1036 * one, e.g: local default mode is 4, the mode-set param
1037 * contains '2,3,5,6', then 5 will be chosen.
1038 */
1039 const pj_str_t STR_FMTP_MODE_SET = {(char *)"mode-set", 8};
1040
1041 if (pj_stricmp(&attr->setting.enc_fmtp.param[i].name,
1042 &STR_FMTP_MODE_SET) == 0)
1043 {
1044 const char *p;
1045 pj_size_t l;
1046 pj_int8_t diff = 99;
1047
1048 p = pj_strbuf(&attr->setting.enc_fmtp.param[i].val);
1049 l = pj_strlen(&attr->setting.enc_fmtp.param[i].val);
1050
1051 while (l--) {
1052 if ((and_media_data->codec_id == AND_AUD_CODEC_AMRNB &&
1053 *p>='0' && *p<='7') ||
1054 (and_media_data->codec_id == AND_AUD_CODEC_AMRWB &&
1055 *p>='0' && *p<='8'))
1056 {
1057 pj_int8_t tmp = (pj_int8_t)(*p - '0' - enc_mode);
1058
1059 if (PJ_ABS(diff) > PJ_ABS(tmp) ||
1060 (PJ_ABS(diff) == PJ_ABS(tmp) && tmp > diff))
1061 {
1062 diff = tmp;
1063 if (diff == 0) break;
1064 }
1065 }
1066 ++p;
1067 }
1068 if (diff == 99) {
1069 goto on_error;
1070 }
1071
1072 enc_mode = (pj_int8_t)(enc_mode + diff);
1073
1074 break;
1075 }
1076 }
1077 /* Initialize AMR specific settings */
1078 s = PJ_POOL_ZALLOC_T(codec_data->pool, amr_settings_t);
1079 codec_data->codec_setting = s;
1080
1081 s->enc_setting.amr_nb = (pj_uint8_t)
1082 (and_media_data->codec_id == AND_AUD_CODEC_AMRNB);
1083 s->enc_setting.octet_aligned = octet_align;
1084 s->enc_setting.reorder = 0;
1085 s->enc_setting.cmr = 15;
1086 s->dec_setting.amr_nb = (pj_uint8_t)
1087 (and_media_data->codec_id == AND_AUD_CODEC_AMRNB);
1088 s->dec_setting.octet_aligned = octet_align;
1089 s->dec_setting.reorder = 0;
1090 /* Apply encoder mode/bitrate */
1091 s->enc_mode = enc_mode;
1092
1093 PJ_LOG(4, (THIS_FILE, "Encoder setting octet_aligned=%d reorder=%d"
1094 " cmr=%d enc_mode=%d",
1095 s->enc_setting.octet_aligned, s->enc_setting.reorder,
1096 s->enc_setting.cmr, enc_mode));
1097 PJ_LOG(4, (THIS_FILE, "Decoder setting octet_aligned=%d reorder=%d",
1098 s->dec_setting.octet_aligned, s->dec_setting.reorder));
1099 }
1100 #endif
1101
1102 if (API_AT_LEAST(28)) {
1103 AMediaCodecOnAsyncNotifyCallback async_cb = {&and_med_on_input_avail,
1104 &and_med_on_output_avail,
1105 &and_med_on_format_changed,
1106 &and_med_on_error};
1107
1108 AMediaCodec_setAsyncNotifyCallback(codec_data->enc, async_cb,
1109 codec_data);
1110 AMediaCodec_setAsyncNotifyCallback(codec_data->dec, async_cb,
1111 codec_data);
1112 }
1113
1114 status = configure_codec(codec_data, PJ_TRUE);
1115 if (status != PJ_SUCCESS) {
1116 goto on_error;
1117 }
1118 status = configure_codec(codec_data, PJ_FALSE);
1119 if (status != PJ_SUCCESS) {
1120 goto on_error;
1121 }
1122
1123 return PJ_SUCCESS;
1124
1125 on_error:
1126 return PJMEDIA_CODEC_EFAILED;
1127 }
1128
1129 /*
1130 * Close codec.
1131 */
1132 static pj_status_t and_media_codec_close(pjmedia_codec *codec)
1133 {
1134 PJ_UNUSED_ARG(codec);
1135
1136 return PJ_SUCCESS;
1137 }
1138
1139 /*
1140 * Modify codec settings.
1141 */
1142 static pj_status_t and_media_codec_modify(pjmedia_codec *codec,
1143 const pjmedia_codec_param *attr)
1144 {
1145 and_media_private_t *codec_data = (and_media_private_t*) codec->codec_data;
1146
1147 codec_data->vad_enabled = (attr->setting.vad != 0);
1148 codec_data->plc_enabled = (attr->setting.plc != 0);
1149
1150 return PJ_SUCCESS;
1151 }
1152
1153 /*
1154 * Get frames in the packet.
1155 */
1156 static pj_status_t and_media_codec_parse(pjmedia_codec *codec,
1157 void *pkt,
1158 pj_size_t pkt_size,
1159 const pj_timestamp *ts,
1160 unsigned *frame_cnt,
1161 pjmedia_frame frames[])
1162 {
1163 and_media_private_t *codec_data = (and_media_private_t*) codec->codec_data;
1164 struct and_media_codec *and_media_data =
1165 &and_media_codec[codec_data->codec_idx];
1166 unsigned count = 0;
1167
1168 PJ_ASSERT_RETURN(frame_cnt, PJ_EINVAL);
1169
1170 if (and_media_data->parse != NULL) {
1171 return and_media_data->parse(codec_data, pkt, pkt_size, ts, frame_cnt,
1172 frames);
1173 }
1174
1175 while (pkt_size >= codec_data->frame_size && count < *frame_cnt) {
1176 frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO;
1177 frames[count].buf = pkt;
1178 frames[count].size = codec_data->frame_size;
1179 frames[count].timestamp.u64 = ts->u64 +
1180 count*and_media_data->samples_per_frame;
1181 pkt = ((char*)pkt) + codec_data->frame_size;
1182 pkt_size -= codec_data->frame_size;
1183 ++count;
1184 }
1185
1186 if (pkt_size && count < *frame_cnt) {
1187 frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO;
1188 frames[count].buf = pkt;
1189 frames[count].size = pkt_size;
1190 frames[count].timestamp.u64 = ts->u64 +
1191 count*and_media_data->samples_per_frame;
1192 ++count;
1193 }
1194
1195 *frame_cnt = count;
1196 return PJ_SUCCESS;
1197 }
1198
1199 /*
1200 * Encode frames.
1201 */
1202 static pj_status_t and_media_codec_encode(pjmedia_codec *codec,
1203 const struct pjmedia_frame *input,
1204 unsigned output_buf_len,
1205 struct pjmedia_frame *output)
1206 {
1207 and_media_private_t *codec_data = (and_media_private_t*) codec->codec_data;
1208 struct and_media_codec *and_media_data =
1209 &and_media_codec[codec_data->codec_idx];
1210 unsigned samples_per_frame;
1211 unsigned nsamples;
1212 unsigned nframes;
1213 pj_size_t tx = 0;
1214 pj_int16_t *pcm_in = (pj_int16_t*)input->buf;
1215 pj_uint8_t *bits_out = (pj_uint8_t*) output->buf;
1216
1217 /* Invoke external VAD if codec has no internal VAD */
1218 if (codec_data->vad && codec_data->vad_enabled) {
1219 pj_bool_t is_silence;
1220 pj_int32_t silence_duration;
1221
1222 silence_duration = pj_timestamp_diff32(&codec_data->last_tx,
1223 &input->timestamp);
1224
1225 is_silence = pjmedia_silence_det_detect(codec_data->vad,
1226 (const pj_int16_t*) input->buf,
1227 (input->size >> 1),
1228 NULL);
1229 if (is_silence &&
1230 (PJMEDIA_CODEC_MAX_SILENCE_PERIOD == -1 ||
1231 silence_duration < (PJMEDIA_CODEC_MAX_SILENCE_PERIOD *
1232 (int)and_media_data->clock_rate / 1000)))
1233 {
1234 output->type = PJMEDIA_FRAME_TYPE_NONE;
1235 output->buf = NULL;
1236 output->size = 0;
1237 output->timestamp = input->timestamp;
1238 return PJ_SUCCESS;
1239 } else {
1240 codec_data->last_tx = input->timestamp;
1241 }
1242 }
1243 nsamples = input->size >> 1;
1244 samples_per_frame = and_media_data->samples_per_frame;
1245 nframes = nsamples / samples_per_frame;
1246
1247 PJ_ASSERT_RETURN(nsamples % samples_per_frame == 0,
1248 PJMEDIA_CODEC_EPCMFRMINLEN);
1249
1250 /* Encode the frames */
1251 while (nsamples >= samples_per_frame) {
1252 pj_size_t output_size;
1253 pj_uint8_t *output_buf;
1254 media_status_t am_status;
1255 unsigned input_size;
1256 pj_uint8_t *input_buf;
1257 and_med_buf_info buf_info;
1258 pj_atomic_queue_t *queue = codec_data->enc_avail_input_buf;
1259
1260 pj_bzero(&buf_info, sizeof(buf_info));
1261 if (pj_atomic_queue_get(queue, &buf_info) != PJ_SUCCESS ||
1262 buf_info.index < 0)
1263 {
1264 PJ_LOG(4,(THIS_FILE, "Encoder failed to get input Buffer[%d]",
1265 buf_info.index));
1266 goto on_return;
1267 }
1268 input_size = samples_per_frame << 1;
1269 input_buf = AMediaCodec_getInputBuffer(codec_data->enc,
1270 buf_info.index, &output_size);
1271 if (input_buf && output_size >= input_size) {
1272 pj_memcpy(input_buf, pcm_in, input_size);
1273
1274 am_status = AMediaCodec_queueInputBuffer(codec_data->enc,
1275 buf_info.index, 0, input_size, 0, 0);
1276 if (am_status != AMEDIA_OK) {
1277 PJ_LOG(4, (THIS_FILE, "Encoder queueInputBuffer return %d",
1278 am_status));
1279 goto on_return;
1280 }
1281 } else {
1282 if (!input_buf) {
1283 PJ_LOG(4,(THIS_FILE, "Encoder getInputBuffer "
1284 "returns no input buff"));
1285 } else {
1286 PJ_LOG(4,(THIS_FILE, "Encoder getInputBuffer "
1287 "size: %lu, expecting %d.",
1288 (unsigned long)output_size,
1289 input_size));
1290 }
1291 goto on_return;
1292 }
1293
1294 pj_bzero(&buf_info, sizeof(buf_info));
1295 queue = codec_data->enc_avail_output_buf;
1296 if (pj_atomic_queue_get(queue, &buf_info) != PJ_SUCCESS ||
1297 buf_info.index < 0)
1298 {
1299 PJ_LOG(4, (THIS_FILE, "Encoder failed to get output Buffer[%d]",
1300 buf_info.index));
1301 goto on_return;
1302 }
1303
1304 output_buf = AMediaCodec_getOutputBuffer(codec_data->enc,
1305 buf_info.index,
1306 &output_size);
1307 if (!output_buf) {
1308 PJ_LOG(4, (THIS_FILE, "Encoder failed getting output buffer, "
1309 "index=%d buffer size=%d, flags %d",
1310 buf_info.index, buf_info.size, buf_info.flags));
1311 goto on_return;
1312 }
1313
1314 pj_memcpy(bits_out, output_buf, buf_info.size);
1315 AMediaCodec_releaseOutputBuffer(codec_data->enc,
1316 buf_info.index,
1317 0);
1318 bits_out += buf_info.size;
1319 tx += buf_info.size;
1320 pcm_in += samples_per_frame;
1321 nsamples -= samples_per_frame;
1322 }
1323 if (and_media_data->pack != NULL) {
1324 and_media_data->pack(codec_data, nframes, output->buf, &tx,
1325 output_buf_len);
1326 }
1327 /* Check if we don't need to transmit the frame (DTX) */
1328 if (tx == 0) {
1329 output->buf = NULL;
1330 output->size = 0;
1331 output->timestamp.u64 = input->timestamp.u64;
1332 output->type = PJMEDIA_FRAME_TYPE_NONE;
1333 return PJ_SUCCESS;
1334 }
1335 output->size = tx;
1336 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
1337 output->timestamp = input->timestamp;
1338
1339 return PJ_SUCCESS;
1340
1341 on_return:
1342 output->size = 0;
1343 output->buf = NULL;
1344 output->type = PJMEDIA_FRAME_TYPE_NONE;
1345 output->timestamp.u64 = input->timestamp.u64;
1346 return PJ_SUCCESS;
1347 }
1348
1349 /*
1350 * Decode frame.
1351 */
1352 static pj_status_t and_media_codec_decode(pjmedia_codec *codec,
1353 const struct pjmedia_frame *input,
1354 unsigned output_buf_len,
1355 struct pjmedia_frame *output)
1356 {
1357 and_media_private_t *codec_data = (and_media_private_t*) codec->codec_data;
1358 struct and_media_codec *and_media_data =
1359 &and_media_codec[codec_data->codec_idx];
1360 unsigned samples_per_frame;
1361 and_med_buf_info buf_info;
1362 pj_uint8_t *input_buf;
1363 pj_size_t input_size;
1364 pj_size_t output_size;
1365 media_status_t am_status;
1366 pj_uint8_t *output_buf;
1367 pjmedia_frame input_;
1368 pj_atomic_queue_t *queue;
1369
1370 pj_bzero(&input_, sizeof(pjmedia_frame));
1371 pj_bzero(&buf_info, sizeof(buf_info));
1372 samples_per_frame = and_media_data->samples_per_frame;
1373
1374 PJ_ASSERT_RETURN(output_buf_len >= samples_per_frame << 1,
1375 PJMEDIA_CODEC_EPCMTOOSHORT);
1376
1377 if (input->type != PJMEDIA_FRAME_TYPE_AUDIO)
1378 {
1379 goto on_return;
1380 }
1381 queue = codec_data->dec_avail_input_buf;
1382 if (pj_atomic_queue_get(queue, &buf_info) != PJ_SUCCESS ||
1383 buf_info.index < 0)
1384 {
1385 PJ_LOG(4,(THIS_FILE, "Decoder failed to get input Buffer[%d]",
1386 buf_info.index));
1387 goto on_return;
1388 }
1389
1390 input_buf = AMediaCodec_getInputBuffer(codec_data->dec,
1391 buf_info.index,
1392 &input_size);
1393 if (input_buf == 0) {
1394 PJ_LOG(4,(THIS_FILE, "Decoder getInputBuffer failed "
1395 "return input_buf=%d, index=%d size=%lu", *input_buf,
1396 buf_info.index, (unsigned long)input_size));
1397 goto on_return;
1398 }
1399
1400 if (and_media_data->predecode) {
1401 input_.buf = input_buf;
1402 and_media_data->predecode(codec_data, input, &input_);
1403 } else {
1404 input_.size = input->size;
1405 pj_memcpy(input_buf, input->buf, input->size);
1406 }
1407
1408 am_status = AMediaCodec_queueInputBuffer(codec_data->dec,
1409 buf_info.index,
1410 0,
1411 input_.size,
1412 input->timestamp.u32.lo,
1413 0);
1414 if (am_status != AMEDIA_OK) {
1415 PJ_LOG(4,(THIS_FILE, "Decoder queueInputBuffer failed return %d",
1416 am_status));
1417 goto on_return;
1418 }
1419
1420 pj_bzero(&buf_info, sizeof(buf_info));
1421 queue = codec_data->dec_avail_output_buf;
1422 if (pj_atomic_queue_get(queue, &buf_info) != PJ_SUCCESS ||
1423 buf_info.index < 0)
1424 {
1425 PJ_LOG(4, (THIS_FILE, "Decoder failed to get output Buffer[%d]",
1426 buf_info.index));
1427 goto on_return;
1428 }
1429
1430 output_buf = AMediaCodec_getOutputBuffer(codec_data->dec,
1431 buf_info.index,
1432 &output_size);
1433 if (output_buf == NULL) {
1434 am_status = AMediaCodec_releaseOutputBuffer(codec_data->dec,
1435 buf_info.index,
1436 0);
1437 if (am_status != AMEDIA_OK) {
1438 PJ_LOG(4,(THIS_FILE, "Decoder releaseOutputBuffer failed %d",
1439 am_status));
1440 }
1441 PJ_LOG(4,(THIS_FILE, "Decoder getOutputBuffer failed"));
1442 goto on_return;
1443 }
1444 pj_memcpy(output->buf, output_buf, buf_info.size);
1445 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
1446 output->size = buf_info.size;
1447 output->timestamp.u64 = input->timestamp.u64;
1448 am_status = AMediaCodec_releaseOutputBuffer(codec_data->dec,
1449 buf_info.index,
1450 0);
1451
1452 /* Invoke external PLC if codec has no internal PLC */
1453 if (codec_data->plc && codec_data->plc_enabled)
1454 pjmedia_plc_save(codec_data->plc, (pj_int16_t*)output->buf);
1455
1456 return PJ_SUCCESS;
1457
1458 on_return:
1459 pjmedia_zero_samples((pj_int16_t*)output->buf, samples_per_frame);
1460 output->size = samples_per_frame << 1;
1461 output->timestamp.u64 = input->timestamp.u64;
1462 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
1463 return PJ_SUCCESS;
1464 }
1465
1466 /*
1467 * Recover lost frame.
1468 */
1469 static pj_status_t and_media_codec_recover(pjmedia_codec *codec,
1470 unsigned output_buf_len,
1471 struct pjmedia_frame *output)
1472 {
1473 and_media_private_t *codec_data = (and_media_private_t*) codec->codec_data;
1474 struct and_media_codec *and_media_data =
1475 &and_media_codec[codec_data->codec_idx];
1476 unsigned samples_per_frame;
1477 pj_bool_t generate_plc = (codec_data->plc_enabled && codec_data->plc);
1478
1479 PJ_UNUSED_ARG(output_buf_len);
1480
1481 samples_per_frame = and_media_data->samples_per_frame;
1482 output->type = PJMEDIA_FRAME_TYPE_AUDIO;
1483 output->size = samples_per_frame << 1;
1484
1485 if (generate_plc)
1486 pjmedia_plc_generate(codec_data->plc, (pj_int16_t*)output->buf);
1487 else
1488 pjmedia_zero_samples((pj_int16_t*)output->buf, samples_per_frame);
1489
1490 return PJ_SUCCESS;
1491 }
1492
1493
1494 #endif /* PJMEDIA_HAS_ANDROID_MEDIACODEC */
1495