]> git.sesse.net Git - mlt/blobdiff - src/modules/sdl/consumer_sdl_audio.c
fix segfault loading jackrack on Fedora 16 (3468312)
[mlt] / src / modules / sdl / consumer_sdl_audio.c
index 5bbf1cc2ec7fa97b7c9fb17c652ca2e6c3154a27..e59aae3c68868e9c875880274583a9f468daed10 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * consumer_sdl_audio.c -- A Simple DirectMedia Layer audio-only consumer
- * Copyright (C) 2009 Ushodaya Enterprises Limited
+ * Copyright (C) 2009, 2010 Ushodaya Enterprises Limited
  * Author: Dan Dennedy <dan@dennedy.org>
  *
  * This library is free software; you can redistribute it and/or
 #include <stdlib.h>
 #include <string.h>
 #include <pthread.h>
-#include <SDL/SDL.h>
+#include <SDL.h>
 #include <sys/time.h>
 
+extern pthread_mutex_t mlt_sdl_mutex;
+
 /** This classes definition.
 */
 
@@ -100,15 +102,16 @@ mlt_consumer consumer_sdl_audio_init( mlt_profile profile, mlt_service_type type
                pthread_cond_init( &this->audio_cond, NULL);
                pthread_mutex_init( &this->video_mutex, NULL );
                pthread_cond_init( &this->video_cond, NULL);
-               
+
                // Default scaler (for now we'll use nearest)
                mlt_properties_set( this->properties, "rescale", "nearest" );
+               mlt_properties_set( this->properties, "deinterlace_method", "onefield" );
 
                // Default buffer for low latency
                mlt_properties_set_int( this->properties, "buffer", 1 );
 
                // Default audio buffer
-               mlt_properties_set_int( this->properties, "audio_buffer", 512 );
+               mlt_properties_set_int( this->properties, "audio_buffer", 2048 );
 
                // Ensure we don't join on a non-running object
                this->joined = 1;
@@ -140,7 +143,7 @@ static void consumer_refresh_cb( mlt_consumer sdl, mlt_consumer parent, char *na
        {
                consumer_sdl this = parent->child;
                pthread_mutex_lock( &this->refresh_mutex );
-               this->refresh_count = this->refresh_count <= 0 ? 1 : this->refresh_count ++;
+               this->refresh_count = this->refresh_count <= 0 ? 1 : this->refresh_count + 1;
                pthread_cond_broadcast( &this->refresh_cond );
                pthread_mutex_unlock( &this->refresh_mutex );
        }
@@ -154,15 +157,17 @@ int consumer_start( mlt_consumer parent )
        {
                consumer_stop( parent );
 
-               this->running = 1;
-               this->joined = 0;
-
-               if ( SDL_Init( SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE ) < 0 )
+               pthread_mutex_lock( &mlt_sdl_mutex );
+               int ret = SDL_Init( SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE );
+               pthread_mutex_unlock( &mlt_sdl_mutex );
+               if ( ret < 0 )
                {
                        mlt_log_error( MLT_CONSUMER_SERVICE(parent), "Failed to initialize SDL: %s\n", SDL_GetError() );
                        return -1;
                }
 
+               this->running = 1;
+               this->joined = 0;
                pthread_create( &this->thread, NULL, consumer_thread, this );
        }
 
@@ -174,7 +179,7 @@ int consumer_stop( mlt_consumer parent )
        // Get the actual object
        consumer_sdl this = parent->child;
 
-       if ( this->joined == 0 )
+       if ( this->running && !this->joined )
        {
                // Kill the thread and clean up
                this->joined = 1;
@@ -186,9 +191,16 @@ int consumer_stop( mlt_consumer parent )
                pthread_mutex_unlock( &this->refresh_mutex );
 
                // Cleanup the main thread
+#ifndef WIN32
                if ( this->thread )
+#endif
                        pthread_join( this->thread, NULL );
 
+               // Unlatch the video thread
+               pthread_mutex_lock( &this->video_mutex );
+               pthread_cond_broadcast( &this->video_cond );
+               pthread_mutex_unlock( &this->video_mutex );
+
                // Unlatch the audio callback
                pthread_mutex_lock( &this->audio_mutex );
                pthread_cond_broadcast( &this->audio_cond );
@@ -256,7 +268,7 @@ static int consumer_play_audio( consumer_sdl this, mlt_frame frame, int init_aud
 {
        // Get the properties of this consumer
        mlt_properties properties = this->properties;
-       mlt_audio_format afmt = mlt_audio_pcm;
+       mlt_audio_format afmt = mlt_audio_s16;
 
        // Set the preferred params of the test card signal
        int channels = mlt_properties_get_int( properties, "channels" );
@@ -268,7 +280,7 @@ static int consumer_play_audio( consumer_sdl this, mlt_frame frame, int init_aud
        int16_t *pcm;
        int bytes;
 
-       mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
+       mlt_frame_get_audio( frame, (void**) &pcm, &afmt, &frequency, &channels, &samples );
        *duration = ( ( samples * 1000 ) / frequency );
 
        if ( mlt_properties_get_int( properties, "audio_off" ) )
@@ -458,7 +470,7 @@ static void *consumer_thread( void *arg )
        int duration = 0;
        int64_t playtime = 0;
        struct timespec tm = { 0, 100000 };
-       int last_position = -1;
+//     int last_position = -1;
        this->refresh_count = 0;
 
        // Loop until told not to
@@ -522,21 +534,28 @@ static void *consumer_thread( void *arg )
                                        consumer_play_video( this, frame );
                                        pthread_cond_wait( &this->refresh_cond, &this->refresh_mutex );
                                }
+                               mlt_frame_close( frame );
                                this->refresh_count --;
                                pthread_mutex_unlock( &this->refresh_mutex );
                        }
+                       else
+                       {
+                               mlt_frame_close( frame );
+                               frame = NULL;
+                       }
 
                        // Optimisation to reduce latency
-                       if ( speed == 1.0 )
+                       if ( frame && speed == 1.0 )
                        {
-                               if ( last_position != -1 && last_position + 1 != mlt_frame_get_position( frame ) )
-                                       mlt_consumer_purge( consumer );
-                               last_position = mlt_frame_get_position( frame );
+                // TODO: disabled due to misbehavior on parallel-consumer
+//                             if ( last_position != -1 && last_position + 1 != mlt_frame_get_position( frame ) )
+//                                     mlt_consumer_purge( consumer );
+//                             last_position = mlt_frame_get_position( frame );
                        }
                        else
                        {
                                mlt_consumer_purge( consumer );
-                               last_position = -1;
+//                             last_position = -1;
                        }
                }
        }
@@ -578,6 +597,10 @@ static void consumer_close( mlt_consumer parent )
        // Destroy mutexes
        pthread_mutex_destroy( &this->audio_mutex );
        pthread_cond_destroy( &this->audio_cond );
+       pthread_mutex_destroy( &this->video_mutex );
+       pthread_cond_destroy( &this->video_cond );
+       pthread_mutex_destroy( &this->refresh_mutex );
+       pthread_cond_destroy( &this->refresh_cond );
 
        // Finally clean up this
        free( this );