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, _this = this;
 43   jQuery.each(minplayer.players, function(name, player) {
 44     var priority = player.getPriority();
 45     if (player.canPlay(_this) && (priority > bestpriority)) {
 46       bestplayer = name;
 47       bestpriority = priority;
 48     }
 49   });
 50   return bestplayer;
 51 };
 52 
 53 /**
 54  * The priority of this file is determined by the priority of the best
 55  * player multiplied by the priority of the mimetype.
 56  *
 57  * @return {integer} The priority of the media file.
 58  */
 59 minplayer.file.prototype.getPriority = function() {
 60   var priority = 1;
 61   if (this.player) {
 62     priority = minplayer.players[this.player].getPriority();
 63   }
 64   switch (this.mimetype) {
 65     case 'video/x-webm':
 66     case 'video/webm':
 67     case 'application/octet-stream':
 68       return priority * 10;
 69     case 'video/mp4':
 70     case 'audio/mp4':
 71     case 'audio/mpeg':
 72       return priority * 9;
 73     case 'video/ogg':
 74     case 'audio/ogg':
 75     case 'video/quicktime':
 76       return priority * 8;
 77     default:
 78       return priority * 5;
 79   }
 80 };
 81 
 82 /**
 83  * Returns the file extension of the file path.
 84  *
 85  * @return {string} The file extension.
 86  */
 87 minplayer.file.prototype.getFileExtension = function() {
 88   return this.path.substring(this.path.lastIndexOf('.') + 1).toLowerCase();
 89 };
 90 
 91 /**
 92  * Returns the proper mimetype based off of the extension.
 93  *
 94  * @return {string} The mimetype of the file based off of extension.
 95  */
 96 minplayer.file.prototype.getMimeType = function() {
 97   switch (this.extension) {
 98     case 'mp4': case 'm4v': case 'flv': case 'f4v':
 99       return 'video/mp4';
100     case'webm':
101       return 'video/webm';
102     case 'ogg': case 'ogv':
103       return 'video/ogg';
104     case '3g2':
105       return 'video/3gpp2';
106     case '3gpp':
107     case '3gp':
108       return 'video/3gpp';
109     case 'mov':
110       return 'video/quicktime';
111     case'swf':
112       return 'application/x-shockwave-flash';
113     case 'oga':
114       return 'audio/ogg';
115     case 'mp3':
116       return 'audio/mpeg';
117     case 'm4a': case 'f4a':
118       return 'audio/mp4';
119     case 'aac':
120       return 'audio/aac';
121     case 'wav':
122       return 'audio/vnd.wave';
123     case 'wma':
124       return 'audio/x-ms-wma';
125     default:
126       return 'unknown';
127   }
128 };
129 
130 /**
131  * The type of media this is: video or audio.
132  *
133  * @return {string} "video" or "audio" based on what the type of media this
134  * is.
135  */
136 minplayer.file.prototype.getType = function() {
137   switch (this.mimetype) {
138     case 'video/mp4':
139     case 'video/webm':
140     case 'application/octet-stream':
141     case 'video/x-webm':
142     case 'video/ogg':
143     case 'video/3gpp2':
144     case 'video/3gpp':
145     case 'video/quicktime':
146       return 'video';
147     case 'audio/mp3':
148     case 'audio/mp4':
149     case 'audio/ogg':
150     case 'audio/mpeg':
151       return 'audio';
152     default:
153       return '';
154   }
155 };
156 
157 /**
158  * Returns the ID for this media file.
159  *
160  * @return {string} The id for this media file which is provided by the player.
161  */
162 minplayer.file.prototype.getId = function() {
163   var player = minplayer.players[this.player];
164   return (player && player.getMediaId) ? player.getMediaId(this) : '';
165 };
166