Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/eme/CDMCaps.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "mozilla/CDMCaps.h"
8
#include "mozilla/EMEUtils.h"
9
#include "nsThreadUtils.h"
10
#include "SamplesWaitingForKey.h"
11
12
namespace mozilla {
13
14
CDMCaps::CDMCaps()
15
0
{
16
0
}
17
18
CDMCaps::~CDMCaps()
19
0
{
20
0
}
21
22
// Keys with MediaKeyStatus::Usable, MediaKeyStatus::Output_downscaled,
23
// or MediaKeyStatus::Output_restricted status can be used by the CDM
24
// to decrypt or decrypt-and-decode samples.
25
static bool
26
IsUsableStatus(dom::MediaKeyStatus aStatus)
27
0
{
28
0
  return aStatus == dom::MediaKeyStatus::Usable ||
29
0
         aStatus == dom::MediaKeyStatus::Output_restricted ||
30
0
         aStatus == dom::MediaKeyStatus::Output_downscaled;
31
0
}
32
33
bool
34
CDMCaps::IsKeyUsable(const CencKeyId& aKeyId)
35
0
{
36
0
  for (const KeyStatus& keyStatus : mKeyStatuses) {
37
0
    if (keyStatus.mId == aKeyId) {
38
0
      return IsUsableStatus(keyStatus.mStatus);
39
0
    }
40
0
  }
41
0
  return false;
42
0
}
43
44
bool
45
CDMCaps::SetKeyStatus(const CencKeyId& aKeyId,
46
                      const nsString& aSessionId,
47
                      const dom::Optional<dom::MediaKeyStatus>& aStatus)
48
0
{
49
0
  if (!aStatus.WasPassed()) {
50
0
    // Called from ForgetKeyStatus.
51
0
    // Return true if the element is found to notify key changes.
52
0
    return mKeyStatuses.RemoveElement(
53
0
      KeyStatus(aKeyId, aSessionId, dom::MediaKeyStatus::Internal_error));
54
0
  }
55
0
56
0
  KeyStatus key(aKeyId, aSessionId, aStatus.Value());
57
0
  auto index = mKeyStatuses.IndexOf(key);
58
0
  if (index != mKeyStatuses.NoIndex) {
59
0
    if (mKeyStatuses[index].mStatus == aStatus.Value()) {
60
0
      // No change.
61
0
      return false;
62
0
    }
63
0
    auto oldStatus = mKeyStatuses[index].mStatus;
64
0
    mKeyStatuses[index].mStatus = aStatus.Value();
65
0
    // The old key status was one for which we can decrypt media. We don't
66
0
    // need to do the "notify usable" step below, as it should be impossible
67
0
    // for us to have anything waiting on this key to become usable, since it
68
0
    // was already usable.
69
0
    if (IsUsableStatus(oldStatus)) {
70
0
      return true;
71
0
    }
72
0
  } else {
73
0
    mKeyStatuses.AppendElement(key);
74
0
  }
75
0
76
0
  // Only call NotifyUsable() for a key when we are going from non-usable
77
0
  // to usable state.
78
0
  if (!IsUsableStatus(aStatus.Value())) {
79
0
    return true;
80
0
  }
81
0
82
0
  auto& waiters = mWaitForKeys;
83
0
  size_t i = 0;
84
0
  while (i < waiters.Length()) {
85
0
    auto& w = waiters[i];
86
0
    if (w.mKeyId == aKeyId) {
87
0
      w.mListener->NotifyUsable(aKeyId);
88
0
      waiters.RemoveElementAt(i);
89
0
    } else {
90
0
      i++;
91
0
    }
92
0
  }
93
0
  return true;
94
0
}
95
96
void
97
CDMCaps::NotifyWhenKeyIdUsable(const CencKeyId& aKey,
98
                               SamplesWaitingForKey* aListener)
99
0
{
100
0
  MOZ_ASSERT(!IsKeyUsable(aKey));
101
0
  MOZ_ASSERT(aListener);
102
0
  mWaitForKeys.AppendElement(WaitForKeys(aKey, aListener));
103
0
}
104
105
void
106
CDMCaps::GetKeyStatusesForSession(const nsAString& aSessionId,
107
                                  nsTArray<KeyStatus>& aOutKeyStatuses)
108
0
{
109
0
  for (const KeyStatus& keyStatus : mKeyStatuses) {
110
0
    if (keyStatus.mSessionId.Equals(aSessionId)) {
111
0
      aOutKeyStatuses.AppendElement(keyStatus);
112
0
    }
113
0
  }
114
0
}
115
116
bool
117
CDMCaps::RemoveKeysForSession(const nsString& aSessionId)
118
0
{
119
0
  bool changed = false;
120
0
  nsTArray<KeyStatus> statuses;
121
0
  GetKeyStatusesForSession(aSessionId, statuses);
122
0
  for (const KeyStatus& status : statuses) {
123
0
    changed |= SetKeyStatus(status.mId,
124
0
                            aSessionId,
125
0
                            dom::Optional<dom::MediaKeyStatus>());
126
0
  }
127
0
  return changed;
128
0
}
129
130
} // namespace mozilla