1 /** The minplayer namespace. */
  2 var minplayer = minplayer || {};
  3 
  4 /** All the media player implementations */
  5 minplayer.players = minplayer.players || {};
  6 
  7 /**
  8  * @constructor
  9  * @extends minplayer.players.base
 10  * @class The Limelight media player.
 11  *
 12  * @param {object} context The jQuery context.
 13  * @param {object} options This components options.
 14  */
 15 minplayer.players.kaltura = function(context, options) {
 16 
 17   // Derive from the base player.
 18   minplayer.players.base.call(this, context, options);
 19 };
 20 
 21 /** Derive from minplayer.players.base. */
 22 minplayer.players.kaltura.prototype = new minplayer.players.base();
 23 
 24 /** Reset the constructor. */
 25 minplayer.players.kaltura.prototype.constructor = minplayer.players.kaltura;
 26 
 27 /**
 28  * @see minplayer.plugin.construct
 29  * @this minplayer.kaltura.limelight
 30  */
 31 minplayer.players.kaltura.prototype.construct = function() {
 32 
 33   // Call the players.base constructor.
 34   minplayer.players.base.prototype.construct.call(this);
 35 
 36   // Set the plugin name within the options.
 37   this.options.pluginName = 'kaltura';
 38 
 39   // Determine if an ad is playing.
 40   this.adPlaying = false;
 41 };
 42 
 43 /**
 44  * Get the default options for this plugin.
 45  *
 46  * @param {object} options The default options for this plugin.
 47  */
 48 minplayer.players.kaltura.prototype.defaultOptions = function(options) {
 49 
 50   // The Kaltura options for this player.
 51   options.entryId = 0;
 52   options.uiConfId = 0;
 53   options.partnerId = 0;
 54 
 55   minplayer.players.base.prototype.defaultOptions.call(this, options);
 56 };
 57 
 58 /**
 59  * @see minplayer.players.base#getPriority
 60  * @return {number} The priority of this media player.
 61  */
 62 minplayer.players.kaltura.getPriority = function() {
 63   return 10;
 64 };
 65 
 66 /**
 67  * @see minplayer.players.base#canPlay
 68  *
 69  * @param {object} file A {@link minplayer.file} object.
 70  * @return {boolean} If this player can play this media type.
 71  */
 72 minplayer.players.kaltura.canPlay = function(file) {
 73 
 74   // Check for the mimetype for kaltura.
 75   if (file.mimetype === 'video/kaltura') {
 76     return true;
 77   }
 78 
 79   // If the path is a kaltura path, then return true.
 80   var regex = /.*kaltura\.com.*/i;
 81   return (file.path.search(regex) === 0);
 82 };
 83 
 84 minplayer.players.kaltura.prototype.adStart = function(data) {
 85   this.adPlaying = true;
 86   this.onPlaying();
 87 };
 88 
 89 minplayer.players.kaltura.prototype.adEnd = function(data) {
 90   this.adPlaying = false;
 91 };
 92 
 93 /**
 94  * Keep track when the player state changes.
 95  *
 96  * @param {type} data
 97  * @returns {undefined}
 98  */
 99 minplayer.players.kaltura.prototype.playerStateChange = function(data) {
100   if (!this.adPlaying) {
101     switch (data) {
102       case 'ready':
103         this.onLoaded();
104         break;
105       case 'loading':
106       case 'buffering':
107         this.onWaiting();
108         break;
109       case 'playing':
110         this.onPlaying();
111         break;
112       case 'paused':
113         this.onPaused();
114         break;
115     }
116   }
117 };
118 
119 /**
120  * Called when the player is ready.
121  *
122  * @returns {undefined}
123  */
124 minplayer.players.kaltura.prototype.mediaReady = function() {
125   this.onLoaded();
126 };
127 
128 /**
129  * Called when the media ends.
130  *
131  * @param {type} data
132  * @returns {undefined}
133  */
134 minplayer.players.kaltura.prototype.playerPlayEnd = function(data) {
135   this.onComplete();
136 };
137 
138 /**
139  * Called as the play updates.
140  *
141  * @param {type} data
142  * @returns {undefined}
143  */
144 minplayer.players.kaltura.prototype.playUpdate = function(data) {
145   this.currentTime.set(data);
146 };
147 
148 /**
149  * Called when the duration changes.
150  *
151  * @param {type} data
152  * @returns {undefined}
153  */
154 minplayer.players.kaltura.prototype.durationChange = function(data) {
155   this.duration.set(data.newValue);
156 };
157 
158 /**
159  * Returns the name of this player instance.
160  *
161  * @returns {String}
162  */
163 minplayer.players.kaltura.prototype.getInstance = function() {
164   if (this.instanceName) {
165     return this.instanceName;
166   }
167   var ids = this.uuid.split('__');
168   var instance = 'minplayer.plugins.' + ids[0];
169   instance += '.' + ids[1];
170   instance += '[' + (ids[2] - 1) + ']';
171   this.instanceName = instance;
172   return instance;
173 };
174 
175 /**
176  * Register for the media player events.
177  *
178  * @returns {undefined}
179  */
180 minplayer.players.kaltura.prototype.registerEvents = function() {
181   this.player.addJsListener("adStart", this.getInstance() + '.adStart');
182   this.player.addJsListener("adEnd", this.getInstance() + '.adEnd');
183   this.player.addJsListener("playerStateChange", this.getInstance() + '.playerStateChange');
184   this.player.addJsListener("durationChange", this.getInstance() + '.durationChange');
185   this.player.addJsListener("mediaReady", this.getInstance() + '.mediaReady');
186   this.player.addJsListener("playerUpdatePlayhead", this.getInstance() + '.playUpdate');
187   this.player.addJsListener("playerPlayEnd", this.getInstance() + '.playerPlayEnd');
188 };
189 
190 /**
191  * @see minplayer.players.base#create
192  * @return {object} The media player entity.
193  */
194 minplayer.players.kaltura.prototype.createPlayer = function() {
195   minplayer.players.base.prototype.createPlayer.call(this);
196 
197   // Set the items.
198   var settings = {};
199   var self = this;
200   jQuery.each(['entryId', 'uiConfId', 'partnerId'], function(index, item) {
201     settings[item] = '';
202     if (self.options[item]) {
203       settings[item] = self.options[item];
204     }
205     else {
206       var regex = null;
207       switch (item) {
208         case 'entryId':
209           regex = /.*kaltura\.com.*entry_id\/([^\/]+)/i;
210           break;
211         case 'uiConfId':
212           regex = /.*kaltura\.com.*uiconf_id\/([^\/]+)/i;
213           break;
214         case 'partnerId':
215           regex = /.*kaltura\.com.*wid\/_([^\/]+)/i;
216           break;
217       }
218 
219       // Set the value for this item.
220       if (regex) {
221         settings[item] = self.mediaFile.path.match(regex);
222         if (settings[item]) {
223           settings[item] = settings[item][1];
224         }
225       }
226     }
227   });
228 
229   // Insert the embed javascript.
230   var tag = document.createElement('script');
231   tag.src = 'http://cdnapi.kaltura.com/p/';
232   tag.src += settings.partnerId;
233   tag.src += '/sp/';
234   tag.src += settings.partnerId;
235   tag.src += '00/embedIframeJs/uiconf_id/';
236   tag.src += settings.uiConfId;
237   tag.src += '/partner_id/';
238   tag.src += settings.partnerId;
239   var firstScriptTag = document.getElementsByTagName('script')[0];
240   firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
241 
242   // The player Id.
243   var playerId = this.options.id + '-player';
244 
245   // Check the embed code every second.
246   setTimeout(function checkKaltura() {
247     if (window.hasOwnProperty('kWidget')) {
248       kWidget.embed({
249         'targetId': playerId,
250 	'wid': '_' + settings.partnerId,
251 	'uiconf_id' : settings.uiConfId,
252 	'entry_id' : settings.entryId,
253 	'flashvars':{
254           'autoPlay': false
255         },
256         'params':{
257           'wmode': 'transparent'
258         },
259         readyCallback: function( playerId ){
260           self.player = jQuery('#' + playerId).get(0);
261           self.registerEvents();
262           self.onReady();
263         }
264       });
265     }
266     else {
267       setTimeout(checkKaltura, 1000);
268     }
269   }, 1000);
270 
271   // Return a div tag.
272   return '<div id="' + playerId + '" style="width:100%;height:100%;"></div>';
273 };
274 
275 /**
276  * @see minplayer.players.base#play
277  */
278 minplayer.players.kaltura.prototype.play = function(callback) {
279   minplayer.players.base.prototype.play.call(this, function() {
280     this.player.sendNotification("doPlay");
281     if (callback) {
282       callback.call(this);
283     }
284   });
285 };
286 
287 /**
288  * @see minplayer.players.base#pause
289  */
290 minplayer.players.kaltura.prototype.pause = function(callback) {
291   minplayer.players.base.prototype.pause.call(this, function() {
292     this.player.sendNotification("doPause");
293     if (callback) {
294       callback.call(this);
295     }
296   });
297 };
298 
299 /**
300  * @see minplayer.players.base#stop
301  */
302 minplayer.players.kaltura.prototype.stop = function(callback) {
303   minplayer.players.base.prototype.stop.call(this, function() {
304     this.player.sendNotification("doStop");
305     if (callback) {
306       callback.call(this);
307     }
308   });
309 };
310 
311 /**
312  * @see minplayer.players.base#seek
313  */
314 minplayer.players.kaltura.prototype._seek = function(pos) {
315   this.seekValue = pos;
316   this.player.sendNotification("doSeek", pos);
317 };
318 
319 /**
320  * @see minplayer.players.base#setVolume
321  */
322 minplayer.players.kaltura.prototype.setVolume = function(vol, callback) {
323   minplayer.players.base.prototype.setVolume.call(this, vol, function() {
324     this.player.sendNotification("changeVolume", vol);
325     if (callback) {
326       callback.call(this);
327     }
328   });
329 };
330