I'm trying to create WebRTC datachannel with flutter_webrtc package but have some Issues.
What I'm doing:
I'm creating RTCPeerConnection? _peerConnection;
. I have working video and audio stream.
But then I'm trying to add dataChannels
.
On Offer side I do:
_createDataChanel() async {
final chanInit = RTCDataChannelInit()
..negotiated = true
..maxRetransmits = 30;
_dataChannel = await _peerConnection!.createDataChannel('chat', chanInit);
_dataChannel!.onMessage = (data) {
print(data);
};
}
On Answer side I do:
_subscribeDataChanel() {
_peerConnection!.onDataChannel = (channel) {
_dataChannel = channel;
_dataChannel!.onMessage = (data) {
print(data);
};
};
}
And nothing happens. It will never send any events to onDataChannel
. May be I do something wrong?
UPD: I made some experiments and figure how to solve.
Solution: We should firstly create DataChannel and then create Offer or Answer. So in code it will be something like:
Offer side:
createOffer(Connection connection) async {
await _createDataChanel();
final offerDesc = await _peerConnection!.createOffer(offerSdpConstraints);
await _peerConnection!.setLocalDescription(offerDesc);
// send to signaling server and so on...
}
Answer side:
createAnswer(Connection connection) async {
_subscribeDataChanel();
// get offer from signaling server and so on ...
}