Fix playing of 8-bit sample files via webaudio.
[chromium-blink-merge.git] / webkit / glue / media / simple_data_source.h
blob77dc35cb565852fc93d97a8f691fe4209e334471
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.
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.
10 #ifndef WEBKIT_GLUE_MEDIA_SIMPLE_DATA_SOURCE_H_
11 #define WEBKIT_GLUE_MEDIA_SIMPLE_DATA_SOURCE_H_
13 #include <algorithm>
14 #include <string>
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"
27 class MessageLoop;
28 class WebMediaPlayerDelegateImpl;
30 namespace media {
31 class MediaLog;
34 namespace webkit_glue {
36 class SimpleDataSource
37 : public WebDataSource,
38 public WebKit::WebURLLoaderClient {
39 public:
40 // Creates a DataSourceFactory for building SimpleDataSource objects.
41 static media::DataSourceFactory* CreateFactory(
42 MessageLoop* render_loop,
43 WebKit::WebFrame* frame,
44 media::MediaLog* media_log,
45 const WebDataSourceBuildObserverHack& build_observer);
47 SimpleDataSource(MessageLoop* render_loop, WebKit::WebFrame* frame);
48 virtual ~SimpleDataSource();
50 // media::Filter implementation.
51 virtual void set_host(media::FilterHost* host);
52 virtual void Stop(const base::Closure& callback);
54 // media::DataSource implementation.
55 virtual void Read(int64 position, size_t size,
56 uint8* data, const DataSource::ReadCallback& read_callback);
57 virtual bool GetSize(int64* size_out);
58 virtual bool IsStreaming();
59 virtual void SetPreload(media::Preload preload);
60 virtual void SetBitrate(int bitrate);
62 // Used to inject a mock used for unittests.
63 virtual void SetURLLoaderForTest(WebKit::WebURLLoader* mock_loader);
65 // WebKit::WebURLLoaderClient implementations.
66 virtual void willSendRequest(
67 WebKit::WebURLLoader* loader,
68 WebKit::WebURLRequest& newRequest,
69 const WebKit::WebURLResponse& redirectResponse);
70 virtual void didSendData(
71 WebKit::WebURLLoader* loader,
72 unsigned long long bytesSent,
73 unsigned long long totalBytesToBeSent);
74 virtual void didReceiveResponse(
75 WebKit::WebURLLoader* loader,
76 const WebKit::WebURLResponse& response);
77 virtual void didDownloadData(
78 WebKit::WebURLLoader* loader,
79 int dataLength);
80 virtual void didReceiveData(
81 WebKit::WebURLLoader* loader,
82 const char* data,
83 int dataLength,
84 int encodedDataLength);
85 virtual void didReceiveCachedMetadata(
86 WebKit::WebURLLoader* loader,
87 const char* data, int dataLength);
88 virtual void didFinishLoading(
89 WebKit::WebURLLoader* loader,
90 double finishTime);
91 virtual void didFail(
92 WebKit::WebURLLoader* loader,
93 const WebKit::WebURLError&);
95 // webkit_glue::WebDataSource implementation.
96 virtual void Initialize(const std::string& url,
97 const media::PipelineStatusCB& callback);
98 virtual void CancelInitialize();
99 virtual bool HasSingleOrigin();
100 virtual void Abort();
102 private:
103 // Creates and starts the resource loading on the render thread.
104 void StartTask();
106 // Cancels and deletes the resource loading on the render thread.
107 void CancelTask();
109 // Perform initialization completion tasks under a lock.
110 void DoneInitialization_Locked(bool success);
112 // Update host() stats like total bytes & buffered bytes.
113 void UpdateHostState();
115 // Primarily used for asserting the bridge is loading on the render thread.
116 MessageLoop* render_loop_;
118 // A webframe for loading.
119 WebKit::WebFrame* frame_;
121 // Does the work of loading and sends data back to this client.
122 scoped_ptr<WebKit::WebURLLoader> url_loader_;
124 GURL url_;
125 std::string data_;
126 int64 size_;
127 bool single_origin_;
129 // Simple state tracking variable.
130 enum State {
131 UNINITIALIZED,
132 INITIALIZING,
133 INITIALIZED,
134 STOPPED,
136 State state_;
138 // Used for accessing |state_|.
139 base::Lock lock_;
141 // Filter callbacks.
142 media::PipelineStatusCB initialize_cb_;
144 // Used to ensure mocks for unittests are used instead of reset in Start().
145 bool keep_test_loader_;
147 DISALLOW_COPY_AND_ASSIGN(SimpleDataSource);
150 } // namespace webkit_glue
152 #endif // WEBKIT_GLUE_MEDIA_SIMPLE_DATA_SOURCE_H_