2018-03-24 14:13:04 +01:00
|
|
|
#include "util/streamlink.hpp"
|
2018-04-27 22:11:19 +02:00
|
|
|
|
|
|
|
#include "application.hpp"
|
2018-03-24 14:13:04 +01:00
|
|
|
#include "helpers.hpp"
|
|
|
|
#include "singletons/settingsmanager.hpp"
|
|
|
|
#include "widgets/qualitypopup.hpp"
|
|
|
|
|
2018-05-06 16:33:00 +02:00
|
|
|
#include <QErrorMessage>
|
2018-03-24 14:13:04 +01:00
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QProcess>
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
namespace streamlink {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const char *GetBinaryName()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
return "streamlink.exe";
|
|
|
|
#else
|
|
|
|
return "streamlink";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *GetDefaultBinaryPath()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
return "C:\\Program Files (x86)\\Streamlink\\bin\\streamlink.exe";
|
|
|
|
#else
|
|
|
|
return "/usr/bin/streamlink";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-05-06 16:33:00 +02:00
|
|
|
QString getStreamlinkProgram()
|
|
|
|
{
|
|
|
|
auto app = getApp();
|
|
|
|
|
|
|
|
if (app->settings->streamlinkUseCustomPath) {
|
|
|
|
return app->settings->streamlinkPath + "/" + GetBinaryName();
|
|
|
|
} else {
|
|
|
|
return GetBinaryName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-24 14:13:04 +01:00
|
|
|
bool CheckStreamlinkPath(const QString &path)
|
|
|
|
{
|
|
|
|
QFileInfo fileinfo(path);
|
|
|
|
|
|
|
|
if (!fileinfo.exists()) {
|
|
|
|
return false;
|
|
|
|
// throw Exception(fS("Streamlink path ({}) is invalid, file does not exist", path));
|
|
|
|
}
|
|
|
|
|
2018-05-06 16:33:00 +02:00
|
|
|
return fileinfo.isExecutable();
|
2018-03-24 14:13:04 +01:00
|
|
|
}
|
|
|
|
|
2018-05-06 16:33:00 +02:00
|
|
|
void showStreamlinkNotFoundError()
|
2018-03-24 14:13:04 +01:00
|
|
|
{
|
2018-05-06 16:33:00 +02:00
|
|
|
static QErrorMessage *msg = new QErrorMessage;
|
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
auto app = getApp();
|
2018-05-06 16:33:00 +02:00
|
|
|
if (app->settings->streamlinkUseCustomPath) {
|
|
|
|
msg->showMessage(
|
|
|
|
"Unable to find Streamlink executable\nMake sure your custom path is pointing "
|
|
|
|
"to the DIRECTORY where the streamlink executable is located");
|
|
|
|
} else {
|
|
|
|
msg->showMessage("Unable to find Streamlink executable.\nIf you have Streamlink "
|
|
|
|
"installed, you might need to enable the custom path option");
|
|
|
|
}
|
|
|
|
}
|
2018-03-24 14:13:04 +01:00
|
|
|
|
2018-05-06 16:33:00 +02:00
|
|
|
QProcess *createStreamlinkProcess()
|
|
|
|
{
|
|
|
|
auto p = new QProcess;
|
|
|
|
p->setProgram(getStreamlinkProgram());
|
|
|
|
|
|
|
|
QObject::connect(p, &QProcess::errorOccurred, [=](auto err) {
|
|
|
|
if (err == QProcess::FailedToStart) {
|
|
|
|
showStreamlinkNotFoundError();
|
|
|
|
} else {
|
|
|
|
qDebug() << "Error occured: " << err; //
|
|
|
|
}
|
2018-03-24 14:13:04 +01:00
|
|
|
|
2018-05-06 16:33:00 +02:00
|
|
|
p->deleteLater();
|
|
|
|
});
|
2018-03-24 14:13:04 +01:00
|
|
|
|
2018-05-06 16:33:00 +02:00
|
|
|
QObject::connect(p, static_cast<void (QProcess::*)(int)>(&QProcess::finished), [=](int res) {
|
|
|
|
p->deleteLater(); //
|
|
|
|
});
|
2018-03-24 14:13:04 +01:00
|
|
|
|
2018-05-06 16:33:00 +02:00
|
|
|
return p;
|
2018-03-24 14:13:04 +01:00
|
|
|
}
|
|
|
|
|
2018-05-06 16:33:00 +02:00
|
|
|
} // namespace
|
|
|
|
|
2018-03-24 14:13:04 +01:00
|
|
|
void GetStreamQualities(const QString &channelURL, std::function<void(QStringList)> cb)
|
|
|
|
{
|
2018-05-06 16:33:00 +02:00
|
|
|
auto p = createStreamlinkProcess();
|
2018-03-24 14:13:04 +01:00
|
|
|
|
2018-05-06 16:33:00 +02:00
|
|
|
QObject::connect(p, static_cast<void (QProcess::*)(int)>(&QProcess::finished), [=](int res) {
|
|
|
|
if (res != 0) {
|
|
|
|
qDebug() << "Got error code" << res;
|
|
|
|
// return;
|
|
|
|
}
|
2018-03-24 14:13:04 +01:00
|
|
|
QString lastLine = QString(p->readAllStandardOutput());
|
|
|
|
lastLine = lastLine.trimmed().split('\n').last().trimmed();
|
|
|
|
if (lastLine.startsWith("Available streams: ")) {
|
|
|
|
QStringList options;
|
|
|
|
QStringList split = lastLine.right(lastLine.length() - 19).split(", ");
|
|
|
|
|
|
|
|
for (int i = split.length() - 1; i >= 0; i--) {
|
|
|
|
QString option = split.at(i);
|
|
|
|
if (option.endsWith(" (worst)")) {
|
|
|
|
options << option.left(option.length() - 8);
|
|
|
|
} else if (option.endsWith(" (best)")) {
|
|
|
|
options << option.left(option.length() - 7);
|
|
|
|
} else {
|
|
|
|
options << option;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(options);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-05-06 16:33:00 +02:00
|
|
|
p->setArguments({channelURL, "--default-stream=KKona"});
|
2018-03-24 14:13:04 +01:00
|
|
|
|
2018-05-06 16:33:00 +02:00
|
|
|
p->start();
|
|
|
|
}
|
2018-03-24 14:13:04 +01:00
|
|
|
|
|
|
|
void OpenStreamlink(const QString &channelURL, const QString &quality, QStringList extraArguments)
|
|
|
|
{
|
2018-04-27 22:11:19 +02:00
|
|
|
auto app = getApp();
|
2018-03-24 14:13:04 +01:00
|
|
|
|
|
|
|
QStringList arguments;
|
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
QString additionalOptions = app->settings->streamlinkOpts.getValue();
|
2018-03-24 14:13:04 +01:00
|
|
|
if (!additionalOptions.isEmpty()) {
|
2018-04-27 22:11:19 +02:00
|
|
|
arguments << app->settings->streamlinkOpts;
|
2018-03-24 14:13:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
arguments.append(extraArguments);
|
|
|
|
|
|
|
|
arguments << channelURL;
|
|
|
|
|
|
|
|
if (!quality.isEmpty()) {
|
|
|
|
arguments << quality;
|
|
|
|
}
|
|
|
|
|
2018-05-10 18:18:12 +02:00
|
|
|
bool res = QProcess::startDetached(getStreamlinkProgram() + " " + QString(arguments.join(' ')));
|
2018-05-06 16:33:00 +02:00
|
|
|
|
|
|
|
if (!res) {
|
|
|
|
showStreamlinkNotFoundError();
|
|
|
|
}
|
2018-03-24 14:13:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Start(const QString &channel)
|
|
|
|
{
|
2018-04-27 22:11:19 +02:00
|
|
|
auto app = getApp();
|
2018-03-24 14:13:04 +01:00
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
QString channelURL = "twitch.tv/" + channel;
|
2018-03-24 14:13:04 +01:00
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
QString preferredQuality = app->settings->preferredQuality;
|
2018-03-24 14:13:04 +01:00
|
|
|
preferredQuality = preferredQuality.toLower();
|
|
|
|
|
|
|
|
if (preferredQuality == "choose") {
|
|
|
|
GetStreamQualities(channelURL, [=](QStringList qualityOptions) {
|
|
|
|
widgets::QualityPopup::showDialog(channel, qualityOptions);
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList args;
|
|
|
|
|
|
|
|
// Quality converted from Chatterino format to Streamlink format
|
|
|
|
QString quality;
|
|
|
|
// Streamlink qualities to exclude
|
|
|
|
QString exclude;
|
|
|
|
|
|
|
|
if (preferredQuality == "high") {
|
|
|
|
exclude = ">720p30";
|
|
|
|
quality = "high,best";
|
|
|
|
} else if (preferredQuality == "medium") {
|
|
|
|
exclude = ">540p30";
|
|
|
|
quality = "medium,best";
|
|
|
|
} else if (preferredQuality == "low") {
|
|
|
|
exclude = ">360p30";
|
|
|
|
quality = "low,best";
|
|
|
|
} else if (preferredQuality == "audio only") {
|
|
|
|
quality = "audio,audio_only";
|
|
|
|
} else {
|
|
|
|
quality = "best";
|
|
|
|
}
|
|
|
|
if (!exclude.isEmpty()) {
|
|
|
|
args << "--stream-sorting-excludes" << exclude;
|
|
|
|
}
|
|
|
|
|
|
|
|
OpenStreamlink(channelURL, quality, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace streamlink
|
|
|
|
} // namespace chatterino
|