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