From 30f3886487ec6960bf647ad547638f093a85a9cf Mon Sep 17 00:00:00 2001 From: Marks Polakovs Date: Tue, 14 Apr 2020 17:29:21 +0200 Subject: [PATCH] send wsid to stateserver --- src/broadcast/rtc_streamer.ts | 1 + src/broadcast/state.ts | 54 +++++++++++++++++++++++++++++------ src/navbar/index.tsx | 2 +- stateserver.py | 8 ++++++ 4 files changed, 55 insertions(+), 10 deletions(-) diff --git a/src/broadcast/rtc_streamer.ts b/src/broadcast/rtc_streamer.ts index b79bb12..1227c48 100644 --- a/src/broadcast/rtc_streamer.ts +++ b/src/broadcast/rtc_streamer.ts @@ -142,6 +142,7 @@ export class WebRTCStreamer extends Streamer { switch (data.kind) { case "HELLO": console.log("WS HELLO, our client ID is " + data.connectionId); + this.dispatch(BroadcastState.setWsID(data.connectionId)); if (this.state !== "HELLO") { this.ws!.close(); } diff --git a/src/broadcast/state.ts b/src/broadcast/state.ts index 52cd98b..4f2b5d8 100644 --- a/src/broadcast/state.ts +++ b/src/broadcast/state.ts @@ -17,6 +17,7 @@ export type BroadcastStageEnum = interface BroadcastState { stage: BroadcastStageEnum; connID: number | null; + wsID: string | null; sourceID: number; autoNewsBeginning: boolean; autoNewsMiddle: boolean; @@ -37,6 +38,7 @@ const broadcastState = createSlice({ initialState: { stage: "NOT_REGISTERED", connID: null, + wsID: null, sourceID: 5, autoNewsBeginning: true, autoNewsMiddle: true, @@ -66,6 +68,9 @@ const broadcastState = createSlice({ state.stage = "NOT_REGISTERED"; } }, + setWsID(state, action: PayloadAction) { + state.wsID = action.payload; + }, setConnectionState(state, action: PayloadAction) { state.connectionState = action.payload; }, @@ -77,6 +82,12 @@ const broadcastState = createSlice({ export default broadcastState.reducer; +export const { + toggleTracklisting, + setTracklisting, + setWsID +} = broadcastState.actions; + export interface TrackListItem { audiologid: number; } @@ -89,7 +100,8 @@ export const changeBroadcastSetting = ( dispatch(changeTimeslot()); }; -export const registerTimeslot = (): AppThunk => async (dispatch, getState) => { +// intentionally renamed to break all existing callsites +export const registerForShow = (): AppThunk => async (dispatch, getState) => { if (!getState().session.userCanBroadcast) { dispatch( NavbarState.showAlert({ @@ -104,14 +116,22 @@ export const registerTimeslot = (): AppThunk => async (dispatch, getState) => { var state = getState().session; const memberid = state.currentUser?.memberid; const timeslotid = state.currentTimeslot?.timeslot_id; + const wsId = getState().broadcast.wsID; + if (wsId === null) { + console.warn("Tried to register for broadcast with no wsID"); + } console.log("Attempting to Register for Broadcast."); var sourceid = getState().broadcast.sourceID; try { - var connID = await sendBroadcastRegister(timeslotid, memberid, sourceid); + var connID = await sendBroadcastRegister( + timeslotid, + memberid, + sourceid, + wsId || undefined + ); console.log(connID); if (connID !== undefined) { dispatch(broadcastState.actions.setConnID(connID["connid"])); - dispatch(startStreaming()); } } catch (e) { if (e instanceof ApiException) { @@ -173,13 +193,18 @@ export const changeTimeslot = (): AppThunk => async (dispatch, getState) => { export function sendBroadcastRegister( timeslotid: number | undefined, memberid: number | undefined, - sourceid: number + sourceid: number, + wsID?: string ): Promise { - return broadcastApiRequest("/registerTimeslot", "POST", { + const payload = { memberid: memberid, timeslotid: timeslotid, sourceid: sourceid - }); + } as any; + if (typeof wsID === "string") { + payload["wsid"] = wsID; + } + return broadcastApiRequest("/registerTimeslot", "POST", payload); } export function sendBroadcastCancel( connid: number | null @@ -205,8 +230,6 @@ export function sendBroadcastChange( }); } -export const { toggleTracklisting, setTracklisting } = broadcastState.actions; - function shouldTracklist( optionValue: "always" | "while_live" | "never", stateValue: boolean @@ -266,7 +289,17 @@ export function sendTracklistStart(trackid: number): Promise { }); } -export const startStreaming = (): AppThunk => async (dispatch, getState) => { +export const goOnAir = (): AppThunk => async (dispatch, getState) => { + if (!getState().session.userCanBroadcast) { + dispatch( + NavbarState.showAlert({ + color: "warning", + content: "You are not WebStudio Trained and cannot go live.", + closure: 7000 + }) + ); + return; + } console.log("starting streamer."); streamer = new WebRTCStreamer(MixerState.destination.stream, dispatch); streamer.addConnectionStateListener(state => { @@ -274,6 +307,9 @@ export const startStreaming = (): AppThunk => async (dispatch, getState) => { if (state === "CONNECTION_LOST") { // un-register if we drop, let the user manually reconnect dispatch(broadcastState.actions.setConnID(null)); + } else if (state === "CONNECTED") { + // okay, we've connected + dispatch(registerForShow()); } }); await streamer.start(); diff --git a/src/navbar/index.tsx b/src/navbar/index.tsx index 07b2af1..a0a08c0 100644 --- a/src/navbar/index.tsx +++ b/src/navbar/index.tsx @@ -73,7 +73,7 @@ export function NavBar() { onClick={() => { switch (broadcastState.stage) { case "NOT_REGISTERED": - dispatch(BroadcastState.registerTimeslot()); + dispatch(BroadcastState.goOnAir()); break; case "REGISTERED": dispatch(BroadcastState.cancelTimeslot()); diff --git a/stateserver.py b/stateserver.py index baa0b1d..5423d0c 100755 --- a/stateserver.py +++ b/stateserver.py @@ -285,6 +285,14 @@ def post_registerCheck() -> Any: 'autoNewsEnd': True, 'wsid': None } + if "wsid" in content: + connection["wsid"] = content["wsid"] + if start_time > now_time + datetime.timedelta(minutes=2): + # they're late, bring them live now + print("({}, {}) late, bringing on air now".format(connection["connid"], connection["wsid"])) + do_ws_srv_telnet(connection["wsid"]) + subprocess.Popen(['sel', '5']) + connections.append(connection) print(connections)