-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
63 lines (47 loc) · 1.55 KB
/
server.py
File metadata and controls
63 lines (47 loc) · 1.55 KB
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
57
58
59
60
61
62
63
from aiortc.contrib.signaling import TcpSocketSignaling, object_to_string, BYE
from aiortc import MediaStreamTrack, RTCPeerConnection, VideoStreamTrack, RTCSessionDescription, RTCIceCandidate
from PIL import Image
import asyncio
import socket
import os
from ball import Ball
PORT = 5000
SERVER = socket.gethostbyname(socket.gethostname())
class FramesTransportTrack(MediaStreamTrack):
kind = "video"
def __init__(self):
super().__init__()
self.counter = 0
self.ball = Ball()
#self.frames = []
self.frames = self.ball.generate_frames()
async def recv(self):
pts, time_base = await self.next_timestamp()
frame = self.frames[self.counter % 10]
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)
#pc.addTransceiver("video", direction='sendonly')
desc = await pc.createOffer() # RTCSessionDescription
await pc.setLocalDescription(desc)
await signaling.send(desc)
# create an aiortc offer and send
print('offer sent')
# consume signaling
answer = await signaling.receive()
await pc.setRemoteDescription(answer)
print("answer received")
for i in range(10):
await mst.recv()
await signaling.close()
# # receive answer
signaling = TcpSocketSignaling(SERVER, PORT)
pc = RTCPeerConnection()
coro = run(signaling, pc)
asyncio.run(coro)