Skip to content

Commit

Permalink
AVFormatWriter: Fix bad error checking
Browse files Browse the repository at this point in the history
* Review by Markus Overhagen, Thanks!
* Renamed fHeaderWritten variable to fCodecOpened too
  • Loading branch information
Numerio committed Apr 7, 2016
1 parent f441a1e commit 6266cf3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
28 changes: 14 additions & 14 deletions src/add-ons/media/plugins/ffmpeg/AVFormatWriter.cpp
Expand Up @@ -380,7 +380,7 @@ AVFormatWriter::StreamCookie::AddTrackInfo(uint32 code,
AVFormatWriter::AVFormatWriter()
:
fContext(avformat_alloc_context()),
fHeaderWritten(false),
fCodecOpened(false),
fHeaderError(-1),
fIOContext(NULL),
fStreamLock("stream lock")
Expand All @@ -398,7 +398,7 @@ AVFormatWriter::~AVFormatWriter()
#if OPEN_CODEC_CONTEXT
// We only need to close the AVCodecContext when we opened it.
// This is experimental, see CommitHeader().
if (fHeaderWritten)
if (fCodecOpened)
avcodec_close(fContext->streams[i]->codec);
#endif
av_freep(&fContext->streams[i]->codec);
Expand Down Expand Up @@ -468,7 +468,7 @@ AVFormatWriter::CommitHeader()
if (fContext == NULL)
return B_NO_INIT;

if (fHeaderWritten)
if (fCodecOpened)
return B_NOT_ALLOWED;

#if OPEN_CODEC_CONTEXT
Expand All @@ -489,13 +489,13 @@ AVFormatWriter::CommitHeader()
}
#endif

// We need to close the codecs we opened, even in case of failure.
fCodecOpened = true;

fHeaderError = avformat_write_header(fContext, NULL);
if (fHeaderError < 0)
TRACE(" avformat_write_header(): %d\n", fHeaderError);

// We need to close the codecs we opened, even in case of failure.
fHeaderWritten = true;

#ifdef TRACE_AVFORMAT_WRITER
TRACE(" wrote header\n");
for (unsigned i = 0; i < fContext->nb_streams; i++) {
Expand Down Expand Up @@ -527,17 +527,17 @@ AVFormatWriter::Close()
if (fContext == NULL)
return B_NO_INIT;

if (!fHeaderWritten)
if (!fCodecOpened)
return B_NOT_ALLOWED;

int result = -1;
// From ffmpeg documentation: [av_write_trailer] may only be called
// after a successful call to avformat_write_header.
if (fHeaderError > 0) {
result = av_write_trailer(fContext);
if (result < 0)
TRACE(" av_write_trailer(): %d\n", result);
}
if (fHeaderError != 0)
return B_ERROR;

int result = av_write_trailer(fContext);
if (result < 0)
TRACE(" av_write_trailer(): %d\n", result);
return result == 0 ? B_OK : B_ERROR;
}

Expand All @@ -548,7 +548,7 @@ AVFormatWriter::AllocateCookie(void** _cookie, media_format* format,
{
TRACE("AVFormatWriter::AllocateCookie()\n");

if (fHeaderWritten)
if (fCodecOpened)
return B_NOT_ALLOWED;

BAutolock _(fStreamLock);
Expand Down
2 changes: 1 addition & 1 deletion src/add-ons/media/plugins/ffmpeg/AVFormatWriter.h
Expand Up @@ -52,7 +52,7 @@ class AVFormatWriter : public Writer {
class StreamCookie;

AVFormatContext* fContext;
bool fHeaderWritten;
bool fCodecOpened;
int fHeaderError;

AVIOContext* fIOContext;
Expand Down

0 comments on commit 6266cf3

Please sign in to comment.