VideoTrack.cpp (2865B)
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/. */ 6 7 #include "mozilla/dom/VideoTrack.h" 8 9 #include "mozilla/dom/HTMLMediaElement.h" 10 #include "mozilla/dom/VideoStreamTrack.h" 11 #include "mozilla/dom/VideoTrackBinding.h" 12 #include "mozilla/dom/VideoTrackList.h" 13 14 namespace mozilla::dom { 15 16 VideoTrack::VideoTrack(nsIGlobalObject* aOwnerGlobal, const nsAString& aId, 17 const nsAString& aKind, const nsAString& aLabel, 18 const nsAString& aLanguage, 19 VideoStreamTrack* aStreamTrack) 20 : MediaTrack(aOwnerGlobal, aId, aKind, aLabel, aLanguage), 21 mSelected(false), 22 mVideoStreamTrack(aStreamTrack) {} 23 24 VideoTrack::~VideoTrack() = default; 25 26 NS_IMPL_CYCLE_COLLECTION_INHERITED(VideoTrack, MediaTrack, mVideoStreamTrack) 27 28 NS_IMPL_ADDREF_INHERITED(VideoTrack, MediaTrack) 29 NS_IMPL_RELEASE_INHERITED(VideoTrack, MediaTrack) 30 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(VideoTrack) 31 NS_INTERFACE_MAP_END_INHERITING(MediaTrack) 32 33 JSObject* VideoTrack::WrapObject(JSContext* aCx, 34 JS::Handle<JSObject*> aGivenProto) { 35 return VideoTrack_Binding::Wrap(aCx, this, aGivenProto); 36 } 37 38 void VideoTrack::SetSelected(bool aSelected) { 39 SetEnabledInternal(aSelected, MediaTrack::DEFAULT); 40 } 41 42 void VideoTrack::SetEnabledInternal(bool aEnabled, int aFlags) { 43 if (aEnabled == mSelected) { 44 return; 45 } 46 47 mSelected = aEnabled; 48 49 // If this VideoTrack is no longer in its original VideoTrackList, then 50 // whether it is selected or not has no effect on its original list. 51 if (!mList) { 52 return; 53 } 54 55 VideoTrackList& list = static_cast<VideoTrackList&>(*mList); 56 if (mSelected) { 57 uint32_t curIndex = 0; 58 59 // Unselect all video tracks except the current one. 60 for (uint32_t i = 0; i < list.Length(); ++i) { 61 if (list[i] == this) { 62 curIndex = i; 63 continue; 64 } 65 66 VideoTrack* track = list[i]; 67 track->SetSelected(false); 68 } 69 70 // Set the index of selected video track to the current's index. 71 list.mSelectedIndex = curIndex; 72 73 HTMLMediaElement* element = mList->GetMediaElement(); 74 if (element) { 75 element->NotifyMediaTrackEnabled(this); 76 } 77 } else { 78 list.mSelectedIndex = -1; 79 80 HTMLMediaElement* element = mList->GetMediaElement(); 81 if (element) { 82 element->NotifyMediaTrackDisabled(this); 83 } 84 } 85 86 // Fire the change event at selection changes on this video track, shall 87 // propose a spec change later. 88 if (!(aFlags & MediaTrack::FIRE_NO_EVENTS)) { 89 list.CreateAndDispatchChangeEvent(); 90 } 91 } 92 93 } // namespace mozilla::dom