Class: Shoes::VideoVlc

Inherits:
Object
  • Object
show all
Includes:
Vlc
Defined in:
lib/shoes/videoffi.rb

Constant Summary

Constants included from Vlc

Vlc::Audio_track, Vlc::LIBVLC_TRACK_AUDIO, Vlc::LIBVLC_TRACK_TEXT, Vlc::LIBVLC_TRACK_UNKNOWN, Vlc::LIBVLC_TRACK_VIDEO, Vlc::Media_stats, Vlc::Media_track, Vlc::Subtitle_track, Vlc::VLC_FUNCTIONS_MAP, Vlc::Video_track

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Vlc

extern, import_symbols, load_lib, vlc_import_done

Constructor Details

#initialize(app, path, attr = nil) ⇒ VideoVlc

Returns a new instance of VideoVlc


234
235
236
237
238
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/shoes/videoffi.rb', line 234

def initialize(app, path, attr=nil)
  @path = path
  attr ||= {}
  @autoplay = attr[:autoplay] || false
  
  Vlc.load_lib unless Vlc.vlc_import_done
  @version = libvlc_get_version
  
  # user gives an array of string options i.e.
  #   vlc_options: ["--no-xlib", "--no-video-title-show"]
  #   libvlc expects : libvlc_new(2, ["--no-xlib", "--no-video-title-show"].pack('p2'))
  opts = attr.delete(:vlc_options)
  sz, args = 0, nil
  if opts
      sz = opts.size
      args = opts.pack("p#{sz}")
  end
  
  @vlci = libvlc_new(sz, args)
  if @vlci.null?
      upraise "Unable to initialize libvlc\nlibvlc_new() failed with  :vlc_options => #{opts.inspect}\n" \
             "and returned : \n#{@vlci.inspect}" 
  end
  
  @player = libvlc_media_player_new(@vlci)
  @list_player = libvlc_media_list_player_new(@vlci)
  libvlc_media_list_player_set_media_player(@list_player, @player)
  @medialist = libvlc_media_list_new(@vlci)
  libvlc_media_list_player_set_media_list(@list_player, @medialist)
  
  @loaded = load_media @path
  vol = attr[:volume] || 85
  libvlc_audio_set_volume(@player, vol)
  attr[:video_width] = video_track_width if video_track_width
  attr[:video_height] = video_track_height if video_track_height
  
  @video = app.video_c attr

  # we must wait for parent (hence video itself) to be drawn in order to get the widget drawable
  # (keep "start" event free for possible use in Shoes script)
  # using "animate" method because of the underlying "g_timeout_add" function (let's have peaceful relations with gtk)
  @wait_ready = app.animate(100) do |fr|
    if @video.drawable_ready?
      @wait_ready.stop
      drID = @video.drawable   # xlib window / HWND / NSView  id
      case RUBY_PLATFORM
        when /linux/
          if libvlc_media_player_get_xwindow(@player) == 0
            libvlc_media_player_set_xwindow(@player, drID)
          end
        when /mingw/
          if libvlc_media_player_get_hwnd(@player).null?
            libvlc_media_player_set_hwnd(@player, drID)
          end
        when /darwin/
          if libvlc_media_player_get_nsobject(@player).null?
            libvlc_media_player_set_nsobject(@player, drID)
          end
      end
      
      # By default, mouse and keyboard events are handled by 
      # the LibVLC video widget, disabling them now!
      # only avalaible on x11 and win32
      unless RUBY_PLATFORM =~ /darwin/
        libvlc_video_set_mouse_input(@player, 0)
        libvlc_video_set_key_input(@player, 0)
      end

      play if @loaded && @autoplay
      
      @wait_ready.remove
      @wait_ready = nil
    end
  end

end

Instance Attribute Details

#autoplayObject

Returns the value of attribute autoplay


230
231
232
# File 'lib/shoes/videoffi.rb', line 230

def autoplay
  @autoplay
end

#have_audio_trackObject (readonly)

Returns the value of attribute have_audio_track


231
232
233
# File 'lib/shoes/videoffi.rb', line 231

def have_audio_track
  @have_audio_track
end

#have_video_trackObject (readonly)

Returns the value of attribute have_video_track


231
232
233
# File 'lib/shoes/videoffi.rb', line 231

def have_video_track
  @have_video_track
end

#loadedObject (readonly)

Returns the value of attribute loaded


231
232
233
# File 'lib/shoes/videoffi.rb', line 231

def loaded
  @loaded
end

#pathObject

Returns the value of attribute path


231
232
233
# File 'lib/shoes/videoffi.rb', line 231

def path
  @path
end

#playerObject (readonly)

Returns the value of attribute player


