From 7e39dc8b10afd76ab1b40844d971cdcb63e8d843 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Tue, 28 Sep 2010 00:33:06 +0200 Subject: [PATCH] Rework error handling, with a fixed deallocation block at the end of Open(). --- modules/access/sdi.cpp | 158 +++++++++++++++++++++-------------------- 1 file changed, 80 insertions(+), 78 deletions(-) diff --git a/modules/access/sdi.cpp b/modules/access/sdi.cpp index 9c6946e950..098a121982 100644 --- a/modules/access/sdi.cpp +++ b/modules/access/sdi.cpp @@ -255,6 +255,13 @@ static int Open( vlc_object_t *p_this ) { demux_t *p_demux = (demux_t*)p_this; demux_sys_t *p_sys; + int ret = VLC_SUCCESS; + char *psz_aspect; + char *psz_display_mode = NULL; + char *psz_video_connection = NULL; + char *psz_audio_connection = NULL; + bool b_found_mode; + int i_card_index; /* Only when selected */ if( *p_demux->psz_access == '\0' ) @@ -278,14 +285,13 @@ static int Open( vlc_object_t *p_this ) if( !decklink_iterator ) { msg_Err( p_demux, "DeckLink drivers not found." ); - decklink_iterator->Release(); - Close( p_this ); - return VLC_EGENERIC; + ret = VLC_EGENERIC; + goto finish; } HRESULT result; - int i_card_index = var_CreateGetInteger( p_demux, "sdi-card-index" ); + i_card_index = var_CreateGetInteger( p_demux, "sdi-card-index" ); for( int i = 0; i <= i_card_index; ++i ) { if( p_sys->p_card ) @@ -295,13 +301,11 @@ static int Open( vlc_object_t *p_this ) break; } - decklink_iterator->Release(); - if( result != S_OK ) { msg_Err( p_demux, "DeckLink PCI card %d not found", i_card_index ); - Close( p_this ); - return VLC_EGENERIC; + ret = VLC_EGENERIC; + goto finish; } const char *psz_model_name; @@ -310,8 +314,8 @@ static int Open( vlc_object_t *p_this ) if( result != S_OK ) { msg_Err( p_demux, "Could not get model name" ); - Close( p_this ); - return VLC_EGENERIC; + ret = VLC_EGENERIC; + goto finish; } msg_Dbg( p_demux, "Opened DeckLink PCI card %d (%s)", i_card_index, psz_model_name ); @@ -319,8 +323,8 @@ static int Open( vlc_object_t *p_this ) if( p_sys->p_card->QueryInterface( IID_IDeckLinkInput, (void**)&p_sys->p_input) != S_OK ) { msg_Err( p_demux, "Card has no inputs" ); - Close( p_this ); - return VLC_EGENERIC; + ret = VLC_EGENERIC; + goto finish; } /* Set up the video and audio sources. */ @@ -328,100 +332,87 @@ static int Open( vlc_object_t *p_this ) if( p_sys->p_card->QueryInterface( IID_IDeckLinkConfiguration, (void**)&p_config) != S_OK ) { msg_Err( p_demux, "Failed to get configuration interface" ); - Close( p_this ); - return VLC_EGENERIC; + ret = VLC_EGENERIC; + goto finish; } - char *psz_tmp = var_CreateGetNonEmptyString( p_demux, "sdi-video-connection" ); - if( psz_tmp ) + psz_video_connection = var_CreateGetNonEmptyString( p_demux, "sdi-video-connection" ); + if( psz_video_connection ) { BMDVideoConnection conn; - if ( !strcmp( psz_tmp, "sdi" ) ) + if ( !strcmp( psz_video_connection, "sdi" ) ) conn = bmdVideoConnectionSDI; - else if ( !strcmp( psz_tmp, "hdmi" ) ) + else if ( !strcmp( psz_video_connection, "hdmi" ) ) conn = bmdVideoConnectionHDMI; - else if ( !strcmp( psz_tmp, "opticalsdi" ) ) + else if ( !strcmp( psz_video_connection, "opticalsdi" ) ) conn = bmdVideoConnectionOpticalSDI; - else if ( !strcmp( psz_tmp, "component" ) ) + else if ( !strcmp( psz_video_connection, "component" ) ) conn = bmdVideoConnectionComponent; - else if ( !strcmp( psz_tmp, "composite" ) ) + else if ( !strcmp( psz_video_connection, "composite" ) ) conn = bmdVideoConnectionComposite; - else if ( !strcmp( psz_tmp, "svideo" ) ) + else if ( !strcmp( psz_video_connection, "svideo" ) ) conn = bmdVideoConnectionSVideo; else { msg_Err( p_demux, "Invalid --sdi-video-connection specified; choose one of " \ "sdi, hdmi, opticalsdi, component, composite, or svideo." ); - p_config->Release(); - free( psz_tmp ); - Close( p_this ); - return VLC_EGENERIC; + ret = VLC_EGENERIC; + goto finish; } - free( psz_tmp ); msg_Dbg( p_demux, "Setting video input format to 0x%x", conn); result = p_config->SetVideoInputFormat( conn ); if( result != S_OK ) { msg_Err( p_demux, "Failed to set video input connection" ); - p_config->Release(); - Close( p_this ); - return VLC_EGENERIC; + ret = VLC_EGENERIC; + goto finish; } } - psz_tmp = var_CreateGetNonEmptyString( p_demux, "sdi-audio-connection" ); - if( psz_tmp ) + psz_audio_connection = var_CreateGetNonEmptyString( p_demux, "sdi-audio-connection" ); + if( psz_audio_connection ) { BMDAudioConnection conn; - if ( !strcmp( psz_tmp, "embedded" ) ) + if ( !strcmp( psz_audio_connection, "embedded" ) ) conn = bmdAudioConnectionEmbedded; - else if ( !strcmp( psz_tmp, "aesebu" ) ) + else if ( !strcmp( psz_audio_connection, "aesebu" ) ) conn = bmdAudioConnectionAESEBU; - else if ( !strcmp( psz_tmp, "analog" ) ) + else if ( !strcmp( psz_audio_connection, "analog" ) ) conn = bmdAudioConnectionAnalog; else { msg_Err( p_demux, "Invalid --sdi-audio-connection specified; choose one of " \ "embedded, aesebu, or analog." ); - free( psz_tmp ); - p_config->Release(); - Close( p_this ); - return VLC_EGENERIC; + ret = VLC_EGENERIC; + goto finish; } - free( psz_tmp ); msg_Dbg( p_demux, "Setting audio input format to 0x%x", conn); result = p_config->SetAudioInputFormat( conn ); if( result != S_OK ) { msg_Err( p_demux, "Failed to set audio input connection" ); - p_config->Release(); - Close( p_this ); - return VLC_EGENERIC; + ret = VLC_EGENERIC; + goto finish; } } - p_config->Release(); - /* Get the list of display modes. */ IDeckLinkDisplayModeIterator *p_display_iterator; result = p_sys->p_input->GetDisplayModeIterator( &p_display_iterator ); if( result != S_OK ) { msg_Err( p_demux, "Failed to enumerate display modes" ); - p_display_iterator->Release(); - Close( p_this ); - return VLC_EGENERIC; + ret = VLC_EGENERIC; + goto finish; } - char *psz_display_mode = var_CreateGetString( p_demux, "sdi-mode" ); + psz_display_mode = var_CreateGetString( p_demux, "sdi-mode" ); if( !psz_display_mode || strlen( psz_display_mode ) == 0 || strlen( psz_display_mode ) > 4 ) { msg_Err( p_demux, "Missing or invalid --sdi-mode string" ); - free( psz_display_mode ); - p_display_iterator->Release(); - Close( p_this ); - return VLC_EGENERIC; + ret = VLC_EGENERIC; + goto finish; } /* @@ -433,12 +424,10 @@ static int Open( vlc_object_t *p_this ) for( int i = 0; i < strlen( psz_display_mode ); ++i ) sz_display_mode_padded[i] = psz_display_mode[i]; - free( psz_display_mode ); - BMDDisplayMode wanted_mode_id; memcpy( &wanted_mode_id, &sz_display_mode_padded, sizeof(wanted_mode_id) ); - bool b_found_mode = false; + b_found_mode = false; for (;;) { @@ -457,9 +446,8 @@ static int Open( vlc_object_t *p_this ) { msg_Err( p_demux, "Failed to get display mode name" ); p_display_mode->Release(); - p_display_iterator->Release(); - Close( p_this ); - return VLC_EGENERIC; + ret = VLC_EGENERIC; + goto finish; } BMDTimeValue frame_duration, time_scale; @@ -468,9 +456,8 @@ static int Open( vlc_object_t *p_this ) { msg_Err( p_demux, "Failed to get frame rate" ); p_display_mode->Release(); - p_display_iterator->Release(); - Close( p_this ); - return VLC_EGENERIC; + ret = VLC_EGENERIC; + goto finish; } const char *psz_field_dominance; @@ -515,23 +502,21 @@ static int Open( vlc_object_t *p_this ) p_display_mode->Release(); } - p_display_iterator->Release(); - if( !b_found_mode ) { msg_Err( p_demux, "Unknown SDI mode specified. " \ "Run VLC with -v --verbose-objects=-all,+sdi " \ "to get a list of supported modes." ); - Close( p_this ); - return VLC_EGENERIC; + ret = VLC_EGENERIC; + goto finish; } result = p_sys->p_input->EnableVideoInput( htonl( wanted_mode_id ), bmdFormat8BitYUV, 0 ); if( result != S_OK ) { msg_Err( p_demux, "Failed to enable video input" ); - Close( p_this ); - return VLC_EGENERIC; + ret = VLC_EGENERIC; + goto finish; } /* Set up audio. */ @@ -543,8 +528,8 @@ static int Open( vlc_object_t *p_this ) if( result != S_OK ) { msg_Err( p_demux, "Failed to enable audio input" ); - Close( p_this ); - return VLC_EGENERIC; + ret = VLC_EGENERIC; + goto finish; } } @@ -555,8 +540,8 @@ static int Open( vlc_object_t *p_this ) if( result != S_OK ) { msg_Err( p_demux, "Failed to start streams" ); - Close( p_this ); - return VLC_EGENERIC; + ret = VLC_EGENERIC; + goto finish; } /* Declare elementary streams */ @@ -570,17 +555,17 @@ static int Open( vlc_object_t *p_this ) video_fmt.video.i_frame_rate_base = p_sys->i_fps_den; video_fmt.i_bitrate = video_fmt.video.i_width * video_fmt.video.i_height * video_fmt.video.i_frame_rate * 2 * 8; - psz_tmp = var_CreateGetNonEmptyString( p_demux, "sdi-aspect-ratio" ); - if( psz_tmp ) + psz_aspect = var_CreateGetNonEmptyString( p_demux, "sdi-aspect-ratio" ); + if( psz_aspect ) { - char *psz_denominator = strchr( psz_tmp, ':' ); + char *psz_denominator = strchr( psz_aspect, ':' ); if( psz_denominator ) { *psz_denominator++ = '\0'; - video_fmt.video.i_sar_num = atoi( psz_tmp ) * video_fmt.video.i_height; + video_fmt.video.i_sar_num = atoi( psz_aspect ) * video_fmt.video.i_height; video_fmt.video.i_sar_den = atoi( psz_denominator ) * video_fmt.video.i_width; } - free( psz_tmp ); + free( psz_aspect ); } msg_Dbg( p_demux, "added new video es %4.4s %dx%d", @@ -602,7 +587,24 @@ static int Open( vlc_object_t *p_this ) /* Update default_pts to a suitable value for access */ var_Create( p_demux, "sdi-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT ); - return VLC_SUCCESS; +finish: + if( decklink_iterator ) + decklink_iterator->Release(); + + if( p_config ) + p_config->Release(); + + free( psz_video_connection ); + free( psz_audio_connection ); + free( psz_display_mode ); + + if( p_display_iterator ) + p_display_iterator->Release(); + + if( ret != VLC_SUCCESS ) + Close( p_this ); + + return ret; } static void Close( vlc_object_t *p_this ) -- 2.39.2