import "pkg:/components/Dialog/DialogUtils.bs" import "pkg:/components/Services/Invidious/InvidiousToContentNode.bs" import "pkg:/components/Services/LoungeService/LongPoller.bs" import "pkg:/components/Services/LoungeService/LoungeConstants.bs" import "pkg:/components/Services/LoungeService/LoungeMessages.bs" import "pkg:/components/Services/LoungeService/LoungeVideoQueue.bs" import "pkg:/components/Services/LoungeService/Notifications/DeviceConnectedNotificationUtils.bs" import "pkg:/source/services/HttpClient.bs" import "pkg:/source/utils/Logging.bs" import "pkg:/source/utils/RegistryUtils.bs" import "pkg:/source/utils/StringUtils.bs" import "pkg:/source/utils/UrlUtils.bs" const COMMAND_HANDLER_PREFIX = "_Handle_" ' Apparently devices can be connected and disconnected very frequently. ' We don't want to spam the user with notifications, so we'll only show a notification ' if the last notification was more than 30 minutes ago. const CONNECT_NOTIFICATION_MIN_INTERVAL_SECONDS = 1800 class LoungeApi public loungeServiceNode public videoQueueNode public notificationsNode public invService public deviceId public deviceName public screenId public screenName public loungeToken public sessionId public gSessionId public devices private ofs private lastProcessedCommandIndex = -1 private poller private outgoingMessagesRequests function new(loungeServiceNode as object, videoQueueNode as object, notificationsNode as object, invidiousNode as object) m.loungeServiceNode = loungeServiceNode m.videoQueueNode = videoQueueNode m.notificationsNode = notificationsNode m.invService = new Invidious.InvidiousService(invidiousNode) m.dateTime = CreateObject("roDateTime") m.deviceId = loungeServiceNode.deviceId deviceName = loungeServiceNode.deviceName m.deviceName = deviceName m.screenName = deviceName m.ofs = 0 m.poller = invalid m.outgoingMessagesRequests = {} m.devices = {} commandHandlers = {} commandHandlers[COMMAND_TYPE_NOOP] = m.HandleIgnoreCommand commandHandlers[COMMAND_TYPE_GET_VOLUME] = m.HandleIgnoreCommand commandHandlers[COMMAND_TYPE_GET_SUBTITLES_TRACK] = m.HandleIgnoreCommand commandHandlers[COMMAND_TYPE_SET_SUBTITLES] = m.HandleIgnoreCommand commandHandlers[COMMAND_TYPE_SET_AUDIO_TRACK] = m.HandleIgnoreCommand commandHandlers[COMMAND_TYPE_DPAD_COMMAND] = m.HandleIgnoreCommand commandHandlers[COMMAND_TYPE_VOICE_COMMAND] = m.HandleIgnoreCommand commandHandlers[COMMAND_TYPE_ON_USER_ACTIVITY] = m.HandleIgnoreCommand commandHandlers[COMMAND_TYPE_REMOTE_DISCONNECTED] = m.HandleIgnoreCommand commandHandlers[COMMAND_TYPE_GET_DISCOVERT_DEVICE_ID] = m.HandleIgnoreCommand commandHandlers[COMMAND_TYPE_GRACEFUL_RECONNECT] = m.HandleIgnoreCommand commandHandlers[COMMAND_TYPE_GET_RECEIVER_STATUS] = m.HandleIgnoreCommand commandHandlers[COMMAND_TYPE_SEND_DEBUG_COMMAND] = m.HandleIgnoreCommand commandHandlers[COMMAND_TYPE_SET_AUTOPLAY_MODE] = m.HandleIgnoreCommand commandHandlers[COMMAND_TYPE_GET_PARTY_GAMES_MODE] = m.HandleIgnoreCommand commandHandlers[COMMAND_TYPE_GET_NOW_PLAYING] = m.HandleGetPlayingNow commandHandlers[COMMAND_TYPE_SESSION_ID] = m.HandleSessionId commandHandlers[COMMAND_TYPE_G_SESSION_ID] = m.HandleGSessionId commandHandlers[COMMAND_TYPE_LOUNGE_STATUS] = m.HandleLoungeStatus commandHandlers[COMMAND_TYPE_REMOTE_CONNECTED] = m.HandleRemoteConnected commandHandlers[COMMAND_TYPE_SET_VOLUME] = m.HandleSetVolume commandHandlers[COMMAND_TYPE_SET_PLAYLIST] = m.HandleSetPlaylist commandHandlers[COMMAND_TYPE_UPDATE_PLAYLIST] = m.HandleUpdatePlaylist commandHandlers[COMMAND_TYPE_PAUSE] = m.HandlePause commandHandlers[COMMAND_TYPE_PLAY] = m.HandlePlay commandHandlers[COMMAND_TYPE_PREVIOUS] = m.HandlePrevious commandHandlers[COMMAND_TYPE_NEXT] = m.HandleNext commandHandlers[COMMAND_TYPE_STOP_VIDEO] = m.HandleStopVideo commandHandlers[COMMAND_TYPE_SEEK_TO] = m.HandleSeekTo commandHandlers[COMMAND_TYPE_SET_PLAYBACK_SPEED] = m.HandleSetPlaybackSpeed commandHandlers[COMMAND_TYPE_GET_PLAYBACK_SPEED] = m.HandleGetPlaybackSpeed for each key in commandHandlers m[COMMAND_HANDLER_PREFIX + key] = commandHandlers[key] end for end function function Zx() as string ' Math.floor(2147483648 * Math.random()).toString(36) + Math.abs(Math.floor(2147483648 * Math.random()) ^ Date.now()).toString(36) rnd1% = rnd(2147483647) rnd2& = rnd(2147483647) m.dateTime.mark() now& = m.dateTime.AsSeconds() now& = now& * 1000 + m.dateTime.GetMilliseconds() rnd2xor& = (rnd2& or now&) and not (rnd2& and now&) return StrI(rnd1%, 36) + StrI(Abs(rnd2xor&), 36) end function function GetScreenId() as dynamic if not StringUtils.IsNullOrEmpty(m.screenId) return m.screenId end if request = HttpClient.Get("https://www.youtube.com/api/lounge/pairing/generate_screen_id") response = request.Await() if not response.IsSuccess() error = Tr(Locale.Dialogs.LoungeFailedToGetScreenId) + `\n${response.ErrorMessage()}` LogError(error) DialogUtils.ShowDialogEx({ message: error title: Tr(Locale.Dialogs.LoungeServiceSetupError) }) return invalid end if id = response.Text() if not IsString(id) error = Tr(Locale.Dialogs.LoungeFailedToGetScreenId) + `\n` = Tr(Locale.Dialogs.LoungeNotAValidScreenId).Replace("%1", `${id}`) LogError(error) DialogUtils.ShowDialogEx({ message: error title: Tr(Locale.Dialogs.LoungeServiceSetupError) }) return invalid end if m.screenId = id return id end function function GetToken() as dynamic if not StringUtils.IsNullOrEmpty(m.loungeToken) return m.loungeToken end if request = HttpClient.Post("https://www.youtube.com/api/lounge/pairing/get_lounge_token_batch", "") request.UrlEncodedForm({ "screen_ids": m.screenId }) response = request.Await() if not response.IsSuccess() error = Tr(Locale.Dialogs.LoungeFailedToGetLoungeToken) + `\n${response.ErrorMessage()}` LogError(error) DialogUtils.ShowDialogEx({ message: error title: Tr(Locale.Dialogs.LoungeServiceSetupError) }) return invalid end if json = response.Json() screens = json.screens if not IsArray(screens) or screens.Count() <> 1 error = Tr(Locale.Dialogs.LoungeFailedToGetLoungeToken) + `\n` + Tr(Locale.Dialogs.LoungeInvalidResponse).Replace("%1", `${json}`) LogError(error) DialogUtils.ShowDialogEx({ message: error title: Tr(Locale.Dialogs.LoungeServiceSetupError) }) return invalid end if ' payload contains more information, but we only need the token m.loungeToken = screens[0].loungeToken return m.loungeToken end function function GetSessionData() as boolean queryParams = { "device": "LOUNGE_SCREEN" "theme": "cl" "capabilities": "vsp" "mdxVersion": "2" "VER": "8" "v": "2" "t": "1" "app": "lb-v4" "RID": "1337" } queryParams["name"] = m.screenName queryParams["id"] = m.deviceId queryParams["loungeIdToken"] = m.loungeToken queryParams["zx"] = m.Zx() request = HttpClient.Post("https://www.youtube.com/api/lounge/bc/bind", "") request.QueryParams(queryParams) request.UrlEncodedForm({ "count": 0 }) response = request.Await() if not response.IsSuccess() error = Tr(Locale.Dialogs.LoungeFailedToGetSessionData) + `\n${response.ErrorMessage()}` LogError(error) DialogUtils.ShowDialogEx({ message: error title: Tr(Locale.Dialogs.LoungeServiceSetupError) }) return false end if text = response.Text() ' First line should be a number, which is the length of the JSON payload text = text.Mid(text.instr(`\n`) + 1) messagesToSend = [] commands = StringUtils.ParseJsonSafe(text) m.ProcessCommands(commands, messagesToSend) m.SendMessages(messagesToSend) return true end function function FetchCommandsRpc(messagePort as object) as void ' Note we're not using https, not supported by LongPoller baseUrl = "http://www.youtube.com/api/lounge/bc/bind" queryParams = { "device": "LOUNGE_SCREEN" "theme": "cl" "capabilities": "vsp" "mdxVersion": "2" "VER": "8" "v": "2" "t": "1" "app": "lb-v4" "RID": "rpc" "CI": "0" } queryParams["name"] = m.screenName queryParams["id"] = m.deviceId queryParams["loungeIdToken"] = m.loungeToken queryParams["SID"] = m.sessionId queryParams["gsessionid"] = m.gSessionId queryParams["zx"] = m.Zx() url = UrlUtils.BuildUrl(baseUrl, invalid, queryParams) if m.poller = invalid m.poller = new LongPoller() end if m.poller.SendRequest(url, messagePort) end function function Poll(socketMessages as object, messagesToSend as object) as boolean chunks = m.poller.Poll(socketMessages) if IsArray(chunks) for each chunk in chunks m.ProcessChunk(chunk, messagesToSend) end for end if return m.poller.IsOpen() end function function ProcessChunk(chunk as string, messagesToSend as object) as void chunk = m.ConsumeNewLines(chunk) newLineIndex = chunk.InStr(`\n`) while newLineIndex > 0 payloadSize = chunk.Left(newLineIndex).trim() payloadSize = payloadSize.ToInt() payload = chunk.Mid(newLineIndex + 1, payloadSize) commands = StringUtils.ParseJsonSafe(payload) m.ProcessCommands(commands, messagesToSend) chunk = chunk.Mid(newLineIndex + payloadSize + 1) newLineIndex = chunk.InStr(`\n`) end while end function function ConsumeNewLines(chunk as string) as string CR = `\r` LF = `\n` startIndex = 0 while true char = chunk.Mid(startIndex, 1) if char = CR or char = LF startIndex += 1 else exit while end if end while if startIndex > 0 return chunk.Mid(startIndex) end if return chunk end function function ProcessCommands(commands as object, messagesToSend as object) as void if not IsArray(commands) return end if for each command in commands if not IsArray(command) or command.Count() <> 2 continue for end if commandIndex = command[0] commandData = command[1] commandType = commandData[0] if commandIndex <= m.lastProcessedCommandIndex and commandType <> COMMAND_TYPE_G_SESSION_ID and commandType <> COMMAND_TYPE_SESSION_ID LogDebug("Skipping command already processed:", command) continue for end if m.lastProcessedCommandIndex = commandIndex commandArgs = invalid if commandData.Count() > 1 commandArgs = commandData[1] end if handler = COMMAND_HANDLER_PREFIX + commandType if m.DoesExist(handler) LogInfo(commandIndex, commandType) m[handler](commandIndex, commandType, commandArgs, messagesToSend) else fingerprint = "Unknown command type:" + ToString(commandType) message = fingerprint + " command:" + ToString(command) LogWarnNoTelemetry(message) LogTelemetryEvent({ level: "warning" message: message fingerprint: fingerprint }) #if DEBUG throw message #end if end if end for end function function HandleIgnoreCommand(_commandId as integer, commandType as string, commandArgs as object, _messagesToSend as object) LogDebug("Ignoring command:", commandType, "args:", commandArgs) end function function HandleSessionId(_commandId as integer, _commandType as string, commandArgs as object, _messagesToSend as object) m.sessionId = commandArgs LogInfo("sessionId:", m.sessionId) end function function HandleGSessionId(_commandId as integer, _commandType as string, commandArgs as object, _messagesToSend as object) m.gSessionId = commandArgs LogInfo("gSessionId:", m.gSessionId) end function function HandleGetPlayingNow(commandId as integer, _commandType as string, _commandArgs as object, messagesToSend as object) as void player = m.videoQueueNode.player message = LoungeMessages.OnHasPreviousNextChanged(m.videoQueueNode.hasPrevious, m.videoQueueNode.hasNext) message["commandId"] = commandId messagesToSend.Push(message) message = LoungeMessages.NowPlaying(player) message["commandId"] = commandId messagesToSend.Push(message) message = LoungeMessages.OnStateChange(player) message["commandId"] = commandId messagesToSend.Push(message) end function function HandleLoungeStatus(_commandId as integer, commandType as string, commandArgs as object, _messagesToSend as object) as void LogInfo(commandType, FormatJson(commandArgs)) devices = commandArgs["devices"] if not IsString(devices) return end if devices = StringUtils.ParseJsonSafe(devices) if devices = invalid return end if for each device in devices if m.devices.DoesExist(device.id) device.lastConnectNotificationTime = m.devices[device.id].lastConnectNotificationTime end if m.devices[device.id] = device end for end function function HandleRemoteConnected(_commandId as integer, commandType as string, commandArgs as object, _messagesToSend as object) as void LogInfo(commandType, FormatJson(commandArgs)) newDevice = commandArgs id = newDevice.id if StringUtils.IsNullOrEmpty(id) return end if if m.devices.DoesExist(id) device = m.devices[id] device.Append(newDevice) newDevice = device end if m.devices[id] = newDevice nowSeconds = TimeUtils.Now().AsSeconds() lastConnectNotificationInterval = nowSeconds - ValidInt(newDevice.lastConnectNotificationTime) if lastConnectNotificationInterval < CONNECT_NOTIFICATION_MIN_INTERVAL_SECONDS LogInfo("Skipping connect notification. Last connect notification interval:", lastConnectNotificationInterval, "seconds") return end if newDevice.lastConnectNotificationTime = nowSeconds userName = newDevice.user deviceName = newDevice.name userAvatarUri = newDevice.userAvatarUri LoungeDeviceConnected.ShowNotifcation(m.notificationsNode, deviceName, userName, userAvatarUri) end function function HandleSetVolume(_commandId as integer, _commandType as string, commandArgs as object, _messagesToSend as object) as void player = m.videoQueueNode.player if player = invalid return end if volume = Val(commandArgs["volume"]) isMute = volume = 0 LogInfo("Setting volume is not supported:", volume, "but can mute:", isMute) ' TODO:P2 volume should be set to the lounge state. ' Also the state needs to be separate from the player, since SponsorBlock can mute sections. player.mute = isMute end function function HandleSetPlaylist(_commandId as integer, commandType as string, commandArgs as object, _messagesToSend as object) LogDebug(commandType, commandArgs) ' TODO:P2 ctt: credentialTransferToken ' We receive credentialTransferTokens here that can be attached to innertube requests. ' The tokens belong to the user logged in on the mobile app. ' These allow us to watch age restricted videos and private videos. ' They are scoped by video. ' Right now we just store only the last ctt, which works ok if we're casting one video at a time. ' But if the age restrcited video is added to the queue, we don't capture a token and playing the video would fail. ' These tokens need to be managed better, based on the video they belong to. if not StringUtils.IsNullOrEmpty(commandArgs[FIELD_CTT]) and not StringUtils.IsNullOrEmpty(commandArgs[FIELD_VIDEO_ID]) m.loungeServiceNode.credentialTransferToken = { "token": commandArgs[FIELD_CTT] "videoId": commandArgs[FIELD_VIDEO_ID] } end if LoungeVideoQueue.SetPlaylist(m.videoQueueNode, m.invService, commandArgs) end function function HandleUpdatePlaylist(_commandId as integer, commandType as string, commandArgs as object, _messagesToSend as object) LogDebug(commandType, commandArgs) LoungeVideoQueue.UpdatePlaylist(m.videoQueueNode, m.invService, commandArgs) end function function HandlePause(_commandId as integer, _commandType as string, _commandArgs as object, _messagesToSend as object) m.videoQueueNode.pause = true end function function HandlePlay(_commandId as integer, _commandType as string, _commandArgs as object, _messagesToSend as object) m.videoQueueNode.play = true end function function HandlePrevious(_commandId as integer, _commandType as string, _commandArgs as object, _messagesToSend as object) m.videoQueueNode.previous = true end function function HandleNext(_commandId as integer, _commandType as string, _commandArgs as object, _messagesToSend as object) m.videoQueueNode.next = true end function function HandleStopVideo(_commandId as integer, _commandType as string, _commandArgs as object, _messagesToSend as object) m.videoQueueNode.closePlayer = true end function function HandleSeekTo(_commandId as integer, _commandType as string, commandArgs as object, _messagesToSend as object) as void player = m.videoQueueNode.player if player = invalid return end if seekTo = Val(commandArgs[FIELD_NEW_TIME]) LogInfo("Seeking to:", seekTo) player.seek = seekTo end function function HandleSetPlaybackSpeed(_commandId as integer, _commandType as string, commandArgs as object, messagesToSend as object) as void player = m.videoQueueNode.player if player = invalid return end if if not player.hasField("playbackSpeed") LogWarn("Video node does not support 'playbackSpeed' field on this device, ignoring setPlaybackSpeed command") return end if speed = Val(commandArgs[FIELD_PLAYBACK_SPEED]) if speed <= 0 speed = 1.0 end if content = player.content if content <> invalid and content.live = true return end if currentSpeed = ValidFloat(player.playbackSpeed) if Abs(currentSpeed - speed) < 0.001 LogDebug("Playback speed is already close to the requested speed. current:", currentSpeed, "requested:", speed) return end if LogInfo("Setting playback speed to:", speed) ' Usually playbackSpeed doesn't get applied without pausing and resuming. didPause = false state = player.state if state = "playing" player.control = "pause" didPause = true end if player.playbackSpeed = speed if didPause player.control = "resume" end if messagesToSend.Push(LoungeMessages.OnPlaybackSpeedChanged(speed, true)) end function function HandleGetPlaybackSpeed(commandId as integer, _commandType as string, _commandArgs as object, messagesToSend as object) as void player = m.videoQueueNode.player if player = invalid return end if if not player.hasField("playbackSpeed") LogWarn("Video node does not support 'playbackSpeed' field on this device, ignoring getPlaybackSpeed command") return end if speed = ValidFloat(player.playbackSpeed) if speed <= 0 speed = 1.0 end if message = LoungeMessages.OnPlaybackSpeedChanged(speed) message["commandId"] = commandId messagesToSend.Push(message) end function function SendMessages(messages as object, loungeData = invalid as object) as void if messages.Count() = 0 return end if queryParams = { "device": "LOUNGE_SCREEN" "theme": "cl" "capabilities": "vsp" "mdxVersion": "2" "VER": "8" "v": "2" "t": "1" "app": "lb-v4" "RID": "1337" } if loungeData = invalid loungeData = m end if queryParams["name"] = loungeData.screenName queryParams["id"] = loungeData.deviceId queryParams["loungeIdToken"] = loungeData.loungeToken queryParams["SID"] = loungeData.sessionId queryParams["gsessionid"] = loungeData.gSessionId queryParams["zx"] = m.Zx() if messages[0].commandId <> invalid queryParams["AID"] = messages[0].commandId end if postData = [ { key: "count", value: messages.Count() } { key: "ofs", value: loungeData.ofs } ] loungeData.ofs += messages.Count() for i = 0 to messages.Count() - 1 message = messages[i] keyPrefix = `req${i}_` postData.Push({ key: `${keyPrefix}${FIELD_COMMAND_NAME}`, value: message.commandType }) commandArgs = message.commandArgs if commandArgs <> invalid for each key in commandArgs postData.Push({ key: `${keyPrefix}${key}`, value: commandArgs[key] }) end for end if end for LogDebug("Sending messages:", postData) request = HttpClient.Post("https://www.youtube.com/api/lounge/bc/bind", "") request.QueryParams(queryParams) request.UrlEncodedForm(postData) request.Send() ' we do not need to wait for requests to finish, we check on them later ' to avoid blocking the thread. ' This is specific to sent lounge messages, since we're not waiting for a response. requestId = request.urlTransfer.GetIdentity().ToStr() m.outgoingMessagesRequests[requestId] = request end function function ProcessPendingOutgoingMessages() as object results = [] for each key in m.outgoingMessagesRequests request = m.outgoingMessagesRequests[key] if request.IsCompleted() response = request.Await() status = response.StatusCode() error = response.ErrorMessage() results.Push({ "status": status "error": error }) if not response.IsSuccess() isGone = response.StatusCode() = 410 unknownSid = response.StatusCode() = 400 and response.ErrorMessage().InStr("Unknown SID") <> -1 ' TODO:P2 handle expired lounge id token ' expiredLoungeIdToken = response.StatusCode() = 401 and response.ErrorMessage().InStr("Expired lounge id token") <> -1 isKnownError = isGone or unknownSid if isKnownError LogErrorNoTelemetry(response.ErrorMessage()) else LogError(response.ErrorMessage()) end if end if m.outgoingMessagesRequests.Delete(key) end if end for return results end function function SaveCurrentLoungeData() as void loungeData = { "__version": 1 } fields = ["screenName", "deviceId", "loungeToken", "sessionId", "gSessionId"] for each field in fields if StringUtils.IsNullOrEmpty(m[field]) LogWarn("Lounge data will not be saved. missing field:", field) return end if loungeData[field] = m[field] end for loungeData = FormatJson(loungeData) RegistryUtils.Write(RegistryUtils.LAST_LOUNGE_DATA, loungeData) end function function DisconnectFromLastLounge() as void loungeData = RegistryUtils.Read(RegistryUtils.LAST_LOUNGE_DATA) if loungeData = invalid return end if RegistryUtils.Delete(RegistryUtils.LAST_LOUNGE_DATA) loungeData = StringUtils.ParseJsonSafe(loungeData) if not IsAssociativeArray(loungeData) LogWarn("Can't disconnect from lounge. Invalid data:", loungeData) return end if if ValidInt(loungeData.__version) <> 1 LogWarn("Can't disconnect from lounge. Unknown version:", loungeData.__version) return end if fields = ["screenName", "deviceId", "loungeToken", "sessionId", "gSessionId"] for each field in fields if StringUtils.IsNullOrEmpty(loungeData[field]) LogWarn("Can't disconnect from lounge. Missing field:", field) return end if end for messages = [{ "commandType": COMMAND_LOUNGE_SCREEN_DISCONNECTED }] LogInfo("Disconnecting from last lounge") ' Every time we send messages, we need to increment the ofs. ' This is so that the server recognizes old messages and doesn't process them. ' But we're currently disconnecting from a previous session, and since ' we didn't keep track of the ofs, so we'll just set it to a large number. ' This is the last message we'll be sending to this lounge. loungeData.ofs = 99999999 m.SendMessages(messages, loungeData) end function end class