]> git.sesse.net Git - mlt/commitdiff
extend Ruby API with PlaylistNextListner and show how to use it
authorDan Dennedy <dan@dennedy.org>
Fri, 31 Aug 2012 18:54:19 +0000 (11:54 -0700)
committerDan Dennedy <dan@dennedy.org>
Fri, 31 Aug 2012 18:54:19 +0000 (11:54 -0700)
src/swig/mlt.i
src/swig/ruby/playlist.rb [new file with mode: 0755]

index 4bb129638f125fa0157048e0a0dc3fb73e60f3c0..c3deaeb9af87c85c6fec2a6c5eedb104512ea633 100644 (file)
@@ -101,6 +101,8 @@ void mlt_log_set_level( int );
 %include <MltParser.h>
 %include <MltFilteredConsumer.h>
 
+
+
 #if defined(SWIGRUBY)
 
 %{
@@ -109,28 +111,31 @@ static void ruby_listener( mlt_properties owner, void *object );
 
 class RubyListener
 {
-       private:
+       protected:
                VALUE callback;
                Mlt::Event *event;
 
        public:
-               RubyListener( Mlt::Properties &properties, char *id, VALUE callback ) : 
+               RubyListener( VALUE callback ) : callback( callback )
+               {}
+
+               RubyListener( Mlt::Properties &properties, char *id, VALUE callback ) :
                        callback( callback ) 
                {
                        event = properties.listen( id, this, ( mlt_listener )ruby_listener );
                }
 
-               ~RubyListener( )
+               virtual ~RubyListener( )
                {
                        delete event;
                }
 
-       void mark( ) 
+               void mark( )
                { 
                        ((void (*)(VALUE))(rb_gc_mark))( callback ); 
                }
 
-       void doit( ) 
+               void doit( )
                {
                ID method = rb_intern( "call" );
                rb_funcall( callback, method, 0 );
@@ -149,19 +154,60 @@ void markRubyListener( void* p )
     o->mark( );
 }
 
+static void on_playlist_next( mlt_properties owner, void *object, int i );
+
+class PlaylistNextListener : RubyListener
+{
+       private:
+               Mlt::Event *event;
+
+       public:
+               PlaylistNextListener( Mlt::Properties *properties, VALUE proc )
+                       : RubyListener( proc )
+               {
+                       event = properties->listen( "playlist-next", this, ( mlt_listener )on_playlist_next );
+               }
+
+               ~PlaylistNextListener()
+               {
+                       delete event;
+               }
+
+               void yield( int i )
+               {
+                       ID method = rb_intern( "call" );
+                       rb_funcall( callback, method, 1, INT2FIX( i ) );
+               }
+};
+
+static void on_playlist_next( mlt_properties owner, void *object, int i )
+{
+       PlaylistNextListener *o = static_cast< PlaylistNextListener * >( object );
+       o->yield( i );
+}
+
 %}
 
 // Ruby wrapper
 %rename( Listener )  RubyListener;
 %markfunc RubyListener "markRubyListener";
+%markfunc PlaylistNextListener "markRubyListener";
 
-class RubyListener 
+class RubyListener
 {
        public:
                RubyListener( Mlt::Properties &properties, char *id, VALUE callback );
 };
 
-#endif
+class PlaylistNextListener
+{
+       public:
+               PlaylistNextListener( Mlt::Properties *properties, VALUE proc );
+};
+
+#endif // SWIGGRUBY
+
+
 
 #if defined(SWIGPYTHON)
 %{
diff --git a/src/swig/ruby/playlist.rb b/src/swig/ruby/playlist.rb
new file mode 100755 (executable)
index 0000000..99bb341
--- /dev/null
@@ -0,0 +1,60 @@
+#!/usr/bin/env ruby
+
+# Import required modules
+require 'mlt'
+
+# Create the mlt system
+Mlt::Factory::init
+
+# Establish the mlt profile
+profile = Mlt::Profile.new
+
+# Get and check the argument
+file = ARGV.shift
+raise "Usage: test.rb file1 file2" if file.nil?
+file2 = ARGV.shift
+raise "Usage: test.rb file1 file2" if file2.nil?
+
+pls = Mlt::Playlist.new(profile)
+
+# Create the producer
+producer = Mlt::Factory::producer( profile, file )
+raise "Unable to load #{file}" if !producer.is_valid
+
+producer2 = Mlt::Factory::producer( profile, file2 )
+raise "Unable to load #{file2}" if !producer2.is_valid
+
+pls.append(producer)
+#pls.repeat(0, 2)
+pls.append(producer2)
+
+# Create the consumer
+consumer = Mlt::Consumer.new( profile, "sdl" )
+raise "Unable to open sdl consumer" if !consumer.is_valid
+
+# Turn off the default rescaling
+consumer.set( "rescale", "none" )
+consumer.set("volume", 0.1)
+consumer.set("terminate_on_pause", 1)
+
+Mlt::PlaylistNextListener.new(pls, Proc.new { |i|
+    info = pls.clip_info(i)
+    puts "finished playing #{info.resource}\n"
+})
+
+
+# Set up a 'wait for' event
+event = consumer.setup_wait_for( "consumer-stopped" )
+
+# Start the consumer
+consumer.start
+
+# Connect the producer to the consumer
+consumer.connect( pls )
+
+# Wait until the user stops the consumer
+consumer.wait_for( event )
+
+# Clean up consumer
+consumer.stop
+