| 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" | |
| 8 #include "base/logging.h" | 7 #include "base/logging.h" |
| 9 #include "base/macros.h" | 8 #include "base/macros.h" |
| 10 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 11 #include "base/synchronization/lock.h" | 10 #include "base/synchronization/lock.h" |
| 12 #include "media/base/container_names.h" | 11 #include "media/base/container_names.h" |
| 13 #include "media/ffmpeg/ffmpeg_common.h" | 12 #include "media/ffmpeg/ffmpeg_common.h" |
| 14 | 13 |
| 15 namespace media { | 14 namespace media { |
| 16 | 15 |
| 17 // Internal buffer size used by AVIO for reading. | 16 // Internal buffer size used by AVIO for reading. |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 return 0; | 79 return 0; |
| 81 | 80 |
| 82 case AV_LOCK_DESTROY: | 81 case AV_LOCK_DESTROY: |
| 83 delete static_cast<base::Lock*>(*lock); | 82 delete static_cast<base::Lock*>(*lock); |
| 84 *lock = nullptr; | 83 *lock = nullptr; |
| 85 return 0; | 84 return 0; |
| 86 } | 85 } |
| 87 return 1; | 86 return 1; |
| 88 } | 87 } |
| 89 | 88 |
| 90 // FFmpeg must only be initialized once, so use a LazyInstance to ensure this. | 89 void FFmpegGlue::InitializeFFmpeg() { |
| 91 class FFmpegInitializer { | 90 static bool initialized = []() { |
| 92 public: | |
| 93 bool initialized() { return initialized_; } | |
| 94 | |
| 95 private: | |
| 96 friend struct base::DefaultLazyInstanceTraits<FFmpegInitializer>; | |
| 97 | |
| 98 FFmpegInitializer() | |
| 99 : initialized_(false) { | |
| 100 // Register our protocol glue code with FFmpeg. | 91 // Register our protocol glue code with FFmpeg. |
| 101 if (av_lockmgr_register(&LockManagerOperation) != 0) | 92 if (av_lockmgr_register(&LockManagerOperation) != 0) |
| 102 return; | 93 return false; |
| 103 | 94 |
| 104 // Now register the rest of FFmpeg. | 95 // Now register the rest of FFmpeg. |
| 105 av_register_all(); | 96 av_register_all(); |
| 97 return true; |
| 98 }(); |
| 106 | 99 |
| 107 initialized_ = true; | 100 CHECK(initialized); |
| 108 } | |
| 109 | |
| 110 ~FFmpegInitializer() { | |
| 111 NOTREACHED() << "FFmpegInitializer should be leaky!"; | |
| 112 } | |
| 113 | |
| 114 bool initialized_; | |
| 115 | |
| 116 DISALLOW_COPY_AND_ASSIGN(FFmpegInitializer); | |
| 117 }; | |
| 118 | |
| 119 static base::LazyInstance<FFmpegInitializer>::Leaky g_lazy_instance = | |
| 120 LAZY_INSTANCE_INITIALIZER; | |
| 121 void FFmpegGlue::InitializeFFmpeg() { | |
| 122 // Get() will invoke the FFmpegInitializer constructor once. | |
| 123 CHECK(g_lazy_instance.Get().initialized()); | |
| 124 } | 101 } |
| 125 | 102 |
| 126 FFmpegGlue::FFmpegGlue(FFmpegURLProtocol* protocol) | 103 FFmpegGlue::FFmpegGlue(FFmpegURLProtocol* protocol) |
| 127 : open_called_(false) { | 104 : open_called_(false) { |
| 128 InitializeFFmpeg(); | 105 InitializeFFmpeg(); |
| 129 | 106 |
| 130 // Initialize an AVIOContext using our custom read and seek operations. Don't | 107 // Initialize an AVIOContext using our custom read and seek operations. Don't |
| 131 // keep pointers to the buffer since FFmpeg may reallocate it on the fly. It | 108 // keep pointers to the buffer since FFmpeg may reallocate it on the fly. It |
| 132 // will be cleaned up | 109 // will be cleaned up |
| 133 format_context_ = avformat_alloc_context(); | 110 format_context_ = avformat_alloc_context(); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 avformat_free_context(format_context_); | 173 avformat_free_context(format_context_); |
| 197 av_free(avio_context_->buffer); | 174 av_free(avio_context_->buffer); |
| 198 return; | 175 return; |
| 199 } | 176 } |
| 200 | 177 |
| 201 avformat_close_input(&format_context_); | 178 avformat_close_input(&format_context_); |
| 202 av_free(avio_context_->buffer); | 179 av_free(avio_context_->buffer); |
| 203 } | 180 } |
| 204 | 181 |
| 205 } // namespace media | 182 } // namespace media |
| OLD | NEW |