1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "mozilla/dom/VideoTrack.h"
7 #include "mozilla/dom/VideoTrackList.h"
8 #include "mozilla/dom/VideoTrackListBinding.h"
10 namespace mozilla::dom
{
12 JSObject
* VideoTrackList::WrapObject(JSContext
* aCx
,
13 JS::Handle
<JSObject
*> aGivenProto
) {
14 return VideoTrackList_Binding::Wrap(aCx
, this, aGivenProto
);
17 VideoTrack
* VideoTrackList::operator[](uint32_t aIndex
) {
18 MediaTrack
* track
= MediaTrackList::operator[](aIndex
);
19 return track
->AsVideoTrack();
22 void VideoTrackList::RemoveTrack(const RefPtr
<MediaTrack
>& aTrack
) {
23 // we need to find the video track before |MediaTrackList::RemoveTrack|. Or
24 // mSelectedIndex will not be valid. The check of mSelectedIndex == -1
25 // need to be done after RemoveTrack. Also the call of
26 // |MediaTrackList::RemoveTrack| is necessary even when mSelectedIndex = -1.
28 VideoTrack
* selectedVideoTrack
= IndexedGetter(mSelectedIndex
, found
);
29 MediaTrackList::RemoveTrack(aTrack
);
30 if (mSelectedIndex
== -1) {
31 // There was no selected track and we don't select another track on removal.
34 MOZ_ASSERT(found
, "When mSelectedIndex is set it should point to a track");
35 MOZ_ASSERT(selectedVideoTrack
,
36 "The mSelectedIndex should be set to video track only");
38 // Let the caller of RemoveTrack deal with choosing the new selected track if
39 // it removes the currently-selected track.
40 if (aTrack
== selectedVideoTrack
) {
45 // The removed track was not the selected track and there is a
46 // currently-selected video track. We need to find the new location of the
48 for (size_t ix
= 0; ix
< mTracks
.Length(); ix
++) {
49 if (mTracks
[ix
] == selectedVideoTrack
) {
56 void VideoTrackList::EmptyTracks() {
58 MediaTrackList::EmptyTracks();
61 VideoTrack
* VideoTrackList::GetSelectedTrack() {
62 if (mSelectedIndex
< 0 ||
63 static_cast<size_t>(mSelectedIndex
) >= mTracks
.Length()) {
67 return operator[](mSelectedIndex
);
70 VideoTrack
* VideoTrackList::IndexedGetter(uint32_t aIndex
, bool& aFound
) {
71 MediaTrack
* track
= MediaTrackList::IndexedGetter(aIndex
, aFound
);
72 return track
? track
->AsVideoTrack() : nullptr;
75 VideoTrack
* VideoTrackList::GetTrackById(const nsAString
& aId
) {
76 MediaTrack
* track
= MediaTrackList::GetTrackById(aId
);
77 return track
? track
->AsVideoTrack() : nullptr;
80 } // namespace mozilla::dom