forked from KatherineZhouTY/kz_hackathon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server copy.py
56 lines (43 loc) · 1.36 KB
/
server copy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from aiortc.contrib.signaling import TcpSocketSignaling, object_to_string
from aiortc import MediaStreamTrack, RTCPeerConnection, VideoStreamTrack
from PIL import Image
import asyncio
import socket
import os
from ball import Ball
PORT = 5000
SERVER = socket.gethostbyname(socket.gethostname())
class FramesTransportTrack(VideoStreamTrack):
kind = "video"
def __init__(self):
super().__init__()
self.counter = 0
self.ball = Ball()
self.frams = self.ball.generate_frames()
async def recv(self):
pts, time_base = await self.next_timestamp()
frame = self.frames[self.counter % 50]
frame.pts = pts
frame.time_base = time_base
self.counter += 1
return frame
async def run(signaling, pc):
await signaling.connect()
print('server connected')
mst = FramesTransportTrack()
pc.addTrack(mst)
desc = await pc.createOffer() # RTCSessionDescription
await pc.setLocalDescription(desc)
await signaling.send(desc)
# create an aiortc offer and send
print('offer sent')
print(mst.recv())
answer = await signaling.receive()
await pc.setRemoteDescription(answer)
print("answer received")
await signaling.close()
# # receive answer
signaling = TcpSocketSignaling(SERVER, PORT)
pc = RTCPeerConnection()
coro = run(signaling, pc)
asyncio.run(coro)