src/js/pop.js
branchpopcorn-port
changeset 709 05ae2aa10d24
parent 706 a759113f6bd1
child 711 323205a7bd39
equal deleted inserted replaced
698:329333e07267 709:05ae2aa10d24
     1 /* wrapper that simulates popcorn.js because
     1 /* wrapper that simulates popcorn.js because
     2    popcorn is a bit unstable at the time */
     2    popcorn is a bit unstable at the time */
     3 
     3 
     4 IriSP.PopcornReplacement = {
     4 IriSP.PopcornReplacement = {  
     5   msgPump : {} /* used by jquery to receive and send messages */,
     5 };
     6   __delay_seek_signal : false
     6 
     7 };
     7 /** base class for our popcorn-compatible players.
     8 
     8  */
     9 IriSP.PopcornReplacement.media = { 
     9 IriSP.PopcornReplacement.player = function(container, options) {
    10   "paused": true,
    10   /* the jwplayer calls the callbacks in the global space so we need to 
    11   "muted": false
    11      preserve them using IriSP.wrap */
    12 };
    12   this.callbacks = {
    13 
    13       onReady:  IriSP.wrap(this, this.__initApi),
    14 IriSP.PopcornReplacement.listen = function(msg, callback) {
    14       onTime:   IriSP.wrap(this, this.__timeHandler),
    15 //  IriSP.jQuery(IriSP.PopcornReplacement.msgPump).bind(msg, function(event, rest) { callback(rest); });
    15       onPlay:   IriSP.wrap(this, this.__playHandler),
    16   if (!IriSP.PopcornReplacement.msgPump.hasOwnProperty(msg))
    16       onPause:  IriSP.wrap(this, this.__pauseHandler),
    17     IriSP.PopcornReplacement.msgPump[msg] = [];
    17       onSeek:   IriSP.wrap(this, this.__seekHandler) 
    18 
    18   };
    19   IriSP.PopcornReplacement.msgPump[msg].push(callback);
    19   
    20 };
    20   this.media = { 
    21 
    21     "paused": true,
    22 IriSP.PopcornReplacement.trigger = function(msg, params) {
    22     "muted": false
    23 //  IriSP.jQuery(IriSP.PopcornReplacement.msgPump).trigger(msg, params);
    23   };
    24   
    24     
    25   if (!IriSP.PopcornReplacement.msgPump.hasOwnProperty(msg))
    25   this.container = container.slice(1); //eschew the '#'
       
    26   
       
    27   this.msgPump = {}; /* dictionnary used to receive and send messages */
       
    28   this.__codes = []; /* used to schedule the execution of a piece of code in 
       
    29                         a segment (similar to the popcorn.code plugin). */
       
    30                           
       
    31 };
       
    32 
       
    33 IriSP.PopcornReplacement.player.prototype.listen = function(msg, callback) {
       
    34   if (!this.msgPump.hasOwnProperty(msg))
       
    35     this.msgPump[msg] = [];
       
    36 
       
    37   this.msgPump[msg].push(callback);
       
    38 };
       
    39 
       
    40 IriSP.PopcornReplacement.player.prototype.trigger = function(msg, params) {
       
    41   if (!this.msgPump.hasOwnProperty(msg))
    26     return;
    42     return;
    27 
    43 
    28   var d = IriSP.PopcornReplacement.msgPump[msg];
    44   var d = this.msgPump[msg];
    29 
    45 
    30   for(var i = 0; i < d.length; i++) {
    46   for(var i = 0; i < d.length; i++) {
    31     d[i].call(window, params);
    47     d[i].call(window, params);
    32   }
    48   }
    33 
    49 
    34 };
    50 };
    35 
    51 
    36 IriSP.PopcornReplacement.guid = function(prefix) {
    52 IriSP.PopcornReplacement.player.prototype.guid = function(prefix) {
    37   var str = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    53   var str = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    38       var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
    54       var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
    39       return v.toString(16);
    55       return v.toString(16);
    40    });
    56    });
    41 
    57 
    42   return prefix + str;
    58   return prefix + str;
    43 };
    59 };
    44 
    60 
    45 IriSP.PopcornReplacement.__initApi = function() {
    61 /** init the api after that flash player has been setup - called by the callback
    46   IriSP.PopcornReplacement.trigger("loadedmetadata"); // we've done more than loading metadata of course,
    62     defined by the embedded flash player 
       
    63 */
       
    64 IriSP.PopcornReplacement.player.prototype.__initApi = function() {
       
    65   this.trigger("loadedmetadata"); // we've done more than loading metadata of course,
    47                                                       // but popcorn doesn't need to know more.
    66                                                       // but popcorn doesn't need to know more.
    48   IriSP.PopcornReplacement.media.muted = jwplayer(IriSP.PopcornReplacement._container).getMute();
    67   this.media.muted = this.playerFns.getMute();
    49   
       
    50   /* some programmed segments are supposed to be run at the beginning */
    68   /* some programmed segments are supposed to be run at the beginning */
    51   var i = 0;
    69   var i = 0;
    52   for(i = 0; i < IriSP.PopcornReplacement.__codes.length; i++) {
    70   for(i = 0; i < this.__codes.length; i++) {
    53     var c = IriSP.PopcornReplacement.__codes[i];
    71     var c = this.__codes[i];
    54     if (0 == c.start) {
    72     if (0 == c.start) {
    55       c.onStart();
    73       c.onStart();
    56     }
    74     }
    57     
    75     
    58     if (0 == c.end) {
    76     if (0 == c.end) {
    59       c.onEnd();
    77       c.onEnd();
    60     }
    78     }
    61   }
    79   }
    62 };
    80 };
    63 
    81 
       
    82 /*
    64 IriSP.PopcornReplacement.jwplayer = function(container, options) {
    83 IriSP.PopcornReplacement.jwplayer = function(container, options) {
    65   IriSP.PopcornReplacement._container = container.slice(1); //eschew the '#'
    84   IriSP.PopcornReplacement._container = container.slice(1); //eschew the '#'
    66   options.events = {
    85   options.events = {
    67       onReady: IriSP.PopcornReplacement.__initApi,
    86       onReady: IriSP.PopcornReplacement.__initApi,
    68       onTime: IriSP.PopcornReplacement.__timeHandler,
    87       onTime: IriSP.PopcornReplacement.__timeHandler,
    73     
    92     
    74   jwplayer(IriSP.PopcornReplacement._container).setup(options);
    93   jwplayer(IriSP.PopcornReplacement._container).setup(options);
    75   IriSP.PopcornReplacement.media.duration = options.duration;
    94   IriSP.PopcornReplacement.media.duration = options.duration;
    76   return IriSP.PopcornReplacement;
    95   return IriSP.PopcornReplacement;
    77 };
    96 };
    78 
    97 */
    79 IriSP.PopcornReplacement.currentTime = function(time) {
    98 
       
    99 IriSP.PopcornReplacement.player.prototype.currentTime = function(time) {
    80   if (typeof(time) === "undefined") {        
   100   if (typeof(time) === "undefined") {        
    81       return jwplayer(IriSP.PopcornReplacement._container).getPosition();            
   101       return this.playerFns.getPosition();            
    82   } else {
   102   } else {
    83      var currentTime = +time;
   103      var currentTime = +time;
    84      jwplayer( IriSP.PopcornReplacement._container ).seek( currentTime );
   104      this.playerFns.seek( currentTime );
    85      IriSP.PopcornReplacement.__delay_seek_signal = true;     
   105      this.__delay_seek_signal = true;    /* FIXME : useless ? */
    86      IriSP.PopcornReplacement.trigger("seeked");           
   106      this.trigger("seeked");           
    87      return currentTime;
   107      return currentTime;
    88   }
   108   }
    89 };
   109 };
    90 
   110 
    91 IriSP.PopcornReplacement.play = function() {
   111 IriSP.PopcornReplacement.player.prototype.play = function() {
    92       IriSP.PopcornReplacement.media.paused = false;
   112   this.media.paused = false;
    93       IriSP.PopcornReplacement.trigger("play");
   113   this.trigger("play");
    94 //      IriSP.PopcornReplacement.trigger("playing");
   114   //IriSP.PopcornReplacement.trigger("playing");
    95       jwplayer( IriSP.PopcornReplacement._container ).play();
   115   this.playerFns.play();
    96 };
   116 };
    97     
   117     
    98 IriSP.PopcornReplacement.pause = function() {
   118 IriSP.PopcornReplacement.player.prototype.pause = function() {
    99       if ( !IriSP.PopcornReplacement.media.paused ) {
   119   if ( !this.media.paused ) {
   100         IriSP.PopcornReplacement.media.paused = true;
   120     this.media.paused = true;
   101         IriSP.PopcornReplacement.trigger( "pause" );
   121     this.trigger( "pause" );
   102         jwplayer( IriSP.PopcornReplacement._container ).pause();
   122     this.playerFns.pause();
       
   123   }
       
   124 };
       
   125 
       
   126 IriSP.PopcornReplacement.player.prototype.muted = function(val) {
       
   127   if (typeof(val) !== "undefined") {
       
   128 
       
   129     if (this.playerFns.getMute() !== val) {
       
   130       if (val) {
       
   131         this.playerFns.setMute(true);
       
   132         this.media.muted = true;
       
   133       } else {
       
   134         this.playerFns.setMute(false);
       
   135         this.media.muted = false;
   103       }
   136       }
   104 };
   137 
   105 
   138       this.trigger( "volumechange" );
   106 IriSP.PopcornReplacement.muted = function(val) {
       
   107   if (typeof(val) !== "undefined") {
       
   108 
       
   109     if (jwplayer(IriSP.PopcornReplacement._container).getMute() !== val) {
       
   110       if (val) {
       
   111         jwplayer(IriSP.PopcornReplacement._container).setMute(true);
       
   112         IriSP.PopcornReplacement.media.muted = true;
       
   113       } else {
       
   114         jwplayer( IriSP.PopcornReplacement._container ).setMute(false);
       
   115         IriSP.PopcornReplacement.media.muted = false;
       
   116       }
       
   117 
       
   118       IriSP.PopcornReplacement.trigger( "volumechange" );
       
   119     }
   139     }
   120     
   140     
   121     return jwplayer( IriSP.PopcornReplacement._container ).getMute();
   141     return this.playerFns.getMute();
   122   } else {
   142   } else {
   123     return jwplayer( IriSP.PopcornReplacement._container ).getMute();
   143     return this.playerFns.getMute();
   124   }
   144   }
   125 };
   145 };
   126 
   146 
   127 IriSP.PopcornReplacement.mute = IriSP.PopcornReplacement.muted;
   147 IriSP.PopcornReplacement.player.prototype.mute = IriSP.PopcornReplacement.player.prototype.muted;
   128 
   148 
   129 IriSP.PopcornReplacement.__codes = [];
   149 IriSP.PopcornReplacement.player.prototype.code = function(options) {
   130 IriSP.PopcornReplacement.code = function(options) {
   150   this.__codes.push(options);
   131   IriSP.PopcornReplacement.__codes.push(options);
   151   return this;
   132   return IriSP.PopcornReplacement;
       
   133 };
   152 };
   134 
   153 
   135 /* called everytime the player updates itself 
   154 /* called everytime the player updates itself 
   136    (onTime event)
   155    (onTime event)
   137  */
   156  */
   138 
   157 
   139 IriSP.PopcornReplacement.__timeHandler = function(event) {
   158 IriSP.PopcornReplacement.player.prototype.__timeHandler = function(event) {
   140   var pos = event.position;
   159   var pos = event.position;
   141   
   160 
   142   var i = 0;
   161   var i = 0;
   143   for(i = 0; i < IriSP.PopcornReplacement.__codes.length; i++) {
   162   for(i = 0; i < this.__codes.length; i++) {
   144      var c = IriSP.PopcornReplacement.__codes[i];
   163      var c = this.__codes[i];
   145 
   164 
   146      if (pos >= c.start && pos < c.end && 
   165      if (pos >= c.start && pos < c.end && 
   147          pos - 1 <= c.start) {       
   166          pos - 1 <= c.start) {       
   148         c.onStart();
   167         c.onStart();
   149      }
   168      }
   153          c.onEnd();
   172          c.onEnd();
   154      }
   173      }
   155    
   174    
   156   }
   175   }
   157  
   176  
   158   IriSP.PopcornReplacement.trigger("timeupdate");
   177   this.trigger("timeupdate");
   159 };
   178 };
   160 
   179 
   161 IriSP.PopcornReplacement.__seekHandler = function(event) {
   180 IriSP.PopcornReplacement.player.prototype.__seekHandler = function(event) {
   162   var i = 0;
   181   var i = 0;
   163   
   182 
   164   for(i = 0; i < IriSP.PopcornReplacement.__codes.length; i++) {
   183   for(i = 0; i < this.__codes.length; i++) {
   165      var c = IriSP.PopcornReplacement.__codes[i];
   184      var c = this.__codes[i];
   166     
   185     
   167      if (event.position >= c.start && event.position < c.end) {        
   186      if (event.position >= c.start && event.position < c.end) {        
   168         c.onEnd();
   187         c.onEnd();
   169      }         
   188      }         
   170    }
   189    }
   171   
   190   
   172    for(i = 0; i < IriSP.PopcornReplacement.__codes.length; i++) {
   191    for(i = 0; i < this.__codes.length; i++) {
   173      var c = IriSP.PopcornReplacement.__codes[i];
   192      var c = this.__codes[i];
   174 
   193 
   175      if (typeof(event.offset) === "undefined")
   194      if (typeof(event.offset) === "undefined")
   176        event.offset = 0;
   195        event.offset = 0;
   177            
   196            
   178      if (event.offset >= c.start && event.offset < c.end) { 
   197      if (event.offset >= c.start && event.offset < c.end) { 
   179        c.onStart();
   198        c.onStart();
   180      }
   199      }
   181      
   200      
   182    }
   201    }
   183 
   202 
   184   IriSP.PopcornReplacement.trigger("timeupdate");
   203   this.trigger("timeupdate");
   185 };
   204 };
   186 
   205 
   187 
   206 IriSP.PopcornReplacement.player.prototype.__playHandler = function(event) {
   188 IriSP.PopcornReplacement.__playHandler = function(event) {
   207   this.media.paused = false;
   189   IriSP.PopcornReplacement.media.paused = false;
   208   this.trigger("play");
   190   IriSP.PopcornReplacement.trigger("play");
   209 };
   191 };
   210 
   192 
   211 IriSP.PopcornReplacement.player.prototype.__pauseHandler = function(event) {
   193 IriSP.PopcornReplacement.__pauseHandler = function(event) {
   212   this.media.paused = true;
   194   IriSP.PopcornReplacement.media.paused = true;
   213   this.trigger("pause");
   195   IriSP.PopcornReplacement.trigger("pause");
   214 };
   196 };
   215 
   197 
   216 IriSP.PopcornReplacement.player.prototype.roundTime = function() {
   198 IriSP.PopcornReplacement.roundTime = function() {
   217   var currentTime = this.currentTime();
   199   var currentTime = IriSP.PopcornReplacement.currentTime();
       
   200   return Math.round(currentTime);
   218   return Math.round(currentTime);
   201 };
   219 };
       
   220 
       
   221 /* To wrap a player the develop should create a new class derived from 
       
   222    the IriSP.PopcornReplacement.player and defining the correct functions */
       
   223 
       
   224 /* Exemple with jwplayer */
       
   225 IriSP.PopcornReplacement.jwplayer = function(container, options) {
       
   226 
       
   227   /* appel du parent pour initialiser les structures communes à tous les players */
       
   228   IriSP.PopcornReplacement.player.call(this, container, options);
       
   229   
       
   230   this.media.duration = options.duration; /* optional */
       
   231   
       
   232   /* Définition des fonctions de l'API -  */
       
   233   this.playerFns = {
       
   234     play: function() { return jwplayer(this.container).play(); },
       
   235     pause: function() { return jwplayer(this.container).pause(); },
       
   236     getPosition: function() { return jwplayer(this.container).getPosition(); },
       
   237     seek: function(pos) { return jwplayer(this.container).seek(pos); },
       
   238     getMute: function() { return jwplayer(this.container).getMute() },
       
   239     setMute: function(p) { return jwplayer(this.container).setMute(p); }
       
   240   }
       
   241 
       
   242   options.events = this.callbacks;
       
   243 
       
   244   jwplayer(this.container).setup(options);
       
   245 };
       
   246 
       
   247 IriSP.PopcornReplacement.jwplayer.prototype = new IriSP.PopcornReplacement.player("", {});