1 /*
2  *  Copyright (C) 2005-2021 Team Kodi (https://kodi.tv)
3  *
4  *  SPDX-License-Identifier: GPL-2.0-or-later
5  *  See LICENSE.md for more information.
6  */
7 
8 #pragma once
9 
10 #include <string>
11 
12 #include "data/Channel.h"
13 #include "data/EpgEntry.h"
14 #include "utilities/StreamUtils.h"
15 #include "StreamManager.h"
16 
17 #include <mutex>
18 
19 #include <kodi/addon-instance/pvr/EPG.h>
20 
21 namespace iptvsimple
22 {
23   class Epg;
24 
25   class CatchupController
26   {
27   public:
28     CatchupController(iptvsimple::Epg& epg, std::mutex* mutex);
29 
30     void ProcessChannelForPlayback(const data::Channel& channel, std::map<std::string, std::string>& catchupProperties);
31     void ProcessEPGTagForTimeshiftedPlayback(const kodi::addon::PVREPGTag& epgTag, const data::Channel& channel, std::map<std::string, std::string>& catchupProperties);
32     void ProcessEPGTagForVideoPlayback(const kodi::addon::PVREPGTag& epgTag, const data::Channel& channel, std::map<std::string, std::string>& catchupProperties);
33 
34     std::string GetCatchupUrlFormatString(const data::Channel& channel) const;
35     std::string GetCatchupUrl(const data::Channel& channel) const;
36     std::string ProcessStreamUrl(const data::Channel& channel) const;
37 
ControlsLiveStream()38     bool ControlsLiveStream() const { return m_controlsLiveStream; }
ResetCatchupState()39     void ResetCatchupState() { m_resetCatchupState = true; }
40     data::EpgEntry* GetEPGEntry(const iptvsimple::data::Channel& myChannel, time_t lookupTime);
41 
42   private:
43     data::EpgEntry* GetLiveEPGEntry(const iptvsimple::data::Channel& myChannel);
44     void SetCatchupInputStreamProperties(bool playbackAsLive, const iptvsimple::data::Channel& channel, std::map<std::string, std::string>& catchupProperties, const StreamType& streamType);
45     StreamType StreamTypeLookup(const data::Channel& channel, bool fromEpg = false);
46     std::string GetStreamTestUrl(const data::Channel& channel, bool fromEpg) const;
47     std::string GetStreamKey(const data::Channel& channel, bool fromEpg) const;
48 
49     // Programme helpers
50     void UpdateProgrammeFrom(const kodi::addon::PVREPGTag& epgTag, int tvgShift);
51     void UpdateProgrammeFrom(const data::EpgEntry& epgEntry, int tvgShift);
52     void ClearProgramme();
53 
54     // State of current stream
55     time_t m_catchupStartTime = 0;
56     time_t m_catchupEndTime = 0;
57     time_t m_timeshiftBufferStartTime = 0;
58     long long m_timeshiftBufferOffset = 0;
59     bool m_resetCatchupState = false;
60     bool m_playbackIsVideo = false;
61     bool m_fromEpgTag = false;
62 
63     // Current programme details
64     time_t m_programmeStartTime = 0;
65     time_t m_programmeEndTime = 0;
66     std::string m_programmeTitle;
67     unsigned int m_programmeUniqueChannelId = 0;
68     int m_programmeChannelTvgShift = 0;
69     std::string m_programmeCatchupId;
70 
71     bool m_controlsLiveStream = false;
72     iptvsimple::Epg& m_epg;
73     std::mutex* m_mutex = nullptr;
74 
75     StreamManager m_streamManager;
76   };
77 } //namespace iptvsimple
78