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')
= nil
= nil
response.read_body do |chunk|
buf += chunk
until buf.empty?
case state
when :commen_header break if buf.size < 8
= buf.slice!(0, 8).unpack('aanN')
state = :payload_header
when :payload_header break if buf.size < 128
= buf.slice!(0, 128).unpack('a4H6Ca4aa*')
state = :payload_data
when :payload_data
jpeg_data_size = [1].to_i(16)
padding_size = [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(, , jpeg_data, padding_data)
end end end end end rescue retry end
|