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_H_ | 14 #ifndef CHROME_RENDERER_MEDIA_VIDEO_RENDERER_IMPL_H_ |
15 #define CHROME_RENDERER_MEDIA_VIDEO_RENDERER_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" |
17 #include "base/gfx/platform_canvas.h" | 21 #include "base/gfx/platform_canvas.h" |
| 22 #include "chrome/renderer/webmediaplayer_delegate_impl.h" |
18 #include "base/gfx/rect.h" | 23 #include "base/gfx/rect.h" |
19 #include "media/base/factory.h" | 24 #include "media/base/factory.h" |
20 #include "media/base/filters.h" | 25 #include "media/base/filters.h" |
| 26 #include "webkit/glue/webmediaplayer_delegate.h" |
21 | 27 |
22 class WebMediaPlayerDelegateImpl; | 28 class SubmitReadsTask; |
23 | 29 |
24 class VideoRendererImpl : public media::VideoRenderer { | 30 class VideoRendererImpl : public media::VideoRenderer { |
25 public: | 31 public: |
26 VideoRendererImpl(WebMediaPlayerDelegateImpl* delegate); | |
27 | |
28 // media::MediaFilter implementation. | 32 // media::MediaFilter implementation. |
29 virtual void Stop(); | 33 virtual void Stop(); |
30 | 34 |
31 // media::VideoRenderer implementation. | 35 // media::VideoRenderer implementation. |
32 virtual bool Initialize(media::VideoDecoder* decoder); | 36 virtual bool Initialize(media::VideoDecoder* decoder); |
33 | 37 |
34 // Called from WebMediaPlayerDelegateImpl from WebKit's main thread. | 38 // Methods for painting called by the WebMediaPlayerDelegateImpl |
35 void Paint(skia::PlatformCanvas* canvas, const gfx::Rect& rect); | 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); |
36 | 42 |
37 // Called from WebMediaPlayerDelegateImpl from WebKit's main thread. | 43 // TODO(ralphl): What is this rect? Is it relative to the canvas? |
38 void SetRect(const gfx::Rect& rect); | 44 virtual void Paint(skia::PlatformCanvas* canvas, const gfx::Rect& rect); |
39 | 45 |
40 // Static method for creating factory for this object. | 46 // Static method for creating factory for this object. |
41 static media::FilterFactory* CreateFactory( | 47 static media::FilterFactory* CreateFactory( |
42 WebMediaPlayerDelegateImpl* delegate) { | 48 WebMediaPlayerDelegateImpl* delegate) { |
43 return new media::FilterFactoryImpl1< | 49 return new media::FilterFactoryImpl1< |
44 VideoRendererImpl, WebMediaPlayerDelegateImpl*>(delegate); | 50 VideoRendererImpl, WebMediaPlayerDelegateImpl*>(delegate); |
45 } | 51 } |
46 | 52 |
| 53 // Implementation of AssignableBuffer<this>::OnAssignment method. |
| 54 void OnAssignment(media::VideoFrame* video_frame); |
| 55 |
| 56 private: |
| 57 friend class SubmitReadsTask; |
| 58 friend class media::FilterFactoryImpl1<VideoRendererImpl, |
| 59 WebMediaPlayerDelegateImpl*>; |
| 60 |
| 61 // Constructor and destructor are private. Only the filter factory is |
| 62 // allowed to create instances. |
| 63 explicit VideoRendererImpl(WebMediaPlayerDelegateImpl* delegate); |
| 64 virtual ~VideoRendererImpl(); |
| 65 |
47 // Answers question from the factory to see if we accept |format|. | 66 // Answers question from the factory to see if we accept |format|. |
48 static bool IsMediaFormatSupported(const media::MediaFormat* format); | 67 static bool IsMediaFormatSupported(const media::MediaFormat* format); |
49 | 68 |
50 protected: | 69 // Used by the IsMediaFormatSupported and Initialize methods. Examines the |
51 virtual ~VideoRendererImpl(); | 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); |
52 | 75 |
53 private: | 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 |
| 116 // 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. |
| 118 void CopyToCurrentFrame(media::VideoFrame* video_frame); |
| 119 |
| 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. |
54 WebMediaPlayerDelegateImpl* delegate_; | 144 WebMediaPlayerDelegateImpl* delegate_; |
55 | 145 |
| 146 // Pointer to the decoder that will feed us with video frames. |
| 147 scoped_refptr<media::VideoDecoder> decoder_; |
| 148 |
| 149 // If non-NULL then a task has been scheduled to submit read requests to the |
| 150 // video decoder. |
| 151 CancelableTask* submit_reads_task_; |
| 152 |
| 153 // The number of buffers we need to request. This member is updated by any |
| 154 // method that removes frames from the queue, such as UpdateQueue and |
| 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 |
56 DISALLOW_COPY_AND_ASSIGN(VideoRendererImpl); | 179 DISALLOW_COPY_AND_ASSIGN(VideoRendererImpl); |
57 }; | 180 }; |
58 | 181 |
59 #endif // CHROME_RENDERER_MEDIA_VIDEO_RENDERER_H_ | 182 #endif // CHROME_RENDERER_MEDIA_VIDEO_RENDERER_IMPL_H_ |
60 | |
OLD | NEW |