Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "media/filters/ffmpeg_glue.h" | 5 #include "media/filters/ffmpeg_glue.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/metrics/sparse_histogram.h" | |
| 9 #include "base/synchronization/lock.h" | 10 #include "base/synchronization/lock.h" |
| 11 #include "media/base/container_names.h" | |
| 10 #include "media/ffmpeg/ffmpeg_common.h" | 12 #include "media/ffmpeg/ffmpeg_common.h" |
| 11 | 13 |
| 12 namespace media { | 14 namespace media { |
| 13 | 15 |
| 14 // Internal buffer size used by AVIO for reading. | 16 // Internal buffer size used by AVIO for reading. |
| 15 // TODO(dalecurtis): Experiment with this buffer size and measure impact on | 17 // TODO(dalecurtis): Experiment with this buffer size and measure impact on |
| 16 // performance. Currently we want to use 32kb to preserve existing behavior | 18 // performance. Currently we want to use 32kb to preserve existing behavior |
| 17 // with the previous URLProtocol based approach. | 19 // with the previous URLProtocol based approach. |
| 18 enum { kBufferSize = 32 * 1024 }; | 20 enum { kBufferSize = 32 * 1024 }; |
| 19 | 21 |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 116 DISALLOW_COPY_AND_ASSIGN(FFmpegInitializer); | 118 DISALLOW_COPY_AND_ASSIGN(FFmpegInitializer); |
| 117 }; | 119 }; |
| 118 | 120 |
| 119 void FFmpegGlue::InitializeFFmpeg() { | 121 void FFmpegGlue::InitializeFFmpeg() { |
| 120 static base::LazyInstance<FFmpegInitializer>::Leaky li = | 122 static base::LazyInstance<FFmpegInitializer>::Leaky li = |
| 121 LAZY_INSTANCE_INITIALIZER; | 123 LAZY_INSTANCE_INITIALIZER; |
| 122 CHECK(li.Get().initialized()); | 124 CHECK(li.Get().initialized()); |
| 123 } | 125 } |
| 124 | 126 |
| 125 FFmpegGlue::FFmpegGlue(FFmpegURLProtocol* protocol) | 127 FFmpegGlue::FFmpegGlue(FFmpegURLProtocol* protocol) |
| 126 : open_called_(false) { | 128 : open_called_(false), |
| 129 protocol_(protocol) { | |
| 127 InitializeFFmpeg(); | 130 InitializeFFmpeg(); |
| 128 | 131 |
| 129 // Initialize an AVIOContext using our custom read and seek operations. Don't | 132 // Initialize an AVIOContext using our custom read and seek operations. Don't |
| 130 // keep pointers to the buffer since FFmpeg may reallocate it on the fly. It | 133 // keep pointers to the buffer since FFmpeg may reallocate it on the fly. It |
| 131 // will be cleaned up | 134 // will be cleaned up |
| 132 format_context_ = avformat_alloc_context(); | 135 format_context_ = avformat_alloc_context(); |
| 133 avio_context_.reset(avio_alloc_context( | 136 avio_context_.reset(avio_alloc_context( |
| 134 static_cast<unsigned char*>(av_malloc(kBufferSize)), kBufferSize, 0, | 137 static_cast<unsigned char*>(av_malloc(kBufferSize)), kBufferSize, 0, |
| 135 protocol, &AVIOReadOperation, NULL, &AVIOSeekOperation)); | 138 protocol, &AVIOReadOperation, NULL, &AVIOSeekOperation)); |
| 136 | 139 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 148 format_context_->pb = avio_context_.get(); | 151 format_context_->pb = avio_context_.get(); |
| 149 } | 152 } |
| 150 | 153 |
| 151 bool FFmpegGlue::OpenContext() { | 154 bool FFmpegGlue::OpenContext() { |
| 152 DCHECK(!open_called_) << "OpenContext() should't be called twice."; | 155 DCHECK(!open_called_) << "OpenContext() should't be called twice."; |
| 153 | 156 |
| 154 // If avformat_open_input() is called we have to take a slightly different | 157 // If avformat_open_input() is called we have to take a slightly different |
| 155 // destruction path to avoid double frees. | 158 // destruction path to avoid double frees. |
| 156 open_called_ = true; | 159 open_called_ = true; |
| 157 | 160 |
| 161 // Attempt to recognize the container by looking at the first few bytes of the | |
| 162 // stream. The stream position is left unchanged. | |
| 163 int64 pos; | |
| 164 scoped_ptr<uint8[]> buffer(new uint8[8192]); | |
| 165 | |
| 166 protocol_->GetPosition(&pos); | |
|
acolwell GONE FROM CHROMIUM
2013/05/18 01:22:09
nit: I think you can use avio_context_->opaque to
jrummell
2013/05/22 18:27:39
Done.
| |
| 167 protocol_->SetPosition(0); | |
| 168 int numRead = protocol_->Read(8192, buffer.get()); | |
| 169 protocol_->SetPosition(pos); | |
| 170 if (numRead > 0) { | |
| 171 // < 0 means Read failed | |
| 172 container_names::FFmpegContainerName container = | |
| 173 container_names::DetermineContainer(buffer.get(), numRead); | |
| 174 UMA_HISTOGRAM_SPARSE_SLOWLY("Media.DetectedContainer", container); | |
| 175 } | |
| 176 | |
| 158 // By passing NULL for the filename (second parameter) we are telling FFmpeg | 177 // By passing NULL for the filename (second parameter) we are telling FFmpeg |
| 159 // to use the AVIO context we setup from the AVFormatContext structure. | 178 // to use the AVIO context we setup from the AVFormatContext structure. |
| 160 return avformat_open_input(&format_context_, NULL, NULL, NULL) == 0; | 179 return avformat_open_input(&format_context_, NULL, NULL, NULL) == 0; |
| 161 } | 180 } |
| 162 | 181 |
| 163 FFmpegGlue::~FFmpegGlue() { | 182 FFmpegGlue::~FFmpegGlue() { |
| 164 // In the event of avformat_open_input() failure, FFmpeg may sometimes free | 183 // In the event of avformat_open_input() failure, FFmpeg may sometimes free |
| 165 // our AVFormatContext behind the scenes, but leave the buffer alive. It will | 184 // our AVFormatContext behind the scenes, but leave the buffer alive. It will |
| 166 // helpfully set |format_context_| to NULL in this case. | 185 // helpfully set |format_context_| to NULL in this case. |
| 167 if (!format_context_) { | 186 if (!format_context_) { |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 195 avcodec_close(stream->codec); | 214 avcodec_close(stream->codec); |
| 196 } | 215 } |
| 197 } | 216 } |
| 198 } | 217 } |
| 199 | 218 |
| 200 avformat_close_input(&format_context_); | 219 avformat_close_input(&format_context_); |
| 201 av_free(avio_context_->buffer); | 220 av_free(avio_context_->buffer); |
| 202 } | 221 } |
| 203 | 222 |
| 204 } // namespace media | 223 } // namespace media |
| OLD | NEW |