Bug 1572460 - Refactor `selection` out of the `InspectorFront`. r=yulia
[gecko.git] / dom / media / VideoTrack.cpp
blob4cd6fb4440a96b3e7745daed5067e3a5ea36ff5a
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 {
14 namespace dom {
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) {}
24 VideoTrack::~VideoTrack() = default;
26 NS_IMPL_CYCLE_COLLECTION_INHERITED(VideoTrack, MediaTrack, mVideoStreamTrack)
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)
33 JSObject* VideoTrack::WrapObject(JSContext* aCx,
34 JS::Handle<JSObject*> aGivenProto) {
35 return VideoTrack_Binding::Wrap(aCx, this, aGivenProto);
38 void VideoTrack::SetSelected(bool aSelected) {
39 SetEnabledInternal(aSelected, MediaTrack::DEFAULT);
42 void VideoTrack::SetEnabledInternal(bool aEnabled, int aFlags) {
43 if (aEnabled == mSelected) {
44 return;
47 mSelected = aEnabled;
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;
55 VideoTrackList& list = static_cast<VideoTrackList&>(*mList);
56 if (mSelected) {
57 uint32_t curIndex = 0;
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;
66 VideoTrack* track = list[i];
67 track->SetSelected(false);
70 // Set the index of selected video track to the current's index.
71 list.mSelectedIndex = curIndex;
73 HTMLMediaElement* element = mList->GetMediaElement();
74 if (element) {
75 element->NotifyMediaTrackEnabled(this);
77 } else {
78 list.mSelectedIndex = -1;
80 HTMLMediaElement* element = mList->GetMediaElement();
81 if (element) {
82 element->NotifyMediaTrackDisabled(this);
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();
93 } // namespace dom
94 } // namespace mozilla