| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <map> | 5 #include <map> |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "services/media/framework/safe_clone.h" | 8 #include "services/media/framework/safe_clone.h" |
| 9 #include "services/media/framework_ffmpeg/av_codec_context.h" |
| 10 #include "services/media/framework_ffmpeg/av_format_context.h" |
| 11 #include "services/media/framework_ffmpeg/av_io_context.h" |
| 9 #include "services/media/framework_ffmpeg/ffmpeg_demux.h" | 12 #include "services/media/framework_ffmpeg/ffmpeg_demux.h" |
| 10 #include "services/media/framework_ffmpeg/ffmpeg_io.h" | |
| 11 #include "services/media/framework_ffmpeg/ffmpeg_type_converters.h" | |
| 12 | 13 |
| 13 namespace mojo { | 14 namespace mojo { |
| 14 namespace media { | 15 namespace media { |
| 15 | 16 |
| 16 class FfmpegDemuxImpl : public FfmpegDemux { | 17 class FfmpegDemuxImpl : public FfmpegDemux { |
| 17 public: | 18 public: |
| 18 FfmpegDemuxImpl(); | 19 FfmpegDemuxImpl(); |
| 19 | 20 |
| 20 ~FfmpegDemuxImpl() override; | 21 ~FfmpegDemuxImpl() override; |
| 21 | 22 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 | 95 |
| 95 // Produces an end-of-stream packet for next_stream_to_end_. | 96 // Produces an end-of-stream packet for next_stream_to_end_. |
| 96 PacketPtr PullEndOfStreamPacket(size_t* stream_index_out); | 97 PacketPtr PullEndOfStreamPacket(size_t* stream_index_out); |
| 97 | 98 |
| 98 // Copies metadata from the specified source into map. | 99 // Copies metadata from the specified source into map. |
| 99 void CopyMetadata( | 100 void CopyMetadata( |
| 100 AVDictionary* source, | 101 AVDictionary* source, |
| 101 std::map<std::string, std::string>& map); | 102 std::map<std::string, std::string>& map); |
| 102 | 103 |
| 103 std::shared_ptr<Reader> reader_; | 104 std::shared_ptr<Reader> reader_; |
| 104 std::unique_ptr<AVFormatContext, AVFormatContextDeleter> format_context_; | 105 AvFormatContextPtr format_context_; |
| 105 AvioContextPtr io_context_; | 106 AvIoContextPtr io_context_; |
| 106 std::vector<DemuxStream*> streams_; | 107 std::vector<DemuxStream*> streams_; |
| 107 std::unique_ptr<Metadata> metadata_; | 108 std::unique_ptr<Metadata> metadata_; |
| 108 int64_t next_presentation_time_; | 109 int64_t next_presentation_time_; |
| 109 int next_stream_to_end_ = -1; // -1: don't end, streams_.size(): stop. | 110 int next_stream_to_end_ = -1; // -1: don't end, streams_.size(): stop. |
| 110 }; | 111 }; |
| 111 | 112 |
| 112 // static | 113 // static |
| 113 std::shared_ptr<Demux> FfmpegDemux::Create() { | 114 std::shared_ptr<Demux> FfmpegDemux::Create() { |
| 114 return std::shared_ptr<Demux>(new FfmpegDemuxImpl()); | 115 return std::shared_ptr<Demux>(new FfmpegDemuxImpl()); |
| 115 } | 116 } |
| 116 | 117 |
| 117 FfmpegDemuxImpl::FfmpegDemuxImpl() {} | 118 FfmpegDemuxImpl::FfmpegDemuxImpl() {} |
| 118 | 119 |
| 119 FfmpegDemuxImpl::~FfmpegDemuxImpl() {} | 120 FfmpegDemuxImpl::~FfmpegDemuxImpl() {} |
| 120 | 121 |
| 121 Result FfmpegDemuxImpl::Init(std::shared_ptr<Reader> reader) { | 122 Result FfmpegDemuxImpl::Init(std::shared_ptr<Reader> reader) { |
| 122 static constexpr uint64_t kNanosecondsPerMicrosecond = 1000; | 123 static constexpr uint64_t kNanosecondsPerMicrosecond = 1000; |
| 123 | 124 |
| 124 reader_ = reader; | 125 reader_ = reader; |
| 125 | 126 |
| 126 io_context_ = CreateAvioContext(reader.get()); | 127 io_context_ = AvIoContext::Create(reader); |
| 127 if (!io_context_) { | 128 if (!io_context_) { |
| 128 LOG(ERROR) << "CreateAvioContext failed (allocation failure)"; | 129 LOG(ERROR) << "AvIoContext::Create failed (allocation failure)"; |
| 129 return Result::kInternalError; | 130 return Result::kInternalError; |
| 130 } | 131 } |
| 131 | 132 |
| 132 // TODO(dalesat): Consider ffmpeg util to centralize memory management. | 133 format_context_ = AvFormatContext::OpenInput(io_context_); |
| 133 AVFormatContext* format_context = avformat_alloc_context(); | 134 if (!format_context_) { |
| 134 format_context->flags |= AVFMT_FLAG_CUSTOM_IO | AVFMT_FLAG_FAST_SEEK; | |
| 135 format_context->pb = io_context_.get(); | |
| 136 | |
| 137 // TODO(dalesat): This synchronous operation may take a long time. | |
| 138 int r = avformat_open_input(&format_context, nullptr, nullptr, nullptr); | |
| 139 format_context_.reset(format_context); | |
| 140 if (r < 0) { | |
| 141 return Result::kInternalError; | 135 return Result::kInternalError; |
| 142 } | 136 } |
| 143 | 137 |
| 144 // TODO(dalesat): This synchronous operation may take a long time. | 138 // TODO(dalesat): This synchronous operation may take a long time. |
| 145 r = avformat_find_stream_info(format_context_.get(), nullptr); | 139 int r = avformat_find_stream_info(format_context_.get(), nullptr); |
| 146 if (r < 0) { | 140 if (r < 0) { |
| 147 LOG(ERROR) << "avformat_find_stream_info failed, result " << r; | 141 LOG(ERROR) << "avformat_find_stream_info failed, result " << r; |
| 148 return Result::kInternalError; | 142 return Result::kInternalError; |
| 149 } | 143 } |
| 150 | 144 |
| 151 std::map<std::string, std::string> metadata_map; | 145 std::map<std::string, std::string> metadata_map; |
| 152 | 146 |
| 153 CopyMetadata(format_context_->metadata, metadata_map); | 147 CopyMetadata(format_context_->metadata, metadata_map); |
| 154 for (uint i = 0; i < format_context_->nb_streams; i++) { | 148 for (uint i = 0; i < format_context_->nb_streams; i++) { |
| 155 streams_.push_back(new FfmpegDemuxStream(*format_context_, i)); | 149 streams_.push_back(new FfmpegDemuxStream(*format_context_, i)); |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 if (map.find(entry->key) == map.end()) { | 241 if (map.find(entry->key) == map.end()) { |
| 248 map.emplace(entry->key, entry->value); | 242 map.emplace(entry->key, entry->value); |
| 249 } | 243 } |
| 250 } | 244 } |
| 251 } | 245 } |
| 252 | 246 |
| 253 FfmpegDemuxImpl::FfmpegDemuxStream::FfmpegDemuxStream( | 247 FfmpegDemuxImpl::FfmpegDemuxStream::FfmpegDemuxStream( |
| 254 const AVFormatContext& format_context, | 248 const AVFormatContext& format_context, |
| 255 size_t index) : | 249 size_t index) : |
| 256 stream_(format_context.streams[index]), index_(index) { | 250 stream_(format_context.streams[index]), index_(index) { |
| 257 stream_type_ = StreamTypeFromAVCodecContext(*stream_->codec); | 251 stream_type_ = AvCodecContext::GetStreamType(*stream_->codec); |
| 258 } | 252 } |
| 259 | 253 |
| 260 FfmpegDemuxImpl::FfmpegDemuxStream::~FfmpegDemuxStream() {} | 254 FfmpegDemuxImpl::FfmpegDemuxStream::~FfmpegDemuxStream() {} |
| 261 | 255 |
| 262 size_t FfmpegDemuxImpl::FfmpegDemuxStream::index() const { | 256 size_t FfmpegDemuxImpl::FfmpegDemuxStream::index() const { |
| 263 return index_; | 257 return index_; |
| 264 } | 258 } |
| 265 | 259 |
| 266 std::unique_ptr<StreamType> FfmpegDemuxImpl::FfmpegDemuxStream::stream_type() | 260 std::unique_ptr<StreamType> FfmpegDemuxImpl::FfmpegDemuxStream::stream_type() |
| 267 const { | 261 const { |
| 268 return SafeClone(stream_type_); | 262 return SafeClone(stream_type_); |
| 269 } | 263 } |
| 270 | 264 |
| 271 } // namespace media | 265 } // namespace media |
| 272 } // namespace mojo | 266 } // namespace mojo |
| OLD | NEW |