]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
Adding support and tests for standard time strings
authorEdsfault <edgardo.arvelaez@gmail.com>
Tue, 5 Apr 2011 00:20:15 +0000 (20:20 -0400)
committerEdsfault <edgardo.arvelaez@gmail.com>
Tue, 5 Apr 2011 00:20:15 +0000 (20:20 -0400)
popcorn.js
test/popcorn.unit.js

index 8b064aafd63f849374e86052b5a82b156b96e42f..8101f0156feb2cdbd32362bdd2364f6a69dacf95 100644 (file)
@@ -25,9 +25,9 @@
 
   //  Declare constructor
   //  Returns an instance object.
-  Popcorn = function( entity ) {
+  Popcorn = function( entity, options ) {
     //  Return new Popcorn object
-    return new Popcorn.p.init( entity );
+    return new Popcorn.p.init( entity, options || null );
   };
 
   //  Instance caching
@@ -87,7 +87,7 @@
   //  the new prototype for our Popcorn constructor
   Popcorn.p = Popcorn.prototype = {
 
-    init: function( entity ) {
+    init: function( entity, options ) {
 
       var matches;
 
       //  Register new instance
       Popcorn.addInstance( this );
 
+      this.options = options || { };
       this.data = {
         history: [],
         events: {},
           //  Adding padding to the front and end of the arrays
           //  this is so we do not fall off either end
 
-          var duration = that.media.duration;
-          //  Check for no duration info (NaN)
-          var videoDurationPlus = duration != duration ? Number.MAX_VALUE : duration + 1;
+          var duration = that.media.duration,
+              //  Check for no duration info (NaN)
+              videoDurationPlus = duration != duration ? Number.MAX_VALUE : duration + 1;
 
           Popcorn.addTrackEvent( that, {
             start: videoDurationPlus,
       track._natives.end = track._natives.end || Popcorn.nop;
     }
 
+    track.start = Popcorn.util.toSeconds( track.start, obj.options.framerate );
+    track.end = Popcorn.util.toSeconds( track.end, obj.options.framerate );
+
     //  Store this definition in an array sorted by times
     obj.data.trackEvents.byStart.push( track );
     obj.data.trackEvents.byEnd.push( track );
     return Popcorn.xhr.getJSONP( url, success, true );
   };
 
+  Popcorn.util = {
+    // Simple function to parse a timestamp into seconds
+    // Acceptable formats are:
+    // HH:MM:SS.MMM
+    // HH:MM:SS;FF
+    // Hours and minutes are optional. They default to 0
+    toSeconds: function( timeStr, framerate ) {
+        //Hours and minutes are optional
+        //Seconds must be specified
+        //Seconds can be followed by milliseconds OR by the frame information
+        var validTimeFormat = /^([0-9]+:){0,2}[0-9]+([.;][0-9]+)?$/,
+            errorMessage = "Invalid time format";
+
+        if ( typeof timeStr === "number" ) {
+          return timeStr;
+        } else if ( typeof timeStr === "string" ) {
+          if ( ! validTimeFormat.test( timeStr ) ) {
+            Popcorn.error( errorMessage );
+          }
+        } else {
+          Popcorn.error( errorMessage );
+        }
+
+        var t = timeStr.split( ":" ),
+            lastIndex = t.length - 1,
+            lastElement = t[ lastIndex ];
+
+        //Fix last element:
+        if ( lastElement.indexOf( ";" ) > -1 ) {
+          var frameInfo = lastElement.split( ";" ),
+              frameTime = 0;
+
+          if ( framerate && ( typeof framerate === "number" ) ) {
+              frameTime = parseFloat( frameInfo[ 1 ], 10 ) / framerate;
+          }
+
+          t[ lastIndex ] =
+            parseInt( frameInfo[ 0 ], 10 ) + frameTime;
+        }
+
+        if ( t.length === 1 ) {
+          return parseFloat( t[ 0 ], 10 );
+        } else if ( t.length === 2 ) {
+          return ( parseInt( t[ 0 ], 10 ) * 60 ) + parseFloat( t[ 1 ], 10 );
+        } else if ( t.length === 3 ) {
+          return ( parseInt( t[ 0 ], 10 ) * 3600 ) +
+                 ( parseInt( t[ 1 ], 10 ) * 60 ) +
+                 parseFloat( t[ 2 ], 10 );
+        }
+    }
+  };
 
   //  Exposes Popcorn to global context
   global.Popcorn = Popcorn;
index 0f63263743651292588a665be32a77b67b44e9e3..2bbf5bfc6d54e4b92a5bd4a778aa632381a2669f 100755 (executable)
@@ -65,6 +65,128 @@ test("Utility", function () {
 
 
 
+});
+
+test("Standard Time Strings" , function () {
+  var framerate = 24,
+      storedStartTime,
+      storedEndTime,
+      areEquivalent,
+      currentPeriod,
+      startTimeStr,
+      endTimeStr,
+      message;
+
+  //Time period data
+  //The correct times that specify frame are calculated with a framerate of
+  //24fps. Quotation is mixes (single and double) for testing purposes.
+  var timePeriods = [
+    { //Testing double quotes
+      start: "01.234",
+      end: "4.003",
+      correctStartTime: 1.234,
+      correctEndTime: 4.003
+    },
+    { //Tesing actual numbers
+      start: 5.333,
+      end: 6,
+      correctStartTime: 5.333,
+      correctEndTime: 6.000
+    },
+    { //Testing times in different data types (number and string)
+      start: 6.004,
+      end: "6.78",
+      correctStartTime: 6.004,
+      correctEndTime: 6.780
+    },
+    { //Testing times in different data types (string and number)
+      start: "8.090",
+      end: 9.11111111,
+      correctStartTime: 8.090,
+      correctEndTime: 9.11111111
+    },
+    { //Testing double quotes
+      start: "10;4",
+      end: "10;17",
+      correctStartTime: 10.1666,
+      correctEndTime: 10.7083
+    },
+    { //Testing single quotes
+      start: '12;1',
+      end: '13;2',
+      correctStartTime: 12.0416,
+      correctEndTime: 13.0833
+    },
+    { //Testing mixed quotes
+      start: "20;11",
+      end: '23;17',
+      correctStartTime: 20.4583,
+      correctEndTime: 23.7083
+    },
+    { //Testing mixed quotes
+      start: '27;7',
+      end: "27;22",
+      correctStartTime: 27.2916,
+      correctEndTime: 27.9166
+    },
+    { //Testing double quotes
+      start: "12:04;12",
+      end: "22:59;23",
+      correctStartTime: 724.5,
+      correctEndTime: 1379.9583
+    },
+    { //Testing single quotes
+      start: '1:48:27;9',
+      end: '3:23:15;1',
+      correctStartTime: 6507.375,
+      correctEndTime: 12195.0416
+    },
+    { //Testing mixed quotes
+      start: '12:56;7',
+      end: "2:02:42;8",
+      correctStartTime: 776.2916,
+      correctEndTime: 7362.3333
+    }
+  ];
+
+  var equivalentTimes = function ( testedTime, correctTime ) {
+    var tolerance = 0.0001;
+    return ( testedTime < ( correctTime + tolerance ) ) &&
+           ( testedTime > ( correctTime - tolerance ) );
+  };
+
+  var logMessage = function ( timeStr, correctTime, incorrectTime ) {
+    return "Time stored in seconds for '" + timeStr +
+           "' should be " + correctTime +
+           ". Time stored was " + incorrectTime ;
+  };
+
+  for ( var periodsIdx = 0, timPeriodsLength = timePeriods.length; periodsIdx < timPeriodsLength; periodsIdx++ ) {
+    currentPeriod = timePeriods[ periodsIdx ];
+    startTimeStr = currentPeriod.start;
+    endTimeStr = currentPeriod.end;
+
+    storedStartTime = Popcorn.util.toSeconds( startTimeStr, framerate );
+    storedEndTime = Popcorn.util.toSeconds( endTimeStr, framerate );
+
+    message = logMessage(
+      startTimeStr,
+      currentPeriod.correctStartTime,
+      storedStartTime
+    );
+
+    areEquivalent = equivalentTimes( storedStartTime, currentPeriod.correctStartTime );
+    equals( areEquivalent, true, message );
+
+    message = logMessage(
+      endTimeStr,
+      currentPeriod.correctEndTime,
+      storedEndTime
+    );
+
+    areEquivalent = equivalentTimes( storedEndTime, currentPeriod.correctEndTime ) ;
+    equals( areEquivalent, true, message );
+  }
 });
 
 test("Instances", function() {