1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef DOM_MEDIA_MEDIACONTROL_MEDIACONTROLUTILS_H_
8 #define DOM_MEDIA_MEDIACONTROL_MEDIACONTROLUTILS_H_
10 #include "MediaController.h"
11 #include "imgIEncoder.h"
12 #include "imgITools.h"
13 #include "mozilla/Logging.h"
14 #include "mozilla/dom/ChromeUtilsBinding.h"
15 #include "mozilla/dom/MediaControllerBinding.h"
16 #include "nsReadableUtils.h"
17 #include "nsServiceManagerUtils.h"
19 extern mozilla::LazyLogModule gMediaControlLog
;
21 namespace mozilla::dom
{
23 inline const char* ToMediaControlKeyStr(const Maybe
<MediaControlKey
>& aKey
) {
24 if (aKey
.isNothing()) {
25 MOZ_ASSERT_UNREACHABLE("Invalid action.");
28 return GetEnumString(aKey
.value()).get();
31 inline MediaControlKey
ConvertMediaSessionActionToControlKey(
32 MediaSessionAction aAction
) {
34 case MediaSessionAction::Play
:
35 return MediaControlKey::Play
;
36 case MediaSessionAction::Pause
:
37 return MediaControlKey::Pause
;
38 case MediaSessionAction::Seekbackward
:
39 return MediaControlKey::Seekbackward
;
40 case MediaSessionAction::Seekforward
:
41 return MediaControlKey::Seekforward
;
42 case MediaSessionAction::Previoustrack
:
43 return MediaControlKey::Previoustrack
;
44 case MediaSessionAction::Nexttrack
:
45 return MediaControlKey::Nexttrack
;
46 case MediaSessionAction::Skipad
:
47 return MediaControlKey::Skipad
;
48 case MediaSessionAction::Seekto
:
49 return MediaControlKey::Seekto
;
51 MOZ_ASSERT(aAction
== MediaSessionAction::Stop
);
52 return MediaControlKey::Stop
;
56 inline const char* ToMediaAudibleStateStr(MediaAudibleState aState
) {
58 case MediaAudibleState::eInaudible
:
60 case MediaAudibleState::eAudible
:
63 MOZ_ASSERT_UNREACHABLE("Invalid audible state.");
68 inline const char* ToMediaSessionPlaybackStateStr(
69 const MediaSessionPlaybackState
& aState
) {
71 case MediaSessionPlaybackState::None
:
73 case MediaSessionPlaybackState::Paused
:
75 case MediaSessionPlaybackState::Playing
:
78 MOZ_ASSERT_UNREACHABLE("Invalid MediaSessionPlaybackState.");
83 BrowsingContext
* GetAliveTopBrowsingContext(BrowsingContext
* aBC
);
85 inline bool IsImageIn(const nsTArray
<MediaImage
>& aArtwork
,
86 const nsAString
& aImageUrl
) {
87 for (const MediaImage
& image
: aArtwork
) {
88 if (image
.mSrc
== aImageUrl
) {
95 // The image buffer would be allocated in aStream whose size is aSize and the
96 // buffer head is aBuffer
97 inline nsresult
GetEncodedImageBuffer(imgIContainer
* aImage
,
98 const nsACString
& aMimeType
,
99 nsIInputStream
** aStream
, uint32_t* aSize
,
103 nsCOMPtr
<imgITools
> imgTools
= do_GetService("@mozilla.org/image/tools;1");
105 return NS_ERROR_FAILURE
;
108 nsCOMPtr
<nsIInputStream
> inputStream
;
109 nsresult rv
= imgTools
->EncodeImage(aImage
, aMimeType
, u
""_ns
,
110 getter_AddRefs(inputStream
));
116 return NS_ERROR_FAILURE
;
119 nsCOMPtr
<imgIEncoder
> encoder
= do_QueryInterface(inputStream
);
121 return NS_ERROR_FAILURE
;
124 rv
= encoder
->GetImageBufferUsed(aSize
);
129 rv
= encoder
->GetImageBuffer(aBuffer
);
134 encoder
.forget(aStream
);
138 inline bool IsValidImageUrl(const nsAString
& aUrl
) {
139 return StringBeginsWith(aUrl
, u
"http://"_ns
) ||
140 StringBeginsWith(aUrl
, u
"https://"_ns
);
143 inline uint32_t GetMediaKeyMask(mozilla::dom::MediaControlKey aKey
) {
144 return 1 << static_cast<uint8_t>(aKey
);
147 } // namespace mozilla::dom
149 #endif // DOM_MEDIA_MEDIACONTROL_MEDIACONTROLUTILS_H_