OLD | NEW |
| (Empty) |
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 | |
3 // LICENSE file. | |
4 // | |
5 // A Chrome specific data source for video stack pipeline. The actual resource | |
6 // loading would happen in the browser process. This class is given a file | |
7 // handle and will ask for progress of downloading from RenderView which | |
8 // delegates requests to browser process through IPC. Asynchronous IO will be | |
9 // performed on the file handle. | |
10 // | |
11 // This class is access by 4 different threads during it's lifetime, namely: | |
12 // 1. Render thread | |
13 // Thread that runs WebKit objects and construct this class. Updates about | |
14 // progress for resource loading also happens in this thread. | |
15 // 2. Pipeline thread | |
16 // Closing thread of the video stack pipeline, it initialized this class | |
17 // and performs stopping in an orderly fashion. | |
18 // 3. Demuxer thread | |
19 // Thread created by the pipeline and ask for data from this class. | |
20 // media::DataSource methods are called from this thread. | |
21 // 4. IO thread | |
22 // Performs file stream construction and callback of read completion also | |
23 // comes from this thread. | |
24 // | |
25 // Methods in the class categorized by the thread(s) they are running on: | |
26 // | |
27 // Render thread | |
28 // +-- DataSourceImpl() | |
29 // | Perform construction of this class. | |
30 // |-- static CreateFactory() | |
31 // | Called during construction of this class. | |
32 // |-- OnInitialize() | |
33 // | Task posted by Initialize() to kick start resource loading. | |
34 // |-- OnCancel() | |
35 // | Cancel the resource loading. | |
36 // |-- OnDownloadProgress() | |
37 // | Receives download progress information for the response data file. | |
38 // |-- OnUploadProgress() | |
39 // |-- OnReceivedRedirect() | |
40 // |-- OnReceivedResponse() | |
41 // |-- OnReceivedData() | |
42 // |-- OnCompletedRequest() | |
43 // |-- GetURLForDebugging() | |
44 // \-- OnDestroy() | |
45 // | |
46 // Pipeline thread | |
47 // +-- Initialize() | |
48 // | Performs initialization of data source in the pipeline. | |
49 // \-- Stop() | |
50 // Cease all activities during pipeline tear down. | |
51 // | |
52 // Demuxer thread | |
53 // +-- Read() | |
54 // | Called to read from the media file. | |
55 // |-- GetPosition() | |
56 // | Called to obtain current position in the file. | |
57 // |-- SetPosition() | |
58 // | Performs a seek operation. | |
59 // |-- GetSize() | |
60 // | Retrieve the size of the resource. | |
61 // \-- IsSeekable() | |
62 // Returns true if URL is file:/// or else false. | |
63 // | |
64 // IO thread | |
65 // +-- OnCreateFileStream() | |
66 // | Callback for construction of net::FileStream in an IO message loop. | |
67 // |-- OnReadFileStream() | |
68 // | Actual read operation on FileStream performs here. | |
69 // |-- OnSeekFileStream() | |
70 // | Actual seek operation happens here. | |
71 // \-- OnDidFileStreamRead() | |
72 // Callback for asynchronous file read completion. | |
73 | |
74 | |
75 #ifndef CHROME_RENDERER_MEDIA_DATA_SOURCE_IMPL_H_ | |
76 #define CHROME_RENDERER_MEDIA_DATA_SOURCE_IMPL_H_ | |
77 | |
78 #include <string> | |
79 | |
80 #include "base/lock.h" | |
81 #include "base/platform_file.h" | |
82 #include "base/scoped_ptr.h" | |
83 #include "base/waitable_event.h" | |
84 #include "media/base/factory.h" | |
85 #include "media/base/filters.h" | |
86 #include "media/base/media_format.h" | |
87 #include "media/base/pipeline.h" | |
88 #include "net/base/completion_callback.h" | |
89 #include "net/base/file_stream.h" | |
90 #include "webkit/glue/resource_loader_bridge.h" | |
91 | |
92 class WebMediaPlayerDelegateImpl; | |
93 | |
94 class DataSourceImpl : public media::DataSource, | |
95 public webkit_glue::ResourceLoaderBridge::Peer { | |
96 public: | |
97 // Methods called from render thread ---------------------------------------- | |
98 // Static methods for creating this class. | |
99 static media::FilterFactory* CreateFactory( | |
100 WebMediaPlayerDelegateImpl* delegate) { | |
101 return new media::FilterFactoryImpl1<DataSourceImpl, | |
102 WebMediaPlayerDelegateImpl*>(delegate); | |
103 } | |
104 | |
105 // webkit_glue::ResourceLoaderBridge::Peer implementations, receive events | |
106 // for resource loading. | |
107 virtual void OnDownloadProgress(uint64 position, uint64 size); | |
108 virtual void OnUploadProgress(uint64 position, uint64 size); | |
109 virtual void OnReceivedRedirect(const GURL& new_url); | |
110 virtual void OnReceivedResponse( | |
111 const webkit_glue::ResourceLoaderBridge::ResponseInfo& info, | |
112 bool content_filtered); | |
113 virtual void OnReceivedData(const char* data, int len); | |
114 virtual void OnCompletedRequest(const URLRequestStatus& status, | |
115 const std::string& security_info); | |
116 virtual std::string GetURLForDebugging(); | |
117 | |
118 // Methods called from pipeline thread -------------------------------------- | |
119 virtual bool Initialize(const std::string& url); | |
120 // media::MediaFilter implementation. | |
121 virtual void Stop(); | |
122 | |
123 // Methods called from demuxer thread --------------------------------------- | |
124 // media::DataSource implementation. | |
125 virtual size_t Read(uint8* data, size_t size); | |
126 virtual bool GetPosition(int64* position_out); | |
127 virtual bool SetPosition(int64 position); | |
128 virtual bool GetSize(int64* size_out); | |
129 virtual bool IsSeekable(); | |
130 | |
131 const media::MediaFormat& media_format(); | |
132 | |
133 private: | |
134 friend class media::FilterFactoryImpl1<DataSourceImpl, | |
135 WebMediaPlayerDelegateImpl*>; | |
136 // Call to filter host to trigger an error, be sure not to call this method | |
137 // while the lock is acquired. | |
138 void HandleError(media::PipelineError error); | |
139 | |
140 // Methods called from render thread ---------------------------------------- | |
141 explicit DataSourceImpl(WebMediaPlayerDelegateImpl* delegate); | |
142 virtual ~DataSourceImpl(); | |
143 | |
144 // Tasks to be posted on render thread. | |
145 void OnInitialize(std::string uri); | |
146 void OnCancel(); | |
147 void OnDestroy(); | |
148 | |
149 // Methods called from IO thread -------------------------------------------- | |
150 // Handlers for file reading. | |
151 void OnCreateFileStream(base::PlatformFile file); | |
152 void OnReadFileStream(uint8* data, size_t size); | |
153 void OnSeekFileStream(net::Whence whence, int64 position); | |
154 void OnDidFileStreamRead(int size); | |
155 | |
156 media::MediaFormat media_format_; | |
157 | |
158 // Pointer to the delegate which provides access to RenderView, this is set | |
159 // in construction and can be accessed in all threads safely. | |
160 WebMediaPlayerDelegateImpl* delegate_; | |
161 | |
162 // Message loop of render thread. | |
163 MessageLoop* render_loop_; | |
164 | |
165 // A common lock for protecting members accessed by multiple threads. | |
166 Lock lock_; | |
167 bool stopped_; | |
168 | |
169 // URI to the resource being downloaded. | |
170 std::string uri_; | |
171 | |
172 // Members for keeping track of downloading progress. | |
173 base::WaitableEvent download_event_; | |
174 int64 downloaded_bytes_; | |
175 int64 total_bytes_; | |
176 bool total_bytes_known_; | |
177 bool download_completed_; | |
178 | |
179 // Members related to resource loading with RenderView. | |
180 scoped_ptr<webkit_glue::ResourceLoaderBridge> resource_loader_bridge_; | |
181 | |
182 // Members used for reading. | |
183 base::WaitableEvent read_event_; | |
184 net::CompletionCallbackImpl<DataSourceImpl> read_callback_; | |
185 scoped_ptr<net::FileStream> stream_; | |
186 size_t last_read_size_; | |
187 int64 position_; | |
188 MessageLoop* io_loop_; | |
189 | |
190 // Events for other operations on stream_. | |
191 base::WaitableEvent seek_event_; | |
192 | |
193 DISALLOW_COPY_AND_ASSIGN(DataSourceImpl); | |
194 }; | |
195 | |
196 #endif // CHROME_RENDERER_MEDIA_DATA_SOURCE_IMPL_H_ | |
OLD | NEW |