Skip to content
This repository has been archived by the owner on Nov 13, 2022. It is now read-only.

Added the ability to mute outgoing audio #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ To make calls, simply use these functions:
The value for `destination` argument equals to the target SIP user without the host part (e.g. `+441234567890` or `bob`).
The omitted host part is equal to host you’ve defined in `SipProvider` props (e.g. `sip.example.com`).

You may also mute your microphone during calls with the `call.toggleMuteMicrophone()`, `call.muteMicrophone` and `call.unmuteMicrophone` methods.
You can check whether the microphone is used with the `call.microphoneIsMuted` property.

---

The values for `sip.status`, `sip.errorType`, `call.status` and `call.direction` can be imported as constants to make typos easier to detect:
Expand Down
30 changes: 30 additions & 0 deletions src/components/SipProvider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default class SipProvider extends React.Component<
callStatus: CallStatus;
callDirection: CallDirection | null;
callCounterpart: string | null;
callMicrophoneIsMuted: boolean;
rtcSession;
}
> {
Expand Down Expand Up @@ -119,6 +120,7 @@ export default class SipProvider extends React.Component<
callStatus: CALL_STATUS_IDLE,
callDirection: null,
callCounterpart: null,
callMicrophoneIsMuted: false,
};

this.ua = null;
Expand All @@ -137,6 +139,10 @@ export default class SipProvider extends React.Component<
status: this.state.callStatus,
direction: this.state.callDirection,
counterpart: this.state.callCounterpart,
microphoneIsMuted: this.state.callMicrophoneIsMuted,
muteMicrophone: this.callMuteMicrophone,
unmuteMicrophone: this.callUnmuteMicrophone,
toggleMuteMicrophone: this.callToggleMuteMicrophone,
},
registerSip: this.registerSip,
unregisterSip: this.unregisterSip,
Expand Down Expand Up @@ -436,6 +442,7 @@ export default class SipProvider extends React.Component<
callStatus: CALL_STATUS_STARTING,
callCounterpart:
foundUri.substring(0, delimiterPosition) || foundUri,
callMicrophoneIsMuted: rtcSession.isMuted().audio,
});
} else if (originator === "remote") {
const foundUri = rtcRequest.from.toString();
Expand All @@ -445,6 +452,7 @@ export default class SipProvider extends React.Component<
callStatus: CALL_STATUS_STARTING,
callCounterpart:
foundUri.substring(0, delimiterPosition) || foundUri,
callMicrophoneIsMuted: rtcSession.isMuted().audio,
});
}

Expand All @@ -471,6 +479,7 @@ export default class SipProvider extends React.Component<
callStatus: CALL_STATUS_IDLE,
callDirection: null,
callCounterpart: null,
callMicrophoneIsMuted: false,
});
});

Expand All @@ -484,6 +493,7 @@ export default class SipProvider extends React.Component<
callStatus: CALL_STATUS_IDLE,
callDirection: null,
callCounterpart: null,
callMicrophoneIsMuted: false,
});
});

Expand Down Expand Up @@ -546,4 +556,24 @@ export default class SipProvider extends React.Component<
public render() {
return this.props.children;
}

private callMuteMicrophone = () => {
if (this.state.rtcSession && !this.state.callMicrophoneIsMuted) {
this.state.rtcSession.mute({ audio: true, video: false });
this.setState({ callMicrophoneIsMuted: true });
}
};

private callUnmuteMicrophone = () => {
if (this.state.rtcSession && this.state.callMicrophoneIsMuted) {
this.state.rtcSession.unmute({ audio: true, video: false });
this.setState({ callMicrophoneIsMuted: false });
}
};

private callToggleMuteMicrophone = () => {
return this.state.callMicrophoneIsMuted
? this.callUnmuteMicrophone()
: this.callMuteMicrophone();
};
}
4 changes: 4 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,8 @@ export const callPropType = PropTypes.shape({
status: PropTypes.string,
direction: PropTypes.string,
counterpart: PropTypes.string,
microphoneIsMuted: PropTypes.bool,
muteMicrophone: PropTypes.func,
unmuteMicrophone: PropTypes.func,
toggleMuteMicrophone: PropTypes.func,
});