Hi,

playing files in a loop mode in melted cause a leak of file descriptors that seen well from /proc/<process id>/fd/<file no>.

the problem comes from avformat's producer re-open code that lost release of audiostream context.

attached patch fix this behaviour...

--
________________________________________
Maksym Veremeyenko
>From 55468ffa298871951ddde9a2ddeb39c7388607c9 Mon Sep 17 00:00:00 2001
From: Maksym Veremeyenko <ve...@m1stereo.tv>
Date: Sun, 3 Jul 2011 16:32:16 +0300
Subject: [PATCH 1/3] fix a audiostream leak in producer reopen

---
 src/modules/avformat/producer_avformat.c |   60 +++++++++++++----------------
 1 files changed, 27 insertions(+), 33 deletions(-)

diff --git a/src/modules/avformat/producer_avformat.c b/src/modules/avformat/producer_avformat.c
index b96bf10..314bcd1 100644
--- a/src/modules/avformat/producer_avformat.c
+++ b/src/modules/avformat/producer_avformat.c
@@ -143,6 +143,8 @@ static void producer_set_up_audio( producer_avformat self, mlt_frame frame );
 static void apply_properties( void *obj, mlt_properties properties, int flags );
 static int video_codec_init( producer_avformat self, int index, mlt_properties properties );
 static void get_audio_streams_info( producer_avformat self );
+static void producer_avformat_release( producer_avformat );
+static int audio_codec_init( producer_avformat self, int index, mlt_properties properties );
 
 #ifdef VDPAU
 #include "vdpau.c"
@@ -849,40 +851,18 @@ static void reopen_video( producer_avformat self, mlt_producer producer )
 {
 	mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
 	mlt_service_lock( MLT_PRODUCER_SERVICE( producer ) );
-	pthread_mutex_lock( &self->audio_mutex );
 
-	if ( self->video_codec )
-	{
-		avformat_lock();
-		avcodec_close( self->video_codec );
-		avformat_unlock();
-	}
-	self->video_codec = NULL;
-	if ( self->dummy_context )
-		av_close_input_file( self->dummy_context );
-	self->dummy_context = NULL;
-	if ( self->video_format )
-		av_close_input_file( self->video_format );
-	self->video_format = NULL;
-
-	int audio_index = self->audio_index;
-	int video_index = self->video_index;
+	producer_avformat_release( self );
 
-	pthread_mutex_unlock( &self->audio_mutex );
-	pthread_mutex_unlock( &self->video_mutex );
 	producer_open( self, mlt_service_profile( MLT_PRODUCER_SERVICE(producer) ),
 		mlt_properties_get( properties, "resource" ) );
-	pthread_mutex_lock( &self->video_mutex );
-	pthread_mutex_lock( &self->audio_mutex );
 
-	self->audio_index = audio_index;
-	if ( self->video_format && video_index > -1 )
-	{
-		self->video_index = video_index;
-		video_codec_init( self, video_index, properties );
-	}
+	if ( self->video_format && self->video_index > -1 )
+		video_codec_init( self, self->video_index, properties );
+
+	if ( self->audio_format && self->audio_index > -1 )
+		audio_codec_init( self, self->audio_index, properties );
 
-	pthread_mutex_unlock( &self->audio_mutex );
 	mlt_service_unlock( MLT_PRODUCER_SERVICE( producer ) );
 }
 
@@ -2531,38 +2511,45 @@ static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int i
 	return 0;
 }
 
-static void producer_avformat_close( producer_avformat self )
+static void producer_avformat_release( producer_avformat self )
 {
-	mlt_log_debug( NULL, "producer_avformat_close\n" );
-
 	// Cleanup av contexts
-	av_free( self->av_frame );
+	if( self->av_frame )
+		av_freep( &self->av_frame );
 	avformat_lock();
 	int i;
 	for ( i = 0; i < MAX_AUDIO_STREAMS; i++ )
 	{
 		if ( self->audio_resample[i] )
 			audio_resample_close( self->audio_resample[i] );
+		self->audio_resample[i] = NULL;
 		mlt_pool_release( self->audio_buffer[i] );
-		av_free( self->decode_buffer[i] );
+		self->audio_buffer[i] = NULL;
+		av_freep( &self->decode_buffer[i] );
 		if ( self->audio_codec[i] )
 			avcodec_close( self->audio_codec[i] );
+		self->audio_codec[i] = NULL;
 	}
 	if ( self->video_codec )
 		avcodec_close( self->video_codec );
+	self->video_codec = NULL;
 	// Close the file
 	if ( self->dummy_context )
 		av_close_input_file( self->dummy_context );
+	self->dummy_context = NULL;
 	if ( self->seekable && self->audio_format )
 		av_close_input_file( self->audio_format );
+	self->audio_format = NULL;
 	if ( self->video_format )
 		av_close_input_file( self->video_format );
+	self->video_format = NULL;
 	avformat_unlock();
 #ifdef VDPAU
 	vdpau_producer_close( self );
 #endif
 	if ( self->image_cache )
 		mlt_cache_close( self->image_cache );
+	self->image_cache = NULL;
 
 	// Cleanup the mutexes
 	pthread_mutex_destroy( &self->audio_mutex );
@@ -2581,6 +2568,13 @@ static void producer_avformat_close( producer_avformat self )
 		av_free_packet( pkt );
 		free( pkt );
 	}
+}
+
+static void producer_avformat_close( producer_avformat self )
+{
+	mlt_log_debug( NULL, "producer_avformat_close\n" );
+
+	producer_avformat_release( self );
 
 	free( self );
 }
-- 
1.7.4.4

------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security 
threats, fraudulent activity, and more. Splunk takes this data and makes 
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
Mlt-devel mailing list
Mlt-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mlt-devel

Reply via email to