From f93668e3fd17f6f0b4d7768588f7150fc2568d29 Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthm@freeswitch.org>
Date: Fri, 18 Mar 2016 18:21:25 -0500
Subject: [PATCH] FS-7800 fix some stuff in multi-canvas

---
 html5/verto/js/src/jquery.FSRTC.js            |  4 +-
 html5/verto/js/src/jquery.verto.js            |  4 +-
 html5/verto/video_demo/js/verto-min.js        |  6 +-
 html5/verto/video_demo/verto.js               |  7 +-
 src/include/switch_types.h                    |  3 +-
 .../mod_conference/conference_api.c           | 36 +++++-----
 .../mod_conference/conference_member.c        |  4 +-
 .../mod_conference/conference_utils.c         |  8 ++-
 .../mod_conference/conference_video.c         | 68 +++++++++++--------
 .../mod_conference/mod_conference.c           |  7 +-
 .../mod_conference/mod_conference.h           |  2 +
 src/switch_core_media.c                       | 51 +++++++++++++-
 12 files changed, 139 insertions(+), 61 deletions(-)

diff --git a/html5/verto/js/src/jquery.FSRTC.js b/html5/verto/js/src/jquery.FSRTC.js
index 7bc2aa8613..c0785576ff 100644
--- a/html5/verto/js/src/jquery.FSRTC.js
+++ b/html5/verto/js/src/jquery.FSRTC.js
@@ -103,7 +103,7 @@
 
 	if (moz) {
             this.constraints = {
-		offerToReceiveAudio: true,
+		offerToReceiveAudio: this.options.useSpeak === "none" ? false : true,
 		offerToReceiveVideo: this.options.useVideo ? true : false,
             };
 	} else {
@@ -111,7 +111,7 @@
 		optional: [{
 		    'DtlsSrtpKeyAgreement': 'true'
 		}],mandatory: {
-		    OfferToReceiveAudio: true,
+		    OfferToReceiveAudio: this.options.useSpeak === "none" ? false : true,
 		    OfferToReceiveVideo: this.options.useVideo ? true : false,
 		}
             };
diff --git a/html5/verto/js/src/jquery.verto.js b/html5/verto/js/src/jquery.verto.js
index 0fd9fb846a..2f10a6cea1 100644
--- a/html5/verto/js/src/jquery.verto.js
+++ b/html5/verto/js/src/jquery.verto.js
@@ -1439,7 +1439,7 @@
 
 		    var vlhtml =  "<div id='" + vlayout_id + "'><br>" +
 			"<b>Video Layout Canvas " + (j+1) + 
-			"</b> <select onChange='$.verto.modfuncs.change_video_layout(\"" + vlayout_id + "\", \"" + j + "\")' id='" + vlselect_id + "'></select> " +
+			"</b> <select onChange='$.verto.modfuncs.change_video_layout(\"" + vlayout_id + "\", \"" + (j+1) + "\")' id='" + vlselect_id + "'></select> " +
 			"<br><br></div>";
 		    jq.append(vlhtml);
 		}
@@ -2154,7 +2154,7 @@
 	    var speaker = dialog.useSpeak;
 	    console.info("Using Speaker: ", speaker);
 
