Skip to content

Commit

Permalink
Mute self mic
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeny-nadymov committed Apr 5, 2021
1 parent ea0cd0c commit cd7b175
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"homepage": "https://evgeny-nadymov.github.io/telegram-react",
"name": "telegram_react",
"version": "0.0.979",
"version": "0.0.983",
"private": true,
"dependencies": {
"tdweb": "^1.7.2",
Expand Down
42 changes: 28 additions & 14 deletions src/Calls/P2P/P2PSdpBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { ChromeP2PSdpBuilder } from './ChromeP2PSdpBuilder';
import { FirefoxP2PSdpBuilder } from './FirefoxP2PSdpBuilder';
import { SafariP2PSdpBuilder } from './SafariP2PSdpBuilder';
import { TG_CALLS_SDP_STRING } from '../../Stores/CallStore';

export function p2pParseCandidate(candidate) {
if (!candidate) {
Expand All @@ -17,10 +18,12 @@ export function p2pParseCandidate(candidate) {
return null;
}

const sdpString = candidate;
candidate = candidate.substr('candidate:'.length);

const [ foundation, component, protocol, priority, ip, port, ...other ] = candidate.split(' ');
const c = {
sdpString,
foundation,
component,
protocol,
Expand Down Expand Up @@ -169,11 +172,13 @@ export function p2pParseSdp(sdp) {
extmap.push({ id: parseInt(id), uri });
} else if (line.startsWith('a=fmtp:')) {
const [ id, str ] = line.substr('a=fmtp:'.length).split(' ');
const obj = { };
const arr = str.split(';').map(x => {
const [ key, value ] = x.split('=');
obj[key] = value;
return { [key]: value };
});
fmtp.set(parseInt(id), arr);
fmtp.set(parseInt(id), obj);
} else if (line.startsWith('a=rtcp-fb:')) {
const [ id, type = '', subtype = '' ] = line.substr('a=rtcp-fb:'.length).split(' ');
if (rtcpFb.has(parseInt(id))) {
Expand Down Expand Up @@ -202,15 +207,15 @@ export function p2pParseSdp(sdp) {

const ssrc = lookup('a=ssrc:', false, mediaIndex, nextMediaIndex);
if (ssrc) {
media.ssrc = parseInt(ssrc.split(' ')[0]);
media.ssrc = ssrc.split(' ')[0];
}

const ssrcGroup = lookup('a=ssrc-group:', false, mediaIndex, nextMediaIndex);
if (ssrcGroup) {
const [ semantics, ...ssrcs ] = ssrcGroup.split(' ');
media.ssrcGroups = [{
semantics,
ssrcs: ssrcs.map(x => parseInt(x))
ssrcs
}]
}

Expand Down Expand Up @@ -256,7 +261,7 @@ a=extmap:${id} ${uri}`;

export function addPayloadTypes(types) {
let sdp = '';

console.log('[SDP] addPayloadTypes', types);
for (let i = 0; i < types.length; i++) {
const type = types[i];
const { id, name, clockrate, channels, feedbackTypes, parameters } = type;
Expand All @@ -270,12 +275,10 @@ a=rtcp-fb:${id} ${[type, subtype].join(' ')}`;
});
}
if (parameters) {
const fmtp = parameters.reduce((arr, x) => {
Object.getOwnPropertyNames(x).forEach(pName => {
arr.push(`${pName}=${x[pName]}`);
});
return arr;
}, []);
const fmtp = [];
Object.getOwnPropertyNames(parameters).forEach(pName => {
fmtp.push(`${pName}=${parameters[pName]}`);
});

sdp += `
a=fmtp:${id} ${fmtp.join(';')}`;
Expand Down Expand Up @@ -327,7 +330,18 @@ export class P2PSdpBuilder {
static generateCandidate(info) {
if (!info) return null;

const { sdpMLineIndex, sdpMid, foundation, component, protocol, priority, address, type, relAddress, generation, tcpType, networkId, networkCost, username } = info;
const { sdpString, sdpMLineIndex, sdpMid, foundation, component, protocol, priority, address, type, relAddress, generation, tcpType, networkId, networkCost, username } = info;
if (TG_CALLS_SDP_STRING) {
if (sdpString) {
return {
candidate: sdpString,
sdpMLineIndex,
sdpMid
};
}
}
throw 'no sdpString';

let candidate = `candidate:${foundation} ${component} ${protocol} ${priority} ${address.ip} ${address.port}`;
const attrs = []
if (type) {
Expand All @@ -337,12 +351,12 @@ export class P2PSdpBuilder {
attrs.push(`raddr ${relAddress.ip}`);
attrs.push(`rport ${relAddress.port}`);
}
if (generation) {
attrs.push(`generation ${generation}`);
}
if (tcpType) {
attrs.push(`tcptype ${tcpType}`);
}
if (generation) {
attrs.push(`generation ${generation}`);
}
if (username) {
attrs.push(`ufrag ${username}`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Calls/CallPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ class CallPanel extends React.Component {
</div>
<div className='call-panel-content scrollbars-hidden' onDoubleClick={this.handleFullScreen}>
<video id='call-output-video' autoPlay={true} muted={false}/>
<video id='call-input-video' autoPlay={true} muted={false}/>
<video id='call-input-video' autoPlay={true} muted={true}/>
</div>
{ !otherAudioEnabled && (
<div className='call-panel-microphone-hint'>
Expand Down
66 changes: 49 additions & 17 deletions src/Stores/CallStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const DATACHANNEL = true;
const TG_CALLS = true;
const TG_CALLS_MEDIA_STATE = true;
const TG_CALLS_SDP = true;
export const TG_CALLS_SDP_STRING = true;

export function LOG_CALL(str, ...data) {
// return;
Expand Down Expand Up @@ -1813,10 +1814,10 @@ class CallStore extends EventEmitter {
const call = this.p2pGet(callId)
if (!call) return;

// const { is_outgoing } = call;
const { is_outgoing } = call;
// if (!is_outgoing) return;

const offer = await connection.createOffer({
let offer = await connection.createOffer({
offerToReceiveAudio: true,
offerToReceiveVideo: true
});
Expand All @@ -1826,14 +1827,16 @@ class CallStore extends EventEmitter {
if (TG_CALLS_SDP) {
const initialSetup = p2pParseSdp(offer.sdp);
initialSetup['@type'] = 'InitialSetup';

this.p2pSendInitialSetup(callId, initialSetup);
if (currentCall.answer) {
await connection.setRemoteDescription(currentCall.answer);
currentCall.answer = null;
if (currentCall.candidates) {
currentCall.candidates.forEach(x => {
LOG_P2P_CALL('[candidate] add postpone', x);
connection.addIceCandidate(x);
this.p2pAddIceCandidate(connection, x);
// LOG_P2P_CALL('[candidate] add postpone', x);
// connection.addIceCandidate(x);
});
currentCall.candidates = [];
}
Expand All @@ -1843,6 +1846,17 @@ class CallStore extends EventEmitter {
}
};

async p2pAddIceCandidate(connection, candidate) {
LOG_P2P_CALL('[candidate] start', candidate);
try {
// if (!candidate.address) return;
await connection.addIceCandidate(candidate);
LOG_P2P_CALL('[candidate] add', candidate);
} catch (e) {
LOG_P2P_CALL('[candidate] error', candidate, e);
}
}

p2pAppendInputStream(inputStream) {
LOG_P2P_CALL('p2pAppendInputStream start', inputStream);
const { currentCall } = this;
Expand Down Expand Up @@ -1957,8 +1971,9 @@ class CallStore extends EventEmitter {
await connection.setRemoteDescription(description);
if (currentCall.candidates) {
currentCall.candidates.forEach(x => {
LOG_P2P_CALL('[candidate] add postpone', x);
connection.addIceCandidate(x);
this.p2pAddIceCandidate(connection, x);
// LOG_P2P_CALL('[candidate] add postpone', x);
// connection.addIceCandidate(x);
});
currentCall.candidates = [];
}
Expand Down Expand Up @@ -1991,8 +2006,9 @@ class CallStore extends EventEmitter {
LOG_P2P_CALL('[candidate] postpone', iceCandidate);
currentCall.candidates.push(iceCandidate);
} else {
LOG_P2P_CALL('[candidate] add', iceCandidate);
await connection.addIceCandidate(iceCandidate);
await this.p2pAddIceCandidate(connection, iceCandidate);
// LOG_P2P_CALL('[candidate] add', iceCandidate);
// await connection.addIceCandidate(iceCandidate);
}
}
break;
Expand All @@ -2004,10 +2020,12 @@ class CallStore extends EventEmitter {
const { fingerprints } = description;
if (fingerprints) {
fingerprints.forEach(x => {
if (is_outgoing) {
x.setup = 'active';
} else {
x.setup = 'passive';
if (x.setup === 'actpass') {
if (is_outgoing) {
x.setup = 'active';
} else {
x.setup = 'passive';
}
}
})
}
Expand All @@ -2022,8 +2040,9 @@ class CallStore extends EventEmitter {
await connection.setRemoteDescription(description);
if (currentCall.candidates) {
currentCall.candidates.forEach(x => {
LOG_P2P_CALL('[candidate] add postpone', x);
connection.addIceCandidate(x);
this.p2pAddIceCandidate(connection, x);
// LOG_P2P_CALL('[candidate] add postpone', x);
// connection.addIceCandidate(x);
});
currentCall.candidates = [];
}
Expand All @@ -2050,10 +2069,22 @@ class CallStore extends EventEmitter {
let candidate = data;
if (UNIFY_CANDIDATE) {
data.candidates.forEach(x => {
// if (x.type === 'local') {
// x.type = 'host';
// } else if (x.type === 'relay') {
// x.component = 1;
// x.relAddress = {
// ip: '0.0.0.0',
// port: 0
// }
// }

candidate = P2PSdpBuilder.generateCandidate(x);
candidate.sdpMLineIndex = 0;

candidates.push(candidate);
// if (x.type === 'relay') {
candidates.push(candidate);
// }
});
}
if (candidates.length > 0) {
Expand All @@ -2064,8 +2095,9 @@ class CallStore extends EventEmitter {
LOG_P2P_CALL('[candidate] postpone', iceCandidate);
currentCall.candidates.push(iceCandidate);
} else {
LOG_P2P_CALL('[candidate] add', iceCandidate);
await connection.addIceCandidate(iceCandidate);
await this.p2pAddIceCandidate(connection, iceCandidate);
// LOG_P2P_CALL('[candidate] add', iceCandidate);
// await connection.addIceCandidate(iceCandidate);
}
});
}
Expand Down

0 comments on commit cd7b175

Please sign in to comment.