Merge mozilla-central to autoland on a CLOSED TREE
[gecko.git] / widget / windows / WindowsSMTCProvider.h
blob3926618d1fd3ea9e444199dbcab6d3b8007f5373
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 private:
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
58 // mSupportedKeys
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
66 // type to "MUSIC"
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
72 // asynchronously
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;
84 void ClearMetadata();
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
96 // interface
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
106 // in processing
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>
118 mImageFetchRequest;
120 HWND mWindow; // handle to the invisible window
122 // EventRegistrationTokens are used to have a handle on a callback (to remove
123 // it again)
124 EventRegistrationToken mButtonPressedToken;
127 #endif // __MINGW32__
128 #endif // WIDGET_WINDOWS_WINDOWSSTMCPROVIDER_H_