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

Side by Side Diff: media/base/pipeline_impl.h

Issue 160213: Merge 21464 - Merged PipelineInternal into PipelineImpl, eliminating a ton of... (Closed) Base URL: svn://chrome-svn/chrome/branches/195/src/
Patch Set: Created 11 years, 4 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | media/base/pipeline_impl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Modified: svn:mergeinfo
Merged /trunk/src/media/base/pipeline_impl.h:r21464
OLDNEW
1 // Copyright (c) 2008-2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2008-2009 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 // Implementation of Pipeline. 5 // Implementation of Pipeline.
6 6
7 #ifndef MEDIA_BASE_PIPELINE_IMPL_H_ 7 #ifndef MEDIA_BASE_PIPELINE_IMPL_H_
8 #define MEDIA_BASE_PIPELINE_IMPL_H_ 8 #define MEDIA_BASE_PIPELINE_IMPL_H_
9 9
10 #include <string> 10 #include <string>
11 #include <vector> 11 #include <vector>
12 #include <set> 12 #include <set>
13 13
14 #include "base/message_loop.h" 14 #include "base/message_loop.h"
15 #include "base/ref_counted.h" 15 #include "base/ref_counted.h"
16 #include "base/thread.h" 16 #include "base/thread.h"
17 #include "base/time.h" 17 #include "base/time.h"
18 #include "media/base/filter_host.h" 18 #include "media/base/filter_host.h"
19 #include "media/base/pipeline.h" 19 #include "media/base/pipeline.h"
20 20
21 namespace media { 21 namespace media {
22 22
23 class PipelineInternal;
24 23
25 // Class which implements the Media::Pipeline contract. The majority of the 24 // PipelineImpl runs the media pipeline. Filters are created and called on the
26 // actual code for this object lives in the PipelineInternal class, which is 25 // message loop injected into this object. PipelineImpl works like a state
27 // responsible for actually building and running the pipeline. This object 26 // machine to perform asynchronous initialization. Initialization is done in
28 // is basically a simple container for state information, and is responsible 27 // multiple passes by InitializeTask(). In each pass a different filter is
29 // for creating and communicating with the PipelineInternal object. 28 // created and chained with a previously created filter.
30 class PipelineImpl : public Pipeline { 29 //
30 // Here's a state diagram that describes the lifetime of this object.
31 //
32 // [ *Created ] -> [ InitDataSource ] -> [ InitDemuxer ] ->
33 // [ InitAudioDecoder ] -> [ InitAudioRenderer ] ->
34 // [ InitVideoDecoder ] -> [ InitVideoRenderer ] -> [ Started ]
35 // | | |
36 // .-> [ Error ] .-> [ Stopped ] <-.
37 //
38 // Initialization is a series of state transitions from "Created" to
39 // "Started". If any error happens during initialization, this object will
40 // transition to the "Error" state from any state. If Stop() is called during
41 // initialization, this object will transition to "Stopped" state.
42 class PipelineImpl : public Pipeline, public FilterHost {
31 public: 43 public:
32 PipelineImpl(MessageLoop* message_loop); 44 explicit PipelineImpl(MessageLoop* message_loop);
33 45
34 // Pipeline implementation. 46 // Pipeline implementation.
35 virtual bool Start(FilterFactory* filter_factory, 47 virtual bool Start(FilterFactory* filter_factory,
36 const std::string& uri, 48 const std::string& uri,
37 PipelineCallback* start_callback); 49 PipelineCallback* start_callback);
38 virtual void Stop(PipelineCallback* stop_callback); 50 virtual void Stop(PipelineCallback* stop_callback);
39 virtual void Seek(base::TimeDelta time, PipelineCallback* seek_callback); 51 virtual void Seek(base::TimeDelta time, PipelineCallback* seek_callback);
40 virtual bool IsRunning() const; 52 virtual bool IsRunning() const;
41 virtual bool IsInitialized() const; 53 virtual bool IsInitialized() const;
42 virtual bool IsRendered(const std::string& major_mime_type) const; 54 virtual bool IsRendered(const std::string& major_mime_type) const;
43 virtual float GetPlaybackRate() const; 55 virtual float GetPlaybackRate() const;
44 virtual void SetPlaybackRate(float playback_rate); 56 virtual void SetPlaybackRate(float playback_rate);
45 virtual float GetVolume() const; 57 virtual float GetVolume() const;
46 virtual void SetVolume(float volume); 58 virtual void SetVolume(float volume);
47 virtual base::TimeDelta GetCurrentTime() const; 59 virtual base::TimeDelta GetCurrentTime() const;
48 virtual base::TimeDelta GetBufferedTime() const; 60 virtual base::TimeDelta GetBufferedTime() const;
49 virtual base::TimeDelta GetDuration() const; 61 virtual base::TimeDelta GetDuration() const;
50 virtual int64 GetBufferedBytes() const; 62 virtual int64 GetBufferedBytes() const;
51 virtual int64 GetTotalBytes() const; 63 virtual int64 GetTotalBytes() const;
52 virtual void GetVideoSize(size_t* width_out, size_t* height_out) const; 64 virtual void GetVideoSize(size_t* width_out, size_t* height_out) const;
53 virtual PipelineError GetError() const; 65 virtual PipelineError GetError() const;
54 66
55 private: 67 private:
56 friend class PipelineInternal;
57 virtual ~PipelineImpl(); 68 virtual ~PipelineImpl();
58 69
59 // Reset the state of the pipeline object to the initial state. This method 70 // Reset the state of the pipeline object to the initial state. This method
60 // is used by the constructor, and the Stop method. 71 // is used by the constructor, and the Stop() method.
61 void ResetState(); 72 void ResetState();
62 73
63 // Used internally to make sure that the thread is in a state that is 74 // Simple method used to make sure the pipeline is running normally.
64 // acceptable to post a task to. It must exist, be initialized, and there 75 bool IsPipelineOk();
65 // must not be an error.
66 bool IsPipelineOk() const;
67 76
68 // Methods called by |pipeline_internal_| to update global pipeline data. 77 // Helper method to tell whether we are in the state of initializing.
69 // 78 bool IsPipelineInitializing();
70 // Although this is the exact same as the FilterHost interface, we need to
71 // let |pipeline_internal_| receive the call first so it can post tasks as
72 // necessary.
73 void SetError(PipelineError error);
74 base::TimeDelta GetTime() const;
75 void SetTime(base::TimeDelta time);
76 void SetDuration(base::TimeDelta duration);
77 void SetBufferedTime(base::TimeDelta buffered_time);
78 void SetTotalBytes(int64 total_bytes);
79 void SetBufferedBytes(int64 buffered_bytes);
80 void SetVideoSize(size_t width, size_t height);
81
82 // Method called by the |pipeline_internal_| to insert a mime type into
83 // the |rendered_mime_types_| set.
84 void InsertRenderedMimeType(const std::string& major_mime_type);
85
86 // Message loop used to execute pipeline tasks.
87 MessageLoop* message_loop_;
88
89 // Holds a ref counted reference to the PipelineInternal object associated
90 // with this pipeline. Prior to the call to the Start() method, this member
91 // will be NULL, since we are not running.
92 scoped_refptr<PipelineInternal> pipeline_internal_;
93
94 // After calling Start, if all of the required filters are created and
95 // initialized, this member will be set to true by the pipeline thread.
96 bool initialized_;
97
98 // Duration of the media in microseconds. Set by filters.
99 base::TimeDelta duration_;
100
101 // Amount of available buffered data in microseconds. Set by filters.
102 base::TimeDelta buffered_time_;
103
104 // Amount of available buffered data. Set by filters.
105 int64 buffered_bytes_;
106
107 // Total size of the media. Set by filters.
108 int64 total_bytes_;
109
110 // Lock used to serialize access for getter/setter methods.
111 mutable Lock lock_;
112
113 // Video width and height. Set by filters.
114 size_t video_width_;
115 size_t video_height_;
116
117 // Current volume level (from 0.0f to 1.0f). This value is set immediately
118 // via SetVolume() and a task is dispatched on the message loop to notify the
119 // filters.
120 float volume_;
121
122 // Current playback rate (>= 0.0f). This value is set immediately via
123 // SetPlaybackRate() and a task is dispatched on the message loop to notify
124 // the filters.
125 float playback_rate_;
126
127 // Current playback time. Set by filters.
128 base::TimeDelta time_;
129
130 // Status of the pipeline. Initialized to PIPELINE_OK which indicates that
131 // the pipeline is operating correctly. Any other value indicates that the
132 // pipeline is stopped or is stopping. Clients can call the Stop method to
133 // reset the pipeline state, and restore this to PIPELINE_OK.
134 PipelineError error_;
135
136 // Vector of major mime types that have been rendered by this pipeline.
137 typedef std::set<std::string> RenderedMimeTypesSet;
138 RenderedMimeTypesSet rendered_mime_types_;
139
140 DISALLOW_COPY_AND_ASSIGN(PipelineImpl);
141 };
142
143
144 // PipelineInternal contains most of the logic involved with running the
145 // media pipeline. Filters are created and called on the message loop injected
146 // into this object. PipelineInternal works like a state machine to perform
147 // asynchronous initialization. Initialization is done in multiple passes by
148 // InitializeTask(). In each pass a different filter is created and chained with
149 // a previously created filter.
150 //
151 // Here's a state diagram that describes the lifetime of this object.
152 //
153 // [ *Created ] -> [ InitDataSource ] -> [ InitDemuxer ] ->
154 // [ InitAudioDecoder ] -> [ InitAudioRenderer ] ->
155 // [ InitVideoDecoder ] -> [ InitVideoRenderer ] -> [ Started ]
156 // | | |
157 // .-> [ Error ] .-> [ Stopped ] <-.
158 //
159 // Initialization is a series of state transitions from "Created" to
160 // "Started". If any error happens during initialization, this object will
161 // transition to the "Error" state from any state. If Stop() is called during
162 // initialization, this object will transition to "Stopped" state.
163
164 class PipelineInternal : public FilterHost,
165 public base::RefCountedThreadSafe<PipelineInternal> {
166 public:
167 // Methods called by PipelineImpl object on the client's thread. These
168 // methods post a task to call a corresponding xxxTask() method on the
169 // message loop. For example, Seek posts a task to call SeekTask.
170 PipelineInternal(PipelineImpl* pipeline, MessageLoop* message_loop);
171
172 // After Start() is called, a task of StartTask() is posted on the pipeline
173 // thread to perform initialization. See StartTask() to learn more about
174 // initialization.
175 void Start(FilterFactory* filter_factory,
176 const std::string& url_media_source,
177 PipelineCallback* start_callback);
178 void Stop(PipelineCallback* stop_callback);
179 void Seek(base::TimeDelta time, PipelineCallback* seek_callback);
180
181 // Notifies that the client has changed the playback rate/volume.
182 void PlaybackRateChanged(float playback_rate);
183 void VolumeChanged(float volume);
184
185 // Returns true if the pipeline has fully initialized.
186 bool IsInitialized() { return state_ == kStarted; }
187
188 private:
189 // Only allow ourselves to be destroyed via ref-counting.
190 friend class base::RefCountedThreadSafe<PipelineInternal>;
191 virtual ~PipelineInternal();
192 79
193 // FilterHost implementation. 80 // FilterHost implementation.
194 virtual void SetError(PipelineError error); 81 virtual void SetError(PipelineError error);
195 virtual base::TimeDelta GetTime() const; 82 virtual base::TimeDelta GetTime() const;
196 virtual void SetTime(base::TimeDelta time); 83 virtual void SetTime(base::TimeDelta time);
197 virtual void SetDuration(base::TimeDelta duration); 84 virtual void SetDuration(base::TimeDelta duration);
198 virtual void SetBufferedTime(base::TimeDelta buffered_time); 85 virtual void SetBufferedTime(base::TimeDelta buffered_time);
199 virtual void SetTotalBytes(int64 total_bytes); 86 virtual void SetTotalBytes(int64 total_bytes);
200 virtual void SetBufferedBytes(int64 buffered_bytes); 87 virtual void SetBufferedBytes(int64 buffered_bytes);
201 virtual void SetVideoSize(size_t width, size_t height); 88 virtual void SetVideoSize(size_t width, size_t height);
202 89
203 enum State { 90 // Method called during initialization to insert a mime type into the
204 kCreated, 91 // |rendered_mime_types_| set.
205 kInitDataSource, 92 void InsertRenderedMimeType(const std::string& major_mime_type);
206 kInitDemuxer,
207 kInitAudioDecoder,
208 kInitAudioRenderer,
209 kInitVideoDecoder,
210 kInitVideoRenderer,
211 kStarted,
212 kStopped,
213 kError,
214 };
215 93
216 // Simple method used to make sure the pipeline is running normally. 94 // Method called during initialization to determine if we rendered anything.
217 bool IsPipelineOk() { return PIPELINE_OK == pipeline_->error_; } 95 bool HasRenderedMimeTypes() const;
218
219 // Helper method to tell whether we are in the state of initializing.
220 bool IsPipelineInitializing() {
221 return state_ == kInitDataSource ||
222 state_ == kInitDemuxer ||
223 state_ == kInitAudioDecoder ||
224 state_ == kInitAudioRenderer ||
225 state_ == kInitVideoDecoder ||
226 state_ == kInitVideoRenderer;
227 }
228 96
229 // Callback executed by filters upon completing initialization and seeking. 97 // Callback executed by filters upon completing initialization and seeking.
230 void OnFilterInitialize(); 98 void OnFilterInitialize();
231 void OnFilterSeek(); 99 void OnFilterSeek();
232 100
233 // The following "task" methods correspond to the public methods, but these 101 // The following "task" methods correspond to the public methods, but these
234 // methods are run as the result of posting a task to the PipelineInternal's 102 // methods are run as the result of posting a task to the PipelineInternal's
235 // message loop. 103 // message loop.
236 void StartTask(FilterFactory* filter_factory, 104 void StartTask(FilterFactory* filter_factory,
237 const std::string& url, 105 const std::string& url,
238 PipelineCallback* start_callback); 106 PipelineCallback* start_callback);
239 107
240 // InitializeTask() performs initialization in multiple passes. It is executed 108 // InitializeTask() performs initialization in multiple passes. It is executed
241 // as a result of calling Start() or InitializationComplete() that advances 109 // as a result of calling Start() or InitializationComplete() that advances
242 // initialization to the next state. It works as a hub of state transition for 110 // initialization to the next state. It works as a hub of state transition for
243 // initialization. 111 // initialization.
244 void InitializeTask(); 112 void InitializeTask();
245 113
246 // StopTask() and ErrorTask() are similar but serve different purposes: 114 // Stops and destroys all filters, placing the pipeline in the kStopped state
247 // - Both destroy the filter chain. 115 // and setting the error code to PIPELINE_STOPPED.
248 // - Both will execute |start_callback| if the pipeline was initializing.
249 // - StopTask() resets the pipeline to a fresh state, where as ErrorTask()
250 // leaves the pipeline as is for client inspection.
251 // - StopTask() can be scheduled by the client calling Stop(), where as
252 // ErrorTask() is scheduled as a result of a filter calling SetError().
253 void StopTask(PipelineCallback* stop_callback); 116 void StopTask(PipelineCallback* stop_callback);
254 void ErrorTask(PipelineError error);
255 117
256 // Carries out notifying filters that the playback rate/volume has changed, 118 // Carries out stopping and destroying all filters, placing the pipeline in
119 // the kError state.
120 void ErrorChangedTask(PipelineError error);
121
122 // Carries out notifying filters that the playback rate has changed.
257 void PlaybackRateChangedTask(float playback_rate); 123 void PlaybackRateChangedTask(float playback_rate);
124
125 // Carries out notifying filters that the volume has changed.
258 void VolumeChangedTask(float volume); 126 void VolumeChangedTask(float volume);
259 127
260 // Carries out notifying filters that we are seeking to a new timestamp. 128 // Carries out notifying filters that we are seeking to a new timestamp.
261 void SeekTask(base::TimeDelta time, PipelineCallback* seek_callback); 129 void SeekTask(base::TimeDelta time, PipelineCallback* seek_callback);
262 130
263 // Internal methods used in the implementation of the pipeline thread. All 131 // Internal methods used in the implementation of the pipeline thread. All
264 // of these methods are only called on the pipeline thread. 132 // of these methods are only called on the pipeline thread.
265 133
266 // The following template functions make use of the fact that media filter 134 // The following template functions make use of the fact that media filter
267 // derived interfaces are self-describing in the sense that they all contain 135 // derived interfaces are self-describing in the sense that they all contain
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 // Examine the list of existing filters to find one that supports the 187 // Examine the list of existing filters to find one that supports the
320 // specified Filter interface. If one exists, the |filter_out| will contain 188 // specified Filter interface. If one exists, the |filter_out| will contain
321 // the filter, |*filter_out| will be NULL. 189 // the filter, |*filter_out| will be NULL.
322 template <class Filter> 190 template <class Filter>
323 void GetFilter(scoped_refptr<Filter>* filter_out) const; 191 void GetFilter(scoped_refptr<Filter>* filter_out) const;
324 192
325 // Stops every filters, filter host and filter thread and releases all 193 // Stops every filters, filter host and filter thread and releases all
326 // references to them. 194 // references to them.
327 void DestroyFilters(); 195 void DestroyFilters();
328 196
329 // Pointer to the pipeline that owns this PipelineInternal.
330 PipelineImpl* pipeline_;
331
332 // Message loop used to execute pipeline tasks. 197 // Message loop used to execute pipeline tasks.
333 MessageLoop* message_loop_; 198 MessageLoop* message_loop_;
334 199
200 // Lock used to serialize access for the following data members.
201 mutable Lock lock_;
202
203 // Whether or not the pipeline is running.
204 bool running_;
205
206 // Duration of the media in microseconds. Set by filters.
207 base::TimeDelta duration_;
208
209 // Amount of available buffered data in microseconds. Set by filters.
210 base::TimeDelta buffered_time_;
211
212 // Amount of available buffered data. Set by filters.
213 int64 buffered_bytes_;
214
215 // Total size of the media. Set by filters.
216 int64 total_bytes_;
217
218 // Video width and height. Set by filters.
219 size_t video_width_;
220 size_t video_height_;
221
222 // Current volume level (from 0.0f to 1.0f). This value is set immediately
223 // via SetVolume() and a task is dispatched on the message loop to notify the
224 // filters.
225 float volume_;
226
227 // Current playback rate (>= 0.0f). This value is set immediately via
228 // SetPlaybackRate() and a task is dispatched on the message loop to notify
229 // the filters.
230 float playback_rate_;
231
232 // Current playback time. Set by filters.
233 base::TimeDelta time_;
234
235 // Status of the pipeline. Initialized to PIPELINE_OK which indicates that
236 // the pipeline is operating correctly. Any other value indicates that the
237 // pipeline is stopped or is stopping. Clients can call the Stop() method to
238 // reset the pipeline state, and restore this to PIPELINE_OK.
239 PipelineError error_;
240
241 // Vector of major mime types that have been rendered by this pipeline.
242 typedef std::set<std::string> RenderedMimeTypesSet;
243 RenderedMimeTypesSet rendered_mime_types_;
244
245 // The following data members are only accessed by tasks posted to
246 // |message_loop_|.
247
335 // Member that tracks the current state. 248 // Member that tracks the current state.
249 enum State {
250 kCreated,
251 kInitDataSource,
252 kInitDemuxer,
253 kInitAudioDecoder,
254 kInitAudioRenderer,
255 kInitVideoDecoder,
256 kInitVideoRenderer,
257 kStarted,
258 kStopped,
259 kError,
260 };
336 State state_; 261 State state_;
337 262
338 // Filter factory as passed in by Start(). 263 // Filter factory as passed in by Start().
339 scoped_refptr<FilterFactory> filter_factory_; 264 scoped_refptr<FilterFactory> filter_factory_;
340 265
341 // URL for the data source as passed in by Start(). 266 // URL for the data source as passed in by Start().
342 std::string url_; 267 std::string url_;
343 268
344 // Callbacks for various pipeline operations. 269 // Callbacks for various pipeline operations.
345 scoped_ptr<PipelineCallback> start_callback_; 270 scoped_ptr<PipelineCallback> start_callback_;
346 scoped_ptr<PipelineCallback> seek_callback_; 271 scoped_ptr<PipelineCallback> seek_callback_;
347 scoped_ptr<PipelineCallback> stop_callback_; 272 scoped_ptr<PipelineCallback> stop_callback_;
348 273
349 // Vector of our filters and map maintaining the relationship between the 274 // Vector of our filters and map maintaining the relationship between the
350 // FilterType and the filter itself. 275 // FilterType and the filter itself.
351 typedef std::vector<scoped_refptr<MediaFilter> > FilterVector; 276 typedef std::vector<scoped_refptr<MediaFilter> > FilterVector;
352 FilterVector filters_; 277 FilterVector filters_;
353 278
354 typedef std::map<FilterType, scoped_refptr<MediaFilter> > FilterTypeMap; 279 typedef std::map<FilterType, scoped_refptr<MediaFilter> > FilterTypeMap;
355 FilterTypeMap filter_types_; 280 FilterTypeMap filter_types_;
356 281
357 // Vector of threads owned by the pipeline and being used by filters. 282 // Vector of threads owned by the pipeline and being used by filters.
358 typedef std::vector<base::Thread*> FilterThreadVector; 283 typedef std::vector<base::Thread*> FilterThreadVector;
359 FilterThreadVector filter_threads_; 284 FilterThreadVector filter_threads_;
360 285
361 DISALLOW_COPY_AND_ASSIGN(PipelineInternal); 286 DISALLOW_COPY_AND_ASSIGN(PipelineImpl);
362 }; 287 };
363 288
364 } // namespace media 289 } // namespace media
365 290
366 #endif // MEDIA_BASE_PIPELINE_IMPL_H_ 291 #endif // MEDIA_BASE_PIPELINE_IMPL_H_
OLDNEW
« no previous file with comments | « no previous file | media/base/pipeline_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698