summaryrefslogtreecommitdiffstats
path: root/dom/media/eme/EMEUtils.cpp
blob: fd51b0ae2aea1445541a63c6bd842c2ff1a36ff8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/EMEUtils.h"

#include "jsfriendapi.h" // for AutoCheckCannotGC
#include "mozilla/dom/UnionTypes.h"

namespace mozilla {

LogModule* GetEMELog() {
  static LazyLogModule log("EME");
  return log;
}

LogModule* GetEMEVerboseLog() {
  static LazyLogModule log("EMEV");
  return log;
}

ArrayData
GetArrayBufferViewOrArrayBufferData(const dom::ArrayBufferViewOrArrayBuffer& aBufferOrView)
{
  MOZ_ASSERT(aBufferOrView.IsArrayBuffer() || aBufferOrView.IsArrayBufferView());
  JS::AutoCheckCannotGC nogc;
  if (aBufferOrView.IsArrayBuffer()) {
    const dom::ArrayBuffer& buffer = aBufferOrView.GetAsArrayBuffer();
    buffer.ComputeLengthAndData();
    return ArrayData(buffer.Data(), buffer.Length());
  } else if (aBufferOrView.IsArrayBufferView()) {
    const dom::ArrayBufferView& bufferview = aBufferOrView.GetAsArrayBufferView();
    bufferview.ComputeLengthAndData();
    return ArrayData(bufferview.Data(), bufferview.Length());
  }
  return ArrayData(nullptr, 0);
}

void
CopyArrayBufferViewOrArrayBufferData(const dom::ArrayBufferViewOrArrayBuffer& aBufferOrView,
                                     nsTArray<uint8_t>& aOutData)
{
  JS::AutoCheckCannotGC nogc;
  ArrayData data = GetArrayBufferViewOrArrayBufferData(aBufferOrView);
  aOutData.Clear();
  if (!data.IsValid()) {
    return;
  }
  aOutData.AppendElements(data.mData, data.mLength);
}

void CopyArrayBufferViewOrArrayBufferData(const dom::ArrayBuffer& aBuffer,
                                          nsTArray<uint8_t>& aOutData) {
  JS::AutoCheckCannotGC nogc;
  aBuffer.ComputeLengthAndData();
  aOutData.Clear();
  aOutData.AppendElements(aBuffer.Data(), aBuffer.Length());
}

bool
IsClearkeyKeySystem(const nsAString& aKeySystem)
{
  return !CompareUTF8toUTF16(kEMEKeySystemClearkey, aKeySystem);
}

bool
IsWidevineKeySystem(const nsAString& aKeySystem)
{
  return !CompareUTF8toUTF16(kEMEKeySystemWidevine, aKeySystem);
}

nsString
KeySystemToGMPName(const nsAString& aKeySystem)
{
  if (IsClearkeyKeySystem(aKeySystem)) {
    return NS_LITERAL_STRING("gmp-clearkey");
  }
  if (IsWidevineKeySystem(aKeySystem)) {
    return NS_LITERAL_STRING("gmp-widevinecdm");
  }
  MOZ_ASSERT(false, "We should only call this for known GMPs");
  return EmptyString();
}

CDMType
ToCDMTypeTelemetryEnum(const nsString& aKeySystem)
{
  if (IsWidevineKeySystem(aKeySystem)) {
    return CDMType::eWidevine;
  } else if (IsClearkeyKeySystem(aKeySystem)) {
    return CDMType::eClearKey;
  }
  return CDMType::eUnknown;
}

} // namespace mozilla