Class: NumRu::DCLExt::Anim::Stream

Inherits:
Object
  • Object
show all
Includes:
NumRu::DCLExt::Anim
Defined in:
lib/numru/dclext/anim/stream.rb

Constant Summary collapse

DEPTH =
24
BPP =
24
DEFAULT_FRAMERATE =
4
DEFAULT_FILENAME =
"dcl.mp4"

Constants included from NumRu::DCLExt::Anim

PADDING, VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from NumRu::DCLExt::Anim

height, width

Constructor Details

#initialize(pipe) ⇒ Stream

Returns a new instance of Stream.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/numru/dclext/anim/stream.rb', line 21

def initialize(pipe)
  @pipe = pipe
  @buffersize = width * height * DEPTH
  @num_frames = 0
  @source_id = nil
  ARGV.each do |arg|
    case arg
    when /\A--dclext-anim-framerate=/
      @framerate = $'.to_i
    when /\A--dclext-anim-filename=/
      @filename = $'
    end
  end
  @framerate ||= ((c=ENV["DCLEXT_ANIM_FRAMERATE"]) && c.to_i) || DEFAULT_FRAMERATE
  @filename ||= ENV["DCLEXT_ANIM_FILENAME"] || DEFAULT_FILENAME
end

Instance Attribute Details

#buffersizeObject (readonly)

Returns the value of attribute buffersize.



19
20
21
# File 'lib/numru/dclext/anim/stream.rb', line 19

def buffersize
  @buffersize
end

#filenameObject

Returns the value of attribute filename.



17
18
19
# File 'lib/numru/dclext/anim/stream.rb', line 17

def filename
  @filename
end

#framerateObject

Returns the value of attribute framerate.



17
18
19
# File 'lib/numru/dclext/anim/stream.rb', line 17

def framerate
  @framerate
end

#pipeObject (readonly)

Returns the value of attribute pipe.



18
19
20
# File 'lib/numru/dclext/anim/stream.rb', line 18

def pipe
  @pipe
end

Instance Method Details

#caps_strObject



84
85
86
# File 'lib/numru/dclext/anim/stream.rb', line 84

def caps_str
  "video/x-raw-rgb,width=(int)#{width},height=(int)#{height},depth=(int)#{DEPTH},bpp=(int)#{BPP},framerate=#{@framerate}/1,endianness=4321,red_mask=16711680,green_mask=65280,blue_mask=255"
end

#get_dataObject



88
89
90
91
# File 'lib/numru/dclext/anim/stream.rb', line 88

def get_data
  sleep(0.1)
  @pipe.read(@buffersize/8)
end

#startObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/numru/dclext/anim/stream.rb', line 38

def start
  require "gst"

  Gst.init

  loop = GLib::MainLoop.new(nil, false)

  x264opts = "path=qual quantizer=18 qp-min=10 qp-max=25 me=umh subme=6 ref=3 threads=0 cabac=true dct8x8=false"
#          x264opts = "bitrate=10000"
  cmd = "appsrc min-latency=100000000 block=true name=dclsource caps=\"#{caps_str}\" ! queue ! ffmpegcolorspace ! video/x-raw-yuv,format=(fourcc)I420,width=#{width},height=#{height} ! videorate ! video/x-raw-yuv,framerate=#{@framerate}/1 ! queue ! x264enc #{x264opts} ! queue ! mp4mux ! filesink location=#{@filename}"

  bin = Gst::Parse.launch(cmd)

  appsrc = bin.get_child("dclsource")
  appsrc.format = Gst::Format::TIME
  appsrc.max_bytes = height*width*DEPTH*5

  bus = bin.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

  appsrc.signal_connect('need-data') do |elm, length|
    start_feed(elm)
  end
  appsrc.signal_connect('enough-data') do |elm|
    stop_feed
  end

  bin.play
  begin
    loop.run
  rescue Interrupt
  ensure
    @pipe.close
    bin.stop
  end
end

#start_feed(appsrc) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/numru/dclext/anim/stream.rb', line 93

def start_feed(appsrc)
  unless @source_id
    @source_id = GLib::Idle.add do
      if data = get_data
        buffer = Gst::Buffer.new(data.size*8)
        buffer.caps = Gst::Caps.parse(caps_str)
        buffer.duration = 10**9/@framerate
        buffer.timestamp = buffer.duration * @num_frames
        buffer.set_data(data)
#                ret = appsrc.signal_emit("push-buffer", buffer)
        ret = appsrc.push_buffer(buffer)
        if ret.name == "GST_FLOW_OK"
          @num_frames += 1
          true
        else
          p ret.name
          appsrc.end_of_stream
          false
        end
      else
        appsrc.end_of_stream
        false
      end
    end
  end
end

#stop_feedObject



119
120
121
122
123
124
# File 'lib/numru/dclext/anim/stream.rb', line 119

def stop_feed
  if @source_id
    GLib::Source.remove(@source_id)
    @source_id = nil
  end
end