12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460 |
- #include "PlayerComponent.h"
- #include <QString>
- #include <Qt>
- #include <QDir>
- #include <QCoreApplication>
- #include <QGuiApplication>
- #include "display/DisplayComponent.h"
- #include "settings/SettingsComponent.h"
- #include "system/SystemComponent.h"
- #include "utils/Utils.h"
- #include "utils/Log.h"
- #include "ComponentManager.h"
- #include "settings/SettingsSection.h"
- #include "PlayerQuickItem.h"
- #include "input/InputComponent.h"
- #include "QsLog.h"
- #include <math.h>
- #include <string.h>
- #include <shared/Paths.h>
- #ifdef TARGET_RPI
- #include <bcm_host.h>
- #include <interface/vmcs_host/vcgencmd.h>
- #endif
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- static void wakeup_cb(void *context)
- {
- PlayerComponent *player = (PlayerComponent *)context;
- emit player->onMpvEvents();
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- PlayerComponent::PlayerComponent(QObject* parent)
- : ComponentBase(parent), m_state(State::finished), m_paused(false), m_playbackActive(false),
- m_windowVisible(false), m_videoPlaybackActive(false), m_inPlayback(false), m_playbackCanceled(false),
- m_bufferingPercentage(100), m_lastBufferingPercentage(-1),
- m_lastPositionUpdate(0.0), m_playbackAudioDelay(0),
- m_window(nullptr), m_mediaFrameRate(0),
- m_restoreDisplayTimer(this), m_reloadAudioTimer(this),
- m_streamSwitchImminent(false), m_doAc3Transcoding(false),
- m_videoRectangle(-1, -1, -1, -1)
- {
- qmlRegisterType<PlayerQuickItem>("Konvergo", 1, 0, "MpvVideo"); // deprecated name
- qmlRegisterType<PlayerQuickItem>("Konvergo", 1, 0, "KonvergoVideo");
- m_restoreDisplayTimer.setSingleShot(true);
- connect(&m_restoreDisplayTimer, &QTimer::timeout, this, &PlayerComponent::onRestoreDisplay);
- connect(&DisplayComponent::Get(), &DisplayComponent::refreshRateChanged, this, &PlayerComponent::onRefreshRateChange);
- m_reloadAudioTimer.setSingleShot(true);
- connect(&m_reloadAudioTimer, &QTimer::timeout, this, &PlayerComponent::updateAudioDevice);
- }
- /////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::componentPostInitialize()
- {
- InputComponent::Get().registerHostCommand("player", this, "userCommand");
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- PlayerComponent::~PlayerComponent()
- {
- if (m_mpv)
- mpv_set_wakeup_callback(m_mpv, nullptr, nullptr);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- bool PlayerComponent::componentInitialize()
- {
- m_mpv = mpv::qt::Handle::FromRawHandle(mpv_create());
- if (!m_mpv)
- throw FatalException(tr("Failed to load mpv."));
- mpv_request_log_messages(m_mpv, "terminal-default");
- mpv::qt::set_property(m_mpv, "msg-level", "all=v");
- mpv::qt::set_property(m_mpv, "config", "yes");
- mpv::qt::set_property(m_mpv, "config-dir", Paths::dataDir());
- mpv_set_wakeup_callback(m_mpv, wakeup_cb, this);
- // Disable native OSD if mpv_command_string() is used.
- mpv::qt::set_property(m_mpv, "osd-level", "0");
- // This forces the player not to rebase playback time to 0 with mkv. We
- // require this, because mkv transcoding lets files start at times other
- // than 0, and web-client expects that we return these times unchanged.
- mpv::qt::set_property(m_mpv, "demuxer-mkv-probe-start-time", false);
- // Upstream mpv sets this to "auto", which disables probing for HLS (at least),
- // in order to speed up playback start. The situation is more complex in PMP
- // due to us wanting to use system codecs, so always enable this.
- mpv::qt::set_property(m_mpv, "demuxer-lavf-probe-info", true);
- // Just discard audio output if no audio device could be opened. This gives
- // us better flexibility how to react to such errors (instead of just
- // aborting playback immediately).
- mpv::qt::set_property(m_mpv, "audio-fallback-to-null", "yes");
- // Do not let the decoder downmix (better customization for us).
- mpv::qt::set_property(m_mpv, "ad-lavc-downmix", false);
- // Make it load the hwdec interop, so hwdec can be enabled at runtime.
- mpv::qt::set_property(m_mpv, "hwdec-preload", "auto");
- // User-visible application name used by some audio APIs (at least PulseAudio).
- mpv::qt::set_property(m_mpv, "audio-client-name", QCoreApplication::applicationName());
- // User-visible stream title used by some audio APIs (at least PulseAudio and wasapi).
- mpv::qt::set_property(m_mpv, "title", QCoreApplication::applicationName());
- // Apply some low-memory settings on RPI, which is relatively memory-constrained.
- #ifdef TARGET_RPI
- // The backbuffer makes seeking back faster (without having to do a HTTP-level seek)
- mpv::qt::set_property(m_mpv, "cache-backbuffer", 10 * 1024); // KB
- // The demuxer queue is used for the readahead, and also for dealing with badly
- // interlaved audio/video. Setting it too low increases sensitivity to network
- // issues, and could cause playback failure with "bad" files.
- mpv::qt::set_property(m_mpv, "demuxer-max-bytes", 50 * 1024 * 1024); // bytes
- // Specifically for enabling mpeg4.
- mpv::qt::set_property(m_mpv, "hwdec-codecs", "all");
- // Do not use exact seeks by default. (This affects the start position in the "loadfile"
- // command in particular. We override the seek mode for normal "seek" commands.)
- mpv::qt::set_property(m_mpv, "hr-seek", "no");
- // Force vo_rpi to fullscreen.
- mpv::qt::set_property(m_mpv, "fullscreen", true);
- #endif
- if (mpv_initialize(m_mpv) < 0)
- throw FatalException(tr("Failed to initialize mpv."));
- mpv_observe_property(m_mpv, 0, "pause", MPV_FORMAT_FLAG);
- mpv_observe_property(m_mpv, 0, "core-idle", MPV_FORMAT_FLAG);
- mpv_observe_property(m_mpv, 0, "cache-buffering-state", MPV_FORMAT_INT64);
- mpv_observe_property(m_mpv, 0, "playback-time", MPV_FORMAT_DOUBLE);
- mpv_observe_property(m_mpv, 0, "vo-configured", MPV_FORMAT_FLAG);
- mpv_observe_property(m_mpv, 0, "duration", MPV_FORMAT_DOUBLE);
- mpv_observe_property(m_mpv, 0, "audio-device-list", MPV_FORMAT_NODE);
- mpv_observe_property(m_mpv, 0, "video-dec-params", MPV_FORMAT_NODE);
- // Setup a hook with the ID 1, which is run during the file is loaded.
- // Used to delay playback start for display framerate switching.
- // (See handler in handleMpvEvent() for details.)
- mpv::qt::command(m_mpv, QStringList() << "hook-add" << "on_load" << "1" << "0");
- // Setup a hook with the ID 2, which is run at a certain stage during loading.
- // We use it to initialize stream selections and to probe the codecs.
- mpv::qt::command(m_mpv, QStringList() << "hook-add" << "on_preloaded" << "2" << "0");
- updateAudioDeviceList();
- setAudioConfiguration();
- updateSubtitleSettings();
- updateVideoSettings();
- connect(SettingsComponent::Get().getSection(SETTINGS_SECTION_VIDEO), &SettingsSection::valuesUpdated,
- this, &PlayerComponent::updateVideoSettings);
- connect(SettingsComponent::Get().getSection(SETTINGS_SECTION_SUBTITLES), &SettingsSection::valuesUpdated,
- this, &PlayerComponent::updateSubtitleSettings);
- connect(SettingsComponent::Get().getSection(SETTINGS_SECTION_AUDIO), &SettingsSection::valuesUpdated,
- this, &PlayerComponent::setAudioConfiguration);
- initializeCodecSupport();
- Codecs::initCodecs();
- QString codecInfo;
- for (auto codec : Codecs::getCachedCodecList())
- {
- if (codec.present)
- {
- if (codecInfo.size())
- codecInfo += " ";
- codecInfo += codec.driver;
- if (codec.type == CodecType::Encoder)
- codecInfo += "(enc)";
- }
- }
- QLOG_INFO() << "Present codecs:" << qPrintable(codecInfo);
- connect(this, &PlayerComponent::onMpvEvents, this, &PlayerComponent::handleMpvEvents, Qt::QueuedConnection);
- emit onMpvEvents();
- return true;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::setVideoRectangle(int x, int y, int w, int h)
- {
- QRect rc(x, y, w, h);
- if (rc != m_videoRectangle)
- {
- m_videoRectangle = rc;
- emit onVideoRecangleChanged();
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::setQtQuickWindow(QQuickWindow* window)
- {
- PlayerQuickItem* video = window->findChild<PlayerQuickItem*>("video");
- if (!video)
- throw FatalException(tr("Failed to load video element."));
- video->initMpv(this);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::setWindow(QQuickWindow* window)
- {
- QString vo = "opengl-cb";
- #ifdef TARGET_RPI
- window->setFlags(Qt::FramelessWindowHint);
- vo = "rpi";
- #endif
- m_window = window;
- if (!window)
- return;
- QString forceVo = SettingsComponent::Get().value(SETTINGS_SECTION_VIDEO, "debug.force_vo").toString();
- if (forceVo.size())
- vo = forceVo;
- mpv::qt::set_property(m_mpv, "vo", vo);
- if (vo == "opengl-cb")
- setQtQuickWindow(window);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- bool PlayerComponent::load(const QString& url, const QVariantMap& options, const QVariantMap &metadata, const QString& audioStream , const QString& subtitleStream)
- {
- stop();
- queueMedia(url, options, metadata, audioStream, subtitleStream);
- return true;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::queueMedia(const QString& url, const QVariantMap& options, const QVariantMap &metadata, const QString& audioStream, const QString& subtitleStream)
- {
- InputComponent::Get().cancelAutoRepeat();
- m_mediaFrameRate = metadata["frameRate"].toFloat(); // returns 0 on failure
- m_serverMediaInfo = metadata["media"].toMap();
- updateVideoSettings();
- QVariantList command;
- command << "loadfile" << url;
- command << "append-play"; // if nothing is playing, play it now, otherwise just enqueue it
- QVariantMap extraArgs;
- quint64 startMilliseconds = options["startMilliseconds"].toLongLong();
- if (startMilliseconds != 0)
- extraArgs.insert("start", "+" + QString::number(startMilliseconds / 1000.0));
- // we're going to select these streams later, in the preloaded hook
- extraArgs.insert("aid", "no");
- extraArgs.insert("sid", "no");
- m_currentSubtitleStream = subtitleStream;
- m_currentAudioStream = audioStream;
- if (metadata["type"] == "music")
- extraArgs.insert("vid", "no");
- extraArgs.insert("pause", options["autoplay"].toBool() ? "no" : "yes");
- QString userAgent = metadata["headers"].toMap()["User-Agent"].toString();
- if (userAgent.size())
- extraArgs.insert("user-agent", userAgent);
- // Make sure the list of requested codecs is reset.
- extraArgs.insert("ad", "");
- extraArgs.insert("vd", "");
- command << extraArgs;
- mpv::qt::command(m_mpv, command);
- }
- /////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::streamSwitch()
- {
- m_streamSwitchImminent = true;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- bool PlayerComponent::switchDisplayFrameRate()
- {
- QLOG_DEBUG() << "Video framerate:" << m_mediaFrameRate << "fps";
- if (!SettingsComponent::Get().value(SETTINGS_SECTION_VIDEO, "refreshrate.auto_switch").toBool())
- {
- QLOG_DEBUG() << "Not switching refresh-rate (disabled by settings).";
- return false;
- }
- bool fs = SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "fullscreen").toBool();
- #if KONVERGO_OPENELEC
- fs = true;
- #endif
- if (!fs)
- {
- QLOG_DEBUG() << "Not switching refresh-rate (not in fullscreen mode).";
- return false;
- }
- if (m_mediaFrameRate < 1)
- {
- QLOG_DEBUG() << "Not switching refresh-rate (no known video framerate).";
- return false;
- }
- // Make sure a timer started by the previous file ending isn't accidentally
- // still in-flight. It could switch the display back after we've switched.
- m_restoreDisplayTimer.stop();
- DisplayComponent* display = &DisplayComponent::Get();
- if (!display->switchToBestVideoMode(m_mediaFrameRate))
- {
- QLOG_DEBUG() << "Switching refresh-rate failed or unnecessary.";
- return false;
- }
- // Make sure settings dependent on the display refresh rate are updated properly.
- updateVideoSettings();
- return true;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::onRestoreDisplay()
- {
- // If the player will in fact start another file (or is playing one), don't restore.
- if (mpv::qt::get_property(m_mpv, "idle-active").toBool())
- DisplayComponent::Get().restorePreviousVideoMode();
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::onRefreshRateChange()
- {
- // Make sure settings dependent on the display refresh rate are updated properly.
- updateVideoSettings();
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::updatePlaybackState()
- {
- State newState = m_state;
- if (m_inPlayback) {
- if (m_paused)
- {
- newState = State::paused;
- }
- else if (m_playbackActive)
- {
- newState = State::playing;
- }
- else
- {
- // Playback not active, but also not buffering means we're in some "other"
- // waiting state. Pretend to web-client that we're buffering.
- if (m_bufferingPercentage == 100)
- m_bufferingPercentage = 0;
- newState = State::buffering;
- }
- }
- else
- {
- if (!m_playbackError.isEmpty())
- newState = State::error;
- else if (m_playbackCanceled)
- newState = State::canceled;
- else
- newState = State::finished;
- }
- if (newState != m_state)
- {
- switch (newState) {
- case State::paused:
- QLOG_INFO() << "Entering state: paused";
- emit paused();
- break;
- case State::playing:
- QLOG_INFO() << "Entering state: playing";
- emit playing();
- break;
- case State::buffering:
- QLOG_INFO() << "Entering state: buffering";
- m_lastBufferingPercentage = -1; /* force update below */
- break;
- case State::finished:
- QLOG_INFO() << "Entering state: finished";
- emit finished();
- emit stopped();
- break;
- case State::canceled:
- QLOG_INFO() << "Entering state: canceled";
- emit canceled();
- emit stopped();
- break;
- case State::error:
- QLOG_INFO() << ("Entering state: error (" + m_playbackError + ")");
- emit error(m_playbackError);
- break;
- }
- emit stateChanged(newState, m_state);
- m_state = newState;
- }
- if (m_state == State::buffering && m_lastBufferingPercentage != m_bufferingPercentage)
- emit buffering(m_bufferingPercentage);
- m_lastBufferingPercentage = m_bufferingPercentage;
- bool is_videoPlaybackActive = m_state == State::playing && m_windowVisible;
- if (m_videoPlaybackActive != is_videoPlaybackActive)
- {
- m_videoPlaybackActive = is_videoPlaybackActive;
- emit videoPlaybackActive(m_videoPlaybackActive);
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::handleMpvEvent(mpv_event *event)
- {
- switch (event->event_id)
- {
- case MPV_EVENT_START_FILE:
- {
- m_inPlayback = true;
- break;
- }
- case MPV_EVENT_END_FILE:
- {
- mpv_event_end_file *endFile = (mpv_event_end_file *)event->data;
- m_inPlayback = false;
- m_playbackCanceled = false;
- m_playbackError = "";
- switch (endFile->reason)
- {
- case MPV_END_FILE_REASON_ERROR:
- {
- m_playbackError = mpv_error_string(endFile->error);
- break;
- }
- case MPV_END_FILE_REASON_STOP:
- {
- m_playbackCanceled = true;
- break;
- }
- }
- if (!m_streamSwitchImminent)
- m_restoreDisplayTimer.start(0);
- m_streamSwitchImminent = false;
- break;
- }
- case MPV_EVENT_PROPERTY_CHANGE:
- {
- mpv_event_property *prop = (mpv_event_property *)event->data;
- if (strcmp(prop->name, "pause") == 0 && prop->format == MPV_FORMAT_FLAG)
- {
- m_paused = !!*(int *)prop->data;
- }
- else if (strcmp(prop->name, "core-idle") == 0 && prop->format == MPV_FORMAT_FLAG)
- {
- m_playbackActive = !*(int *)prop->data;
- }
- else if (strcmp(prop->name, "cache-buffering-state") == 0)
- {
- m_bufferingPercentage = prop->format == MPV_FORMAT_INT64 ? *(int64_t *)prop->data : 100;
- }
- else if (strcmp(prop->name, "playback-time") == 0 && prop->format == MPV_FORMAT_DOUBLE)
- {
- double pos = *(double*)prop->data;
- if (fabs(pos - m_lastPositionUpdate) > 0.015)
- {
- quint64 ms = (quint64)(qMax(pos * 1000.0, 0.0));
- emit positionUpdate(ms);
- m_lastPositionUpdate = pos;
- }
- }
- else if (strcmp(prop->name, "vo-configured") == 0)
- {
- int state = prop->format == MPV_FORMAT_FLAG ? *(int *)prop->data : 0;
- m_windowVisible = state;
- emit windowVisible(m_windowVisible);
- }
- else if (strcmp(prop->name, "duration") == 0)
- {
- if (prop->format == MPV_FORMAT_DOUBLE)
- emit updateDuration(*(double *)prop->data * 1000.0);
- }
- else if (strcmp(prop->name, "audio-device-list") == 0)
- {
- updateAudioDeviceList();
- }
- else if (strcmp(prop->name, "video-dec-params") == 0)
- {
- // Aspect might be known now (or it changed during playback), so update settings
- // dependent on the aspect ratio.
- updateVideoAspectSettings();
- }
- break;
- }
- case MPV_EVENT_LOG_MESSAGE:
- {
- mpv_event_log_message *msg = (mpv_event_log_message *)event->data;
- // Strip the trailing '\n'
- size_t len = strlen(msg->text);
- if (len > 0 && msg->text[len - 1] == '\n')
- len -= 1;
- QString logline = QString::fromUtf8(msg->prefix) + ": " + QString::fromUtf8(msg->text, (int)len);
- if (msg->log_level >= MPV_LOG_LEVEL_V)
- QLOG_DEBUG() << qPrintable(logline);
- else if (msg->log_level >= MPV_LOG_LEVEL_INFO)
- QLOG_INFO() << qPrintable(logline);
- else if (msg->log_level >= MPV_LOG_LEVEL_WARN)
- QLOG_WARN() << qPrintable(logline);
- else
- QLOG_ERROR() << qPrintable(logline);
- break;
- }
- case MPV_EVENT_CLIENT_MESSAGE:
- {
- mpv_event_client_message *msg = (mpv_event_client_message *)event->data;
- if (msg->num_args < 3 || strcmp(msg->args[0], "hook_run") != 0)
- break;
- QString resumeId = QString::fromUtf8(msg->args[2]);
- // Start "on_load" hook.
- // This happens when the player is about to load the file, but no actual loading has taken part yet.
- // We use this to block loading until we explicitly tell it to continue.
- if (!strcmp(msg->args[1], "1"))
- {
- // Calling this lambda will instruct mpv to continue loading the file.
- auto resume = [=] {
- QLOG_INFO() << "checking codecs";
- startCodecsLoading([=] {
- QLOG_INFO() << "resuming loading";
- mpv::qt::command(m_mpv, QStringList() << "hook-ack" << resumeId);
- });
- };
- if (switchDisplayFrameRate())
- {
- // Now wait for some time for mode change - this is needed because mode changing can take some
- // time, during which the screen is black, and initializing hardware decoding could fail due
- // to various strange OS-related reasons.
- // (Better hope the user doesn't try to exit Konvergo during mode change.)
- int pause = SettingsComponent::Get().value(SETTINGS_SECTION_VIDEO, "refreshrate.delay").toInt() * 1000;
- QLOG_INFO() << "waiting" << pause << "msec after rate switch before loading";
- QTimer::singleShot(pause, resume);
- }
- else
- {
- resume();
- }
- break;
- }
- // Start "on_preloaded" hook.
- // Used initialize stream selections and to probe codecs.
- if (!strcmp(msg->args[1], "2"))
- {
- reselectStream(m_currentSubtitleStream, MediaType::Subtitle);
- reselectStream(m_currentAudioStream, MediaType::Audio);
- startCodecsLoading([=] {
- mpv::qt::command(m_mpv, QStringList() << "hook-ack" << resumeId);
- });
- break;
- }
- break;
- }
- default:; /* ignore */
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::handleMpvEvents()
- {
- // Process all events, until the event queue is empty.
- while (1)
- {
- mpv_event *event = mpv_wait_event(m_mpv, 0);
- if (event->event_id == MPV_EVENT_NONE)
- break;
- handleMpvEvent(event);
- }
- // Once we got all status updates, determine the new canonical state.
- updatePlaybackState();
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::setVideoOnlyMode(bool enable)
- {
- if (m_window)
- {
- QQuickItem *web = m_window->findChild<QQuickItem *>("web");
- if (web)
- web->setVisible(!enable);
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::play()
- {
- QStringList args = (QStringList() << "set" << "pause" << "no");
- mpv::qt::command(m_mpv, args);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::stop()
- {
- QStringList args("stop");
- mpv::qt::command(m_mpv, args);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::clearQueue()
- {
- QStringList args("playlist_clear");
- mpv::qt::command(m_mpv, args);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::pause()
- {
- QStringList args = (QStringList() << "set" << "pause" << "yes");
- mpv::qt::command(m_mpv, args);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::seekTo(qint64 ms)
- {
- double timeSecs = ms / 1000.0;
- QVariantList args = (QVariantList() << "seek" << timeSecs << "absolute+exact");
- mpv::qt::command(m_mpv, args);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- QVariant PlayerComponent::getAudioDeviceList()
- {
- return mpv::qt::get_property(m_mpv, "audio-device-list");
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::setAudioDevice(const QString& name)
- {
- mpv::qt::set_property(m_mpv, "audio-device", name);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::setVolume(int volume)
- {
- // Will fail if no audio output opened (i.e. no file playing)
- mpv::qt::set_property(m_mpv, "volume", volume);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- int PlayerComponent::volume()
- {
- QVariant volume = mpv::qt::get_property(m_mpv, "volume");
- if (volume.isValid())
- return volume.toInt();
- return 0;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::setMuted(bool muted)
- {
- // Will fail if no audio output opened (i.e. no file playing)
- mpv::qt::set_property(m_mpv, "mute", muted);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- bool PlayerComponent::muted()
- {
- QVariant mute = mpv::qt::get_property(m_mpv, "mute");
- if (mute.isValid())
- return mute.toBool();
- return false;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- QVariantList PlayerComponent::findStreamsForURL(const QString &url)
- {
- bool isExternal = !url.isEmpty();
- QVariantList res;
- auto tracks = mpv::qt::get_property(m_mpv, "track-list");
- for (auto track : tracks.toList())
- {
- QVariantMap map = track.toMap();
- if (map["external"].toBool() != isExternal)
- continue;
- if (!isExternal || map["external-filename"].toString() == url)
- res += map;
- }
- return res;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::reselectStream(const QString &streamSelection, MediaType target)
- {
- QString streamIdPropertyName;
- QString streamAddCommandName;
- QString mpvStreamTypeName;
- switch (target)
- {
- case MediaType::Subtitle:
- mpvStreamTypeName = "sub";
- streamIdPropertyName = "sid";
- streamAddCommandName = "sub-add";
- break;
- case MediaType::Audio:
- mpvStreamTypeName = "audio";
- streamIdPropertyName = "aid";
- streamAddCommandName = "audio-add";
- break;
- }
- QString streamName;
- QString streamID;
- if (streamSelection.startsWith("#"))
- {
- int splitPos = streamSelection.indexOf(",");
- if (splitPos < 0)
- {
- // Stream from the main file
- streamID = streamSelection.mid(1);
- streamName = "";
- }
- else
- {
- // Stream from an external file
- streamID = streamSelection.mid(1, splitPos - 1);
- streamName = streamSelection.mid(splitPos + 1);
- }
- }
- else if (!streamSelection.isEmpty())
- {
- if (target == MediaType::Audio)
- {
- // For some reason, audio stream selections never start with '#'.
- streamID = streamSelection;
- streamName = "";
- }
- else
- {
- // Legacy web-client single external subtitle
- streamID = "0";
- streamName = streamSelection;
- }
- }
- if (!streamName.isEmpty())
- {
- auto streams = findStreamsForURL(streamName);
- if (streams.isEmpty())
- {
- QStringList args = (QStringList() << streamAddCommandName << streamName);
- mpv::qt::command(m_mpv, args);
- }
- }
- QString selection = "no";
- for (auto stream : findStreamsForURL(streamName))
- {
- auto map = stream.toMap();
- if (map["type"].toString() != mpvStreamTypeName)
- continue;
- if (!streamID.isEmpty() && map["ff-index"].toString() == streamID)
- {
- selection = map["id"].toString();
- break;
- }
- }
- // Fallback to the first stream if none could be found.
- // Useful if web-client uses wrong stream IDs when e.g. transcoding.
- if ((target == MediaType::Audio || !streamID.isEmpty()) && selection == "no")
- selection = "1";
- mpv::qt::set_property(m_mpv, streamIdPropertyName, selection);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::setSubtitleStream(const QString &subtitleStream)
- {
- m_currentSubtitleStream = subtitleStream;
- reselectStream(m_currentSubtitleStream, MediaType::Subtitle);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::setAudioStream(const QString &audioStream)
- {
- m_currentAudioStream = audioStream;
- reselectStream(m_currentAudioStream, MediaType::Audio);
- }
- /////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::setAudioDelay(qint64 milliseconds)
- {
- m_playbackAudioDelay = milliseconds;
- double displayFps = DisplayComponent::Get().currentRefreshRate();
- const char *audioDelaySetting = "audio_delay.normal";
- if (fabs(displayFps - 24) < 0.5) // cover 24Hz, 23.976Hz, and values very close
- audioDelaySetting = "audio_delay.24hz";
- else if (fabs(displayFps - 25) < 0.5)
- audioDelaySetting = "audio_delay.25hz";
- else if (fabs(displayFps - 50) < 0.5)
- audioDelaySetting = "audio_delay.50hz";
- double fixedDelay = SettingsComponent::Get().value(SETTINGS_SECTION_VIDEO, audioDelaySetting).toFloat();
- mpv::qt::set_property(m_mpv, "audio-delay", (fixedDelay + m_playbackAudioDelay) / 1000.0);
- }
- /////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::setSubtitleDelay(qint64 milliseconds)
- {
- mpv::qt::set_property(m_mpv, "sub-delay", milliseconds / 1000.0);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- // This is called with the set of previous audio devices that were detected, and the set of current
- // audio devices. From this we guess whether we should reopen the audio device. If the user-selected
- // device went away previously, and now comes back, reinitializing the player's audio output will
- // force the player and/or the OS to move audio output back to the user-selected device.
- void PlayerComponent::checkCurrentAudioDevice(const QSet<QString>& old_devs, const QSet<QString>& new_devs)
- {
- QString userDevice = SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "device").toString();
- QSet<QString> removed = old_devs - new_devs;
- QSet<QString> added = new_devs - old_devs;
- QLOG_DEBUG() << "Audio devices removed:" << removed;
- QLOG_DEBUG() << "Audio devices added:" << added;
- QLOG_DEBUG() << "Audio device selected:" << userDevice;
- if (userDevice.length())
- {
- if (added.contains(userDevice) || removed.contains(userDevice))
- {
- // The timer is for debouncing the reload. Several change notifications could
- // come in quick succession. Also, it's possible that trying to open the
- // reappeared audio device immediately can fail.
- m_reloadAudioTimer.start(500);
- }
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::updateAudioDeviceList()
- {
- QString userDevice = SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "device").toString();
- bool userDeviceFound = false;
- QVariantList settingList;
- QVariant list = getAudioDeviceList();
- QSet<QString> devices;
- for(const QVariant& d : list.toList())
- {
- Q_ASSERT(d.type() == QVariant::Map);
- QVariantMap dmap = d.toMap();
- QString device = dmap["name"].toString();
- QString description = dmap["description"].toString();
- devices.insert(device);
- if (userDevice == device)
- userDeviceFound = true;
- QVariantMap entry;
- entry["value"] = device;
- entry["title"] = description;
- settingList << entry;
- }
- if (!userDeviceFound)
- {
- QVariantMap entry;
- entry["value"] = userDevice;
- entry["title"] = "[Disconnected device: " + userDevice + "]";
- settingList << entry;
- }
- SettingsComponent::Get().updatePossibleValues(SETTINGS_SECTION_AUDIO, "device", settingList);
- checkCurrentAudioDevice(m_audioDevices, devices);
- m_audioDevices = devices;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::updateAudioDevice()
- {
- QString device = SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "device").toString();
- if (!m_audioDevices.contains(device))
- {
- QLOG_WARN() << "Not using audio device" << device << "because it's not present.";
- device = "auto";
- }
- mpv::qt::set_property(m_mpv, "audio-device", device);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::setAudioConfiguration()
- {
- QString deviceType = SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "devicetype").toString();
- mpv::qt::set_property(m_mpv, "audio-exclusive", SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "exclusive").toBool());
- updateAudioDevice();
- QString resampleOpts = "";
- bool normalize = SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "normalize").toBool();
- resampleOpts += QString(":normalize=") + (normalize ? "yes" : "no");
- // Make downmix more similar to PHT.
- resampleOpts += ":o=[surround_mix_level=1]";
- mpv::qt::set_property(m_mpv, "af-defaults", "lavrresample" + resampleOpts);
- m_passthroughCodecs.clear();
- // passthrough doesn't make sense with basic type
- if (deviceType != AUDIO_DEVICE_TYPE_BASIC)
- {
- SettingsSection* audioSection = SettingsComponent::Get().getSection(SETTINGS_SECTION_AUDIO);
- QStringList codecs;
- if (deviceType == AUDIO_DEVICE_TYPE_SPDIF)
- codecs = AudioCodecsSPDIF();
- else if (deviceType == AUDIO_DEVICE_TYPE_HDMI)
- codecs = AudioCodecsAll();
- for(const QString& key : codecs)
- {
- if (audioSection->value("passthrough." + key).toBool())
- m_passthroughCodecs << key;
- }
- // dts-hd includes dts, but listing dts before dts-hd may disable dts-hd.
- if (m_passthroughCodecs.indexOf("dts-hd") != -1)
- m_passthroughCodecs.removeAll("dts");
- }
- QString passthroughCodecs = m_passthroughCodecs.join(",");
- mpv::qt::set_property(m_mpv, "audio-spdif", passthroughCodecs);
- // set the channel layout
- QVariant layout = SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "channels");
- // always force either stereo or transcoding
- if (deviceType == AUDIO_DEVICE_TYPE_SPDIF)
- layout = "2.0";
- mpv::qt::set_property(m_mpv, "audio-channels", layout);
- // if the user has indicated that PCM only works for stereo, and that
- // the receiver supports AC3, set this extra option that allows us to transcode
- // 5.1 audio into a usable format, note that we only support AC3
- // here for now. We might need to add support for DTS transcoding
- // if we see user requests for it.
- //
- m_doAc3Transcoding = false;
- if (deviceType == AUDIO_DEVICE_TYPE_SPDIF &&
- SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "passthrough.ac3").toBool())
- {
- QString filterArgs = "";
- mpv::qt::command(m_mpv, QStringList() << "af" << "add" << ("@ac3:lavcac3enc" + filterArgs));
- m_doAc3Transcoding = true;
- }
- else
- {
- mpv::qt::command(m_mpv, QStringList() << "af" << "del" << "@ac3");
- }
- QVariant device = SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "device");
- // Make a informational log message.
- QString audioConfig = QString(QString("Audio Config - device: %1, ") +
- "channel layout: %2, " +
- "passthrough codecs: %3, " +
- "ac3 transcoding: %4").arg(device.toString(),
- layout.toString(),
- passthroughCodecs.isEmpty() ? "none" : passthroughCodecs,
- m_doAc3Transcoding ? "yes" : "no");
- QLOG_INFO() << qPrintable(audioConfig);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::updateSubtitleSettings()
- {
- QVariant size = SettingsComponent::Get().value(SETTINGS_SECTION_SUBTITLES, "size");
- mpv::qt::set_property(m_mpv, "sub-text-font-size", size);
- QVariant colorsString = SettingsComponent::Get().value(SETTINGS_SECTION_SUBTITLES, "color");
- auto colors = colorsString.toString().split(",");
- if (colors.length() == 2)
- {
- mpv::qt::set_property(m_mpv, "sub-text-color", colors[0]);
- mpv::qt::set_property(m_mpv, "sub-text-border-color", colors[1]);
- }
- QVariant subposString = SettingsComponent::Get().value(SETTINGS_SECTION_SUBTITLES, "placement");
- auto subpos = subposString.toString().split(",");
- if (subpos.length() == 2)
- {
- mpv::qt::set_property(m_mpv, "sub-text-align-x", subpos[0]);
- mpv::qt::set_property(m_mpv, "sub-text-align-y", subpos[1]);
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::updateVideoAspectSettings()
- {
- QVariant mode = SettingsComponent::Get().value(SETTINGS_SECTION_VIDEO, "aspect").toString();
- bool disableScaling = false;
- bool keepAspect = true;
- QString forceAspect = "-1";
- double panScan = 0.0;
- if (mode == "custom")
- {
- // in particular, do not restore anything - the intention is not to touch the user's mpv.conf settings, or whatever
- return;
- }
- else if (mode == "zoom")
- {
- panScan = 1.0;
- }
- else if (mode == "force_4_3")
- {
- forceAspect = "4:3";
- }
- else if (mode == "force_16_9")
- {
- forceAspect = "16:9";
- }
- else if (mode == "force_16_9_if_4_3")
- {
- auto params = mpv::qt::get_property(m_mpv, "video-dec-params").toMap();
- auto aspect = params["aspect"].toFloat();
- if (fabs(aspect - 4.0/3.0) < 0.1)
- forceAspect = "16:9";
- }
- else if (mode == "stretch")
- {
- keepAspect = false;
- }
- else if (mode == "noscaling")
- {
- disableScaling = true;
- }
- mpv::qt::set_property(m_mpv, "video-unscaled", disableScaling);
- mpv::qt::set_property(m_mpv, "video-aspect", forceAspect);
- mpv::qt::set_property(m_mpv, "keepaspect", keepAspect);
- mpv::qt::set_property(m_mpv, "panscan", panScan);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::updateVideoSettings()
- {
- if (!m_mpv)
- return;
- QVariant syncMode = SettingsComponent::Get().value(SETTINGS_SECTION_VIDEO, "sync_mode");
- mpv::qt::set_property(m_mpv, "video-sync", syncMode);
- QString hardwareDecodingMode = SettingsComponent::Get().value(SETTINGS_SECTION_VIDEO, "hardwareDecoding").toString();
- QString hwdecMode = "no";
- QString hwdecVTFormat = "nv12";
- if (hardwareDecodingMode == "enabled")
- hwdecMode = "auto";
- else if (hardwareDecodingMode == "osx_compat")
- {
- hwdecMode = "auto";
- hwdecVTFormat = "uyvy422";
- }
- else if (hardwareDecodingMode == "copy")
- {
- hwdecMode = "auto-copy";
- }
- mpv::qt::set_property(m_mpv, "hwdec", hwdecMode);
- mpv::qt::set_property(m_mpv, "videotoolbox-format", hwdecVTFormat);
- QVariant deinterlace = SettingsComponent::Get().value(SETTINGS_SECTION_VIDEO, "deinterlace");
- mpv::qt::set_property(m_mpv, "deinterlace", deinterlace.toBool() ? "yes" : "no");
- #ifndef TARGET_RPI
- double displayFps = DisplayComponent::Get().currentRefreshRate();
- mpv::qt::set_property(m_mpv, "display-fps", displayFps);
- #endif
- setAudioDelay(m_playbackAudioDelay);
- QVariant cache = SettingsComponent::Get().value(SETTINGS_SECTION_VIDEO, "cache");
- mpv::qt::set_property(m_mpv, "cache", cache.toInt() * 1024);
- updateVideoAspectSettings();
- }
- /////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::userCommand(QString command)
- {
- QByteArray cmdUtf8 = command.toUtf8();
- mpv_command_string(m_mpv, cmdUtf8.data());
- }
- /////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::initializeCodecSupport()
- {
- QMap<QString, QString> all = { {"vc1", "WVC1"}, {"mpeg2video", "MPG2"} };
- for (auto name : all.keys())
- {
- bool ok = true;
- #ifdef TARGET_RPI
- char res[100] = "";
- bcm_host_init();
- if (vc_gencmd(res, sizeof(res), "codec_enabled %s", all[name].toUtf8().data()))
- res[0] = '\0'; // error
- ok = !!strstr(res, "=enabled");
- #endif
- m_codecSupport[name] = ok;
- QLOG_INFO() << "Codec" << name << (ok ? "present" : "disabled");
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////
- bool PlayerComponent::checkCodecSupport(const QString& codec)
- {
- if (m_codecSupport.contains(codec))
- return m_codecSupport[codec];
- return true; // doesn't matter if unknown codecs are reported as "ok"
- }
- /////////////////////////////////////////////////////////////////////////////////////////
- QList<CodecDriver> convertCodecList(QVariant list, CodecType type)
- {
- QList<CodecDriver> codecs;
- foreach (const QVariant& e, list.toList())
- {
- QVariantMap map = e.toMap();
- QString family = map["family"].toString();
- QString codec = map["codec"].toString();
- QString driver = map["driver"].toString();
- // Only include FFmpeg codecs; exclude pseudo-codecs like spdif.
- if (family != "lavc")
- continue;
- CodecDriver ncodec = {};
- ncodec.type = type;
- ncodec.format = codec;
- ncodec.driver = driver;
- ncodec.present = true;
- codecs.append(ncodec);
- }
- return codecs;
- }
- /////////////////////////////////////////////////////////////////////////////////////////
- QList<CodecDriver> PlayerComponent::installedCodecDrivers()
- {
- QList<CodecDriver> codecs;
- codecs.append(convertCodecList(mpv::qt::get_property(m_mpv, "decoder-list"), CodecType::Decoder));
- codecs.append(convertCodecList(mpv::qt::get_property(m_mpv, "encoder-list"), CodecType::Encoder));
- return codecs;
- }
- /////////////////////////////////////////////////////////////////////////////////////////
- QStringList PlayerComponent::installedDecoderCodecs()
- {
- QStringList formats;
- bool hasPcm = false;
- for (auto driver : installedCodecDrivers())
- {
- if (driver.type == CodecType::Decoder && checkCodecSupport(driver.format))
- {
- QString name = Codecs::plexNameFromFF(driver.format);
- if (name.startsWith("pcm_") && name != "pcm_bluray" && name != "pcm_dvd")
- {
- if (hasPcm)
- continue;
- hasPcm = true;
- name = "pcm";
- }
- formats.append(name);
- }
- }
- return formats;
- }
- /////////////////////////////////////////////////////////////////////////////////////////
- PlaybackInfo PlayerComponent::getPlaybackInfo()
- {
- PlaybackInfo info = {};
- for (auto codec : m_passthroughCodecs)
- {
- // Normalize back to canonical codec names.
- if (codec == "dts-hd")
- codec = "dts";
- info.audioPassthroughCodecs.insert(codec);
- }
- info.enableAC3Transcoding = m_doAc3Transcoding;
- auto tracks = mpv::qt::get_property(m_mpv, "track-list");
- for (auto track : tracks.toList())
- {
- QVariantMap map = track.toMap();
- QString type = map["type"].toString();
- StreamInfo stream = {};
- stream.isVideo = type == "video";
- stream.isAudio = type == "audio";
- stream.codec = map["codec"].toString();
- stream.audioChannels = map["demux-channel-count"].toInt();
- stream.audioSampleRate = map["demux-samplerate"].toInt();
- stream.videoResolution = QSize(map["demux-w"].toInt(), map["demux-h"].toInt());
- // Get the profile from the server, because mpv can't determine it yet.
- if (stream.isVideo)
- {
- int index = map["ff-index"].toInt();
- for (auto partInfo : m_serverMediaInfo["Part"].toList())
- {
- for (auto streamInfo : partInfo.toMap()["Stream"].toList())
- {
- auto streamInfoMap = streamInfo.toMap();
- bool ok = false;
- if (streamInfoMap["index"].toInt(&ok) == index && ok)
- {
- stream.profile = streamInfoMap["profile"].toString();
- QLOG_DEBUG() << "h264profile:" << stream.profile;
- }
- }
- }
- }
- info.streams.append(stream);
- }
- // If we're in an early stage where we don't have streams yet, try to get the
- // info from the PMS metadata.
- if (!info.streams.size())
- {
- for (auto partInfo : m_serverMediaInfo["Part"].toList())
- {
- for (auto streamInfo : partInfo.toMap()["Stream"].toList())
- {
- auto streamInfoMap = streamInfo.toMap();
- StreamInfo stream = {};
- stream.isVideo = streamInfoMap["width"].isValid();
- stream.isAudio = streamInfoMap["channels"].isValid();
- stream.codec = Codecs::plexNameToFF(streamInfoMap["codec"].toString());
- stream.audioChannels = streamInfoMap["channels"].toInt();
- stream.videoResolution = QSize(streamInfoMap["width"].toInt(), streamInfoMap["height"].toInt());
- stream.profile = streamInfoMap["profile"].toString();
- info.streams.append(stream);
- }
- }
- }
- return info;
- }
- /////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::setPreferredCodecs(const QList<CodecDriver>& codecs)
- {
- QStringList items;
- for (auto codec : codecs)
- {
- if (codec.type == CodecType::Decoder)
- {
- items << codec.driver;
- }
- }
- QString opt = items.join(",");
- // For simplicity, we don't distinguish between audio and video. The player
- // will ignore entries with mismatching media type.
- mpv::qt::set_property(m_mpv, "ad", opt);
- mpv::qt::set_property(m_mpv, "vd", opt);
- }
- // For QVariant.
- Q_DECLARE_METATYPE(std::function<void()>);
- /////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::startCodecsLoading(std::function<void()> resume)
- {
- auto fetcher = new CodecsFetcher();
- fetcher->userData = QVariant::fromValue(resume);
- connect(fetcher, &CodecsFetcher::done, this, &PlayerComponent::onCodecsLoadingDone);
- Codecs::updateCachedCodecList();
- QList<CodecDriver> codecs = Codecs::determineRequiredCodecs(getPlaybackInfo());
- setPreferredCodecs(codecs);
- fetcher->installCodecs(codecs);
- }
- /////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::onCodecsLoadingDone(CodecsFetcher* sender)
- {
- sender->deleteLater();
- sender->userData.value<std::function<void()>>()();
- }
- /////////////////////////////////////////////////////////////////////////////////////////
- static QString get_mpv_osd(mpv_handle *ctx, const QString& property)
- {
- char *s = mpv_get_property_osd_string(ctx, property.toUtf8().data());
- if (!s)
- return "-";
- QString r = QString::fromUtf8(s);
- mpv_free(s);
- if (r.size() > 400)
- r = r.mid(0, 400) + "...";
- Log::CensorAuthTokens(r);
- return r;
- }
- #define MPV_PROPERTY(p) get_mpv_osd(m_mpv, p)
- #define MPV_PROPERTY_BOOL(p) (mpv::qt::get_property(m_mpv, p).toBool())
- /////////////////////////////////////////////////////////////////////////////////////////
- void PlayerComponent::appendAudioFormat(QTextStream& info, const QString& property) const
- {
- // Guess if it's a passthrough format. Don't show the channel layout in this
- // case, because it's confusing.
- QString audioFormat = MPV_PROPERTY(property + "/format");
- if (audioFormat.startsWith("spdif-"))
- {
- info << "passthrough (" << audioFormat.mid(6) << ")";
- }
- else
- {
- QString hr = MPV_PROPERTY(property + "/hr-channels");
- QString full = MPV_PROPERTY(property + "/channels");
- info << hr;
- if (hr != full)
- info << " (" << full << ")";
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////
- QString PlayerComponent::videoInformation() const
- {
- QString infoStr;
- QTextStream info(&infoStr);
- // check if video is playing
- if (mpv::qt::get_property(m_mpv, "idle-active").toBool())
- return "";
- info << "File:" << endl;
- info << "URL: " << MPV_PROPERTY("path") << endl;
- info << "Container: " << MPV_PROPERTY("file-format") << endl;
- info << "Native seeking: " << ((MPV_PROPERTY_BOOL("seekable") &&
- !MPV_PROPERTY_BOOL("partially-seekable"))
- ? "yes" : "no") << endl;
- info << endl;
- info << "Video:" << endl;
- info << "Codec: " << MPV_PROPERTY("video-codec") << endl;
- info << "Size: " << MPV_PROPERTY("video-params/dw") << "x"
- << MPV_PROPERTY("video-params/dh") << endl;
- info << "FPS (container): " << MPV_PROPERTY("container-fps") << endl;
- info << "FPS (filters): " << MPV_PROPERTY("estimated-vf-fps") << endl;
- info << "Aspect: " << MPV_PROPERTY("video-aspect") << endl;
- info << "Bitrate: " << MPV_PROPERTY("video-bitrate") << endl;
- double displayFps = DisplayComponent::Get().currentRefreshRate();
- info << "Display FPS: " << MPV_PROPERTY("display-fps")
- << " (" << displayFps << ")" << endl;
- info << "Hardware Decoding: " << MPV_PROPERTY("hwdec-current")
- << " (" << MPV_PROPERTY("hwdec-interop") << ")" << endl;
- info << endl;
- info << "Audio: " << endl;
- info << "Codec: " << MPV_PROPERTY("audio-codec") << endl;
- info << "Bitrate: " << MPV_PROPERTY("audio-bitrate") << endl;
- info << "Channels: ";
- appendAudioFormat(info, "audio-params");
- info << " -> ";
- appendAudioFormat(info, "audio-out-params");
- info << endl;
- info << "Output driver: " << MPV_PROPERTY("current-ao") << endl;
- info << endl;
- info << "Performance: " << endl;
- info << "A/V: " << MPV_PROPERTY("avsync") << endl;
- info << "Dropped frames: " << MPV_PROPERTY("vo-drop-frame-count") << endl;
- bool dispSync = MPV_PROPERTY_BOOL("display-sync-active");
- info << "Display Sync: ";
- if (!dispSync)
- {
- info << "no" << endl;
- }
- else
- {
- info << "yes (ratio " << MPV_PROPERTY("vsync-ratio") << ")" << endl;
- info << "Mistimed frames: " << MPV_PROPERTY("mistimed-frame-count")
- << "/" << MPV_PROPERTY("vo-delayed-frame-count") << endl;
- info << "Measured FPS: " << MPV_PROPERTY("estimated-display-fps")
- << " (" << MPV_PROPERTY("vsync-jitter") << ")" << endl;
- info << "V. speed corr.: " << MPV_PROPERTY("video-speed-correction") << endl;
- info << "A. speed corr.: " << MPV_PROPERTY("audio-speed-correction") << endl;
- }
- info << endl;
- info << "Cache:" << endl;
- info << "Seconds: " << MPV_PROPERTY("demuxer-cache-duration") << endl;
- info << "Extra readahead: " << MPV_PROPERTY("cache-used") << endl;
- info << "Buffering: " << MPV_PROPERTY("cache-buffering-state") << endl;
- info << "Speed: " << MPV_PROPERTY("cache-speed") << endl;
- info << endl;
- info << "Misc: " << endl;
- info << "Time: " << MPV_PROPERTY("playback-time") << " / "
- << MPV_PROPERTY("duration")
- << " (" << MPV_PROPERTY("percent-pos") << "%)" << endl;
- info << "State: " << (MPV_PROPERTY_BOOL("pause") ? "paused " : "")
- << (MPV_PROPERTY_BOOL("paused-for-cache") ? "buffering " : "")
- << (MPV_PROPERTY_BOOL("core-idle") ? "waiting " : "playing ")
- << (MPV_PROPERTY_BOOL("seeking") ? "seeking " : "")
- << endl;
- info << flush;
- return infoStr;
- }
|