From babaf3674c3c5333089be4b48ff6ad3496887753 Mon Sep 17 00:00:00 2001 From: Mahesh Kondiparthi Date: Wed, 13 Jan 2016 12:00:32 +0530 Subject: [PATCH] Graceful exit when wrong file is fed to muxer. Added null check to output_uri in gst_add_track to return if set_data_sink fails. Modify mediamuxer to return an error if the media_format during add_track is not matching with media_format of the write_sample packet Update mediamuxer test-suite to return an error if inappropriate A/V codecs were fed. Hence, user is restricted to feed a container-file with same encoded-media as what he choose during 'o' Addressed review comments Change-Id: Id8f4fe3637564e9957084f77dab5f6d856b4b2f6 Signed-off-by: Mahesh Kondiparthi --- src/port_gst/mediamuxer_port_gst.c | 53 +++++++++++++++++++++++++++++++++++++- test/mediamuxer_test.c | 20 ++++++++++---- test/mediamuxer_test_gst.c | 35 +++++++++++++++++-------- 3 files changed, 91 insertions(+), 17 deletions(-) diff --git a/src/port_gst/mediamuxer_port_gst.c b/src/port_gst/mediamuxer_port_gst.c index a5e10f5..fd712a0 100644 --- a/src/port_gst/mediamuxer_port_gst.c +++ b/src/port_gst/mediamuxer_port_gst.c @@ -136,6 +136,11 @@ static int gst_muxer_add_track(MMHandleType pHandle, mx_gst_track *current = NULL; mx_gst_track *last = NULL; + if (!mx_handle_gst->output_uri) { + MX_E("URI is null. Possibly, set_data_sink failed. returning. \n"); + return MX_ERROR_INVALID_ARGUMENT; + } + current = (mx_gst_track *)g_malloc(sizeof(mx_gst_track)); if (!current) { MX_E("Not able to allocate memory\n"); @@ -465,6 +470,8 @@ mx_ret_e _gst_create_pipeline(mxgst_handle_t *gst_handle) current->parser = gst_element_factory_make("mpeg4videoparse", str_parser); } else { MX_E("Can't retrive mimetype for the current track. Unsupported MIME Type\n"); + ret = MEDIAMUXER_ERROR_INVALID_PARAMETER; + goto ERROR; } if ((!current->appsrc) || (!current->parser)) { @@ -783,6 +790,8 @@ int _gst_set_caps(MMHandleType pHandle, media_packet_h packet, int track_index) char *codec_data; unsigned int codec_data_size; mx_gst_track *current = NULL; + media_format_mimetype_e track_mime; + media_format_mimetype_e current_mime; /* Reach that track index and set the codec data */ for (current = gst_handle->track_info.track_head; current; current = current->next) @@ -811,6 +820,25 @@ int _gst_set_caps(MMHandleType pHandle, media_packet_h packet, int track_index) MX_E("\n\nThis is not an audio track_index. Track_index is not in 3*n+1 format\n\n"); goto ERROR; } + + /* return if track_mime is different to current_mime */ + if (media_format_get_audio_info((media_format_h)(current->media_format), &track_mime, NULL, NULL, NULL, NULL) + == MEDIA_FORMAT_ERROR_NONE) { + if (media_format_get_audio_info((media_format_h)(format), ¤t_mime, NULL, NULL, NULL, NULL) + == MEDIA_FORMAT_ERROR_NONE) { + if (track_mime != current_mime) { + MX_E("audio track_mime is not matching with packet mime. returning"); + return MX_ERROR_INVALID_ARGUMENT; + } + } else { + MX_E("cant read audio mime in packet. returning\n"); + return MX_ERROR_INVALID_ARGUMENT; + } + } else { + MX_E("cant read audio mime, set during add_track. returning\n"); + return MX_ERROR_INVALID_ARGUMENT; + } + if (media_packet_get_extra(packet, (void **)&codec_data)) { MX_E("media_packet_get_extra call failed\n"); @@ -890,6 +918,25 @@ int _gst_set_caps(MMHandleType pHandle, media_packet_h packet, int track_index) MX_E("\n\nThis is not an video track_index. Video track_index is not in 3*n format\n\n"); goto ERROR; } + + /* return if track_mime is different to current_mime */ + if (media_format_get_video_info((media_format_h)(current->media_format), &track_mime, NULL, NULL, NULL, NULL) + == MEDIA_FORMAT_ERROR_NONE) { + if (media_format_get_audio_info((media_format_h)(format), ¤t_mime, NULL, NULL, NULL, NULL) + == MEDIA_FORMAT_ERROR_NONE) { + if (track_mime != current_mime) { + MX_E("video track_mime is not matching with packet mime. returning"); + return MX_ERROR_INVALID_ARGUMENT; + } + } else { + MX_E("cant read video mime. returning\n"); + return MX_ERROR_INVALID_ARGUMENT; + } + } else { + MX_E("cant read video mime in packet. returning\n"); + return MX_ERROR_INVALID_ARGUMENT; + } + if (media_packet_get_extra(packet, (void **)&codec_data)) { MX_E("media_packet_get_extra call failed\n"); @@ -1083,7 +1130,11 @@ static int gst_muxer_write_sample(MMHandleType pHandle, int track_index, goto ERROR; } - _gst_set_caps(pHandle, inbuf, track_index); + if (_gst_set_caps(pHandle, inbuf, track_index) != MX_ERROR_NONE) { + ret = MX_ERROR_INVALID_ARGUMENT; + goto ERROR; + } + MX_I("Track_index passed = %d, working-with_track_index = %d\n", track_index, current->track_index); GstBuffer *gst_inbuf2 = NULL; diff --git a/test/mediamuxer_test.c b/test/mediamuxer_test.c index e522b95..747c34b 100644 --- a/test/mediamuxer_test.c +++ b/test/mediamuxer_test.c @@ -157,6 +157,9 @@ int test_mediamuxer_set_data_sink() } else if (strncmp(data_sink, "42", 2) == 0) { op_uri = "MuxTest_wb.amr"; ret = mediamuxer_set_data_sink(myMuxer, op_uri, MEDIAMUXER_CONTAINER_FORMAT_AMR_WB); + } else { + g_print("Invalid option choosen. Only the displayed options are valid\n"); + ret = MEDIAMUXER_ERROR_INVALID_PARAMETER; } g_print("\nFile will be saved to: %s\n", op_uri); @@ -417,13 +420,20 @@ void _interpret_main_menu(char *cmd) test_mediamuxer_add_track_audio(); } else if (strncmp(cmd, "v", 1) == 0) { if (!validate_with_codec) { - have_vid_track = true; - if (have_mp4 == false) { - g_menu_state = CURRENT_STATUS_MP4_FILENAME; - have_mp4 = true; + if ((strncmp(data_sink, "11", 2) == 0 || strncmp(data_sink, "12", 2) == 0 || strncmp(data_sink, "13", 2) == 0 + || strncmp(data_sink, "21", 2) == 0 || strncmp(data_sink, "22", 2) == 0)) { + have_vid_track = true; + if (have_mp4 == false) { + g_menu_state = CURRENT_STATUS_MP4_FILENAME; + have_mp4 = true; + } } } - test_mediamuxer_add_track_video(); + if ((strncmp(data_sink, "11", 2) == 0 || strncmp(data_sink, "12", 2) == 0 || strncmp(data_sink, "13", 2) == 0 + || strncmp(data_sink, "21", 2) == 0 || strncmp(data_sink, "22", 2) == 0)) + test_mediamuxer_add_track_video(); + else + g_print("Ignoring, data_sink=%s doesnt need video track testing", data_sink); } else if (strncmp(cmd, "m", 1) == 0) { test_mediamuxer_write_sample(); } else if (strncmp(cmd, "t", 1) == 0) { diff --git a/test/mediamuxer_test_gst.c b/test/mediamuxer_test_gst.c index d4e8269..c342c33 100644 --- a/test/mediamuxer_test_gst.c +++ b/test/mediamuxer_test_gst.c @@ -105,24 +105,32 @@ static void __audio_app_sink_callback(GstElement *sink, CustomData *data) return; } - if (g_str_has_prefix(new_pad_type_aud, "audio/mpeg")) { + g_print("audio data_sink = %s\n", data_sink); + /* check if the mime selected during set_data_sink is matching with the mime of the file inputted.*/ + if (g_str_has_prefix(new_pad_type_aud, "audio/mpeg") + && (strncmp(data_sink, "11", 2) == 0 || strncmp(data_sink, "12", 2) == 0 || strncmp(data_sink, "13", 2) == 0 + || strncmp(data_sink, "21", 2) == 0 || strncmp(data_sink, "22", 2) == 0)) { if (media_format_set_audio_mime(audfmt, MEDIA_FORMAT_AAC_LC)) { g_print("media_format_set_audio_mime failed\n"); return; } - } else if (g_str_has_prefix(new_pad_type_aud, "audio/AMR-WB")) { + } else if (g_str_has_prefix(new_pad_type_aud, "audio/AMR-WB") + && strncmp(data_sink, "42", 2) == 0) { g_print("For amr-wb, setting encoded media type as MEDIA_FORMAT_AMR_WB\n"); if (media_format_set_audio_mime(audfmt, MEDIA_FORMAT_AMR_WB)) { g_print("media_format_set_audio_mime failed\n"); return; } - } else if (g_str_has_prefix(new_pad_type_aud, "audio/AMR")) { + } else if (g_str_has_prefix(new_pad_type_aud, "audio/AMR") + && (strncmp(data_sink, "23", 2) == 0 || strncmp(data_sink, "41", 2) == 0)) { g_print("For amr-nb, setting encoded media type as MEDIA_FORMAT_AMR_NB\n"); if (media_format_set_audio_mime(audfmt, MEDIA_FORMAT_AMR_NB)) { g_print("media_format_set_audio_mime failed\n"); return; } - } else if (g_str_has_prefix(new_pad_type_aud, "audio/x-wav")) { g_print("creating audio-wav\n"); + } else if (g_str_has_prefix(new_pad_type_aud, "audio/x-wav") + && strncmp(data_sink, "31", 2) == 0) { + g_print("creating audio-wav\n"); if (media_format_set_audio_mime(audfmt, MEDIA_FORMAT_PCM)) { g_print("media_format_set_audio_mime failed\n"); return; @@ -133,7 +141,8 @@ static void __audio_app_sink_callback(GstElement *sink, CustomData *data) g_print("wav media_format_set_audio_channel failed"); if (media_format_set_audio_samplerate(audfmt, 44100)) g_print("wav media_format_set_audio_samplerate failed"); - } + } else + g_print("Unsupported audio mime\n"); if (media_packet_create(audfmt, NULL, NULL, &aud_pkt)) { g_print("create audio media_packet failed\n"); @@ -237,27 +246,31 @@ static void __video_app_sink_callback(GstElement *sink, CustomData *data) g_print("media_format_create failed\n"); return; } + g_print("video data_sink = %s\n", data_sink); - if (g_str_has_prefix(new_pad_type_vid, "video/x-h264")) { + /* check if the mime selected during set_data_sink is matching with the mime of the file inputted.*/ + if (g_str_has_prefix(new_pad_type_vid, "video/x-h264") + && (strncmp(data_sink, "11", 2) == 0 || strncmp(data_sink, "21", 2) == 0 )) { if (media_format_set_video_mime(vidfmt, MEDIA_FORMAT_H264_SP)) { g_print("media_format_set_video_mime to H264_SP failed\n"); return; } - } else if (g_str_has_prefix(new_pad_type_vid, "video/mpeg")) { + } else if (g_str_has_prefix(new_pad_type_vid, "video/mpeg") + && strncmp(data_sink, "13", 2) == 0 ) { g_print("For mpeg4, setting encoded media type as MEDIA_FORMAT_MPEG4_SP\n"); if (media_format_set_video_mime(vidfmt, MEDIA_FORMAT_MPEG4_SP)) { g_print("media_format_set_video_mime to MPEG4_SP failed\n"); return; } - } else if (g_str_has_prefix(new_pad_type_vid, "video/x-h263")) { + } else if (g_str_has_prefix(new_pad_type_vid, "video/x-h263") + && (strncmp(data_sink, "12", 2) == 0 || strncmp(data_sink, "22", 2) == 0 || strncmp(data_sink, "23", 2) == 0)) { g_print("For h263, setting encoded media type as MEDIA_FORMAT_H263\n"); if (media_format_set_video_mime(vidfmt, MEDIA_FORMAT_H263)) { g_print("media_format_set_vidio_mime failed\n"); return; } - } else { - g_print("Unsupported encoded video mime. Currently muxer supports:h263, h264 & mpeg4"); - } + } else + g_print("Unsupported encoded video mime. Currently muxer supports:h263, h264 & mpeg4\n"); if (!GST_BUFFER_FLAG_IS_SET(buffer, GST_BUFFER_FLAG_DELTA_UNIT)) { /* g_print("Key Frame\n"); */ -- 2.7.4