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
;
50 ~WindowsSMTCProvider();
51 void UnregisterEvents();
52 bool RegisterEvents();
54 void OnButtonPressed(mozilla::dom::MediaControlKey aKey
) const;
55 // Enable the SMTC interface
56 bool EnableControl(bool aEnabled
) const;
57 // Sets the play, pause, next, previous buttons on the SMTC interface by
59 bool UpdateButtons() const;
60 bool IsKeySupported(mozilla::dom::MediaControlKey aKey
) const;
61 bool EnableKey(mozilla::dom::MediaControlKey aKey
, bool aEnable
) const;
63 bool InitDisplayAndControls();
65 // Sets the Metadata for the currently playing media and sets the playback
67 bool SetMusicMetadata(const nsString
& aArtist
, const nsString
& aTitle
);
69 // Sets one of the artwork to the SMTC interface asynchronously
70 void LoadThumbnail(const nsTArray
<mozilla::dom::MediaImage
>& aArtwork
);
71 // Stores the image at index aIndex of the mArtwork to the Thumbnail
73 void LoadImageAtIndex(const size_t aIndex
);
74 // Stores the raw binary data of an image to mImageStream and set it to the
75 // Thumbnail asynchronously
76 void LoadImage(const char* aImageData
, uint32_t aDataSize
);
77 // Sets the Thumbnail to the image stored in mImageStream
78 bool SetThumbnail(const nsAString
& aUrl
);
79 void ClearThumbnail();
81 bool UpdateThumbnail(const nsAString
& aUrl
);
82 void CancelPendingStoreAsyncOperation() const;
86 bool mInitialized
= false;
88 // A bit table indicating what keys are enabled
89 uint32_t mSupportedKeys
= 0;
91 ComPtr
<ISMTC
> mControls
;
92 ComPtr
<ISMTCDisplayUpdater
> mDisplay
;
94 // Use mImageDataWriter to write the binary data of image into mImageStream
95 // and refer the image by mImageStreamReference and then set it to the SMTC
97 ComPtr
<IDataWriter
> mImageDataWriter
;
98 ComPtr
<IRandomAccessStream
> mImageStream
;
99 ComPtr
<IRandomAccessStreamReference
> mImageStreamReference
;
100 ComPtr
<IAsyncOperation
<unsigned int>> mStoreAsyncOperation
;
102 // mThumbnailUrl is the url of the current Thumbnail
103 // mProcessingUrl is the url that is being processed. The process starts from
104 // fetching an image from the url and then storing the fetched image to the
105 // mImageStream. If mProcessingUrl is not empty, it means there is an image is
107 // mThumbnailUrl and mProcessingUrl won't be set at the same time and they can
108 // only be touched on main thread
109 nsString mThumbnailUrl
;
110 nsString mProcessingUrl
;
112 // mArtwork can only be used in main thread in case of data racing
113 CopyableTArray
<mozilla::dom::MediaImage
> mArtwork
;
114 size_t mNextImageIndex
;
116 mozilla::UniquePtr
<mozilla::dom::FetchImageHelper
> mImageFetcher
;
117 mozilla::MozPromiseRequestHolder
<mozilla::dom::ImagePromise
>
120 HWND mWindow
; // handle to the invisible window
122 // EventRegistrationTokens are used to have a handle on a callback (to remove
124 EventRegistrationToken mButtonPressedToken
;
127 #endif // __MINGW32__
128 #endif // WIDGET_WINDOWS_WINDOWSSTMCPROVIDER_H_