summaryrefslogtreecommitdiff
path: root/src/chromecast.cpp
blob: 923b1f75e103e00ad924dd9b4575c1140432c94e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*  smplayer, GUI front-end for mplayer.
    Copyright (C) 2006-2018 Ricardo Villalba <rvm@users.sourceforge.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "chromecast.h"
#include "links.h"
#include <QUrl>
#include <QFileInfo>
#include <QDesktopServices>
#include <QNetworkInterface>
#include <QProcess>
#include <QDir>
#include <QSettings>
#include <QDebug>
#include "helper.h"

#ifdef CONVERT_TO_VTT
#include "subreader.h"
#endif

//#define CHROMECAST_USE_SERVER_WEBFSD
//#define CHROMECAST_USE_SERVER_SIMPLE_WEB_SERVER

#if !defined(CHROMECAST_USE_SERVER_WEBFSD) && !defined(CHROMECAST_USE_SERVER_SIMPLE_WEB_SERVER)
  #define CHROMECAST_USE_SERVER_SIMPLE_WEB_SERVER
#endif

Chromecast * Chromecast::instance_obj = 0;

Chromecast * Chromecast::instance() {
	if (instance_obj == 0) {
		instance_obj = new Chromecast();
	}
	return instance_obj;
}

void Chromecast::deleteInstance() {
	if (instance_obj) {
		delete instance_obj;
		instance_obj = 0;
	}
}


Chromecast::Chromecast(QObject * parent)
	: QObject(parent)
	, server_process(0)
	, settings(0)
	, server_port(8010)
	, directory_listing(true)
	, server_needs_restart(false)
{
#ifdef CONVERT_TO_VTT
	autoconvert_to_vtt = true;
	sub_encoding = "ISO-8859-1";
	sub_position = -1;
	overwrite_vtt = false;
	use_sub_filter = true;
#endif
}

Chromecast::~Chromecast() {
	if (server_process) {
		stopServer();
	}
	saveSettings();
}

void Chromecast::openStream(const QString & url, const QString & title) {
	QDesktopServices::openUrl(QUrl(URL_CHROMECAST "/?title=" + title.toUtf8().toBase64() +
		"&url=" + url.toUtf8().toBase64()));
}

void Chromecast::openLocal(const QString & file, const QString & title, const QString & subtitle) {
	qDebug() << "Chromecast::openLocal: file:" << file << "subtitle:" << subtitle;

	QFileInfo fi(file);
	QString dir = fi.absolutePath();
	QString filename = fi.fileName();

	qDebug() << "Chromecast::openLocal: dir:" << dir;
	qDebug() << "Chromecast::openLocal: filename:" << filename;

	QString local_address = localAddress();
	if (local_address.isEmpty()) local_address = findLocalAddress();
	qDebug() << "Chromecast::openLocal: chosen address:" << local_address;

	// Find subtitle file
#ifdef SERVE_FILE_DIR_ONLY
	QString sub_filename;
	if (!subtitle.isEmpty()) {
		// Check if subtitle is in the video directory
		if (QFile::exists(dir +"/"+ subtitle)) {
			sub_filename = subtitle;
			qDebug() << "Chromecast::openLocal: sub_filename:" << sub_filename;
		} else {
			// Check if it's an absolute path
			if (QFile::exists(subtitle)) {
				QFileInfo sub_fi(subtitle);
				if (QFile::exists(dir +"/"+ sub_fi.fileName())) {
					sub_filename = sub_fi.fileName();
				}
			}
		}
		sub_filename = checkForVTT(dir, sub_filename);
		qDebug() << "Chromecast::openLocal: sub_filename:" << sub_filename;
	}
#else
	QString sub_filepath;
	if (!subtitle.isEmpty()) {
		if (QFile::exists(subtitle)) {
			sub_filepath = subtitle;
		} else {
			if (QFile::exists(dir +"/"+ subtitle)) {
				sub_filepath = dir +"/"+ subtitle;
			}
		}
		qDebug() << "Chromecast::openLocal: sub_filepath:" << sub_filepath;
	}
#endif

	if (!local_address.isEmpty()) {
		// Run web server

		#ifdef SERVE_FILE_DIR_ONLY
		startServer(dir);
		QString url = "http://" + local_address + ":" + QString::number(server_port) + "/" + filename;
		QString sub_url;
		if (!sub_filename.isEmpty()) {
			sub_url = "http://" + local_address + ":" + QString::number(server_port) + "/" + sub_filename;
		}
		#else
		QString root = QDir::rootPath();
		startServer(root);

		QString abs_filename = filepathWithoutRoot(file);
		qDebug() << "Chromecast::openLocal: abs_filename:" << abs_filename;
		QString url = "http://" + local_address + ":" + QString::number(server_port) + "/" + abs_filename;

		QString abs_sub_filename = filepathWithoutRoot(sub_filepath);
		qDebug() << "Chromecast::openLocal: abs_sub_filename:" << abs_sub_filename;
		QString sub_url = "http://" + local_address + ":" + QString::number(server_port) + "/" + abs_sub_filename;
		#endif

		qDebug() << "Chromecast::openLocal: url:" << url;
		qDebug() << "Chromecast::openLocal: sub_url:" << sub_url;

		#if 1
		QDesktopServices::openUrl(QUrl(URL_CHROMECAST "/?title=" + title.toUtf8().toBase64() +
			"&url=" + url.toUtf8().toBase64() + "&subtitles=" + sub_url.toUtf8().toBase64()));
		#endif
	}
}

