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/synchronization/lock.h" | 9 #include "base/synchronization/lock.h" |
| 10 #include "media/base/container_names.h" |
10 #include "media/ffmpeg/ffmpeg_common.h" | 11 #include "media/ffmpeg/ffmpeg_common.h" |
11 | 12 |
12 namespace media { | 13 namespace media { |
13 | 14 |
14 // Internal buffer size used by AVIO for reading. | 15 // Internal buffer size used by AVIO for reading. |
15 // TODO(dalecurtis): Experiment with this buffer size and measure impact on | 16 // TODO(dalecurtis): Experiment with this buffer size and measure impact on |
16 // performance. Currently we want to use 32kb to preserve existing behavior | 17 // performance. Currently we want to use 32kb to preserve existing behavior |
17 // with the previous URLProtocol based approach. | 18 // with the previous URLProtocol based approach. |
18 enum { kBufferSize = 32 * 1024 }; | 19 enum { kBufferSize = 32 * 1024 }; |
19 | 20 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 DISALLOW_COPY_AND_ASSIGN(FFmpegInitializer); | 117 DISALLOW_COPY_AND_ASSIGN(FFmpegInitializer); |
117 }; | 118 }; |
118 | 119 |
119 void FFmpegGlue::InitializeFFmpeg() { | 120 void FFmpegGlue::InitializeFFmpeg() { |
120 static base::LazyInstance<FFmpegInitializer>::Leaky li = | 121 static base::LazyInstance<FFmpegInitializer>::Leaky li = |
121 LAZY_INSTANCE_INITIALIZER; | 122 LAZY_INSTANCE_INITIALIZER; |
122 CHECK(li.Get().initialized()); | 123 CHECK(li.Get().initialized()); |
123 } | 124 } |
124 | 125 |
125 FFmpegGlue::FFmpegGlue(FFmpegURLProtocol* protocol) | 126 FFmpegGlue::FFmpegGlue(FFmpegURLProtocol* protocol) |
126 : open_called_(false) { | 127 : open_called_(false), |
| 128 protocol_(protocol) { |
127 InitializeFFmpeg(); | 129 InitializeFFmpeg(); |
128 | 130 |
129 // Initialize an AVIOContext using our custom read and seek operations. Don't | 131 // 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 | 132 // keep pointers to the buffer since FFmpeg may reallocate it on the fly. It |
131 // will be cleaned up | 133 // will be cleaned up |
132 format_context_ = avformat_alloc_context(); | 134 format_context_ = avformat_alloc_context(); |
133 avio_context_.reset(avio_alloc_context( | 135 avio_context_.reset(avio_alloc_context( |
134 static_cast<unsigned char*>(av_malloc(kBufferSize)), kBufferSize, 0, | 136 static_cast<unsigned char*>(av_malloc(kBufferSize)), kBufferSize, 0, |
135 protocol, &AVIOReadOperation, NULL, &AVIOSeekOperation)); | 137 protocol, &AVIOReadOperation, NULL, &AVIOSeekOperation)); |
136 | 138 |
(...skipping 13 matching lines...) Expand all Loading... |
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 |
158 // By passing NULL for the filename (second parameter) we are telling FFmpeg | 160 // By passing NULL for the filename (second parameter) we are telling FFmpeg |
159 // to use the AVIO context we setup from the AVFormatContext structure. | 161 // to use the AVIO context we setup from the AVFormatContext structure. |
160 return avformat_open_input(&format_context_, NULL, NULL, NULL) == 0; | 162 int result = avformat_open_input(&format_context_, NULL, NULL, NULL); |
| 163 if (result == 0) { |
| 164 // FFmpeg decoded the container, so report what it found |
| 165 ContainerNames::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 scoped_ptr<uint8[]> buffer(new uint8[8192]); |
| 175 |
| 176 protocol_->GetPosition(&pos); |
| 177 protocol_->SetPosition(0); |
| 178 int numRead = protocol_->Read(8192, buffer.get()); |
| 179 protocol_->SetPosition(pos); |
| 180 ContainerNames::LogContainer(buffer.get(), 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 |