summaryrefslogtreecommitdiffstats
path: root/media/base/pipeline_impl.h
blob: b72e368e0346710fda0ab803d2a673d7534ad7c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Copyright (c) 2008-2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Implementation of Pipeline.

#ifndef MEDIA_BASE_PIPELINE_IMPL_H_
#define MEDIA_BASE_PIPELINE_IMPL_H_

#include <string>
#include <vector>
#include <set>

#include "base/message_loop.h"
#include "base/ref_counted.h"
#include "base/thread.h"
#include "base/time.h"
#include "media/base/filter_host.h"
#include "media/base/pipeline.h"

namespace media {

class PipelineInternal;

// Class which implements the Media::Pipeline contract.  The majority of the
// actual code for this object lives in the PipelineInternal class, which is
// responsible for actually building and running the pipeline.  This object
// is basically a simple container for state information, and is responsible
// for creating and communicating with the PipelineInternal object.
class PipelineImpl : public Pipeline {
 public:
  PipelineImpl(MessageLoop* message_loop);

  // Pipeline implementation.
  virtual bool Start(FilterFactory* filter_factory,
                     const std::string& uri,
                     PipelineCallback* start_callback);
  virtual void Stop(PipelineCallback* stop_callback);
  virtual void Seek(base::TimeDelta time, PipelineCallback* seek_callback);
  virtual bool IsRunning() const;
  virtual bool IsInitialized() const;
  virtual bool IsRendered(const std::string& major_mime_type) const;
  virtual float GetPlaybackRate() const;
  virtual void SetPlaybackRate(float playback_rate);
  virtual float GetVolume() const;
  virtual void SetVolume(float volume);
  virtual base::TimeDelta GetCurrentTime() const;
  virtual base::TimeDelta GetBufferedTime() const;
  virtual base::TimeDelta GetDuration() const;
  virtual int64 GetBufferedBytes() const;
  virtual int64 GetTotalBytes() const;
  virtual void GetVideoSize(size_t* width_out, size_t* height_out) const;
  virtual PipelineError GetError() const;

 private:
  friend class PipelineInternal;
  virtual ~PipelineImpl();

  // Reset the state of the pipeline object to the initial state.  This method
  // is used by the constructor, and the Stop method.
  void ResetState();

  // Used internally to make sure that the thread is in a state that is
  // acceptable to post a task to.  It must exist, be initialized, and there
  // must not be an error.
  bool IsPipelineOk() const;

  // Methods called by |pipeline_internal_| to update global pipeline data.
  //
  // Although this is the exact same as the FilterHost interface, we need to
  // let |pipeline_internal_| receive the call first so it can post tasks as
  // necessary.
  void SetError(PipelineError error);
  base::TimeDelta GetTime() const;
  void SetTime(base::TimeDelta time);
  void SetDuration(base::TimeDelta duration);
  void SetBufferedTime(base::TimeDelta buffered_time);
  void SetTotalBytes(int64 total_bytes);
  void SetBufferedBytes(int64 buffered_bytes);
  void SetVideoSize(size_t width, size_t height);

  // Method called by the |pipeline_internal_| to insert a mime type into
  // the |rendered_mime_types_| set.
  void InsertRenderedMimeType(const std::string& major_mime_type);

  // Message loop used to execute pipeline tasks.
  MessageLoop* message_loop_;

  // Holds a ref counted reference to the PipelineInternal object associated
  // with this pipeline.  Prior to the call to the Start() method, this member
  // will be NULL, since we are not running.
  scoped_refptr<PipelineInternal> pipeline_internal_;

  // After calling Start, if all of the required filters are created and
  // initialized, this member will be set to true by the pipeline thread.
  bool initialized_;

  // Duration of the media in microseconds.  Set by filters.
  base::TimeDelta duration_;

  // Amount of available buffered data in microseconds.  Set by filters.
  base::TimeDelta buffered_time_;

  // Amount of available buffered data.  Set by filters.
  int64 buffered_bytes_;

  // Total size of the media.  Set by filters.
  int64 total_bytes_;

  // Lock used to serialize access for getter/setter methods.
  mutable Lock lock_;

  // Video width and height.  Set by filters.
  size_t video_width_;
  size_t video_height_;

  // Current volume level (from 0.0f to 1.0f).  This value is set immediately
  // via SetVolume() and a task is dispatched on the message loop to notify the
  // filters.
  float volume_;

  // Current playback rate (>= 0.0f).  This value is set immediately via
  // SetPlaybackRate() and a task is dispatched on the message loop to notify
  // the filters.
  float playback_rate_;

  // Current playback time.  Set by filters.
  base::TimeDelta time_;

