/
StreamUtils.cpp
391 lines (326 loc) · 14.7 KB
/
StreamUtils.cpp
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
* Copyright (C) 2005-2021 Team Kodi (https://kodi.tv)
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSE.md for more information.
*/
#include "StreamUtils.h"
#include "../Settings.h"
#include "FileUtils.h"
#include "Logger.h"
#include "WebUtils.h"
#include <kodi/General.h>
#include <kodi/tools/StringUtils.h>
using namespace kodi::tools;
using namespace iptvsimple;
using namespace iptvsimple::data;
using namespace iptvsimple::utilities;
void StreamUtils::SetAllStreamProperties(std::vector<kodi::addon::PVRStreamProperty>& properties, const iptvsimple::data::Channel& channel, const std::string& streamURL, bool isChannelURL, std::map<std::string, std::string>& catchupProperties)
{
if (ChannelSpecifiesInputstream(channel))
{
// Channel has an inputstream class set so we only set the stream URL
properties.emplace_back(PVR_STREAM_PROPERTY_STREAMURL, streamURL);
if (channel.GetInputStreamName() != PVR_STREAM_PROPERTY_VALUE_INPUTSTREAMFFMPEG)
CheckInputstreamInstalledAndEnabled(channel.GetInputStreamName());
if (channel.GetInputStreamName() == INPUTSTREAM_FFMPEGDIRECT)
InspectAndSetFFmpegDirectStreamProperties(properties, channel, streamURL, isChannelURL);
}
else
{
StreamType streamType = StreamUtils::GetStreamType(streamURL, channel);
if (streamType == StreamType::OTHER_TYPE)
streamType = StreamUtils::InspectStreamType(streamURL, channel);
// Using kodi's built in inputstreams
if (StreamUtils::UseKodiInputstreams(streamType))
{
std::string ffmpegStreamURL = StreamUtils::GetURLWithFFmpegReconnectOptions(streamURL, streamType, channel);
properties.emplace_back(PVR_STREAM_PROPERTY_STREAMURL, streamURL);
if (!channel.HasMimeType() && StreamUtils::HasMimeType(streamType))
properties.emplace_back(PVR_STREAM_PROPERTY_MIMETYPE, StreamUtils::GetMimeType(streamType));
if (streamType == StreamType::HLS || streamType == StreamType::TS || streamType == StreamType::OTHER_TYPE)
{
if (channel.IsCatchupSupported() && channel.CatchupSupportsTimeshifting() &&
CheckInputstreamInstalledAndEnabled(CATCHUP_INPUTSTREAM_NAME))
{
properties.emplace_back(PVR_STREAM_PROPERTY_INPUTSTREAM, CATCHUP_INPUTSTREAM_NAME);
// this property is required to force VideoPlayer for Radio channels
properties.emplace_back("inputstream-player", "videodefaultplayer");
SetFFmpegDirectManifestTypeStreamProperty(properties, channel, streamURL, streamType);
}
else if (channel.SupportsLiveStreamTimeshifting() && isChannelURL &&
CheckInputstreamInstalledAndEnabled(INPUTSTREAM_FFMPEGDIRECT))
{
properties.emplace_back(PVR_STREAM_PROPERTY_INPUTSTREAM, INPUTSTREAM_FFMPEGDIRECT);
// this property is required to force VideoPlayer for Radio channels
properties.emplace_back("inputstream-player", "videodefaultplayer");
SetFFmpegDirectManifestTypeStreamProperty(properties, channel, streamURL, streamType);
properties.emplace_back("inputstream.ffmpegdirect.stream_mode", "timeshift");
properties.emplace_back("inputstream.ffmpegdirect.is_realtime_stream", "true");
}
else if (streamType == StreamType::HLS || streamType == StreamType::TS)
{
properties.emplace_back(PVR_STREAM_PROPERTY_INPUTSTREAM, PVR_STREAM_PROPERTY_VALUE_INPUTSTREAMFFMPEG);
}
}
}
else // inputstream.adaptive
{
CheckInputstreamInstalledAndEnabled(INPUTSTREAM_ADAPTIVE);
bool streamUrlSet = false;
// If no media headers are explicitly set for inputstream.adaptive,
// strip the headers from streamURL and put it to media headers property
if (channel.GetProperty("inputstream.adaptive.stream_headers").empty())
{
// No stream headers declared by property, check if stream URL has any
size_t found = streamURL.find_first_of('|');
if (found != std::string::npos)
{
// Headers found, split and url-encode them
const std::string& url = streamURL.substr(0, found);
const std::string& protocolOptions = streamURL.substr(found + 1, streamURL.length());
const std::string& encodedProtocolOptions = StreamUtils::GetUrlEncodedProtocolOptions(protocolOptions);
// Set stream URL without headers and encoded headers as property
properties.emplace_back(PVR_STREAM_PROPERTY_STREAMURL, url);
properties.emplace_back("inputstream.adaptive.stream_headers", encodedProtocolOptions);
streamUrlSet = true;
}
}
// Set intact stream URL if not previously set
if (!streamUrlSet)
properties.emplace_back(PVR_STREAM_PROPERTY_STREAMURL, streamURL);
properties.emplace_back(PVR_STREAM_PROPERTY_INPUTSTREAM, INPUTSTREAM_ADAPTIVE);
properties.emplace_back("inputstream.adaptive.manifest_type", StreamUtils::GetManifestType(streamType));
if (streamType == StreamType::HLS || streamType == StreamType::DASH)
properties.emplace_back(PVR_STREAM_PROPERTY_MIMETYPE, StreamUtils::GetMimeType(streamType));
if (streamType == StreamType::DASH)
properties.emplace_back("inputstream.adaptive.manifest_update_parameter", "full");
}
}
if (!channel.GetProperties().empty())
{
for (auto& prop : channel.GetProperties())
properties.emplace_back(prop.first, prop.second);
}
if (!catchupProperties.empty())
{
for (auto& prop : catchupProperties)
properties.emplace_back(prop.first, prop.second);
}
}
bool StreamUtils::CheckInputstreamInstalledAndEnabled(const std::string& inputstreamName)
{
std::string version;
bool enabled;
if (kodi::IsAddonAvailable(inputstreamName, version, enabled))
{
if (!enabled)
{
std::string message = StringUtils::Format(kodi::GetLocalizedString(30502).c_str(), inputstreamName.c_str());
kodi::QueueNotification(QueueMsg::QUEUE_ERROR, kodi::GetLocalizedString(30500), message);
}
}
else // Not installed
{
std::string message = StringUtils::Format(kodi::GetLocalizedString(30501).c_str(), inputstreamName.c_str());
kodi::QueueNotification(QueueMsg::QUEUE_ERROR, kodi::GetLocalizedString(30500), message);
}
return true;
}
void StreamUtils::InspectAndSetFFmpegDirectStreamProperties(std::vector<kodi::addon::PVRStreamProperty>& properties, const iptvsimple::data::Channel& channel, const std::string& streamURL, bool isChannelURL)
{
// If there is no MIME type and no manifest type (BOTH!) set then potentially inspect the stream and set them
if (!channel.HasMimeType() && !channel.GetProperty("inputstream.ffmpegdirect.manifest_type").empty())
{
StreamType streamType = StreamUtils::GetStreamType(streamURL, channel);
if (streamType == StreamType::OTHER_TYPE)
streamType = StreamUtils::InspectStreamType(streamURL, channel);
if (!channel.HasMimeType() && StreamUtils::HasMimeType(streamType))
properties.emplace_back(PVR_STREAM_PROPERTY_MIMETYPE, StreamUtils::GetMimeType(streamType));
SetFFmpegDirectManifestTypeStreamProperty(properties, channel, streamURL, streamType);
}
if (channel.SupportsLiveStreamTimeshifting() && isChannelURL &&
channel.GetProperty("inputstream.ffmpegdirect.stream_mode").empty() &&
Settings::GetInstance().AlwaysEnableTimeshiftModeIfMissing())
{
properties.emplace_back("inputstream.ffmpegdirect.stream_mode", "timeshift");
if (channel.GetProperty("inputstream.ffmpegdirect.is_realtime_stream").empty())
properties.emplace_back("inputstream.ffmpegdirect.is_realtime_stream", "true");
}
}
void StreamUtils::SetFFmpegDirectManifestTypeStreamProperty(std::vector<kodi::addon::PVRStreamProperty>& properties, const iptvsimple::data::Channel& channel, const std::string& streamURL, const StreamType& streamType)
{
std::string manifestType = channel.GetProperty("inputstream.ffmpegdirect.manifest_type");
if (manifestType.empty())
manifestType = StreamUtils::GetManifestType(streamType);
if (!manifestType.empty())
properties.emplace_back("inputstream.ffmpegdirect.manifest_type", manifestType);
}
std::string StreamUtils::GetEffectiveInputStreamName(const StreamType& streamType, const iptvsimple::data::Channel& channel)
{
std::string inputStreamName = channel.GetInputStreamName();
if (inputStreamName.empty())
{
if (StreamUtils::UseKodiInputstreams(streamType))
{
if (streamType == StreamType::HLS || streamType == StreamType::TS)
{
if (channel.IsCatchupSupported() && channel.CatchupSupportsTimeshifting())
inputStreamName = CATCHUP_INPUTSTREAM_NAME;
else
inputStreamName = PVR_STREAM_PROPERTY_VALUE_INPUTSTREAMFFMPEG;
}
}
else // inputstream.adpative
{
inputStreamName = "inputstream.adaptive";
}
}
return inputStreamName;
}
const StreamType StreamUtils::GetStreamType(const std::string& url, const Channel& channel)
{
if (StringUtils::StartsWith(url, "plugin://"))
return StreamType::PLUGIN;
std::string mimeType = channel.GetProperty(PVR_STREAM_PROPERTY_MIMETYPE);
if (url.find(".m3u8") != std::string::npos ||
mimeType == "application/x-mpegURL" ||
mimeType == "application/vnd.apple.mpegurl")
return StreamType::HLS;
if (url.find(".mpd") != std::string::npos || mimeType == "application/xml+dash")
return StreamType::DASH;
if (url.find(".ism") != std::string::npos &&
!(url.find(".ismv") != std::string::npos || url.find(".isma") != std::string::npos))
return StreamType::SMOOTH_STREAMING;
if (mimeType == "video/mp2t" || channel.IsCatchupTSStream())
return StreamType::TS;
// it has a MIME type but not one we recognise
if (!mimeType.empty())
return StreamType::MIME_TYPE_UNRECOGNISED;
return StreamType::OTHER_TYPE;
}
const StreamType StreamUtils::InspectStreamType(const std::string& url, const Channel& channel)
{
if (!FileUtils::FileExists(url))
return StreamType::OTHER_TYPE;
int httpCode = 0;
const std::string source = WebUtils::ReadFileContentsStartOnly(url, &httpCode);
if (httpCode == 200)
{
if (StringUtils::StartsWith(source, "#EXTM3U") && (source.find("#EXT-X-STREAM-INF") != std::string::npos || source.find("#EXT-X-VERSION") != std::string::npos))
return StreamType::HLS;
if (source.find("<MPD") != std::string::npos)
return StreamType::DASH;
if (source.find("<SmoothStreamingMedia") != std::string::npos)
return StreamType::SMOOTH_STREAMING;
}
// If we can't inspect the stream type the only option left for default, append or shift mode is TS
if (channel.GetCatchupMode() == CatchupMode::DEFAULT ||
channel.GetCatchupMode() == CatchupMode::APPEND ||
channel.GetCatchupMode() == CatchupMode::SHIFT ||
channel.GetCatchupMode() == CatchupMode::TIMESHIFT)
return StreamType::TS;
return StreamType::OTHER_TYPE;
}
const std::string StreamUtils::GetManifestType(const StreamType& streamType)
{
switch (streamType)
{
case StreamType::HLS:
return "hls";
case StreamType::DASH:
return "mpd";
case StreamType::SMOOTH_STREAMING:
return "ism";
default:
return "";
}
}
const std::string StreamUtils::GetMimeType(const StreamType& streamType)
{
switch (streamType)
{
case StreamType::HLS:
return "application/x-mpegURL";
case StreamType::DASH:
return "application/xml+dash";
case StreamType::TS:
return "video/mp2t";
default:
return "";
}
}
bool StreamUtils::HasMimeType(const StreamType& streamType)
{
return streamType != StreamType::OTHER_TYPE && streamType != StreamType::SMOOTH_STREAMING;
}
std::string StreamUtils::GetURLWithFFmpegReconnectOptions(const std::string& streamUrl, const StreamType& streamType, const iptvsimple::data::Channel& channel)
{
std::string newStreamUrl = streamUrl;
if (WebUtils::IsHttpUrl(streamUrl) && SupportsFFmpegReconnect(streamType, channel) &&
(channel.GetProperty("http-reconnect") == "true" || Settings::GetInstance().UseFFmpegReconnect()))
{
newStreamUrl = AddHeaderToStreamUrl(newStreamUrl, "reconnect", "1");
if (streamType != StreamType::HLS)
newStreamUrl = AddHeaderToStreamUrl(newStreamUrl, "reconnect_at_eof", "1");
newStreamUrl = AddHeaderToStreamUrl(newStreamUrl, "reconnect_streamed", "1");
newStreamUrl = AddHeaderToStreamUrl(newStreamUrl, "reconnect_delay_max", "4294");
Logger::Log(LogLevel::LEVEL_DEBUG, "%s - FFmpeg Reconnect Stream URL: %s", __FUNCTION__, WebUtils::RedactUrl(newStreamUrl).c_str());
}
return newStreamUrl;
}
std::string StreamUtils::AddHeaderToStreamUrl(const std::string& streamUrl, const std::string& headerName, const std::string& headerValue)
{
return StreamUtils::AddHeader(streamUrl, headerName, headerValue, false);
}
std::string StreamUtils::AddHeader(const std::string& headerTarget, const std::string& headerName, const std::string& headerValue, bool encodeHeaderValue)
{
std::string newHeaderTarget = headerTarget;
bool hasProtocolOptions = false;
bool addHeader = true;
size_t found = newHeaderTarget.find("|");
if (found != std::string::npos)
{
hasProtocolOptions = true;
addHeader = newHeaderTarget.find(headerName + "=", found + 1) == std::string::npos;
}
if (addHeader)
{
if (!hasProtocolOptions)
newHeaderTarget += "|";
else
newHeaderTarget += "&";
newHeaderTarget += headerName + "=" + (encodeHeaderValue ? WebUtils::UrlEncode(headerValue) : headerValue);
}
return newHeaderTarget;
}
bool StreamUtils::UseKodiInputstreams(const StreamType& streamType)
{
return streamType == StreamType::OTHER_TYPE || streamType == StreamType::TS || streamType == StreamType::PLUGIN ||
(streamType == StreamType::HLS && !Settings::GetInstance().UseInputstreamAdaptiveforHls());
}
bool StreamUtils::ChannelSpecifiesInputstream(const iptvsimple::data::Channel& channel)
{
return !channel.GetInputStreamName().empty();
}
bool StreamUtils::SupportsFFmpegReconnect(const StreamType& streamType, const iptvsimple::data::Channel& channel)
{
return streamType == StreamType::HLS ||
channel.GetProperty(PVR_STREAM_PROPERTY_INPUTSTREAM) == PVR_STREAM_PROPERTY_VALUE_INPUTSTREAMFFMPEG;
}
std::string StreamUtils::GetUrlEncodedProtocolOptions(const std::string& protocolOptions)
{
std::string encodedProtocolOptions = "";
std::vector<std::string> headers = StringUtils::Split(protocolOptions, "&");
for (std::string header : headers)
{
std::string::size_type pos(header.find('='));
if(pos == std::string::npos)
continue;
encodedProtocolOptions = StreamUtils::AddHeader(encodedProtocolOptions, header.substr(0, pos), header.substr(pos + 1), true);
}
// We'll return the protocol options without the leading '|'
if (!encodedProtocolOptions.empty() && encodedProtocolOptions[0] == '|')
encodedProtocolOptions.erase(0, 1);
return encodedProtocolOptions;
}