Module: IPCam

Defined in:
lib/ipcam/main.rb,
lib/ipcam/version.rb,
lib/ipcam/websock.rb,
lib/ipcam/webserver.rb

Defined Under Namespace

Classes: WebServer, WebSocket

Constant Summary collapse

BASIS_SIZE =
640 * 480
Stop =
Class.new(Exception)
Restart =
Class.new(Exception)
VERSION =
"0.4.2"

Class Method Summary collapse

Class Method Details

.abort?Boolean

Returns:

  • (Boolean)


401
402
403
# File 'lib/ipcam/main.rb', line 401

def abort?
  @state == :ABORT
end

.add_client(que) ⇒ Object



377
378
379
# File 'lib/ipcam/main.rb', line 377

def add_client(que)
  @clients << {:que => que}
end

.alive?Boolean

Returns:

  • (Boolean)


397
398
399
# File 'lib/ipcam/main.rb', line 397

def alive?
  @state == :ALIVE
end

.broadcast(name, *args) ⇒ Object



222
223
224
# File 'lib/ipcam/main.rb', line 222

def broadcast(name, *args)
  WebSocket.broadcast(name, *args)
end

.camera_threadObject



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/ipcam/main.rb', line 239

def camera_thread
  $logger.info("main") {"camera thread start"}

  @camera = Video4Linux2::Camera.new($target)
  if not @camera.support_formats.any? {|x| x.fcc == "MJPG"}
    raise("#{$target} is not support Motion-JPEG")
  end

  snd_thr = Thread.new {sender_thread}

  begin
    @mutex.synchronize {
      @config = load_settings()

      @camera.start
      change_state(:ALIVE)
    }

    loop {
      @img_que << @camera.capture
    }

  rescue Stop
    $logger.info("main") {"accept stop request"}
    change_state(:STOP)

  rescue Restart
    $logger.info("main") {"restart camera"}
    @camera.stop
    retry

  ensure
    @camera.stop if (@camera.busy? rescue false)
  end

rescue => e
  $logger.error("main") {"camera error occured (#{e.message})"}
  change_state(:ABORT, :force)

ensure
  @camera&.close
  @camera = nil

  snd_thr&.raise(Stop)
  snd_thr&.join

  $logger.info("main") {"camera thread stop"}
end

.change_state(state, force = false) ⇒ Object



227
228
229
230
231
232
233
234
235
236
# File 'lib/ipcam/main.rb', line 227

def change_state(state, force = false)
  flag = @mutex.try_lock

  if @state != state or force
    @state = state
    broadcast(:change_state, state)
  end

  @mutex.unlock if flag
end

.create_capability_list(cam) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/ipcam/main.rb', line 89

def create_capability_list(cam)
  ret = cam.frame_capabilities(:MJPEG).inject([]) { |m, n|
    m << pack_capability(n)
  }

  return ret
end

.create_control_list(cam) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/ipcam/main.rb', line 138

def create_control_list(cam)
  ret = cam.controls.inject([]) { |m, n|
    case n
    when Video4Linux2::Camera::IntegerControl
      m << pack_integer_control(n)

    when Video4Linux2::Camera::BooleanControl
      m << pack_boolean_control(n)

    when Video4Linux2::Camera::MenuControl
      m << pack_menu_control(n)

    else
      raise("Unknwon control found #{n.class}")
    end
  }

  return ret
end

.create_setting_entry(cam) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/ipcam/main.rb', line 159

def create_setting_entry(cam)
  cap  = select_capabilities(cam)
  rate = cap.rate.sort.first

  ret  = {
    :image_width  => cap.width,
    :image_height => cap.height,
    :framerate    => [rate.numerator, rate.denominator],
    :capabilities => create_capability_list(cam),
    :controls     => create_control_list(cam)
  }

  return ret
end

.get_camera_infoObject



306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/ipcam/main.rb', line 306

def get_camera_info
  @mutex.synchronize {
    ret = {
      :device => $target,
      :state  => @state
    }

    ret.merge!(:bus => @camera.bus, :name => @camera.name) if @camera

    return ret
  }
end

.get_configObject



324
325
326
327
# File 'lib/ipcam/main.rb', line 324

def get_config
  raise("state violation") if @state != :ALIVE
  return @config
end

.get_ident_stringObject



319
320
321
322
# File 'lib/ipcam/main.rb', line 319

def get_ident_string
  raise("state violation") if @state != :ALIVE
  return "#{@camera.name}@#{@camera.bus}"
end

.load_settingsObject



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/ipcam/main.rb', line 200

def load_settings
  ret = @db.dig(@camera.bus.to_sym, @camera.name.to_sym)

  if not ret
    ret = create_setting_entry(@camera)
    (@db[@camera.bus] ||= {})[@camera.name] = ret

    $db_file.binwrite(@db.to_msgpack)
  end

  @camera.image_height = ret[:image_height]
  @camera.image_width  = ret[:image_width]
  @camera.framerate    = Rational(*ret[:framerate])

  ret[:controls].each { |ctr|
    @camera.set_control(ctr[:id], ctr[:value]) rescue :ignore
  }

  return ret
end

.pack_boolean_control(c) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/ipcam/main.rb', line 113

def pack_boolean_control(c)
  ret = {
    :type  => :boolean,
    :id    => c.id,
    :name  => c.name,
    :value => c.default
  }

  return ret
end

.pack_capability(cap) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ipcam/main.rb', line 76

