[videodb] remove unused seasons table from episode_view
[xbmc.git] / xbmc / cores / VideoPlayer / VideoPlayer.cpp
blob1deb7d2ba7dd518aa46eac3374b99a063b4491c8
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "VideoPlayer.h"
11 #include "DVDCodecs/DVDCodecUtils.h"
12 #include "DVDDemuxers/DVDDemux.h"
13 #include "DVDDemuxers/DVDDemuxCC.h"
14 #include "DVDDemuxers/DVDDemuxFFmpeg.h"
15 #include "DVDDemuxers/DVDDemuxUtils.h"
16 #include "DVDDemuxers/DVDDemuxVobsub.h"
17 #include "DVDDemuxers/DVDFactoryDemuxer.h"
18 #include "DVDInputStreams/DVDFactoryInputStream.h"
19 #include "DVDInputStreams/DVDInputStream.h"
20 #include "network/NetworkFileItemClassify.h"
21 #if defined(HAVE_LIBBLURAY)
22 #include "DVDInputStreams/DVDInputStreamBluray.h"
23 #endif
24 #include "DVDInputStreams/DVDInputStreamNavigator.h"
25 #include "DVDInputStreams/InputStreamPVRBase.h"
26 #include "DVDMessage.h"
27 #include "FileItem.h"
28 #include "GUIUserMessages.h"
29 #include "LangInfo.h"
30 #include "ServiceBroker.h"
31 #include "URL.h"
32 #include "Util.h"
33 #include "VideoPlayerAudio.h"
34 #include "VideoPlayerRadioRDS.h"
35 #include "VideoPlayerVideo.h"
36 #include "application/Application.h"
37 #include "cores/DataCacheCore.h"
38 #include "cores/EdlEdit.h"
39 #include "cores/FFmpeg.h"
40 #include "cores/VideoPlayer/Process/ProcessInfo.h"
41 #include "cores/VideoPlayer/VideoRenderers/RenderManager.h"
42 #include "dialogs/GUIDialogKaiToast.h"
43 #include "guilib/GUIComponent.h"
44 #include "guilib/GUIWindowManager.h"
45 #include "guilib/LocalizeStrings.h"
46 #include "guilib/StereoscopicsManager.h"
47 #include "input/actions/Action.h"
48 #include "input/actions/ActionIDs.h"
49 #include "messaging/ApplicationMessenger.h"
50 #include "network/NetworkFileItemClassify.h"
51 #include "settings/AdvancedSettings.h"
52 #include "settings/Settings.h"
53 #include "settings/SettingsComponent.h"
54 #include "threads/SingleLock.h"
55 #include "utils/FontUtils.h"
56 #include "utils/JobManager.h"
57 #include "utils/LangCodeExpander.h"
58 #include "utils/StreamDetails.h"
59 #include "utils/StreamUtils.h"
60 #include "utils/StringUtils.h"
61 #include "utils/URIUtils.h"
62 #include "utils/Variant.h"
63 #include "utils/log.h"
64 #include "video/Bookmark.h"
65 #include "video/VideoInfoTag.h"
66 #include "windowing/WinSystem.h"
68 #include <chrono>
69 #include <iterator>
70 #include <memory>
71 #include <mutex>
72 #include <utility>
74 using namespace KODI;
75 using namespace std::chrono_literals;
77 //------------------------------------------------------------------------------
78 // selection streams
79 //------------------------------------------------------------------------------
81 #define PREDICATE_RETURN(lh, rh) \
82 do { \
83 if((lh) != (rh)) \
84 return (lh) > (rh); \
85 } while(0)
87 class PredicateSubtitleFilter
89 private:
90 std::string audiolang;
91 bool original;
92 bool nosub;
93 bool onlyforced;
94 int currentSubStream;
95 public:
96 /** \brief The class' operator() decides if the given (subtitle) SelectionStream is relevant wrt.
97 * preferred subtitle language and audio language. If the subtitle is relevant <B>false</B> false is returned.
99 * A subtitle is relevant if
100 * - it was previously selected, or
101 * - it's an external sub, or
102 * - it's a forced sub and "original stream's language" was selected and audio stream language matches, or
103 * - it's a default and a forced sub (could lead to users seeing forced subs in a foreign language!), or
104 * - its language matches the preferred subtitle's language (unequal to "original stream's language")
106 explicit PredicateSubtitleFilter(const std::string& lang, int subStream)
107 : audiolang(lang),
108 currentSubStream(subStream)
110 const std::string subtitleLang = CServiceBroker::GetSettingsComponent()->GetSettings()->GetString(CSettings::SETTING_LOCALE_SUBTITLELANGUAGE);
111 original = StringUtils::EqualsNoCase(subtitleLang, "original");
112 nosub = StringUtils::EqualsNoCase(subtitleLang, "none");
113 onlyforced = StringUtils::EqualsNoCase(subtitleLang, "forced_only");
116 bool operator()(const SelectionStream& ss) const
118 if (ss.type_index == currentSubStream)
119 return false;
121 if (nosub)
122 return true;
124 if (onlyforced)
126 if ((ss.flags & StreamFlags::FLAG_FORCED) && g_LangCodeExpander.CompareISO639Codes(ss.language, audiolang))
127 return false;
128 else
129 return true;
132 if(STREAM_SOURCE_MASK(ss.source) == STREAM_SOURCE_DEMUX_SUB || STREAM_SOURCE_MASK(ss.source) == STREAM_SOURCE_TEXT)
133 return false;
135 if ((ss.flags & StreamFlags::FLAG_FORCED) && g_LangCodeExpander.CompareISO639Codes(ss.language, audiolang))
136 return false;
138 if ((ss.flags & StreamFlags::FLAG_FORCED) && (ss.flags & StreamFlags::FLAG_DEFAULT))
139 return false;
141 if (ss.language == "cc" && ss.flags & StreamFlags::FLAG_HEARING_IMPAIRED)
142 return false;
144 if(!original)
146 std::string subtitle_language = g_langInfo.GetSubtitleLanguage();
147 if (g_LangCodeExpander.CompareISO639Codes(subtitle_language, ss.language))
148 return false;
150 else if (ss.flags & StreamFlags::FLAG_DEFAULT)
151 return false;
153 return true;
157 class PredicateAudioFilter
159 private:
160 int currentAudioStream;
161 bool preferStereo;
162 public:
163 explicit PredicateAudioFilter(int audioStream, bool preferStereo)
164 : currentAudioStream(audioStream)
165 , preferStereo(preferStereo)
168 bool operator()(const SelectionStream& lh, const SelectionStream& rh)
170 PREDICATE_RETURN(lh.type_index == currentAudioStream
171 , rh.type_index == currentAudioStream);
173 const std::shared_ptr<CSettings> settings = CServiceBroker::GetSettingsComponent()->GetSettings();
175 if (!StringUtils::EqualsNoCase(settings->GetString(CSettings::SETTING_LOCALE_AUDIOLANGUAGE), "mediadefault"))
177 if (!StringUtils::EqualsNoCase(settings->GetString(CSettings::SETTING_LOCALE_AUDIOLANGUAGE), "original"))
179 std::string audio_language = g_langInfo.GetAudioLanguage();
180 PREDICATE_RETURN(g_LangCodeExpander.CompareISO639Codes(audio_language, lh.language)
181 , g_LangCodeExpander.CompareISO639Codes(audio_language, rh.language));
183 else
185 PREDICATE_RETURN(lh.flags & StreamFlags::FLAG_ORIGINAL,
186 rh.flags & StreamFlags::FLAG_ORIGINAL);
189 bool hearingimp = settings->GetBool(CSettings::SETTING_ACCESSIBILITY_AUDIOHEARING);
190 PREDICATE_RETURN(!hearingimp ? !(lh.flags & StreamFlags::FLAG_HEARING_IMPAIRED) : lh.flags & StreamFlags::FLAG_HEARING_IMPAIRED
191 , !hearingimp ? !(rh.flags & StreamFlags::FLAG_HEARING_IMPAIRED) : rh.flags & StreamFlags::FLAG_HEARING_IMPAIRED);
193 bool visualimp = settings->GetBool(CSettings::SETTING_ACCESSIBILITY_AUDIOVISUAL);
194 PREDICATE_RETURN(!visualimp ? !(lh.flags & StreamFlags::FLAG_VISUAL_IMPAIRED) : lh.flags & StreamFlags::FLAG_VISUAL_IMPAIRED
195 , !visualimp ? !(rh.flags & StreamFlags::FLAG_VISUAL_IMPAIRED) : rh.flags & StreamFlags::FLAG_VISUAL_IMPAIRED);
198 if (settings->GetBool(CSettings::SETTING_VIDEOPLAYER_PREFERDEFAULTFLAG))
200 PREDICATE_RETURN(lh.flags & StreamFlags::FLAG_DEFAULT,
201 rh.flags & StreamFlags::FLAG_DEFAULT);
204 if (preferStereo)
205 PREDICATE_RETURN(lh.channels == 2,
206 rh.channels == 2);
207 else
208 PREDICATE_RETURN(lh.channels,
209 rh.channels);
211 PREDICATE_RETURN(StreamUtils::GetCodecPriority(lh.codec),
212 StreamUtils::GetCodecPriority(rh.codec));
214 PREDICATE_RETURN(lh.flags & StreamFlags::FLAG_DEFAULT,
215 rh.flags & StreamFlags::FLAG_DEFAULT);
216 return false;
220 /** \brief The class' operator() decides if the given (subtitle) SelectionStream lh is 'better than' the given (subtitle) SelectionStream rh.
221 * If lh is 'better than' rh the return value is true, false otherwise.
223 * A subtitle lh is 'better than' a subtitle rh (in evaluation order) if
224 * - lh was previously selected, or
225 * - lh is an external sub and rh not, or
226 * - lh is a forced sub and ("original stream's language" was selected or subtitles are off) and audio stream language matches sub language and rh not, or
227 * - lh is a default sub and ("original stream's language" was selected or subtitles are off) and audio stream language matches sub language and rh not, or
228 * - lh is a sub where audio stream language matches sub language and (original stream's language" was selected or subtitles are off) and rh not, or
229 * - lh is a forced sub and a default sub ("original stream's language" was selected or subtitles are off)
230 * - lh is an external sub and its language matches the preferred subtitle's language (unequal to "original stream's language") and rh not, or
231 * - lh is language matches the preferred subtitle's language (unequal to "original stream's language") and rh not, or
232 * - lh is a default sub and rh not
234 class PredicateSubtitlePriority
236 private:
237 std::string audiolang;
238 bool original;
239 bool subson;
240 PredicateSubtitleFilter filter;
241 int subStream;
242 public:
243 explicit PredicateSubtitlePriority(const std::string& lang, int stream, bool ison)
244 : audiolang(lang),
245 original(StringUtils::EqualsNoCase(CServiceBroker::GetSettingsComponent()->GetSettings()->GetString(CSettings::SETTING_LOCALE_SUBTITLELANGUAGE), "original")),
246 subson(ison),
247 filter(lang, stream),
248 subStream(stream)
252 bool relevant(const SelectionStream& ss) const
254 return !filter(ss);
257 bool operator()(const SelectionStream& lh, const SelectionStream& rh) const
259 PREDICATE_RETURN(relevant(lh)
260 , relevant(rh));
262 PREDICATE_RETURN(lh.type_index == subStream
263 , rh.type_index == subStream);
265 // prefer external subs
266 PREDICATE_RETURN(STREAM_SOURCE_MASK(lh.source) == STREAM_SOURCE_DEMUX_SUB || STREAM_SOURCE_MASK(lh.source) == STREAM_SOURCE_TEXT
267 , STREAM_SOURCE_MASK(rh.source) == STREAM_SOURCE_DEMUX_SUB || STREAM_SOURCE_MASK(rh.source) == STREAM_SOURCE_TEXT);
269 if (!subson || original)
271 PREDICATE_RETURN(lh.flags & StreamFlags::FLAG_FORCED && g_LangCodeExpander.CompareISO639Codes(lh.language, audiolang)
272 , rh.flags & StreamFlags::FLAG_FORCED && g_LangCodeExpander.CompareISO639Codes(rh.language, audiolang));
274 PREDICATE_RETURN(lh.flags & StreamFlags::FLAG_DEFAULT && g_LangCodeExpander.CompareISO639Codes(lh.language, audiolang)
275 , rh.flags & StreamFlags::FLAG_DEFAULT && g_LangCodeExpander.CompareISO639Codes(rh.language, audiolang));
277 PREDICATE_RETURN(g_LangCodeExpander.CompareISO639Codes(lh.language, audiolang)
278 , g_LangCodeExpander.CompareISO639Codes(rh.language, audiolang));
280 PREDICATE_RETURN((lh.flags & (StreamFlags::FLAG_FORCED | StreamFlags::FLAG_DEFAULT)) == (StreamFlags::FLAG_FORCED | StreamFlags::FLAG_DEFAULT)
281 , (rh.flags & (StreamFlags::FLAG_FORCED | StreamFlags::FLAG_DEFAULT)) == (StreamFlags::FLAG_FORCED | StreamFlags::FLAG_DEFAULT));
285 std::string subtitle_language = g_langInfo.GetSubtitleLanguage();
286 if (!original)
288 PREDICATE_RETURN((STREAM_SOURCE_MASK(lh.source) == STREAM_SOURCE_DEMUX_SUB || STREAM_SOURCE_MASK(lh.source) == STREAM_SOURCE_TEXT) && g_LangCodeExpander.CompareISO639Codes(subtitle_language, lh.language)
289 , (STREAM_SOURCE_MASK(rh.source) == STREAM_SOURCE_DEMUX_SUB || STREAM_SOURCE_MASK(rh.source) == STREAM_SOURCE_TEXT) && g_LangCodeExpander.CompareISO639Codes(subtitle_language, rh.language));
292 if (!original)
294 PREDICATE_RETURN(g_LangCodeExpander.CompareISO639Codes(subtitle_language, lh.language)
295 , g_LangCodeExpander.CompareISO639Codes(subtitle_language, rh.language));
297 bool hearingimp = CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(CSettings::SETTING_ACCESSIBILITY_SUBHEARING);
298 PREDICATE_RETURN(!hearingimp ? !(lh.flags & StreamFlags::FLAG_HEARING_IMPAIRED) : lh.flags & StreamFlags::FLAG_HEARING_IMPAIRED
299 , !hearingimp ? !(rh.flags & StreamFlags::FLAG_HEARING_IMPAIRED) : rh.flags & StreamFlags::FLAG_HEARING_IMPAIRED);
302 PREDICATE_RETURN(lh.flags & StreamFlags::FLAG_DEFAULT
303 , rh.flags & StreamFlags::FLAG_DEFAULT);
305 return false;
309 class PredicateVideoFilter
311 private:
312 int currentVideoStream;
313 public:
314 explicit PredicateVideoFilter(int videoStream) : currentVideoStream(videoStream)
317 bool operator()(const SelectionStream& lh, const SelectionStream& rh)
319 PREDICATE_RETURN(lh.type_index == currentVideoStream,
320 rh.type_index == currentVideoStream);
322 PREDICATE_RETURN(lh.flags & StreamFlags::FLAG_DEFAULT,
323 rh.flags & StreamFlags::FLAG_DEFAULT);
324 return false;
328 void CSelectionStreams::Clear(StreamType type, StreamSource source)
330 auto new_end = std::remove_if(m_Streams.begin(), m_Streams.end(),
331 [type, source](const SelectionStream &stream)
333 return (type == STREAM_NONE || stream.type == type) &&
334 (source == 0 || stream.source == source);
336 m_Streams.erase(new_end, m_Streams.end());
339 SelectionStream& CSelectionStreams::Get(StreamType type, int index)
341 return const_cast<SelectionStream&>(std::as_const(*this).Get(type, index));
344 const SelectionStream& CSelectionStreams::Get(StreamType type, int index) const
346 int count = -1;
347 for (size_t i = 0; i < m_Streams.size(); ++i)
349 if (m_Streams[i].type != type)
350 continue;
351 count++;
352 if (count == index)
353 return m_Streams[i];
355 return m_invalid;
358 std::vector<SelectionStream> CSelectionStreams::Get(StreamType type)
360 std::vector<SelectionStream> streams;
361 std::copy_if(m_Streams.begin(), m_Streams.end(), std::back_inserter(streams),
362 [type](const SelectionStream &stream)
364 return stream.type == type;
366 return streams;
369 bool CSelectionStreams::Get(StreamType type, StreamFlags flag, SelectionStream& out)
371 for(size_t i=0;i<m_Streams.size();i++)
373 if(m_Streams[i].type != type)
374 continue;
375 if((m_Streams[i].flags & flag) != flag)
376 continue;
377 out = m_Streams[i];
378 return true;
380 return false;
383 int CSelectionStreams::TypeIndexOf(StreamType type, int source, int64_t demuxerId, int id) const
385 if (id < 0)
386 return -1;
388 auto it = std::find_if(m_Streams.begin(), m_Streams.end(),
389 [&](const SelectionStream& stream) {return stream.type == type
390 && stream.source == source && stream.id == id
391 && stream.demuxerId == demuxerId;});
393 if (it != m_Streams.end())
394 return it->type_index;
395 else
396 return -1;
399 int CSelectionStreams::Source(StreamSource source, const std::string& filename)
401 int index = source - 1;
402 for (size_t i=0; i<m_Streams.size(); i++)
404 SelectionStream &s = m_Streams[i];
405 if (STREAM_SOURCE_MASK(s.source) != source)
406 continue;
407 // if it already exists, return same
408 if (s.filename == filename)
409 return s.source;
410 if (index < s.source)
411 index = s.source;
413 // return next index
414 return index + 1;
417 void CSelectionStreams::Update(SelectionStream& s)
419 int index = TypeIndexOf(s.type, s.source, s.demuxerId, s.id);
420 if(index >= 0)
422 SelectionStream& o = Get(s.type, index);
423 s.type_index = o.type_index;
424 o = s;
426 else
428 s.type_index = CountType(s.type);
429 m_Streams.push_back(s);
433 void CSelectionStreams::Update(const std::shared_ptr<CDVDInputStream>& input,
434 CDVDDemux* demuxer,
435 const std::string& filename2)
437 if(input && input->IsStreamType(DVDSTREAM_TYPE_DVD))
439 std::shared_ptr<CDVDInputStreamNavigator> nav = std::static_pointer_cast<CDVDInputStreamNavigator>(input);
440 std::string filename = nav->GetFileName();
441 int source = Source(STREAM_SOURCE_NAV, filename);
443 int count;
444 count = nav->GetAudioStreamCount();
445 for(int i=0;i<count;i++)
447 SelectionStream s;
448 s.source = source;
449 s.type = STREAM_AUDIO;
450 s.id = i;
451 s.flags = StreamFlags::FLAG_NONE;
452 s.filename = filename;
454 AudioStreamInfo info = nav->GetAudioStreamInfo(i);
455 s.name = info.name;
456 s.codec = info.codecName;
457 s.language = g_LangCodeExpander.ConvertToISO6392B(info.language);
458 s.channels = info.channels;
459 s.flags = info.flags;
460 Update(s);
463 count = nav->GetSubTitleStreamCount();
464 for(int i=0;i<count;i++)
466 SelectionStream s;
467 s.source = source;
468 s.type = STREAM_SUBTITLE;
469 s.id = i;
470 s.filename = filename;
471 s.channels = 0;
473 SubtitleStreamInfo info = nav->GetSubtitleStreamInfo(i);
474 s.name = info.name;
475 s.flags = info.flags;
476 s.language = g_LangCodeExpander.ConvertToISO6392B(info.language);
477 Update(s);
480 VideoStreamInfo info = nav->GetVideoStreamInfo();
481 for (int i = 1; i <= info.angles; i++)
483 SelectionStream s;
484 s.source = source;
485 s.type = STREAM_VIDEO;
486 s.id = i;
487 s.flags = StreamFlags::FLAG_NONE;
488 s.filename = filename;
489 s.channels = 0;
490 s.aspect_ratio = info.videoAspectRatio;
491 s.width = info.width;
492 s.height = info.height;
493 s.codec = info.codecName;
494 s.name = StringUtils::Format("{} {}", g_localizeStrings.Get(38032), i);
495 Update(s);
498 else if(demuxer)
500 std::string filename = demuxer->GetFileName();
501 int source;
502 if(input) /* hack to know this is sub decoder */
503 source = Source(STREAM_SOURCE_DEMUX, filename);
504 else if (!filename2.empty())
505 source = Source(STREAM_SOURCE_DEMUX_SUB, filename);
506 else
507 source = Source(STREAM_SOURCE_VIDEOMUX, filename);
509 for (auto stream : demuxer->GetStreams())
511 /* skip streams with no type */
512 if (stream->type == STREAM_NONE)
513 continue;
514 /* make sure stream is marked with right source */
515 stream->source = source;
517 SelectionStream s;
518 s.source = source;
519 s.type = stream->type;
520 s.id = stream->uniqueId;
521 s.demuxerId = stream->demuxerId;
522 s.language = g_LangCodeExpander.ConvertToISO6392B(stream->language);
523 s.flags = stream->flags;
524 s.filename = demuxer->GetFileName();
525 s.filename2 = filename2;
526 s.name = stream->GetStreamName();
527 s.codec = demuxer->GetStreamCodecName(stream->demuxerId, stream->uniqueId);
528 s.channels = 0; // Default to 0. Overwrite if STREAM_AUDIO below.
529 if(stream->type == STREAM_VIDEO)
531 CDemuxStreamVideo* vstream = static_cast<CDemuxStreamVideo*>(stream);
532 s.width = vstream->iWidth;
533 s.height = vstream->iHeight;
534 s.aspect_ratio = vstream->fAspect;
535 s.stereo_mode = vstream->stereo_mode;
536 s.bitrate = vstream->iBitRate;
537 s.hdrType = vstream->hdr_type;
539 if(stream->type == STREAM_AUDIO)
541 std::string type;
542 type = static_cast<CDemuxStreamAudio*>(stream)->GetStreamType();
543 if(type.length() > 0)
545 if(s.name.length() > 0)
546 s.name += " - ";
547 s.name += type;
549 s.channels = static_cast<CDemuxStreamAudio*>(stream)->iChannels;
550 s.bitrate = static_cast<CDemuxStreamAudio*>(stream)->iBitRate;
552 Update(s);
555 CServiceBroker::GetDataCacheCore().SignalAudioInfoChange();
556 CServiceBroker::GetDataCacheCore().SignalVideoInfoChange();
557 CServiceBroker::GetDataCacheCore().SignalSubtitleInfoChange();
560 void CSelectionStreams::Update(const std::shared_ptr<CDVDInputStream>& input, CDVDDemux* demuxer)
562 Update(input, demuxer, "");
565 int CSelectionStreams::CountTypeOfSource(StreamType type, StreamSource source) const
567 return std::count_if(m_Streams.begin(), m_Streams.end(),
568 [&](const SelectionStream& stream) {return (stream.type == type) && (stream.source == source);});
571 int CSelectionStreams::CountType(StreamType type) const
573 return std::count_if(m_Streams.begin(), m_Streams.end(),
574 [&](const SelectionStream& stream) { return stream.type == type; });
577 //------------------------------------------------------------------------------
578 // main class
579 //------------------------------------------------------------------------------
581 void CVideoPlayer::CreatePlayers()
583 if (m_players_created)
584 return;
586 m_VideoPlayerVideo =
587 new CVideoPlayerVideo(&m_clock, &m_overlayContainer, m_messenger, m_renderManager,
588 *m_processInfo, m_messageQueueTimeSize);
589 m_VideoPlayerAudio =
590 new CVideoPlayerAudio(&m_clock, m_messenger, *m_processInfo, m_messageQueueTimeSize);
591 m_VideoPlayerSubtitle = new CVideoPlayerSubtitle(&m_overlayContainer, *m_processInfo);
592 m_VideoPlayerTeletext = new CDVDTeletextData(*m_processInfo);
593 m_VideoPlayerRadioRDS = new CDVDRadioRDSData(*m_processInfo);
594 m_VideoPlayerAudioID3 = std::make_unique<CVideoPlayerAudioID3>(*m_processInfo);
595 m_players_created = true;
598 void CVideoPlayer::DestroyPlayers()
600 if (!m_players_created)
601 return;
603 delete m_VideoPlayerVideo;
604 delete m_VideoPlayerAudio;
605 delete m_VideoPlayerSubtitle;
606 delete m_VideoPlayerTeletext;
607 delete m_VideoPlayerRadioRDS;
608 m_VideoPlayerAudioID3.reset();
610 m_players_created = false;
613 CVideoPlayer::CVideoPlayer(IPlayerCallback& callback)
614 : IPlayer(callback),
615 CThread("VideoPlayer"),
616 m_CurrentAudio(STREAM_AUDIO, VideoPlayer_AUDIO),
617 m_CurrentVideo(STREAM_VIDEO, VideoPlayer_VIDEO),
618 m_CurrentSubtitle(STREAM_SUBTITLE, VideoPlayer_SUBTITLE),
619 m_CurrentTeletext(STREAM_TELETEXT, VideoPlayer_TELETEXT),
620 m_CurrentRadioRDS(STREAM_RADIO_RDS, VideoPlayer_RDS),
621 m_CurrentAudioID3(STREAM_AUDIO_ID3, VideoPlayer_ID3),
622 m_messenger("player"),
623 m_outboundEvents(std::make_unique<CJobQueue>(false, 1, CJob::PRIORITY_NORMAL)),
624 m_pInputStream(nullptr),
625 m_pDemuxer(nullptr),
626 m_pSubtitleDemuxer(nullptr),
627 m_pCCDemuxer(nullptr),
628 m_renderManager(m_clock, this)
630 m_players_created = false;
632 m_dvd.Clear();
633 m_State.Clear();
635 m_bAbortRequest = false;
636 m_offset_pts = 0.0;
637 m_playSpeed = DVD_PLAYSPEED_NORMAL;
638 m_streamPlayerSpeed = DVD_PLAYSPEED_NORMAL;
639 m_caching = CACHESTATE_DONE;
640 m_HasVideo = false;
641 m_HasAudio = false;
642 m_UpdateStreamDetails = false;
644 const int tenthsSeconds = CServiceBroker::GetSettingsComponent()->GetSettings()->GetInt(
645 CSettings::SETTING_VIDEOPLAYER_QUEUETIMESIZE);
647 m_messageQueueTimeSize = static_cast<double>(tenthsSeconds) / 10.0;
649 m_SkipCommercials = true;
651 m_processInfo.reset(CProcessInfo::CreateInstance());
652 // if we have a gui, register the cache
653 m_processInfo->SetDataCache(&CServiceBroker::GetDataCacheCore());
654 m_processInfo->SetSpeed(1.0);
655 m_processInfo->SetTempo(1.0);
656 m_processInfo->SetFrameAdvance(false);
658 CreatePlayers();
660 m_displayLost = false;
661 m_error = false;
662 m_bCloseRequest = false;
663 CServiceBroker::GetWinSystem()->Register(this);
666 CVideoPlayer::~CVideoPlayer()
668 CServiceBroker::GetWinSystem()->Unregister(this);
670 CloseFile();
671 DestroyPlayers();
673 while (m_outboundEvents->IsProcessing())
675 CThread::Sleep(10ms);
679 bool CVideoPlayer::OpenFile(const CFileItem& file, const CPlayerOptions &options)
681 CLog::Log(LOGINFO, "VideoPlayer::OpenFile: {}", CURL::GetRedacted(file.GetPath()));
683 if (IsRunning())
685 CDVDMsgOpenFile::FileParams params;
686 params.m_item = file;
687 params.m_options = options;
688 params.m_item.SetMimeTypeForInternetFile();
689 m_messenger.Put(std::make_shared<CDVDMsgOpenFile>(params), 1);
691 return true;
694 m_item = file;
695 m_playerOptions = options;
697 m_processInfo->SetPlayTimes(0,0,0,0);
698 m_bAbortRequest = false;
699 m_error = false;
700 m_bCloseRequest = false;
701 m_renderManager.PreInit();
703 Create();
704 m_messenger.Init();
706 m_callback.OnPlayBackStarted(m_item);
708 return true;
711 bool CVideoPlayer::CloseFile(bool reopen)
713 CLog::Log(LOGINFO, "CVideoPlayer::CloseFile()");
715 // set the abort request so that other threads can finish up
716 m_bAbortRequest = true;
717 m_bCloseRequest = true;
719 // tell demuxer to abort
720 if(m_pDemuxer)
721 m_pDemuxer->Abort();
723 if(m_pSubtitleDemuxer)
724 m_pSubtitleDemuxer->Abort();
726 if(m_pInputStream)
727 m_pInputStream->Abort();
729 m_renderManager.UnInit();
731 CLog::Log(LOGINFO, "VideoPlayer: waiting for threads to exit");
733 // wait for the main thread to finish up
734 // since this main thread cleans up all other resources and threads
735 // we are done after the StopThread call
737 CSingleExit exitlock(CServiceBroker::GetWinSystem()->GetGfxContext());
738 StopThread();
741 m_Edl.Clear();
742 CServiceBroker::GetDataCacheCore().Reset();
744 m_HasVideo = false;
745 m_HasAudio = false;
747 CLog::Log(LOGINFO, "VideoPlayer: finished waiting");
748 return true;
751 bool CVideoPlayer::IsPlaying() const
753 return !m_bStop;
756 void CVideoPlayer::OnStartup()
758 m_CurrentVideo.Clear();
759 m_CurrentAudio.Clear();
760 m_CurrentSubtitle.Clear();
761 m_CurrentTeletext.Clear();
762 m_CurrentRadioRDS.Clear();
763 m_CurrentAudioID3.Clear();
765 UTILS::FONT::ClearTemporaryFonts();
768 bool CVideoPlayer::OpenInputStream()
770 if (m_pInputStream.use_count() > 1)
771 throw std::runtime_error("m_pInputStream reference count is greater than 1");
772 m_pInputStream.reset();
774 CLog::Log(LOGINFO, "Creating InputStream");
776 m_pInputStream = CDVDFactoryInputStream::CreateInputStream(this, m_item, true);
777 if (m_pInputStream == nullptr)
779 CLog::Log(LOGERROR, "CVideoPlayer::OpenInputStream - unable to create input stream for [{}]",
780 CURL::GetRedacted(m_item.GetPath()));
781 return false;
784 if (!m_pInputStream->Open())
786 CLog::Log(LOGERROR, "CVideoPlayer::OpenInputStream - error opening [{}]",
787 CURL::GetRedacted(m_item.GetPath()));
788 return false;
791 // find any available external subtitles for non dvd files
792 if (!m_pInputStream->IsStreamType(DVDSTREAM_TYPE_DVD) &&
793 !m_pInputStream->IsStreamType(DVDSTREAM_TYPE_PVRMANAGER))
795 // find any available external subtitles
796 std::vector<std::string> filenames;
798 if (!URIUtils::IsUPnP(m_item.GetPath()))
799 CUtil::ScanForExternalSubtitles(m_item.GetDynPath(), filenames);
801 // load any subtitles from file item
802 std::string key("subtitle:1");
803 for (unsigned s = 1; m_item.HasProperty(key); key = StringUtils::Format("subtitle:{}", ++s))
804 filenames.push_back(m_item.GetProperty(key).asString());
806 for (unsigned int i=0;i<filenames.size();i++)
808 // if vobsub subtitle:
809 if (URIUtils::HasExtension(filenames[i], ".idx"))
811 std::string strSubFile;
812 if (CUtil::FindVobSubPair( filenames, filenames[i], strSubFile))
813 AddSubtitleFile(filenames[i], strSubFile);
815 else
817 if (!CUtil::IsVobSub(filenames, filenames[i] ))
819 AddSubtitleFile(filenames[i]);
822 } // end loop over all subtitle files
825 m_clock.Reset();
826 m_dvd.Clear();
828 return true;
831 bool CVideoPlayer::OpenDemuxStream()
833 CloseDemuxer();
835 CLog::Log(LOGINFO, "Creating Demuxer");
837 int attempts = 10;
838 while (!m_bStop && attempts-- > 0)
840 m_pDemuxer.reset(CDVDFactoryDemuxer::CreateDemuxer(m_pInputStream));
841 if(!m_pDemuxer && m_pInputStream->IsStreamType(DVDSTREAM_TYPE_PVRMANAGER))
843 continue;
845 else if(!m_pDemuxer && m_pInputStream->NextStream() != CDVDInputStream::NEXTSTREAM_NONE)
847 CLog::Log(LOGDEBUG, "{} - New stream available from input, retry open", __FUNCTION__);
848 continue;
850 break;
853 if (!m_pDemuxer)
855 CLog::Log(LOGERROR, "{} - Error creating demuxer", __FUNCTION__);
856 return false;
859 m_SelectionStreams.Clear(STREAM_NONE, STREAM_SOURCE_DEMUX);
860 m_SelectionStreams.Clear(STREAM_NONE, STREAM_SOURCE_NAV);
861 m_SelectionStreams.Update(m_pInputStream, m_pDemuxer.get());
862 m_pDemuxer->GetPrograms(m_programs);
863 UpdateContent();
864 m_demuxerSpeed = DVD_PLAYSPEED_NORMAL;
865 m_processInfo->SetStateRealtime(false);
867 int64_t len = m_pInputStream->GetLength();
868 int64_t tim = m_pDemuxer->GetStreamLength();
869 if (len > 0 && tim > 0)
870 m_pInputStream->SetReadRate(static_cast<uint32_t>(len * 1000 / tim));
872 m_offset_pts = 0;
874 return true;
877 void CVideoPlayer::CloseDemuxer()
879 m_pDemuxer.reset();
880 m_SelectionStreams.Clear(STREAM_NONE, STREAM_SOURCE_DEMUX);
882 CServiceBroker::GetDataCacheCore().SignalAudioInfoChange();
883 CServiceBroker::GetDataCacheCore().SignalVideoInfoChange();
884 CServiceBroker::GetDataCacheCore().SignalSubtitleInfoChange();
887 void CVideoPlayer::OpenDefaultStreams(bool reset)
889 // if input stream dictate, we will open later
890 if (m_dvd.iSelectedAudioStream >= 0 ||
891 m_dvd.iSelectedSPUStream >= 0)
892 return;
894 bool valid;
896 // open video stream
897 valid = false;
899 PredicateVideoFilter vf(m_processInfo->GetVideoSettings().m_VideoStream);
900 for (const auto &stream : m_SelectionStreams.Get(STREAM_VIDEO, vf))
902 if (OpenStream(m_CurrentVideo, stream.demuxerId, stream.id, stream.source, reset))
904 valid = true;
905 break;
908 if (!valid)
910 CloseStream(m_CurrentVideo, true);
911 m_processInfo->ResetVideoCodecInfo();
914 // open audio stream
915 valid = false;
916 if (!m_playerOptions.videoOnly)
918 PredicateAudioFilter af(m_processInfo->GetVideoSettings().m_AudioStream, m_playerOptions.preferStereo);
919 for (const auto &stream : m_SelectionStreams.Get(STREAM_AUDIO, af))
921 if(OpenStream(m_CurrentAudio, stream.demuxerId, stream.id, stream.source, reset))
923 valid = true;
924 break;
929 if(!valid)
931 CloseStream(m_CurrentAudio, true);
932 m_processInfo->ResetAudioCodecInfo();
935 // enable or disable subtitles
936 bool visible = m_processInfo->GetVideoSettings().m_SubtitleOn;
938 // open subtitle stream
939 SelectionStream as = m_SelectionStreams.Get(STREAM_AUDIO, GetAudioStream());
940 PredicateSubtitlePriority psp(as.language,
941 m_processInfo->GetVideoSettings().m_SubtitleStream,
942 m_processInfo->GetVideoSettings().m_SubtitleOn);
943 valid = false;
944 // We need to close CC subtitles to avoid conflicts with external sub stream
945 if (m_CurrentSubtitle.source == STREAM_SOURCE_VIDEOMUX)
946 CloseStream(m_CurrentSubtitle, false);
948 for (const auto &stream : m_SelectionStreams.Get(STREAM_SUBTITLE, psp))
950 if (OpenStream(m_CurrentSubtitle, stream.demuxerId, stream.id, stream.source))
952 valid = true;
953 if(!psp.relevant(stream))
954 visible = false;
955 else if(stream.flags & StreamFlags::FLAG_FORCED)
956 visible = true;
957 break;
960 if(!valid)
961 CloseStream(m_CurrentSubtitle, false);
963 // only set subtitle visibility if state not stored by dvd navigator, because navigator will restore it (if visible)
964 if (!std::dynamic_pointer_cast<CDVDInputStreamNavigator>(m_pInputStream) ||
965 m_playerOptions.state.empty())
967 // SetEnableStream only if not visible, when visible OpenStream already implied that stream is enabled
968 if (valid && !visible)
969 SetEnableStream(m_CurrentSubtitle, false);
971 SetSubtitleVisibleInternal(visible);
974 // open teletext stream
975 valid = false;
976 for (const auto &stream : m_SelectionStreams.Get(STREAM_TELETEXT))
978 if (OpenStream(m_CurrentTeletext, stream.demuxerId, stream.id, stream.source))
980 valid = true;
981 break;
984 if(!valid)
985 CloseStream(m_CurrentTeletext, false);
987 // open RDS stream
988 valid = false;
989 for (const auto &stream : m_SelectionStreams.Get(STREAM_RADIO_RDS))
991 if (OpenStream(m_CurrentRadioRDS, stream.demuxerId, stream.id, stream.source))
993 valid = true;
994 break;
997 if(!valid)
998 CloseStream(m_CurrentRadioRDS, false);
1000 // open ID3 stream
1001 valid = false;
1002 for (const auto& stream : m_SelectionStreams.Get(STREAM_AUDIO_ID3))
1004 if (OpenStream(m_CurrentAudioID3, stream.demuxerId, stream.id, stream.source))
1006 valid = true;
1007 break;
1010 if (!valid)
1011 CloseStream(m_CurrentAudioID3, false);
1013 // disable demux streams
1014 if (NETWORK::IsRemote(m_item) && m_pDemuxer)
1016 for (auto &stream : m_SelectionStreams.m_Streams)
1018 if (STREAM_SOURCE_MASK(stream.source) == STREAM_SOURCE_DEMUX)
1020 if (stream.id != m_CurrentVideo.id && stream.id != m_CurrentAudio.id &&
1021 stream.id != m_CurrentSubtitle.id && stream.id != m_CurrentTeletext.id &&
1022 stream.id != m_CurrentRadioRDS.id && stream.id != m_CurrentAudioID3.id)
1024 m_pDemuxer->EnableStream(stream.demuxerId, stream.id, false);
1031 bool CVideoPlayer::ReadPacket(DemuxPacket*& packet, CDemuxStream*& stream)
1034 // check if we should read from subtitle demuxer
1035 if (m_pSubtitleDemuxer && m_VideoPlayerSubtitle->AcceptsData())
1037 packet = m_pSubtitleDemuxer->Read();
1039 if(packet)
1041 UpdateCorrection(packet, m_offset_pts);
1042 if(packet->iStreamId < 0)
1043 return true;
1045 stream = m_pSubtitleDemuxer->GetStream(packet->demuxerId, packet->iStreamId);
1046 if (!stream)
1048 CLog::Log(LOGERROR, "{} - Error demux packet doesn't belong to a valid stream",
1049 __FUNCTION__);
1050 return false;
1052 if (stream->source == STREAM_SOURCE_NONE)
1054 m_SelectionStreams.Clear(STREAM_NONE, STREAM_SOURCE_DEMUX_SUB);
1055 m_SelectionStreams.Update(NULL, m_pSubtitleDemuxer.get());
1056 UpdateContent();
1058 return true;
1062 // read a data frame from stream.
1063 if (m_pDemuxer)
1064 packet = m_pDemuxer->Read();
1066 if (packet)
1068 // stream changed, update and open defaults
1069 if (packet->iStreamId == DMX_SPECIALID_STREAMCHANGE)
1071 m_SelectionStreams.Clear(STREAM_NONE, STREAM_SOURCE_DEMUX);
1072 m_SelectionStreams.Update(m_pInputStream, m_pDemuxer.get());
1073 m_pDemuxer->GetPrograms(m_programs);
1074 UpdateContent();
1075 OpenDefaultStreams(false);
1077 // reevaluate HasVideo/Audio, we may have switched from/to a radio channel
1078 if(m_CurrentVideo.id < 0)
1079 m_HasVideo = false;
1080 if(m_CurrentAudio.id < 0)
1081 m_HasAudio = false;
1083 return true;
1086 UpdateCorrection(packet, m_offset_pts);
1088 if(packet->iStreamId < 0)
1089 return true;
1091 if(m_pDemuxer)
1093 stream = m_pDemuxer->GetStream(packet->demuxerId, packet->iStreamId);
1094 if (!stream)
1096 CLog::Log(LOGERROR, "{} - Error demux packet doesn't belong to a valid stream",
1097 __FUNCTION__);
1098 return false;
1100 if(stream->source == STREAM_SOURCE_NONE)
1102 m_SelectionStreams.Clear(STREAM_NONE, STREAM_SOURCE_DEMUX);
1103 m_SelectionStreams.Update(m_pInputStream, m_pDemuxer.get());
1104 UpdateContent();
1107 return true;
1109 return false;
1112 bool CVideoPlayer::IsValidStream(const CCurrentStream& stream)
1114 if(stream.id<0)
1115 return true; // we consider non selected as valid
1117 int source = STREAM_SOURCE_MASK(stream.source);
1118 if(source == STREAM_SOURCE_TEXT)
1119 return true;
1120 if (source == STREAM_SOURCE_DEMUX_SUB)
1122 CDemuxStream* st = m_pSubtitleDemuxer->GetStream(stream.demuxerId, stream.id);
1123 if(st == NULL || st->disabled)
1124 return false;
1125 if(st->type != stream.type)
1126 return false;
1127 return true;
1129 if (source == STREAM_SOURCE_DEMUX)
1131 CDemuxStream* st = m_pDemuxer->GetStream(stream.demuxerId, stream.id);
1132 if(st == NULL || st->disabled)
1133 return false;
1134 if(st->type != stream.type)
1135 return false;
1137 if (m_pInputStream && m_pInputStream->IsStreamType(DVDSTREAM_TYPE_DVD))
1139 if (stream.type == STREAM_AUDIO && st->dvdNavId != m_dvd.iSelectedAudioStream)
1140 return false;
1141 if(stream.type == STREAM_SUBTITLE && st->dvdNavId != m_dvd.iSelectedSPUStream)
1142 return false;
1145 return true;
1147 if (source == STREAM_SOURCE_VIDEOMUX)
1149 CDemuxStream* st = m_pCCDemuxer->GetStream(stream.id);
1150 if (st == NULL || st->disabled)
1151 return false;
1152 if (st->type != stream.type)
1153 return false;
1154 return true;
1157 return false;
1160 bool CVideoPlayer::IsBetterStream(const CCurrentStream& current, CDemuxStream* stream)
1162 // Do not reopen non-video streams if we're in video-only mode
1163 if (m_playerOptions.videoOnly && current.type != STREAM_VIDEO)
1164 return false;
1166 if(stream->disabled)
1167 return false;
1169 if (m_pInputStream && (m_pInputStream->IsStreamType(DVDSTREAM_TYPE_DVD) ||
1170 m_pInputStream->IsStreamType(DVDSTREAM_TYPE_BLURAY)))
1172 int source_type;
1174 source_type = STREAM_SOURCE_MASK(current.source);
1175 if (source_type != STREAM_SOURCE_DEMUX &&
1176 source_type != STREAM_SOURCE_NONE)
1177 return false;
1179 source_type = STREAM_SOURCE_MASK(stream->source);
1180 if(source_type != STREAM_SOURCE_DEMUX ||
1181 stream->type != current.type ||
1182 stream->uniqueId == current.id)
1183 return false;
1185 if(current.type == STREAM_AUDIO && stream->dvdNavId == m_dvd.iSelectedAudioStream)
1186 return true;
1187 if(current.type == STREAM_SUBTITLE && stream->dvdNavId == m_dvd.iSelectedSPUStream)
1188 return true;
1189 if(current.type == STREAM_VIDEO && current.id < 0)
1190 return true;
1192 else
1194 if(stream->source == current.source &&
1195 stream->uniqueId == current.id &&
1196 stream->demuxerId == current.demuxerId)
1197 return false;
1199 if(stream->type != current.type)
1200 return false;
1202 if(current.type == STREAM_SUBTITLE)
1203 return false;
1205 if(current.id < 0)
1206 return true;
1208 return false;
1211 void CVideoPlayer::CheckBetterStream(CCurrentStream& current, CDemuxStream* stream)
1213 IDVDStreamPlayer* player = GetStreamPlayer(current.player);
1214 if (!IsValidStream(current) && (player == NULL || player->IsStalled()))
1215 CloseStream(current, true);
1217 if (IsBetterStream(current, stream))
1218 OpenStream(current, stream->demuxerId, stream->uniqueId, stream->source);
1221 void CVideoPlayer::Prepare()
1223 CFFmpegLog::SetLogLevel(1);
1224 SetPlaySpeed(DVD_PLAYSPEED_NORMAL);
1225 m_processInfo->SetSpeed(1.0);
1226 m_processInfo->SetTempo(1.0);
1227 m_processInfo->SetFrameAdvance(false);
1228 m_State.Clear();
1229 m_CurrentVideo.hint.Clear();
1230 m_CurrentAudio.hint.Clear();
1231 m_CurrentSubtitle.hint.Clear();
1232 m_CurrentTeletext.hint.Clear();
1233 m_CurrentRadioRDS.hint.Clear();
1234 m_CurrentAudioID3.hint.Clear();
1235 m_SpeedState.Reset(DVD_NOPTS_VALUE);
1236 m_offset_pts = 0;
1237 m_CurrentAudio.lastdts = DVD_NOPTS_VALUE;
1238 m_CurrentVideo.lastdts = DVD_NOPTS_VALUE;
1240 IPlayerCallback *cb = &m_callback;
1241 CFileItem fileItem = m_item;
1242 m_outboundEvents->Submit([=]() {
1243 cb->RequestVideoSettings(fileItem);
1246 if (!OpenInputStream())
1248 m_bAbortRequest = true;
1249 m_error = true;
1250 return;
1253 bool discStateRestored = false;
1254 if (std::shared_ptr<CDVDInputStream::IMenus> ptr = std::dynamic_pointer_cast<CDVDInputStream::IMenus>(m_pInputStream))
1256 CLog::Log(LOGINFO, "VideoPlayer: playing a file with menu's");
1258 if (!m_playerOptions.state.empty())
1260 discStateRestored = ptr->SetState(m_playerOptions.state);
1262 else if(std::shared_ptr<CDVDInputStreamNavigator> nav = std::dynamic_pointer_cast<CDVDInputStreamNavigator>(m_pInputStream))
1264 nav->EnableSubtitleStream(m_processInfo->GetVideoSettings().m_SubtitleOn);
1268 if (!OpenDemuxStream())
1270 m_bAbortRequest = true;
1271 m_error = true;
1272 return;
1274 // give players a chance to reconsider now codecs are known
1275 CreatePlayers();
1277 if (!discStateRestored)
1278 OpenDefaultStreams();
1281 * Check to see if the demuxer should start at something other than time 0. This will be the case
1282 * if there was a start time specified as part of the "Start from where last stopped" (aka
1283 * auto-resume) feature or if there is an EDL cut or commercial break that starts at time 0.
1285 std::chrono::milliseconds starttime = 0ms;
1286 if (m_playerOptions.starttime > 0 || m_playerOptions.startpercent > 0)
1288 if (m_playerOptions.startpercent > 0 && m_pDemuxer)
1290 std::chrono::milliseconds playerStartTime =
1291 std::chrono::milliseconds(static_cast<int>((static_cast<double>(
1292 m_pDemuxer->GetStreamLength() * (m_playerOptions.startpercent / 100.0)))));
1293 starttime = m_Edl.GetTimeAfterRestoringCuts(playerStartTime);
1295 else
1297 starttime =
1298 m_Edl.GetTimeAfterRestoringCuts(std::chrono::duration_cast<std::chrono::milliseconds>(
1299 std::chrono::seconds(static_cast<int>(m_playerOptions.starttime))));
1301 CLog::Log(LOGDEBUG, "{} - Start position set to last stopped position: {}", __FUNCTION__,
1302 starttime.count());
1304 else
1306 const auto hasEdit = m_Edl.InEdit(starttime);
1307 if (hasEdit)
1309 const auto& edit = hasEdit.value();
1310 // save last edit times
1311 m_Edl.SetLastEditTime(edit->start);
1312 m_Edl.SetLastEditActionType(edit->action);
1314 if (edit->action == EDL::Action::CUT)
1316 starttime = edit->end;
1317 CLog::Log(LOGDEBUG, "{} - Start position set to end of first cut: {}", __FUNCTION__,
1318 starttime.count());
1320 else if (edit->action == EDL::Action::COMM_BREAK)
1322 if (m_SkipCommercials)
1324 starttime = edit->end;
1325 CLog::Log(LOGDEBUG, "{} - Start position set to end of first commercial break: {}",
1326 __FUNCTION__, starttime.count());
1329 const std::shared_ptr<CAdvancedSettings> advancedSettings =
1330 CServiceBroker::GetSettingsComponent()->GetAdvancedSettings();
1331 if (advancedSettings && advancedSettings->m_EdlDisplayCommbreakNotifications)
1333 const std::string timeString =
1334 StringUtils::SecondsToTimeString(edit->end.count(), TIME_FORMAT_MM_SS);
1335 CGUIDialogKaiToast::QueueNotification(g_localizeStrings.Get(25011), timeString);
1341 if (starttime > 0ms)
1343 double startpts = DVD_NOPTS_VALUE;
1344 if (m_pDemuxer)
1346 if (m_pDemuxer->SeekTime(starttime.count(), true, &startpts))
1348 FlushBuffers(starttime.count() / 1000 * AV_TIME_BASE, true, true);
1349 CLog::Log(LOGDEBUG, "{} - starting demuxer from: {}", __FUNCTION__, starttime.count());
1351 else
1352 CLog::Log(LOGDEBUG, "{} - failed to start demuxing from: {}", __FUNCTION__,
1353 starttime.count());
1356 if (m_pSubtitleDemuxer)
1358 if (m_pSubtitleDemuxer->SeekTime(starttime.count(), true, &startpts))
1359 CLog::Log(LOGDEBUG, "{} - starting subtitle demuxer from: {}", __FUNCTION__,
1360 starttime.count());
1361 else
1362 CLog::Log(LOGDEBUG, "{} - failed to start subtitle demuxing from: {}", __FUNCTION__,
1363 starttime.count());
1366 m_clock.Discontinuity(DVD_MSEC_TO_TIME(starttime.count()));
1369 UpdatePlayState(0);
1371 SetCaching(CACHESTATE_FLUSH);
1374 void CVideoPlayer::Process()
1376 // Try to resolve the correct mime type. This can take some time, for example if a requested
1377 // item is located at a slow/not reachable remote source. So, do mime type detection in vp worker
1378 // thread, not directly when initalizing the player to keep GUI responsible.
1379 m_item.SetMimeTypeForInternetFile();
1381 CServiceBroker::GetWinSystem()->RegisterRenderLoop(this);
1383 Prepare();
1385 while (!m_bAbortRequest)
1387 // check display lost
1388 if (m_displayLost)
1390 CThread::Sleep(50ms);
1391 continue;
1394 // check if in an edit (cut or commercial break) that should be automatically skipped
1395 CheckAutoSceneSkip();
1397 // handle messages send to this thread, like seek or demuxer reset requests
1398 HandleMessages();
1400 if (m_bAbortRequest)
1401 break;
1403 // should we open a new input stream?
1404 if (!m_pInputStream)
1406 if (OpenInputStream() == false)
1408 m_bAbortRequest = true;
1409 break;
1413 // should we open a new demuxer?
1414 if (!m_pDemuxer)
1416 if (m_pInputStream->NextStream() == CDVDInputStream::NEXTSTREAM_NONE)
1417 break;
1419 if (m_pInputStream->IsEOF())
1420 break;
1422 if (OpenDemuxStream() == false)
1424 m_bAbortRequest = true;
1425 break;
1428 // on channel switch we don't want to close stream players at this
1429 // time. we'll get the stream change event later
1430 if (!m_pInputStream->IsStreamType(DVDSTREAM_TYPE_PVRMANAGER) ||
1431 !m_SelectionStreams.m_Streams.empty())
1432 OpenDefaultStreams();
1434 UpdatePlayState(0);
1437 // handle eventual seeks due to playspeed
1438 HandlePlaySpeed();
1440 // update player state
1441 UpdatePlayState(200);
1443 // make sure we run subtitle process here
1444 m_VideoPlayerSubtitle->Process(m_clock.GetClock() + m_State.time_offset - m_VideoPlayerVideo->GetSubtitleDelay(), m_State.time_offset);
1446 // tell demuxer if we want to fill buffers
1447 if (m_demuxerSpeed != DVD_PLAYSPEED_PAUSE)
1449 int audioLevel = 90;
1450 int videoLevel = 90;
1451 bool fillBuffer = false;
1452 if (m_CurrentAudio.id >= 0)
1453 audioLevel = m_VideoPlayerAudio->GetLevel();
1454 if (m_CurrentVideo.id >= 0)
1455 videoLevel = m_processInfo->GetLevelVQ();
1456 if (videoLevel < 85 && audioLevel < 85)
1458 fillBuffer = true;
1460 if (m_pDemuxer)
1461 m_pDemuxer->FillBuffer(fillBuffer);
1464 // if the queues are full, no need to read more
1465 if ((!m_VideoPlayerAudio->AcceptsData() && m_CurrentAudio.id >= 0) ||
1466 (!m_VideoPlayerVideo->AcceptsData() && m_CurrentVideo.id >= 0))
1468 if (m_playSpeed == DVD_PLAYSPEED_PAUSE &&
1469 m_demuxerSpeed != DVD_PLAYSPEED_PAUSE)
1471 if (m_pDemuxer)
1472 m_pDemuxer->SetSpeed(DVD_PLAYSPEED_PAUSE);
1473 m_demuxerSpeed = DVD_PLAYSPEED_PAUSE;
1475 CThread::Sleep(10ms);
1476 continue;
1479 // adjust demuxer speed; some rtsp servers wants to know for i.e. ff
1480 // delay pause until queue is full
1481 if (m_playSpeed != DVD_PLAYSPEED_PAUSE &&
1482 m_demuxerSpeed != m_playSpeed)
1484 if (m_pDemuxer)
1485 m_pDemuxer->SetSpeed(m_playSpeed);
1486 m_demuxerSpeed = m_playSpeed;
1489 DemuxPacket* pPacket = NULL;
1490 CDemuxStream *pStream = NULL;
1491 ReadPacket(pPacket, pStream);
1492 if (pPacket && !pStream)
1494 /* probably a empty packet, just free it and move on */
1495 CDVDDemuxUtils::FreeDemuxPacket(pPacket);
1496 continue;
1499 if (!pPacket)
1501 // when paused, demuxer could be be returning empty
1502 if (m_playSpeed == DVD_PLAYSPEED_PAUSE)
1503 continue;
1505 // check for a still frame state
1506 if (std::shared_ptr<CDVDInputStream::IMenus> pStream = std::dynamic_pointer_cast<CDVDInputStream::IMenus>(m_pInputStream))
1508 // stills will be skipped
1509 if(m_dvd.state == DVDSTATE_STILL)
1511 if (m_dvd.iDVDStillTime > 0ms)
1513 const auto now = std::chrono::steady_clock::now();
1514 const auto duration = now - m_dvd.iDVDStillStartTime;
1516 if (duration >= m_dvd.iDVDStillTime)
1518 m_dvd.iDVDStillTime = 0ms;
1519 m_dvd.iDVDStillStartTime = {};
1520 m_dvd.state = DVDSTATE_NORMAL;
1521 pStream->SkipStill();
1522 continue;
1528 // if there is another stream available, reopen demuxer
1529 CDVDInputStream::ENextStream next = m_pInputStream->NextStream();
1530 if(next == CDVDInputStream::NEXTSTREAM_OPEN)
1532 CloseDemuxer();
1534 SetCaching(CACHESTATE_DONE);
1535 CLog::Log(LOGINFO, "VideoPlayer: next stream, wait for old streams to be finished");
1536 CloseStream(m_CurrentAudio, true);
1537 CloseStream(m_CurrentVideo, true);
1539 m_CurrentAudio.Clear();
1540 m_CurrentVideo.Clear();
1541 m_CurrentSubtitle.Clear();
1542 continue;
1545 // input stream asked us to just retry
1546 if(next == CDVDInputStream::NEXTSTREAM_RETRY)
1548 CThread::Sleep(100ms);
1549 continue;
1552 if (m_CurrentVideo.inited)
1554 m_VideoPlayerVideo->SendMessage(std::make_shared<CDVDMsg>(CDVDMsg::VIDEO_DRAIN));
1557 m_CurrentAudio.inited = false;
1558 m_CurrentVideo.inited = false;
1559 m_CurrentSubtitle.inited = false;
1560 m_CurrentTeletext.inited = false;
1561 m_CurrentRadioRDS.inited = false;
1562 m_CurrentAudioID3.inited = false;
1564 // if we are caching, start playing it again
1565 SetCaching(CACHESTATE_DONE);
1567 // while players are still playing, keep going to allow seekbacks
1568 if (m_VideoPlayerAudio->HasData() ||
1569 m_VideoPlayerVideo->HasData())
1571 CThread::Sleep(100ms);
1572 continue;
1575 if (!m_pInputStream->IsEOF())
1576 CLog::Log(LOGINFO, "{} - eof reading from demuxer", __FUNCTION__);
1578 break;
1581 // see if we can find something better to play
1582 CheckBetterStream(m_CurrentAudio, pStream);
1583 CheckBetterStream(m_CurrentVideo, pStream);
1584 CheckBetterStream(m_CurrentSubtitle, pStream);
1585 CheckBetterStream(m_CurrentTeletext, pStream);
1586 CheckBetterStream(m_CurrentRadioRDS, pStream);
1587 CheckBetterStream(m_CurrentAudioID3, pStream);
1589 // demux video stream
1590 if (CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(CSettings::SETTING_SUBTITLES_PARSECAPTIONS) && CheckIsCurrent(m_CurrentVideo, pStream, pPacket))
1592 if (m_pCCDemuxer)
1594 bool first = true;
1595 while (!m_bAbortRequest)
1597 DemuxPacket *pkt = m_pCCDemuxer->Read(first ? pPacket : NULL);
1598 if (!pkt)
1599 break;
1601 first = false;
1602 if (m_pCCDemuxer->GetNrOfStreams() != m_SelectionStreams.CountTypeOfSource(STREAM_SUBTITLE, STREAM_SOURCE_VIDEOMUX))
1604 m_SelectionStreams.Clear(STREAM_SUBTITLE, STREAM_SOURCE_VIDEOMUX);
1605 m_SelectionStreams.Update(NULL, m_pCCDemuxer.get(), "");
1606 UpdateContent();
1607 OpenDefaultStreams(false);
1609 CDemuxStream *pSubStream = m_pCCDemuxer->GetStream(pkt->iStreamId);
1610 if (pSubStream && m_CurrentSubtitle.id == pkt->iStreamId && m_CurrentSubtitle.source == STREAM_SOURCE_VIDEOMUX)
1611 ProcessSubData(pSubStream, pkt);
1612 else
1613 CDVDDemuxUtils::FreeDemuxPacket(pkt);
1618 if (IsInMenuInternal())
1620 if (std::shared_ptr<CDVDInputStream::IMenus> menu = std::dynamic_pointer_cast<CDVDInputStream::IMenus>(m_pInputStream))
1622 double correction = menu->GetTimeStampCorrection();
1623 if (pPacket->dts != DVD_NOPTS_VALUE && pPacket->dts > correction)
1624 pPacket->dts -= correction;
1625 if (pPacket->pts != DVD_NOPTS_VALUE && pPacket->pts > correction)
1626 pPacket->pts -= correction;
1628 if (m_dvd.syncClock)
1630 m_clock.Discontinuity(pPacket->dts);
1631 m_dvd.syncClock = false;
1635 // process the packet
1636 ProcessPacket(pStream, pPacket);
1640 bool CVideoPlayer::CheckIsCurrent(const CCurrentStream& current,
1641 CDemuxStream* stream,
1642 DemuxPacket* pkg)
1644 if(current.id == pkg->iStreamId &&
1645 current.demuxerId == stream->demuxerId &&
1646 current.source == stream->source &&
1647 current.type == stream->type)
1648 return true;
1649 else
1650 return false;
1653 void CVideoPlayer::ProcessPacket(CDemuxStream* pStream, DemuxPacket* pPacket)
1655 // process packet if it belongs to selected stream.
1656 // for dvd's don't allow automatic opening of streams*/
1658 if (CheckIsCurrent(m_CurrentAudio, pStream, pPacket))
1659 ProcessAudioData(pStream, pPacket);
1660 else if (CheckIsCurrent(m_CurrentVideo, pStream, pPacket))
1661 ProcessVideoData(pStream, pPacket);
1662 else if (CheckIsCurrent(m_CurrentSubtitle, pStream, pPacket))
1663 ProcessSubData(pStream, pPacket);
1664 else if (CheckIsCurrent(m_CurrentTeletext, pStream, pPacket))
1665 ProcessTeletextData(pStream, pPacket);
1666 else if (CheckIsCurrent(m_CurrentRadioRDS, pStream, pPacket))
1667 ProcessRadioRDSData(pStream, pPacket);
1668 else if (CheckIsCurrent(m_CurrentAudioID3, pStream, pPacket))
1669 ProcessAudioID3Data(pStream, pPacket);
1670 else
1672 CDVDDemuxUtils::FreeDemuxPacket(pPacket); // free it since we won't do anything with it
1676 void CVideoPlayer::CheckStreamChanges(CCurrentStream& current, CDemuxStream* stream)
1678 if (current.stream != (void*)stream
1679 || current.changes != stream->changes)
1681 /* check so that dmuxer hints or extra data hasn't changed */
1682 /* if they have, reopen stream */
1684 if (current.hint != CDVDStreamInfo(*stream, true))
1686 m_SelectionStreams.Clear(STREAM_NONE, STREAM_SOURCE_DEMUX);
1687 m_SelectionStreams.Update(m_pInputStream, m_pDemuxer.get());
1688 UpdateContent();
1689 OpenDefaultStreams(false);
1692 current.stream = (void*)stream;
1693 current.changes = stream->changes;
1697 void CVideoPlayer::ProcessAudioData(CDemuxStream* pStream, DemuxPacket* pPacket)
1699 CheckStreamChanges(m_CurrentAudio, pStream);
1701 bool checkcont = CheckContinuity(m_CurrentAudio, pPacket);
1702 UpdateTimestamps(m_CurrentAudio, pPacket);
1704 if (checkcont && (m_CurrentAudio.avsync == CCurrentStream::AV_SYNC_CHECK))
1705 m_CurrentAudio.avsync = CCurrentStream::AV_SYNC_NONE;
1707 bool drop = false;
1708 if (CheckPlayerInit(m_CurrentAudio))
1709 drop = true;
1712 * If CheckSceneSkip() returns true then demux point is inside an EDL cut and the packets are dropped.
1714 if (CheckSceneSkip(m_CurrentAudio))
1716 drop = true;
1718 else
1720 const auto hasEdit = m_Edl.InEdit(
1721 std::chrono::milliseconds(DVD_TIME_TO_MSEC(m_CurrentAudio.dts + m_offset_pts)));
1722 if (hasEdit && hasEdit.value()->action == EDL::Action::MUTE)
1723 drop = true;
1726 m_VideoPlayerAudio->SendMessage(std::make_shared<CDVDMsgDemuxerPacket>(pPacket, drop));
1728 if (!drop)
1729 m_CurrentAudio.packets++;
1732 void CVideoPlayer::ProcessVideoData(CDemuxStream* pStream, DemuxPacket* pPacket)
1734 CheckStreamChanges(m_CurrentVideo, pStream);
1735 bool checkcont = false;
1737 if( pPacket->iSize != 4) //don't check the EOF_SEQUENCE of stillframes
1739 checkcont = CheckContinuity(m_CurrentVideo, pPacket);
1740 UpdateTimestamps(m_CurrentVideo, pPacket);
1742 if (checkcont && (m_CurrentVideo.avsync == CCurrentStream::AV_SYNC_CHECK))
1743 m_CurrentVideo.avsync = CCurrentStream::AV_SYNC_NONE;
1745 bool drop = false;
1746 if (CheckPlayerInit(m_CurrentVideo))
1747 drop = true;
1749 if (CheckSceneSkip(m_CurrentVideo))
1750 drop = true;
1752 m_VideoPlayerVideo->SendMessage(std::make_shared<CDVDMsgDemuxerPacket>(pPacket, drop));
1754 if (!drop)
1755 m_CurrentVideo.packets++;
1758 void CVideoPlayer::ProcessSubData(CDemuxStream* pStream, DemuxPacket* pPacket)
1760 CheckStreamChanges(m_CurrentSubtitle, pStream);
1762 UpdateTimestamps(m_CurrentSubtitle, pPacket);
1764 bool drop = false;
1765 if (CheckPlayerInit(m_CurrentSubtitle))
1766 drop = true;
1768 if (CheckSceneSkip(m_CurrentSubtitle))
1769 drop = true;
1771 m_VideoPlayerSubtitle->SendMessage(std::make_shared<CDVDMsgDemuxerPacket>(pPacket, drop));
1773 if(m_pInputStream && m_pInputStream->IsStreamType(DVDSTREAM_TYPE_DVD))
1774 m_VideoPlayerSubtitle->UpdateOverlayInfo(std::static_pointer_cast<CDVDInputStreamNavigator>(m_pInputStream), LIBDVDNAV_BUTTON_NORMAL);
1777 void CVideoPlayer::ProcessTeletextData(CDemuxStream* pStream, DemuxPacket* pPacket)
1779 CheckStreamChanges(m_CurrentTeletext, pStream);
1781 UpdateTimestamps(m_CurrentTeletext, pPacket);
1783 bool drop = false;
1784 if (CheckPlayerInit(m_CurrentTeletext))
1785 drop = true;
1787 if (CheckSceneSkip(m_CurrentTeletext))
1788 drop = true;
1790 m_VideoPlayerTeletext->SendMessage(std::make_shared<CDVDMsgDemuxerPacket>(pPacket, drop));
1793 void CVideoPlayer::ProcessRadioRDSData(CDemuxStream* pStream, DemuxPacket* pPacket)
1795 CheckStreamChanges(m_CurrentRadioRDS, pStream);
1797 UpdateTimestamps(m_CurrentRadioRDS, pPacket);
1799 bool drop = false;
1800 if (CheckPlayerInit(m_CurrentRadioRDS))
1801 drop = true;
1803 if (CheckSceneSkip(m_CurrentRadioRDS))
1804 drop = true;
1806 m_VideoPlayerRadioRDS->SendMessage(std::make_shared<CDVDMsgDemuxerPacket>(pPacket, drop));
1809 void CVideoPlayer::ProcessAudioID3Data(CDemuxStream* pStream, DemuxPacket* pPacket)
1811 CheckStreamChanges(m_CurrentAudioID3, pStream);
1813 UpdateTimestamps(m_CurrentAudioID3, pPacket);
1815 bool drop = false;
1816 if (CheckPlayerInit(m_CurrentAudioID3))
1817 drop = true;
1819 if (CheckSceneSkip(m_CurrentAudioID3))
1820 drop = true;
1822 m_VideoPlayerAudioID3->SendMessage(std::make_shared<CDVDMsgDemuxerPacket>(pPacket, drop));
1825 CacheInfo CVideoPlayer::GetCachingTimes()
1827 CacheInfo info{};
1829 if (!m_pInputStream || !m_pDemuxer)
1830 return info;
1832 XFILE::SCacheStatus status;
1833 if (!m_pInputStream->GetCacheStatus(&status))
1834 return info;
1836 const uint64_t& maxforward = status.maxforward;
1837 const uint64_t& cached = status.forward;
1838 const uint32_t& currate = status.currate;
1839 const uint32_t& maxrate = status.maxrate;
1840 const uint32_t& lowrate = status.lowrate;
1842 int64_t length = m_pInputStream->GetLength();
1843 int64_t remain = length - m_pInputStream->Seek(0, SEEK_CUR);
1845 if (length <= 0 || remain < 0)
1846 return info;
1848 double queueTime = GetQueueTime();
1849 double play_sbp = DVD_MSEC_TO_TIME(m_pDemuxer->GetStreamLength()) / length;
1850 double queued = 1000.0 * queueTime / play_sbp;
1852 info.level = 0.0;
1853 info.offset = (cached + queued) / length;
1854 info.time = 0.0;
1855 info.valid = true;
1857 if (currate == 0)
1858 return info;
1860 // estimated playback time of current cached bytes
1861 const double cacheTime = (static_cast<double>(cached) / currate) + (queueTime / 1000.0);
1863 // cache level as current forward bytes / max forward bytes [0.0 - 1.0]
1864 const double cacheLevel = (maxforward > 0) ? static_cast<double>(cached) / maxforward : 0.0;
1866 info.time = cacheTime;
1868 if (lowrate > 0)
1870 // buffer is full & our read rate is too low
1871 CLog::Log(LOGDEBUG, "Readrate {} was too low with {} required", lowrate, maxrate);
1872 info.level = -1.0;
1874 else
1875 info.level = cacheLevel;
1877 return info;
1880 void CVideoPlayer::HandlePlaySpeed()
1882 const bool isInMenu = IsInMenuInternal();
1883 const bool tolerateStall =
1884 isInMenu || (m_CurrentVideo.hint.flags & StreamFlags::FLAG_STILL_IMAGES);
1886 if (tolerateStall && m_caching != CACHESTATE_DONE)
1887 SetCaching(CACHESTATE_DONE);
1889 if (m_caching == CACHESTATE_FULL)
1891 CacheInfo cache = GetCachingTimes();
1892 if (cache.valid)
1894 if (cache.level < 0.0)
1896 CGUIDialogKaiToast::QueueNotification(g_localizeStrings.Get(21454), g_localizeStrings.Get(21455));
1897 SetCaching(CACHESTATE_INIT);
1899 // Note: Previously used cache.level >= 1 would keep video stalled
1900 // event after cache was full
1901 // Talk link: https://github.com/xbmc/xbmc/pull/23760
1902 if (cache.time > m_messageQueueTimeSize)
1903 SetCaching(CACHESTATE_INIT);
1905 else
1907 if ((!m_VideoPlayerAudio->AcceptsData() && m_CurrentAudio.id >= 0) ||
1908 (!m_VideoPlayerVideo->AcceptsData() && m_CurrentVideo.id >= 0))
1909 SetCaching(CACHESTATE_INIT);
1912 // if audio stream stalled, wait until demux queue filled 10%
1913 if (m_pInputStream->IsRealtime() &&
1914 (m_CurrentAudio.id < 0 || m_VideoPlayerAudio->GetLevel() > 10))
1916 SetCaching(CACHESTATE_INIT);
1920 if (m_caching == CACHESTATE_INIT)
1922 // if all enabled streams have been inited we are done
1923 if ((m_CurrentVideo.id >= 0 || m_CurrentAudio.id >= 0) &&
1924 (m_CurrentVideo.id < 0 || m_CurrentVideo.syncState != IDVDStreamPlayer::SYNC_STARTING) &&
1925 (m_CurrentAudio.id < 0 || m_CurrentAudio.syncState != IDVDStreamPlayer::SYNC_STARTING))
1926 SetCaching(CACHESTATE_PLAY);
1928 // handle exceptions
1929 if (m_CurrentAudio.id >= 0 && m_CurrentVideo.id >= 0)
1931 if ((!m_VideoPlayerAudio->AcceptsData() || !m_VideoPlayerVideo->AcceptsData()) &&
1932 m_cachingTimer.IsTimePast())
1934 SetCaching(CACHESTATE_DONE);
1939 if (m_caching == CACHESTATE_PLAY)
1941 // if all enabled streams have started playing we are done
1942 if ((m_CurrentVideo.id < 0 || !m_VideoPlayerVideo->IsStalled()) &&
1943 (m_CurrentAudio.id < 0 || !m_VideoPlayerAudio->IsStalled()))
1944 SetCaching(CACHESTATE_DONE);
1947 if (m_caching == CACHESTATE_DONE)
1949 if (m_playSpeed == DVD_PLAYSPEED_NORMAL && !tolerateStall)
1951 // take action if audio or video stream is stalled
1952 if (((m_VideoPlayerAudio->IsStalled() && m_CurrentAudio.inited) ||
1953 (m_VideoPlayerVideo->IsStalled() && m_CurrentVideo.inited)) &&
1954 m_syncTimer.IsTimePast())
1956 if (m_pInputStream->IsRealtime())
1958 if ((m_CurrentAudio.id >= 0 && m_CurrentAudio.syncState == IDVDStreamPlayer::SYNC_INSYNC &&
1959 m_VideoPlayerAudio->IsStalled()) ||
1960 (m_CurrentVideo.id >= 0 && m_CurrentVideo.syncState == IDVDStreamPlayer::SYNC_INSYNC &&
1961 m_processInfo->GetLevelVQ() == 0))
1963 CLog::Log(LOGDEBUG, "Stream stalled, start buffering. Audio: {} - Video: {}",
1964 m_VideoPlayerAudio->GetLevel(), m_processInfo->GetLevelVQ());
1966 if (m_VideoPlayerAudio->AcceptsData() && m_VideoPlayerVideo->AcceptsData())
1967 SetCaching(CACHESTATE_FULL);
1968 else
1969 FlushBuffers(DVD_NOPTS_VALUE, false, true);
1972 else
1974 // start caching if audio and video have run dry
1975 if (m_VideoPlayerAudio->GetLevel() <= 50 &&
1976 m_processInfo->GetLevelVQ() <= 50)
1978 SetCaching(CACHESTATE_FULL);
1980 else if (m_CurrentAudio.id >= 0 && m_CurrentAudio.inited &&
1981 m_CurrentAudio.syncState == IDVDStreamPlayer::SYNC_INSYNC &&
1982 m_VideoPlayerAudio->GetLevel() == 0)
1984 CLog::Log(LOGDEBUG,"CVideoPlayer::HandlePlaySpeed - audio stream stalled, triggering re-sync");
1985 FlushBuffers(DVD_NOPTS_VALUE, true, true);
1986 CDVDMsgPlayerSeek::CMode mode;
1987 mode.time = (int)GetUpdatedTime();
1988 mode.backward = false;
1989 mode.accurate = true;
1990 mode.sync = true;
1991 m_messenger.Put(std::make_shared<CDVDMsgPlayerSeek>(mode));
1995 // care for live streams
1996 else if (m_pInputStream->IsRealtime())
1998 if (m_CurrentAudio.id >= 0)
2000 double adjust = -1.0; // a unique value
2001 if (m_clock.GetSpeedAdjust() >= 0 && m_VideoPlayerAudio->GetLevel() < 5)
2002 adjust = -0.05;
2004 if (m_clock.GetSpeedAdjust() < 0 && m_VideoPlayerAudio->GetLevel() > 10)
2005 adjust = 0.0;
2007 if (adjust != -1.0)
2009 m_clock.SetSpeedAdjust(adjust);
2016 // sync streams to clock
2017 if ((m_CurrentVideo.syncState == IDVDStreamPlayer::SYNC_WAITSYNC) ||
2018 (m_CurrentAudio.syncState == IDVDStreamPlayer::SYNC_WAITSYNC))
2020 unsigned int threshold = 20;
2021 if (m_pInputStream->IsRealtime())
2022 threshold = 40;
2024 bool video = m_CurrentVideo.id < 0 || (m_CurrentVideo.syncState == IDVDStreamPlayer::SYNC_WAITSYNC) ||
2025 (m_CurrentVideo.packets == 0 && m_CurrentAudio.packets > threshold) ||
2026 (!m_VideoPlayerAudio->AcceptsData() && m_processInfo->GetLevelVQ() < 10);
2027 bool audio = m_CurrentAudio.id < 0 || (m_CurrentAudio.syncState == IDVDStreamPlayer::SYNC_WAITSYNC) ||
2028 (m_CurrentAudio.packets == 0 && m_CurrentVideo.packets > threshold) ||
2029 (!m_VideoPlayerVideo->AcceptsData() && m_VideoPlayerAudio->GetLevel() < 10);
2031 if (m_CurrentAudio.syncState == IDVDStreamPlayer::SYNC_WAITSYNC &&
2032 (m_CurrentAudio.avsync == CCurrentStream::AV_SYNC_CONT ||
2033 m_CurrentVideo.syncState == IDVDStreamPlayer::SYNC_INSYNC))
2035 m_CurrentAudio.syncState = IDVDStreamPlayer::SYNC_INSYNC;
2036 m_CurrentAudio.avsync = CCurrentStream::AV_SYNC_NONE;
2037 m_VideoPlayerAudio->SendMessage(
2038 std::make_shared<CDVDMsgDouble>(CDVDMsg::GENERAL_RESYNC, m_clock.GetClock()), 1);
2040 else if (m_CurrentVideo.syncState == IDVDStreamPlayer::SYNC_WAITSYNC &&
2041 (m_CurrentVideo.avsync == CCurrentStream::AV_SYNC_CONT ||
2042 m_CurrentAudio.syncState == IDVDStreamPlayer::SYNC_INSYNC))
2044 m_CurrentVideo.syncState = IDVDStreamPlayer::SYNC_INSYNC;
2045 m_CurrentVideo.avsync = CCurrentStream::AV_SYNC_NONE;
2046 m_VideoPlayerVideo->SendMessage(
2047 std::make_shared<CDVDMsgDouble>(CDVDMsg::GENERAL_RESYNC, m_clock.GetClock()), 1);
2049 else if (video && audio)
2051 double clock = 0;
2052 if (m_CurrentAudio.syncState == IDVDStreamPlayer::SYNC_WAITSYNC)
2053 CLog::Log(LOGDEBUG, "VideoPlayer::Sync - Audio - pts: {:f}, cache: {:f}, totalcache: {:f}",
2054 m_CurrentAudio.starttime, m_CurrentAudio.cachetime, m_CurrentAudio.cachetotal);
2055 if (m_CurrentVideo.syncState == IDVDStreamPlayer::SYNC_WAITSYNC)
2056 CLog::Log(LOGDEBUG, "VideoPlayer::Sync - Video - pts: {:f}, cache: {:f}, totalcache: {:f}",
2057 m_CurrentVideo.starttime, m_CurrentVideo.cachetime, m_CurrentVideo.cachetotal);
2059 if (m_CurrentVideo.starttime != DVD_NOPTS_VALUE && m_CurrentVideo.packets > 0 &&
2060 m_playSpeed == DVD_PLAYSPEED_PAUSE)
2062 clock = m_CurrentVideo.starttime;
2064 else if (m_CurrentAudio.starttime != DVD_NOPTS_VALUE && m_CurrentAudio.packets > 0)
2066 if (m_pInputStream->IsRealtime())
2067 clock = m_CurrentAudio.starttime - m_CurrentAudio.cachetotal - DVD_MSEC_TO_TIME(400);
2068 else
2069 clock = m_CurrentAudio.starttime - m_CurrentAudio.cachetime;
2071 if (m_CurrentVideo.starttime != DVD_NOPTS_VALUE && (m_CurrentVideo.packets > 0))
2073 if (m_CurrentVideo.starttime - m_CurrentVideo.cachetotal < clock)
2075 clock = m_CurrentVideo.starttime - m_CurrentVideo.cachetotal;
2077 else if (m_CurrentVideo.starttime > m_CurrentAudio.starttime &&
2078 !m_pInputStream->IsRealtime())
2080 int audioLevel = m_VideoPlayerAudio->GetLevel();
2081 //@todo hardcoded 8 seconds in message queue
2082 double maxAudioTime = clock + DVD_MSEC_TO_TIME(80 * audioLevel);
2083 if ((m_CurrentVideo.starttime - m_CurrentVideo.cachetotal) > maxAudioTime)
2084 clock = maxAudioTime;
2085 else
2086 clock = m_CurrentVideo.starttime - m_CurrentVideo.cachetotal;
2090 else if (m_CurrentVideo.starttime != DVD_NOPTS_VALUE && m_CurrentVideo.packets > 0)
2092 clock = m_CurrentVideo.starttime - m_CurrentVideo.cachetotal;
2095 m_clock.Discontinuity(clock);
2096 m_CurrentAudio.syncState = IDVDStreamPlayer::SYNC_INSYNC;
2097 m_CurrentAudio.avsync = CCurrentStream::AV_SYNC_NONE;
2098 m_CurrentVideo.syncState = IDVDStreamPlayer::SYNC_INSYNC;
2099 m_CurrentVideo.avsync = CCurrentStream::AV_SYNC_NONE;
2100 m_VideoPlayerAudio->SendMessage(
2101 std::make_shared<CDVDMsgDouble>(CDVDMsg::GENERAL_RESYNC, clock), 1);
2102 m_VideoPlayerVideo->SendMessage(
2103 std::make_shared<CDVDMsgDouble>(CDVDMsg::GENERAL_RESYNC, clock), 1);
2104 SetCaching(CACHESTATE_DONE);
2105 UpdatePlayState(0);
2107 m_syncTimer.Set(3000ms);
2109 if (!m_State.streamsReady)
2111 if (m_playerOptions.fullscreen)
2113 CServiceBroker::GetAppMessenger()->PostMsg(TMSG_SWITCHTOFULLSCREEN);
2116 IPlayerCallback *cb = &m_callback;
2117 CFileItem fileItem = m_item;
2118 m_outboundEvents->Submit([=]() {
2119 cb->OnAVStarted(fileItem);
2121 m_State.streamsReady = true;
2124 else
2126 // exceptions for which stream players won't start properly
2127 // 1. videoplayer has not detected a keyframe within length of demux buffers
2128 if (m_CurrentAudio.id >= 0 && m_CurrentVideo.id >= 0 &&
2129 !m_VideoPlayerAudio->AcceptsData() &&
2130 m_CurrentVideo.syncState == IDVDStreamPlayer::SYNC_STARTING &&
2131 m_VideoPlayerVideo->IsStalled() &&
2132 m_CurrentVideo.packets > 10)
2134 m_VideoPlayerAudio->AcceptsData();
2135 CLog::Log(LOGWARNING, "VideoPlayer::Sync - stream player video does not start, flushing buffers");
2136 FlushBuffers(DVD_NOPTS_VALUE, true, true);
2141 // handle ff/rw
2142 if (m_playSpeed != DVD_PLAYSPEED_NORMAL && m_playSpeed != DVD_PLAYSPEED_PAUSE)
2144 if (isInMenu)
2146 // this can't be done in menu
2147 SetPlaySpeed(DVD_PLAYSPEED_NORMAL);
2150 else
2152 bool check = true;
2154 // only check if we have video
2155 if (m_CurrentVideo.id < 0 || m_CurrentVideo.syncState != IDVDStreamPlayer::SYNC_INSYNC)
2156 check = false;
2157 // video message queue either initiated or already seen eof
2158 else if (m_CurrentVideo.inited == false && m_playSpeed >= 0)
2159 check = false;
2160 // don't check if time has not advanced since last check
2161 else if (m_SpeedState.lasttime == GetTime())
2162 check = false;
2163 // skip if frame at screen has no valid timestamp
2164 else if (m_VideoPlayerVideo->GetCurrentPts() == DVD_NOPTS_VALUE)
2165 check = false;
2166 // skip if frame on screen has not changed
2167 else if (m_SpeedState.lastpts == m_VideoPlayerVideo->GetCurrentPts() &&
2168 (m_SpeedState.lastpts > m_State.dts || m_playSpeed > 0))
2169 check = false;
2171 if (check)
2173 m_SpeedState.lastpts = m_VideoPlayerVideo->GetCurrentPts();
2174 m_SpeedState.lasttime = GetTime();
2175 m_SpeedState.lastabstime = m_clock.GetAbsoluteClock();
2177 double error;
2178 error = m_clock.GetClock() - m_SpeedState.lastpts;
2179 error *= m_playSpeed / abs(m_playSpeed);
2181 // allow a bigger error when going ff, the faster we go
2182 // the the bigger is the error we allow
2183 if (m_playSpeed > DVD_PLAYSPEED_NORMAL)
2185 double errorwin = static_cast<double>(m_playSpeed) / DVD_PLAYSPEED_NORMAL;
2186 if (errorwin > 8.0)
2187 errorwin = 8.0;
2188 error /= errorwin;
2191 if (error > DVD_MSEC_TO_TIME(1000))
2193 error = (m_clock.GetClock() - m_SpeedState.lastseekpts) / 1000;
2195 if (std::abs(error) > 1000 || (m_VideoPlayerVideo->IsRewindStalled() && std::abs(error) > 100))
2197 CLog::Log(LOGDEBUG, "CVideoPlayer::Process - Seeking to catch up, error was: {:f}",
2198 error);
2199 m_SpeedState.lastseekpts = m_clock.GetClock();
2200 int direction = (m_playSpeed > 0) ? 1 : -1;
2201 double iTime = (m_clock.GetClock() + m_State.time_offset + 1000000.0 * direction) / 1000;
2202 CDVDMsgPlayerSeek::CMode mode;
2203 mode.time = iTime;
2204 mode.backward = (m_playSpeed < 0);
2205 mode.accurate = false;
2206 mode.restore = false;
2207 mode.trickplay = true;
2208 mode.sync = false;
2209 m_messenger.Put(std::make_shared<CDVDMsgPlayerSeek>(mode));
2216 // reset tempo
2217 if (!m_State.cantempo)
2219 float currentTempo = m_processInfo->GetNewTempo();
2220 if (currentTempo != 1.0f)
2222 SetTempo(1.0f);
2227 bool CVideoPlayer::CheckPlayerInit(CCurrentStream& current)
2229 if (current.inited)
2230 return false;
2232 if (current.startpts != DVD_NOPTS_VALUE)
2234 if(current.dts == DVD_NOPTS_VALUE)
2236 CLog::Log(LOGDEBUG, "{} - dropping packet type:{} dts:{:f} to get to start point at {:f}",
2237 __FUNCTION__, current.player, current.dts, current.startpts);
2238 return true;
2241 if ((current.startpts - current.dts) > DVD_SEC_TO_TIME(20))
2243 CLog::Log(LOGDEBUG, "{} - too far to decode before finishing seek", __FUNCTION__);
2244 if(m_CurrentAudio.startpts != DVD_NOPTS_VALUE)
2245 m_CurrentAudio.startpts = current.dts;
2246 if(m_CurrentVideo.startpts != DVD_NOPTS_VALUE)
2247 m_CurrentVideo.startpts = current.dts;
2248 if(m_CurrentSubtitle.startpts != DVD_NOPTS_VALUE)
2249 m_CurrentSubtitle.startpts = current.dts;
2250 if(m_CurrentTeletext.startpts != DVD_NOPTS_VALUE)
2251 m_CurrentTeletext.startpts = current.dts;
2252 if(m_CurrentRadioRDS.startpts != DVD_NOPTS_VALUE)
2253 m_CurrentRadioRDS.startpts = current.dts;
2254 if (m_CurrentAudioID3.startpts != DVD_NOPTS_VALUE)
2255 m_CurrentAudioID3.startpts = current.dts;
2258 if(current.dts < current.startpts)
2260 CLog::Log(LOGDEBUG, "{} - dropping packet type:{} dts:{:f} to get to start point at {:f}",
2261 __FUNCTION__, current.player, current.dts, current.startpts);
2262 return true;
2266 if (current.dts != DVD_NOPTS_VALUE)
2268 current.inited = true;
2269 current.startpts = current.dts;
2271 return false;
2274 void CVideoPlayer::UpdateCorrection(DemuxPacket* pkt, double correction)
2276 pkt->m_ptsOffsetCorrection = correction;
2278 if(pkt->dts != DVD_NOPTS_VALUE)
2279 pkt->dts -= correction;
2280 if(pkt->pts != DVD_NOPTS_VALUE)
2281 pkt->pts -= correction;
2284 void CVideoPlayer::UpdateTimestamps(CCurrentStream& current, DemuxPacket* pPacket)
2286 double dts = current.dts;
2287 /* update stored values */
2288 if(pPacket->dts != DVD_NOPTS_VALUE)
2289 dts = pPacket->dts;
2290 else if(pPacket->pts != DVD_NOPTS_VALUE)
2291 dts = pPacket->pts;
2293 /* calculate some average duration */
2294 if(pPacket->duration != DVD_NOPTS_VALUE)
2295 current.dur = pPacket->duration;
2296 else if(dts != DVD_NOPTS_VALUE && current.dts != DVD_NOPTS_VALUE)
2297 current.dur = 0.1 * (current.dur * 9 + (dts - current.dts));
2299 current.dts = dts;
2301 current.dispTime = pPacket->dispTime;
2304 static void UpdateLimits(double& minimum, double& maximum, double dts)
2306 if(dts == DVD_NOPTS_VALUE)
2307 return;
2308 if(minimum == DVD_NOPTS_VALUE || minimum > dts) minimum = dts;
2309 if(maximum == DVD_NOPTS_VALUE || maximum < dts) maximum = dts;
2312 bool CVideoPlayer::CheckContinuity(CCurrentStream& current, DemuxPacket* pPacket)
2314 if (m_playSpeed < DVD_PLAYSPEED_PAUSE)
2315 return false;
2317 if( pPacket->dts == DVD_NOPTS_VALUE || current.dts == DVD_NOPTS_VALUE)
2318 return false;
2320 double mindts = DVD_NOPTS_VALUE, maxdts = DVD_NOPTS_VALUE;
2321 UpdateLimits(mindts, maxdts, m_CurrentAudio.dts);
2322 UpdateLimits(mindts, maxdts, m_CurrentVideo.dts);
2323 UpdateLimits(mindts, maxdts, m_CurrentAudio.dts_end());
2324 UpdateLimits(mindts, maxdts, m_CurrentVideo.dts_end());
2326 /* if we don't have max and min, we can't do anything more */
2327 if( mindts == DVD_NOPTS_VALUE || maxdts == DVD_NOPTS_VALUE )
2328 return false;
2330 double correction = 0.0;
2331 if( pPacket->dts > maxdts + DVD_MSEC_TO_TIME(1000))
2333 CLog::Log(LOGDEBUG,
2334 "CVideoPlayer::CheckContinuity - resync forward :{}, prev:{:f}, curr:{:f}, diff:{:f}",
2335 current.type, current.dts, pPacket->dts, pPacket->dts - maxdts);
2336 correction = pPacket->dts - maxdts;
2339 /* if it's large scale jump, correct for it after having confirmed the jump */
2340 if(pPacket->dts + DVD_MSEC_TO_TIME(500) < current.dts_end())
2342 CLog::Log(
2343 LOGDEBUG,
2344 "CVideoPlayer::CheckContinuity - resync backward :{}, prev:{:f}, curr:{:f}, diff:{:f}",
2345 current.type, current.dts, pPacket->dts, pPacket->dts - current.dts);
2346 correction = pPacket->dts - current.dts_end();
2348 else if(pPacket->dts < current.dts)
2350 CLog::Log(LOGDEBUG,
2351 "CVideoPlayer::CheckContinuity - wrapback :{}, prev:{:f}, curr:{:f}, diff:{:f}",
2352 current.type, current.dts, pPacket->dts, pPacket->dts - current.dts);
2355 double lastdts = pPacket->dts;
2356 if(correction != 0.0)
2358 // we want the dts values of two streams to close, or for one to be invalid (e.g. from a missing audio stream)
2359 double this_dts = pPacket->dts;
2360 double that_dts = current.type == STREAM_AUDIO ? m_CurrentVideo.lastdts : m_CurrentAudio.lastdts;
2362 if (m_CurrentAudio.id == -1 || m_CurrentVideo.id == -1 ||
2363 current.lastdts == DVD_NOPTS_VALUE ||
2364 fabs(this_dts - that_dts) < DVD_MSEC_TO_TIME(1000))
2366 m_offset_pts += correction;
2367 UpdateCorrection(pPacket, correction);
2368 lastdts = pPacket->dts;
2369 CLog::Log(LOGDEBUG, "CVideoPlayer::CheckContinuity - update correction: {:f}", correction);
2370 if (current.avsync == CCurrentStream::AV_SYNC_CHECK)
2371 current.avsync = CCurrentStream::AV_SYNC_CONT;
2373 else
2375 // not sure yet - flags the packets as unknown until we get confirmation on another audio/video packet
2376 pPacket->dts = DVD_NOPTS_VALUE;
2377 pPacket->pts = DVD_NOPTS_VALUE;
2380 else
2382 if (current.avsync == CCurrentStream::AV_SYNC_CHECK)
2383 current.avsync = CCurrentStream::AV_SYNC_CONT;
2385 current.lastdts = lastdts;
2386 return true;
2389 bool CVideoPlayer::CheckSceneSkip(const CCurrentStream& current)
2391 if (!m_Edl.HasEdits())
2392 return false;
2394 if(current.dts == DVD_NOPTS_VALUE)
2395 return false;
2397 if(current.inited == false)
2398 return false;
2400 const auto hasEdit =
2401 m_Edl.InEdit(std::chrono::milliseconds(std::lround(current.dts + m_offset_pts)));
2402 return hasEdit && hasEdit.value()->action == EDL::Action::CUT;
2405 void CVideoPlayer::CheckAutoSceneSkip()
2407 if (!m_Edl.HasEdits())
2408 return;
2410 // Check that there is an audio and video stream.
2411 if((m_CurrentAudio.id < 0 || m_CurrentAudio.syncState != IDVDStreamPlayer::SYNC_INSYNC) ||
2412 (m_CurrentVideo.id < 0 || m_CurrentVideo.syncState != IDVDStreamPlayer::SYNC_INSYNC))
2413 return;
2415 // If there is a startpts defined for either the audio or video stream then VideoPlayer is still
2416 // still decoding frames to get to the previously requested seek point.
2417 if (m_CurrentAudio.inited == false ||
2418 m_CurrentVideo.inited == false)
2419 return;
2421 const std::chrono::milliseconds clock{GetTime()};
2423 const std::chrono::milliseconds correctClock = m_Edl.GetTimeAfterRestoringCuts(clock);
2424 const auto hasEdit = m_Edl.InEdit(correctClock);
2425 if (!hasEdit)
2427 // @note: Users are allowed to jump back into EDL commercial breaks
2428 // do not reset the last edit time if the last surpassed edit is a commercial break
2429 if (m_Edl.GetLastEditActionType() != EDL::Action::COMM_BREAK)
2431 m_Edl.ResetLastEditTime();
2433 return;
2436 const auto& edit = hasEdit.value();
2437 if (edit->action == EDL::Action::CUT)
2439 if ((m_playSpeed > 0 && correctClock < (edit->start + 1s)) ||
2440 (m_playSpeed < 0 && correctClock < (edit->end - 1s)))
2442 CLog::Log(LOGDEBUG, "{} - Clock in EDL cut [{} - {}]: {}. Automatically skipping over.",
2443 __FUNCTION__, CEdl::MillisecondsToTimeString(edit->start),
2444 CEdl::MillisecondsToTimeString(edit->end), CEdl::MillisecondsToTimeString(clock));
2446 // Seeking either goes to the start or the end of the cut depending on the play direction.
2447 std::chrono::milliseconds seek = m_playSpeed >= 0 ? edit->end : edit->start;
2448 if (m_Edl.GetLastEditTime() != seek)
2450 CDVDMsgPlayerSeek::CMode mode;
2451 mode.time = seek.count();
2452 mode.backward = true;
2453 mode.accurate = true;
2454 mode.restore = false;
2455 mode.trickplay = false;
2456 mode.sync = true;
2457 m_messenger.Put(std::make_shared<CDVDMsgPlayerSeek>(mode));
2459 m_Edl.SetLastEditTime(seek);
2460 m_Edl.SetLastEditActionType(edit->action);
2464 else if (edit->action == EDL::Action::COMM_BREAK)
2466 // marker for commbreak may be inaccurate. allow user to skip into break from the back
2467 if (m_playSpeed >= 0 && m_Edl.GetLastEditTime() != edit->start && clock < edit->end - 1s)
2469 const std::shared_ptr<CAdvancedSettings> advancedSettings =
2470 CServiceBroker::GetSettingsComponent()->GetAdvancedSettings();
2471 if (advancedSettings && advancedSettings->m_EdlDisplayCommbreakNotifications)
2473 const std::string timeString = StringUtils::SecondsToTimeString(
2474 std::chrono::duration_cast<std::chrono::seconds>(edit->end - edit->start).count(),
2475 TIME_FORMAT_MM_SS);
2476 CGUIDialogKaiToast::QueueNotification(g_localizeStrings.Get(25011), timeString);
2479 m_Edl.SetLastEditTime(edit->start);
2480 m_Edl.SetLastEditActionType(edit->action);
2482 if (m_SkipCommercials)
2484 CLog::Log(LOGDEBUG,
2485 "{} - Clock in commercial break [{} - {}]: {}. Automatically skipping to end of "
2486 "commercial break",
2487 __FUNCTION__, CEdl::MillisecondsToTimeString(edit->start),
2488 CEdl::MillisecondsToTimeString(edit->end), CEdl::MillisecondsToTimeString(clock));
2490 CDVDMsgPlayerSeek::CMode mode;
2491 mode.time = edit->end.count();
2492 mode.backward = true;
2493 mode.accurate = true;
2494 mode.restore = false;
2495 mode.trickplay = false;
2496 mode.sync = true;
2497 m_messenger.Put(std::make_shared<CDVDMsgPlayerSeek>(mode));
2504 void CVideoPlayer::SynchronizeDemuxer()
2506 if(IsCurrentThread())
2507 return;
2508 if(!m_messenger.IsInited())
2509 return;
2511 auto message = std::make_shared<CDVDMsgGeneralSynchronize>(500ms, SYNCSOURCE_PLAYER);
2512 m_messenger.Put(message);
2513 message->Wait(m_bStop, 0);
2516 IDVDStreamPlayer* CVideoPlayer::GetStreamPlayer(unsigned int target)
2518 if(target == VideoPlayer_AUDIO)
2519 return m_VideoPlayerAudio;
2520 if(target == VideoPlayer_VIDEO)
2521 return m_VideoPlayerVideo;
2522 if(target == VideoPlayer_SUBTITLE)
2523 return m_VideoPlayerSubtitle;
2524 if(target == VideoPlayer_TELETEXT)
2525 return m_VideoPlayerTeletext;
2526 if(target == VideoPlayer_RDS)
2527 return m_VideoPlayerRadioRDS;
2528 if (target == VideoPlayer_ID3)
2529 return m_VideoPlayerAudioID3.get();
2530 return NULL;
2533 void CVideoPlayer::SendPlayerMessage(std::shared_ptr<CDVDMsg> pMsg, unsigned int target)
2535 IDVDStreamPlayer* player = GetStreamPlayer(target);
2536 if(player)
2537 player->SendMessage(std::move(pMsg), 0);
2540 void CVideoPlayer::OnExit()
2542 CLog::Log(LOGINFO, "CVideoPlayer::OnExit()");
2544 // set event to inform openfile something went wrong in case openfile is still waiting for this event
2545 SetCaching(CACHESTATE_DONE);
2547 // close each stream
2548 if (!m_bAbortRequest)
2549 CLog::Log(LOGINFO, "VideoPlayer: eof, waiting for queues to empty");
2551 CFileItem fileItem(m_item);
2552 UpdateFileItemStreamDetails(fileItem);
2554 CloseStream(m_CurrentAudio, !m_bAbortRequest);
2555 CloseStream(m_CurrentVideo, !m_bAbortRequest);
2556 CloseStream(m_CurrentTeletext,!m_bAbortRequest);
2557 CloseStream(m_CurrentRadioRDS, !m_bAbortRequest);
2558 CloseStream(m_CurrentAudioID3, !m_bAbortRequest);
2559 // the generalization principle was abused for subtitle player. actually it is not a stream player like
2560 // video and audio. subtitle player does not run on its own thread, hence waitForBuffers makes
2561 // no sense here. waitForBuffers is abused to clear overlay container (false clears container)
2562 // subtitles are added from video player. after video player has finished, overlays have to be cleared.
2563 CloseStream(m_CurrentSubtitle, false); // clear overlay container
2565 CServiceBroker::GetWinSystem()->UnregisterRenderLoop(this);
2567 IPlayerCallback *cb = &m_callback;
2568 CVideoSettings vs = m_processInfo->GetVideoSettings();
2569 m_outboundEvents->Submit([=]() {
2570 cb->StoreVideoSettings(fileItem, vs);
2573 CBookmark bookmark;
2574 bookmark.totalTimeInSeconds = 0;
2575 bookmark.timeInSeconds = 0;
2576 if (m_State.startTime == 0)
2578 bookmark.totalTimeInSeconds = m_State.timeMax / 1000;
2579 bookmark.timeInSeconds = m_State.time / 1000;
2581 bookmark.player = m_name;
2582 bookmark.playerState = GetPlayerState();
2583 m_outboundEvents->Submit([=]() {
2584 cb->OnPlayerCloseFile(fileItem, bookmark);
2587 // destroy objects
2588 m_renderManager.Flush(false, false);
2589 m_pDemuxer.reset();
2590 m_pSubtitleDemuxer.reset();
2591 m_subtitleDemuxerMap.clear();
2592 m_pCCDemuxer.reset();
2593 if (m_pInputStream.use_count() > 1)
2594 throw std::runtime_error("m_pInputStream reference count is greater than 1");
2595 m_pInputStream.reset();
2597 // clean up all selection streams
2598 m_SelectionStreams.Clear(STREAM_NONE, STREAM_SOURCE_NONE);
2600 m_messenger.End();
2602 CFFmpegLog::ClearLogLevel();
2603 m_bStop = true;
2605 bool error = m_error;
2606 bool close = m_bCloseRequest;
2607 m_outboundEvents->Submit([=]() {
2608 if (close)
2609 cb->OnPlayBackStopped();
2610 else if (error)
2611 cb->OnPlayBackError();
2612 else
2613 cb->OnPlayBackEnded();
2617 void CVideoPlayer::HandleMessages()
2619 std::shared_ptr<CDVDMsg> pMsg = nullptr;
2621 while (m_messenger.Get(pMsg, 0ms) == MSGQ_OK)
2623 if (pMsg->IsType(CDVDMsg::PLAYER_OPENFILE) &&
2624 m_messenger.GetPacketCount(CDVDMsg::PLAYER_OPENFILE) == 0)
2626 CDVDMsgOpenFile& msg(*std::static_pointer_cast<CDVDMsgOpenFile>(pMsg));
2628 IPlayerCallback *cb = &m_callback;
2629 CFileItem fileItem(m_item);
2630 UpdateFileItemStreamDetails(fileItem);
2631 CVideoSettings vs = m_processInfo->GetVideoSettings();
2632 m_outboundEvents->Submit([=]() {
2633 cb->StoreVideoSettings(fileItem, vs);
2636 CBookmark bookmark;
2637 bookmark.totalTimeInSeconds = 0;
2638 bookmark.timeInSeconds = 0;
2639 if (m_State.startTime == 0)
2641 bookmark.totalTimeInSeconds = m_State.timeMax / 1000;
2642 bookmark.timeInSeconds = m_State.time / 1000;
2644 bookmark.player = m_name;
2645 bookmark.playerState = GetPlayerState();
2646 m_outboundEvents->Submit([=]() {
2647 cb->OnPlayerCloseFile(fileItem, bookmark);
2650 m_item = msg.GetItem();
2651 m_playerOptions = msg.GetOptions();
2653 m_processInfo->SetPlayTimes(0,0,0,0);
2655 m_outboundEvents->Submit([this]() {
2656 m_callback.OnPlayBackStarted(m_item);
2659 FlushBuffers(DVD_NOPTS_VALUE, true, true);
2660 m_renderManager.Flush(false, false);
2661 m_pDemuxer.reset();
2662 m_pSubtitleDemuxer.reset();
2663 m_subtitleDemuxerMap.clear();
2664 m_pCCDemuxer.reset();
2665 if (m_pInputStream.use_count() > 1)
2666 throw std::runtime_error("m_pInputStream reference count is greater than 1");
2667 m_pInputStream.reset();
2669 m_SelectionStreams.Clear(STREAM_NONE, STREAM_SOURCE_NONE);
2671 Prepare();
2673 else if (pMsg->IsType(CDVDMsg::PLAYER_SEEK) &&
2674 m_messenger.GetPacketCount(CDVDMsg::PLAYER_SEEK) == 0 &&
2675 m_messenger.GetPacketCount(CDVDMsg::PLAYER_SEEK_CHAPTER) == 0)
2677 CDVDMsgPlayerSeek& msg(*std::static_pointer_cast<CDVDMsgPlayerSeek>(pMsg));
2679 if (!m_State.canseek)
2681 m_processInfo->SetStateSeeking(false);
2682 continue;
2685 // skip seeks if player has not finished the last seek
2686 if (m_CurrentVideo.id >= 0 &&
2687 m_CurrentVideo.syncState != IDVDStreamPlayer::SYNC_INSYNC)
2689 double now = m_clock.GetAbsoluteClock();
2690 if (m_playSpeed == DVD_PLAYSPEED_NORMAL &&
2691 (now - m_State.lastSeek)/1000 < 2000 &&
2692 !msg.GetAccurate())
2694 m_processInfo->SetStateSeeking(false);
2695 continue;
2699 if (!msg.GetTrickPlay())
2701 m_processInfo->SeekFinished(0);
2702 SetCaching(CACHESTATE_FLUSH);
2705 double start = DVD_NOPTS_VALUE;
2707 double time = msg.GetTime();
2708 if (msg.GetRelative())
2709 time = (m_clock.GetClock() + m_State.time_offset) / 1000l + time;
2711 time = msg.GetRestore()
2712 ? m_Edl.GetTimeAfterRestoringCuts(std::chrono::milliseconds(std::lround(time)))
2713 .count()
2714 : time;
2716 // if input stream doesn't support ISeekTime, convert back to pts
2717 //! @todo
2718 //! After demuxer we add an offset to input pts so that displayed time and clock are
2719 //! increasing steadily. For seeking we need to determine the boundaries and offset
2720 //! of the desired segment. With the current approach calculated time may point
2721 //! to nirvana
2722 if (m_pInputStream->GetIPosTime() == nullptr)
2723 time -= m_State.time_offset/1000l;
2725 CLog::Log(LOGDEBUG, "demuxer seek to: {:f}", time);
2726 if (m_pDemuxer && m_pDemuxer->SeekTime(time, msg.GetBackward(), &start))
2728 CLog::Log(LOGDEBUG, "demuxer seek to: {:f}, success", time);
2729 if(m_pSubtitleDemuxer)
2731 if(!m_pSubtitleDemuxer->SeekTime(time, msg.GetBackward()))
2732 CLog::Log(LOGDEBUG, "failed to seek subtitle demuxer: {:f}, success", time);
2734 // dts after successful seek
2735 if (start == DVD_NOPTS_VALUE)
2736 start = DVD_MSEC_TO_TIME(time) - m_State.time_offset;
2738 m_State.dts = start;
2739 m_State.lastSeek = m_clock.GetAbsoluteClock();
2741 FlushBuffers(start, msg.GetAccurate(), msg.GetSync());
2743 else if (m_pDemuxer)
2745 CLog::Log(LOGDEBUG, "VideoPlayer: seek failed or hit end of stream");
2746 // dts after successful seek
2747 if (start == DVD_NOPTS_VALUE)
2748 start = DVD_MSEC_TO_TIME(time) - m_State.time_offset;
2750 m_State.dts = start;
2752 FlushBuffers(start, false, true);
2753 if (m_playSpeed != DVD_PLAYSPEED_PAUSE)
2755 SetPlaySpeed(DVD_PLAYSPEED_NORMAL);
2759 // set flag to indicate we have finished a seeking request
2760 if(!msg.GetTrickPlay())
2762 m_processInfo->SeekFinished(0);
2765 // dvd's will issue a HOP_CHANNEL that we need to skip
2766 if(m_pInputStream->IsStreamType(DVDSTREAM_TYPE_DVD))
2767 m_dvd.state = DVDSTATE_SEEK;
2769 m_processInfo->SetStateSeeking(false);
2771 else if (pMsg->IsType(CDVDMsg::PLAYER_SEEK_CHAPTER) &&
2772 m_messenger.GetPacketCount(CDVDMsg::PLAYER_SEEK) == 0 &&
2773 m_messenger.GetPacketCount(CDVDMsg::PLAYER_SEEK_CHAPTER) == 0)
2775 m_processInfo->SeekFinished(0);
2776 SetCaching(CACHESTATE_FLUSH);
2778 CDVDMsgPlayerSeekChapter& msg(*std::static_pointer_cast<CDVDMsgPlayerSeekChapter>(pMsg));
2779 double start = DVD_NOPTS_VALUE;
2780 int offset = 0;
2782 // This should always be the case.
2783 if(m_pDemuxer && m_pDemuxer->SeekChapter(msg.GetChapter(), &start))
2785 FlushBuffers(start, true, true);
2786 int64_t beforeSeek = GetTime();
2787 offset = DVD_TIME_TO_MSEC(start) - static_cast<int>(beforeSeek);
2788 m_callback.OnPlayBackSeekChapter(msg.GetChapter());
2790 else if (m_pInputStream)
2792 CDVDInputStream::IChapter* pChapter = m_pInputStream->GetIChapter();
2793 if (pChapter && pChapter->SeekChapter(msg.GetChapter()))
2795 FlushBuffers(start, true, true);
2796 int64_t beforeSeek = GetTime();
2797 offset = DVD_TIME_TO_MSEC(start) - static_cast<int>(beforeSeek);
2798 m_callback.OnPlayBackSeekChapter(msg.GetChapter());
2801 m_processInfo->SeekFinished(offset);
2803 else if (pMsg->IsType(CDVDMsg::DEMUXER_RESET))
2805 m_CurrentAudio.stream = NULL;
2806 m_CurrentVideo.stream = NULL;
2807 m_CurrentSubtitle.stream = NULL;
2809 // we need to reset the demuxer, probably because the streams have changed
2810 if(m_pDemuxer)
2811 m_pDemuxer->Reset();
2812 if(m_pSubtitleDemuxer)
2813 m_pSubtitleDemuxer->Reset();
2815 else if (pMsg->IsType(CDVDMsg::PLAYER_SET_AUDIOSTREAM))
2817 auto pMsg2 = std::static_pointer_cast<CDVDMsgPlayerSetAudioStream>(pMsg);
2819 SelectionStream& st = m_SelectionStreams.Get(STREAM_AUDIO, pMsg2->GetStreamId());
2820 if(st.source != STREAM_SOURCE_NONE)
2822 if(st.source == STREAM_SOURCE_NAV && m_pInputStream && m_pInputStream->IsStreamType(DVDSTREAM_TYPE_DVD))
2824 std::shared_ptr<CDVDInputStreamNavigator> pStream = std::static_pointer_cast<CDVDInputStreamNavigator>(m_pInputStream);
2825 if(pStream->SetActiveAudioStream(st.id))
2827 m_dvd.iSelectedAudioStream = -1;
2828 CloseStream(m_CurrentAudio, false);
2829 CDVDMsgPlayerSeek::CMode mode;
2830 mode.time = (int)GetUpdatedTime();
2831 mode.backward = true;
2832 mode.accurate = true;
2833 mode.trickplay = true;
2834 mode.sync = true;
2835 m_messenger.Put(std::make_shared<CDVDMsgPlayerSeek>(mode));
2838 else
2840 CloseStream(m_CurrentAudio, false);
2841 OpenStream(m_CurrentAudio, st.demuxerId, st.id, st.source);
2842 AdaptForcedSubtitles();
2844 CDVDMsgPlayerSeek::CMode mode;
2845 mode.time = (int)GetUpdatedTime();
2846 mode.backward = true;
2847 mode.accurate = true;
2848 mode.trickplay = true;
2849 mode.sync = true;
2850 m_messenger.Put(std::make_shared<CDVDMsgPlayerSeek>(mode));
2854 else if (pMsg->IsType(CDVDMsg::PLAYER_SET_VIDEOSTREAM))
2856 auto pMsg2 = std::static_pointer_cast<CDVDMsgPlayerSetVideoStream>(pMsg);
2858 SelectionStream& st = m_SelectionStreams.Get(STREAM_VIDEO, pMsg2->GetStreamId());
2859 if (st.source != STREAM_SOURCE_NONE)
2861 if (st.source == STREAM_SOURCE_NAV && m_pInputStream && m_pInputStream->IsStreamType(DVDSTREAM_TYPE_DVD))
2863 std::shared_ptr<CDVDInputStreamNavigator> pStream = std::static_pointer_cast<CDVDInputStreamNavigator>(m_pInputStream);
2864 if (pStream->SetAngle(st.id))
2866 m_dvd.iSelectedVideoStream = st.id;
2868 CDVDMsgPlayerSeek::CMode mode;
2869 mode.time = (int)GetUpdatedTime();
2870 mode.backward = true;
2871 mode.accurate = true;
2872 mode.trickplay = true;
2873 mode.sync = true;
2874 m_messenger.Put(std::make_shared<CDVDMsgPlayerSeek>(mode));
2877 else
2879 CloseStream(m_CurrentVideo, false);
2880 OpenStream(m_CurrentVideo, st.demuxerId, st.id, st.source);
2881 CDVDMsgPlayerSeek::CMode mode;
2882 mode.time = (int)GetUpdatedTime();
2883 mode.backward = true;
2884 mode.accurate = true;
2885 mode.trickplay = true;
2886 mode.sync = true;
2887 m_messenger.Put(std::make_shared<CDVDMsgPlayerSeek>(mode));
2891 else if (pMsg->IsType(CDVDMsg::PLAYER_SET_SUBTITLESTREAM))
2893 auto pMsg2 = std::static_pointer_cast<CDVDMsgPlayerSetSubtitleStream>(pMsg);
2895 SelectionStream& st = m_SelectionStreams.Get(STREAM_SUBTITLE, pMsg2->GetStreamId());
2896 if(st.source != STREAM_SOURCE_NONE)
2898 if(st.source == STREAM_SOURCE_NAV && m_pInputStream && m_pInputStream->IsStreamType(DVDSTREAM_TYPE_DVD))
2900 std::shared_ptr<CDVDInputStreamNavigator> pStream = std::static_pointer_cast<CDVDInputStreamNavigator>(m_pInputStream);
2901 if(pStream->SetActiveSubtitleStream(st.id))
2903 m_dvd.iSelectedSPUStream = -1;
2904 CloseStream(m_CurrentSubtitle, false);
2907 else
2909 CloseStream(m_CurrentSubtitle, false);
2910 OpenStream(m_CurrentSubtitle, st.demuxerId, st.id, st.source);
2914 else if (pMsg->IsType(CDVDMsg::PLAYER_SET_SUBTITLESTREAM_VISIBLE))
2916 bool isVisible = std::static_pointer_cast<CDVDMsgBool>(pMsg)->m_value;
2918 // SetEnableStream only if not visible, when visible OpenStream already implied that stream is enabled
2919 if (!isVisible)
2920 SetEnableStream(m_CurrentSubtitle, false);
2922 SetSubtitleVisibleInternal(isVisible);
2924 else if (pMsg->IsType(CDVDMsg::PLAYER_SET_PROGRAM))
2926 auto msg = std::static_pointer_cast<CDVDMsgInt>(pMsg);
2927 if (m_pDemuxer)
2929 m_pDemuxer->SetProgram(msg->m_value);
2930 FlushBuffers(DVD_NOPTS_VALUE, false, true);
2933 else if (pMsg->IsType(CDVDMsg::PLAYER_SET_STATE))
2935 SetCaching(CACHESTATE_FLUSH);
2937 auto pMsgPlayerSetState = std::static_pointer_cast<CDVDMsgPlayerSetState>(pMsg);
2939 if (std::shared_ptr<CDVDInputStream::IMenus> ptr = std::dynamic_pointer_cast<CDVDInputStream::IMenus>(m_pInputStream))
2941 if(ptr->SetState(pMsgPlayerSetState->GetState()))
2943 m_dvd.state = DVDSTATE_NORMAL;
2944 m_dvd.iDVDStillStartTime = {};
2945 m_dvd.iDVDStillTime = 0ms;
2949 m_processInfo->SeekFinished(0);
2951 else if (pMsg->IsType(CDVDMsg::GENERAL_FLUSH))
2953 FlushBuffers(DVD_NOPTS_VALUE, true, true);
2955 else if (pMsg->IsType(CDVDMsg::PLAYER_SETSPEED))
2957 int speed = std::static_pointer_cast<CDVDMsgPlayerSetSpeed>(pMsg)->GetSpeed();
2959 // correct our current clock, as it would start going wrong otherwise
2960 if (m_State.timestamp > 0)
2962 double offset;
2963 offset = m_clock.GetAbsoluteClock() - m_State.timestamp;
2964 offset *= m_playSpeed / DVD_PLAYSPEED_NORMAL;
2965 offset = DVD_TIME_TO_MSEC(offset);
2966 if (offset > 1000)
2967 offset = 1000;
2968 if (offset < -1000)
2969 offset = -1000;
2970 m_State.time += offset;
2971 m_State.timestamp = m_clock.GetAbsoluteClock();
2974 if (speed != DVD_PLAYSPEED_PAUSE && m_playSpeed != DVD_PLAYSPEED_PAUSE && speed != m_playSpeed)
2976 m_callback.OnPlayBackSpeedChanged(speed / DVD_PLAYSPEED_NORMAL);
2977 m_processInfo->SeekFinished(0);
2980 if (m_pInputStream->IsStreamType(DVDSTREAM_TYPE_PVRMANAGER) && speed != m_playSpeed)
2982 std::shared_ptr<CInputStreamPVRBase> pvrinputstream = std::static_pointer_cast<CInputStreamPVRBase>(m_pInputStream);
2983 pvrinputstream->Pause(speed == 0);
2986 // do a seek after rewind, clock is not in sync with current pts
2987 if ((speed == DVD_PLAYSPEED_NORMAL) &&
2988 (m_playSpeed != DVD_PLAYSPEED_NORMAL) &&
2989 (m_playSpeed != DVD_PLAYSPEED_PAUSE))
2991 double iTime = m_VideoPlayerVideo->GetCurrentPts();
2992 if (iTime == DVD_NOPTS_VALUE)
2993 iTime = m_clock.GetClock();
2994 iTime = (iTime + m_State.time_offset) / 1000;
2996 CDVDMsgPlayerSeek::CMode mode;
2997 mode.time = iTime;
2998 mode.backward = m_playSpeed < 0;
2999 mode.accurate = true;
3000 mode.trickplay = true;
3001 mode.sync = true;
3002 mode.restore = false;
3003 m_messenger.Put(std::make_shared<CDVDMsgPlayerSeek>(mode));
3006 if (std::static_pointer_cast<CDVDMsgPlayerSetSpeed>(pMsg)->IsTempo())
3007 m_processInfo->SetTempo(static_cast<float>(speed) / DVD_PLAYSPEED_NORMAL);
3008 else
3009 m_processInfo->SetSpeed(static_cast<float>(speed) / DVD_PLAYSPEED_NORMAL);
3011 m_processInfo->SetFrameAdvance(false);
3013 m_playSpeed = speed;
3015 m_caching = CACHESTATE_DONE;
3016 m_clock.SetSpeed(speed);
3017 m_VideoPlayerAudio->SetSpeed(speed);
3018 m_VideoPlayerVideo->SetSpeed(speed);
3019 m_streamPlayerSpeed = speed;
3021 else if (pMsg->IsType(CDVDMsg::PLAYER_FRAME_ADVANCE))
3023 if (m_playSpeed == DVD_PLAYSPEED_PAUSE)
3025 int frames = std::static_pointer_cast<CDVDMsgInt>(pMsg)->m_value;
3026 double time = DVD_TIME_BASE / static_cast<double>(m_processInfo->GetVideoFps()) * frames;
3027 m_processInfo->SetFrameAdvance(true);
3028 m_clock.Advance(time);
3031 else if (pMsg->IsType(CDVDMsg::GENERAL_GUI_ACTION))
3032 OnAction(std::static_pointer_cast<CDVDMsgType<CAction>>(pMsg)->m_value);
3033 else if (pMsg->IsType(CDVDMsg::PLAYER_STARTED))
3035 SStartMsg& msg = std::static_pointer_cast<CDVDMsgType<SStartMsg>>(pMsg)->m_value;
3036 if (msg.player == VideoPlayer_AUDIO)
3038 m_CurrentAudio.syncState = IDVDStreamPlayer::SYNC_WAITSYNC;
3039 m_CurrentAudio.cachetime = msg.cachetime;
3040 m_CurrentAudio.cachetotal = msg.cachetotal;
3041 m_CurrentAudio.starttime = msg.timestamp;
3043 if (msg.player == VideoPlayer_VIDEO)
3045 m_CurrentVideo.syncState = IDVDStreamPlayer::SYNC_WAITSYNC;
3046 m_CurrentVideo.cachetime = msg.cachetime;
3047 m_CurrentVideo.cachetotal = msg.cachetotal;
3048 m_CurrentVideo.starttime = msg.timestamp;
3050 CLog::Log(LOGDEBUG, "CVideoPlayer::HandleMessages - player started {}", msg.player);
3052 else if (pMsg->IsType(CDVDMsg::PLAYER_REPORT_STATE))
3054 SStateMsg& msg = std::static_pointer_cast<CDVDMsgType<SStateMsg>>(pMsg)->m_value;
3055 if (msg.player == VideoPlayer_AUDIO)
3057 m_CurrentAudio.syncState = msg.syncState;
3059 if (msg.player == VideoPlayer_VIDEO)
3061 m_CurrentVideo.syncState = msg.syncState;
3063 CLog::Log(LOGDEBUG, "CVideoPlayer::HandleMessages - player {} reported state: {}", msg.player,
3064 msg.syncState);
3066 else if (pMsg->IsType(CDVDMsg::SUBTITLE_ADDFILE))
3068 int id = AddSubtitleFile(std::static_pointer_cast<CDVDMsgType<std::string>>(pMsg)->m_value);
3069 if (id >= 0)
3071 SetSubtitle(id);
3072 SetSubtitleVisibleInternal(true);
3075 else if (pMsg->IsType(CDVDMsg::GENERAL_SYNCHRONIZE))
3077 if (std::static_pointer_cast<CDVDMsgGeneralSynchronize>(pMsg)->Wait(100ms, SYNCSOURCE_PLAYER))
3078 CLog::Log(LOGDEBUG, "CVideoPlayer - CDVDMsg::GENERAL_SYNCHRONIZE");
3080 else if (pMsg->IsType(CDVDMsg::PLAYER_AVCHANGE))
3082 CServiceBroker::GetDataCacheCore().SignalAudioInfoChange();
3083 CServiceBroker::GetDataCacheCore().SignalVideoInfoChange();
3084 CServiceBroker::GetDataCacheCore().SignalSubtitleInfoChange();
3085 IPlayerCallback *cb = &m_callback;
3086 m_outboundEvents->Submit([=]() {
3087 cb->OnAVChange();
3090 else if (pMsg->IsType(CDVDMsg::PLAYER_ABORT))
3092 CLog::Log(LOGDEBUG, "CVideoPlayer - CDVDMsg::PLAYER_ABORT");
3093 m_bAbortRequest = true;
3095 else if (pMsg->IsType(CDVDMsg::PLAYER_SET_UPDATE_STREAM_DETAILS))
3096 m_UpdateStreamDetails = true;
3100 void CVideoPlayer::SetCaching(ECacheState state)
3102 if(state == CACHESTATE_FLUSH)
3104 CacheInfo cache = GetCachingTimes();
3105 if (cache.valid)
3106 state = CACHESTATE_FULL;
3107 else
3108 state = CACHESTATE_INIT;
3111 if(m_caching == state)
3112 return;
3114 CLog::Log(LOGDEBUG, "CVideoPlayer::SetCaching - caching state {}", state);
3115 if (state == CACHESTATE_FULL ||
3116 state == CACHESTATE_INIT)
3118 m_clock.SetSpeed(DVD_PLAYSPEED_PAUSE);
3120 m_VideoPlayerAudio->SetSpeed(DVD_PLAYSPEED_PAUSE);
3121 m_VideoPlayerVideo->SetSpeed(DVD_PLAYSPEED_PAUSE);
3122 m_streamPlayerSpeed = DVD_PLAYSPEED_PAUSE;
3124 m_cachingTimer.Set(5000ms);
3127 if (state == CACHESTATE_PLAY ||
3128 (state == CACHESTATE_DONE && m_caching != CACHESTATE_PLAY))
3130 m_clock.SetSpeed(m_playSpeed);
3131 m_VideoPlayerAudio->SetSpeed(m_playSpeed);
3132 m_VideoPlayerVideo->SetSpeed(m_playSpeed);
3133 m_streamPlayerSpeed = m_playSpeed;
3135 m_caching = state;
3137 m_clock.SetSpeedAdjust(0);
3140 void CVideoPlayer::SetPlaySpeed(int speed)
3142 if (IsPlaying())
3144 CDVDMsgPlayerSetSpeed::SpeedParams params = { speed, false };
3145 m_messenger.Put(std::make_shared<CDVDMsgPlayerSetSpeed>(params));
3147 else
3149 m_playSpeed = speed;
3150 m_streamPlayerSpeed = speed;
3154 bool CVideoPlayer::CanPause() const
3156 std::unique_lock<CCriticalSection> lock(m_StateSection);
3157 return m_State.canpause;
3160 void CVideoPlayer::Pause()
3162 // toggle between pause and normal speed
3163 if (m_processInfo->GetNewSpeed() == 0)
3165 SetSpeed(1);
3167 else
3169 SetSpeed(0);
3173 bool CVideoPlayer::HasVideo() const
3175 return m_HasVideo;
3178 bool CVideoPlayer::HasAudio() const
3180 return m_HasAudio;
3183 bool CVideoPlayer::HasRDS() const
3185 return m_CurrentRadioRDS.id >= 0;
3188 bool CVideoPlayer::HasID3() const
3190 return m_CurrentAudioID3.id >= 0;
3193 bool CVideoPlayer::IsPassthrough() const
3195 return m_VideoPlayerAudio->IsPassthrough();
3198 bool CVideoPlayer::CanSeek() const
3200 std::unique_lock<CCriticalSection> lock(m_StateSection);
3201 return m_State.canseek;
3204 void CVideoPlayer::Seek(bool bPlus, bool bLargeStep, bool bChapterOverride)
3206 if (!m_State.canseek)
3207 return;
3209 if (bLargeStep && bChapterOverride && GetChapter() > 0 && GetChapterCount() > 1)
3211 if (!bPlus)
3213 SeekChapter(GetPreviousChapter());
3214 return;
3216 else if (GetChapter() < GetChapterCount())
3218 SeekChapter(GetChapter() + 1);
3219 return;
3223 int64_t seekTarget;
3224 const std::shared_ptr<CAdvancedSettings> advancedSettings = CServiceBroker::GetSettingsComponent()->GetAdvancedSettings();
3225 if (advancedSettings->m_videoUseTimeSeeking && m_processInfo->GetMaxTime() > 2000 * advancedSettings->m_videoTimeSeekForwardBig)
3227 if (bLargeStep)
3228 seekTarget = bPlus ? advancedSettings->m_videoTimeSeekForwardBig :
3229 advancedSettings->m_videoTimeSeekBackwardBig;
3230 else
3231 seekTarget = bPlus ? advancedSettings->m_videoTimeSeekForward :
3232 advancedSettings->m_videoTimeSeekBackward;
3233 seekTarget *= 1000;
3234 seekTarget += GetTime();
3236 else
3238 int percent;
3239 if (bLargeStep)
3240 percent = bPlus ? advancedSettings->m_videoPercentSeekForwardBig : advancedSettings->m_videoPercentSeekBackwardBig;
3241 else
3242 percent = bPlus ? advancedSettings->m_videoPercentSeekForward : advancedSettings->m_videoPercentSeekBackward;
3243 seekTarget = static_cast<int64_t>(m_processInfo->GetMaxTime() * (GetPercentage() + percent) / 100);
3246 bool restore = true;
3248 int64_t time = GetTime();
3249 if(g_application.CurrentFileItem().IsStack() &&
3250 (seekTarget > m_processInfo->GetMaxTime() || seekTarget < 0))
3252 g_application.SeekTime((seekTarget - time) * 0.001 + g_application.GetTime());
3253 // warning, don't access any VideoPlayer variables here as
3254 // the VideoPlayer object may have been destroyed
3255 return;
3258 CDVDMsgPlayerSeek::CMode mode;
3259 mode.time = (int)seekTarget;
3260 mode.backward = !bPlus;
3261 mode.accurate = false;
3262 mode.restore = restore;
3263 mode.trickplay = false;
3264 mode.sync = true;
3266 m_messenger.Put(std::make_shared<CDVDMsgPlayerSeek>(mode));
3267 SynchronizeDemuxer();
3268 if (seekTarget < 0)
3269 seekTarget = 0;
3270 m_callback.OnPlayBackSeek(seekTarget, seekTarget - time);
3273 bool CVideoPlayer::SeekScene(Direction seekDirection)
3275 if (!m_Edl.HasSceneMarker())
3276 return false;
3279 * There is a 5 second grace period applied when seeking for scenes backwards. If there is no
3280 * grace period applied it is impossible to go backwards past a scene marker.
3282 auto clock = std::chrono::milliseconds(GetTime());
3283 if (seekDirection == Direction::BACKWARD && clock > 5s) // 5 seconds
3284 clock -= 5s;
3286 const std::optional<std::chrono::milliseconds> sceneMarker =
3287 m_Edl.GetNextSceneMarker(seekDirection, clock);
3288 if (sceneMarker)
3291 * Seeking is flushed and inaccurate, just like Seek()
3293 CDVDMsgPlayerSeek::CMode mode;
3294 mode.time = sceneMarker.value().count();
3295 mode.backward = seekDirection == Direction::BACKWARD;
3296 mode.accurate = false;
3297 mode.restore = false;
3298 mode.trickplay = false;
3299 mode.sync = true;
3301 m_messenger.Put(std::make_shared<CDVDMsgPlayerSeek>(mode));
3302 SynchronizeDemuxer();
3303 return true;
3305 return false;
3308 void CVideoPlayer::GetGeneralInfo(std::string& strGeneralInfo)
3310 if (!m_bStop)
3312 double apts = m_VideoPlayerAudio->GetCurrentPts();
3313 double vpts = m_VideoPlayerVideo->GetCurrentPts();
3314 double dDiff = 0;
3316 if (apts != DVD_NOPTS_VALUE && vpts != DVD_NOPTS_VALUE)
3317 dDiff = (apts - vpts) / DVD_TIME_BASE;
3319 std::string strBuf;
3320 std::unique_lock<CCriticalSection> lock(m_StateSection);
3321 if (m_State.cache_bytes >= 0)
3323 strBuf += StringUtils::Format("forward: {} / {:2.0f}% / {:6.3f}s / {:.3f}%",
3324 StringUtils::SizeToString(m_State.cache_bytes),
3325 m_State.cache_level * 100.0, m_State.cache_time,
3326 m_State.cache_offset * 100.0);
3329 strGeneralInfo = StringUtils::Format("Player: a/v:{: 6.3f}, {}", dDiff, strBuf);
3333 void CVideoPlayer::SeekPercentage(float iPercent)
3335 int64_t iTotalTime = m_processInfo->GetMaxTime();
3337 if (!iTotalTime)
3338 return;
3340 SeekTime((int64_t)(iTotalTime * iPercent / 100));
3343 float CVideoPlayer::GetPercentage()
3345 int64_t iTotalTime = m_processInfo->GetMaxTime();
3347 if (!iTotalTime)
3348 return 0.0f;
3350 return GetTime() * 100 / (float)iTotalTime;
3353 float CVideoPlayer::GetCachePercentage() const
3355 std::unique_lock<CCriticalSection> lock(m_StateSection);
3356 return (float) (m_State.cache_offset * 100); // NOTE: Percentage returned is relative
3359 void CVideoPlayer::SetAVDelay(float fValue)
3361 m_processInfo->GetVideoSettingsLocked().SetAudioDelay(fValue);
3362 m_renderManager.SetDelay(static_cast<int>(fValue * 1000.0f));
3365 float CVideoPlayer::GetAVDelay()
3367 return static_cast<float>(m_renderManager.GetDelay()) / 1000.0f;
3370 void CVideoPlayer::SetSubTitleDelay(float fValue)
3372 m_processInfo->GetVideoSettingsLocked().SetSubtitleDelay(fValue);
3373 m_VideoPlayerVideo->SetSubtitleDelay(static_cast<double>(-fValue) * DVD_TIME_BASE);
3376 float CVideoPlayer::GetSubTitleDelay()
3378 return (float) -m_VideoPlayerVideo->GetSubtitleDelay() / DVD_TIME_BASE;
3381 bool CVideoPlayer::GetSubtitleVisible() const
3383 if (m_pInputStream && m_pInputStream->IsStreamType(DVDSTREAM_TYPE_DVD))
3385 std::shared_ptr<CDVDInputStreamNavigator> pStream = std::static_pointer_cast<CDVDInputStreamNavigator>(m_pInputStream);
3386 return pStream->IsSubtitleStreamEnabled();
3389 return m_VideoPlayerVideo->IsSubtitleEnabled();
3392 void CVideoPlayer::SetSubtitleVisible(bool bVisible)
3394 m_messenger.Put(
3395 std::make_shared<CDVDMsgBool>(CDVDMsg::PLAYER_SET_SUBTITLESTREAM_VISIBLE, bVisible));
3396 m_processInfo->GetVideoSettingsLocked().SetSubtitleVisible(bVisible);
3399 void CVideoPlayer::SetEnableStream(CCurrentStream& current, bool isEnabled)
3401 if (m_pDemuxer && STREAM_SOURCE_MASK(current.source) == STREAM_SOURCE_DEMUX)
3402 m_pDemuxer->EnableStream(current.demuxerId, current.id, isEnabled);
3405 void CVideoPlayer::SetSubtitleVisibleInternal(bool bVisible)
3407 m_VideoPlayerVideo->EnableSubtitle(bVisible);
3409 if (m_pInputStream && m_pInputStream->IsStreamType(DVDSTREAM_TYPE_DVD))
3410 std::static_pointer_cast<CDVDInputStreamNavigator>(m_pInputStream)->EnableSubtitleStream(bVisible);
3412 CServiceBroker::GetDataCacheCore().SignalSubtitleInfoChange();
3415 void CVideoPlayer::SetSubtitleVerticalPosition(int value, bool save)
3417 m_processInfo->GetVideoSettingsLocked().SetSubtitleVerticalPosition(value, save);
3418 m_renderManager.SetSubtitleVerticalPosition(value, save);
3421 std::shared_ptr<TextCacheStruct_t> CVideoPlayer::GetTeletextCache()
3423 if (m_CurrentTeletext.id < 0)
3424 return nullptr;
3426 return m_VideoPlayerTeletext->GetTeletextCache();
3429 bool CVideoPlayer::HasTeletextCache() const
3431 return m_CurrentTeletext.id >= 0;
3434 void CVideoPlayer::LoadPage(int p, int sp, unsigned char* buffer)
3436 if (m_CurrentTeletext.id < 0)
3437 return;
3439 return m_VideoPlayerTeletext->LoadPage(p, sp, buffer);
3442 void CVideoPlayer::SeekTime(int64_t iTime)
3444 int64_t seekOffset = iTime - GetTime();
3446 CDVDMsgPlayerSeek::CMode mode;
3447 mode.time = static_cast<double>(iTime);
3448 mode.backward = true;
3449 mode.accurate = true;
3450 mode.trickplay = false;
3451 mode.sync = true;
3453 m_messenger.Put(std::make_shared<CDVDMsgPlayerSeek>(mode));
3454 SynchronizeDemuxer();
3455 m_callback.OnPlayBackSeek(iTime, seekOffset);
3456 m_processInfo->SeekFinished(seekOffset);
3459 bool CVideoPlayer::SeekTimeRelative(int64_t iTime)
3461 int64_t abstime = GetTime() + iTime;
3463 // if the file has EDL cuts we can't rely on m_clock for relative seeks
3464 // EDL cuts remove time from the original file, hence we might seek to
3465 // positions too far from the current m_clock position. Seek to absolute
3466 // time instead
3467 if (m_Edl.HasCuts())
3469 SeekTime(abstime);
3470 return true;
3473 CDVDMsgPlayerSeek::CMode mode;
3474 mode.time = (int)iTime;
3475 mode.relative = true;
3476 mode.backward = (iTime < 0) ? true : false;
3477 mode.accurate = false;
3478 mode.trickplay = false;
3479 mode.sync = true;
3481 m_messenger.Put(std::make_shared<CDVDMsgPlayerSeek>(mode));
3482 m_processInfo->SetStateSeeking(true);
3484 m_callback.OnPlayBackSeek(abstime, iTime);
3485 m_processInfo->SeekFinished(iTime);
3486 return true;
3489 // return the time in milliseconds
3490 int64_t CVideoPlayer::GetTime()
3492 std::unique_lock<CCriticalSection> lock(m_StateSection);
3493 return llrint(m_State.time);
3496 void CVideoPlayer::SetSpeed(float speed)
3498 // can't rewind in menu as seeking isn't possible
3499 // forward is fine
3500 if (speed < 0 && IsInMenu())
3501 return;
3503 if (!CanSeek() && !CanPause())
3504 return;
3506 int iSpeed = static_cast<int>(speed * DVD_PLAYSPEED_NORMAL);
3508 if (!CanSeek())
3510 if ((iSpeed != DVD_PLAYSPEED_NORMAL) && (iSpeed != DVD_PLAYSPEED_PAUSE))
3511 return;
3514 float currentSpeed = m_processInfo->GetNewSpeed();
3515 m_processInfo->SetNewSpeed(speed);
3516 if (iSpeed != currentSpeed)
3518 if (iSpeed == DVD_PLAYSPEED_NORMAL)
3519 m_callback.OnPlayBackResumed();
3520 else if (iSpeed == DVD_PLAYSPEED_PAUSE)
3521 m_callback.OnPlayBackPaused();
3523 if (iSpeed == DVD_PLAYSPEED_NORMAL)
3525 float currentTempo = m_processInfo->GetNewTempo();
3526 if (currentTempo != 1.0f)
3528 SetTempo(currentTempo);
3529 return;
3532 SetPlaySpeed(iSpeed);
3536 void CVideoPlayer::SetTempo(float tempo)
3538 tempo = floor(tempo * 100.0f + 0.5f) / 100.0f;
3539 if (m_processInfo->IsTempoAllowed(tempo))
3541 int speed = tempo * DVD_PLAYSPEED_NORMAL;
3542 CDVDMsgPlayerSetSpeed::SpeedParams params = { speed, true };
3543 m_messenger.Put(std::make_shared<CDVDMsgPlayerSetSpeed>(params));
3545 m_processInfo->SetNewTempo(tempo);
3549 void CVideoPlayer::FrameAdvance(int frames)
3551 float currentSpeed = m_processInfo->GetNewSpeed();
3552 if (currentSpeed != DVD_PLAYSPEED_PAUSE)
3553 return;
3555 m_messenger.Put(std::make_shared<CDVDMsgInt>(CDVDMsg::PLAYER_FRAME_ADVANCE, frames));
3558 bool CVideoPlayer::SupportsTempo() const
3560 return m_State.cantempo;
3563 bool CVideoPlayer::OpenStream(CCurrentStream& current, int64_t demuxerId, int iStream, int source, bool reset /*= true*/)
3565 CDemuxStream* stream = NULL;
3566 CDVDStreamInfo hint;
3568 CLog::Log(LOGINFO, "Opening stream: {} source: {}", iStream, source);
3570 if(STREAM_SOURCE_MASK(source) == STREAM_SOURCE_DEMUX_SUB)
3572 int index = m_SelectionStreams.TypeIndexOf(current.type, source, demuxerId, iStream);
3573 if (index < 0)
3574 return false;
3575 const SelectionStream& st = m_SelectionStreams.Get(current.type, index);
3577 CLog::Log(LOGINFO, "Opening Subtitle file: {}", CURL::GetRedacted(st.filename));
3578 m_pSubtitleDemuxer.reset();
3579 const auto demux = m_subtitleDemuxerMap.find(demuxerId);
3580 if (demux == m_subtitleDemuxerMap.end())
3582 CLog::Log(LOGINFO, "No demuxer found for file {}", CURL::GetRedacted(st.filename));
3583 return false;
3586 m_pSubtitleDemuxer = demux->second;
3588 double pts = m_VideoPlayerVideo->GetCurrentPts();
3589 if(pts == DVD_NOPTS_VALUE)
3590 pts = m_CurrentVideo.dts;
3591 if(pts == DVD_NOPTS_VALUE)
3592 pts = 0;
3593 pts += m_offset_pts;
3594 if (!m_pSubtitleDemuxer->SeekTime((int)(1000.0 * pts / (double)DVD_TIME_BASE)))
3595 CLog::Log(LOGDEBUG, "{} - failed to start subtitle demuxing from: {:f}", __FUNCTION__, pts);
3596 stream = m_pSubtitleDemuxer->GetStream(demuxerId, iStream);
3597 if(!stream || stream->disabled)
3598 return false;
3600 m_pSubtitleDemuxer->EnableStream(demuxerId, iStream, true);
3602 hint.Assign(*stream, true);
3604 else if(STREAM_SOURCE_MASK(source) == STREAM_SOURCE_TEXT)
3606 int index = m_SelectionStreams.TypeIndexOf(current.type, source, demuxerId, iStream);
3607 if(index < 0)
3608 return false;
3610 hint.Clear();
3611 hint.filename = m_SelectionStreams.Get(current.type, index).filename;
3612 hint.fpsscale = m_CurrentVideo.hint.fpsscale;
3613 hint.fpsrate = m_CurrentVideo.hint.fpsrate;
3615 else if(STREAM_SOURCE_MASK(source) == STREAM_SOURCE_DEMUX)
3617 if(!m_pDemuxer)
3618 return false;
3620 m_pDemuxer->OpenStream(demuxerId, iStream);
3622 stream = m_pDemuxer->GetStream(demuxerId, iStream);
3623 if (!stream || stream->disabled)
3624 return false;
3626 hint.Assign(*stream, true);
3628 if(m_pInputStream && m_pInputStream->IsStreamType(DVDSTREAM_TYPE_DVD))
3629 hint.filename = "dvd";
3631 else if(STREAM_SOURCE_MASK(source) == STREAM_SOURCE_VIDEOMUX)
3633 if(!m_pCCDemuxer)
3634 return false;
3636 stream = m_pCCDemuxer->GetStream(iStream);
3637 if(!stream || stream->disabled)
3638 return false;
3640 hint.Assign(*stream, false);
3643 bool res;
3644 switch(current.type)
3646 case STREAM_AUDIO:
3647 res = OpenAudioStream(hint, reset);
3648 break;
3649 case STREAM_VIDEO:
3650 res = OpenVideoStream(hint, reset);
3651 break;
3652 case STREAM_SUBTITLE:
3653 res = OpenSubtitleStream(hint);
3654 break;
3655 case STREAM_TELETEXT:
3656 res = OpenTeletextStream(hint);
3657 break;
3658 case STREAM_RADIO_RDS:
3659 res = OpenRadioRDSStream(hint);
3660 break;
3661 case STREAM_AUDIO_ID3:
3662 res = OpenAudioID3Stream(hint);
3663 break;
3664 default:
3665 res = false;
3666 break;
3669 if (res)
3671 int oldId = current.id;
3672 current.id = iStream;
3673 current.demuxerId = demuxerId;
3674 current.source = source;
3675 current.hint = hint;
3676 current.stream = (void*)stream;
3677 current.lastdts = DVD_NOPTS_VALUE;
3678 if (oldId >= 0 && current.avsync != CCurrentStream::AV_SYNC_FORCE)
3679 current.avsync = CCurrentStream::AV_SYNC_CHECK;
3680 if(stream)
3681 current.changes = stream->changes;
3683 else
3685 if(stream)
3687 /* mark stream as disabled, to disallow further attempts*/
3688 CLog::Log(LOGWARNING, "{} - Unsupported stream {}. Stream disabled.", __FUNCTION__,
3689 stream->uniqueId);
3690 stream->disabled = true;
3694 UpdateContentState();
3695 CServiceBroker::GetDataCacheCore().SignalAudioInfoChange();
3696 CServiceBroker::GetDataCacheCore().SignalVideoInfoChange();
3697 CServiceBroker::GetDataCacheCore().SignalSubtitleInfoChange();
3699 return res;
3702 bool CVideoPlayer::OpenAudioStream(CDVDStreamInfo& hint, bool reset)
3704 IDVDStreamPlayer* player = GetStreamPlayer(m_CurrentAudio.player);
3705 if(player == nullptr)
3706 return false;
3708 if(m_CurrentAudio.id < 0 ||
3709 m_CurrentAudio.hint != hint)
3711 if (!player->OpenStream(hint))
3712 return false;
3714 player->SendMessage(std::make_shared<CDVDMsgBool>(CDVDMsg::GENERAL_PAUSE, m_displayLost), 1);
3716 static_cast<IDVDStreamPlayerAudio*>(player)->SetSpeed(m_streamPlayerSpeed);
3717 m_CurrentAudio.syncState = IDVDStreamPlayer::SYNC_STARTING;
3718 m_CurrentAudio.packets = 0;
3720 else if (reset)
3721 player->SendMessage(std::make_shared<CDVDMsg>(CDVDMsg::GENERAL_RESET), 0);
3723 m_HasAudio = true;
3725 static_cast<IDVDStreamPlayerAudio*>(player)->SendMessage(
3726 std::make_shared<CDVDMsg>(CDVDMsg::PLAYER_REQUEST_STATE), 1);
3728 return true;
3731 bool CVideoPlayer::OpenVideoStream(CDVDStreamInfo& hint, bool reset)
3733 if (m_pInputStream && m_pInputStream->IsStreamType(DVDSTREAM_TYPE_DVD))
3735 /* set aspect ratio as requested by navigator for dvd's */
3736 float aspect = std::static_pointer_cast<CDVDInputStreamNavigator>(m_pInputStream)->GetVideoAspectRatio();
3737 if (aspect != 0.0f)
3739 hint.aspect = static_cast<double>(aspect);
3740 hint.forced_aspect = true;
3742 hint.dvd = true;
3744 else if (m_pInputStream && m_pInputStream->IsStreamType(DVDSTREAM_TYPE_PVRMANAGER))
3746 // set framerate if not set by demuxer
3747 if (hint.fpsrate == 0 || hint.fpsscale == 0)
3749 int fpsidx = CServiceBroker::GetSettingsComponent()->GetSettings()->GetInt(CSettings::SETTING_PVRPLAYBACK_FPS);
3750 if (fpsidx == 1)
3752 hint.fpsscale = 1000;
3753 hint.fpsrate = 50000;
3755 else if (fpsidx == 2)
3757 hint.fpsscale = 1001;
3758 hint.fpsrate = 60000;
3763 std::shared_ptr<CDVDInputStream::IMenus> pMenus = std::dynamic_pointer_cast<CDVDInputStream::IMenus>(m_pInputStream);
3764 if(pMenus && pMenus->IsInMenu())
3765 hint.stills = true;
3767 if (hint.stereo_mode.empty())
3769 CGUIComponent *gui = CServiceBroker::GetGUI();
3770 if (gui != nullptr)
3772 const CStereoscopicsManager &stereoscopicsManager = gui->GetStereoscopicsManager();
3773 hint.stereo_mode = stereoscopicsManager.DetectStereoModeByString(m_item.GetPath());
3777 if (hint.flags & AV_DISPOSITION_ATTACHED_PIC)
3778 return false;
3780 // set desired refresh rate
3781 if (m_CurrentVideo.id < 0 && m_playerOptions.fullscreen &&
3782 CServiceBroker::GetWinSystem()->GetGfxContext().IsFullScreenRoot() && hint.fpsrate != 0 &&
3783 hint.fpsscale != 0)
3785 if (CServiceBroker::GetSettingsComponent()->GetSettings()->GetInt(CSettings::SETTING_VIDEOPLAYER_ADJUSTREFRESHRATE) != ADJUST_REFRESHRATE_OFF)
3787 const double framerate = DVD_TIME_BASE / CDVDCodecUtils::NormalizeFrameduration(
3788 (double)DVD_TIME_BASE * hint.fpsscale /
3789 (hint.fpsrate * (hint.interlaced ? 2 : 1)));
3791 RESOLUTION res = CResolutionUtils::ChooseBestResolution(static_cast<float>(framerate), hint.width, hint.height, !hint.stereo_mode.empty());
3792 CServiceBroker::GetWinSystem()->GetGfxContext().SetVideoResolution(res, false);
3793 m_renderManager.TriggerUpdateResolution(framerate, hint.width, hint.height, hint.stereo_mode);
3797 IDVDStreamPlayer* player = GetStreamPlayer(m_CurrentVideo.player);
3798 if(player == nullptr)
3799 return false;
3801 if(m_CurrentVideo.id < 0 ||
3802 m_CurrentVideo.hint != hint)
3804 if (hint.codec == AV_CODEC_ID_MPEG2VIDEO || hint.codec == AV_CODEC_ID_H264)
3805 m_pCCDemuxer.reset();
3807 if (!player->OpenStream(hint))
3808 return false;
3810 player->SendMessage(std::make_shared<CDVDMsgBool>(CDVDMsg::GENERAL_PAUSE, m_displayLost), 1);
3812 // look for any EDL files
3813 m_Edl.Clear();
3814 float fFramesPerSecond = 0.0f;
3815 if (m_CurrentVideo.hint.fpsscale > 0.0f)
3816 fFramesPerSecond = static_cast<float>(m_CurrentVideo.hint.fpsrate) / static_cast<float>(m_CurrentVideo.hint.fpsscale);
3817 m_Edl.ReadEditDecisionLists(m_item, fFramesPerSecond);
3818 CServiceBroker::GetDataCacheCore().SetEditList(m_Edl.GetEditList());
3819 CServiceBroker::GetDataCacheCore().SetCuts(m_Edl.GetCutMarkers());
3820 CServiceBroker::GetDataCacheCore().SetSceneMarkers(m_Edl.GetSceneMarkers());
3822 static_cast<IDVDStreamPlayerVideo*>(player)->SetSpeed(m_streamPlayerSpeed);
3823 m_CurrentVideo.syncState = IDVDStreamPlayer::SYNC_STARTING;
3824 m_CurrentVideo.packets = 0;
3826 else if (reset)
3827 player->SendMessage(std::make_shared<CDVDMsg>(CDVDMsg::GENERAL_RESET), 0);
3829 m_HasVideo = true;
3831 static_cast<IDVDStreamPlayerVideo*>(player)->SendMessage(
3832 std::make_shared<CDVDMsg>(CDVDMsg::PLAYER_REQUEST_STATE), 1);
3834 // open CC demuxer if video is mpeg2
3835 if ((hint.codec == AV_CODEC_ID_MPEG2VIDEO || hint.codec == AV_CODEC_ID_H264) && !m_pCCDemuxer)
3837 m_pCCDemuxer = std::make_unique<CDVDDemuxCC>(hint.codec);
3838 m_SelectionStreams.Clear(STREAM_NONE, STREAM_SOURCE_VIDEOMUX);
3841 return true;
3844 bool CVideoPlayer::OpenSubtitleStream(const CDVDStreamInfo& hint)
3846 IDVDStreamPlayer* player = GetStreamPlayer(m_CurrentSubtitle.player);
3847 if(player == nullptr)
3848 return false;
3850 if(m_CurrentSubtitle.id < 0 ||
3851 m_CurrentSubtitle.hint != hint)
3853 if (!player->OpenStream(hint))
3854 return false;
3857 return true;
3860 void CVideoPlayer::AdaptForcedSubtitles()
3862 SelectionStream ss = m_SelectionStreams.Get(STREAM_SUBTITLE, GetSubtitle());
3863 if (ss.flags & StreamFlags::FLAG_FORCED)
3865 SelectionStream as = m_SelectionStreams.Get(STREAM_AUDIO, GetAudioStream());
3866 bool isVisible = false;
3867 for (const auto &stream : m_SelectionStreams.Get(STREAM_SUBTITLE))
3869 if (stream.flags & StreamFlags::FLAG_FORCED && g_LangCodeExpander.CompareISO639Codes(stream.language, as.language))
3871 if (OpenStream(m_CurrentSubtitle, stream.demuxerId, stream.id, stream.source))
3873 isVisible = true;
3874 break;
3878 // SetEnableStream only if not visible, when visible OpenStream already implied that stream is enabled
3879 if (!isVisible)
3880 SetEnableStream(m_CurrentSubtitle, false);
3882 SetSubtitleVisibleInternal(isVisible);
3886 bool CVideoPlayer::OpenTeletextStream(CDVDStreamInfo& hint)
3888 if (!m_VideoPlayerTeletext->CheckStream(hint))
3889 return false;
3891 IDVDStreamPlayer* player = GetStreamPlayer(m_CurrentTeletext.player);
3892 if(player == nullptr)
3893 return false;
3895 if(m_CurrentTeletext.id < 0 ||
3896 m_CurrentTeletext.hint != hint)
3898 if (!player->OpenStream(hint))
3899 return false;
3902 return true;
3905 bool CVideoPlayer::OpenRadioRDSStream(CDVDStreamInfo& hint)
3907 if (!m_VideoPlayerRadioRDS->CheckStream(hint))
3908 return false;
3910 IDVDStreamPlayer* player = GetStreamPlayer(m_CurrentRadioRDS.player);
3911 if(player == nullptr)
3912 return false;
3914 if(m_CurrentRadioRDS.id < 0 ||
3915 m_CurrentRadioRDS.hint != hint)
3917 if (!player->OpenStream(hint))
3918 return false;
3921 return true;
3924 bool CVideoPlayer::OpenAudioID3Stream(CDVDStreamInfo& hint)
3926 if (!m_VideoPlayerAudioID3->CheckStream(hint))
3927 return false;
3929 IDVDStreamPlayer* player = GetStreamPlayer(m_CurrentAudioID3.player);
3930 if (player == nullptr)
3931 return false;
3933 if (m_CurrentAudioID3.id < 0 || m_CurrentAudioID3.hint != hint)
3935 if (!player->OpenStream(hint))
3936 return false;
3939 return true;
3942 bool CVideoPlayer::CloseStream(CCurrentStream& current, bool bWaitForBuffers)
3944 if (current.id < 0)
3945 return false;
3947 CLog::Log(LOGINFO, "Closing stream player {}", current.player);
3949 if(bWaitForBuffers)
3950 SetCaching(CACHESTATE_DONE);
3952 SetEnableStream(current, false);
3954 IDVDStreamPlayer* player = GetStreamPlayer(current.player);
3955 if (player)
3957 if ((current.type == STREAM_AUDIO && current.syncState != IDVDStreamPlayer::SYNC_INSYNC) ||
3958 (current.type == STREAM_VIDEO && current.syncState != IDVDStreamPlayer::SYNC_INSYNC) ||
3959 m_bAbortRequest)
3960 bWaitForBuffers = false;
3961 player->CloseStream(bWaitForBuffers);
3964 current.Clear();
3965 return true;
3968 void CVideoPlayer::FlushBuffers(double pts, bool accurate, bool sync)
3970 CLog::Log(LOGDEBUG, "CVideoPlayer::FlushBuffers - flushing buffers");
3972 double startpts;
3973 if (accurate)
3974 startpts = pts;
3975 else
3976 startpts = DVD_NOPTS_VALUE;
3978 m_SpeedState.Reset(pts);
3980 if (sync)
3982 m_CurrentAudio.inited = false;
3983 m_CurrentAudio.avsync = CCurrentStream::AV_SYNC_FORCE;
3984 m_CurrentAudio.starttime = DVD_NOPTS_VALUE;
3985 m_CurrentVideo.inited = false;
3986 m_CurrentVideo.avsync = CCurrentStream::AV_SYNC_FORCE;
3987 m_CurrentVideo.starttime = DVD_NOPTS_VALUE;
3988 m_CurrentSubtitle.inited = false;
3989 m_CurrentTeletext.inited = false;
3990 m_CurrentRadioRDS.inited = false;
3993 m_CurrentAudio.dts = DVD_NOPTS_VALUE;
3994 m_CurrentAudio.startpts = startpts;
3995 m_CurrentAudio.packets = 0;
3997 m_CurrentVideo.dts = DVD_NOPTS_VALUE;
3998 m_CurrentVideo.startpts = startpts;
3999 m_CurrentVideo.packets = 0;
4001 m_CurrentSubtitle.dts = DVD_NOPTS_VALUE;
4002 m_CurrentSubtitle.startpts = startpts;
4003 m_CurrentSubtitle.packets = 0;
4005 m_CurrentTeletext.dts = DVD_NOPTS_VALUE;
4006 m_CurrentTeletext.startpts = startpts;
4007 m_CurrentTeletext.packets = 0;
4009 m_CurrentRadioRDS.dts = DVD_NOPTS_VALUE;
4010 m_CurrentRadioRDS.startpts = startpts;
4011 m_CurrentRadioRDS.packets = 0;
4013 m_CurrentAudioID3.dts = DVD_NOPTS_VALUE;
4014 m_CurrentAudioID3.startpts = startpts;
4015 m_CurrentAudioID3.packets = 0;
4017 m_VideoPlayerAudio->Flush(sync);
4018 m_VideoPlayerVideo->Flush(sync);
4019 m_VideoPlayerSubtitle->Flush();
4020 m_VideoPlayerTeletext->Flush();
4021 m_VideoPlayerRadioRDS->Flush();
4022 m_VideoPlayerAudioID3->Flush();
4024 if (m_playSpeed == DVD_PLAYSPEED_NORMAL || m_playSpeed == DVD_PLAYSPEED_PAUSE ||
4025 (m_playSpeed >= DVD_PLAYSPEED_NORMAL * m_processInfo->MinTempoPlatform() &&
4026 m_playSpeed <= DVD_PLAYSPEED_NORMAL * m_processInfo->MaxTempoPlatform()))
4028 // make sure players are properly flushed, should put them in stalled state
4029 auto msg = std::make_shared<CDVDMsgGeneralSynchronize>(1s, SYNCSOURCE_AUDIO | SYNCSOURCE_VIDEO);
4030 m_VideoPlayerAudio->SendMessage(msg, 1);
4031 m_VideoPlayerVideo->SendMessage(msg, 1);
4032 msg->Wait(m_bStop, 0);
4034 // purge any pending PLAYER_STARTED messages
4035 m_messenger.Flush(CDVDMsg::PLAYER_STARTED);
4037 // we should now wait for init cache
4038 SetCaching(CACHESTATE_FLUSH);
4039 if (sync)
4041 m_CurrentAudio.syncState = IDVDStreamPlayer::SYNC_STARTING;
4042 m_CurrentVideo.syncState = IDVDStreamPlayer::SYNC_STARTING;
4046 if(pts != DVD_NOPTS_VALUE && sync)
4047 m_clock.Discontinuity(pts);
4048 UpdatePlayState(0);
4050 m_demuxerSpeed = DVD_PLAYSPEED_NORMAL;
4051 if (m_pDemuxer)
4052 m_pDemuxer->SetSpeed(DVD_PLAYSPEED_NORMAL);
4055 // since we call ffmpeg functions to decode, this is being called in the same thread as ::Process() is
4056 int CVideoPlayer::OnDiscNavResult(void* pData, int iMessage)
4058 if (!m_pInputStream)
4059 return 0;
4061 #if defined(HAVE_LIBBLURAY)
4062 if (m_pInputStream->IsStreamType(DVDSTREAM_TYPE_BLURAY))
4064 switch (iMessage)
4066 case BD_EVENT_MENU_OVERLAY:
4067 m_overlayContainer.ProcessAndAddOverlayIfValid(
4068 *static_cast<std::shared_ptr<CDVDOverlay>*>(pData));
4069 break;
4070 case BD_EVENT_PLAYLIST_STOP:
4071 m_dvd.state = DVDSTATE_NORMAL;
4072 m_dvd.iDVDStillTime = 0ms;
4073 m_messenger.Put(std::make_shared<CDVDMsg>(CDVDMsg::GENERAL_FLUSH));
4074 break;
4075 case BD_EVENT_AUDIO_STREAM:
4076 m_dvd.iSelectedAudioStream = *static_cast<int*>(pData);
4077 break;
4079 case BD_EVENT_PG_TEXTST_STREAM:
4080 m_dvd.iSelectedSPUStream = *static_cast<int*>(pData);
4081 break;
4082 case BD_EVENT_PG_TEXTST:
4084 bool enable = (*static_cast<int*>(pData) != 0);
4085 m_VideoPlayerVideo->EnableSubtitle(enable);
4087 break;
4088 case BD_EVENT_STILL_TIME:
4090 if (m_dvd.state != DVDSTATE_STILL)
4092 // else notify the player we have received a still frame
4094 m_dvd.iDVDStillTime = std::chrono::milliseconds(*static_cast<int*>(pData));
4095 m_dvd.iDVDStillStartTime = std::chrono::steady_clock::now();
4097 if (m_dvd.iDVDStillTime > 0ms)
4098 m_dvd.iDVDStillTime *= 1000;
4100 /* adjust for the output delay in the video queue */
4101 std::chrono::milliseconds time = 0ms;
4102 if (m_CurrentVideo.stream && m_dvd.iDVDStillTime > 0ms)
4104 time = std::chrono::milliseconds(
4105 static_cast<int>(m_VideoPlayerVideo->GetOutputDelay() / (DVD_TIME_BASE / 1000)));
4106 if (time < 10000ms && time > 0ms)
4107 m_dvd.iDVDStillTime += time;
4109 m_dvd.state = DVDSTATE_STILL;
4110 CLog::Log(LOGDEBUG, "BD_EVENT_STILL_TIME - waiting {} msec, with delay of {} msec",
4111 m_dvd.iDVDStillTime.count(), time.count());
4114 break;
4115 case BD_EVENT_STILL:
4117 bool on = static_cast<bool>(*static_cast<int*>(pData));
4118 if (on && m_dvd.state != DVDSTATE_STILL)
4120 m_dvd.state = DVDSTATE_STILL;
4121 m_dvd.iDVDStillStartTime = std::chrono::steady_clock::now();
4122 m_dvd.iDVDStillTime = 0ms;
4123 CLog::Log(LOGDEBUG, "CDVDPlayer::OnDVDNavResult - libbluray DVDSTATE_STILL start");
4125 else if (!on && m_dvd.state == DVDSTATE_STILL)
4127 m_dvd.state = DVDSTATE_NORMAL;
4128 m_dvd.iDVDStillStartTime = {};
4129 m_dvd.iDVDStillTime = 0ms;
4130 CLog::Log(LOGDEBUG, "CDVDPlayer::OnDVDNavResult - libbluray DVDSTATE_STILL end");
4133 break;
4134 case BD_EVENT_MENU_ERROR:
4136 m_dvd.state = DVDSTATE_NORMAL;
4137 CLog::Log(LOGDEBUG, "CVideoPlayer::OnDiscNavResult - libbluray menu not supported (DVDSTATE_NORMAL)");
4138 CGUIDialogKaiToast::QueueNotification(g_localizeStrings.Get(25008), g_localizeStrings.Get(25009));
4140 break;
4141 case BD_EVENT_ENC_ERROR:
4143 m_dvd.state = DVDSTATE_NORMAL;
4144 CLog::Log(LOGDEBUG, "CVideoPlayer::OnDiscNavResult - libbluray the disc/file is encrypted and can't be played (DVDSTATE_NORMAL)");
4145 CGUIDialogKaiToast::QueueNotification(g_localizeStrings.Get(16026), g_localizeStrings.Get(29805));
4147 break;
4148 default:
4149 break;
4152 return 0;
4154 #endif
4156 if (m_pInputStream->IsStreamType(DVDSTREAM_TYPE_DVD))
4158 std::shared_ptr<CDVDInputStreamNavigator> pStream = std::static_pointer_cast<CDVDInputStreamNavigator>(m_pInputStream);
4160 switch (iMessage)
4162 case DVDNAV_STILL_FRAME:
4164 //CLog::Log(LOGDEBUG, "DVDNAV_STILL_FRAME");
4166 dvdnav_still_event_t *still_event = static_cast<dvdnav_still_event_t*>(pData);
4167 // should wait the specified time here while we let the player running
4168 // after that call dvdnav_still_skip(m_dvdnav);
4170 if (m_dvd.state != DVDSTATE_STILL)
4172 // else notify the player we have received a still frame
4174 if(still_event->length < 0xff)
4175 m_dvd.iDVDStillTime = std::chrono::seconds(still_event->length);
4176 else
4177 m_dvd.iDVDStillTime = 0ms;
4179 m_dvd.iDVDStillStartTime = std::chrono::steady_clock::now();
4181 /* adjust for the output delay in the video queue */
4182 std::chrono::milliseconds time = 0ms;
4183 if (m_CurrentVideo.stream && m_dvd.iDVDStillTime > 0ms)
4185 time = std::chrono::milliseconds(
4186 static_cast<int>(m_VideoPlayerVideo->GetOutputDelay() / (DVD_TIME_BASE / 1000)));
4187 if (time < 10000ms && time > 0ms)
4188 m_dvd.iDVDStillTime += time;
4190 m_dvd.state = DVDSTATE_STILL;
4191 CLog::Log(LOGDEBUG, "DVDNAV_STILL_FRAME - waiting {} sec, with delay of {} msec",
4192 still_event->length, time.count());
4194 return NAVRESULT_HOLD;
4196 break;
4197 case DVDNAV_SPU_CLUT_CHANGE:
4199 m_VideoPlayerSubtitle->SendMessage(
4200 std::make_shared<CDVDMsgSubtitleClutChange>((uint8_t*)pData));
4202 break;
4203 case DVDNAV_SPU_STREAM_CHANGE:
4205 dvdnav_spu_stream_change_event_t* event = static_cast<dvdnav_spu_stream_change_event_t*>(pData);
4207 int iStream = event->physical_wide;
4208 bool visible = !(iStream & 0x80);
4210 SetSubtitleVisibleInternal(visible);
4212 if (iStream >= 0)
4213 m_dvd.iSelectedSPUStream = (iStream & ~0x80);
4214 else
4215 m_dvd.iSelectedSPUStream = -1;
4217 m_CurrentSubtitle.stream = NULL;
4219 break;
4220 case DVDNAV_AUDIO_STREAM_CHANGE:
4222 dvdnav_audio_stream_change_event_t* event = static_cast<dvdnav_audio_stream_change_event_t*>(pData);
4223 // Tell system what audiostream should be opened by default
4224 m_dvd.iSelectedAudioStream = event->physical;
4225 m_CurrentAudio.stream = NULL;
4227 break;
4228 case DVDNAV_HIGHLIGHT:
4230 //dvdnav_highlight_event_t* pInfo = (dvdnav_highlight_event_t*)pData;
4231 int iButton = pStream->GetCurrentButton();
4232 CLog::Log(LOGDEBUG, "DVDNAV_HIGHLIGHT: Highlight button {}", iButton);
4233 m_VideoPlayerSubtitle->UpdateOverlayInfo(std::static_pointer_cast<CDVDInputStreamNavigator>(m_pInputStream), LIBDVDNAV_BUTTON_NORMAL);
4235 break;
4236 case DVDNAV_VTS_CHANGE:
4238 //dvdnav_vts_change_event_t* vts_change_event = (dvdnav_vts_change_event_t*)pData;
4239 CLog::Log(LOGDEBUG, "DVDNAV_VTS_CHANGE");
4241 //Make sure we clear all the old overlays here, or else old forced items are left.
4242 m_overlayContainer.Clear();
4244 //Force an aspect ratio that is set in the dvdheaders if available
4245 m_CurrentVideo.hint.aspect = static_cast<double>(pStream->GetVideoAspectRatio());
4246 if( m_VideoPlayerVideo->IsInited() )
4247 m_VideoPlayerVideo->SendMessage(std::make_shared<CDVDMsgDouble>(
4248 CDVDMsg::VIDEO_SET_ASPECT, m_CurrentVideo.hint.aspect));
4250 m_SelectionStreams.Clear(STREAM_NONE, STREAM_SOURCE_NAV);
4251 m_SelectionStreams.Update(m_pInputStream, m_pDemuxer.get());
4252 UpdateContent();
4254 return NAVRESULT_HOLD;
4256 break;
4257 case DVDNAV_CELL_CHANGE:
4259 //dvdnav_cell_change_event_t* cell_change_event = (dvdnav_cell_change_event_t*)pData;
4260 CLog::Log(LOGDEBUG, "DVDNAV_CELL_CHANGE");
4262 if (m_dvd.state != DVDSTATE_STILL)
4263 m_dvd.state = DVDSTATE_NORMAL;
4265 break;
4266 case DVDNAV_NAV_PACKET:
4268 //pci_t* pci = (pci_t*)pData;
4270 // this should be possible to use to make sure we get
4271 // seamless transitions over these boundaries
4272 // if we remember the old vobunits boundaries
4273 // when a packet comes out of demuxer that has
4274 // pts values outside that boundary, it belongs
4275 // to the new vobunit, which has new timestamps
4276 UpdatePlayState(0);
4278 break;
4279 case DVDNAV_HOP_CHANNEL:
4281 // This event is issued whenever a non-seamless operation has been executed.
4282 // Applications with fifos should drop the fifos content to speed up responsiveness.
4283 CLog::Log(LOGDEBUG, "DVDNAV_HOP_CHANNEL");
4284 if(m_dvd.state == DVDSTATE_SEEK)
4285 m_dvd.state = DVDSTATE_NORMAL;
4286 else
4288 bool sync = !IsInMenuInternal();
4289 FlushBuffers(DVD_NOPTS_VALUE, false, sync);
4290 m_dvd.syncClock = true;
4291 m_dvd.state = DVDSTATE_NORMAL;
4292 if (m_pDemuxer)
4293 m_pDemuxer->Flush();
4296 return NAVRESULT_ERROR;
4298 break;
4299 case DVDNAV_STOP:
4301 CLog::Log(LOGDEBUG, "DVDNAV_STOP");
4302 m_dvd.state = DVDSTATE_NORMAL;
4304 break;
4305 case DVDNAV_ERROR:
4307 CLog::Log(LOGDEBUG, "DVDNAV_ERROR");
4308 m_dvd.state = DVDSTATE_NORMAL;
4309 CGUIDialogKaiToast::QueueNotification(g_localizeStrings.Get(16026),
4310 g_localizeStrings.Get(16029));
4312 break;
4313 default:
4315 break;
4318 return NAVRESULT_NOP;
4321 void CVideoPlayer::GetVideoResolution(unsigned int &width, unsigned int &height)
4323 RESOLUTION_INFO res = CServiceBroker::GetWinSystem()->GetGfxContext().GetResInfo();
4324 width = res.iWidth;
4325 height = res.iHeight;
4328 bool CVideoPlayer::OnAction(const CAction &action)
4330 #define THREAD_ACTION(action) \
4331 do \
4333 if (!IsCurrentThread()) \
4335 m_messenger.Put( \
4336 std::make_shared<CDVDMsgType<CAction>>(CDVDMsg::GENERAL_GUI_ACTION, action)); \
4337 return true; \
4339 } while (false)
4341 std::shared_ptr<CDVDInputStream::IMenus> pMenus = std::dynamic_pointer_cast<CDVDInputStream::IMenus>(m_pInputStream);
4342 if (pMenus)
4344 if (m_dvd.state == DVDSTATE_STILL && m_dvd.iDVDStillTime != 0ms &&
4345 pMenus->GetTotalButtons() == 0)
4347 switch(action.GetID())
4349 case ACTION_NEXT_ITEM:
4350 case ACTION_MOVE_RIGHT:
4351 case ACTION_MOVE_UP:
4352 case ACTION_SELECT_ITEM:
4354 THREAD_ACTION(action);
4355 /* this will force us out of the stillframe */
4356 CLog::Log(LOGDEBUG, "{} - User asked to exit stillframe", __FUNCTION__);
4357 m_dvd.iDVDStillStartTime = {};
4358 m_dvd.iDVDStillTime = 1ms;
4360 return true;
4365 switch (action.GetID())
4367 /* this code is disabled to allow switching playlist items (dvdimage "stacks") */
4368 #if 0
4369 case ACTION_PREV_ITEM: // SKIP-:
4371 THREAD_ACTION(action);
4372 CLog::Log(LOGDEBUG, " - pushed prev");
4373 pMenus->OnPrevious();
4374 m_processInfo->SeekFinished(0);
4375 return true;
4377 break;
4378 case ACTION_NEXT_ITEM: // SKIP+:
4380 THREAD_ACTION(action);
4381 CLog::Log(LOGDEBUG, " - pushed next");
4382 pMenus->OnNext();
4383 m_processInfo->SeekFinished(0);
4384 return true;
4386 break;
4387 #endif
4388 case ACTION_SHOW_VIDEOMENU: // start button
4390 THREAD_ACTION(action);
4391 CLog::LogF(LOGDEBUG, "Trying to go to the menu");
4392 if (pMenus->OnMenu())
4394 if (m_playSpeed == DVD_PLAYSPEED_PAUSE)
4396 SetPlaySpeed(DVD_PLAYSPEED_NORMAL);
4397 m_callback.OnPlayBackResumed();
4400 // send a message to everyone that we've gone to the menu
4401 CGUIMessage msg(GUI_MSG_VIDEO_MENU_STARTED, 0, 0);
4402 CServiceBroker::GetGUI()->GetWindowManager().SendThreadMessage(msg);
4404 return true;
4406 break;
4409 if (pMenus->IsInMenu())
4411 switch (action.GetID())
4413 case ACTION_NEXT_ITEM:
4414 THREAD_ACTION(action);
4415 CLog::Log(LOGDEBUG, " - pushed next in menu, stream will decide");
4416 if (pMenus->CanSeek() && GetChapterCount() > 0 && GetChapter() < GetChapterCount())
4417 m_messenger.Put(std::make_shared<CDVDMsgPlayerSeekChapter>(GetChapter() + 1));
4418 else
4419 pMenus->OnNext();
4421 m_processInfo->SeekFinished(0);
4422 return true;
4423 case ACTION_PREV_ITEM:
4424 THREAD_ACTION(action);
4425 CLog::Log(LOGDEBUG, " - pushed prev in menu, stream will decide");
4426 if (pMenus->CanSeek() && GetChapterCount() > 0 && GetChapter() > 0)
4427 m_messenger.Put(std::make_shared<CDVDMsgPlayerSeekChapter>(GetPreviousChapter()));
4428 else
4429 pMenus->OnPrevious();
4431 m_processInfo->SeekFinished(0);
4432 return true;
4433 case ACTION_PREVIOUS_MENU:
4434 case ACTION_NAV_BACK:
4436 THREAD_ACTION(action);
4437 CLog::Log(LOGDEBUG, " - menu back");
4438 pMenus->OnBack();
4440 break;
4441 case ACTION_MOVE_LEFT:
4443 THREAD_ACTION(action);
4444 CLog::Log(LOGDEBUG, " - move left");
4445 pMenus->OnLeft();
4447 break;
4448 case ACTION_MOVE_RIGHT:
4450 THREAD_ACTION(action);
4451 CLog::Log(LOGDEBUG, " - move right");
4452 pMenus->OnRight();
4454 break;
4455 case ACTION_MOVE_UP:
4457 THREAD_ACTION(action);
4458 CLog::Log(LOGDEBUG, " - move up");
4459 pMenus->OnUp();
4461 break;
4462 case ACTION_MOVE_DOWN:
4464 THREAD_ACTION(action);
4465 CLog::Log(LOGDEBUG, " - move down");
4466 pMenus->OnDown();
4468 break;
4470 case ACTION_MOUSE_MOVE:
4471 case ACTION_MOUSE_LEFT_CLICK:
4473 CRect rs, rd, rv;
4474 m_renderManager.GetVideoRect(rs, rd, rv);
4475 CPoint pt(action.GetAmount(), action.GetAmount(1));
4476 if (!rd.PtInRect(pt))
4477 return false; // out of bounds
4478 THREAD_ACTION(action);
4479 // convert to video coords...
4480 pt -= CPoint(rd.x1, rd.y1);
4481 pt.x *= rs.Width() / rd.Width();
4482 pt.y *= rs.Height() / rd.Height();
4483 pt += CPoint(rs.x1, rs.y1);
4484 if (action.GetID() == ACTION_MOUSE_LEFT_CLICK)
4486 if (pMenus->OnMouseClick(pt))
4487 return true;
4488 else
4490 CServiceBroker::GetAppMessenger()->PostMsg(
4491 TMSG_GUI_ACTION, WINDOW_INVALID, -1,
4492 static_cast<void*>(new CAction(ACTION_TRIGGER_OSD)));
4493 return false;
4496 return pMenus->OnMouseMove(pt);
4498 break;
4499 case ACTION_SELECT_ITEM:
4501 THREAD_ACTION(action);
4502 CLog::Log(LOGDEBUG, " - button select");
4503 // show button pushed overlay
4504 if(m_pInputStream->IsStreamType(DVDSTREAM_TYPE_DVD))
4505 m_VideoPlayerSubtitle->UpdateOverlayInfo(std::static_pointer_cast<CDVDInputStreamNavigator>(m_pInputStream), LIBDVDNAV_BUTTON_CLICKED);
4507 pMenus->ActivateButton();
4509 break;
4510 case REMOTE_0:
4511 case REMOTE_1:
4512 case REMOTE_2:
4513 case REMOTE_3:
4514 case REMOTE_4:
4515 case REMOTE_5:
4516 case REMOTE_6:
4517 case REMOTE_7:
4518 case REMOTE_8:
4519 case REMOTE_9:
4521 THREAD_ACTION(action);
4522 // Offset from key codes back to button number
4523 int button = action.GetID() - REMOTE_0;
4524 CLog::Log(LOGDEBUG, " - button pressed {}", button);
4525 pMenus->SelectButton(button);
4527 break;
4528 default:
4529 return false;
4530 break;
4532 return true; // message is handled
4536 pMenus.reset();
4538 switch (action.GetID())
4540 case ACTION_NEXT_ITEM:
4541 if (GetChapter() > 0 && GetChapter() < GetChapterCount())
4543 m_messenger.Put(std::make_shared<CDVDMsgPlayerSeekChapter>(GetChapter() + 1));
4544 m_processInfo->SeekFinished(0);
4545 return true;
4547 else if (SeekScene(Direction::FORWARD))
4548 return true;
4549 else
4550 break;
4551 case ACTION_PREV_ITEM:
4552 if (GetChapter() > 0)
4554 m_messenger.Put(std::make_shared<CDVDMsgPlayerSeekChapter>(GetPreviousChapter()));
4555 m_processInfo->SeekFinished(0);
4556 return true;
4558 else if (SeekScene(Direction::BACKWARD))
4559 return true;
4560 else
4561 break;
4562 case ACTION_TOGGLE_COMMSKIP:
4563 m_SkipCommercials = !m_SkipCommercials;
4564 CGUIDialogKaiToast::QueueNotification(g_localizeStrings.Get(25011),
4565 g_localizeStrings.Get(m_SkipCommercials ? 25013 : 25012));
4566 break;
4567 case ACTION_PLAYER_DEBUG:
4568 m_renderManager.ToggleDebug();
4569 break;
4570 case ACTION_PLAYER_DEBUG_VIDEO:
4571 m_renderManager.ToggleDebugVideo();
4572 break;
4574 case ACTION_PLAYER_PROCESS_INFO:
4575 if (CServiceBroker::GetGUI()->GetWindowManager().GetActiveWindow() != WINDOW_DIALOG_PLAYER_PROCESS_INFO)
4577 CServiceBroker::GetGUI()->GetWindowManager().ActivateWindow(WINDOW_DIALOG_PLAYER_PROCESS_INFO);
4578 return true;
4580 break;
4583 // return false to inform the caller we didn't handle the message
4584 return false;
4587 bool CVideoPlayer::IsInMenuInternal() const
4589 std::shared_ptr<CDVDInputStream::IMenus> pStream = std::dynamic_pointer_cast<CDVDInputStream::IMenus>(m_pInputStream);
4590 if (pStream)
4592 if (m_dvd.state == DVDSTATE_STILL)
4593 return true;
4594 else
4595 return pStream->IsInMenu();
4597 return false;
4601 bool CVideoPlayer::IsInMenu() const
4603 std::unique_lock<CCriticalSection> lock(m_StateSection);
4604 return m_State.isInMenu;
4607 MenuType CVideoPlayer::GetSupportedMenuType() const
4609 std::unique_lock<CCriticalSection> lock(m_StateSection);
4610 return m_State.menuType;
4613 std::string CVideoPlayer::GetPlayerState()
4615 std::unique_lock<CCriticalSection> lock(m_StateSection);
4616 return m_State.player_state;
4619 bool CVideoPlayer::SetPlayerState(const std::string& state)
4621 m_messenger.Put(std::make_shared<CDVDMsgPlayerSetState>(state));
4622 return true;
4625 int CVideoPlayer::GetChapterCount() const
4627 std::unique_lock<CCriticalSection> lock(m_StateSection);
4628 return m_State.chapters.size();
4631 int CVideoPlayer::GetChapter() const
4633 std::unique_lock<CCriticalSection> lock(m_StateSection);
4634 return m_State.chapter;
4637 void CVideoPlayer::GetChapterName(std::string& strChapterName, int chapterIdx) const
4639 std::unique_lock<CCriticalSection> lock(m_StateSection);
4640 if (chapterIdx == -1 && m_State.chapter > 0 && m_State.chapter <= (int) m_State.chapters.size())
4641 strChapterName = m_State.chapters[m_State.chapter - 1].first;
4642 else if (chapterIdx > 0 && chapterIdx <= (int) m_State.chapters.size())
4643 strChapterName = m_State.chapters[chapterIdx - 1].first;
4646 int CVideoPlayer::SeekChapter(int iChapter)
4648 if (GetChapter() > 0)
4650 if (iChapter < 0)
4651 iChapter = 0;
4652 if (iChapter > GetChapterCount())
4653 return 0;
4655 // Seek to the chapter.
4656 m_messenger.Put(std::make_shared<CDVDMsgPlayerSeekChapter>(iChapter));
4657 SynchronizeDemuxer();
4660 return 0;
4663 int64_t CVideoPlayer::GetChapterPos(int chapterIdx) const
4665 std::unique_lock<CCriticalSection> lock(m_StateSection);
4666 if (chapterIdx > 0 && chapterIdx <= (int) m_State.chapters.size())
4667 return m_State.chapters[chapterIdx - 1].second;
4669 return -1;
4672 int CVideoPlayer::GetPreviousChapter()
4674 // 5-second grace period from chapter start to skip backwards to previous chapter
4675 // Afterwards skip to start of current chapter.
4676 const int chapter = GetChapter();
4678 if (chapter > 0 && (GetTime() < (GetChapterPos(chapter) + 5) * 1000))
4679 return chapter - 1;
4680 else
4681 return chapter;
4684 void CVideoPlayer::AddSubtitle(const std::string& strSubPath)
4686 m_messenger.Put(
4687 std::make_shared<CDVDMsgType<std::string>>(CDVDMsg::SUBTITLE_ADDFILE, strSubPath));
4690 bool CVideoPlayer::IsCaching() const
4692 std::unique_lock<CCriticalSection> lock(m_StateSection);
4693 return !m_State.isInMenu && m_State.caching;
4696 int CVideoPlayer::GetCacheLevel() const
4698 std::unique_lock<CCriticalSection> lock(m_StateSection);
4699 return (int)(m_State.cache_level * 100);
4702 double CVideoPlayer::GetQueueTime()
4704 int a = m_VideoPlayerAudio->GetLevel();
4705 int v = m_processInfo->GetLevelVQ();
4706 return std::max(a, v) * m_messageQueueTimeSize * 1000.0 / 100.0;
4709 int CVideoPlayer::AddSubtitleFile(const std::string& filename, const std::string& subfilename)
4711 std::string ext = URIUtils::GetExtension(filename);
4712 std::string vobsubfile = subfilename;
4713 if (ext == ".idx" || ext == ".sup")
4715 std::shared_ptr<CDVDDemux> pDemux;
4716 if (ext == ".idx")
4718 if (vobsubfile.empty())
4720 // find corresponding .sub (e.g. in case of manually selected .idx sub)
4721 vobsubfile = CUtil::GetVobSubSubFromIdx(filename);
4722 if (vobsubfile.empty())
4723 return -1;
4726 auto pDemuxVobsub = std::make_shared<CDVDDemuxVobsub>();
4727 if (!pDemuxVobsub->Open(filename, STREAM_SOURCE_NONE, vobsubfile))
4728 return -1;
4730 m_SelectionStreams.Update(nullptr, pDemuxVobsub.get(), vobsubfile);
4731 pDemux = pDemuxVobsub;
4733 else // .sup file
4735 CFileItem item(filename, false);
4736 std::shared_ptr<CDVDInputStream> pInput;
4737 pInput = CDVDFactoryInputStream::CreateInputStream(nullptr, item);
4738 if (!pInput || !pInput->Open())
4739 return -1;
4741 auto pDemuxFFmpeg = std::make_shared<CDVDDemuxFFmpeg>();
4742 if (!pDemuxFFmpeg->Open(pInput, false))
4743 return -1;
4745 m_SelectionStreams.Update(nullptr, pDemuxFFmpeg.get(), filename);
4746 pDemux = pDemuxFFmpeg;
4749 ExternalStreamInfo info =
4750 CUtil::GetExternalStreamDetailsFromFilename(m_item.GetDynPath(), filename);
4752 for (auto sub : pDemux->GetStreams())
4754 if (sub->type != STREAM_SUBTITLE)
4755 continue;
4757 int index = m_SelectionStreams.TypeIndexOf(STREAM_SUBTITLE,
4758 m_SelectionStreams.Source(STREAM_SOURCE_DEMUX_SUB, filename),
4759 sub->demuxerId, sub->uniqueId);
4760 SelectionStream& stream = m_SelectionStreams.Get(STREAM_SUBTITLE, index);
4762 if (stream.name.empty())
4763 stream.name = info.name;
4765 if (stream.language.empty())
4766 stream.language = info.language;
4768 if (static_cast<StreamFlags>(info.flag) != StreamFlags::FLAG_NONE)
4769 stream.flags = static_cast<StreamFlags>(info.flag);
4772 UpdateContent();
4773 // the demuxer id is unique
4774 m_subtitleDemuxerMap[pDemux->GetDemuxerId()] = pDemux;
4775 return m_SelectionStreams.TypeIndexOf(
4776 STREAM_SUBTITLE, m_SelectionStreams.Source(STREAM_SOURCE_DEMUX_SUB, filename),
4777 pDemux->GetDemuxerId(), 0);
4780 if(ext == ".sub")
4782 // if this looks like vobsub file (i.e. .idx found), add it as such
4783 std::string vobsubidx = CUtil::GetVobSubIdxFromSub(filename);
4784 if (!vobsubidx.empty())
4785 return AddSubtitleFile(vobsubidx, filename);
4788 SelectionStream s;
4789 s.source = m_SelectionStreams.Source(STREAM_SOURCE_TEXT, filename);
4790 s.type = STREAM_SUBTITLE;
4791 s.id = 0;
4792 s.filename = filename;
4793 ExternalStreamInfo info = CUtil::GetExternalStreamDetailsFromFilename(m_item.GetDynPath(), filename);
4794 s.name = info.name;
4795 s.language = info.language;
4796 if (static_cast<StreamFlags>(info.flag) != StreamFlags::FLAG_NONE)
4797 s.flags = static_cast<StreamFlags>(info.flag);
4799 m_SelectionStreams.Update(s);
4800 UpdateContent();
4801 return m_SelectionStreams.TypeIndexOf(STREAM_SUBTITLE, s.source, s.demuxerId, s.id);
4804 void CVideoPlayer::UpdatePlayState(double timeout)
4806 if (m_State.timestamp != 0 &&
4807 m_State.timestamp + DVD_MSEC_TO_TIME(timeout) > m_clock.GetAbsoluteClock())
4808 return;
4810 SPlayerState state(m_State);
4812 state.dts = DVD_NOPTS_VALUE;
4813 if (m_CurrentVideo.dts != DVD_NOPTS_VALUE)
4814 state.dts = m_CurrentVideo.dts;
4815 else if (m_CurrentAudio.dts != DVD_NOPTS_VALUE)
4816 state.dts = m_CurrentAudio.dts;
4817 else if (m_CurrentVideo.startpts != DVD_NOPTS_VALUE)
4818 state.dts = m_CurrentVideo.startpts;
4819 else if (m_CurrentAudio.startpts != DVD_NOPTS_VALUE)
4820 state.dts = m_CurrentAudio.startpts;
4822 state.startTime = 0;
4823 state.timeMin = 0;
4825 std::shared_ptr<CDVDInputStream::IMenus> pMenu = std::dynamic_pointer_cast<CDVDInputStream::IMenus>(m_pInputStream);
4827 if (m_pDemuxer)
4829 if (IsInMenuInternal() && pMenu && !pMenu->CanSeek())
4830 state.chapter = 0;
4831 else
4832 state.chapter = m_pDemuxer->GetChapter();
4834 state.chapters.clear();
4835 if (m_pDemuxer->GetChapterCount() > 0)
4837 for (int i = 0, ie = m_pDemuxer->GetChapterCount(); i < ie; ++i)
4839 std::string name;
4840 m_pDemuxer->GetChapterName(name, i + 1);
4841 state.chapters.emplace_back(name, m_pDemuxer->GetChapterPos(i + 1));
4844 CServiceBroker::GetDataCacheCore().SetChapters(state.chapters);
4846 state.time = m_clock.GetClock(false) * 1000 / DVD_TIME_BASE;
4847 state.timeMax = m_pDemuxer->GetStreamLength();
4850 state.canpause = false;
4851 state.canseek = false;
4852 state.cantempo = false;
4853 state.isInMenu = false;
4854 state.menuType = MenuType::NONE;
4856 if (m_pInputStream)
4858 CDVDInputStream::IChapter* pChapter = m_pInputStream->GetIChapter();
4859 if (pChapter)
4861 if (IsInMenuInternal() && pMenu && !pMenu->CanSeek())
4862 state.chapter = 0;
4863 else
4864 state.chapter = pChapter->GetChapter();
4866 state.chapters.clear();
4867 if (pChapter->GetChapterCount() > 0)
4869 for (int i = 0, ie = pChapter->GetChapterCount(); i < ie; ++i)
4871 std::string name;
4872 pChapter->GetChapterName(name, i + 1);
4873 state.chapters.emplace_back(name, pChapter->GetChapterPos(i + 1));
4876 CServiceBroker::GetDataCacheCore().SetChapters(state.chapters);
4879 CDVDInputStream::ITimes* pTimes = m_pInputStream->GetITimes();
4880 CDVDInputStream::IDisplayTime* pDisplayTime = m_pInputStream->GetIDisplayTime();
4882 CDVDInputStream::ITimes::Times times;
4883 if (pTimes && pTimes->GetTimes(times))
4885 state.startTime = times.startTime;
4886 state.time = (m_clock.GetClock(false) - times.ptsStart) * 1000 / DVD_TIME_BASE;
4887 state.timeMax = (times.ptsEnd - times.ptsStart) * 1000 / DVD_TIME_BASE;
4888 state.timeMin = (times.ptsBegin - times.ptsStart) * 1000 / DVD_TIME_BASE;
4889 state.time_offset = -times.ptsStart;
4891 else if (pDisplayTime && pDisplayTime->GetTotalTime() > 0)
4893 if (state.dts != DVD_NOPTS_VALUE)
4895 int dispTime = 0;
4896 if (m_CurrentVideo.id >= 0 && m_CurrentVideo.dispTime)
4897 dispTime = m_CurrentVideo.dispTime;
4898 else if (m_CurrentAudio.dispTime)
4899 dispTime = m_CurrentAudio.dispTime;
4901 state.time_offset = DVD_MSEC_TO_TIME(dispTime) - state.dts;
4903 state.time += state.time_offset * 1000 / DVD_TIME_BASE;
4904 state.timeMax = pDisplayTime->GetTotalTime();
4906 else
4908 state.time_offset = 0;
4911 if (pMenu)
4913 if (!pMenu->GetState(state.player_state))
4914 state.player_state = "";
4916 if (m_dvd.state == DVDSTATE_STILL)
4918 const auto now = std::chrono::steady_clock::now();
4919 const auto duration =
4920 std::chrono::duration_cast<std::chrono::milliseconds>(now - m_dvd.iDVDStillStartTime);
4921 state.time = duration.count();
4922 state.timeMax = m_dvd.iDVDStillTime.count();
4923 state.isInMenu = true;
4925 else if (IsInMenuInternal())
4927 state.time = pDisplayTime->GetTime();
4928 state.isInMenu = true;
4929 if (!pMenu->CanSeek())
4930 state.time_offset = 0;
4932 state.menuType = pMenu->GetSupportedMenuType();
4935 state.canpause = m_pInputStream->CanPause();
4937 bool realtime = m_pInputStream->IsRealtime();
4939 if (CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(CSettings::SETTING_VIDEOPLAYER_USEDISPLAYASCLOCK) &&
4940 !realtime)
4942 state.cantempo = true;
4944 else
4946 state.cantempo = false;
4949 m_processInfo->SetStateRealtime(realtime);
4952 if (m_Edl.HasCuts())
4954 state.time = static_cast<double>(
4955 m_Edl.GetTimeWithoutCuts(std::chrono::milliseconds(std::lround(state.time))).count());
4956 state.timeMax = state.timeMax - static_cast<double>(m_Edl.GetTotalCutTime().count());
4959 if (m_caching > CACHESTATE_DONE && m_caching < CACHESTATE_PLAY)
4960 state.caching = true;
4961 else
4962 state.caching = false;
4964 double queueTime = GetQueueTime();
4965 CacheInfo cache = GetCachingTimes();
4967 if (cache.valid)
4969 state.cache_level = std::max(0.0, std::min(1.0, cache.level));
4970 state.cache_offset = cache.offset;
4971 state.cache_time = cache.time;
4973 else
4975 state.cache_level = std::min(1.0, queueTime / (m_messageQueueTimeSize * 1000.0));
4976 state.cache_offset = queueTime / state.timeMax;
4977 state.cache_time = queueTime / 1000.0;
4980 XFILE::SCacheStatus status;
4981 if (m_pInputStream && m_pInputStream->GetCacheStatus(&status))
4983 state.cache_bytes = status.forward;
4984 if(state.timeMax)
4985 state.cache_bytes += m_pInputStream->GetLength() * (int64_t)(queueTime / state.timeMax);
4987 else
4988 state.cache_bytes = 0;
4990 state.timestamp = m_clock.GetAbsoluteClock();
4992 if (state.timeMax <= 0)
4994 state.timeMax = state.time;
4995 state.timeMin = state.time;
4997 if (state.timeMin == state.timeMax)
4999 state.canseek = false;
5000 state.cantempo = false;
5002 else
5004 state.canseek = true;
5005 state.canpause = true;
5008 m_processInfo->SetPlayTimes(state.startTime, state.time, state.timeMin, state.timeMax);
5010 std::unique_lock<CCriticalSection> lock(m_StateSection);
5011 m_State = state;
5014 int64_t CVideoPlayer::GetUpdatedTime()
5016 UpdatePlayState(0);
5017 return llrint(m_State.time);
5020 void CVideoPlayer::SetDynamicRangeCompression(long drc)
5022 m_processInfo->GetVideoSettingsLocked().SetVolumeAmplification(static_cast<float>(drc) / 100);
5023 m_VideoPlayerAudio->SetDynamicRangeCompression(drc);
5026 CVideoSettings CVideoPlayer::GetVideoSettings() const
5028 return m_processInfo->GetVideoSettings();
5031 void CVideoPlayer::SetVideoSettings(CVideoSettings& settings)
5033 m_processInfo->SetVideoSettings(settings);
5034 m_renderManager.SetVideoSettings(settings);
5035 m_renderManager.SetDelay(static_cast<int>(settings.m_AudioDelay * 1000.0f));
5036 m_renderManager.SetSubtitleVerticalPosition(settings.m_subtitleVerticalPosition,
5037 settings.m_subtitleVerticalPositionSave);
5038 m_VideoPlayerVideo->EnableSubtitle(settings.m_SubtitleOn);
5039 m_VideoPlayerVideo->SetSubtitleDelay(static_cast<int>(-settings.m_SubtitleDelay * DVD_TIME_BASE));
5042 void CVideoPlayer::FrameMove()
5044 m_renderManager.FrameMove();
5047 void CVideoPlayer::Render(bool clear, uint32_t alpha, bool gui)
5049 m_renderManager.Render(clear, 0, alpha, gui);
5052 void CVideoPlayer::FlushRenderer()
5054 m_renderManager.Flush(true, true);
5057 void CVideoPlayer::SetRenderViewMode(int mode, float zoom, float par, float shift, bool stretch)
5059 m_processInfo->GetVideoSettingsLocked().SetViewMode(mode, zoom, par, shift, stretch);
5060 m_renderManager.SetVideoSettings(m_processInfo->GetVideoSettings());
5061 m_renderManager.SetViewMode(mode);
5064 float CVideoPlayer::GetRenderAspectRatio() const
5066 return m_renderManager.GetAspectRatio();
5069 void CVideoPlayer::GetRects(CRect& source, CRect& dest, CRect& view) const
5071 m_renderManager.GetVideoRect(source, dest, view);
5074 unsigned int CVideoPlayer::GetOrientation() const
5076 return m_renderManager.GetOrientation();
5079 void CVideoPlayer::TriggerUpdateResolution()
5081 std::string stereomode;
5082 m_renderManager.TriggerUpdateResolution(0, 0, 0, stereomode);
5085 bool CVideoPlayer::IsRenderingVideo() const
5087 return m_renderManager.IsConfigured();
5090 bool CVideoPlayer::Supports(EINTERLACEMETHOD method) const
5092 if (!m_processInfo)
5093 return false;
5094 return m_processInfo->Supports(method);
5097 EINTERLACEMETHOD CVideoPlayer::GetDeinterlacingMethodDefault() const
5099 if (!m_processInfo)
5100 return EINTERLACEMETHOD::VS_INTERLACEMETHOD_NONE;
5101 return m_processInfo->GetDeinterlacingMethodDefault();
5104 bool CVideoPlayer::Supports(ESCALINGMETHOD method) const
5106 return m_renderManager.Supports(method);
5109 bool CVideoPlayer::Supports(ERENDERFEATURE feature) const
5111 return m_renderManager.Supports(feature);
5114 unsigned int CVideoPlayer::RenderCaptureAlloc()
5116 return m_renderManager.AllocRenderCapture();
5119 void CVideoPlayer::RenderCapture(unsigned int captureId, unsigned int width, unsigned int height, int flags)
5121 m_renderManager.StartRenderCapture(captureId, width, height, flags);
5124 void CVideoPlayer::RenderCaptureRelease(unsigned int captureId)
5126 m_renderManager.ReleaseRenderCapture(captureId);
5129 bool CVideoPlayer::RenderCaptureGetPixels(unsigned int captureId, unsigned int millis, uint8_t *buffer, unsigned int size)
5131 return m_renderManager.RenderCaptureGetPixels(captureId, millis, buffer, size);
5134 void CVideoPlayer::VideoParamsChange()
5136 m_messenger.Put(std::make_shared<CDVDMsg>(CDVDMsg::PLAYER_AVCHANGE));
5139 void CVideoPlayer::GetDebugInfo(std::string &audio, std::string &video, std::string &general)
5141 audio = m_VideoPlayerAudio->GetPlayerInfo();
5142 video = m_VideoPlayerVideo->GetPlayerInfo();
5143 GetGeneralInfo(general);
5146 void CVideoPlayer::UpdateClockSync(bool enabled)
5148 m_processInfo->SetRenderClockSync(enabled);
5151 void CVideoPlayer::UpdateRenderInfo(CRenderInfo &info)
5153 m_processInfo->UpdateRenderInfo(info);
5156 void CVideoPlayer::UpdateRenderBuffers(int queued, int discard, int free)
5158 m_processInfo->UpdateRenderBuffers(queued, discard, free);
5161 void CVideoPlayer::UpdateGuiRender(bool gui)
5163 m_processInfo->SetGuiRender(gui);
5166 void CVideoPlayer::UpdateVideoRender(bool video)
5168 m_processInfo->SetVideoRender(video);
5171 // IDispResource interface
5172 void CVideoPlayer::OnLostDisplay()
5174 CLog::Log(LOGINFO, "VideoPlayer: OnLostDisplay received");
5175 m_VideoPlayerAudio->SendMessage(std::make_shared<CDVDMsgBool>(CDVDMsg::GENERAL_PAUSE, true), 1);
5176 m_VideoPlayerVideo->SendMessage(std::make_shared<CDVDMsgBool>(CDVDMsg::GENERAL_PAUSE, true), 1);
5177 m_clock.Pause(true);
5178 m_displayLost = true;
5179 FlushRenderer();
5182 void CVideoPlayer::OnResetDisplay()
5184 if (!m_displayLost)
5185 return;
5187 CLog::Log(LOGINFO, "VideoPlayer: OnResetDisplay received");
5188 m_VideoPlayerAudio->SendMessage(std::make_shared<CDVDMsgBool>(CDVDMsg::GENERAL_PAUSE, false), 1);
5189 m_VideoPlayerVideo->SendMessage(std::make_shared<CDVDMsgBool>(CDVDMsg::GENERAL_PAUSE, false), 1);
5190 m_clock.Pause(false);
5191 m_displayLost = false;
5192 m_VideoPlayerAudio->SendMessage(std::make_shared<CDVDMsg>(CDVDMsg::PLAYER_DISPLAY_RESET), 1);
5195 void CVideoPlayer::UpdateFileItemStreamDetails(CFileItem& item)
5197 if (!m_UpdateStreamDetails)
5198 return;
5199 m_UpdateStreamDetails = false;
5201 CLog::Log(LOGDEBUG, "CVideoPlayer: updating file item stream details with available streams");
5203 VideoStreamInfo videoInfo;
5204 AudioStreamInfo audioInfo;
5205 SubtitleStreamInfo subtitleInfo;
5206 CVideoInfoTag* info = item.GetVideoInfoTag();
5207 GetVideoStreamInfo(CURRENT_STREAM, videoInfo);
5208 info->m_streamDetails.SetStreams(videoInfo, m_processInfo->GetMaxTime() / 1000, audioInfo,
5209 subtitleInfo);
5211 //grab all the audio and subtitle info and save it
5213 for (int i = 0; i < GetAudioStreamCount(); i++)
5215 GetAudioStreamInfo(i, audioInfo);
5216 info->m_streamDetails.AddStream(new CStreamDetailAudio(audioInfo));
5219 for (int i = 0; i < GetSubtitleCount(); i++)
5221 GetSubtitleStreamInfo(i, subtitleInfo);
5222 info->m_streamDetails.AddStream(new CStreamDetailSubtitle(subtitleInfo));
5226 //------------------------------------------------------------------------------
5227 // content related methods
5228 //------------------------------------------------------------------------------
5230 void CVideoPlayer::UpdateContent()
5232 std::unique_lock<CCriticalSection> lock(m_content.m_section);
5233 m_content.m_selectionStreams = m_SelectionStreams;
5234 m_content.m_programs = m_programs;
5237 void CVideoPlayer::UpdateContentState()
5239 std::unique_lock<CCriticalSection> lock(m_content.m_section);
5241 m_content.m_videoIndex = m_SelectionStreams.TypeIndexOf(STREAM_VIDEO, m_CurrentVideo.source,
5242 m_CurrentVideo.demuxerId, m_CurrentVideo.id);
5243 m_content.m_audioIndex = m_SelectionStreams.TypeIndexOf(STREAM_AUDIO, m_CurrentAudio.source,
5244 m_CurrentAudio.demuxerId, m_CurrentAudio.id);
5245 m_content.m_subtitleIndex = m_SelectionStreams.TypeIndexOf(STREAM_SUBTITLE, m_CurrentSubtitle.source,
5246 m_CurrentSubtitle.demuxerId, m_CurrentSubtitle.id);
5248 if (m_pInputStream->IsStreamType(DVDSTREAM_TYPE_DVD) && m_content.m_videoIndex == -1 &&
5249 m_content.m_audioIndex == -1)
5251 std::shared_ptr<CDVDInputStreamNavigator> nav =
5252 std::static_pointer_cast<CDVDInputStreamNavigator>(m_pInputStream);
5254 m_content.m_videoIndex = m_SelectionStreams.TypeIndexOf(STREAM_VIDEO, STREAM_SOURCE_NAV, -1,
5255 nav->GetActiveAngle());
5256 m_content.m_audioIndex = m_SelectionStreams.TypeIndexOf(STREAM_AUDIO, STREAM_SOURCE_NAV, -1,
5257 nav->GetActiveAudioStream());
5259 // only update the subtitle index in libdvdnav if the subtitle is provided by the dvd itself,
5260 // i.e. for external subtitles the index is always greater than the subtitlecount in dvdnav
5261 if (m_content.m_subtitleIndex < nav->GetSubTitleStreamCount())
5263 m_content.m_subtitleIndex = m_SelectionStreams.TypeIndexOf(
5264 STREAM_SUBTITLE, STREAM_SOURCE_NAV, -1, nav->GetActiveSubtitleStream());
5269 void CVideoPlayer::GetVideoStreamInfo(int streamId, VideoStreamInfo& info) const
5271 std::unique_lock<CCriticalSection> lock(m_content.m_section);
5273 if (streamId == CURRENT_STREAM)
5274 streamId = m_content.m_videoIndex;
5276 if (streamId < 0 || streamId > GetVideoStreamCount() - 1)
5278 info.valid = false;
5279 return;
5282 const SelectionStream& s = m_content.m_selectionStreams.Get(STREAM_VIDEO, streamId);
5283 if (s.language.length() > 0)
5284 info.language = s.language;
5286 if (s.name.length() > 0)
5287 info.name = s.name;
5289 m_renderManager.GetVideoRect(info.SrcRect, info.DestRect, info.VideoRect);
5291 info.valid = true;
5292 info.bitrate = s.bitrate;
5293 info.width = s.width;
5294 info.height = s.height;
5295 info.codecName = s.codec;
5296 info.videoAspectRatio = s.aspect_ratio;
5297 info.stereoMode = s.stereo_mode;
5298 info.flags = s.flags;
5299 info.hdrType = s.hdrType;
5302 int CVideoPlayer::GetVideoStreamCount() const
5304 std::unique_lock<CCriticalSection> lock(m_content.m_section);
5305 return m_content.m_selectionStreams.CountType(STREAM_VIDEO);
5308 int CVideoPlayer::GetVideoStream() const
5310 std::unique_lock<CCriticalSection> lock(m_content.m_section);
5311 return m_content.m_videoIndex;
5314 void CVideoPlayer::SetVideoStream(int iStream)
5316 m_messenger.Put(std::make_shared<CDVDMsgPlayerSetVideoStream>(iStream));
5317 m_processInfo->GetVideoSettingsLocked().SetVideoStream(iStream);
5318 SynchronizeDemuxer();
5321 void CVideoPlayer::GetAudioStreamInfo(int index, AudioStreamInfo& info) const
5323 std::unique_lock<CCriticalSection> lock(m_content.m_section);
5325 if (index == CURRENT_STREAM)
5326 index = m_content.m_audioIndex;
5328 if (index < 0 || index > GetAudioStreamCount() - 1)
5330 info.valid = false;
5331 return;
5334 const SelectionStream& s = m_content.m_selectionStreams.Get(STREAM_AUDIO, index);
5335 info.language = s.language;
5336 info.name = s.name;
5338 if (s.type == STREAM_NONE)
5339 info.name += " (Invalid)";
5341 info.valid = true;
5342 info.bitrate = s.bitrate;
5343 info.channels = s.channels;
5344 info.codecName = s.codec;
5345 info.flags = s.flags;
5348 int CVideoPlayer::GetAudioStreamCount() const
5350 std::unique_lock<CCriticalSection> lock(m_content.m_section);
5351 return m_content.m_selectionStreams.CountType(STREAM_AUDIO);
5354 int CVideoPlayer::GetAudioStream()
5356 std::unique_lock<CCriticalSection> lock(m_content.m_section);
5357 return m_content.m_audioIndex;
5360 void CVideoPlayer::SetAudioStream(int iStream)
5362 m_messenger.Put(std::make_shared<CDVDMsgPlayerSetAudioStream>(iStream));
5363 m_processInfo->GetVideoSettingsLocked().SetAudioStream(iStream);
5364 SynchronizeDemuxer();
5367 void CVideoPlayer::GetSubtitleStreamInfo(int index, SubtitleStreamInfo& info) const
5369 std::unique_lock<CCriticalSection> lock(m_content.m_section);
5371 if (index == CURRENT_STREAM)
5372 index = m_content.m_subtitleIndex;
5374 if (index < 0 || index > GetSubtitleCount() - 1)
5376 info.valid = false;
5377 info.language.clear();
5378 info.flags = StreamFlags::FLAG_NONE;
5379 return;
5382 const SelectionStream& s = m_content.m_selectionStreams.Get(STREAM_SUBTITLE, index);
5383 info.name = s.name;
5385 if (s.type == STREAM_NONE)
5386 info.name += "(Invalid)";
5388 info.language = s.language;
5389 info.flags = s.flags;
5392 void CVideoPlayer::SetSubtitle(int iStream)
5394 m_messenger.Put(std::make_shared<CDVDMsgPlayerSetSubtitleStream>(iStream));
5395 m_processInfo->GetVideoSettingsLocked().SetSubtitleStream(iStream);
5398 int CVideoPlayer::GetSubtitleCount() const
5400 std::unique_lock<CCriticalSection> lock(m_content.m_section);
5401 return m_content.m_selectionStreams.CountType(STREAM_SUBTITLE);
5404 int CVideoPlayer::GetSubtitle()
5406 std::unique_lock<CCriticalSection> lock(m_content.m_section);
5407 return m_content.m_subtitleIndex;
5410 int CVideoPlayer::GetPrograms(std::vector<ProgramInfo>& programs)
5412 std::unique_lock<CCriticalSection> lock(m_content.m_section);
5413 programs = m_programs;
5414 return programs.size();
5417 void CVideoPlayer::SetProgram(int progId)
5419 m_messenger.Put(std::make_shared<CDVDMsgInt>(CDVDMsg::PLAYER_SET_PROGRAM, progId));
5422 int CVideoPlayer::GetProgramsCount() const
5424 std::unique_lock<CCriticalSection> lock(m_content.m_section);
5425 return m_programs.size();
5428 void CVideoPlayer::SetUpdateStreamDetails()
5430 m_messenger.Put(std::make_shared<CDVDMsg>(CDVDMsg::PLAYER_SET_UPDATE_STREAM_DETAILS));