1 /*
2  *  Copyright (C) 2012-2019 Team Kodi
3  *  This file is part of Kodi - https://kodi.tv
4  *
5  *  SPDX-License-Identifier: GPL-2.0-or-later
6  *  See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "threads/CriticalSection.h"
12 
13 #include <memory>
14 #include <string>
15 
16 class CDateTime;
17 class CFileItem;
18 
19 namespace PVR
20 {
21 class CPVRChannel;
22 class CPVRChannelGroup;
23 class CPVREpgInfoTag;
24 class CPVRRecording;
25 
26 class CPVRPlaybackState
27 {
28 public:
29   /*!
30    * @brief ctor.
31    */
32   CPVRPlaybackState();
33 
34   /*!
35    * @brief dtor.
36    */
37   virtual ~CPVRPlaybackState();
38 
39   /*!
40    * @brief clear instances, keep stored UIDs.
41    */
42   void Clear();
43 
44   /*!
45    * @brief re-init using stored UIDs.
46    */
47   void ReInit();
48 
49   /*!
50    * @brief Inform that playback of an item just started.
51    * @param item The item that started to play.
52    */
53   void OnPlaybackStarted(const std::shared_ptr<CFileItem>& item);
54 
55   /*!
56    * @brief Inform that playback of an item was stopped due to user interaction.
57    * @param item The item that stopped to play.
58    * @return True, if the state has changed, false otherwise
59    */
60   bool OnPlaybackStopped(const std::shared_ptr<CFileItem>& item);
61 
62   /*!
63    * @brief Inform that playback of an item has stopped without user interaction.
64    * @param item The item that ended to play.
65    */
66   void OnPlaybackEnded(const std::shared_ptr<CFileItem>& item);
67 
68   /*!
69    * @brief Check if a TV channel, radio channel or recording is playing.
70    * @return True if it's playing, false otherwise.
71    */
72   bool IsPlaying() const;
73 
74   /*!
75    * @brief Check if a TV channel is playing.
76    * @return True if it's playing, false otherwise.
77    */
78   bool IsPlayingTV() const;
79 
80   /*!
81    * @brief Check if a radio channel is playing.
82    * @return True if it's playing, false otherwise.
83    */
84   bool IsPlayingRadio() const;
85 
86   /*!
87    * @brief Check if a an encrypted TV or radio channel is playing.
88    * @return True if it's playing, false otherwise.
89    */
90   bool IsPlayingEncryptedChannel() const;
91 
92   /*!
93    * @brief Check if a recording is playing.
94    * @return True if it's playing, false otherwise.
95    */
96   bool IsPlayingRecording() const;
97 
98   /*!
99    * @brief Check if an epg tag is playing.
100    * @return True if it's playing, false otherwise.
101    */
102   bool IsPlayingEpgTag() const;
103 
104   /*!
105    * @brief Check whether playing channel matches given uids.
106    * @param iClientID The client id.
107    * @param iUniqueChannelID The channel uid.
108    * @return True on match, false if there is no match or no channel is playing.
109    */
110   bool IsPlayingChannel(int iClientID, int iUniqueChannelID) const;
111 
112   /*!
113    * @brief Check if the given channel is playing.
114    * @param channel The channel to check.
115    * @return True if it's playing, false otherwise.
116    */
117   bool IsPlayingChannel(const std::shared_ptr<CPVRChannel>& channel) const;
118 
119   /*!
120    * @brief Check if the given recording is playing.
121    * @param recording The recording to check.
122    * @return True if it's playing, false otherwise.
123    */
124   bool IsPlayingRecording(const std::shared_ptr<CPVRRecording>& recording) const;
125 
126   /*!
127    * @brief Check if the given epg tag is playing.
128    * @param epgTag The tag to check.
129    * @return True if it's playing, false otherwise.
130    */
131   bool IsPlayingEpgTag(const std::shared_ptr<CPVREpgInfoTag>& epgTag) const;
132 
133   /*!
134    * @brief Return the channel that is currently playing.
135    * @return The channel or nullptr if none is playing.
136    */
137   std::shared_ptr<CPVRChannel> GetPlayingChannel() const;
138 
139   /*!
140    * @brief Return the recording that is currently playing.
141    * @return The recording or nullptr if none is playing.
142    */
143   std::shared_ptr<CPVRRecording> GetPlayingRecording() const;
144 
145   /*!
146    * @brief Return the epg tag that is currently playing.
147    * @return The tag or nullptr if none is playing.
148    */
149   std::shared_ptr<CPVREpgInfoTag> GetPlayingEpgTag() const;
150 
151   /*!
152    * @brief Return playing channel unique identifier
153    * @return The channel id or -1 if not present
154    */
155   int GetPlayingChannelUniqueID() const;
156 
157   /*!
158    * @brief Get the name of the playing client, if there is one.
159    * @return The name of the client or an empty string if nothing is playing.
160    */
161   std::string GetPlayingClientName() const;
162 
163   /*!
164    * @brief Get the ID of the playing client, if there is one.
165    * @return The ID or -1 if no client is playing.
166    */
167   int GetPlayingClientID() const;
168 
169   /*!
170    * @brief Check whether there are active recordings.
171    * @return True if there are active recordings, false otherwise.
172    */
173   bool IsRecording() const;
174 
175   /*!
176    * @brief Check whether there is an active recording on the currenlyt playing channel.
177    * @return True if there is a playing channel and there is an active recording on that channel, false otherwise.
178    */
179   bool IsRecordingOnPlayingChannel() const;
180 
181   /*!
182    * @brief Check if an active recording is playing.
183    * @return True if an in-progress (active) recording is playing, false otherwise.
184    */
185   bool IsPlayingActiveRecording() const;
186 
187   /*!
188    * @brief Check whether the currently playing channel can be recorded.
189    * @return True if there is a playing channel that can be recorded, false otherwise.
190    */
191   bool CanRecordOnPlayingChannel() const;
192 
193   /*!
194    * @brief Set the current playing group, used to load the right channel.
195    * @param group The new group.
196    */
197   void SetPlayingGroup(const std::shared_ptr<CPVRChannelGroup>& group);
198 
199   /*!
200    * @brief Get the current playing group, used to load the right channel.
201    * @param bRadio True to get the current radio group, false to get the current TV group.
202    * @return The current group or the group containing all channels if it's not set.
203    */
204   std::shared_ptr<CPVRChannelGroup> GetPlayingGroup(bool bRadio) const;
205 
206   /*!
207    * @brief Get current playback time for the given channel, taking timeshifting and playing
208    * epg tags into account.
209    * @param iClientID The client id.
210    * @param iUniqueChannelID The channel uid.
211    * @return The playback time or 'now' if not playing.
212    */
213   CDateTime GetPlaybackTime(int iClientID, int iUniqueChannelID) const;
214 
215   /*!
216    * @brief Get current playback time for the given channel, taking timeshifting into account.
217    * @param iClientID The client id.
218    * @param iUniqueChannelID The channel uid.
219    * @return The playback time or 'now' if not playing.
220    */
221   CDateTime GetChannelPlaybackTime(int iClientID, int iUniqueChannelID) const;
222 
223 private:
224   /*!
225    * @brief Set the playing group to the first group the channel is in if the given channel is not part of the current playing group
226    * @param channel The channel
227    */
228   void SetPlayingGroup(const std::shared_ptr<CPVRChannel>& channel);
229 
230   /*!
231    * @brief Updates the last watched timestamps of the channel and group which are currently playing.
232    * @param channel The channel which is updated
233    * @param time The last watched time to set
234    */
235   void UpdateLastWatched(const std::shared_ptr<CPVRChannel>& channel, const CDateTime& time);
236 
237   mutable CCriticalSection m_critSection;
238 
239   std::shared_ptr<CPVRChannel> m_playingChannel;
240   std::shared_ptr<CPVRRecording> m_playingRecording;
241   std::shared_ptr<CPVREpgInfoTag> m_playingEpgTag;
242   std::string m_strPlayingClientName;
243   int m_playingClientId = -1;
244   int m_playingChannelUniqueId = -1;
245   std::string m_strPlayingRecordingUniqueId;
246   int m_playingEpgTagChannelUniqueId = -1;
247   unsigned int m_playingEpgTagUniqueId = 0;
248 
249   class CLastWatchedUpdateTimer;
250   std::unique_ptr<CLastWatchedUpdateTimer> m_lastWatchedUpdateTimer;
251 };
252 } // namespace PVR
253