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.
9 #include "PlayListPlayer.h"
12 #include "GUIUserMessages.h"
13 #include "PartyModeManager.h"
14 #include "ServiceBroker.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
;
50 m_bPlayedFirstFile
= false;
51 m_bPlaybackStarted
= false;
53 m_failedSongsStart
= std::chrono::steady_clock::now();
56 CPlayListPlayer::~CPlayListPlayer(void)
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())
71 else if (action
.GetID() == ACTION_NEXT_ITEM
&& !IsSingleItemNonRepeatPlaylist())
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());
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
);
103 m_iCurrentPlayList
= TYPE_NONE
;
108 case GUI_MSG_PLAYBACK_STARTED
:
110 m_bPlaybackStarted
= true;
118 int CPlayListPlayer::GetNextSong(int offset
) const
120 if (m_iCurrentPlayList
== TYPE_NONE
)
123 const CPlayList
& playlist
= GetPlaylist(m_iCurrentPlayList
);
124 if (playlist
.size() <= 0)
127 int song
= m_iCurrentSong
;
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
))
138 if (song
>= playlist
.size() && Repeated(m_iCurrentPlayList
))
139 song
%= playlist
.size();
144 int CPlayListPlayer::GetNextSong()
146 if (m_iCurrentPlayList
== TYPE_NONE
)
148 CPlayList
& playlist
= GetPlaylist(m_iCurrentPlayList
);
149 if (playlist
.size() <= 0)
151 int iSong
= m_iCurrentSong
;
154 if (g_partyModeManager
.IsEnabled() && GetCurrentPlaylist() == TYPE_MUSIC
)
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
);
168 m_iCurrentPlayList
= TYPE_NONE
;
174 // if we've gone beyond the playlist and repeat all is enabled,
175 // then we clear played status and wrap around
177 if (iSong
>= playlist
.size() && Repeated(m_iCurrentPlayList
))
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))
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
);
196 m_iCurrentPlayList
= TYPE_NONE
;
200 return Play(iSong
, "", false);
203 bool CPlayListPlayer::PlayPrevious()
205 if (m_iCurrentPlayList
== TYPE_NONE
)
208 const CPlayList
& playlist
= GetPlaylist(m_iCurrentPlayList
);
209 int iSong
= m_iCurrentSong
;
211 if (!RepeatedOne(m_iCurrentPlayList
))
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));
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
)
237 const CPlayList
& playlist
= GetPlaylist(m_iCurrentPlayList
);
238 if (playlist
.size() <= 0)
244 bool CPlayListPlayer::PlaySongId(int songId
)
246 if (m_iCurrentPlayList
== TYPE_NONE
)
249 CPlayList
& playlist
= GetPlaylist(m_iCurrentPlayList
);
250 if (playlist
.size() <= 0)
253 for (int i
= 0; i
< playlist
.size(); i
++)
255 if (playlist
[i
]->HasMusicInfoTag() && playlist
[i
]->GetMusicInfoTag()->GetDatabaseId() == songId
)
261 bool CPlayListPlayer::Play(const CFileItemPtr
& pItem
, const std::string
& player
)
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
);
273 playlistId
= TYPE_MUSIC
;
275 playlistId
= TYPE_VIDEO
;
280 "Playlist Player: ListItem type must be audio or video, use ListItem::setInfo to specify!");
284 ClearPlaylist(playlistId
);
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
)
300 CPlayList
& playlist
= GetPlaylist(m_iCurrentPlayList
);
301 if (playlist
.size() <= 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
))
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
);
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
336 m_failedSongsStart
= playAttempt
;
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");
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
);
357 GetPlaylist(m_iCurrentPlayList
).Clear();
358 m_iCurrentPlayList
= TYPE_NONE
;
360 m_failedSongsStart
= std::chrono::steady_clock::now();
364 // how many playable items are in the playlist?
365 if (playlist
.GetPlayable() > 0)
367 return bPlayPrevious
? PlayPrevious() : PlayNext();
369 // none? then abort playback
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
);
376 m_iCurrentPlayList
= TYPE_NONE
;
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
389 // consecutive error counter so reset if the current item is playing
391 m_failedSongsStart
= std::chrono::steady_clock::now();
392 m_bPlayedFirstFile
= 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
)
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
);
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
)
444 return *m_PlaylistMusic
;
447 return *m_PlaylistVideo
;
450 m_PlaylistEmpty
->Clear();
451 return *m_PlaylistEmpty
;
456 const CPlayList
& CPlayListPlayer::GetPlaylist(Id playlistId
) const
461 return *m_PlaylistMusic
;
464 return *m_PlaylistVideo
;
467 // NOTE: This playlist may not be empty if the caller of the non-const version alters it!
468 return *m_PlaylistEmpty
;
473 int CPlayListPlayer::RemoveDVDItems()
475 int nRemovedM
= m_PlaylistMusic
->RemoveDVDItems();
476 int nRemovedV
= m_PlaylistVideo
->RemoveDVDItems();
478 return nRemovedM
+ nRemovedV
;
481 void CPlayListPlayer::Reset()
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
;
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
);
513 void CPlayListPlayer::SetShuffle(Id playlistId
, bool bYesNo
, bool bNotify
/* = false */)
515 if (playlistId
!= TYPE_MUSIC
&& playlistId
!= TYPE_VIDEO
)
518 // disable shuffle in party mode
519 if (g_partyModeManager
.IsEnabled() && playlistId
== TYPE_MUSIC
)
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
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
535 playlist
.UnShuffle();
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
548 int iIndex
= playlist
.FindOrder(iOrder
);
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
)
572 if (playlistId
== TYPE_MUSIC
|| playlistId
== TYPE_VIDEO
)
573 return GetPlaylist(playlistId
).IsShuffled();
578 void CPlayListPlayer::SetRepeat(Id playlistId
, RepeatState state
, bool bNotify
/* = false */)
580 if (playlistId
!= TYPE_MUSIC
&& playlistId
!= TYPE_VIDEO
)
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
596 iLocalizedString
= 597; // Repeat: All
597 CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Info
, g_localizeStrings
.Get(559), g_localizeStrings
.Get(iLocalizedString
));
600 m_repeatState
[playlistId
] = state
;
605 case RepeatState::ONE
:
608 case RepeatState::ALL
:
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
658 GetPlaylist(playlistId
).Shuffle(iPosition
);
662 void CPlayListPlayer::Add(Id playlistId
, const CPlayList
& playlist
)
664 if (playlistId
!= TYPE_MUSIC
&& playlistId
!= TYPE_VIDEO
)
666 CPlayList
& list
= GetPlaylist(playlistId
);
667 int iSize
= list
.size();
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
)
677 CPlayList
& list
= GetPlaylist(playlistId
);
678 int iSize
= list
.size();
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
)
688 CPlayList
& list
= GetPlaylist(playlistId
);
689 int iSize
= list
.size();
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
)
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
)
712 void CPlayListPlayer::Insert(Id playlistId
, const CFileItemPtr
& pItem
, int iIndex
)
714 if (playlistId
!= TYPE_MUSIC
&& playlistId
!= TYPE_VIDEO
)
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
)
725 void CPlayListPlayer::Insert(Id playlistId
, const CFileItemList
& items
, int iIndex
)
727 if (playlistId
!= TYPE_MUSIC
&& playlistId
!= TYPE_VIDEO
)
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
)
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
)
746 CPlayList
& list
= GetPlaylist(playlistId
);
747 list
.Remove(iPosition
);
748 if (m_iCurrentPlayList
== playlistId
&& m_iCurrentSong
>= iPosition
)
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()
759 m_PlaylistMusic
->Clear();
761 m_PlaylistVideo
->Clear();
763 m_PlaylistEmpty
->Clear();
766 void CPlayListPlayer::Swap(Id playlistId
, int indexItem1
, int indexItem2
)
768 if (playlistId
!= TYPE_MUSIC
&& playlistId
!= TYPE_VIDEO
)
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()))
798 data
["player"]["playerid"] = playlistId
;
799 data
["property"][strProperty
] = value
;
800 CServiceBroker::GetAnnouncementManager()->Announce(ANNOUNCEMENT::Player
, "OnPropertyChanged",
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
, "");
830 case TMSG_PLAYLISTPLAYER_PLAY_SONG_ID
:
831 if (pMsg
->param1
!= -1)
833 bool *result
= (bool*)pMsg
->lpVoid
;
834 *result
= PlaySongId(pMsg
->param1
);
840 case TMSG_PLAYLISTPLAYER_NEXT
:
844 case TMSG_PLAYLISTPLAYER_PREV
:
848 case TMSG_PLAYLISTPLAYER_ADD
:
851 CFileItemList
*list
= static_cast<CFileItemList
*>(pMsg
->lpVoid
);
853 Add(pMsg
->param1
, (*list
));
858 case TMSG_PLAYLISTPLAYER_INSERT
:
861 CFileItemList
*list
= static_cast<CFileItemList
*>(pMsg
->lpVoid
);
862 Insert(pMsg
->param1
, (*list
), pMsg
->param2
);
867 case TMSG_PLAYLISTPLAYER_REMOVE
:
868 if (pMsg
->param1
!= -1)
869 Remove(pMsg
->param1
, pMsg
->param2
);
872 case TMSG_PLAYLISTPLAYER_CLEAR
:
873 ClearPlaylist(pMsg
->param1
);
876 case TMSG_PLAYLISTPLAYER_SHUFFLE
:
877 SetShuffle(pMsg
->param1
, pMsg
->param2
> 0);
880 case TMSG_PLAYLISTPLAYER_REPEAT
:
881 SetRepeat(pMsg
->param1
, static_cast<RepeatState
>(pMsg
->param2
));
884 case TMSG_PLAYLISTPLAYER_GET_ITEMS
:
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
]));
895 case TMSG_PLAYLISTPLAYER_SWAP
:
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));
905 case TMSG_MEDIA_PLAY
:
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.
916 CFileItem
*item
= static_cast<CFileItem
*>(pMsg
->lpVoid
);
917 g_application
.PlayFile(*item
, "", pMsg
->param1
!= 0);
922 //g_application.StopPlaying();
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
;
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
))
951 if (item
->IsAudio() || item
->IsVideo())
952 Play(item
, pMsg
->strParam
);
954 g_application
.PlayMedia(*item
, pMsg
->strParam
, playlistId
);
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()),
966 Add(playlistId
, (*list
));
967 Play(pMsg
->param1
, pMsg
->strParam
);
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
);
983 case TMSG_MEDIA_RESTART
:
984 g_application
.Restart(true);
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();
1010 // stop playing file
1011 if (appPlayer
->IsPlaying())
1012 g_application
.StopPlaying();
1016 case TMSG_MEDIA_PAUSE
:
1017 if (appPlayer
->HasPlayer())
1024 case TMSG_MEDIA_UNPAUSE
:
1025 if (appPlayer
->IsPausedPlayback())
1032 case TMSG_MEDIA_PAUSE_IF_PLAYING
:
1033 if (appPlayer
->IsPlaying() && !appPlayer
->IsPaused())
1040 case TMSG_MEDIA_SEEK_TIME
:
1042 if (appPlayer
->IsPlaying() || appPlayer
->IsPaused())
1043 appPlayer
->SeekTime(pMsg
->param3
);