From 2669182ccf9c281c005691079339637329a3784f Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Fri, 24 Apr 2020 17:32:01 -0700 Subject: [PATCH] make sure to sink and stop all the media... Likely still need to stop the media in a few other error branches as well.. --- dist/audiotest.html | 1 + server/server.py | 3 ++- src/index.js | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/dist/audiotest.html b/dist/audiotest.html index a9e7c2d..b4b75c8 100644 --- a/dist/audiotest.html +++ b/dist/audiotest.html @@ -13,6 +13,7 @@

undefined
+

diff --git a/server/server.py b/server/server.py index b61cb83..a8fc15a 100644 --- a/server/server.py +++ b/server/server.py @@ -6,7 +6,7 @@ import uuid from aiohttp import web from aiortc import RTCPeerConnection, RTCIceCandidate, RTCSessionDescription -from aiortc.contrib.media import MediaPlayer +from aiortc.contrib.media import MediaPlayer, MediaBlackhole logger = logging.getLogger('pc') logger.setLevel(logging.INFO) @@ -109,6 +109,7 @@ async def ws_handler(request): if track.kind == "audio": pc.addTrack(mixer.audio) + MediaBlackhole().addTrack(track) #mixer.addTrack(track) @track.on("ended") diff --git a/src/index.js b/src/index.js index b91df36..db5e870 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,23 @@ import jamming from './jamming'; import { v4 as uuidv4 } from 'uuid'; import WebSocketAsPromised from 'websocket-as-promised'; +/* Deal with MediaStream not having a stop anymore */ +/* from: https://stackoverflow.com/a/11646945 */ +var MediaStream = window.MediaStream; + +if (typeof MediaStream === 'undefined' && typeof webkitMediaStream !== 'undefined') { + MediaStream = webkitMediaStream; +} + +/*global MediaStream:true */ +if (typeof MediaStream !== 'undefined' && !('stop' in MediaStream.prototype)) { + MediaStream.prototype.stop = function() { + this.getTracks().forEach(function(track) { + track.stop(); + }); + }; +} + function sendServer(obj) { let lclobj = Object.assign({}, obj); lclobj.uuid = uuid; @@ -62,6 +79,7 @@ async function runPage() { } }); await wsp.open(); + constatus.textContent = 'connection to server opened'; function sendServer(obj) { var lclobj = Object.assign({}, obj); @@ -95,6 +113,7 @@ async function runPage() { }; pc.ontrack = (event) => { audioSink.srcObject = event.streams[0]; + constatus.textContent = 'playing audio'; }; pc.addStream(stream); @@ -111,6 +130,23 @@ async function runPage() { var ld = pc.localDescription; sendServer({ sdp: ld.sdp, type: ld.type }); + + async function stopEverything() { + constatus.textContent = 'a'; + stream.stop(); + constatus.textContent = 'b'; + var v = wsp.close(); + constatus.textContent = 'c'; + pc.close(); + constatus.textContent = 'd'; + await v; + constatus.textContent = 'Stopped'; + }; + + global.stopPage = () => { + constatus.textContent = 'pa'; + stopEverything(); + } } runPage()