6. What is WebRTC?
WebRTC (Web Real-Time Communication) is an API
definition drafted by the World Wide Web Consortium
(W3C) that supports browser-to-browser applications
for voice calling, video chat, and P2P file sharing
without the need of either internal or external plugins.
29. The Protocol
WebSocket based, JSON payload
Users enter the roulette when they
connect over WebSocket
Session is negotiated / established
No end message, just disconnect the
WebSocket
45. # request offer
offer_request = YoPayload(yo='yo')
peerA.write(json.dumps(offer_request.to_struct()))
!
# get offer
data = yield from peerA.read(timeout=READ_TIMEOUT)
if not data:
yield from close_connections()
return
!
offer = parse(data)
if offer is None or offer.jsep is None or offer.jsep.type != 'offer':
log.warning('Invalid offer received')
yield from close_connections()
return
!
# send offer
peerB.write(json.dumps(offer.to_struct()))
46. # wait for answer
data = yield from peerB.read(timeout=READ_TIMEOUT)
if not data:
yield from close_connections()
return
!
answer = parse(data)
if answer is None or answer.jsep is None or answer.jsep.type != 'answer':
log.warning('Invalid answer received')
yield from close_connections()
return
!
# dispatch answer
peerA.write(json.dumps(answer.to_struct()))
47. # wait for candidates / end
while True:
peer_a_read = asyncio.async(peerA.read())
peer_a_read.other_peer = peerB
peer_b_read = asyncio.async(peerB.read())
peer_b_read.other_peer = peerA
done, pending = yield from asyncio.wait([peer_a_read, peer_b_read],
return_when=asyncio.FIRST_COMPLETED)
for task in pending:
task.cancel()
for task in done:
data = task.result()
if not data:
break
# all we can get at this point is trickled ICE candidates
candidate = parse(data)
if candidate is None or candidate.candidate is None:
log.warning('Invalid candidate received!')
break
task.other_peer.write(json.dumps(candidate.to_struct()))
else:
continue
break
# close connections
yield from close_connections()