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 #ifdef USE_MEDIA_CODEC
33 #include <media_codec.h>
36 /*-----------------------------------------------------------------------
37 | GLOBAL CONSTANT DEFINITIONS: |
38 -----------------------------------------------------------------------*/
39 #define MAX_STRING_LEN 100
40 #define PACKAGE "mediademuxer_test"
43 CURRENT_STATUS_MAINMENU,
44 CURRENT_STATUS_FILENAME,
45 CURRENT_STATUS_SET_DATA
48 /*-----------------------------------------------------------------------
49 | GLOBAL VARIABLE DEFINITIONS: |
50 -----------------------------------------------------------------------*/
51 mediademuxer_h demuxer = NULL;
52 media_format_mimetype_e v_mime;
53 media_format_mimetype_e a_mime;
54 media_format_mimetype_e t_mime;
55 media_format_text_type_e t_type;
56 int g_menu_state = CURRENT_STATUS_MAINMENU;
67 bool vid_eos_track = 0;
68 bool aud_eos_track = 0;
69 bool text_eos_track = 0;
71 /*-----------------------------------------------------------------------
73 -----------------------------------------------------------------------*/
75 * D E B U G M E S S A G E
77 #define MMF_DEBUG "** (demuxer testsuite) DEBUG: "
78 #define MMF_ERR "** (demuxer testsuite) ERROR: "
79 #define MMF_INFO "** (demuxer testsuite) INFO: "
80 #define MMF_WARN "** (demuxer testsuite) WARNING: "
82 #define CHECK_MM_ERROR(expr) \
86 if (ret != MEDIADEMUXER_ERROR_NONE) {\
87 printf("[%s:%d] error code : %x \n", __func__, __LINE__, ret); \
92 #define debug_msg_t(fmt, arg...)\
94 fprintf(stderr, MMF_DEBUG"[%s:%05d] " fmt "\n", __func__, __LINE__, ##arg); \
97 #define err_msg_t(fmt, arg...)\
99 fprintf(stderr, MMF_ERR"[%s:%05d] " fmt "\n", __func__, __LINE__, ##arg); \
102 #define info_msg_t(fmt, arg...)\
104 fprintf(stderr, MMF_INFO"[%s:%05d] " fmt "\n", __func__, __LINE__, ##arg); \
107 #define warn_msg_t(fmt, arg...)\
109 fprintf(stderr, MMF_WARN"[%s:%05d] " fmt "\n", __func__, __LINE__, ##arg); \
113 /*-----------------------------------------------------------------------
114 | TEST VARIABLE DEFINITIONS: |
115 -----------------------------------------------------------------------*/
116 #define DEMUXER_OUTPUT_DUMP 1
118 #if DEMUXER_OUTPUT_DUMP
119 FILE *fp_audio_out = NULL;
120 FILE *fp_video_out = NULL;
121 FILE *fp_text_out = NULL;
122 bool validate_dump = false;
124 #define ADTS_HEADER_SIZE 7
125 unsigned char buf_adts[ADTS_HEADER_SIZE];
127 #define AMR_NB_MIME_HDR_SIZE 6
128 #define AMR_WB_MIME_HDR_SIZE 9
129 static const char AMRNB_HDR[] = "#!AMR\n";
130 static const char AMRWB_HDR[] = "#!AMR-WB\n";
131 int write_amrnb_header = 0; /* write magic number for AMR-NB Header at one time */
132 int write_amrwb_header = 0; /* write magic number for AMR-WB Header at one time */
135 bool validate_with_codec = false;
137 FILE *fp_out_codec_audio = NULL;
138 FILE *fp_out_codec_video = NULL;
139 #ifdef USE_MEDIA_CODEC
140 mediacodec_h g_media_codec = NULL;
141 mediacodec_h g_media_codec_1 = NULL;
144 /*-----------------------------------------------------------------------
146 -----------------------------------------------------------------------*/
148 #if DEMUXER_OUTPUT_DUMP
150 * Add ADTS header at the beginning of each and every AAC packet.
151 * This is needed as MediaCodec encoder generates a packet of raw AAC data.
152 * Note the packetLen must count in the ADTS header itself.
154 void generate_header_aac_adts(unsigned char *buffer, int packetLen)
156 int profile = 2; /* AAC LC (0x01) */
157 int freqIdx = 4; /* 44KHz (0x04) */
158 int chanCfg = 1; /* CPE (0x01) */
160 if (samplerate == 96000) freqIdx = 0;
161 else if (samplerate == 88200) freqIdx = 1;
162 else if (samplerate == 64000) freqIdx = 2;
163 else if (samplerate == 48000) freqIdx = 3;
164 else if (samplerate == 44100) freqIdx = 4;
165 else if (samplerate == 32000) freqIdx = 5;
166 else if (samplerate == 24000) freqIdx = 6;
167 else if (samplerate == 22050) freqIdx = 7;
168 else if (samplerate == 16000) freqIdx = 8;
169 else if (samplerate == 12000) freqIdx = 9;
170 else if (samplerate == 11025) freqIdx = 10;
171 else if (samplerate == 8000) freqIdx = 11;
173 if ((channel == 1) || (channel == 2))
176 /* Make ADTS header */
177 buffer[0] = (char)0xFF;
178 buffer[1] = (char)0xF1;
179 buffer[2] = (char)(((profile - 1) << 6) + (freqIdx << 2) + (chanCfg >> 2));
180 buffer[3] = (char)(((chanCfg & 3) << 6) + (packetLen >> 11));
181 buffer[4] = (char)((packetLen & 0x7FF) >> 3);
182 buffer[5] = (char)(((packetLen & 7) << 5) + 0x1F);
183 buffer[6] = (char)0xFC;
187 /*-----------------------------------------------------------------------
189 -----------------------------------------------------------------------*/
191 int test_mediademuxer_create()
194 g_print("test_mediademuxer_create\n");
195 ret = mediademuxer_create(&demuxer);
199 int test_mediademuxer_set_data_source(mediademuxer_h demuxer, const char *path)
202 g_print("test_mediademuxer_set_data_source\n");
204 #if DEMUXER_OUTPUT_DUMP
205 fp_audio_out = fopen("/tmp/dump_audio.out", "wb");
206 if (fp_audio_out != NULL) {
207 validate_dump = true;
208 fp_video_out = fopen("/tmp/dump_video.out", "wb");
209 fp_text_out = fopen("/tmp/dump_text.out", "wb");
211 g_print("Error - Cannot open file for file dump, Please check root\n");
215 ret = mediademuxer_set_data_source(demuxer, path);
219 int test_mediademuxer_prepare()
222 g_print("test_mediademuxer_prepare\n");
223 ret = mediademuxer_prepare(demuxer);
227 int test_mediademuxer_get_track_count()
229 g_print("test_mediademuxer_get_track_count\n");
230 mediademuxer_get_track_count(demuxer, &num_tracks);
231 g_print("Number of total tracks [%d]\n", num_tracks);
235 int test_mediademuxer_select_track()
238 g_print("test_mediademuxer_select_track\n");
239 for (track = 0; track < num_tracks; track++) {
240 if (mediademuxer_select_track(demuxer, track)) {
241 g_print("mediademuxer_select_track index [%d] failed\n", track);
244 g_print("select track index is [%d] of the total track [%d]\n", track, num_tracks);
249 int test_mediademuxer_start()
252 g_print("test_mediademuxer_start\n");
253 ret = mediademuxer_start(demuxer);
257 int test_mediademuxer_get_track_info()
262 g_print("test_mediademuxer_get_track_info\n");
263 for (; track < num_tracks; track++) {
264 media_format_h g_media_format;
265 ret = mediademuxer_get_track_info(demuxer, track, &g_media_format);
267 if (media_format_get_video_info(g_media_format, &v_mime,
268 &w, &h, NULL, NULL) == MEDIA_FORMAT_ERROR_NONE) {
269 g_print("media_format_get_video_info is success!\n");
270 g_print("\t\t[media_format_get_video]mime:%x, width :%d, height :%d\n",
273 } else if (media_format_get_audio_info(g_media_format, &a_mime,
274 &channel, &samplerate, &bit, NULL) == MEDIA_FORMAT_ERROR_NONE) {
275 g_print("media_format_get_audio_info is success!\n");
276 g_print("\t\t[media_format_get_audio]mime:%x, channel :%d, samplerate :%d, bit :%d\n",
277 a_mime, channel, samplerate, bit);
278 if (a_mime == MEDIA_FORMAT_AAC_LC) {
279 media_format_get_audio_aac_type(g_media_format, &is_adts);
280 g_print("\t\t[media_format_get_audio]is_adts:%d", is_adts);
283 } else if (media_format_get_text_info(g_media_format, &t_mime, &t_type) == MEDIA_FORMAT_ERROR_NONE) {
284 g_print("media_format_get_text_info is success!\n");
285 g_print("\t\t[media_format_get_text]mime:%x, type:%x\n", t_mime, t_type);
288 g_print("Not Supported YET\n");
290 media_format_unref(g_media_format);
291 g_media_format = NULL;
293 g_print("Error while getting mediademuxer_get_track_info\n");
297 #if DEMUXER_OUTPUT_DUMP
298 if ((a_mime == MEDIA_FORMAT_AAC_LC) && (is_adts == 0)) {
299 g_print("MIME : MEDIA_FORMAT_AAC_LC ------Need to add header for dump test \n");
300 } else if (a_mime == MEDIA_FORMAT_AMR_NB) {
301 g_print("MIME : MEDIA_FORMAT_AMR_NB ------Need to add header for dump test \n");
302 write_amrnb_header = 1;
303 } else if (a_mime == MEDIA_FORMAT_AMR_WB) {
304 g_print("MIME : MEDIA_FORMAT_AMR_WB ------Need to add header for dump test \n");
305 write_amrwb_header = 1;
307 g_print("--------------------------- Don't Need to add header for dump test\n");
313 #ifdef USE_MEDIA_CODEC
314 static void mediacodec_finish(mediacodec_h handle, FILE *fp)
318 mediacodec_unset_output_buffer_available_cb(handle);
319 err = mediacodec_unprepare(handle);
320 if (err != MEDIACODEC_ERROR_NONE) {
321 g_print("mediacodec_unprepare failed error = %d \n", err);
324 err = mediacodec_destroy(handle);
325 if (err != MEDIACODEC_ERROR_NONE)
326 g_print("mediacodec_destroy failed error = %d \n", err);
330 static void _mediacodec_fill_audio_buffer_cb(media_packet_h pkt, void *user_data)
333 uint64_t buf_size = 0;
335 media_packet_h output_buf;
338 err = mediacodec_get_output(g_media_codec, &output_buf, 0);
339 if (err == MEDIACODEC_ERROR_NONE) {
340 media_packet_get_buffer_size(output_buf, &buf_size);
341 media_packet_get_buffer_data_ptr(output_buf, &data);
343 fwrite(data, 1, buf_size, fp_out_codec_audio);
345 g_print("Data is null inside _mediacodec_fill_audio_buffer_cb\n");
347 media_packet_unref(output_buf);
349 g_print("mediacodec_get_output failed inside _mediacodec_fill_audio_buffer_cb err = %d\n", err);
353 g_print("audio pkt from mediacodec is null\n");
358 static void mediacodec_init_audio(int codecid, int flag, int samplerate, int channel, int bit)
360 /* This file will be used to dump the audio data coming out from mediacodec */
361 fp_out_codec_audio = fopen("/opt/usr/codec_dump_audio.out", "wb");
362 g_print("Create dumped file as codec_dump_audio.out\n");
364 if (g_media_codec != NULL) {
365 mediacodec_unprepare(g_media_codec);
366 mediacodec_destroy(g_media_codec);
367 g_media_codec = NULL;
369 if (mediacodec_create(&g_media_codec) != MEDIACODEC_ERROR_NONE) {
370 g_print("mediacodec_create is failed\n");
373 /* Now set the code info */
374 if ((mediacodec_set_codec(g_media_codec, (mediacodec_codec_type_e)codecid,
375 (mediacodec_support_type_e)flag) != MEDIACODEC_ERROR_NONE)) {
376 g_print("mediacodec_set_codec is failed\n");
379 /* set the audio dec info */
380 if ((mediacodec_set_adec_info(g_media_codec, samplerate, channel, bit)) != MEDIACODEC_ERROR_NONE) {
381 g_print("mediacodec_set_adec is failed\n");
384 /* Set the callback for output data, which will be used to write the data to file */
385 mediacodec_set_output_buffer_available_cb(g_media_codec,
386 _mediacodec_fill_audio_buffer_cb,
389 if (MEDIACODEC_ERROR_NONE != mediacodec_prepare(g_media_codec)) {
390 g_print("mediacodec prepare is failed\n");
395 static void mediacodec_process_audio_pkt(media_packet_h in_buf)
397 if (g_media_codec != NULL) {
398 /* process the media packet */
399 if (MEDIACODEC_ERROR_NONE != mediacodec_process_input(g_media_codec, in_buf, 0)) {
400 g_print("mediacodec_process_input is failed inside mediacodec_process_audio_pkt\n");
406 static void _mediacodec_fill_video_buffer_cb(media_packet_h pkt, void *user_data)
409 uint64_t buf_size = 0;
411 media_packet_h output_buf;
414 err = mediacodec_get_output(g_media_codec_1, &output_buf, 0);
415 if (err == MEDIACODEC_ERROR_NONE) {
416 media_packet_get_buffer_size(output_buf, &buf_size);
417 /* g_print("%s - output_buf size = %lld\n", __func__, buf_size); */
418 media_packet_get_buffer_data_ptr(output_buf, &data);
420 fwrite(data, 1, buf_size, fp_out_codec_video);
422 g_print("Data is null inside _mediacodec_fill_video_buffer_cb\n");
423 media_packet_unref(output_buf);
425 g_print("mediacodec_get_output failed inside _mediacodec_fill_video_buffer_cb err = %d\n", err);
429 g_print("video pkt from mediacodec is null\n");
434 static void mediacodec_init_video(int codecid, int flag, int width, int height)
436 /* This file will be used to dump the data */
437 fp_out_codec_video = fopen("/opt/usr/codec_dump_video.out", "wb");
438 g_print("Create dumped file as codec_dump_video.out\n");
440 if (g_media_codec_1 != NULL) {
441 mediacodec_unprepare(g_media_codec_1);
442 mediacodec_destroy(g_media_codec_1);
443 g_media_codec_1 = NULL;
445 if (mediacodec_create(&g_media_codec_1) != MEDIACODEC_ERROR_NONE) {
446 g_print("mediacodec_create is failed\n");
449 /* Now set the code info */
450 if ((mediacodec_set_codec(g_media_codec_1, (mediacodec_codec_type_e)codecid,
451 (mediacodec_support_type_e)flag) != MEDIACODEC_ERROR_NONE)) {
452 g_print("mediacodec_set_codec is failed\n");
455 /* set the video dec info */
456 if ((mediacodec_set_vdec_info(g_media_codec_1, width, height)) != MEDIACODEC_ERROR_NONE) {
457 g_print("mediacodec_set_vdec is failed\n");
460 /* Set the callback for output data, which will be used to write the data to file */
461 mediacodec_set_output_buffer_available_cb(g_media_codec_1,
462 _mediacodec_fill_video_buffer_cb,
465 if (MEDIACODEC_ERROR_NONE != mediacodec_prepare(g_media_codec_1)) {
466 g_print("mediacodec_prepare is failed\n");
471 static void mediacodec_process_video_pkt(media_packet_h in_buf)
473 if (g_media_codec_1 != NULL) {
474 /* process the media packet */
475 if (MEDIACODEC_ERROR_NONE != mediacodec_process_input(g_media_codec_1, in_buf, 0)) {
476 g_print("mediacodec process input is failed inside mediacodec_process_video_pkt\n");
480 g_print("mediacodec handle is invalid inside mediacodec_process_video_pkt()\n");
486 void *_fetch_audio_data(void *ptr)
488 int ret = MEDIADEMUXER_ERROR_NONE;
489 int *status = (int *)g_malloc(sizeof(int));
490 media_packet_h audbuf;
492 uint64_t buf_size = 0;
496 g_print("Audio Data function\n");
498 #ifdef USE_MEDIA_CODEC
499 if (validate_with_codec) {
501 if (a_mime == MEDIA_FORMAT_AAC_LC || a_mime == MEDIA_FORMAT_AAC_HE ||
502 a_mime == MEDIA_FORMAT_AAC_HE_PS) {
504 g_print("mediacodec_init_audio() for MEDIACODEC_AAC\n");
505 mediacodec_init_audio(MEDIACODEC_AAC, flag, samplerate, channel, bit);
506 } else if (a_mime == MEDIA_FORMAT_MP3) {
508 g_print("mediacodec_init_audio() for MEDIACODEC_MP3\n");
509 mediacodec_init_audio(MEDIACODEC_MP3, flag, samplerate, channel, bit);
510 } else if (a_mime == MEDIA_FORMAT_AMR_NB) {
512 g_print("mediacodec_init_audio() for MEDIACODEC_AMR_NB\n");
513 mediacodec_init_audio(MEDIACODEC_AMR_NB, flag, samplerate, channel, bit);
514 } else if (a_mime == MEDIA_FORMAT_AMR_WB) {
516 g_print("mediacodec_init_audio() for MEDIACODEC_AMR_WB\n");
517 mediacodec_init_audio(MEDIACODEC_AMR_WB, flag, samplerate, channel, bit);
518 } else if (a_mime == MEDIA_FORMAT_FLAC) {
520 g_print("mediacodec_init_audio() for MEDIACODEC_FLAC\n");
521 mediacodec_init_audio(MEDIACODEC_FLAC, flag, samplerate, channel, bit);
522 } else if (a_mime == MEDIA_FORMAT_VORBIS) {
524 g_print("mediacodec_init_audio() for MEDIACODEC_VORBIS\n");
525 mediacodec_init_audio(MEDIACODEC_VORBIS, flag, samplerate, channel, bit);
527 g_print("Not Supported YET- Need to add mime for validating with audio codec\n");
528 return (void *)status;
533 ret = mediademuxer_read_sample(demuxer, aud_track, &audbuf);
534 if (ret != MEDIADEMUXER_ERROR_NONE) {
535 g_print("Error (%d) return of mediademuxer_read_sample()\n", ret);
541 media_packet_get_buffer_size(audbuf, &buf_size);
542 media_packet_get_buffer_data_ptr(audbuf, &data);
543 g_print("Audio Read Count::[%4d] frame - get_buffer_size = %"PRIu64"\n", count, buf_size);
545 #if DEMUXER_OUTPUT_DUMP
547 if ((a_mime == MEDIA_FORMAT_AAC_LC) && (is_adts == 0)) {
548 /* This is used only AAC raw case for adding each ADTS frame header */
549 generate_header_aac_adts(buf_adts, (buf_size+ADTS_HEADER_SIZE));
550 fwrite(&buf_adts[0], 1, ADTS_HEADER_SIZE, fp_audio_out);
551 } else if ((a_mime == MEDIA_FORMAT_AMR_NB) && (write_amrnb_header == 1)) {
552 /* This is used only AMR-NB case for adding magic header in only first frame */
553 g_print("%s - AMRNB_HDR write in first frame\n", __func__);
554 fwrite(&AMRNB_HDR[0], 1, sizeof(AMRNB_HDR) - 1, fp_audio_out);
555 write_amrnb_header = 0;
556 } else if ((a_mime == MEDIA_FORMAT_AMR_WB) && (write_amrwb_header == 1)) {
557 /* This is used only AMR-WB case for adding magic header in only first frame */
558 g_print("%s - AMRWB_HDR write in first frame\n", __func__);
559 fwrite(&AMRWB_HDR[0], 1, sizeof(AMRWB_HDR) - 1, fp_audio_out);
560 write_amrwb_header = 0;
564 fwrite(data, 1, buf_size, fp_audio_out);
566 g_print("DUMP : write(audio data) fail for NULL\n");
570 #ifdef USE_MEDIA_CODEC
571 if (validate_with_codec)
572 mediacodec_process_audio_pkt(audbuf);
575 media_packet_unref(audbuf);
578 g_print("EOS return of mediademuxer_read_sample() for audio\n");
580 #ifdef USE_MEDIA_CODEC
581 if (validate_with_codec)
582 mediacodec_finish(g_media_codec, fp_out_codec_audio);
584 return (void *)status;
588 static void _local_media_packet_get_codec_data(media_packet_h pkt)
590 unsigned char* get_codec_data;
591 unsigned int get_codec_data_size;
593 if (media_packet_get_codec_data(pkt, (void**) &get_codec_data, &get_codec_data_size) == MEDIA_PACKET_ERROR_NONE) {
594 g_print("media_packet_get_codec_data is success ... !\n");
595 g_print("codec_data_size = %u\n", get_codec_data_size);
596 get_codec_data[get_codec_data_size] = '\0';
597 if (get_codec_data_size == 0)
599 g_print("media packet codec_data is [%s] \n", get_codec_data);
601 g_print("media_packet_get_codec_data is failed...\n");
606 void *_fetch_video_data(void *ptr)
608 int ret = MEDIADEMUXER_ERROR_NONE;
609 int *status = (int *)g_malloc(sizeof(int));
610 media_packet_h vidbuf;
612 uint64_t buf_size = 0;
616 g_print("Video Data function\n");
618 #ifdef USE_MEDIA_CODEC
619 if (validate_with_codec) {
621 if (v_mime == MEDIA_FORMAT_H264_SP || v_mime == MEDIA_FORMAT_H264_MP ||
622 v_mime == MEDIA_FORMAT_H264_HP) {
624 g_print("mediacodec_init_video() for MEDIACODEC_H264\n");
625 mediacodec_init_video(MEDIACODEC_H264, flag, w, h);
626 } else if (v_mime == MEDIA_FORMAT_H263) {
627 g_print("mediacodec_init_video() for MEDIACODEC_H263\n");
629 mediacodec_init_video(MEDIACODEC_H263, flag, w, h);
631 g_print("Not Supported YET- Need to add mime for validating with video codec\n");
632 return (void *)status;
637 ret = mediademuxer_read_sample(demuxer, vid_track, &vidbuf);
638 if (ret != MEDIADEMUXER_ERROR_NONE) {
639 g_print("Error (%d) return of mediademuxer_read_sample()\n", ret);
645 media_packet_get_buffer_size(vidbuf, &buf_size);
646 media_packet_get_buffer_data_ptr(vidbuf, &data);
647 g_print("Video Read Count::[%4d] frame - get_buffer_size = %"PRIu64"\n", count, buf_size);
649 /* This is used for debugging purpose */
650 _local_media_packet_get_codec_data(vidbuf);
652 #if DEMUXER_OUTPUT_DUMP
655 fwrite(data, 1, buf_size, fp_video_out);
657 g_print("DUMP : write(video data) fail for NULL\n");
661 #ifdef USE_MEDIA_CODEC
662 if (validate_with_codec)
663 mediacodec_process_video_pkt(vidbuf);
666 media_packet_unref(vidbuf);
668 g_print("EOS return of mediademuxer_read_sample() for video\n");
671 #ifdef USE_MEDIA_CODEC
672 if (validate_with_codec)
673 mediacodec_finish(g_media_codec_1, fp_out_codec_video);
675 return (void *)status;
678 void *_fetch_text_data(void *ptr)
680 int ret = MEDIADEMUXER_ERROR_NONE;
681 int *status = (int *)g_malloc(sizeof(int));
682 media_packet_h txtbuf;
684 uint64_t buf_size = 0;
688 g_print("text Data function\n");
689 g_print("Text track number is :%d\n", txt_track);
691 ret = mediademuxer_read_sample(demuxer, txt_track, &txtbuf);
692 if (ret != MEDIADEMUXER_ERROR_NONE) {
693 g_print("Error (%d) return of mediademuxer_read_sample()\n", ret);
699 media_packet_get_buffer_size(txtbuf, &buf_size);
700 media_packet_get_buffer_data_ptr(txtbuf, &data);
701 g_print("Text Read Count::[%4d] frame - get_buffer_size = %"PRIu64"\n", count, buf_size);
702 #if DEMUXER_OUTPUT_DUMP
705 fwrite(data, 1, buf_size, fp_text_out);
707 g_print("DUMP : write(text data) fail for NULL\n");
711 g_print("EOS return of mediademuxer_read_sample()\n");
713 return (void *)status;
716 int test_mediademuxer_read_sample()
720 /* Initialize and set thread detached attribute */
721 pthread_attr_init(&attr);
722 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
723 if (vid_track != -1) {
724 g_print("In main: creating thread for video\n");
725 pthread_create(&thread[0], &attr, _fetch_video_data, NULL);
727 if (aud_track != -1) {
728 g_print("In main: creating thread for audio\n");
729 pthread_create(&thread[1], &attr, _fetch_audio_data, NULL);
731 if (txt_track != -1) {
732 g_print("In main: creating thread for text\n");
733 pthread_create(&thread[2], &attr, _fetch_text_data, NULL);
735 pthread_attr_destroy(&attr);
739 int test_mediademuxer_seek_to()
741 g_print("test_mediademuxer_seek_to\n");
743 mediademuxer_seek(demuxer, pos);
744 g_print("Number of tracks [%d]\n", num_tracks);
748 int test_mediademuxer_unselect_track()
750 g_print("test_mediademuxer_select_track\n");
751 if (mediademuxer_unselect_track(demuxer, 0)) {
752 g_print("mediademuxer_select track 0 failed\n");
758 int test_mediademuxer_stop()
761 g_print("test_mediademuxer_stop\n");
762 ret = mediademuxer_stop(demuxer);
766 int test_mediademuxer_unprepare()
769 g_print("test_mediademuxer_unprepare\n");
770 ret = mediademuxer_unprepare(demuxer);
774 int test_mediademuxer_destroy()
777 g_print("test_mediademuxer_destroy\n");
778 ret = mediademuxer_destroy(demuxer);
781 #if DEMUXER_OUTPUT_DUMP
783 fclose(fp_audio_out);
785 fclose(fp_video_out);
790 int test_mediademuxer_get_state()
792 g_print("test_mediademuxer_get_state\n");
793 mediademuxer_state state;
794 if (mediademuxer_get_state(demuxer, &state) == MEDIADEMUXER_ERROR_NONE) {
795 if (state == MEDIADEMUXER_STATE_NONE)
796 g_print("Mediademuxer_state = NONE\n");
797 else if (state == MEDIADEMUXER_STATE_IDLE)
798 g_print("Mediademuxer_state = IDLE\n");
799 else if (state == MEDIADEMUXER_STATE_READY)
800 g_print("Mediademuxer_state = READY\n");
801 else if (state == MEDIADEMUXER_STATE_DEMUXING)
802 g_print("Mediademuxer_state = DEMUXING\n");
804 g_print("Mediademuxer_state = NOT SUPPORT STATE\n");
806 g_print("Mediademuxer_state call failed\n");
811 void app_err_cb(mediademuxer_error_e error, void *user_data)
813 printf("Got Error %d from Mediademuxer\n", error);
816 void app_eos_cb(int track_index, void *user_data)
818 printf("Got EOS for track -- %d from Mediademuxer\n", track_index);
819 if (track_index == vid_track)
820 vid_eos_track = true;
821 else if (track_index == aud_track)
822 aud_eos_track = true;
823 else if (track_index == txt_track)
824 text_eos_track = true;
826 g_print("EOS for invalid track number\n");
829 int test_mediademuxer_set_error_cb()
832 g_print("test_mediademuxer_set_error_cb\n");
833 ret = mediademuxer_set_error_cb(demuxer, app_err_cb, demuxer);
837 int test_mediademuxer_set_eos_cb()
840 g_print("test_mediademuxer_set_eos_cb\n");
841 ret = mediademuxer_set_eos_cb(demuxer, app_eos_cb, demuxer);
845 /*-----------------------------------------------------------------------
847 -----------------------------------------------------------------------*/
848 int test_mediademuxer_get_sample_track_index()
850 g_print("test_mediademuxer_get_sample_track_index\n");
854 int test_mediademuxer_get_sample_track_time()
856 g_print("test_mediademuxer_get_sample_track_time\n");
860 int test_mediademuxer_advance()
862 g_print("test_mediademuxer_advance\n");
866 int test_mediademuxer_is_key_frame()
868 g_print("test_mediademuxer_is_key_frame\n");
872 int test_mediademuxer_is_encrypted()
874 g_print("test_mediademuxer_is_encrypted\n");
878 int test_mediademuxer_audio_only()
880 g_print("AUDIO ONLY\n");
884 int test_mediademuxer_video_only()
886 g_print("VIDEO ONLY\n");
890 int test_mediademuxer_audio_video_only()
892 g_print("AUDIO & VIDEO ONLY\n");
896 /*-----------------------------------------------------------------------
898 -----------------------------------------------------------------------*/
899 static void display_sub_basic()
902 g_print("===========================================================================\n");
903 g_print(" media demuxer test\n");
904 g_print(" SELECT : c -> (s) -> e -> p -> (goto submenu) -> d -> q \n");
905 g_print("---------------------------------------------------------------------------\n");
906 g_print("c. Create \t");
907 g_print("s. Set callback \t");
908 g_print("e. Set eos callback \t");
909 g_print("p. Path \t");
910 g_print("d. Destroy \t");
911 g_print("q. Quit \n");
912 g_print("---------------------------------------------------------------------------\n");
913 #ifdef USE_MEDIA_CODEC
914 if (validate_with_codec)
915 g_print("[Validation with Media codec]\n");
918 g_print("[validation as stand alone. To validate with media codec, run mediademuxer_test with -c option]\n");
921 void _interpret_main_menu(char *cmd)
923 int len = strlen(cmd);
925 if (strncmp(cmd, "c", 1) == 0) {
926 test_mediademuxer_create();
927 } else if (strncmp(cmd, "s", 1) == 0) {
928 test_mediademuxer_set_error_cb();
929 } else if (strncmp(cmd, "e", 1) == 0) {
930 test_mediademuxer_set_eos_cb();
931 } else if (strncmp(cmd, "p", 1) == 0) {
932 g_menu_state = CURRENT_STATUS_FILENAME;
933 } else if (strncmp(cmd, "d", 1) == 0) {
934 test_mediademuxer_unprepare();
935 test_mediademuxer_destroy();
936 } else if (strncmp(cmd, "q", 1) == 0) {
939 g_print("unknown menu \n");
945 static void displaymenu(void)
947 if (g_menu_state == CURRENT_STATUS_MAINMENU) {
949 } else if (g_menu_state == CURRENT_STATUS_FILENAME) {
950 g_print("*** input media path.\n");
951 } else if (g_menu_state == CURRENT_STATUS_SET_DATA) {
953 g_print("=============================================================\n");
954 g_print(" media demuxer submenu\n");
955 g_print(" SELECT from 1 to 9, others are option \n");
956 g_print("-------------------------------------------------------------\n");
957 g_print(" 1. Get Track\t");
958 g_print(" 2. Select Track\n");
959 g_print(" 3. Start\t");
960 g_print(" 4. Get Info\n");
961 g_print(" 5. Read Sample\n");
962 g_print(" 6. Stop\t");
963 g_print(" 7. Unprepare\t");
964 g_print(" 8. Get state\n");
965 g_print(" 9. Go Back to main menu\n");
966 g_print(" a. Seek\t");
967 g_print(" b. Uselect Track\n");
968 g_print(" c. Get Sample Track Index\t");
969 g_print(" d. Get Sample Track Time\n");
970 g_print(" e. Advance\t");
971 g_print(" f. Is Key Frame\t");
972 g_print(" g. Is Key encrypted\n");
973 g_print("-------------------------------------------------------------\n");
975 g_print("*** unknown status.\n");
981 void reset_menu_state()
983 g_menu_state = CURRENT_STATUS_MAINMENU;
987 gboolean timeout_menu_display(void *data)
993 static void interpret(char *cmd)
995 switch (g_menu_state) {
996 case CURRENT_STATUS_MAINMENU: {
997 _interpret_main_menu(cmd);
1000 case CURRENT_STATUS_FILENAME: {
1002 ret = test_mediademuxer_set_data_source(demuxer, cmd);
1003 if (ret == MEDIADEMUXER_ERROR_NONE) {
1004 ret = test_mediademuxer_prepare();
1005 if (ret == MEDIADEMUXER_ERROR_NONE) {
1006 g_menu_state = CURRENT_STATUS_SET_DATA;
1008 g_print("test_mediademuxer_prepare failed \n");
1009 if (ret == MEDIADEMUXER_ERROR_INVALID_OPERATION)
1010 g_menu_state = CURRENT_STATUS_MAINMENU;
1012 g_menu_state = CURRENT_STATUS_FILENAME;
1015 if (ret == MEDIADEMUXER_ERROR_INVALID_PATH)
1016 g_print("Invalid path, file does not exist\n");
1017 g_menu_state = CURRENT_STATUS_FILENAME;
1021 case CURRENT_STATUS_SET_DATA: {
1022 int len = strlen(cmd);
1024 if (strncmp(cmd, "1", len) == 0)
1025 test_mediademuxer_get_track_count();
1026 else if (strncmp(cmd, "2", len) == 0)
1027 test_mediademuxer_select_track();
1028 else if (strncmp(cmd, "3", len) == 0)
1029 test_mediademuxer_start();
1030 else if (strncmp(cmd, "4", len) == 0)
1031 test_mediademuxer_get_track_info();
1032 else if (strncmp(cmd, "5", len) == 0)
1033 test_mediademuxer_read_sample();
1034 else if (strncmp(cmd, "6", len) == 0)
1035 test_mediademuxer_stop();
1036 else if (strncmp(cmd, "7", len) == 0)
1037 test_mediademuxer_unprepare();
1038 else if (strncmp(cmd, "8", len) == 0)
1039 test_mediademuxer_get_state();
1040 else if (strncmp(cmd, "9", len) == 0)
1042 else if (strncmp(cmd, "a", len) == 0)
1043 test_mediademuxer_seek_to();
1044 else if (strncmp(cmd, "b", len) == 0)
1045 test_mediademuxer_unselect_track();
1046 else if (strncmp(cmd, "c", len) == 0)
1047 test_mediademuxer_get_sample_track_index();
1048 else if (strncmp(cmd, "d", len) == 0)
1049 test_mediademuxer_get_sample_track_time();
1050 else if (strncmp(cmd, "e", len) == 0)
1051 test_mediademuxer_advance();
1052 else if (strncmp(cmd, "f", len) == 0)
1053 test_mediademuxer_is_key_frame();
1054 else if (strncmp(cmd, "g", len) == 0)
1055 test_mediademuxer_is_encrypted();
1057 g_print("UNKNOW COMMAND\n");
1059 g_print("UNKNOW COMMAND\n");
1066 g_timeout_add(100, timeout_menu_display, 0);
1070 * This function is to execute command.
1072 * @param channel [in] 1st parameter
1074 * @return This function returns TRUE/FALSE
1078 gboolean input(GIOChannel *channel)
1080 gchar buf[MAX_STRING_LEN];
1082 GError *error = NULL;
1083 g_io_channel_read_chars(channel, buf, MAX_STRING_LEN, &read, &error);
1091 * This function is the example main function for mediademuxer API.
1095 * @return This function returns 0.
1097 * @see other functions
1099 int main(int argc, char *argv[])
1101 GIOChannel *stdin_channel;
1102 GMainLoop *loop = g_main_loop_new(NULL, 0);
1103 stdin_channel = g_io_channel_unix_new(0);
1104 g_io_channel_set_flags(stdin_channel, G_IO_FLAG_NONBLOCK, NULL);
1105 g_io_add_watch(stdin_channel, G_IO_IN, (GIOFunc) input, NULL);
1108 /* Check whether validation with media codec is required */
1109 if (argv[1][0] == '-' && argv[1][1] == 'c')
1110 validate_with_codec = true;
1114 g_print("RUN main loop\n");
1115 g_main_loop_run(loop);
1116 g_print("STOP main loop\n");
1118 g_main_loop_unref(loop);