PlayerComponent.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. #include "PlayerComponent.h"
  2. #include <QString>
  3. #include <Qt>
  4. #include <QDir>
  5. #include <QCoreApplication>
  6. #include "display/DisplayComponent.h"
  7. #include "settings/SettingsComponent.h"
  8. #include "system/SystemComponent.h"
  9. #include "utils/Utils.h"
  10. #include "ComponentManager.h"
  11. #include "settings/SettingsSection.h"
  12. #include "PlayerQuickItem.h"
  13. #include "input/InputComponent.h"
  14. #include "QsLog.h"
  15. #include <math.h>
  16. #include <string.h>
  17. #include <shared/Paths.h>
  18. #ifdef TARGET_RPI
  19. #include <bcm_host.h>
  20. #include <interface/vmcs_host/vcgencmd.h>
  21. #endif
  22. ///////////////////////////////////////////////////////////////////////////////////////////////////
  23. static void wakeup_cb(void *context)
  24. {
  25. PlayerComponent *player = (PlayerComponent *)context;
  26. emit player->onMpvEvents();
  27. }
  28. ///////////////////////////////////////////////////////////////////////////////////////////////////
  29. PlayerComponent::PlayerComponent(QObject* parent)
  30. : ComponentBase(parent), m_lastPositionUpdate(0.0), m_playbackAudioDelay(0), m_playbackStartSent(false), m_window(nullptr), m_mediaFrameRate(0),
  31. m_restoreDisplayTimer(this), m_reloadAudioTimer(this),
  32. m_streamSwitchImminent(false), m_doAc3Transcoding(false)
  33. {
  34. qmlRegisterType<PlayerQuickItem>("Konvergo", 1, 0, "MpvVideo"); // deprecated name
  35. qmlRegisterType<PlayerQuickItem>("Konvergo", 1, 0, "KonvergoVideo");
  36. m_restoreDisplayTimer.setSingleShot(true);
  37. connect(&m_restoreDisplayTimer, &QTimer::timeout, this, &PlayerComponent::onRestoreDisplay);
  38. connect(&DisplayComponent::Get(), &DisplayComponent::refreshRateChanged, this, &PlayerComponent::onRefreshRateChange);
  39. m_reloadAudioTimer.setSingleShot(true);
  40. connect(&m_reloadAudioTimer, &QTimer::timeout, this, &PlayerComponent::onReloadAudio);
  41. }
  42. /////////////////////////////////////////////////////////////////////////////////////////
  43. void PlayerComponent::componentPostInitialize()
  44. {
  45. InputComponent::Get().registerHostCommand("player", this, "userCommand");
  46. }
  47. ///////////////////////////////////////////////////////////////////////////////////////////////////
  48. PlayerComponent::~PlayerComponent()
  49. {
  50. if (m_mpv)
  51. mpv_set_wakeup_callback(m_mpv, nullptr, nullptr);
  52. }
  53. ///////////////////////////////////////////////////////////////////////////////////////////////////
  54. bool PlayerComponent::componentInitialize()
  55. {
  56. m_mpv = mpv::qt::Handle::FromRawHandle(mpv_create());
  57. if (!m_mpv)
  58. throw FatalException(tr("Failed to load mpv."));
  59. mpv_request_log_messages(m_mpv, "terminal-default");
  60. mpv_set_option_string(m_mpv, "msg-level", "all=v");
  61. // No mouse events
  62. mpv_set_option_string(m_mpv, "input-cursor", "no");
  63. mpv_set_option_string(m_mpv, "cursor-autohide", "no");
  64. mpv_set_option_string(m_mpv, "config", "yes");
  65. mpv_set_option_string(m_mpv, "config-dir", Paths::dataDir().toUtf8().data());
  66. // Disable native OSD if mpv_command_string() is used.
  67. mpv_set_option_string(m_mpv, "osd-level", "0");
  68. // This forces the player not to rebase playback time to 0 with mkv. We
  69. // require this, because mkv transcoding lets files start at times other
  70. // than 0, and web-client expects that we return these times unchanged.
  71. mpv::qt::set_option_variant(m_mpv, "demuxer-mkv-probe-start-time", false);
  72. // Always use the internal mixer by default.
  73. mpv_set_option_string(m_mpv, "softvol", "yes");
  74. // Just discard audio output if no audio device could be opened. This gives
  75. // us better flexibility how to react to such errors (instead of just
  76. // aborting playback immediately).
  77. mpv_set_option_string(m_mpv, "audio-fallback-to-null", "yes");
  78. // Do not let the decoder downmix (better customization for us).
  79. mpv::qt::set_option_variant(m_mpv, "ad-lavc-downmix", false);
  80. // Make it load the hwdec interop, so hwdec can be enabled at runtime.
  81. mpv::qt::set_option_variant(m_mpv, "hwdec-preload", "auto");
  82. // User-visible application name used by some audio APIs (at least PulseAudio).
  83. mpv_set_option_string(m_mpv, "audio-client-name", QCoreApplication::applicationName().toUtf8().data());
  84. // User-visible stream title used by some audio APIs (at least PulseAudio and wasapi).
  85. mpv_set_option_string(m_mpv, "title", QCoreApplication::applicationName().toUtf8().data());
  86. // Apply some low-memory settings on RPI, which is relatively memory-constrained.
  87. #ifdef TARGET_RPI
  88. // The backbuffer makes seeking back faster (without having to do a HTTP-level seek)
  89. mpv::qt::set_option_variant(m_mpv, "cache-backbuffer", 10 * 1024); // KB
  90. // The demuxer queue is used for the readahead, and also for dealing with badly
  91. // interlaved audio/video. Setting it too low increases sensitivity to network
  92. // issues, and could cause playback failure with "bad" files.
  93. mpv::qt::set_option_variant(m_mpv, "demuxer-max-bytes", 50 * 1024 * 1024); // bytes
  94. // Specifically for enabling mpeg4.
  95. mpv::qt::set_option_variant(m_mpv, "hwdec-codecs", "all");
  96. // Do not use exact seeks by default. (This affects the start position in the "loadfile"
  97. // command in particular. We override the seek mode for normal "seek" commands.)
  98. mpv::qt::set_option_variant(m_mpv, "hr-seek", "no");
  99. #endif
  100. mpv_observe_property(m_mpv, 0, "pause", MPV_FORMAT_FLAG);
  101. mpv_observe_property(m_mpv, 0, "core-idle", MPV_FORMAT_FLAG);
  102. mpv_observe_property(m_mpv, 0, "cache-buffering-state", MPV_FORMAT_INT64);
  103. mpv_observe_property(m_mpv, 0, "playback-time", MPV_FORMAT_DOUBLE);
  104. mpv_observe_property(m_mpv, 0, "vo-configured", MPV_FORMAT_FLAG);
  105. mpv_observe_property(m_mpv, 0, "duration", MPV_FORMAT_DOUBLE);
  106. mpv_observe_property(m_mpv, 0, "audio-device-list", MPV_FORMAT_NODE);
  107. connect(this, &PlayerComponent::onMpvEvents, this, &PlayerComponent::handleMpvEvents, Qt::QueuedConnection);
  108. mpv_set_wakeup_callback(m_mpv, wakeup_cb, this);
  109. if (mpv_initialize(m_mpv) < 0)
  110. throw FatalException(tr("Failed to initialize mpv."));
  111. // Setup a hook with the ID 1, which is run during the file is loaded.
  112. // Used to delay playback start for display framerate switching.
  113. // (See handler in handleMpvEvent() for details.)
  114. mpv::qt::command_variant(m_mpv, QStringList() << "hook-add" << "on_load" << "1" << "0");
  115. // Setup a hook with the ID 1, which is run at a certain stage during loading.
  116. // We use it to probe the codecs.
  117. mpv::qt::command_variant(m_mpv, QStringList() << "hook-add" << "on_preloaded" << "2" << "0");
  118. updateAudioDeviceList();
  119. setAudioConfiguration();
  120. updateSubtitleSettings();
  121. updateVideoSettings();
  122. connect(SettingsComponent::Get().getSection(SETTINGS_SECTION_VIDEO), &SettingsSection::valuesUpdated,
  123. this, &PlayerComponent::updateVideoSettings);
  124. connect(SettingsComponent::Get().getSection(SETTINGS_SECTION_SUBTITLES), &SettingsSection::valuesUpdated,
  125. this, &PlayerComponent::updateSubtitleSettings);
  126. connect(SettingsComponent::Get().getSection(SETTINGS_SECTION_AUDIO), &SettingsSection::valuesUpdated,
  127. this, &PlayerComponent::setAudioConfiguration);
  128. initializeCodecSupport();
  129. Codecs::updateCachedCodecList();
  130. QString codecInfo;
  131. for (auto codec : Codecs::getCachecCodecList())
  132. {
  133. if (codec.present)
  134. {
  135. if (codecInfo.size())
  136. codecInfo += " ";
  137. codecInfo += codec.driver;
  138. if (codec.type == CodecType::Encoder)
  139. codecInfo += "(enc)";
  140. }
  141. }
  142. QLOG_INFO() << "Present codecs:" << qPrintable(codecInfo);
  143. return true;
  144. }
  145. ///////////////////////////////////////////////////////////////////////////////////////////////////
  146. void PlayerComponent::setQtQuickWindow(QQuickWindow* window)
  147. {
  148. PlayerQuickItem* video = window->findChild<PlayerQuickItem*>("video");
  149. if (!video)
  150. throw FatalException(tr("Failed to load video element."));
  151. mpv_set_option_string(m_mpv, "vo", "opengl-cb");
  152. video->initMpv(this);
  153. }
  154. ///////////////////////////////////////////////////////////////////////////////////////////////////
  155. void PlayerComponent::setRpiWindow(QQuickWindow* window)
  156. {
  157. window->setFlags(Qt::FramelessWindowHint);
  158. mpv_set_option_string(m_mpv, "vo", "rpi");
  159. }
  160. ///////////////////////////////////////////////////////////////////////////////////////////////////
  161. void PlayerComponent::setWindow(QQuickWindow* window)
  162. {
  163. bool useRpi = false;
  164. #ifdef TARGET_RPI
  165. useRpi = true;
  166. #endif
  167. m_window = window;
  168. if (!window)
  169. return;
  170. QString forceVo = SettingsComponent::Get().value(SETTINGS_SECTION_VIDEO, "debug.force_vo").toString();
  171. if (forceVo.size())
  172. mpv::qt::set_option_variant(m_mpv, "vo", forceVo);
  173. else if (useRpi)
  174. setRpiWindow(window);
  175. else
  176. setQtQuickWindow(window);
  177. }
  178. ///////////////////////////////////////////////////////////////////////////////////////////////////
  179. bool PlayerComponent::load(const QString& url, const QVariantMap& options, const QVariantMap &metadata, const QString& audioStream , const QString& subtitleStream)
  180. {
  181. stop();
  182. queueMedia(url, options, metadata, audioStream, subtitleStream);
  183. return true;
  184. }
  185. ///////////////////////////////////////////////////////////////////////////////////////////////////
  186. void PlayerComponent::queueMedia(const QString& url, const QVariantMap& options, const QVariantMap &metadata, const QString& audioStream, const QString& subtitleStream)
  187. {
  188. m_mediaFrameRate = metadata["frameRate"].toFloat(); // returns 0 on failure
  189. m_serverMediaInfo = metadata["media"].toMap();
  190. updateVideoSettings();
  191. QVariantList command;
  192. command << "loadfile" << url;
  193. command << "append-play"; // if nothing is playing, play it now, otherwise just enqueue it
  194. QVariantMap extraArgs;
  195. quint64 startMilliseconds = options["startMilliseconds"].toLongLong();
  196. if (startMilliseconds != 0)
  197. extraArgs.insert("start", "+" + QString::number(startMilliseconds / 1000.0));
  198. // force default audio selection
  199. extraArgs.insert("ff-aid", "auto");
  200. // by default ignore all subtitles, unless overridden
  201. extraArgs.insert("sid", "no");
  202. extraArgs.insert("ff-sid", "auto");
  203. // detect subtitles
  204. if (!subtitleStream.isEmpty())
  205. {
  206. // If the stream title starts with a #, then it's an index
  207. if (subtitleStream.startsWith("#"))
  208. extraArgs.insert("ff-sid", subtitleStream.mid(1));
  209. else {
  210. extraArgs.insert("sub-file", subtitleStream);
  211. extraArgs.insert("sid", "auto"); // select the external one by default
  212. }
  213. }
  214. if (metadata["type"] == "music")
  215. extraArgs.insert("vid", "no");
  216. // and then the audio stream
  217. if (!audioStream.isEmpty())
  218. extraArgs.insert("ff-aid", audioStream);
  219. extraArgs.insert("pause", options["autoplay"].toBool() ? "no" : "yes");
  220. QString userAgent = metadata["headers"].toMap()["User-Agent"].toString();
  221. if (userAgent.size())
  222. extraArgs.insert("user-agent", userAgent);
  223. // Make sure the list of requested codecs is reset.
  224. extraArgs.insert("ad", "");
  225. extraArgs.insert("vd", "");
  226. command << extraArgs;
  227. QLOG_DEBUG() << command;
  228. mpv::qt::command_variant(m_mpv, command);
  229. }
  230. /////////////////////////////////////////////////////////////////////////////////////////
  231. void PlayerComponent::streamSwitch()
  232. {
  233. m_streamSwitchImminent = true;
  234. }
  235. ///////////////////////////////////////////////////////////////////////////////////////////////////
  236. bool PlayerComponent::switchDisplayFrameRate()
  237. {
  238. QLOG_DEBUG() << "Video framerate:" << m_mediaFrameRate << "fps";
  239. if (!SettingsComponent::Get().value(SETTINGS_SECTION_VIDEO, "refreshrate.auto_switch").toBool())
  240. {
  241. QLOG_DEBUG() << "Not switching refresh-rate (disabled by settings).";
  242. return false;
  243. }
  244. bool fs = SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "fullscreen").toBool();
  245. #if KONVERGO_OPENELEC
  246. fs = true;
  247. #endif
  248. if (!fs)
  249. {
  250. QLOG_DEBUG() << "Not switching refresh-rate (not in fullscreen mode).";
  251. return false;
  252. }
  253. if (m_mediaFrameRate < 1)
  254. {
  255. QLOG_DEBUG() << "Not switching refresh-rate (no known video framerate).";
  256. return false;
  257. }
  258. // Make sure a timer started by the previous file ending isn't accidentally
  259. // still in-flight. It could switch the display back after we've switched.
  260. m_restoreDisplayTimer.stop();
  261. DisplayComponent* display = &DisplayComponent::Get();
  262. if (!display->switchToBestVideoMode(m_mediaFrameRate))
  263. {
  264. QLOG_DEBUG() << "Switching refresh-rate failed or unnecessary.";
  265. return false;
  266. }
  267. // Make sure settings dependent on the display refresh rate are updated properly.
  268. updateVideoSettings();
  269. return true;
  270. }
  271. ///////////////////////////////////////////////////////////////////////////////////////////////////
  272. void PlayerComponent::onRestoreDisplay()
  273. {
  274. // If the player will in fact start another file (or is playing one), don't restore.
  275. if (mpv::qt::get_property_variant(m_mpv, "idle").toBool())
  276. DisplayComponent::Get().restorePreviousVideoMode();
  277. }
  278. ///////////////////////////////////////////////////////////////////////////////////////////////////
  279. void PlayerComponent::onRefreshRateChange()
  280. {
  281. // Make sure settings dependent on the display refresh rate are updated properly.
  282. updateVideoSettings();
  283. }
  284. ///////////////////////////////////////////////////////////////////////////////////////////////////
  285. void PlayerComponent::handleMpvEvent(mpv_event *event)
  286. {
  287. switch (event->event_id)
  288. {
  289. case MPV_EVENT_START_FILE:
  290. {
  291. m_currentUrl = mpv::qt::get_property_variant(m_mpv, "path").toString();
  292. m_playbackStartSent = false;
  293. break;
  294. }
  295. case MPV_EVENT_FILE_LOADED:
  296. {
  297. emit playing(m_currentUrl);
  298. break;
  299. }
  300. case MPV_EVENT_END_FILE:
  301. {
  302. mpv_event_end_file *endFile = (mpv_event_end_file *)event->data;
  303. switch (endFile->reason)
  304. {
  305. case MPV_END_FILE_REASON_EOF:
  306. emit finished(m_currentUrl);
  307. break;
  308. case MPV_END_FILE_REASON_ERROR:
  309. emit error(endFile->error, mpv_error_string(endFile->error));
  310. break;
  311. default:
  312. emit stopped(m_currentUrl);
  313. break;
  314. }
  315. emit playbackEnded(m_currentUrl);
  316. m_currentUrl = "";
  317. if (!m_streamSwitchImminent)
  318. m_restoreDisplayTimer.start(0);
  319. m_streamSwitchImminent = false;
  320. break;
  321. }
  322. case MPV_EVENT_IDLE:
  323. {
  324. emit playbackAllDone();
  325. break;
  326. }
  327. case MPV_EVENT_PLAYBACK_RESTART:
  328. {
  329. // it's also sent after seeks are completed
  330. if (!m_playbackStartSent)
  331. emit playbackStarting();
  332. m_playbackStartSent = true;
  333. break;
  334. }
  335. case MPV_EVENT_PROPERTY_CHANGE:
  336. {
  337. mpv_event_property *prop = (mpv_event_property *)event->data;
  338. if (strcmp(prop->name, "pause") == 0 && prop->format == MPV_FORMAT_FLAG)
  339. {
  340. int state = *(int *)prop->data;
  341. emit paused(state);
  342. }
  343. else if (strcmp(prop->name, "core-idle") == 0 && prop->format == MPV_FORMAT_FLAG)
  344. {
  345. emit playbackActive(!*(int *)prop->data);
  346. }
  347. else if (strcmp(prop->name, "cache-buffering-state") == 0 && prop->format == MPV_FORMAT_INT64)
  348. {
  349. int64_t percentage = *(int64_t *)prop->data;
  350. emit buffering(percentage);
  351. }
  352. else if (strcmp(prop->name, "playback-time") == 0 && prop->format == MPV_FORMAT_DOUBLE)
  353. {
  354. double pos = *(double*)prop->data;
  355. if (fabs(pos - m_lastPositionUpdate) > 0.015)
  356. {
  357. quint64 ms = (quint64)(qMax(pos * 1000.0, 0.0));
  358. emit positionUpdate(ms);
  359. m_lastPositionUpdate = pos;
  360. }
  361. }
  362. else if (strcmp(prop->name, "vo-configured") == 0)
  363. {
  364. int state = prop->format == MPV_FORMAT_FLAG ? *(int *)prop->data : 0;
  365. emit windowVisible(state);
  366. }
  367. else if (strcmp(prop->name, "duration") == 0)
  368. {
  369. if (prop->format == MPV_FORMAT_DOUBLE)
  370. emit updateDuration(*(double *)prop->data * 1000.0);
  371. }
  372. else if (strcmp(prop->name, "audio-device-list") == 0)
  373. {
  374. updateAudioDeviceList();
  375. }
  376. break;
  377. }
  378. case MPV_EVENT_LOG_MESSAGE:
  379. {
  380. mpv_event_log_message *msg = (mpv_event_log_message *)event->data;
  381. // Strip the trailing '\n'
  382. size_t len = strlen(msg->text);
  383. if (len > 0 && msg->text[len - 1] == '\n')
  384. len -= 1;
  385. QString logline = QString::fromUtf8(msg->prefix) + ": " + QString::fromUtf8(msg->text, len);
  386. if (msg->log_level >= MPV_LOG_LEVEL_V)
  387. QLOG_DEBUG() << qPrintable(logline);
  388. else if (msg->log_level >= MPV_LOG_LEVEL_INFO)
  389. QLOG_INFO() << qPrintable(logline);
  390. else if (msg->log_level >= MPV_LOG_LEVEL_WARN)
  391. QLOG_WARN() << qPrintable(logline);
  392. else
  393. QLOG_ERROR() << qPrintable(logline);
  394. break;
  395. }
  396. case MPV_EVENT_CLIENT_MESSAGE:
  397. {
  398. mpv_event_client_message *msg = (mpv_event_client_message *)event->data;
  399. if (msg->num_args < 3 || strcmp(msg->args[0], "hook_run") != 0)
  400. break;
  401. QString resumeId = QString::fromUtf8(msg->args[2]);
  402. // Start "on_load" hook.
  403. // This happens when the player is about to load the file, but no actual loading has taken part yet.
  404. // We use this to block loading until we explicitly tell it to continue.
  405. if (!strcmp(msg->args[1], "1"))
  406. {
  407. // Calling this lambda will instruct mpv to continue loading the file.
  408. auto resume = [=] {
  409. QLOG_INFO() << "checking codecs";
  410. startCodecsLoading([=] {
  411. QLOG_INFO() << "resuming loading";
  412. mpv::qt::command_variant(m_mpv, QStringList() << "hook-ack" << resumeId);
  413. });
  414. };
  415. if (switchDisplayFrameRate())
  416. {
  417. // Now wait for some time for mode change - this is needed because mode changing can take some
  418. // time, during which the screen is black, and initializing hardware decoding could fail due
  419. // to various strange OS-related reasons.
  420. // (Better hope the user doesn't try to exit Konvergo during mode change.)
  421. int pause = SettingsComponent::Get().value(SETTINGS_SECTION_VIDEO, "refreshrate.delay").toInt() * 1000;
  422. QLOG_INFO() << "waiting" << pause << "msec after rate switch before loading";
  423. QTimer::singleShot(pause, resume);
  424. }
  425. else
  426. {
  427. resume();
  428. }
  429. break;
  430. }
  431. // Start "on_preload" hook.
  432. // Used to probe codecs.
  433. if (!strcmp(msg->args[1], "2"))
  434. {
  435. startCodecsLoading([=] {
  436. mpv::qt::command_variant(m_mpv, QStringList() << "hook-ack" << resumeId);
  437. });
  438. break;
  439. }
  440. break;
  441. }
  442. default:; /* ignore */
  443. }
  444. }
  445. ///////////////////////////////////////////////////////////////////////////////////////////////////
  446. void PlayerComponent::handleMpvEvents()
  447. {
  448. // Process all events, until the event queue is empty.
  449. while (1)
  450. {
  451. mpv_event *event = mpv_wait_event(m_mpv, 0);
  452. if (event->event_id == MPV_EVENT_NONE)
  453. break;
  454. handleMpvEvent(event);
  455. }
  456. }
  457. ///////////////////////////////////////////////////////////////////////////////////////////////////
  458. void PlayerComponent::setVideoOnlyMode(bool enable)
  459. {
  460. if (m_window)
  461. {
  462. QQuickItem *web = m_window->findChild<QQuickItem *>("web");
  463. if (web)
  464. web->setVisible(!enable);
  465. }
  466. }
  467. ///////////////////////////////////////////////////////////////////////////////////////////////////
  468. void PlayerComponent::play()
  469. {
  470. QStringList args = (QStringList() << "set" << "pause" << "no");
  471. mpv::qt::command_variant(m_mpv, args);
  472. }
  473. ///////////////////////////////////////////////////////////////////////////////////////////////////
  474. void PlayerComponent::stop()
  475. {
  476. QStringList args("stop");
  477. mpv::qt::command_variant(m_mpv, args);
  478. }
  479. ///////////////////////////////////////////////////////////////////////////////////////////////////
  480. void PlayerComponent::clearQueue()
  481. {
  482. QStringList args("playlist_clear");
  483. mpv::qt::command_variant(m_mpv, args);
  484. }
  485. ///////////////////////////////////////////////////////////////////////////////////////////////////
  486. void PlayerComponent::pause()
  487. {
  488. QStringList args = (QStringList() << "set" << "pause" << "yes");
  489. mpv::qt::command_variant(m_mpv, args);
  490. }
  491. ///////////////////////////////////////////////////////////////////////////////////////////////////
  492. void PlayerComponent::seekTo(qint64 ms)
  493. {
  494. double timeSecs = ms / 1000.0;
  495. QVariantList args = (QVariantList() << "seek" << timeSecs << "absolute+exact");
  496. mpv::qt::command_variant(m_mpv, args);
  497. }
  498. ///////////////////////////////////////////////////////////////////////////////////////////////////
  499. QVariant PlayerComponent::getAudioDeviceList()
  500. {
  501. return mpv::qt::get_property_variant(m_mpv, "audio-device-list");
  502. }
  503. ///////////////////////////////////////////////////////////////////////////////////////////////////
  504. void PlayerComponent::setAudioDevice(const QString& name)
  505. {
  506. mpv::qt::set_property_variant(m_mpv, "audio-device", name);
  507. }
  508. ///////////////////////////////////////////////////////////////////////////////////////////////////
  509. void PlayerComponent::setVolume(int volume)
  510. {
  511. // Will fail if no audio output opened (i.e. no file playing)
  512. mpv::qt::set_property_variant(m_mpv, "volume", volume);
  513. }
  514. ///////////////////////////////////////////////////////////////////////////////////////////////////
  515. int PlayerComponent::volume()
  516. {
  517. QVariant volume = mpv::qt::get_property_variant(m_mpv, "volume");
  518. if (volume.isValid())
  519. return volume.toInt();
  520. return 0;
  521. }
  522. ///////////////////////////////////////////////////////////////////////////////////////////////////
  523. void PlayerComponent::setMuted(bool muted)
  524. {
  525. // Will fail if no audio output opened (i.e. no file playing)
  526. mpv::qt::set_property_variant(m_mpv, "mute", muted);
  527. }
  528. ///////////////////////////////////////////////////////////////////////////////////////////////////
  529. bool PlayerComponent::muted()
  530. {
  531. QVariant mute = mpv::qt::get_property_variant(m_mpv, "mute");
  532. if (mute.isValid())
  533. return mute.toBool();
  534. return false;
  535. }
  536. ///////////////////////////////////////////////////////////////////////////////////////////////////
  537. void PlayerComponent::setSubtitleStream(const QString &subtitleStream)
  538. {
  539. if (subtitleStream.isEmpty())
  540. {
  541. mpv::qt::set_property_variant(m_mpv, "ff-sid", "no");
  542. }
  543. else if (subtitleStream.startsWith("#"))
  544. {
  545. mpv::qt::set_property_variant(m_mpv, "ff-sid", subtitleStream.mid(1));
  546. }
  547. else
  548. {
  549. QStringList args = (QStringList() << "sub-add" << subtitleStream << "cached");
  550. mpv::qt::command_variant(m_mpv, args);
  551. }
  552. }
  553. ///////////////////////////////////////////////////////////////////////////////////////////////////
  554. void PlayerComponent::setAudioStream(const QString &audioStream)
  555. {
  556. if (audioStream.isEmpty())
  557. mpv::qt::set_property_variant(m_mpv, "ff-aid", "no");
  558. else
  559. mpv::qt::set_property_variant(m_mpv, "ff-aid", audioStream);
  560. }
  561. /////////////////////////////////////////////////////////////////////////////////////////
  562. void PlayerComponent::setAudioDelay(qint64 milliseconds)
  563. {
  564. m_playbackAudioDelay = milliseconds;
  565. double displayFps = DisplayComponent::Get().currentRefreshRate();
  566. const char *audioDelaySetting = "audio_delay.normal";
  567. if (fabs(displayFps - 24) < 1) // cover 24Hz, 23.976Hz, and values very close
  568. audioDelaySetting = "audio_delay.24hz";
  569. double fixedDelay = SettingsComponent::Get().value(SETTINGS_SECTION_VIDEO, audioDelaySetting).toFloat();
  570. mpv::qt::set_option_variant(m_mpv, "audio-delay", (fixedDelay + m_playbackAudioDelay) / 1000.0);
  571. }
  572. /////////////////////////////////////////////////////////////////////////////////////////
  573. void PlayerComponent::setSubtitleDelay(qint64 milliseconds)
  574. {
  575. mpv::qt::set_option_variant(m_mpv, "sub-delay", milliseconds / 1000.0);
  576. }
  577. ///////////////////////////////////////////////////////////////////////////////////////////////////
  578. void PlayerComponent::onReloadAudio()
  579. {
  580. mpv::qt::command_variant(m_mpv, QStringList() << "ao-reload");
  581. }
  582. ///////////////////////////////////////////////////////////////////////////////////////////////////
  583. // This is called with the set of previous audio devices that were detected, and the set of current
  584. // audio devices. From this we guess whether we should reopen the audio device. If the user-selected
  585. // device went away previously, and now comes back, reinitializing the player's audio output will
  586. // force the player and/or the OS to move audio output back to the user-selected device.
  587. void PlayerComponent::checkCurrentAudioDevice(const QSet<QString>& old_devs, const QSet<QString>& new_devs)
  588. {
  589. QString userDevice = SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "device").toString();
  590. QSet<QString> removed = old_devs - new_devs;
  591. QSet<QString> added = new_devs - old_devs;
  592. QLOG_DEBUG() << "Audio devices removed:" << removed;
  593. QLOG_DEBUG() << "Audio devices added:" << added;
  594. QLOG_DEBUG() << "Audio device selected:" << userDevice;
  595. if (!mpv::qt::get_property_variant(m_mpv, "idle").toBool() && userDevice.length())
  596. {
  597. if (added.contains(userDevice))
  598. {
  599. // The timer is for debouncing the reload. Several change notifications could
  600. // come in quick succession. Also, it's possible that trying to open the
  601. // reappeared audio device immediately can fail.
  602. m_reloadAudioTimer.start(500);
  603. }
  604. }
  605. }
  606. ///////////////////////////////////////////////////////////////////////////////////////////////////
  607. void PlayerComponent::updateAudioDeviceList()
  608. {
  609. QVariantList settingList;
  610. QVariant list = getAudioDeviceList();
  611. QSet<QString> devices;
  612. for(const QVariant& d : list.toList())
  613. {
  614. Q_ASSERT(d.type() == QVariant::Map);
  615. QVariantMap dmap = d.toMap();
  616. devices.insert(dmap["name"].toString());
  617. QVariantMap entry;
  618. entry["value"] = dmap["name"];
  619. entry["title"] = dmap["description"];
  620. settingList << entry;
  621. }
  622. SettingsComponent::Get().updatePossibleValues(SETTINGS_SECTION_AUDIO, "device", settingList);
  623. checkCurrentAudioDevice(m_audioDevices, devices);
  624. m_audioDevices = devices;
  625. }
  626. ///////////////////////////////////////////////////////////////////////////////////////////////////
  627. void PlayerComponent::setAudioConfiguration()
  628. {
  629. QStringList aoDefaults;
  630. QString deviceType = SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "devicetype").toString();
  631. if (SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "exclusive").toBool())
  632. {
  633. aoDefaults << "wasapi:exclusive=yes";
  634. aoDefaults << "coreaudio:exclusive=yes";
  635. }
  636. mpv::qt::set_option_variant(m_mpv, "ao-defaults", aoDefaults.join(','));
  637. // set the audio device
  638. QVariant device = SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "device");
  639. mpv::qt::set_property_variant(m_mpv, "audio-device", device);
  640. QString resampleOpts = "";
  641. bool normalize = SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "normalize").toBool();
  642. resampleOpts += QString(":normalize=") + (normalize ? "yes" : "no");
  643. // Make downmix more similar to PHT.
  644. resampleOpts += ":o=[surround_mix_level=1]";
  645. mpv::qt::set_option_variant(m_mpv, "af-defaults", "lavrresample" + resampleOpts);
  646. m_passthroughCodecs.clear();
  647. // passthrough doesn't make sense with basic type
  648. if (deviceType != AUDIO_DEVICE_TYPE_BASIC)
  649. {
  650. SettingsSection* audioSection = SettingsComponent::Get().getSection(SETTINGS_SECTION_AUDIO);
  651. QStringList codecs;
  652. if (deviceType == AUDIO_DEVICE_TYPE_SPDIF)
  653. codecs = AudioCodecsSPDIF();
  654. else if (deviceType == AUDIO_DEVICE_TYPE_HDMI && audioSection->value("advanced").toBool())
  655. codecs = AudioCodecsAll();
  656. for(const QString& key : codecs)
  657. {
  658. if (audioSection->value("passthrough." + key).toBool())
  659. m_passthroughCodecs << key;
  660. }
  661. // dts-hd includes dts, but listing dts before dts-hd may disable dts-hd.
  662. if (m_passthroughCodecs.indexOf("dts-hd") != -1)
  663. m_passthroughCodecs.removeAll("dts");
  664. }
  665. QString passthroughCodecs = m_passthroughCodecs.join(",");
  666. mpv::qt::set_option_variant(m_mpv, "audio-spdif", passthroughCodecs);
  667. // set the channel layout
  668. QVariant layout = SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "channels");
  669. // always force either stereo or transcoding
  670. if (deviceType == AUDIO_DEVICE_TYPE_SPDIF)
  671. layout = "2.0";
  672. mpv::qt::set_option_variant(m_mpv, "audio-channels", layout);
  673. // if the user has indicated that PCM only works for stereo, and that
  674. // the receiver supports AC3, set this extra option that allows us to transcode
  675. // 5.1 audio into a usable format, note that we only support AC3
  676. // here for now. We might need to add support for DTS transcoding
  677. // if we see user requests for it.
  678. //
  679. m_doAc3Transcoding = false;
  680. if (layout == "2.0" &&
  681. SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "passthrough.ac3").toBool())
  682. {
  683. mpv::qt::command_variant(m_mpv, QStringList() << "af" << "add" << "@ac3:lavcac3enc");
  684. m_doAc3Transcoding = true;
  685. }
  686. else
  687. {
  688. mpv::qt::command_variant(m_mpv, QStringList() << "af" << "del" << "@ac3");
  689. }
  690. // Make a informational log message.
  691. QString audioConfig = QString(QString("Audio Config - device: %1, ") +
  692. "channel layout: %2, " +
  693. "passthrough codecs: %3, " +
  694. "ac3 transcoding: %4").arg(device.toString(),
  695. layout.toString(),
  696. passthroughCodecs.isEmpty() ? "none" : passthroughCodecs,
  697. m_doAc3Transcoding ? "yes" : "no");
  698. QLOG_INFO() << qPrintable(audioConfig);
  699. }
  700. ///////////////////////////////////////////////////////////////////////////////////////////////////
  701. void PlayerComponent::updateSubtitleSettings()
  702. {
  703. QVariant size = SettingsComponent::Get().value(SETTINGS_SECTION_SUBTITLES, "size");
  704. mpv::qt::set_option_variant(m_mpv, "sub-text-font-size", size);
  705. QVariant colorsString = SettingsComponent::Get().value(SETTINGS_SECTION_SUBTITLES, "color");
  706. auto colors = colorsString.toString().split(",");
  707. if (colors.length() == 2)
  708. {
  709. mpv::qt::set_option_variant(m_mpv, "sub-text-color", colors[0]);
  710. mpv::qt::set_option_variant(m_mpv, "sub-text-border-color", colors[1]);
  711. }
  712. QVariant subposString = SettingsComponent::Get().value(SETTINGS_SECTION_SUBTITLES, "placement");
  713. auto subpos = subposString.toString().split(",");
  714. if (subpos.length() == 2)
  715. {
  716. mpv::qt::set_option_variant(m_mpv, "sub-text-align-x", subpos[0]);
  717. mpv::qt::set_option_variant(m_mpv, "sub-text-align-y", subpos[1]);
  718. }
  719. }
  720. ///////////////////////////////////////////////////////////////////////////////////////////////////
  721. void PlayerComponent::updateVideoAspectSettings()
  722. {
  723. QVariant mode = SettingsComponent::Get().value(SETTINGS_SECTION_VIDEO, "aspect").toString();
  724. bool disableScaling = false;
  725. bool keepAspect = true;
  726. QString forceAspect = "-1";
  727. double panScan = 0.0;
  728. if (mode == "custom")
  729. {
  730. // in particular, do not restore anything - the intention is not to touch the user's mpv.conf settings, or whatever
  731. return;
  732. }
  733. else if (mode == "zoom")
  734. {
  735. panScan = 1.0;
  736. }
  737. else if (mode == "force_4_3")
  738. {
  739. forceAspect = "4:3";
  740. }
  741. else if (mode == "force_16_9")
  742. {
  743. forceAspect = "16:9";
  744. }
  745. else if (mode == "stretch")
  746. {
  747. keepAspect = false;
  748. }
  749. else if (mode == "noscaling")
  750. {
  751. disableScaling = true;
  752. }
  753. mpv::qt::set_property_variant(m_mpv, "video-unscaled", disableScaling);
  754. mpv::qt::set_property_variant(m_mpv, "video-aspect", forceAspect);
  755. mpv::qt::set_option_variant(m_mpv, "keepaspect", keepAspect);
  756. mpv::qt::set_property_variant(m_mpv, "panscan", panScan);
  757. }
  758. ///////////////////////////////////////////////////////////////////////////////////////////////////
  759. void PlayerComponent::updateVideoSettings()
  760. {
  761. QVariant syncMode = SettingsComponent::Get().value(SETTINGS_SECTION_VIDEO, "sync_mode");
  762. mpv::qt::set_option_variant(m_mpv, "video-sync", syncMode);
  763. QString hardwareDecodingMode = SettingsComponent::Get().value(SETTINGS_SECTION_VIDEO, "hardwareDecoding").toString();
  764. bool hwdecEnabled = false;
  765. QString hwdecVTFormat = "nv12";
  766. if (hardwareDecodingMode == "enabled") {
  767. hwdecEnabled = true;
  768. } else if (hardwareDecodingMode == "osx_compat") {
  769. hwdecEnabled = true;
  770. hwdecVTFormat = "uyvy422";
  771. }
  772. mpv::qt::set_property_variant(m_mpv, "hwdec", hwdecEnabled);
  773. mpv::qt::set_option_variant(m_mpv, "videotoolbox-format", hwdecVTFormat);
  774. QVariant deinterlace = SettingsComponent::Get().value(SETTINGS_SECTION_VIDEO, "deinterlace");
  775. mpv::qt::set_option_variant(m_mpv, "deinterlace", deinterlace.toBool() ? "yes" : "no");
  776. #ifndef TARGET_RPI
  777. double displayFps = DisplayComponent::Get().currentRefreshRate();
  778. mpv::qt::set_property_variant(m_mpv, "display-fps", displayFps);
  779. #endif
  780. setAudioDelay(m_playbackAudioDelay);
  781. QVariant cache = SettingsComponent::Get().value(SETTINGS_SECTION_VIDEO, "cache");
  782. mpv::qt::set_option_variant(m_mpv, "cache", cache.toInt() * 1024);
  783. updateVideoAspectSettings();
  784. }
  785. /////////////////////////////////////////////////////////////////////////////////////////
  786. void PlayerComponent::userCommand(QString command)
  787. {
  788. QByteArray cmdUtf8 = command.toUtf8();
  789. mpv_command_string(m_mpv, cmdUtf8.data());
  790. }
  791. /////////////////////////////////////////////////////////////////////////////////////////
  792. void PlayerComponent::initializeCodecSupport()
  793. {
  794. QMap<QString, QString> all = { {"vc1", "WVC1"}, {"mpeg2video", "MPG2"} };
  795. for (auto name : all.keys())
  796. {
  797. bool ok = true;
  798. #ifdef TARGET_RPI
  799. char res[100] = "";
  800. bcm_host_init();
  801. if (vc_gencmd(res, sizeof(res), "codec_enabled %s", all[name].toUtf8().data()))
  802. res[0] = '\0'; // error
  803. ok = !!strstr(res, "=enabled");
  804. #endif
  805. m_codecSupport[name] = ok;
  806. QLOG_INFO() << "Codec" << name << (ok ? "present" : "disabled");
  807. }
  808. }
  809. /////////////////////////////////////////////////////////////////////////////////////////
  810. bool PlayerComponent::checkCodecSupport(const QString& codec)
  811. {
  812. if (m_codecSupport.contains(codec))
  813. return m_codecSupport[codec];
  814. return true; // doesn't matter if unknown codecs are reported as "ok"
  815. }
  816. /////////////////////////////////////////////////////////////////////////////////////////
  817. QList<CodecDriver> convertCodecList(QVariant list, CodecType type)
  818. {
  819. QList<CodecDriver> codecs;
  820. foreach (const QVariant& e, list.toList())
  821. {
  822. QVariantMap map = e.toMap();
  823. QString family = map["family"].toString();
  824. QString codec = map["codec"].toString();
  825. QString driver = map["driver"].toString();
  826. // Only include FFmpeg codecs; exclude pseudo-codecs like spdif.
  827. if (family != "lavc")
  828. continue;
  829. CodecDriver ncodec = {};
  830. ncodec.type = type;
  831. ncodec.format = codec;
  832. ncodec.driver = driver;
  833. ncodec.present = true;
  834. codecs.append(ncodec);
  835. }
  836. return codecs;
  837. }
  838. /////////////////////////////////////////////////////////////////////////////////////////
  839. QList<CodecDriver> PlayerComponent::installedCodecs()
  840. {
  841. QList<CodecDriver> codecs;
  842. codecs.append(convertCodecList(mpv::qt::get_property_variant(m_mpv, "decoder-list"), CodecType::Decoder));
  843. codecs.append(convertCodecList(mpv::qt::get_property_variant(m_mpv, "encoder-list"), CodecType::Encoder));
  844. return codecs;
  845. }
  846. /////////////////////////////////////////////////////////////////////////////////////////
  847. PlaybackInfo PlayerComponent::getPlaybackInfo()
  848. {
  849. PlaybackInfo info = {};
  850. for (auto codec : m_passthroughCodecs)
  851. {
  852. // Normalize back to canonical codec names.
  853. if (codec == "dts-hd")
  854. codec = "dts";
  855. info.audioPassthroughCodecs.insert(codec);
  856. }
  857. info.enableAC3Transcoding = m_doAc3Transcoding;
  858. auto tracks = mpv::qt::get_property_variant(m_mpv, "track-list");
  859. for (auto track : tracks.toList())
  860. {
  861. QVariantMap map = track.toMap();
  862. QString type = map["type"].toString();
  863. StreamInfo stream = {};
  864. stream.isVideo = type == "video";
  865. stream.isAudio = type == "audio";
  866. stream.codec = map["codec"].toString();
  867. stream.audioChannels = map["demux-channel-count"].toInt();
  868. stream.videoResolution = QSize(map["demux-w"].toInt(), map["demux-h"].toInt());
  869. if (stream.isVideo)
  870. {
  871. // Assume there's only 1 video stream. We get the profile from the
  872. // server because mpv can't be bothered to determine it.
  873. stream.profile = m_serverMediaInfo["videoProfile"].toString();
  874. }
  875. info.streams.append(stream);
  876. }
  877. // If we're in an early stage where we don't have streams yet, try to get the
  878. // info from the PMS metadata.
  879. if (!info.streams.size())
  880. {
  881. for (auto partInfo : m_serverMediaInfo["Part"].toList())
  882. {
  883. for (auto streamInfo : partInfo.toMap()["Stream"].toList())
  884. {
  885. auto streamInfoMap = streamInfo.toMap();
  886. StreamInfo stream = {};
  887. stream.isVideo = streamInfoMap["width"].isValid();
  888. stream.isAudio = streamInfoMap["channels"].isValid();
  889. stream.codec = Codecs::plexNameToFF(streamInfoMap["codec"].toString());
  890. stream.audioChannels = streamInfoMap["channels"].toInt();
  891. stream.videoResolution = QSize(streamInfoMap["width"].toInt(), streamInfoMap["height"].toInt());
  892. stream.profile = streamInfoMap["profile"].toString();
  893. info.streams.append(stream);
  894. }
  895. }
  896. }
  897. return info;
  898. }
  899. /////////////////////////////////////////////////////////////////////////////////////////
  900. void PlayerComponent::setPreferredCodecs(const QList<CodecDriver>& codecs)
  901. {
  902. QStringList items;
  903. for (auto codec : codecs)
  904. {
  905. if (codec.type == CodecType::Decoder)
  906. {
  907. items << QString("lavc:") + codec.driver;
  908. }
  909. }
  910. QString opt = items.join(",");
  911. // For simplicity, we don't distinguish between audio and video. The player
  912. // will ignore entries with mismatching media type.
  913. mpv::qt::set_option_variant(m_mpv, "ad", opt);
  914. mpv::qt::set_option_variant(m_mpv, "vd", opt);
  915. }
  916. // For QVariant.
  917. Q_DECLARE_METATYPE(std::function<void()>);
  918. /////////////////////////////////////////////////////////////////////////////////////////
  919. void PlayerComponent::startCodecsLoading(std::function<void()> resume)
  920. {
  921. auto fetcher = new CodecsFetcher();
  922. fetcher->userData = QVariant::fromValue(resume);
  923. connect(fetcher, &CodecsFetcher::done, this, &PlayerComponent::onCodecsLoadingDone);
  924. Codecs::updateCachedCodecList();
  925. QList<CodecDriver> codecs = Codecs::determineRequiredCodecs(getPlaybackInfo());
  926. setPreferredCodecs(codecs);
  927. fetcher->installCodecs(codecs);
  928. }
  929. /////////////////////////////////////////////////////////////////////////////////////////
  930. void PlayerComponent::onCodecsLoadingDone(CodecsFetcher* sender)
  931. {
  932. sender->deleteLater();
  933. sender->userData.value<std::function<void()>>()();
  934. }
  935. /////////////////////////////////////////////////////////////////////////////////////////
  936. static QString get_mpv_osd(mpv_handle *ctx, const QString& property)
  937. {
  938. char *s = mpv_get_property_osd_string(ctx, property.toUtf8().data());
  939. if (!s)
  940. return "-";
  941. QString r = QString::fromUtf8(s);
  942. mpv_free(s);
  943. return r;
  944. }
  945. #define MPV_PROPERTY(p) get_mpv_osd(m_mpv, p)
  946. #define MPV_PROPERTY_BOOL(p) (mpv::qt::get_property_variant(m_mpv, p).toBool())
  947. /////////////////////////////////////////////////////////////////////////////////////////
  948. void PlayerComponent::appendAudioFormat(QTextStream& info, const QString& property) const
  949. {
  950. // Guess if it's a passthrough format. Don't show the channel layout in this
  951. // case, because it's confusing.
  952. QString audioFormat = MPV_PROPERTY(property + "/format");
  953. if (audioFormat.startsWith("spdif-"))
  954. {
  955. info << "passthrough (" << audioFormat.mid(6) << ")";
  956. }
  957. else
  958. {
  959. QString hr = MPV_PROPERTY(property + "/hr-channels");
  960. QString full = MPV_PROPERTY(property + "/channels");
  961. info << hr;
  962. if (hr != full)
  963. info << " (" << full << ")";
  964. }
  965. }
  966. /////////////////////////////////////////////////////////////////////////////////////////
  967. QString PlayerComponent::videoInformation() const
  968. {
  969. QString infoStr;
  970. QTextStream info(&infoStr);
  971. // check if video is playing
  972. if (mpv::qt::get_property_variant(m_mpv, "idle").toBool())
  973. return "";
  974. info << "File:" << endl;
  975. info << "URL: " << MPV_PROPERTY("path") << endl;
  976. info << "Container: " << MPV_PROPERTY("file-format") << endl;
  977. info << "Native seeking: " << ((MPV_PROPERTY_BOOL("seekable") &&
  978. !MPV_PROPERTY_BOOL("partially-seekable"))
  979. ? "yes" : "no") << endl;
  980. info << endl;
  981. info << "Video:" << endl;
  982. info << "Codec: " << MPV_PROPERTY("video-codec") << endl;
  983. info << "Size: " << MPV_PROPERTY("video-params/dw") << "x"
  984. << MPV_PROPERTY("video-params/dh") << endl;
  985. info << "FPS (container): " << MPV_PROPERTY("fps") << endl;
  986. info << "FPS (filters): " << MPV_PROPERTY("estimated-vf-fps") << endl;
  987. info << "Aspect: " << MPV_PROPERTY("video-aspect") << endl;
  988. info << "Bitrate: " << MPV_PROPERTY("video-bitrate") << endl;
  989. double displayFps = DisplayComponent::Get().currentRefreshRate();
  990. info << "Display FPS: " << MPV_PROPERTY("display-fps")
  991. << " (" << displayFps << ")" << endl;
  992. info << "Hardware Decoding: " << MPV_PROPERTY("hwdec-active")
  993. << " (" << MPV_PROPERTY("hwdec-detected") << ")" << endl;
  994. info << endl;
  995. info << "Audio: " << endl;
  996. info << "Codec: " << MPV_PROPERTY("audio-codec") << endl;
  997. info << "Bitrate: " << MPV_PROPERTY("audio-bitrate") << endl;
  998. info << "Channels (input): ";
  999. appendAudioFormat(info, "audio-params");
  1000. info << endl;
  1001. info << "Channels (output): ";
  1002. appendAudioFormat(info, "audio-out-params");
  1003. info << endl;
  1004. info << endl;
  1005. info << "Performance: " << endl;
  1006. info << "A/V: " << MPV_PROPERTY("avsync") << endl;
  1007. info << "Dropped frames: " << MPV_PROPERTY("vo-drop-frame-count") << endl;
  1008. bool dispSync = MPV_PROPERTY_BOOL("display-sync-active");
  1009. info << "Display Sync: ";
  1010. if (!dispSync)
  1011. {
  1012. info << "no" << endl;
  1013. }
  1014. else
  1015. {
  1016. info << "yes (ratio " << MPV_PROPERTY("vsync-ratio") << ")" << endl;
  1017. info << "Mistimed frames: " << MPV_PROPERTY("mistimed-frame-count")
  1018. << "/" << MPV_PROPERTY("vo-delayed-frame-count") << endl;
  1019. info << "Measured FPS: " << MPV_PROPERTY("estimated-display-fps")
  1020. << " (" << MPV_PROPERTY("vsync-jitter") << ")" << endl;
  1021. info << "V. speed corr.: " << MPV_PROPERTY("video-speed-correction") << endl;
  1022. info << "A. speed corr.: " << MPV_PROPERTY("audio-speed-correction") << endl;
  1023. }
  1024. info << endl;
  1025. info << "Cache:" << endl;
  1026. info << "Seconds: " << MPV_PROPERTY("demuxer-cache-duration") << endl;
  1027. info << "Extra readahead: " << MPV_PROPERTY("cache-used") << endl;
  1028. info << "Buffering: " << MPV_PROPERTY("cache-buffering-state") << endl;
  1029. info << "Speed: " << MPV_PROPERTY("cache-speed") << endl;
  1030. info << endl;
  1031. info << "Misc: " << endl;
  1032. info << "Time: " << MPV_PROPERTY("playback-time") << " / "
  1033. << MPV_PROPERTY("duration")
  1034. << " (" << MPV_PROPERTY("percent-pos") << "%)" << endl;
  1035. info << "State: " << (MPV_PROPERTY_BOOL("pause") ? "paused " : "")
  1036. << (MPV_PROPERTY_BOOL("paused-for-cache") ? "buffering " : "")
  1037. << (MPV_PROPERTY_BOOL("core-idle") ? "waiting " : "playing ")
  1038. << (MPV_PROPERTY_BOOL("seeking") ? "seeking " : "")
  1039. << endl;
  1040. info << flush;
  1041. return infoStr;
  1042. }