Files.app: Remove wait for startup animation.
[chromium-blink-merge.git] / media / blink / webmediaplayer_util.cc
blobfc1a07943fc434595d90740c7d044459f7229e11
1 // Copyright 2013 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 #include "media/blink/webmediaplayer_util.h"
7 #include <math.h>
9 #include "base/metrics/histogram.h"
10 #include "media/base/bind_to_current_loop.h"
11 #include "media/base/media_client.h"
12 #include "media/base/media_keys.h"
13 #include "third_party/WebKit/public/platform/WebMediaPlayerClient.h"
15 namespace media {
17 // Compile asserts shared by all platforms.
19 #define STATIC_ASSERT_MATCHING_ENUM(name) \
20 static_assert( \
21 static_cast<int>(blink::WebMediaPlayerClient::MediaKeyErrorCode ## name) == \
22 static_cast<int>(MediaKeys::k ## name ## Error), \
23 "mismatching enum values: " #name)
24 STATIC_ASSERT_MATCHING_ENUM(Unknown);
25 STATIC_ASSERT_MATCHING_ENUM(Client);
26 #undef STATIC_ASSERT_MATCHING_ENUM
28 base::TimeDelta ConvertSecondsToTimestamp(double seconds) {
29 double microseconds = seconds * base::Time::kMicrosecondsPerSecond;
30 return base::TimeDelta::FromMicroseconds(
31 microseconds > 0 ? microseconds + 0.5 : ceil(microseconds - 0.5));
34 blink::WebTimeRanges ConvertToWebTimeRanges(
35 const Ranges<base::TimeDelta>& ranges) {
36 blink::WebTimeRanges result(ranges.size());
37 for (size_t i = 0; i < ranges.size(); ++i) {
38 result[i].start = ranges.start(i).InSecondsF();
39 result[i].end = ranges.end(i).InSecondsF();
41 return result;
44 blink::WebMediaPlayer::NetworkState PipelineErrorToNetworkState(
45 PipelineStatus error) {
46 DCHECK_NE(error, PIPELINE_OK);
48 switch (error) {
49 case PIPELINE_ERROR_NETWORK:
50 case PIPELINE_ERROR_READ:
51 return blink::WebMediaPlayer::NetworkStateNetworkError;
53 // TODO(vrk): Because OnPipelineInitialize() directly reports the
54 // NetworkStateFormatError instead of calling OnPipelineError(), I believe
55 // this block can be deleted. Should look into it! (crbug.com/126070)
56 case PIPELINE_ERROR_INITIALIZATION_FAILED:
57 case PIPELINE_ERROR_COULD_NOT_RENDER:
58 case PIPELINE_ERROR_URL_NOT_FOUND:
59 case DEMUXER_ERROR_COULD_NOT_OPEN:
60 case DEMUXER_ERROR_COULD_NOT_PARSE:
61 case DEMUXER_ERROR_NO_SUPPORTED_STREAMS:
62 case DECODER_ERROR_NOT_SUPPORTED:
63 return blink::WebMediaPlayer::NetworkStateFormatError;
65 case PIPELINE_ERROR_DECODE:
66 case PIPELINE_ERROR_ABORT:
67 case PIPELINE_ERROR_OPERATION_PENDING:
68 case PIPELINE_ERROR_INVALID_STATE:
69 return blink::WebMediaPlayer::NetworkStateDecodeError;
71 case PIPELINE_OK:
72 NOTREACHED() << "Unexpected status! " << error;
74 return blink::WebMediaPlayer::NetworkStateFormatError;
77 namespace {
79 // Helper enum for reporting scheme histograms.
80 enum URLSchemeForHistogram {
81 kUnknownURLScheme,
82 kMissingURLScheme,
83 kHttpURLScheme,
84 kHttpsURLScheme,
85 kFtpURLScheme,
86 kChromeExtensionURLScheme,
87 kJavascriptURLScheme,
88 kFileURLScheme,
89 kBlobURLScheme,
90 kDataURLScheme,
91 kFileSystemScheme,
92 kMaxURLScheme = kFileSystemScheme // Must be equal to highest enum value.
95 URLSchemeForHistogram URLScheme(const GURL& url) {
96 if (!url.has_scheme()) return kMissingURLScheme;
97 if (url.SchemeIs("http")) return kHttpURLScheme;
98 if (url.SchemeIs("https")) return kHttpsURLScheme;
99 if (url.SchemeIs("ftp")) return kFtpURLScheme;
100 if (url.SchemeIs("chrome-extension")) return kChromeExtensionURLScheme;
101 if (url.SchemeIs("javascript")) return kJavascriptURLScheme;
102 if (url.SchemeIs("file")) return kFileURLScheme;
103 if (url.SchemeIs("blob")) return kBlobURLScheme;
104 if (url.SchemeIs("data")) return kDataURLScheme;
105 if (url.SchemeIs("filesystem")) return kFileSystemScheme;
107 return kUnknownURLScheme;
110 std::string LoadTypeToString(blink::WebMediaPlayer::LoadType load_type) {
111 switch (load_type) {
112 case blink::WebMediaPlayer::LoadTypeURL:
113 return "SRC";
114 case blink::WebMediaPlayer::LoadTypeMediaSource:
115 return "MSE";
116 case blink::WebMediaPlayer::LoadTypeMediaStream:
117 return "MS";
120 NOTREACHED();
121 return "Unknown";
124 } // namespace
126 // TODO(xhwang): Call this from WebMediaPlayerMS to report metrics for
127 // MediaStream as well.
128 void ReportMetrics(blink::WebMediaPlayer::LoadType load_type,
129 const GURL& url,
130 const GURL& origin_url) {
131 // Report URL scheme, such as http, https, file, blob etc.
132 UMA_HISTOGRAM_ENUMERATION("Media.URLScheme", URLScheme(url),
133 kMaxURLScheme + 1);
135 // Keep track if this is a MSE or non-MSE playback.
136 // TODO(xhwang): This name is not intuitive. We should have a histogram for
137 // all load types.
138 UMA_HISTOGRAM_BOOLEAN(
139 "Media.MSE.Playback",
140 load_type == blink::WebMediaPlayer::LoadTypeMediaSource);
142 // Report the origin from where the media player is created.
143 if (GetMediaClient()) {
144 GetMediaClient()->RecordRapporURL(
145 "Media.OriginUrl." + LoadTypeToString(load_type), origin_url);
149 EmeInitDataType ConvertToEmeInitDataType(
150 blink::WebEncryptedMediaInitDataType init_data_type) {
151 switch (init_data_type) {
152 case blink::WebEncryptedMediaInitDataType::Webm:
153 return EmeInitDataType::WEBM;
154 case blink::WebEncryptedMediaInitDataType::Cenc:
155 return EmeInitDataType::CENC;
156 case blink::WebEncryptedMediaInitDataType::Keyids:
157 return EmeInitDataType::KEYIDS;
158 case blink::WebEncryptedMediaInitDataType::Unknown:
159 return EmeInitDataType::UNKNOWN;
162 NOTREACHED();
163 return EmeInitDataType::UNKNOWN;
166 blink::WebEncryptedMediaInitDataType ConvertToWebInitDataType(
167 EmeInitDataType init_data_type) {
168 switch (init_data_type) {
169 case EmeInitDataType::WEBM:
170 return blink::WebEncryptedMediaInitDataType::Webm;
171 case EmeInitDataType::CENC:
172 return blink::WebEncryptedMediaInitDataType::Cenc;
173 case EmeInitDataType::KEYIDS:
174 return blink::WebEncryptedMediaInitDataType::Keyids;
175 case EmeInitDataType::UNKNOWN:
176 return blink::WebEncryptedMediaInitDataType::Unknown;
179 NOTREACHED();
180 return blink::WebEncryptedMediaInitDataType::Unknown;
183 namespace {
184 // This class wraps a scoped WebSetSinkIdCB pointer such that
185 // copying objects of this class actually performs moving, thus
186 // maintaining clear ownership of the WebSetSinkIdCB pointer.
187 // The rationale for this class is that the SwichOutputDevice method
188 // can make a copy of its base::Callback parameter, which implies
189 // copying its bound parameters.
190 // SwitchOutputDevice actually wants to move its base::Callback
191 // parameter since only the final copy will be run, but base::Callback
192 // does not support move semantics and there is no base::MovableCallback.
193 // Since scoped pointers are not copyable, we cannot bind them directly
194 // to a base::Callback in this case. Thus, we use this helper class,
195 // whose copy constructor transfers ownership of the scoped pointer.
197 class SetSinkIdCallback {
198 public:
199 explicit SetSinkIdCallback(WebSetSinkIdCB* web_callback)
200 : web_callback_(web_callback) {}
201 SetSinkIdCallback(const SetSinkIdCallback& other)
202 : web_callback_(other.web_callback_.Pass()) {}
203 ~SetSinkIdCallback() {}
204 friend void RunSetSinkIdCallback(const SetSinkIdCallback& callback,
205 SwitchOutputDeviceResult result);
207 private:
208 // Mutable is required so that Pass() can be called in the copy
209 // constructor.
210 mutable scoped_ptr<WebSetSinkIdCB> web_callback_;
213 void RunSetSinkIdCallback(const SetSinkIdCallback& callback,
214 SwitchOutputDeviceResult result) {
215 DVLOG(1) << __FUNCTION__;
216 if (!callback.web_callback_)
217 return;
219 switch (result) {
220 case SWITCH_OUTPUT_DEVICE_RESULT_SUCCESS:
221 callback.web_callback_->onSuccess();
222 break;
223 case SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_FOUND:
224 callback.web_callback_->onError(new blink::WebSetSinkIdError(
225 blink::WebSetSinkIdError::ErrorTypeNotFound, "Device not found"));
226 break;
227 case SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_AUTHORIZED:
228 callback.web_callback_->onError(new blink::WebSetSinkIdError(
229 blink::WebSetSinkIdError::ErrorTypeSecurity,
230 "No permission to access device"));
231 break;
232 case SWITCH_OUTPUT_DEVICE_RESULT_ERROR_OBSOLETE:
233 callback.web_callback_->onError(new blink::WebSetSinkIdError(
234 blink::WebSetSinkIdError::ErrorTypeAbort,
235 "The requested operation became obsolete and was aborted"));
236 break;
237 case SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_SUPPORTED:
238 callback.web_callback_->onError(new blink::WebSetSinkIdError(
239 blink::WebSetSinkIdError::ErrorTypeAbort,
240 "The requested operation cannot be performed and was aborted"));
241 break;
242 default:
243 NOTREACHED();
246 callback.web_callback_ = nullptr;
249 } // namespace
251 SwitchOutputDeviceCB ConvertToSwitchOutputDeviceCB(
252 WebSetSinkIdCB* web_callbacks) {
253 return media::BindToCurrentLoop(
254 base::Bind(RunSetSinkIdCallback, SetSinkIdCallback(web_callbacks)));
257 } // namespace media