Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for getDisplayMedia audio capture #3173

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 23 additions & 11 deletions html/janus.js
Expand Up @@ -2269,11 +2269,12 @@ function Janus(gatewayCallbacks) {
let groups = {};
for(let track of tracks) {
delete track.gumGroup;
if(!track.type || !['audio', 'video'].includes(track.type))
if(!track.type || !['audio', 'video', 'screen'].includes(track.type))
continue;
if(!track.capture || track.capture instanceof MediaStreamTrack)
continue;
let group = track.group ? track.group : 'default';
// Avoid assigning regular default group if it is a screen capture
let group = track.group ? track.group : (track.type === 'screen' ? 'screendefault' : 'default');
if(!groups[group])
groups[group] = {};
if(groups[group][track.type])
Expand All @@ -2284,11 +2285,13 @@ function Janus(gatewayCallbacks) {
let keys = Object.keys(groups);
for(let key of keys) {
let group = groups[key];
if(!group.audio || !group.video) {
if(!group.audio || (!group.video && !group.screen)) {
if(group.audio)
delete group.audio.gumGroup;
if(group.video)
delete group.video.gumGroup;
if(group.screen)
delete group.screen.gumGroup;
delete groups[key];
}
}
Expand Down Expand Up @@ -2369,7 +2372,7 @@ function Janus(gatewayCallbacks) {
await sender.replaceTrack(null);
} else if(track.capture) {
if(track.gumGroup && groups[track.gumGroup] && groups[track.gumGroup].stream) {
// We did a getUserMedia before already
// We did a getUserMedia/getDisplayMedia before already
let stream = groups[track.gumGroup].stream;
nt = (track.type === 'audio' ? stream.getAudioTracks()[0] : stream.getVideoTracks()[0]);
delete groups[track.gumGroup].stream;
Expand All @@ -2384,26 +2387,35 @@ function Janus(gatewayCallbacks) {
pluginHandle.consentDialog(true);
}
let constraints = Janus.trackConstraints(track), stream = null;
if(track.type === 'audio' || track.type === 'video') {
// Use getUserMedia: check if we need to group audio and video together
if(track.type === 'audio' || (track.type === 'video' || track.type === 'screen') ) {
rjnpnfigueiredo marked this conversation as resolved.
Show resolved Hide resolved
// Set if getUserMedia should be used or if it should be getDisplayMedia
let captureUserMedia = track.type !== 'screen';
rjnpnfigueiredo marked this conversation as resolved.
Show resolved Hide resolved
rjnpnfigueiredo marked this conversation as resolved.
Show resolved Hide resolved
// Check if we need to group audio and video together
if(track.gumGroup) {
let otherType = (track.type === 'audio' ? 'video' : 'audio');
if(groups[track.gumGroup] && groups[track.gumGroup][otherType]) {
let otherTrack = groups[track.gumGroup][otherType];
if(!otherTrack && otherType === 'video') {
// If there other track is not video, then it is screen and should be a getDisplayMedia capture
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to wrap up my mind around this check and the action it implements as a consequence, but I'm struggling to understand the logic behind it. Could you elaborate on why this should work that way? The check and the comment seem at odds.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for my own late reply, been otherwise occupied.

Logic is a bit strange, yes. This is meant to address the issue of asking for audio before screen in the group.
If the audio track is handled first, we need to know what the other track type is. If it fails to get the other track and we had the type at video (from the other type ternary above) then it means the other type is actually screen and we should treat this as a display media request.

otherType = 'screen'
otherTrack = groups[track.gumGroup]['screen'];
captureUserMedia = false;
}
let otherConstraints = Janus.trackConstraints(otherTrack);
constraints[otherType] = otherConstraints[otherType];
}
}
stream = await navigator.mediaDevices.getUserMedia(constraints);
if(captureUserMedia) {
stream = await navigator.mediaDevices.getUserMedia(constraints);
} else {
stream = await navigator.mediaDevices.getDisplayMedia(constraints);
}
if(track.gumGroup && constraints.audio && constraints.video) {
// We just performed a grouped getUserMedia, keep track of the
// We just performed a grouped getUserMedia/getDisplayMedia, keep track of the
// stream so that we can immediately assign the track later
groups[track.gumGroup].stream = stream;
delete track.gumGroup;
}
} else {
// Use getDisplayMedia
stream = await navigator.mediaDevices.getDisplayMedia(constraints);
}
nt = (track.type === 'audio' ? stream.getAudioTracks()[0] : stream.getVideoTracks()[0]);
}
Expand Down