Skip to content

Commit

Permalink
Fix handling of supportedMediaCommands MEDIA_STATUS bitflag
Browse files Browse the repository at this point in the history
  • Loading branch information
hensm committed Apr 17, 2022
1 parent 0405907 commit 61f9ff0
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions ext/src/cast/api/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,26 @@ import {

import { Image, Receiver, SenderApplication } from "./dataClasses";
import { SessionStatus } from "./enums";
import { Media, LoadRequest, QueueLoadRequest, QueueItem } from "./media";
import {
Media,
LoadRequest,
QueueLoadRequest,
QueueItem,
MediaCommand
} from "./media";

const NS_MEDIA = "urn:x-cast:com.google.cast.media";

/** supportedMediaCommands bitflag returned in MEDIA_STATUS messages */
enum _MediaCommand {
PAUSE = 1,
SEEK = 2,
STREAM_VOLUME = 4,
STREAM_MUTE = 8,
QUEUE_NEXT = 64,
QUEUE_PREV = 128
}

/**
* Takes a media object and a media status object and merges
* the status with the existing media object, updating it with
Expand All @@ -40,11 +56,35 @@ function updateMedia(media: Media, status: MediaStatus) {

// Copy props
for (const prop in status) {
if (prop !== "items" && status.hasOwnProperty(prop)) {
switch (prop) {
case "items":
case "supportedMediaCommands":
continue;
}

if (status.hasOwnProperty(prop)) {
(media as any)[prop] = (status as any)[prop];
}
}

// Convert supportedMediaCommands bitflag to string array
const supportedMediaCommands: string[] = [];
if (status.supportedMediaCommands & _MediaCommand.PAUSE) {
supportedMediaCommands.push(MediaCommand.PAUSE);
} else if (status.supportedMediaCommands & _MediaCommand.SEEK) {
supportedMediaCommands.push(MediaCommand.SEEK);
} else if (status.supportedMediaCommands & _MediaCommand.STREAM_VOLUME) {
supportedMediaCommands.push(MediaCommand.STREAM_VOLUME);
} else if (status.supportedMediaCommands & _MediaCommand.STREAM_MUTE) {
supportedMediaCommands.push(MediaCommand.STREAM_MUTE);
} else if (status.supportedMediaCommands & _MediaCommand.QUEUE_NEXT) {
supportedMediaCommands.push("queue_next");
} else if (status.supportedMediaCommands & _MediaCommand.QUEUE_PREV) {
supportedMediaCommands.push("queue_prev");
}

media.supportedMediaCommands = supportedMediaCommands;

// Update queue state
if (status.items) {
const newItems: QueueItem[] = [];
Expand Down

0 comments on commit 61f9ff0

Please sign in to comment.