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 | |
xhwang
2013/04/29 21:00:25
keep this empty line: http://google-styleguide.goo
jrummell
2013/04/30 21:36:50
Done.
| |
7 #include "base/lazy_instance.h" | 6 #include "base/lazy_instance.h" |
8 #include "base/logging.h" | 7 #include "base/logging.h" |
9 #include "base/synchronization/lock.h" | 8 #include "base/synchronization/lock.h" |
9 #include "media/base/container_names.h" | |
10 #include "media/ffmpeg/ffmpeg_common.h" | 10 #include "media/ffmpeg/ffmpeg_common.h" |
11 | 11 |
12 namespace media { | 12 namespace media { |
13 | 13 |
14 // Internal buffer size used by AVIO for reading. | 14 // Internal buffer size used by AVIO for reading. |
15 // TODO(dalecurtis): Experiment with this buffer size and measure impact on | 15 // TODO(dalecurtis): Experiment with this buffer size and measure impact on |
16 // performance. Currently we want to use 32kb to preserve existing behavior | 16 // performance. Currently we want to use 32kb to preserve existing behavior |
17 // with the previous URLProtocol based approach. | 17 // with the previous URLProtocol based approach. |
18 enum { kBufferSize = 32 * 1024 }; | 18 enum { kBufferSize = 32 * 1024 }; |
19 | 19 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
116 DISALLOW_COPY_AND_ASSIGN(FFmpegInitializer); | 116 DISALLOW_COPY_AND_ASSIGN(FFmpegInitializer); |
117 }; | 117 }; |
118 | 118 |
119 void FFmpegGlue::InitializeFFmpeg() { | 119 void FFmpegGlue::InitializeFFmpeg() { |
120 static base::LazyInstance<FFmpegInitializer>::Leaky li = | 120 static base::LazyInstance<FFmpegInitializer>::Leaky li = |
121 LAZY_INSTANCE_INITIALIZER; | 121 LAZY_INSTANCE_INITIALIZER; |
122 CHECK(li.Get().initialized()); | 122 CHECK(li.Get().initialized()); |
123 } | 123 } |
124 | 124 |
125 FFmpegGlue::FFmpegGlue(FFmpegURLProtocol* protocol) | 125 FFmpegGlue::FFmpegGlue(FFmpegURLProtocol* protocol) |
126 : open_called_(false) { | 126 : open_called_(false), |
127 protocol_(protocol) { | |
127 InitializeFFmpeg(); | 128 InitializeFFmpeg(); |
128 | 129 |
129 // Initialize an AVIOContext using our custom read and seek operations. Don't | 130 // 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 | 131 // keep pointers to the buffer since FFmpeg may reallocate it on the fly. It |
131 // will be cleaned up | 132 // will be cleaned up |
132 format_context_ = avformat_alloc_context(); | 133 format_context_ = avformat_alloc_context(); |
133 avio_context_.reset(avio_alloc_context( | 134 avio_context_.reset(avio_alloc_context( |
134 static_cast<unsigned char*>(av_malloc(kBufferSize)), kBufferSize, 0, | 135 static_cast<unsigned char*>(av_malloc(kBufferSize)), kBufferSize, 0, |
135 protocol, &AVIOReadOperation, NULL, &AVIOSeekOperation)); | 136 protocol, &AVIOReadOperation, NULL, &AVIOSeekOperation)); |
136 | 137 |
(...skipping 13 matching lines...) Expand all Loading... | |
150 | 151 |
151 bool FFmpegGlue::OpenContext() { | 152 bool FFmpegGlue::OpenContext() { |
152 DCHECK(!open_called_) << "OpenContext() should't be called twice."; | 153 DCHECK(!open_called_) << "OpenContext() should't be called twice."; |
153 | 154 |
154 // If avformat_open_input() is called we have to take a slightly different | 155 // If avformat_open_input() is called we have to take a slightly different |
155 // destruction path to avoid double frees. | 156 // destruction path to avoid double frees. |
156 open_called_ = true; | 157 open_called_ = true; |
157 | 158 |
158 // By passing NULL for the filename (second parameter) we are telling FFmpeg | 159 // By passing NULL for the filename (second parameter) we are telling FFmpeg |
159 // to use the AVIO context we setup from the AVFormatContext structure. | 160 // to use the AVIO context we setup from the AVFormatContext structure. |
160 return avformat_open_input(&format_context_, NULL, NULL, NULL) == 0; | 161 int result = avformat_open_input(&format_context_, NULL, NULL, NULL); |
162 ContainerNames container; | |
163 if (result == 0) { | |
164 // FFmpeg decoded the container, so report what it found | |
165 container.LogContainer(format_context_->iformat->name); | |
166 return true; | |
167 } | |
168 | |
169 // Stream was not recognized by FFmpeg. Re-read part of the stream and see if | |
170 // the stream can be recognized. Unfortunately FFmpeg has released the buffer | |
171 // where it read the header, so we need to read it again. We leave the stream | |
172 // position unchanged. | |
173 int64 pos; | |
174 uint8 buf[8192]; | |
xhwang
2013/04/29 21:00:25
This code path only happens in the failure case. B
jrummell
2013/04/30 21:36:50
Done.
| |
175 | |
176 protocol_->GetPosition(&pos); | |
177 protocol_->SetPosition(0); | |
178 int numRead = protocol_->Read(sizeof(buf), buf); | |
179 protocol_->SetPosition(pos); | |
180 container.LogContainer(buf, numRead); | |
181 return false; | |
161 } | 182 } |
162 | 183 |
163 FFmpegGlue::~FFmpegGlue() { | 184 FFmpegGlue::~FFmpegGlue() { |
164 // In the event of avformat_open_input() failure, FFmpeg may sometimes free | 185 // 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 | 186 // our AVFormatContext behind the scenes, but leave the buffer alive. It will |
166 // helpfully set |format_context_| to NULL in this case. | 187 // helpfully set |format_context_| to NULL in this case. |
167 if (!format_context_) { | 188 if (!format_context_) { |
168 av_free(avio_context_->buffer); | 189 av_free(avio_context_->buffer); |
169 return; | 190 return; |
170 } | 191 } |
(...skipping 24 matching lines...) Expand all Loading... | |
195 avcodec_close(stream->codec); | 216 avcodec_close(stream->codec); |
196 } | 217 } |
197 } | 218 } |
198 } | 219 } |
199 | 220 |
200 avformat_close_input(&format_context_); | 221 avformat_close_input(&format_context_); |
201 av_free(avio_context_->buffer); | 222 av_free(avio_context_->buffer); |
202 } | 223 } |
203 | 224 |
204 } // namespace media | 225 } // namespace media |
OLD | NEW |