ruby-****@sourc*****
ruby-****@sourc*****
2010年 1月 6日 (水) 23:05:20 JST
------------------------- REMOTE_ADDR = 83.250.6.136 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gst-helloworld ------------------------- @@ -4,50 +4,66 @@ We will create a simple first application, a complete MP3 player, using standard GStreamer components. The player will read from a file that is given as the first argument to the program. == The code - + #!/usr/bin/ruby -w + require 'gst' - - Gst.init + unless ARGV.length == 1 $stderr.puts "Usage: #{__FILE__} <mp3 filename>" exit 1 end - + # create a new pipeline to hold the elements pipeline = Gst::Pipeline.new - + # create a disk reader filesrc = Gst::ElementFactory.make("filesrc") filesrc.location = ARGV.first - + # now it's time to get the decoder decoder = Gst::ElementFactory.make("mad") - + # and an audio sink - audiosink = Gst::ElementFactory.make("osssink") - + audiosink = Gst::ElementFactory.make("autoaudiosink") + # add objects to the main pipeline pipeline.add(filesrc, decoder, audiosink) - + # link elements filesrc >> decoder >> audiosink - + + # create the program's main loop + loop = GLib::MainLoop.new(nil, false) + + # listen to playback events + bus = pipeline.bus + bus.add_watch do |bus, message| + case message.type + when Gst::Message::EOS + loop.quit + when Gst::Message::ERROR + p message.parse + loop.quit + end + true + end + # start playing pipeline.play + begin + loop.run + rescue Interrupt + ensure + pipeline.stop + end - while pipeline.iterate do end - - # stop the pipeline - pipeline.stop - == The Code Explained Let's go through this example step by step. -The first thing you have to do is to require the Ruby/GStreamer library and initialize the framework. +The first thing you have to do is to require the Ruby/GStreamer library. require 'gst' - Gst.init ... We are going to create an empty pipeline. As you have seen in the basic introduction, this pipeline will hold and manage all the elements we are going to pack into it. @@ -67,7 +83,23 @@ filesrc.location = ARGV.first ... -((*Note*)) You can check if filesrc.nil? to verify the creation of the disk source element. +((*Note*)) You can check filesrc.nil? to verify the creation of the disk source element. We now create the MP3 decoder element. This assumes that the 'mad' plugin is installed on the system where this application is executed. @@ -78,11 +94,27 @@ Gst::ElementFactory.make may take two arguments: a string that will identify the element you need and a second argument: how you want to name the element. The name of the element is something you can choose yourself and might be used to retrieve the element from a bin/pipeline. Here we choose to not provide a name for the element, it means that GStreamer will automatically generate a name for us. -Finally we create our audio sink element. This element will be able to play back the audio using OSS. +Finally we create our audio sink element. This element will be able to play back the audio using whatever sound system is available. ... # and an audio sink - audiosink = Gst::ElementFactory.make("osssink") + audiosink = Gst::ElementFactory.make("autoaudiosink") ... We then add the elements to the pipeline. @@ -104,6 +120,39 @@ {{image_left("hello-world.png")}} {{br}} +To know when the pipeline is done playing, or if an error occurs, we need to listen to events and handle them: + + ... + # listen to playback events + bus = pipeline.bus + bus.add_watch do |bus, message| + case message.type + when Gst::Message::EOS + loop.quit + when Gst::Message::ERROR + p message.parse + loop.quit + end + true + end + ... + Everything is now set up to start streaming. We use the following statements to change the state of the pipeline: ... @@ -113,17 +146,49 @@ ((*Note*)) GStreamer will take care of the READY and PAUSED state for you when going from NULL to PLAYING. -Since we do not use threads, nothing will happen yet. We have to call Gst::Bin#iterate to execute one iteration of the pipeline. +Since we do not use threads, nothing will happen yet. We have to start the main loop that we created earlier: ... - while pipeline.iterate do end - ... - -The Gst::Bin#iterate method will return true as long as something interesting happened inside the pipeline. When the end-of-file has been reached the Gst::Bin#iterate method will return false and we can end the loop. - + begin + loop.run + rescue Interrupt + ensure + pipeline.stop + end ... - # stop the pipeline - pipeline.stop ((*Note*)) Don't forget to stop the pipeline after use. This will free all of the resources held by the elements.