Discussions
Agent Stream in Flutter only output audio, no video
3 months ago by profresearchh
Basically this is my webrtc implementation using flutter_webrtc and the connection seems to be already connected. But only the audio got outputted meanwhile video doesn't. It's been weeks i'm stuck at this problem, it there a flutter sample code that i can reference ? or i implement it incorrectly
_setupPeerConnection() async {
try {
_remoteRTCVideoRenderer.initialize();
final learnaiRepository = di.locator<LearnaiRepository>();
final session = await learnaiRepository.createNewStream();
_rtcPeerConnection = await createPeerConnection({
'iceServers': [
{
'urls': session.iceServers.first.urls,
'username': session.iceServers.first.username,
'credential': session.iceServers.first.credential,
}
],
});
_rtcPeerConnection!.onIceGatheringState = (event) async {
print("onIceGatheringStateChange: ${event}");
};
_rtcPeerConnection!.onIceCandidate = (event) async {
print(
"onIceCandidate: ${event.candidate} ${event.sdpMLineIndex} ${event.sdpMid}");
_rtcPeerConnection!.addCandidate(event);
if (event.candidate != null &&
event.sdpMLineIndex != null &&
event.sdpMid != null) {
await learnaiRepository.submitNetworkInfo(
event.candidate!,
event.sdpMid!,
event.sdpMLineIndex!,
session.sessionId,
session.id,
);
}
};
_rtcPeerConnection!.onIceConnectionState = (event) async {
print("onIceConnectionStateChange: ${event}");
if (event == RTCIceConnectionState.RTCIceConnectionStateConnected ||
event == RTCIceConnectionState.RTCIceConnectionStateCompleted) {
_chatBotBloc.add(
ChatbotEvent.avatarInitialized(session.sessionId, session.id));
} else if (event ==
RTCIceConnectionState.RTCIceConnectionStateDisconnected ||
event == RTCIceConnectionState.RTCIceConnectionStateFailed) {
_chatBotBloc.add(const ChatbotEvent.disconnectAvatar());
}
};
_rtcPeerConnection!.onConnectionState = (event) {
print("onConnectionStateChange: ${event}");
if (event == RTCIceConnectionState.RTCIceConnectionStateDisconnected ||
event == RTCIceConnectionState.RTCIceConnectionStateFailed) {
_chatBotBloc.add(const ChatbotEvent.disconnectAvatar());
}
};
_rtcPeerConnection!.onSignalingState = (event) {
print("onSignalingStateChange: ${event}");
};
await _rtcPeerConnection!.setRemoteDescription(
RTCSessionDescription(session.offer.sdp, session.offer.type),
);
final sessionClientAnswer = await _rtcPeerConnection!.createAnswer();
await _rtcPeerConnection!.setLocalDescription(sessionClientAnswer);
if (sessionClientAnswer.sdp != null && sessionClientAnswer.type != null) {
await learnaiRepository.sendSDPAnswer(
session.id,
sessionClientAnswer.sdp!,
sessionClientAnswer.type!,
session.sessionId);
} else {
throw Exception('Something went wrong went creating avatar');
}
_rtcPeerConnection!.onTrack = (event) {
print("onTrack: ${event.track}");
_remoteRTCVideoRenderer.srcObject = event.streams[0];
setState(() {});
};
} catch (e, s) {
print(e);
print(s);
}
}