2014-04-16 07:06:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 et tw=78: */
|
|
|
|
/* 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 "MediaTrack.h"
|
|
|
|
#include "MediaTrackList.h"
|
|
|
|
#include "mozilla/AsyncEventDispatcher.h"
|
2014-05-23 13:34:14 +04:00
|
|
|
#include "mozilla/dom/HTMLMediaElement.h"
|
2014-04-16 07:06:45 +04:00
|
|
|
#include "mozilla/dom/AudioTrack.h"
|
2016-05-10 12:02:15 +03:00
|
|
|
#include "mozilla/dom/VideoStreamTrack.h"
|
2014-04-16 07:06:45 +04:00
|
|
|
#include "mozilla/dom/VideoTrack.h"
|
|
|
|
#include "mozilla/dom/TrackEvent.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
MediaTrackList::MediaTrackList(nsPIDOMWindowInner* aOwnerWindow,
|
2014-04-16 07:06:45 +04:00
|
|
|
HTMLMediaElement* aMediaElement)
|
|
|
|
: DOMEventTargetHelper(aOwnerWindow)
|
|
|
|
, mMediaElement(aMediaElement)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaTrackList::~MediaTrackList()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(MediaTrackList,
|
|
|
|
DOMEventTargetHelper,
|
|
|
|
mTracks,
|
|
|
|
mMediaElement)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(MediaTrackList, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(MediaTrackList, DOMEventTargetHelper)
|
2017-08-30 02:02:48 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MediaTrackList)
|
2014-04-16 07:06:45 +04:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
|
|
|
|
|
|
|
MediaTrack*
|
|
|
|
MediaTrackList::operator[](uint32_t aIndex)
|
|
|
|
{
|
|
|
|
return mTracks.ElementAt(aIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaTrack*
|
|
|
|
MediaTrackList::IndexedGetter(uint32_t aIndex, bool& aFound)
|
|
|
|
{
|
|
|
|
aFound = aIndex < mTracks.Length();
|
2015-08-13 15:22:48 +03:00
|
|
|
if (!aFound) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return mTracks[aIndex];
|
2014-04-16 07:06:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MediaTrack*
|
|
|
|
MediaTrackList::GetTrackById(const nsAString& aId)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mTracks.Length(); ++i) {
|
|
|
|
if (aId.Equals(mTracks[i]->GetId())) {
|
|
|
|
return mTracks[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaTrackList::AddTrack(MediaTrack* aTrack)
|
|
|
|
{
|
2018-04-11 17:27:00 +03:00
|
|
|
MOZ_ASSERT(aTrack->GetOwnerGlobal() == GetOwnerGlobal(),
|
|
|
|
"Where is this track from?");
|
2014-04-16 07:06:45 +04:00
|
|
|
mTracks.AppendElement(aTrack);
|
|
|
|
aTrack->SetTrackList(this);
|
|
|
|
CreateAndDispatchTrackEventRunner(aTrack, NS_LITERAL_STRING("addtrack"));
|
2016-08-24 11:58:04 +03:00
|
|
|
|
|
|
|
if ((!aTrack->AsAudioTrack() || !aTrack->AsAudioTrack()->Enabled()) &&
|
|
|
|
(!aTrack->AsVideoTrack() || !aTrack->AsVideoTrack()->Selected())) {
|
|
|
|
// Track not enabled, no need to notify media element.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HTMLMediaElement* element = GetMediaElement()) {
|
|
|
|
element->NotifyMediaTrackEnabled(aTrack);
|
|
|
|
}
|
2014-04-16 07:06:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-10-18 08:24:48 +03:00
|
|
|
MediaTrackList::RemoveTrack(const RefPtr<MediaTrack>& aTrack)
|
2014-04-16 07:06:45 +04:00
|
|
|
{
|
|
|
|
mTracks.RemoveElement(aTrack);
|
2016-08-24 11:58:04 +03:00
|
|
|
aTrack->SetEnabledInternal(false, MediaTrack::FIRE_NO_EVENTS);
|
2014-04-16 07:06:45 +04:00
|
|
|
aTrack->SetTrackList(nullptr);
|
|
|
|
CreateAndDispatchTrackEventRunner(aTrack, NS_LITERAL_STRING("removetrack"));
|
|
|
|
}
|
|
|
|
|
2014-07-04 07:55:06 +04:00
|
|
|
void
|
|
|
|
MediaTrackList::RemoveTracks()
|
|
|
|
{
|
|
|
|
while (!mTracks.IsEmpty()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaTrack> track = mTracks.LastElement();
|
2014-07-04 07:55:06 +04:00
|
|
|
RemoveTrack(track);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-23 13:34:14 +04:00
|
|
|
already_AddRefed<AudioTrack>
|
2018-04-11 17:27:00 +03:00
|
|
|
MediaTrackList::CreateAudioTrack(nsIGlobalObject* aOwnerGlobal,
|
|
|
|
const nsAString& aId,
|
2014-05-23 13:34:14 +04:00
|
|
|
const nsAString& aKind,
|
|
|
|
const nsAString& aLabel,
|
|
|
|
const nsAString& aLanguage,
|
|
|
|
bool aEnabled)
|
|
|
|
{
|
2018-04-11 17:27:00 +03:00
|
|
|
RefPtr<AudioTrack> track = new AudioTrack(aOwnerGlobal,
|
|
|
|
aId, aKind, aLabel, aLanguage,
|
|
|
|
aEnabled);
|
2014-05-23 13:34:14 +04:00
|
|
|
return track.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<VideoTrack>
|
2018-04-11 17:27:00 +03:00
|
|
|
MediaTrackList::CreateVideoTrack(nsIGlobalObject* aOwnerGlobal,
|
|
|
|
const nsAString& aId,
|
2014-05-23 13:34:14 +04:00
|
|
|
const nsAString& aKind,
|
|
|
|
const nsAString& aLabel,
|
2016-05-10 12:02:15 +03:00
|
|
|
const nsAString& aLanguage,
|
|
|
|
VideoStreamTrack* aVideoTrack)
|
2014-05-23 13:34:14 +04:00
|
|
|
{
|
2018-04-11 17:27:00 +03:00
|
|
|
RefPtr<VideoTrack> track = new VideoTrack(aOwnerGlobal, aId, aKind, aLabel,
|
|
|
|
aLanguage, aVideoTrack);
|
2014-05-23 13:34:14 +04:00
|
|
|
return track.forget();
|
|
|
|
}
|
|
|
|
|
2014-04-16 07:06:45 +04:00
|
|
|
void
|
|
|
|
MediaTrackList::EmptyTracks()
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mTracks.Length(); ++i) {
|
2016-08-24 11:58:04 +03:00
|
|
|
mTracks[i]->SetEnabledInternal(false, MediaTrack::FIRE_NO_EVENTS);
|
2014-04-16 07:06:45 +04:00
|
|
|
mTracks[i]->SetTrackList(nullptr);
|
|
|
|
}
|
|
|
|
mTracks.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaTrackList::CreateAndDispatchChangeEvent()
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AsyncEventDispatcher> asyncDispatcher =
|
2018-06-25 19:23:50 +03:00
|
|
|
new AsyncEventDispatcher(this,
|
|
|
|
NS_LITERAL_STRING("change"),
|
|
|
|
CanBubble::eNo);
|
2014-04-16 07:06:45 +04:00
|
|
|
asyncDispatcher->PostDOMEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaTrackList::CreateAndDispatchTrackEventRunner(MediaTrack* aTrack,
|
|
|
|
const nsAString& aEventName)
|
|
|
|
{
|
|
|
|
TrackEventInit eventInit;
|
|
|
|
|
|
|
|
if (aTrack->AsAudioTrack()) {
|
|
|
|
eventInit.mTrack.SetValue().SetAsAudioTrack() = aTrack->AsAudioTrack();
|
|
|
|
} else if (aTrack->AsVideoTrack()) {
|
|
|
|
eventInit.mTrack.SetValue().SetAsVideoTrack() = aTrack->AsVideoTrack();
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<TrackEvent> event =
|
2014-04-16 07:06:45 +04:00
|
|
|
TrackEvent::Constructor(this, aEventName, eventInit);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AsyncEventDispatcher> asyncDispatcher =
|
2014-04-16 07:06:45 +04:00
|
|
|
new AsyncEventDispatcher(this, event);
|
|
|
|
asyncDispatcher->PostDOMEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|