HTMLMediaElement.h 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_dom_HTMLMediaElement_h
  6. #define mozilla_dom_HTMLMediaElement_h
  7. #include "nsAutoPtr.h"
  8. #include "nsIDOMHTMLMediaElement.h"
  9. #include "nsGenericHTMLElement.h"
  10. #include "MediaDecoderOwner.h"
  11. #include "nsCycleCollectionParticipant.h"
  12. #include "nsIObserver.h"
  13. #include "mozilla/CORSMode.h"
  14. #include "DecoderTraits.h"
  15. #include "nsIAudioChannelAgent.h"
  16. #include "mozilla/Attributes.h"
  17. #include "mozilla/dom/TextTrackManager.h"
  18. #include "mozilla/WeakPtr.h"
  19. #include "MediaDecoder.h"
  20. #ifdef MOZ_EME
  21. #include "mozilla/dom/MediaKeys.h"
  22. #endif
  23. #include "mozilla/StateWatching.h"
  24. #include "nsGkAtoms.h"
  25. #include "PrincipalChangeObserver.h"
  26. // X.h on Linux #defines CurrentTime as 0L, so we have to #undef it here.
  27. #ifdef CurrentTime
  28. #undef CurrentTime
  29. #endif
  30. #include "mozilla/dom/HTMLMediaElementBinding.h"
  31. // Define to output information on decoding and painting framerate
  32. /* #define DEBUG_FRAME_RATE 1 */
  33. typedef uint16_t nsMediaNetworkState;
  34. typedef uint16_t nsMediaReadyState;
  35. typedef uint32_t SuspendTypes;
  36. typedef uint32_t AudibleChangedReasons;
  37. typedef uint8_t AudibleState;
  38. namespace mozilla {
  39. class DecoderDoctorDiagnostics;
  40. class DOMMediaStream;
  41. class ErrorResult;
  42. class MediaResource;
  43. class MediaDecoder;
  44. class VideoFrameContainer;
  45. namespace dom {
  46. class AudioChannelAgent;
  47. class MediaKeys;
  48. class TextTrack;
  49. class TimeRanges;
  50. class WakeLock;
  51. class MediaTrack;
  52. class MediaStreamTrack;
  53. class VideoStreamTrack;
  54. } // namespace dom
  55. } // namespace mozilla
  56. class AutoNotifyAudioChannelAgent;
  57. class nsIChannel;
  58. class nsIHttpChannel;
  59. class nsILoadGroup;
  60. class nsIRunnable;
  61. class nsITimer;
  62. class nsRange;
  63. namespace mozilla {
  64. namespace dom {
  65. // Number of milliseconds between timeupdate events as defined by spec
  66. #define TIMEUPDATE_MS 250
  67. class MediaError;
  68. class MediaSource;
  69. class Promise;
  70. class TextTrackList;
  71. class AudioTrackList;
  72. class VideoTrackList;
  73. class HTMLMediaElement : public nsGenericHTMLElement,
  74. public nsIDOMHTMLMediaElement,
  75. public MediaDecoderOwner,
  76. public nsIAudioChannelAgentCallback,
  77. public PrincipalChangeObserver<DOMMediaStream>,
  78. public SupportsWeakPtr<HTMLMediaElement>
  79. {
  80. friend AutoNotifyAudioChannelAgent;
  81. public:
  82. typedef mozilla::TimeStamp TimeStamp;
  83. typedef mozilla::layers::ImageContainer ImageContainer;
  84. typedef mozilla::VideoFrameContainer VideoFrameContainer;
  85. typedef mozilla::MediaStream MediaStream;
  86. typedef mozilla::MediaResource MediaResource;
  87. typedef mozilla::MediaDecoderOwner MediaDecoderOwner;
  88. typedef mozilla::MetadataTags MetadataTags;
  89. MOZ_DECLARE_WEAKREFERENCE_TYPENAME(HTMLMediaElement)
  90. CORSMode GetCORSMode() {
  91. return mCORSMode;
  92. }
  93. explicit HTMLMediaElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo);
  94. /**
  95. * This is used when the browser is constructing a video element to play
  96. * a channel that we've already started loading. The src attribute and
  97. * <source> children are ignored.
  98. * @param aChannel the channel to use
  99. * @param aListener returns a stream listener that should receive
  100. * notifications for the stream
  101. */
  102. nsresult LoadWithChannel(nsIChannel *aChannel, nsIStreamListener **aListener);
  103. // nsIDOMHTMLMediaElement
  104. NS_DECL_NSIDOMHTMLMEDIAELEMENT
  105. NS_DECL_NSIAUDIOCHANNELAGENTCALLBACK
  106. // nsISupports
  107. NS_DECL_ISUPPORTS_INHERITED
  108. NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(HTMLMediaElement,
  109. nsGenericHTMLElement)
  110. virtual bool ParseAttribute(int32_t aNamespaceID,
  111. nsIAtom* aAttribute,
  112. const nsAString& aValue,
  113. nsAttrValue& aResult) override;
  114. virtual nsresult BindToTree(nsIDocument* aDocument, nsIContent* aParent,
  115. nsIContent* aBindingParent,
  116. bool aCompileEventHandlers) override;
  117. virtual void UnbindFromTree(bool aDeep = true,
  118. bool aNullParent = true) override;
  119. virtual void DoneCreatingElement() override;
  120. virtual bool IsHTMLFocusable(bool aWithMouse, bool *aIsFocusable,
  121. int32_t *aTabIndex) override;
  122. virtual int32_t TabIndexDefault() override;
  123. /**
  124. * Call this to reevaluate whether we should start/stop due to our owner
  125. * document being active, inactive, visible or hidden.
  126. */
  127. virtual void NotifyOwnerDocumentActivityChanged();
  128. // Called by the video decoder object, on the main thread,
  129. // when it has read the metadata containing video dimensions,
  130. // etc.
  131. virtual void MetadataLoaded(const MediaInfo* aInfo,
  132. nsAutoPtr<const MetadataTags> aTags) final override;
  133. // Called by the decoder object, on the main thread,
  134. // when it has read the first frame of the video or audio.
  135. virtual void FirstFrameLoaded() final override;
  136. // Called by the video decoder object, on the main thread,
  137. // when the resource has a network error during loading.
  138. virtual void NetworkError() final override;
  139. // Called by the video decoder object, on the main thread, when the
  140. // resource has a decode error during metadata loading or decoding.
  141. virtual void DecodeError(const MediaResult& aError) final override;
  142. // Return true if error attribute is not null.
  143. virtual bool HasError() const final override;
  144. // Called by the video decoder object, on the main thread, when the
  145. // resource load has been cancelled.
  146. virtual void LoadAborted() final override;
  147. // Called by the video decoder object, on the main thread,
  148. // when the video playback has ended.
  149. virtual void PlaybackEnded() final override;
  150. // Called by the video decoder object, on the main thread,
  151. // when the resource has started seeking.
  152. virtual void SeekStarted() final override;
  153. // Called by the video decoder object, on the main thread,
  154. // when the resource has completed seeking.
  155. virtual void SeekCompleted() final override;
  156. // Called by the media stream, on the main thread, when the download
  157. // has been suspended by the cache or because the element itself
  158. // asked the decoder to suspend the download.
  159. virtual void DownloadSuspended() final override;
  160. // Called by the media stream, on the main thread, when the download
  161. // has been resumed by the cache or because the element itself
  162. // asked the decoder to resumed the download.
  163. // If aForceNetworkLoading is True, ignore the fact that the download has
  164. // previously finished. We are downloading the middle of the media after
  165. // having downloaded the end, we need to notify the element a download in
  166. // ongoing.
  167. virtual void DownloadResumed(bool aForceNetworkLoading = false) final override;
  168. // Called to indicate the download is progressing.
  169. virtual void DownloadProgressed() final override;
  170. // Called by the media decoder to indicate whether the media cache has
  171. // suspended the channel.
  172. virtual void NotifySuspendedByCache(bool aIsSuspended) final override;
  173. virtual bool IsActive() const final override;
  174. virtual bool IsHidden() const final override;
  175. // Called by the media decoder and the video frame to get the
  176. // ImageContainer containing the video data.
  177. virtual VideoFrameContainer* GetVideoFrameContainer() final override;
  178. layers::ImageContainer* GetImageContainer();
  179. // From PrincipalChangeObserver<DOMMediaStream>.
  180. void PrincipalChanged(DOMMediaStream* aStream) override;
  181. void UpdateSrcStreamVideoPrincipal(const PrincipalHandle& aPrincipalHandle);
  182. // Called after the MediaStream we're playing rendered a frame to aContainer
  183. // with a different principalHandle than the previous frame.
  184. void PrincipalHandleChangedForVideoFrameContainer(VideoFrameContainer* aContainer,
  185. const PrincipalHandle& aNewPrincipalHandle);
  186. // Dispatch events
  187. virtual nsresult DispatchAsyncEvent(const nsAString& aName) final override;
  188. // Triggers a recomputation of readyState.
  189. void UpdateReadyState() override { UpdateReadyStateInternal(); }
  190. // Dispatch events that were raised while in the bfcache
  191. nsresult DispatchPendingMediaEvents();
  192. // Return true if we can activate autoplay assuming enough data has arrived.
  193. bool CanActivateAutoplay();
  194. // Notify that state has changed that might cause an autoplay element to
  195. // start playing.
  196. // If the element is 'autoplay' and is ready to play back (not paused,
  197. // autoplay pref enabled, etc), it should start playing back.
  198. void CheckAutoplayDataReady();
  199. // Check if the media element had crossorigin set when loading started
  200. bool ShouldCheckAllowOrigin();
  201. // Returns true if the currently loaded resource is CORS same-origin with
  202. // respect to the document.
  203. bool IsCORSSameOrigin();
  204. // Is the media element potentially playing as defined by the HTML 5 specification.
  205. // http://www.whatwg.org/specs/web-apps/current-work/#potentially-playing
  206. bool IsPotentiallyPlaying() const;
  207. // Has playback ended as defined by the HTML 5 specification.
  208. // http://www.whatwg.org/specs/web-apps/current-work/#ended
  209. bool IsPlaybackEnded() const;
  210. // principal of the currently playing resource. Anything accessing the contents
  211. // of this element must have a principal that subsumes this principal.
  212. // Returns null if nothing is playing.
  213. already_AddRefed<nsIPrincipal> GetCurrentPrincipal();
  214. // Principal of the currently playing video resource. Anything accessing the
  215. // image container of this element must have a principal that subsumes this
  216. // principal. If there are no live video tracks but content has been rendered
  217. // to the image container, we return the last video principal we had. Should
  218. // the image container be empty with no live video tracks, we return nullptr.
  219. already_AddRefed<nsIPrincipal> GetCurrentVideoPrincipal();
  220. // called to notify that the principal of the decoder's media resource has changed.
  221. void NotifyDecoderPrincipalChanged() final override;
  222. // An interface for observing principal changes on the media elements
  223. // MediaDecoder. This will also be notified if the active CORSMode changes.
  224. class DecoderPrincipalChangeObserver
  225. {
  226. public:
  227. virtual void NotifyDecoderPrincipalChanged() = 0;
  228. };
  229. /**
  230. * Add a DecoderPrincipalChangeObserver to this media element.
  231. *
  232. * Ownership of the DecoderPrincipalChangeObserver remains with the caller,
  233. * and it's the caller's responsibility to remove the observer before it dies.
  234. */
  235. void AddDecoderPrincipalChangeObserver(DecoderPrincipalChangeObserver* aObserver);
  236. /**
  237. * Remove an added DecoderPrincipalChangeObserver from this media element.
  238. *
  239. * Returns true if it was successfully removed.
  240. */
  241. bool RemoveDecoderPrincipalChangeObserver(DecoderPrincipalChangeObserver* aObserver);
  242. class StreamCaptureTrackSource;
  243. class DecoderCaptureTrackSource;
  244. class CaptureStreamTrackSourceGetter;
  245. // Update the visual size of the media. Called from the decoder on the
  246. // main thread when/if the size changes.
  247. void UpdateMediaSize(const nsIntSize& aSize);
  248. // Like UpdateMediaSize, but only updates the size if no size has yet
  249. // been set.
  250. void UpdateInitialMediaSize(const nsIntSize& aSize);
  251. // Returns the CanPlayStatus indicating if we can handle the
  252. // full MIME type including the optional codecs parameter.
  253. static CanPlayStatus GetCanPlay(const nsAString& aType,
  254. DecoderDoctorDiagnostics* aDiagnostics);
  255. /**
  256. * Called when a child source element is added to this media element. This
  257. * may queue a task to run the select resource algorithm if appropriate.
  258. */
  259. void NotifyAddedSource();
  260. /**
  261. * Called when there's been an error fetching the resource. This decides
  262. * whether it's appropriate to fire an error event.
  263. */
  264. void NotifyLoadError();
  265. /**
  266. * Called by one of our associated MediaTrackLists (audio/video) when an
  267. * AudioTrack is enabled or a VideoTrack is selected.
  268. */
  269. void NotifyMediaTrackEnabled(MediaTrack* aTrack);
  270. /**
  271. * Called by one of our associated MediaTrackLists (audio/video) when an
  272. * AudioTrack is disabled or a VideoTrack is unselected.
  273. */
  274. void NotifyMediaTrackDisabled(MediaTrack* aTrack);
  275. /**
  276. * Called when tracks become available to the source media stream.
  277. */
  278. void NotifyMediaStreamTracksAvailable(DOMMediaStream* aStream);
  279. /**
  280. * Called when a captured MediaStreamTrack is stopped so we can clean up its
  281. * MediaInputPort.
  282. */
  283. void NotifyOutputTrackStopped(DOMMediaStream* aOwningStream,
  284. TrackID aDestinationTrackID);
  285. virtual bool IsNodeOfType(uint32_t aFlags) const override;
  286. /**
  287. * Returns the current load ID. Asynchronous events store the ID that was
  288. * current when they were enqueued, and if it has changed when they come to
  289. * fire, they consider themselves cancelled, and don't fire.
  290. */
  291. uint32_t GetCurrentLoadID() { return mCurrentLoadID; }
  292. /**
  293. * Returns the load group for this media element's owner document.
  294. * XXX XBL2 issue.
  295. */
  296. already_AddRefed<nsILoadGroup> GetDocumentLoadGroup();
  297. /**
  298. * Returns true if the media has played or completed a seek.
  299. * Used by video frame to determine whether to paint the poster.
  300. */
  301. bool GetPlayedOrSeeked() const { return mHasPlayedOrSeeked; }
  302. nsresult CopyInnerTo(Element* aDest);
  303. /**
  304. * Sets the Accept header on the HTTP channel to the required
  305. * video or audio MIME types.
  306. */
  307. virtual nsresult SetAcceptHeader(nsIHttpChannel* aChannel) = 0;
  308. /**
  309. * Sets the required request headers on the HTTP channel for
  310. * video or audio requests.
  311. */
  312. void SetRequestHeaders(nsIHttpChannel* aChannel);
  313. /**
  314. * Asynchronously awaits a stable state, whereupon aRunnable runs on the main
  315. * thread. This adds an event which run aRunnable to the appshell's list of
  316. * sections synchronous the next time control returns to the event loop.
  317. */
  318. void RunInStableState(nsIRunnable* aRunnable);
  319. /**
  320. * Fires a timeupdate event. If aPeriodic is true, the event will only
  321. * be fired if we've not fired a timeupdate event (for any reason) in the
  322. * last 250ms, as required by the spec when the current time is periodically
  323. * increasing during playback.
  324. */
  325. virtual void FireTimeUpdate(bool aPeriodic) final override;
  326. /**
  327. * This will return null if mSrcStream is null, or if mSrcStream is not
  328. * null but its GetPlaybackStream() returns null --- which can happen during
  329. * cycle collection unlinking!
  330. */
  331. MediaStream* GetSrcMediaStream() const;
  332. // WebIDL
  333. MediaError* GetError() const;
  334. // XPCOM GetSrc() is OK
  335. void SetSrc(const nsAString& aSrc, ErrorResult& aRv)
  336. {
  337. SetHTMLAttr(nsGkAtoms::src, aSrc, aRv);
  338. }
  339. // XPCOM GetCurrentSrc() is OK
  340. void GetCrossOrigin(nsAString& aResult)
  341. {
  342. // Null for both missing and invalid defaults is ok, since we
  343. // always parse to an enum value, so we don't need an invalid
  344. // default, and we _want_ the missing default to be null.
  345. GetEnumAttr(nsGkAtoms::crossorigin, nullptr, aResult);
  346. }
  347. void SetCrossOrigin(const nsAString& aCrossOrigin, ErrorResult& aError)
  348. {
  349. SetOrRemoveNullableStringAttr(nsGkAtoms::crossorigin, aCrossOrigin, aError);
  350. }
  351. uint16_t NetworkState() const
  352. {
  353. return mNetworkState;
  354. }
  355. void NotifyXPCOMShutdown() final override;
  356. // Called by media decoder when the audible state changed or when input is
  357. // a media stream.
  358. virtual void SetAudibleState(bool aAudible) final override;
  359. // Notify agent when the MediaElement changes its audible state.
  360. void NotifyAudioPlaybackChanged(AudibleChangedReasons aReason);
  361. // XPCOM GetPreload() is OK
  362. void SetPreload(const nsAString& aValue, ErrorResult& aRv)
  363. {
  364. SetHTMLAttr(nsGkAtoms::preload, aValue, aRv);
  365. }
  366. already_AddRefed<TimeRanges> Buffered() const;
  367. // XPCOM Load() is OK
  368. // XPCOM CanPlayType() is OK
  369. uint16_t ReadyState() const
  370. {
  371. return mReadyState;
  372. }
  373. bool Seeking() const;
  374. double CurrentTime() const;
  375. void SetCurrentTime(double aCurrentTime, ErrorResult& aRv);
  376. void FastSeek(double aTime, ErrorResult& aRv);
  377. already_AddRefed<Promise> SeekToNextFrame(ErrorResult& aRv);
  378. double Duration() const;
  379. bool HasAudio() const
  380. {
  381. return mMediaInfo.HasAudio();
  382. }
  383. bool HasVideo() const
  384. {
  385. return mMediaInfo.HasVideo();
  386. }
  387. bool IsEncrypted() const
  388. {
  389. return mIsEncrypted;
  390. }
  391. bool Paused() const
  392. {
  393. return mPaused;
  394. }
  395. double DefaultPlaybackRate() const
  396. {
  397. return mDefaultPlaybackRate;
  398. }
  399. void SetDefaultPlaybackRate(double aDefaultPlaybackRate, ErrorResult& aRv);
  400. double PlaybackRate() const
  401. {
  402. return mPlaybackRate;
  403. }
  404. void SetPlaybackRate(double aPlaybackRate, ErrorResult& aRv);
  405. already_AddRefed<TimeRanges> Played();
  406. already_AddRefed<TimeRanges> Seekable() const;
  407. bool Ended();
  408. bool Autoplay() const
  409. {
  410. return GetBoolAttr(nsGkAtoms::autoplay);
  411. }
  412. void SetAutoplay(bool aValue, ErrorResult& aRv)
  413. {
  414. SetHTMLBoolAttr(nsGkAtoms::autoplay, aValue, aRv);
  415. }
  416. bool Loop() const
  417. {
  418. return GetBoolAttr(nsGkAtoms::loop);
  419. }
  420. void SetLoop(bool aValue, ErrorResult& aRv)
  421. {
  422. SetHTMLBoolAttr(nsGkAtoms::loop, aValue, aRv);
  423. }
  424. already_AddRefed<Promise> Play(ErrorResult& aRv);
  425. void Pause(ErrorResult& aRv);
  426. bool Controls() const
  427. {
  428. return GetBoolAttr(nsGkAtoms::controls);
  429. }
  430. void SetControls(bool aValue, ErrorResult& aRv)
  431. {
  432. SetHTMLBoolAttr(nsGkAtoms::controls, aValue, aRv);
  433. }
  434. double Volume() const
  435. {
  436. return mVolume;
  437. }
  438. void SetVolume(double aVolume, ErrorResult& aRv);
  439. bool Muted() const
  440. {
  441. return mMuted & MUTED_BY_CONTENT;
  442. }
  443. // XPCOM SetMuted() is OK
  444. bool DefaultMuted() const
  445. {
  446. return GetBoolAttr(nsGkAtoms::muted);
  447. }
  448. void SetDefaultMuted(bool aMuted, ErrorResult& aRv)
  449. {
  450. SetHTMLBoolAttr(nsGkAtoms::muted, aMuted, aRv);
  451. }
  452. bool MozAllowCasting() const
  453. {
  454. return mAllowCasting;
  455. }
  456. void SetMozAllowCasting(bool aShow)
  457. {
  458. mAllowCasting = aShow;
  459. }
  460. bool MozIsCasting() const
  461. {
  462. return mIsCasting;
  463. }
  464. void SetMozIsCasting(bool aShow)
  465. {
  466. mIsCasting = aShow;
  467. }
  468. already_AddRefed<MediaSource> GetMozMediaSourceObject() const;
  469. // Returns a string describing the state of the media player internal
  470. // data. Used for debugging purposes.
  471. void GetMozDebugReaderData(nsAString& aString);
  472. void MozDumpDebugInfo();
  473. void SetVisible(bool aVisible);
  474. already_AddRefed<DOMMediaStream> GetSrcObject() const;
  475. void SetSrcObject(DOMMediaStream& aValue);
  476. void SetSrcObject(DOMMediaStream* aValue);
  477. // TODO: remove prefixed versions soon (1183495).
  478. already_AddRefed<DOMMediaStream> GetMozSrcObject() const;
  479. void SetMozSrcObject(DOMMediaStream& aValue);
  480. void SetMozSrcObject(DOMMediaStream* aValue);
  481. bool MozPreservesPitch() const
  482. {
  483. return mPreservesPitch;
  484. }
  485. // XPCOM MozPreservesPitch() is OK
  486. #ifdef MOZ_EME
  487. MediaKeys* GetMediaKeys() const;
  488. already_AddRefed<Promise> SetMediaKeys(MediaKeys* mediaKeys,
  489. ErrorResult& aRv);
  490. mozilla::dom::EventHandlerNonNull* GetOnencrypted();
  491. void SetOnencrypted(mozilla::dom::EventHandlerNonNull* aCallback);
  492. mozilla::dom::EventHandlerNonNull* GetOnwaitingforkey();
  493. void SetOnwaitingforkey(mozilla::dom::EventHandlerNonNull* aCallback);
  494. void DispatchEncrypted(const nsTArray<uint8_t>& aInitData,
  495. const nsAString& aInitDataType) override;
  496. bool IsEventAttributeName(nsIAtom* aName) override;
  497. // Returns the principal of the "top level" document; the origin displayed
  498. // in the URL bar of the browser window.
  499. already_AddRefed<nsIPrincipal> GetTopLevelPrincipal();
  500. bool ContainsRestrictedContent();
  501. #endif // MOZ_EME
  502. void CannotDecryptWaitingForKey();
  503. bool MozAutoplayEnabled() const
  504. {
  505. return mAutoplayEnabled;
  506. }
  507. already_AddRefed<DOMMediaStream> CaptureAudio(ErrorResult& aRv,
  508. MediaStreamGraph* aGraph);
  509. already_AddRefed<DOMMediaStream> MozCaptureStream(ErrorResult& aRv);
  510. already_AddRefed<DOMMediaStream> MozCaptureStreamUntilEnded(ErrorResult& aRv);
  511. bool MozAudioCaptured() const
  512. {
  513. return mAudioCaptured;
  514. }
  515. /**
  516. * Ensures any MediaStreamTracks captured from a MediaDecoder are ended.
  517. */
  518. void EndPreCreatedCapturedDecoderTracks();
  519. void MozGetMetadata(JSContext* aCx, JS::MutableHandle<JSObject*> aResult,
  520. ErrorResult& aRv);
  521. double MozFragmentEnd();
  522. AudioChannel MozAudioChannelType() const
  523. {
  524. return mAudioChannel;
  525. }
  526. void SetMozAudioChannelType(AudioChannel aValue, ErrorResult& aRv);
  527. AudioTrackList* AudioTracks();
  528. VideoTrackList* VideoTracks();
  529. TextTrackList* GetTextTracks();
  530. already_AddRefed<TextTrack> AddTextTrack(TextTrackKind aKind,
  531. const nsAString& aLabel,
  532. const nsAString& aLanguage);
  533. void AddTextTrack(TextTrack* aTextTrack) {
  534. GetOrCreateTextTrackManager()->AddTextTrack(aTextTrack);
  535. }
  536. void RemoveTextTrack(TextTrack* aTextTrack, bool aPendingListOnly = false) {
  537. if (mTextTrackManager) {
  538. mTextTrackManager->RemoveTextTrack(aTextTrack, aPendingListOnly);
  539. }
  540. }
  541. void NotifyCueAdded(TextTrackCue& aCue) {
  542. if (mTextTrackManager) {
  543. mTextTrackManager->NotifyCueAdded(aCue);
  544. }
  545. }
  546. void NotifyCueRemoved(TextTrackCue& aCue) {
  547. if (mTextTrackManager) {
  548. mTextTrackManager->NotifyCueRemoved(aCue);
  549. }
  550. }
  551. void NotifyCueUpdated(TextTrackCue *aCue) {
  552. if (mTextTrackManager) {
  553. mTextTrackManager->NotifyCueUpdated(aCue);
  554. }
  555. }
  556. void NotifyCueDisplayStatesChanged();
  557. bool GetHasUserInteraction()
  558. {
  559. return mHasUserInteraction;
  560. }
  561. // A method to check whether we are currently playing.
  562. bool IsCurrentlyPlaying() const;
  563. // Returns true if the media element is being destroyed. Used in
  564. // dormancy checks to prevent dormant processing for an element
  565. // that will soon be gone.
  566. bool IsBeingDestroyed();
  567. IMPL_EVENT_HANDLER(mozinterruptbegin)
  568. IMPL_EVENT_HANDLER(mozinterruptend)
  569. // These are used for testing only
  570. float ComputedVolume() const;
  571. bool ComputedMuted() const;
  572. nsSuspendedTypes ComputedSuspended() const;
  573. void SetMediaInfo(const MediaInfo& aInfo);
  574. // Telemetry: to record the usage of a {visible / invisible} video element as
  575. // the source of {drawImage(), createPattern(), createImageBitmap() and
  576. // captureStream()} APIs.
  577. enum class CallerAPI {
  578. DRAW_IMAGE,
  579. CREATE_PATTERN,
  580. CREATE_IMAGEBITMAP,
  581. CAPTURE_STREAM,
  582. };
  583. void MarkAsContentSource(CallerAPI aAPI);
  584. protected:
  585. virtual ~HTMLMediaElement();
  586. class ChannelLoader;
  587. class ErrorSink;
  588. class MediaLoadListener;
  589. class MediaStreamTracksAvailableCallback;
  590. class MediaStreamTrackListener;
  591. class StreamListener;
  592. class StreamSizeListener;
  593. class ShutdownObserver;
  594. MediaDecoderOwner::NextFrameStatus NextFrameStatus();
  595. void SetDecoder(MediaDecoder* aDecoder) {
  596. MOZ_ASSERT(aDecoder); // Use ShutdownDecoder() to clear.
  597. if (mDecoder) {
  598. ShutdownDecoder();
  599. }
  600. mDecoder = aDecoder;
  601. }
  602. class WakeLockBoolWrapper {
  603. public:
  604. explicit WakeLockBoolWrapper(bool val = false)
  605. : mValue(val), mCanPlay(true), mOuter(nullptr) {}
  606. ~WakeLockBoolWrapper();
  607. void SetOuter(HTMLMediaElement* outer) { mOuter = outer; }
  608. void SetCanPlay(bool aCanPlay);
  609. MOZ_IMPLICIT operator bool() const { return mValue; }
  610. WakeLockBoolWrapper& operator=(bool val);
  611. bool operator !() const { return !mValue; }
  612. static void TimerCallback(nsITimer* aTimer, void* aClosure);
  613. private:
  614. void UpdateWakeLock();
  615. bool mValue;
  616. bool mCanPlay;
  617. HTMLMediaElement* mOuter;
  618. nsCOMPtr<nsITimer> mTimer;
  619. };
  620. // Holds references to the DOM wrappers for the MediaStreams that we're
  621. // writing to.
  622. struct OutputMediaStream {
  623. OutputMediaStream();
  624. ~OutputMediaStream();
  625. RefPtr<DOMMediaStream> mStream;
  626. TrackID mNextAvailableTrackID;
  627. bool mFinishWhenEnded;
  628. bool mCapturingAudioOnly;
  629. bool mCapturingDecoder;
  630. bool mCapturingMediaStream;
  631. // The following members are keeping state for a captured MediaDecoder.
  632. // Tracks that were created on main thread before MediaDecoder fed them
  633. // to the MediaStreamGraph.
  634. nsTArray<RefPtr<MediaStreamTrack>> mPreCreatedTracks;
  635. // The following members are keeping state for a captured MediaStream.
  636. nsTArray<Pair<nsString, RefPtr<MediaInputPort>>> mTrackPorts;
  637. };
  638. already_AddRefed<Promise> PlayInternal(ErrorResult& aRv);
  639. /** Use this method to change the mReadyState member, so required
  640. * events can be fired.
  641. */
  642. void ChangeReadyState(nsMediaReadyState aState);
  643. /**
  644. * Use this method to change the mNetworkState member, so required
  645. * actions will be taken during the transition.
  646. */
  647. void ChangeNetworkState(nsMediaNetworkState aState);
  648. /**
  649. * These two methods are called by the WakeLockBoolWrapper when the wakelock
  650. * has to be created or released.
  651. */
  652. virtual void WakeLockCreate();
  653. virtual void WakeLockRelease();
  654. RefPtr<WakeLock> mWakeLock;
  655. /**
  656. * Logs a warning message to the web console to report various failures.
  657. * aMsg is the localized message identifier, aParams is the parameters to
  658. * be substituted into the localized message, and aParamCount is the number
  659. * of parameters in aParams.
  660. */
  661. void ReportLoadError(const char* aMsg,
  662. const char16_t** aParams = nullptr,
  663. uint32_t aParamCount = 0);
  664. /**
  665. * Changes mHasPlayedOrSeeked to aValue. If mHasPlayedOrSeeked changes
  666. * we'll force a reflow so that the video frame gets reflowed to reflect
  667. * the poster hiding or showing immediately.
  668. */
  669. void SetPlayedOrSeeked(bool aValue);
  670. /**
  671. * Initialize the media element for playback of aStream
  672. */
  673. void SetupSrcMediaStreamPlayback(DOMMediaStream* aStream);
  674. /**
  675. * Stop playback on mSrcStream.
  676. */
  677. void EndSrcMediaStreamPlayback();
  678. /**
  679. * Ensure we're playing mSrcStream if and only if we're not paused.
  680. */
  681. enum { REMOVING_SRC_STREAM = 0x1 };
  682. void UpdateSrcMediaStreamPlaying(uint32_t aFlags = 0);
  683. /**
  684. * Called by our DOMMediaStream::TrackListener when a new MediaStreamTrack has
  685. * been added to the playback stream of |mSrcStream|.
  686. */
  687. void NotifyMediaStreamTrackAdded(const RefPtr<MediaStreamTrack>& aTrack);
  688. /**
  689. * Called by our DOMMediaStream::TrackListener when a MediaStreamTrack in
  690. * |mSrcStream|'s playback stream has ended.
  691. */
  692. void NotifyMediaStreamTrackRemoved(const RefPtr<MediaStreamTrack>& aTrack);
  693. /**
  694. * Enables or disables all tracks forwarded from mSrcStream to all
  695. * OutputMediaStreams. We do this for muting the tracks when pausing,
  696. * and unmuting when playing the media element again.
  697. *
  698. * If mSrcStream is unset, this does nothing.
  699. */
  700. void SetCapturedOutputStreamsEnabled(bool aEnabled);
  701. /**
  702. * Create a new MediaStreamTrack for aTrack and add it to the DOMMediaStream
  703. * in aOutputStream. This automatically sets the output track to enabled or
  704. * disabled depending on our current playing state.
  705. */
  706. void AddCaptureMediaTrackToOutputStream(MediaTrack* aTrack,
  707. OutputMediaStream& aOutputStream,
  708. bool aAsyncAddtrack = true);
  709. /**
  710. * Returns an DOMMediaStream containing the played contents of this
  711. * element. When aFinishWhenEnded is true, when this element ends playback
  712. * we will finish the stream and not play any more into it.
  713. * When aFinishWhenEnded is false, ending playback does not finish the stream.
  714. * The stream will never finish.
  715. *
  716. * When aCaptureAudio is true, we stop playout of audio and instead route it
  717. * to the DOMMediaStream. Volume and mute state will be applied to the audio
  718. * reaching the stream. No video tracks will be captured in this case.
  719. */
  720. already_AddRefed<DOMMediaStream> CaptureStreamInternal(bool aFinishWhenEnded,
  721. bool aCaptureAudio,
  722. MediaStreamGraph* aGraph);
  723. /**
  724. * Initialize a decoder as a clone of an existing decoder in another
  725. * element.
  726. * mLoadingSrc must already be set.
  727. */
  728. nsresult InitializeDecoderAsClone(MediaDecoder* aOriginal);
  729. /**
  730. * Initialize a decoder to load the given channel. The decoder's stream
  731. * listener is returned via aListener.
  732. * mLoadingSrc must already be set.
  733. */
  734. nsresult InitializeDecoderForChannel(nsIChannel *aChannel,
  735. nsIStreamListener **aListener);
  736. /**
  737. * Finish setting up the decoder after Load() has been called on it.
  738. * Called by InitializeDecoderForChannel/InitializeDecoderAsClone.
  739. */
  740. nsresult FinishDecoderSetup(MediaDecoder* aDecoder,
  741. MediaResource* aStream,
  742. nsIStreamListener **aListener);
  743. /**
  744. * Call this after setting up mLoadingSrc and mDecoder.
  745. */
  746. void AddMediaElementToURITable();
  747. /**
  748. * Call this before modifying mLoadingSrc.
  749. */
  750. void RemoveMediaElementFromURITable();
  751. /**
  752. * Call this to find a media element with the same NodePrincipal and mLoadingSrc
  753. * set to aURI, and with a decoder on which Load() has been called.
  754. */
  755. HTMLMediaElement* LookupMediaElementURITable(nsIURI* aURI);
  756. /**
  757. * Shutdown and clear mDecoder and maintain associated invariants.
  758. */
  759. void ShutdownDecoder();
  760. /**
  761. * Execute the initial steps of the load algorithm that ensure existing
  762. * loads are aborted, the element is emptied, and a new load ID is
  763. * created.
  764. */
  765. void AbortExistingLoads();
  766. /**
  767. * These are the dedicated media source failure steps.
  768. * Called when all potential resources are exhausted. Changes network
  769. * state to NETWORK_NO_SOURCE, and sends error event with code
  770. * MEDIA_ERR_SRC_NOT_SUPPORTED.
  771. */
  772. void NoSupportedMediaSourceError(const nsACString& aErrorDetails = nsCString());
  773. /**
  774. * Attempts to load resources from the <source> children. This is a
  775. * substep of the resource selection algorithm. Do not call this directly,
  776. * call QueueLoadFromSourceTask() instead.
  777. */
  778. void LoadFromSourceChildren();
  779. /**
  780. * Asynchronously awaits a stable state, and then causes
  781. * LoadFromSourceChildren() to be called on the main threads' event loop.
  782. */
  783. void QueueLoadFromSourceTask();
  784. /**
  785. * Runs the media resource selection algorithm.
  786. */
  787. void SelectResource();
  788. /**
  789. * A wrapper function that allows us to cleanly reset flags after a call
  790. * to SelectResource()
  791. */
  792. void SelectResourceWrapper();
  793. /**
  794. * Asynchronously awaits a stable state, and then causes SelectResource()
  795. * to be run on the main thread's event loop.
  796. */
  797. void QueueSelectResourceTask();
  798. /**
  799. * When loading a new source on an existing media element, make sure to reset
  800. * everything that is accessible using the media element API.
  801. */
  802. void ResetState();
  803. /**
  804. * The resource-fetch algorithm step of the load algorithm.
  805. */
  806. nsresult LoadResource();
  807. /**
  808. * Selects the next <source> child from which to load a resource. Called
  809. * during the resource selection algorithm. Stores the return value in
  810. * mSourceLoadCandidate before returning.
  811. */
  812. nsIContent* GetNextSource();
  813. /**
  814. * Changes mDelayingLoadEvent, and will call BlockOnLoad()/UnblockOnLoad()
  815. * on the owning document, so it can delay the load event firing.
  816. */
  817. void ChangeDelayLoadStatus(bool aDelay);
  818. /**
  819. * If we suspended downloading after the first frame, unsuspend now.
  820. */
  821. void StopSuspendingAfterFirstFrame();
  822. /**
  823. * Called when our channel is redirected to another channel.
  824. * Updates our mChannel reference to aNewChannel.
  825. */
  826. nsresult OnChannelRedirect(nsIChannel *aChannel,
  827. nsIChannel *aNewChannel,
  828. uint32_t aFlags);
  829. /**
  830. * Call this to reevaluate whether we should be holding a self-reference.
  831. */
  832. void AddRemoveSelfReference();
  833. /**
  834. * Called asynchronously to release a self-reference to this element.
  835. */
  836. void DoRemoveSelfReference();
  837. /**
  838. * Called when "xpcom-shutdown" event is received.
  839. */
  840. void NotifyShutdownEvent();
  841. /**
  842. * Possible values of the 'preload' attribute.
  843. */
  844. enum PreloadAttrValue : uint8_t {
  845. PRELOAD_ATTR_EMPTY, // set to ""
  846. PRELOAD_ATTR_NONE, // set to "none"
  847. PRELOAD_ATTR_METADATA, // set to "metadata"
  848. PRELOAD_ATTR_AUTO // set to "auto"
  849. };
  850. /**
  851. * The preloading action to perform. These dictate how we react to the
  852. * preload attribute. See mPreloadAction.
  853. */
  854. enum PreloadAction {
  855. PRELOAD_UNDEFINED = 0, // not determined - used only for initialization
  856. PRELOAD_NONE = 1, // do not preload
  857. PRELOAD_METADATA = 2, // preload only the metadata (and first frame)
  858. PRELOAD_ENOUGH = 3 // preload enough data to allow uninterrupted
  859. // playback
  860. };
  861. /**
  862. * The guts of Load(). Load() acts as a wrapper around this which sets
  863. * mIsDoingExplicitLoad to true so that when script calls 'load()'
  864. * preload-none will be automatically upgraded to preload-metadata.
  865. */
  866. void DoLoad();
  867. /**
  868. * Suspends the load of mLoadingSrc, so that it can be resumed later
  869. * by ResumeLoad(). This is called when we have a media with a 'preload'
  870. * attribute value of 'none', during the resource selection algorithm.
  871. */
  872. void SuspendLoad();
  873. /**
  874. * Resumes a previously suspended load (suspended by SuspendLoad(uri)).
  875. * Will continue running the resource selection algorithm.
  876. * Sets mPreloadAction to aAction.
  877. */
  878. void ResumeLoad(PreloadAction aAction);
  879. /**
  880. * Handle a change to the preload attribute. Should be called whenever the
  881. * value (or presence) of the preload attribute changes. The change in
  882. * attribute value may cause a change in the mPreloadAction of this
  883. * element. If there is a change then this method will initiate any
  884. * behaviour that is necessary to implement the action.
  885. */
  886. void UpdatePreloadAction();
  887. /**
  888. * Fire progress events if needed according to the time and byte constraints
  889. * outlined in the specification. aHaveNewProgress is true if progress has
  890. * just been detected. Otherwise the method is called as a result of the
  891. * progress timer.
  892. */
  893. void CheckProgress(bool aHaveNewProgress);
  894. static void ProgressTimerCallback(nsITimer* aTimer, void* aClosure);
  895. /**
  896. * Start timer to update download progress.
  897. */
  898. void StartProgressTimer();
  899. /**
  900. * Start sending progress and/or stalled events.
  901. */
  902. void StartProgress();
  903. /**
  904. * Stop progress information timer and events.
  905. */
  906. void StopProgress();
  907. /**
  908. * Dispatches an error event to a child source element.
  909. */
  910. void DispatchAsyncSourceError(nsIContent* aSourceElement);
  911. /**
  912. * Resets the media element for an error condition as per aErrorCode.
  913. * aErrorCode must be one of nsIDOMHTMLMediaError codes.
  914. */
  915. void Error(uint16_t aErrorCode, const nsACString& aErrorDetails = nsCString());
  916. /**
  917. * Returns the URL spec of the currentSrc.
  918. **/
  919. void GetCurrentSpec(nsCString& aString);
  920. /**
  921. * Process any media fragment entries in the URI
  922. */
  923. void ProcessMediaFragmentURI();
  924. /**
  925. * Mute or unmute the audio and change the value that the |muted| map.
  926. */
  927. void SetMutedInternal(uint32_t aMuted);
  928. /**
  929. * Update the volume of the output audio stream to match the element's
  930. * current mMuted/mVolume/mAudioChannelFaded state.
  931. */
  932. void SetVolumeInternal();
  933. /**
  934. * Suspend (if aPauseForInactiveDocument) or resume element playback and
  935. * resource download. If aSuspendEvents is true, event delivery is
  936. * suspended (and events queued) until the element is resumed.
  937. */
  938. void SuspendOrResumeElement(bool aPauseElement, bool aSuspendEvents);
  939. // Get the HTMLMediaElement object if the decoder is being used from an
  940. // HTML media element, and null otherwise.
  941. virtual HTMLMediaElement* GetMediaElement() final override
  942. {
  943. return this;
  944. }
  945. // Return true if decoding should be paused
  946. virtual bool GetPaused() final override
  947. {
  948. bool isPaused = false;
  949. GetPaused(&isPaused);
  950. return isPaused;
  951. }
  952. // Check the permissions for audiochannel.
  953. bool CheckAudioChannelPermissions(const nsAString& aType);
  954. // Seeks to aTime seconds. aSeekType can be Exact to seek to exactly the
  955. // seek target, or PrevSyncPoint if a quicker but less precise seek is
  956. // desired, and we'll seek to the sync point (keyframe and/or start of the
  957. // next block of audio samples) preceeding seek target.
  958. already_AddRefed<Promise> Seek(double aTime, SeekTarget::Type aSeekType, ErrorResult& aRv);
  959. // A method to check if we are playing through the AudioChannel.
  960. bool IsPlayingThroughTheAudioChannel() const;
  961. // Update the audio channel playing state
  962. void UpdateAudioChannelPlayingState(bool aForcePlaying = false);
  963. // Adds to the element's list of pending text tracks each text track
  964. // in the element's list of text tracks whose text track mode is not disabled
  965. // and whose text track readiness state is loading.
  966. void PopulatePendingTextTrackList();
  967. // Gets a reference to the MediaElement's TextTrackManager. If the
  968. // MediaElement doesn't yet have one then it will create it.
  969. TextTrackManager* GetOrCreateTextTrackManager();
  970. // Recomputes ready state and fires events as necessary based on current state.
  971. void UpdateReadyStateInternal();
  972. // Notifies the audio channel agent when the element starts or stops playing.
  973. void NotifyAudioChannelAgent(bool aPlaying);
  974. // True if we create the audio channel agent successfully or we already have
  975. // one. The agent is used to communicate with the AudioChannelService. eg.
  976. // notify we are playing/audible and receive muted/unmuted/suspend/resume
  977. // commands from AudioChannelService.
  978. bool MaybeCreateAudioChannelAgent();
  979. // Determine if the element should be paused because of suspend conditions.
  980. bool ShouldElementBePaused();
  981. // Create or destroy the captured stream depend on mAudioCapturedByWindow.
  982. void AudioCaptureStreamChangeIfNeeded();
  983. /**
  984. * We have different kinds of suspended cases,
  985. * - SUSPENDED_PAUSE
  986. * It's used when we temporary lost platform audio focus. MediaElement can
  987. * only be resumed when we gain the audio focus again.
  988. *
  989. * - SUSPENDED_PAUSE_DISPOSABLE
  990. * It's used when user press the pause botton on the remote media-control.
  991. * MediaElement can be resumed by reomte media-control or via play().
  992. *
  993. * - SUSPENDED_BLOCK
  994. * It's used to reduce the power comsuption, we won't play the auto-play
  995. * audio/video in the page we have never visited before. MediaElement would
  996. * be resumed when the page is active. See bug647429 for more details.
  997. *
  998. * - SUSPENDED_STOP_DISPOSABLE
  999. * When we permanently lost platform audio focus, we shuold stop playing
  1000. * and stop the audio channel agent. MediaElement can only be restarted by
  1001. * play().
  1002. */
  1003. void PauseByAudioChannel(SuspendTypes aSuspend);
  1004. void BlockByAudioChannel();
  1005. void ResumeFromAudioChannel();
  1006. void ResumeFromAudioChannelPaused(SuspendTypes aSuspend);
  1007. void ResumeFromAudioChannelBlocked();
  1008. bool IsSuspendedByAudioChannel() const;
  1009. void SetAudioChannelSuspended(SuspendTypes aSuspend);
  1010. // A method to check whether the media element is allowed to start playback.
  1011. bool IsAllowedToPlay();
  1012. bool IsAllowedToPlayByAudioChannel();
  1013. // If the network state is empty and then we would trigger DoLoad().
  1014. void MaybeDoLoad();
  1015. // True if the tab which media element belongs to has been to foreground at
  1016. // least once or activated by manually clicking the unblocking tab icon.
  1017. bool IsTabActivated() const;
  1018. AudibleState IsAudible() const;
  1019. // It's used for fennec only, send the notification when the user resumes the
  1020. // media which was paused by media control.
  1021. void MaybeNotifyMediaResumed(SuspendTypes aSuspend);
  1022. class nsAsyncEventRunner;
  1023. class nsNotifyAboutPlayingRunner;
  1024. class nsResolveOrRejectPendingPlayPromisesRunner;
  1025. using nsGenericHTMLElement::DispatchEvent;
  1026. // For nsAsyncEventRunner.
  1027. nsresult DispatchEvent(const nsAString& aName);
  1028. // Open unsupported types media with the external app when the media element
  1029. // triggers play() after loaded fail. eg. preload the data before start play.
  1030. void OpenUnsupportedMediaWithExternalAppIfNeeded() const;
  1031. // This method moves the mPendingPlayPromises into a temperate object. So the
  1032. // mPendingPlayPromises is cleared after this method call.
  1033. nsTArray<RefPtr<Promise>> TakePendingPlayPromises();
  1034. // This method snapshots the mPendingPlayPromises by TakePendingPlayPromises()
  1035. // and queues a task to resolve them.
  1036. void AsyncResolvePendingPlayPromises();
  1037. // This method snapshots the mPendingPlayPromises by TakePendingPlayPromises()
  1038. // and queues a task to reject them.
  1039. void AsyncRejectPendingPlayPromises(nsresult aError);
  1040. // This method snapshots the mPendingPlayPromises by TakePendingPlayPromises()
  1041. // and queues a task to resolve them also to dispatch a "playing" event.
  1042. void NotifyAboutPlaying();
  1043. already_AddRefed<Promise> CreateDOMPromise(ErrorResult& aRv) const;
  1044. virtual nsresult AfterSetAttr(int32_t aNameSpaceID, nsIAtom* aName,
  1045. const nsAttrValue* aValue,
  1046. const nsAttrValue* aOldValue,
  1047. bool aNotify) override;
  1048. virtual nsresult OnAttrSetButNotChanged(int32_t aNamespaceID, nsIAtom* aName,
  1049. const nsAttrValueOrString& aValue,
  1050. bool aNotify) override;
  1051. // The current decoder. Load() has been called on this decoder.
  1052. // At most one of mDecoder and mSrcStream can be non-null.
  1053. RefPtr<MediaDecoder> mDecoder;
  1054. // Observers listening to changes to the mDecoder principal.
  1055. // Used by streams captured from this element.
  1056. nsTArray<DecoderPrincipalChangeObserver*> mDecoderPrincipalChangeObservers;
  1057. // State-watching manager.
  1058. WatchManager<HTMLMediaElement> mWatchManager;
  1059. // A reference to the VideoFrameContainer which contains the current frame
  1060. // of video to display.
  1061. RefPtr<VideoFrameContainer> mVideoFrameContainer;
  1062. // Holds a reference to the DOM wrapper for the MediaStream that has been
  1063. // set in the src attribute.
  1064. RefPtr<DOMMediaStream> mSrcAttrStream;
  1065. // Holds a reference to the DOM wrapper for the MediaStream that we're
  1066. // actually playing.
  1067. // At most one of mDecoder and mSrcStream can be non-null.
  1068. RefPtr<DOMMediaStream> mSrcStream;
  1069. // True once mSrcStream's initial set of tracks are known.
  1070. bool mSrcStreamTracksAvailable;
  1071. // If non-negative, the time we should return for currentTime while playing
  1072. // mSrcStream.
  1073. double mSrcStreamPausedCurrentTime;
  1074. // Holds a reference to the stream connecting this stream to the capture sink.
  1075. RefPtr<MediaInputPort> mCaptureStreamPort;
  1076. // Holds references to the DOM wrappers for the MediaStreams that we're
  1077. // writing to.
  1078. nsTArray<OutputMediaStream> mOutputStreams;
  1079. // Holds a reference to the MediaStreamListener attached to mSrcStream's
  1080. // playback stream.
  1081. RefPtr<StreamListener> mMediaStreamListener;
  1082. // Holds a reference to the size-getting MediaStreamListener attached to
  1083. // mSrcStream.
  1084. RefPtr<StreamSizeListener> mMediaStreamSizeListener;
  1085. // The selected video stream track which contained mMediaStreamSizeListener.
  1086. RefPtr<VideoStreamTrack> mSelectedVideoStreamTrack;
  1087. const RefPtr<ShutdownObserver> mShutdownObserver;
  1088. // Holds a reference to the MediaSource, if any, referenced by the src
  1089. // attribute on the media element.
  1090. RefPtr<MediaSource> mSrcMediaSource;
  1091. // Holds a reference to the MediaSource supplying data for playback. This
  1092. // may either match mSrcMediaSource or come from Source element children.
  1093. // This is set when and only when mLoadingSrc corresponds to an object url
  1094. // that resolved to a MediaSource.
  1095. RefPtr<MediaSource> mMediaSource;
  1096. RefPtr<ChannelLoader> mChannelLoader;
  1097. // The current media load ID. This is incremented every time we start a
  1098. // new load. Async events note the ID when they're first sent, and only fire
  1099. // if the ID is unchanged when they come to fire.
  1100. uint32_t mCurrentLoadID;
  1101. // Points to the child source elements, used to iterate through the children
  1102. // when selecting a resource to load.
  1103. RefPtr<nsRange> mSourcePointer;
  1104. // Points to the document whose load we're blocking. This is the document
  1105. // we're bound to when loading starts.
  1106. nsCOMPtr<nsIDocument> mLoadBlockedDoc;
  1107. // Contains names of events that have been raised while in the bfcache.
  1108. // These events get re-dispatched when the bfcache is exited.
  1109. nsTArray<nsString> mPendingEvents;
  1110. // Media loading flags. See:
  1111. // http://www.whatwg.org/specs/web-apps/current-work/#video)
  1112. nsMediaNetworkState mNetworkState;
  1113. Watchable<nsMediaReadyState> mReadyState;
  1114. enum LoadAlgorithmState {
  1115. // No load algorithm instance is waiting for a source to be added to the
  1116. // media in order to continue loading.
  1117. NOT_WAITING,
  1118. // We've run the load algorithm, and we tried all source children of the
  1119. // media element, and failed to load any successfully. We're waiting for
  1120. // another source element to be added to the media element, and will try
  1121. // to load any such element when its added.
  1122. WAITING_FOR_SOURCE
  1123. };
  1124. // Denotes the waiting state of a load algorithm instance. When the load
  1125. // algorithm is waiting for a source element child to be added, this is set
  1126. // to WAITING_FOR_SOURCE, otherwise it's NOT_WAITING.
  1127. LoadAlgorithmState mLoadWaitStatus;
  1128. // Current audio volume
  1129. double mVolume;
  1130. nsAutoPtr<const MetadataTags> mTags;
  1131. // URI of the resource we're attempting to load. This stores the value we
  1132. // return in the currentSrc attribute. Use GetCurrentSrc() to access the
  1133. // currentSrc attribute.
  1134. // This is always the original URL we're trying to load --- before
  1135. // redirects etc.
  1136. nsCOMPtr<nsIURI> mLoadingSrc;
  1137. // Stores the current preload action for this element. Initially set to
  1138. // PRELOAD_UNDEFINED, its value is changed by calling
  1139. // UpdatePreloadAction().
  1140. PreloadAction mPreloadAction;
  1141. // Time that the last timeupdate event was fired. Read/Write from the
  1142. // main thread only.
  1143. TimeStamp mTimeUpdateTime;
  1144. // Time that the last progress event was fired. Read/Write from the
  1145. // main thread only.
  1146. TimeStamp mProgressTime;
  1147. // Time that data was last read from the media resource. Used for
  1148. // computing if the download has stalled and to rate limit progress events
  1149. // when data is arriving slower than PROGRESS_MS.
  1150. // Read/Write from the main thread only.
  1151. TimeStamp mDataTime;
  1152. // Media 'currentTime' value when the last timeupdate event occurred.
  1153. // Read/Write from the main thread only.
  1154. double mLastCurrentTime;
  1155. // Logical start time of the media resource in seconds as obtained
  1156. // from any media fragments. A negative value indicates that no
  1157. // fragment time has been set. Read/Write from the main thread only.
  1158. double mFragmentStart;
  1159. // Logical end time of the media resource in seconds as obtained
  1160. // from any media fragments. A negative value indicates that no
  1161. // fragment time has been set. Read/Write from the main thread only.
  1162. double mFragmentEnd;
  1163. // The defaultPlaybackRate attribute gives the desired speed at which the
  1164. // media resource is to play, as a multiple of its intrinsic speed.
  1165. double mDefaultPlaybackRate;
  1166. // The playbackRate attribute gives the speed at which the media resource
  1167. // plays, as a multiple of its intrinsic speed. If it is not equal to the
  1168. // defaultPlaybackRate, then the implication is that the user is using a
  1169. // feature such as fast forward or slow motion playback.
  1170. double mPlaybackRate;
  1171. // True if pitch correction is applied when playbackRate is set to a
  1172. // non-intrinsic value.
  1173. bool mPreservesPitch;
  1174. // Reference to the source element last returned by GetNextSource().
  1175. // This is the child source element which we're trying to load from.
  1176. nsCOMPtr<nsIContent> mSourceLoadCandidate;
  1177. // Range of time played.
  1178. RefPtr<TimeRanges> mPlayed;
  1179. // Timer used for updating progress events.
  1180. nsCOMPtr<nsITimer> mProgressTimer;
  1181. #ifdef MOZ_EME
  1182. // Encrypted Media Extension media keys.
  1183. RefPtr<MediaKeys> mMediaKeys;
  1184. #endif
  1185. // Stores the time at the start of the current 'played' range.
  1186. double mCurrentPlayRangeStart;
  1187. // If true then we have begun downloading the media content.
  1188. // Set to false when completed, or not yet started.
  1189. bool mBegun;
  1190. // True if loadeddata has been fired.
  1191. bool mLoadedDataFired;
  1192. // Indicates whether current playback is a result of user action
  1193. // (ie. calling of the Play method), or automatic playback due to
  1194. // the 'autoplay' attribute being set. A true value indicates the
  1195. // latter case.
  1196. // The 'autoplay' HTML attribute indicates that the video should
  1197. // start playing when loaded. The 'autoplay' attribute of the object
  1198. // is a mirror of the HTML attribute. These are different from this
  1199. // 'mAutoplaying' flag, which indicates whether the current playback
  1200. // is a result of the autoplay attribute.
  1201. bool mAutoplaying;
  1202. // Indicates whether |autoplay| will actually autoplay based on the pref
  1203. // media.autoplay.enabled
  1204. bool mAutoplayEnabled;
  1205. // Playback of the video is paused either due to calling the
  1206. // 'Pause' method, or playback not yet having started.
  1207. WakeLockBoolWrapper mPaused;
  1208. enum MutedReasons {
  1209. MUTED_BY_CONTENT = 0x01,
  1210. MUTED_BY_INVALID_PLAYBACK_RATE = 0x02,
  1211. MUTED_BY_AUDIO_CHANNEL = 0x04,
  1212. MUTED_BY_AUDIO_TRACK = 0x08
  1213. };
  1214. uint32_t mMuted;
  1215. SuspendTypes mAudioChannelSuspended;
  1216. // True if the media statistics are currently being shown by the builtin
  1217. // video controls
  1218. bool mStatsShowing;
  1219. // The following two fields are here for the private storage of the builtin
  1220. // video controls, and control 'casting' of the video to external devices
  1221. // (TVs, projectors etc.)
  1222. // True if casting is currently allowed
  1223. bool mAllowCasting;
  1224. // True if currently casting this video
  1225. bool mIsCasting;
  1226. // True if the sound is being captured.
  1227. bool mAudioCaptured;
  1228. // True if the sound is being captured by the window.
  1229. bool mAudioCapturedByWindow;
  1230. // If TRUE then the media element was actively playing before the currently
  1231. // in progress seeking. If FALSE then the media element is either not seeking
  1232. // or was not actively playing before the current seek. Used to decide whether
  1233. // to raise the 'waiting' event as per 4.7.1.8 in HTML 5 specification.
  1234. bool mPlayingBeforeSeek;
  1235. // if TRUE then the seek started while content was in active playing state
  1236. // if FALSE then the seek started while the content was not playing.
  1237. bool mPlayingThroughTheAudioChannelBeforeSeek;
  1238. // True iff this element is paused because the document is inactive or has
  1239. // been suspended by the audio channel service.
  1240. bool mPausedForInactiveDocumentOrChannel;
  1241. // True iff event delivery is suspended (mPausedForInactiveDocumentOrChannel must also be true).
  1242. bool mEventDeliveryPaused;
  1243. // True if we're running the "load()" method.
  1244. bool mIsRunningLoadMethod;
  1245. // True if we're running or waiting to run queued tasks due to an explicit
  1246. // call to "load()".
  1247. bool mIsDoingExplicitLoad;
  1248. // True if we're loading the resource from the child source elements.
  1249. bool mIsLoadingFromSourceChildren;
  1250. // True if we're delaying the "load" event. They are delayed until either
  1251. // an error occurs, or the first frame is loaded.
  1252. bool mDelayingLoadEvent;
  1253. // True when we've got a task queued to call SelectResource(),
  1254. // or while we're running SelectResource().
  1255. bool mIsRunningSelectResource;
  1256. // True when we already have select resource call queued
  1257. bool mHaveQueuedSelectResource;
  1258. // True if we suspended the decoder because we were paused,
  1259. // preloading metadata is enabled, autoplay was not enabled, and we loaded
  1260. // the first frame.
  1261. bool mSuspendedAfterFirstFrame;
  1262. // True if we are allowed to suspend the decoder because we were paused,
  1263. // preloading metdata was enabled, autoplay was not enabled, and we loaded
  1264. // the first frame.
  1265. bool mAllowSuspendAfterFirstFrame;
  1266. // True if we've played or completed a seek. We use this to determine
  1267. // when the poster frame should be shown.
  1268. bool mHasPlayedOrSeeked;
  1269. // True if we've added a reference to ourselves to keep the element
  1270. // alive while no-one is referencing it but the element may still fire
  1271. // events of its own accord.
  1272. bool mHasSelfReference;
  1273. // True if we've received a notification that the engine is shutting
  1274. // down.
  1275. bool mShuttingDown;
  1276. // True if we've suspended a load in the resource selection algorithm
  1277. // due to loading a preload:none media. When true, the resource we'll
  1278. // load when the user initiates either playback or an explicit load is
  1279. // stored in mPreloadURI.
  1280. bool mSuspendedForPreloadNone;
  1281. // True if we've connected mSrcStream to the media element output.
  1282. bool mSrcStreamIsPlaying;
  1283. // True if a same-origin check has been done for the media element and resource.
  1284. bool mMediaSecurityVerified;
  1285. // The CORS mode when loading the media element
  1286. CORSMode mCORSMode;
  1287. // Info about the played media.
  1288. MediaInfo mMediaInfo;
  1289. // True if the media has encryption information.
  1290. bool mIsEncrypted;
  1291. enum WaitingForKeyState {
  1292. NOT_WAITING_FOR_KEY = 0,
  1293. WAITING_FOR_KEY = 1,
  1294. WAITING_FOR_KEY_DISPATCHED = 2
  1295. };
  1296. // True when the CDM cannot decrypt the current block due to lacking a key.
  1297. // Note: the "waitingforkey" event is not dispatched until all decoded data
  1298. // has been rendered.
  1299. WaitingForKeyState mWaitingForKey;
  1300. // Listens for waitingForKey events from the owned decoder.
  1301. MediaEventListener mWaitingForKeyListener;
  1302. #ifdef MOZ_EME
  1303. // Init Data that needs to be sent in 'encrypted' events in MetadataLoaded().
  1304. EncryptionInfo mPendingEncryptedInitData;
  1305. #endif
  1306. // True if the media's channel's download has been suspended.
  1307. Watchable<bool> mDownloadSuspendedByCache;
  1308. // Audio Channel.
  1309. AudioChannel mAudioChannel;
  1310. // The audio channel volume
  1311. float mAudioChannelVolume;
  1312. // Is this media element playing?
  1313. bool mPlayingThroughTheAudioChannel;
  1314. // Disable the video playback by track selection. This flag might not be
  1315. // enough if we ever expand the ability of supporting multi-tracks video
  1316. // playback.
  1317. bool mDisableVideo;
  1318. // An agent used to join audio channel service and its life cycle would equal
  1319. // to media element.
  1320. RefPtr<AudioChannelAgent> mAudioChannelAgent;
  1321. RefPtr<TextTrackManager> mTextTrackManager;
  1322. RefPtr<AudioTrackList> mAudioTrackList;
  1323. RefPtr<VideoTrackList> mVideoTrackList;
  1324. nsAutoPtr<MediaStreamTrackListener> mMediaStreamTrackListener;
  1325. // The principal guarding mVideoFrameContainer access when playing a
  1326. // MediaStream.
  1327. nsCOMPtr<nsIPrincipal> mSrcStreamVideoPrincipal;
  1328. // True if UnbindFromTree() is called on the element.
  1329. // Note this flag is false when the element is in a phase after creation and
  1330. // before attaching to the DOM tree.
  1331. bool mUnboundFromTree = false;
  1332. private:
  1333. // Indicates if user has interacted with the element.
  1334. // Used to block autoplay when disabled.
  1335. bool mHasUserInteraction;
  1336. // True if the first frame has been successfully loaded.
  1337. bool mFirstFrameLoaded;
  1338. // Media elements also have a default playback start position, which must
  1339. // initially be set to zero seconds. This time is used to allow the element to
  1340. // be seeked even before the media is loaded.
  1341. double mDefaultPlaybackStartPosition;
  1342. // True if the audio track is not silent.
  1343. bool mIsAudioTrackAudible;
  1344. // Indicate whether media element is audible for users.
  1345. AudibleState mAudible;
  1346. Visibility mVisibilityState;
  1347. UniquePtr<ErrorSink> mErrorSink;
  1348. // A list of pending play promises. The elements are pushed during the play()
  1349. // method call and are resolved/rejected during further playback steps.
  1350. nsTArray<RefPtr<Promise>> mPendingPlayPromises;
  1351. // A list of already-dispatched but not yet run
  1352. // nsResolveOrRejectPendingPlayPromisesRunners.
  1353. // Runners whose Run() method is called remove themselves from this list.
  1354. // We keep track of these because the load algorithm resolves/rejects all
  1355. // already-dispatched pending play promises.
  1356. nsTArray<nsResolveOrRejectPendingPlayPromisesRunner*> mPendingPlayPromisesRunners;
  1357. private:
  1358. /**
  1359. * This function is called by AfterSetAttr and OnAttrSetButNotChanged.
  1360. * It will not be called if the value is being unset.
  1361. *
  1362. * @param aNamespaceID the namespace of the attr being set
  1363. * @param aName the localname of the attribute being set
  1364. * @param aNotify Whether we plan to notify document observers.
  1365. */
  1366. void AfterMaybeChangeAttr(int32_t aNamespaceID, nsIAtom* aName, bool aNotify);
  1367. };
  1368. } // namespace dom
  1369. } // namespace mozilla
  1370. #endif // mozilla_dom_HTMLMediaElement_h