--- /dev/null
+<!DOCTYPE html>
+<html>
+<head>
+ <title>Popcorn Code Plugin Demo</title>
+
+ <script src="../../popcorn.js"></script>
+ <script src="popcorn.code.js"></script>
+ <script>
+ var output;
+
+ document.addEventListener('DOMContentLoaded', function () {
+ output = document.getElementById('output');
+
+ var p = Popcorn('#video')
+
+ .code({
+ start: 1,
+ end: 3,
+ onStart: function( options ) {
+ output.innerHTML = 'Test 1 - onStart (no onEnd)';
+ }
+ })
+
+ .code({
+ start: 5,
+ end: 8,
+ onStart: function( options ) {
+ output.innerHTML = 'Test 2 - onStart';
+ },
+ onEnd: function ( options ) {
+ output.innerHTML = 'Test 2 - onEnd';
+ }
+ })
+
+ .code({
+ start: 10,
+ end: 14,
+ onStart: function( options ) {
+ output.innerHTML = 'Test 3 - onStart [Frames: ';
+ },
+ onFrame: function ( options ) {
+ output.innerHTML += '.';
+ },
+ onEnd: function ( options ) {
+ output.innerHTML += '] Test 3 - onEnd';
+ }
+ })
+
+ .volume(0)
+ .play();
+ }, false);
+ </script>
+</head>
+<body>
+ <h1 id="qunit-header">Popcorn Code Plugin Demo</h1>
+ <p>Arbitrary JavaScript functions are run.</p>
+ <div>
+ <video id='video'
+ autobuffer
+ preload="auto"
+ controls
+ width= '250px'
+ poster="../../test/poster.png">
+
+ <source id='mp4'
+ src="../../test/trailer.mp4"
+ type='video/mp4; codecs="avc1, mp4a"'>
+
+ <source id='ogv'
+ src="../../test/trailer.ogv"
+ type='video/ogg; codecs="theora, vorbis"'>
+
+ <p>Your user agent does not support the HTML5 Video element.</p>
+
+ </video>
+ </div>
+ <div style="width:700px">
+ <div>Code Plugin Output</div>
+ <div id="output" style="position:relative;float:left;width:300px;height:400px"></div>
+ </div>
+</body>
+</html>
--- /dev/null
+// PLUGIN: Code
+
+(function (Popcorn) {
+
+ /**
+ * Code Popcorn Plug-in
+ *
+ * Adds the ability to run arbitrary code (JavaScript functions) according to video timing.
+ *
+ * @param {Object} options
+ *
+ * Required parameters: start, end, template, data, and target.
+ * Optional parameter: static.
+ *
+ * start: the time in seconds when the mustache template should be rendered
+ * in the target div.
+ *
+ * end: the time in seconds when the rendered mustache template should be
+ * removed from the target div.
+ *
+ * onStart: the function to be run when the start time is reached.
+ *
+ * onFrame: [optional] a function to be run on each paint call
+ * (e.g., called ~60 times per second) between the start and end times.
+ *
+ * onEnd: [optional] a function to be run when the end time is reached.
+ *
+ * Example:
+ var p = Popcorn('#video')
+
+ /* onStart function only
+ .code({
+ start: 1,
+ end: 4,
+ onStart: function( options ) {
+ // called on start
+ }
+ })
+
+ /* onStart + onEnd only
+ .code({
+ start: 6,
+ end: 8,
+ onStart: function( options ) {
+ // called on start
+ },
+ onEnd: function ( options ) {
+ // called on end
+ }
+ })
+
+ /* onStart, onEnd, onFrame
+ .code({
+ start: 10,
+ end: 14,
+ onStart: function( options ) {
+ // called on start
+ },
+ onFrame: function ( options ) {
+ // called on every paint frame between start and end.
+ // uses mozRequestAnimationFrame, webkitRequestAnimationFrame,
+ // or setTimeout with 16ms window.
+ },
+ onEnd: function ( options ) {
+ // called on end
+ }
+ });
+ *
+ */
+
+ Popcorn.plugin( "code" , function() {
+
+ function get( name, options ) {
+ return options._instance[name];
+ }
+
+ function set( name, options, value ) {
+ options._instance[name] = value;
+ }
+
+ // Setup a proper frame interval function (60fps), favouring paint events.
+ var step = ( function() {
+
+ var buildFrameRunner = function( runner ) {
+ var _runner = runner;
+
+ return function( f, options ) {
+
+ var _options = options;
+ var _f = function() {
+ f();
+ if ( get( 'running', _options ) ) {
+ _runner( _f );
+ }
+ };
+
+ _f();
+
+ };
+ };
+
+ // Figure out which level of browser support we have for this
+ if ( window.webkitRequestAnimationFrame ) {
+ return buildFrameRunner( window.webkitRequestAnimationFrame );
+ } else if ( window.mozRequestAnimationFrame ) {
+ return buildFrameRunner( window.mozRequestAnimationFrame );
+ } else {
+ return buildFrameRunner( function( f ) {
+ window.setTimeout( f, 16 );
+ } );
+ }
+
+ } )();
+
+
+ return {
+ manifest: {
+ about: {
+ name: "Popcorn Code Plugin",
+ version: "0.1",
+ author: "David Humphrey (@humphd)",
+ website: "http://vocamus.net/dave"
+ },
+ options: {
+ start: {elem:'input', type:'text', label:'In'},
+ end: {elem:'input', type:'text', label:'Out'},
+ // TODO: how to deal with functions, eval strings?
+ onStart: {elem:'input', type:'text', label:'onStart'},
+ onFrame: {elem:'input', type:'text', label:'onFrame'},
+ onEnd: {elem:'input', type:'text', label:'onEnd'},
+ }
+ },
+
+ _setup : function( options ) {
+ if ( !options.onStart || !( typeof options.onStart === 'function' ) ) {
+ throw "Popcorn Code Plugin Error: onStart must be a function.";
+ }
+
+ if ( options.onEnd && !( typeof options.onEnd === 'function' ) ) {
+ throw "Popcorn Code Plugin Error: onEnd must be a function.";
+ }
+
+ if ( options.onFrame && !( typeof options.onFrame === 'function' ) ) {
+ throw "Popcorn Code Plugin Error: onFrame must be a function.";
+ }
+
+ options._instance = { running: false };
+ },
+
+ start: function( event, options ) {
+ options.onStart( options );
+
+ if ( options.onFrame ) {
+ set( 'running', options, true );
+ step( options.onFrame, options );
+ }
+ },
+
+ end: function( event, options ) {
+ if ( options.onFrame ) {
+ set( 'running', options, false );
+ }
+
+ if ( options.onEnd ) {
+ options.onEnd( options );
+ }
+ }
+
+ };
+
+ });
+
+})( Popcorn );
--- /dev/null
+<!DOCTYPE html>
+<html>
+<head>
+ <title>Popcorn API</title>
+ <link rel="stylesheet" href="../../test/qunit/qunit.css" type="text/css" media="screen">
+ <script src="../../test/qunit/qunit.js"></script>
+ <!--
+ do not move - this must be called immediately prior to
+ popcorn-api-draft.js
+ -->
+
+ <script src="../../popcorn.js"></script>
+ <script src="../../test/jquery.js"></script>
+ <script src="popcorn.code.js"></script>
+
+ <script src="popcorn.code.unit.js"></script>
+</head>
+<body>
+
+ <h1 id="qunit-header">Popcorn Code Plugin</h1>
+ <h2 id="qunit-banner"></h2>
+ <div id="qunit-testrunner-toolbar"></div>
+ <h2 id="qunit-userAgent"></h2>
+ <ol id="qunit-tests"></ol>
+ <div id="qunit-fixture"> </div>
+
+ <video id='video'
+ autobuffer
+ preload="auto"
+ controls
+ width= '250px'
+ poster="../../test/poster.png">
+
+ <source id='mp4'
+ src="../../test/trailer.mp4"
+ type='video/mp4; codecs="avc1, mp4a"'>
+
+ <source id='ogv'
+ src="../../test/trailer.ogv"
+ type='video/ogg; codecs="theora, vorbis"'>
+
+ <p>Your user agent does not support the HTML5 Video element.</p>
+
+ </video>
+ <div id="code-div"></div>
+</body>
+</html>
--- /dev/null
+test('Popcorn Code Plugin', function () {
+
+ var popped = Popcorn('#video'),
+ expects = 8,
+ count = 0,
+ frames = 0,
+ codeDiv = document.getElementById('code-div');
+
+ expect( expects );
+
+ function plus() {
+ if ( ++count === expects) {
+ start();
+ }
+ }
+
+ stop();
+
+ ok('code' in popped, 'code is a method of the popped instance');
+ plus();
+
+ equals ( codeDiv.innerHTML, '', 'initially, there is nothing inside the code-div' );
+ plus();
+
+ popped.code({
+ start: 1,
+ end: 3,
+ onStart: function( options ) {
+ codeDiv.innerHTML = 'Test 1 - onStart (no onEnd)';
+ ok(true, 'Test 1 onStart was run.');
+ plus();
+ }
+ });
+
+ popped.code({
+ start: 5,
+ end: 8,
+ onStart: function( options ) {
+ codeDiv.innerHTML = 'Test 2 - onStart';
+ ok(true, 'Test 2 onStart was run.');
+ plus();
+ },
+ onEnd: function ( options ) {
+ codeDiv.innerHTML = 'Test 2 - onEnd';
+ ok(true, 'Test 2 onEnd was run.');
+ plus();
+ }
+ });
+
+ popped.code({
+ start: 10,
+ end: 14,
+ onStart: function( options ) {
+ codeDiv.innerHTML = 'Test 3 - onStart [Frames: ';
+ ok(true, 'Test 3 onStart was run.');
+ plus();
+ },
+ onFrame: function ( options ) {
+ codeDiv.innerHTML += '.';
+ frames++;
+ },
+ onEnd: function ( options ) {
+ codeDiv.innerHTML += '] Test 3 - onEnd';
+ ok(true, 'Test 3 onEnd was run.');
+ plus();
+ ok(frames > 100, 'Test 3 onFrames was run.');
+ plus();
+ popped.pause();
+ }
+ });
+
+ popped.volume(0);
+ popped.play();
+});