2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /*=======================================================================
19 ========================================================================*/
29 #include <mediademuxer.h>
30 #include <media_format.h>
31 #include <media_packet.h>
32 #include <media_codec.h>
34 /*-----------------------------------------------------------------------
35 | GLOBAL CONSTANT DEFINITIONS: |
36 -----------------------------------------------------------------------*/
37 #define MAX_STRING_LEN 100
38 #define PACKAGE "mediademuxer_test"
41 CURRENT_STATUS_MAINMENU,
42 CURRENT_STATUS_FILENAME,
43 CURRENT_STATUS_SET_DATA
46 /*-----------------------------------------------------------------------
47 | GLOBAL VARIABLE DEFINITIONS: |
48 -----------------------------------------------------------------------*/
49 mediademuxer_h demuxer = NULL;
50 media_format_mimetype_e v_mime;
51 media_format_mimetype_e a_mime;
52 media_format_mimetype_e t_mime;
53 media_format_text_type_e t_type;
54 int g_menu_state = CURRENT_STATUS_MAINMENU;
65 bool vid_eos_track = 0;
66 bool aud_eos_track = 0;
67 bool text_eos_track = 0;
69 /*-----------------------------------------------------------------------
71 -----------------------------------------------------------------------*/
73 * D E B U G M E S S A G E
75 #define MMF_DEBUG "** (demuxer testsuite) DEBUG: "
76 #define MMF_ERR "** (demuxer testsuite) ERROR: "
77 #define MMF_INFO "** (demuxer testsuite) INFO: "
78 #define MMF_WARN "** (demuxer testsuite) WARNING: "
80 #define CHECK_MM_ERROR(expr) \
84 if (ret != MEDIADEMUXER_ERROR_NONE) {\
85 printf("[%s:%d] error code : %x \n", __func__, __LINE__, ret); \
90 #define debug_msg_t(fmt, arg...)\
92 fprintf(stderr, MMF_DEBUG"[%s:%05d] " fmt "\n", __func__, __LINE__, ##arg); \
95 #define err_msg_t(fmt, arg...)\
97 fprintf(stderr, MMF_ERR"[%s:%05d] " fmt "\n", __func__, __LINE__, ##arg); \
100 #define info_msg_t(fmt, arg...)\
102 fprintf(stderr, MMF_INFO"[%s:%05d] " fmt "\n", __func__, __LINE__, ##arg); \
105 #define warn_msg_t(fmt, arg...)\
107 fprintf(stderr, MMF_WARN"[%s:%05d] " fmt "\n", __func__, __LINE__, ##arg); \
111 /*-----------------------------------------------------------------------
112 | TEST VARIABLE DEFINITIONS: |
113 -----------------------------------------------------------------------*/
114 #define DEMUXER_OUTPUT_DUMP 1
116 #if DEMUXER_OUTPUT_DUMP
117 FILE *fp_audio_out = NULL;
118 FILE *fp_video_out = NULL;
119 FILE *fp_text_out = NULL;
120 bool validate_dump = false;
122 #define ADTS_HEADER_SIZE 7
123 unsigned char buf_adts[ADTS_HEADER_SIZE];
125 #define AMR_NB_MIME_HDR_SIZE 6
126 #define AMR_WB_MIME_HDR_SIZE 9
127 static const char AMRNB_HDR[] = "#!AMR\n";
128 static const char AMRWB_HDR[] = "#!AMR-WB\n";
129 int write_amrnb_header = 0; /* write magic number for AMR-NB Header at one time */
130 int write_amrwb_header = 0; /* write magic number for AMR-WB Header at one time */
133 bool validate_with_codec = false;
134 mediacodec_h g_media_codec = NULL;
135 FILE *fp_out_codec_audio = NULL;
136 mediacodec_h g_media_codec_1 = NULL;
137 FILE *fp_out_codec_video = NULL;
139 /*-----------------------------------------------------------------------
141 -----------------------------------------------------------------------*/
143 #if DEMUXER_OUTPUT_DUMP
145 * Add ADTS header at the beginning of each and every AAC packet.
146 * This is needed as MediaCodec encoder generates a packet of raw AAC data.
147 * Note the packetLen must count in the ADTS header itself.
149 void generate_header_aac_adts(unsigned char *buffer, int packetLen)
151 int profile = 2; /* AAC LC (0x01) */
152 int freqIdx = 4; /* 44KHz (0x04) */
153 int chanCfg = 1; /* CPE (0x01) */
155 if (samplerate == 96000) freqIdx = 0;
156 else if (samplerate == 88200) freqIdx = 1;
157 else if (samplerate == 64000) freqIdx = 2;
158 else if (samplerate == 48000) freqIdx = 3;
159 else if (samplerate == 44100) freqIdx = 4;
160 else if (samplerate == 32000) freqIdx = 5;
161 else if (samplerate == 24000) freqIdx = 6;
162 else if (samplerate == 22050) freqIdx = 7;
163 else if (samplerate == 16000) freqIdx = 8;
164 else if (samplerate == 12000) freqIdx = 9;
165 else if (samplerate == 11025) freqIdx = 10;
166 else if (samplerate == 8000) freqIdx = 11;
168 if ((channel == 1) || (channel == 2))
171 /* Make ADTS header */
172 buffer[0] = (char)0xFF;
173 buffer[1] = (char)0xF1;
174 buffer[2] = (char)(((profile - 1) << 6) + (freqIdx << 2) + (chanCfg >> 2));
175 buffer[3] = (char)(((chanCfg & 3) << 6) + (packetLen >> 11));
176 buffer[4] = (char)((packetLen & 0x7FF) >> 3);
177 buffer[5] = (char)(((packetLen & 7) << 5) + 0x1F);
178 buffer[6] = (char)0xFC;
182 /*-----------------------------------------------------------------------
184 -----------------------------------------------------------------------*/
186 int test_mediademuxer_create()
189 g_print("test_mediademuxer_create\n");
190 ret = mediademuxer_create(&demuxer);
194 int test_mediademuxer_set_data_source(mediademuxer_h demuxer, const char *path)
197 g_print("test_mediademuxer_set_data_source\n");
199 #if DEMUXER_OUTPUT_DUMP
200 fp_audio_out = fopen("/tmp/dump_audio.out", "wb");
201 if (fp_audio_out != NULL) {
202 validate_dump = true;
203 fp_video_out = fopen("/tmp/dump_video.out", "wb");
204 fp_text_out = fopen("/tmp/dump_text.out", "wb");
206 g_print("Error - Cannot open file for file dump, Please chek root\n");
210 ret = mediademuxer_set_data_source(demuxer, path);
214 int test_mediademuxer_prepare()
217 g_print("test_mediademuxer_prepare\n");
218 ret = mediademuxer_prepare(demuxer);
222 int test_mediademuxer_get_track_count()
224 g_print("test_mediademuxer_get_track_count\n");
225 mediademuxer_get_track_count(demuxer, &num_tracks);
226 g_print("Number of total tracks [%d]\n", num_tracks);
230 int test_mediademuxer_select_track()
233 g_print("test_mediademuxer_select_track\n");
234 for (track = 0; track < num_tracks; track++) {
235 if (mediademuxer_select_track(demuxer, track)) {
236 g_print("mediademuxer_select_track index [%d] failed\n", track);
239 g_print("select track index is [%d] of the total track [%d]\n", track, num_tracks);
244 int test_mediademuxer_start()
247 g_print("test_mediademuxer_start\n");
248 ret = mediademuxer_start(demuxer);
252 int test_mediademuxer_get_track_info()
257 g_print("test_mediademuxer_get_track_info\n");
258 for (; track < num_tracks; track++) {
259 media_format_h g_media_format;
260 ret = mediademuxer_get_track_info(demuxer, track, &g_media_format);
262 if (media_format_get_video_info(g_media_format, &v_mime,
263 &w, &h, NULL, NULL) == MEDIA_FORMAT_ERROR_NONE) {
264 g_print("media_format_get_video_info is sucess!\n");
265 g_print("\t\t[media_format_get_video]mime:%x, width :%d, height :%d\n",
268 } else if (media_format_get_audio_info(g_media_format, &a_mime,
269 &channel, &samplerate, &bit, NULL) == MEDIA_FORMAT_ERROR_NONE) {
270 g_print("media_format_get_audio_info is sucess!\n");
271 g_print("\t\t[media_format_get_audio]mime:%x, channel :%d, samplerate :%d, bit :%d\n",
272 a_mime, channel, samplerate, bit);
273 if (a_mime == MEDIA_FORMAT_AAC_LC)
274 media_format_get_audio_aac_type(g_media_format, &is_adts);
276 } else if (media_format_get_text_info(g_media_format, &t_mime, &t_type) == MEDIA_FORMAT_ERROR_NONE) {
277 g_print("media_format_get_text_info is sucess!\n");
278 g_print("\t\t[media_format_get_text]mime:%x, type:%x\n", t_mime, t_type);
281 g_print("Not Supported YET\n");
283 media_format_unref(g_media_format);
284 g_media_format = NULL;
286 g_print("Error while getting mediademuxer_get_track_info\n");
290 #if DEMUXER_OUTPUT_DUMP
291 if ((a_mime == MEDIA_FORMAT_AAC_LC) && (is_adts == 0)) {
292 g_print("MIME : MEDIA_FORMAT_AAC_LC ------Need to add header for dump test \n");
293 } else if (a_mime == MEDIA_FORMAT_AMR_NB) {
294 g_print("MIME : MEDIA_FORMAT_AMR_NB ------Need to add header for dump test \n");
295 write_amrnb_header = 1;
296 } else if (a_mime == MEDIA_FORMAT_AMR_WB) {
297 g_print("MIME : MEDIA_FORMAT_AMR_WB ------Need to add header for dump test \n");
298 write_amrwb_header = 1;
300 g_print("--------------------------- Don't Need to add header for dump test\n");
306 static void mediacodec_finish(mediacodec_h handle, FILE *fp)
310 mediacodec_unset_output_buffer_available_cb(handle);
311 err = mediacodec_unprepare(handle);
312 if (err != MEDIACODEC_ERROR_NONE) {
313 g_print("mediacodec_unprepare failed error = %d \n", err);
316 err = mediacodec_destroy(handle);
317 if (err != MEDIACODEC_ERROR_NONE)
318 g_print("mediacodec_destory failed error = %d \n", err);
322 static void _mediacodec_fill_audio_buffer_cb(media_packet_h pkt, void *user_data)
325 uint64_t buf_size = 0;
327 media_packet_h output_buf;
330 err = mediacodec_get_output(g_media_codec, &output_buf, 0);
331 if (err == MEDIACODEC_ERROR_NONE) {
332 media_packet_get_buffer_size(output_buf, &buf_size);
333 media_packet_get_buffer_data_ptr(output_buf, &data);
335 fwrite(data, 1, buf_size, fp_out_codec_audio);
337 g_print("Data is null inside _mediacodec_fill_audio_buffer_cb\n");
339 media_packet_destroy(output_buf);
341 g_print("mediacodec_get_output failed inside _mediacodec_fill_audio_buffer_cb err = %d\n", err);
345 g_print("audio pkt from mediacodec is null\n");
350 static void mediacodec_init_audio(int codecid, int flag, int samplerate, int channel, int bit)
352 /* This file will be used to dump the audio data coming out from mediacodec */
353 fp_out_codec_audio = fopen("/opt/usr/codec_dump_audio.out", "wb");
354 g_print("Create dumped file as codec_dump_audio.out\n");
356 if (g_media_codec != NULL) {
357 mediacodec_unprepare(g_media_codec);
358 mediacodec_destroy(g_media_codec);
359 g_media_codec = NULL;
361 if (mediacodec_create(&g_media_codec) != MEDIACODEC_ERROR_NONE) {
362 g_print("mediacodec_create is failed\n");
365 /* Now set the code info */
366 if ((mediacodec_set_codec(g_media_codec, (mediacodec_codec_type_e)codecid,
367 (mediacodec_support_type_e)flag) != MEDIACODEC_ERROR_NONE)) {
368 g_print("mediacodec_set_codec is failed\n");
371 /* set the audio dec info */
372 if ((mediacodec_set_adec_info(g_media_codec, samplerate, channel, bit)) != MEDIACODEC_ERROR_NONE) {
373 g_print("mediacodec_set_adec is failed\n");
376 /* Set the callback for output data, which will be used to write the data to file */
377 mediacodec_set_output_buffer_available_cb(g_media_codec,
378 _mediacodec_fill_audio_buffer_cb,
381 if (MEDIACODEC_ERROR_NONE != mediacodec_prepare(g_media_codec)) {
382 g_print("mediacodec prepare is failed\n");
387 static void mediacodec_process_audio_pkt(media_packet_h in_buf)
389 if (g_media_codec != NULL) {
390 /* process the media packet */
391 if (MEDIACODEC_ERROR_NONE != mediacodec_process_input(g_media_codec, in_buf, 0)) {
392 g_print("mediacodec_process_input is failed inside mediacodec_process_audio_pkt\n");
398 void *_fetch_audio_data(void *ptr)
400 int ret = MEDIADEMUXER_ERROR_NONE;
401 int *status = (int *)g_malloc(sizeof(int) * 1);
403 g_print("Fail malloc fetch audio data retur status value\n");
406 media_packet_h audbuf;
408 uint64_t buf_size = 0;
412 g_print("Audio Data function\n");
414 if (validate_with_codec) {
416 if (a_mime == MEDIA_FORMAT_AAC_LC || a_mime == MEDIA_FORMAT_AAC_HE ||
417 a_mime == MEDIA_FORMAT_AAC_HE_PS) {
419 g_print("mediacodec_init_audio() for MEDIACODEC_AAC\n");
420 mediacodec_init_audio(MEDIACODEC_AAC, flag, samplerate, channel, bit);
421 } else if (a_mime == MEDIA_FORMAT_MP3) {
423 g_print("mediacodec_init_audio() for MEDIACODEC_MP3\n");
424 mediacodec_init_audio(MEDIACODEC_MP3, flag, samplerate, channel, bit);
425 } else if (a_mime == MEDIA_FORMAT_AMR_NB) {
427 g_print("mediacodec_init_audio() for MEDIACODEC_AMR_NB\n");
428 mediacodec_init_audio(MEDIACODEC_AMR_NB, flag, samplerate, channel, bit);
429 } else if (a_mime == MEDIA_FORMAT_AMR_WB) {
431 g_print("mediacodec_init_audio() for MEDIACODEC_AMR_WB\n");
432 mediacodec_init_audio(MEDIACODEC_AMR_WB, flag, samplerate, channel, bit);
433 } else if (a_mime == MEDIA_FORMAT_FLAC) {
435 g_print("mediacodec_init_audio() for MEDIACODEC_FLAC\n");
436 mediacodec_init_audio(MEDIACODEC_FLAC, flag, samplerate, channel, bit);
437 } else if (a_mime == MEDIA_FORMAT_VORBIS) {
439 g_print("mediacodec_init_audio() for MEDIACODEC_VORBIS\n");
440 mediacodec_init_audio(MEDIACODEC_VORBIS, flag, samplerate, channel, bit);
442 g_print("Not Supported YET- Need to add mime for validating with audio codec\n");
443 return (void *)status;
448 ret = mediademuxer_read_sample(demuxer, aud_track, &audbuf);
449 if (ret != MEDIADEMUXER_ERROR_NONE) {
450 g_print("Error (%d) return of mediademuxer_read_sample()\n", ret);
456 media_packet_get_buffer_size(audbuf, &buf_size);
457 media_packet_get_buffer_data_ptr(audbuf, &data);
458 g_print("Audio Read Count::[%4d] frame - get_buffer_size = %"PRIu64"\n", count, buf_size);
460 #if DEMUXER_OUTPUT_DUMP
462 if ((a_mime == MEDIA_FORMAT_AAC_LC) && (is_adts == 0)) {
463 /* This is used only AAC raw case for adding each ADTS frame header */
464 generate_header_aac_adts(buf_adts, (buf_size+ADTS_HEADER_SIZE));
465 fwrite(&buf_adts[0], 1, ADTS_HEADER_SIZE, fp_audio_out);
466 } else if ((a_mime == MEDIA_FORMAT_AMR_NB) && (write_amrnb_header == 1)) {
467 /* This is used only AMR-NB case for adding magic header in only first frame */
468 g_print("%s - AMRNB_HDR write in first frame\n", __func__);
469 fwrite(&AMRNB_HDR[0], 1, sizeof(AMRNB_HDR) - 1, fp_audio_out);
470 write_amrnb_header = 0;
471 } else if ((a_mime == MEDIA_FORMAT_AMR_WB) && (write_amrwb_header == 1)) {
472 /* This is used only AMR-WB case for adding magic header in only first frame */
473 g_print("%s - AMRWB_HDR write in first frame\n", __func__);
474 fwrite(&AMRWB_HDR[0], 1, sizeof(AMRWB_HDR) - 1, fp_audio_out);
475 write_amrwb_header = 0;
479 fwrite(data, 1, buf_size, fp_audio_out);
481 g_print("DUMP : write(audio data) fail for NULL\n");
485 if (validate_with_codec)
486 mediacodec_process_audio_pkt(audbuf);
488 media_packet_destroy(audbuf);
491 g_print("EOS return of mediademuxer_read_sample() for audio\n");
493 if (validate_with_codec)
494 mediacodec_finish(g_media_codec, fp_out_codec_audio);
495 return (void *)status;
498 static void _mediacodec_fill_video_buffer_cb(media_packet_h pkt, void *user_data)
501 uint64_t buf_size = 0;
503 media_packet_h output_buf;
506 err = mediacodec_get_output(g_media_codec_1, &output_buf, 0);
507 if (err == MEDIACODEC_ERROR_NONE) {
508 media_packet_get_buffer_size(output_buf, &buf_size);
509 /* g_print("%s - output_buf size = %lld\n", __func__, buf_size); */
510 media_packet_get_buffer_data_ptr(output_buf, &data);
512 fwrite(data, 1, buf_size, fp_out_codec_video);
514 g_print("Data is null inside _mediacodec_fill_video_buffer_cb\n");
515 media_packet_destroy(output_buf);
517 g_print("mediacodec_get_output failed inside _mediacodec_fill_video_buffer_cb lerr = %d\n", err);
521 g_print("video pkt from mediacodec is null\n");
526 static void mediacodec_init_video(int codecid, int flag, int width, int height)
528 /* This file will be used to dump the data */
529 fp_out_codec_video = fopen("/opt/usr/codec_dump_video.out", "wb");
530 g_print("Create dumped file as codec_dump_video.out\n");
532 if (g_media_codec_1 != NULL) {
533 mediacodec_unprepare(g_media_codec_1);
534 mediacodec_destroy(g_media_codec_1);
535 g_media_codec_1 = NULL;
537 if (mediacodec_create(&g_media_codec_1) != MEDIACODEC_ERROR_NONE) {
538 g_print("mediacodec_create is failed\n");
541 /* Now set the code info */
542 if ((mediacodec_set_codec(g_media_codec_1, (mediacodec_codec_type_e)codecid,
543 (mediacodec_support_type_e)flag) != MEDIACODEC_ERROR_NONE)) {
544 g_print("mediacodec_set_codec is failed\n");
547 /* set the video dec info */
548 if ((mediacodec_set_vdec_info(g_media_codec_1, width, height)) != MEDIACODEC_ERROR_NONE) {
549 g_print("mediacodec_set_vdec is failed\n");
552 /* Set the callback for output data, which will be used to write the data to file */
553 mediacodec_set_output_buffer_available_cb(g_media_codec_1,
554 _mediacodec_fill_video_buffer_cb,
557 if (MEDIACODEC_ERROR_NONE != mediacodec_prepare(g_media_codec_1)) {
558 g_print("mediacodec_prepare is failed\n");
563 static void mediacodec_process_video_pkt(media_packet_h in_buf)
565 if (g_media_codec_1 != NULL) {
566 /* process the media packet */
567 if (MEDIACODEC_ERROR_NONE != mediacodec_process_input(g_media_codec_1, in_buf, 0)) {
568 g_print("mediacodec process input is failed inside mediacodec_process_video_pkt\n");
572 g_print("mediacodec handle is invalid inside mediacodec_process_video_pkt()\n");
577 static void _local_media_packet_get_codec_data(media_packet_h pkt)
579 unsigned char* get_codec_data;
580 unsigned int get_codec_data_size;
582 if (media_packet_get_codec_data(pkt, (void**) &get_codec_data, &get_codec_data_size) == MEDIA_PACKET_ERROR_NONE) {
583 g_print("media_packet_get_codec_data is sucess ... !\n");
584 g_print("codec_data_size = %u\n", get_codec_data_size);
585 get_codec_data[get_codec_data_size] = '\0';
586 if (get_codec_data_size == 0)
588 g_print("media packet codec_data is [%s] \n", get_codec_data);
590 g_print("media_packet_get_codec_data is failed...\n");
595 void *_fetch_video_data(void *ptr)
597 int ret = MEDIADEMUXER_ERROR_NONE;
598 int *status = (int *)g_malloc(sizeof(int) * 1);
600 g_print("Fail malloc fetch video data retur status value\n");
603 media_packet_h vidbuf;
605 uint64_t buf_size = 0;
609 g_print("Video Data function\n");
611 if (validate_with_codec) {
613 if (v_mime == MEDIA_FORMAT_H264_SP || v_mime == MEDIA_FORMAT_H264_MP ||
614 v_mime == MEDIA_FORMAT_H264_HP) {
616 g_print("mediacodec_init_video() for MEDIACODEC_H264\n");
617 mediacodec_init_video(MEDIACODEC_H264, flag, w, h);
618 } else if (v_mime == MEDIA_FORMAT_H263) {
619 g_print("mediacodec_init_video() for MEDIACODEC_H263\n");
621 mediacodec_init_video(MEDIACODEC_H263, flag, w, h);
623 g_print("Not Supported YET- Need to add mime for validating with video codec\n");
624 return (void *)status;
628 ret = mediademuxer_read_sample(demuxer, vid_track, &vidbuf);
629 if (ret != MEDIADEMUXER_ERROR_NONE) {
630 g_print("Error (%d) return of mediademuxer_read_sample()\n", ret);
636 media_packet_get_buffer_size(vidbuf, &buf_size);
637 media_packet_get_buffer_data_ptr(vidbuf, &data);
638 g_print("Video Read Count::[%4d] frame - get_buffer_size = %"PRIu64"\n", count, buf_size);
640 /* This is used for debugging purpose */
641 _local_media_packet_get_codec_data(vidbuf);
643 #if DEMUXER_OUTPUT_DUMP
646 fwrite(data, 1, buf_size, fp_video_out);
648 g_print("DUMP : write(video data) fail for NULL\n");
652 if (validate_with_codec)
653 mediacodec_process_video_pkt(vidbuf);
655 media_packet_destroy(vidbuf);
657 g_print("EOS return of mediademuxer_read_sample() for video\n");
659 if (validate_with_codec)
660 mediacodec_finish(g_media_codec_1, fp_out_codec_video);
662 return (void *)status;
665 void *_fetch_text_data(void *ptr)
667 int ret = MEDIADEMUXER_ERROR_NONE;
668 int *status = (int *)g_malloc(sizeof(int) * 1);
670 g_print("Fail malloc fetch video data retur status value\n");
673 media_packet_h txtbuf;
675 uint64_t buf_size = 0;
679 g_print("text Data function\n");
680 g_print("Text track number is :%d\n", txt_track);
682 ret = mediademuxer_read_sample(demuxer, txt_track, &txtbuf);
683 if (ret != MEDIADEMUXER_ERROR_NONE) {
684 g_print("Error (%d) return of mediademuxer_read_sample()\n", ret);
690 media_packet_get_buffer_size(txtbuf, &buf_size);
691 media_packet_get_buffer_data_ptr(txtbuf, &data);
692 g_print("Text Read Count::[%4d] frame - get_buffer_size = %"PRIu64"\n", count, buf_size);
693 #if DEMUXER_OUTPUT_DUMP
696 fwrite(data, 1, buf_size, fp_text_out);
698 g_print("DUMP : write(text data) fail for NULL\n");
702 g_print("EOS return of mediademuxer_read_sample()\n");
704 return (void *)status;
707 int test_mediademuxer_read_sample()
711 /* Initialize and set thread detached attribute */
712 pthread_attr_init(&attr);
713 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
714 if (vid_track != -1) {
715 g_print("In main: creating thread for video\n");
716 pthread_create(&thread[0], &attr, _fetch_video_data, NULL);
718 if (aud_track != -1) {
719 g_print("In main: creating thread for audio\n");
720 pthread_create(&thread[1], &attr, _fetch_audio_data, NULL);
722 if (txt_track != -1) {
723 g_print("In main: creating thread for text\n");
724 pthread_create(&thread[2], &attr, _fetch_text_data, NULL);
726 pthread_attr_destroy(&attr);
730 int test_mediademuxer_seek_to()
732 g_print("test_mediademuxer_seek_to\n");
734 mediademuxer_seek(demuxer, pos);
735 g_print("Number of tracks [%d]\n", num_tracks);
739 int test_mediademuxer_unselect_track()
741 g_print("test_mediademuxer_select_track\n");
742 if (mediademuxer_unselect_track(demuxer, 0)) {
743 g_print("mediademuxer_select track 0 failed\n");
749 int test_mediademuxer_stop()
752 g_print("test_mediademuxer_stop\n");
753 ret = mediademuxer_stop(demuxer);
757 int test_mediademuxer_unprepare()
760 g_print("test_mediademuxer_unprepare\n");
761 ret = mediademuxer_unprepare(demuxer);
765 int test_mediademuxer_destroy()
768 g_print("test_mediademuxer_destroy\n");
769 ret = mediademuxer_destroy(demuxer);
772 #if DEMUXER_OUTPUT_DUMP
774 fclose(fp_audio_out);
776 fclose(fp_video_out);
781 int test_mediademuxer_get_state()
783 g_print("test_mediademuxer_get_state\n");
784 mediademuxer_state state;
785 if (mediademuxer_get_state(demuxer, &state) == MEDIADEMUXER_ERROR_NONE) {
786 if (state == MEDIADEMUXER_STATE_NONE)
787 g_print("Mediademuxer_state = NONE\n");
788 else if (state == MEDIADEMUXER_STATE_IDLE)
789 g_print("Mediademuxer_state = IDLE\n");
790 else if (state == MEDIADEMUXER_STATE_READY)
791 g_print("Mediademuxer_state = READY\n");
792 else if (state == MEDIADEMUXER_STATE_DEMUXING)
793 g_print("Mediademuxer_state = DEMUXING\n");
795 g_print("Mediademuxer_state = NOT SUPPORT STATE\n");
797 g_print("Mediademuxer_state call failed\n");
802 void app_err_cb(mediademuxer_error_e error, void *user_data)
804 printf("Got Error %d from Mediademuxer\n", error);
807 void app_eos_cb(int track_index, void *user_data)
809 printf("Got EOS for track -- %d from Mediademuxer\n", track_index);
810 if (track_index == vid_track)
811 vid_eos_track = true;
812 else if (track_index == aud_track)
813 aud_eos_track = true;
814 else if (track_index == txt_track)
815 text_eos_track = true;
817 g_print("EOS for invalid track number\n");
820 int test_mediademuxer_set_error_cb()
823 g_print("test_mediademuxer_set_error_cb\n");
824 ret = mediademuxer_set_error_cb(demuxer, app_err_cb, demuxer);
828 int test_mediademuxer_set_eos_cb()
831 g_print("test_mediademuxer_set_eos_cb\n");
832 ret = mediademuxer_set_eos_cb(demuxer, app_eos_cb, demuxer);
836 /*-----------------------------------------------------------------------
838 -----------------------------------------------------------------------*/
839 int test_mediademuxer_get_sample_track_index()
841 g_print("test_mediademuxer_get_sample_track_index\n");
845 int test_mediademuxer_get_sample_track_time()
847 g_print("test_mediademuxer_get_sample_track_time\n");
851 int test_mediademuxer_advance()
853 g_print("test_mediademuxer_advance\n");
857 int test_mediademuxer_is_key_frame()
859 g_print("test_mediademuxer_is_key_frame\n");
863 int test_mediademuxer_is_encrypted()
865 g_print("test_mediademuxer_is_encrypted\n");
869 int test_mediademuxer_audio_only()
871 g_print("AUDIO ONLY\n");
875 int test_mediademuxer_video_only()
877 g_print("VIDEO ONLY\n");
881 int test_mediademuxer_audio_video_only()
883 g_print("AUDIO & VIDEO ONLY\n");
887 /*-----------------------------------------------------------------------
889 -----------------------------------------------------------------------*/
890 static void display_sub_basic()
893 g_print("===========================================================================\n");
894 g_print(" media demuxer test\n");
895 g_print(" SELECT : c -> (s) -> e -> p -> (goto submenu) -> d -> q \n");
896 g_print("---------------------------------------------------------------------------\n");
897 g_print("c. Create \t");
898 g_print("s. Set callback \t");
899 g_print("e. Set eos callback \t");
900 g_print("p. Path \t");
901 g_print("d. Destroy \t");
902 g_print("q. Quit \n");
903 g_print("---------------------------------------------------------------------------\n");
904 if (validate_with_codec)
905 g_print("[Validation with Media codec]\n");
907 g_print("[validation as stand alone. To validate with media codec, run mediademuxertest with -c option]\n");
910 void _interpret_main_menu(char *cmd)
912 int len = strlen(cmd);
914 if (strncmp(cmd, "c", 1) == 0) {
915 test_mediademuxer_create();
916 } else if (strncmp(cmd, "s", 1) == 0) {
917 test_mediademuxer_set_error_cb();
918 } else if (strncmp(cmd, "e", 1) == 0) {
919 test_mediademuxer_set_eos_cb();
920 } else if (strncmp(cmd, "p", 1) == 0) {
921 g_menu_state = CURRENT_STATUS_FILENAME;
922 } else if (strncmp(cmd, "d", 1) == 0) {
923 test_mediademuxer_unprepare();
924 test_mediademuxer_destroy();
925 } else if (strncmp(cmd, "q", 1) == 0) {
928 g_print("unknown menu \n");
934 static void displaymenu(void)
936 if (g_menu_state == CURRENT_STATUS_MAINMENU) {
938 } else if (g_menu_state == CURRENT_STATUS_FILENAME) {
939 g_print("*** input media path.\n");
940 } else if (g_menu_state == CURRENT_STATUS_SET_DATA) {
942 g_print("=============================================================\n");
943 g_print(" media demuxer submenu\n");
944 g_print(" SELECT from 1 to 9, others are option \n");
945 g_print("-------------------------------------------------------------\n");
946 g_print(" 1. Get Track\t");
947 g_print(" 2. Select Track\n");
948 g_print(" 3. Start\t");
949 g_print(" 4. Get Info\n");
950 g_print(" 5. Read Sample\n");
951 g_print(" 6. Stop\t");
952 g_print(" 7. Unprepare\t");
953 g_print(" 8. Get state\n");
954 g_print(" 9. Go Back to main menu\n");
955 g_print(" a. Seek\t");
956 g_print(" b. Uselect Track\n");
957 g_print(" c. Get Sample Track Index\t");
958 g_print(" d. Get Sample Track Time\n");
959 g_print(" e. Advance\t");
960 g_print(" f. Is Key Frame\t");
961 g_print(" g. Is Key encrypted\n");
962 g_print("-------------------------------------------------------------\n");
964 g_print("*** unknown status.\n");
970 void reset_menu_state()
972 g_menu_state = CURRENT_STATUS_MAINMENU;
976 gboolean timeout_menu_display(void *data)
982 static void interpret(char *cmd)
984 switch (g_menu_state) {
985 case CURRENT_STATUS_MAINMENU: {
986 _interpret_main_menu(cmd);
989 case CURRENT_STATUS_FILENAME: {
991 ret = test_mediademuxer_set_data_source(demuxer, cmd);
992 if (ret == MEDIADEMUXER_ERROR_NONE) {
993 ret = test_mediademuxer_prepare();
994 if (ret == MEDIADEMUXER_ERROR_NONE) {
995 g_menu_state = CURRENT_STATUS_SET_DATA;
997 g_print("test_mediademuxer_prepare failed \n");
998 if (ret == MEDIADEMUXER_ERROR_INVALID_OPERATION)
999 g_menu_state = CURRENT_STATUS_MAINMENU;
1001 g_menu_state = CURRENT_STATUS_FILENAME;
1004 if (ret == MEDIADEMUXER_ERROR_INVALID_PATH)
1005 g_print("Invalid path, file does not exist\n");
1006 g_menu_state = CURRENT_STATUS_FILENAME;
1010 case CURRENT_STATUS_SET_DATA: {
1011 int len = strlen(cmd);
1013 if (strncmp(cmd, "1", len) == 0)
1014 test_mediademuxer_get_track_count();
1015 else if (strncmp(cmd, "2", len) == 0)
1016 test_mediademuxer_select_track();
1017 else if (strncmp(cmd, "3", len) == 0)
1018 test_mediademuxer_start();
1019 else if (strncmp(cmd, "4", len) == 0)
1020 test_mediademuxer_get_track_info();
1021 else if (strncmp(cmd, "5", len) == 0)
1022 test_mediademuxer_read_sample();
1023 else if (strncmp(cmd, "6", len) == 0)
1024 test_mediademuxer_stop();
1025 else if (strncmp(cmd, "7", len) == 0)
1026 test_mediademuxer_unprepare();
1027 else if (strncmp(cmd, "8", len) == 0)
1028 test_mediademuxer_get_state();
1029 else if (strncmp(cmd, "9", len) == 0)
1031 else if (strncmp(cmd, "a", len) == 0)
1032 test_mediademuxer_seek_to();
1033 else if (strncmp(cmd, "b", len) == 0)
1034 test_mediademuxer_unselect_track();
1035 else if (strncmp(cmd, "c", len) == 0)
1036 test_mediademuxer_get_sample_track_index();
1037 else if (strncmp(cmd, "d", len) == 0)
1038 test_mediademuxer_get_sample_track_time();
1039 else if (strncmp(cmd, "e", len) == 0)
1040 test_mediademuxer_advance();
1041 else if (strncmp(cmd, "f", len) == 0)
1042 test_mediademuxer_is_key_frame();
1043 else if (strncmp(cmd, "g", len) == 0)
1044 test_mediademuxer_is_encrypted();
1046 g_print("UNKNOW COMMAND\n");
1047 } else if (len == 2) {
1048 if (strncmp(cmd, "10", len) == 0)
1049 g_print("UNKNOW COMMAND\n");
1051 g_print("UNKNOW COMMAND\n");
1053 g_print("UNKNOW COMMAND\n");
1060 g_timeout_add(100, timeout_menu_display, 0);
1064 * This function is to execute command.
1066 * @param channel [in] 1st parameter
1068 * @return This function returns TRUE/FALSE
1072 gboolean input(GIOChannel *channel)
1074 gchar buf[MAX_STRING_LEN];
1076 GError *error = NULL;
1077 g_io_channel_read_chars(channel, buf, MAX_STRING_LEN, &read, &error);
1085 * This function is the example main function for mediademuxer API.
1089 * @return This function returns 0.
1091 * @see other functions
1093 int main(int argc, char *argv[])
1095 GIOChannel *stdin_channel;
1096 GMainLoop *loop = g_main_loop_new(NULL, 0);
1097 stdin_channel = g_io_channel_unix_new(0);
1098 g_io_channel_set_flags(stdin_channel, G_IO_FLAG_NONBLOCK, NULL);
1099 g_io_add_watch(stdin_channel, G_IO_IN, (GIOFunc) input, NULL);
1102 /* Check whether validation with media codec is required */
1103 if (argv[1][0] == '-' && argv[1][1] == 'c')
1104 validate_with_codec = true;
1108 g_print("RUN main loop\n");
1109 g_main_loop_run(loop);
1110 g_print("STOP main loop\n");
1112 g_main_loop_unref(loop);