sbM3UPlaylistHandler.js
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2010 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
28 const TOKEN_EXTM3U = "#EXTM3U";
29 const TOKEN_EXTINF = "#EXTINF:";
30 const TOKEN_COMMENT = "#";
31 
33  this._originalURI = null;
34 }
35 
36 // sbIPlaylistReader
37 sbM3UPlaylistHandler.prototype.__defineGetter__("originalURI",
38 function()
39 {
40  return this._originalURI;
41 });
42 
43 sbM3UPlaylistHandler.prototype.__defineSetter__("originalURI",
44 function(value)
45 {
46  this._originalURI = value;
47 });
48 
49 sbM3UPlaylistHandler.prototype.read =
50 function(aFile, aMediaList, aReplace, aPlaylistFormatType)
51 {
52  var nextFileMetadata = {};
53  var toAdd = [];
54 
55  // Match TOKEN_EXTINF then all characters that are not commas, then an
56  // optional comma, then capture the rest of the string
57  var re = new RegExp("^" + TOKEN_EXTINF + "([^,]*),?(.*)?$");
58 
59  var self = this;
60  SB_DetectCharsetAndProcessFile(aFile, function(aLine) {
61  // Skip the TOKEN_EXTM3U header and blank lines
62  if (aLine == TOKEN_EXTM3U || aLine == "")
63  return;
64 
65  // If we get a TOKEN_EXTINF line, parse and store the data for the next
66  // file
67  var a = aLine.match(re);
68  if (a) {
69  var duration = parseInt(a[1], 10);
70  if (!isNaN(duration) && duration >= 0)
71  nextFileMetadata.duration = duration;
72  if (a[2] != "")
73  nextFileMetadata.title = a[2];
74  }
75 
76  // Skip any other line starting with the comment token
77  if (aLine.indexOf(TOKEN_COMMENT) == 0) {
78  return;
79  }
80 
81  if (aPlaylistFormatType && aPlaylistFormatType.pathSeparator) {
82  // Convert the playlist format path separator to a forward slash.
83  aLine = aLine.replace(aPlaylistFormatType.pathSeparator, "/", "g");
84  }
85 
86  // Otherwise, this line is a URL. Add it to the list
87  var newUri = SB_ResolveURI(aLine, this._originalURI);
88 
89  if (newUri) {
90  var item = { uri: newUri, properties: {}};
91  if (nextFileMetadata.title)
92  item.properties[SBProperties.trackName] = nextFileMetadata.title;
93  if (nextFileMetadata.duration)
94  item.properties[SBProperties.duration] = nextFileMetadata.duration * 1000000;
95  toAdd.push(item);
96 
97  nextFileMetadata = {};
98  }
99 
100  }, this);
101 
102  SB_AddItems(toAdd, aMediaList, aReplace);
103 
104 }
105 
106 sbM3UPlaylistHandler.prototype.vote =
107 function(aURL)
108 {
109  return 10000;
110 }
111 
112 sbM3UPlaylistHandler.prototype.name =
113 function()
114 {
115  return "Songbird M3U Reader";
116 }
117 
118 sbM3UPlaylistHandler.prototype.description =
119 function()
120 {
121  return "Loads M3U playlists from remote and local locations.";
122 }
123 
124 sbM3UPlaylistHandler.prototype.supportedMIMETypes =
125 function(aMIMECount)
126 {
127  var mimeTypes = ["audio/mpegurl", "audio/x-mpegurl"];
128  aMIMECount.value = mimeTypes.length;
129  return mimeTypes;
130 }
131 
132 sbM3UPlaylistHandler.prototype.supportedFileExtensions =
133 function(aExtCount)
134 {
135  var exts = ["m3u", "m3u8"];
136  aExtCount.value = exts.length;
137  return exts;
138 }
139 
140 sbM3UPlaylistHandler.prototype.QueryInterface =
141 function(iid)
142 {
143  if (!iid.equals(Ci.sbIPlaylistReader) &&
144  !iid.equals(Ci.nsISupports))
145  throw Cr.NS_ERROR_NO_INTERFACE;
146  return this;
147 }
148 
const TOKEN_EXTM3U
const TOKEN_EXTINF
function SB_DetectCharsetAndProcessFile(aFile, aCallback, aThis)
Detect the charset of the file aFile, convert the file encode to the detected one, and process the file line by line with the callback function aCallback.
_hideDatepicker duration
function sbM3UPlaylistHandler()
return null
Definition: FeedWriter.js:1143
var uri
Definition: FeedWriter.js:1135
function SB_AddItems(aItems, aMediaList, aAddDistinctOnly)
countRef value
Definition: FeedWriter.js:1423
const Cr
const Ci
function SB_ResolveURI(aStringURL, aBaseURI)
const TOKEN_COMMENT