Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements for InputStreamAdaptive #882

Merged
merged 5 commits into from
Aug 24, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 42 additions & 13 deletions src/iptvsimple/utilities/StreamUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,36 @@ using namespace iptvsimple;
using namespace iptvsimple::data;
using namespace iptvsimple::utilities;

namespace
{
bool SplitUrlProtocolOpts(const std::string& streamURL,
std::string& url,
std::string& encodedProtocolOptions)
{
size_t found = streamURL.find_first_of('|');
if (found != std::string::npos)
{
// Headers found, split and url-encode them
url = streamURL.substr(0, found);
const std::string& protocolOptions = streamURL.substr(found + 1, streamURL.length());
encodedProtocolOptions = StreamUtils::GetUrlEncodedProtocolOptions(protocolOptions);
return true;
}
return false;
}
} // unnamed namespace

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, std::shared_ptr<InstanceSettings>& settings)
{
if (ChannelSpecifiesInputstream(channel))
// Check if the channel has explicitly set up the use of inputstream.adaptive,
// if so, the best behaviour for media services is:
// - Always add mimetype to prevent kodi core to make an HTTP HEADER requests
// this because in some cases services refuse this request and can also deny downloads
// - If requested by settings, always add the "user-agent" header to ISA properties
const bool isISAdaptiveSet =
channel.GetProperty(PVR_STREAM_PROPERTY_INPUTSTREAM) == INPUTSTREAM_ADAPTIVE;

if (!isISAdaptiveSet && ChannelSpecifiesInputstream(channel))
{
// Channel has an inputstream class set so we only set the stream URL
properties.emplace_back(PVR_STREAM_PROPERTY_STREAMURL, streamURL);
Expand All @@ -40,7 +67,7 @@ void StreamUtils::SetAllStreamProperties(std::vector<kodi::addon::PVRStreamPrope
streamType = StreamUtils::InspectStreamType(streamURL, channel);

// Using kodi's built in inputstreams
if (StreamUtils::UseKodiInputstreams(streamType, settings))
if (!isISAdaptiveSet && StreamUtils::UseKodiInputstreams(streamType, settings))
{
std::string ffmpegStreamURL = StreamUtils::GetURLWithFFmpegReconnectOptions(streamURL, streamType, channel, settings);

Expand Down Expand Up @@ -83,19 +110,17 @@ void StreamUtils::SetAllStreamProperties(std::vector<kodi::addon::PVRStreamPrope
// 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())
if (channel.GetProperty("inputstream.adaptive.manifest_headers").empty() &&
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)
std::string url;
std::string encodedProtocolOptions;
if (SplitUrlProtocolOpts(streamURL, url, encodedProtocolOptions))
{
// 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.manifest_headers", encodedProtocolOptions);
properties.emplace_back("inputstream.adaptive.stream_headers", encodedProtocolOptions);
streamUrlSet = true;
}
Expand All @@ -105,9 +130,11 @@ void StreamUtils::SetAllStreamProperties(std::vector<kodi::addon::PVRStreamPrope
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)
if (!isISAdaptiveSet)
properties.emplace_back(PVR_STREAM_PROPERTY_INPUTSTREAM, INPUTSTREAM_ADAPTIVE);

if (streamType == StreamType::HLS || streamType == StreamType::DASH ||
streamType == StreamType::SMOOTH_STREAMING)
properties.emplace_back(PVR_STREAM_PROPERTY_MIMETYPE, StreamUtils::GetMimeType(streamType));
}
}
Expand Down Expand Up @@ -287,6 +314,8 @@ const std::string StreamUtils::GetMimeType(const StreamType& streamType)
return "application/x-mpegURL";
case StreamType::DASH:
return "application/xml+dash";
case StreamType::SMOOTH_STREAMING:
return "application/vnd.ms-sstr+xml";
case StreamType::TS:
return "video/mp2t";
default:
Expand Down