1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
/*
* Copyright (C) 2017-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "PVRGUIChannelNavigator.h"
#include "FileItem.h"
#include "GUIInfoManager.h"
#include "ServiceBroker.h"
#include "guilib/GUIComponent.h"
#include "pvr/PVRManager.h"
#include "pvr/PVRPlaybackState.h"
#include "pvr/channels/PVRChannelGroup.h"
#include "pvr/guilib/PVRGUIActionsPlayback.h"
#include "settings/Settings.h"
#include "settings/SettingsComponent.h"
#include "threads/SystemClock.h"
#include "utils/Job.h"
#include "utils/JobManager.h"
#include "utils/XTimeUtils.h"
#include <mutex>
using namespace KODI::GUILIB::GUIINFO;
using namespace PVR;
using namespace std::chrono_literals;
namespace
{
class CPVRChannelTimeoutJobBase : public CJob, public IJobCallback
{
public:
CPVRChannelTimeoutJobBase() = delete;
CPVRChannelTimeoutJobBase(PVR::CPVRGUIChannelNavigator& channelNavigator,
std::chrono::milliseconds timeout)
: m_channelNavigator(channelNavigator)
{
m_delayTimer.Set(timeout);
}
~CPVRChannelTimeoutJobBase() override = default;
virtual void OnTimeout() = 0;
void OnJobComplete(unsigned int iJobID, bool bSuccess, CJob* job) override {}
bool DoWork() override
{
while (!ShouldCancel(0, 0))
{
if (m_delayTimer.IsTimePast())
{
OnTimeout();
return true;
}
KODI::TIME::Sleep(10ms);
}
return false;
}
protected:
PVR::CPVRGUIChannelNavigator& m_channelNavigator;
private:
XbmcThreads::EndTime<> m_delayTimer;
};
class CPVRChannelEntryTimeoutJob : public CPVRChannelTimeoutJobBase
{
public:
CPVRChannelEntryTimeoutJob(PVR::CPVRGUIChannelNavigator& channelNavigator,
std::chrono::milliseconds timeout)
: CPVRChannelTimeoutJobBase(channelNavigator, timeout)
{
}
~CPVRChannelEntryTimeoutJob() override = default;
const char* GetType() const override { return "pvr-channel-entry-timeout-job"; }
void OnTimeout() override { m_channelNavigator.SwitchToCurrentChannel(); }
};
class CPVRChannelInfoTimeoutJob : public CPVRChannelTimeoutJobBase
{
public:
CPVRChannelInfoTimeoutJob(PVR::CPVRGUIChannelNavigator& channelNavigator,
std::chrono::milliseconds timeout)
: CPVRChannelTimeoutJobBase(channelNavigator, timeout)
{
}
~CPVRChannelInfoTimeoutJob() override = default;
const char* GetType() const override { return "pvr-channel-info-timeout-job"; }
void OnTimeout() override { m_channelNavigator.HideInfo(); }
};
} // unnamed namespace
CPVRGUIChannelNavigator::CPVRGUIChannelNavigator()
{
// Note: we cannot subscribe to PlayerInfoProvider here, as we're getting constructed
// before the info providers. We will subscribe once our first subscriber appears.
}
CPVRGUIChannelNavigator::~CPVRGUIChannelNavigator()
{
const auto gui = CServiceBroker::GetGUI();
if (!gui)
return;
gui->GetInfoManager().GetInfoProviders().GetPlayerInfoProvider().Events().Unsubscribe(this);
}
void CPVRGUIChannelNavigator::SubscribeToShowInfoEventStream()
{
CServiceBroker::GetGUI()
->GetInfoManager()
.GetInfoProviders()
.GetPlayerInfoProvider()
.Events()
.Subscribe(this, &CPVRGUIChannelNavigator::Notify);
}
void CPVRGUIChannelNavigator::CheckAndPublishPreviewAndPlayerShowInfoChangedEvent()
{
std::unique_lock<CCriticalSection> lock(m_critSection);
const bool currentValue = IsPreview() && m_playerShowInfo;
if (m_previewAndPlayerShowInfo != currentValue)
{
m_previewAndPlayerShowInfo = currentValue;
// inform subscribers
m_events.Publish(PVRPreviewAndPlayerShowInfoChangedEvent(currentValue));
}
}
void CPVRGUIChannelNavigator::Notify(const PlayerShowInfoChangedEvent& event)
{
std::unique_lock<CCriticalSection> lock(m_critSection);
m_playerShowInfo = event.m_showInfo;
CheckAndPublishPreviewAndPlayerShowInfoChangedEvent();
}
void CPVRGUIChannelNavigator::SelectNextChannel(ChannelSwitchMode eSwitchMode)
{
std::unique_lock<CCriticalSection> lock(m_critSection);
if (!m_playerShowInfo && eSwitchMode == ChannelSwitchMode::NO_SWITCH)
{
// show info for current channel on first next channel selection.
ShowInfo(false);
return;
}
const std::shared_ptr<CPVRChannelGroupMember> nextMember = GetNextOrPrevChannel(true);
if (nextMember)
SelectChannel(nextMember, eSwitchMode);
}
void CPVRGUIChannelNavigator::SelectPreviousChannel(ChannelSwitchMode eSwitchMode)
{
std::unique_lock<CCriticalSection> lock(m_critSection);
if (!m_playerShowInfo && eSwitchMode == ChannelSwitchMode::NO_SWITCH)
{
// show info for current channel on first previous channel selection.
ShowInfo(false);
return;
}
const std::shared_ptr<CPVRChannelGroupMember> prevMember = GetNextOrPrevChannel(false);
if (prevMember)
SelectChannel(prevMember, eSwitchMode);
}
std::shared_ptr<CPVRChannelGroupMember> CPVRGUIChannelNavigator::GetNextOrPrevChannel(bool bNext)
{
const bool bPlayingRadio = CServiceBroker::GetPVRManager().PlaybackState()->IsPlayingRadio();
const bool bPlayingTV = CServiceBroker::GetPVRManager().PlaybackState()->IsPlayingTV();
if (bPlayingTV || bPlayingRadio)
{
const std::shared_ptr<CPVRChannelGroup> group =
CServiceBroker::GetPVRManager().PlaybackState()->GetActiveChannelGroup(bPlayingRadio);
if (group)
{
std::unique_lock<CCriticalSection> lock(m_critSection);
return bNext ? group->GetNextChannelGroupMember(m_currentChannel)
: group->GetPreviousChannelGroupMember(m_currentChannel);
}
}
return {};
}
void CPVRGUIChannelNavigator::SelectChannel(
const std::shared_ptr<CPVRChannelGroupMember>& groupMember, ChannelSwitchMode eSwitchMode)
{
CServiceBroker::GetGUI()->GetInfoManager().SetCurrentItem(CFileItem(groupMember));
std::unique_lock<CCriticalSection> lock(m_critSection);
m_currentChannel = groupMember;
ShowInfo(false);
CheckAndPublishPreviewAndPlayerShowInfoChangedEvent();
if (IsPreview() && eSwitchMode == ChannelSwitchMode::INSTANT_OR_DELAYED_SWITCH)
{
auto timeout =
std::chrono::milliseconds(CServiceBroker::GetSettingsComponent()->GetSettings()->GetInt(
CSettings::SETTING_PVRPLAYBACK_CHANNELENTRYTIMEOUT));
if (timeout > 0ms)
{
// delayed switch
if (m_iChannelEntryJobId >= 0)
CServiceBroker::GetJobManager()->CancelJob(m_iChannelEntryJobId);
CPVRChannelEntryTimeoutJob* job = new CPVRChannelEntryTimeoutJob(*this, timeout);
m_iChannelEntryJobId =
CServiceBroker::GetJobManager()->AddJob(job, dynamic_cast<IJobCallback*>(job));
}
else
{
// instant switch
SwitchToCurrentChannel();
}
}
}
void CPVRGUIChannelNavigator::SwitchToCurrentChannel()
{
std::unique_ptr<CFileItem> item;
{
std::unique_lock<CCriticalSection> lock(m_critSection);
if (m_iChannelEntryJobId >= 0)
{
CServiceBroker::GetJobManager()->CancelJob(m_iChannelEntryJobId);
m_iChannelEntryJobId = -1;
}
item = std::make_unique<CFileItem>(m_currentChannel);
}
if (item)
CServiceBroker::GetPVRManager().Get<PVR::GUI::Playback>().SwitchToChannel(*item, false);
}
bool CPVRGUIChannelNavigator::IsPreview() const
{
std::unique_lock<CCriticalSection> lock(m_critSection);
return m_currentChannel != m_playingChannel;
}
bool CPVRGUIChannelNavigator::IsPreviewAndShowInfo() const
{
std::unique_lock<CCriticalSection> lock(m_critSection);
return m_previewAndPlayerShowInfo;
}
void CPVRGUIChannelNavigator::ShowInfo()
{
ShowInfo(true);
}
void CPVRGUIChannelNavigator::ShowInfo(bool bForce)
{
auto timeout = std::chrono::seconds(CServiceBroker::GetSettingsComponent()->GetSettings()->GetInt(
CSettings::SETTING_PVRMENU_DISPLAYCHANNELINFO));
if (bForce || timeout > 0s)
{
CServiceBroker::GetGUI()
->GetInfoManager()
.GetInfoProviders()
.GetPlayerInfoProvider()
.SetShowInfo(true);
std::unique_lock<CCriticalSection> lock(m_critSection);
if (m_iChannelInfoJobId >= 0)
{
CServiceBroker::GetJobManager()->CancelJob(m_iChannelInfoJobId);
m_iChannelInfoJobId = -1;
}
if (!bForce && timeout > 0s)
{
CPVRChannelInfoTimeoutJob* job = new CPVRChannelInfoTimeoutJob(*this, timeout);
m_iChannelInfoJobId =
CServiceBroker::GetJobManager()->AddJob(job, dynamic_cast<IJobCallback*>(job));
}
}
}
void CPVRGUIChannelNavigator::HideInfo()
{
CServiceBroker::GetGUI()->GetInfoManager().GetInfoProviders().GetPlayerInfoProvider().SetShowInfo(
false);
CFileItemPtr item;
{
std::unique_lock<CCriticalSection> lock(m_critSection);
if (m_iChannelInfoJobId >= 0)
{
CServiceBroker::GetJobManager()->CancelJob(m_iChannelInfoJobId);
m_iChannelInfoJobId = -1;
}
if (m_currentChannel != m_playingChannel)
{
m_currentChannel = m_playingChannel;
if (m_playingChannel)
item.reset(new CFileItem(m_playingChannel));
}
CheckAndPublishPreviewAndPlayerShowInfoChangedEvent();
}
if (item)
CServiceBroker::GetGUI()->GetInfoManager().SetCurrentItem(*item);
}
void CPVRGUIChannelNavigator::ToggleInfo()
{
std::unique_lock<CCriticalSection> lock(m_critSection);
if (m_playerShowInfo)
HideInfo();
else
ShowInfo();
}
void CPVRGUIChannelNavigator::SetPlayingChannel(
const std::shared_ptr<CPVRChannelGroupMember>& groupMember)
{
CFileItemPtr item;
if (groupMember)
{
std::unique_lock<CCriticalSection> lock(m_critSection);
m_playingChannel = groupMember;
if (m_currentChannel != m_playingChannel)
{
m_currentChannel = m_playingChannel;
if (m_playingChannel)
item.reset(new CFileItem(m_playingChannel));
}
CheckAndPublishPreviewAndPlayerShowInfoChangedEvent();
}
if (item)
CServiceBroker::GetGUI()->GetInfoManager().SetCurrentItem(*item);
ShowInfo(false);
}
void CPVRGUIChannelNavigator::ClearPlayingChannel()
{
std::unique_lock<CCriticalSection> lock(m_critSection);
m_playingChannel.reset();
HideInfo();
CheckAndPublishPreviewAndPlayerShowInfoChangedEvent();
}
|