mirror-chatterino2/src/util/StreamLink.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

278 lines
7.7 KiB
C++
Raw Normal View History

2018-06-26 14:09:39 +02:00
#include "util/StreamLink.hpp"
2018-06-26 14:09:39 +02:00
#include "Application.hpp"
#include "common/QLogging.hpp"
#include "common/Version.hpp"
#include "providers/irc/IrcMessageBuilder.hpp"
2018-06-28 19:46:45 +02:00
#include "singletons/Settings.hpp"
#include "singletons/WindowManager.hpp"
2018-11-23 17:51:55 +01:00
#include "util/Helpers.hpp"
#include "util/SplitCommand.hpp"
2018-06-26 15:11:45 +02:00
#include "widgets/dialogs/QualityPopup.hpp"
#include "widgets/splits/Split.hpp"
#include "widgets/Window.hpp"
#include <QErrorMessage>
#include <QFileInfo>
#include <QProcess>
#include <functional>
namespace chatterino {
namespace {
2018-08-15 22:46:20 +02:00
const char *getBinaryName()
{
#ifdef _WIN32
2018-08-15 22:46:20 +02:00
return "streamlink.exe";
#else
2018-08-15 22:46:20 +02:00
return "streamlink";
#endif
2018-08-15 22:46:20 +02:00
}
2018-08-15 22:46:20 +02:00
const char *getDefaultBinaryPath()
{
#ifdef _WIN32
2018-08-15 22:46:20 +02:00
return "C:\\Program Files (x86)\\Streamlink\\bin\\streamlink.exe";
#else
2018-08-15 22:46:20 +02:00
return "/usr/bin/streamlink";
#endif
}
2018-08-15 22:46:20 +02:00
bool checkStreamlinkPath(const QString &path)
{
QFileInfo fileinfo(path);
2018-08-15 22:46:20 +02:00
if (!fileinfo.exists())
{
return false;
// throw Exception(fS("Streamlink path ({}) is invalid, file does
// not exist", path));
}
2018-08-15 22:46:20 +02:00
return fileinfo.isExecutable();
}
2018-08-15 22:46:20 +02:00
void showStreamlinkNotFoundError()
{
static QErrorMessage *msg = new QErrorMessage;
msg->setWindowTitle("Chatterino - streamlink not found");
2018-08-15 22:46:20 +02:00
if (getSettings()->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
{
2018-08-15 22:46:20 +02:00
msg->showMessage(
"Unable to find Streamlink executable.\nIf you have Streamlink "
"installed, you might need to enable the custom path option");
}
2018-08-15 22:46:20 +02:00
}
QProcess *createStreamlinkProcess()
{
auto *p = new QProcess;
const QString path = []() -> QString {
if (getSettings()->streamlinkUseCustomPath)
{
const QString path = getSettings()->streamlinkPath;
return path.trimmed() + "/" + getBinaryName();
}
return {getBinaryName()};
}();
if (Version::instance().isFlatpak())
{
p->setProgram("flatpak-spawn");
p->setArguments({"--host", path});
}
else
{
p->setProgram(path);
}
2018-08-15 22:46:20 +02:00
QObject::connect(p, &QProcess::errorOccurred, [=](auto err) {
if (err == QProcess::FailedToStart)
{
showStreamlinkNotFoundError();
}
else
{
qCWarning(chatterinoStreamlink) << "Error occurred" << err;
2018-08-15 22:46:20 +02:00
}
2018-08-15 22:46:20 +02:00
p->deleteLater();
});
2018-08-15 22:46:20 +02:00
QObject::connect(
p,
static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(
&QProcess::finished),
[=](int /*exitCode*/, QProcess::ExitStatus /*exitStatus*/) {
p->deleteLater();
2018-08-15 22:46:20 +02:00
});
return p;
}
} // namespace
2018-07-06 19:23:47 +02:00
void getStreamQualities(const QString &channelURL,
std::function<void(QStringList)> cb)
{
auto p = createStreamlinkProcess();
QObject::connect(
p,
static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(
&QProcess::finished),
[=](int exitCode, QProcess::ExitStatus /*exitStatus*/) {
if (exitCode != 0)
{
qCWarning(chatterinoStreamlink) << "Got error code" << exitCode;
// return;
}
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(", ");
2018-08-06 21:17:03 +02:00
for (int i = split.length() - 1; i >= 0; i--)
{
QString option = split.at(i);
if (option == "best)")
{
// As it turns out, sometimes, one quality option can
// be the best and worst quality at the same time.
// Since we start loop from the end, we can check
// that and act accordingly
option = split.at(--i);
// "900p60 (worst"
options << option.left(option.length() - 7);
}
else if (option.endsWith(" (worst)"))
{
options << option.left(option.length() - 8);
}
else if (option.endsWith(" (best)"))
{
options << option.left(option.length() - 7);
2018-08-06 21:17:03 +02:00
}
else
{
options << option;
2018-08-06 21:17:03 +02:00
}
}
cb(options);
}
});
p->setArguments(p->arguments() +
QStringList{channelURL, "--default-stream=KKona"});
p->start();
}
2018-07-06 19:23:47 +02:00
void openStreamlink(const QString &channelURL, const QString &quality,
QStringList extraArguments)
{
auto proc = createStreamlinkProcess();
auto arguments = proc->arguments()
<< extraArguments << channelURL << quality;
// Remove empty arguments before appending additional streamlink options
// as the options might purposely contain empty arguments
arguments.removeAll(QString());
QString additionalOptions = getSettings()->streamlinkOpts.getValue();
arguments << splitCommand(additionalOptions);
proc->setArguments(std::move(arguments));
bool res = proc->startDetached();
if (!res)
{
showStreamlinkNotFoundError();
}
}
2018-07-06 19:23:47 +02:00
void openStreamlinkForChannel(const QString &channel)
{
static const QString INFO_TEMPLATE("Opening %1 in Streamlink ...");
auto *currentPage = dynamic_cast<SplitContainer *>(
getApp()->windows->getMainWindow().getNotebook().getSelectedPage());
if (currentPage != nullptr)
{
if (auto currentSplit = currentPage->getSelectedSplit();
currentSplit != nullptr)
{
currentSplit->getChannel()->addMessage(
makeSystemMessage(INFO_TEMPLATE.arg(channel)));
}
}
QString channelURL = "twitch.tv/" + channel;
QString preferredQuality = getSettings()->preferredQuality.getValue();
preferredQuality = preferredQuality.toLower();
if (preferredQuality == "choose")
{
2018-07-06 19:23:47 +02:00
getStreamQualities(channelURL, [=](QStringList qualityOptions) {
QualityPopup::showDialog(channelURL, 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;
}
2018-07-06 19:23:47 +02:00
openStreamlink(channelURL, quality, args);
}
} // namespace chatterino