OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found 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 // WebMediaPlayerImpl works with multiple objects, the most important ones are: | 12 // WebMediaPlayerImpl works with multiple objects, the most important ones are: |
13 // | 13 // |
14 // media::PipelineImpl | 14 // media::PipelineImpl |
15 // The media playback pipeline. | 15 // The media playback pipeline. |
16 // | 16 // |
17 // WebVideoRenderer | 17 // WebVideoRenderer |
18 // Video renderer object. | 18 // Video renderer object. |
19 // | 19 // |
20 // WebMediaPlayerImpl::Proxy | |
21 // Proxies methods calls from the media pipeline to WebKit. | |
22 // | |
23 // WebKit::WebMediaPlayerClient | 20 // WebKit::WebMediaPlayerClient |
24 // WebKit client of this media player object. | 21 // WebKit client of this media player object. |
25 // | 22 // |
26 // The following diagram shows the relationship of these objects: | 23 // The following diagram shows the relationship of these objects: |
27 // (note: ref-counted reference is marked by a "r".) | 24 // (note: ref-counted reference is marked by a "r".) |
28 // | 25 // |
29 // WebMediaPlayerImpl ------> PipelineImpl | 26 // WebMediaPlayerClient (WebKit object) |
30 // | ^ | r | 27 // ^ |
31 // | | v | |
32 // | | WebVideoRenderer | |
33 // | | ^ r | |
34 // | | | | |
35 // | r | r | | |
36 // .------> Proxy <-----. | |
37 // | | 28 // | |
38 // | | 29 // WebMediaPlayerImpl ---> PipelineImpl |
39 // v | 30 // | ^ | |
40 // WebMediaPlayerClient | 31 // | | v r |
| 32 // | | WebVideoRenderer |
| 33 // | | | ^ r |
| 34 // | r | v r | |
| 35 // '---> WebMediaPlayerProxy --' |
41 // | 36 // |
42 // Notice that Proxy and WebVideoRenderer are referencing each other. This | 37 // Notice that WebMediaPlayerProxy and WebVideoRenderer are referencing each |
43 // interdependency has to be treated carefully. | 38 // other. This interdependency has to be treated carefully. |
44 // | 39 // |
45 // Other issues: | 40 // Other issues: |
46 // During tear down of the whole browser or a tab, the DOM tree may not be | 41 // During tear down of the whole browser or a tab, the DOM tree may not be |
47 // destructed nicely, and there will be some dangling media threads trying to | 42 // destructed nicely, and there will be some dangling media threads trying to |
48 // the main thread, so we need this class to listen to destruction event of the | 43 // the main thread, so we need this class to listen to destruction event of the |
49 // main thread and cleanup the media threads when the even is received. Also | 44 // main thread and cleanup the media threads when the even is received. Also |
50 // at destruction of this class we will need to unhook it from destruction event | 45 // at destruction of this class we will need to unhook it from destruction event |
51 // list of the main thread. | 46 // list of the main thread. |
52 | 47 |
53 #ifndef WEBKIT_GLUE_WEBMEDIAPLAYER_IMPL_H_ | 48 #ifndef WEBKIT_GLUE_WEBMEDIAPLAYER_IMPL_H_ |
54 #define WEBKIT_GLUE_WEBMEDIAPLAYER_IMPL_H_ | 49 #define WEBKIT_GLUE_WEBMEDIAPLAYER_IMPL_H_ |
55 | 50 |
56 #include "base/memory/ref_counted.h" | 51 #include "base/memory/ref_counted.h" |
57 #include "base/memory/scoped_ptr.h" | 52 #include "base/memory/scoped_ptr.h" |
58 #include "base/message_loop.h" | 53 #include "base/message_loop.h" |
59 #include "base/threading/thread.h" | |
60 #include "base/synchronization/lock.h" | |
61 #include "base/synchronization/waitable_event.h" | |
62 #include "media/filters/chunk_demuxer.h" | |
63 #include "media/filters/chunk_demuxer_client.h" | |
64 #include "media/base/filters.h" | 54 #include "media/base/filters.h" |
65 #include "media/base/message_loop_factory.h" | 55 #include "media/base/message_loop_factory.h" |
66 #include "media/base/pipeline.h" | 56 #include "media/base/pipeline.h" |
67 #include "skia/ext/platform_canvas.h" | 57 #include "skia/ext/platform_canvas.h" |
68 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaPlayer.h" | 58 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaPlayer.h" |
69 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaPlayerClient.
h" | 59 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaPlayerClient.
h" |
70 #include "ui/gfx/rect.h" | |
71 #include "ui/gfx/size.h" | |
72 #include "webkit/glue/media/web_data_source.h" | |
73 | 60 |
74 class GURL; | 61 class GURL; |
75 | 62 |
76 namespace WebKit { | 63 namespace WebKit { |
77 class WebFrame; | 64 class WebFrame; |
78 } | 65 } |
79 | 66 |
80 namespace media { | 67 namespace media { |
81 class MediaLog; | 68 class MediaLog; |
82 } | 69 } |
83 | 70 |
84 namespace webkit_glue { | 71 namespace webkit_glue { |
85 | 72 |
86 class MediaResourceLoaderBridgeFactory; | 73 class MediaResourceLoaderBridgeFactory; |
87 class MediaStreamClient; | 74 class MediaStreamClient; |
| 75 class WebMediaPlayerProxy; |
88 class WebVideoRenderer; | 76 class WebVideoRenderer; |
89 | 77 |
90 class WebMediaPlayerImpl | 78 class WebMediaPlayerImpl |
91 : public WebKit::WebMediaPlayer, | 79 : public WebKit::WebMediaPlayer, |
92 public MessageLoop::DestructionObserver { | 80 public MessageLoop::DestructionObserver { |
93 public: | 81 public: |
94 // A proxy class that dispatches method calls from the media pipeline to | |
95 // WebKit. Since there are multiple threads in the media pipeline and there's | |
96 // need for the media pipeline to call to WebKit, e.g. repaint requests, | |
97 // initialization events, etc, we have this class to bridge all method calls | |
98 // from the media pipeline on different threads and serialize these calls | |
99 // on the render thread. | |
100 // Because of the nature of this object that it works with different threads, | |
101 // it is made ref-counted. | |
102 class Proxy | |
103 : public base::RefCountedThreadSafe<Proxy>, | |
104 public media::ChunkDemuxerClient { | |
105 public: | |
106 Proxy(MessageLoop* render_loop, | |
107 WebMediaPlayerImpl* webmediaplayer); | |
108 | |
109 // Methods for Filter -> WebMediaPlayerImpl communication. | |
110 void Repaint(); | |
111 void SetVideoRenderer(scoped_refptr<WebVideoRenderer> video_renderer); | |
112 WebDataSourceBuildObserverHack* GetBuildObserver(); | |
113 | |
114 // Methods for WebMediaPlayerImpl -> Filter communication. | |
115 void Paint(SkCanvas* canvas, const gfx::Rect& dest_rect); | |
116 void SetSize(const gfx::Rect& rect); | |
117 void Detach(); | |
118 void GetCurrentFrame(scoped_refptr<media::VideoFrame>* frame_out); | |
119 void PutCurrentFrame(scoped_refptr<media::VideoFrame> frame); | |
120 bool HasSingleOrigin(); | |
121 void AbortDataSources(); | |
122 | |
123 // Methods for PipelineImpl -> WebMediaPlayerImpl communication. | |
124 void PipelineInitializationCallback(media::PipelineStatus status); | |
125 void PipelineSeekCallback(media::PipelineStatus status); | |
126 void PipelineEndedCallback(media::PipelineStatus status); | |
127 void PipelineErrorCallback(media::PipelineStatus error); | |
128 void NetworkEventCallback(media::PipelineStatus status); | |
129 | |
130 // Methods for ChunkDemuxerClient interface. | |
131 virtual void DemuxerOpened(media::ChunkDemuxer* demuxer); | |
132 virtual void DemuxerClosed(); | |
133 | |
134 // Methods for Demuxer communication. | |
135 void DemuxerFlush(); | |
136 bool DemuxerAppend(const uint8* data, size_t length); | |
137 void DemuxerEndOfStream(media::PipelineStatus status); | |
138 void DemuxerShutdown(); | |
139 | |
140 void DemuxerOpenedTask(const scoped_refptr<media::ChunkDemuxer>& demuxer); | |
141 void DemuxerClosedTask(); | |
142 | |
143 // Returns the message loop used by the proxy. | |
144 MessageLoop* message_loop() { return render_loop_; } | |
145 | |
146 private: | |
147 friend class base::RefCountedThreadSafe<Proxy>; | |
148 | |
149 virtual ~Proxy(); | |
150 | |
151 // Adds a data source to data_sources_. | |
152 void AddDataSource(WebDataSource* data_source); | |
153 | |
154 // Invoke |webmediaplayer_| to perform a repaint. | |
155 void RepaintTask(); | |
156 | |
157 // Notify |webmediaplayer_| that initialization has finished. | |
158 void PipelineInitializationTask(media::PipelineStatus status); | |
159 | |
160 // Notify |webmediaplayer_| that a seek has finished. | |
161 void PipelineSeekTask(media::PipelineStatus status); | |
162 | |
163 // Notify |webmediaplayer_| that the media has ended. | |
164 void PipelineEndedTask(media::PipelineStatus status); | |
165 | |
166 // Notify |webmediaplayer_| that a pipeline error has occurred during | |
167 // playback. | |
168 void PipelineErrorTask(media::PipelineStatus error); | |
169 | |
170 // Notify |webmediaplayer_| that there's a network event. | |
171 void NetworkEventTask(media::PipelineStatus status); | |
172 | |
173 // The render message loop where WebKit lives. | |
174 MessageLoop* render_loop_; | |
175 WebMediaPlayerImpl* webmediaplayer_; | |
176 | |
177 base::Lock data_sources_lock_; | |
178 typedef std::list<scoped_refptr<WebDataSource> > DataSourceList; | |
179 DataSourceList data_sources_; | |
180 scoped_ptr<WebDataSourceBuildObserverHack> build_observer_; | |
181 | |
182 scoped_refptr<WebVideoRenderer> video_renderer_; | |
183 | |
184 base::Lock lock_; | |
185 int outstanding_repaints_; | |
186 | |
187 scoped_refptr<media::ChunkDemuxer> chunk_demuxer_; | |
188 }; | |
189 | |
190 // Construct a WebMediaPlayerImpl with reference to the client, and media | 82 // Construct a WebMediaPlayerImpl with reference to the client, and media |
191 // filter collection. By providing the filter collection the implementor can | 83 // filter collection. By providing the filter collection the implementor can |
192 // provide more specific media filters that does resource loading and | 84 // provide more specific media filters that does resource loading and |
193 // rendering. |collection| should contain filter factories for: | 85 // rendering. |collection| should contain filter factories for: |
194 // 1. Data source | 86 // 1. Data source |
195 // 2. Audio renderer | 87 // 2. Audio renderer |
196 // 3. Video renderer (optional) | 88 // 3. Video renderer (optional) |
197 // | 89 // |
198 // There are some default filters provided by this method: | 90 // There are some default filters provided by this method: |
199 // 1. FFmpeg demuxer | 91 // 1. FFmpeg demuxer |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 | 182 |
291 // As we are closing the tab or even the browser, |main_loop_| is destroyed | 183 // As we are closing the tab or even the browser, |main_loop_| is destroyed |
292 // even before this object gets destructed, so we need to know when | 184 // even before this object gets destructed, so we need to know when |
293 // |main_loop_| is being destroyed and we can stop posting repaint task | 185 // |main_loop_| is being destroyed and we can stop posting repaint task |
294 // to it. | 186 // to it. |
295 virtual void WillDestroyCurrentMessageLoop(); | 187 virtual void WillDestroyCurrentMessageLoop(); |
296 | 188 |
297 void Repaint(); | 189 void Repaint(); |
298 | 190 |
299 void OnPipelineInitialize(media::PipelineStatus status); | 191 void OnPipelineInitialize(media::PipelineStatus status); |
300 | |
301 void OnPipelineSeek(media::PipelineStatus status); | 192 void OnPipelineSeek(media::PipelineStatus status); |
302 | |
303 void OnPipelineEnded(media::PipelineStatus status); | 193 void OnPipelineEnded(media::PipelineStatus status); |
304 | |
305 void OnPipelineError(media::PipelineStatus error); | 194 void OnPipelineError(media::PipelineStatus error); |
306 | |
307 void OnNetworkEvent(media::PipelineStatus status); | 195 void OnNetworkEvent(media::PipelineStatus status); |
308 | |
309 void OnDemuxerOpened(); | 196 void OnDemuxerOpened(); |
310 | 197 |
311 private: | 198 private: |
312 // Helpers that set the network/ready state and notifies the client if | 199 // Helpers that set the network/ready state and notifies the client if |
313 // they've changed. | 200 // they've changed. |
314 void SetNetworkState(WebKit::WebMediaPlayer::NetworkState state); | 201 void SetNetworkState(WebKit::WebMediaPlayer::NetworkState state); |
315 void SetReadyState(WebKit::WebMediaPlayer::ReadyState state); | 202 void SetReadyState(WebKit::WebMediaPlayer::ReadyState state); |
316 | 203 |
317 // Destroy resources held. | 204 // Destroy resources held. |
318 void Destroy(); | 205 void Destroy(); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 // time we pause and then return that value in currentTime(). Otherwise our | 238 // time we pause and then return that value in currentTime(). Otherwise our |
352 // clock can creep forward a little bit while the asynchronous | 239 // clock can creep forward a little bit while the asynchronous |
353 // SetPlaybackRate(0) is being executed. | 240 // SetPlaybackRate(0) is being executed. |
354 bool paused_; | 241 bool paused_; |
355 bool seeking_; | 242 bool seeking_; |
356 float playback_rate_; | 243 float playback_rate_; |
357 base::TimeDelta paused_time_; | 244 base::TimeDelta paused_time_; |
358 | 245 |
359 WebKit::WebMediaPlayerClient* client_; | 246 WebKit::WebMediaPlayerClient* client_; |
360 | 247 |
361 scoped_refptr<Proxy> proxy_; | 248 scoped_refptr<WebMediaPlayerProxy> proxy_; |
362 | 249 |
363 MediaStreamClient* media_stream_client_; | 250 MediaStreamClient* media_stream_client_; |
364 | 251 |
365 #if WEBKIT_USING_CG | 252 #if WEBKIT_USING_CG |
366 scoped_ptr<skia::PlatformCanvas> skia_canvas_; | 253 scoped_ptr<skia::PlatformCanvas> skia_canvas_; |
367 #endif | 254 #endif |
368 | 255 |
369 scoped_refptr<media::MediaLog> media_log_; | 256 scoped_refptr<media::MediaLog> media_log_; |
370 | 257 |
371 DISALLOW_COPY_AND_ASSIGN(WebMediaPlayerImpl); | 258 DISALLOW_COPY_AND_ASSIGN(WebMediaPlayerImpl); |
372 }; | 259 }; |
373 | 260 |
374 } // namespace webkit_glue | 261 } // namespace webkit_glue |
375 | 262 |
376 #endif // WEBKIT_GLUE_WEBMEDIAPLAYER_IMPL_H_ | 263 #endif // WEBKIT_GLUE_WEBMEDIAPLAYER_IMPL_H_ |
OLD | NEW |