#ifndef SERVE_FILE_DIR_ONLY
QString Chromecast::filepathWithoutRoot(const QString & filepath) {
	QString root = QDir::rootPath();
	QFileInfo fi(filepath);
	QString abs_filename = fi.absoluteFilePath();
	if (abs_filename.startsWith(root)) {
		abs_filename = abs_filename.mid(root.length());
	}
	return abs_filename;
}
#endif

QString Chromecast::checkForVTT(const QString & video_path, const QString & subtitle_file) {
	QString actual_subtitle = subtitle_file;
	QString dir = video_path;

	QString subtitle_path = dir +"/"+ subtitle_file;
	QFileInfo fi(subtitle_path);
	if (fi.suffix().toLower() == "srt") {
		qDebug() << "Chromecast::checkForVTT: subtitle is in srt format";

		#ifdef CONVERT_TO_VTT
		if (autoconvert_to_vtt) {
			SubReader sr;
			sr.setInputCodec(sub_encoding.toLatin1());
			sr.setVTTLinePosition(sub_position);
			if (use_sub_filter) {
				QString filter = sub_filter;
				if (filter.isEmpty()) filter = "opensubtitles|subtitulos\\.es|tusubtitulo|osdb|addic7ed";
				sr.setTextFilter(filter);
			}
			sr.setOverwriteVTT(overwrite_vtt);
			sr.autoConvertToVTT(subtitle_path);
		}
		#endif

		// Check if a subtitle file with vtt extension exists
		QString vtt_subtitle = fi.completeBaseName() + ".vtt";
		QString vtt_subtitle_path = dir +"/"+ vtt_subtitle;
		if (QFile::exists(vtt_subtitle_path)) {
			qDebug() << "Chromecast::checkForVTT: using" << vtt_subtitle;
			actual_subtitle = vtt_subtitle;
		}
	}

	return actual_subtitle;
}

QStringList Chromecast::localAddresses() {
	QStringList l;

	foreach(const QHostAddress &address, QNetworkInterface::allAddresses()) {
		if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress(QHostAddress::LocalHost)) {
			l << address.toString();
		}
	}

	return l;
}

QString Chromecast::findLocalAddress() {
	QString local_address;

	QStringList addresses = localAddresses();
	qDebug() << "Chromecast::defaultLocalAddress: all IPv4 addresses:" << addresses;

	foreach(QString address, addresses) {
		//qDebug() << "Chromecast::defaultLocalAddress: address:" << address;
		if (address.startsWith("192.") && !address.endsWith(".1")) {
			local_address = address;
			break;
		}
	}

	if (local_address.isEmpty() && !addresses.isEmpty()) local_address = addresses[0];
	return local_address;
}

