media_file_checker.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #include "media/filters/media_file_checker.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <map>
  8. #include <memory>
  9. #include <utility>
  10. #include "base/bind.h"
  11. #include "base/time/time.h"
  12. #include "media/ffmpeg/ffmpeg_common.h"
  13. #include "media/ffmpeg/ffmpeg_decoding_loop.h"
  14. #include "media/ffmpeg/ffmpeg_deleters.h"
  15. #include "media/filters/blocking_url_protocol.h"
  16. #include "media/filters/ffmpeg_glue.h"
  17. #include "media/filters/file_data_source.h"
  18. namespace media {
  19. namespace {
  20. constexpr int64_t kMaxCheckTimeInSeconds = 5;
  21. void OnMediaFileCheckerError(bool* called) {
  22. *called = false;
  23. }
  24. struct Decoder {
  25. std::unique_ptr<AVCodecContext, ScopedPtrAVFreeContext> context;
  26. std::unique_ptr<FFmpegDecodingLoop> loop;
  27. };
  28. } // namespace
  29. MediaFileChecker::MediaFileChecker(base::File file) : file_(std::move(file)) {}
  30. MediaFileChecker::~MediaFileChecker() = default;
  31. bool MediaFileChecker::Start(base::TimeDelta check_time) {
  32. media::FileDataSource source;
  33. if (!source.Initialize(std::move(file_)))
  34. return false;
  35. bool read_ok = true;
  36. media::BlockingUrlProtocol protocol(
  37. &source, base::BindRepeating(&OnMediaFileCheckerError, &read_ok));
  38. media::FFmpegGlue glue(&protocol);
  39. AVFormatContext* format_context = glue.format_context();
  40. if (!glue.OpenContext())
  41. return false;
  42. if (avformat_find_stream_info(format_context, NULL) < 0)
  43. return false;
  44. // Remember the codec context for any decodable audio or video streams.
  45. bool found_streams = false;
  46. std::vector<Decoder> stream_contexts(format_context->nb_streams);
  47. for (size_t i = 0; i < format_context->nb_streams; ++i) {
  48. AVCodecParameters* cp = format_context->streams[i]->codecpar;
  49. if (cp->codec_type == AVMEDIA_TYPE_AUDIO ||
  50. cp->codec_type == AVMEDIA_TYPE_VIDEO) {
  51. auto context = AVStreamToAVCodecContext(format_context->streams[i]);
  52. if (!context)
  53. continue;
  54. const AVCodec* codec = avcodec_find_decoder(cp->codec_id);
  55. if (codec && avcodec_open2(context.get(), codec, nullptr) >= 0) {
  56. auto loop = std::make_unique<FFmpegDecodingLoop>(context.get());
  57. stream_contexts[i] = {std::move(context), std::move(loop)};
  58. found_streams = true;
  59. }
  60. }
  61. }
  62. if (!found_streams)
  63. return false;
  64. AVPacket packet;
  65. int result = 0;
  66. auto do_nothing_cb = base::BindRepeating([](AVFrame*) { return true; });
  67. const base::TimeTicks deadline =
  68. base::TimeTicks::Now() +
  69. std::min(check_time, base::Seconds(kMaxCheckTimeInSeconds));
  70. do {
  71. result = av_read_frame(glue.format_context(), &packet);
  72. if (result < 0)
  73. break;
  74. auto& decoder = stream_contexts[packet.stream_index];
  75. if (decoder.loop) {
  76. result = decoder.loop->DecodePacket(&packet, do_nothing_cb) ==
  77. FFmpegDecodingLoop::DecodeStatus::kOkay
  78. ? 0
  79. : -1;
  80. }
  81. av_packet_unref(&packet);
  82. } while (base::TimeTicks::Now() < deadline && read_ok && result >= 0);
  83. stream_contexts.clear();
  84. return read_ok && (result == AVERROR_EOF || result >= 0);
  85. }
  86. } // namespace media