Debian Codegraph @

src/playlist.cpp

raw
1 /*
2 * This file is part of Bino, a 3D video player.
3 *
4 * Copyright (C) 2022, 2023, 2024
5 * Martin Lambers <[email protected]>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <QFile>
22 #include <QTextStream>
23 #include <QCommandLineParser>
24
25 #include "playlist.hpp"
26 #include "tools.hpp"
27 #include "log.hpp"
28
29
30 PlaylistEntry::PlaylistEntry() :
31 url(), inputMode(Input_Unknown), surroundMode(Surround_Unknown),
32 videoTrack(NoTrack), audioTrack(NoTrack), subtitleTrack(NoTrack)
33 {
34 }
35
36 PlaylistEntry::PlaylistEntry(const QUrl& url,
37 InputMode inputMode,
38 SurroundMode surroundMode,
39 int videoTrack, int audioTrack, int subtitleTrack) :
40 url(url), inputMode(inputMode), surroundMode(surroundMode),
41 videoTrack(videoTrack), audioTrack(audioTrack), subtitleTrack(subtitleTrack)
42 {
43 }
44
45 bool PlaylistEntry::noMedia() const
46 {
47 return url.isEmpty();
48 }
49
50 QString PlaylistEntry::optionsToString() const
51 {
52 QString s;
53 if (inputMode != Input_Unknown) {
54 s.append(" --input=");
55 s.append(inputModeToString(inputMode));
56 }
57 if (surroundMode != Surround_Unknown) {
58 s.append(" --surround=");
59 s.append(surroundModeToString(surroundMode));
60 }
61 if (videoTrack >= 0) {
62 s.append(" --video-track=");
63 s.append(QString::number(videoTrack));
64 }
65 if (audioTrack >= 0) {
66 s.append(" --audio-track=");
67 s.append(QString::number(audioTrack));
68 }
69 if (subtitleTrack >= 0) {
70 s.append(" --subtitle-track=");
71 s.append(QString::number(subtitleTrack));
72 }
73 return s;
74 }
75
76 bool PlaylistEntry::optionsFromString(const QString& s)
77 {
78 QCommandLineParser parser;
79 parser.addOption({ "input", "", "x" });
80 parser.addOption({ "surround", "", "x" });
81 parser.addOption({ "video-track", "", "x" });
82 parser.addOption({ "audio-track", "", "x" });
83 parser.addOption({ "subtitle-track", "", "x" });
84 if (!parser.parse((QString("dummy ") + s).split(' ')) || parser.positionalArguments().length() != 0) {
85 return false;
86 }
87 InputMode inputMode = Input_Unknown;
88 SurroundMode surroundMode = Surround_Unknown;
89 int videoTrack = PlaylistEntry::DefaultTrack;
90 int audioTrack = PlaylistEntry::DefaultTrack;
91 int subtitleTrack = PlaylistEntry::DefaultTrack;
92 bool ok = true;
93 if (parser.isSet("input")) {
94 inputMode = inputModeFromString(parser.value("input"), &ok);
95 }
96 if (parser.isSet("surround")) {
97 surroundMode = surroundModeFromString(parser.value("surround"), &ok);
98 }
99 if (parser.isSet("video-track")) {
100 int t = parser.value("video-track").toInt(&ok);
101 if (ok && t >= 0)
102 videoTrack = t;
103 else
104 ok = false;
105 }
106 if (parser.isSet("audio-track")) {
107 int t = parser.value("audio-track").toInt(&ok);
108 if (ok && t >= 0)
109 audioTrack = t;
110 else
111 ok = false;
112 }
113 if (parser.isSet("subtitle-track") && parser.value("subtitle-track").length() > 0) {
114 int t = parser.value("subtitle-track").toInt(&ok);
115 if (ok && t >= 0)
116 subtitleTrack = t;
117 else
118 ok = false;
119 }
120 if (ok) {
121 *this = PlaylistEntry(this->url, inputMode, surroundMode, videoTrack, audioTrack, subtitleTrack);
122 }
123 return ok;
124 }
125
126
127 static Playlist* playlistSingleton = nullptr;
128
129 Playlist::Playlist() :
130 _preferredAudio(QLocale::system().language()),
131 _preferredSubtitle(QLocale::system().language()),
132 _wantSubtitle(false),
133 _loopMode(Loop_Off),
134 _waitMode(Wait_Off),
135 _currentIndex(-1)
136 {
137 Q_ASSERT(!playlistSingleton);
138 playlistSingleton = this;
139 }
140
141 Playlist* Playlist::instance()
142 {
143 return playlistSingleton;
144 }
145
146 QLocale::Language Playlist::preferredAudio() const
147 {
148 return _preferredAudio;
149 }
150
151 void Playlist::setPreferredAudio(const QLocale::Language& lang)
152 {
153 _preferredAudio = lang;
154 }
155
156 QLocale::Language Playlist::preferredSubtitle() const
157 {
158 return _preferredSubtitle;
159 }
160
161 void Playlist::setPreferredSubtitle(const QLocale::Language& lang)
162 {
163 _preferredSubtitle = lang;
164 }
165
166 bool Playlist::wantSubtitle() const
167 {
168 return _wantSubtitle;
169 }
170
171 void Playlist::setWantSubtitle(bool want)
172 {
173 _wantSubtitle = want;
174 }
175
176 QList<PlaylistEntry>& Playlist::entries()
177 {
178 return _entries;
179 }
180
181 const QList<PlaylistEntry>& Playlist::entries() const
182 {
183 return _entries;
184 }
185
186 void Playlist::emitMediaChanged()
187 {
188 if (_currentIndex >= 0 && _currentIndex < length()) {
189 emit mediaChanged(_entries[_currentIndex]);
190 } else {
191 emit mediaChanged(PlaylistEntry());
192 }
193 }
194
195 int Playlist::length() const
196 {
197 return _entries.length();
198 }
199
200 void Playlist::append(const PlaylistEntry& entry)
201 {
202 _entries.append(entry);
203 }
204
205 void Playlist::insert(int index, const PlaylistEntry& entry)
206 {
207 _entries.insert(index, entry);
208 }
209
210 void Playlist::remove(int index)
211 {
212 if (index >= 0 && index < length()) {
213 _entries.remove(index);
214 if (_currentIndex == index) {
215 if (_currentIndex >= length())
216 _currentIndex--;
217 emitMediaChanged();
218 } else if (_currentIndex > index) {
219 _currentIndex--;
220 }
221 }
222 }
223
224 void Playlist::clear()
225 {
226 _entries.clear();
227 int prevIndex = _currentIndex;
228 _currentIndex = -1;
229 if (prevIndex >= 0)
230 emitMediaChanged();
231 }
232
233 void Playlist::start()
234 {
235 if (length() > 0 && _currentIndex < 0)
236 setCurrentIndex(0);
237 }
238
239 void Playlist::stop()
240 {
241 setCurrentIndex(-1);
242 }
243
244 void Playlist::next()
245 {
246 if (length() > 0) {
247 switch (loopMode()) {
248 case Loop_Off:
249 if (_currentIndex < length() - 1)
250 setCurrentIndex(_currentIndex + 1);
251 break;
252 case Loop_One:
253 // keep current index but play again
254 emitMediaChanged();
255 break;
256 case Loop_All:
257 if (_currentIndex == length() - 1)
258 setCurrentIndex(0);
259 else
260 setCurrentIndex(_currentIndex + 1);
261 break;
262 }
263 }
264 }
265
266 void Playlist::prev()
267 {
268 if (length() > 0) {
269 switch (loopMode()) {
270 case Loop_Off:
271 if (_currentIndex > 0)
272 setCurrentIndex(_currentIndex - 1);
273 break;
274 case Loop_One:
275 // keep current index but play again
276 emitMediaChanged();
277 break;
278 case Loop_All:
279 if (_currentIndex == 0)
280 setCurrentIndex(length() - 1);
281 else
282 setCurrentIndex(_currentIndex - 1);
283 break;
284 }
285 }
286 }
287
288 void Playlist::setCurrentIndex(int index)
289 {
290 if (index == _currentIndex) {
291 return;
292 }
293
294 if (length() == 0 || index < 0) {
295 _currentIndex = -1;
296 emitMediaChanged();
297 return;
298 }
299
300 if (index >= length()) {
301 index = length() - 1;
302 }
303 if (_currentIndex != index) {
304 LOG_DEBUG("playlist: switching index from %d to %d", _currentIndex, index);
305 _currentIndex = index;
306 emitMediaChanged();
307 }
308 }
309
310 LoopMode Playlist::loopMode() const
311 {
312 return _loopMode;
313 }
314
315 WaitMode Playlist::waitMode() const
316 {
317 return _waitMode;
318 }
319
320 void Playlist::setLoopMode(LoopMode loopMode)
321 {
322 _loopMode = loopMode;
323 }
324
325 void Playlist::setWaitMode(WaitMode waitMode)
326 {
327 _waitMode = waitMode;
328 }
329
330 void Playlist::setWaitModeAuto()
331 {
332 WaitMode waitMode = Wait_Off;
333 for (int i = 0; i < length(); i++) {
334 QString extension = getExtension(entries()[i].url);
335 if (extension == "jpg" || extension == "jpeg"
336 || extension == "png"
337 || extension == "jps"
338 || extension == "mpo") {
339 waitMode = Wait_On;
340 break;
341 }
342 }
343 _waitMode = waitMode;
344 }
345
346 void Playlist::mediaEnded()
347 {
348 if (waitMode() == Wait_Off) {
349 next();
350 }
351 }
352
353 bool Playlist::save(const QString& fileName, QString& errStr) const
354 {
355 QFile file(fileName);
356 if (!file.open(QIODeviceBase::WriteOnly | QIODeviceBase::Truncate | QIODeviceBase::Text)) {
357 errStr = file.errorString();
358 return false;
359 }
360
361 QTextStream out(&file);
362 out << "#EXTM3U\n";
363 for (int i = 0; i < length(); i++) {
364 out << "#EXTINF:0,\n";
365 out << "#EXTBINOOPT:" << entries()[i].optionsToString() << "\n";
366 out << entries()[i].url.toString() << "\n";
367 }
368 file.flush();
369 file.close();
370 if (file.error() != QFileDevice::NoError) {
371 errStr = file.errorString();
372 return false;
373 }
374 return true;
375 }
376
377
378 bool Playlist::load(const QString& fileName, QString& errStr)
379 {
380 QFile file(fileName);
381 if (!file.open(QIODeviceBase::ReadOnly | QIODeviceBase::Text)) {
382 errStr = file.errorString();
383 return false;
384 }
385
386 QList<PlaylistEntry> entries;
387 PlaylistEntry entry;
388 QTextStream in(&file);
389 int lineIndex = 0;
390 while (!in.atEnd()) {
391 QString line = in.readLine();
392 lineIndex++;
393 if (line.startsWith("#EXTBINOOPT: ")) {
394 if (!entry.optionsFromString(line.mid(13))) {
395 LOG_DEBUG("%s line %d: ignoring invalid Bino options", qPrintable(fileName), lineIndex);
396 }
397 } else if (line.isEmpty() || line.startsWith("#")) {
398 continue;
399 } else {
400 entry.url = QUrl::fromUserInput(line, QString("."), QUrl::AssumeLocalFile);
401 if (entry.url.isValid() && (
402 entry.url.scheme() == "file"
403 || entry.url.scheme() == "https"
404 || entry.url.scheme() == "http")) {
405 entries.append(entry);
406 } else {
407 LOG_DEBUG("%s line %d: ignoring invalid URL", qPrintable(fileName), lineIndex);
408 }
409 }
410 }
411 if (file.error() == QFileDevice::NoError) {
412 // overwrite this playlist with new information
413 _entries = entries;
414 _currentIndex = -1;
415 return true;
416 } else {
417 errStr = file.errorString();
418 return false;
419 }
420 }