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"
9 #include "base/metrics/histogram.h"
10 #include "media/base/media_keys.h"
11 #include "third_party/WebKit/public/platform/WebMediaPlayerClient.h"
15 // Compile asserts shared by all platforms.
17 #define STATIC_ASSERT_MATCHING_ENUM(name) \
19 static_cast<int>(blink::WebMediaPlayerClient::MediaKeyErrorCode ## name) == \
20 static_cast<int>(MediaKeys::k ## name ## Error), \
21 "mismatching enum values: " #name)
22 STATIC_ASSERT_MATCHING_ENUM(Unknown
);
23 STATIC_ASSERT_MATCHING_ENUM(Client
);
24 #undef STATIC_ASSERT_MATCHING_ENUM
26 base::TimeDelta
ConvertSecondsToTimestamp(double seconds
) {
27 double microseconds
= seconds
* base::Time::kMicrosecondsPerSecond
;
28 return base::TimeDelta::FromMicroseconds(
29 microseconds
> 0 ? microseconds
+ 0.5 : ceil(microseconds
- 0.5));
32 blink::WebTimeRanges
ConvertToWebTimeRanges(
33 const Ranges
<base::TimeDelta
>& ranges
) {
34 blink::WebTimeRanges
result(ranges
.size());
35 for (size_t i
= 0; i
< ranges
.size(); ++i
) {
36 result
[i
].start
= ranges
.start(i
).InSecondsF();
37 result
[i
].end
= ranges
.end(i
).InSecondsF();
42 blink::WebMediaPlayer::NetworkState
PipelineErrorToNetworkState(
43 PipelineStatus error
) {
44 DCHECK_NE(error
, PIPELINE_OK
);
47 case PIPELINE_ERROR_NETWORK
:
48 case PIPELINE_ERROR_READ
:
49 return blink::WebMediaPlayer::NetworkStateNetworkError
;
51 // TODO(vrk): Because OnPipelineInitialize() directly reports the
52 // NetworkStateFormatError instead of calling OnPipelineError(), I believe
53 // this block can be deleted. Should look into it! (crbug.com/126070)
54 case PIPELINE_ERROR_INITIALIZATION_FAILED
:
55 case PIPELINE_ERROR_COULD_NOT_RENDER
:
56 case PIPELINE_ERROR_URL_NOT_FOUND
:
57 case DEMUXER_ERROR_COULD_NOT_OPEN
:
58 case DEMUXER_ERROR_COULD_NOT_PARSE
:
59 case DEMUXER_ERROR_NO_SUPPORTED_STREAMS
:
60 case DECODER_ERROR_NOT_SUPPORTED
:
61 return blink::WebMediaPlayer::NetworkStateFormatError
;
63 case PIPELINE_ERROR_DECODE
:
64 case PIPELINE_ERROR_ABORT
:
65 case PIPELINE_ERROR_OPERATION_PENDING
:
66 case PIPELINE_ERROR_INVALID_STATE
:
67 return blink::WebMediaPlayer::NetworkStateDecodeError
;
69 case PIPELINE_ERROR_DECRYPT
:
70 // TODO(xhwang): Change to use NetworkStateDecryptError once it's added in
71 // Webkit (see http://crbug.com/124486).
72 return blink::WebMediaPlayer::NetworkStateDecodeError
;
75 NOTREACHED() << "Unexpected status! " << error
;
77 return blink::WebMediaPlayer::NetworkStateFormatError
;
82 // Helper enum for reporting scheme histograms.
83 enum URLSchemeForHistogram
{
89 kChromeExtensionURLScheme
,
95 kMaxURLScheme
= kFileSystemScheme
// Must be equal to highest enum value.
98 URLSchemeForHistogram
URLScheme(const GURL
& url
) {
99 if (!url
.has_scheme()) return kMissingURLScheme
;
100 if (url
.SchemeIs("http")) return kHttpURLScheme
;
101 if (url
.SchemeIs("https")) return kHttpsURLScheme
;
102 if (url
.SchemeIs("ftp")) return kFtpURLScheme
;
103 if (url
.SchemeIs("chrome-extension")) return kChromeExtensionURLScheme
;
104 if (url
.SchemeIs("javascript")) return kJavascriptURLScheme
;
105 if (url
.SchemeIs("file")) return kFileURLScheme
;
106 if (url
.SchemeIs("blob")) return kBlobURLScheme
;
107 if (url
.SchemeIs("data")) return kDataURLScheme
;
108 if (url
.SchemeIs("filesystem")) return kFileSystemScheme
;
110 return kUnknownURLScheme
;
115 void ReportMediaSchemeUma(const GURL
& url
) {
116 UMA_HISTOGRAM_ENUMERATION("Media.URLScheme", URLScheme(url
),