Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(175)

Side by Side Diff: media/filters/ffmpeg_glue.cc

Issue 2668813002: Remove LazyInstance usage from media/ (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 // FFmpeg must only be initialized once per process.
91 class FFmpegInitializer { 90 static bool InitializeInternal() {
92 public: 91 // Register our protocol glue code with FFmpeg.
93 bool initialized() { return initialized_; } 92 if (av_lockmgr_register(&LockManagerOperation) != 0)
93 return false;
94 94
95 private: 95 // Now register the rest of FFmpeg.
96 friend struct base::DefaultLazyInstanceTraits<FFmpegInitializer>; 96 av_register_all();
97 return true;
98 }
97 99
98 FFmpegInitializer()
99 : initialized_(false) {
100 // Register our protocol glue code with FFmpeg.
101 if (av_lockmgr_register(&LockManagerOperation) != 0)
102 return;
103
104 // Now register the rest of FFmpeg.
105 av_register_all();
106
107 initialized_ = true;
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() { 100 void FFmpegGlue::InitializeFFmpeg() {
122 // Get() will invoke the FFmpegInitializer constructor once. 101 static bool initialized = InitializeInternal();
scottmg 2017/01/31 21:10:34 Could consider inlining the Internal() function he
Mark Mentovai 2017/01/31 21:33:56 You can eliminate InitializeInternal() by writing
DaleCurtis 2017/01/31 22:04:33 Thanks, done!
123 CHECK(g_lazy_instance.Get().initialized()); 102 CHECK(initialized);
124 } 103 }
125 104
126 FFmpegGlue::FFmpegGlue(FFmpegURLProtocol* protocol) 105 FFmpegGlue::FFmpegGlue(FFmpegURLProtocol* protocol)
127 : open_called_(false) { 106 : open_called_(false) {
128 InitializeFFmpeg(); 107 InitializeFFmpeg();
129 108
130 // Initialize an AVIOContext using our custom read and seek operations. Don't 109 // 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 110 // keep pointers to the buffer since FFmpeg may reallocate it on the fly. It
132 // will be cleaned up 111 // will be cleaned up
133 format_context_ = avformat_alloc_context(); 112 format_context_ = avformat_alloc_context();
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 avformat_free_context(format_context_); 175 avformat_free_context(format_context_);
197 av_free(avio_context_->buffer); 176 av_free(avio_context_->buffer);
198 return; 177 return;
199 } 178 }
200 179
201 avformat_close_input(&format_context_); 180 avformat_close_input(&format_context_);
202 av_free(avio_context_->buffer); 181 av_free(avio_context_->buffer);
203 } 182 }
204 183
205 } // namespace media 184 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698