-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstreamer.py
173 lines (138 loc) · 5.79 KB
/
streamer.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
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
import logging
import asyncio
import argparse
import os
import json
from aiohttp import web
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
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
Gst.init(None)
class WebRTCClient:
def __init__(self, pipeline_description, conn):
self.conn = conn
self.loop = asyncio.get_running_loop()
self.pipeline = Gst.parse_launch(pipeline_description)
self.webrtc = self.pipeline.get_by_name('webrtcbin')
self.webrtc.connect('on-negotiation-needed', self.on_negotiation_needed)
self.webrtc.connect('on-ice-candidate', self.send_ice_candidate_message)
self.bus = self.pipeline.get_bus()
self.bus.add_signal_watch()
self.bus.connect('message', self.on_message)
def on_message(self, bus, message):
if message.type == Gst.MessageType.EOS:
self.pipeline.seek_simple(Gst.Format.TIME, Gst.SeekFlags.FLUSH, 0)
elif message.type == Gst.MessageType.ERROR:
err, debug_info = message.parse_error()
logging.info(f"Error received from element {message.src.get_name()}: {err.message}")
logging.info(f"Debugging information: {debug_info}")
def start(self):
logging.info("Starting pipeline")
ret = self.pipeline.set_state(Gst.State.PLAYING)
if ret == Gst.StateChangeReturn.FAILURE:
logging.error("Unable to set the pipeline to the playing state")
else:
logging.info("Pipeline is now playing")
def play(self):
logging.info("Playing pipeline")
self.pipeline.set_state(Gst.State.PLAYING)
def pause(self):
logging.info("Pausing pipeline")
self.pipeline.set_state(Gst.State.PAUSED)
def seek(self, time_sec):
logging.info(f"Seeking to {time_sec} seconds")
self.pipeline.seek_simple(Gst.Format.TIME, Gst.SeekFlags.FLUSH, time_sec * Gst.SECOND)
def send_sdp_offer(self, offer):
text = offer.sdp.as_text()
logging.info(f"sending SDP offer")
msg = json.dumps({'event': 'offer', 'data':
{
'type': 'offer',
'sdp': text
}
})
loop = asyncio.new_event_loop()
loop.run_until_complete(self.conn.send_str(msg))
def on_offer_created(self, promise,_, __):
promise.wait()
reply = promise.get_reply()
offer = reply.get_value('offer')
promise = Gst.Promise.new()
self.webrtc.emit('set-local-description', offer, promise)
logging.info("set local description")
promise.interrupt()
self.send_sdp_offer(offer)
def on_negotiation_needed(self, element):
promise = Gst.Promise.new_with_change_func(self.on_offer_created, None, None)
logging.info("Creating offer")
element.emit('create-offer', None, promise)
def send_ice_candidate_message(self, _, mlineindex, candidate):
icemsg = json.dumps({'event': 'candidate', 'data':
{
'candidate': candidate,
'sdpMLineIndex': mlineindex}
})
loop = asyncio.new_event_loop()
loop.run_until_complete(self.conn.send_str(icemsg))
def create_pipeline(container_path):
pipeline_str = f"""
filesrc location={container_path} ! qtdemux name=demux
webrtcbin name=webrtcbin bundle-policy=max-bundle stun-server=stun://stun.l.google.com:19302
demux.video_0 ! h264parse ! rtph264pay config-interval=-1 ! queue ! application/x-rtp,media=video,encoding-name=H264,payload=96 ! webrtcbin.
"""
return pipeline_str
async def websocket_handler(request):
conn = web.WebSocketResponse()
await conn.prepare(request)
# starting the pipeline
pipeline_str = create_pipeline(container_path)
pipeline = WebRTCClient(pipeline_str, conn)
pipeline.start()
async for msg in conn:
if msg.type == web.WSMsgType.TEXT:
data = json.loads(msg.data)
if data["event"] == "answer":
answer = data["data"]
sdp = answer["sdp"]
answer_type = answer["type"]
assert answer_type == "answer"
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()
pipeline.webrtc.emit('set-remote-description', answer, promise)
promise.interrupt()
elif data["event"] == "candidate":
candidate = data["data"]
candidate_val = candidate['candidate']
pipeline.webrtc.emit('add-ice-candidate', candidate["sdpMLineIndex"], candidate_val)
elif data["event"] == "play":
pipeline.play()
elif data["event"] == "pause":
pipeline.pause()
elif data["event"] == "seek":
seek_time = int(data["data"])
pipeline.seek(seek_time)
return conn
async def index(request):
return web.FileResponse("index.html")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='WebRTC streaming server')
parser.add_argument('--video', help='Path to the media container')
args = parser.parse_args()
container_path = args.video
if not container_path:
raise Exception("Container path must be specified")
app = web.Application()
app.add_routes([web.get("/", index), web.get("/ws", websocket_handler)])
logging.info("Starting pipeline")
web.run_app(app, port=8080)