| 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/av_packet.h" | 12 #include "services/media/framework_ffmpeg/av_packet.h" |
| 10 #include "services/media/framework_ffmpeg/ffmpeg_demux.h" | 13 #include "services/media/framework_ffmpeg/ffmpeg_demux.h" |
| 11 #include "services/media/framework_ffmpeg/ffmpeg_io.h" | |
| 12 #include "services/media/framework_ffmpeg/ffmpeg_type_converters.h" | |
| 13 | 14 |
| 14 namespace mojo { | 15 namespace mojo { |
| 15 namespace media { | 16 namespace media { |
| 16 | 17 |
| 17 class FfmpegDemuxImpl : public FfmpegDemux { | 18 class FfmpegDemuxImpl : public FfmpegDemux { |
| 18 public: | 19 public: |
| 19 FfmpegDemuxImpl(); | 20 FfmpegDemuxImpl(); |
| 20 | 21 |
| 21 ~FfmpegDemuxImpl() override; | 22 ~FfmpegDemuxImpl() override; |
| 22 | 23 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 | 91 |
| 91 // Produces an end-of-stream packet for next_stream_to_end_. | 92 // Produces an end-of-stream packet for next_stream_to_end_. |
| 92 PacketPtr PullEndOfStreamPacket(size_t* stream_index_out); | 93 PacketPtr PullEndOfStreamPacket(size_t* stream_index_out); |
| 93 | 94 |
| 94 // Copies metadata from the specified source into map. | 95 // Copies metadata from the specified source into map. |
| 95 void CopyMetadata( | 96 void CopyMetadata( |
| 96 AVDictionary* source, | 97 AVDictionary* source, |
| 97 std::map<std::string, std::string>& map); | 98 std::map<std::string, std::string>& map); |
| 98 | 99 |
| 99 std::shared_ptr<Reader> reader_; | 100 std::shared_ptr<Reader> reader_; |
| 100 std::unique_ptr<AVFormatContext, AVFormatContextDeleter> format_context_; | 101 AvFormatContextPtr format_context_; |
| 101 AvioContextPtr io_context_; | 102 AvIoContextPtr io_context_; |
| 102 std::vector<DemuxStream*> streams_; | 103 std::vector<DemuxStream*> streams_; |
| 103 std::unique_ptr<Metadata> metadata_; | 104 std::unique_ptr<Metadata> metadata_; |
| 104 int64_t next_pts_; | 105 int64_t next_pts_; |
| 105 int next_stream_to_end_ = -1; // -1: don't end, streams_.size(): stop. | 106 int next_stream_to_end_ = -1; // -1: don't end, streams_.size(): stop. |
| 106 }; | 107 }; |
| 107 | 108 |
| 108 // static | 109 // static |
| 109 std::shared_ptr<Demux> FfmpegDemux::Create() { | 110 std::shared_ptr<Demux> FfmpegDemux::Create() { |
| 110 return std::shared_ptr<Demux>(new FfmpegDemuxImpl()); | 111 return std::shared_ptr<Demux>(new FfmpegDemuxImpl()); |
| 111 } | 112 } |
| 112 | 113 |
| 113 FfmpegDemuxImpl::FfmpegDemuxImpl() {} | 114 FfmpegDemuxImpl::FfmpegDemuxImpl() {} |
| 114 | 115 |
| 115 FfmpegDemuxImpl::~FfmpegDemuxImpl() {} | 116 FfmpegDemuxImpl::~FfmpegDemuxImpl() {} |
| 116 | 117 |
| 117 Result FfmpegDemuxImpl::Init(std::shared_ptr<Reader> reader) { | 118 Result FfmpegDemuxImpl::Init(std::shared_ptr<Reader> reader) { |
| 118 static constexpr uint64_t kNanosecondsPerMicrosecond = 1000; | 119 static constexpr uint64_t kNanosecondsPerMicrosecond = 1000; |
| 119 | 120 |
| 120 reader_ = reader; | 121 reader_ = reader; |
| 121 | 122 |
| 122 io_context_ = CreateAvioContext(reader.get()); | 123 io_context_ = AvIoContext::Create(reader); |
| 123 if (!io_context_) { | 124 if (!io_context_) { |
| 124 LOG(ERROR) << "CreateAvioContext failed (allocation failure)"; | 125 LOG(ERROR) << "AvIoContext::Create failed (allocation failure)"; |
| 125 return Result::kInternalError; | 126 return Result::kInternalError; |
| 126 } | 127 } |
| 127 | 128 |
| 128 // TODO(dalesat): Consider ffmpeg util to centralize memory management. | 129 format_context_ = AvFormatContext::OpenInput(io_context_); |
| 129 AVFormatContext* format_context = avformat_alloc_context(); | 130 if (!format_context_) { |
| 130 format_context->flags |= AVFMT_FLAG_CUSTOM_IO | AVFMT_FLAG_FAST_SEEK; | |
| 131 format_context->pb = io_context_.get(); | |
| 132 | |
| 133 // TODO(dalesat): This synchronous operation may take a long time. | |
| 134 int r = avformat_open_input(&format_context, nullptr, nullptr, nullptr); | |
| 135 format_context_.reset(format_context); | |
| 136 if (r < 0) { | |
| 137 return Result::kInternalError; | 131 return Result::kInternalError; |
| 138 } | 132 } |
| 139 | 133 |
| 140 // TODO(dalesat): This synchronous operation may take a long time. | 134 // TODO(dalesat): This synchronous operation may take a long time. |
| 141 r = avformat_find_stream_info(format_context_.get(), nullptr); | 135 int r = avformat_find_stream_info(format_context_.get(), nullptr); |
| 142 if (r < 0) { | 136 if (r < 0) { |
| 143 LOG(ERROR) << "avformat_find_stream_info failed, result " << r; | 137 LOG(ERROR) << "avformat_find_stream_info failed, result " << r; |
| 144 return Result::kInternalError; | 138 return Result::kInternalError; |
| 145 } | 139 } |
| 146 | 140 |
| 147 std::map<std::string, std::string> metadata_map; | 141 std::map<std::string, std::string> metadata_map; |
| 148 | 142 |
| 149 CopyMetadata(format_context_->metadata, metadata_map); | 143 CopyMetadata(format_context_->metadata, metadata_map); |
| 150 for (uint i = 0; i < format_context_->nb_streams; i++) { | 144 for (uint i = 0; i < format_context_->nb_streams; i++) { |
| 151 streams_.push_back(new FfmpegDemuxStream(*format_context_, i)); | 145 streams_.push_back(new FfmpegDemuxStream(*format_context_, i)); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 if (map.find(entry->key) == map.end()) { | 234 if (map.find(entry->key) == map.end()) { |
| 241 map.emplace(entry->key, entry->value); | 235 map.emplace(entry->key, entry->value); |
| 242 } | 236 } |
| 243 } | 237 } |
| 244 } | 238 } |
| 245 | 239 |
| 246 FfmpegDemuxImpl::FfmpegDemuxStream::FfmpegDemuxStream( | 240 FfmpegDemuxImpl::FfmpegDemuxStream::FfmpegDemuxStream( |
| 247 const AVFormatContext& format_context, | 241 const AVFormatContext& format_context, |
| 248 size_t index) : | 242 size_t index) : |
| 249 stream_(format_context.streams[index]), index_(index) { | 243 stream_(format_context.streams[index]), index_(index) { |
| 250 stream_type_ = StreamTypeFromAVCodecContext(*stream_->codec); | 244 stream_type_ = AvCodecContext::GetStreamType(*stream_->codec); |
| 251 } | 245 } |
| 252 | 246 |
| 253 FfmpegDemuxImpl::FfmpegDemuxStream::~FfmpegDemuxStream() {} | 247 FfmpegDemuxImpl::FfmpegDemuxStream::~FfmpegDemuxStream() {} |
| 254 | 248 |
| 255 size_t FfmpegDemuxImpl::FfmpegDemuxStream::index() const { | 249 size_t FfmpegDemuxImpl::FfmpegDemuxStream::index() const { |
| 256 return index_; | 250 return index_; |
| 257 } | 251 } |
| 258 | 252 |
| 259 std::unique_ptr<StreamType> FfmpegDemuxImpl::FfmpegDemuxStream::stream_type() | 253 std::unique_ptr<StreamType> FfmpegDemuxImpl::FfmpegDemuxStream::stream_type() |
| 260 const { | 254 const { |
| 261 return SafeClone(stream_type_); | 255 return SafeClone(stream_type_); |
| 262 } | 256 } |
| 263 | 257 |
| 264 } // namespace media | 258 } // namespace media |
| 265 } // namespace mojo | 259 } // namespace mojo |
| OLD | NEW |