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