2014-12-02 07:01:30 -05:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
|
|
|
This is an attempt at bridging matrix clients into a Jitis meet room via Matrix
|
|
|
|
video call. It uses hard-coded xml strings overg XMPP BOSH. It can display one
|
|
|
|
of the streams from the Jitsi bridge until the second lot of SDP comes down and
|
|
|
|
we set the remote SDP at which point the stream ends. Our video never gets to
|
|
|
|
the bridge.
|
|
|
|
|
|
|
|
Requires:
|
2019-06-17 13:21:30 -04:00
|
|
|
npm install jquery jsdom
|
2014-12-02 07:01:30 -05:00
|
|
|
"""
|
|
|
|
import json
|
|
|
|
import subprocess
|
|
|
|
import time
|
|
|
|
|
2020-07-20 16:43:49 -04:00
|
|
|
import gevent
|
|
|
|
import grequests
|
|
|
|
from BeautifulSoup import BeautifulSoup
|
|
|
|
|
|
|
|
ACCESS_TOKEN = ""
|
2014-12-02 07:01:30 -05:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
MATRIXBASE = "https://matrix.org/_matrix/client/api/v1/"
|
|
|
|
MYUSERNAME = "@davetest:matrix.org"
|
2014-12-02 07:01:30 -05:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
HTTPBIND = "https://meet.jit.si/http-bind"
|
|
|
|
# HTTPBIND = 'https://jitsi.vuc.me/http-bind'
|
|
|
|
# ROOMNAME = "matrix"
|
2014-12-02 07:01:30 -05:00
|
|
|
ROOMNAME = "pibble"
|
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
HOST = "guest.jit.si"
|
|
|
|
# HOST="jitsi.vuc.me"
|
2014-12-02 07:01:30 -05:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
TURNSERVER = "turn.guest.jit.si"
|
|
|
|
# TURNSERVER="turn.jitsi.vuc.me"
|
|
|
|
|
|
|
|
ROOMDOMAIN = "meet.jit.si"
|
|
|
|
# ROOMDOMAIN="conference.jitsi.vuc.me"
|
2014-12-02 07:01:30 -05:00
|
|
|
|
|
|
|
|
|
|
|
class TrivialMatrixClient:
|
2014-12-02 09:10:24 -05:00
|
|
|
def __init__(self, access_token):
|
|
|
|
self.token = None
|
|
|
|
self.access_token = access_token
|
|
|
|
|
|
|
|
def getEvent(self):
|
|
|
|
while True:
|
2019-06-20 05:32:02 -04:00
|
|
|
url = (
|
|
|
|
MATRIXBASE
|
|
|
|
+ "events?access_token="
|
|
|
|
+ self.access_token
|
|
|
|
+ "&timeout=60000"
|
|
|
|
)
|
2014-12-02 09:10:24 -05:00
|
|
|
if self.token:
|
2019-06-20 05:32:02 -04:00
|
|
|
url += "&from=" + self.token
|
2014-12-02 09:10:24 -05:00
|
|
|
req = grequests.get(url)
|
|
|
|
resps = grequests.map([req])
|
|
|
|
obj = json.loads(resps[0].content)
|
2019-06-20 05:32:02 -04:00
|
|
|
print("incoming from matrix", obj)
|
|
|
|
if "end" not in obj:
|
2014-12-02 09:10:24 -05:00
|
|
|
continue
|
2019-06-20 05:32:02 -04:00
|
|
|
self.token = obj["end"]
|
|
|
|
if len(obj["chunk"]):
|
|
|
|
return obj["chunk"][0]
|
2014-12-02 09:10:24 -05:00
|
|
|
|
|
|
|
def joinRoom(self, roomId):
|
2019-06-20 05:32:02 -04:00
|
|
|
url = MATRIXBASE + "rooms/" + roomId + "/join?access_token=" + self.access_token
|
2019-06-17 13:21:30 -04:00
|
|
|
print(url)
|
2019-06-20 05:32:02 -04:00
|
|
|
headers = {"Content-Type": "application/json"}
|
|
|
|
req = grequests.post(url, headers=headers, data="{}")
|
2014-12-02 09:10:24 -05:00
|
|
|
resps = grequests.map([req])
|
|
|
|
obj = json.loads(resps[0].content)
|
2019-06-20 05:32:02 -04:00
|
|
|
print("response: ", obj)
|
2014-12-02 09:10:24 -05:00
|
|
|
|
|
|
|
def sendEvent(self, roomId, evType, event):
|
2019-06-20 05:32:02 -04:00
|
|
|
url = (
|
|
|
|
MATRIXBASE
|
|
|
|
+ "rooms/"
|
|
|
|
+ roomId
|
|
|
|
+ "/send/"
|
|
|
|
+ evType
|
|
|
|
+ "?access_token="
|
|
|
|
+ self.access_token
|
|
|
|
)
|
2019-06-17 13:21:30 -04:00
|
|
|
print(url)
|
|
|
|
print(json.dumps(event))
|
2019-06-20 05:32:02 -04:00
|
|
|
headers = {"Content-Type": "application/json"}
|
2014-12-02 09:10:24 -05:00
|
|
|
req = grequests.post(url, headers=headers, data=json.dumps(event))
|
|
|
|
resps = grequests.map([req])
|
|
|
|
obj = json.loads(resps[0].content)
|
2019-06-20 05:32:02 -04:00
|
|
|
print("response: ", obj)
|
2014-12-02 07:01:30 -05:00
|
|
|
|
|
|
|
|
|
|
|
xmppClients = {}
|
|
|
|
|
|
|
|
|
|
|
|
def matrixLoop():
|
2014-12-02 09:10:24 -05:00
|
|
|
while True:
|
|
|
|
ev = matrixCli.getEvent()
|
2019-06-17 13:21:30 -04:00
|
|
|
print(ev)
|
2019-06-20 05:32:02 -04:00
|
|
|
if ev["type"] == "m.room.member":
|
|
|
|
print("membership event")
|
|
|
|
if ev["membership"] == "invite" and ev["state_key"] == MYUSERNAME:
|
|
|
|
roomId = ev["room_id"]
|
2019-06-17 13:21:30 -04:00
|
|
|
print("joining room %s" % (roomId))
|
2014-12-02 09:10:24 -05:00
|
|
|
matrixCli.joinRoom(roomId)
|
2019-06-20 05:32:02 -04:00
|
|
|
elif ev["type"] == "m.room.message":
|
|
|
|
if ev["room_id"] in xmppClients:
|
2019-06-17 13:21:30 -04:00
|
|
|
print("already have a bridge for that user, ignoring")
|
2014-12-02 09:10:24 -05:00
|
|
|
continue
|
2019-06-17 13:21:30 -04:00
|
|
|
print("got message, connecting")
|
2019-06-20 05:32:02 -04:00
|
|
|
xmppClients[ev["room_id"]] = TrivialXmppClient(ev["room_id"], ev["user_id"])
|
|
|
|
gevent.spawn(xmppClients[ev["room_id"]].xmppLoop)
|
|
|
|
elif ev["type"] == "m.call.invite":
|
2019-06-17 13:21:30 -04:00
|
|
|
print("Incoming call")
|
2019-06-20 05:32:02 -04:00
|
|
|
# sdp = ev['content']['offer']['sdp']
|
|
|
|
# print "sdp: %s" % (sdp)
|
|
|
|
# xmppClients[ev['room_id']] = TrivialXmppClient(ev['room_id'], ev['user_id'])
|
|
|
|
# gevent.spawn(xmppClients[ev['room_id']].xmppLoop)
|
|
|
|
elif ev["type"] == "m.call.answer":
|
2019-06-17 13:21:30 -04:00
|
|
|
print("Call answered")
|
2019-06-20 05:32:02 -04:00
|
|
|
sdp = ev["content"]["answer"]["sdp"]
|
|
|
|
if ev["room_id"] not in xmppClients:
|
2019-06-17 13:21:30 -04:00
|
|
|
print("We didn't have a call for that room")
|
2014-12-02 09:10:24 -05:00
|
|
|
continue
|
|
|
|
# should probably check call ID too
|
2019-06-20 05:32:02 -04:00
|
|
|
xmppCli = xmppClients[ev["room_id"]]
|
2014-12-02 09:10:24 -05:00
|
|
|
xmppCli.sendAnswer(sdp)
|
2019-06-20 05:32:02 -04:00
|
|
|
elif ev["type"] == "m.call.hangup":
|
|
|
|
if ev["room_id"] in xmppClients:
|
|
|
|
xmppClients[ev["room_id"]].stop()
|
|
|
|
del xmppClients[ev["room_id"]]
|
|
|
|
|
2014-12-02 09:10:24 -05:00
|
|
|
|
2014-12-02 07:01:30 -05:00
|
|
|
class TrivialXmppClient:
|
2014-12-02 09:10:24 -05:00
|
|
|
def __init__(self, matrixRoom, userId):
|
|
|
|
self.rid = 0
|
|
|
|
self.matrixRoom = matrixRoom
|
|
|
|
self.userId = userId
|
|
|
|
self.running = True
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.running = False
|
|
|
|
|
|
|
|
def nextRid(self):
|
|
|
|
self.rid += 1
|
2019-06-20 05:32:02 -04:00
|
|
|
return "%d" % (self.rid)
|
2014-12-02 09:10:24 -05:00
|
|
|
|
|
|
|
def sendIq(self, xml):
|
2019-06-20 05:32:02 -04:00
|
|
|
fullXml = (
|
|
|
|
"<body rid='%s' xmlns='http://jabber.org/protocol/httpbind' sid='%s'>%s</body>"
|
|
|
|
% (self.nextRid(), self.sid, xml)
|
|
|
|
)
|
|
|
|
# print "\t>>>%s" % (fullXml)
|
2014-12-02 09:10:24 -05:00
|
|
|
return self.xmppPoke(fullXml)
|
|
|
|
|
|
|
|
def xmppPoke(self, xml):
|
2019-06-20 05:32:02 -04:00
|
|
|
headers = {"Content-Type": "application/xml"}
|
2014-12-02 09:10:24 -05:00
|
|
|
req = grequests.post(HTTPBIND, verify=False, headers=headers, data=xml)
|
|
|
|
resps = grequests.map([req])
|
|
|
|
obj = BeautifulSoup(resps[0].content)
|
|
|
|
return obj
|
|
|
|
|
|
|
|
def sendAnswer(self, answer):
|
2019-06-20 05:32:02 -04:00
|
|
|
print("sdp from matrix client", answer)
|
|
|
|
p = subprocess.Popen(
|
|
|
|
["node", "unjingle/unjingle.js", "--sdp"],
|
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
)
|
2014-12-02 09:10:24 -05:00
|
|
|
jingle, out_err = p.communicate(answer)
|
|
|
|
jingle = jingle % {
|
2019-06-20 05:32:02 -04:00
|
|
|
"tojid": self.callfrom,
|
|
|
|
"action": "session-accept",
|
|
|
|
"initiator": self.callfrom,
|
|
|
|
"responder": self.jid,
|
|
|
|
"sid": self.callsid,
|
2014-12-02 09:10:24 -05:00
|
|
|
}
|
2019-06-20 05:32:02 -04:00
|
|
|
print("answer jingle from sdp", jingle)
|
2014-12-02 09:10:24 -05:00
|
|
|
res = self.sendIq(jingle)
|
2019-06-20 05:32:02 -04:00
|
|
|
print("reply from answer: ", res)
|
2014-12-02 09:10:24 -05:00
|
|
|
|
|
|
|
self.ssrcs = {}
|
|
|
|
jingleSoup = BeautifulSoup(jingle)
|
2019-06-20 05:32:02 -04:00
|
|
|
for cont in jingleSoup.iq.jingle.findAll("content"):
|
2014-12-02 09:10:24 -05:00
|
|
|
if cont.description:
|
2019-06-20 05:32:02 -04:00
|
|
|
self.ssrcs[cont["name"]] = cont.description["ssrc"]
|
|
|
|
print("my ssrcs:", self.ssrcs)
|
2014-12-02 09:10:24 -05:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
gevent.joinall([gevent.spawn(self.advertiseSsrcs)])
|
2014-12-02 09:10:24 -05:00
|
|
|
|
|
|
|
def advertiseSsrcs(self):
|
2019-06-17 13:21:30 -04:00
|
|
|
time.sleep(7)
|
|
|
|
print("SSRC spammer started")
|
2014-12-02 09:10:24 -05:00
|
|
|
while self.running:
|
2022-03-29 06:41:19 -04:00
|
|
|
ssrcMsg = (
|
|
|
|
"<presence to='%(tojid)s' xmlns='jabber:client'><x xmlns='http://jabber.org/protocol/muc'/><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://jitsi.org/jitsimeet' ver='0WkSdhFnAUxrz4ImQQLdB80GFlE='/><nick xmlns='http://jabber.org/protocol/nick'>%(nick)s</nick><stats xmlns='http://jitsi.org/jitmeet/stats'><stat name='bitrate_download' value='175'/><stat name='bitrate_upload' value='176'/><stat name='packetLoss_total' value='0'/><stat name='packetLoss_download' value='0'/><stat name='packetLoss_upload' value='0'/></stats><media xmlns='http://estos.de/ns/mjs'><source type='audio' ssrc='%(assrc)s' direction='sendre'/><source type='video' ssrc='%(vssrc)s' direction='sendre'/></media></presence>"
|
|
|
|
% {
|
|
|
|
"tojid": "%s@%s/%s" % (ROOMNAME, ROOMDOMAIN, self.shortJid),
|
|
|
|
"nick": self.userId,
|
|
|
|
"assrc": self.ssrcs["audio"],
|
|
|
|
"vssrc": self.ssrcs["video"],
|
|
|
|
}
|
|
|
|
)
|
2014-12-02 09:10:24 -05:00
|
|
|
res = self.sendIq(ssrcMsg)
|
2019-06-20 05:32:02 -04:00
|
|
|
print("reply from ssrc announce: ", res)
|
2014-12-02 09:10:24 -05:00
|
|
|
time.sleep(10)
|
|
|
|
|
|
|
|
def xmppLoop(self):
|
|
|
|
self.matrixCallId = time.time()
|
2019-06-20 05:32:02 -04:00
|
|
|
res = self.xmppPoke(
|
|
|
|
"<body rid='%s' xmlns='http://jabber.org/protocol/httpbind' to='%s' xml:lang='en' wait='60' hold='1' content='text/xml; charset=utf-8' ver='1.6' xmpp:version='1.0' xmlns:xmpp='urn:xmpp:xbosh'/>"
|
|
|
|
% (self.nextRid(), HOST)
|
|
|
|
)
|
2014-12-02 09:10:24 -05:00
|
|
|
|
2019-06-17 13:21:30 -04:00
|
|
|
print(res)
|
2019-06-20 05:32:02 -04:00
|
|
|
self.sid = res.body["sid"]
|
2019-06-17 13:21:30 -04:00
|
|
|
print("sid %s" % (self.sid))
|
2014-12-02 09:10:24 -05:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
res = self.sendIq(
|
|
|
|
"<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='ANONYMOUS'/>"
|
|
|
|
)
|
2014-12-02 09:10:24 -05:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
res = self.xmppPoke(
|
|
|
|
"<body rid='%s' xmlns='http://jabber.org/protocol/httpbind' sid='%s' to='%s' xml:lang='en' xmpp:restart='true' xmlns:xmpp='urn:xmpp:xbosh'/>"
|
|
|
|
% (self.nextRid(), self.sid, HOST)
|
|
|
|
)
|
2014-12-02 09:10:24 -05:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
res = self.sendIq(
|
|
|
|
"<iq type='set' id='_bind_auth_2' xmlns='jabber:client'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/></iq>"
|
|
|
|
)
|
2019-06-17 13:21:30 -04:00
|
|
|
print(res)
|
2014-12-02 09:10:24 -05:00
|
|
|
|
|
|
|
self.jid = res.body.iq.bind.jid.string
|
2019-06-17 13:21:30 -04:00
|
|
|
print("jid: %s" % (self.jid))
|
2019-06-20 05:32:02 -04:00
|
|
|
self.shortJid = self.jid.split("-")[0]
|
2014-12-02 09:10:24 -05:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
res = self.sendIq(
|
|
|
|
"<iq type='set' id='_session_auth_2' xmlns='jabber:client'><session xmlns='urn:ietf:params:xml:ns:xmpp-session'/></iq>"
|
|
|
|
)
|
2014-12-02 09:10:24 -05:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
# randomthing = res.body.iq['to']
|
|
|
|
# whatsitpart = randomthing.split('-')[0]
|
2014-12-02 09:10:24 -05:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
# print "other random bind thing: %s" % (randomthing)
|
2014-12-02 09:10:24 -05:00
|
|
|
|
|
|
|
# advertise preence to the jitsi room, with our nick
|
2019-06-20 05:32:02 -04:00
|
|
|
res = self.sendIq(
|
|
|
|
"<iq type='get' to='%s' xmlns='jabber:client' id='1:sendIQ'><services xmlns='urn:xmpp:extdisco:1'><service host='%s'/></services></iq><presence to='%s@%s/d98f6c40' xmlns='jabber:client'><x xmlns='http://jabber.org/protocol/muc'/><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://jitsi.org/jitsimeet' ver='0WkSdhFnAUxrz4ImQQLdB80GFlE='/><nick xmlns='http://jabber.org/protocol/nick'>%s</nick></presence>"
|
|
|
|
% (HOST, TURNSERVER, ROOMNAME, ROOMDOMAIN, self.userId)
|
|
|
|
)
|
|
|
|
self.muc = {"users": []}
|
|
|
|
for p in res.body.findAll("presence"):
|
2014-12-02 09:10:24 -05:00
|
|
|
u = {}
|
2019-06-20 05:32:02 -04:00
|
|
|
u["shortJid"] = p["from"].split("/")[1]
|
2014-12-02 09:10:24 -05:00
|
|
|
if p.c and p.c.nick:
|
2019-06-20 05:32:02 -04:00
|
|
|
u["nick"] = p.c.nick.string
|
|
|
|
self.muc["users"].append(u)
|
|
|
|
print("muc: ", self.muc)
|
2014-12-02 09:10:24 -05:00
|
|
|
|
|
|
|
# wait for stuff
|
|
|
|
while True:
|
2019-06-17 13:21:30 -04:00
|
|
|
print("waiting...")
|
2014-12-02 09:10:24 -05:00
|
|
|
res = self.sendIq("")
|
2019-06-20 05:32:02 -04:00
|
|
|
print("got from stream: ", res)
|
2014-12-02 09:10:24 -05:00
|
|
|
if res.body.iq:
|
2019-06-20 05:32:02 -04:00
|
|
|
jingles = res.body.iq.findAll("jingle")
|
2014-12-02 09:10:24 -05:00
|
|
|
if len(jingles):
|
2019-06-20 05:32:02 -04:00
|
|
|
self.callfrom = res.body.iq["from"]
|
2014-12-02 09:10:24 -05:00
|
|
|
self.handleInvite(jingles[0])
|
2019-06-20 05:32:02 -04:00
|
|
|
elif "type" in res.body and res.body["type"] == "terminate":
|
2014-12-02 09:10:24 -05:00
|
|
|
self.running = False
|
|
|
|
del xmppClients[self.matrixRoom]
|
2019-06-17 13:21:30 -04:00
|
|
|
return
|
2014-12-02 09:10:24 -05:00
|
|
|
|
|
|
|
def handleInvite(self, jingle):
|
2019-06-20 05:32:02 -04:00
|
|
|
self.initiator = jingle["initiator"]
|
|
|
|
self.callsid = jingle["sid"]
|
|
|
|
p = subprocess.Popen(
|
|
|
|
["node", "unjingle/unjingle.js", "--jingle"],
|
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
)
|
|
|
|
print("raw jingle invite", str(jingle))
|
2014-12-02 09:10:24 -05:00
|
|
|
sdp, out_err = p.communicate(str(jingle))
|
2019-06-20 05:32:02 -04:00
|
|
|
print("transformed remote offer sdp", sdp)
|
2014-12-02 09:10:24 -05:00
|
|
|
inviteEvent = {
|
2019-06-20 05:32:02 -04:00
|
|
|
"offer": {"type": "offer", "sdp": sdp},
|
|
|
|
"call_id": self.matrixCallId,
|
|
|
|
"version": 0,
|
|
|
|
"lifetime": 30000,
|
2014-12-02 09:10:24 -05:00
|
|
|
}
|
2019-06-20 05:32:02 -04:00
|
|
|
matrixCli.sendEvent(self.matrixRoom, "m.call.invite", inviteEvent)
|
2014-12-02 09:10:24 -05:00
|
|
|
|
2014-12-02 07:01:30 -05:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
matrixCli = TrivialMatrixClient(ACCESS_TOKEN) # Undefined name
|
2014-12-02 07:01:30 -05:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
gevent.joinall([gevent.spawn(matrixLoop)])
|