Bug 1890793: Assert CallArgs::newTarget is not gray. r=spidermonkey-reviewers,sfink...
[gecko.git] / dom / media / VideoTrack.cpp
blob57ed30685783a5c061e5476acf8fd3a16a6be48f
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 et tw=78: */
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
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/HTMLMediaElement.h"
8 #include "mozilla/dom/VideoStreamTrack.h"
9 #include "mozilla/dom/VideoTrack.h"
10 #include "mozilla/dom/VideoTrackBinding.h"
11 #include "mozilla/dom/VideoTrackList.h"
13 namespace mozilla::dom {
15 VideoTrack::VideoTrack(nsIGlobalObject* aOwnerGlobal, const nsAString& aId,
16 const nsAString& aKind, const nsAString& aLabel,
17 const nsAString& aLanguage,
18 VideoStreamTrack* aStreamTrack)
19 : MediaTrack(aOwnerGlobal, aId, aKind, aLabel, aLanguage),
20 mSelected(false),
21 mVideoStreamTrack(aStreamTrack) {}
23 VideoTrack::~VideoTrack() = default;
25 NS_IMPL_CYCLE_COLLECTION_INHERITED(VideoTrack, MediaTrack, mVideoStreamTrack)
27 NS_IMPL_ADDREF_INHERITED(VideoTrack, MediaTrack)
28 NS_IMPL_RELEASE_INHERITED(VideoTrack, MediaTrack)
29 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(VideoTrack)
30 NS_INTERFACE_MAP_END_INHERITING(MediaTrack)
32 JSObject* VideoTrack::WrapObject(JSContext* aCx,
33 JS::Handle<JSObject*> aGivenProto) {
34 return VideoTrack_Binding::Wrap(aCx, this, aGivenProto);
37 void VideoTrack::SetSelected(bool aSelected) {
38 SetEnabledInternal(aSelected, MediaTrack::DEFAULT);
41 void VideoTrack::SetEnabledInternal(bool aEnabled, int aFlags) {
42 if (aEnabled == mSelected) {
43 return;
46 mSelected = aEnabled;
48 // If this VideoTrack is no longer in its original VideoTrackList, then
49 // whether it is selected or not has no effect on its original list.
50 if (!mList) {
51 return;
54 VideoTrackList& list = static_cast<VideoTrackList&>(*mList);
55 if (mSelected) {
56 uint32_t curIndex = 0;
58 // Unselect all video tracks except the current one.
59 for (uint32_t i = 0; i < list.Length(); ++i) {
60 if (list[i] == this) {
61 curIndex = i;
62 continue;
65 VideoTrack* track = list[i];
66 track->SetSelected(false);
69 // Set the index of selected video track to the current's index.
70 list.mSelectedIndex = curIndex;
72 HTMLMediaElement* element = mList->GetMediaElement();
73 if (element) {
74 element->NotifyMediaTrackEnabled(this);
76 } else {
77 list.mSelectedIndex = -1;
79 HTMLMediaElement* element = mList->GetMediaElement();
80 if (element) {
81 element->NotifyMediaTrackDisabled(this);
85 // Fire the change event at selection changes on this video track, shall
86 // propose a spec change later.
87 if (!(aFlags & MediaTrack::FIRE_NO_EVENTS)) {
88 list.CreateAndDispatchChangeEvent();
92 } // namespace mozilla::dom