diff --git a/package.json b/package.json index af2672c..0b19751 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "camelcase": "^5.2.0", "case-sensitive-paths-webpack-plugin": "2.2.0", "css-loader": "2.1.1", + "date-fns": "^2.12.0", "dotenv": "6.2.0", "dotenv-expand": "5.1.0", "eslint": "^6.1.0", diff --git a/src/broadcast/rtc_streamer.ts b/src/broadcast/rtc_streamer.ts index 0edf918..2a4f581 100644 --- a/src/broadcast/rtc_streamer.ts +++ b/src/broadcast/rtc_streamer.ts @@ -1,4 +1,7 @@ import SdpTransform from "sdp-transform"; +import * as DateFns from "date-fns"; + +import * as MixerState from "../mixer/state"; import { Streamer, @@ -15,6 +18,9 @@ export class WebRTCStreamer extends Streamer { state: StreamerState = "HELLO"; isActive = false; + newsInTimeout?: number; + newsOutTimeout?: number; + constructor(stream: MediaStream) { super(); this.stream = stream; @@ -50,6 +56,9 @@ export class WebRTCStreamer extends Streamer { } console.log("ICE Connection state change: " + this.pc.iceConnectionState); this.onStateChange(this.mapStateToConnectionState()); + if (this.mapStateToConnectionState() === "CONNECTED") { + this.doTheNews(); + } }; this.stream.getAudioTracks().forEach(track => this.pc!.addTrack(track)); @@ -78,6 +87,39 @@ export class WebRTCStreamer extends Streamer { } } + doTheNews() { + window.clearTimeout(this.newsInTimeout); + window.clearTimeout(this.newsOutTimeout); + const now = new Date(); + if ( + now.getMinutes() < 59 || + (now.getMinutes() === 59 && now.getSeconds() < 45) + ) { + const newsTime = DateFns.set(now, { + minutes: 59, + seconds: 45 + }); + const delta = newsTime.valueOf() - now.valueOf(); + this.newsInTimeout = window.setTimeout(async () => { + await MixerState.playNewsIntro(); + }, delta); + } + if ( + now.getMinutes() < 1 || + now.getMinutes() >= 2 || + (now.getMinutes() === 1 && now.getSeconds() < 50) + ) { + const newsEndTime = DateFns.set(now, { + minutes: 0, + seconds: 55 + }); + const delta = newsEndTime.valueOf() - now.valueOf(); + this.newsOutTimeout = window.setTimeout(async () => { + await MixerState.playNewsEnd(); + }, delta); + } + } + async onMessage(evt: MessageEvent) { const data = JSON.parse(evt.data); switch (data.kind) { @@ -136,9 +178,11 @@ export class WebRTCStreamer extends Streamer { break; case "ACTIVATED": this.isActive = true; + this.onStateChange("LIVE"); break; case "DEACTIVATED": this.isActive = false; + this.onStateChange(this.mapStateToConnectionState()); break; case "DIED": // oo-er diff --git a/yarn.lock b/yarn.lock index 39700b4..fcda2b0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3530,6 +3530,11 @@ data-urls@^1.0.0, data-urls@^1.1.0: whatwg-mimetype "^2.2.0" whatwg-url "^7.0.0" +date-fns@^2.12.0: + version "2.12.0" + resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.12.0.tgz#01754c8a2f3368fc1119cf4625c3dad8c1845ee6" + integrity sha512-qJgn99xxKnFgB1qL4jpxU7Q2t0LOn1p8KMIveef3UZD7kqjT3tpFNNdXJelEHhE+rUgffriXriw/sOSU+cS1Hw== + debug@2.6.9, debug@^2.2.0, debug@^2.3.3, debug@^2.6.0, debug@^2.6.9: version "2.6.9" resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"