Chromium Code Reviews| 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..10968c7720e333e0273f275beca86562608d01f8 |
| --- /dev/null |
| +++ b/media/base/media_file_checker.cc |
| @@ -0,0 +1,131 @@ |
| +// 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/stl_util.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 { |
| + |
| +namespace { |
| + |
| +const int64 kMaxCheckTimeIneconds = 5; |
|
DaleCurtis
2013/08/08 21:04:33
InSeconds
vandebo (ex-Chrome)
2013/08/08 23:04:18
Done.
|
| + |
| +void OnError(bool* called) { |
| + *called = false; |
| +} |
| + |
| +struct StreamContext { |
| + StreamContext() |
| + : codec_context(NULL), |
| + codec(NULL) { |
| + } |
| + AVCodecContext* codec_context; |
| + AVCodec* codec; |
| +}; |
| + |
| +} // namespace |
| + |
| + |
| +MediaFileChecker::MediaFileChecker(base::PlatformFile file) |
| + : file_(file), |
| + file_closer_(&file_) { |
| +} |
| + |
| +MediaFileChecker::~MediaFileChecker() { |
| +} |
| + |
| +bool MediaFileChecker::Start(const 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 for any decodable audio or video streams. |
| + std::map<int, StreamContext> 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].codec_context = c; |
| + stream_contexts[i].codec = codec; |
|
DaleCurtis
2013/08/08 21:04:33
Doesn't look like you actually use codec anywhere.
vandebo (ex-Chrome)
2013/08/08 23:04:18
Done.
|
| + } |
| + } |
| + } |
| + |
| + 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; |
|
DaleCurtis
2013/08/08 21:04:33
const base::Time deadline = base::Time::Now() + ba
vandebo (ex-Chrome)
2013/08/08 23:04:18
Used std::min (negative check time will have the s
|
| + const base::TimeDelta max_check_time = |
| + base::TimeDelta::FromSeconds(kMaxCheckTimeIneconds); |
| + if (check_time > max_check_time) |
| + deadline = base::Time::Now() + max_check_time; |
| + else |
| + deadline = base::Time::Now() + check_time; |
| + |
| + do { |
| + result = av_read_frame(glue.format_context(), &packet); |
| + if (result < 0) |
| + break; |
| + |
|
DaleCurtis
2013/08/08 21:04:33
I think you need an av_dup_packet(&packet) here ot
vandebo (ex-Chrome)
2013/08/08 23:04:18
Done.
|
| + if (!ContainsKey(stream_contexts, packet.stream_index)) |
|
DaleCurtis
2013/08/08 21:04:33
You end up doing multiple map look ups below. Ins
vandebo (ex-Chrome)
2013/08/08 23:04:18
Done.
|
| + continue; |
| + |
| + int frame_decoded = 0; |
| + AVMediaType type = |
| + stream_contexts[packet.stream_index].codec_context->codec_type; |
| + if (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( |
| + stream_contexts[packet.stream_index].codec_context, frame.get(), |
| + &frame_decoded, &packet); |
|
DaleCurtis
2013/08/08 21:04:33
temp_packet
vandebo (ex-Chrome)
2013/08/08 23:04:18
Done.
|
| + if (result < 0) |
| + break; |
| + temp_packet.size -= result; |
| + temp_packet.data += result; |
| + } while (temp_packet.size > 0); |
|
DaleCurtis
2013/08/08 21:04:33
Extra space.
vandebo (ex-Chrome)
2013/08/08 23:04:18
Done.
vandebo (ex-Chrome)
2013/08/08 23:04:18
BTW - This is what made me think I didn't need the
|
| + } else if (type == AVMEDIA_TYPE_VIDEO) { |
| + avcodec_get_frame_defaults(frame.get()); |
| + result = avcodec_decode_video2( |
| + stream_contexts[packet.stream_index].codec_context, frame.get(), |
| + &frame_decoded, &packet); |
| + } |
| + } while (base::Time::Now() < deadline && read_ok && result >= 0); |
| + |
| + av_free_packet(&packet); |
|
DaleCurtis
2013/08/08 21:04:33
This needs to be inside the while loop.
vandebo (ex-Chrome)
2013/08/08 23:04:18
Done.
|
| + |
| + if (result == AVERROR_EOF) |
|
DaleCurtis
2013/08/08 21:04:33
Could be rewritten as:
return read_ok && (result =
vandebo (ex-Chrome)
2013/08/08 23:04:18
Done.
|
| + result = 0; |
| + return read_ok && result >= 0; |
| +} |
| + |
| +} // namespace media |