Module: Sonycam::Liveview

Extended by:
Liveview
Included in:
Liveview
Defined in:
lib/sonycam/liveview.rb

Instance Method Summary collapse

Instance Method Details

#stream(liveview_url) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sonycam/liveview.rb', line 7

def stream liveview_url
  uri = URI(liveview_url)
  Net::HTTP.start(uri.host, uri.port) do |http|
    request = Net::HTTP::Get.new uri
    http.request request do |response|
      state = :commen_header
      buf = ''.force_encoding('BINARY')
      commen_header = nil
      payload_header = nil
      response.read_body do |chunk|
        buf += chunk
        until buf.empty?
          case state
          when :commen_header # 8 bytes
            break if buf.size < 8
            commen_header = buf.slice!(0, 8).unpack('aanN')
            state = :payload_header
          when :payload_header # 128 bytes
            break if buf.size < 128
            payload_header = buf.slice!(0, 128).unpack('a4H6Ca4aa*')
            state = :payload_data
          when :payload_data
            jpeg_data_size = payload_header[1].to_i(16)
            padding_size = payload_header[2]
            break if buf.size < jpeg_data_size + padding_size
            jpeg_data = buf.slice!(0, jpeg_data_size)
            padding_data = buf.slice!(0, padding_size)
            state = :commen_header
            yield Packet.new(commen_header, payload_header, jpeg_data, padding_data)
          end # case state
        end # until buf.empty?
      end # response.read_body do |chunk|
    end # http.request request do |response|
  end rescue retry # Net::HTTP.start(uri.host, uri.port) do |http|
end