Bug 1913773 - Ensure mCurrentShmem is valid. r=aosmond
[gecko.git] / widget / windows / WindowsSMTCProvider.h
blob2f0d1f8344fbe0cb5a0dc8c600761fe492424c60
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_
9 #ifndef __MINGW32__
11 # include <functional>
12 # include <Windows.Media.h>
13 # include <wrl.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)
34 public:
35 WindowsSMTCProvider();
37 bool IsOpened() const override;
38 bool Open() 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;
52 private:
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
61 // by mSupportedKeys
62 bool UpdateButtons();
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
71 // type to "MUSIC"
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
77 // asynchronously
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;
89 void ClearMetadata();
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
101 // interface
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
111 // in processing
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>
123 mImageFetchRequest;
125 HWND mWindow; // handle to the invisible window
127 // EventRegistrationTokens are used to have a handle on a callback (to remove
128 // it again)
129 EventRegistrationToken mButtonPressedToken;
130 EventRegistrationToken mSeekRegistrationToken;
133 #endif // __MINGW32__
134 #endif // WIDGET_WINDOWS_WINDOWSSTMCPROVIDER_H_