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

Fix FFmpegOptions parsing and enable local network access #281

Merged
merged 2 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 9 additions & 4 deletions Samples/MediaPlayerCPP/MainPage.xaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,16 @@ void MainPage::URIBoxKeyUp(Platform::Object^ sender, Windows::UI::Xaml::Input::K

task<void> MainPage::OpenUriStream(Platform::String^ uri)
{
// Set FFmpeg specific options. List of options can be found in https://www.ffmpeg.org/ffmpeg-protocols.html

// Set FFmpeg specific options:
// https://www.ffmpeg.org/ffmpeg-protocols.html
// https://www.ffmpeg.org/ffmpeg-formats.html
//
// If format cannot be detected, try to increase probesize, max_probe_packets and analyzeduration!

// Below are some sample options that you can set to configure RTSP streaming
// Config->FFmpegOptions->Insert("rtsp_flags", "prefer_tcp");
// Config->FFmpegOptions->Insert("stimeout", 100000);
//Config->FFmpegOptions->Insert("rtsp_flags", "prefer_tcp");
Config->FFmpegOptions->Insert("stimeout", 1000000);
Config->FFmpegOptions->Insert("timeout", 1000000);

// Instantiate FFmpegMediaSource using the URI
mediaPlayer->Source = nullptr;
Expand Down
1 change: 1 addition & 0 deletions Samples/MediaPlayerCPP/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@
<Capabilities>
<Capability Name="internetClient" />
<uap3:Capability Name="backgroundMediaPlayback" />
<Capability Name="privateNetworkClientServer"/>
</Capabilities>
</Package>
9 changes: 7 additions & 2 deletions Samples/MediaPlayerCS/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,16 @@ private async void URIBoxKeyUp(object sender, KeyRoutedEventArgs e)

try
{
// Set FFmpeg specific options. List of options can be found in https://www.ffmpeg.org/ffmpeg-protocols.html
// Set FFmpeg specific options:
// https://www.ffmpeg.org/ffmpeg-protocols.html
// https://www.ffmpeg.org/ffmpeg-formats.html

// If format cannot be detected, try to increase probesize, max_probe_packets and analyzeduration!

// Below are some sample options that you can set to configure RTSP streaming
// Config.FFmpegOptions.Add("rtsp_flags", "prefer_tcp");
// Config.FFmpegOptions.Add("stimeout", 100000);
Config.FFmpegOptions.Add("stimeout", 1000000);
Config.FFmpegOptions.Add("timeout", 1000000);

// Instantiate FFmpegMediaSource using the URI
mediaPlayer.Source = null;
Expand Down
4 changes: 3 additions & 1 deletion Samples/MediaPlayerCS/Package.appxmanifest
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" IgnorableNamespaces="uap mp">
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:uap3="http://schemas.microsoft.com/appx/manifest/uap/windows10/3" IgnorableNamespaces="uap mp uap3">
<Identity Name="5388ef25-5c75-440f-a383-14d03f338feb" Publisher="CN=FFmpegInterop" Version="1.0.2.0" />
<mp:PhoneIdentity PhoneProductId="5388ef25-5c75-440f-a383-14d03f338feb" PhonePublisherId="00000000-0000-0000-0000-000000000000" />
<Properties>
Expand All @@ -24,5 +24,7 @@
</Applications>
<Capabilities>
<Capability Name="internetClient" />
<Capability Name="privateNetworkClientServer"/>
<uap3:Capability Name="backgroundMediaPlayback"/>
</Capabilities>
</Package>
9 changes: 4 additions & 5 deletions Source/FFmpegMediaSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1659,13 +1659,12 @@ namespace winrt::FFmpegInteropX::implementation
while (options.HasCurrent())
{
auto key = StringUtils::PlatformStringToUtf8String(options.Current().Key());
auto stringValue = options.Current().Value().try_as<IStringable>();
if (stringValue)
hstring value = StringUtils::ToString(options.Current().Value());
if (!value.empty())
{
auto value = StringUtils::PlatformStringToUtf8String(stringValue.ToString());

// Add key and value pair entry
if (av_dict_set(&avDict, key.c_str(), value.c_str(), 0) < 0)
auto valCstr = StringUtils::PlatformStringToUtf8String(value);
if (av_dict_set(&avDict, key.c_str(), valCstr.c_str(), 0) < 0)
{
hr = E_INVALIDARG;
break;
Expand Down
20 changes: 17 additions & 3 deletions Source/MediaSampleProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,28 @@ MediaStreamSample MediaSampleProvider::GetNextSample()
HRESULT MediaSampleProvider::GetNextPacket(AVPacket** avPacket, LONGLONG& packetPts, LONGLONG& packetDuration)
{
HRESULT hr = S_OK;
unsigned int errorCount = 0;

// Continue reading until there is an appropriate packet in the stream
while (m_packetQueue.empty())
{
if (m_pReader->ReadPacket() < 0)
auto result = m_pReader->ReadPacket();
if (result < 0)
{
DebugMessage(L"GetNextPacket reaching EOF\n");
break;
if (result == AVERROR_EOF || (m_pAvFormatCtx->pb && m_pAvFormatCtx->pb->eof_reached))
{
DebugMessage(L"GetNextPacket reaching EOF\n");
break;
}
else if (errorCount++ >= m_config.SkipErrors())
{
DebugMessage(L"Aborting after to too many read errors.\n");
break;
}
else
{
DebugMessage(L"Read error.\n");
}
}
}

Expand Down
29 changes: 29 additions & 0 deletions Source/StringUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,34 @@ namespace FFmpegInteropX
return std::string("");
}

inline static winrt::hstring ToString(winrt::Windows::Foundation::IInspectable inspectable)
{
auto valHstring = inspectable.try_as<winrt::hstring>();
if (valHstring.has_value())
{
return valHstring.value();
}
auto valI32 = inspectable.try_as<INT32>();
if (valI32.has_value())
{
return winrt::to_hstring(valI32.value());
}
auto valI64 = inspectable.try_as<INT64>();
if (valI64.has_value())
{
return winrt::to_hstring(valI64.value());
}
auto valf = inspectable.try_as<FLOAT>();
if (valf.has_value())
{
return winrt::to_hstring(valf.value());
}
auto vald = inspectable.try_as<DOUBLE>();
if (vald.has_value())
{
return winrt::to_hstring(vald.value());
}
return L"";
}
};
}
2 changes: 2 additions & 0 deletions Source/UncompressedSampleProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ HRESULT UncompressedSampleProvider::CreateNextSampleBuffer(IBuffer* pBuffer, int

if (!SUCCEEDED(hr) && errorCount++ < m_config.SkipErrors())
{
DebugMessage(L"Decode error.\n");

// unref any buffers in old frame
av_frame_unref(avFrame);

Expand Down