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/AudioStreamTrack.h"
8 #include "mozilla/dom/AudioTrack.h"
9 #include "mozilla/dom/AudioTrackBinding.h"
10 #include "mozilla/dom/AudioTrackList.h"
11 #include "mozilla/dom/HTMLMediaElement.h"
12 
13 namespace mozilla::dom {
14 
AudioTrack(nsIGlobalObject * aOwnerGlobal,const nsAString & aId,const nsAString & aKind,const nsAString & aLabel,const nsAString & aLanguage,bool aEnabled,AudioStreamTrack * aStreamTrack)15 AudioTrack::AudioTrack(nsIGlobalObject* aOwnerGlobal, const nsAString& aId,
16                        const nsAString& aKind, const nsAString& aLabel,
17                        const nsAString& aLanguage, bool aEnabled,
18                        AudioStreamTrack* aStreamTrack)
19     : MediaTrack(aOwnerGlobal, aId, aKind, aLabel, aLanguage),
20       mEnabled(aEnabled),
21       mAudioStreamTrack(aStreamTrack) {}
22 
23 AudioTrack::~AudioTrack() = default;
24 
NS_IMPL_CYCLE_COLLECTION_INHERITED(AudioTrack,MediaTrack,mAudioStreamTrack)25 NS_IMPL_CYCLE_COLLECTION_INHERITED(AudioTrack, MediaTrack, mAudioStreamTrack)
26 
27 NS_IMPL_ADDREF_INHERITED(AudioTrack, MediaTrack)
28 NS_IMPL_RELEASE_INHERITED(AudioTrack, MediaTrack)
29 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AudioTrack)
30 NS_INTERFACE_MAP_END_INHERITING(MediaTrack)
31 
32 JSObject* AudioTrack::WrapObject(JSContext* aCx,
33                                  JS::Handle<JSObject*> aGivenProto) {
34   return AudioTrack_Binding::Wrap(aCx, this, aGivenProto);
35 }
36 
SetEnabled(bool aEnabled)37 void AudioTrack::SetEnabled(bool aEnabled) {
38   SetEnabledInternal(aEnabled, MediaTrack::DEFAULT);
39 }
40 
SetEnabledInternal(bool aEnabled,int aFlags)41 void AudioTrack::SetEnabledInternal(bool aEnabled, int aFlags) {
42   if (aEnabled == mEnabled) {
43     return;
44   }
45   mEnabled = aEnabled;
46 
47   // If this AudioTrack is no longer in its original AudioTrackList, then
48   // whether it is enabled or not has no effect on its original list.
49   if (!mList) {
50     return;
51   }
52 
53   if (mEnabled) {
54     HTMLMediaElement* element = mList->GetMediaElement();
55     if (element) {
56       element->NotifyMediaTrackEnabled(this);
57     }
58   } else {
59     HTMLMediaElement* element = mList->GetMediaElement();
60     if (element) {
61       element->NotifyMediaTrackDisabled(this);
62     }
63   }
64 
65   if (!(aFlags & MediaTrack::FIRE_NO_EVENTS)) {
66     mList->CreateAndDispatchChangeEvent();
67   }
68 }
69 
70 }  // namespace mozilla::dom
71