I'm using django-channels
and aiortc
, I want to create server to peer
connection as WebRTC
.
In my case, if client send the new-peer
action, then server send the offer.
And after client receive the offer, client send the answer as new-answer
action.
But server can't remember peer
, so can't setRemoteDescription
.
Of course I know server don't remember, but how do I deal with situations where server need to remember instances like mine?
I also looked for session variables, but it seems that only simple values can be stored, and it doesn't seem to have anything to do with the instance.
Please provide helpful documents or solutions related to this.
consumers.receive method:
async def receive(self, text_data):
receive_dict = json.loads(text_data)
message = receive_dict['message']
action = receive_dict['action']
if (action == 'new-peer'):
# Create Offer
peer, offer = await create_offer()
receive_dict['message']['sdp'] = offer
receive_dict['action'] = 'new-offer'
receive_dict['message']['receiver_channel_name'] = self.channel_name
await self.channel_layer.send(
self.channel_name,
{
'type': 'send.sdp',
'receive_dict': receive_dict
}
)
return None
elif (action == 'new-answer'):
await peer.setRemoteDescription(receive_dict['message']['sdp'])
receiver_channel_name = receive_dict['message']['receiver_channel_name']
receive_dict['message']['receiver_channel_name'] = self.channel_name
await self.channel_layer.send(
receiver_channel_name,
{
'type': 'send.sdp',
'receive_dict': receive_dict,
}
)
return None
else:
raise ValueError('Unexpected action value');
create_offer function:
async def create_offer():
pc = RTCPeerConnection(RTCConfiguration(iceServers))
rtsp_video = MediaPlayer(rtsp_test_url)
relay = MediaRelay()
pc.addTrack(relay.subscribe(rtsp_video.video))
@pc.on("connectionstatechange")
async def on_connectionstatechange():
print("Connection state is %s", pc.connectionState)
if pc.connectionState == "failed":
await pc.close()
await pc.setLocalDescription(await pc.createOffer())
return pc, object_to_string(pc.localDescription)
Solved: django session support saving instance to their session.