Skip to content
This repository has been archived by the owner on Aug 25, 2021. It is now read-only.

Commit

Permalink
WIP: Implement signaling (#16)
Browse files Browse the repository at this point in the history
- add RpcConnection trait for Web Client connection
- add config for rpc connection
- add media track and peer
- impl PeerStateMachine for Peer
- impl signaling protocol
- impl signaling and handle Command from Web Client
- impl participant service for Room for store members with its rpc connection
  • Loading branch information
Kirguir authored May 7, 2019
1 parent b5742fb commit bc48960
Show file tree
Hide file tree
Showing 23 changed files with 2,095 additions and 326 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ toml = "0.4"
[dev-dependencies]
serial_test = "0.2"
serial_test_derive = "0.2"
tokio = "0.1"
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ Medea
Medea media server

__DEVELOPMENT IN PROGRESS__

{"MakeSdpOffer":{"peer":0,"sdp_offer":"caller_offer"}}
161 changes: 161 additions & 0 deletions signaling_test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<!DOCTYPE html>
<html>
<head>
<title>Chat</title>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>

<script type="text/javascript">

var socket;
var pcs = {};

function connectSocket(caller) {
let role = caller ? "1/caller_credentials" : "2/responder_credentials";
socket = new WebSocket("ws://localhost:8080/ws/1/" + role);
socket.onmessage = handleSocketMessage;

window.setInterval(function () {
socket.send("{\"ping\":1}");
}, 8000);
};

function handleSocketMessage(message) {

var msg = JSON.parse(message.data);

switch (msg.event) {
case 'PeerCreated':
handlePeerCreated(msg.data);
break;
case 'SdpAnswerMade':
handleSdpAnswerMade(msg.data);
break;
case 'IceCandidateDiscovered':
handleIceCandidateDiscovered(msg.data);
break;
}
}

async function handlePeerCreated(msg) {
var sendAudio = false;
var sendVideo = false;
var recvAudio = false;
var recvVideo = false;

msg.tracks.forEach((track) => {

if (track.media_type.Audio != undefined) {
if (track.direction.Send != undefined) {
sendAudio = true;
}

if (track.direction.Recv != undefined) {
recvAudio = true;
}

}
if (track.media_type.Video != undefined) {
if (track.direction.Send != undefined) {
sendVideo = true;
}

if (track.direction.Recv != undefined) {
recvVideo = true;
}
}
});

pcs[msg.peer_id] = new RTCPeerConnection();

var pc = pcs[msg.peer_id];
pc.ontrack = function (e) {
console.log("ontrack");
partnervid.srcObject = e.streams[0];
};

if (sendVideo || sendAudio) {
const stream = await navigator.mediaDevices.getUserMedia({
audio: sendAudio,
video: sendVideo
});

yourvid.srcObject = stream;

stream.getTracks().forEach((track) => {
pc.addTrack(track, stream);
});
}


var mediaConstraints = {
'mandatory': {
'OfferToReceiveAudio': recvAudio,
'OfferToReceiveVideo': recvVideo
}
};

if (msg.sdp_offer != undefined && msg.sdp_offer != null) {
pc.setRemoteDescription(new RTCSessionDescription(JSON.parse(msg.sdp_offer)));
var answer = await pc.createAnswer(mediaConstraints);
pc.setLocalDescription(answer);

socket.send(JSON.stringify({
command: "MakeSdpAnswer",
data: {
peer_id: msg.peer_id,
sdp_answer: JSON.stringify(answer)
}
}));
} else {
var offer = await pc.createOffer(mediaConstraints);
pc.setLocalDescription(offer);
socket.send(JSON.stringify({
command: "MakeSdpOffer",
data: {
peer_id: msg.peer_id,
sdp_offer: JSON.stringify(offer)
}
}));
}


pc.onicecandidate = function (e) {

if (e.candidate) {
socket.send(JSON.stringify({
command: "SetIceCandidate",
data: {
peer_id: msg.peer_id,
candidate: JSON.stringify(e.candidate)
}
}))
}

};
}

async function handleSdpAnswerMade(msg) {
var pc = pcs[msg.peer_id];
pc.setRemoteDescription(new RTCSessionDescription(JSON.parse(msg.sdp_answer)))
}

async function handleIceCandidateDiscovered(msg) {
var pc = pcs[msg.peer_id];
pc.addIceCandidate(JSON.parse(msg.candidate));
}

</script>
</head>
<body>
<div>
<div>
<div>
<video id="yourvid" muted autoplay width="100%" height="100%"></video>
</div>
<div>
<video id="partnervid" autoplay width="100%" height="100%"></video>
</div>
</div>
</div>
</body>
</html>
7 changes: 3 additions & 4 deletions src/api/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Implementation of Client API.
pub mod room;
pub mod server;
pub mod session;
mod session;

pub use self::{room::*, server::*, session::*};
pub mod rpc_connection;
pub mod server;
180 changes: 0 additions & 180 deletions src/api/client/room.rs

This file was deleted.

Loading

0 comments on commit bc48960

Please sign in to comment.