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

Side by Side 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: 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "media/base/media_file_checker.h"
6
7 #include <map>
8
9 #include "base/bind.h"
10 #include "base/stl_util.h"
11 #include "base/time/time.h"
12 #include "media/ffmpeg/ffmpeg_common.h"
13 #include "media/filters/blocking_url_protocol.h"
14 #include "media/filters/ffmpeg_glue.h"
15 #include "media/filters/file_data_source.h"
16
17 namespace media {
18
19 namespace {
20
21 void ClearBoolCallback(bool* called) {
DaleCurtis 2013/08/07 18:27:07 Name is pretty meaningless. How about OnError() i
vandebo (ex-Chrome) 2013/08/07 21:00:43 Done.
22 *called = false;
23 }
24
25 struct StreamContext {
26 StreamContext()
27 : codec_context(NULL),
28 codec(NULL) {
29 }
30 AVCodecContext* codec_context;
31 AVCodec* codec;
32 };
33
34 } // namespace
35
36
37 MediaFileChecker::MediaFileChecker(base::PlatformFile file)
38 : file_(file),
39 file_closer_(&file_) {
40 }
41
42 MediaFileChecker::~MediaFileChecker() {
43 }
44
45 bool MediaFileChecker::Start(const base::TimeDelta& check_time) {
DaleCurtis 2013/08/07 18:27:07 Sanity check check_time to be positive and less th
vandebo (ex-Chrome) 2013/08/07 21:00:43 Done.
46 media::FileDataSource source;
47 bool read_ok = true;
48 media::BlockingUrlProtocol protocol(&source,
49 base::Bind(&ClearBoolCallback,
50 base::Unretained(&read_ok)));
DaleCurtis 2013/08/07 18:27:07 Is unretained necessary here?
vandebo (ex-Chrome) 2013/08/07 21:00:43 Done.
51 media::FFmpegGlue glue(&protocol);
52 source.InitializeFromPlatformFile(file_);
53 AVFormatContext* format_context = glue.format_context();
54
55 if (!glue.OpenContext())
56 return false;
57
58 if (avformat_find_stream_info(format_context, NULL) < 0)
59 return false;
60
61 // Remember the codec for any decodable audio or video streams.
62 std::map<int, StreamContext> stream_contexts;
63 for (size_t i = 0; i < format_context->nb_streams; ++i) {
DaleCurtis 2013/08/07 18:27:07 Just FYI, the way you're doing this doesn't verify
vandebo (ex-Chrome) 2013/08/07 21:00:43 Not what it says it is in what sense? It doesn't
64 AVCodecContext* c = format_context->streams[i]->codec;
65 if (c->codec_type == AVMEDIA_TYPE_AUDIO ||
66 c->codec_type == AVMEDIA_TYPE_VIDEO) {
67 AVCodec* codec = avcodec_find_decoder(c->codec_id);
68 if (codec && avcodec_open2(c, codec, NULL) >= 0) {
69 stream_contexts[i].codec_context = c;
70 stream_contexts[i].codec = codec;
71 }
72 }
73 }
74
75 if (stream_contexts.size() == 0)
76 return false;
77
78
DaleCurtis 2013/08/07 18:27:07 Spacing?
vandebo (ex-Chrome) 2013/08/07 21:00:43 Done. Don't know how that got in there... :-/
79
80 AVPacket packet;
81 scoped_ptr_malloc<AVFrame, media::ScopedPtrAVFree> frame(
82 avcodec_alloc_frame());
83 int result_code = 0;
84 base::Time deadline = base::Time::Now() + check_time;
85
86 do {
DaleCurtis 2013/08/07 18:27:07 Not quite correct, you may need to call this multi
vandebo (ex-Chrome) 2013/08/07 21:00:43 I don't really understand the API, but I've copied
DaleCurtis 2013/08/07 22:56:20 This is not what's in AudioFileReader, note that A
87 result_code = av_read_frame(glue.format_context(), &packet);
88 if (result_code < 0)
89 break;
90
91 if (!ContainsKey(stream_contexts, packet.stream_index))
92 continue;
93
94 avcodec_get_frame_defaults(frame.get());
95 int frame_decoded = 0;
96 AVMediaType type =
97 stream_contexts[packet.stream_index].codec_context->codec_type;
98 if (type == AVMEDIA_TYPE_AUDIO) {
99 result_code = avcodec_decode_audio4(
100 stream_contexts[packet.stream_index].codec_context, frame.get(),
101 &frame_decoded, &packet);
102 } else if (type == AVMEDIA_TYPE_VIDEO) {
103 result_code = avcodec_decode_video2(
104 stream_contexts[packet.stream_index].codec_context, frame.get(),
105 &frame_decoded, &packet);
106 }
107 } while (base::Time::Now() < deadline && result_code >= 0);
DaleCurtis 2013/08/07 18:27:07 && read_ok
vandebo (ex-Chrome) 2013/08/07 21:00:43 Done.
108
109 av_free_packet(&packet);
110
111 if (result_code == AVERROR_EOF)
112 result_code = 0;
113 return read_ok && result_code >= 0;
114 }
115
116 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698