MediaControlUtils.h (4397B)
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/. */ 6 7 #ifndef DOM_MEDIA_MEDIACONTROL_MEDIACONTROLUTILS_H_ 8 #define DOM_MEDIA_MEDIACONTROL_MEDIACONTROLUTILS_H_ 9 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" 18 19 extern mozilla::LazyLogModule gMediaControlLog; 20 21 namespace mozilla::dom { 22 23 inline const char* ToMediaControlKeyStr(const Maybe<MediaControlKey>& aKey) { 24 if (aKey.isNothing()) { 25 MOZ_ASSERT_UNREACHABLE("Invalid action."); 26 return "Unknown"; 27 } 28 return GetEnumString(aKey.value()).get(); 29 } 30 31 inline MediaControlKey ConvertMediaSessionActionToControlKey( 32 MediaSessionAction aAction) { 33 switch (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; 50 default: 51 MOZ_ASSERT(aAction == MediaSessionAction::Stop); 52 return MediaControlKey::Stop; 53 } 54 } 55 56 inline const char* ToMediaAudibleStateStr(MediaAudibleState aState) { 57 switch (aState) { 58 case MediaAudibleState::eInaudible: 59 return "inaudible"; 60 case MediaAudibleState::eAudible: 61 return "audible"; 62 default: 63 MOZ_ASSERT_UNREACHABLE("Invalid audible state."); 64 return "Unknown"; 65 } 66 } 67 68 inline const char* ToMediaSessionPlaybackStateStr( 69 const MediaSessionPlaybackState& aState) { 70 switch (aState) { 71 case MediaSessionPlaybackState::None: 72 return "none"; 73 case MediaSessionPlaybackState::Paused: 74 return "paused"; 75 case MediaSessionPlaybackState::Playing: 76 return "playing"; 77 default: 78 MOZ_ASSERT_UNREACHABLE("Invalid MediaSessionPlaybackState."); 79 return "Unknown"; 80 } 81 } 82 83 BrowsingContext* GetAliveTopBrowsingContext(BrowsingContext* aBC); 84 85 inline bool IsImageIn(const nsTArray<MediaImage>& aArtwork, 86 const nsAString& aImageUrl) { 87 for (const MediaImage& image : aArtwork) { 88 if (image.mSrc == aImageUrl) { 89 return true; 90 } 91 } 92 return false; 93 } 94 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, 100 char** aBuffer) { 101 MOZ_ASSERT(aImage); 102 103 nsCOMPtr<imgITools> imgTools = do_GetService("@mozilla.org/image/tools;1"); 104 if (!imgTools) { 105 return NS_ERROR_FAILURE; 106 } 107 108 nsCOMPtr<nsIInputStream> inputStream; 109 nsresult rv = imgTools->EncodeImage(aImage, aMimeType, u""_ns, 110 getter_AddRefs(inputStream)); 111 if (NS_FAILED(rv)) { 112 return rv; 113 } 114 115 if (!inputStream) { 116 return NS_ERROR_FAILURE; 117 } 118 119 nsCOMPtr<imgIEncoder> encoder = do_QueryInterface(inputStream); 120 if (!encoder) { 121 return NS_ERROR_FAILURE; 122 } 123 124 rv = encoder->GetImageBufferUsed(aSize); 125 if (NS_FAILED(rv)) { 126 return rv; 127 } 128 129 rv = encoder->GetImageBuffer(aBuffer); 130 if (NS_FAILED(rv)) { 131 return rv; 132 } 133 134 encoder.forget(aStream); 135 return NS_OK; 136 } 137 138 inline bool IsValidImageUrl(const nsAString& aUrl) { 139 return StringBeginsWith(aUrl, u"http://"_ns) || 140 StringBeginsWith(aUrl, u"https://"_ns); 141 } 142 143 inline uint32_t GetMediaKeyMask(mozilla::dom::MediaControlKey aKey) { 144 return 1 << static_cast<uint8_t>(aKey); 145 } 146 147 } // namespace mozilla::dom 148 149 #endif // DOM_MEDIA_MEDIACONTROL_MEDIACONTROLUTILS_H_