2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #include "PlayListM3U.h"
22 #include "filesystem/File.h"
25 #include "utils/StringUtils.h"
26 #include "utils/CharsetConverter.h"
27 #include "utils/RegExp.h"
28 #include "utils/log.h"
29 #include "utils/URIUtils.h"
30 #include "settings/AdvancedSettings.h"
31 #include "music/tags/MusicInfoTag.h"
33 using namespace PLAYLIST;
34 using namespace XFILE;
36 #define M3U_START_MARKER "#EXTM3U"
37 #define M3U_INFO_MARKER "#EXTINF"
38 #define M3U_ARTIST_MARKER "#EXTART"
39 #define M3U_ALBUM_MARKER "#EXTALB"
40 #define M3U_STREAM_MARKER "#EXT-X-STREAM-INF"
41 #define M3U_BANDWIDTH_MARKER "BANDWIDTH"
45 // #EXTART:Demo Artist
48 // E:\Program Files\Winamp3\demo.mp3
50 // E:\Program Files\Winamp3\demo.mp3
53 // example m3u8 containing streams of different bitrates
55 // #EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=1600000
57 // #EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=3000000
59 // #EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=800000
63 CPlayListM3U::CPlayListM3U(void)
66 CPlayListM3U::~CPlayListM3U(void)
70 bool CPlayListM3U::Load(const CStdString& strFileName)
74 CStdString strInfo = "";
79 m_strPlayListName = URIUtils::GetFileName(strFileName);
80 URIUtils::GetParentPath(strFileName, m_strBasePath);
83 if (!file.Open(strFileName) )
89 while (file.ReadString(szLine, 1024))
92 strLine.TrimRight(" \t\r\n");
93 strLine.TrimLeft(" \t");
95 if (strLine.Left( (int)strlen(M3U_INFO_MARKER) ) == M3U_INFO_MARKER)
98 int iColon = (int)strLine.find(":");
99 int iComma = (int)strLine.find(",");
100 if (iColon >= 0 && iComma >= 0 && iComma > iColon)
102 // Read the info and duration
104 CStdString strLength = strLine.Mid(iColon, iComma - iColon);
105 lDuration = atoi(strLength.c_str());
107 strInfo = strLine.Right((int)strLine.size() - iComma);
108 g_charsetConverter.unknownToUTF8(strInfo);
111 else if (strLine != M3U_START_MARKER && strLine.Left(strlen(M3U_ARTIST_MARKER)) != M3U_ARTIST_MARKER && strLine.Left(strlen(M3U_ALBUM_MARKER)) != M3U_ALBUM_MARKER )
113 CStdString strFileName = strLine;
115 if (strFileName.size() > 0 && strFileName[0] == '#')
116 continue; // assume a comment or something else we don't support
118 // Skip self - do not load playlist recursively
119 if (URIUtils::GetFileName(strFileName).Equals(m_strPlayListName))
122 if (strFileName.length() > 0)
124 g_charsetConverter.unknownToUTF8(strFileName);
126 // If no info was read from from the extended tag information, use the file name
127 if (strInfo.length() == 0)
129 strInfo = URIUtils::GetFileName(strFileName);
132 // should substitition occur befor or after charset conversion??
133 strFileName = URIUtils::SubstitutePath(strFileName);
135 // Get the full path file name and add it to the the play list
136 CUtil::GetQualifiedFilename(m_strBasePath, strFileName);
137 CFileItemPtr newItem(new CFileItem(strInfo));
138 newItem->SetPath(strFileName);
139 if (lDuration && newItem->IsAudio())
140 newItem->GetMusicInfoTag()->SetDuration(lDuration);
143 // Reset the values just in case there part of the file have the extended marker
155 void CPlayListM3U::Save(const CStdString& strFileName) const
157 if (!m_vecItems.size())
159 CStdString strPlaylist = CUtil::MakeLegalPath(strFileName);
161 if (!file.OpenForWrite(strPlaylist,true))
163 CLog::Log(LOGERROR, "Could not save M3U playlist: [%s]", strPlaylist.c_str());
167 strLine.Format("%s\n",M3U_START_MARKER);
168 file.Write(strLine.c_str(),strLine.size());
169 for (int i = 0; i < (int)m_vecItems.size(); ++i)
171 CFileItemPtr item = m_vecItems[i];
172 CStdString strDescription=item->GetLabel();
173 g_charsetConverter.utf8ToStringCharset(strDescription);
174 strLine.Format( "%s:%i,%s\n", M3U_INFO_MARKER, item->GetMusicInfoTag()->GetDuration() / 1000, strDescription.c_str() );
175 file.Write(strLine.c_str(),strLine.size());
176 CStdString strFileName = ResolveURL(item);
177 g_charsetConverter.utf8ToStringCharset(strFileName);
178 strLine.Format("%s\n",strFileName.c_str());
179 file.Write(strLine.c_str(),strLine.size());
184 CStdString CPlayListM3U::GetBestBandwidthStream(const CStdString &strFileName, size_t bandwidth)
186 // we may be passed a playlist that does not contain playlists of different
187 // bitrates (eg: this playlist is really the HLS video). So, default the
188 // return to the filename so it can be played
191 CStdString strPlaylist = strFileName;
192 size_t maxBandwidth = 0;
194 // if we cannot get the last / we wont be able to determine the sub-playlists
195 size_t baseEnd = strPlaylist.rfind('/');
196 if (baseEnd == std::string::npos)
199 // store the base path (the path without the filename)
200 CStdString basePath = strPlaylist.substr(0, baseEnd + 1);
202 // open the file, and if it fails, return
204 if (!file.Open(strFileName) )
210 // convert bandwidth specified in kbps to bps used by the m3u8
213 while (file.ReadString(szLine, 1024))
215 // read and trim a line
217 strLine.TrimRight(" \t\r\n");
218 strLine.TrimLeft(" \t");
220 // skip the first line
221 if (strLine == M3U_START_MARKER)
223 else if (strLine.Left(strlen(M3U_STREAM_MARKER)) == M3U_STREAM_MARKER)
225 // parse the line so we can pull out the bandwidth
226 std::map< CStdString, CStdString > params = ParseStreamLine(strLine);
227 std::map< CStdString, CStdString >::iterator it = params.find(M3U_BANDWIDTH_MARKER);
229 if (it != params.end())
231 size_t streamBandwidth = atoi(it->second.c_str());
232 if ((maxBandwidth < streamBandwidth) && (streamBandwidth <= bandwidth))
234 // read the next line
235 if (!file.ReadString(szLine, 1024))
239 strLine.TrimRight(" \t\r\n");
240 strLine.TrimLeft(" \t");
242 // this line was empty
246 // store the max bandwidth
247 maxBandwidth = streamBandwidth;
249 // if the path is absolute just use it
250 if (CURL::IsFullPath(strLine))
251 strPlaylist = strLine;
253 strPlaylist = basePath + strLine;
259 CLog::Log(LOGINFO, "Auto-selecting %s based on configured bandwidth.", strPlaylist.c_str());
264 std::map< CStdString, CStdString > CPlayListM3U::ParseStreamLine(const CStdString &streamLine)
266 std::map< CStdString, CStdString > params;
268 // ensure the line has something beyond the stream marker and ':'
269 if (strlen(streamLine) < strlen(M3U_STREAM_MARKER) + 2)
272 // get the actual params following the :
273 CStdString strParams(streamLine.substr(strlen(M3U_STREAM_MARKER) + 1));
275 // separate the parameters
276 CStdStringArray vecParams = StringUtils::SplitString(strParams, ",");
277 for (size_t i = 0; i < vecParams.size(); i++)
279 // split the param, ensure there was an =
280 CStdStringArray vecTuple = StringUtils::SplitString(vecParams[i].Trim(), "=");
281 if (vecTuple.size() < 2)
284 // remove white space from name and value and store it in the dictionary
285 params[vecTuple[0].Trim()] = vecTuple[1].Trim();