MediaControlKeySource.h (4643B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #ifndef DOM_MEDIA_MEDIACONTROL_MEDIACONTROLKEYSOURCE_H_ 6 #define DOM_MEDIA_MEDIACONTROL_MEDIACONTROLKEYSOURCE_H_ 7 8 #include "mozilla/Maybe.h" 9 #include "mozilla/dom/MediaControllerBinding.h" 10 #include "mozilla/dom/MediaMetadata.h" 11 #include "mozilla/dom/MediaSession.h" 12 #include "mozilla/dom/MediaSessionBinding.h" 13 #include "nsISupportsImpl.h" 14 #include "nsTArray.h" 15 16 namespace mozilla::dom { 17 18 // This is used to store seek related properties from MediaSessionActionDetails. 19 // https://w3c.github.io/mediasession/#the-mediasessionactiondetails-dictionary 20 struct AbsoluteSeek { 21 double mSeekTime; 22 bool mFastSeek; 23 }; 24 struct SeekDetails { 25 Maybe<AbsoluteSeek> mAbsolute; 26 Maybe<double> mRelativeSeekOffset; 27 28 SeekDetails() = default; 29 SeekDetails(double aSeekTime, bool aFastSeek) 30 : mAbsolute(Some(AbsoluteSeek{aSeekTime, aFastSeek})) {} 31 explicit SeekDetails(double aRelativeSeekOffset) 32 : mRelativeSeekOffset(Some(aRelativeSeekOffset)) {} 33 }; 34 35 struct MediaControlAction { 36 MediaControlAction() = default; 37 explicit MediaControlAction(MediaControlKey aKey) : mKey(Some(aKey)) {} 38 MediaControlAction(MediaControlKey aKey, const SeekDetails& aDetails) 39 : mKey(Some(aKey)), mDetails(Some(aDetails)) {} 40 Maybe<MediaControlKey> mKey; 41 Maybe<SeekDetails> mDetails; 42 }; 43 44 /** 45 * MediaControlKeyListener is a pure interface, which is used to monitor 46 * MediaControlKey, we can add it onto the MediaControlKeySource, 47 * and then everytime when the media key events occur, `OnActionPerformed` will 48 * be called so that we can do related handling. 49 */ 50 class MediaControlKeyListener { 51 public: 52 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING 53 MediaControlKeyListener() = default; 54 55 virtual void OnActionPerformed(const MediaControlAction& aAction) = 0; 56 57 protected: 58 virtual ~MediaControlKeyListener() = default; 59 }; 60 61 /** 62 * MediaControlKeyHandler is used to operate media controller by corresponding 63 * received media control key events. 64 */ 65 class MediaControlKeyHandler final : public MediaControlKeyListener { 66 public: 67 NS_INLINE_DECL_REFCOUNTING(MediaControlKeyHandler, override) 68 void OnActionPerformed(const MediaControlAction& aAction) override; 69 70 private: 71 virtual ~MediaControlKeyHandler() = default; 72 }; 73 74 /** 75 * MediaControlKeySource is an abstract class which is used to implement 76 * transporting media control keys event to all its listeners when media keys 77 * event happens. 78 */ 79 class MediaControlKeySource { 80 public: 81 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING 82 MediaControlKeySource(); 83 84 using MediaKeysArray = nsTArray<MediaControlKey>; 85 86 virtual void AddListener(MediaControlKeyListener* aListener); 87 virtual void RemoveListener(MediaControlKeyListener* aListener); 88 size_t GetListenersNum() const; 89 90 // Return true if the initialization of the source succeeds, and inherited 91 // sources should implement this method to handle the initialization fails. 92 virtual bool Open() = 0; 93 virtual void Close(); 94 virtual bool IsOpened() const = 0; 95 96 /** 97 * All following `SetXXX()` functions are used to update the playback related 98 * properties change from a specific tab, which can represent the playback 99 * status for Firefox instance. Even if we have multiple tabs playing media at 100 * the same time, we would only update information from one of that tabs that 101 * would be done by `MediaControlService`. 102 */ 103 virtual void SetPlaybackState(MediaSessionPlaybackState aState); 104 virtual MediaSessionPlaybackState GetPlaybackState() const; 105 106 // Override this method if the event source needs to handle the metadata. 107 virtual void SetMediaMetadata(const MediaMetadataBase& aMetadata) {} 108 109 // Set the supported media keys which the event source can use to determine 110 // what kinds of buttons should be shown on the UI. 111 virtual void SetSupportedMediaKeys(const MediaKeysArray& aSupportedKeys) = 0; 112 113 // Override these methods if the inherited key source want to know the change 114 // for following attributes. For example, GeckoView would use these methods 115 // to notify change to the embedded application. 116 virtual void SetEnableFullScreen(bool aIsEnabled) {}; 117 virtual void SetEnablePictureInPictureMode(bool aIsEnabled) {}; 118 virtual void SetPositionState(const Maybe<PositionState>& aState) {}; 119 120 protected: 121 virtual ~MediaControlKeySource() = default; 122 nsTArray<RefPtr<MediaControlKeyListener>> mListeners; 123 MediaSessionPlaybackState mPlaybackState; 124 }; 125 126 } // namespace mozilla::dom 127 128 #endif