| 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/metrics/sparse_histogram.h" |
| 10 #include "base/synchronization/lock.h" | 10 #include "base/synchronization/lock.h" |
| 11 #include "media/base/container_names.h" | 11 #include "media/base/container_names.h" |
| 12 #include "media/ffmpeg/ffmpeg_common.h" | 12 #include "media/ffmpeg/ffmpeg_common.h" |
| 13 | 13 |
| 14 namespace media { | 14 namespace media { |
| 15 | 15 |
| 16 // Internal buffer size used by AVIO for reading. | 16 // Internal buffer size used by AVIO for reading. |
| 17 // TODO(dalecurtis): Experiment with this buffer size and measure impact on | 17 // TODO(dalecurtis): Experiment with this buffer size and measure impact on |
| 18 // performance. Currently we want to use 32kb to preserve existing behavior | 18 // performance. Currently we want to use 32kb to preserve existing behavior |
| 19 // with the previous URLProtocol based approach. | 19 // with the previous URLProtocol based approach. |
| 20 enum { kBufferSize = 32 * 1024 }; | 20 enum { kBufferSize = 32 * 1024 }; |
| 21 | 21 |
| 22 static int AVIOReadOperation(void* opaque, uint8_t* buf, int buf_size) { | 22 static int AVIOReadOperation(void* opaque, uint8_t* buf, int buf_size) { |
| 23 FFmpegURLProtocol* protocol = reinterpret_cast<FFmpegURLProtocol*>(opaque); | 23 FFmpegURLProtocol* protocol = reinterpret_cast<FFmpegURLProtocol*>(opaque); |
| 24 int result = protocol->Read(buf_size, buf); | 24 int result = protocol->Read(buf_size, buf); |
| 25 if (result < 0) | 25 if (result < 0) |
| 26 result = AVERROR(EIO); | 26 result = AVERROR(EIO); |
| 27 return result; | 27 return result; |
| 28 } | 28 } |
| 29 | 29 |
| 30 static int64 AVIOSeekOperation(void* opaque, int64 offset, int whence) { | 30 static int64_t AVIOSeekOperation(void* opaque, int64_t offset, int whence) { |
| 31 FFmpegURLProtocol* protocol = reinterpret_cast<FFmpegURLProtocol*>(opaque); | 31 FFmpegURLProtocol* protocol = reinterpret_cast<FFmpegURLProtocol*>(opaque); |
| 32 int64 new_offset = AVERROR(EIO); | 32 int64_t new_offset = AVERROR(EIO); |
| 33 switch (whence) { | 33 switch (whence) { |
| 34 case SEEK_SET: | 34 case SEEK_SET: |
| 35 if (protocol->SetPosition(offset)) | 35 if (protocol->SetPosition(offset)) |
| 36 protocol->GetPosition(&new_offset); | 36 protocol->GetPosition(&new_offset); |
| 37 break; | 37 break; |
| 38 | 38 |
| 39 case SEEK_CUR: | 39 case SEEK_CUR: |
| 40 int64 pos; | 40 int64_t pos; |
| 41 if (!protocol->GetPosition(&pos)) | 41 if (!protocol->GetPosition(&pos)) |
| 42 break; | 42 break; |
| 43 if (protocol->SetPosition(pos + offset)) | 43 if (protocol->SetPosition(pos + offset)) |
| 44 protocol->GetPosition(&new_offset); | 44 protocol->GetPosition(&new_offset); |
| 45 break; | 45 break; |
| 46 | 46 |
| 47 case SEEK_END: | 47 case SEEK_END: |
| 48 int64 size; | 48 int64_t size; |
| 49 if (!protocol->GetSize(&size)) | 49 if (!protocol->GetSize(&size)) |
| 50 break; | 50 break; |
| 51 if (protocol->SetPosition(size + offset)) | 51 if (protocol->SetPosition(size + offset)) |
| 52 protocol->GetPosition(&new_offset); | 52 protocol->GetPosition(&new_offset); |
| 53 break; | 53 break; |
| 54 | 54 |
| 55 case AVSEEK_SIZE: | 55 case AVSEEK_SIZE: |
| 56 protocol->GetSize(&new_offset); | 56 protocol->GetSize(&new_offset); |
| 57 break; | 57 break; |
| 58 | 58 |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 | 154 |
| 155 bool FFmpegGlue::OpenContext() { | 155 bool FFmpegGlue::OpenContext() { |
| 156 DCHECK(!open_called_) << "OpenContext() shouldn't be called twice."; | 156 DCHECK(!open_called_) << "OpenContext() shouldn't be called twice."; |
| 157 | 157 |
| 158 // If avformat_open_input() is called we have to take a slightly different | 158 // If avformat_open_input() is called we have to take a slightly different |
| 159 // destruction path to avoid double frees. | 159 // destruction path to avoid double frees. |
| 160 open_called_ = true; | 160 open_called_ = true; |
| 161 | 161 |
| 162 // Attempt to recognize the container by looking at the first few bytes of the | 162 // Attempt to recognize the container by looking at the first few bytes of the |
| 163 // stream. The stream position is left unchanged. | 163 // stream. The stream position is left unchanged. |
| 164 scoped_ptr<std::vector<uint8> > buffer(new std::vector<uint8>(8192)); | 164 scoped_ptr<std::vector<uint8_t>> buffer(new std::vector<uint8_t>(8192)); |
| 165 | 165 |
| 166 int64 pos = AVIOSeekOperation(avio_context_.get()->opaque, 0, SEEK_CUR); | 166 int64_t pos = AVIOSeekOperation(avio_context_.get()->opaque, 0, SEEK_CUR); |
| 167 AVIOSeekOperation(avio_context_.get()->opaque, 0, SEEK_SET); | 167 AVIOSeekOperation(avio_context_.get()->opaque, 0, SEEK_SET); |
| 168 int numRead = AVIOReadOperation( | 168 int numRead = AVIOReadOperation( |
| 169 avio_context_.get()->opaque, buffer.get()->data(), buffer.get()->size()); | 169 avio_context_.get()->opaque, buffer.get()->data(), buffer.get()->size()); |
| 170 AVIOSeekOperation(avio_context_.get()->opaque, pos, SEEK_SET); | 170 AVIOSeekOperation(avio_context_.get()->opaque, pos, SEEK_SET); |
| 171 if (numRead > 0) { | 171 if (numRead > 0) { |
| 172 // < 0 means Read failed | 172 // < 0 means Read failed |
| 173 container_names::MediaContainerName container = | 173 container_names::MediaContainerName container = |
| 174 container_names::DetermineContainer(buffer.get()->data(), numRead); | 174 container_names::DetermineContainer(buffer.get()->data(), numRead); |
| 175 UMA_HISTOGRAM_SPARSE_SLOWLY("Media.DetectedContainer", container); | 175 UMA_HISTOGRAM_SPARSE_SLOWLY("Media.DetectedContainer", container); |
| 176 } | 176 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 avcodec_close(stream->codec); | 215 avcodec_close(stream->codec); |
| 216 } | 216 } |
| 217 } | 217 } |
| 218 } | 218 } |
| 219 | 219 |
| 220 avformat_close_input(&format_context_); | 220 avformat_close_input(&format_context_); |
| 221 av_free(avio_context_->buffer); | 221 av_free(avio_context_->buffer); |
| 222 } | 222 } |
| 223 | 223 |
| 224 } // namespace media | 224 } // namespace media |
| OLD | NEW |