-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathclient.py
107 lines (87 loc) · 2.93 KB
/
client.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import argparse
import asyncio
import logging
import aiohttp
from aiortc import RTCPeerConnection, RTCSessionDescription
from aiortc.contrib.media import MediaBlackhole, MediaPlayer, MediaRecorder
async def send_offer(
url: str,
offer: RTCSessionDescription,
) -> RTCSessionDescription:
offer_data = {"sdp": offer.sdp, "type": offer.type}
async with aiohttp.ClientSession() as client:
async with client.post(url, json=offer_data) as response:
answer_data = await response.json()
return RTCSessionDescription(
sdp=answer_data["sdp"], type=answer_data["type"]
)
async def run(*, pc, player, recorder, url):
connected = asyncio.Event()
@pc.on("connectionstatechange")
def on_connectionstatechange():
print("Connection state %s" % pc.connectionState)
if pc.connectionState == "connected":
connected.set()
@pc.on("track")
def on_track(track):
print("Receiving %s" % track.kind)
recorder.addTrack(track)
# add local media
if player and player.audio:
pc.addTrack(player.audio)
else:
pc.addTransceiver("audio")
if player and player.video:
pc.addTrack(player.video)
# send offer
await pc.setLocalDescription(await pc.createOffer())
answer = await send_offer(url, pc.localDescription)
# apply answer
await pc.setRemoteDescription(answer)
# wait for connected state
await connected.wait()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="WebRTC Echo Client demo")
parser.add_argument("--play-from", help="Read the media from a file and sent it."),
parser.add_argument("--record-to", help="Write received media to a file."),
parser.add_argument("--verbose", "-v", action="count")
parser.add_argument("url", help="The URL to which the SDP offer is sent."),
args = parser.parse_args()
if args.verbose:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
# create peer connection
pc = RTCPeerConnection()
# create media source
if args.play_from:
player = MediaPlayer(args.play_from)
else:
player = None
# create media sink
if args.record_to:
recorder = MediaRecorder(args.record_to)
else:
recorder = MediaBlackhole()
# run event loop
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(
asyncio.wait_for(
run(
pc=pc,
player=player,
recorder=recorder,
url=args.url,
),
timeout=10,
)
)
if args.play_from or args.record_to:
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
# cleanup
loop.run_until_complete(recorder.stop())
loop.run_until_complete(pc.close())