void Chromecast::startServer(QString doc_root) {
	qDebug("Chromecast::startServer");

	static QString last_doc_root;

	if (server_process == 0) {
		server_process = new QProcess(this);
		server_process->setProcessChannelMode( QProcess::MergedChannels );
		connect(server_process, SIGNAL(readyReadStandardOutput()), this, SLOT(readProcessOutput()));
		connect(server_process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processFinished(int, QProcess::ExitStatus)));
		connect(server_process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
	}

	if (server_process->state() == QProcess::Running) {
		if (!server_needs_restart && last_doc_root == doc_root) {
			return;
		} else {
			stopServer();
		}
	}

	QString prog;
	QStringList args;

#if defined(Q_OS_WIN) && defined(SERVE_FILE_DIR_ONLY)
	doc_root = Helper::shortPathName(doc_root);
#endif

#if defined(CHROMECAST_USE_SERVER_WEBFSD)
	prog = "webfsd";
	args << "-F" << "-d" << "-4" << "-p" << QString::number(server_port) << "-r" << doc_root;
	if (!directoryListing()) args << "-j";
#elif defined(CHROMECAST_USE_SERVER_SIMPLE_WEB_SERVER)
	prog = "simple_web_server";
	#ifdef Q_OS_WIN
	prog += ".exe";
	#endif
	args << "-p" << QString::number(server_port) << "-r" << doc_root;
	if (!directoryListing()) args << "-j";
#else
	#error "No server defined"
#endif

	{
		QString command = prog + " " + args.join(" ");
		qDebug() << "Chromecast::startServer: command:" << command;
	}

	server_process->start(prog, args);

	last_doc_root = doc_root;
	server_needs_restart = false;
}


void Chromecast::stopServer() {
	qDebug("Chromecast::stopServer");

	if (server_process) {
		server_process->terminate();
		if (!server_process->waitForFinished(5000)) {
			server_process->kill();
			server_process->waitForFinished();
		}
	}
}

// Slots
void Chromecast::readProcessOutput() {
	QByteArray line;
	while (server_process->canReadLine()) {
		line = server_process->readLine().trimmed();
		qDebug() << "Chromecast::readProcessOutput: line:" << line;
	}
}

void Chromecast::processFinished(int exit_code, QProcess::ExitStatus exit_status) {
	qDebug() << "Chromecast::processFinished: exit_code:" << exit_code << "exit_status:" << exit_status;
}

void Chromecast::processError(QProcess::ProcessError error) {
	qDebug() << "Chromecast::processError:" << error;
	if (error == QProcess::FailedToStart) {
		qDebug("Chromecast::processError: process failed to start");
	}
}

void Chromecast::loadSettings() {
	qDebug("Chromecast::loadSettings");

	if (settings) {
		settings->beginGroup("chromecast/server");
		setServerPort(settings->value("port", serverPort()).toInt());
		setLocalAddress(settings->value("local_address", localAddress()).toString());
		setDirectoryListing(settings->value("directory_listing", directoryListing()).toBool());
		settings->endGroup();

		#ifdef CONVERT_TO_VTT
		settings->beginGroup("chromecast/subtitles");
		setAutoConvertToVTT(settings->value("autoconvert_to_vtt", autoConvertToVTT()).toBool());
		//setSubtitleEncoding(settings->value("encoding", subtitleEncoding()).toString());
		setSubtitlePosition(settings->value("position_on_screen", 93).toInt());
		setSubtitleFilter(settings->value("text_filter", subtitleFilter()).toString());
		setOverwriteVTT(settings->value("overwrite_vtt", overwriteVTT()).toBool());
		enableSubtitleFilter(settings->value("use_sub_filter", isSubtitleFilterEnabled()).toBool());
		settings->endGroup();
		#endif
	}
}

void Chromecast::saveSettings() {
	qDebug("Chromecast::saveSettings");

	if (settings) {
		settings->beginGroup("chromecast/server");
		settings->setValue("port", serverPort());
		settings->setValue("local_address", localAddress());
		settings->setValue("directory_listing", directoryListing());
		settings->endGroup();

		#ifdef CONVERT_TO_VTT
		settings->beginGroup("chromecast/subtitles");
		settings->setValue("autoconvert_to_vtt", autoConvertToVTT());
		//settings->setValue("encoding", subtitleEncoding());
		settings->setValue("position_on_screen", subtitlePosition());
		settings->setValue("text_filter", subtitleFilter());
		settings->setValue("overwrite_vtt", overwriteVTT());
		settings->setValue("use_sub_filter", isSubtitleFilterEnabled());
		settings->endGroup();
		#endif
	}
}

#include "moc_chromecast.cpp"