lib/HTML/Object/DOM/Element/Video.pm

filter: Statementstmt 63.1% 24 / 38 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 44.4% 8 / 18 Podpod 80.0% 8 / 10 totaltotal 57.1% 40 / 70 CC 3 SCAR 11.3
CC
3
Cov
57%
init
12.0
autoPictureInPicture
2.0
disablePictureInPicture
2.0
CRAP 3.1
« blib/lib/HTML/Object/XPath/Literal.pm ↑ Summary blib/lib/HTML/Object/XPath/Root.pm »
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/DOM/Element/Video.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2021 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/12/23
7 ## Modified 2022/09/18
8 ## All rights reserved
9 ##
10 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.
13 ##----------------------------------------------------------------------------
14 package HTML::Object::DOM::Element::Video;
15 BEGIN
16 {
17 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶ warnings::register_categories( 'HTML::Object' );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶ use parent qw( HTML::Object::DOM::Element::Media );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶ use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶ use HTML::Object::DOM::Element::Shared qw( :video );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 our $VERSION = 'v0.2.0';
24 };
25  
26 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 sub init
30 {
31 0 ▶ my $self = shift( @_ );
Subroutine init: not called CC=3 SCAR=24.8
Pod documented
32 0 $self->{_init_strict_use_sub} = 1;
33 0 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 0 0
34 0 ▶ $self->{tag} = 'video' if( !CORE::length( "$self->{tag}" ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length qq[$self->{"tag"}] 0 0 0
35 0 return( $self );
36 }
37  
38 # Note: property autoPictureInPicture
39 0 ▶sub autoPictureInPicture : lvalue { return( shift->_set_get_property({ attribute => 'autopictureinpicture', is_boolean => 1 }, @_ ) ); }
Subroutine autoPictureInPicture: not called CC=1 SCAR=6.9
Pod documented
40  
41 # Note: property disablePictureInPicture
42 0 ▶sub disablePictureInPicture : lvalue { return( shift->_set_get_property({ attribute => 'disablepictureinpicture', is_boolean => 1 }, @_ ) ); }
Subroutine disablePictureInPicture: not called CC=1 SCAR=6.9
Pod documented
43  
44 0 ▶sub getVideoPlaybackQuality { return; }
Subroutine getVideoPlaybackQuality: not called CC=1 SCAR=6.9
Pod documented
45  
46 # Note: property height is inherited
47  
48 0 ▶sub onenterpictureinpicture : lvalue { return( shift->on( 'enterpictureinpicture', @_ ) ); }
Subroutine onenterpictureinpicture: not called CC=1 SCAR=6.9
Pod undocumented
49  
50 0 ▶sub onleavepictureinpicture : lvalue { return( shift->on( 'leavepictureinpicture', @_ ) ); }
Subroutine onleavepictureinpicture: not called CC=1 SCAR=6.9
Pod undocumented
51  
52 # Note: property poster
53 0 ▶sub poster : lvalue { return( shift->_set_get_property({ attribute => 'poster', is_uri => 1 }, @_ ) ); }
Subroutine poster: not called CC=1 SCAR=6.9
Pod documented
54  
55 0 ▶sub requestPictureInPicture { return; }
Subroutine requestPictureInPicture: not called CC=1 SCAR=6.9
Pod documented
56  
57 # Note: property videoHeight read-only
58 0 ▶sub videoHeight : lvalue { return( shift->_set_get_number( 'videoheight', @_ ) ); }
Subroutine videoHeight: not called CC=1 SCAR=6.9
Pod documented
59  
60 # Note: property videoWidth read-only
61 0 ▶sub videoWidth : lvalue { return( shift->_set_get_number( 'videowidth', @_ ) ); }
Subroutine videoWidth: not called CC=1 SCAR=6.9
Pod documented
62  
63 # Note: property width is inherited
64  
65 1 1;
66 # NOTE: POD
67 __END__
68  
69 =encoding utf-8
70  
71 =head1 NAME
72  
73 HTML::Object::DOM::Element::Video - HTML Object DOM Video Class
74  
75 =head1 SYNOPSIS
76  
77 use HTML::Object::DOM::Element::Video;
78 my $video = HTML::Object::DOM::Element::Video->new ||
79 die( HTML::Object::DOM::Element::Video->error, "\n" );
80  
81 =head1 VERSION
82  
83 v0.2.0
84  
85 =head1 DESCRIPTION
86  
87 This interface provides special properties and methods for manipulating video objects. It also inherits properties and methods of L<HTML::Object::DOM::Element::Media> and L<HTML::Object::DOM::Element>.
88  
89 <video controls width="250">
90 <source src="/some/where/video.webm" type="video/webm">
91 <source src="/some/where/video.mp4" type="video/mp4">
92 Sorry, your browser does not support embedded videos.
93 </video>
94  
95 =head1 INHERITANCE
96  
97 +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+ +-----------------------------------+ +-----------------------------------+
98 | HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::Node | --> | HTML::Object::DOM::Element | --> | HTML::Object::DOM::Element::Media | --> | HTML::Object::DOM::Element::Video |
99 +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+ +-----------------------------------+ +-----------------------------------+
100  
101 =head1 PROPERTIES
102  
103 Inherits properties from its parent L<HTML::Object::DOM::Element::Media>
104  
105 =head2 autoPictureInPicture
106  
107 The autoPictureInPicture property reflects the HTML attribute indicating whether the video should enter or leave picture-in-picture mode automatically when the user switches tab and/or applications.
108  
109 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLVideoElement/autoPictureInPicture>
110  
111 =head2 disablePictureInPicture
112  
113 The C<disablePictureInPicture> property reflects the HTML attribute indicating whether the user agent should suggest the picture-in-picture feature to users, or request it automatically.
114  
115 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLVideoElement/disablePictureInPicture>
116  
117 =head2 height
118  
119 Is a string that reflects the height HTML attribute, which specifies the height of the display area, in CSS pixels.
120  
121 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLVideoElement/height>
122  
123 =head2 poster
124  
125 Is a string that reflects the poster HTML attribute, which an URL for an image to be shown while the video is downloading. If this attribute is not specified, nothing is displayed until the first frame is available, then the first frame is shown as the poster frame.
126  
127 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLVideoElement/poster>
128  
129 =head2 videoHeight
130  
131 Under perl, this returns C<undef> by default, but you can set whatever number value you want.
132  
133 Under JavaScript, this is read-only and returns an unsigned integer value indicating the intrinsic height of the resource in CSS pixels, or 0 if no media is available yet.
134  
135 Example:
136  
137 my $v = $doc->getElementById("myVideo");
138  
139 $v->addEventListener( resize => sub
140 {
141 my $w = $v->videoWidth;
142 my $h = $v->videoHeight;
143  
144 if( $w && $h )
145 {
146 $v->style->width = $w;
147 $v->style->height = $h;
148 }
149 }, { capture => 0 });
150  
151 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLVideoElement/videoHeight>
152  
153 =head2 videoWidth
154  
155 Under perl, this is read-only and returns C<undef> by default, but you can set whatever number value you want.
156  
157 Under JavaScript, this returns an unsigned integer value indicating the intrinsic width of the resource in CSS pixels, or 0 if no media is available yet.
158  
159 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLVideoElement/videoWidth>
160  
161 =head2 width
162  
163 Is a string that reflects the width HTML attribute, which specifies the width of the display area, in CSS pixels.
164  
165 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLVideoElement/width>
166  
167 =head1 METHODS
168  
169 Inherits methods from its parent L<HTML::Object::DOM::Element::Media>
170  
171 =head2 getVideoPlaybackQuality
172  
173 Under perl, this always returns C<undef>.
174  
175 Under JavaScript, this returns a C<VideoPlaybackQuality> object that contains the current playback metrics. This information includes things like the number of dropped or corrupted frames, as well as the total number of frames.
176  
177 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLVideoElement/getVideoPlaybackQuality>
178  
179 =head2 requestPictureInPicture
180  
181 Under perl, this always returns C<undef> obviously.
182  
183 Under JavaScript, this requests that the user agent make video enters picture-in-picture mode
184  
185 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLVideoElement/requestPictureInPicture>
186  
187 =head1 EVENTS
188  
189 Event listeners for those events can also be found by prepending C<on> before the event type:
190  
191 For example, C<enterpictureinpicture> event listeners can be set also with C<onenterpictureinpicture> method:
192  
193 $e->onenterpictureinpicture(sub{ # do something });
194 # or as an lvalue method
195 $e->onenterpictureinpicture = sub{ # do something };
196  
197 =head2 enterpictureinpicture
198  
199 Sent to a L<HTML::Object::DOM::Element::Video> when it enters Picture-in-Picture mode. The associated event handler is L<HTML::Object::DOM::Element::Video>.onenterpictureinpicture
200  
201 Example:
202  
203 my $video = $doc->querySelector('#$video');
204 my $button = $doc->querySelector('#$button');
205  
206 sub onEnterPip
207 {
208 say( "Picture-in-Picture mode activated!" );
209 }
210  
211 $video->addEventListener( enterpictureinpicture => \&onEnterPip, { capture => 0 });
212  
213 $button->onclick = sub
214 {
215 $video->requestPictureInPicture();
216 }
217  
218 my $video = $doc->querySelector('#$video');
219 my $button = $doc->querySelector('#$button');
220  
221 sub onEnterPip
222 {
223 say( "Picture-in-Picture mode activated!" );
224 }
225  
226 $video->onenterpictureinpicture = \&onEnterPip;
227  
228 $button->onclick = sub
229 {
230 $video->requestPictureInPicture();
231 }
232  
233 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLVideoElement/enterpictureinpicture_event>
234  
235 =head2 leavepictureinpicture
236  
237 Sent to a L<HTML::Object::DOM::Element::Video> when it leaves Picture-in-Picture mode. The associated event handler is L<HTML::Object::DOM::Element::Video>.onleavepictureinpicture
238  
239 Example:
240  
241 my $video = $doc->querySelector('#$video');
242 my $button = $doc->querySelector('#$button');
243  
244 sub onExitPip
245 {
246 say( "Picture-in-Picture mode deactivated!" );
247 }
248  
249 $video->addEventListener( leavepictureinpicture => \&onExitPip, { capture => 0 });
250  
251 $button->onclick = sub
252 {
253 if( $doc->pictureInPictureElement )
254 {
255 $doc->exitPictureInPicture();
256 }
257 }
258  
259 my $video = $doc->querySelector('#$video');
260 my $button = $doc->querySelector('#$button');
261  
262 sub onExitPip
263 {
264 say( "Picture-in-Picture mode deactivated!" );
265 }
266  
267 $video->onleavepictureinpicture = \&onExitPip;
268  
269 $button->onclick = sub
270 {
271 if( $doc->pictureInPictureElement )
272 {
273 $doc->exitPictureInPicture();
274 }
275 }
276  
277 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLVideoElement/leavepictureinpicture_event>
278  
279 =head1 EXAMPLE
280  
281 <h1>Ask a question at a given point in a video</h1>
282  
283 <p>The question is displayed after 10 seconds, if the answer given is correct, the video continues, if not it starts again from the beginning</p>
284  
285 <video id="myVideo" controls="">
286 <source src="https://example.org/some/where/videos/video.mp4" />
287 <source src="https://example.org/some/where/videos/video.webm" />
288 </video>
289  
290 // This only works in a web browser, so this is intentionally in JavaScript
291 // First identify the video in the DOM
292 var myVideo = document.getElementById( 'myVideo' );
293 myVideo.ontimeupdate = function()
294 {
295 // Remove the decimal numbers from the time
296 var currentTime = Math.floor( myVideo.currentTime );
297 if( currentTime == 10 )
298 {
299 myVideo.pause ();
300 // Ask the question with a promt
301 var r = prompt( "What is the video about?" );
302 // check the answer
303 if( r.toLowerCase() == "Example" )
304 {
305 myVideo.currentTime = 11; // Add a second otherwise the question will be displayed again;
306 myVideo.play();
307 }
308 else
309 {
310 myVideo.currentTime = 0; // Put the video back to 0;
311 myVideo.play();
312 }
313 }
314 }
315  
316 Example taken from L<EduTech Wiki|https://edutechwiki.unige.ch/en/HTML5_video_and_JavaScript>
317  
318 Picture in Picture (a.k.a. PiP)
319  
320 The W3C states that "the specification intends to provide APIs to allow websites to create a floating video window always on top of other windows so that users may continue consuming media while they interact with other content sites, or applications on their device."
321  
322 <video id="videoElement" controls="true" src="demo.mp4"></video>
323  
324 <!-- button will be used to toggle the PiP mode -->
325 <button id="togglePipButton">Toggle Picture-in-Picture Mode!</button>
326  
327 Call L</requestPictureInPicture> on C<click> of C<togglePipButton> button element.
328  
329 When the promise resolves, the browser will shrink the video into a mini window that the user can move around and position over other windows.
330  
331 let video = document.getElementById('videoElement');
332 let togglePipButton = document.getElementById('togglePipButton');
333  
334 togglePipButton.addEventListener('click', async function (event) {
335 togglePipButton.disabled = true; //disable toggle button while the event occurs
336 try {
337 // If there is no element in Picture-in-Picture yet, request for it
338 if (video !== document.pictureInPictureElement) {
339 await video.requestPictureInPicture();
340 }
341 // If Picture-in-Picture already exists, exit the mode
342 else {
343 await document.exitPictureInPicture();
344 }
345  
346 } catch (error) {
347 console.log(`Oh Horror! ${error}`);
348 } finally {
349 togglePipButton.disabled = false; //enable toggle button after the event
350 }
351 });
352  
353 Check for Picture-in-Picture event changes
354  
355 let video = document.getElementById('videoElement');
356 video.addEventListener('enterpictureinpicture', function (event) {
357 console.log('Entered PiP');
358 pipWindow = event.pictureInPictureWindow;
359 console.log(`Window size - \n Width: ${pipWindow.width} \n Height: ${pipWindow.height}`); // get the width and height of PiP window
360 });
361  
362 video.addEventListener('leavepictureinpicture', function (event) {
363 console.log('Left PiP');
364 togglePipButton.disabled = false;
365 });
366  
367 Example taken from L<https://dev.to/ananyaneogi/implement-picture-in-picture-on-the-web-17g8>
368  
369 =head1 AUTHOR
370  
371 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
372  
373 =head1 SEE ALSO
374  
375 L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLVideoElement>, L<Mozilla documentation on video element|https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video>, L<W3C specifications for PiP|https://w3c.github.io/picture-in-picture/>
376  
377 =head1 COPYRIGHT & LICENSE
378  
379 Copyright(c) 2021 DEGUEST Pte. Ltd.
380  
381 All rights reserved
382  
383 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
384  
385 =cut
« blib/lib/HTML/Object/XPath/Literal.pm ↑ Summary blib/lib/HTML/Object/XPath/Root.pm »