MediaTrackList.h 3.3 KB

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