From d4eaf0b005e1e439574bc52d69f5371d7f1983b2 Mon Sep 17 00:00:00 2001 From: Mario Lang Date: Sun, 12 Oct 2014 14:42:35 +0200 Subject: [PATCH 1/4] Initial conversion to gstreamer 1.0 API. Single playback works now, resetting the URI breaks the pipe. --- lib/soundcli/player.rb | 70 +++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 42 deletions(-) diff --git a/lib/soundcli/player.rb b/lib/soundcli/player.rb index 7b7429c..8727729 100644 --- a/lib/soundcli/player.rb +++ b/lib/soundcli/player.rb @@ -4,11 +4,10 @@ class Player def initialize - # create the playbin - @playbin = Gst::ElementFactory.make("playbin2") + # create the bin + @bin, error = Gst.parse_launch("souphttpsrc name=httpsrc ! decodebin ! audioconvert ! audioresample ! autoaudiosink") #watch the bus for messages - bus = @playbin.bus - bus.add_watch do |bus, message| + @bin.bus.add_watch do |bus, message| handle_bus_message(message) end end @@ -17,8 +16,7 @@ def set(uri, comments) @comments = comments @comment_ptr = 0 - @playbin.set_property("buffer-size", Settings::all['buffer-size']) - @playbin.set_property("uri",uri) + @bin.get_by_name("httpsrc").location = uri end protected @@ -38,47 +36,35 @@ def ns_to_str(ns) # get position of the playbin def position - begin - @query_position = Gst::QueryPosition.new(Gst::Format::TIME) - @playbin.query(@query_position) - pos = @query_position - rescue - pos = 0 - end + result, pos = @bin.query_position(Gst::Format::TIME) return pos end # get song duration def duration - begin - @query_duration = Gst::QueryDuration.new(Gst::Format::TIME) - @playbin.query(@query_duration) - pos = @query_duration - rescue - pos = 0 - end - return pos + result, dur = @bin.query_duration(Gst::Format::TIME) + return dur end public #set or get the volume def volume(v) - @playbin.set_property("volume", v) if v and (0..1).cover? v - return @playbin.get_property("volume") + @bin.set_property("volume", v) if v and (0..1).cover? v + return @bin.get_property("volume") end def quit - @playbin.stop + @bin.stop @mainloop.quit end def play - @playbin.play + @bin.play GLib::Timeout.add(100) do - @duration = self.ns_to_str(self.duration.parse[1]) - @position = self.ns_to_str(self.position.parse[1]) - timestamp = self.position.parse[1]/1000000 + @duration = self.ns_to_str(self.duration) + @position = self.ns_to_str(self.position) + timestamp = self.position/1000000 if self.playing? Helpers::say("#{@position}/#{@duration} \r", :normal) @@ -116,20 +102,20 @@ def play end def resume - @playbin.set_state(Gst::State::PLAYING) - @playbin.play + @bin.set_state(Gst::State::PLAYING) + @bin.play end def pause - @playbin.set_state(Gst::State::PAUSED) - @playbin.pause + @bin.set_state(Gst::State::PAUSED) + @bin.pause Helpers::say("--- PAUSED ---\r", :normal) end def handle_bus_message(msg) case msg.type - when Gst::Message::Type::BUFFERING - buffer = msg.parse + when Gst::MessageType::BUFFERING + buffer = msg.parse_buffering if buffer < 100 Helpers::say("Buffering: #{buffer}% \r", :normal) self.pause if self.playing? @@ -139,26 +125,26 @@ def handle_bus_message(msg) end $stdout.flush - when Gst::Message::Type::ERROR - @playbin.set_state(Gst::State::NULL) - $stderr.puts msg.parse + when Gst::MessageType::ERROR + @bin.set_state(Gst::State::NULL) + $stderr.puts msg.parse_error self.quit - when Gst::Message::Type::EOS - @playbin.set_state(Gst::State::NULL) + when Gst::MessageType::EOS + @bin.set_state(Gst::State::NULL) self.quit end true end def done? - return (@playbin.get_state[1] == Gst::State::NULL) + return (@bin.get_state(Gst::CLOCK_TIME_NONE)[1] == Gst::State::NULL) end def playing? - return (@playbin.get_state[1] == Gst::State::PLAYING) + return (@bin.get_state(Gst::CLOCK_TIME_NONE)[1] == Gst::State::PLAYING) end def paused? - return (@playbin.get_state[1] == Gst::State::PAUSED) + return (@bin.get_state(Gst::CLOCK_TIME_NONE)[1] == Gst::State::PAUSED) end end From 0777839bf59574b9f038b7069a9f8479ff062a3a Mon Sep 17 00:00:00 2001 From: Mario Lang Date: Sun, 12 Oct 2014 14:59:49 +0200 Subject: [PATCH 2/4] Switch to playbin, which makes uri resetting work as expected. --- lib/soundcli/player.rb | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/soundcli/player.rb b/lib/soundcli/player.rb index 8727729..302ac0b 100644 --- a/lib/soundcli/player.rb +++ b/lib/soundcli/player.rb @@ -5,9 +5,9 @@ class Player def initialize # create the bin - @bin, error = Gst.parse_launch("souphttpsrc name=httpsrc ! decodebin ! audioconvert ! audioresample ! autoaudiosink") + @playbin, error = Gst.parse_launch("playbin") #watch the bus for messages - @bin.bus.add_watch do |bus, message| + @playbin.bus.add_watch do |bus, message| handle_bus_message(message) end end @@ -16,7 +16,7 @@ def set(uri, comments) @comments = comments @comment_ptr = 0 - @bin.get_by_name("httpsrc").location = uri + @playbin.uri = uri end protected @@ -36,30 +36,30 @@ def ns_to_str(ns) # get position of the playbin def position - result, pos = @bin.query_position(Gst::Format::TIME) + result, pos = @playbin.query_position(Gst::Format::TIME) return pos end # get song duration def duration - result, dur = @bin.query_duration(Gst::Format::TIME) + result, dur = @playbin.query_duration(Gst::Format::TIME) return dur end public #set or get the volume def volume(v) - @bin.set_property("volume", v) if v and (0..1).cover? v - return @bin.get_property("volume") + @playbin.set_property("volume", v) if v and (0..1).cover? v + return @playbin.get_property("volume") end def quit - @bin.stop + @playbin.stop @mainloop.quit end def play - @bin.play + @playbin.play GLib::Timeout.add(100) do @duration = self.ns_to_str(self.duration) @@ -102,13 +102,13 @@ def play end def resume - @bin.set_state(Gst::State::PLAYING) - @bin.play + @playbin.set_state(Gst::State::PLAYING) + @playbin.play end def pause - @bin.set_state(Gst::State::PAUSED) - @bin.pause + @playbin.set_state(Gst::State::PAUSED) + @playbin.pause Helpers::say("--- PAUSED ---\r", :normal) end @@ -126,25 +126,25 @@ def handle_bus_message(msg) $stdout.flush when Gst::MessageType::ERROR - @bin.set_state(Gst::State::NULL) + @playbin.set_state(Gst::State::NULL) $stderr.puts msg.parse_error self.quit when Gst::MessageType::EOS - @bin.set_state(Gst::State::NULL) + @playbin.set_state(Gst::State::NULL) self.quit end true end def done? - return (@bin.get_state(Gst::CLOCK_TIME_NONE)[1] == Gst::State::NULL) + return (@playbin.get_state(Gst::CLOCK_TIME_NONE)[1] == Gst::State::NULL) end def playing? - return (@bin.get_state(Gst::CLOCK_TIME_NONE)[1] == Gst::State::PLAYING) + return (@playbin.get_state(Gst::CLOCK_TIME_NONE)[1] == Gst::State::PLAYING) end def paused? - return (@bin.get_state(Gst::CLOCK_TIME_NONE)[1] == Gst::State::PAUSED) + return (@playbin.get_state(Gst::CLOCK_TIME_NONE)[1] == Gst::State::PAUSED) end end From 66435b2abdd378887d3e8fb1a7a14ceef32c1be5 Mon Sep 17 00:00:00 2001 From: Mario Lang Date: Sun, 12 Oct 2014 19:47:47 +0200 Subject: [PATCH 3/4] No need for @. --- lib/soundcli/player.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/soundcli/player.rb b/lib/soundcli/player.rb index 302ac0b..298a77b 100644 --- a/lib/soundcli/player.rb +++ b/lib/soundcli/player.rb @@ -62,12 +62,12 @@ def play @playbin.play GLib::Timeout.add(100) do - @duration = self.ns_to_str(self.duration) - @position = self.ns_to_str(self.position) + duration = self.ns_to_str(self.duration) + position = self.ns_to_str(self.position) timestamp = self.position/1000000 if self.playing? - Helpers::say("#{@position}/#{@duration} \r", :normal) + Helpers::say("#{position}/#{duration} \r", :normal) $stdout.flush end From e72e45655314ca23ca36ab1015db891fac92d963 Mon Sep 17 00:00:00 2001 From: Mario Lang Date: Sun, 12 Oct 2014 20:08:16 +0200 Subject: [PATCH 4/4] No need to call set_state for pause/resume. --- lib/soundcli/player.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/soundcli/player.rb b/lib/soundcli/player.rb index 298a77b..d83759f 100644 --- a/lib/soundcli/player.rb +++ b/lib/soundcli/player.rb @@ -5,7 +5,7 @@ class Player def initialize # create the bin - @playbin, error = Gst.parse_launch("playbin") + @playbin = Gst.parse_launch("playbin") #watch the bus for messages @playbin.bus.add_watch do |bus, message| handle_bus_message(message) @@ -102,12 +102,10 @@ def play end def resume - @playbin.set_state(Gst::State::PLAYING) @playbin.play end def pause - @playbin.set_state(Gst::State::PAUSED) @playbin.pause Helpers::say("--- PAUSED ---\r", :normal) end