From: Brad Chen Date: Fri, 11 Mar 2011 07:07:11 +0000 (-0500) Subject: Initial draft of the normalized approach. X-Git-Tag: v0.4~7^2~7^2~2 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=73bcf4d0c4e750b3b87c2ed78893c42f6b800bee;p=popcorn-js.git Initial draft of the normalized approach. --- diff --git a/players/youtube/popcorn.youtube.html b/players/youtube/popcorn.youtube.html new file mode 100644 index 0000000..0f82bd0 --- /dev/null +++ b/players/youtube/popcorn.youtube.html @@ -0,0 +1,87 @@ + + + + Popcorn Youtube Player Example + + + + + +
+ + + + +
+
+ + + +
+
    +
  • Status: Not Ready
  • +
  • Current Time (s):
  • +
  • Video Duration (s):
  • +
+
+
+
+ + diff --git a/players/youtube/popcorn.youtube.js b/players/youtube/popcorn.youtube.js new file mode 100644 index 0000000..67e7d28 --- /dev/null +++ b/players/youtube/popcorn.youtube.js @@ -0,0 +1,269 @@ +// Popcorn Youtube Player Warpper + +( function( Popcorn ) { + /** + * Youtube wrapper for popcorn. + * This plug-in adds capability for Popcorn.js to deal with Youtube + * videos. This plug-in also doesn't use Popcorn's plugin() API and + * instead hacks directly into Popcorn's core. + * + * To use this plug-in, onYouTubePlayerReady() event handler needs to be + * called by the Youtube video player, before videos can be registered. + * Once videos are registered, calls to them can be made the same way as + * regular Popcorn objects. Also note that enablejsapi=1 needs to be added + * to the embed code, in order for Youtube's JavaScript API to work. + * + * Note that there are a few methods, properties and events that are not + * supported. See the bottom of this plug-in for a complete list. + */ + + // Intended + var undef; + + // Config parameters + var debug = true; + var timeupdateInterval = 33; // 33 is suitable for 30 fps + var progressInterval = 250; // as specified by WHATWG + + // Ready State Constants + var READY_STATE_HAVE_NOTHING = 0; + var READY_STATE_HAVE_METADATA = 1; + var READY_STATE_HAVE_CURRENT_DATA = 2; + var READY_STATE_HAVE_FUTURE_DATA = 3; + var READY_STATE_HAVE_ENOUGH_DATA = 4; + + // Youtube State Constants + var YOUTUBE_STATE_UNSTARTED = -1; + var YOUTUBE_STATE_ENDED = 0; + var YOUTUBE_STATE_PLAYING = 1; + var YOUTUBE_STATE_PAUSED = 2; + var YOUTUBE_STATE_BUFFERING = 3; + var YOUTUBE_STATE_CUED = 5; + + Popcorn.youtube = function( elementId ) { + return new Popcorn.youtube.init( elementId ); + }; + + Popcorn.youtube.init = function( elementId ) { + if ( !elementId ) { + throw "Element id is invalid."; + } + + this.video = document.getElementById( elementId ); + this.readyState = READY_STATE_HAVE_NOTHING; + this.duration = this.video.getDuration(); + this.eventListeners = {}; + this.loadStarted = false; + this.loadedData = false; + this.fullyLoaded = false; + this.timeUpdater = null; + this.progressUpdater = null; + this.registerYoutubeEventHandlers(); + this.registerInternalEventHandlers(); + }; // end Popcorn.youtube.init + + // For internal use only. + Popcorn.youtube.init.p.registerYoutubeEventHandlers = function() { + var youcorn = this, + stateChangeHandler = 'Popcorn.youtube.stateChangeEventHandler', + errorHandler = 'Popcorn.youtube.errorEventHandler'; + this.video.addEventListener( 'onStateChange', stateChangeHandler ); + this.video.addEventListener( 'onError', errorHandler ); + + /** + * Since Flash can only call named functions, they are declared + * separately here. + */ + Popcorn.youtube.stateChangeEventHandler = function( state ) { + if ( debug ) { + console.log( 'Youtube state updated: ' + state ); + } + if ( state == YOUTUBE_STATE_UNSTARTED ) { + youcorn.readyState = READY_STATE_HAVE_METADATA; + youcorn.raiseEvent('loadedmetadata'); + } else if ( state == YOUTUBE_STATE_ENDED ) { + youcorn.raiseEvent('ended'); + } else if ( state == YOUTUBE_STATE_PLAYING ) { + // Being able to play means current data is loaded. + if ( !this.loadedData ) { + this.loadedData = true; + youcorn.raiseEvent( 'loadeddata' ); + } + + youcorn.readyState = READY_STATE_HAVE_CURRENT_DATA; + youcorn.raiseEvent( 'playing' ); + } else if ( state == YOUTUBE_STATE_PAUSED ) { + youcorn.raiseEvent( 'pause' ); + } else if ( state == YOUTUBE_STATE_BUFFERING ) { + youcorn.raiseEvent( 'waiting' ); + } else if ( state == YOUTUBE_STATE_CUED ) { + // not handled + } + }; + + Popcorn.youtube.errorEventHandler = function( state ) { + if ( debug ) { + console.log( 'Youtube error encountered: ' + error ); + } + youcorn.raiseEvent( 'error' ); + }; + }; + + Popcorn.youtube.init.p = Popcorn.youtube.init.prototype; + + // For internal use only. + Popcorn.youtube.init.p.registerInternalEventHandlers = function() { + this.addEventListener( 'playing', function() { + startTimeUpdater( this ); + }); + this.addEventListener( 'loadedmetadata', function() { + startProgressUpdater( this ); + }); + }; + + Popcorn.youtube.init.p.play = function() { + this.raiseEvent( 'play' ); + this.video.playVideo(); + }; + + Popcorn.youtube.init.p.pause = function() { + this.video.pauseVideo(); + // pause event is raised by Youtube. + }; + + Popcorn.youtube.init.p.load = function() { + this.video.playVideo(); + this.video.pauseVideo(); + }; + + Popcorn.youtube.init.p.currentTime = function( time ) { + if ( time === undef ) { + return this.video.getCurrentTime(); + } + var playing = this.video.getPlayerState() == YOUTUBE_STATE_PLAYING; + this.video.seekTo( time, true ); + + // Prevent Youtube's behaviour to start playing video after seeking. + if ( !playing ) { + this.video.pauseVideo(); + } + + // Data need to be loaded again. + if ( !this.fullyLoaded ) { + this.loadedData = false; + } + + // Raise event. + this.raiseEvent( 'seeked' ); + }; + + Popcorn.youtube.init.p.duration = function() { + return this.video.getDuration(); + }; + + Popcorn.youtube.init.p.mute = function() { + if ( this.video.getVolume() != 0 ) { + this.video.mute(); + this.raiseEvent( 'volumechange' ); + } + }; + + Popcorn.youtube.init.p.volume = function( vol ) { + if ( vol == undef ) { + return this.video.getVolume() / 100; + } + if ( vol != this.volume() ) { + this.video.setVolume( vol * 100 ); + this.raiseEvent( 'volumechange' ); + } + }; + + Popcorn.youtube.init.p.addEventListener = function( eventName, func ) { + if ( !this.eventListeners[eventName] ) { + this.eventListeners[eventName] = []; + } + this.eventListeners[eventName].push( func ); + }; + + /** + * Notify event listeners about an event. + */ + Popcorn.youtube.init.p.raiseEvent = function( name ) { + if ( debug ) { + console.log( 'Raising event: ' + name ); + } + if ( !this.eventListeners[name] ) { + return; + } + for ( var i in this.eventListeners[name] ) { + // @TODO should we mimic the event object sent by HTMLMediaElement? + this.eventListeners[name][i].call( this, null ); + } + } + + function startTimeUpdater( youcorn ) { + youcorn.timeUpdater = setInterval(function() { + var state = youcorn.video.getPlayerState(); + if ( (state == YOUTUBE_STATE_ENDED) || (state == YOUTUBE_STATE_PAUSED) ) { + clearInterval(youcorn.timeUpdater); + return; + } + youcorn.raiseEvent( 'timeupdate' ); + }, timeupdateInterval); + } + + function startProgressUpdater( youcorn ) { + youcorn.progressUpdater = setInterval(function() { + var bytesLoaded = youcorn.video.getVideoBytesLoaded(); + var bytesToLoad = youcorn.video.getVideoBytesTotal(); + if ( debug ) { + console.log( 'Bytes loaded: ' + bytesLoaded ); + console.log( 'Bytes to load: ' + bytesToLoad ); + } + + // do nothing if size is not yet determined + if ( bytesToLoad == 0 ) { + return; + } + + // raise an event if load has just started + if ( !youcorn.loadStarted ) { + youcorn.loadStarted = true; + youcorn.raiseEvent( 'loadstart' ); + } + + // fully loaded + if ( bytesLoaded >= bytesToLoad ) { + youcorn.fullyLoaded = true; + youcorn.readyState = READY_STATE_HAVE_ENOUGH_DATA; + youcorn.raiseEvent( 'canplaythrough' ); + clearInterval( youcorn.progressUpdater ); + return; + } + + youcorn.raiseEvent( 'progress' ); + }, progressInterval); + } + + /* Unsupported properties, methods and events. */ + + Popcorn.youtube.init.p.defaultPlaybackRate = function( arg ) { + }; + + Popcorn.youtube.init.p.playbackRate = function( arg ) { + }; + + /** + * Unsupported events are: + * * suspend + * * abort + * * emptied + * * stalled + * * canplay + * * seeking + * * ratechange + * * durationchange + */ + +})( Popcorn ); + diff --git a/players/youtube/popcorn.youtube.unit.html b/players/youtube/popcorn.youtube.unit.html new file mode 100644 index 0000000..16e1751 --- /dev/null +++ b/players/youtube/popcorn.youtube.unit.html @@ -0,0 +1,41 @@ + + + + Popcorn YouTube Player + + + + + + + + +