def pack_capability(cap)
  ret = {
    :width  => cap.width,
    :height => cap.height,
    :rate   => cap.rate.inject([]) { |m, n|
      m << [n.numerator, n.denominator]
    }
  }

  return ret
end

.pack_integer_control(c) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ipcam/main.rb', line 98

def pack_integer_control(c)
  ret = {
    :type  => :integer,
    :id    => c.id,
    :name  => c.name,
    :value => c.default,
    :min   => c.min,
    :max   => c.max,
    :step  => c.step
  }

  return ret
end

.pack_menu_control(c) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ipcam/main.rb', line 125

def pack_menu_control(c)
  ret = {
    :type  => :menu,
    :id    => c.id,
    :name  => c.name,
    :value => c.default,
    :items => c.items.inject({}) {|m, n| m[n.name] = n.index; m}
  }

  return ret
end

.remove_client(que) ⇒ Object



381
382
383
# File 'lib/ipcam/main.rb', line 381

def remove_client(que)
  @clients.reject! {|c| c[:que] == que}
end

.restart_cameraObject



55
56
57
# File 'lib/ipcam/main.rb', line 55

def restart_camera
  @cam_thr.raise(Restart)
end

.restore_dbObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/ipcam/main.rb', line 175

def restore_db
  begin
    blob = $db_file.binread
    @db  = MessagePack.unpack(blob, :symbolize_keys => true)

    @db.keys { |bus|
      @db[bus].keys { |name|
        @db[bus][name.to_s] = @db[bus].delete(name)
      }

      @db[bus.to_s] = @db.deleye(bus)
    }

  rescue
    begin
      $db_file.delete
    rescue Errno::ENOENT
      # ignore
    end
      
    @db  = {}
  end
end

.save_configObject



368
369
370
371
372
373
374
375
# File 'lib/ipcam/main.rb', line 368

def save_config
  $logger.info('main') {"save config to #{$db_file.to_s}"}
  @mutex.synchronize {
    $db_file.binwrite(@db.to_msgpack)
  }

  broadcast(:save_complete)
end

.select_capabilities(cam) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ipcam/main.rb', line 60

def select_capabilities(cam)
  ret = cam.frame_capabilities(:MJPEG).instance_eval {
    self.sort! { |a,b|
      da = (BASIS_SIZE - (a.width * a.height)).abs
      db = (BASIS_SIZE - (b.width * b.height)).abs

      da <=> db
    }

    self.first
  }

  return ret
end

.sender_threadObject



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/ipcam/main.rb', line 289

def sender_thread
  $logger.info("main") {"sender thread start"}

  loop {
    data = @img_que.deq
    @clients.each {|c| c[:que] << data}
    broadcast(:update_image, {:type => "image/jpeg", :data => data})
  }

rescue Stop
  @clients.each {|c| c[:que] << nil}

ensure
  $logger.info("main") {"sender thread stop"}
end

.set_control(id, val) ⇒ Object



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/ipcam/main.rb', line 352

def set_control(id, val)
  raise("state violation") if @state != :ALIVE

  entry = nil

  @mutex.synchronize {
    entry = @config[:controls].find {|obj| obj[:id] == id} 
    entry[:value] = val if entry
  }

  if entry
    restart_camera()
    broadcast(:update_control, id, val)
  end
end

.set_framerate(num, deno) ⇒ Object



341
342
343
344
345
346
347
348
349
350
# File 'lib/ipcam/main.rb', line 341

def set_framerate(num, deno)
  raise("state violation") if @state != :ALIVE

  @mutex.synchronize {
    @config[:framerate] = [num, deno]
  }

  restart_camera()
  broadcast(:update_framerate, num, deno)
end

.set_image_size(width, height) ⇒ Object



329
330
331
332
333
334
335
336
337
338
339
# File 'lib/ipcam/main.rb', line 329

def set_image_size(width, height)
  raise("state violation") if @state != :ALIVE

  @mutex.synchronize {
    @config[:image_width]  = width
    @config[:image_height] = height
  }

  restart_camera()
  broadcast(:update_image_size, width, height)
end

.startObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ipcam/main.rb', line 19

def start
  restore_db()

  @mutex   = Mutex.new
  @camera  = nil
  @state   = :STOP
  @img_que = Thread::Queue.new
  @clients = []

  start_thread()

  WebServer.start(self)
  WebSocket.start(self)

  EM.run
end

.start_cameraObject



385
386
387
388
389
# File 'lib/ipcam/main.rb', line 385

def start_camera
  raise("state violation") if @state != :STOP and @state != :ABORT

  start_thread()
end

.start_threadObject



43
44
45
# File 'lib/ipcam/main.rb', line 43

def start_thread
  @cam_thr = Thread.new {camera_thread}
end

.stopObject



36
37
38
39
40
41
# File 'lib/ipcam/main.rb', line 36

def stop
  stop_thread()

  WebServer.stop
  EM.stop
end

.stop?Boolean

Returns:

  • (Boolean)


405
406
407
# File 'lib/ipcam/main.rb', line 405

def stop?
  @state == :STOP
end

.stop_cameraObject



391
392
393
394
395
# File 'lib/ipcam/main.rb', line 391

def stop_camera
  raise("state violation") if @state != :ALIVE

  stop_thread()
end

.stop_threadObject



48
49
50
51
52
# File 'lib/ipcam/main.rb', line 48

def stop_thread
  @cam_thr.raise(Stop)
  @cam_thr.join
  @cam_thr = nil
end