Home | History | Annotate | Download | only in blink
      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.
      4 
      5 #include "media/blink/webmediaplayer_util.h"
      6 
      7 #include <math.h>
      8 
      9 #include "base/metrics/histogram.h"
     10 #include "media/base/media_keys.h"
     11 #include "third_party/WebKit/public/platform/WebMediaPlayerClient.h"
     12 
     13 namespace media {
     14 
     15 // Compile asserts shared by all platforms.
     16 
     17 #define COMPILE_ASSERT_MATCHING_ENUM(name) \
     18   COMPILE_ASSERT( \
     19   static_cast<int>(blink::WebMediaPlayerClient::MediaKeyErrorCode ## name) == \
     20   static_cast<int>(MediaKeys::k ## name ## Error), \
     21   mismatching_enums)
     22 COMPILE_ASSERT_MATCHING_ENUM(Unknown);
     23 COMPILE_ASSERT_MATCHING_ENUM(Client);
     24 #undef COMPILE_ASSERT_MATCHING_ENUM
     25 
     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));
     30 }
     31 
     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();
     38   }
     39   return result;
     40 }
     41 
     42 blink::WebMediaPlayer::NetworkState PipelineErrorToNetworkState(
     43     PipelineStatus error) {
     44   DCHECK_NE(error, PIPELINE_OK);
     45 
     46   switch (error) {
     47     case PIPELINE_ERROR_NETWORK:
     48     case PIPELINE_ERROR_READ:
     49       return blink::WebMediaPlayer::NetworkStateNetworkError;
     50 
     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;
     62 
     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;
     68 
     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;
     73 
     74     case PIPELINE_OK:
     75       NOTREACHED() << "Unexpected status! " << error;
     76   }
     77   return blink::WebMediaPlayer::NetworkStateFormatError;
     78 }
     79 
     80 namespace {
     81 
     82 // Helper enum for reporting scheme histograms.
     83 enum URLSchemeForHistogram {
     84   kUnknownURLScheme,
     85   kMissingURLScheme,
     86   kHttpURLScheme,
     87   kHttpsURLScheme,
     88   kFtpURLScheme,
     89   kChromeExtensionURLScheme,
     90   kJavascriptURLScheme,
     91   kFileURLScheme,
     92   kBlobURLScheme,
     93   kDataURLScheme,
     94   kFileSystemScheme,
     95   kMaxURLScheme = kFileSystemScheme  // Must be equal to highest enum value.
     96 };
     97 
     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;
    109 
    110   return kUnknownURLScheme;
    111 }
    112 
    113 }  // namespace
    114 
    115 void ReportMediaSchemeUma(const GURL& url) {
    116   UMA_HISTOGRAM_ENUMERATION("Media.URLScheme", URLScheme(url),
    117                             kMaxURLScheme + 1);
    118 }
    119 
    120 }  // namespace media
    121