Browse Source

make sure to sink and stop all the media... Likely still need to

stop the media in a few other error branches as well..
main
John-Mark Gurney 3 years ago
parent
commit
2669182ccf
3 changed files with 39 additions and 1 deletions
  1. +1
    -0
      dist/audiotest.html
  2. +2
    -1
      server/server.py
  3. +36
    -0
      src/index.js

+ 1
- 0
dist/audiotest.html View File

@@ -13,6 +13,7 @@
<body>
<p>
<div id="constatus">undefined</div>
<div><input type="button" value="Stop" onclick="stopPage();"></div>
<audio id="audioSink" autoplay></audio>
</p>
<script type="text/javascript" src="jamming.js"></script>


+ 2
- 1
server/server.py View File

@@ -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")


+ 36
- 0
src/index.js View File

@@ -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()



Loading…
Cancel
Save