Skip to content

Commit

Permalink
Merge pull request #14 from stefanalund/media-prefs
Browse files Browse the repository at this point in the history
Saves audio/video preferences
  • Loading branch information
MorganLindqvist committed Oct 14, 2014
2 parents 8bb5ebc + 856c512 commit 613789b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
4 changes: 4 additions & 0 deletions server/client/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ video {
-moz-box-shadow: 0px 5px 5px 0px rgba(0,0,0,0.4);
box-shadow: 0px 5px 5px 0px rgba(0,0,0,0.4);
}
#audio-only-container {
visibility: hidden;
color: red;
}
#share-container {
visibility: hidden;
padding-top: 5px;
Expand Down
59 changes: 54 additions & 5 deletions server/client/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ if (window.mozRTCPeerConnection) {

var remoteView;
var callButton;
var audioCheckBox;
var videoCheckBox;
var audioOnlyView;

var signalingChannel;
var pc;
Expand All @@ -22,12 +25,20 @@ window.onload = function () {
callButton = document.getElementById("call_but");
var selfView = document.getElementById("self_view");
var joinButton = document.getElementById("join_but");
var audioCheckBox = document.getElementById("audio_cb");
var videoCheckBox = document.getElementById("video_cb");
audioCheckBox = document.getElementById("audio_cb");
videoCheckBox = document.getElementById("video_cb");
audioOnlyView = document.getElementById("audio-only-container");
var shareView = document.getElementById("share-container");

updateMediaPrefs();

joinButton.disabled = !navigator.webkitGetUserMedia;
joinButton.onclick = function (evt) {
if (!saveMediaPrefs()) {
alert("Choose at least audio or video");
return;
}

audioCheckBox.disabled = videoCheckBox.disabled = joinButton.disabled = true;

// get a local stream
Expand All @@ -39,7 +50,10 @@ window.onload = function () {
localStream = stream;

joinButton.disabled = true;
selfView.style.visibility = "visible";
if (videoCheckBox.checked)
selfView.style.visibility = "visible";
else
audioOnlyView.style.visibility = "visible";

var sessionId = document.getElementById("session_txt").value;
signalingChannel = new SignalingChannel(sessionId);
Expand All @@ -48,7 +62,6 @@ window.onload = function () {
var link = document.getElementById("share_link");
var maybeAddHash = window.location.href.indexOf('#') !== -1 ? "" : ("#" + sessionId);
link.href = link.text = window.location.href + maybeAddHash;
window.location.hash = sessionId;
shareView.style.visibility = "visible";

callButton.onclick = function () {
Expand Down Expand Up @@ -89,6 +102,39 @@ window.onload = function () {
}
}

function saveMediaPrefs() {
var prefs;
if (audioCheckBox.checked && videoCheckBox.checked)
prefs = "both";
else if (audioCheckBox.checked)
prefs = "audio";
else if (videoCheckBox.checked)
prefs = "video";
else
return false;
localStorage.setItem("media-prefs", prefs);
return true;
}

function updateMediaPrefs() {
var mediaPrefs = localStorage.getItem("media-prefs");
switch (mediaPrefs) {
case "audio":
audioCheckBox.checked = true;
videoCheckBox.checked = false;
break;
case "video":
audioCheckBox.checked = false;
videoCheckBox.checked = true;
break;
case "both":
audioCheckBox.checked = videoCheckBox.checked = true;
break;
default:
break;
}
}

This comment has been minimized.

Copy link
@adam-be

adam-be Oct 16, 2014

Contributor

If we saved one key for each media type, we could get rid of pretty much all the logic in the save and update functions.

This comment has been minimized.

Copy link
@stefanalund

stefanalund Oct 16, 2014

Contributor

Indeed: #16

// handle signaling messages received from the other peer
function handleMessage(evt) {
if (!pc)
Expand Down Expand Up @@ -127,7 +173,10 @@ function start(isInitiator) {
// once the remote stream arrives, show it in the remote video element
pc.onaddstream = function (evt) {
remoteView.src = URL.createObjectURL(evt.stream);
remoteView.style.visibility = "visible";
if (videoCheckBox.checked)
remoteView.style.visibility = "visible";
else
audioOnlyView.style.visibility = "visible";
};

pc.addStream(localStream);
Expand Down
1 change: 1 addition & 0 deletions server/client/webrtc_example.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ <h2>Simple WebRTC Example</h2>
<input class="btn" type="button" id="call_but" value="Call" disabled><br>
<input type="checkbox" id="audio_cb">Audio<br>
<input type="checkbox" id="video_cb" checked>Video
<div id="audio-only-container">Audio-only call</div>
<div id="share-container">
Send link to a friend: <a id="share_link" target="_blank" href=""></a>
</div>
Expand Down

0 comments on commit 613789b

Please sign in to comment.