Debian Codegraph @

xbmc/playlists/PlayListM3U.cpp

raw
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #include "PlayListM3U.h"
10
11 #include "FileItem.h"
12 #include "URL.h"
13 #include "Util.h"
14 #include "filesystem/File.h"
15 #include "music/tags/MusicInfoTag.h"
16 #include "utils/CharsetConverter.h"
17 #include "utils/URIUtils.h"
18 #include "utils/log.h"
19 #include "video/VideoInfoTag.h"
20
21 #include <inttypes.h>
22
23 using namespace PLAYLIST;
24 using namespace XFILE;
25
26 const char* CPlayListM3U::StartMarker = "#EXTCPlayListM3U::M3U";
27 const char* CPlayListM3U::InfoMarker = "#EXTINF";
28 const char* CPlayListM3U::ArtistMarker = "#EXTART";
29 const char* CPlayListM3U::AlbumMarker = "#EXTALB";
30 const char* CPlayListM3U::PropertyMarker = "#KODIPROP";
31 const char* CPlayListM3U::VLCOptMarker = "#EXTVLCOPT";
32 const char* CPlayListM3U::StreamMarker = "#EXT-X-STREAM-INF";
33 const char* CPlayListM3U::BandwidthMarker = "BANDWIDTH";
34 const char* CPlayListM3U::OffsetMarker = "#EXT-KX-OFFSET";
35
36 // example m3u file:
37 // #EXTM3U
38 // #EXTART:Demo Artist
39 // #EXTALB:Demo Album
40 // #KODIPROP:name=value
41 // #EXTINF:5,demo
42 // E:\Program Files\Winamp3\demo.mp3
43
44
45
46 // example m3u8 containing streams of different bitrates
47 // #EXTM3U
48 // #EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=1600000
49 // playlist_1600.m3u8
50 // #EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=3000000
51 // playlist_3000.m3u8
52 // #EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=800000
53 // playlist_800.m3u8
54
55
56 CPlayListM3U::CPlayListM3U(void) = default;
57
58 CPlayListM3U::~CPlayListM3U(void) = default;
59
60
61 bool CPlayListM3U::Load(const std::string& strFileName)
62 {
63 char szLine[4096];
64 std::string strLine;
65 std::string strInfo;
66 std::vector<std::pair<std::string, std::string> > properties;
67
68 int lDuration = 0;
69 int iStartOffset = 0;
70 int iEndOffset = 0;
71
72 bool utf8 = false;
73 if (URIUtils::GetExtension(strFileName) == ".m3u8")
74 utf8 = true;
75
76 Clear();
77
78 m_strPlayListName = URIUtils::GetFileName(strFileName);
79 URIUtils::GetParentPath(strFileName, m_strBasePath);
80
81 CFile file;
82 if (!file.Open(strFileName) )
83 {
84 file.Close();
85 return false;
86 }
87
88 while (file.ReadString(szLine, 4095))
89 {
90 strLine = szLine;
91 StringUtils::Trim(strLine);
92
93 if (StringUtils::StartsWith(strLine, InfoMarker))
94 {
95 // start of info
96 size_t iColon = strLine.find(':');
97 size_t iComma = strLine.find(',');
98 if (iColon != std::string::npos &&
99 iComma != std::string::npos &&
100 iComma > iColon)
101 {
102 // Read the info and duration
103 iColon++;
104 std::string strLength = strLine.substr(iColon, iComma - iColon);
105 lDuration = atoi(strLength.c_str());
106 iComma++;
107 strInfo = strLine.substr(iComma);
108 if (!utf8)
109 g_charsetConverter.unknownToUTF8(strInfo);
110 }
111 }
112 else if (StringUtils::StartsWith(strLine, OffsetMarker))
113 {
114 size_t iColon = strLine.find(':');
115 size_t iComma = strLine.find(',');
116 if (iColon != std::string::npos &&
117 iComma != std::string::npos &&
118 iComma > iColon)
119 {
120 // Read the start and end offset
121 iColon++;
122 iStartOffset = atoi(strLine.substr(iColon, iComma - iColon).c_str());
123 iComma++;
124 iEndOffset = atoi(strLine.substr(iComma).c_str());
125 }
126 }
127 else if (StringUtils::StartsWith(strLine, PropertyMarker)
128 || StringUtils::StartsWith(strLine, VLCOptMarker))
129 {
130 size_t iColon = strLine.find(':');
131 size_t iEqualSign = strLine.find('=');
132 if (iColon != std::string::npos &&
133 iEqualSign != std::string::npos &&
134 iEqualSign > iColon)
135 {
136 std::string strFirst, strSecond;
137 properties.emplace_back(
138 StringUtils::Trim((strFirst = strLine.substr(iColon + 1, iEqualSign - iColon - 1))),
139 StringUtils::Trim((strSecond = strLine.substr(iEqualSign + 1))));
140 }
141 }
142 else if (strLine != StartMarker &&
143 !StringUtils::StartsWith(strLine, ArtistMarker) &&
144 !StringUtils::StartsWith(strLine, AlbumMarker))
145 {
146 std::string strFileName = strLine;
147
148 if (!strFileName.empty() && strFileName[0] == '#')
149 continue; // assume a comment or something else we don't support
150
151 // Skip self - do not load playlist recursively
152 // We compare case-less in case user has input incorrect case of the current playlist
153 if (StringUtils::EqualsNoCase(URIUtils::GetFileName(strFileName), m_strPlayListName))
154 continue;
155
156 if (strFileName.length() > 0)
157 {
158 if (!utf8)
159 g_charsetConverter.unknownToUTF8(strFileName);
160
161 // If no info was read from from the extended tag information, use the file name
162 if (strInfo.length() == 0)
163 {
164 strInfo = URIUtils::GetFileName(strFileName);
165 }
166
167 // should substitution occur before or after charset conversion??
168 strFileName = URIUtils::SubstitutePath(strFileName);
169
170 // Get the full path file name and add it to the the play list
171 CUtil::GetQualifiedFilename(m_strBasePath, strFileName);
172 CFileItemPtr newItem(new CFileItem(strInfo));
173 newItem->SetPath(strFileName);
174 if (iStartOffset != 0 || iEndOffset != 0)
175 {
176 newItem->SetStartOffset(iStartOffset);
177 newItem->m_lStartPartNumber = 1;
178 newItem->SetProperty("item_start", iStartOffset);
179 newItem->SetEndOffset(iEndOffset);
180 // Prevent load message from file and override offset set here
181 newItem->GetMusicInfoTag()->SetLoaded();
182 newItem->GetMusicInfoTag()->SetTitle(strInfo);
183 if (iEndOffset)
184 lDuration = static_cast<int>(CUtil::ConvertMilliSecsToSecsIntRounded(iEndOffset - iStartOffset));
185 }
186 if (newItem->IsVideo() && !newItem->HasVideoInfoTag()) // File is a video and needs a VideoInfoTag
187 newItem->GetVideoInfoTag()->Reset(); // Force VideoInfoTag creation
188 if (lDuration && newItem->IsAudio())
189 newItem->GetMusicInfoTag()->SetDuration(lDuration);
190 for (auto &prop : properties)
191 {
192 newItem->SetProperty(prop.first, prop.second);
193 }
194
195 newItem->SetMimeType(newItem->GetProperty("mimetype").asString());
196 if (!newItem->GetMimeType().empty())
197 newItem->SetContentLookup(false);
198
199 Add(newItem);
200
201 // Reset the values just in case there part of the file have the extended marker
202 // and part don't
203 strInfo = "";
204 lDuration = 0;
205 iStartOffset = 0;
206 iEndOffset = 0;
207 properties.clear();
208 }
209 }
210 }
211
212 file.Close();
213 return true;
214 }
215
216 void CPlayListM3U::Save(const std::string& strFileName) const
217 {
218 if (!m_vecItems.size())
219 return;
220 bool utf8 = false;
221 if (URIUtils::GetExtension(strFileName) == ".m3u8")
222 utf8 = true;
223 std::string strPlaylist = CUtil::MakeLegalPath(strFileName);
224 CFile file;
225 if (!file.OpenForWrite(strPlaylist,true))
226 {
227 CLog::Log(LOGERROR, "Could not save M3U playlist: [{}]", strPlaylist);
228 return;
229 }
230 std::string strLine = StringUtils::Format("{}\n", StartMarker);
231 if (file.Write(strLine.c_str(), strLine.size()) != static_cast<ssize_t>(strLine.size()))
232 return; // error
233
234 for (int i = 0; i < (int)m_vecItems.size(); ++i)
235 {
236 CFileItemPtr item = m_vecItems[i];
237 std::string strDescription=item->GetLabel();
238 if (!utf8)
239 g_charsetConverter.utf8ToStringCharset(strDescription);
240 strLine = StringUtils::Format("{}:{},{}\n", InfoMarker,
241 item->GetMusicInfoTag()->GetDuration(), strDescription);
242 if (file.Write(strLine.c_str(), strLine.size()) != static_cast<ssize_t>(strLine.size()))
243 return; // error
244 if (item->GetStartOffset() != 0 || item->GetEndOffset() != 0)
245 {
246 strLine = StringUtils::Format("{}:{},{}\n", OffsetMarker, item->GetStartOffset(),
247 item->GetEndOffset());
248 file.Write(strLine.c_str(),strLine.size());
249 }
250 std::string strFileName = ResolveURL(item);
251 if (!utf8)
252 g_charsetConverter.utf8ToStringCharset(strFileName);
253 strLine = StringUtils::Format("{}\n", strFileName);
254 if (file.Write(strLine.c_str(), strLine.size()) != static_cast<ssize_t>(strLine.size()))
255 return; // error
256 }
257 file.Close();
258 }
259
260 std::map< std::string, std::string > CPlayListM3U::ParseStreamLine(const std::string &streamLine)
261 {
262 std::map< std::string, std::string > params;
263
264 // ensure the line has something beyond the stream marker and ':'
265 if (streamLine.size() < strlen(StreamMarker) + 2)
266 return params;
267
268 // get the actual params following the :
269 std::string strParams(streamLine.substr(strlen(StreamMarker) + 1));
270
271 // separate the parameters
272 std::vector<std::string> vecParams = StringUtils::Split(strParams, ",");
273 for (std::vector<std::string>::iterator i = vecParams.begin(); i != vecParams.end(); ++i)
274 {
275 // split the param, ensure there was an =
276 StringUtils::Trim(*i);
277 std::vector<std::string> vecTuple = StringUtils::Split(*i, "=");
278 if (vecTuple.size() < 2)
279 continue;
280
281 // remove white space from name and value and store it in the dictionary
282 StringUtils::Trim(vecTuple[0]);
283 StringUtils::Trim(vecTuple[1]);
284 params[vecTuple[0]] = vecTuple[1];
285 }
286
287 return params;
288 }
289