OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef MEDIA_BASE_COMPOSITE_FILTER_H_ |
| 6 #define MEDIA_BASE_COMPOSITE_FILTER_H_ |
| 7 |
| 8 #include "base/threading/thread.h" |
| 9 #include "media/base/filter_host.h" |
| 10 #include "media/base/filters.h" |
| 11 |
| 12 namespace media { |
| 13 |
| 14 class CompositeFilter : public Filter { |
| 15 public: |
| 16 typedef base::Thread* (*ThreadFactoryFunction)(const char* thread_name); |
| 17 |
| 18 CompositeFilter(MessageLoop* message_loop); |
| 19 |
| 20 // Constructor that allows the default thread creation strategy to be |
| 21 // overridden. |
| 22 CompositeFilter(MessageLoop* message_loop, |
| 23 ThreadFactoryFunction thread_factory); |
| 24 |
| 25 // Adds a filter to the composite. This is only allowed after set_host() |
| 26 // is called and before the first state changing operation such as Play(), |
| 27 // Flush(), Stop(), or Seek(). True is returned if the filter was successfully |
| 28 // added to the composite. False is returned if the filter couldn't be added |
| 29 // because the composite is in the wrong state or the filter needed a thread |
| 30 // and the composite was unable to create one. |
| 31 bool AddFilter(scoped_refptr<Filter> filter); |
| 32 |
| 33 // media::Filter methods. |
| 34 virtual const char* major_mime_type() const; |
| 35 virtual void set_host(FilterHost* host); |
| 36 virtual FilterHost* host(); |
| 37 virtual bool requires_message_loop() const; |
| 38 virtual const char* message_loop_name() const; |
| 39 virtual void set_message_loop(MessageLoop* message_loop); |
| 40 virtual MessageLoop* message_loop(); |
| 41 virtual void Play(FilterCallback* play_callback); |
| 42 virtual void Pause(FilterCallback* pause_callback); |
| 43 virtual void Flush(FilterCallback* flush_callback); |
| 44 virtual void Stop(FilterCallback* stop_callback); |
| 45 virtual void SetPlaybackRate(float playback_rate); |
| 46 virtual void Seek(base::TimeDelta time, FilterCallback* seek_callback); |
| 47 virtual void OnAudioRendererDisabled(); |
| 48 |
| 49 protected: |
| 50 virtual ~CompositeFilter(); |
| 51 |
| 52 /// Default thread factory strategy. |
| 53 static base::Thread* DefaultThreadFactory(const char* thread_name); |
| 54 |
| 55 void SetError(PipelineError error); |
| 56 |
| 57 private: |
| 58 class FilterHostImpl; |
| 59 |
| 60 enum State { |
| 61 kInvalid, |
| 62 kCreated, |
| 63 kPaused, |
| 64 kPlayPending, |
| 65 kStopWhilePlayPending, |
| 66 kPlaying, |
| 67 kPausePending, |
| 68 kStopWhilePausePending, |
| 69 kFlushPending, |
| 70 kStopWhileFlushPending, |
| 71 kSeekPending, |
| 72 kStopWhileSeekPending, |
| 73 kStopPending, |
| 74 kStopped, |
| 75 kError |
| 76 }; |
| 77 |
| 78 // Initialization method called by constructors. |
| 79 void Init(MessageLoop* message_loop, ThreadFactoryFunction thread_factory); |
| 80 |
| 81 // Transition to a new state. |
| 82 void ChangeState(State new_state); |
| 83 |
| 84 // Start calling filters in a sequence. |
| 85 void StartSerialCallSequence(); |
| 86 |
| 87 // Call filters in parallel. |
| 88 void StartParallelCallSequence(); |
| 89 |
| 90 // Call the filter based on the current value of state_. |
| 91 void CallFilter(scoped_refptr<Filter>& filter, FilterCallback* callback); |
| 92 |
| 93 // Calls |callback_| and then clears the reference. |
| 94 void DispatchPendingCallback(); |
| 95 |
| 96 // Gets the state to transition to given |state|. |
| 97 State GetNextState(State state) const; |
| 98 |
| 99 // Filter callback for a serial sequence. |
| 100 void SerialCallback(); |
| 101 |
| 102 // Filter callback for a parallel sequence. |
| 103 void ParallelCallback(); |
| 104 |
| 105 // Called when a parallel or serial call sequence completes. |
| 106 void OnCallSequenceDone(); |
| 107 |
| 108 // Helper function for sending an error to the FilterHost. |
| 109 void SendErrorToHost(PipelineError error); |
| 110 |
| 111 // Helper function for handling errors during call sequences. |
| 112 void HandleError(PipelineError error); |
| 113 |
| 114 // Creates a callback that can be called from any thread, but is guaranteed |
| 115 // to call the specified method on the thread associated with this filter. |
| 116 FilterCallback* NewThreadSafeCallback(void (CompositeFilter::*method)()); |
| 117 |
| 118 // Helper function used by NewThreadSafeCallback() to make sure the |
| 119 // method gets called on the right thread. |
| 120 void OnCallback(MessageLoop* message_loop, |
| 121 void (CompositeFilter::*method)()); |
| 122 |
| 123 // Helper function that indicates whether SetError() calls can be forwarded |
| 124 // to the host of this filter. |
| 125 bool CanForwardError(); |
| 126 |
| 127 // Vector of threads owned by the composite and used by filters in |filters_|. |
| 128 typedef std::vector<base::Thread*> FilterThreadVector; |
| 129 FilterThreadVector filter_threads_; |
| 130 |
| 131 // Vector of the filters added to the composite. |
| 132 typedef std::vector<scoped_refptr<Filter> > FilterVector; |
| 133 FilterVector filters_; |
| 134 |
| 135 // Factory function used to create filter threads. |
| 136 ThreadFactoryFunction thread_factory_; |
| 137 |
| 138 // Callback for the pending request. |
| 139 scoped_ptr<FilterCallback> callback_; |
| 140 |
| 141 // Time parameter for the pending Seek() request. |
| 142 base::TimeDelta pending_seek_time_; |
| 143 |
| 144 // Current state of this filter. |
| 145 State state_; |
| 146 |
| 147 // The index of the filter currently processing a request. |
| 148 unsigned int sequence_index_; |
| 149 |
| 150 // Message loop passed into the constructor. |
| 151 MessageLoop* message_loop_; |
| 152 |
| 153 // FilterHost implementation passed to Filters owned by this |
| 154 // object. |
| 155 scoped_ptr<FilterHostImpl> host_impl_; |
| 156 |
| 157 // Error passed in the last SetError() call. |
| 158 PipelineError error_; |
| 159 |
| 160 DISALLOW_COPY_AND_ASSIGN(CompositeFilter); |
| 161 }; |
| 162 |
| 163 } // namespace media |
| 164 |
| 165 #endif // MEDIA_BASE_COMPOSITE_FILTER_H_ |
OLD | NEW |