| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this |
| 2 // source code is governed by a BSD-style license that can be found in the | 2 // source code is governed by a BSD-style license that can be found in the |
| 3 // LICENSE file. | 3 // LICENSE file. |
| 4 | 4 |
| 5 // A base class that provides the plumbing for a decoder filters. | 5 // A base class that provides the plumbing for a decoder filters. |
| 6 | 6 |
| 7 #ifndef MEDIA_FILTERS_DECODER_BASE_H_ | 7 #ifndef MEDIA_FILTERS_DECODER_BASE_H_ |
| 8 #define MEDIA_FILTERS_DECODER_BASE_H_ | 8 #define MEDIA_FILTERS_DECODER_BASE_H_ |
| 9 | 9 |
| 10 #include <deque> | 10 #include <deque> |
| 11 | 11 |
| 12 #include "base/lock.h" | 12 #include "base/lock.h" |
| 13 #include "base/stl_util-inl.h" | 13 #include "base/stl_util-inl.h" |
| 14 #include "base/task.h" | 14 #include "base/task.h" |
| 15 #include "base/thread.h" | 15 #include "base/thread.h" |
| 16 #include "media/base/buffers.h" | 16 #include "media/base/buffers.h" |
| 17 #include "media/base/filters.h" | 17 #include "media/base/filters.h" |
| 18 #include "media/base/filter_host.h" | 18 #include "media/base/filter_host.h" |
| 19 | 19 |
| 20 namespace media { | 20 namespace media { |
| 21 | 21 |
| 22 template <class Decoder, class Output> | 22 template <class Decoder, class Output> |
| 23 class DecoderBase : public Decoder { | 23 class DecoderBase : public Decoder { |
| 24 public: | 24 public: |
| 25 typedef CallbackRunner< Tuple1<Output*> > ReadCallback; | 25 typedef CallbackRunner< Tuple1<Output*> > ReadCallback; |
| 26 | 26 |
| 27 // MediaFilter implementation. | 27 // MediaFilter implementation. |
| 28 virtual void Stop() { | 28 virtual void Stop() { |
| 29 message_loop()->PostTask(FROM_HERE, | 29 this->message_loop()->PostTask(FROM_HERE, |
| 30 NewRunnableMethod(this, &DecoderBase::StopTask)); | 30 NewRunnableMethod(this, &DecoderBase::StopTask)); |
| 31 } | 31 } |
| 32 | 32 |
| 33 virtual void Seek(base::TimeDelta time) { | 33 virtual void Seek(base::TimeDelta time) { |
| 34 message_loop()->PostTask(FROM_HERE, | 34 this->message_loop()->PostTask(FROM_HERE, |
| 35 NewRunnableMethod(this, &DecoderBase::SeekTask, time)); | 35 NewRunnableMethod(this, &DecoderBase::SeekTask, time)); |
| 36 } | 36 } |
| 37 | 37 |
| 38 // Decoder implementation. | 38 // Decoder implementation. |
| 39 virtual bool Initialize(DemuxerStream* demuxer_stream) { | 39 virtual bool Initialize(DemuxerStream* demuxer_stream) { |
| 40 message_loop()->PostTask(FROM_HERE, | 40 this->message_loop()->PostTask(FROM_HERE, |
| 41 NewRunnableMethod(this, &DecoderBase::InitializeTask, demuxer_stream)); | 41 NewRunnableMethod(this, &DecoderBase::InitializeTask, demuxer_stream)); |
| 42 return true; | 42 return true; |
| 43 } | 43 } |
| 44 | 44 |
| 45 virtual const MediaFormat& media_format() { return media_format_; } | 45 virtual const MediaFormat& media_format() { return media_format_; } |
| 46 | 46 |
| 47 // Audio or video decoder. | 47 // Audio or video decoder. |
| 48 virtual void Read(ReadCallback* read_callback) { | 48 virtual void Read(ReadCallback* read_callback) { |
| 49 message_loop()->PostTask(FROM_HERE, | 49 this->message_loop()->PostTask(FROM_HERE, |
| 50 NewRunnableMethod(this, &DecoderBase::ReadTask, read_callback)); | 50 NewRunnableMethod(this, &DecoderBase::ReadTask, read_callback)); |
| 51 } | 51 } |
| 52 | 52 |
| 53 void OnReadComplete(Buffer* buffer) { | 53 void OnReadComplete(Buffer* buffer) { |
| 54 // Little bit of magic here to get NewRunnableMethod() to generate a Task | 54 // Little bit of magic here to get NewRunnableMethod() to generate a Task |
| 55 // that holds onto a reference via scoped_refptr<>. | 55 // that holds onto a reference via scoped_refptr<>. |
| 56 // | 56 // |
| 57 // TODO(scherkus): change the callback format to pass a scoped_refptr<> or | 57 // TODO(scherkus): change the callback format to pass a scoped_refptr<> or |
| 58 // better yet see if we can get away with not using reference counting. | 58 // better yet see if we can get away with not using reference counting. |
| 59 scoped_refptr<Buffer> buffer_ref = buffer; | 59 scoped_refptr<Buffer> buffer_ref = buffer; |
| 60 message_loop()->PostTask(FROM_HERE, | 60 this->message_loop()->PostTask(FROM_HERE, |
| 61 NewRunnableMethod(this, &DecoderBase::ReadCompleteTask, buffer_ref)); | 61 NewRunnableMethod(this, &DecoderBase::ReadCompleteTask, buffer_ref)); |
| 62 } | 62 } |
| 63 | 63 |
| 64 protected: | 64 protected: |
| 65 DecoderBase() | 65 DecoderBase() |
| 66 : pending_reads_(0), | 66 : pending_reads_(0), |
| 67 seeking_(false), | 67 seeking_(false), |
| 68 state_(UNINITIALIZED), | 68 state_(UNINITIALIZED), |
| 69 thread_id_(NULL) { | 69 thread_id_(NULL) { |
| 70 } | 70 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 virtual void OnDecode(Buffer* input) = 0; | 109 virtual void OnDecode(Buffer* input) = 0; |
| 110 | 110 |
| 111 // Used for subclasses who friend unit tests and need to set the thread id. | 111 // Used for subclasses who friend unit tests and need to set the thread id. |
| 112 virtual void set_thread_id(PlatformThreadId thread_id) { | 112 virtual void set_thread_id(PlatformThreadId thread_id) { |
| 113 thread_id_ = thread_id; | 113 thread_id_ = thread_id; |
| 114 } | 114 } |
| 115 | 115 |
| 116 MediaFormat media_format_; | 116 MediaFormat media_format_; |
| 117 | 117 |
| 118 private: | 118 private: |
| 119 // GCC doesn't let us access superclass member variables directly, so use | |
| 120 // a helper to get around the situation. | |
| 121 // | |
| 122 // TODO(scherkus): another reason to add protected accessors to MediaFilter. | |
| 123 FilterHost* host() const { return Decoder::host_; } | |
| 124 MessageLoop* message_loop() const { return Decoder::message_loop_; } | |
| 125 bool IsStopped() { return state_ == STOPPED; } | 119 bool IsStopped() { return state_ == STOPPED; } |
| 126 | 120 |
| 127 void StopTask() { | 121 void StopTask() { |
| 128 DCHECK_EQ(PlatformThread::CurrentId(), thread_id_); | 122 DCHECK_EQ(PlatformThread::CurrentId(), thread_id_); |
| 129 // Delegate to the subclass first. | 123 // Delegate to the subclass first. |
| 130 OnStop(); | 124 OnStop(); |
| 131 | 125 |
| 132 // Throw away all buffers in all queues. | 126 // Throw away all buffers in all queues. |
| 133 result_queue_.clear(); | 127 result_queue_.clear(); |
| 134 STLDeleteElements(&read_queue_); | 128 STLDeleteElements(&read_queue_); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 152 DCHECK(state_ == UNINITIALIZED); | 146 DCHECK(state_ == UNINITIALIZED); |
| 153 DCHECK(!demuxer_stream_); | 147 DCHECK(!demuxer_stream_); |
| 154 DCHECK(!thread_id_ || thread_id_ == PlatformThread::CurrentId()); | 148 DCHECK(!thread_id_ || thread_id_ == PlatformThread::CurrentId()); |
| 155 demuxer_stream_ = demuxer_stream; | 149 demuxer_stream_ = demuxer_stream; |
| 156 | 150 |
| 157 // Grab the thread id for debugging. | 151 // Grab the thread id for debugging. |
| 158 thread_id_ = PlatformThread::CurrentId(); | 152 thread_id_ = PlatformThread::CurrentId(); |
| 159 | 153 |
| 160 // Delegate to subclass first. | 154 // Delegate to subclass first. |
| 161 if (!OnInitialize(demuxer_stream_)) { | 155 if (!OnInitialize(demuxer_stream_)) { |
| 162 host()->Error(PIPELINE_ERROR_DECODE); | 156 this->host()->Error(PIPELINE_ERROR_DECODE); |
| 163 return; | 157 return; |
| 164 } | 158 } |
| 165 | 159 |
| 166 // TODO(scherkus): subclass shouldn't mutate superclass media format. | 160 // TODO(scherkus): subclass shouldn't mutate superclass media format. |
| 167 DCHECK(!media_format_.empty()) << "Subclass did not set media_format_"; | 161 DCHECK(!media_format_.empty()) << "Subclass did not set media_format_"; |
| 168 state_ = INITIALIZED; | 162 state_ = INITIALIZED; |
| 169 host()->InitializationComplete(); | 163 this->host()->InitializationComplete(); |
| 170 } | 164 } |
| 171 | 165 |
| 172 void ReadTask(ReadCallback* read_callback) { | 166 void ReadTask(ReadCallback* read_callback) { |
| 173 DCHECK_EQ(PlatformThread::CurrentId(), thread_id_); | 167 DCHECK_EQ(PlatformThread::CurrentId(), thread_id_); |
| 174 // TODO(scherkus): should reply with a null operation (empty buffer). | 168 // TODO(scherkus): should reply with a null operation (empty buffer). |
| 175 if (IsStopped()) { | 169 if (IsStopped()) { |
| 176 delete read_callback; | 170 delete read_callback; |
| 177 return; | 171 return; |
| 178 } | 172 } |
| 179 | 173 |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 | 278 |
| 285 // Used for debugging. | 279 // Used for debugging. |
| 286 PlatformThreadId thread_id_; | 280 PlatformThreadId thread_id_; |
| 287 | 281 |
| 288 DISALLOW_COPY_AND_ASSIGN(DecoderBase); | 282 DISALLOW_COPY_AND_ASSIGN(DecoderBase); |
| 289 }; | 283 }; |
| 290 | 284 |
| 291 } // namespace media | 285 } // namespace media |
| 292 | 286 |
| 293 #endif // MEDIA_FILTERS_DECODER_BASE_H_ | 287 #endif // MEDIA_FILTERS_DECODER_BASE_H_ |
| OLD | NEW |