| 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 // VideoRendererBase creates its own thread for the sole purpose of timing frame | 5 // VideoRendererBase creates its own thread for the sole purpose of timing frame |
| 6 // presentation. It handles reading from the decoder and stores the results in | 6 // presentation. It handles reading from the decoder and stores the results in |
| 7 // a queue of decoded frames, calling OnFrameAvailable() on subclasses to notify | 7 // a queue of decoded frames, calling OnFrameAvailable() on subclasses to notify |
| 8 // when a frame is ready to display. | 8 // when a frame is ready to display. |
| 9 // | 9 // |
| 10 // The media filter methods Initialize(), Stop(), SetPlaybackRate() and Seek() | 10 // The media filter methods Initialize(), Stop(), SetPlaybackRate() and Seek() |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 public: | 30 public: |
| 31 VideoRendererBase(); | 31 VideoRendererBase(); |
| 32 virtual ~VideoRendererBase(); | 32 virtual ~VideoRendererBase(); |
| 33 | 33 |
| 34 // Helper method for subclasses to parse out video-related information from | 34 // Helper method for subclasses to parse out video-related information from |
| 35 // a MediaFormat. Returns true if |width_out| and |height_out| were assigned. | 35 // a MediaFormat. Returns true if |width_out| and |height_out| were assigned. |
| 36 static bool ParseMediaFormat(const MediaFormat& media_format, | 36 static bool ParseMediaFormat(const MediaFormat& media_format, |
| 37 int* width_out, int* height_out); | 37 int* width_out, int* height_out); |
| 38 | 38 |
| 39 // MediaFilter implementation. | 39 // MediaFilter implementation. |
| 40 virtual void Play(FilterCallback* callback); |
| 41 virtual void Pause(FilterCallback* callback); |
| 40 virtual void Stop(); | 42 virtual void Stop(); |
| 41 virtual void SetPlaybackRate(float playback_rate); | 43 virtual void SetPlaybackRate(float playback_rate); |
| 42 virtual void Seek(base::TimeDelta time, FilterCallback* callback); | 44 virtual void Seek(base::TimeDelta time, FilterCallback* callback); |
| 43 | 45 |
| 44 // VideoRenderer implementation. | 46 // VideoRenderer implementation. |
| 45 virtual void Initialize(VideoDecoder* decoder, FilterCallback* callback); | 47 virtual void Initialize(VideoDecoder* decoder, FilterCallback* callback); |
| 46 | 48 |
| 47 // PlatformThread::Delegate implementation. | 49 // PlatformThread::Delegate implementation. |
| 48 virtual void ThreadMain(); | 50 virtual void ThreadMain(); |
| 49 | 51 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 66 | 68 |
| 67 // Subclass interface. Called when a new frame is ready for display, which | 69 // Subclass interface. Called when a new frame is ready for display, which |
| 68 // can be accessed via GetCurrentFrame(). | 70 // can be accessed via GetCurrentFrame(). |
| 69 // | 71 // |
| 70 // Implementors should avoid doing any sort of heavy work in this method and | 72 // Implementors should avoid doing any sort of heavy work in this method and |
| 71 // instead post a task to a common/worker thread to handle rendering. Slowing | 73 // instead post a task to a common/worker thread to handle rendering. Slowing |
| 72 // down the video thread may result in losing synchronization with audio. | 74 // down the video thread may result in losing synchronization with audio. |
| 73 virtual void OnFrameAvailable() = 0; | 75 virtual void OnFrameAvailable() = 0; |
| 74 | 76 |
| 75 private: | 77 private: |
| 76 // Read complete callback from video decoder. | 78 // Read complete callback from video decoder and decrements |pending_reads_|. |
| 77 void OnReadComplete(VideoFrame* frame); | 79 void OnReadComplete(VideoFrame* frame); |
| 78 | 80 |
| 79 // Helper method that schedules an asynchronous read from the decoder. | 81 // Helper method that schedules an asynchronous read from the decoder and |
| 82 // increments |pending_reads_|. |
| 80 // | 83 // |
| 81 // Safe to call from any thread. | 84 // Safe to call from any thread. |
| 82 void ScheduleRead(); | 85 void ScheduleRead_Locked(); |
| 83 | 86 |
| 84 // Called by ThreadMain() to handle preroll. Returns false if the thread | 87 // Calculates the duration to sleep for based on |current_frame_|'s timestamp, |
| 85 // should exit due to Stop() being called. | 88 // the next frame timestamp (may be NULL), and the provided playback rate. |
| 86 bool WaitForInitialized(); | 89 // |
| 90 // We don't use |playback_rate_| to avoid locking. |
| 91 base::TimeDelta CalculateSleepDuration(VideoFrame* next_frame, |
| 92 float playback_rate); |
| 93 |
| 94 // Allocates YV12 frame based on |width_| and |height_|, and sets its data to |
| 95 // the YUV equivalent of RGB(0,0,0). |
| 96 void CreateBlackFrame(scoped_refptr<VideoFrame>* frame_out); |
| 97 |
| 98 // Used for accessing data members. |
| 99 Lock lock_; |
| 87 | 100 |
| 88 scoped_refptr<VideoDecoder> decoder_; | 101 scoped_refptr<VideoDecoder> decoder_; |
| 89 | 102 |
| 103 // Video dimensions parsed from the decoder's media format. |
| 104 int width_; |
| 105 int height_; |
| 106 |
| 90 // Queue of incoming frames as well as the current frame since the last time | 107 // Queue of incoming frames as well as the current frame since the last time |
| 91 // OnFrameAvailable() was called. | 108 // OnFrameAvailable() was called. |
| 92 typedef std::deque< scoped_refptr<VideoFrame> > VideoFrameQueue; | 109 typedef std::deque< scoped_refptr<VideoFrame> > VideoFrameQueue; |
| 93 VideoFrameQueue frames_; | 110 VideoFrameQueue frames_; |
| 94 scoped_refptr<VideoFrame> current_frame_; | 111 scoped_refptr<VideoFrame> current_frame_; |
| 95 | 112 |
| 96 // Used for accessing |frames_|. | |
| 97 Lock lock_; | |
| 98 | |
| 99 // Used to signal |thread_| as frames are added to |frames_|. Rule of thumb: | 113 // Used to signal |thread_| as frames are added to |frames_|. Rule of thumb: |
| 100 // always check |state_| to see if it was set to STOPPED after waking up! | 114 // always check |state_| to see if it was set to STOPPED after waking up! |
| 101 ConditionVariable frame_available_; | 115 ConditionVariable frame_available_; |
| 102 | 116 |
| 103 // Simple state tracking variable. | 117 // Simple state tracking variable. |
| 104 enum State { | 118 enum State { |
| 105 UNINITIALIZED, | 119 kUninitialized, |
| 106 INITIALIZING, | 120 kPaused, |
| 107 INITIALIZED, | 121 kSeeking, |
| 108 STOPPED, | 122 kPlaying, |
| 109 ERRORED, | 123 kStopped, |
| 124 kError, |
| 110 }; | 125 }; |
| 111 State state_; | 126 State state_; |
| 112 | 127 |
| 113 // Video thread handle. | 128 // Video thread handle. |
| 114 PlatformThreadHandle thread_; | 129 PlatformThreadHandle thread_; |
| 115 | 130 |
| 116 // Previous time returned from the pipeline. | 131 // Previous time returned from the pipeline. |
| 117 base::TimeDelta previous_time_; | 132 base::TimeDelta previous_time_; |
| 118 | 133 |
| 134 // Keeps track of our pending reads. We *must* have no pending reads before |
| 135 // executing the pause callback, otherwise we breach the contract that all |
| 136 // filters are idling. |
| 137 // |
| 138 // We use size_t since we compare against std::deque::size(). |
| 139 size_t pending_reads_; |
| 140 |
| 119 float playback_rate_; | 141 float playback_rate_; |
| 120 | 142 |
| 121 // Filter callbacks. | 143 // Filter callbacks. |
| 122 scoped_ptr<FilterCallback> initialize_callback_; | 144 scoped_ptr<FilterCallback> pause_callback_; |
| 145 scoped_ptr<FilterCallback> seek_callback_; |
| 123 | 146 |
| 124 DISALLOW_COPY_AND_ASSIGN(VideoRendererBase); | 147 DISALLOW_COPY_AND_ASSIGN(VideoRendererBase); |
| 125 }; | 148 }; |
| 126 | 149 |
| 127 } // namespace media | 150 } // namespace media |
| 128 | 151 |
| 129 #endif // MEDIA_FILTERS_VIDEO_RENDERER_BASE_H_ | 152 #endif // MEDIA_FILTERS_VIDEO_RENDERER_BASE_H_ |
| OLD | NEW |