231
232
233
# File 'lib/shoes/videoffi.rb', line 231

def player
  @player
end

#versionObject (readonly)

Returns the value of attribute version


231
232
233
# File 'lib/shoes/videoffi.rb', line 231

def version
  @version
end

#video_track_heightObject (readonly)

Returns the value of attribute video_track_height


231
232
233
# File 'lib/shoes/videoffi.rb', line 231

def video_track_height
  @video_track_height
end

#video_track_widthObject (readonly)

Returns the value of attribute video_track_width


231
232
233
# File 'lib/shoes/videoffi.rb', line 231

def video_track_width
  @video_track_width
end

Instance Method Details

#access_media_list {|@medialist| ... } ⇒ Object

Yields:

  • (@medialist)

379
380
381
382
383
# File 'lib/shoes/videoffi.rb', line 379

def access_media_list
  libvlc_media_list_lock(@medialist)
  yield @medialist
  libvlc_media_list_unlock(@medialist)
end

#displace(x, y) ⇒ Object


502
# File 'lib/shoes/videoffi.rb', line 502

def displace(x, y); @video.displace(x, y); end

#heightObject

ditto


505
# File 'lib/shoes/videoffi.rb', line 505

def height; @video.height; end

#hideObject


485
# File 'lib/shoes/videoffi.rb', line 485

def hide; @video.hide; end

#leftObject


506
# File 'lib/shoes/videoffi.rb', line 506

def left; @video.left; end

#lengthObject


430
431
432
433
# File 'lib/shoes/videoffi.rb', line 430

def length
  # in ms, or -1 if there is no media.
  libvlc_media_player_get_length(@player) #(buggy, might be removed)
end

#load_media(path) ⇒ Object


311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/shoes/videoffi.rb', line 311

def load_media(path)
  @have_video_track = @have_audio_track = nil
  @video_track_width = @video_track_height = nil
  
  # empty string given, nothing to do, waiting for media to be loaded later
  return nil if @path.empty?
  
  @media = 
  if path =~ %r{://}
    libvlc_media_new_location(@vlci, path)
  else
    upraise "Sorry, That File doesn't exists :\n#{path} !" unless File.exists? path
    libvlc_media_new_path(@vlci, path)
  end
  
  if @media.null?  # TODO media is not a null pointer in case of failure ...
      upraise "Sorry, i can't load that media! :\n#{path}"
  end

  access_media_list do |medialist|
    libvlc_media_list_remove_index(medialist, 0) if (libvlc_media_list_count(medialist) != 0)
    libvlc_media_list_add_media(medialist, @media);
  end

  ### Get some info about the tracks inside the @media 
  ### how to deal with a C array OUT parameter (tracks_buf.ref)
  libvlc_media_parse(@media)
  
  tracks_buf = Fiddle::Pointer.malloc(Media_track.size)
  n_tracks = libvlc_media_tracks_get( @media, tracks_buf.ref )
  libvlc_media_release(@media)

  ptr_size, unpkf = 8, 'Q'      # 64 bits arch
  if Fiddle::SIZEOF_VOIDP == 4  # 32 bits arch
    ptr_size = 4
    unpkf = 'L'
  end
#    We could also have taken the unpack route
#     (0...n_tracks).each do |i|
#        tr_p = tracks_buf[ptr_size*i, ptr_size]
#        tr_pv = Fiddle::Pointer.new(tr_p.unpack(unpkf)[0])[0, Media_track.size] 
#        i_type = tr_pv[12,4].unpack('i')[0]
#        ...
#     end

  (0...n_tracks).each do |i|
    tr_p = tracks_buf[ptr_size*i, ptr_size]   # get the pointer (4/8 bytes) at index i of the tracks_buf array
    tr_pa = tr_p.unpack(unpkf)[0]             # get the address (integer) of that pointer
    mdt = Media_track.new(tr_pa)              # use Fiddle structure wrapper

    if mdt.i_type == LIBVLC_TRACK_VIDEO
      @have_video_track = true
      v = Video_track.new Fiddle::Pointer[mdt.kind]
      @video_track_width = v.i_width
      @video_track_height = v.i_height
    end

    if mdt.i_type == LIBVLC_TRACK_AUDIO
      @have_audio_track = true
#        a = Audio_track.new Fiddle::Pointer[mdt.kind]
    end
#       puts "mdt.psz_language : #{mdt.psz_language}" unless mdt.psz_language.null?
  end
  libvlc_media_tracks_release(tracks_buf, n_tracks);

  true
end

#move(x, y) ⇒ Object


503
# File 'lib/shoes/videoffi.rb', line 503

def move(x, y); @video.move(x, y); end

#next_mediaObject


455
456
457
458
# File 'lib/shoes/videoffi.rb', line 455

def next_media
  r = libvlc_media_list_player_next(@list_player)
  return r == 0 ? true : false
end

#parentObject


487
# File 'lib/shoes/videoffi.rb', line 487

def parent; @video.parent; end

#pauseObject


408
409
410
# File 'lib/shoes/videoffi.rb', line 408

def pause
  libvlc_media_list_player_pause(@list_player)
end

#playObject


404
405
406
# File 'lib/shoes/videoffi.rb', line 404

def play
  libvlc_media_list_player_play(@list_player)
end

#play_at(index) ⇒ Object


478
479
480
481
# File 'lib/shoes/videoffi.rb', line 478

def play_at(index)
    r = libvlc_media_list_player_play_item_at_index(@list_player, index)
    return r == 0 ? true : false
end

#play_list_add(path) ⇒ Object


470
471
472
473
474
475
476
# File 'lib/shoes/videoffi.rb', line 470

def play_list_add(path)
    r = nil
    access_media_list do |medialist|
      r = libvlc_media_list_add_media(medialist, libvlc_media_new_path(@vlci, path))
    end
    r == 0 ? true : false
end

#play_media(path) ⇒ Object


465
466
467
468
# File 'lib/shoes/videoffi.rb', line 465

def play_media(path)
    media = libvlc_media_new_path(@vlci, path)
    libvlc_media_list_player_play_item(@list_player, media)
end

#playing?Boolean

Returns:

  • (Boolean)

416
417
418
# File 'lib/shoes/videoffi.rb', line 416

def playing?
  libvlc_media_list_player_is_playing(@list_player) == 1 ? true : false
end

#positionObject


445
446
447
448
# File 'lib/shoes/videoffi.rb', line 445

def position
  #  percentage between 0.0 and 1.0
  libvlc_media_player_get_position(@player)
end

#position=(pos) ⇒ Object


450
451
452
453
# File 'lib/shoes/videoffi.rb', line 450

def position=(pos)
  #  percentage between 0.0 and 1.0
  libvlc_media_player_set_position(@player, pos)
end

#previous_mediaObject


460
461
462
463
# File 'lib/shoes/videoffi.rb', line 460

def previous_media
  r = libvlc_media_list_player_previous(@list_player)
  return r == 0 ? true : false
end

#removeObject


488
489
490
491
492
493
494
495
496
# File 'lib/shoes/videoffi.rb', line 488

def remove
  libvlc_media_list_release(@medialist)
  libvlc_media_player_release(@player)
  libvlc_media_list_player_release(@list_player)
  libvlc_release(@vlci)
  @vlci  = @list_player = @player = @medialist = nil
  @video.remove
  @video = nil
end

#showObject

redirecting to shoes C video methods


484
# File 'lib/shoes/videoffi.rb', line 484

def show; @video.show; end

#stopObject


412
413
414
# File 'lib/shoes/videoffi.rb', line 412

def stop
  libvlc_media_list_player_stop(@list_player)
end

#style(attrb = nil) ⇒ Object


498
499
500
501
# File 'lib/shoes/videoffi.rb', line 498

def style(attrb=nil)
  return @video.style if attrb == nil
  @video.style(attrb)
end

#timeObject


435
436
437
438
# File 'lib/shoes/videoffi.rb', line 435

def time
  # in ms
  libvlc_media_player_get_time(@player)
end

#time=(time) ⇒ Object


440
441
442
443
# File 'lib/shoes/videoffi.rb', line 440

def time=(time)
  # in ms
  libvlc_media_player_set_time(@player, time)
end

#toggleObject


486
# File 'lib/shoes/videoffi.rb', line 486

def toggle; @video.toggle; end

#tooltipObject


509
# File 'lib/shoes/videoffi.rb', line 509

def tooltip; @video.tooltip; end

#tooltip=(tooltip) ⇒ Object


510
# File 'lib/shoes/videoffi.rb', line 510

def tooltip=(tooltip); @video.tooltip = tooltip; end

#topObject


507
# File 'lib/shoes/videoffi.rb', line 507

def top; @video.top; end

#volumeObject


420
421
422
423
# File 'lib/shoes/videoffi.rb', line 420

def volume
  # software volume in percents (0 = mute, 100 = nominal / 0dB)
  libvlc_audio_get_volume(@player)
end

#volume=(vol) ⇒ Object


425
426
427
428
# File 'lib/shoes/videoffi.rb', line 425

def volume=(vol)
  # software volume in percents (0 = mute, 100 = nominal / 0dB)
  libvlc_audio_set_volume(@player, vol)
end

#widthObject

widget width, not video track width


504
# File 'lib/shoes/videoffi.rb', line 504

def width; @video.width; end