Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(456)

Unified Diff: media/base/media_file_checker.cc

Issue 20572004: Add media file validation to utility process (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Final nits Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « media/base/media_file_checker.h ('k') | media/base/media_file_checker_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: media/base/media_file_checker.cc
diff --git a/media/base/media_file_checker.cc b/media/base/media_file_checker.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d4708e506c7619fb75f95d45d9c0ee7f09632ce2
--- /dev/null
+++ b/media/base/media_file_checker.cc
@@ -0,0 +1,110 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "media/base/media_file_checker.h"
+
+#include <map>
+
+#include "base/bind.h"
+#include "base/time/time.h"
+#include "media/ffmpeg/ffmpeg_common.h"
+#include "media/filters/blocking_url_protocol.h"
+#include "media/filters/ffmpeg_glue.h"
+#include "media/filters/file_data_source.h"
+
+namespace media {
+
+static const int64 kMaxCheckTimeInSeconds = 5;
+
+static void OnError(bool* called) {
+ *called = false;
+}
+
+MediaFileChecker::MediaFileChecker(const base::PlatformFile& file)
+ : file_(file),
+ file_closer_(&file_) {
+}
+
+MediaFileChecker::~MediaFileChecker() {
+}
+
+bool MediaFileChecker::Start(base::TimeDelta check_time) {
+ media::FileDataSource source;
+ bool read_ok = true;
+ media::BlockingUrlProtocol protocol(&source, base::Bind(&OnError, &read_ok));
+ media::FFmpegGlue glue(&protocol);
+ source.InitializeFromPlatformFile(file_);
+ AVFormatContext* format_context = glue.format_context();
+
+ if (!glue.OpenContext())
+ return false;
+
+ if (avformat_find_stream_info(format_context, NULL) < 0)
+ return false;
+
+ // Remember the codec context for any decodable audio or video streams.
+ std::map<int, AVCodecContext*> stream_contexts;
+ for (size_t i = 0; i < format_context->nb_streams; ++i) {
+ AVCodecContext* c = format_context->streams[i]->codec;
+ if (c->codec_type == AVMEDIA_TYPE_AUDIO ||
+ c->codec_type == AVMEDIA_TYPE_VIDEO) {
+ AVCodec* codec = avcodec_find_decoder(c->codec_id);
+ if (codec && avcodec_open2(c, codec, NULL) >= 0)
+ stream_contexts[i] = c;
+ }
+ }
+
+ if (stream_contexts.size() == 0)
+ return false;
+
+ AVPacket packet;
+ scoped_ptr_malloc<AVFrame, media::ScopedPtrAVFree> frame(
+ avcodec_alloc_frame());
+ int result = 0;
+
+ base::Time deadline = base::Time::Now() +
+ std::min(check_time,
+ base::TimeDelta::FromSeconds(kMaxCheckTimeInSeconds));
+ do {
+ result = av_read_frame(glue.format_context(), &packet);
+ if (result < 0)
+ break;
+ result = av_dup_packet(&packet);
+ if (result < 0)
+ break;
+
+ std::map<int, AVCodecContext*>::const_iterator it =
+ stream_contexts.find(packet.stream_index);
+ if (it == stream_contexts.end()) {
+ av_free_packet(&packet);
+ continue;
+ }
+ AVCodecContext* av_context = it->second;
+
+ int frame_decoded = 0;
+ if (av_context->codec_type == AVMEDIA_TYPE_AUDIO) {
+ // A shallow copy of packet so we can slide packet.data as frames are
+ // decoded; otherwise av_free_packet() will corrupt memory.
+ AVPacket temp_packet = packet;
+ do {
+ avcodec_get_frame_defaults(frame.get());
+ result = avcodec_decode_audio4(av_context, frame.get(), &frame_decoded,
+ &temp_packet);
+ if (result < 0)
+ break;
+ temp_packet.size -= result;
+ temp_packet.data += result;
+ } while (temp_packet.size > 0);
+ } else if (av_context->codec_type == AVMEDIA_TYPE_VIDEO) {
+ avcodec_get_frame_defaults(frame.get());
+ result = avcodec_decode_video2(av_context, frame.get(), &frame_decoded,
+ &packet);
+ }
+ av_free_packet(&packet);
+ } while (base::Time::Now() < deadline && read_ok && result >= 0);
+
+ return read_ok && (result == AVERROR_EOF || result >= 0);
+}
+
+} // namespace media
« no previous file with comments | « media/base/media_file_checker.h ('k') | media/base/media_file_checker_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698