Debian Codegraph @

src/WaipuData.h

raw
1 #pragma once
2 /*
3 * Copyright (C) 2019 - 2021 flubshi
4 * https://github.com/flubshi
5 *
6 * This Program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * This Program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with XBMC; see the file COPYING. If not, write to
18 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 */
22
23 #include "Curl.h"
24 #include "HLSAllowlist.h"
25 #include "JWT.h"
26 #include "SynchronizedQueue.h"
27 #include "categories.h"
28 #include "kodi/Network.h"
29 #include "kodi/addon-instance/PVR.h"
30
31 #include <atomic>
32 #include <map>
33 #include <mutex>
34 #include <thread>
35 #include <vector>
36
37 // User Agent for HTTP Requests
38 static std::string WAIPU_USER_AGENT = "Kodi/pvr.waipu - UA will be initialized on start";
39 static const int WAIPU_LOGIN_FAILED_LOCK_LIMIT = 3;
40
41 enum WAIPU_PROVIDER
42 {
43 WAIPU_PROVIDER_WAIPU = 0,
44 WAIPU_PROVIDER_O2 = 1,
45 WAIPU_PROVIDER_WAIPU_DEVICE = 2
46 };
47
48 enum WAIPU_CHANNEL_IMPORT_FILTER
49 {
50 CHANNEL_FILTER_ALL_VISIBLE = 0,
51 CHANNEL_FILTER_ALL = 1,
52 CHANNEL_FILTER_FAVOURITES = 2,
53 CHANNEL_FILTER_LIVE = 3,
54 CHANNEL_FILTER_NONE = 4,
55 };
56
57 enum class WAIPU_LOGIN_STATUS
58 {
59 OK,
60 INVALID_CREDENTIALS,
61 NO_NETWORK,
62 UNKNOWN
63 };
64
65 static const unsigned int EPG_TAG_FLAG_IS_RECORDABLE_WAIPU = (1 << 28);
66 static const unsigned int EPG_TAG_FLAG_INSTANT_RESTART_ALLOWED_WAIPU = (1 << 29);
67
68 class ATTR_DLL_LOCAL WaipuData : public kodi::addon::CAddonBase,
69 public kodi::addon::CInstancePVRClient
70 {
71 public:
72 WaipuData() = default;
73 WaipuData(const WaipuData&) = delete;
74 WaipuData(WaipuData&&) = delete;
75 ~WaipuData();
76
77 WaipuData& operator=(const WaipuData&) = delete;
78 WaipuData& operator=(WaipuData&&) = delete;
79
80 ADDON_STATUS Create() override;
81 ADDON_STATUS SetSetting(const std::string& settingName,
82 const kodi::addon::CSettingValue& settingValue) override;
83
84 PVR_ERROR GetCapabilities(kodi::addon::PVRCapabilities& capabilities) override;
85 PVR_ERROR GetBackendName(std::string& name) override;
86 PVR_ERROR GetBackendVersion(std::string& version) override;
87
88 PVR_ERROR GetChannelsAmount(int& amount) override;
89 PVR_ERROR GetChannels(bool radio, kodi::addon::PVRChannelsResultSet& results) override;
90
91 PVR_ERROR GetChannelGroupsAmount(int& amount) override;
92 PVR_ERROR GetChannelGroups(bool radio, kodi::addon::PVRChannelGroupsResultSet& results) override;
93 PVR_ERROR GetChannelGroupMembers(const kodi::addon::PVRChannelGroup& group,
94 kodi::addon::PVRChannelGroupMembersResultSet& results) override;
95 PVR_ERROR GetChannelStreamProperties(
96 const kodi::addon::PVRChannel& channel,
97 std::vector<kodi::addon::PVRStreamProperty>& properties) override;
98
99 PVR_ERROR GetEPGForChannel(int channelUid,
100 time_t start,
101 time_t end,
102 kodi::addon::PVREPGTagsResultSet& results) override;
103 kodi::addon::PVREPGTag ParseEPGTagEntry(const rapidjson::Value& tagEntry, const int kodiChanneliUniqueId, const std::string waipuChannelID, const bool reSchedule);
104 PVR_ERROR IsEPGTagRecordable(const kodi::addon::PVREPGTag& tag, bool& isRecordable) override;
105 PVR_ERROR IsEPGTagPlayable(const kodi::addon::PVREPGTag& tag, bool& isPlayable) override;
106 std::string GetEPGTagStreamURL(const kodi::addon::PVREPGTag& tag, const std::string& protocol);
107 PVR_ERROR GetEPGTagStreamProperties(
108 const kodi::addon::PVREPGTag& tag,
109 std::vector<kodi::addon::PVRStreamProperty>& properties) override;
110
111 PVR_ERROR GetRecordingsAmount(bool deleted, int& amount) override;
112 kodi::addon::PVRRecording ParseRecordingEntry(const rapidjson::Value& recordingEntry);
113 PVR_ERROR GetRecordings(bool deleted, kodi::addon::PVRRecordingsResultSet& results) override;
114 PVR_ERROR DeleteRecording(const kodi::addon::PVRRecording& recording) override;
115 PVR_ERROR GetRecordingStreamProperties(
116 const kodi::addon::PVRRecording& recording,
117 std::vector<kodi::addon::PVRStreamProperty>& properties) override;
118
119 PVR_ERROR GetTimerTypes(std::vector<kodi::addon::PVRTimerType>& types) override;
120 PVR_ERROR GetTimersAmount(int& amount) override;
121 PVR_ERROR GetTimers(kodi::addon::PVRTimersResultSet& results) override;
122 PVR_ERROR DeleteTimer(const kodi::addon::PVRTimer& timer, bool forceDelete) override;
123 PVR_ERROR AddTimer(const kodi::addon::PVRTimer& timer) override;
124 PVR_ERROR GetDriveSpace(uint64_t& total, uint64_t& used) override;
125 PVR_ERROR OnSystemWake() override;
126 PVR_ERROR GetRecordingLastPlayedPosition(const kodi::addon::PVRRecording& recording,
127 int& position) override;
128 PVR_ERROR SetRecordingLastPlayedPosition(const kodi::addon::PVRRecording& recording,
129 int lastplayedposition) override;
130
131 private:
132 bool m_isConnected = false;
133 bool m_epg_show_preview_images = false;
134 std::atomic<bool> m_loginThreadRunning = {false};
135 std::thread m_loginThread;
136 void LoginThread();
137 std::atomic<bool> m_EPGTaskThreadRunning = {false};
138 std::thread m_EPGTaskThread;
139 void EPGTaskThread();
140 time_t m_nextLoginAttempt = 0;
141 WAIPU_CHANNEL_IMPORT_FILTER m_channel_filter =
142 WAIPU_CHANNEL_IMPORT_FILTER::CHANNEL_FILTER_ALL_VISIBLE;
143
144 struct WaipuChannel
145 {
146 int iUniqueId;
147 std::string waipuID; // waipu[id]
148 int iChannelNumber; //position
149 std::string strChannelName; //waipu[displayName]
150 std::string strIconPath; // waipu[links][rel=iconlargehd]
151 bool tvfuse; // tvfuse is on demand channel
152 bool isFavorite; // channel is marked as favorite
153 };
154
155 struct EPGQueueTask
156 {
157 std::string epgid;
158 std::string waipuChannelID;
159 int kodiChannelID;
160 };
161
162 struct WaipuChannelGroup
163 {
164 std::string name;
165 std::vector<WaipuChannel> channels;
166 };
167
168 static std::mutex mutex;
169 std::string m_username;
170 std::string m_password;
171 std::string m_userhandle = "";
172 std::string m_protocol;
173 std::string m_device_id;
174 int m_first_channel_number = 1;
175 SynchronizedQueue<EPGQueueTask> m_queue_epgtag_tasks;
176 WAIPU_PROVIDER m_provider = WAIPU_PROVIDER_WAIPU;
177
178 std::vector<WaipuChannel> m_channels;
179 std::vector<WaipuChannelGroup> m_channelGroups;
180
181 JWT m_accessToken;
182 JWT m_refreshToken;
183 JWT m_deviceCapabilitiesToken;
184
185 std::string m_license;
186 int m_recordings_count = 0;
187 bool m_recordings_backend_handle_position = false;
188 int m_timers_count = 0;
189 int m_login_failed_counter = 0;
190 time_t m_login_failed_locktime = 0;
191 bool m_active_recordings_update = false;
192 bool m_account_replay_allowed = false;
193 int m_account_hours_recording = 0;
194 uint64_t m_finishedRecordingsSeconds = 0;
195 WAIPU_LOGIN_STATUS m_login_status = WAIPU_LOGIN_STATUS::UNKNOWN;
196 HLSAllowlist m_hls_allowlist;
197 Categories m_categories;
198 time_t m_lastUpdate = 0;
199
200 void ReadSettings();
201 bool ParseAccessToken();
202
203 void AddTimerType(std::vector<kodi::addon::PVRTimerType>& types, int id, int attributes);
204
205 std::string GetChannelStreamURL(int uniqueId,
206 const std::string& protocol,
207 const std::string& startTime);
208 std::string GetRecordingURL(const kodi::addon::PVRRecording& recording,
209 const std::string& protocol);
210 const std::string& GetLicense();
211 const std::map<std::string, std::string> GetOAuthDeviceCode(const std::string& tenant);
212 const std::map<std::string, std::string> CheckOAuthState(const std::string& device_code);
213 void SetStreamProperties(std::vector<kodi::addon::PVRStreamProperty>& properties,
214 const std::string& url,
215 bool realtime,
216 bool playTimeshiftBuffer,
217 const std::string& protocol);
218
219 std::string HttpGet(const std::string& url,
220 const std::map<std::string,
221 std::string>& headers = {},
222 bool ignoreStatusCode = false);
223 std::string HttpDelete(const std::string& url,
224 const std::string& postData,
225 const std::map<std::string,
226 std::string>& headers = {},
227 bool ignoreStatusCode = false);
228 std::string HttpPost(const std::string& url,
229 const std::string& postData,
230 const std::map<std::string,
231 std::string>& headers = {},
232 bool ignoreStatusCode = false);
233 std::string HttpRequest(const std::string& action,
234 const std::string& url,
235 const std::string& postData,
236 const std::map<std::string,
237 std::string>& headers = {},
238 bool ignoreStatusCode = false);
239 std::string HttpRequestToCurl(Curl& curl,
240 const std::string& action,
241 const std::string& url,
242 const std::string& postData,
243 int& statusCode,
244 bool ignoreStatusCode = false);
245 bool IsConnected();
246 WAIPU_LOGIN_STATUS Login();
247 WAIPU_LOGIN_STATUS DeviceLogin(const std::string& tenant);
248 WAIPU_LOGIN_STATUS OAuthRequest(const std::string& postData);
249 bool LoadChannelData();
250 bool RefreshDeviceCapabiltiesToken();
251 };