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 3// found in the LICENSE file. 4 5// An extremely simple implementation of DataSource that downloads the entire 6// media resource into memory before signaling that initialization has finished. 7// Primarily used to test <audio> and <video> with buffering/caching removed 8// from the equation. 9 10#ifndef WEBKIT_GLUE_MEDIA_SIMPLE_DATA_SOURCE_H_ 11#define WEBKIT_GLUE_MEDIA_SIMPLE_DATA_SOURCE_H_ 12 13#include <algorithm> 14#include <string> 15 16#include "base/memory/scoped_ptr.h" 17#include "base/message_loop.h" 18#include "media/base/filter_factories.h" 19#include "media/base/filters.h" 20#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" 21#include "third_party/WebKit/Source/WebKit/chromium/public/WebURLLoader.h" 22#include "third_party/WebKit/Source/WebKit/chromium/public/WebURLLoaderClient.h" 23#include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" 24#include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" 25#include "webkit/glue/media/web_data_source.h" 26 27class MessageLoop; 28class WebMediaPlayerDelegateImpl; 29 30namespace webkit_glue { 31 32class SimpleDataSource : public WebDataSource, 33 public WebKit::WebURLLoaderClient { 34 public: 35 // Creates a DataSourceFactory for building SimpleDataSource objects. 36 static media::DataSourceFactory* CreateFactory( 37 MessageLoop* render_loop, 38 WebKit::WebFrame* frame, 39 WebDataSourceBuildObserverHack* build_observer); 40 41 SimpleDataSource(MessageLoop* render_loop, WebKit::WebFrame* frame); 42 virtual ~SimpleDataSource(); 43 44 // media::Filter implementation. 45 virtual void set_host(media::FilterHost* host); 46 virtual void Stop(media::FilterCallback* callback); 47 48 // media::DataSource implementation. 49 virtual const media::MediaFormat& media_format(); 50 virtual void Read(int64 position, size_t size, 51 uint8* data, ReadCallback* read_callback); 52 virtual bool GetSize(int64* size_out); 53 virtual bool IsStreaming(); 54 virtual void SetPreload(media::Preload preload); 55 56 // Used to inject a mock used for unittests. 57 virtual void SetURLLoaderForTest(WebKit::WebURLLoader* mock_loader); 58 59 // WebKit::WebURLLoaderClient implementations. 60 virtual void willSendRequest( 61 WebKit::WebURLLoader* loader, 62 WebKit::WebURLRequest& newRequest, 63 const WebKit::WebURLResponse& redirectResponse); 64 virtual void didSendData( 65 WebKit::WebURLLoader* loader, 66 unsigned long long bytesSent, 67 unsigned long long totalBytesToBeSent); 68 virtual void didReceiveResponse( 69 WebKit::WebURLLoader* loader, 70 const WebKit::WebURLResponse& response); 71 virtual void didDownloadData( 72 WebKit::WebURLLoader* loader, 73 int dataLength); 74 virtual void didReceiveData( 75 WebKit::WebURLLoader* loader, 76 const char* data, 77 int dataLength, 78 int encodedDataLength); 79 virtual void didReceiveCachedMetadata( 80 WebKit::WebURLLoader* loader, 81 const char* data, int dataLength); 82 virtual void didFinishLoading( 83 WebKit::WebURLLoader* loader, 84 double finishTime); 85 virtual void didFail( 86 WebKit::WebURLLoader* loader, 87 const WebKit::WebURLError&); 88 89 // webkit_glue::WebDataSource implementation. 90 virtual void Initialize(const std::string& url, 91 media::PipelineStatusCallback* callback); 92 virtual void CancelInitialize(); 93 virtual bool HasSingleOrigin(); 94 virtual void Abort(); 95 96 private: 97 // Updates |url_| and |media_format_| with the given URL. 98 void SetURL(const GURL& url); 99 100 // Creates and starts the resource loading on the render thread. 101 void StartTask(); 102 103 // Cancels and deletes the resource loading on the render thread. 104 void CancelTask(); 105 106 // Perform initialization completion tasks under a lock. 107 void DoneInitialization_Locked(bool success); 108 109 // Update host() stats like total bytes & buffered bytes. 110 void UpdateHostState(); 111 112 // Primarily used for asserting the bridge is loading on the render thread. 113 MessageLoop* render_loop_; 114 115 // A webframe for loading. 116 WebKit::WebFrame* frame_; 117 118 // Does the work of loading and sends data back to this client. 119 scoped_ptr<WebKit::WebURLLoader> url_loader_; 120 121 media::MediaFormat media_format_; 122 GURL url_; 123 std::string data_; 124 int64 size_; 125 bool single_origin_; 126 127 // Simple state tracking variable. 128 enum State { 129 UNINITIALIZED, 130 INITIALIZING, 131 INITIALIZED, 132 STOPPED, 133 }; 134 State state_; 135 136 // Used for accessing |state_|. 137 base::Lock lock_; 138 139 // Filter callbacks. 140 scoped_ptr<media::PipelineStatusCallback> initialize_callback_; 141 142 // Used to ensure mocks for unittests are used instead of reset in Start(). 143 bool keep_test_loader_; 144 145 DISALLOW_COPY_AND_ASSIGN(SimpleDataSource); 146}; 147 148} // namespace webkit_glue 149 150#endif // WEBKIT_GLUE_MEDIA_SIMPLE_DATA_SOURCE_H_ 151