OLD | NEW |
1 // Copyright (c) 2008-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008-2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be found | 2 // Use of this source code is governed by a BSD-style license that can be found |
3 // in the LICENSE file. | 3 // in the LICENSE file. |
4 // | 4 // |
5 // Delegate calls from WebCore::MediaPlayerPrivate to Chrome's video player. | 5 // Delegate calls from WebCore::MediaPlayerPrivate to Chrome's video player. |
6 // It contains PipelineImpl which is the actual media player pipeline, it glues | 6 // It contains PipelineImpl which is the actual media player pipeline, it glues |
7 // the media player pipeline, data source, audio renderer and renderer. | 7 // the media player pipeline, data source, audio renderer and renderer. |
8 // PipelineImpl would creates multiple threads and access some public methods | 8 // PipelineImpl would creates multiple threads and access some public methods |
9 // of this class, so we need to be extra careful about concurrent access of | 9 // of this class, so we need to be extra careful about concurrent access of |
10 // methods and members. | 10 // methods and members. |
11 // | 11 // |
12 // Properties that are shared by main thread and media threads: | 12 // WebMediaPlayerImpl works with multiple objects, the most important ones are: |
13 // CancelableTaskList tasks_; | |
14 // ^--- This property is shared for keeping records of the tasks posted to | |
15 // make sure there will be only one task for each task type that can | |
16 // exist in the main thread. | |
17 // | 13 // |
18 // Methods that are accessed in media threads: | 14 // media::PipelineImpl |
19 // SetAudioRenderer() | 15 // The media playback pipeline. |
20 // ^--- Called during the initialization of the pipeline, essentially from the | |
21 // the pipeline thread. | |
22 // SetVideoRenderer() | |
23 // ^--- Called during the initialization of the pipeline, essentially from the | |
24 // the pipeline thread. | |
25 // PostRepaintTask() | |
26 // ^--- Called from the video renderer thread to notify a video frame has | |
27 // been prepared. | |
28 // PostTask() | |
29 // ^--- A method that helps posting tasks to the main thread, it is | |
30 // accessed from main thread and media threads, it access the |tasks_| | |
31 // internally. Needs locking inside to avoid concurrent access to | |
32 // |tasks_|. | |
33 // | 16 // |
| 17 // VideoRendererImpl |
| 18 // Video renderer object. |
| 19 // |
| 20 // WebMediaPlayerImpl::Proxy |
| 21 // Proxies methods calls from the media pipeline to WebKit. |
| 22 // |
| 23 // WebKit::WebMediaPlayerClient |
| 24 // WebKit client of this media player object. |
| 25 // |
| 26 // The following diagram shows the relationship of these objects: |
| 27 // (note: ref-counted reference is marked by a "r".) |
| 28 // |
| 29 // WebMediaPlayerImpl ------> PipelineImpl |
| 30 // | ^ | r |
| 31 // | | v |
| 32 // | | VideoRendererImpl |
| 33 // | | ^ r |
| 34 // | | | |
| 35 // | r | r | |
| 36 // .------> Proxy <-----. |
| 37 // | |
| 38 // | |
| 39 // v |
| 40 // WebMediaPlayerClient |
| 41 // |
| 42 // Notice that Proxy and VideoRendererImpl are referencing each other. This |
| 43 // interdependency has to be treated carefully. |
34 // | 44 // |
35 // Other issues: | 45 // Other issues: |
36 // During tear down of the whole browser or a tab, the DOM tree may not be | 46 // During tear down of the whole browser or a tab, the DOM tree may not be |
37 // destructed nicely, and there will be some dangling media threads trying to | 47 // destructed nicely, and there will be some dangling media threads trying to |
38 // the main thread, so we need this class to listen to destruction event of the | 48 // the main thread, so we need this class to listen to destruction event of the |
39 // main thread and cleanup the media threads when the even is received. Also | 49 // main thread and cleanup the media threads when the even is received. Also |
40 // at destruction of this class we will need to unhook it from destruction event | 50 // at destruction of this class we will need to unhook it from destruction event |
41 // list of the main thread. | 51 // list of the main thread. |
42 | 52 |
43 #ifndef WEBKIT_GLUE_WEBMEDIAPLAYER_IMPL_H_ | 53 #ifndef WEBKIT_GLUE_WEBMEDIAPLAYER_IMPL_H_ |
44 #define WEBKTI_GLUE_WEBMEDIAPLAYER_IMPL_H_ | 54 #define WEBKIT_GLUE_WEBMEDIAPLAYER_IMPL_H_ |
45 | 55 |
46 #include <vector> | 56 #include <vector> |
47 | 57 |
48 #include "base/gfx/platform_canvas.h" | 58 #include "base/gfx/platform_canvas.h" |
| 59 #include "base/gfx/rect.h" |
| 60 #include "base/gfx/size.h" |
49 #include "base/lock.h" | 61 #include "base/lock.h" |
50 #include "base/message_loop.h" | 62 #include "base/message_loop.h" |
| 63 #include "base/ref_counted.h" |
51 #include "media/base/filters.h" | 64 #include "media/base/filters.h" |
52 #include "media/base/pipeline_impl.h" | 65 #include "media/base/pipeline_impl.h" |
53 #include "webkit/api/public/WebMediaPlayer.h" | 66 #include "webkit/api/public/WebMediaPlayer.h" |
54 #include "webkit/api/public/WebMediaPlayerClient.h" | 67 #include "webkit/api/public/WebMediaPlayerClient.h" |
55 | 68 |
56 class AudioRendererImpl; | |
57 class DataSourceImpl; | |
58 class GURL; | 69 class GURL; |
59 class RenderView; | |
60 class VideoRendererImpl; | |
61 | 70 |
62 namespace media { | 71 namespace media { |
63 class FilterFactoryCollection; | 72 class FilterFactoryCollection; |
64 } | 73 } |
65 | 74 |
66 namespace webkit_glue { | 75 namespace webkit_glue { |
67 | 76 |
68 // This typedef is used for WebMediaPlayerImpl::PostTask() and | 77 class VideoRendererImpl; |
69 // NotifyWebMediaPlayerTask in the source file. | |
70 typedef void (WebKit::WebMediaPlayerClient::*WebMediaPlayerClientMethod)(); | |
71 | 78 |
72 class WebMediaPlayerImpl : public WebKit::WebMediaPlayer, | 79 class WebMediaPlayerImpl : public WebKit::WebMediaPlayer, |
73 public MessageLoop::DestructionObserver { | 80 public MessageLoop::DestructionObserver { |
74 public: | 81 public: |
| 82 // A proxy class that dispatches method calls from the media pipeline to |
| 83 // WebKit. Since there are multiple threads in the media pipeline and there's |
| 84 // need for the media pipeline to call to WebKit, e.g. repaint requests, |
| 85 // initialization events, etc, we have this class to bridge all method calls |
| 86 // from the media pipeline on different threads and serialize these calls |
| 87 // on the render thread. |
| 88 // Because of the nature of this object that it works with different threads, |
| 89 // it is made ref-counted. |
| 90 class Proxy : public base::RefCountedThreadSafe<Proxy> { |
| 91 public: |
| 92 Proxy(MessageLoop* render_loop, |
| 93 WebMediaPlayerImpl* webmediaplayer); |
| 94 virtual ~Proxy(); |
| 95 |
| 96 // Fire a repaint event to WebKit. |
| 97 void Repaint(); |
| 98 |
| 99 // Report to WebKit that time has changed. |
| 100 void TimeChanged(); |
| 101 |
| 102 // Report to WebKit that network state has changed. |
| 103 void NetworkStateChanged(WebKit::WebMediaPlayer::NetworkState state); |
| 104 |
| 105 // Report the WebKit that ready state has changed. |
| 106 void ReadyStateChanged(WebKit::WebMediaPlayer::ReadyState state); |
| 107 |
| 108 // Public methods to be called from video renderer. |
| 109 void SetVideoRenderer(VideoRendererImpl* video_renderer); |
| 110 |
| 111 private: |
| 112 friend class WebMediaPlayerImpl; |
| 113 |
| 114 // Invoke |webmediaplayer_| to perform a repaint. |
| 115 void RepaintTask(); |
| 116 |
| 117 // Invoke |webmediaplayer_| to notify a time change event. |
| 118 void TimeChangedTask(); |
| 119 |
| 120 // Saves the internal network state and notify WebKit to pick up the change. |
| 121 void NetworkStateChangedTask(WebKit::WebMediaPlayer::NetworkState state); |
| 122 |
| 123 // Saves the internal ready state and notify WebKit to pick the change. |
| 124 void ReadyStateChangedTask(WebKit::WebMediaPlayer::ReadyState state); |
| 125 |
| 126 void Paint(skia::PlatformCanvas* canvas, const gfx::Rect& dest_rect); |
| 127 |
| 128 void SetSize(const gfx::Rect& rect); |
| 129 |
| 130 // Detach from |webmediaplayer_|. |
| 131 void Detach(); |
| 132 |
| 133 void PipelineInitializationCallback(bool success); |
| 134 |
| 135 void PipelineSeekCallback(bool success); |
| 136 |
| 137 // The render message loop where WebKit lives. |
| 138 MessageLoop* render_loop_; |
| 139 WebMediaPlayerImpl* webmediaplayer_; |
| 140 scoped_refptr<VideoRendererImpl> video_renderer_; |
| 141 |
| 142 Lock lock_; |
| 143 int outstanding_repaints_; |
| 144 }; |
| 145 |
75 // Construct a WebMediaPlayerImpl with reference to the client, and media | 146 // Construct a WebMediaPlayerImpl with reference to the client, and media |
76 // filter factory collection. By providing the filter factory collection | 147 // filter factory collection. By providing the filter factory collection |
77 // the implementor can provide more specific media filters that does resource | 148 // the implementor can provide more specific media filters that does resource |
78 // loading and rendering. |factory| should contain filter factories for: | 149 // loading and rendering. |factory| should contain filter factories for: |
79 // 1. Data source | 150 // 1. Data source |
80 // 2. Audio renderer | 151 // 2. Audio renderer |
81 // 3. Video renderer (optional) | 152 // 3. Video renderer (optional) |
82 // | 153 // |
83 // There are some default filters provided by this method: | 154 // There are some default filters provided by this method: |
84 // 1. FFmpeg demuxer | 155 // 1. FFmpeg demuxer |
(...skipping 10 matching lines...) Expand all Loading... |
95 WebMediaPlayerImpl(WebKit::WebMediaPlayerClient* client, | 166 WebMediaPlayerImpl(WebKit::WebMediaPlayerClient* client, |
96 media::FilterFactoryCollection* factory); | 167 media::FilterFactoryCollection* factory); |
97 virtual ~WebMediaPlayerImpl(); | 168 virtual ~WebMediaPlayerImpl(); |
98 | 169 |
99 virtual void load(const WebKit::WebURL& url); | 170 virtual void load(const WebKit::WebURL& url); |
100 virtual void cancelLoad(); | 171 virtual void cancelLoad(); |
101 | 172 |
102 // Playback controls. | 173 // Playback controls. |
103 virtual void play(); | 174 virtual void play(); |
104 virtual void pause(); | 175 virtual void pause(); |
105 virtual void stop(); | |
106 virtual void seek(float seconds); | 176 virtual void seek(float seconds); |
107 virtual void setEndTime(float seconds); | 177 virtual void setEndTime(float seconds); |
108 virtual void setRate(float rate); | 178 virtual void setRate(float rate); |
109 virtual void setVolume(float volume); | 179 virtual void setVolume(float volume); |
110 virtual void setVisible(bool visible); | 180 virtual void setVisible(bool visible); |
111 virtual bool setAutoBuffer(bool autoBuffer); | 181 virtual bool setAutoBuffer(bool autoBuffer); |
112 virtual bool totalBytesKnown(); | 182 virtual bool totalBytesKnown(); |
113 virtual float maxTimeBuffered() const; | 183 virtual float maxTimeBuffered() const; |
114 virtual float maxTimeSeekable() const; | 184 virtual float maxTimeSeekable() const; |
115 | 185 |
(...skipping 29 matching lines...) Expand all Loading... |
145 | 215 |
146 virtual unsigned long long bytesLoaded() const; | 216 virtual unsigned long long bytesLoaded() const; |
147 virtual unsigned long long totalBytes() const; | 217 virtual unsigned long long totalBytes() const; |
148 | 218 |
149 // As we are closing the tab or even the browser, |main_loop_| is destroyed | 219 // As we are closing the tab or even the browser, |main_loop_| is destroyed |
150 // even before this object gets destructed, so we need to know when | 220 // even before this object gets destructed, so we need to know when |
151 // |main_loop_| is being destroyed and we can stop posting repaint task | 221 // |main_loop_| is being destroyed and we can stop posting repaint task |
152 // to it. | 222 // to it. |
153 virtual void WillDestroyCurrentMessageLoop(); | 223 virtual void WillDestroyCurrentMessageLoop(); |
154 | 224 |
155 // Notification from |pipeline_| when initialization has finished. | 225 void Repaint(); |
156 void OnPipelineInitialize(bool successful); | |
157 | 226 |
158 // Notification from |pipeline_| when a seek has finished. | 227 void TimeChanged(); |
159 void OnPipelineSeek(bool successful); | |
160 | 228 |
161 // Called from tasks posted to |main_loop_| from this object to remove | 229 void SetNetworkState(WebKit::WebMediaPlayer::NetworkState state); |
162 // reference of them. | |
163 void DidTask(CancelableTask* task); | |
164 | 230 |
165 // Public methods to be called from renderers and data source so that | 231 void SetReadyState(WebKit::WebMediaPlayer::ReadyState state); |
166 // WebMediaPlayerImpl has references to them. | |
167 void SetVideoRenderer(VideoRendererImpl* video_renderer); | |
168 | |
169 // Called from VideoRenderer to fire a repaint task to |main_loop_|. | |
170 void PostRepaintTask(); | |
171 | |
172 // Inline getters. | |
173 WebKit::WebMediaPlayerClient* client() { return client_; } | |
174 | 232 |
175 private: | 233 private: |
176 // Methods for posting tasks and cancelling tasks. This method may lives in | 234 // Destroy resources held. |
177 // the main thread or the media threads. | 235 void Destroy(); |
178 void PostTask(int index, WebMediaPlayerClientMethod method); | |
179 | 236 |
180 // Cancel all tasks currently live in |main_loop_|. | 237 // Getter method to |client_|. |
181 void CancelAllTasks(); | 238 WebKit::WebMediaPlayerClient* GetClient(); |
182 | |
183 // Indexes for tasks. | |
184 enum { | |
185 kRepaintTaskIndex = 0, | |
186 kReadyStateTaskIndex, | |
187 kNetworkStateTaskIndex, | |
188 kTimeChangedTaskIndex, | |
189 kLastTaskIndex | |
190 }; | |
191 | 239 |
192 // TODO(hclam): get rid of these members and read from the pipeline directly. | 240 // TODO(hclam): get rid of these members and read from the pipeline directly. |
193 WebKit::WebMediaPlayer::NetworkState network_state_; | 241 WebKit::WebMediaPlayer::NetworkState network_state_; |
194 WebKit::WebMediaPlayer::ReadyState ready_state_; | 242 WebKit::WebMediaPlayer::ReadyState ready_state_; |
195 | 243 |
196 // Message loops for posting tasks between Chrome's main thread. Also used | 244 // Message loops for posting tasks between Chrome's main thread. Also used |
197 // for DCHECKs so methods calls won't execute in the wrong thread. | 245 // for DCHECKs so methods calls won't execute in the wrong thread. |
198 MessageLoop* main_loop_; | 246 MessageLoop* main_loop_; |
199 | 247 |
200 // A collection of factories for creating filters. | 248 // A collection of factories for creating filters. |
201 scoped_refptr<media::FilterFactoryCollection> filter_factory_; | 249 scoped_refptr<media::FilterFactoryCollection> filter_factory_; |
202 | 250 |
203 // The actual pipeline. We do it a composition here because we expect to have | 251 // The actual pipeline. We do it a composition here because we expect to have |
204 // the same lifetime as the pipeline. | 252 // the same lifetime as the pipeline. |
205 media::PipelineImpl pipeline_; | 253 media::PipelineImpl pipeline_; |
206 | 254 |
207 // We have the interface to VideoRenderer to delegate paint messages to it | |
208 // from WebKit. | |
209 scoped_refptr<VideoRendererImpl> video_renderer_; | |
210 | |
211 WebKit::WebMediaPlayerClient* client_; | 255 WebKit::WebMediaPlayerClient* client_; |
212 | 256 |
213 // List of tasks for holding pointers to all tasks currently in the | 257 scoped_refptr<Proxy> proxy_; |
214 // |main_loop_|. |tasks_| can be access from main thread or the media threads | |
215 // we need a lock for protecting it. | |
216 Lock task_lock_; | |
217 typedef std::vector<CancelableTask*> CancelableTaskList; | |
218 CancelableTaskList tasks_; | |
219 | 258 |
220 DISALLOW_COPY_AND_ASSIGN(WebMediaPlayerImpl); | 259 DISALLOW_COPY_AND_ASSIGN(WebMediaPlayerImpl); |
221 }; | 260 }; |
222 | 261 |
223 } // namespace webkit_glue | 262 } // namespace webkit_glue |
224 | 263 |
225 #endif // WEBKIT_GLUE_WEBMEDIAPLAYER_IMPL_H_ | 264 #endif // WEBKIT_GLUE_WEBMEDIAPLAYER_IMPL_H_ |
OLD | NEW |