From 020fba32dc0e07d855d644bd575d88365b7e2deb Mon Sep 17 00:00:00 2001
From: "acolwell@chromium.org"
 <acolwell@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>
Date: Wed, 29 Jun 2011 16:37:46 +0000
Subject: Adding ChunkDemuxer implementation.

BUG=86536
TEST=ChunkDemuxerTest.*

Review URL: http://codereview.chromium.org/7203002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@90966 0039d316-1c4b-4281-b951-d872f2087c98
---
 media/ffmpeg/ffmpeg_common.cc | 24 ++++++++++++++++++++++++
 media/ffmpeg/ffmpeg_common.h  |  4 ++++
 2 files changed, 28 insertions(+)

(limited to 'media/ffmpeg')

diff --git a/media/ffmpeg/ffmpeg_common.cc b/media/ffmpeg/ffmpeg_common.cc
index 7af9635..c86acb5 100644
--- a/media/ffmpeg/ffmpeg_common.cc
+++ b/media/ffmpeg/ffmpeg_common.cc
@@ -206,4 +206,28 @@ int GetSurfaceWidth(AVStream* stream) {
   return width & ~1;
 }
 
+void DestroyAVFormatContext(AVFormatContext* format_context) {
+  DCHECK(format_context);
+
+  // Iterate each stream and destroy each one of them.
+  int streams = format_context->nb_streams;
+  for (int i = 0; i < streams; ++i) {
+    AVStream* stream = format_context->streams[i];
+
+    // The conditions for calling avcodec_close():
+    // 1. AVStream is alive.
+    // 2. AVCodecContext in AVStream is alive.
+    // 3. AVCodec in AVCodecContext is alive.
+    // Notice that closing a codec context without prior avcodec_open() will
+    // result in a crash in FFmpeg.
+    if (stream && stream->codec && stream->codec->codec) {
+      stream->discard = AVDISCARD_ALL;
+      avcodec_close(stream->codec);
+    }
+  }
+
+  // Then finally cleanup the format context.
+  av_close_input_file(format_context);
+}
+
 }  // namespace media
diff --git a/media/ffmpeg/ffmpeg_common.h b/media/ffmpeg/ffmpeg_common.h
index 07daae1..5ac0607 100644
--- a/media/ffmpeg/ffmpeg_common.h
+++ b/media/ffmpeg/ffmpeg_common.h
@@ -109,6 +109,10 @@ bool GetStreamByteCountOverRange(AVStream* stream,
 int GetSurfaceHeight(AVStream* stream);
 int GetSurfaceWidth(AVStream* stream);
 
+// Closes & destroys all AVStreams in the context and then closes &
+// destroys the AVFormatContext.
+void DestroyAVFormatContext(AVFormatContext* format_context);
+
 }  // namespace media
 
 #endif  // MEDIA_FFMPEG_FFMPEG_COMMON_H_
-- 
cgit v1.1