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 "content/renderer/media/webmediaplayer_util.h"
6
7#include <math.h>
8
9#include "media/base/media_keys.h"
10#include "third_party/WebKit/public/web/WebMediaPlayerClient.h"
11
12namespace content {
13
14// Compile asserts shared by all platforms.
15
16#define COMPILE_ASSERT_MATCHING_ENUM(name) \
17  COMPILE_ASSERT( \
18  static_cast<int>(WebKit::WebMediaPlayerClient::MediaKeyErrorCode ## name) == \
19  static_cast<int>(media::MediaKeys::k ## name ## Error), \
20  mismatching_enums)
21COMPILE_ASSERT_MATCHING_ENUM(Unknown);
22COMPILE_ASSERT_MATCHING_ENUM(Client);
23#undef COMPILE_ASSERT_MATCHING_ENUM
24
25base::TimeDelta ConvertSecondsToTimestamp(double seconds) {
26  double microseconds = seconds * base::Time::kMicrosecondsPerSecond;
27  return base::TimeDelta::FromMicroseconds(
28      microseconds > 0 ? microseconds + 0.5 : ceil(microseconds - 0.5));
29}
30
31WebKit::WebTimeRanges ConvertToWebTimeRanges(
32    const media::Ranges<base::TimeDelta>& ranges) {
33  WebKit::WebTimeRanges result(ranges.size());
34  for (size_t i = 0; i < ranges.size(); i++) {
35    result[i].start = ranges.start(i).InSecondsF();
36    result[i].end = ranges.end(i).InSecondsF();
37  }
38  return result;
39}
40
41WebKit::WebMediaPlayer::NetworkState PipelineErrorToNetworkState(
42    media::PipelineStatus error) {
43  DCHECK_NE(error, media::PIPELINE_OK);
44
45  switch (error) {
46    case media::PIPELINE_ERROR_NETWORK:
47    case media::PIPELINE_ERROR_READ:
48      return WebKit::WebMediaPlayer::NetworkStateNetworkError;
49
50    // TODO(vrk): Because OnPipelineInitialize() directly reports the
51    // NetworkStateFormatError instead of calling OnPipelineError(), I believe
52    // this block can be deleted. Should look into it! (crbug.com/126070)
53    case media::PIPELINE_ERROR_INITIALIZATION_FAILED:
54    case media::PIPELINE_ERROR_COULD_NOT_RENDER:
55    case media::PIPELINE_ERROR_URL_NOT_FOUND:
56    case media::DEMUXER_ERROR_COULD_NOT_OPEN:
57    case media::DEMUXER_ERROR_COULD_NOT_PARSE:
58    case media::DEMUXER_ERROR_NO_SUPPORTED_STREAMS:
59    case media::DECODER_ERROR_NOT_SUPPORTED:
60      return WebKit::WebMediaPlayer::NetworkStateFormatError;
61
62    case media::PIPELINE_ERROR_DECODE:
63    case media::PIPELINE_ERROR_ABORT:
64    case media::PIPELINE_ERROR_OPERATION_PENDING:
65    case media::PIPELINE_ERROR_INVALID_STATE:
66      return WebKit::WebMediaPlayer::NetworkStateDecodeError;
67
68    case media::PIPELINE_ERROR_DECRYPT:
69      // TODO(xhwang): Change to use NetworkStateDecryptError once it's added in
70      // Webkit (see http://crbug.com/124486).
71      return WebKit::WebMediaPlayer::NetworkStateDecodeError;
72
73    case media::PIPELINE_OK:
74    case media::PIPELINE_STATUS_MAX:
75      NOTREACHED() << "Unexpected status! " << error;
76  }
77  return WebKit::WebMediaPlayer::NetworkStateFormatError;
78}
79
80}  // namespace content
81