| 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 // The video renderer implementation to be use by the media pipeline. It lives | 5 // The video renderer implementation to be use by the media pipeline. It lives |
| 6 // inside video renderer thread and also WebKit's main thread. We need to be | 6 // inside video renderer thread and also WebKit's main thread. We need to be |
| 7 // extra careful about members shared by two different threads, especially | 7 // extra careful about members shared by two different threads, especially |
| 8 // video frame buffers. | 8 // video frame buffers. |
| 9 // | 9 // |
| 10 // Methods called from WebKit's main thread: | 10 // Methods called from WebKit's main thread: |
| 11 // Paint() | 11 // Paint() |
| 12 // SetRect() | 12 // SetRect() |
| 13 | 13 |
| 14 #ifndef CHROME_RENDERER_MEDIA_VIDEO_RENDERER_IMPL_H_ | 14 #ifndef CHROME_RENDERER_MEDIA_VIDEO_RENDERER_IMPL_H_ |
| 15 #define CHROME_RENDERER_MEDIA_VIDEO_RENDERER_IMPL_H_ | 15 #define CHROME_RENDERER_MEDIA_VIDEO_RENDERER_IMPL_H_ |
| 16 | 16 |
| 17 #include <deque> | |
| 18 | |
| 19 #include "base/lock.h" | |
| 20 #include "base/task.h" | |
| 21 #include "base/gfx/platform_canvas.h" | 17 #include "base/gfx/platform_canvas.h" |
| 18 #include "base/gfx/rect.h" |
| 19 #include "base/gfx/size.h" |
| 22 #include "chrome/renderer/webmediaplayer_delegate_impl.h" | 20 #include "chrome/renderer/webmediaplayer_delegate_impl.h" |
| 23 #include "base/gfx/rect.h" | 21 #include "media/base/buffers.h" |
| 24 #include "media/base/factory.h" | 22 #include "media/base/factory.h" |
| 25 #include "media/base/filters.h" | 23 #include "media/base/filters.h" |
| 24 #include "media/filters/video_renderer_base.h" |
| 26 #include "webkit/glue/webmediaplayer_delegate.h" | 25 #include "webkit/glue/webmediaplayer_delegate.h" |
| 27 | 26 |
| 28 class SubmitReadsTask; | 27 class VideoRendererImpl : public media::VideoRendererBase { |
| 28 public: |
| 29 // Methods for painting called by the WebMediaPlayerDelegateImpl |
| 29 | 30 |
| 30 class VideoRendererImpl : public media::VideoRenderer { | 31 // This method is called with the same rect as the Paint method and could |
| 31 public: | 32 // be used by future implementations to implement an improved color space + |
| 32 // media::MediaFilter implementation. | 33 // scale code on a seperate thread. Since we always do the streach on the |
| 33 virtual void Stop(); | 34 // same thread as the Paint method, we just ignore the call for now. |
| 35 virtual void SetRect(const gfx::Rect& rect) {} |
| 34 | 36 |
| 35 // media::VideoRenderer implementation. | 37 // Paint the current front frame on the |canvas| streaching it to fit the |
| 36 virtual bool Initialize(media::VideoDecoder* decoder); | 38 // |dest_rect| |
| 37 | 39 virtual void Paint(skia::PlatformCanvas* canvas, const gfx::Rect& dest_rect); |
| 38 // Methods for painting called by the WebMediaPlayerDelegateImpl | |
| 39 // TODO(ralphl): What the *$*%##@ is this? What does it mean? How do we | |
| 40 // treat the "rect"? Is is clipping? | |
| 41 virtual void SetRect(const gfx::Rect& rect); | |
| 42 | |
| 43 // TODO(ralphl): What is this rect? Is it relative to the canvas? | |
| 44 virtual void Paint(skia::PlatformCanvas* canvas, const gfx::Rect& rect); | |
| 45 | 40 |
| 46 // Static method for creating factory for this object. | 41 // Static method for creating factory for this object. |
| 47 static media::FilterFactory* CreateFactory( | 42 static media::FilterFactory* CreateFactory( |
| 48 WebMediaPlayerDelegateImpl* delegate) { | 43 WebMediaPlayerDelegateImpl* delegate) { |
| 49 return new media::FilterFactoryImpl1< | 44 return new media::FilterFactoryImpl1< |
| 50 VideoRendererImpl, WebMediaPlayerDelegateImpl*>(delegate); | 45 VideoRendererImpl, WebMediaPlayerDelegateImpl*>(delegate); |
| 51 } | 46 } |
| 52 | 47 |
| 53 // Implementation of AssignableBuffer<this>::OnAssignment method. | 48 private: |
| 54 void OnAssignment(media::VideoFrame* video_frame); | 49 // Method called by base class during initialization. |
| 50 virtual bool OnInitialize(size_t width, size_t height); |
| 55 | 51 |
| 56 private: | 52 // Method called by the VideoRendererBase when a repaint is needed. |
| 57 friend class SubmitReadsTask; | 53 virtual void OnPaintNeeded(); |
| 54 |
| 58 friend class media::FilterFactoryImpl1<VideoRendererImpl, | 55 friend class media::FilterFactoryImpl1<VideoRendererImpl, |
| 59 WebMediaPlayerDelegateImpl*>; | 56 WebMediaPlayerDelegateImpl*>; |
| 60 | 57 |
| 61 // Constructor and destructor are private. Only the filter factory is | 58 // Constructor and destructor are private. Only the filter factory is |
| 62 // allowed to create instances. | 59 // allowed to create instances. |
| 63 explicit VideoRendererImpl(WebMediaPlayerDelegateImpl* delegate); | 60 explicit VideoRendererImpl(WebMediaPlayerDelegateImpl* delegate); |
| 64 virtual ~VideoRendererImpl(); | 61 virtual ~VideoRendererImpl() {} |
| 65 | |
| 66 // Answers question from the factory to see if we accept |format|. | |
| 67 static bool IsMediaFormatSupported(const media::MediaFormat* format); | |
| 68 | |
| 69 // Used by the IsMediaFormatSupported and Initialize methods. Examines the | |
| 70 // |media_format| and returns true if the format is supported. Both output | |
| 71 // parameters, |width_out| and |height_out| are required and must not be NULL. | |
| 72 static bool ParseMediaFormat(const media::MediaFormat* media_format, | |
| 73 int* width_out, | |
| 74 int* height_out); | |
| 75 | |
| 76 // Used internally to post a task that will call the SubmitReads() method. | |
| 77 // The |lock_| must be acquired before calling this method. If the value of | |
| 78 // |number_of_reads_needed_| is 0 or if there is already a pending task then | |
| 79 // this method simply returns and does not post a new task. | |
| 80 void PostSubmitReadsTask(); | |
| 81 | |
| 82 // Examines the |number_of_reads_needed_| member and calls the decoder to | |
| 83 // read the necessary number of frames. | |
| 84 void SubmitReads(); | |
| 85 | |
| 86 // For simplicity, we use the |delegate_| member to indicate if we have been | |
| 87 // stopped or not. | |
| 88 bool IsRunning() const { return (delegate_ != NULL); } | |
| 89 | |
| 90 // Throw away all frames in the queue. The |lock_| must have been acquired | |
| 91 // before calling this method. | |
| 92 void DiscardAllFrames(); | |
| 93 | |
| 94 // This method is always called with the object's |lock_| acquired.. | |
| 95 // The bool return value indicates weather or not the front of the queue has | |
| 96 // been updated. If this method returns true, then the front of the queue | |
| 97 // is a new video frame, otherwise, the front is the same as the last call. | |
| 98 // Given the current |time|, this method updates the state of the video frame | |
| 99 // queue. The caller may pass in a |new_frame| which will be added to the | |
| 100 // queue in the appropriate position based on the frame's timestamp. The | |
| 101 // |front_frame_out| and |time_next_frame_out| parameters are both optional | |
| 102 // and can be NULL. If |front_frame_out| is non-NULL, then it is returned as | |
| 103 // either NULL, which indicates that there are no frames in the queue, or | |
| 104 // it will be a pointer to the frame at the front of the queue. NOTE: THIS | |
| 105 // FRAME'S REFERENCE COUNT HAS BEEN INCREMENTED BY THIS CALL. THE | |
| 106 // CALLING FUNCTION MUST CALL |front_frame_out|->Release(); | |
| 107 // If the |time_next_frame_out| parameter is non-NULL then it will be assigned | |
| 108 // the stream time of the next frame in the queue. This is used by the Paint | |
| 109 // method to schedule a time update callback at the appropriate presentation | |
| 110 // time for the next frame. | |
| 111 bool UpdateQueue(base::TimeDelta time, | |
| 112 media::VideoFrame* new_frame, | |
| 113 media::VideoFrame** front_frame_out, | |
| 114 base::TimeDelta* time_next_frame_out); | |
| 115 | 62 |
| 116 // Internal method used by the Paint method to convert the specified video | 63 // Internal method used by the Paint method to convert the specified video |
| 117 // frame to RGB, placing the converted pixels in the |current_frame_| bitmap. | 64 // frame to RGB, placing the converted pixels in the |current_frame_| bitmap. |
| 118 void CopyToCurrentFrame(media::VideoFrame* video_frame); | 65 void CopyToCurrentFrame(media::VideoFrame* video_frame); |
| 119 | 66 |
| 120 // Called when the clock is updated by the audio renderer of when a scheduled | |
| 121 // callback is called based on the interpolated current position of the media | |
| 122 // stream. | |
| 123 void TimeUpdateCallback(base::TimeDelta time); | |
| 124 | |
| 125 // Critical section. There is only one for this object. Used to serialize | |
| 126 // access to the following members: | |
| 127 // |queue_| for obvious reasons | |
| 128 // |delegate_| because it is used by methods that can be called from random | |
| 129 // threads to determine if the render has been stopped (it will | |
| 130 // be NULL if stopped) | |
| 131 // |submit_reads_task_| to prevent multiple scheduling of the task and to | |
| 132 // allow for safe cancelation of the task. | |
| 133 // |current_frame_timestamp_| because member is used by the render | |
| 134 // thread in the Paint method and by DiscardAllFrames which can be | |
| 135 // called on the decoder's thread when a seek occurs. | |
| 136 // |number_of_reads_needed_| is modified by UpdateQueue from the decoder | |
| 137 // thread, the renderer thread, and the pipeline thread. | |
| 138 // |preroll_complete_| has a very small potential race condition if the | |
| 139 // OnAssignment method were reentered for the last frame in the queue | |
| 140 // and an end-of-stream frame. | |
| 141 Lock lock_; | |
| 142 | |
| 143 // Pointer to our parent object that is called to request repaints. | 67 // Pointer to our parent object that is called to request repaints. |
| 144 WebMediaPlayerDelegateImpl* delegate_; | 68 WebMediaPlayerDelegateImpl* delegate_; |
| 145 | 69 |
| 146 // Pointer to the decoder that will feed us with video frames. | 70 // An RGB bitmap used to convert the video frames. |
| 147 scoped_refptr<media::VideoDecoder> decoder_; | 71 SkBitmap bitmap_; |
| 148 | 72 |
| 149 // If non-NULL then a task has been scheduled to submit read requests to the | 73 // These two members are used to determine if the |bitmap_| contains |
| 150 // video decoder. | 74 // an already converted image of the current frame. IMPORTANT NOTE: The |
| 151 CancelableTask* submit_reads_task_; | 75 // value of |last_converted_frame_| must only be used for comparison purposes, |
| 76 // and it should be assumed that the value of the pointer is INVALID unless |
| 77 // it matches the pointer returned from GetCurrentFrame(). Even then, just |
| 78 // to make sure, we compare the timestamp to be sure the bits in the |
| 79 // |current_frame_bitmap_| are valid. |
| 80 media::VideoFrame* last_converted_frame_; |
| 81 base::TimeDelta last_converted_timestamp_; |
| 152 | 82 |
| 153 // The number of buffers we need to request. This member is updated by any | 83 // The size of the video. |
| 154 // method that removes frames from the queue, such as UpdateQueue and | 84 gfx::Size video_size_; |
| 155 // DiscardAllFrames. | |
| 156 int number_of_reads_needed_; | |
| 157 | |
| 158 // An RGB bitmap of the current frame. Note that we use the timestamp to | |
| 159 // determine if the frame contents need to be color space converted, or if the | |
| 160 // |current_frame_| member contains the correct image for the queue front. | |
| 161 SkBitmap current_frame_; | |
| 162 base::TimeDelta current_frame_timestamp_; | |
| 163 | |
| 164 // TODO(ralphl): Try to understand all of the various "rect" and dimension | |
| 165 // aspects of the renderer and document it so that other people can understand | |
| 166 // it too. I can not accurately describe what the role of this member is now. | |
| 167 gfx::Rect rect_; | |
| 168 | |
| 169 // The queue of video frames. The front of the queue is the frame that should | |
| 170 // be displayed. | |
| 171 typedef std::deque<media::VideoFrame*> VideoFrameQueue; | |
| 172 VideoFrameQueue queue_; | |
| 173 | |
| 174 // True if we have received a full queue of video frames from the decoder. | |
| 175 // We don't call FilterHost::InitializationComplete() until the the queue | |
| 176 // is full. | |
| 177 bool preroll_complete_; | |
| 178 | 85 |
| 179 DISALLOW_COPY_AND_ASSIGN(VideoRendererImpl); | 86 DISALLOW_COPY_AND_ASSIGN(VideoRendererImpl); |
| 180 }; | 87 }; |
| 181 | 88 |
| 182 #endif // CHROME_RENDERER_MEDIA_VIDEO_RENDERER_IMPL_H_ | 89 #endif // CHROME_RENDERER_MEDIA_VIDEO_RENDERER_IMPL_H_ |
| OLD | NEW |