Popcorn YouTube Player Plugin

+

+
+

+
    +
    + +

    + Note about this set of tests: +

    + +

    + The test assumes that the YouTube video is mostly loaded, before it is run. + Therefore, if failures occur for the first time, just run it again. Likely, + failures would disappear the second time. +

    +
    + + + + +
    + + + diff --git a/players/youtube/popcorn.youtube.unit.js b/players/youtube/popcorn.youtube.unit.js new file mode 100644 index 0000000..4ac6662 --- /dev/null +++ b/players/youtube/popcorn.youtube.unit.js @@ -0,0 +1,156 @@ +var ytReady = false; +function onYouTubePlayerReady() { + ytReady = true; +} + +test( "Popcorn YouTube Plugin Startup", function() { + var time = 0, + wait = 100, + timeout = 10000; + + // wait for YouTube to start + stop( timeout + wait ); + + // run in an interval check if YouTube has started + var interval = setInterval(function() { + time += wait; + + if ( ytReady ) { + start(); + ok( true, "YouTube has started." ); + clearInterval( interval ); + return; + } + + if ( time > timeout ) { + ok( false, "YouTube cannot be started." ); + clearInterval( interval ); + } + }, wait); +}); + +test( "Popcorn YouTube Plugin Event Tests", function() { + if ( !ytReady ) { + ok( false, "YouTube did not start." ); + return; + } + + var popcorn = Popcorn( new Popcorn.youtube( 'video' ) ); + popcorn.volume(1); // is muted later + + // check time sync + popcorn.exec(2, function() { + ok( popcorn.currentTime() >= 2, "Check time synchronization." ); + }); + popcorn.exec(49, function() { + ok( popcorn.currentTime() >= 49, "Check time synchronization." ); + }); + popcorn.exec(40, function() { + ok( false, "This should not be run." ); + }); + + // events must be fired in this order + var expectedEvents = [ + 'play', + 'loadeddata', + 'playing', + 'volumechange', + 'pause', + 'play', + 'playing', + 'seeked', + 'playing', + 'pause', + 'ended' + ]; + + var expectedEventCount = expectedEvents.length; + expect(expectedEventCount + 2); + + // register each events + var eventCount = 0; + var added = []; + for ( var i in expectedEvents ) { + (function( event ) { + // skip same listeners already added + for ( var i in added ) { + if ( added[i] == event ) { + return; + } + } + + popcorn.listen( event, function() { + eventCount++; + var expected = expectedEvents.shift(); + if ( expected == event ) { + ok( true, event + " is fired." ); + } else { + ok( false, event + " is fired, expecting: " + expected ); + } + }); + added.push( event ); + })( expectedEvents[i] ); + } + + // begin the test + popcorn.play(); + + // operations set1 + var set1Executed = false; + popcorn.listen( 'playing', function() { + // prevent double calling + if ( set1Executed ) { + return; + } + + // toggle volume 1 second after playing + setTimeout(function() { + popcorn.volume(0); + }, 1000); + + // pause 3 seconds after playing + setTimeout(function() { + popcorn.pause(); + }, 3000); + + set1Executed = true; + }); + + // operations set2 + var set2Executed = false; + popcorn.listen( 'pause', function() { + if ( set2Executed ) { + return; + } + + // continue to play + setTimeout(function() { + popcorn.play(); + }, 500); + + // seek to the end + setTimeout(function() { + popcorn.currentTime(48); + }, 1000); + + set2Executed = true; + }); + + var time = 0, + wait = 100, + timeout = 15000; + + stop( timeout + wait ); + var interval = setInterval( function() { + time += wait; + if ( time > timeout ) { + clearInterval( interval ); + return; + } + if ( eventCount == expectedEventCount ) { + start(); + clearInterval( interval ); + } + }, wait ); +}); +