| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "base/scoped_ptr.h" | 5 #include "base/scoped_ptr.h" |
| 6 #include "base/string_util.h" | 6 #include "base/string_util.h" |
| 7 #include "base/time.h" | 7 #include "base/time.h" |
| 8 #include "media/base/filter_host.h" | 8 #include "media/base/filter_host.h" |
| 9 #include "media/filters/ffmpeg_common.h" | 9 #include "media/filters/ffmpeg_common.h" |
| 10 #include "media/filters/ffmpeg_demuxer.h" | 10 #include "media/filters/ffmpeg_demuxer.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 scoped_ptr<AVPacket> packet_; | 42 scoped_ptr<AVPacket> packet_; |
| 43 | 43 |
| 44 DISALLOW_COPY_AND_ASSIGN(AVPacketBuffer); | 44 DISALLOW_COPY_AND_ASSIGN(AVPacketBuffer); |
| 45 }; | 45 }; |
| 46 | 46 |
| 47 | 47 |
| 48 // | 48 // |
| 49 // FFmpegDemuxerStream | 49 // FFmpegDemuxerStream |
| 50 // | 50 // |
| 51 FFmpegDemuxerStream::FFmpegDemuxerStream(FFmpegDemuxer* demuxer, | 51 FFmpegDemuxerStream::FFmpegDemuxerStream(FFmpegDemuxer* demuxer, |
| 52 const AVStream& stream) | 52 AVStream* stream) |
| 53 : demuxer_(demuxer) { | 53 : demuxer_(demuxer), |
| 54 av_stream_(stream) { |
| 54 DCHECK(demuxer_); | 55 DCHECK(demuxer_); |
| 55 | 56 |
| 56 // Determine our media format. | 57 // Determine our media format. |
| 57 switch (stream.codec->codec_type) { | 58 switch (stream->codec->codec_type) { |
| 58 case CODEC_TYPE_AUDIO: | 59 case CODEC_TYPE_AUDIO: |
| 59 media_format_.SetAsString(MediaFormat::kMimeType, | 60 media_format_.SetAsString(MediaFormat::kMimeType, |
| 60 mime_type::kFFmpegAudio); | 61 mime_type::kFFmpegAudio); |
| 61 media_format_.SetAsInteger(MediaFormat::kChannels, | 62 media_format_.SetAsInteger(MediaFormat::kChannels, |
| 62 stream.codec->channels); | 63 stream->codec->channels); |
| 63 media_format_.SetAsInteger(MediaFormat::kSampleRate, | 64 media_format_.SetAsInteger(MediaFormat::kSampleRate, |
| 64 stream.codec->sample_rate); | 65 stream->codec->sample_rate); |
| 65 break; | 66 break; |
| 66 case CODEC_TYPE_VIDEO: | 67 case CODEC_TYPE_VIDEO: |
| 67 media_format_.SetAsString(MediaFormat::kMimeType, | 68 media_format_.SetAsString(MediaFormat::kMimeType, |
| 68 mime_type::kFFmpegVideo); | 69 mime_type::kFFmpegVideo); |
| 69 media_format_.SetAsInteger(MediaFormat::kHeight, | 70 media_format_.SetAsInteger(MediaFormat::kHeight, |
| 70 stream.codec->height); | 71 stream->codec->height); |
| 71 media_format_.SetAsInteger(MediaFormat::kWidth, | 72 media_format_.SetAsInteger(MediaFormat::kWidth, |
| 72 stream.codec->width); | 73 stream->codec->width); |
| 73 break; | 74 break; |
| 74 default: | 75 default: |
| 75 NOTREACHED(); | 76 NOTREACHED(); |
| 76 break; | 77 break; |
| 77 } | 78 } |
| 78 int codec_id = static_cast<int>(stream.codec->codec_id); | 79 int codec_id = static_cast<int>(stream->codec->codec_id); |
| 79 media_format_.SetAsInteger(kFFmpegCodecID, codec_id); | 80 media_format_.SetAsInteger(kFFmpegCodecID, codec_id); |
| 80 | 81 |
| 81 // Calculate the time base and duration in microseconds. | 82 // Calculate the time base and duration in microseconds. |
| 82 int64 time_base_us = static_cast<int64>(av_q2d(stream.time_base) * | 83 int64 time_base_us = static_cast<int64>(av_q2d(stream->time_base) * |
| 83 base::Time::kMicrosecondsPerSecond); | 84 base::Time::kMicrosecondsPerSecond); |
| 84 int64 duration_us = static_cast<int64>(time_base_us * stream.duration); | 85 int64 duration_us = static_cast<int64>(time_base_us * stream->duration); |
| 85 time_base_ = base::TimeDelta::FromMicroseconds(time_base_us); | 86 time_base_ = base::TimeDelta::FromMicroseconds(time_base_us); |
| 86 duration_ = base::TimeDelta::FromMicroseconds(duration_us); | 87 duration_ = base::TimeDelta::FromMicroseconds(duration_us); |
| 87 } | 88 } |
| 88 | 89 |
| 89 FFmpegDemuxerStream::~FFmpegDemuxerStream() { | 90 FFmpegDemuxerStream::~FFmpegDemuxerStream() { |
| 90 // Since |input_queue_| and |output_queue_| use scoped_refptr everything | 91 // Since |input_queue_| and |output_queue_| use scoped_refptr everything |
| 91 // should get released. | 92 // should get released. |
| 92 } | 93 } |
| 93 | 94 |
| 95 // static |
| 96 const char* FFmpegDemuxerStream::interface_id() { |
| 97 return interface_id::kFFmpegDemuxerStream; |
| 98 } |
| 99 |
| 100 void* FFmpegDemuxerStream::QueryInterface(const char* id) { |
| 101 DCHECK(id); |
| 102 FFmpegDemuxerStream* interface_ptr = NULL; |
| 103 if (0 == strcmp(id, interface_id())) { |
| 104 interface_ptr = this; |
| 105 } |
| 106 return interface_ptr; |
| 107 } |
| 108 |
| 94 bool FFmpegDemuxerStream::HasPendingReads() { | 109 bool FFmpegDemuxerStream::HasPendingReads() { |
| 95 AutoLock auto_lock(lock_); | 110 AutoLock auto_lock(lock_); |
| 96 return !output_queue_.empty(); | 111 return !output_queue_.empty(); |
| 97 } | 112 } |
| 98 | 113 |
| 99 void FFmpegDemuxerStream::EnqueuePacket(AVPacket* packet) { | 114 void FFmpegDemuxerStream::EnqueuePacket(AVPacket* packet) { |
| 100 base::TimeDelta timestamp = time_base_ * packet->pts; | 115 base::TimeDelta timestamp = time_base_ * packet->pts; |
| 101 base::TimeDelta duration = time_base_ * packet->duration; | 116 base::TimeDelta duration = time_base_ * packet->duration; |
| 102 Buffer* buffer = new AVPacketBuffer(packet, timestamp, duration); | 117 Buffer* buffer = new AVPacketBuffer(packet, timestamp, duration); |
| 103 DCHECK(buffer); | 118 DCHECK(buffer); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 // | 166 // |
| 152 FFmpegDemuxer::FFmpegDemuxer() | 167 FFmpegDemuxer::FFmpegDemuxer() |
| 153 : demuxing_(false), | 168 : demuxing_(false), |
| 154 format_context_(NULL) { | 169 format_context_(NULL) { |
| 155 } | 170 } |
| 156 | 171 |
| 157 FFmpegDemuxer::~FFmpegDemuxer() { | 172 FFmpegDemuxer::~FFmpegDemuxer() { |
| 158 if (format_context_) { | 173 if (format_context_) { |
| 159 av_free(format_context_); | 174 av_free(format_context_); |
| 160 } | 175 } |
| 161 while (!streams_.empty()) { | |
| 162 delete streams_.back(); | |
| 163 streams_.pop_back(); | |
| 164 } | |
| 165 } | 176 } |
| 166 | 177 |
| 167 void FFmpegDemuxer::ScheduleDemux() { | 178 void FFmpegDemuxer::ScheduleDemux() { |
| 168 if (!demuxing_) { | 179 if (!demuxing_) { |
| 169 demuxing_ = true; | 180 demuxing_ = true; |
| 170 host_->PostTask(NewRunnableMethod(this, &FFmpegDemuxer::Demux)); | 181 host_->PostTask(NewRunnableMethod(this, &FFmpegDemuxer::Demux)); |
| 171 } | 182 } |
| 172 } | 183 } |
| 173 | 184 |
| 174 void FFmpegDemuxer::Stop() { | 185 void FFmpegDemuxer::Stop() { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 return false; | 219 return false; |
| 209 } | 220 } |
| 210 | 221 |
| 211 // Create demuxer streams for all supported streams. | 222 // Create demuxer streams for all supported streams. |
| 212 base::TimeDelta max_duration; | 223 base::TimeDelta max_duration; |
| 213 for (size_t i = 0; i < format_context_->nb_streams; ++i) { | 224 for (size_t i = 0; i < format_context_->nb_streams; ++i) { |
| 214 CodecType codec_type = format_context_->streams[i]->codec->codec_type; | 225 CodecType codec_type = format_context_->streams[i]->codec->codec_type; |
| 215 if (codec_type == CODEC_TYPE_AUDIO || codec_type == CODEC_TYPE_VIDEO) { | 226 if (codec_type == CODEC_TYPE_AUDIO || codec_type == CODEC_TYPE_VIDEO) { |
| 216 AVStream* stream = format_context_->streams[i]; | 227 AVStream* stream = format_context_->streams[i]; |
| 217 FFmpegDemuxerStream* demuxer_stream | 228 FFmpegDemuxerStream* demuxer_stream |
| 218 = new FFmpegDemuxerStream(this, *stream); | 229 = new FFmpegDemuxerStream(this, stream); |
| 219 DCHECK(demuxer_stream); | 230 DCHECK(demuxer_stream); |
| 220 streams_.push_back(demuxer_stream); | 231 streams_.push_back(demuxer_stream); |
| 221 max_duration = std::max(max_duration, demuxer_stream->duration()); | 232 max_duration = std::max(max_duration, demuxer_stream->duration()); |
| 222 } | 233 } |
| 223 } | 234 } |
| 224 if (streams_.empty()) { | 235 if (streams_.empty()) { |
| 225 host_->Error(DEMUXER_ERROR_NO_SUPPORTED_STREAMS); | 236 host_->Error(DEMUXER_ERROR_NO_SUPPORTED_STREAMS); |
| 226 return false; | 237 return false; |
| 227 } | 238 } |
| 228 | 239 |
| 229 // We have at least one supported stream, set the duration and notify we're | 240 // We have at least one supported stream, set the duration and notify we're |
| 230 // done initializing. | 241 // done initializing. |
| 231 host_->SetDuration(max_duration); | 242 host_->SetDuration(max_duration); |
| 232 host_->InitializationComplete(); | 243 host_->InitializationComplete(); |
| 233 return true; | 244 return true; |
| 234 } | 245 } |
| 235 | 246 |
| 236 size_t FFmpegDemuxer::GetNumberOfStreams() { | 247 size_t FFmpegDemuxer::GetNumberOfStreams() { |
| 237 return streams_.size(); | 248 return streams_.size(); |
| 238 } | 249 } |
| 239 | 250 |
| 240 DemuxerStream* FFmpegDemuxer::GetStream(int stream) { | 251 scoped_refptr<DemuxerStream> FFmpegDemuxer::GetStream(int stream) { |
| 241 DCHECK(stream >= 0); | 252 DCHECK(stream >= 0); |
| 242 DCHECK(stream < static_cast<int>(streams_.size())); | 253 DCHECK(stream < static_cast<int>(streams_.size())); |
| 243 return streams_[stream]; | 254 return streams_[stream].get(); |
| 244 } | 255 } |
| 245 | 256 |
| 246 void FFmpegDemuxer::Demux() { | 257 void FFmpegDemuxer::Demux() { |
| 247 DCHECK(demuxing_); | 258 DCHECK(demuxing_); |
| 248 | 259 |
| 249 // Loop until we've satisfied every stream. | 260 // Loop until we've satisfied every stream. |
| 250 while (StreamsHavePendingReads()) { | 261 while (StreamsHavePendingReads()) { |
| 251 // Allocate and read an AVPacket from the media. | 262 // Allocate and read an AVPacket from the media. |
| 252 scoped_ptr<AVPacket> packet(new AVPacket()); | 263 scoped_ptr<AVPacket> packet(new AVPacket()); |
| 253 int result = av_read_frame(format_context_, packet.get()); | 264 int result = av_read_frame(format_context_, packet.get()); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 273 StreamVector::iterator iter; | 284 StreamVector::iterator iter; |
| 274 for (iter = streams_.begin(); iter != streams_.end(); ++iter) { | 285 for (iter = streams_.begin(); iter != streams_.end(); ++iter) { |
| 275 if ((*iter)->HasPendingReads()) { | 286 if ((*iter)->HasPendingReads()) { |
| 276 return true; | 287 return true; |
| 277 } | 288 } |
| 278 } | 289 } |
| 279 return false; | 290 return false; |
| 280 } | 291 } |
| 281 | 292 |
| 282 } // namespace media | 293 } // namespace media |
| OLD | NEW |