UNPKG

nativescript-cast-ns6-beta

Version:

NativeScript Chromecast Plugin.

465 lines 21.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var utils_1 = require("tns-core-modules/utils/utils"); var color_1 = require("tns-core-modules/color"); var cast_common_1 = require("./cast.common"); var cast_types_1 = require("./cast.types"); var camelCase = require('lodash/fp/camelCase'); var snakeCase = require('lodash/fp/snakeCase'); var _a = androidx.mediarouter.media, MediaRouter = _a.MediaRouter, MediaRouteSelector = _a.MediaRouteSelector, MediaControlIntent = _a.MediaControlIntent; var _b = com.google.android.gms.cast.framework, CastButtonFactory = _b.CastButtonFactory, CastContext = _b.CastContext; var MediaInfo = com.google.android.gms.cast.MediaInfo; var MediaMetadata = com.google.android.gms.cast.MediaMetadata; var WebImage = com.google.android.gms.common.images.WebImage; var MediaTrack = com.google.android.gms.cast.MediaTrack; var ArrayList = java.util.ArrayList; var MediaStatus = com.google.android.gms.cast.MediaStatus; var MediaRouterCallback = (function (_super) { __extends(MediaRouterCallback, _super); function MediaRouterCallback(owner) { var _this = _super.call(this) || this; _this.owner = owner; return global.__native(_this); } MediaRouterCallback.prototype.onProviderAdded = function (router, provider) { this.owner.sendEvent(cast_common_1.CastButtonBase.castEvent, { eventName: cast_types_1.CastEvent.onProviderAdded, router: router, provider: provider, android: this.owner.nativeView }); }; MediaRouterCallback.prototype.onProviderChanged = function (router, provider) { this.owner.sendEvent(cast_common_1.CastButtonBase.castEvent, { eventName: cast_types_1.CastEvent.onProviderChanged, router: router, provider: provider, android: this.owner.nativeView }); }; MediaRouterCallback.prototype.onProviderRemoved = function (router, provider) { this.owner.sendEvent(cast_common_1.CastButtonBase.castEvent, { eventName: cast_types_1.CastEvent.onProviderRemoved, router: router, provider: provider, android: this.owner.nativeView }); }; MediaRouterCallback.prototype.onRouteAdded = function (router, route) { this.owner.sendEvent(cast_common_1.CastButtonBase.castEvent, { eventName: cast_types_1.CastEvent.onRouteAdded, router: router, route: route, android: this.owner.nativeView }); }; MediaRouterCallback.prototype.onRouteChanged = function (router, route) { this.owner.sendEvent(cast_common_1.CastButtonBase.castEvent, { eventName: cast_types_1.CastEvent.onDeviceChanged, router: router, route: route, android: this.owner.nativeView }); }; MediaRouterCallback.prototype.onRoutePresentationDisplayChanged = function (router, route) { this.owner.sendEvent(cast_common_1.CastButtonBase.castEvent, { eventName: cast_types_1.CastEvent.onRoutePresentationDisplayChanged, router: router, route: route, android: this.owner.nativeView }); }; MediaRouterCallback.prototype.onRouteRemoved = function (router, route) { this.owner.sendEvent(cast_common_1.CastButtonBase.castEvent, { eventName: cast_types_1.CastEvent.onRouteRemoved, router: router, route: route, android: this.owner.nativeView }); }; MediaRouterCallback.prototype.onRouteSelected = function (router, route) { this.owner.sendEvent(cast_common_1.CastButtonBase.castEvent, { eventName: cast_types_1.CastEvent.onRouteSelected, router: router, route: route, android: this.owner.nativeView }); }; MediaRouterCallback.prototype.onRouteUnselected = function (router, route) { this.owner.sendEvent(cast_common_1.CastButtonBase.castEvent, { eventName: cast_types_1.CastEvent.onRouteUnselected, router: router, route: route, android: this.owner.nativeView }); }; MediaRouterCallback.prototype.onRouteVolumeChanged = function (router, route) { this.owner.sendEvent(cast_common_1.CastButtonBase.castEvent, { eventName: cast_types_1.CastEvent.onDeviceVolumeChanged, router: router, route: route, volume: route.getVolume() / 20, android: this.owner.nativeView, }); }; return MediaRouterCallback; }(androidx.mediarouter.media.MediaRouter.Callback)); var SessionManagerListener; function initSessionManagerListener() { if (SessionManagerListener) { return; } var SessionManagerListenerImpl = (function (_super) { __extends(SessionManagerListenerImpl, _super); function SessionManagerListenerImpl(owner) { var _this = _super.call(this) || this; _this.owner = owner; return global.__native(_this); } SessionManagerListenerImpl.prototype.onSessionEnded = function (session, error) { this.owner.sendEvent(cast_common_1.CastButtonBase.castEvent, { eventName: cast_types_1.CastEvent.onSessionEnded, session: session, error: error, android: this.owner.nativeView }); }; SessionManagerListenerImpl.prototype.onSessionEnding = function (session) { this.owner.sendEvent(cast_common_1.CastButtonBase.castEvent, { eventName: cast_types_1.CastEvent.onSessionEnding, session: session, android: this.owner.nativeView }); }; SessionManagerListenerImpl.prototype.onSessionResumeFailed = function (session, error) { }; SessionManagerListenerImpl.prototype.onSessionResumed = function (session, wasSuspended) { this.owner.sendEvent(cast_common_1.CastButtonBase.castEvent, { eventName: cast_types_1.CastEvent.onSessionResumed, session: session, wasSuspended: wasSuspended, android: this.owner.nativeView }); }; SessionManagerListenerImpl.prototype.onSessionResuming = function (session, sessionId) { this.owner.sendEvent(cast_common_1.CastButtonBase.castEvent, { eventName: cast_types_1.CastEvent.onSessionResuming, session: session, sessionId: sessionId, android: this.owner.nativeView }); }; SessionManagerListenerImpl.prototype.onSessionStartFailed = function (session, error) { this.owner.sendEvent(cast_common_1.CastButtonBase.castEvent, { eventName: cast_types_1.CastEvent.onSessionStartFailed, session: session, error: error, android: this.owner.nativeView }); }; SessionManagerListenerImpl.prototype.onSessionStarted = function (session, sessionId) { this.owner.sendEvent(cast_common_1.CastButtonBase.castEvent, { eventName: cast_types_1.CastEvent.onSessionStarted, session: session, sessionId: sessionId, android: this.owner.nativeView }); }; SessionManagerListenerImpl.prototype.onSessionStarting = function (session) { this.owner.sendEvent(cast_common_1.CastButtonBase.castEvent, { eventName: cast_types_1.CastEvent.onSessionStarting, session: session, android: this.owner.nativeView }); }; SessionManagerListenerImpl.prototype.onSessionSuspended = function (session, reason) { this.owner.sendEvent(cast_common_1.CastButtonBase.castEvent, { eventName: cast_types_1.CastEvent.onSessionSuspended, session: session, reason: reason, android: this.owner.nativeView }); }; SessionManagerListenerImpl = __decorate([ Interfaces([com.google.android.gms.cast.framework.SessionManagerListener]), __metadata("design:paramtypes", [Object]) ], SessionManagerListenerImpl); return SessionManagerListenerImpl; }(java.lang.Object)); SessionManagerListener = SessionManagerListenerImpl; } var RemoteMediaClientListener; function initRemoteMediaClientListener() { if (RemoteMediaClientListener) { return; } var RemoteMediaClientListenerImpl = (function (_super) { __extends(RemoteMediaClientListenerImpl, _super); function RemoteMediaClientListenerImpl(owner) { var _this = _super.call(this) || this; _this.owner = owner; return global.__native(_this); } RemoteMediaClientListenerImpl.prototype.onStatusUpdated = function () { this.owner.onMediaStatusUpdate(); }; RemoteMediaClientListenerImpl.prototype.onMetadataUpdated = function () { }; RemoteMediaClientListenerImpl.prototype.onQueueStatusUpdated = function () { }; RemoteMediaClientListenerImpl.prototype.onPreloadStatusUpdated = function () { }; RemoteMediaClientListenerImpl.prototype.onSendingRemoteMediaRequest = function () { }; RemoteMediaClientListenerImpl.prototype.onAdBreakStatusUpdated = function () { }; RemoteMediaClientListenerImpl = __decorate([ Interfaces([com.google.android.gms.cast.framework.media.RemoteMediaClient.Listener]), __metadata("design:paramtypes", [Object]) ], RemoteMediaClientListenerImpl); return RemoteMediaClientListenerImpl; }(java.lang.Object)); RemoteMediaClientListener = RemoteMediaClientListenerImpl; } var CastButton = (function (_super) { __extends(CastButton, _super); function CastButton() { return _super.call(this) || this; } CastButton.prototype.createNativeView = function () { var appContext = utils_1.ad.getApplicationContext(); initSessionManagerListener(); initRemoteMediaClientListener(); var button = new androidx.mediarouter.app.MediaRouteButton(this._context); CastButtonFactory.setUpMediaRouteButton(appContext, button); this.mMediaRouter = MediaRouter.getInstance(appContext); this.mMediaRouteSelector = new MediaRouteSelector.Builder() .addControlCategory(MediaControlIntent.CATEGORY_LIVE_VIDEO) .addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK) .build(); this.mMediaRouterCallback = new MediaRouterCallback(this); this.mCastContext = CastContext.getSharedInstance(appContext); this.mSessionManager = this.mCastContext.getSessionManager(); this.mSessionManagerListener = new SessionManagerListener(this); this.mRemoteMediaClientListener = new RemoteMediaClientListener(this); this.addMediaRouterCallback(); this.addSessionManagerListener(); this.CastDevice = com.google.android.gms.cast.CastDevice; return button; }; CastButton.prototype.getNativeView = function () { return this.nativeView; }; CastButton.prototype.initNativeView = function () { this.nativeView.owner = this; _super.prototype.initNativeView.call(this); }; CastButton.prototype.disposeNativeView = function () { this.nativeView.owner = null; this.removeMediaRouterCallback(); this.removeSessionManagerListener(); _super.prototype.disposeNativeView.call(this); }; CastButton.prototype.addMediaRouterCallback = function () { this.mMediaRouter.addCallback(this.mMediaRouteSelector, this.mMediaRouterCallback, MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY); }; CastButton.prototype.removeMediaRouterCallback = function () { if (this.mMediaRouter && this.mMediaRouterCallback) { this.mMediaRouter.removeCallback(this.mMediaRouterCallback); } }; CastButton.prototype.addSessionManagerListener = function () { this.mSessionManager.addSessionManagerListener(this.mSessionManagerListener); }; CastButton.prototype.removeSessionManagerListener = function () { this.mSessionManager.removeSessionManagerListener(this.mSessionManagerListener); }; CastButton.prototype.getRemoteMediaClient = function () { return this.mSessionManager.getCurrentCastSession().getRemoteMediaClient(); }; CastButton.prototype.loadMedia = function (mediaInfo, autoplay, position) { if (autoplay === void 0) { autoplay = true; } var metadataPrefix = 'KEY_'; var metadata; if (mediaInfo.metadata) { var metadataType = typeof mediaInfo.metadata.metadataType === 'string' ? this.metadataTypeStringToNumber(mediaInfo.metadata.metadataType) : mediaInfo.metadata.metadataType; metadata = new MediaMetadata(metadataType); Object.keys(mediaInfo.metadata).forEach(function (key) { if (cast_common_1.CastButtonBase.validMetadataKeys.indexOf(key) > -1) { var fixedKey = metadataPrefix + snakeCase(key).toUpperCase(); var value = mediaInfo.metadata[key]; metadata.putString(MediaMetadata[fixedKey], value); } }); if (mediaInfo.metadata.images && mediaInfo.metadata.images.length) { mediaInfo.metadata.images.forEach(function (img) { var uri = android.net.Uri.parse(img.url); var thumb = new WebImage(uri, img.width, img.height); metadata.addImage(thumb); }); } } var streamType = typeof mediaInfo.streamType === 'string' ? this.streamTypeStringToNumber(mediaInfo.streamType) : mediaInfo.streamType; var builtMediaInfo = new MediaInfo.Builder(mediaInfo.contentId) .setContentType(mediaInfo.contentType) .setStreamType(MediaInfo[streamType]); if (metadata) { builtMediaInfo.setMetadata(metadata); } if (mediaInfo.duration) { builtMediaInfo.setStreamDuration(mediaInfo.duration); } if (mediaInfo.customData) { var customData = new org.json.JSONObject(JSON.stringify(mediaInfo.customData)); builtMediaInfo.setCustomData(customData); } if (mediaInfo.textTracks && mediaInfo.textTracks.length > 0) { var tracks_1 = new ArrayList(); mediaInfo.textTracks.forEach(function (item, index) { var track = new MediaTrack.Builder(index + 1, MediaTrack.TYPE_TEXT) .setContentId(item.src) .setContentType(item.contentType) .setSubtype(MediaTrack.SUBTYPE_SUBTITLES) .setName(item.name) .setLanguage(item.language) .build(); tracks_1.add(track); }); builtMediaInfo.setMediaTracks(tracks_1); } var remoteMediaClient = this.getRemoteMediaClient(); remoteMediaClient.addListener(this.mRemoteMediaClientListener); remoteMediaClient.load(builtMediaInfo.build(), autoplay, position); }; CastButton.prototype.showController = function () { var intent = new android.content.Intent(this._context, com.google.android.gms.cast.framework.media.widget.ExpandedControllerActivity.class); this._context.startActivity(intent); }; CastButton.prototype.getMediaInfo = function () { var mediaInfo = this.getRemoteMediaClient().getMediaInfo(); return this.convertMediaInfo(mediaInfo); }; CastButton.prototype.pauseMedia = function (customData) { this.getRemoteMediaClient().pause(customData); }; CastButton.prototype.playMedia = function (customData) { this.getRemoteMediaClient().play(customData); }; CastButton.prototype.seekMedia = function (position, resumeState, customData) { if (resumeState === void 0) { resumeState = 0; } this.getRemoteMediaClient().seek(position * 1000, resumeState, customData); }; CastButton.prototype.stopMedia = function (customData) { this.getRemoteMediaClient().stop(customData); }; CastButton.prototype.setActiveTrackIds = function (trackIds) { this.getRemoteMediaClient().setActiveMediaTracks(trackIds); }; CastButton.prototype.setTintColor = function (color) { var mRouteButton = this.getNativeView(); var appContext = utils_1.ad.getApplicationContext(); var castContext = new android.view.ContextThemeWrapper(appContext, androidx.mediarouter.mediarouter.R.style.Theme_MediaRouter); var tintColor = new color_1.Color(color).android; var a = castContext.obtainStyledAttributes(null, androidx.mediarouter.mediarouter.R.styleable.MediaRouteButton, androidx.mediarouter.mediarouter.R.attr.mediaRouteButtonStyle, 0); var drawable = a.getDrawable(androidx.mediarouter.mediarouter.R.styleable.MediaRouteButton_externalRouteEnabledDrawable); a.recycle(); androidx.core.graphics.drawable.DrawableCompat.setTint(drawable, tintColor); mRouteButton.setRemoteIndicatorDrawable(drawable); }; CastButton.prototype.onMediaStatusUpdate = function () { var mediaStatus = this.getRemoteMediaClient().getMediaStatus(); var info = null; var status = null; if (mediaStatus) { var mediaInfo = mediaStatus.getMediaInfo(); if (mediaInfo) { info = this.convertMediaInfo(mediaInfo); } var playerState = cast_types_1.PlayerState.UNKNOWN; var trackIds = mediaStatus.getActiveTrackIds(); var activeTrackIds = []; if (trackIds) { for (var i = 0; i < trackIds.length; i++) { activeTrackIds.push(trackIds[i]); } } switch (mediaStatus.getPlayerState()) { case MediaStatus.PLAYER_STATE_IDLE: playerState = cast_types_1.PlayerState.IDLE; break; case MediaStatus.PLAYER_STATE_PLAYING: playerState = cast_types_1.PlayerState.PLAYING; break; case MediaStatus.PLAYER_STATE_PAUSED: playerState = cast_types_1.PlayerState.PAUSED; break; case MediaStatus.PLAYER_STATE_BUFFERING: playerState = cast_types_1.PlayerState.BUFFERING; break; } status = { playerState: playerState, activeTrackIds: activeTrackIds, }; } this.sendEvent(cast_common_1.CastButtonBase.castEvent, { eventName: cast_types_1.CastEvent.onMediaStatusChanged, status: status, info: info, android: this.nativeView }); }; CastButton.prototype.convertMediaInfo = function (mediaInfo) { if (!mediaInfo) { return null; } var metadata = mediaInfo.getMetadata(); var metaDataKeys = utils_1.ad.collections.stringSetToStringArray(metadata.keySet()); var images = metadata.getImages(); var tracks = mediaInfo.getMediaTracks(); var textTracks = []; if (tracks) { for (var i = 0; i < tracks.size(); i++) { var track = tracks.get(i); if (track.getType() === MediaTrack.TYPE_TEXT) { textTracks.push({ id: track.getId(), src: track.getContentId(), contentType: track.getContentType(), name: track.getName(), language: track.getLanguage(), }); } } } var castMetadata = { metadataType: this.metadataTypeNumberToString(metadata.getMediaType()), images: [], }; metaDataKeys.forEach(function (key) { var fixedKey = camelCase(key.replace('com.google.android.gms.cast.metadata.', '')); castMetadata[fixedKey] = metadata.getString(key); }); if (images) { for (var index_1 = 0; index_1 < images.size(); index_1++) { var img = images.get(index_1); castMetadata.images.push({ url: img.getUrl().toString(), width: img.getWidth(), height: img.getHeight() }); } } return { contentId: mediaInfo.getContentId(), streamType: this.streamTypeNumberToString(mediaInfo.getStreamType()), contentType: mediaInfo.getContentType(), metadata: castMetadata, duration: mediaInfo.getStreamDuration() / 1000, textTracks: textTracks }; }; return CastButton; }(cast_common_1.CastButtonBase)); exports.CastButton = CastButton; //# sourceMappingURL=cast.android.js.map