1 /** The minplayer namespace. */
  2 var minplayer = minplayer || {};
  3 
  4 /**
  5  * @constructor
  6  * @class A wrapper class used to provide all the data necessary to control an
  7  * individual file within this media player.
  8  *
  9  * @param {object} file A media file object with minimal required information.
 10  */
 11 minplayer.file = function(file) {
 12   this.duration = file.duration || 0;
 13   this.bytesTotal = file.bytesTotal || 0;
 14   this.quality = file.quality || 0;
 15   this.stream = file.stream || '';
 16   this.path = file.path || '';
 17   this.codecs = file.codecs || '';
 18 
 19   // These should be provided, but just in case...
 20   this.extension = file.extension || this.getFileExtension();
 21   this.mimetype = file.mimetype || file.filemime || this.getMimeType();
 22   this.type = file.type || this.getType();
 23 
 24   // Fail safe to try and guess the mimetype and media type.
 25   if (!this.type) {
 26     this.mimetype = this.getMimeType();
 27     this.type = this.getType();
 28   }
 29 
 30   // Get the player.
 31   this.player = file.player || this.getBestPlayer();
 32   this.priority = file.priority || this.getPriority();
 33   this.id = file.id || this.getId();
 34 };
 35 
 36 /**
 37  * Returns the best player for the job.
 38  *
 39  * @return {string} The best player to play the media file.
 40  */
 41 minplayer.file.prototype.getBestPlayer = function() {
 42   var bestplayer = null, bestpriority = 0;
 43   jQuery.each(minplayer.players, (function(file) {
 44     return function(name, player) {
 45       var priority = player.getPriority();
 46       if (player.canPlay(file) && (priority > bestpriority)) {
 47         bestplayer = name;
 48         bestpriority = priority;
 49       }
 50     };
 51   })(this));
 52   return bestplayer;
 53 };
 54 
 55 /**
 56  * The priority of this file is determined by the priority of the best
 57  * player multiplied by the priority of the mimetype.
 58  *
 59  * @return {integer} The priority of the media file.
 60  */
 61 minplayer.file.prototype.getPriority = function() {
 62   var priority = 1;
 63   if (this.player) {
 64     priority = minplayer.players[this.player].getPriority();
 65   }
 66   switch (this.mimetype) {
 67     case 'video/x-webm':
 68     case 'video/webm':
 69     case 'application/octet-stream':
 70       return priority * 10;
 71     case 'video/mp4':
 72     case 'audio/mp4':
 73     case 'audio/mpeg':
 74       return priority * 9;
 75     case 'video/ogg':
 76     case 'audio/ogg':
 77     case 'video/quicktime':
 78       return priority * 8;
 79     default:
 80       return priority * 5;
 81   }
 82 };
 83 
 84 /**
 85  * Returns the file extension of the file path.
 86  *
 87  * @return {string} The file extension.
 88  */
 89 minplayer.file.prototype.getFileExtension = function() {
 90   return this.path.substring(this.path.lastIndexOf('.') + 1).toLowerCase();
 91 };
 92 
 93 /**
 94  * Returns the proper mimetype based off of the extension.
 95  *
 96  * @return {string} The mimetype of the file based off of extension.
 97  */
 98 minplayer.file.prototype.getMimeType = function() {
 99   switch (this.extension) {
100     case 'mp4': case 'm4v': case 'flv': case 'f4v':
101       return 'video/mp4';
102     case'webm':
103       return 'video/webm';
104     case 'ogg': case 'ogv':
105       return 'video/ogg';
106     case '3g2':
107       return 'video/3gpp2';
108     case '3gpp':
109     case '3gp':
110       return 'video/3gpp';
111     case 'mov':
112       return 'video/quicktime';
113     case'swf':
114       return 'application/x-shockwave-flash';
115     case 'oga':
116       return 'audio/ogg';
117     case 'mp3':
118       return 'audio/mpeg';
119     case 'm4a': case 'f4a':
120       return 'audio/mp4';
121     case 'aac':
122       return 'audio/aac';
123     case 'wav':
124       return 'audio/vnd.wave';
125     case 'wma':
126       return 'audio/x-ms-wma';
127     default:
128       return 'unknown';
129   }
130 };
131 
132 /**
133  * The type of media this is: video or audio.
134  *
135  * @return {string} "video" or "audio" based on what the type of media this
136  * is.
137  */
138 minplayer.file.prototype.getType = function() {
139   switch (this.mimetype) {
140     case 'video/mp4':
141     case 'video/webm':
142     case 'application/octet-stream':
143     case 'video/x-webm':
144     case 'video/ogg':
145     case 'video/3gpp2':
146     case 'video/3gpp':
147     case 'video/quicktime':
148       return 'video';
149     case 'audio/mp3':
150     case 'audio/mp4':
151     case 'audio/ogg':
152     case 'audio/mpeg':
153       return 'audio';
154     default:
155       return '';
156   }
157 };
158 
159 /**
160  * Returns the ID for this media file.
161  *
162  * @return {string} The id for this media file which is provided by the player.
163  */
164 minplayer.file.prototype.getId = function() {
165   var player = minplayer.players[this.player];
166   return (player && player.getMediaId) ? player.getMediaId(this) : '';
167 };
168