1 /* -*- Mode: C++; tab-width: 2; 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/. */
6 #ifndef WIDGET_WINDOWS_WINDOWSSTMCPROVIDER_H_
7 #define WIDGET_WINDOWS_WINDOWSSTMCPROVIDER_H_
11 # include <functional>
12 # include <Windows.Media.h>
15 # include "mozilla/dom/FetchImageHelper.h"
16 # include "mozilla/dom/MediaController.h"
17 # include "mozilla/dom/MediaControlKeySource.h"
18 # include "mozilla/UniquePtr.h"
20 using ISMTC
= ABI::Windows::Media::ISystemMediaTransportControls
;
21 using SMTCProperty
= ABI::Windows::Media::SystemMediaTransportControlsProperty
;
22 using ISMTCDisplayUpdater
=
23 ABI::Windows::Media::ISystemMediaTransportControlsDisplayUpdater
;
25 using ABI::Windows::Foundation::IAsyncOperation
;
26 using ABI::Windows::Storage::Streams::IDataWriter
;
27 using ABI::Windows::Storage::Streams::IRandomAccessStream
;
28 using ABI::Windows::Storage::Streams::IRandomAccessStreamReference
;
29 using Microsoft::WRL::ComPtr
;
31 class WindowsSMTCProvider final
: public mozilla::dom::MediaControlKeySource
{
32 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WindowsSMTCProvider
, override
)
35 WindowsSMTCProvider();
37 bool IsOpened() const override
;
39 void Close() override
;
41 void SetPlaybackState(
42 mozilla::dom::MediaSessionPlaybackState aState
) override
;
44 void SetMediaMetadata(
45 const mozilla::dom::MediaMetadataBase
& aMetadata
) override
;
47 void SetSupportedMediaKeys(const MediaKeysArray
& aSupportedKeys
) override
;
49 void SetPositionState(
50 const mozilla::Maybe
<mozilla::dom::PositionState
>& aState
) override
;
53 ~WindowsSMTCProvider();
54 void UnregisterEvents();
55 bool RegisterEvents();
57 void OnButtonPressed(mozilla::dom::MediaControlKey aKey
) const;
58 // Enable the SMTC interface
59 bool EnableControl(bool aEnabled
) const;
60 // Sets the play, pause, next, previous, seekto buttons on the SMTC interface
63 bool IsKeySupported(mozilla::dom::MediaControlKey aKey
) const;
64 bool EnableKey(mozilla::dom::MediaControlKey aKey
, bool aEnable
) const;
66 void OnPositionChangeRequested(double aPosition
) const;
68 bool InitDisplayAndControls();
70 // Sets the Metadata for the currently playing media and sets the playback
72 bool SetMusicMetadata(const nsString
& aArtist
, const nsString
& aTitle
);
74 // Sets one of the artwork to the SMTC interface asynchronously
75 void LoadThumbnail(const nsTArray
<mozilla::dom::MediaImage
>& aArtwork
);
76 // Stores the image at index aIndex of the mArtwork to the Thumbnail
78 void LoadImageAtIndex(const size_t aIndex
);
79 // Stores the raw binary data of an image to mImageStream and set it to the
80 // Thumbnail asynchronously
81 void LoadImage(const char* aImageData
, uint32_t aDataSize
);
82 // Sets the Thumbnail to the image stored in mImageStream
83 bool SetThumbnail(const nsAString
& aUrl
);
84 void ClearThumbnail();
86 bool UpdateThumbnail(const nsAString
& aUrl
);
87 void CancelPendingStoreAsyncOperation() const;
91 bool mInitialized
= false;
93 // A bit table indicating what keys are enabled
94 uint32_t mSupportedKeys
= 0;
96 ComPtr
<ISMTC
> mControls
;
97 ComPtr
<ISMTCDisplayUpdater
> mDisplay
;
99 // Use mImageDataWriter to write the binary data of image into mImageStream
100 // and refer the image by mImageStreamReference and then set it to the SMTC
102 ComPtr
<IDataWriter
> mImageDataWriter
;
103 ComPtr
<IRandomAccessStream
> mImageStream
;
104 ComPtr
<IRandomAccessStreamReference
> mImageStreamReference
;
105 ComPtr
<IAsyncOperation
<unsigned int>> mStoreAsyncOperation
;
107 // mThumbnailUrl is the url of the current Thumbnail
108 // mProcessingUrl is the url that is being processed. The process starts from
109 // fetching an image from the url and then storing the fetched image to the
110 // mImageStream. If mProcessingUrl is not empty, it means there is an image is
112 // mThumbnailUrl and mProcessingUrl won't be set at the same time and they can
113 // only be touched on main thread
114 nsString mThumbnailUrl
;
115 nsString mProcessingUrl
;
117 // mArtwork can only be used in main thread in case of data racing
118 CopyableTArray
<mozilla::dom::MediaImage
> mArtwork
;
119 size_t mNextImageIndex
;
121 mozilla::UniquePtr
<mozilla::dom::FetchImageHelper
> mImageFetcher
;
122 mozilla::MozPromiseRequestHolder
<mozilla::dom::ImagePromise
>
125 HWND mWindow
; // handle to the invisible window
127 // EventRegistrationTokens are used to have a handle on a callback (to remove
129 EventRegistrationToken mButtonPressedToken
;
130 EventRegistrationToken mSeekRegistrationToken
;
133 #endif // __MINGW32__
134 #endif // WIDGET_WINDOWS_WINDOWSSTMCPROVIDER_H_