  // Status of the pipeline.  Initialized to PIPELINE_OK which indicates that
  // the pipeline is operating correctly. Any other value indicates that the
  // pipeline is stopped or is stopping.  Clients can call the Stop method to
  // reset the pipeline state, and restore this to PIPELINE_OK.
  PipelineError error_;

  // Vector of major mime types that have been rendered by this pipeline.
  typedef std::set<std::string> RenderedMimeTypesSet;
  RenderedMimeTypesSet rendered_mime_types_;

  DISALLOW_COPY_AND_ASSIGN(PipelineImpl);
};


// PipelineInternal contains most of the logic involved with running the
// media pipeline. Filters are created and called on the message loop injected
// into this object. PipelineInternal works like a state machine to perform
// asynchronous initialization. Initialization is done in multiple passes by
// InitializeTask(). In each pass a different filter is created and chained with
// a previously created filter.
//
// Here's a state diagram that describes the lifetime of this object.
//
// [ *Created ] -> [ InitDataSource ] -> [ InitDemuxer ] ->
// [ InitAudioDecoder ] -> [ InitAudioRenderer ] ->
// [ InitVideoDecoder ] -> [ InitVideoRenderer ] -> [ Started ]
//        |                    |                         |
//        .-> [ Error ]        .->      [ Stopped ]    <-.
//
// Initialization is a series of state transitions from "Created" to
// "Started". If any error happens during initialization, this object will
// transition to the "Error" state from any state. If Stop() is called during
// initialization, this object will transition to "Stopped" state.

class PipelineInternal : public FilterHost,
    public base::RefCountedThreadSafe<PipelineInternal> {
 public:
  // Methods called by PipelineImpl object on the client's thread.  These
  // methods post a task to call a corresponding xxxTask() method on the
  // message loop.  For example, Seek posts a task to call SeekTask.
  PipelineInternal(PipelineImpl* pipeline, MessageLoop* message_loop);

  // After Start() is called, a task of StartTask() is posted on the pipeline
  // thread to perform initialization. See StartTask() to learn more about
  // initialization.
  void Start(FilterFactory* filter_factory,
             const std::string& url_media_source,
             PipelineCallback* start_callback);
  void Stop(PipelineCallback* stop_callback);
  void Seek(base::TimeDelta time, PipelineCallback* seek_callback);

  // Notifies that the client has changed the playback rate/volume.
  void PlaybackRateChanged(float playback_rate);
  void VolumeChanged(float volume);

  // Returns true if the pipeline has fully initialized.
  bool IsInitialized() { return state_ == kStarted; }

 private:
  // Only allow ourselves to be destroyed via ref-counting.
  friend class base::RefCountedThreadSafe<PipelineInternal>;
  virtual ~PipelineInternal();

  // FilterHost implementation.
  virtual void SetError(PipelineError error);
  virtual base::TimeDelta GetTime() const;
  virtual void SetTime(base::TimeDelta time);
  virtual void SetDuration(base::TimeDelta duration);
  virtual void SetBufferedTime(base::TimeDelta buffered_time);
  virtual void SetTotalBytes(int64 total_bytes);
  virtual void SetBufferedBytes(int64 buffered_bytes);
  virtual void SetVideoSize(size_t width, size_t height);

  enum State {
    kCreated,
    kInitDataSource,
    kInitDemuxer,
    kInitAudioDecoder,
    kInitAudioRenderer,
    kInitVideoDecoder,
    kInitVideoRenderer,
    kStarted,
    kStopped,
    kError,
  };

  // Simple method used to make sure the pipeline is running normally.
  bool IsPipelineOk() { return PIPELINE_OK == pipeline_->error_; }

  // Helper method to tell whether we are in the state of initializing.
  bool IsPipelineInitializing() {
    return state_ == kInitDataSource ||
           state_ == kInitDemuxer ||
           state_ == kInitAudioDecoder ||
           state_ == kInitAudioRenderer ||
           state_ == kInitVideoDecoder ||
           state_ == kInitVideoRenderer;
  }

  // Callback executed by filters upon completing initialization and seeking.
  void OnFilterInitialize();
  void OnFilterSeek();

  // The following "task" methods correspond to the public methods, but these
  // methods are run as the result of posting a task to the PipelineInternal's
  // message loop.
  void StartTask(FilterFactory* filter_factory,
                 const std::string& url,
                 PipelineCallback* start_callback);

  // InitializeTask() performs initialization in multiple passes. It is executed
  // as a result of calling Start() or InitializationComplete() that advances
  // initialization to the next state. It works as a hub of state transition for
  // initialization.
  void InitializeTask();

  // StopTask() and ErrorTask() are similar but serve different purposes:
  //   - Both destroy the filter chain.
  //   - Both will execute |start_callback| if the pipeline was initializing.
  //   - StopTask() resets the pipeline to a fresh state, where as ErrorTask()
  //     leaves the pipeline as is for client inspection.
  //   - StopTask() can be scheduled by the client calling Stop(), where as
  //     ErrorTask() is scheduled as a result of a filter calling SetError().
  void StopTask(PipelineCallback* stop_callback);
  void ErrorTask(PipelineError error);

  // Carries out notifying filters that the playback rate/volume has changed,
  void PlaybackRateChangedTask(float playback_rate);
  void VolumeChangedTask(float volume);

  // Carries out notifying filters that we are seeking to a new timestamp.
  void SeekTask(base::TimeDelta time, PipelineCallback* seek_callback);

  // Internal methods used in the implementation of the pipeline thread.  All
  // of these methods are only called on the pipeline thread.

  // The following template functions make use of the fact that media filter
  // derived interfaces are self-describing in the sense that they all contain
  // the static method filter_type() which returns a FilterType enum that
  // uniquely identifies the filter's interface.  In addition, filters that are
  // specific to audio or video also support a static method major_mime_type()
  // which returns a string of "audio/" or "video/".
  //
  // Uses the FilterFactory to create a new filter of the Filter class, and
  // initializes it using the Source object.  The source may be another filter
  // or it could be a string in the case of a DataSource.
  //
  // The CreateFilter() method actually does much more than simply creating the
  // filter.  It also creates the filter's thread and injects a FilterHost and
  // MessageLoop.  Finally, it calls the filter's type-specific Initialize()
  // method to initialize the filter.  If the required filter cannot be created,
  // PIPELINE_ERROR_REQUIRED_FILTER_MISSING is raised, initialization is halted
  // and this object will remain in the "Error" state.
  template <class Filter, class Source>
  void CreateFilter(FilterFactory* filter_factory,
                    Source source,
                    const MediaFormat& source_media_format);

  // Creates a Filter and initializes it with the given |source|.  If a Filter
  // could not be created or an error occurred, this method returns NULL and the
  // pipeline's |error_| member will contain a specific error code.  Note that
  // the Source could be a filter or a DemuxerStream, but it must support the
  // GetMediaFormat() method.
  template <class Filter, class Source>
  void CreateFilter(FilterFactory* filter_factory, Source* source) {
    CreateFilter<Filter, Source*>(filter_factory,
                                  source,
                                  source->media_format());
  }

  // Creates a DataSource (the first filter in a pipeline).
  void CreateDataSource();

  // Creates a Demuxer.
  void CreateDemuxer();

  // Creates a decoder of type Decoder. Returns true if the asynchronous action
  // of creating decoder has started. Returns false if this method did nothing
  // because the corresponding audio/video stream does not exist.
  template <class Decoder>
  bool CreateDecoder();

  // Creates a renderer of type Renderer and connects it with Decoder. Returns
  // true if the asynchronous action of creating renderer has started. Returns
  // false if this method did nothing because the corresponding audio/video
  // stream does not exist.
  template <class Decoder, class Renderer>
  bool CreateRenderer();

  // Examine the list of existing filters to find one that supports the
  // specified Filter interface. If one exists, the |filter_out| will contain
  // the filter, |*filter_out| will be NULL.
  template <class Filter>
  void GetFilter(scoped_refptr<Filter>* filter_out) const;

  // Stops every filters, filter host and filter thread and releases all
  // references to them.
  void DestroyFilters();

  // Pointer to the pipeline that owns this PipelineInternal.
  PipelineImpl* pipeline_;

  // Message loop used to execute pipeline tasks.
  MessageLoop* message_loop_;

  // Member that tracks the current state.
  State state_;

  // Filter factory as passed in by Start().
  scoped_refptr<FilterFactory> filter_factory_;

  // URL for the data source as passed in by Start().
  std::string url_;

  // Callbacks for various pipeline operations.
  scoped_ptr<PipelineCallback> start_callback_;
  scoped_ptr<PipelineCallback> seek_callback_;
  scoped_ptr<PipelineCallback> stop_callback_;

  // Vector of our filters and map maintaining the relationship between the
  // FilterType and the filter itself.
  typedef std::vector<scoped_refptr<MediaFilter> > FilterVector;
  FilterVector filters_;

  typedef std::map<FilterType, scoped_refptr<MediaFilter> > FilterTypeMap;
  FilterTypeMap filter_types_;

  // Vector of threads owned by the pipeline and being used by filters.
  typedef std::vector<base::Thread*> FilterThreadVector;
  FilterThreadVector filter_threads_;

  DISALLOW_COPY_AND_ASSIGN(PipelineInternal);
};

}  // namespace media

#endif  // MEDIA_BASE_PIPELINE_IMPL_H_