Skip to content

Instantly share code, notes, and snippets.

@shibafu528
Created May 23, 2014 10:28
Show Gist options
  • Save shibafu528/f2878d28caa91408cdd7 to your computer and use it in GitHub Desktop.
Save shibafu528/f2878d28caa91408cdd7 to your computer and use it in GitHub Desktop.
Ruby/Gstを試してみよう
# -*- coding: utf-8 -*-
require "gst"
=begin
Ruby/GStreamer 2.2.0 を ruby 2.1.0 上で使用する際に、
Gst.init で例外がスローされ使用することができなくなる不具合を修正します
パッチはすでにGithub上でマージされていますので、
次期バージョン以降はこのモンキーパッチを使用する必要は無くなると期待されます
パッチ引用元:
https://github.com/ruby-gnome2/ruby-gnome2/issues/232
https://github.com/ruby-gnome2/ruby-gnome2/commit/29dd9ccdf06b2fe7d9f5cf6ace886bb89adcebf2
=end
module Gst
class Loader
def call_init_function(repository, namespace)
init_check = repository.find(namespace, "init_check")
arguments = [
1 + @init_arguments.size,
[$0] + @init_arguments,
]
succeeded, argc, argv, error = init_check.invoke(:arguments => arguments)
succeeded, argv, error = init_check.invoke(:arguments => arguments)
@init_arguments.replace(argv[1..-1])
raise error unless succeeded
end
end
end
# -*- coding: utf-8 -*-
require "gst"
require_relative "gstfix"
# 引数: 再生するファイル
# オーディオ再生部分のBinを作成
# 図にするとこんな感じのものを作っている
# _____|
# |sink|--[audioconvert]->[autoaudiosink]-->実デバイス等に出力
# ‾‾‾‾‾|
audio = Gst::Bin.new("audiobin")
conv = Gst::ElementFactory.make("audioconvert")
audiopad = conv.get_static_pad("sink")
sink = Gst::ElementFactory.make("autoaudiosink")
audio << conv << sink
conv >> sink
# audioconvertの入力からゴーストパッドを作成、Binの外向きのインタフェースとする感じ
audio.add_pad(Gst::GhostPad.new("sink", audiopad))
# パイプラインを作成
# 図:
#
# ファイル入力-->[filesrc]->[decodebin] [再生Bin]
#
pipeline = Gst::Pipeline.new("pipeline")
src = Gst::ElementFactory.make("filesrc")
src.location = ARGV.first
decoder = Gst::ElementFactory.make("decodebin")
decoder.signal_connect("pad-added") do |decoder, pad|
# デコーダ上でストリームを認識した時のシグナル
# デコーダElementから出力する動的パッドが生成されるのでこいつを再生部分に繋ぐ
# オーディオ再生Binのsink Padを取得
# ThinkPadではない
audiopad = audio.get_static_pad("sink")
# デコーダElement->再生Binをリンク
pad.link(audiopad)
end
# ソースElement-デコーダElement-再生Binをパイプラインに追加
pipeline << src << decoder << audio
# ソースElement->デコーダElementをリンク
# デコーダ-再生Bin間をリンクしていないのは、この時点ではまだデコーダからの出力パッドが存在しないから
src >> decoder
pipeline.play
begin
running = true
bus = pipeline.bus
while running
message = bus.poll(Gst::MessageType::ANY, Gst::CLOCK_TIME_NONE)
raise "[Gst] message nil" if message.nil?
case message.type
when Gst::MessageType::EOS then
running = false
when Gst::MessageType::ERROR then
activity :error, "[Gst]再生エラー: #{message.parse}"
running = false
end
end
ensure
pipeline.stop
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment