1 | # ---------------------------------------------------------------------- |
---|
2 | # COMPONENT: videopreview - previewing movies |
---|
3 | # |
---|
4 | # ====================================================================== |
---|
5 | # AUTHOR: Derrick Kearney, Purdue University |
---|
6 | # Copyright (c) 2005-2010 Purdue Research Foundation |
---|
7 | # |
---|
8 | # See the file "license.terms" for information on usage and redistribution of |
---|
9 | # this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
10 | # ====================================================================== |
---|
11 | |
---|
12 | package require Itk |
---|
13 | package require BLT |
---|
14 | package require Img |
---|
15 | package require Rappture |
---|
16 | package require RapptureGUI |
---|
17 | |
---|
18 | option add *Video.width 300 widgetDefault |
---|
19 | option add *Video.height 300 widgetDefault |
---|
20 | option add *Video.font \ |
---|
21 | -*-helvetica-medium-r-normal-*-12-* widgetDefault |
---|
22 | |
---|
23 | itcl::class Rappture::VideoPreview { |
---|
24 | inherit itk::Widget |
---|
25 | |
---|
26 | itk_option define -width width Width -1 |
---|
27 | itk_option define -height height Height -1 |
---|
28 | itk_option define -variable variable Variable "" |
---|
29 | |
---|
30 | constructor { args } { |
---|
31 | # defined below |
---|
32 | } |
---|
33 | destructor { |
---|
34 | # defined below |
---|
35 | } |
---|
36 | |
---|
37 | public method load {type data} |
---|
38 | public method loadcb {args} |
---|
39 | public method video {args} |
---|
40 | public method query {type} |
---|
41 | |
---|
42 | protected method Play {} |
---|
43 | protected method Seek {n} |
---|
44 | protected method fixSize {} |
---|
45 | protected method _fixValue {args} |
---|
46 | protected method Upload {args} |
---|
47 | protected method eventually {args} |
---|
48 | |
---|
49 | |
---|
50 | private common _settings |
---|
51 | private common _pendings |
---|
52 | |
---|
53 | private variable _variable "" ;# quick way to change movie |
---|
54 | private variable _path "" ;# path of movie |
---|
55 | private variable _width -1 ;# start x for rubberbanding |
---|
56 | private variable _height -1 ;# start x for rubberbanding |
---|
57 | private variable _movie "" ;# movie we grab images from |
---|
58 | private variable _lastFrame 0 ;# last frame in the movie |
---|
59 | private variable _imh "" ;# current image being displayed |
---|
60 | private variable _id "" ;# id of the next play command from after |
---|
61 | private variable _framerate 30 ;# video frame rate |
---|
62 | private variable _mspf 7 ;# milliseconds per frame wait time |
---|
63 | private variable _ofrd 19 ;# observed frame retrieval delay of |
---|
64 | ;# underlying c lib in milliseconds |
---|
65 | private variable _delay 0 ;# milliseconds between play calls |
---|
66 | private variable _nextframe 0 ;# |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | itk::usual VideoPreview { |
---|
71 | } |
---|
72 | |
---|
73 | # ---------------------------------------------------------------------- |
---|
74 | # CONSTRUCTOR |
---|
75 | # ---------------------------------------------------------------------- |
---|
76 | itcl::body Rappture::VideoPreview::constructor {args} { |
---|
77 | |
---|
78 | array set _settings [subst { |
---|
79 | $this-framenum 0 |
---|
80 | $this-play 0 |
---|
81 | }] |
---|
82 | |
---|
83 | array set _pendings [subst { |
---|
84 | play 0 |
---|
85 | }] |
---|
86 | |
---|
87 | itk_component add main { |
---|
88 | canvas $itk_interior.main \ |
---|
89 | -background black |
---|
90 | } { |
---|
91 | usual |
---|
92 | rename -background -controlbackground controlBackground Background |
---|
93 | } |
---|
94 | bind $itk_component(main) <Configure> [itcl::code $this fixSize] |
---|
95 | |
---|
96 | # hold the video frames in an image on the canvas |
---|
97 | set _imh [image create photo] |
---|
98 | $itk_component(main) create image 0 0 -anchor nw -image $_imh |
---|
99 | |
---|
100 | # setup movie controls |
---|
101 | itk_component add moviecontrols { |
---|
102 | frame $itk_interior.moviecontrols |
---|
103 | } { |
---|
104 | usual |
---|
105 | rename -background -controlbackground controlBackground Background |
---|
106 | } |
---|
107 | |
---|
108 | # setup frame number frame |
---|
109 | itk_component add frnumfr { |
---|
110 | frame $itk_component(moviecontrols).frnumfr |
---|
111 | } { |
---|
112 | usual |
---|
113 | rename -background -controlbackground controlBackground Background |
---|
114 | } |
---|
115 | |
---|
116 | set imagesDir [file join $RapptureGUI::library scripts images] |
---|
117 | |
---|
118 | |
---|
119 | # Play |
---|
120 | itk_component add play { |
---|
121 | Rappture::PushButton $itk_component(moviecontrols).play \ |
---|
122 | -onimage [Rappture::icon flow-pause] \ |
---|
123 | -offimage [Rappture::icon flow-play] \ |
---|
124 | -variable [itcl::scope _settings($this-play)] \ |
---|
125 | -command [itcl::code $this video play] |
---|
126 | } |
---|
127 | set fg [option get $itk_component(hull) font Font] |
---|
128 | Rappture::Tooltip::for $itk_component(play) \ |
---|
129 | "Play/Pause movie" |
---|
130 | |
---|
131 | # Video Dial Major |
---|
132 | itk_component add dialmajor { |
---|
133 | Rappture::Videodial1 $itk_component(moviecontrols).dialmajor \ |
---|
134 | -length 10 -valuewidth 0 -valuepadding 0 -padding 6 \ |
---|
135 | -linecolor "" -activelinecolor "" \ |
---|
136 | -min 0 -max 1 \ |
---|
137 | -variable [itcl::scope _settings($this-framenum)] \ |
---|
138 | -dialoutlinecolor black \ |
---|
139 | -knobimage [Rappture::icon knob2] -knobposition center@middle |
---|
140 | } { |
---|
141 | usual |
---|
142 | ignore -dialprogresscolor |
---|
143 | rename -background -controlbackground controlBackground Background |
---|
144 | } |
---|
145 | $itk_component(dialmajor) current 0 |
---|
146 | bind $itk_component(dialmajor) <<Value>> [itcl::code $this video update] |
---|
147 | |
---|
148 | itk_component add framenumlabel { |
---|
149 | label $itk_component(frnumfr).framenuml -text "Frame:" -font $fg \ |
---|
150 | -highlightthickness 0 |
---|
151 | } { |
---|
152 | usual |
---|
153 | ignore -highlightthickness |
---|
154 | rename -background -controlbackground controlBackground Background |
---|
155 | } |
---|
156 | |
---|
157 | # Current Frame Number |
---|
158 | itk_component add framenum { |
---|
159 | label $itk_component(frnumfr).framenum \ |
---|
160 | -width 5 -font "arial 9" \ |
---|
161 | -textvariable [itcl::scope _settings($this-framenum)] |
---|
162 | } { |
---|
163 | usual |
---|
164 | ignore -highlightthickness |
---|
165 | rename -background -controlbackground controlBackground Background |
---|
166 | } |
---|
167 | Rappture::Tooltip::for $itk_component(framenum) \ |
---|
168 | "Current frame number" |
---|
169 | |
---|
170 | |
---|
171 | pack $itk_component(framenumlabel) -side left |
---|
172 | pack $itk_component(framenum) -side right |
---|
173 | |
---|
174 | |
---|
175 | blt::table $itk_component(moviecontrols) \ |
---|
176 | 0,0 $itk_component(play) -padx {2 0} \ |
---|
177 | 0,1 $itk_component(dialmajor) -fill x -padx {2 2} \ |
---|
178 | 0,2 $itk_component(frnumfr) -padx {2 4} |
---|
179 | |
---|
180 | blt::table configure $itk_component(moviecontrols) c* -resize none |
---|
181 | blt::table configure $itk_component(moviecontrols) c1 -resize both |
---|
182 | blt::table configure $itk_component(moviecontrols) r0 -pady 1 |
---|
183 | |
---|
184 | |
---|
185 | blt::table $itk_interior \ |
---|
186 | 0,0 $itk_component(main) -fill both \ |
---|
187 | 1,0 $itk_component(moviecontrols) -fill x |
---|
188 | blt::table configure $itk_interior c* -resize both |
---|
189 | blt::table configure $itk_interior r0 -resize both |
---|
190 | blt::table configure $itk_interior r1 -resize both |
---|
191 | |
---|
192 | eval itk_initialize $args |
---|
193 | |
---|
194 | $itk_component(main) configure -background black |
---|
195 | puts "width = [$itk_component(main) cget -width]" |
---|
196 | puts "height = [$itk_component(main) cget -height]" |
---|
197 | } |
---|
198 | |
---|
199 | # ---------------------------------------------------------------------- |
---|
200 | # DESTRUCTOR |
---|
201 | # ---------------------------------------------------------------------- |
---|
202 | itcl::body Rappture::VideoPreview::destructor {} { |
---|
203 | array unset _settings $this-* |
---|
204 | if {[info exists _imh]} { |
---|
205 | image delete $_imh |
---|
206 | } |
---|
207 | } |
---|
208 | |
---|
209 | # ---------------------------------------------------------------------- |
---|
210 | # load - load a video file |
---|
211 | # type - type of data, "data" or "file" |
---|
212 | # data - what to load. |
---|
213 | # if type == "data", data is treated like binary data |
---|
214 | # if type == "file", data is treated like the name of a file |
---|
215 | # and is opened and then loaded. |
---|
216 | # ---------------------------------------------------------------------- |
---|
217 | itcl::body Rappture::VideoPreview::load {type data} { |
---|
218 | |
---|
219 | # open the file |
---|
220 | |
---|
221 | set fname "" |
---|
222 | switch $type { |
---|
223 | "data" { |
---|
224 | set fname "/tmp/tmpVV[pid].video" |
---|
225 | set fid [open $fname "w"] |
---|
226 | fconfigure $fid -translation binary -encoding binary |
---|
227 | puts $fid $data |
---|
228 | close $fid |
---|
229 | set type "file" |
---|
230 | set data $fname |
---|
231 | } |
---|
232 | "file" { |
---|
233 | # do nothing |
---|
234 | } |
---|
235 | default { |
---|
236 | error "bad value: \"$type\": should be \"load \[data|file\] <data>\"" |
---|
237 | } |
---|
238 | } |
---|
239 | |
---|
240 | |
---|
241 | if {([info exists _movie]) && ("" != ${_movie})} { |
---|
242 | ${_movie} release |
---|
243 | } |
---|
244 | |
---|
245 | set _movie [Rappture::Video $type $data] |
---|
246 | file delete $fname |
---|
247 | set _framerate [${_movie} get framerate] |
---|
248 | set _mspf [expr round(((1.0/${_framerate})*1000))] |
---|
249 | set _delay [expr {${_mspf} - ${_ofrd}}] |
---|
250 | |
---|
251 | video seek 0 |
---|
252 | |
---|
253 | # update the dial and framenum widgets |
---|
254 | set _settings($this-framenum) 0 |
---|
255 | |
---|
256 | # setup the image display |
---|
257 | |
---|
258 | foreach {w h} [query dimensions] break |
---|
259 | if {${_width} == -1} { |
---|
260 | set _width $w |
---|
261 | } |
---|
262 | if {${_height} == -1} { |
---|
263 | set _height $h |
---|
264 | } |
---|
265 | |
---|
266 | set _lastFrame [$_movie get position end] |
---|
267 | |
---|
268 | # update the dial with video information |
---|
269 | $itk_component(dialmajor) configure -min 0 -max ${_lastFrame} |
---|
270 | |
---|
271 | fixSize |
---|
272 | } |
---|
273 | |
---|
274 | # ---------------------------------------------------------------------- |
---|
275 | # loadcb - load callback |
---|
276 | # ---------------------------------------------------------------------- |
---|
277 | itcl::body Rappture::VideoPreview::loadcb {args} { |
---|
278 | |
---|
279 | Rappture::filexfer::upload {piv tool} {id label desc} [itcl::code $this Upload] |
---|
280 | |
---|
281 | #uplevel 1 [list $args] |
---|
282 | } |
---|
283 | |
---|
284 | # ---------------------------------------------------------------------- |
---|
285 | # Upload - |
---|
286 | # ---------------------------------------------------------------------- |
---|
287 | itcl::body Rappture::VideoPreview::Upload {args} { |
---|
288 | array set data $args |
---|
289 | |
---|
290 | if {[info exists data(error)]} { |
---|
291 | Rappture::Tooltip::cue $itk::component(main) $data(error) |
---|
292 | puts stderr $data(error) |
---|
293 | } |
---|
294 | |
---|
295 | if {[info exists data(path)] && [info exists data(data)]} { |
---|
296 | Rappture::Tooltip::cue hide ;# take down note about the popup window |
---|
297 | |
---|
298 | # load data |
---|
299 | load "data" $data(data) |
---|
300 | } |
---|
301 | |
---|
302 | } |
---|
303 | |
---|
304 | # ---------------------------------------------------------------------- |
---|
305 | # USAGE: _fixValue ?<name1> <name2> <op>? |
---|
306 | # |
---|
307 | # Invoked automatically whenever the -variable associated with this |
---|
308 | # widget is modified. Copies the value to the current settings for |
---|
309 | # the widget. |
---|
310 | # ---------------------------------------------------------------------- |
---|
311 | itcl::body Rappture::VideoPreview::_fixValue {args} { |
---|
312 | if {"" == $itk_option(-variable)} { |
---|
313 | return |
---|
314 | } |
---|
315 | upvar #0 $itk_option(-variable) var |
---|
316 | if {[file readable $var]} { |
---|
317 | # load and start playing the video |
---|
318 | set _path $var |
---|
319 | load file ${_path} |
---|
320 | set _settings($this-play) 1 |
---|
321 | video play |
---|
322 | } |
---|
323 | } |
---|
324 | |
---|
325 | # ---------------------------------------------------------------------- |
---|
326 | # fixSize |
---|
327 | # ---------------------------------------------------------------------- |
---|
328 | itcl::body Rappture::VideoPreview::fixSize {} { |
---|
329 | |
---|
330 | if {[string compare "" ${_movie}] == 0} { |
---|
331 | return |
---|
332 | } |
---|
333 | |
---|
334 | set cw [winfo width $itk_component(main)] |
---|
335 | set ch [winfo height $itk_component(main)] |
---|
336 | |
---|
337 | # FIXME: right now we only deal with 16:9 ratio videos |
---|
338 | # always keep the aspect ratio correct |
---|
339 | set _width [expr int(int($cw/16.0)*16.0)] |
---|
340 | set _height [expr int(${_width}/(16.0/9.0))] |
---|
341 | if {${_height} > $ch} { |
---|
342 | # if height is limiting us, shrink some more |
---|
343 | set _height [expr int(int($ch/9.0)*9.0)] |
---|
344 | set _width [expr int(${_height}*(16.0/9.0))] |
---|
345 | } |
---|
346 | |
---|
347 | # get an image with the new size |
---|
348 | ${_imh} blank |
---|
349 | ${_imh} put [${_movie} get image ${_width} ${_height}] |
---|
350 | |
---|
351 | # make the canvas fit the image |
---|
352 | $itk_component(main) configure -width ${_width} -height ${_height} |
---|
353 | puts "bbox = [$itk_component(main) bbox all]" |
---|
354 | } |
---|
355 | |
---|
356 | # ---------------------------------------------------------------------- |
---|
357 | # video - play, stop, rewind, fastforward the video |
---|
358 | # ---------------------------------------------------------------------- |
---|
359 | itcl::body Rappture::VideoPreview::video { args } { |
---|
360 | set option [lindex $args 0] |
---|
361 | switch -- $option { |
---|
362 | "play" { |
---|
363 | if {$_settings($this-play) == 1} { |
---|
364 | eventually play |
---|
365 | } else { |
---|
366 | # pause/stop |
---|
367 | after cancel $_id |
---|
368 | set _pendings(play) 0 |
---|
369 | set _settings($this-play) 0 |
---|
370 | } |
---|
371 | } |
---|
372 | "seek" { |
---|
373 | Seek [lreplace $args 0 0] |
---|
374 | } |
---|
375 | "stop" { |
---|
376 | after cancel $_id |
---|
377 | set _settings($this-play) 0 |
---|
378 | } |
---|
379 | "update" { |
---|
380 | # eventually seek [expr round($_settings($this-framenum))] |
---|
381 | Seek [expr round($_settings($this-framenum))] |
---|
382 | } |
---|
383 | default { |
---|
384 | error "bad option \"$option\": should be play, stop, toggle, position, or reset." |
---|
385 | } |
---|
386 | } |
---|
387 | } |
---|
388 | |
---|
389 | # ---------------------------------------------------------------------- |
---|
390 | # query - query things about the video |
---|
391 | # |
---|
392 | # dimensions - returns width and height as a list |
---|
393 | # frames - number of frames in video (last frame + 1) |
---|
394 | # framenum - current position |
---|
395 | # ---------------------------------------------------------------------- |
---|
396 | itcl::body Rappture::VideoPreview::query { type } { |
---|
397 | set ret "" |
---|
398 | switch -- $type { |
---|
399 | "dimensions" { |
---|
400 | set ret [${_movie} size] |
---|
401 | } |
---|
402 | "frames" { |
---|
403 | set ret [expr [${_movie} get position end] + 1] |
---|
404 | } |
---|
405 | "framenum" { |
---|
406 | set ret [${_movie} get position cur] |
---|
407 | } |
---|
408 | default { |
---|
409 | error "bad type \"$type\": should be dimensions, frames, framenum." |
---|
410 | } |
---|
411 | } |
---|
412 | return $ret |
---|
413 | } |
---|
414 | |
---|
415 | # ---------------------------------------------------------------------- |
---|
416 | # Play - get the next video frame |
---|
417 | # ---------------------------------------------------------------------- |
---|
418 | itcl::body Rappture::VideoPreview::Play {} { |
---|
419 | |
---|
420 | set cur ${_nextframe} |
---|
421 | |
---|
422 | # time how long it takes to retrieve the next frame |
---|
423 | set _ofrd [time { |
---|
424 | # use seek instead of next fxn incase the ${_nextframe} is |
---|
425 | # not the current frame+1. this happens when we skip frames |
---|
426 | # because the underlying c lib is too slow at reading. |
---|
427 | $_movie seek $cur |
---|
428 | $_imh put [$_movie get image ${_width} ${_height}] |
---|
429 | } 1] |
---|
430 | regexp {(\d+\.?\d*) microseconds per iteration} ${_ofrd} match _ofrd |
---|
431 | set _ofrd [expr {round(${_ofrd}/1000)}] |
---|
432 | |
---|
433 | # calculate the delay we shoud see |
---|
434 | # between frames being placed on screen |
---|
435 | # taking into account the cost of retrieving the frame |
---|
436 | set _delay [expr {${_mspf}-${_ofrd}}] |
---|
437 | if {0 > ${_delay}} { |
---|
438 | set _delay 0 |
---|
439 | } |
---|
440 | |
---|
441 | set cur [$_movie get position cur] |
---|
442 | |
---|
443 | # update the dial and framenum widgets |
---|
444 | set _settings($this-framenum) $cur |
---|
445 | |
---|
446 | # no play cmds pending |
---|
447 | set _pendings(play) 0 |
---|
448 | |
---|
449 | # schedule the next frame to be displayed |
---|
450 | if {$cur < ${_lastFrame}} { |
---|
451 | set _id [after ${_delay} [itcl::code $this eventually play]] |
---|
452 | } else { |
---|
453 | video stop |
---|
454 | } |
---|
455 | |
---|
456 | event generate $itk_component(hull) <<Frame>> |
---|
457 | } |
---|
458 | |
---|
459 | |
---|
460 | # ---------------------------------------------------------------------- |
---|
461 | # Seek - go to a frame in the video |
---|
462 | # Seek +5 |
---|
463 | # Seek -5 |
---|
464 | # Seek 35 |
---|
465 | # ---------------------------------------------------------------------- |
---|
466 | itcl::body Rappture::VideoPreview::Seek {args} { |
---|
467 | set val [lindex $args 0] |
---|
468 | if {"" == $val} { |
---|
469 | error "bad value: \"$val\": should be \"seek value\"" |
---|
470 | } |
---|
471 | set cur [$_movie get position cur] |
---|
472 | if {[string compare $cur $val] == 0} { |
---|
473 | # already at the frame to seek to |
---|
474 | return |
---|
475 | } |
---|
476 | ${_movie} seek $val |
---|
477 | ${_imh} put [${_movie} get image ${_width} ${_height}] |
---|
478 | |
---|
479 | # update the dial and framenum widgets |
---|
480 | set _settings($this-framenum) [$_movie get position cur] |
---|
481 | event generate $itk_component(main) <<Frame>> |
---|
482 | |
---|
483 | } |
---|
484 | |
---|
485 | |
---|
486 | # ---------------------------------------------------------------------- |
---|
487 | # eventually - |
---|
488 | # seek |
---|
489 | # play |
---|
490 | # ---------------------------------------------------------------------- |
---|
491 | itcl::body Rappture::VideoPreview::eventually {args} { |
---|
492 | set option [lindex $args 0] |
---|
493 | switch -- $option { |
---|
494 | "seek" { |
---|
495 | if {$_pendings(seek) == 0} { |
---|
496 | # no seek pending, schedule one |
---|
497 | set $_pendings(seek) 1 |
---|
498 | after idle [itcl::code $this Seek [lindex $args 1]] |
---|
499 | } else { |
---|
500 | # there is a seek pending, update its seek value |
---|
501 | } |
---|
502 | } |
---|
503 | "play" { |
---|
504 | if {0 == $_pendings(play)} { |
---|
505 | # no play pending schedule one |
---|
506 | set _pendings(play) 1 |
---|
507 | set _nextframe [expr {[$_movie get position cur] + 1}] |
---|
508 | after idle [itcl::code $this Play] |
---|
509 | } else { |
---|
510 | # there is a play pending, update its frame value |
---|
511 | incr _nextframe |
---|
512 | } |
---|
513 | } |
---|
514 | default { |
---|
515 | } |
---|
516 | } |
---|
517 | } |
---|
518 | |
---|
519 | # ---------------------------------------------------------------------- |
---|
520 | # CONFIGURE: -variable |
---|
521 | # ---------------------------------------------------------------------- |
---|
522 | itcl::configbody Rappture::VideoPreview::variable { |
---|
523 | if {"" != $_variable} { |
---|
524 | upvar #0 $_variable var |
---|
525 | trace remove variable var write [itcl::code $this _fixValue] |
---|
526 | } |
---|
527 | |
---|
528 | set _variable $itk_option(-variable) |
---|
529 | |
---|
530 | if {"" != $_variable} { |
---|
531 | upvar #0 $_variable var |
---|
532 | trace add variable var write [itcl::code $this _fixValue] |
---|
533 | |
---|
534 | # sync to the current value of this variable |
---|
535 | if {[info exists var]} { |
---|
536 | _fixValue |
---|
537 | } |
---|
538 | } |
---|
539 | } |
---|
540 | |
---|
541 | # ---------------------------------------------------------------------- |
---|
542 | # OPTION: -width |
---|
543 | # ---------------------------------------------------------------------- |
---|
544 | itcl::configbody Rappture::VideoPreview::width { |
---|
545 | # $_dispatcher event -idle !fixsize |
---|
546 | if {[string is integer $itk_option(-width)] == 0} { |
---|
547 | error "bad value: \"$itk_option(-width)\": width should be an integer" |
---|
548 | } |
---|
549 | set _width $itk_option(-width) |
---|
550 | after idle [itcl::code $this fixSize] |
---|
551 | } |
---|
552 | |
---|
553 | # ---------------------------------------------------------------------- |
---|
554 | # OPTION: -height |
---|
555 | # ---------------------------------------------------------------------- |
---|
556 | itcl::configbody Rappture::VideoPreview::height { |
---|
557 | # $_dispatcher event -idle !fixsize |
---|
558 | if {[string is integer $itk_option(-height)] == 0} { |
---|
559 | error "bad value: \"$itk_option(-height)\": height should be an integer" |
---|
560 | } |
---|
561 | set _height $itk_option(-height) |
---|
562 | after idle [itcl::code $this fixSize] |
---|
563 | } |
---|