AVFormatWriter: Fix bad error checking

* Review by Markus Overhagen, Thanks!
* Renamed fHeaderWritten variable to fCodecOpened too
This commit is contained in:
Dario Casalinuovo 2016-04-07 19:35:25 +02:00
parent f441a1e2ce
commit 6266cf35de
2 changed files with 15 additions and 15 deletions

View File

@ -380,7 +380,7 @@ AVFormatWriter::StreamCookie::AddTrackInfo(uint32 code,
AVFormatWriter::AVFormatWriter() AVFormatWriter::AVFormatWriter()
: :
fContext(avformat_alloc_context()), fContext(avformat_alloc_context()),
fHeaderWritten(false), fCodecOpened(false),
fHeaderError(-1), fHeaderError(-1),
fIOContext(NULL), fIOContext(NULL),
fStreamLock("stream lock") fStreamLock("stream lock")
@ -398,7 +398,7 @@ AVFormatWriter::~AVFormatWriter()
#if OPEN_CODEC_CONTEXT #if OPEN_CODEC_CONTEXT
// We only need to close the AVCodecContext when we opened it. // We only need to close the AVCodecContext when we opened it.
// This is experimental, see CommitHeader(). // This is experimental, see CommitHeader().
if (fHeaderWritten) if (fCodecOpened)
avcodec_close(fContext->streams[i]->codec); avcodec_close(fContext->streams[i]->codec);
#endif #endif
av_freep(&fContext->streams[i]->codec); av_freep(&fContext->streams[i]->codec);
@ -468,7 +468,7 @@ AVFormatWriter::CommitHeader()
if (fContext == NULL) if (fContext == NULL)
return B_NO_INIT; return B_NO_INIT;
if (fHeaderWritten) if (fCodecOpened)
return B_NOT_ALLOWED; return B_NOT_ALLOWED;
#if OPEN_CODEC_CONTEXT #if OPEN_CODEC_CONTEXT
@ -489,13 +489,13 @@ AVFormatWriter::CommitHeader()
} }
#endif #endif
// We need to close the codecs we opened, even in case of failure.
fCodecOpened = true;
fHeaderError = avformat_write_header(fContext, NULL); fHeaderError = avformat_write_header(fContext, NULL);
if (fHeaderError < 0) if (fHeaderError < 0)
TRACE(" avformat_write_header(): %d\n", fHeaderError); 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 #ifdef TRACE_AVFORMAT_WRITER
TRACE(" wrote header\n"); TRACE(" wrote header\n");
for (unsigned i = 0; i < fContext->nb_streams; i++) { for (unsigned i = 0; i < fContext->nb_streams; i++) {
@ -527,17 +527,17 @@ AVFormatWriter::Close()
if (fContext == NULL) if (fContext == NULL)
return B_NO_INIT; return B_NO_INIT;
if (!fHeaderWritten) if (!fCodecOpened)
return B_NOT_ALLOWED; return B_NOT_ALLOWED;
int result = -1;
// From ffmpeg documentation: [av_write_trailer] may only be called // From ffmpeg documentation: [av_write_trailer] may only be called
// after a successful call to avformat_write_header. // after a successful call to avformat_write_header.
if (fHeaderError > 0) { if (fHeaderError != 0)
result = av_write_trailer(fContext); return B_ERROR;
if (result < 0)
TRACE(" av_write_trailer(): %d\n", result); int result = av_write_trailer(fContext);
} if (result < 0)
TRACE(" av_write_trailer(): %d\n", result);
return result == 0 ? B_OK : B_ERROR; return result == 0 ? B_OK : B_ERROR;
} }
@ -548,7 +548,7 @@ AVFormatWriter::AllocateCookie(void** _cookie, media_format* format,
{ {
TRACE("AVFormatWriter::AllocateCookie()\n"); TRACE("AVFormatWriter::AllocateCookie()\n");
if (fHeaderWritten) if (fCodecOpened)
return B_NOT_ALLOWED; return B_NOT_ALLOWED;
BAutolock _(fStreamLock); BAutolock _(fStreamLock);

View File

@ -52,7 +52,7 @@ private:
class StreamCookie; class StreamCookie;
AVFormatContext* fContext; AVFormatContext* fContext;
bool fHeaderWritten; bool fCodecOpened;
int fHeaderError; int fHeaderError;
AVIOContext* fIOContext; AVIOContext* fIOContext;