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 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
148 format_context_->pb = avio_context_.get(); | 150 format_context_->pb = avio_context_.get(); |
149 } | 151 } |
150 | 152 |
151 bool FFmpegGlue::OpenContext() { | 153 bool FFmpegGlue::OpenContext() { |
152 DCHECK(!open_called_) << "OpenContext() should't be called twice."; | 154 DCHECK(!open_called_) << "OpenContext() should't be called twice."; |
153 | 155 |
154 // If avformat_open_input() is called we have to take a slightly different | 156 // If avformat_open_input() is called we have to take a slightly different |
155 // destruction path to avoid double frees. | 157 // destruction path to avoid double frees. |
156 open_called_ = true; | 158 open_called_ = true; |
157 | 159 |
160 // Attempt to recognize the container by looking at the first few bytes of the | |
161 // stream. The stream position is left unchanged. | |
162 scoped_ptr<uint8[]> buffer(new uint8[8192]); | |
acolwell GONE FROM CHROMIUM
2013/05/22 21:03:14
nit: Could std::vector<uint8> work here? If so the
jrummell
2013/05/22 23:59:46
Done.
| |
163 | |
164 int64 pos = AVIOSeekOperation(avio_context_.get()->opaque, 0, SEEK_CUR); | |
165 AVIOSeekOperation(avio_context_.get()->opaque, 0, SEEK_SET); | |
166 int numRead = | |
167 AVIOReadOperation(avio_context_.get()->opaque, buffer.get(), 8192); | |
168 AVIOSeekOperation(avio_context_.get()->opaque, pos, SEEK_SET); | |
169 if (numRead > 0) { | |
170 // < 0 means Read failed | |
171 container_names::MediaContainerName container = | |
172 container_names::DetermineContainer(buffer.get(), numRead); | |
173 UMA_HISTOGRAM_SPARSE_SLOWLY("Media.DetectedContainer", container); | |
174 } | |
175 | |
158 // By passing NULL for the filename (second parameter) we are telling FFmpeg | 176 // By passing NULL for the filename (second parameter) we are telling FFmpeg |
159 // to use the AVIO context we setup from the AVFormatContext structure. | 177 // to use the AVIO context we setup from the AVFormatContext structure. |
160 return avformat_open_input(&format_context_, NULL, NULL, NULL) == 0; | 178 return avformat_open_input(&format_context_, NULL, NULL, NULL) == 0; |
161 } | 179 } |
162 | 180 |
163 FFmpegGlue::~FFmpegGlue() { | 181 FFmpegGlue::~FFmpegGlue() { |
164 // In the event of avformat_open_input() failure, FFmpeg may sometimes free | 182 // 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 | 183 // our AVFormatContext behind the scenes, but leave the buffer alive. It will |
166 // helpfully set |format_context_| to NULL in this case. | 184 // helpfully set |format_context_| to NULL in this case. |
167 if (!format_context_) { | 185 if (!format_context_) { |
(...skipping 27 matching lines...) Expand all Loading... | |
195 avcodec_close(stream->codec); | 213 avcodec_close(stream->codec); |
196 } | 214 } |
197 } | 215 } |
198 } | 216 } |
199 | 217 |
200 avformat_close_input(&format_context_); | 218 avformat_close_input(&format_context_); |
201 av_free(avio_context_->buffer); | 219 av_free(avio_context_->buffer); |
202 } | 220 } |
203 | 221 |
204 } // namespace media | 222 } // namespace media |
OLD | NEW |