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

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

Issue 11368064: Handle FFmpeg initialization in a lazy instance. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Leaky! Created 8 years, 1 month 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
7 #include "base/logging.h" 8 #include "base/logging.h"
8 #include "base/synchronization/lock.h" 9 #include "base/synchronization/lock.h"
9 #include "media/ffmpeg/ffmpeg_common.h" 10 #include "media/ffmpeg/ffmpeg_common.h"
10 11
11 namespace media { 12 namespace media {
12 13
13 // Internal buffer size used by AVIO for reading. 14 // Internal buffer size used by AVIO for reading.
14 // TODO(dalecurtis): Experiment with this buffer size and measure impact on 15 // TODO(dalecurtis): Experiment with this buffer size and measure impact on
15 // performance. Currently we want to use 32kb to preserve existing behavior 16 // performance. Currently we want to use 32kb to preserve existing behavior
16 // with the previous URLProtocol based approach. 17 // with the previous URLProtocol based approach.
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 return 0; 77 return 0;
77 78
78 case AV_LOCK_DESTROY: 79 case AV_LOCK_DESTROY:
79 delete static_cast<base::Lock*>(*lock); 80 delete static_cast<base::Lock*>(*lock);
80 *lock = NULL; 81 *lock = NULL;
81 return 0; 82 return 0;
82 } 83 }
83 return 1; 84 return 1;
84 } 85 }
85 86
86 static bool InitializeFFmpegInternal() { 87 // FFmpeg must only be initialized once, so use a LazyInstance to ensure this.
87 // Before doing anything disable logging as it interferes with layout tests. 88 class FFmpegInitializer {
88 av_log_set_level(AV_LOG_QUIET); 89 public:
90 bool initialized() { return initialized_; }
89 91
90 // Register our protocol glue code with FFmpeg. 92 private:
91 if (av_lockmgr_register(&LockManagerOperation) != 0) 93 friend struct base::DefaultLazyInstanceTraits<FFmpegInitializer>;
92 return false;
93 94
94 // Now register the rest of FFmpeg. 95 FFmpegInitializer()
95 av_register_all(); 96 : initialized_(false) {
96 return true; 97 // Before doing anything disable logging as it interferes with layout tests.
97 } 98 av_log_set_level(AV_LOG_QUIET);
99
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 };
98 118
99 void FFmpegGlue::InitializeFFmpeg() { 119 void FFmpegGlue::InitializeFFmpeg() {
100 // FFmpeg only needs to be initialized once. 120 static base::LazyInstance<FFmpegInitializer>::Leaky li =
101 static const bool kStatus = InitializeFFmpegInternal(); 121 LAZY_INSTANCE_INITIALIZER;
102 CHECK(kStatus); 122 CHECK(li.Get().initialized());
103 } 123 }
104 124
105 FFmpegGlue::FFmpegGlue(FFmpegURLProtocol* protocol) 125 FFmpegGlue::FFmpegGlue(FFmpegURLProtocol* protocol)
106 : open_called_(false) { 126 : open_called_(false) {
107 InitializeFFmpeg(); 127 InitializeFFmpeg();
108 128
109 // Initialize an AVIOContext using our custom read and seek operations. Don't 129 // Initialize an AVIOContext using our custom read and seek operations. Don't
110 // keep pointers to the buffer since FFmpeg may reallocate it on the fly. It 130 // keep pointers to the buffer since FFmpeg may reallocate it on the fly. It
111 // will be cleaned up 131 // will be cleaned up
112 format_context_ = avformat_alloc_context(); 132 format_context_ = avformat_alloc_context();
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 avcodec_close(stream->codec); 195 avcodec_close(stream->codec);
176 } 196 }
177 } 197 }
178 } 198 }
179 199
180 avformat_close_input(&format_context_); 200 avformat_close_input(&format_context_);
181 av_free(avio_context_->buffer); 201 av_free(avio_context_->buffer);
182 } 202 }
183 203
184 } // namespace media 204 } // namespace media
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698