2019-08-07 04:46:10 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2019-10-30 02:08:05 +03:00
|
|
|
#ifndef DOM_MEDIA_MEDIACONTROL_MEDIACONTROLUTILS_H_
|
|
|
|
#define DOM_MEDIA_MEDIACONTROL_MEDIACONTROLUTILS_H_
|
2019-08-07 04:46:10 +03:00
|
|
|
|
2020-07-29 18:59:44 +03:00
|
|
|
#include "imgIEncoder.h"
|
|
|
|
#include "imgITools.h"
|
2019-08-07 04:46:10 +03:00
|
|
|
#include "MediaController.h"
|
2019-12-21 03:10:13 +03:00
|
|
|
#include "mozilla/dom/ChromeUtilsBinding.h"
|
2020-06-08 21:51:12 +03:00
|
|
|
#include "mozilla/dom/MediaControllerBinding.h"
|
2019-12-21 03:10:13 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2020-07-29 18:59:46 +03:00
|
|
|
#include "nsReadableUtils.h"
|
2019-08-07 04:46:10 +03:00
|
|
|
|
2019-11-26 12:25:33 +03:00
|
|
|
extern mozilla::LazyLogModule gMediaControlLog;
|
2019-08-07 04:46:10 +03:00
|
|
|
|
2019-08-08 23:01:19 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2020-06-09 05:59:57 +03:00
|
|
|
inline const char* ToMediaControlKeyStr(MediaControlKey aKey) {
|
|
|
|
switch (aKey) {
|
|
|
|
case MediaControlKey::Focus:
|
2020-05-28 01:47:35 +03:00
|
|
|
return "Focus";
|
2020-06-09 05:59:57 +03:00
|
|
|
case MediaControlKey::Pause:
|
2019-12-13 03:52:00 +03:00
|
|
|
return "Pause";
|
2020-06-09 05:59:57 +03:00
|
|
|
case MediaControlKey::Play:
|
2019-12-13 03:52:00 +03:00
|
|
|
return "Play";
|
2020-06-09 05:59:57 +03:00
|
|
|
case MediaControlKey::Playpause:
|
2019-12-13 03:52:00 +03:00
|
|
|
return "Play & pause";
|
2020-06-09 05:59:57 +03:00
|
|
|
case MediaControlKey::Previoustrack:
|
2019-12-13 03:52:00 +03:00
|
|
|
return "Previous track";
|
2020-06-09 05:59:57 +03:00
|
|
|
case MediaControlKey::Nexttrack:
|
2019-12-13 03:52:00 +03:00
|
|
|
return "Next track";
|
2020-06-09 05:59:57 +03:00
|
|
|
case MediaControlKey::Seekbackward:
|
2019-12-13 03:52:00 +03:00
|
|
|
return "Seek backward";
|
2020-06-09 05:59:57 +03:00
|
|
|
case MediaControlKey::Seekforward:
|
2019-12-13 03:52:00 +03:00
|
|
|
return "Seek forward";
|
2020-07-10 21:59:31 +03:00
|
|
|
case MediaControlKey::Skipad:
|
|
|
|
return "Skip Ad";
|
2020-07-16 03:16:33 +03:00
|
|
|
case MediaControlKey::Seekto:
|
|
|
|
return "Seek to";
|
2020-06-09 05:59:57 +03:00
|
|
|
case MediaControlKey::Stop:
|
2019-12-13 03:52:00 +03:00
|
|
|
return "Stop";
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Invalid action.");
|
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 23:05:08 +03:00
|
|
|
inline const char* ToMediaSessionActionStr(MediaSessionAction aAction) {
|
|
|
|
switch (aAction) {
|
|
|
|
case MediaSessionAction::Play:
|
|
|
|
return "play";
|
|
|
|
case MediaSessionAction::Pause:
|
|
|
|
return "pause";
|
|
|
|
case MediaSessionAction::Seekbackward:
|
|
|
|
return "seek backward";
|
|
|
|
case MediaSessionAction::Seekforward:
|
|
|
|
return "seek forward";
|
|
|
|
case MediaSessionAction::Previoustrack:
|
|
|
|
return "previous track";
|
|
|
|
case MediaSessionAction::Nexttrack:
|
|
|
|
return "next track";
|
2020-07-10 21:59:31 +03:00
|
|
|
case MediaSessionAction::Skipad:
|
|
|
|
return "skip ad";
|
2020-07-16 03:16:33 +03:00
|
|
|
case MediaSessionAction::Seekto:
|
|
|
|
return "Seek to";
|
2020-05-04 23:05:08 +03:00
|
|
|
default:
|
|
|
|
MOZ_ASSERT(aAction == MediaSessionAction::Stop);
|
|
|
|
return "stop";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-09 05:59:57 +03:00
|
|
|
inline MediaControlKey ConvertMediaSessionActionToControlKey(
|
2020-06-09 05:23:35 +03:00
|
|
|
MediaSessionAction aAction) {
|
|
|
|
switch (aAction) {
|
|
|
|
case MediaSessionAction::Play:
|
2020-06-09 05:59:57 +03:00
|
|
|
return MediaControlKey::Play;
|
2020-06-09 05:23:35 +03:00
|
|
|
case MediaSessionAction::Pause:
|
2020-06-09 05:59:57 +03:00
|
|
|
return MediaControlKey::Pause;
|
2020-06-09 05:23:35 +03:00
|
|
|
case MediaSessionAction::Seekbackward:
|
2020-06-09 05:59:57 +03:00
|
|
|
return MediaControlKey::Seekbackward;
|
2020-06-09 05:23:35 +03:00
|
|
|
case MediaSessionAction::Seekforward:
|
2020-06-09 05:59:57 +03:00
|
|
|
return MediaControlKey::Seekforward;
|
2020-06-09 05:23:35 +03:00
|
|
|
case MediaSessionAction::Previoustrack:
|
2020-06-09 05:59:57 +03:00
|
|
|
return MediaControlKey::Previoustrack;
|
2020-06-09 05:23:35 +03:00
|
|
|
case MediaSessionAction::Nexttrack:
|
2020-06-09 05:59:57 +03:00
|
|
|
return MediaControlKey::Nexttrack;
|
2020-07-10 21:59:31 +03:00
|
|
|
case MediaSessionAction::Skipad:
|
|
|
|
return MediaControlKey::Skipad;
|
2020-07-16 03:16:33 +03:00
|
|
|
case MediaSessionAction::Seekto:
|
|
|
|
return MediaControlKey::Seekto;
|
2020-06-09 05:23:35 +03:00
|
|
|
default:
|
|
|
|
MOZ_ASSERT(aAction == MediaSessionAction::Stop);
|
2020-06-09 05:59:57 +03:00
|
|
|
return MediaControlKey::Stop;
|
2020-06-09 05:23:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline MediaSessionAction ConvertToMediaSessionAction(uint8_t aActionValue) {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aActionValue < uint8_t(MediaSessionAction::EndGuard_));
|
|
|
|
return static_cast<MediaSessionAction>(aActionValue);
|
|
|
|
}
|
|
|
|
|
2020-04-28 10:14:05 +03:00
|
|
|
inline const char* ToMediaPlaybackStateStr(MediaPlaybackState aState) {
|
2020-01-03 04:34:34 +03:00
|
|
|
switch (aState) {
|
2020-04-28 10:14:05 +03:00
|
|
|
case MediaPlaybackState::eStarted:
|
2020-01-03 04:34:34 +03:00
|
|
|
return "started";
|
2020-04-28 10:14:05 +03:00
|
|
|
case MediaPlaybackState::ePlayed:
|
2020-01-03 04:34:34 +03:00
|
|
|
return "played";
|
2020-04-28 10:14:05 +03:00
|
|
|
case MediaPlaybackState::ePaused:
|
2020-01-03 04:34:34 +03:00
|
|
|
return "paused";
|
2020-04-28 10:14:05 +03:00
|
|
|
case MediaPlaybackState::eStopped:
|
2020-01-03 04:34:34 +03:00
|
|
|
return "stopped";
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Invalid media state.");
|
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
}
|
2020-01-03 04:29:38 +03:00
|
|
|
|
2020-05-13 20:05:31 +03:00
|
|
|
inline const char* ToMediaAudibleStateStr(MediaAudibleState aState) {
|
|
|
|
switch (aState) {
|
|
|
|
case MediaAudibleState::eInaudible:
|
|
|
|
return "inaudible";
|
|
|
|
case MediaAudibleState::eAudible:
|
|
|
|
return "audible";
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Invalid audible state.");
|
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 21:50:45 +03:00
|
|
|
inline const char* ToMediaSessionPlaybackStateStr(
|
|
|
|
const MediaSessionPlaybackState& aState) {
|
|
|
|
switch (aState) {
|
|
|
|
case MediaSessionPlaybackState::None:
|
|
|
|
return "none";
|
|
|
|
case MediaSessionPlaybackState::Paused:
|
|
|
|
return "paused";
|
|
|
|
case MediaSessionPlaybackState::Playing:
|
|
|
|
return "playing";
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Invalid MediaSessionPlaybackState.");
|
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-03 04:34:34 +03:00
|
|
|
BrowsingContext* GetAliveTopBrowsingContext(BrowsingContext* aBC);
|
2019-08-08 23:01:19 +03:00
|
|
|
|
2020-07-29 18:59:44 +03:00
|
|
|
inline bool IsImageIn(const nsTArray<MediaImage>& aArtwork,
|
|
|
|
const nsAString& aImageUrl) {
|
|
|
|
for (const MediaImage& image : aArtwork) {
|
|
|
|
if (image.mSrc == aImageUrl) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The image buffer would be allocated in aStream whose size is aSize and the
|
|
|
|
// buffer head is aBuffer
|
|
|
|
inline nsresult GetEncodedImageBuffer(imgIContainer* aImage,
|
|
|
|
const nsACString& aMimeType,
|
|
|
|
nsIInputStream** aStream, uint32_t* aSize,
|
|
|
|
char** aBuffer) {
|
|
|
|
MOZ_ASSERT(aImage);
|
|
|
|
|
|
|
|
nsCOMPtr<imgITools> imgTools = do_GetService("@mozilla.org/image/tools;1");
|
|
|
|
if (!imgTools) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> inputStream;
|
2020-09-23 18:17:15 +03:00
|
|
|
nsresult rv = imgTools->EncodeImage(aImage, aMimeType, u""_ns,
|
2020-07-29 18:59:44 +03:00
|
|
|
getter_AddRefs(inputStream));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!inputStream) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<imgIEncoder> encoder = do_QueryInterface(inputStream);
|
|
|
|
if (!encoder) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = encoder->GetImageBufferUsed(aSize);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = encoder->GetImageBuffer(aBuffer);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
encoder.forget(aStream);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-07-29 18:59:46 +03:00
|
|
|
inline bool IsValidImageUrl(const nsAString& aUrl) {
|
|
|
|
return StringBeginsWith(aUrl, u"http://"_ns) ||
|
|
|
|
StringBeginsWith(aUrl, u"https://"_ns);
|
|
|
|
}
|
|
|
|
|
2020-08-18 21:28:19 +03:00
|
|
|
inline uint32_t GetMediaKeyMask(mozilla::dom::MediaControlKey aKey) {
|
|
|
|
return 1 << static_cast<uint8_t>(aKey);
|
|
|
|
}
|
|
|
|
|
2019-08-08 23:01:19 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2019-10-30 02:08:05 +03:00
|
|
|
#endif // DOM_MEDIA_MEDIACONTROL_MEDIACONTROLUTILS_H_
|