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 #ifndef mozilla_dom_MediaTrackList_h
8 #define mozilla_dom_MediaTrackList_h
9 
10 #include "mozilla/DOMEventTargetHelper.h"
11 
12 namespace mozilla {
13 class DOMMediaStream;
14 
15 namespace dom {
16 
17 class AudioStreamTrack;
18 class AudioTrack;
19 class AudioTrackList;
20 class HTMLMediaElement;
21 class MediaTrack;
22 class VideoStreamTrack;
23 class VideoTrack;
24 class VideoTrackList;
25 
26 /**
27  * Base class of AudioTrackList and VideoTrackList. The AudioTrackList and
28  * VideoTrackList objects represent a dynamic list of zero or more audio and
29  * video tracks respectively.
30  *
31  * When a media element is to forget its media-resource-specific tracks, its
32  * audio track list and video track list will be emptied.
33  */
34 class MediaTrackList : public DOMEventTargetHelper {
35  public:
36   MediaTrackList(nsIGlobalObject* aOwnerObject,
37                  HTMLMediaElement* aMediaElement);
38 
39   NS_DECL_ISUPPORTS_INHERITED
40   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(MediaTrackList, DOMEventTargetHelper)
41 
42   using DOMEventTargetHelper::DispatchTrustedEvent;
43 
44   // The return value is non-null, assert an error if aIndex is out of bound
45   // for array mTracks.
46   MediaTrack* operator[](uint32_t aIndex);
47 
48   // The track must be from the same Window as this MediaTrackList.
49   void AddTrack(MediaTrack* aTrack);
50 
51   // In remove track case, the VideoTrackList::mSelectedIndex should be updated
52   // due to mTracks changed. No need to take care this in add track case.
53   virtual void RemoveTrack(const RefPtr<MediaTrack>& aTrack);
54 
55   void RemoveTracks();
56 
57   // For the case of src of HTMLMediaElement is non-MediaStream, leave the
58   // aAudioTrack as default(nullptr).
59   static already_AddRefed<AudioTrack> CreateAudioTrack(
60       nsIGlobalObject* aOwnerGlobal, const nsAString& aId,
61       const nsAString& aKind, const nsAString& aLabel,
62       const nsAString& aLanguage, bool aEnabled,
63       AudioStreamTrack* aAudioTrack = nullptr);
64 
65   // For the case of src of HTMLMediaElement is non-MediaStream, leave the
66   // aVideoTrack as default(nullptr).
67   static already_AddRefed<VideoTrack> CreateVideoTrack(
68       nsIGlobalObject* aOwnerGlobal, const nsAString& aId,
69       const nsAString& aKind, const nsAString& aLabel,
70       const nsAString& aLanguage, VideoStreamTrack* aVideoTrack = nullptr);
71 
72   virtual void EmptyTracks();
73 
74   void CreateAndDispatchChangeEvent();
75 
76   // WebIDL
77   MediaTrack* IndexedGetter(uint32_t aIndex, bool& aFound);
78 
79   MediaTrack* GetTrackById(const nsAString& aId);
80 
IsEmpty()81   bool IsEmpty() const { return mTracks.IsEmpty(); }
82 
Length()83   uint32_t Length() const { return mTracks.Length(); }
84 
85   IMPL_EVENT_HANDLER(change)
86   IMPL_EVENT_HANDLER(addtrack)
87   IMPL_EVENT_HANDLER(removetrack)
88 
89   friend class AudioTrack;
90   friend class VideoTrack;
91 
92  protected:
93   virtual ~MediaTrackList();
94 
95   void CreateAndDispatchTrackEventRunner(MediaTrack* aTrack,
96                                          const nsAString& aEventName);
97 
AsAudioTrackList()98   virtual AudioTrackList* AsAudioTrackList() { return nullptr; }
99 
AsVideoTrackList()100   virtual VideoTrackList* AsVideoTrackList() { return nullptr; }
101 
GetMediaElement()102   HTMLMediaElement* GetMediaElement() { return mMediaElement; }
103 
104   nsTArray<RefPtr<MediaTrack>> mTracks;
105   RefPtr<HTMLMediaElement> mMediaElement;
106 };
107 
108 }  // namespace dom
109 }  // namespace mozilla
110 
111 #endif  // mozilla_dom_MediaTrackList_h
112