| 1 |
/* |
| 2 |
* Copyright (C) 2012-2018 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 |
#include "PVRGUITimesInfo.h" |
| 10 |
|
| 11 |
#include "ServiceBroker.h" |
| 12 |
#include "cores/DataCacheCore.h" |
| 13 |
#include "pvr/PVRManager.h" |
| 14 |
#include "pvr/PVRPlaybackState.h" |
| 15 |
#include "pvr/channels/PVRChannel.h" |
| 16 |
#include "pvr/channels/PVRChannelGroupsContainer.h" |
| 17 |
#include "pvr/epg/EpgInfoTag.h" |
| 18 |
#include "pvr/recordings/PVRRecording.h" |
| 19 |
#include "settings/AdvancedSettings.h" |
| 20 |
#include "settings/SettingsComponent.h" |
| 21 |
#include "utils/StringUtils.h" |
| 22 |
|
| 23 |
#include <cmath> |
| 24 |
#include <ctime> |
| 25 |
#include <memory> |
| 26 |
#include <mutex> |
| 27 |
|
| 28 |
using namespace PVR; |
| 29 |
|
| 30 |
CPVRGUITimesInfo::CPVRGUITimesInfo() |
| 31 |
{ |
| 32 |
Reset(); |
| 33 |
} |
| 34 |
|
| 35 |
void CPVRGUITimesInfo::Reset() |
| 36 |
{ |
| 37 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 38 |
|
| 39 |
m_iStartTime = 0; |
| 40 |
m_iDuration = 0; |
| 41 |
m_iTimeshiftStartTime = 0; |
| 42 |
m_iTimeshiftEndTime = 0; |
| 43 |
m_iTimeshiftPlayTime = 0; |
| 44 |
m_iTimeshiftOffset = 0; |
| 45 |
|
| 46 |
m_iTimeshiftProgressStartTime = 0; |
| 47 |
m_iTimeshiftProgressEndTime = 0; |
| 48 |
m_iTimeshiftProgressDuration = 0; |
| 49 |
|
| 50 |
m_playingEpgTag.reset(); |
| 51 |
m_playingChannel.reset(); |
| 52 |
} |
| 53 |
|
| 54 |
void CPVRGUITimesInfo::UpdatePlayingTag() |
| 55 |
{ |
| 56 |
const std::shared_ptr<const CPVRChannel> currentChannel = |
| 57 |
CServiceBroker::GetPVRManager().PlaybackState()->GetPlayingChannel(); |
| 58 |
std::shared_ptr<CPVREpgInfoTag> currentTag = CServiceBroker::GetPVRManager().PlaybackState()->GetPlayingEpgTag(); |
| 59 |
|
| 60 |
if (currentChannel || currentTag) |
| 61 |
{ |
| 62 |
if (currentChannel && !currentTag) |
| 63 |
currentTag = currentChannel->GetEPGNow(); |
| 64 |
|
| 65 |
const std::shared_ptr<const CPVRChannelGroupsContainer> groups = |
| 66 |
CServiceBroker::GetPVRManager().ChannelGroups(); |
| 67 |
|
| 68 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 69 |
|
| 70 |
const std::shared_ptr<const CPVRChannel> playingChannel = |
| 71 |
m_playingEpgTag ? groups->GetChannelForEpgTag(m_playingEpgTag) : nullptr; |
| 72 |
|
| 73 |
if (!m_playingEpgTag || !currentTag || !playingChannel || !currentChannel || |
| 74 |
m_playingEpgTag->StartAsUTC() != currentTag->StartAsUTC() || |
| 75 |
m_playingEpgTag->EndAsUTC() != currentTag->EndAsUTC() || *playingChannel != *currentChannel) |
| 76 |
{ |
| 77 |
if (currentTag) |
| 78 |
{ |
| 79 |
m_playingEpgTag = currentTag; |
| 80 |
m_iDuration = m_playingEpgTag->GetDuration(); |
| 81 |
} |
| 82 |
else if (m_iTimeshiftEndTime > m_iTimeshiftStartTime) |
| 83 |
{ |
| 84 |
m_playingEpgTag.reset(); |
| 85 |
m_iDuration = m_iTimeshiftEndTime - m_iTimeshiftStartTime; |
| 86 |
} |
| 87 |
else |
| 88 |
{ |
| 89 |
m_playingEpgTag.reset(); |
| 90 |
m_iDuration = 0; |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
else |
| 95 |
{ |
| 96 |
const std::shared_ptr<const CPVRRecording> recording = |
| 97 |
CServiceBroker::GetPVRManager().PlaybackState()->GetPlayingRecording(); |
| 98 |
if (recording) |
| 99 |
{ |
| 100 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 101 |
m_playingEpgTag.reset(); |
| 102 |
m_iDuration = recording->GetDuration(); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
void CPVRGUITimesInfo::UpdateTimeshiftData() |
| 108 |
{ |
| 109 |
if (!CServiceBroker::GetPVRManager().PlaybackState()->IsPlayingTV() && !CServiceBroker::GetPVRManager().PlaybackState()->IsPlayingRadio()) |
| 110 |
{ |
| 111 |
// If nothing is playing (anymore), there is no need to update data. |
| 112 |
Reset(); |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
time_t now = std::time(nullptr); |
| 117 |
time_t iStartTime; |
| 118 |
int64_t iPlayTime, iMinTime, iMaxTime; |
| 119 |
CServiceBroker::GetDataCacheCore().GetPlayTimes(iStartTime, iPlayTime, iMinTime, iMaxTime); |
| 120 |
bool bPlaying = CServiceBroker::GetDataCacheCore().GetSpeed() == 1.0f; |
| 121 |
const std::shared_ptr<const CPVRChannel> playingChannel = |
| 122 |
CServiceBroker::GetPVRManager().PlaybackState()->GetPlayingChannel(); |
| 123 |
|
| 124 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 125 |
|
| 126 |
if (playingChannel != m_playingChannel) |
| 127 |
{ |
| 128 |
// playing channel changed. we need to reset offset and playtime. |
| 129 |
m_iTimeshiftOffset = 0; |
| 130 |
m_iTimeshiftPlayTime = 0; |
| 131 |
m_playingChannel = playingChannel; |
| 132 |
} |
| 133 |
|
| 134 |
if (!iStartTime) |
| 135 |
{ |
| 136 |
if (m_iStartTime == 0) |
| 137 |
iStartTime = now; |
| 138 |
else |
| 139 |
iStartTime = m_iStartTime; |
| 140 |
|
| 141 |
iMinTime = iPlayTime; |
| 142 |
iMaxTime = iPlayTime; |
| 143 |
} |
| 144 |
|
| 145 |
m_iStartTime = iStartTime; |
| 146 |
m_iTimeshiftStartTime = iStartTime + iMinTime / 1000; |
| 147 |
m_iTimeshiftEndTime = iStartTime + iMaxTime / 1000; |
| 148 |
|
| 149 |
if (m_iTimeshiftEndTime > m_iTimeshiftStartTime) |
| 150 |
{ |
| 151 |
// timeshifting supported |
| 152 |
m_iTimeshiftPlayTime = iStartTime + iPlayTime / 1000; |
| 153 |
if (iMaxTime > iPlayTime) |
| 154 |
m_iTimeshiftOffset = (iMaxTime - iPlayTime) / 1000; |
| 155 |
else |
| 156 |
m_iTimeshiftOffset = 0; |
| 157 |
} |
| 158 |
else |
| 159 |
{ |
| 160 |
// timeshifting not supported |
| 161 |
if (bPlaying) |
| 162 |
m_iTimeshiftPlayTime = now - m_iTimeshiftOffset; |
| 163 |
|
| 164 |
m_iTimeshiftOffset = now - m_iTimeshiftPlayTime; |
| 165 |
} |
| 166 |
|
| 167 |
UpdateTimeshiftProgressData(); |
| 168 |
} |
| 169 |
|
| 170 |
void CPVRGUITimesInfo::UpdateTimeshiftProgressData() |
| 171 |
{ |
| 172 |
// Note: General idea of the ts progress is always to be able to visualise both the complete |
| 173 |
// ts buffer and the complete playing epg event (if any) side by side with the same time |
| 174 |
// scale. TS progress start and end times will be calculated accordingly. |
| 175 |
// + Start is usually ts buffer start, except if start time of playing epg event is |
| 176 |
// before ts buffer start, then progress start is epg event start. |
| 177 |
// + End is usually ts buffer end, except if end time of playing epg event is |
| 178 |
// after ts buffer end, then progress end is epg event end. |
| 179 |
// In simple timeshift mode (settings value), progress start is always the start time of |
| 180 |
// playing epg event and progress end is always the end time of playing epg event. |
| 181 |
|
| 182 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 183 |
|
| 184 |
////////////////////////////////////////////////////////////////////////////////////// |
| 185 |
// start time |
| 186 |
////////////////////////////////////////////////////////////////////////////////////// |
| 187 |
bool bUpdatedStartTime = false; |
| 188 |
if (m_playingEpgTag) |
| 189 |
{ |
| 190 |
time_t start = 0; |
| 191 |
m_playingEpgTag->StartAsUTC().GetAsTime(start); |
| 192 |
if (start < m_iTimeshiftStartTime || |
| 193 |
CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_bPVRTimeshiftSimpleOSD) |
| 194 |
{ |
| 195 |
// playing event started before start of ts buffer or simple ts osd to be used |
| 196 |
m_iTimeshiftProgressStartTime = start; |
| 197 |
bUpdatedStartTime = true; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
if (!bUpdatedStartTime) |
| 202 |
{ |
| 203 |
// default to ts buffer start |
| 204 |
m_iTimeshiftProgressStartTime = m_iTimeshiftStartTime; |
| 205 |
} |
| 206 |
|
| 207 |
////////////////////////////////////////////////////////////////////////////////////// |
| 208 |
// end time |
| 209 |
////////////////////////////////////////////////////////////////////////////////////// |
| 210 |
bool bUpdatedEndTime = false; |
| 211 |
if (m_playingEpgTag) |
| 212 |
{ |
| 213 |
time_t end = 0; |
| 214 |
m_playingEpgTag->EndAsUTC().GetAsTime(end); |
| 215 |
if (end > m_iTimeshiftEndTime || |
| 216 |
CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_bPVRTimeshiftSimpleOSD) |
| 217 |
{ |
| 218 |
// playing event will end after end of ts buffer or simple ts osd to be used |
| 219 |
m_iTimeshiftProgressEndTime = end; |
| 220 |
bUpdatedEndTime = true; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
if (!bUpdatedEndTime) |
| 225 |
{ |
| 226 |
// default to ts buffer end |
| 227 |
m_iTimeshiftProgressEndTime = m_iTimeshiftEndTime; |
| 228 |
} |
| 229 |
|
| 230 |
////////////////////////////////////////////////////////////////////////////////////// |
| 231 |
// duration |
| 232 |
////////////////////////////////////////////////////////////////////////////////////// |
| 233 |
m_iTimeshiftProgressDuration = m_iTimeshiftProgressEndTime - m_iTimeshiftProgressStartTime; |
| 234 |
} |
| 235 |
|
| 236 |
void CPVRGUITimesInfo::Update() |
| 237 |
{ |
| 238 |
UpdatePlayingTag(); |
| 239 |
UpdateTimeshiftData(); |
| 240 |
} |
| 241 |
|
| 242 |
std::string CPVRGUITimesInfo::TimeToTimeString(time_t datetime, TIME_FORMAT format, bool withSeconds) |
| 243 |
{ |
| 244 |
CDateTime time(datetime); |
| 245 |
return time.GetAsLocalDateTime().GetAsLocalizedTime(format, withSeconds); |
| 246 |
} |
| 247 |
|
| 248 |
std::string CPVRGUITimesInfo::GetTimeshiftStartTime(TIME_FORMAT format) const |
| 249 |
{ |
| 250 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 251 |
return TimeToTimeString(m_iTimeshiftStartTime, format, false); |
| 252 |
} |
| 253 |
|
| 254 |
std::string CPVRGUITimesInfo::GetTimeshiftEndTime(TIME_FORMAT format) const |
| 255 |
{ |
| 256 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 257 |
return TimeToTimeString(m_iTimeshiftEndTime, format, false); |
| 258 |
} |
| 259 |
|
| 260 |
std::string CPVRGUITimesInfo::GetTimeshiftPlayTime(TIME_FORMAT format) const |
| 261 |
{ |
| 262 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 263 |
return TimeToTimeString(m_iTimeshiftPlayTime, format, true); |
| 264 |
} |
| 265 |
|
| 266 |
std::string CPVRGUITimesInfo::GetTimeshiftOffset(TIME_FORMAT format) const |
| 267 |
{ |
| 268 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 269 |
return StringUtils::SecondsToTimeString(m_iTimeshiftOffset, format); |
| 270 |
} |
| 271 |
|
| 272 |
std::string CPVRGUITimesInfo::GetTimeshiftProgressDuration(TIME_FORMAT format) const |
| 273 |
{ |
| 274 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 275 |
return StringUtils::SecondsToTimeString(m_iTimeshiftProgressDuration, format); |
| 276 |
} |
| 277 |
|
| 278 |
std::string CPVRGUITimesInfo::GetTimeshiftProgressStartTime(TIME_FORMAT format) const |
| 279 |
{ |
| 280 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 281 |
return TimeToTimeString(m_iTimeshiftProgressStartTime, format, false); |
| 282 |
} |
| 283 |
|
| 284 |
std::string CPVRGUITimesInfo::GetTimeshiftProgressEndTime(TIME_FORMAT format) const |
| 285 |
{ |
| 286 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 287 |
return TimeToTimeString(m_iTimeshiftProgressEndTime, format, false); |
| 288 |
} |
| 289 |
|
| 290 |
std::string CPVRGUITimesInfo::GetEpgEventDuration( |
| 291 |
const std::shared_ptr<const CPVREpgInfoTag>& epgTag, TIME_FORMAT format) const |
| 292 |
{ |
| 293 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 294 |
return StringUtils::SecondsToTimeString(GetEpgEventDuration(epgTag), format); |
| 295 |
} |
| 296 |
|
| 297 |
std::string CPVRGUITimesInfo::GetEpgEventElapsedTime( |
| 298 |
const std::shared_ptr<const CPVREpgInfoTag>& epgTag, TIME_FORMAT format) const |
| 299 |
{ |
| 300 |
int iElapsed = 0; |
| 301 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 302 |
if (epgTag && m_playingEpgTag && *epgTag != *m_playingEpgTag) |
| 303 |
iElapsed = epgTag->Progress(); |
| 304 |
else |
| 305 |
iElapsed = GetElapsedTime(); |
| 306 |
|
| 307 |
return StringUtils::SecondsToTimeString(iElapsed, format); |
| 308 |
} |
| 309 |
|
| 310 |
std::string CPVRGUITimesInfo::GetEpgEventRemainingTime( |
| 311 |
const std::shared_ptr<const CPVREpgInfoTag>& epgTag, TIME_FORMAT format) const |
| 312 |
{ |
| 313 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 314 |
return StringUtils::SecondsToTimeString(GetRemainingTime(epgTag), format); |
| 315 |
} |
| 316 |
|
| 317 |
std::string CPVRGUITimesInfo::GetEpgEventFinishTime( |
| 318 |
const std::shared_ptr<const CPVREpgInfoTag>& epgTag, TIME_FORMAT format) const |
| 319 |
{ |
| 320 |
CDateTime finish = CDateTime::GetCurrentDateTime(); |
| 321 |
finish += CDateTimeSpan(0, 0, 0, GetRemainingTime(epgTag)); |
| 322 |
return finish.GetAsLocalizedTime(format); |
| 323 |
} |
| 324 |
|
| 325 |
std::string CPVRGUITimesInfo::GetEpgEventSeekTime(int iSeekSize, TIME_FORMAT format) const |
| 326 |
{ |
| 327 |
return StringUtils::SecondsToTimeString(GetElapsedTime() + iSeekSize, format); |
| 328 |
} |
| 329 |
|
| 330 |
int CPVRGUITimesInfo::GetElapsedTime() const |
| 331 |
{ |
| 332 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 333 |
if (m_playingEpgTag || m_iTimeshiftStartTime) |
| 334 |
{ |
| 335 |
CDateTime current(m_iTimeshiftPlayTime); |
| 336 |
CDateTime start = m_playingEpgTag ? m_playingEpgTag->StartAsUTC() : CDateTime(m_iTimeshiftStartTime); |
| 337 |
CDateTimeSpan time = current > start ? current - start : CDateTimeSpan(0, 0, 0, 0); |
| 338 |
return time.GetSecondsTotal(); |
| 339 |
} |
| 340 |
else |
| 341 |
{ |
| 342 |
return 0; |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
int CPVRGUITimesInfo::GetRemainingTime(const std::shared_ptr<const CPVREpgInfoTag>& epgTag) const |
| 347 |
{ |
| 348 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 349 |
if (epgTag && m_playingEpgTag && *epgTag != *m_playingEpgTag) |
| 350 |
return epgTag->GetDuration() - epgTag->Progress(); |
| 351 |
else |
| 352 |
return m_iDuration - GetElapsedTime(); |
| 353 |
} |
| 354 |
|
| 355 |
int CPVRGUITimesInfo::GetTimeshiftProgress() const |
| 356 |
{ |
| 357 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 358 |
return std::lrintf(static_cast<float>(m_iTimeshiftPlayTime - m_iTimeshiftStartTime) / (m_iTimeshiftEndTime - m_iTimeshiftStartTime) * 100); |
| 359 |
} |
| 360 |
|
| 361 |
int CPVRGUITimesInfo::GetTimeshiftProgressDuration() const |
| 362 |
{ |
| 363 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 364 |
return m_iTimeshiftProgressDuration; |
| 365 |
} |
| 366 |
|
| 367 |
int CPVRGUITimesInfo::GetTimeshiftProgressPlayPosition() const |
| 368 |
{ |
| 369 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 370 |
return std::lrintf(static_cast<float>(m_iTimeshiftPlayTime - m_iTimeshiftProgressStartTime) / m_iTimeshiftProgressDuration * 100); |
| 371 |
} |
| 372 |
|
| 373 |
int CPVRGUITimesInfo::GetTimeshiftProgressEpgStart() const |
| 374 |
{ |
| 375 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 376 |
if (m_playingEpgTag) |
| 377 |
{ |
| 378 |
time_t epgStart = 0; |
| 379 |
m_playingEpgTag->StartAsUTC().GetAsTime(epgStart); |
| 380 |
return std::lrintf(static_cast<float>(epgStart - m_iTimeshiftProgressStartTime) / m_iTimeshiftProgressDuration * 100); |
| 381 |
} |
| 382 |
return 0; |
| 383 |
} |
| 384 |
|
| 385 |
int CPVRGUITimesInfo::GetTimeshiftProgressEpgEnd() const |
| 386 |
{ |
| 387 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 388 |
if (m_playingEpgTag) |
| 389 |
{ |
| 390 |
time_t epgEnd = 0; |
| 391 |
m_playingEpgTag->EndAsUTC().GetAsTime(epgEnd); |
| 392 |
return std::lrintf(static_cast<float>(epgEnd - m_iTimeshiftProgressStartTime) / m_iTimeshiftProgressDuration * 100); |
| 393 |
} |
| 394 |
return 0; |
| 395 |
} |
| 396 |
|
| 397 |
int CPVRGUITimesInfo::GetTimeshiftProgressBufferStart() const |
| 398 |
{ |
| 399 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 400 |
return std::lrintf(static_cast<float>(m_iTimeshiftStartTime - m_iTimeshiftProgressStartTime) / m_iTimeshiftProgressDuration * 100); |
| 401 |
} |
| 402 |
|
| 403 |
int CPVRGUITimesInfo::GetTimeshiftProgressBufferEnd() const |
| 404 |
{ |
| 405 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 406 |
return std::lrintf(static_cast<float>(m_iTimeshiftEndTime - m_iTimeshiftProgressStartTime) / m_iTimeshiftProgressDuration * 100); |
| 407 |
} |
| 408 |
|
| 409 |
int CPVRGUITimesInfo::GetEpgEventDuration(const std::shared_ptr<const CPVREpgInfoTag>& epgTag) const |
| 410 |
{ |
| 411 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 412 |
if (epgTag && m_playingEpgTag && *epgTag != *m_playingEpgTag) |
| 413 |
return epgTag->GetDuration(); |
| 414 |
else |
| 415 |
return m_iDuration; |
| 416 |
} |
| 417 |
|
| 418 |
int CPVRGUITimesInfo::GetEpgEventProgress(const std::shared_ptr<const CPVREpgInfoTag>& epgTag) const |
| 419 |
{ |
| 420 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 421 |
if (epgTag && m_playingEpgTag && *epgTag != *m_playingEpgTag) |
| 422 |
return std::lrintf(epgTag->ProgressPercentage()); |
| 423 |
else |
| 424 |
return std::lrintf(static_cast<float>(GetElapsedTime()) / m_iDuration * 100); |
| 425 |
} |
| 426 |
|
| 427 |
bool CPVRGUITimesInfo::IsTimeshifting() const |
| 428 |
{ |
| 429 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 430 |
return (m_iTimeshiftOffset > static_cast<unsigned int>(CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_iPVRTimeshiftThreshold)); |
| 431 |
} |