summaryrefslogtreecommitdiffstats
path: root/dom/media/mediasource/SourceBufferList.cpp
blob: 9a98f83a1cd7f6aa18bf99ac2bd776430c1f3faa (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "SourceBufferList.h"

#include "AsyncEventRunner.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/dom/SourceBufferListBinding.h"
#include "mozilla/mozalloc.h"
#include "nsCOMPtr.h"
#include "nsIRunnable.h"
#include "nsString.h"
#include "nsThreadUtils.h"
#include "mozilla/Logging.h"

extern mozilla::LogModule* GetMediaSourceLog();
extern mozilla::LogModule* GetMediaSourceAPILog();

#define MSE_API(arg, ...)                                   \
  MOZ_LOG(GetMediaSourceAPILog(), mozilla::LogLevel::Debug, \
          ("SourceBufferList(%p)::%s: " arg, this, __func__, ##__VA_ARGS__))
#define MSE_DEBUG(arg, ...)                              \
  MOZ_LOG(GetMediaSourceLog(), mozilla::LogLevel::Debug, \
          ("SourceBufferList(%p)::%s: " arg, this, __func__, ##__VA_ARGS__))

struct JSContext;
class JSObject;

using TimeUnit = mozilla::media::TimeUnit;

namespace mozilla::dom {

SourceBufferList::~SourceBufferList() = default;

SourceBuffer* SourceBufferList::IndexedGetter(uint32_t aIndex, bool& aFound) {
  MOZ_ASSERT(NS_IsMainThread());
  aFound = aIndex < mSourceBuffers.Length();

  if (!aFound) {
    return nullptr;
  }
  return mSourceBuffers[aIndex];
}

uint32_t SourceBufferList::Length() {
  MOZ_ASSERT(NS_IsMainThread());
  return mSourceBuffers.Length();
}

void SourceBufferList::Append(SourceBuffer* aSourceBuffer) {
  MOZ_ASSERT(NS_IsMainThread());
  mSourceBuffers.AppendElement(aSourceBuffer);
  QueueAsyncSimpleEvent("addsourcebuffer");
}

void SourceBufferList::AppendSimple(SourceBuffer* aSourceBuffer) {
  MOZ_ASSERT(NS_IsMainThread());
  mSourceBuffers.AppendElement(aSourceBuffer);
}

void SourceBufferList::Remove(SourceBuffer* aSourceBuffer) {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ALWAYS_TRUE(mSourceBuffers.RemoveElement(aSourceBuffer));
  aSourceBuffer->Detach();
  QueueAsyncSimpleEvent("removesourcebuffer");
}

bool SourceBufferList::Contains(SourceBuffer* aSourceBuffer) {
  MOZ_ASSERT(NS_IsMainThread());
  return mSourceBuffers.Contains(aSourceBuffer);
}

void SourceBufferList::Clear() {
  MOZ_ASSERT(NS_IsMainThread());
  for (uint32_t i = 0; i < mSourceBuffers.Length(); ++i) {
    mSourceBuffers[i]->Detach();
  }
  mSourceBuffers.Clear();
  QueueAsyncSimpleEvent("removesourcebuffer");
}

void SourceBufferList::ClearSimple() {
  MOZ_ASSERT(NS_IsMainThread());
  mSourceBuffers.Clear();
}

bool SourceBufferList::IsEmpty() {
  MOZ_ASSERT(NS_IsMainThread());
  return mSourceBuffers.IsEmpty();
}

bool SourceBufferList::AnyUpdating() {
  MOZ_ASSERT(NS_IsMainThread());
  for (uint32_t i = 0; i < mSourceBuffers.Length(); ++i) {
    if (mSourceBuffers[i]->Updating()) {
      return true;
    }
  }
  return false;
}

void SourceBufferList::RangeRemoval(double aStart, double aEnd) {
  MOZ_ASSERT(NS_IsMainThread());
  MSE_DEBUG("RangeRemoval(aStart=%f, aEnd=%f)", aStart, aEnd);
  for (uint32_t i = 0; i < mSourceBuffers.Length(); ++i) {
    mSourceBuffers[i]->RangeRemoval(aStart, aEnd);
  }
}

void SourceBufferList::Ended() {
  MOZ_ASSERT(NS_IsMainThread());
  for (uint32_t i = 0; i < mSourceBuffers.Length(); ++i) {
    mSourceBuffers[i]->Ended();
  }
}

TimeUnit SourceBufferList::GetHighestBufferedEndTime() {
  MOZ_ASSERT(NS_IsMainThread());
  TimeUnit highestEndTime = TimeUnit::Zero();
  for (uint32_t i = 0; i < mSourceBuffers.Length(); ++i) {
    highestEndTime =
        std::max(highestEndTime, mSourceBuffers[i]->GetBufferedEnd());
  }
  return highestEndTime;
}

void SourceBufferList::DispatchSimpleEvent(const char* aName) {
  MOZ_ASSERT(NS_IsMainThread());
  MSE_API("Dispatch event '%s'", aName);
  DispatchTrustedEvent(NS_ConvertUTF8toUTF16(aName));
}

void SourceBufferList::QueueAsyncSimpleEvent(const char* aName) {
  MSE_DEBUG("Queue event '%s'", aName);
  nsCOMPtr<nsIRunnable> event =
      new AsyncEventRunner<SourceBufferList>(this, aName);
  mAbstractMainThread->Dispatch(event.forget());
}

SourceBufferList::SourceBufferList(MediaSource* aMediaSource)
    : DOMEventTargetHelper(aMediaSource->GetParentObject()),
      mMediaSource(aMediaSource),
      mAbstractMainThread(mMediaSource->AbstractMainThread()) {
  MOZ_ASSERT(aMediaSource);
}

MediaSource* SourceBufferList::GetParentObject() const { return mMediaSource; }

TimeUnit SourceBufferList::HighestStartTime() {
  MOZ_ASSERT(NS_IsMainThread());
  TimeUnit highestStartTime = TimeUnit::Zero();
  for (auto& sourceBuffer : mSourceBuffers) {
    highestStartTime =
        std::max(sourceBuffer->HighestStartTime(), highestStartTime);
  }
  return highestStartTime;
}

TimeUnit SourceBufferList::HighestEndTime() {
  MOZ_ASSERT(NS_IsMainThread());
  TimeUnit highestEndTime = TimeUnit::Zero();
  for (auto& sourceBuffer : mSourceBuffers) {
    highestEndTime = std::max(sourceBuffer->HighestEndTime(), highestEndTime);
  }
  return highestEndTime;
}

JSObject* SourceBufferList::WrapObject(JSContext* aCx,
                                       JS::Handle<JSObject*> aGivenProto) {
  return SourceBufferList_Binding::Wrap(aCx, this, aGivenProto);
}

NS_IMPL_CYCLE_COLLECTION_INHERITED(SourceBufferList, DOMEventTargetHelper,
                                   mMediaSource, mSourceBuffers)

NS_IMPL_ADDREF_INHERITED(SourceBufferList, DOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(SourceBufferList, DOMEventTargetHelper)

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SourceBufferList)
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)

#undef MSE_API
#undef MSE_DEBUG
}  // namespace mozilla::dom