-	    if (speaker && speaker !== "any") {
+	    if (speaker && speaker !== "any" && speaker !== "none") {
 		setTimeout(function() {
 		    dialog.setAudioPlaybackDevice(speaker);
 		}, 500);
diff --git a/html5/verto/video_demo/js/verto-min.js b/html5/verto/video_demo/js/verto-min.js
index ce4dd194f3..35ecf5767a 100644
--- a/html5/verto/video_demo/js/verto-min.js
+++ b/html5/verto/video_demo/js/verto-min.js
@@ -6,7 +6,7 @@ function getCodecPayloadType(sdpLine){var pattern=new RegExp('a=rtpmap:(\\d+) \\
 function setDefaultCodec(mLine,payload){var elements=mLine.split(' ');var newLine=[];var index=0;for(var i=0;i<elements.length;i++){if(index===3){newLine[index++]=payload;}
 if(elements[i]!==payload)newLine[index++]=elements[i];}
 return newLine.join(' ');}
-$.FSRTC=function(options){this.options=$.extend({useVideo:null,useStereo:false,userData:null,localVideo:null,screenShare:false,useCamera:"any",iceServers:false,videoParams:{},audioParams:{},callbacks:{onICEComplete:function(){},onICE:function(){},onOfferSDP:function(){}},},options);this.audioEnabled=true;this.videoEnabled=true;this.mediaData={SDP:null,profile:{},candidateList:[]};if(moz){this.constraints={offerToReceiveAudio:true,offerToReceiveVideo:this.options.useVideo?true:false,};}else{this.constraints={optional:[{'DtlsSrtpKeyAgreement':'true'}],mandatory:{OfferToReceiveAudio:true,OfferToReceiveVideo:this.options.useVideo?true:false,}};}
+$.FSRTC=function(options){this.options=$.extend({useVideo:null,useStereo:false,userData:null,localVideo:null,screenShare:false,useCamera:"any",iceServers:false,videoParams:{},audioParams:{},callbacks:{onICEComplete:function(){},onICE:function(){},onOfferSDP:function(){}},},options);this.audioEnabled=true;this.videoEnabled=true;this.mediaData={SDP:null,profile:{},candidateList:[]};if(moz){this.constraints={offerToReceiveAudio:this.options.useSpeak==="none"?false:true,offerToReceiveVideo:this.options.useVideo?true:false,};}else{this.constraints={optional:[{'DtlsSrtpKeyAgreement':'true'}],mandatory:{OfferToReceiveAudio:this.options.useSpeak==="none"?false:true,OfferToReceiveVideo:this.options.useVideo?true:false,}};}
 if(self.options.useVideo){self.options.useVideo.style.display='none';}
 setCompat();checkCompat();};$.FSRTC.validRes=[];$.FSRTC.prototype.useVideo=function(obj,local){var self=this;if(obj){self.options.useVideo=obj;self.options.localVideo=local;if(moz){self.constraints.offerToReceiveVideo=true;}else{self.constraints.mandatory.OfferToReceiveVideo=true;}}else{self.options.useVideo=null;self.options.localVideo=null;if(moz){self.constraints.offerToReceiveVideo=false;}else{self.constraints.mandatory.OfferToReceiveVideo=false;}}
 if(self.options.useVideo){self.options.useVideo.style.display='none';}};$.FSRTC.prototype.useStereo=function(on){var self=this;self.options.useStereo=on;};$.FSRTC.prototype.stereoHack=function(sdp){var self=this;if(!self.options.useStereo){return sdp;}
@@ -225,7 +225,7 @@ this.modCommand("vid-res-id",parseInt(memberID),"presenter");};$.verto.conf.prot
 this.modCommand("vid-floor",parseInt(memberID),"force");};$.verto.conf.prototype.banner=function(memberID,text){if(!this.params.hasVid){throw'Conference has no video';}
 this.modCommand("vid-banner",parseInt(memberID),escape(text));};$.verto.conf.prototype.volumeDown=function(memberID){this.modCommand("volume_out",parseInt(memberID),"down");};$.verto.conf.prototype.volumeUp=function(memberID){this.modCommand("volume_out",parseInt(memberID),"up");};$.verto.conf.prototype.gainDown=function(memberID){this.modCommand("volume_in",parseInt(memberID),"down");};$.verto.conf.prototype.gainUp=function(memberID){this.modCommand("volume_in",parseInt(memberID),"up");};$.verto.conf.prototype.transfer=function(memberID,exten){this.modCommand("transfer",parseInt(memberID),exten);};$.verto.conf.prototype.sendChat=function(message,type){var conf=this;conf.verto.rpcClient.call("verto.broadcast",{"eventChannel":conf.params.laData.chatChannel,"data":{"action":"send","message":message,"type":type}});};}
 $.verto.modfuncs={};$.verto.confMan=function(verto,params){var confMan=this;confMan.params=$.extend({tableID:null,statusID:null,mainModID:null,dialog:null,hasVid:false,laData:null,onBroadcast:null,onLaChange:null,onLaRow:null},params);confMan.verto=verto;confMan.serno=CONFMAN_SERNO++;confMan.canvasCount=confMan.params.laData.canvasCount;function genMainMod(jq){var play_id="play_"+confMan.serno;var stop_id="stop_"+confMan.serno;var recording_id="recording_"+confMan.serno;var snapshot_id="snapshot_"+confMan.serno;var rec_stop_id="recording_stop"+confMan.serno;var div_id="confman_"+confMan.serno;var html="<div id='"+div_id+"'><br>"+"<button class='ctlbtn' id='"+play_id+"'>Play</button>"+"<button class='ctlbtn' id='"+stop_id+"'>Stop</button>"+"<button class='ctlbtn' id='"+recording_id+"'>Record</button>"+"<button class='ctlbtn' id='"+rec_stop_id+"'>Record Stop</button>"+
-(confMan.params.hasVid?"<button class='ctlbtn' id='"+snapshot_id+"'>PNG Snapshot</button>":"")+"<br><br></div>";jq.html(html);$.verto.modfuncs.change_video_layout=function(id,canvas_id){var val=$("#"+id+" option:selected").text();if(val!=="none"){confMan.modCommand("vid-layout",null,[val,canvas_id]);}};if(confMan.params.hasVid){for(var j=0;j<confMan.canvasCount;j++){var vlayout_id="confman_vid_layout_"+j+"_"+confMan.serno;var vlselect_id="confman_vl_select_"+j+"_"+confMan.serno;var vlhtml="<div id='"+vlayout_id+"'><br>"+"<b>Video Layout Canvas "+(j+1)+"</b> <select onChange='$.verto.modfuncs.change_video_layout(\""+vlayout_id+"\", \""+j+"\")' id='"+vlselect_id+"'></select> "+"<br><br></div>";jq.append(vlhtml);}
+(confMan.params.hasVid?"<button class='ctlbtn' id='"+snapshot_id+"'>PNG Snapshot</button>":"")+"<br><br></div>";jq.html(html);$.verto.modfuncs.change_video_layout=function(id,canvas_id){var val=$("#"+id+" option:selected").text();if(val!=="none"){confMan.modCommand("vid-layout",null,[val,canvas_id]);}};if(confMan.params.hasVid){for(var j=0;j<confMan.canvasCount;j++){var vlayout_id="confman_vid_layout_"+j+"_"+confMan.serno;var vlselect_id="confman_vl_select_"+j+"_"+confMan.serno;var vlhtml="<div id='"+vlayout_id+"'><br>"+"<b>Video Layout Canvas "+(j+1)+"</b> <select onChange='$.verto.modfuncs.change_video_layout(\""+vlayout_id+"\", \""+(j+1)+"\")' id='"+vlselect_id+"'></select> "+"<br><br></div>";jq.append(vlhtml);}
 $("#"+snapshot_id).click(function(){var file=prompt("Please enter file name","");if(file){confMan.modCommand("vid-write-png",null,file);}});}
 $("#"+play_id).click(function(){var file=prompt("Please enter file name","");if(file){confMan.modCommand("play",null,file);}});$("#"+stop_id).click(function(){confMan.modCommand("stop",null,"all");});$("#"+recording_id).click(function(){var file=prompt("Please enter file name","");if(file){confMan.modCommand("recording",null,["start",file]);}});$("#"+rec_stop_id).click(function(){confMan.modCommand("recording",null,["stop","all"]);});}
 function genControls(jq,rowid){var x=parseInt(rowid);var kick_id="kick_"+x;var canvas_in_next_id="canvas_in_next_"+x;var canvas_in_prev_id="canvas_in_prev_"+x;var canvas_out_next_id="canvas_out_next_"+x;var canvas_out_prev_id="canvas_out_prev_"+x;var canvas_in_set_id="canvas_in_set_"+x;var canvas_out_set_id="canvas_out_set_"+x;var layer_set_id="layer_set_"+x;var layer_next_id="layer_next_"+x;var layer_prev_id="layer_prev_"+x;var tmute_id="tmute_"+x;var tvmute_id="tvmute_"+x;var vbanner_id="vbanner_"+x;var tvpresenter_id="tvpresenter_"+x;var tvfloor_id="tvfloor_"+x;var box_id="box_"+x;var gainup_id="gain_in_up"+x;var gaindn_id="gain_in_dn"+x;var volup_id="vol_in_up"+x;var voldn_id="vol_in_dn"+x;var transfer_id="transfer"+x;var html="<div id='"+box_id+"'>";html+="<b>General Controls</b><hr noshade>";html+="<button class='ctlbtn' id='"+kick_id+"'>Kick</button>"+"<button class='ctlbtn' id='"+tmute_id+"'>Mute</button>"+"<button class='ctlbtn' id='"+gainup_id+"'>Gain -</button>"+"<button class='ctlbtn' id='"+gaindn_id+"'>Gain +</button>"+"<button class='ctlbtn' id='"+voldn_id+"'>Vol -</button>"+"<button class='ctlbtn' id='"+volup_id+"'>Vol +</button>"+"<button class='ctlbtn' id='"+transfer_id+"'>Transfer</button>";if(confMan.params.hasVid){html+="<br><br><b>Video Controls</b><hr noshade>";html+="<button class='ctlbtn' id='"+tvmute_id+"'>VMute</button>"+"<button class='ctlbtn' id='"+tvpresenter_id+"'>Presenter</button>"+"<button class='ctlbtn' id='"+tvfloor_id+"'>Vid Floor</button>"+"<button class='ctlbtn' id='"+vbanner_id+"'>Banner</button>";if(confMan.canvasCount>1){html+="<br><br><b>Canvas Controls</b><hr noshade>"+"<button class='ctlbtn' id='"+canvas_in_set_id+"'>Set Input Canvas</button>"+"<button class='ctlbtn' id='"+canvas_in_prev_id+"'>Prev Input Canvas</button>"+"<button class='ctlbtn' id='"+canvas_in_next_id+"'>Next Input Canvas</button>"+"<br>"+"<button class='ctlbtn' id='"+canvas_out_set_id+"'>Set Watching Canvas</button>"+"<button class='ctlbtn' id='"+canvas_out_prev_id+"'>Prev Watching Canvas</button>"+"<button class='ctlbtn' id='"+canvas_out_next_id+"'>Next Watching Canvas</button>";}
@@ -266,7 +266,7 @@ if(dialog.state==state||!checkStateChange(dialog.state,state)){console.error("Di
 console.log("Dialog "+dialog.callID+": state change from "+dialog.state.name+" to "+state.name);dialog.lastState=dialog.state;dialog.state=state;if(!dialog.causeCode){dialog.causeCode=16;}
 if(!dialog.cause){dialog.cause="NORMAL CLEARING";}
 if(dialog.callbacks.onDialogState){dialog.callbacks.onDialogState(this);}
-switch(dialog.state){case $.verto.enum.state.early:case $.verto.enum.state.active:var speaker=dialog.useSpeak;console.info("Using Speaker: ",speaker);if(speaker&&speaker!=="any"){setTimeout(function(){dialog.setAudioPlaybackDevice(speaker);},500);}
+switch(dialog.state){case $.verto.enum.state.early:case $.verto.enum.state.active:var speaker=dialog.useSpeak;console.info("Using Speaker: ",speaker);if(speaker&&speaker!=="any"&&speaker!=="none"){setTimeout(function(){dialog.setAudioPlaybackDevice(speaker);},500);}
 break;case $.verto.enum.state.trying:setTimeout(function(){if(dialog.state==$.verto.enum.state.trying){dialog.setState($.verto.enum.state.hangup);}},30000);break;case $.verto.enum.state.purge:dialog.setState($.verto.enum.state.destroy);break;case $.verto.enum.state.hangup:if(dialog.lastState.val>$.verto.enum.state.requesting.val&&dialog.lastState.val<$.verto.enum.state.hangup.val){dialog.sendMethod("verto.bye",{});}
 dialog.setState($.verto.enum.state.destroy);break;case $.verto.enum.state.destroy:delete dialog.verto.dialogs[dialog.callID];if(dialog.params.screenShare){dialog.rtc.stopPeer();}else{dialog.rtc.stop();}
 break;}
diff --git a/html5/verto/video_demo/verto.js b/html5/verto/video_demo/verto.js
index 2df2f0824d..997d63a119 100644
--- a/html5/verto/video_demo/verto.js
+++ b/html5/verto/video_demo/verto.js
@@ -799,6 +799,7 @@ function docall() {
 
     check_vid_res();
     console.error(outgoingBandwidth, incomingBandwidth);
+
     cur_call = vertoHandle.newCall({
         destination_number: $("#ext").val(),
         caller_id_name: $("#cidname").val(),
@@ -807,9 +808,9 @@ function docall() {
 	incomingBandwidth: incomingBandwidth,
         useVideo: check_vid(),
         useStereo: $("#use_stereo").is(':checked'),
-	useCamera: sessid ? "none" : $("#usecamera").find(":selected").val(),
-	useMic: $("#usemic").find(":selected").val(),
-	useSpeak: $("#usespeak").find(":selected").val(),
+	useCamera: (sessid || canvas_id) ? "none" : $("#usecamera").find(":selected").val(),
+	useMic: (sessid || canvas_id) ? "none" : $("#usemic").find(":selected").val(),
+	useSpeak: (sessid || canvas_id) ? "none" : $("#usespeak").find(":selected").val(),
 	dedEnc: $("#use_dedenc").is(':checked'),
 	mirrorInput: $("#mirror_input").is(':checked'),
         userVariables: {
diff --git a/src/include/switch_types.h b/src/include/switch_types.h
index f740b94932..fda2d20b22 100644
--- a/src/include/switch_types.h
+++ b/src/include/switch_types.h
@@ -2551,7 +2551,8 @@ typedef enum {
 	SWITCH_MEDIA_FLOW_SENDRECV = 0,
 	SWITCH_MEDIA_FLOW_SENDONLY,
 	SWITCH_MEDIA_FLOW_RECVONLY,
-	SWITCH_MEDIA_FLOW_INACTIVE
+	SWITCH_MEDIA_FLOW_INACTIVE,
+	SWITCH_MEDIA_FLOW_DISABLED
 } switch_media_flow_t;
 
 typedef enum {
diff --git a/src/mod/applications/mod_conference/conference_api.c b/src/mod/applications/mod_conference/conference_api.c
index 0b685d3c3a..f3fce9d5a1 100644
--- a/src/mod/applications/mod_conference/conference_api.c
+++ b/src/mod/applications/mod_conference/conference_api.c
@@ -1161,6 +1161,8 @@ switch_status_t conference_api_sub_write_png(conference_obj_t *conference, switc
 switch_status_t conference_api_sub_vid_layout(conference_obj_t *conference, switch_stream_handle_t *stream, int argc, char **argv)
 {
 	video_layout_t *vlayout = NULL;
+	char *group_name = NULL;
+
 	int idx = 0;
 
 	if (!argv[2]) {
@@ -1186,7 +1188,6 @@ switch_status_t conference_api_sub_vid_layout(conference_obj_t *conference, swit
 
 	if (!strncasecmp(argv[2], "group", 5)) {
 		layout_group_t *lg = NULL;
-		char *group_name = NULL;
 		int xx = 4;
 
 		if ((group_name = strchr(argv[2], ':'))) {
@@ -1195,7 +1196,7 @@ switch_status_t conference_api_sub_vid_layout(conference_obj_t *conference, swit
 		} else {
 			group_name = argv[3];
 		}
-
+		
 		if (!group_name) {
 			stream->write_function(stream, "Group name not specified.\n");
 			return SWITCH_STATUS_SUCCESS;
@@ -1206,33 +1207,30 @@ switch_status_t conference_api_sub_vid_layout(conference_obj_t *conference, swit
 					conference->video_layout_group = switch_core_strdup(conference->pool, group_name);
 					conference_utils_set_flag(conference, CFLAG_REFRESH_LAYOUT);
 					return SWITCH_STATUS_SUCCESS;
-				} else {
-					vlayout = conference_video_find_best_layout(conference, lg, 0);
 				}
-			}
-
-			if (!vlayout) {
-				stream->write_function(stream, "Invalid group layout [%s]\n", group_name);
-				return SWITCH_STATUS_SUCCESS;
+			} else {
+				group_name = NULL;
 			}
 
 			stream->write_function(stream, "Change to layout group [%s]\n", group_name);
-			conference->video_layout_group = switch_core_strdup(conference->pool, group_name);			
 			
 			if (argv[xx]) {
-				idx = atoi(argv[xx]);
+				if ((idx = atoi(argv[xx])) > 0) {
+					idx--;
+				}
 			}
 		}
 	}
 
 	if (!vlayout && (vlayout = switch_core_hash_find(conference->layout_hash, argv[2]))) {
-		conference->video_layout_group = NULL;
 		if (argv[3]) {
-			idx = atoi(argv[3]);
+			if ((idx = atoi(argv[3]))) {
+				idx--;
+			}
 		}
 	}
 
-	if (!vlayout) {
+	if (!vlayout && !group_name) {
 		stream->write_function(stream, "Invalid layout [%s]\n", argv[2]);
 		return SWITCH_STATUS_SUCCESS;
 	}
@@ -1246,9 +1244,15 @@ switch_status_t conference_api_sub_vid_layout(conference_obj_t *conference, swit
 		conference->new_personal_vlayout = vlayout;
 		switch_mutex_unlock(conference->member_mutex);
 	} else {
-		stream->write_function(stream, "Change canvas %d to layout [%s]\n", idx + 1, vlayout->name);
+
 		switch_mutex_lock(conference->canvases[idx]->mutex);
-		conference->canvases[idx]->new_vlayout = vlayout;
+		if (vlayout) {
+			stream->write_function(stream, "Change canvas %d to layout [%s]\n", idx + 1, vlayout->name);
+			conference->canvases[idx]->new_vlayout = vlayout;
+		} else if (group_name) {
+			conference->canvases[idx]->video_layout_group = switch_core_strdup(conference->pool, group_name);
+			conference_utils_set_flag(conference, CFLAG_REFRESH_LAYOUT);
+		}
 		switch_mutex_unlock(conference->canvases[idx]->mutex);
 	}
 
diff --git a/src/mod/applications/mod_conference/conference_member.c b/src/mod/applications/mod_conference/conference_member.c
index 6eb2668331..21e29dbc2a 100644
--- a/src/mod/applications/mod_conference/conference_member.c
+++ b/src/mod/applications/mod_conference/conference_member.c
@@ -736,12 +736,12 @@ switch_status_t conference_member_add(conference_obj_t *conference, conference_m
 
 		channel = switch_core_session_get_channel(member->session);
 
-		conference_video_check_avatar(member, SWITCH_FALSE);
-
 		if (switch_true(switch_channel_get_variable_dup(member->channel, "video_second_screen", SWITCH_FALSE, -1))) {
 			conference_utils_member_set_flag(member, MFLAG_SECOND_SCREEN);
 		}
 
+		conference_video_check_avatar(member, SWITCH_FALSE);
+
 		if ((var = switch_channel_get_variable_dup(member->channel, "video_initial_canvas", SWITCH_FALSE, -1))) {
 			uint32_t id = atoi(var) - 1;
 			if (id < conference->canvas_count) {
diff --git a/src/mod/applications/mod_conference/conference_utils.c b/src/mod/applications/mod_conference/conference_utils.c
index 274b499e44..269e558879 100644
--- a/src/mod/applications/mod_conference/conference_utils.c
+++ b/src/mod/applications/mod_conference/conference_utils.c
@@ -338,12 +338,18 @@ switch_bool_t conference_utils_test_mflag(conference_obj_t *conference, member_f
 void conference_utils_member_set_flag(conference_member_t *member, member_flag_t flag)
 {
 	member->flags[flag] = 1;
+
+	if (flag == MFLAG_SECOND_SCREEN) {
+		member->flags[MFLAG_CAN_SPEAK] = 0;
+		member->flags[MFLAG_CAN_HEAR] = 0;
+		member->flags[MFLAG_CAN_BE_SEEN] = 0;
+	}
 }
 
 void conference_utils_member_set_flag_locked(conference_member_t *member, member_flag_t flag)
 {
 	switch_mutex_lock(member->flag_mutex);
-	member->flags[flag] = 1;
+	conference_utils_member_set_flag(member, flag);
 	switch_mutex_unlock(member->flag_mutex);
 }
 
diff --git a/src/mod/applications/mod_conference/conference_video.c b/src/mod/applications/mod_conference/conference_video.c
index c473769880..87a0d24b93 100644
--- a/src/mod/applications/mod_conference/conference_video.c
+++ b/src/mod/applications/mod_conference/conference_video.c
@@ -1340,7 +1340,13 @@ video_layout_t *conference_video_find_best_layout(conference_obj_t *conference,
 {
 	video_layout_node_t *vlnode = NULL, *last = NULL;
 
-	if (!count) count = conference->members_with_video + conference->members_with_avatar;
+	if (!count) {
+		count = conference->members_with_video;
+
+		if (!conference_utils_test_flag(conference, CFLAG_VIDEO_REQUIRED_FOR_CANVAS)) {
+			count += conference->members_with_avatar;
+		}
+	}
 
 	for (vlnode = lg->layouts; vlnode; vlnode = vlnode->next) {
 		if (vlnode->vlayout->layers >= (int)count) {
@@ -1624,6 +1630,10 @@ void conference_video_check_avatar(conference_member_t *member, switch_bool_t fo
 		return;
 	}
 
+	if (conference_utils_member_test_flag(member, MFLAG_SECOND_SCREEN)) {
+		return;
+	}
+
 	canvas = conference_video_get_canvas_locked(member);
 
 	if (conference_utils_test_flag(member->conference, CFLAG_VIDEO_REQUIRED_FOR_CANVAS) &&
@@ -2149,6 +2159,7 @@ void *SWITCH_THREAD_FUNC conference_video_muxing_thread_run(switch_thread_t *thr
 	int last_personal = conference_utils_test_flag(conference, CFLAG_PERSONAL_CANVAS) ? 1 : 0;
 
 	canvas->video_timer_reset = 1;
+	canvas->video_layout_group = conference->video_layout_group;
 
 	packet = switch_core_alloc(conference->pool, SWITCH_RTP_MAX_BUF_LEN);
 	
@@ -2161,7 +2172,8 @@ void *SWITCH_THREAD_FUNC conference_video_muxing_thread_run(switch_thread_t *thr
 		switch_image_t *async_file_img = NULL, *normal_file_img = NULL, *file_imgs[2] = { 0 };
 		switch_frame_t file_frame = { 0 };
 		int j = 0, personal = conference_utils_test_flag(conference, CFLAG_PERSONAL_CANVAS) ? 1 : 0;
-		
+		int video_count = 0;
+
 		if (!personal) {
 			if (canvas->new_vlayout && switch_mutex_trylock(conference->canvas_mutex) == SWITCH_STATUS_SUCCESS) {
 				conference_video_init_canvas_layers(conference, canvas, NULL);
@@ -2180,7 +2192,24 @@ void *SWITCH_THREAD_FUNC conference_video_muxing_thread_run(switch_thread_t *thr
 			canvas->send_keyframe = 1;
 		}
 
-		
+		video_count = 0;
+
+		switch_mutex_lock(conference->member_mutex);
+		for (imember = conference->members; imember; imember = imember->next) {
+			int no_muted = conference_utils_test_flag(imember->conference, CFLAG_VIDEO_MUTE_EXIT_CANVAS);
+			int no_av = conference_utils_test_flag(imember->conference, CFLAG_VIDEO_REQUIRED_FOR_CANVAS);
+			int seen = conference_utils_member_test_flag(imember, MFLAG_CAN_BE_SEEN);
+			
+			if (imember->channel && switch_channel_ready(imember->channel) && switch_channel_test_flag(imember->channel, CF_VIDEO_READY) && 
+				!conference_utils_member_test_flag(imember, MFLAG_SECOND_SCREEN) &&
+				conference_utils_member_test_flag(imember, MFLAG_RUNNING) && (!no_muted || seen) && (!no_av || imember->avatar_png_img)
+				&& imember->canvas_id == canvas->canvas_id && imember->video_media_flow != SWITCH_MEDIA_FLOW_SENDONLY) {
+				video_count++;
+			}
+		}
+		canvas->video_count = video_count;
+		switch_mutex_unlock(conference->member_mutex);
+
 		switch_core_timer_next(&canvas->timer);
 
 		now = switch_micro_time_now();
@@ -2204,28 +2233,18 @@ void *SWITCH_THREAD_FUNC conference_video_muxing_thread_run(switch_thread_t *thr
 		if (members_with_avatar != conference->members_with_avatar) {
 			count_changed = 1;
 		}
+	
+		if (conference_utils_test_flag(conference, CFLAG_REFRESH_LAYOUT)) {
+			count_changed = 1;
+			conference_utils_clear_flag(conference, CFLAG_REFRESH_LAYOUT);
+		}
 
 		if (count_changed && !personal) {
 			layout_group_t *lg = NULL;
 			video_layout_t *vlayout = NULL;
-			int canvas_count = 0;
-			
-			switch_mutex_lock(conference->member_mutex);
-			for (imember = conference->members; imember; imember = imember->next) {
-				int no_muted = conference_utils_test_flag(imember->conference, CFLAG_VIDEO_MUTE_EXIT_CANVAS);
-				int no_av = conference_utils_test_flag(imember->conference, CFLAG_VIDEO_REQUIRED_FOR_CANVAS);
-				int seen = conference_utils_member_test_flag(imember, MFLAG_CAN_BE_SEEN);
 
-				if (imember->channel && switch_channel_ready(imember->channel) && switch_channel_test_flag(imember->channel, CF_VIDEO_READY) && 
-					conference_utils_member_test_flag(imember, MFLAG_RUNNING) && (!no_muted || seen) && (!no_av || imember->avatar_png_img)
-					&& imember->canvas_id == canvas->canvas_id && imember->video_media_flow != SWITCH_MEDIA_FLOW_SENDONLY) {
-					canvas_count++;
-				}
-			}
-			switch_mutex_unlock(conference->member_mutex);
-
-			if (conference->video_layout_group && (lg = switch_core_hash_find(conference->layout_group_hash, conference->video_layout_group))) {
-				if ((vlayout = conference_video_find_best_layout(conference, lg, canvas_count))) {
+			if (canvas->video_layout_group && (lg = switch_core_hash_find(conference->layout_group_hash, canvas->video_layout_group))) {
+				if ((vlayout = conference_video_find_best_layout(conference, lg, canvas->video_count))) {
 					switch_mutex_lock(conference->member_mutex);
 					canvas->new_vlayout = vlayout;
 					switch_mutex_unlock(conference->member_mutex);
@@ -2534,11 +2553,6 @@ void *SWITCH_THREAD_FUNC conference_video_muxing_thread_run(switch_thread_t *thr
 
 			switch_mutex_lock(conference->member_mutex);
 
-			if (conference_utils_test_flag(conference, CFLAG_REFRESH_LAYOUT)) {
-				count_changed = 1;
-				conference_utils_clear_flag(conference, CFLAG_REFRESH_LAYOUT);
-			}
-			
 			for (imember = conference->members; imember; imember = imember->next) {
 
 				if (!imember->session || !switch_channel_test_flag(imember->channel, CF_VIDEO_READY) ||
@@ -2547,7 +2561,7 @@ void *SWITCH_THREAD_FUNC conference_video_muxing_thread_run(switch_thread_t *thr
 				}
 
 				if (!imember->canvas) {
-					if ((vlayout = conference_video_get_layout(conference, conference->video_layout_name, conference->video_layout_group))) {
+					if ((vlayout = conference_video_get_layout(conference, conference->video_layout_name, canvas->video_layout_group))) {
 						conference_video_init_canvas(conference, vlayout, &imember->canvas);
 						conference_video_init_canvas_layers(conference, imember->canvas, vlayout);
 					} else {
@@ -2584,7 +2598,7 @@ void *SWITCH_THREAD_FUNC conference_video_muxing_thread_run(switch_thread_t *thr
 						total = 0;
 					}
 					
-					if (conference->video_layout_group && (lg = switch_core_hash_find(conference->layout_group_hash, conference->video_layout_group))) {
+					if (canvas->video_layout_group && (lg = switch_core_hash_find(conference->layout_group_hash, canvas->video_layout_group))) {
 						if ((vlayout = conference_video_find_best_layout(conference, lg, total + file_count))) {
 							conference_video_init_canvas_layers(conference, imember->canvas, vlayout);
 						}
diff --git a/src/mod/applications/mod_conference/mod_conference.c b/src/mod/applications/mod_conference/mod_conference.c
index 791b722ce4..3ed558f983 100644
--- a/src/mod/applications/mod_conference/mod_conference.c
+++ b/src/mod/applications/mod_conference/mod_conference.c
@@ -279,7 +279,12 @@ void *SWITCH_THREAD_FUNC conference_thread_run(switch_thread_t *thread, void *ob
 					}
 				}
 
-				if (switch_channel_ready(channel) && switch_channel_test_flag(channel, CF_VIDEO_READY) && imember->video_media_flow != SWITCH_MEDIA_FLOW_SENDONLY && (!conference_utils_test_flag(conference, CFLAG_VIDEO_MUTE_EXIT_CANVAS) || conference_utils_member_test_flag(imember, MFLAG_CAN_BE_SEEN))) {
+				if (switch_channel_ready(channel) && 
+					switch_channel_test_flag(channel, CF_VIDEO_READY) && 
+					imember->video_media_flow != SWITCH_MEDIA_FLOW_SENDONLY && 
+					!conference_utils_member_test_flag(imember, MFLAG_SECOND_SCREEN) && 
+					(!conference_utils_test_flag(conference, CFLAG_VIDEO_MUTE_EXIT_CANVAS) || 
+					 conference_utils_member_test_flag(imember, MFLAG_CAN_BE_SEEN))) {
 					members_with_video++;
 				}
 
diff --git a/src/mod/applications/mod_conference/mod_conference.h b/src/mod/applications/mod_conference/mod_conference.h
index c7407391fd..02b3355384 100644
--- a/src/mod/applications/mod_conference/mod_conference.h
+++ b/src/mod/applications/mod_conference/mod_conference.h
@@ -485,6 +485,8 @@ typedef struct mcu_canvas_s {
 	int refresh;
 	int send_keyframe;
 	int play_file;
+	int video_count;
+	char *video_layout_group;
 	switch_rgb_color_t bgcolor;
 	switch_rgb_color_t border_color;
 	switch_rgb_color_t letterbox_bgcolor;
diff --git a/src/switch_core_media.c b/src/switch_core_media.c
index 7441f018b9..f4a1a9b491 100644
--- a/src/switch_core_media.c
+++ b/src/switch_core_media.c
@@ -3625,7 +3625,7 @@ SWITCH_DECLARE(uint8_t) switch_core_media_negotiate_sdp(switch_core_session_t *s
 	switch_channel_t *channel = switch_core_session_get_channel(session);
 	const char *val;
 	const char *crypto = NULL;
-	int got_crypto = 0, got_video_crypto = 0, got_audio = 0, got_avp = 0, got_video_avp = 0, got_video_savp = 0, got_savp = 0, got_udptl = 0, got_webrtc = 0;
+	int got_crypto = 0, got_video_crypto = 0, got_audio = 0, saw_audio = 0, got_avp = 0, got_video_avp = 0, got_video_savp = 0, got_savp = 0, got_udptl = 0, got_webrtc = 0;
 	int scrooge = 0;
 	sdp_parser_t *parser = NULL;
 	sdp_session_t *sdp;
@@ -3666,6 +3666,8 @@ SWITCH_DECLARE(uint8_t) switch_core_media_negotiate_sdp(switch_core_session_t *s
 		return 0;
 	}
 
+	switch_channel_clear_flag(channel, CF_AUDIO_PAUSE);
+
 	if (dtls_ok(session) && (tmp = switch_channel_get_variable(smh->session->channel, "webrtc_enable_dtls")) && switch_false(tmp)) {
 		switch_channel_clear_flag(smh->session->channel, CF_DTLS_OK);
 		switch_channel_clear_flag(smh->session->channel, CF_DTLS);
@@ -3751,6 +3753,10 @@ SWITCH_DECLARE(uint8_t) switch_core_media_negotiate_sdp(switch_core_session_t *s
 			continue;
 		}
 
+		if (m->m_type == sdp_media_audio) {
+			saw_audio = 1;
+		}
+
 		ptime = dptime;
 		maxptime = dmaxptime;
 
@@ -3929,7 +3935,7 @@ SWITCH_DECLARE(uint8_t) switch_core_media_negotiate_sdp(switch_core_session_t *s
 					break;
 				}
 			}
-
+			
 			for (attr = sdp->sdp_attributes; attr; attr = attr->a_next) {
 				if (zstr(attr->a_name)) {
 					continue;
@@ -4780,6 +4786,35 @@ SWITCH_DECLARE(uint8_t) switch_core_media_negotiate_sdp(switch_core_session_t *s
 		}
 	}
 
+	if (!saw_audio) {
+		payload_map_t *pmap;
+
+		a_engine->rmode = SWITCH_MEDIA_FLOW_DISABLED;
+		switch_channel_set_variable(smh->session->channel, "audio_media_flow", "inactive");
+		
+
+		pmap = switch_core_media_add_payload_map(session,
+												 SWITCH_MEDIA_TYPE_AUDIO,
+												 "L16",
+												 NULL,
+												 NULL,
+												 SDP_TYPE_REQUEST,
+												 97,
+												 8000,
+												 20,
+												 1,
+												 SWITCH_TRUE);
+
+		pmap->remote_sdp_ip = "127.0.0.1";
+		pmap->remote_sdp_port = 9999;
+		pmap->agreed_pt = 97;
+		pmap->recv_pt = 97;
+		pmap->codec_ms = 20;
+		a_engine->cur_payload_map = pmap;
+		switch_channel_set_flag(channel, CF_AUDIO_PAUSE);
+	}
+
+
 	if (!match && vmatch) match = 1;
 
  done:
@@ -6480,7 +6515,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_activate_rtp(switch_core_sessi
 			if (!strcasecmp(val, "passthru")) {
 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "Activating RTCP PASSTHRU PORT %d\n", remote_rtcp_port);
 				switch_rtp_activate_rtcp(a_engine->rtp_session, -1, remote_rtcp_port, a_engine->rtcp_mux > 0);
-			} else {
+			} else if (remote_rtcp_port) {
 				int interval = atoi(val);
 				if (interval < 100 || interval > 500000) {
 					switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR,
@@ -7766,6 +7801,10 @@ SWITCH_DECLARE(void) switch_core_media_gen_local_sdp(switch_core_session_t *sess
 					username, smh->owner_id, smh->session_id, family, ip, username, family, ip, srbuf);
 
 
+	if (a_engine->rmode == SWITCH_MEDIA_FLOW_DISABLED) {
+		goto video;
+	}
+
 	if (switch_channel_test_flag(smh->session->channel, CF_ICE)) {
 		gen_ice(session, SWITCH_MEDIA_TYPE_AUDIO, ip, port);
 		switch_snprintf(buf + strlen(buf), SDPBUFLEN - strlen(buf), "a=msid-semantic: WMS %s\r\n", smh->msid);
@@ -8059,6 +8098,8 @@ SWITCH_DECLARE(void) switch_core_media_gen_local_sdp(switch_core_session_t *sess
 
 	}
 
+ video:
+
 	if (!switch_channel_test_flag(session->channel, CF_VIDEO_POSSIBLE)) {
 		if (switch_channel_test_flag(session->channel, CF_VIDEO_SDP_RECVD)) {
 			switch_channel_clear_flag(session->channel, CF_VIDEO_SDP_RECVD);
@@ -9129,6 +9170,10 @@ SWITCH_DECLARE(switch_bool_t) switch_core_media_check_dtls(switch_core_session_t
 	
 	engine = &smh->engines[type];
 
+	if (engine->rmode == SWITCH_MEDIA_FLOW_DISABLED) {
+		return SWITCH_TRUE;
+	}
+
 	do {
 		if (engine->rtp_session) checking = check_engine(engine);
 	} while (switch_channel_ready(session->channel) && checking);