forked from steveseguin/raspberry_ninja
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
270 lines (234 loc) · 13 KB
/
server.py
File metadata and controls
270 lines (234 loc) · 13 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import random
import ssl
import websockets
import asyncio
import os
import sys
import json
import argparse
import time
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst
gi.require_version('GstWebRTC', '1.0')
from gi.repository import GstWebRTC
gi.require_version('GstSdp', '1.0')
from gi.repository import GstSdp
class WebRTCClient:
def __init__(self, peer_id, server):
self.conn = None
self.pipe = None
self.webrtc = None
self.UUID = None
self.session = None
self.peer_id = peer_id
self.server = server ### To avoid causing issues for production; streams can be view at https://backup.obs.ninja as a result.
async def connect(self):
sslctx = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
self.conn = await websockets.connect(self.server, ssl=sslctx)
msg = json.dumps({"request":"seed","streamID":self.peer_id})
await self.conn.send(msg)
def on_offer_created(self, promise, _, __): ## This is all based on the legacy API of OBS.Ninja; gstreamer-1.19 lacks support for the newer API.
print("ON OFFER CREATED")
promise.wait()
reply = promise.get_reply()
offer = reply.get_value('offer')
promise = Gst.Promise.new()
self.webrtc.emit('set-local-description', offer, promise)
promise.interrupt()
print("SEND SDP OFFER")
text = offer.sdp.as_text()
msg = json.dumps({'description': {'type': 'offer', 'sdp': text}, 'UUID': self.UUID, 'session': self.session, 'streamID':self.peer_id})
print(msg)
loop = asyncio.new_event_loop()
loop.run_until_complete(self.conn.send(msg))
print("SENT MESSAGE")
def on_negotiation_needed(self, element):
print("ON NEGO NEEDED")
promise = Gst.Promise.new_with_change_func(self.on_offer_created, element, None)
element.emit('create-offer', None, promise)
def create_answer(self):
promise = Gst.Promise.new_with_change_func(self.on_answer_created, self.webrtc, None)
self.webrtc.emit('create-answer', None, promise)
def on_answer_created(self, promise, _, __):
print("ON ANSWER CREATED")
promise.wait()
reply = promise.get_reply()
answer = reply.get_value('answer')
promise = Gst.Promise.new()
self.webrtc.emit('set-local-description', answer, promise)
promise.interrupt()
print("SEND SDP ANSWER")
text = answer.sdp.as_text()
msg = json.dumps({'description': {'type': 'answer', 'sdp': text, 'UUID': self.UUID, 'session': self.session}})
loop = asyncio.new_event_loop()
loop.run_until_complete(self.conn.send(msg))
print("SENT MESSAGE")
def send_ice_candidate_message(self, _, mlineindex, candidate):
print("SEND ICE")
icemsg = json.dumps({'candidates': [{'candidate': candidate, 'sdpMLineIndex': mlineindex}], 'session':self.session, 'type':'local', 'UUID':self.UUID})
loop = asyncio.new_event_loop()
loop.run_until_complete(self.conn.send(icemsg))
def on_incoming_decodebin_stream(self, _, pad): # If daring to capture inbound video; support not assured at this point.
print("ON INCOMING")
if not pad.has_current_caps():
print (pad, 'has no caps, ignoring')
return
caps = pad.get_current_caps()
name = caps.to_string()
if name.startswith('video'):
q = Gst.ElementFactory.make('queue')
conv = Gst.ElementFactory.make('videoconvert')
# sink = Gst.ElementFactory.make('filesink', "fsink") # record inbound stream to file
sink = Gst.ElementFactory.make('autovideosink')
# sink.set_property("location", str(time.time())+'.mkv')
self.pipe.add(q)
self.pipe.add(conv)
self.pipe.add(sink)
self.pipe.sync_children_states()
pad.link(q.get_static_pad('sink'))
q.link(conv)
conv.link(sink)
elif name.startswith('audio'):
q = Gst.ElementFactory.make('queue')
conv = Gst.ElementFactory.make('audioconvert')
resample = Gst.ElementFactory.make('audioresample')
sink = Gst.ElementFactory.make('autoaudiosink')
self.pipe.add(q)
self.pipe.add(conv)
self.pipe.add(resample)
self.pipe.add(sink)
self.pipe.sync_children_states()
pad.link(q.get_static_pad('sink'))
q.link(conv)
conv.link(resample)
resample.link(sink)
def on_incoming_stream(self, _, pad):
print("ON INCOMING STREAM")
try:
if Gst.PadDirection.SRC != pad.direction:
return
except:
return
print("INCOMING STREAM")
decodebin = Gst.ElementFactory.make('decodebin')
decodebin.connect('pad-added', self.on_incoming_decodebin_stream)
self.pipe.add(decodebin)
decodebin.sync_state_with_parent()
self.webrtc.link(decodebin)
def start_pipeline(self):
print("START PIPE")
if self.pipe:
self.pipe.set_state(Gst.State.NULL)
self.pipe = Gst.parse_launch(PIPELINE_DESC)
self.webrtc = self.pipe.get_by_name('sendrecv')
self.webrtc.connect('on-negotiation-needed', self.on_negotiation_needed)
self.webrtc.connect('on-ice-candidate', self.send_ice_candidate_message)
self.webrtc.connect('pad-added', self.on_incoming_stream)
self.pipe.set_state(Gst.State.PLAYING)
async def handle_sdp(self, msg):
print("HANDLE SDP")
assert (self.webrtc)
if 'sdp' in msg:
msg = msg
assert(msg['type'] == 'answer')
sdp = msg['sdp']
print ('Received answer:\n%s' % sdp)
res, sdpmsg = GstSdp.SDPMessage.new()
GstSdp.sdp_message_parse_buffer(bytes(sdp.encode()), sdpmsg)
answer = GstWebRTC.WebRTCSessionDescription.new(GstWebRTC.WebRTCSDPType.ANSWER, sdpmsg)
promise = Gst.Promise.new()
self.webrtc.emit('set-remote-description', answer, promise)
promise.interrupt()
elif 'candidate' in msg:
candidate = msg['candidate']
sdpmlineindex = msg['sdpMLineIndex']
self.webrtc.emit('add-ice-candidate', sdpmlineindex, candidate)
async def handle_offer(self, msg):
print("HANDLE SDP OFFER")
assert (self.webrtc)
if 'sdp' in msg:
msg = msg
assert(msg['type'] == 'offer')
sdp = msg['sdp']
print ('Received offer:\n%s' % sdp)
res, sdpmsg = GstSdp.SDPMessage.new()
GstSdp.sdp_message_parse_buffer(bytes(sdp.encode()), sdpmsg)
offer = GstWebRTC.WebRTCSessionDescription.new(GstWebRTC.WebRTCSDPType.OFFER, sdpmsg)
promise = Gst.Promise.new()
self.webrtc.emit('set-remote-description', offer, promise)
promise.interrupt()
self.create_answer()
async def loop(self):
assert self.conn
print("WSS CONNECTED")
async for message in self.conn:
msg = json.loads(message)
print(msg)
if 'UUID' in msg:
self.UUID = msg['UUID']
if 'session' in msg:
self.session = msg['session']
if 'description' in msg:
msg = msg['description']
if 'type' in msg:
if msg['type'] == "offer":
self.start_pipeline()
await self.handle_offer(msg)
elif msg['type'] == "answer":
await self.handle_sdp(msg)
elif 'candidates' in msg:
for ice in msg['candidates']:
await self.handle_sdp(ice)
elif 'request' in msg:
if 'offerSDP' in msg['request']:
self.start_pipeline()
else:
print (message)
# return 1 ## disconnects on bad message
return 0
def check_plugins():
needed = ["opus", "vpx", "nice", "webrtc", "dtls", "srtp", "rtp", ## vpx probably isn't needed
"rtpmanager", "videotestsrc", "audiotestsrc"]
missing = list(filter(lambda p: Gst.Registry.get().find_plugin(p) is None, needed))
if len(missing):
print('Missing gstreamer plugins:', missing)
return False
return True
if __name__=='__main__':
Gst.init(None)
if not check_plugins():
sys.exit(1)
parser = argparse.ArgumentParser()
parser.add_argument('--streamid', help='Stream ID of the peer to connect to')
parser.add_argument('--server', help='Handshake server to use, eg: "wss://backupapi.obs.ninja:443"')
parser.add_argument('--bitrate', help='Sets the video bitrate. This is not adaptive, so packet loss and insufficient bandwidth will cause frame loss')
args = parser.parse_args()
server = args.server or "wss://apibackup.obs.ninja:443"
streamid = args.streamid or str(random.randint(1000000,9999999))
bitrate = args.bitrate or str(4000)
print("\nAvailable options include --streamid, --bitrate, and --server. Default bitrate is 4000 (kbps)")
print("\nYou can view this stream at: https://backup.vdo.ninja/?password=false&view="+streamid);
## RASPBERRY PI camera needed; audio source removed to perserve simplicity. See below for some more pipelines to experiment with
PIPELINE_DESC = "webrtcbin name=sendrecv stun-server=stun://stun4.l.google.com:19302 bundle-policy=max-bundle rpicamsrc bitrate="+bitrate+"000 ! video/x-h264,profile=constrained-baseline,width=1280,height=720,level=3.0 ! queue ! h264parse ! rtph264pay config-interval=-1 ! queue ! application/x-rtp,media=video,encoding-name=H264,payload=96 ! sendrecv. "
## FOR JETSON ## PIPELINE_DESC = "v4l2src device=/dev/video0 io-mode=2 ! image/jpeg,framerate=30/1,width=1920,height=1080 ! jpegparse ! nvjpegdec ! video/x-raw ! nvvidconv ! video/x-raw(memory:NVMM) ! omxh264enc bitrate="+bitrate+"000 ! video/x-h264, stream-format=(string)byte-stream ! h264parse ! rtph264pay config-interval=-1 ! application/x-rtp,media=video,encoding-name=H264,payload=96 ! webrtcbin name=sendrecv "
c = WebRTCClient(streamid, server)
asyncio.get_event_loop().run_until_complete(c.connect())
res = asyncio.get_event_loop().run_until_complete(c.loop())
sys.exit(res)
### pipelines you can use
#PIPELINE_DESC = '''
# webrtcbin name=sendrecv bundle-policy=max-bundle
# videotestsrc ! videoconvert ! queue ! vp8enc deadline=1 ! rtpvp8pay !
# queue ! application/x-rtp,media=video,encoding-name=VP8,payload=97 ! sendrecv.
# audiotestsrc is-live=true wave=red-noise ! audioconvert ! audioresample ! queue ! opusenc ! rtpopuspay !
# queue ! application/x-rtp,media=audio,encoding-name=OPUS,payload=96 ! sendrecv.
# ''' ## A sample of how Audio could work; not bothering to setup it up though.
## This is untested, fopr the nvidia jetson, but could work with the CSI-port built into the Jetson.
PIPELINE_DESC = "nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080, format=(string)NV12, framerate=(fraction)30/1 ! omxh264enc ! video/x-h264, stream-format=(string)byte-stream ! queue ! h264parse ! queue ! rtph264pay config-interval=-1 ! queue ! application/x-rtp,media=video,encoding-name=H264,payload=96 ! webrtcbin name=sendrecv "
## This is a good test pipeline for the Nvidia Jetson; uses a fake source to test with. -- no camera needed
PIPELINE_DESC = "videotestsrc ! video/x-raw, width=(int)1920, height=(int)1080, format=(string)NV12, framerate=(fraction)30/1 ! omxh264enc ! video/x-h264, stream-format=(string)byte-stream ! queue ! h264parse ! queue ! rtph264pay config-interval=-1 ! queue ! application/x-rtp,media=video,encoding-name=H264,payload=96 ! webrtcbin name=sendrecv "
## For Nvidia Jetson; Works with those cheap HDMI to USB 2.0 UVC capture dongle. Assumes just one UVC device is connected.
PIPELINE_DESC = "v4l2src device=/dev/video0 io-mode=2 ! image/jpeg,framerate=30/1,width=1920,height=1080 ! jpegparse ! nvjpegdec ! video/x-raw ! nvvidconv ! video/x-raw(memory:NVMM) ! omxh264enc bitrate=10000000 ! video/x-h264, stream-format=(string)byte-stream ! h264parse ! rtph264pay config-interval=-1 ! application/x-rtp,media=video,encoding-name=H264,payload=96 ! webrtcbin name=sendrecv "
## This pipeline lets you plug a UVC-based camera/HDMI source into the USB of a Raspberry pi and stream it. It expects the camera source to support MJPEG. You can tweak the pipeline as needed for your own purpose. The pipeline needs a lot more tuning.
PIPELINE_DESC = "webrtcbin name=sendrecv v4l2src device=/dev/video0 ! image/jpeg,framerate=30/1,width=1280,height=720 ! jpegparse ! jpegdec ! video/x-raw ! videoconvert ! video/x-raw ! omxh264enc ! video/x-h264 ! h264parse ! rtph264pay config-interval=-1 ! application/x-rtp,media=video,encoding-name=H264,payload=96 ! queue ! sendrecv. "