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 #ifndef CHROME_RENDERER_MEDIA_BUFFERED_DATA_SOURCE_H_ |
| 6 #define CHROME_RENDERER_MEDIA_BUFFERED_DATA_SOURCE_H_ |
| 7 |
| 8 #include <deque> |
| 9 #include <string> |
| 10 |
| 11 #include "base/lock.h" |
| 12 #include "base/scoped_ptr.h" |
| 13 #include "base/waitable_event.h" |
| 14 #include "media/base/factory.h" |
| 15 #include "media/base/filters.h" |
| 16 #include "media/base/media_format.h" |
| 17 #include "media/base/pipeline.h" |
| 18 #include "net/base/completion_callback.h" |
| 19 #include "net/base/file_stream.h" |
| 20 #include "webkit/glue/resource_loader_bridge.h" |
| 21 #include "googleurl/src/gurl.h" |
| 22 |
| 23 class WebMediaPlayerDelegateImpl; |
| 24 |
| 25 ///////////////////////////////////////////////////////////////////////////// |
| 26 // BufferedResourceLoader |
| 27 // This class works inside demuxer thread and render thread. It contains a |
| 28 // resource loader bridge and does the actual resource loading. This object |
| 29 // does buffering internally, it defers the resource loading if buffer is |
| 30 // full and un-defers the resource loading if it is under buffered. |
| 31 class BufferedResourceLoader : |
| 32 public base::RefCountedThreadSafe<BufferedResourceLoader>, |
| 33 public webkit_glue::ResourceLoaderBridge::Peer { |
| 34 public: |
| 35 BufferedResourceLoader(int route_id, |
| 36 const GURL& url, |
| 37 int64 first_byte_position, |
| 38 int64 last_byte_position); |
| 39 virtual ~BufferedResourceLoader(); |
| 40 |
| 41 // Start the resource loading with the specified URL and range. |
| 42 // This method call can operate in two modes, synchronous and asynchronous. |
| 43 // If |start_callback| is NULL, this method operates in synchronous mode and |
| 44 // it returns true if the load has started successfully, false otherwise. It |
| 45 // returns only if a resource is received from the server or this loader is |
| 46 // called to stop. |
| 47 // If |start_callback| is not NULL, this method operates in asynchronous mode |
| 48 // and it returns net::ERR_IO_PENDING if the request is going to start. |
| 49 // Once there's a response from the server, success or fail |start_callback| |
| 50 // is called with the result. |
| 51 // Note that |start_callback| is called within a lock to prevent invoking an |
| 52 // invalid callback method while this object is called to stop. |
| 53 int Start(net::CompletionCallback* callback); |
| 54 |
| 55 // Stops this loader. Wakes up all synchronous actions. |
| 56 void Stop(); |
| 57 |
| 58 // Reads the specified |size| into |buffer| and returns number of bytes copied |
| 59 // into the buffer. Returns 0 if the response has completed and there's no |
| 60 // no buffer left. Returns media::kReadError on error. The read starts from |
| 61 // the current position referred by calling GetOffset(). This method call is |
| 62 // synchronous, it returns only the required amount of bytes is read, the |
| 63 // loader is stopped, this resource loading has completed or the read has |
| 64 // timed out. Read() and SeekForward() cannot be called concurrently. |
| 65 size_t Read(uint8* buffer, size_t size); |
| 66 |
| 67 // Seek forward to |position| in bytes in the entire instance of the media |
| 68 // object, returns true if successful. If the seek operation cannot be |
| 69 // performed because it's seeking backward, the loader has been stopped, |
| 70 // the seek |position| exceed bufferable range or the seek operation has |
| 71 // timed out, returns false. |
| 72 // There cannot be SeekForward() while another thread is calling Read(). |
| 73 bool SeekForward(int64 position); |
| 74 |
| 75 // Returns the position in bytes that this loader is downloading from. |
| 76 int64 GetOffset(); |
| 77 |
| 78 // Gets and sets the buffering limit of this loader. |
| 79 int64 GetBufferLimit(); |
| 80 void SetBufferLimit(size_t buffe_limit); |
| 81 |
| 82 // Gets and sets the timeout for the synchronous operations. |
| 83 size_t GetTimeout(); |
| 84 void SetTimeout(size_t milliseconds); |
| 85 |
| 86 // Gets the content length in bytes of the instance after this loader has been |
| 87 // started. |
| 88 int64 content_length() { return content_length_; } |
| 89 |
| 90 ///////////////////////////////////////////////////////////////////////////// |
| 91 // webkit_glue::ResourceLoaderBridge::Peer implementations. |
| 92 virtual void OnUploadProgress(uint64 position, uint64 size) {} |
| 93 virtual void OnReceivedRedirect(const GURL& new_url); |
| 94 virtual void OnReceivedResponse( |
| 95 const webkit_glue::ResourceLoaderBridge::ResponseInfo& info, |
| 96 bool content_filtered); |
| 97 virtual void OnReceivedData(const char* data, int len); |
| 98 virtual void OnCompletedRequest(const URLRequestStatus& status, |
| 99 const std::string& security_info); |
| 100 std::string GetURLForDebugging() { return url_.spec(); } |
| 101 |
| 102 private: |
| 103 // Append buffer to the queue of buffers. |
| 104 void AppendToBuffer(const uint8* buffer, size_t size); |
| 105 void SignalComplete(); |
| 106 bool ShouldEnableDefer(); |
| 107 bool ShouldDisableDefer(); |
| 108 |
| 109 void OnStart(); |
| 110 void OnDestroy(); |
| 111 void OnDisableDeferLoading(); |
| 112 void OnEnableDeferLoading(); |
| 113 |
| 114 void InvokeAndResetStartCallback(int error); |
| 115 |
| 116 struct Buffer { |
| 117 Buffer(size_t len) : taken(0), size(len), data(new uint8[len]) { } |
| 118 |
| 119 // The amount of buffer in bytes consumed in this buffer starting from |
| 120 // index 0. |
| 121 size_t taken; |
| 122 size_t size; |
| 123 scoped_array<uint8> data; |
| 124 }; |
| 125 |
| 126 scoped_ptr<net::CompletionCallback> start_callback_; |
| 127 scoped_ptr<webkit_glue::ResourceLoaderBridge> bridge_; |
| 128 int64 offset_; |
| 129 int64 content_length_; |
| 130 |
| 131 std::deque<Buffer*> buffers_; |
| 132 size_t buffered_bytes_; |
| 133 size_t buffer_limit_; |
| 134 base::WaitableEvent buffer_event_; |
| 135 |
| 136 bool deferred_; |
| 137 bool stopped_; |
| 138 bool completed_; |
| 139 bool range_requested_; |
| 140 bool async_start_; |
| 141 |
| 142 int route_id_; |
| 143 GURL url_; |
| 144 int64 first_byte_position_; |
| 145 int64 last_byte_position_; |
| 146 |
| 147 MessageLoop* render_loop_; |
| 148 // A lock that protects usage of the following members: |
| 149 // - buffers_ |
| 150 // - buffered_bytes_ |
| 151 // - buffered_limit_ |
| 152 // - deferred_ |
| 153 // - stopped_ |
| 154 // - completed_ |
| 155 Lock lock_; |
| 156 |
| 157 DISALLOW_COPY_AND_ASSIGN(BufferedResourceLoader); |
| 158 }; |
| 159 |
| 160 class BufferedDataSource : public media::DataSource { |
| 161 public: |
| 162 // Methods called from pipeline thread |
| 163 // Static methods for creating this class. |
| 164 static media::FilterFactory* CreateFactory( |
| 165 WebMediaPlayerDelegateImpl* delegate) { |
| 166 return new media::FilterFactoryImpl1<BufferedDataSource, |
| 167 WebMediaPlayerDelegateImpl*>(delegate); |
| 168 } |
| 169 virtual bool Initialize(const std::string& url); |
| 170 |
| 171 // media::MediaFilter implementation. |
| 172 virtual void Stop(); |
| 173 |
| 174 // media::DataSource implementation. |
| 175 // Called from demuxer thread. |
| 176 virtual size_t Read(uint8* data, size_t size); |
| 177 virtual bool GetPosition(int64* position_out); |
| 178 virtual bool SetPosition(int64 position); |
| 179 virtual bool GetSize(int64* size_out); |
| 180 virtual bool IsSeekable(); |
| 181 |
| 182 const media::MediaFormat& media_format(); |
| 183 |
| 184 private: |
| 185 friend class media::FilterFactoryImpl1<BufferedDataSource, |
| 186 WebMediaPlayerDelegateImpl*>; |
| 187 // Call to filter host to trigger an error, be sure not to call this method |
| 188 // while the lock is acquired. |
| 189 void HandleError(media::PipelineError error); |
| 190 |
| 191 // Callback method from BufferedResourceLoader for the initial url request. |
| 192 // |error| is net::OK if the request has started successfully or |error| is |
| 193 // a code representing the actual network error. |
| 194 void InitialRequestStarted(int error); |
| 195 void OnInitialRequestStarted(int error); |
| 196 |
| 197 explicit BufferedDataSource(WebMediaPlayerDelegateImpl* delegate); |
| 198 virtual ~BufferedDataSource(); |
| 199 |
| 200 media::MediaFormat media_format_; |
| 201 GURL url_; |
| 202 |
| 203 // Pointer to the delegate which provides access to RenderView, this is set |
| 204 // in construction and can be accessed in all threads safely. |
| 205 // TODO(hclam): get rid of this and save the routing id and pointer to |
| 206 // ResourceDispatcher. |
| 207 WebMediaPlayerDelegateImpl* delegate_; |
| 208 |
| 209 // A common lock for protecting members accessed by multiple threads. |
| 210 Lock lock_; |
| 211 bool stopped_; |
| 212 |
| 213 // Members used for reading. |
| 214 int64 position_; |
| 215 // Members for total bytes of the requested object. |
| 216 int64 total_bytes_; |
| 217 |
| 218 // Members related to resource loading with RenderView. |
| 219 scoped_refptr<BufferedResourceLoader> buffered_resource_loader_; |
| 220 |
| 221 // The message loop of the pipeline thread. |
| 222 MessageLoop* pipeline_loop_; |
| 223 |
| 224 DISALLOW_COPY_AND_ASSIGN(BufferedDataSource); |
| 225 }; |
| 226 |
| 227 #endif // CHROME_RENDERER_MEDIA_BUFFERED_DATA_SOURCE_H_ |
OLD | NEW |