Skip to content

Commit c9e09f7

Browse files
committed
update media locally and on the remote streams
1 parent 8e89945 commit c9e09f7

File tree

2 files changed

+85
-7
lines changed

2 files changed

+85
-7
lines changed

.gitignore

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
2+
#
3+
# If you find yourself ignoring temporary files generated by your text editor
4+
# or operating system, you probably want to add a global ignore instead:
5+
# git config --global core.excludesfile '~/.gitignore_global'
6+
7+
# Ignore all DS_Store files
8+
*.DS_Store
9+
10+
# Ignore bundler config.
11+
/.bundle
12+
13+
# Ignore all .DS_Store files
14+
*.DS_Store
15+
16+
# Ignore the default SQLite database.
17+
/db/*.sqlite3
18+
/db/*.sqlite3-journal
19+
20+
# Ignore all logfiles and tempfiles.
21+
/log/*.log
22+
/tmp
23+
24+
# IDE files
25+
*.ini
26+
.idea/*
27+
.keep

js/webrtc.js

+58-7
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ var PHONE = window.PHONE = function(config) {
4343
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
4444
// Local Microphone and Camera Media (one per device)
4545
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
46-
navigator.getUserMedia =
46+
navigator.getUserMedia =
4747
navigator.getUserMedia ||
4848
navigator.webkitGetUserMedia ||
4949
navigator.mozGetUserMedia ||
@@ -178,6 +178,7 @@ var PHONE = window.PHONE = function(config) {
178178
talk.ended = function(cb) {talk.end = cb; return talk};
179179
talk.connected = function(cb) {talk.connect = cb; return talk};
180180
talk.message = function(cb) {talk.usermsg = cb; return talk};
181+
talk.media = function(cb) {talk.mediachanged = cb; return talk};
181182

182183
// Add Local Media Streams Audio Video Mic Camera
183184
talk.pc.addStream(mystream);
@@ -253,6 +254,21 @@ var PHONE = window.PHONE = function(config) {
253254
return talk;
254255
};
255256

257+
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
258+
// Update Media - Mute/Unmute, Stop Video
259+
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
260+
PHONE.updateMedia = function(media) {
261+
mediaconf = media || {audio: true, video: true};
262+
update_tracks(mystream, mediaconf.audio, mediaconf.video);
263+
PUBNUB.each( conversations, function( number, talk ) {
264+
transmit(number, {
265+
media: true,
266+
video: mediaconf.video,
267+
audio: mediaconf.audio
268+
})
269+
} );
270+
};
271+
256272
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
257273
// Send Image Snap - Send Image Snap to All Calls or a Specific Call
258274
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
@@ -346,15 +362,12 @@ var PHONE = window.PHONE = function(config) {
346362
// Visually Display New Stream
347363
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
348364
function onaddstream(obj) {
349-
var vid = document.createElement('video');
350365
var stream = obj.stream;
351366
var number = (obj.srcElement || obj.target).number;
352367
var talk = get_conversation(number);
353368

354-
vid.setAttribute( 'autoplay', 'autoplay' );
355-
vid.src = URL.createObjectURL(stream);
356-
357-
talk.video = vid;
369+
talk.video = create_video(stream);
370+
talk.stream = stream;
358371
talk.connect(talk);
359372
}
360373

@@ -451,6 +464,11 @@ var PHONE = window.PHONE = function(config) {
451464
// If Hangup Request
452465
if (message.packet.hangup) return talk.hangup(false);
453466

467+
// Update Media
468+
if (message.packet.media && talk.stream) {
469+
return update_media(talk, message);
470+
}
471+
454472
// If Peer Calling Inbound (Incoming)
455473
if ( message.packet.sdp && !talk.received ) {
456474
talk.received = true;
@@ -530,10 +548,43 @@ var PHONE = window.PHONE = function(config) {
530548
);
531549
}
532550

551+
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
552+
// Update the stream media and call the session callback
553+
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
554+
function update_media(talk, message) {
555+
update_tracks(talk.stream, message.packet.audio, message.packet.video);
556+
talk.mediachanged(talk)
557+
}
558+
559+
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
560+
// Update the stream video and audio tracks
561+
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
562+
function update_tracks(stream, audio, video) {
563+
var audioTracks = stream.getAudioTracks();
564+
var videoTracks = stream.getVideoTracks();
565+
566+
// if MediaStream has reference to microphone
567+
if (audioTracks[0]) {
568+
audioTracks[0].enabled = audio;
569+
}
570+
571+
// if MediaStream has reference to webcam
572+
if (videoTracks[0]) {
573+
videoTracks[0].enabled = video;
574+
}
575+
}
576+
577+
function create_video(stream) {
578+
var video = document.createElement('video');
579+
video.src = URL.createObjectURL(stream);
580+
video.setAttribute( 'autoplay', 'autoplay' );
581+
return video;
582+
}
583+
533584
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
534585
// Main - Request Camera and Mic
535586
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
536-
getusermedia()
587+
getusermedia();
537588

538589
return PHONE;
539590
};

0 commit comments

Comments
 (0)