[PVR][Estuary] Timer settings dialog: Show client name in timer type selection dialog...
[xbmc.git] / xbmc / PlayListPlayer.cpp
blobb73cd665e081fc8de084b47e73b3b5204487e45f
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 "PlayListPlayer.h"
11 #include "FileItem.h"
12 #include "GUIUserMessages.h"
13 #include "PartyModeManager.h"
14 #include "ServiceBroker.h"
15 #include "URL.h"
16 #include "application/Application.h"
17 #include "application/ApplicationComponents.h"
18 #include "application/ApplicationPlayer.h"
19 #include "application/ApplicationPowerHandling.h"
20 #include "dialogs/GUIDialogKaiToast.h"
21 #include "filesystem/PluginDirectory.h"
22 #include "filesystem/VideoDatabaseFile.h"
23 #include "guilib/GUIComponent.h"
24 #include "guilib/GUIWindowManager.h"
25 #include "guilib/LocalizeStrings.h"
26 #include "input/actions/Action.h"
27 #include "input/actions/ActionIDs.h"
28 #include "interfaces/AnnouncementManager.h"
29 #include "messaging/ApplicationMessenger.h"
30 #include "messaging/helpers/DialogOKHelper.h"
31 #include "music/tags/MusicInfoTag.h"
32 #include "playlists/PlayList.h"
33 #include "settings/AdvancedSettings.h"
34 #include "settings/SettingsComponent.h"
35 #include "utils/StringUtils.h"
36 #include "utils/URIUtils.h"
37 #include "utils/Variant.h"
38 #include "utils/log.h"
39 #include "video/VideoDatabase.h"
41 using namespace PLAYLIST;
42 using namespace KODI::MESSAGING;
44 CPlayListPlayer::CPlayListPlayer(void)
46 m_PlaylistMusic = new CPlayList(TYPE_MUSIC);
47 m_PlaylistVideo = new CPlayList(TYPE_VIDEO);
48 m_PlaylistEmpty = new CPlayList;
49 m_iCurrentSong = -1;
50 m_bPlayedFirstFile = false;
51 m_bPlaybackStarted = false;
52 m_iFailedSongs = 0;
53 m_failedSongsStart = std::chrono::steady_clock::now();
56 CPlayListPlayer::~CPlayListPlayer(void)
58 Clear();
59 delete m_PlaylistMusic;
60 delete m_PlaylistVideo;
61 delete m_PlaylistEmpty;
64 bool CPlayListPlayer::OnAction(const CAction &action)
66 if (action.GetID() == ACTION_PREV_ITEM && !IsSingleItemNonRepeatPlaylist())
68 PlayPrevious();
69 return true;
71 else if (action.GetID() == ACTION_NEXT_ITEM && !IsSingleItemNonRepeatPlaylist())
73 PlayNext();
74 return true;
76 else
77 return false;
80 bool CPlayListPlayer::OnMessage(CGUIMessage &message)
82 switch (message.GetMessage())
84 case GUI_MSG_NOTIFY_ALL:
85 if (message.GetParam1() == GUI_MSG_UPDATE_ITEM && message.GetItem())
87 // update the items in our playlist(s) if necessary
88 for (Id playlistId : {TYPE_MUSIC, TYPE_VIDEO})
90 CPlayList& playlist = GetPlaylist(playlistId);
91 CFileItemPtr item = std::static_pointer_cast<CFileItem>(message.GetItem());
92 playlist.UpdateItem(item.get());
95 break;
96 case GUI_MSG_PLAYBACK_STOPPED:
98 if (m_iCurrentPlayList != TYPE_NONE && m_bPlaybackStarted)
100 CGUIMessage msg(GUI_MSG_PLAYLISTPLAYER_STOPPED, 0, 0, m_iCurrentPlayList, m_iCurrentSong);
101 CServiceBroker::GetGUI()->GetWindowManager().SendThreadMessage(msg);
102 Reset();
103 m_iCurrentPlayList = TYPE_NONE;
104 return true;
107 break;
108 case GUI_MSG_PLAYBACK_STARTED:
110 m_bPlaybackStarted = true;
112 break;
115 return false;
118 int CPlayListPlayer::GetNextSong(int offset) const
120 if (m_iCurrentPlayList == TYPE_NONE)
121 return -1;
123 const CPlayList& playlist = GetPlaylist(m_iCurrentPlayList);
124 if (playlist.size() <= 0)
125 return -1;
127 int song = m_iCurrentSong;
129 // party mode
130 if (g_partyModeManager.IsEnabled() && GetCurrentPlaylist() == TYPE_MUSIC)
131 return song + offset;
133 // wrap around in the case of repeating
134 if (RepeatedOne(m_iCurrentPlayList))
135 return song;
137 song += offset;
138 if (song >= playlist.size() && Repeated(m_iCurrentPlayList))
139 song %= playlist.size();
141 return song;
144 int CPlayListPlayer::GetNextSong()
146 if (m_iCurrentPlayList == TYPE_NONE)
147 return -1;
148 CPlayList& playlist = GetPlaylist(m_iCurrentPlayList);
149 if (playlist.size() <= 0)
150 return -1;
151 int iSong = m_iCurrentSong;
153 // party mode
154 if (g_partyModeManager.IsEnabled() && GetCurrentPlaylist() == TYPE_MUSIC)
155 return iSong + 1;
157 // if repeat one, keep playing the current song if its valid
158 if (RepeatedOne(m_iCurrentPlayList))
160 // otherwise immediately abort playback
161 if (m_iCurrentSong >= 0 && m_iCurrentSong < playlist.size() && playlist[m_iCurrentSong]->GetProperty("unplayable").asBoolean())
163 CLog::Log(LOGERROR, "Playlist Player: RepeatOne stuck on unplayable item: {}, path [{}]",
164 m_iCurrentSong, playlist[m_iCurrentSong]->GetPath());
165 CGUIMessage msg(GUI_MSG_PLAYLISTPLAYER_STOPPED, 0, 0, m_iCurrentPlayList, m_iCurrentSong);
166 CServiceBroker::GetGUI()->GetWindowManager().SendThreadMessage(msg);
167 Reset();
168 m_iCurrentPlayList = TYPE_NONE;
169 return -1;
171 return iSong;
174 // if we've gone beyond the playlist and repeat all is enabled,
175 // then we clear played status and wrap around
176 iSong++;
177 if (iSong >= playlist.size() && Repeated(m_iCurrentPlayList))
178 iSong = 0;
180 return iSong;
183 bool CPlayListPlayer::PlayNext(int offset, bool bAutoPlay)
185 int iSong = GetNextSong(offset);
186 const CPlayList& playlist = GetPlaylist(m_iCurrentPlayList);
188 if ((iSong < 0) || (iSong >= playlist.size()) || (playlist.GetPlayable() <= 0))
190 if(!bAutoPlay)
191 CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Info, g_localizeStrings.Get(559), g_localizeStrings.Get(34201));
193 CGUIMessage msg(GUI_MSG_PLAYLISTPLAYER_STOPPED, 0, 0, m_iCurrentPlayList, m_iCurrentSong);
194 CServiceBroker::GetGUI()->GetWindowManager().SendThreadMessage(msg);
195 Reset();
196 m_iCurrentPlayList = TYPE_NONE;
197 return false;
200 return Play(iSong, "", false);
203 bool CPlayListPlayer::PlayPrevious()
205 if (m_iCurrentPlayList == TYPE_NONE)
206 return false;
208 const CPlayList& playlist = GetPlaylist(m_iCurrentPlayList);
209 int iSong = m_iCurrentSong;
211 if (!RepeatedOne(m_iCurrentPlayList))
212 iSong--;
214 if (iSong < 0 && Repeated(m_iCurrentPlayList))
215 iSong = playlist.size() - 1;
217 if (iSong < 0 || playlist.size() <= 0)
219 CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Info, g_localizeStrings.Get(559), g_localizeStrings.Get(34202));
220 return false;
223 return Play(iSong, "", false, true);
226 bool CPlayListPlayer::IsSingleItemNonRepeatPlaylist() const
228 const CPlayList& playlist = GetPlaylist(m_iCurrentPlayList);
229 return (playlist.size() <= 1 && !RepeatedOne(m_iCurrentPlayList) && !Repeated(m_iCurrentPlayList));
232 bool CPlayListPlayer::Play()
234 if (m_iCurrentPlayList == TYPE_NONE)
235 return false;
237 const CPlayList& playlist = GetPlaylist(m_iCurrentPlayList);
238 if (playlist.size() <= 0)
239 return false;
241 return Play(0, "");
244 bool CPlayListPlayer::PlaySongId(int songId)
246 if (m_iCurrentPlayList == TYPE_NONE)
247 return false;
249 CPlayList& playlist = GetPlaylist(m_iCurrentPlayList);
250 if (playlist.size() <= 0)
251 return Play();
253 for (int i = 0; i < playlist.size(); i++)
255 if (playlist[i]->HasMusicInfoTag() && playlist[i]->GetMusicInfoTag()->GetDatabaseId() == songId)
256 return Play(i, "");
258 return Play();
261 bool CPlayListPlayer::Play(const CFileItemPtr& pItem, const std::string& player)
263 Id playlistId;
264 bool isVideo{pItem->IsVideo()};
265 bool isAudio{pItem->IsAudio()};
266 if (isVideo && isAudio && pItem->HasProperty("playlist_type_hint"))
268 // If an extension is set in both audio / video lists (e.g. playlist .strm),
269 // is not possible detect the type of playlist then we rely on the hint
270 playlistId = pItem->GetProperty("playlist_type_hint").asInteger32(TYPE_NONE);
272 else if (isAudio)
273 playlistId = TYPE_MUSIC;
274 else if (isVideo)
275 playlistId = TYPE_VIDEO;
276 else
278 CLog::Log(
279 LOGWARNING,
280 "Playlist Player: ListItem type must be audio or video, use ListItem::setInfo to specify!");
281 return false;
284 ClearPlaylist(playlistId);
285 Reset();
286 SetCurrentPlaylist(playlistId);
287 Add(playlistId, pItem);
289 return Play(0, player);
292 bool CPlayListPlayer::Play(int iSong,
293 const std::string& player,
294 bool bAutoPlay /* = false */,
295 bool bPlayPrevious /* = false */)
297 if (m_iCurrentPlayList == TYPE_NONE)
298 return false;
300 CPlayList& playlist = GetPlaylist(m_iCurrentPlayList);
301 if (playlist.size() <= 0)
302 return false;
303 if (iSong < 0)
304 iSong = 0;
305 if (iSong >= playlist.size())
306 iSong = playlist.size() - 1;
308 // check if the item itself is a playlist, and can be expanded
309 // only allow a few levels, this could end up in a loop
310 // if they refer to each other in a loop
311 for (int i=0; i<5; i++)
313 if(!playlist.Expand(iSong))
314 break;
317 m_iCurrentSong = iSong;
318 CFileItemPtr item = playlist[m_iCurrentSong];
319 if (item->IsVideoDb() && !item->HasVideoInfoTag())
320 *(item->GetVideoInfoTag()) = XFILE::CVideoDatabaseFile::GetVideoTag(CURL(item->GetDynPath()));
322 playlist.SetPlayed(true);
324 m_bPlaybackStarted = false;
326 const auto playAttempt = std::chrono::steady_clock::now();
327 bool ret = g_application.PlayFile(*item, player, bAutoPlay);
328 if (!ret)
330 CLog::Log(LOGERROR, "Playlist Player: skipping unplayable item: {}, path [{}]", m_iCurrentSong,
331 CURL::GetRedacted(item->GetDynPath()));
332 playlist.SetUnPlayable(m_iCurrentSong);
334 // abort on 100 failed CONSECUTIVE songs
335 if (!m_iFailedSongs)
336 m_failedSongsStart = playAttempt;
337 m_iFailedSongs++;
338 const std::shared_ptr<CAdvancedSettings> advancedSettings = CServiceBroker::GetSettingsComponent()->GetAdvancedSettings();
340 auto now = std::chrono::steady_clock::now();
341 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now - m_failedSongsStart);
343 if ((m_iFailedSongs >= advancedSettings->m_playlistRetries &&
344 advancedSettings->m_playlistRetries >= 0) ||
345 ((duration.count() >=
346 static_cast<unsigned int>(advancedSettings->m_playlistTimeout) * 1000) &&
347 advancedSettings->m_playlistTimeout))
349 CLog::Log(LOGDEBUG,"Playlist Player: one or more items failed to play... aborting playback");
351 // open error dialog
352 HELPERS::ShowOKDialogText(CVariant{16026}, CVariant{16027});
354 CGUIMessage msg(GUI_MSG_PLAYLISTPLAYER_STOPPED, 0, 0, m_iCurrentPlayList, m_iCurrentSong);
355 CServiceBroker::GetGUI()->GetWindowManager().SendThreadMessage(msg);
356 Reset();
357 GetPlaylist(m_iCurrentPlayList).Clear();
358 m_iCurrentPlayList = TYPE_NONE;
359 m_iFailedSongs = 0;
360 m_failedSongsStart = std::chrono::steady_clock::now();
361 return false;
364 // how many playable items are in the playlist?
365 if (playlist.GetPlayable() > 0)
367 return bPlayPrevious ? PlayPrevious() : PlayNext();
369 // none? then abort playback
370 else
372 CLog::Log(LOGDEBUG,"Playlist Player: no more playable items... aborting playback");
373 CGUIMessage msg(GUI_MSG_PLAYLISTPLAYER_STOPPED, 0, 0, m_iCurrentPlayList, m_iCurrentSong);
374 CServiceBroker::GetGUI()->GetWindowManager().SendThreadMessage(msg);
375 Reset();
376 m_iCurrentPlayList = TYPE_NONE;
377 return false;
381 // reset the start offset of this item
382 if (item->GetStartOffset() == STARTOFFSET_RESUME)
383 item->SetStartOffset(0);
385 //! @todo - move the above failure logic and the below success logic
386 //! to callbacks instead so we don't rely on the return value
387 //! of PlayFile()
389 // consecutive error counter so reset if the current item is playing
390 m_iFailedSongs = 0;
391 m_failedSongsStart = std::chrono::steady_clock::now();
392 m_bPlayedFirstFile = true;
393 return true;
396 void CPlayListPlayer::SetCurrentSong(int iSong)
398 if (iSong >= -1 && iSong < GetPlaylist(m_iCurrentPlayList).size())
399 m_iCurrentSong = iSong;
402 int CPlayListPlayer::GetCurrentSong() const
404 return m_iCurrentSong;
407 Id CPlayListPlayer::GetCurrentPlaylist() const
409 return m_iCurrentPlayList;
412 void CPlayListPlayer::SetCurrentPlaylist(Id playlistId)
414 if (playlistId == m_iCurrentPlayList)
415 return;
417 // changing the current playlist while party mode is on
418 // disables party mode
419 if (g_partyModeManager.IsEnabled())
420 g_partyModeManager.Disable();
422 m_iCurrentPlayList = playlistId;
423 m_bPlayedFirstFile = false;
426 void CPlayListPlayer::ClearPlaylist(Id playlistId)
428 // clear our applications playlist file
429 g_application.m_strPlayListFile.clear();
431 CPlayList& playlist = GetPlaylist(playlistId);
432 playlist.Clear();
434 // its likely that the playlist changed
435 CGUIMessage msg(GUI_MSG_PLAYLIST_CHANGED, 0, 0);
436 CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg);
439 CPlayList& CPlayListPlayer::GetPlaylist(Id playlistId)
441 switch (playlistId)
443 case TYPE_MUSIC:
444 return *m_PlaylistMusic;
445 break;
446 case TYPE_VIDEO:
447 return *m_PlaylistVideo;
448 break;
449 default:
450 m_PlaylistEmpty->Clear();
451 return *m_PlaylistEmpty;
452 break;
456 const CPlayList& CPlayListPlayer::GetPlaylist(Id playlistId) const
458 switch (playlistId)
460 case TYPE_MUSIC:
461 return *m_PlaylistMusic;
462 break;
463 case TYPE_VIDEO:
464 return *m_PlaylistVideo;
465 break;
466 default:
467 // NOTE: This playlist may not be empty if the caller of the non-const version alters it!
468 return *m_PlaylistEmpty;
469 break;
473 int CPlayListPlayer::RemoveDVDItems()
475 int nRemovedM = m_PlaylistMusic->RemoveDVDItems();
476 int nRemovedV = m_PlaylistVideo->RemoveDVDItems();
478 return nRemovedM + nRemovedV;
481 void CPlayListPlayer::Reset()
483 m_iCurrentSong = -1;
484 m_bPlayedFirstFile = false;
485 m_bPlaybackStarted = false;
487 // its likely that the playlist changed
488 CGUIMessage msg(GUI_MSG_PLAYLIST_CHANGED, 0, 0);
489 CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg);
492 bool CPlayListPlayer::HasPlayedFirstFile() const
494 return m_bPlayedFirstFile;
497 bool CPlayListPlayer::Repeated(Id playlistId) const
499 const auto repStatePos = m_repeatState.find(playlistId);
500 if (repStatePos != m_repeatState.end())
501 return repStatePos->second == RepeatState::ALL;
502 return false;
505 bool CPlayListPlayer::RepeatedOne(Id playlistId) const
507 const auto repStatePos = m_repeatState.find(playlistId);
508 if (repStatePos != m_repeatState.end())
509 return (repStatePos->second == RepeatState::ONE);
510 return false;
513 void CPlayListPlayer::SetShuffle(Id playlistId, bool bYesNo, bool bNotify /* = false */)
515 if (playlistId != TYPE_MUSIC && playlistId != TYPE_VIDEO)
516 return;
518 // disable shuffle in party mode
519 if (g_partyModeManager.IsEnabled() && playlistId == TYPE_MUSIC)
520 return;
522 // do we even need to do anything?
523 if (bYesNo != IsShuffled(playlistId))
525 // save the order value of the current song so we can use it find its new location later
526 int iOrder = -1;
527 CPlayList& playlist = GetPlaylist(playlistId);
528 if (m_iCurrentSong >= 0 && m_iCurrentSong < playlist.size())
529 iOrder = playlist[m_iCurrentSong]->m_iprogramCount;
531 // shuffle or unshuffle as necessary
532 if (bYesNo)
533 playlist.Shuffle();
534 else
535 playlist.UnShuffle();
537 if (bNotify)
539 std::string shuffleStr =
540 StringUtils::Format("{}: {}", g_localizeStrings.Get(191),
541 g_localizeStrings.Get(bYesNo ? 593 : 591)); // Shuffle: All/Off
542 CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Info, g_localizeStrings.Get(559), shuffleStr);
545 // find the previous order value and fix the current song marker
546 if (iOrder >= 0)
548 int iIndex = playlist.FindOrder(iOrder);
549 if (iIndex >= 0)
550 m_iCurrentSong = iIndex;
551 // if iIndex < 0, something unexpected happened
552 // so dont do anything
556 // its likely that the playlist changed
557 if (CServiceBroker::GetGUI() != nullptr)
559 CGUIMessage msg(GUI_MSG_PLAYLIST_CHANGED, 0, 0);
560 CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg);
563 AnnouncePropertyChanged(playlistId, "shuffled", IsShuffled(playlistId));
566 bool CPlayListPlayer::IsShuffled(Id playlistId) const
568 // even if shuffled, party mode says its not
569 if (g_partyModeManager.IsEnabled() && playlistId == TYPE_MUSIC)
570 return false;
572 if (playlistId == TYPE_MUSIC || playlistId == TYPE_VIDEO)
573 return GetPlaylist(playlistId).IsShuffled();
575 return false;
578 void CPlayListPlayer::SetRepeat(Id playlistId, RepeatState state, bool bNotify /* = false */)
580 if (playlistId != TYPE_MUSIC && playlistId != TYPE_VIDEO)
581 return;
583 // disable repeat in party mode
584 if (g_partyModeManager.IsEnabled() && playlistId == TYPE_MUSIC)
585 state = RepeatState::NONE;
587 // notify the user if there was a change in the repeat state
588 if (m_repeatState[playlistId] != state && bNotify)
590 int iLocalizedString;
591 if (state == RepeatState::NONE)
592 iLocalizedString = 595; // Repeat: Off
593 else if (state == RepeatState::ONE)
594 iLocalizedString = 596; // Repeat: One
595 else
596 iLocalizedString = 597; // Repeat: All
597 CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Info, g_localizeStrings.Get(559), g_localizeStrings.Get(iLocalizedString));
600 m_repeatState[playlistId] = state;
602 CVariant data;
603 switch (state)
605 case RepeatState::ONE:
606 data = "one";
607 break;
608 case RepeatState::ALL:
609 data = "all";
610 break;
611 default:
612 data = "off";
613 break;
616 // its likely that the playlist changed
617 if (CServiceBroker::GetGUI() != nullptr)
619 CGUIMessage msg(GUI_MSG_PLAYLIST_CHANGED, 0, 0);
620 CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg);
623 AnnouncePropertyChanged(playlistId, "repeat", data);
626 RepeatState CPlayListPlayer::GetRepeat(Id playlistId) const
628 const auto repStatePos = m_repeatState.find(playlistId);
629 if (repStatePos != m_repeatState.end())
630 return repStatePos->second;
631 return RepeatState::NONE;
634 void CPlayListPlayer::ReShuffle(Id playlistId, int iPosition)
636 // playlist has not played yet so shuffle the entire list
637 // (this only really works for new video playlists)
638 if (!GetPlaylist(playlistId).WasPlayed())
640 GetPlaylist(playlistId).Shuffle();
642 // we're trying to shuffle new items into the currently playing playlist
643 // so we shuffle starting at two positions below the current item
644 else if (playlistId == m_iCurrentPlayList)
646 const auto& components = CServiceBroker::GetAppComponents();
647 const auto appPlayer = components.GetComponent<CApplicationPlayer>();
648 if ((appPlayer->IsPlayingAudio() && playlistId == TYPE_MUSIC) ||
649 (appPlayer->IsPlayingVideo() && playlistId == TYPE_VIDEO))
651 GetPlaylist(playlistId).Shuffle(m_iCurrentSong + 2);
654 // otherwise, shuffle from the passed position
655 // which is the position of the first new item added
656 else
658 GetPlaylist(playlistId).Shuffle(iPosition);
662 void CPlayListPlayer::Add(Id playlistId, const CPlayList& playlist)
664 if (playlistId != TYPE_MUSIC && playlistId != TYPE_VIDEO)
665 return;
666 CPlayList& list = GetPlaylist(playlistId);
667 int iSize = list.size();
668 list.Add(playlist);
669 if (list.IsShuffled())
670 ReShuffle(playlistId, iSize);
673 void CPlayListPlayer::Add(Id playlistId, const CFileItemPtr& pItem)
675 if (playlistId != TYPE_MUSIC && playlistId != TYPE_VIDEO)
676 return;
677 CPlayList& list = GetPlaylist(playlistId);
678 int iSize = list.size();
679 list.Add(pItem);
680 if (list.IsShuffled())
681 ReShuffle(playlistId, iSize);
684 void CPlayListPlayer::Add(Id playlistId, const CFileItemList& items)
686 if (playlistId != TYPE_MUSIC && playlistId != TYPE_VIDEO)
687 return;
688 CPlayList& list = GetPlaylist(playlistId);
689 int iSize = list.size();
690 list.Add(items);
691 if (list.IsShuffled())
692 ReShuffle(playlistId, iSize);
694 // its likely that the playlist changed
695 CGUIMessage msg(GUI_MSG_PLAYLIST_CHANGED, 0, 0);
696 CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg);
699 void CPlayListPlayer::Insert(Id playlistId, const CPlayList& playlist, int iIndex)
701 if (playlistId != TYPE_MUSIC && playlistId != TYPE_VIDEO)
702 return;
703 CPlayList& list = GetPlaylist(playlistId);
704 int iSize = list.size();
705 list.Insert(playlist, iIndex);
706 if (list.IsShuffled())
707 ReShuffle(playlistId, iSize);
708 else if (m_iCurrentPlayList == playlistId && m_iCurrentSong >= iIndex)
709 m_iCurrentSong++;
712 void CPlayListPlayer::Insert(Id playlistId, const CFileItemPtr& pItem, int iIndex)
714 if (playlistId != TYPE_MUSIC && playlistId != TYPE_VIDEO)
715 return;
716 CPlayList& list = GetPlaylist(playlistId);
717 int iSize = list.size();
718 list.Insert(pItem, iIndex);
719 if (list.IsShuffled())
720 ReShuffle(playlistId, iSize);
721 else if (m_iCurrentPlayList == playlistId && m_iCurrentSong >= iIndex)
722 m_iCurrentSong++;
725 void CPlayListPlayer::Insert(Id playlistId, const CFileItemList& items, int iIndex)
727 if (playlistId != TYPE_MUSIC && playlistId != TYPE_VIDEO)
728 return;
729 CPlayList& list = GetPlaylist(playlistId);
730 int iSize = list.size();
731 list.Insert(items, iIndex);
732 if (list.IsShuffled())
733 ReShuffle(playlistId, iSize);
734 else if (m_iCurrentPlayList == playlistId && m_iCurrentSong >= iIndex)
735 m_iCurrentSong++;
737 // its likely that the playlist changed
738 CGUIMessage msg(GUI_MSG_PLAYLIST_CHANGED, 0, 0);
739 CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg);
742 void CPlayListPlayer::Remove(Id playlistId, int iPosition)
744 if (playlistId != TYPE_MUSIC && playlistId != TYPE_VIDEO)
745 return;
746 CPlayList& list = GetPlaylist(playlistId);
747 list.Remove(iPosition);
748 if (m_iCurrentPlayList == playlistId && m_iCurrentSong >= iPosition)
749 m_iCurrentSong--;
751 // its likely that the playlist changed
752 CGUIMessage msg(GUI_MSG_PLAYLIST_CHANGED, 0, 0);
753 CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg);
756 void CPlayListPlayer::Clear()
758 if (m_PlaylistMusic)
759 m_PlaylistMusic->Clear();
760 if (m_PlaylistVideo)
761 m_PlaylistVideo->Clear();
762 if (m_PlaylistEmpty)
763 m_PlaylistEmpty->Clear();
766 void CPlayListPlayer::Swap(Id playlistId, int indexItem1, int indexItem2)
768 if (playlistId != TYPE_MUSIC && playlistId != TYPE_VIDEO)
769 return;
771 CPlayList& list = GetPlaylist(playlistId);
772 if (list.Swap(indexItem1, indexItem2) && playlistId == m_iCurrentPlayList)
774 if (m_iCurrentSong == indexItem1)
775 m_iCurrentSong = indexItem2;
776 else if (m_iCurrentSong == indexItem2)
777 m_iCurrentSong = indexItem1;
780 // its likely that the playlist changed
781 CGUIMessage msg(GUI_MSG_PLAYLIST_CHANGED, 0, 0);
782 CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg);
785 void CPlayListPlayer::AnnouncePropertyChanged(Id playlistId,
786 const std::string& strProperty,
787 const CVariant& value)
789 const auto& components = CServiceBroker::GetAppComponents();
790 const auto appPlayer = components.GetComponent<CApplicationPlayer>();
792 if (strProperty.empty() || value.isNull() ||
793 (playlistId == TYPE_VIDEO && !appPlayer->IsPlayingVideo()) ||
794 (playlistId == TYPE_MUSIC && !appPlayer->IsPlayingAudio()))
795 return;
797 CVariant data;
798 data["player"]["playerid"] = playlistId;
799 data["property"][strProperty] = value;
800 CServiceBroker::GetAnnouncementManager()->Announce(ANNOUNCEMENT::Player, "OnPropertyChanged",
801 data);
804 int PLAYLIST::CPlayListPlayer::GetMessageMask()
806 return TMSG_MASK_PLAYLISTPLAYER;
809 void PLAYLIST::CPlayListPlayer::OnApplicationMessage(KODI::MESSAGING::ThreadMessage* pMsg)
811 auto& components = CServiceBroker::GetAppComponents();
812 const auto appPlayer = components.GetComponent<CApplicationPlayer>();
814 auto wakeScreensaver = []() {
815 auto& components = CServiceBroker::GetAppComponents();
816 const auto appPower = components.GetComponent<CApplicationPowerHandling>();
817 appPower->ResetScreenSaver();
818 appPower->WakeUpScreenSaverAndDPMS();
821 switch (pMsg->dwMessage)
823 case TMSG_PLAYLISTPLAYER_PLAY:
824 if (pMsg->param1 != -1)
825 Play(pMsg->param1, "");
826 else
827 Play();
828 break;
830 case TMSG_PLAYLISTPLAYER_PLAY_SONG_ID:
831 if (pMsg->param1 != -1)
833 bool *result = (bool*)pMsg->lpVoid;
834 *result = PlaySongId(pMsg->param1);
836 else
837 Play();
838 break;
840 case TMSG_PLAYLISTPLAYER_NEXT:
841 PlayNext();
842 break;
844 case TMSG_PLAYLISTPLAYER_PREV:
845 PlayPrevious();
846 break;
848 case TMSG_PLAYLISTPLAYER_ADD:
849 if (pMsg->lpVoid)
851 CFileItemList *list = static_cast<CFileItemList*>(pMsg->lpVoid);
853 Add(pMsg->param1, (*list));
854 delete list;
856 break;
858 case TMSG_PLAYLISTPLAYER_INSERT:
859 if (pMsg->lpVoid)
861 CFileItemList *list = static_cast<CFileItemList*>(pMsg->lpVoid);
862 Insert(pMsg->param1, (*list), pMsg->param2);
863 delete list;
865 break;
867 case TMSG_PLAYLISTPLAYER_REMOVE:
868 if (pMsg->param1 != -1)
869 Remove(pMsg->param1, pMsg->param2);
870 break;
872 case TMSG_PLAYLISTPLAYER_CLEAR:
873 ClearPlaylist(pMsg->param1);
874 break;
876 case TMSG_PLAYLISTPLAYER_SHUFFLE:
877 SetShuffle(pMsg->param1, pMsg->param2 > 0);
878 break;
880 case TMSG_PLAYLISTPLAYER_REPEAT:
881 SetRepeat(pMsg->param1, static_cast<RepeatState>(pMsg->param2));
882 break;
884 case TMSG_PLAYLISTPLAYER_GET_ITEMS:
885 if (pMsg->lpVoid)
887 PLAYLIST::CPlayList playlist = GetPlaylist(pMsg->param1);
888 CFileItemList *list = static_cast<CFileItemList*>(pMsg->lpVoid);
890 for (int i = 0; i < playlist.size(); i++)
891 list->Add(std::make_shared<CFileItem>(*playlist[i]));
893 break;
895 case TMSG_PLAYLISTPLAYER_SWAP:
896 if (pMsg->lpVoid)
898 auto indexes = static_cast<std::vector<int>*>(pMsg->lpVoid);
899 if (indexes->size() == 2)
900 Swap(pMsg->param1, indexes->at(0), indexes->at(1));
901 delete indexes;
903 break;
905 case TMSG_MEDIA_PLAY:
907 wakeScreensaver();
909 // first check if we were called from the PlayFile() function
910 if (pMsg->lpVoid && pMsg->param2 == 0)
912 // Discard the current playlist, if TMSG_MEDIA_PLAY gets posted with just a single item.
913 // Otherwise items may fail to play, when started while a playlist is playing.
914 Reset();
916 CFileItem *item = static_cast<CFileItem*>(pMsg->lpVoid);
917 g_application.PlayFile(*item, "", pMsg->param1 != 0);
918 delete item;
919 return;
922 //g_application.StopPlaying();
923 // play file
924 if (pMsg->lpVoid)
926 CFileItemList *list = static_cast<CFileItemList*>(pMsg->lpVoid);
928 if (list->Size() > 0)
930 Id playlistId = TYPE_MUSIC;
931 for (int i = 0; i < list->Size(); i++)
933 if ((*list)[i]->IsVideo())
935 playlistId = TYPE_VIDEO;
936 break;
940 ClearPlaylist(playlistId);
941 SetCurrentPlaylist(playlistId);
942 if (list->Size() == 1 && !(*list)[0]->IsPlayList())
944 CFileItemPtr item = (*list)[0];
945 // if the item is a plugin we need to resolve the URL to ensure the infotags are filled.
946 if (URIUtils::HasPluginPath(*item) &&
947 !XFILE::CPluginDirectory::GetResolvedPluginResult(*item))
949 return;
951 if (item->IsAudio() || item->IsVideo())
952 Play(item, pMsg->strParam);
953 else
954 g_application.PlayMedia(*item, pMsg->strParam, playlistId);
956 else
958 // Handle "shuffled" option if present
959 if (list->HasProperty("shuffled") && list->GetProperty("shuffled").isBoolean())
960 SetShuffle(playlistId, list->GetProperty("shuffled").asBoolean(), false);
961 // Handle "repeat" option if present
962 if (list->HasProperty("repeat") && list->GetProperty("repeat").isInteger())
963 SetRepeat(playlistId, static_cast<RepeatState>(list->GetProperty("repeat").asInteger()),
964 false);
966 Add(playlistId, (*list));
967 Play(pMsg->param1, pMsg->strParam);
971 delete list;
973 else if (pMsg->param1 == TYPE_MUSIC || pMsg->param1 == TYPE_VIDEO)
975 if (GetCurrentPlaylist() != pMsg->param1)
976 SetCurrentPlaylist(pMsg->param1);
978 CServiceBroker::GetAppMessenger()->SendMsg(TMSG_PLAYLISTPLAYER_PLAY, pMsg->param2);
981 break;
983 case TMSG_MEDIA_RESTART:
984 g_application.Restart(true);
985 break;
987 case TMSG_MEDIA_STOP:
989 // restore to previous window if needed
990 bool stopSlideshow = true;
991 bool stopVideo = true;
992 bool stopMusic = true;
994 Id playlistId = pMsg->param1;
995 if (playlistId != TYPE_NONE)
997 stopSlideshow = (playlistId == TYPE_PICTURE);
998 stopVideo = (playlistId == TYPE_VIDEO);
999 stopMusic = (playlistId == TYPE_MUSIC);
1002 if ((stopSlideshow && CServiceBroker::GetGUI()->GetWindowManager().GetActiveWindow() == WINDOW_SLIDESHOW) ||
1003 (stopVideo && CServiceBroker::GetGUI()->GetWindowManager().GetActiveWindow() == WINDOW_FULLSCREEN_VIDEO) ||
1004 (stopVideo && CServiceBroker::GetGUI()->GetWindowManager().GetActiveWindow() == WINDOW_FULLSCREEN_GAME) ||
1005 (stopMusic && CServiceBroker::GetGUI()->GetWindowManager().GetActiveWindow() == WINDOW_VISUALISATION))
1006 CServiceBroker::GetGUI()->GetWindowManager().PreviousWindow();
1008 wakeScreensaver();
1010 // stop playing file
1011 if (appPlayer->IsPlaying())
1012 g_application.StopPlaying();
1014 break;
1016 case TMSG_MEDIA_PAUSE:
1017 if (appPlayer->HasPlayer())
1019 wakeScreensaver();
1020 appPlayer->Pause();
1022 break;
1024 case TMSG_MEDIA_UNPAUSE:
1025 if (appPlayer->IsPausedPlayback())
1027 wakeScreensaver();
1028 appPlayer->Pause();
1030 break;
1032 case TMSG_MEDIA_PAUSE_IF_PLAYING:
1033 if (appPlayer->IsPlaying() && !appPlayer->IsPaused())
1035 wakeScreensaver();
1036 appPlayer->Pause();
1038 break;
1040 case TMSG_MEDIA_SEEK_TIME:
1042 if (appPlayer->IsPlaying() || appPlayer->IsPaused())
1043 appPlayer->SeekTime(pMsg->param3);
1045 break;
1047 default:
1048 break;