Skip to content

Commit

Permalink
Skip a few broken packets. Disable stream processing once threshold h…
Browse files Browse the repository at this point in the history
…as been exceeded.

This fixes microsoft#175
  • Loading branch information
lukasf committed Oct 10, 2017
1 parent 962cfb6 commit 83bb3cc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
45 changes: 36 additions & 9 deletions FFmpegInterop/Source/MediaSampleProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ MediaSampleProvider::MediaSampleProvider(
, m_streamIndex(AVERROR_STREAM_NOT_FOUND)
, m_startOffset(AV_NOPTS_VALUE)
, m_nextFramePts(0)
, m_isEnabled(true)
{
DebugMessage(L"MediaSampleProvider\n");
}
Expand Down Expand Up @@ -70,17 +71,20 @@ MediaStreamSample^ MediaSampleProvider::GetNextSample()
HRESULT hr = S_OK;

MediaStreamSample^ sample;
DataWriter^ dataWriter = ref new DataWriter();
if (m_isEnabled)
{
DataWriter^ dataWriter = ref new DataWriter();

LONGLONG pts = 0;
LONGLONG dur = 0;
LONGLONG pts = 0;
LONGLONG dur = 0;

hr = GetNextPacket(dataWriter, pts, dur);
hr = GetNextPacket(dataWriter, pts, dur);

if (hr == S_OK)
{
sample = MediaStreamSample::CreateFromBuffer(dataWriter->DetachBuffer(), { pts });
sample->Duration = { dur };
if (hr == S_OK)
{
sample = MediaStreamSample::CreateFromBuffer(dataWriter->DetachBuffer(), { pts });
sample->Duration = { dur };
}
}

return sample;
Expand Down Expand Up @@ -121,7 +125,14 @@ void MediaSampleProvider::QueuePacket(AVPacket packet)
{
DebugMessage(L" - QueuePacket\n");

m_packetQueue.push_back(packet);
if (m_isEnabled)
{
m_packetQueue.push_back(packet);
}
else
{
av_packet_unref(&packet);
}
}

AVPacket MediaSampleProvider::PopPacket()
Expand Down Expand Up @@ -154,6 +165,7 @@ HRESULT FFmpegInterop::MediaSampleProvider::GetNextPacket(DataWriter ^ writer, L
bool frameComplete = false;
bool decodeSuccess = true;
int64_t framePts = 0, frameDuration = 0;
int errorCount = 0;

while (SUCCEEDED(hr) && !frameComplete)
{
Expand All @@ -178,6 +190,13 @@ HRESULT FFmpegInterop::MediaSampleProvider::GetNextPacket(DataWriter ^ writer, L
// Decode the packet if necessary, it will update the presentation time if necessary
hr = DecodeAVPacket(writer, &avPacket, framePts, frameDuration);
frameComplete = (hr == S_OK);

if (!frameComplete && errorCount++ < 10)
{
// skip a few broken packets (maybe make this configurable later)
DebugMessage(L"Skipping broken packet\n");
hr = S_OK;
}
}
}

Expand All @@ -202,6 +221,14 @@ HRESULT FFmpegInterop::MediaSampleProvider::GetNextPacket(DataWriter ^ writer, L

av_packet_unref(&avPacket);

if (FAILED(hr))
{
// flush stream and disable any further processing
DebugMessage(L"Too many broken packets - disable stream\n");
m_isEnabled = false;
Flush();
}

return hr;
}

Expand Down
1 change: 1 addition & 0 deletions FFmpegInterop/Source/MediaSampleProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ namespace FFmpegInterop
int m_streamIndex;
int64 m_startOffset;
int64 m_nextFramePts;
bool m_isEnabled;

internal:
// The FFmpeg context. Because they are complex types
Expand Down

0 comments on commit 83bb3cc

Please sign in to comment.