VideoTrackList.cpp (2797B)
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/. */ 5 6 #include "mozilla/dom/VideoTrackList.h" 7 8 #include "mozilla/dom/VideoTrack.h" 9 #include "mozilla/dom/VideoTrackListBinding.h" 10 11 namespace mozilla::dom { 12 13 JSObject* VideoTrackList::WrapObject(JSContext* aCx, 14 JS::Handle<JSObject*> aGivenProto) { 15 return VideoTrackList_Binding::Wrap(aCx, this, aGivenProto); 16 } 17 18 VideoTrack* VideoTrackList::operator[](uint32_t aIndex) { 19 MediaTrack* track = MediaTrackList::operator[](aIndex); 20 return track->AsVideoTrack(); 21 } 22 23 void VideoTrackList::RemoveTrack(const RefPtr<MediaTrack>& aTrack) { 24 // we need to find the video track before |MediaTrackList::RemoveTrack|. Or 25 // mSelectedIndex will not be valid. The check of mSelectedIndex == -1 26 // need to be done after RemoveTrack. Also the call of 27 // |MediaTrackList::RemoveTrack| is necessary even when mSelectedIndex = -1. 28 bool found; 29 VideoTrack* selectedVideoTrack = IndexedGetter(mSelectedIndex, found); 30 MediaTrackList::RemoveTrack(aTrack); 31 if (mSelectedIndex == -1) { 32 // There was no selected track and we don't select another track on removal. 33 return; 34 } 35 MOZ_ASSERT(found, "When mSelectedIndex is set it should point to a track"); 36 MOZ_ASSERT(selectedVideoTrack, 37 "The mSelectedIndex should be set to video track only"); 38 39 // Let the caller of RemoveTrack deal with choosing the new selected track if 40 // it removes the currently-selected track. 41 if (aTrack == selectedVideoTrack) { 42 mSelectedIndex = -1; 43 return; 44 } 45 46 // The removed track was not the selected track and there is a 47 // currently-selected video track. We need to find the new location of the 48 // selected track. 49 for (size_t ix = 0; ix < mTracks.Length(); ix++) { 50 if (mTracks[ix] == selectedVideoTrack) { 51 mSelectedIndex = ix; 52 return; 53 } 54 } 55 } 56 57 void VideoTrackList::EmptyTracks() { 58 mSelectedIndex = -1; 59 MediaTrackList::EmptyTracks(); 60 } 61 62 VideoTrack* VideoTrackList::GetSelectedTrack() { 63 if (mSelectedIndex < 0 || 64 static_cast<size_t>(mSelectedIndex) >= mTracks.Length()) { 65 return nullptr; 66 } 67 68 return operator[](mSelectedIndex); 69 } 70 71 VideoTrack* VideoTrackList::IndexedGetter(uint32_t aIndex, bool& aFound) { 72 MediaTrack* track = MediaTrackList::IndexedGetter(aIndex, aFound); 73 return track ? track->AsVideoTrack() : nullptr; 74 } 75 76 VideoTrack* VideoTrackList::GetTrackById(const nsAString& aId) { 77 MediaTrack* track = MediaTrackList::GetTrackById(aId); 78 return track ? track->AsVideoTrack() : nullptr; 79 } 80 81 } // namespace mozilla::dom