2007-08-06 17:53:01 +02:00
|
|
|
##
|
|
|
|
## Copyright (C) 2006 Gajim Team
|
|
|
|
##
|
|
|
|
## This program is free software; you can redistribute it and/or modify
|
|
|
|
## it under the terms of the GNU General Public License as published
|
|
|
|
## by the Free Software Foundation; version 2 only.
|
|
|
|
##
|
|
|
|
## This program is distributed in the hope that it will be useful,
|
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
|
|
|
''' Handles the jingle signalling protocol. '''
|
|
|
|
|
2009-09-13 11:02:49 +02:00
|
|
|
#TODO:
|
2009-09-16 13:55:54 +02:00
|
|
|
# * things in XEP 0166, including:
|
2009-09-13 11:02:49 +02:00
|
|
|
# - 'senders' attribute of 'content' element
|
|
|
|
# - security preconditions
|
|
|
|
# * actions:
|
2009-09-26 21:55:43 +02:00
|
|
|
# - content-modify
|
2009-09-13 11:02:49 +02:00
|
|
|
# - description-info, session-info
|
|
|
|
# - security-info
|
|
|
|
# - transport-accept, transport-reject
|
|
|
|
# * sid/content related:
|
|
|
|
# - tiebreaking
|
|
|
|
# - if there already is a session, use it
|
2009-09-16 13:55:54 +02:00
|
|
|
# * things in XEP 0176, including:
|
|
|
|
# - http://xmpp.org/extensions/xep-0176.html#protocol-restarts
|
|
|
|
# - http://xmpp.org/extensions/xep-0176.html#fallback
|
|
|
|
# * XEP 0177 (raw udp)
|
|
|
|
|
2009-09-13 11:02:49 +02:00
|
|
|
# * UI:
|
|
|
|
# - make state and codec informations available to the user
|
2009-09-18 20:17:35 +02:00
|
|
|
# - video integration
|
2009-09-13 11:02:49 +02:00
|
|
|
# * config:
|
|
|
|
# - codecs
|
|
|
|
# - STUN
|
2009-09-18 20:17:35 +02:00
|
|
|
|
2009-09-16 13:55:54 +02:00
|
|
|
# * DONE: figure out why it doesn't work with pidgin:
|
|
|
|
# That's a bug in pidgin: http://xmpp.org/extensions/xep-0176.html#protocol-checks
|
2009-09-18 20:17:35 +02:00
|
|
|
|
2009-09-13 11:02:49 +02:00
|
|
|
# * timeout
|
|
|
|
|
|
|
|
# * split this file in several modules
|
|
|
|
# For example, a file dedicated for XEP0166, one for XEP0176,
|
2009-10-16 19:04:04 +02:00
|
|
|
# and one for XEP0167
|
2009-09-13 11:02:49 +02:00
|
|
|
|
|
|
|
# * handle different kinds of sink and src elements
|
2007-08-24 01:42:31 +02:00
|
|
|
|
2007-08-07 01:19:57 +02:00
|
|
|
import gajim
|
2007-08-06 17:53:01 +02:00
|
|
|
import xmpp
|
2009-09-26 10:29:08 +02:00
|
|
|
import helpers
|
2007-08-06 17:53:01 +02:00
|
|
|
|
2009-09-13 11:02:49 +02:00
|
|
|
import farsight, gst
|
2007-08-24 01:42:31 +02:00
|
|
|
|
2009-09-13 11:02:49 +02:00
|
|
|
def get_first_gst_element(elements):
|
|
|
|
''' Returns, if it exists, the first available element of the list. '''
|
|
|
|
for name in elements:
|
|
|
|
factory = gst.element_factory_find(name)
|
|
|
|
if factory:
|
|
|
|
return factory.create()
|
|
|
|
|
|
|
|
#FIXME: Move it to JingleSession.States?
|
2007-08-06 17:53:01 +02:00
|
|
|
class JingleStates(object):
|
|
|
|
''' States in which jingle session may exist. '''
|
2009-09-19 17:24:59 +02:00
|
|
|
ended = 0
|
|
|
|
pending = 1
|
|
|
|
active = 2
|
2007-08-06 17:53:01 +02:00
|
|
|
|
2009-09-13 11:02:49 +02:00
|
|
|
#FIXME: Move it to JingleTransport.Type?
|
|
|
|
class TransportType(object):
|
|
|
|
''' Possible types of a JingleTransport '''
|
|
|
|
datagram = 1
|
|
|
|
streaming = 2
|
|
|
|
|
|
|
|
class OutOfOrder(Exception):
|
|
|
|
''' Exception that should be raised when an action is received when in the wrong state. '''
|
|
|
|
|
2009-10-16 19:04:04 +02:00
|
|
|
class TieBreak(Exception):
|
|
|
|
''' Exception that should be raised in case of a tie, when we overrule the other action. '''
|
|
|
|
|
2007-08-06 17:53:01 +02:00
|
|
|
class JingleSession(object):
|
|
|
|
''' This represents one jingle session. '''
|
2007-08-07 18:34:09 +02:00
|
|
|
def __init__(self, con, weinitiate, jid, sid=None):
|
2007-08-06 17:53:01 +02:00
|
|
|
''' con -- connection object,
|
2009-09-16 20:41:12 +02:00
|
|
|
weinitiate -- boolean, are we the initiator?
|
|
|
|
jid - jid of the other entity'''
|
|
|
|
self.contents = {} # negotiated contents
|
|
|
|
self.connection = con # connection to use
|
2007-08-06 17:53:01 +02:00
|
|
|
# our full jid
|
2009-09-16 20:41:12 +02:00
|
|
|
self.ourjid = gajim.get_jid_from_account(self.connection.name) + '/' + \
|
|
|
|
con.server_resource
|
2009-09-25 20:41:22 +02:00
|
|
|
self.peerjid = jid # jid we connect to
|
2007-08-06 17:53:01 +02:00
|
|
|
# jid we use as the initiator
|
2009-09-16 20:41:12 +02:00
|
|
|
self.initiator = weinitiate and self.ourjid or self.peerjid
|
2007-08-06 17:53:01 +02:00
|
|
|
# jid we use as the responder
|
2009-09-16 20:41:12 +02:00
|
|
|
self.responder = weinitiate and self.peerjid or self.ourjid
|
2007-08-06 17:53:01 +02:00
|
|
|
# are we an initiator?
|
2009-09-16 20:41:12 +02:00
|
|
|
self.weinitiate = weinitiate
|
2007-08-06 17:53:01 +02:00
|
|
|
# what state is session in? (one from JingleStates)
|
2009-09-16 20:41:12 +02:00
|
|
|
self.state = JingleStates.ended
|
2007-08-07 18:34:09 +02:00
|
|
|
if not sid:
|
2009-09-16 20:41:12 +02:00
|
|
|
sid = con.connection.getAnID()
|
|
|
|
self.sid = sid # sessionid
|
2007-08-06 17:53:01 +02:00
|
|
|
|
2009-09-16 20:41:12 +02:00
|
|
|
self.accepted = True # is this session accepted by user
|
2007-08-24 01:42:31 +02:00
|
|
|
|
2007-08-06 17:53:01 +02:00
|
|
|
# callbacks to call on proper contents
|
2007-08-07 18:34:09 +02:00
|
|
|
# use .prepend() to add new callbacks, especially when you're going
|
|
|
|
# to send error instead of ack
|
2009-09-19 17:24:59 +02:00
|
|
|
self.callbacks = {
|
2009-09-25 19:32:13 +02:00
|
|
|
'content-accept': [self.__contentAcceptCB, self.__broadcastCB,
|
|
|
|
self.__defaultCB],
|
|
|
|
'content-add': [self.__contentAddCB, self.__broadcastCB,
|
|
|
|
self.__defaultCB], #TODO
|
2009-09-13 11:02:49 +02:00
|
|
|
'content-modify': [self.__defaultCB], #TODO
|
2009-09-25 19:32:13 +02:00
|
|
|
'content-reject': [self.__defaultCB, self.__contentRemoveCB], #TODO
|
2009-09-13 11:02:49 +02:00
|
|
|
'content-remove': [self.__defaultCB, self.__contentRemoveCB],
|
2009-09-28 22:23:48 +02:00
|
|
|
'description-info': [self.__broadcastCB, self.__defaultCB], #TODO
|
2009-09-13 11:02:49 +02:00
|
|
|
'security-info': [self.__defaultCB], #TODO
|
2009-09-16 20:41:12 +02:00
|
|
|
'session-accept': [self.__sessionAcceptCB, self.__contentAcceptCB,
|
|
|
|
self.__broadcastCB, self.__defaultCB],
|
2009-09-27 22:56:09 +02:00
|
|
|
'session-info': [self.__sessionInfoCB, self.__broadcastCB, self.__defaultCB],
|
2009-09-16 20:41:12 +02:00
|
|
|
'session-initiate': [self.__sessionInitiateCB, self.__broadcastCB,
|
|
|
|
self.__defaultCB],
|
|
|
|
'session-terminate': [self.__sessionTerminateCB, self.__broadcastAllCB,
|
|
|
|
self.__defaultCB],
|
2007-08-15 11:47:29 +02:00
|
|
|
'transport-info': [self.__broadcastCB, self.__defaultCB],
|
2009-09-13 11:02:49 +02:00
|
|
|
'transport-replace': [self.__broadcastCB, self.__transportReplaceCB], #TODO
|
|
|
|
'transport-accept': [self.__defaultCB], #TODO
|
|
|
|
'transport-reject': [self.__defaultCB], #TODO
|
2007-08-07 18:34:09 +02:00
|
|
|
'iq-result': [],
|
2009-09-17 23:36:26 +02:00
|
|
|
'iq-error': [self.__errorCB],
|
2007-08-07 18:34:09 +02:00
|
|
|
}
|
2007-08-07 01:19:57 +02:00
|
|
|
|
2007-08-24 01:42:31 +02:00
|
|
|
''' Interaction with user '''
|
2009-09-19 17:24:59 +02:00
|
|
|
def approve_session(self):
|
2009-09-16 20:41:12 +02:00
|
|
|
''' Called when user accepts session in UI (when we aren't the initiator).
|
|
|
|
'''
|
2009-09-19 17:24:59 +02:00
|
|
|
self.accept_session()
|
2007-08-24 01:42:31 +02:00
|
|
|
|
2009-09-19 17:24:59 +02:00
|
|
|
def decline_session(self):
|
2009-09-16 20:41:12 +02:00
|
|
|
''' Called when user declines session in UI (when we aren't the initiator)
|
|
|
|
'''
|
2009-09-13 11:02:49 +02:00
|
|
|
reason = xmpp.Node('reason')
|
|
|
|
reason.addChild('decline')
|
2009-09-22 22:33:00 +02:00
|
|
|
self._session_terminate(reason)
|
2007-08-24 01:42:31 +02:00
|
|
|
|
2009-09-25 23:05:14 +02:00
|
|
|
def approve_content(self, media):
|
|
|
|
content = self.get_content(media)
|
|
|
|
if content:
|
|
|
|
content.accepted = True
|
|
|
|
self.on_session_state_changed(content)
|
|
|
|
|
|
|
|
def reject_content(self, media):
|
|
|
|
content = self.get_content(media)
|
|
|
|
if content:
|
2009-09-26 12:31:56 +02:00
|
|
|
if self.state == JingleStates.active:
|
|
|
|
self.__content_reject(content)
|
2009-09-25 23:05:14 +02:00
|
|
|
content.destroy()
|
|
|
|
self.on_session_state_changed()
|
|
|
|
|
2009-09-16 22:35:11 +02:00
|
|
|
def end_session(self):
|
2009-09-19 17:24:59 +02:00
|
|
|
''' Called when user stops or cancel session in UI. '''
|
2009-09-16 22:35:11 +02:00
|
|
|
reason = xmpp.Node('reason')
|
|
|
|
if self.state == JingleStates.active:
|
|
|
|
reason.addChild('success')
|
|
|
|
else:
|
2009-09-17 23:36:26 +02:00
|
|
|
reason.addChild('cancel')
|
2009-09-22 22:33:00 +02:00
|
|
|
self._session_terminate(reason)
|
2009-09-16 22:35:11 +02:00
|
|
|
|
2007-08-06 17:53:01 +02:00
|
|
|
''' Middle-level functions to manage contents. Handle local content
|
|
|
|
cache and send change notifications. '''
|
2009-09-25 19:32:13 +02:00
|
|
|
def get_content(self, media=None):
|
|
|
|
if media == 'audio':
|
|
|
|
cls = JingleVoIP
|
|
|
|
elif media == 'video':
|
|
|
|
cls = JingleVideo
|
|
|
|
#elif media == None:
|
|
|
|
# cls = JingleContent
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
for content in self.contents.values():
|
|
|
|
if isinstance(content, cls):
|
|
|
|
return content
|
|
|
|
|
2009-09-19 17:24:59 +02:00
|
|
|
def add_content(self, name, content, creator='we'):
|
2007-08-06 17:53:01 +02:00
|
|
|
''' Add new content to session. If the session is active,
|
|
|
|
this will send proper stanza to update session.
|
2007-08-12 23:22:25 +02:00
|
|
|
Creator must be one of ('we', 'peer', 'initiator', 'responder')'''
|
2007-08-24 01:42:31 +02:00
|
|
|
assert creator in ('we', 'peer', 'initiator', 'responder')
|
|
|
|
|
2009-09-16 20:41:12 +02:00
|
|
|
if (creator == 'we' and self.weinitiate) or (creator == 'peer' and \
|
|
|
|
not self.weinitiate):
|
|
|
|
creator = 'initiator'
|
|
|
|
elif (creator == 'peer' and self.weinitiate) or (creator == 'we' and \
|
|
|
|
not self.weinitiate):
|
|
|
|
creator = 'responder'
|
2007-08-12 23:22:25 +02:00
|
|
|
content.creator = creator
|
2007-08-07 01:19:57 +02:00
|
|
|
content.name = name
|
2009-09-19 17:24:59 +02:00
|
|
|
self.contents[(creator, name)] = content
|
2007-08-06 17:53:01 +02:00
|
|
|
|
2009-09-25 23:52:39 +02:00
|
|
|
if (creator == 'initiator') == self.weinitiate:
|
|
|
|
# The content is from us, accept it
|
|
|
|
content.accepted = True
|
|
|
|
|
2009-09-19 17:24:59 +02:00
|
|
|
def remove_content(self, creator, name):
|
2007-08-07 01:19:57 +02:00
|
|
|
''' We do not need this now '''
|
2009-09-25 19:32:13 +02:00
|
|
|
#TODO:
|
|
|
|
if (creator, name) in self.contents:
|
|
|
|
content = self.contents[(creator, name)]
|
|
|
|
if len(self.contents) > 1:
|
|
|
|
self.__content_remove(content)
|
|
|
|
self.contents[(creator, name)].destroy()
|
|
|
|
if len(self.contents) == 0:
|
|
|
|
self.end_session()
|
2007-08-07 01:19:57 +02:00
|
|
|
|
2009-09-19 17:24:59 +02:00
|
|
|
def modify_content(self, creator, name, *someother):
|
2007-08-07 01:19:57 +02:00
|
|
|
''' We do not need this now '''
|
|
|
|
pass
|
|
|
|
|
2009-09-25 19:32:13 +02:00
|
|
|
def on_session_state_changed(self, content=None):
|
2009-09-26 21:55:43 +02:00
|
|
|
if self.state == JingleStates.ended:
|
|
|
|
# Session not yet started, only one action possible: session-initiate
|
|
|
|
if self.is_ready() and self.weinitiate:
|
|
|
|
self.__session_initiate()
|
|
|
|
elif self.state == JingleStates.pending:
|
|
|
|
# We can either send a session-accept or a content-add
|
|
|
|
if self.is_ready() and not self.weinitiate:
|
|
|
|
self.__session_accept()
|
|
|
|
elif content and (content.creator == 'initiator') == self.weinitiate:
|
|
|
|
self.__content_add(content)
|
|
|
|
elif content and self.weinitiate:
|
|
|
|
self.__content_accept(content)
|
|
|
|
elif self.state == JingleStates.active:
|
|
|
|
# We can either send a content-add or a content-accept
|
2009-09-25 19:32:13 +02:00
|
|
|
if not content:
|
|
|
|
return
|
|
|
|
if (content.creator == 'initiator') == self.weinitiate:
|
|
|
|
# We initiated this content. It's a pending content-add.
|
|
|
|
self.__content_add(content)
|
|
|
|
else:
|
|
|
|
# The other side created this content, we accept it.
|
|
|
|
self.__content_accept(content)
|
2009-09-13 11:02:49 +02:00
|
|
|
|
2009-09-18 20:17:35 +02:00
|
|
|
def is_ready(self):
|
2009-09-19 17:24:59 +02:00
|
|
|
''' Returns True when all codecs and candidates are ready
|
|
|
|
(for all contents). '''
|
2009-09-25 19:32:13 +02:00
|
|
|
return (all((content.is_ready() for content in self.contents.itervalues()))
|
|
|
|
and self.accepted)
|
2009-09-18 20:17:35 +02:00
|
|
|
|
2007-08-06 17:53:01 +02:00
|
|
|
''' Middle-level function to do stanza exchange. '''
|
2009-09-25 19:32:13 +02:00
|
|
|
def accept_session(self):
|
|
|
|
''' Mark the session as accepted. '''
|
|
|
|
self.accepted = True
|
|
|
|
self.on_session_state_changed()
|
|
|
|
|
2009-09-19 17:24:59 +02:00
|
|
|
def start_session(self):
|
2009-09-25 19:32:13 +02:00
|
|
|
''' Mark the session as ready to be started. '''
|
|
|
|
self.accepted = True
|
|
|
|
self.on_session_state_changed()
|
2007-08-06 17:53:01 +02:00
|
|
|
|
2009-09-19 17:24:59 +02:00
|
|
|
def send_session_info(self):
|
|
|
|
pass
|
2007-08-06 17:53:01 +02:00
|
|
|
|
2009-09-19 17:24:59 +02:00
|
|
|
def send_content_accept(self, content):
|
2009-09-16 20:41:12 +02:00
|
|
|
assert self.state != JingleStates.ended
|
2009-09-19 17:24:59 +02:00
|
|
|
stanza, jingle = self.__make_jingle('content-accept')
|
2009-09-13 11:02:49 +02:00
|
|
|
jingle.addChild(node=content)
|
|
|
|
self.connection.connection.send(stanza)
|
|
|
|
|
2009-09-19 17:24:59 +02:00
|
|
|
def send_transport_info(self, content):
|
|
|
|
assert self.state != JingleStates.ended
|
|
|
|
stanza, jingle = self.__make_jingle('transport-info')
|
2009-09-13 11:02:49 +02:00
|
|
|
jingle.addChild(node=content)
|
|
|
|
self.connection.connection.send(stanza)
|
|
|
|
|
2007-08-24 01:42:31 +02:00
|
|
|
''' Session callbacks. '''
|
2007-08-06 17:53:01 +02:00
|
|
|
def stanzaCB(self, stanza):
|
|
|
|
''' A callback for ConnectionJingle. It gets stanza, then
|
|
|
|
tries to send it to all internally registered callbacks.
|
|
|
|
First one to raise xmpp.NodeProcessed breaks function.'''
|
|
|
|
jingle = stanza.getTag('jingle')
|
|
|
|
error = stanza.getTag('error')
|
|
|
|
if error:
|
|
|
|
# it's an iq-error stanza
|
2007-08-15 11:47:29 +02:00
|
|
|
action = 'iq-error'
|
2007-08-07 01:19:57 +02:00
|
|
|
elif jingle:
|
2007-08-06 17:53:01 +02:00
|
|
|
# it's a jingle action
|
|
|
|
action = jingle.getAttr('action')
|
2009-09-13 11:02:49 +02:00
|
|
|
if action not in self.callbacks:
|
2009-09-18 20:17:35 +02:00
|
|
|
self.__send_error(stanza, 'bad_request')
|
2009-09-13 11:02:49 +02:00
|
|
|
return
|
|
|
|
#FIXME: If we aren't initiated and it's not a session-initiate...
|
|
|
|
if action != 'session-initiate' and self.state == JingleStates.ended:
|
2009-09-18 20:17:35 +02:00
|
|
|
self.__send_error(stanza, 'item-not-found', 'unknown-session')
|
2009-09-13 11:02:49 +02:00
|
|
|
return
|
2007-08-06 17:53:01 +02:00
|
|
|
else:
|
|
|
|
# it's an iq-result (ack) stanza
|
2007-08-15 11:47:29 +02:00
|
|
|
action = 'iq-result'
|
2007-08-06 17:53:01 +02:00
|
|
|
|
2007-08-15 11:47:29 +02:00
|
|
|
callables = self.callbacks[action]
|
2007-08-06 17:53:01 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
for callable in callables:
|
2007-08-15 11:47:29 +02:00
|
|
|
callable(stanza=stanza, jingle=jingle, error=error, action=action)
|
2007-08-06 17:53:01 +02:00
|
|
|
except xmpp.NodeProcessed:
|
|
|
|
pass
|
2009-10-16 19:04:04 +02:00
|
|
|
except TieBreak:
|
|
|
|
self.__send_error(stanza, 'conflict', 'tiebreak')
|
2009-09-13 11:02:49 +02:00
|
|
|
except OutOfOrder:
|
2009-09-18 20:17:35 +02:00
|
|
|
self.__send_error(stanza, 'unexpected-request', 'out-of-order')#FIXME
|
2007-08-06 17:53:01 +02:00
|
|
|
|
2007-08-15 11:47:29 +02:00
|
|
|
def __defaultCB(self, stanza, jingle, error, action):
|
2007-08-06 17:53:01 +02:00
|
|
|
''' Default callback for action stanzas -- simple ack
|
|
|
|
and stop processing. '''
|
|
|
|
response = stanza.buildReply('result')
|
2007-08-07 18:34:09 +02:00
|
|
|
self.connection.connection.send(response)
|
2007-08-06 17:53:01 +02:00
|
|
|
|
2009-09-17 23:36:26 +02:00
|
|
|
def __errorCB(self, stanza, jingle, error, action):
|
|
|
|
#FIXME
|
|
|
|
text = error.getTagData('text')
|
|
|
|
jingle_error = None
|
|
|
|
xmpp_error = None
|
|
|
|
for child in error.getChildren():
|
|
|
|
if child.getNamespace() == xmpp.NS_JINGLE_ERRORS:
|
|
|
|
jingle_error = child.getName()
|
|
|
|
elif child.getNamespace() == xmpp.NS_STANZAS:
|
|
|
|
xmpp_error = child.getName()
|
|
|
|
self.__dispatch_error(xmpp_error, jingle_error, text)
|
2009-09-18 20:17:35 +02:00
|
|
|
#FIXME: Not sure when we would want to do that...
|
|
|
|
if xmpp_error == 'item-not-found':
|
2009-10-16 19:04:04 +02:00
|
|
|
self.connection.delete_jingle_session(self.peerjid, self.sid)
|
2009-09-17 23:36:26 +02:00
|
|
|
|
2009-09-13 11:02:49 +02:00
|
|
|
def __transportReplaceCB(self, stanza, jingle, error, action):
|
|
|
|
for content in jingle.iterTags('content'):
|
|
|
|
creator = content['creator']
|
|
|
|
name = content['name']
|
|
|
|
if (creator, name) in self.contents:
|
|
|
|
transport_ns = content.getTag('transport').getNamespace()
|
|
|
|
if transport_ns == xmpp.JINGLE_ICE_UDP:
|
|
|
|
#FIXME: We don't manage anything else than ICE-UDP now...
|
|
|
|
#What was the previous transport?!?
|
|
|
|
#Anyway, content's transport is not modifiable yet
|
|
|
|
pass
|
|
|
|
else:
|
2009-09-19 17:24:59 +02:00
|
|
|
stanza, jingle = self.__make_jingle('transport-reject')
|
|
|
|
content = jingle.setTag('content', attrs={'creator': creator,
|
2009-09-16 20:41:12 +02:00
|
|
|
'name': name})
|
2009-09-19 17:24:59 +02:00
|
|
|
content.setTag('transport', namespace=transport_ns)
|
2009-09-13 11:02:49 +02:00
|
|
|
self.connection.connection.send(stanza)
|
|
|
|
raise xmpp.NodeProcessed
|
|
|
|
else:
|
|
|
|
#FIXME: This ressource is unknown to us, what should we do?
|
|
|
|
#For now, reject the transport
|
2009-09-19 17:24:59 +02:00
|
|
|
stanza, jingle = self.__make_jingle('transport-reject')
|
2009-09-13 11:02:49 +02:00
|
|
|
c = jingle.setTag('content', attrs={'creator': creator,
|
2009-09-16 20:41:12 +02:00
|
|
|
'name': name})
|
2009-09-13 11:02:49 +02:00
|
|
|
c.setTag('transport', namespace=transport_ns)
|
|
|
|
self.connection.connection.send(stanza)
|
|
|
|
raise xmpp.NodeProcessed
|
|
|
|
|
|
|
|
def __sessionInfoCB(self, stanza, jingle, error, action):
|
2009-09-27 22:56:09 +02:00
|
|
|
#TODO: ringing, active, (un)hold, (un)mute
|
2009-09-13 11:02:49 +02:00
|
|
|
payload = jingle.getPayload()
|
|
|
|
if len(payload) > 0:
|
2009-09-18 20:17:35 +02:00
|
|
|
self.__send_error(stanza, 'feature-not-implemented', 'unsupported-info')
|
2009-09-13 11:02:49 +02:00
|
|
|
raise xmpp.NodeProcessed
|
|
|
|
|
|
|
|
def __contentRemoveCB(self, stanza, jingle, error, action):
|
|
|
|
for content in jingle.iterTags('content'):
|
|
|
|
creator = content['creator']
|
|
|
|
name = content['name']
|
|
|
|
if (creator, name) in self.contents:
|
2009-09-26 14:14:58 +02:00
|
|
|
content = self.contents[(creator, name)]
|
|
|
|
#TODO: this will fail if content is not an RTP content
|
|
|
|
self.connection.dispatch('JINGLE_DISCONNECTED',
|
|
|
|
(self.peerjid, self.sid, content.media, 'removed'))
|
|
|
|
content.destroy()
|
2009-09-13 11:02:49 +02:00
|
|
|
if len(self.contents) == 0:
|
|
|
|
reason = xmpp.Node('reason')
|
2009-09-25 19:32:13 +02:00
|
|
|
reason.setTag('success')
|
2009-09-22 22:33:00 +02:00
|
|
|
self._session_terminate(reason)
|
2009-09-13 11:02:49 +02:00
|
|
|
|
|
|
|
def __sessionAcceptCB(self, stanza, jingle, error, action):
|
|
|
|
if self.state != JingleStates.pending: #FIXME
|
|
|
|
raise OutOfOrder
|
2009-09-17 23:36:26 +02:00
|
|
|
self.state = JingleStates.active
|
2009-09-13 11:02:49 +02:00
|
|
|
|
2007-08-15 11:47:29 +02:00
|
|
|
def __contentAcceptCB(self, stanza, jingle, error, action):
|
2007-08-07 01:19:57 +02:00
|
|
|
''' Called when we get content-accept stanza or equivalent one
|
|
|
|
(like session-accept).'''
|
2007-08-15 11:47:29 +02:00
|
|
|
# check which contents are accepted
|
2007-08-07 01:19:57 +02:00
|
|
|
for content in jingle.iterTags('content'):
|
|
|
|
creator = content['creator']
|
2009-09-13 11:02:49 +02:00
|
|
|
name = content['name']#TODO...
|
2007-08-07 01:19:57 +02:00
|
|
|
|
2009-09-25 19:32:13 +02:00
|
|
|
def __contentAddCB(self, stanza, jingle, error, action):
|
|
|
|
if self.state == JingleStates.ended:
|
|
|
|
raise OutOfOrder
|
2009-09-26 12:22:41 +02:00
|
|
|
|
|
|
|
parse_result = self.__parse_contents(jingle)
|
|
|
|
contents = parse_result[2]
|
|
|
|
rejected_contents = parse_result[3]
|
|
|
|
|
|
|
|
for name, creator in rejected_contents:
|
|
|
|
#TODO:
|
|
|
|
content = JingleContent()
|
|
|
|
self.add_content(name, content, creator)
|
|
|
|
self.__content_reject(content)
|
|
|
|
self.contents[(content.creator, content.name)].destroy()
|
|
|
|
|
2009-09-26 14:14:58 +02:00
|
|
|
self.connection.dispatch('JINGLE_INCOMING', (self.peerjid, self.sid,
|
2009-09-26 12:22:41 +02:00
|
|
|
contents))
|
2009-09-25 19:32:13 +02:00
|
|
|
|
2007-08-15 11:47:29 +02:00
|
|
|
def __sessionInitiateCB(self, stanza, jingle, error, action):
|
2007-08-07 18:34:09 +02:00
|
|
|
''' We got a jingle session request from other entity,
|
2007-08-24 01:42:31 +02:00
|
|
|
therefore we are the receiver... Unpack the data,
|
|
|
|
inform the user. '''
|
2009-10-16 19:04:04 +02:00
|
|
|
|
|
|
|
if self.state != JingleStates.ended:
|
2009-09-13 11:02:49 +02:00
|
|
|
raise OutOfOrder
|
|
|
|
|
2007-08-07 18:34:09 +02:00
|
|
|
self.initiator = jingle['initiator']
|
|
|
|
self.responder = self.ourjid
|
2007-08-12 23:22:25 +02:00
|
|
|
self.peerjid = self.initiator
|
2007-08-24 01:42:31 +02:00
|
|
|
self.accepted = False # user did not accept this session yet
|
|
|
|
|
|
|
|
# TODO: If the initiator is unknown to the receiver (e.g., via presence
|
|
|
|
# subscription) and the receiver has a policy of not communicating via
|
|
|
|
# Jingle with unknown entities, it SHOULD return a <service-unavailable/>
|
|
|
|
# error.
|
2007-08-07 18:34:09 +02:00
|
|
|
|
2007-08-24 01:42:31 +02:00
|
|
|
# Lets check what kind of jingle session does the peer want
|
2009-09-26 12:22:41 +02:00
|
|
|
contents_ok, transports_ok, contents, pouet = self.__parse_contents(jingle)
|
2007-08-07 18:34:09 +02:00
|
|
|
|
2007-08-24 01:42:31 +02:00
|
|
|
# If there's no content we understand...
|
2009-09-13 11:02:49 +02:00
|
|
|
if not contents_ok:
|
|
|
|
# TODO: http://xmpp.org/extensions/xep-0166.html#session-terminate
|
|
|
|
reason = xmpp.Node('reason')
|
|
|
|
reason.setTag('unsupported-applications')
|
|
|
|
self.__defaultCB(stanza, jingle, error, action)
|
2009-09-22 22:33:00 +02:00
|
|
|
self._session_terminate(reason)
|
2009-09-13 11:02:49 +02:00
|
|
|
raise xmpp.NodeProcessed
|
|
|
|
|
|
|
|
if not transports_ok:
|
|
|
|
# TODO: http://xmpp.org/extensions/xep-0166.html#session-terminate
|
|
|
|
reason = xmpp.Node('reason')
|
|
|
|
reason.setTag('unsupported-transports')
|
|
|
|
self.__defaultCB(stanza, jingle, error, action)
|
2009-09-22 22:33:00 +02:00
|
|
|
self._session_terminate(reason)
|
2007-08-07 18:34:09 +02:00
|
|
|
raise xmpp.NodeProcessed
|
|
|
|
|
|
|
|
self.state = JingleStates.pending
|
|
|
|
|
2007-08-24 01:42:31 +02:00
|
|
|
# Send event about starting a session
|
2009-09-26 14:14:58 +02:00
|
|
|
self.connection.dispatch('JINGLE_INCOMING', (self.peerjid, self.sid,
|
2009-09-16 20:41:12 +02:00
|
|
|
contents))
|
2007-08-24 01:42:31 +02:00
|
|
|
|
2007-08-15 11:47:29 +02:00
|
|
|
def __broadcastCB(self, stanza, jingle, error, action):
|
|
|
|
''' Broadcast the stanza contents to proper content handlers. '''
|
|
|
|
for content in jingle.iterTags('content'):
|
|
|
|
name = content['name']
|
|
|
|
creator = content['creator']
|
|
|
|
cn = self.contents[(creator, name)]
|
|
|
|
cn.stanzaCB(stanza, content, error, action)
|
|
|
|
|
2009-09-13 11:02:49 +02:00
|
|
|
def __sessionTerminateCB(self, stanza, jingle, error, action):
|
2009-10-16 19:04:04 +02:00
|
|
|
self.connection.delete_jingle_session(self.peerjid, self.sid)
|
2009-09-17 23:36:26 +02:00
|
|
|
reason, text = self.__reason_from_stanza(jingle)
|
|
|
|
if reason not in ('success', 'cancel', 'decline'):
|
|
|
|
self.__dispatch_error(reason, reason, text)
|
|
|
|
if text:
|
|
|
|
text = '%s (%s)' % (reason, text)
|
|
|
|
else:
|
|
|
|
text = reason#TODO
|
2009-09-26 14:14:58 +02:00
|
|
|
self.connection.dispatch('JINGLE_DISCONNECTED',
|
|
|
|
(self.peerjid, self.sid, None, text))
|
2009-09-17 23:36:26 +02:00
|
|
|
|
2009-09-19 17:24:59 +02:00
|
|
|
def __broadcastAllCB(self, stanza, jingle, error, action):
|
|
|
|
''' Broadcast the stanza to all content handlers. '''
|
|
|
|
for content in self.contents.itervalues():
|
|
|
|
content.stanzaCB(stanza, None, error, action)
|
|
|
|
|
2009-09-26 12:22:41 +02:00
|
|
|
''' Internal methods. '''
|
|
|
|
def __parse_contents(self, jingle):
|
2009-09-26 21:55:43 +02:00
|
|
|
#TODO: Needs some reworking
|
2009-09-26 12:22:41 +02:00
|
|
|
contents = []
|
|
|
|
contents_rejected = []
|
|
|
|
contents_ok = False
|
|
|
|
transports_ok = False
|
|
|
|
|
|
|
|
for element in jingle.iterTags('content'):
|
|
|
|
desc = element.getTag('description')
|
|
|
|
desc_ns = desc.getNamespace()
|
|
|
|
tran_ns = element.getTag('transport').getNamespace()
|
|
|
|
if desc_ns == xmpp.NS_JINGLE_RTP and desc['media'] in ('audio', 'video'):
|
|
|
|
contents_ok = True
|
|
|
|
#TODO: Everything here should be moved somewhere else
|
|
|
|
if tran_ns == xmpp.NS_JINGLE_ICE_UDP:
|
|
|
|
if desc['media'] == 'audio':
|
|
|
|
self.add_content(element['name'], JingleVoIP(self), 'peer')
|
|
|
|
else:
|
|
|
|
self.add_content(element['name'], JingleVideo(self), 'peer')
|
|
|
|
contents.append((desc['media'],))
|
|
|
|
transports_ok = True
|
|
|
|
else:
|
|
|
|
contents_rejected.append((element['name'], 'peer'))
|
|
|
|
else:
|
|
|
|
contents_rejected.append((element['name'], 'peer'))
|
|
|
|
|
|
|
|
return (contents_ok, transports_ok, contents, contents_rejected)
|
2009-09-17 23:36:26 +02:00
|
|
|
|
2009-09-18 20:17:35 +02:00
|
|
|
def __dispatch_error(self, error, jingle_error=None, text=None):
|
2009-09-17 23:36:26 +02:00
|
|
|
if jingle_error:
|
|
|
|
error = jingle_error
|
|
|
|
if text:
|
|
|
|
text = '%s (%s)' % (error, text)
|
|
|
|
else:
|
|
|
|
text = error
|
|
|
|
self.connection.dispatch('JINGLE_ERROR', (self.peerjid, self.sid, text))
|
2009-09-13 11:02:49 +02:00
|
|
|
|
2009-09-17 23:36:26 +02:00
|
|
|
def __reason_from_stanza(self, stanza):
|
|
|
|
reason = 'success'
|
|
|
|
reasons = ['success', 'busy', 'cancel', 'connectivity-error',
|
|
|
|
'decline', 'expired', 'failed-application', 'failed-transport',
|
|
|
|
'general-error', 'gone', 'incompatible-parameters', 'media-error',
|
|
|
|
'security-error', 'timeout', 'unsupported-applications',
|
|
|
|
'unsupported-transports']
|
|
|
|
tag = stanza.getTag('reason')
|
|
|
|
if tag:
|
|
|
|
text = tag.getTagData('text')
|
|
|
|
for r in reasons:
|
|
|
|
if tag.getTag(r):
|
|
|
|
reason = r
|
|
|
|
break
|
|
|
|
return (reason, text)
|
|
|
|
|
2007-08-06 17:53:01 +02:00
|
|
|
''' Methods that make/send proper pieces of XML. They check if the session
|
|
|
|
is in appropriate state. '''
|
2009-09-19 17:24:59 +02:00
|
|
|
def __make_jingle(self, action):
|
2007-08-07 18:34:09 +02:00
|
|
|
stanza = xmpp.Iq(typ='set', to=xmpp.JID(self.peerjid))
|
2009-09-13 11:02:49 +02:00
|
|
|
attrs = {'action': action,
|
|
|
|
'sid': self.sid}
|
|
|
|
if action == 'session-initiate':
|
|
|
|
attrs['initiator'] = self.initiator
|
|
|
|
elif action == 'session-accept':
|
|
|
|
attrs['responder'] = self.responder
|
|
|
|
jingle = stanza.addChild('jingle', attrs=attrs, namespace=xmpp.NS_JINGLE)
|
2007-08-06 17:53:01 +02:00
|
|
|
return stanza, jingle
|
|
|
|
|
2009-09-26 12:22:41 +02:00
|
|
|
def __send_error(self, stanza, error, jingle_error=None, text=None):
|
|
|
|
err = xmpp.Error(stanza, error)
|
|
|
|
err.setNamespace(xmpp.NS_STANZAS)
|
|
|
|
if jingle_error:
|
|
|
|
err.setTag(jingle_error, namespace=xmpp.NS_JINGLE_ERRORS)
|
|
|
|
if text:
|
|
|
|
err.setTagData('text', text)
|
|
|
|
self.connection.connection.send(err)
|
|
|
|
self.__dispatch_error(error, jingle_error, text)
|
|
|
|
|
2009-09-19 17:24:59 +02:00
|
|
|
def __append_content(self, jingle, content):
|
2007-08-06 17:53:01 +02:00
|
|
|
''' Append <content/> element to <jingle/> element,
|
|
|
|
with (full=True) or without (full=False) <content/>
|
|
|
|
children. '''
|
2007-08-24 01:42:31 +02:00
|
|
|
jingle.addChild('content',
|
|
|
|
attrs={'name': content.name, 'creator': content.creator})
|
2007-08-06 17:53:01 +02:00
|
|
|
|
2009-09-19 17:24:59 +02:00
|
|
|
def __append_contents(self, jingle):
|
2007-08-06 17:53:01 +02:00
|
|
|
''' Append all <content/> elements to <jingle/>.'''
|
|
|
|
# TODO: integrate with __appendContent?
|
|
|
|
# TODO: parameters 'name', 'content'?
|
|
|
|
for content in self.contents.values():
|
2009-09-19 17:24:59 +02:00
|
|
|
self.__append_content(jingle, content)
|
2007-08-06 17:53:01 +02:00
|
|
|
|
2009-09-19 17:24:59 +02:00
|
|
|
def __session_initiate(self):
|
|
|
|
assert self.state == JingleStates.ended
|
|
|
|
stanza, jingle = self.__make_jingle('session-initiate')
|
|
|
|
self.__append_contents(jingle)
|
2007-08-24 01:42:31 +02:00
|
|
|
self.__broadcastCB(stanza, jingle, None, 'session-initiate-sent')
|
2007-08-07 18:34:09 +02:00
|
|
|
self.connection.connection.send(stanza)
|
2009-09-13 11:02:49 +02:00
|
|
|
self.state = JingleStates.pending
|
2007-08-06 17:53:01 +02:00
|
|
|
|
2009-09-19 17:24:59 +02:00
|
|
|
def __session_accept(self):
|
|
|
|
assert self.state == JingleStates.pending
|
|
|
|
stanza, jingle = self.__make_jingle('session-accept')
|
|
|
|
self.__append_contents(jingle)
|
2007-08-24 01:42:31 +02:00
|
|
|
self.__broadcastCB(stanza, jingle, None, 'session-accept-sent')
|
2007-08-07 18:34:09 +02:00
|
|
|
self.connection.connection.send(stanza)
|
2009-09-17 23:36:26 +02:00
|
|
|
self.state = JingleStates.active
|
2007-08-06 17:53:01 +02:00
|
|
|
|
2009-09-19 17:24:59 +02:00
|
|
|
def __session_info(self, payload=None):
|
2009-09-16 20:41:12 +02:00
|
|
|
assert self.state != JingleStates.ended
|
2009-09-19 17:24:59 +02:00
|
|
|
stanza, jingle = self.__make_jingle('session-info')
|
2007-08-06 17:53:01 +02:00
|
|
|
if payload:
|
|
|
|
jingle.addChild(node=payload)
|
2007-08-07 18:34:09 +02:00
|
|
|
self.connection.connection.send(stanza)
|
2007-08-06 17:53:01 +02:00
|
|
|
|
2009-09-22 22:33:00 +02:00
|
|
|
def _session_terminate(self, reason=None):
|
2009-09-16 20:41:12 +02:00
|
|
|
assert self.state != JingleStates.ended
|
2009-09-19 17:24:59 +02:00
|
|
|
stanza, jingle = self.__make_jingle('session-terminate')
|
2009-09-13 11:02:49 +02:00
|
|
|
if reason is not None:
|
|
|
|
jingle.addChild(node=reason)
|
2007-08-24 15:25:12 +02:00
|
|
|
self.__broadcastAllCB(stanza, jingle, None, 'session-terminate-sent')
|
2007-08-07 18:34:09 +02:00
|
|
|
self.connection.connection.send(stanza)
|
2009-09-17 23:36:26 +02:00
|
|
|
reason, text = self.__reason_from_stanza(jingle)
|
|
|
|
if reason not in ('success', 'cancel', 'decline'):
|
|
|
|
self.__dispatch_error(reason, reason, text)
|
|
|
|
if text:
|
|
|
|
text = '%s (%s)' % (reason, text)
|
|
|
|
else:
|
|
|
|
text = reason
|
2009-10-16 19:04:04 +02:00
|
|
|
self.connection.delete_jingle_session(self.peerjid, self.sid)
|
2009-09-26 14:14:58 +02:00
|
|
|
self.connection.dispatch('JINGLE_DISCONNECTED',
|
|
|
|
(self.peerjid, self.sid, None, text))
|
2009-09-25 19:32:13 +02:00
|
|
|
|
|
|
|
def __content_add(self, content):
|
|
|
|
#TODO: test
|
|
|
|
assert self.state != JingleStates.ended
|
|
|
|
stanza, jingle = self.__make_jingle('content-add')
|
|
|
|
self.__append_content(jingle, content)
|
|
|
|
self.__broadcastCB(stanza, jingle, None, 'content-add-sent')
|
|
|
|
self.connection.connection.send(stanza)
|
2007-08-06 17:53:01 +02:00
|
|
|
|
2009-09-25 19:32:13 +02:00
|
|
|
def __content_accept(self, content):
|
|
|
|
#TODO: test
|
|
|
|
assert self.state != JingleStates.ended
|
|
|
|
stanza, jingle = self.__make_jingle('content-accept')
|
|
|
|
self.__append_content(jingle, content)
|
|
|
|
self.__broadcastCB(stanza, jingle, None, 'content-accept-sent')
|
|
|
|
self.connection.connection.send(stanza)
|
2007-08-06 17:53:01 +02:00
|
|
|
|
2009-09-25 19:32:13 +02:00
|
|
|
def __content_reject(self, content):
|
2009-09-16 20:41:12 +02:00
|
|
|
assert self.state != JingleStates.ended
|
2009-09-25 19:32:13 +02:00
|
|
|
stanza, jingle = self.__make_jingle('content-reject')
|
|
|
|
self.__append_content(jingle, content)
|
|
|
|
self.connection.connection.send(stanza)
|
2009-09-26 21:55:43 +02:00
|
|
|
#TODO: this will fail if content is not an RTP content
|
|
|
|
self.connection.dispatch('JINGLE_DISCONNECTED',
|
|
|
|
(self.peerjid, self.sid, content.media, 'rejected'))
|
2007-08-06 17:53:01 +02:00
|
|
|
|
2009-09-19 17:24:59 +02:00
|
|
|
def __content_modify(self):
|
2009-09-16 20:41:12 +02:00
|
|
|
assert self.state != JingleStates.ended
|
2007-08-06 17:53:01 +02:00
|
|
|
|
2009-09-25 19:32:13 +02:00
|
|
|
def __content_remove(self, content):
|
2009-09-16 20:41:12 +02:00
|
|
|
assert self.state != JingleStates.ended
|
2009-09-25 19:32:13 +02:00
|
|
|
stanza, jingle = self.__make_jingle('content-remove')
|
|
|
|
self.__append_content(jingle, content)
|
|
|
|
self.connection.connection.send(stanza)
|
2009-09-26 14:14:58 +02:00
|
|
|
#TODO: this will fail if content is not an RTP content
|
|
|
|
self.connection.dispatch('JINGLE_DISCONNECTED',
|
|
|
|
(self.peerjid, self.sid, content.media, 'removed'))
|
2007-08-06 17:53:01 +02:00
|
|
|
|
2009-09-17 14:48:15 +02:00
|
|
|
def content_negociated(self, media):
|
|
|
|
self.connection.dispatch('JINGLE_CONNECTED', (self.peerjid, self.sid,
|
|
|
|
media))
|
2007-08-24 01:42:31 +02:00
|
|
|
|
2009-09-19 17:24:59 +02:00
|
|
|
#TODO:
|
|
|
|
#class JingleTransport(object):
|
|
|
|
# ''' An abstraction of a transport in Jingle sessions. '''
|
|
|
|
# def __init__(self):
|
|
|
|
# pass
|
2007-08-06 17:53:01 +02:00
|
|
|
|
|
|
|
|
2007-08-12 23:22:25 +02:00
|
|
|
class JingleContent(object):
|
|
|
|
''' An abstraction of content in Jingle sessions. '''
|
|
|
|
def __init__(self, session, node=None):
|
|
|
|
self.session = session
|
|
|
|
# will be filled by JingleSession.add_content()
|
|
|
|
# don't uncomment these lines, we will catch more buggy code then
|
|
|
|
# (a JingleContent not added to session shouldn't send anything)
|
|
|
|
#self.creator = None
|
|
|
|
#self.name = None
|
2009-09-25 23:05:14 +02:00
|
|
|
self.accepted = False
|
2009-09-27 22:56:09 +02:00
|
|
|
self.sent = False
|
2009-09-13 11:02:49 +02:00
|
|
|
self.candidates = [] # Local transport candidates
|
2009-10-03 22:40:12 +02:00
|
|
|
self.remote_candidates = [] # Remote transport candidates
|
2007-08-12 23:22:25 +02:00
|
|
|
|
2009-09-16 13:55:54 +02:00
|
|
|
self.senders = 'both' #FIXME
|
2009-09-13 11:02:49 +02:00
|
|
|
self.allow_sending = True # Used for stream direction, attribute 'senders'
|
2007-08-07 01:19:57 +02:00
|
|
|
|
2009-09-16 20:41:12 +02:00
|
|
|
self.callbacks = {
|
2007-08-24 01:42:31 +02:00
|
|
|
# these are called when *we* get stanzas
|
2009-09-25 19:32:13 +02:00
|
|
|
'content-accept': [self.__transportInfoCB],
|
|
|
|
'content-add': [self.__transportInfoCB],
|
2007-08-15 11:47:29 +02:00
|
|
|
'content-modify': [],
|
2009-09-28 22:23:48 +02:00
|
|
|
'content-reject': [],
|
2007-08-15 11:47:29 +02:00
|
|
|
'content-remove': [],
|
2009-09-28 22:23:48 +02:00
|
|
|
'description-info': [],
|
|
|
|
'security-info': [],
|
2009-09-13 11:02:49 +02:00
|
|
|
'session-accept': [self.__transportInfoCB],
|
2007-08-15 11:47:29 +02:00
|
|
|
'session-info': [],
|
2009-09-13 11:02:49 +02:00
|
|
|
'session-initiate': [self.__transportInfoCB],
|
|
|
|
'session-terminate': [],
|
2007-08-15 11:47:29 +02:00
|
|
|
'transport-info': [self.__transportInfoCB],
|
2009-09-28 22:23:48 +02:00
|
|
|
'transport-replace': [],
|
|
|
|
'transport-accept': [],
|
|
|
|
'transport-reject': [],
|
2007-08-15 11:47:29 +02:00
|
|
|
'iq-result': [],
|
|
|
|
'iq-error': [],
|
2007-08-24 01:42:31 +02:00
|
|
|
# these are called when *we* sent these stanzas
|
2009-09-25 19:32:13 +02:00
|
|
|
'content-accept-sent': [self.__fillJingleStanza],
|
|
|
|
'content-add-sent': [self.__fillJingleStanza],
|
2009-09-13 11:02:49 +02:00
|
|
|
'session-initiate-sent': [self.__fillJingleStanza],
|
|
|
|
'session-accept-sent': [self.__fillJingleStanza],
|
|
|
|
'session-terminate-sent': [],
|
|
|
|
}
|
2007-08-15 11:47:29 +02:00
|
|
|
|
2009-09-20 22:41:17 +02:00
|
|
|
def is_ready(self):
|
|
|
|
#print '[%s] %s, %s' % (self.media, self.candidates_ready,
|
|
|
|
# self.p2psession.get_property('codecs-ready'))
|
2009-09-27 22:56:09 +02:00
|
|
|
return (self.accepted and self.candidates_ready and not self.sent
|
2009-09-25 23:05:14 +02:00
|
|
|
and self.p2psession.get_property('codecs-ready'))
|
2009-09-20 22:41:17 +02:00
|
|
|
|
2009-09-13 11:02:49 +02:00
|
|
|
def stanzaCB(self, stanza, content, error, action):
|
|
|
|
''' Called when something related to our content was sent by peer. '''
|
|
|
|
if action in self.callbacks:
|
|
|
|
for callback in self.callbacks[action]:
|
|
|
|
callback(stanza, content, error, action)
|
2007-08-15 11:47:29 +02:00
|
|
|
|
|
|
|
def __transportInfoCB(self, stanza, content, error, action):
|
|
|
|
''' Got a new transport candidate. '''
|
|
|
|
candidates = []
|
2009-09-13 11:02:49 +02:00
|
|
|
transport = content.getTag('transport')
|
|
|
|
for candidate in transport.iterTags('candidate'):
|
|
|
|
cand = farsight.Candidate()
|
|
|
|
cand.component_id = int(candidate['component'])
|
|
|
|
cand.ip = str(candidate['ip'])
|
|
|
|
cand.port = int(candidate['port'])
|
|
|
|
cand.foundation = str(candidate['foundation'])
|
|
|
|
#cand.type = farsight.CANDIDATE_TYPE_LOCAL
|
|
|
|
cand.priority = int(candidate['priority'])
|
|
|
|
|
2009-09-16 20:41:12 +02:00
|
|
|
if candidate['protocol'] == 'udp':
|
2009-09-19 17:24:59 +02:00
|
|
|
cand.proto = farsight.NETWORK_PROTOCOL_UDP
|
2007-08-21 01:14:57 +02:00
|
|
|
else:
|
|
|
|
# we actually don't handle properly different tcp options in jingle
|
2009-09-16 20:41:12 +02:00
|
|
|
cand.proto = farsight.NETWORK_PROTOCOL_TCP
|
2009-09-13 11:02:49 +02:00
|
|
|
|
|
|
|
cand.username = str(transport['ufrag'])
|
|
|
|
cand.password = str(transport['pwd'])
|
|
|
|
|
|
|
|
#FIXME: huh?
|
|
|
|
types = {'host': farsight.CANDIDATE_TYPE_HOST,
|
2009-09-16 20:41:12 +02:00
|
|
|
'srflx': farsight.CANDIDATE_TYPE_SRFLX,
|
|
|
|
'prflx': farsight.CANDIDATE_TYPE_PRFLX,
|
|
|
|
'relay': farsight.CANDIDATE_TYPE_RELAY,
|
|
|
|
'multicast': farsight.CANDIDATE_TYPE_MULTICAST}
|
2009-09-13 11:02:49 +02:00
|
|
|
if 'type' in candidate and candidate['type'] in types:
|
|
|
|
cand.type = types[candidate['type']]
|
2009-09-28 22:23:48 +02:00
|
|
|
else:
|
|
|
|
print 'Unknown type %s', candidate['type']
|
2007-08-15 11:47:29 +02:00
|
|
|
candidates.append(cand)
|
2009-09-13 11:02:49 +02:00
|
|
|
#FIXME: connectivity should not be etablished yet
|
|
|
|
# Instead, it should be etablished after session-accept!
|
|
|
|
if len(candidates) > 0:
|
2009-10-03 22:40:12 +02:00
|
|
|
if self.sent:
|
|
|
|
self.p2pstream.set_remote_candidates(candidates)
|
|
|
|
else:
|
|
|
|
self.remote_candidates.extend(candidates)
|
|
|
|
#self.p2pstream.set_remote_candidates(candidates)
|
|
|
|
#print self.media, self.creator, self.name, candidates
|
2007-08-11 23:47:53 +02:00
|
|
|
|
2007-08-12 23:22:25 +02:00
|
|
|
def __content(self, payload=[]):
|
|
|
|
''' Build a XML content-wrapper for our data. '''
|
|
|
|
return xmpp.Node('content',
|
2009-09-13 11:02:49 +02:00
|
|
|
attrs={'name': self.name, 'creator': self.creator},
|
2007-08-12 23:22:25 +02:00
|
|
|
payload=payload)
|
|
|
|
|
2009-09-13 11:02:49 +02:00
|
|
|
def __candidate(self, candidate):
|
|
|
|
types = {farsight.CANDIDATE_TYPE_HOST: 'host',
|
2009-09-28 22:23:48 +02:00
|
|
|
farsight.CANDIDATE_TYPE_SRFLX: 'srflx',
|
2009-09-28 22:31:28 +02:00
|
|
|
farsight.CANDIDATE_TYPE_PRFLX: 'prflx',
|
2009-09-16 20:41:12 +02:00
|
|
|
farsight.CANDIDATE_TYPE_RELAY: 'relay',
|
|
|
|
farsight.CANDIDATE_TYPE_MULTICAST: 'multicast'}
|
2009-09-19 17:24:59 +02:00
|
|
|
attrs = {
|
2009-09-13 11:02:49 +02:00
|
|
|
'component': candidate.component_id,
|
2007-08-15 11:47:29 +02:00
|
|
|
'foundation': '1', # hack
|
|
|
|
'generation': '0',
|
2009-09-13 11:02:49 +02:00
|
|
|
'ip': candidate.ip,
|
2007-08-15 11:47:29 +02:00
|
|
|
'network': '0',
|
2009-09-13 11:02:49 +02:00
|
|
|
'port': candidate.port,
|
|
|
|
'priority': int(candidate.priority), # hack
|
2007-08-15 11:47:29 +02:00
|
|
|
}
|
2009-09-13 11:02:49 +02:00
|
|
|
if candidate.type in types:
|
|
|
|
attrs['type'] = types[candidate.type]
|
2009-09-19 17:24:59 +02:00
|
|
|
if candidate.proto == farsight.NETWORK_PROTOCOL_UDP:
|
2009-09-16 20:41:12 +02:00
|
|
|
attrs['protocol'] = 'udp'
|
2007-08-21 01:14:57 +02:00
|
|
|
else:
|
|
|
|
# we actually don't handle properly different tcp options in jingle
|
2009-09-16 20:41:12 +02:00
|
|
|
attrs['protocol'] = 'tcp'
|
2009-09-13 11:02:49 +02:00
|
|
|
return xmpp.Node('candidate', attrs=attrs)
|
|
|
|
|
2009-09-19 17:24:59 +02:00
|
|
|
def iter_candidates(self):
|
2009-09-13 11:02:49 +02:00
|
|
|
for candidate in self.candidates:
|
|
|
|
yield self.__candidate(candidate)
|
|
|
|
|
|
|
|
def send_candidate(self, candidate):
|
2009-09-19 17:24:59 +02:00
|
|
|
content = self.__content()
|
|
|
|
transport = content.addChild(xmpp.NS_JINGLE_ICE_UDP + ' transport')
|
2009-09-13 11:02:49 +02:00
|
|
|
|
2009-09-19 17:24:59 +02:00
|
|
|
if candidate.username and candidate.password:
|
|
|
|
transport['ufrag'] = candidate.username
|
|
|
|
transport['pwd'] = candidate.password
|
2009-09-13 11:02:49 +02:00
|
|
|
|
2009-09-19 17:24:59 +02:00
|
|
|
transport.addChild(node=self.__candidate(candidate))
|
|
|
|
self.session.send_transport_info(content)
|
2007-08-15 11:47:29 +02:00
|
|
|
|
2009-09-13 11:02:49 +02:00
|
|
|
def __fillJingleStanza(self, stanza, content, error, action):
|
|
|
|
''' Add our things to session-initiate stanza. '''
|
|
|
|
self._fillContent(content)
|
|
|
|
|
2009-09-27 22:56:09 +02:00
|
|
|
self.sent = True
|
|
|
|
|
2009-09-16 20:41:12 +02:00
|
|
|
if self.candidates and self.candidates[0].username and \
|
|
|
|
self.candidates[0].password:
|
2009-09-13 11:02:49 +02:00
|
|
|
attrs = {'ufrag': self.candidates[0].username,
|
2009-09-16 20:41:12 +02:00
|
|
|
'pwd': self.candidates[0].password}
|
2009-09-13 11:02:49 +02:00
|
|
|
else:
|
|
|
|
attrs = {}
|
2009-09-16 20:41:12 +02:00
|
|
|
content.addChild(xmpp.NS_JINGLE_ICE_UDP + ' transport', attrs=attrs,
|
2009-09-19 17:24:59 +02:00
|
|
|
payload=self.iter_candidates())
|
2009-09-13 11:02:49 +02:00
|
|
|
|
2009-09-25 19:32:13 +02:00
|
|
|
def destroy(self):
|
|
|
|
self.callbacks = None
|
|
|
|
del self.session.contents[(self.creator, self.name)]
|
|
|
|
|
|
|
|
|
2009-09-13 11:02:49 +02:00
|
|
|
class JingleRTPContent(JingleContent):
|
2009-09-16 13:55:54 +02:00
|
|
|
def __init__(self, session, media, node=None):
|
2009-09-13 11:02:49 +02:00
|
|
|
JingleContent.__init__(self, session, node)
|
2009-09-16 13:55:54 +02:00
|
|
|
self.media = media
|
2009-09-16 22:35:11 +02:00
|
|
|
self.farsight_media = {'audio': farsight.MEDIA_TYPE_AUDIO,
|
|
|
|
'video': farsight.MEDIA_TYPE_VIDEO}[media]
|
2009-09-13 11:02:49 +02:00
|
|
|
self.got_codecs = False
|
|
|
|
|
2009-09-17 23:36:26 +02:00
|
|
|
self.candidates_ready = False # True when local candidates are prepared
|
|
|
|
|
2009-09-13 11:02:49 +02:00
|
|
|
self.callbacks['session-initiate'] += [self.__getRemoteCodecsCB]
|
2009-09-26 23:07:09 +02:00
|
|
|
self.callbacks['content-add'] += [self.__getRemoteCodecsCB]
|
|
|
|
self.callbacks['content-accept'] += [self.__getRemoteCodecsCB,
|
|
|
|
self.__contentAcceptCB]
|
|
|
|
self.callbacks['session-accept'] += [self.__getRemoteCodecsCB,
|
|
|
|
self.__contentAcceptCB]
|
|
|
|
self.callbacks['session-accept-sent'] += [self.__contentAcceptCB]
|
|
|
|
self.callbacks['content-accept-sent'] += [self.__contentAcceptCB]
|
2009-09-16 13:55:54 +02:00
|
|
|
self.callbacks['session-terminate'] += [self.__stop]
|
|
|
|
self.callbacks['session-terminate-sent'] += [self.__stop]
|
2009-09-13 11:02:49 +02:00
|
|
|
|
2009-09-19 17:24:59 +02:00
|
|
|
def setup_stream(self):
|
2009-09-13 11:02:49 +02:00
|
|
|
# pipeline and bus
|
|
|
|
self.pipeline = gst.Pipeline()
|
|
|
|
bus = self.pipeline.get_bus()
|
|
|
|
bus.add_signal_watch()
|
|
|
|
bus.connect('message', self._on_gst_message)
|
|
|
|
|
|
|
|
# conference
|
|
|
|
self.conference = gst.element_factory_make('fsrtpconference')
|
|
|
|
self.conference.set_property("sdes-cname", self.session.ourjid)
|
|
|
|
self.pipeline.add(self.conference)
|
|
|
|
self.funnel = None
|
|
|
|
|
2009-09-16 22:35:11 +02:00
|
|
|
self.p2psession = self.conference.new_session(self.farsight_media)
|
|
|
|
|
|
|
|
participant = self.conference.new_participant(self.session.peerjid)
|
2009-09-27 22:56:09 +02:00
|
|
|
#FIXME: Consider a workaround, here...
|
|
|
|
# pidgin and telepathy-gabble don't follow the XEP, and it won't work
|
|
|
|
# due to bad controlling-mode
|
2009-09-16 22:35:11 +02:00
|
|
|
params = {'controlling-mode': self.session.weinitiate,# 'debug': False}
|
|
|
|
'stun-ip': '69.0.208.27', 'debug': False}
|
|
|
|
|
|
|
|
self.p2pstream = self.p2psession.new_stream(participant,
|
|
|
|
farsight.DIRECTION_RECV, 'nice', params)
|
|
|
|
|
2009-09-16 13:55:54 +02:00
|
|
|
def _fillContent(self, content):
|
2009-09-16 20:41:12 +02:00
|
|
|
content.addChild(xmpp.NS_JINGLE_RTP + ' description',
|
2009-09-19 17:24:59 +02:00
|
|
|
attrs={'media': self.media}, payload=self.iter_codecs())
|
2009-09-16 13:55:54 +02:00
|
|
|
|
2009-09-25 19:32:13 +02:00
|
|
|
def _setup_funnel(self):
|
|
|
|
self.funnel = gst.element_factory_make('fsfunnel')
|
|
|
|
self.pipeline.add(self.funnel)
|
|
|
|
self.funnel.set_state(gst.STATE_PLAYING)
|
|
|
|
self.sink.set_state(gst.STATE_PLAYING)
|
|
|
|
self.funnel.link(self.sink)
|
|
|
|
|
|
|
|
def _on_src_pad_added(self, stream, pad, codec):
|
|
|
|
if not self.funnel:
|
|
|
|
self._setup_funnel()
|
|
|
|
pad.link(self.funnel.get_pad('sink%d'))
|
|
|
|
|
2009-09-13 11:02:49 +02:00
|
|
|
def _on_gst_message(self, bus, message):
|
|
|
|
if message.type == gst.MESSAGE_ELEMENT:
|
|
|
|
name = message.structure.get_name()
|
|
|
|
if name == 'farsight-new-active-candidate-pair':
|
|
|
|
pass
|
|
|
|
elif name == 'farsight-recv-codecs-changed':
|
|
|
|
pass
|
2009-09-17 23:36:26 +02:00
|
|
|
elif name == 'farsight-codecs-changed':
|
2009-09-25 19:32:13 +02:00
|
|
|
if self.is_ready():
|
|
|
|
self.session.on_session_state_changed(self)
|
2009-09-28 22:23:48 +02:00
|
|
|
#TODO: description-info
|
2009-09-13 11:02:49 +02:00
|
|
|
elif name == 'farsight-local-candidates-prepared':
|
2009-09-17 23:36:26 +02:00
|
|
|
self.candidates_ready = True
|
2009-09-25 19:32:13 +02:00
|
|
|
if self.is_ready():
|
|
|
|
self.session.on_session_state_changed(self)
|
2009-09-13 11:02:49 +02:00
|
|
|
elif name == 'farsight-new-local-candidate':
|
|
|
|
candidate = message.structure['candidate']
|
|
|
|
self.candidates.append(candidate)
|
2009-09-17 23:36:26 +02:00
|
|
|
if self.candidates_ready:
|
2009-09-16 20:41:12 +02:00
|
|
|
#FIXME: Is this case even possible?
|
2009-09-13 11:02:49 +02:00
|
|
|
self.send_candidate(candidate)
|
|
|
|
elif name == 'farsight-component-state-changed':
|
|
|
|
state = message.structure['state']
|
|
|
|
print message.structure['component'], state
|
2009-09-22 22:33:00 +02:00
|
|
|
if state == farsight.STREAM_STATE_FAILED:
|
|
|
|
reason = xmpp.Node('reason')
|
|
|
|
reason.setTag('failed-transport')
|
|
|
|
self.session._session_terminate(reason)
|
2009-09-13 11:02:49 +02:00
|
|
|
elif name == 'farsight-error':
|
|
|
|
print 'Farsight error #%d!' % message.structure['error-no']
|
|
|
|
print 'Message: %s' % message.structure['error-msg']
|
|
|
|
print 'Debug: %s' % message.structure['debug-msg']
|
2009-09-16 13:55:54 +02:00
|
|
|
else:
|
|
|
|
print name
|
2009-09-13 11:02:49 +02:00
|
|
|
|
2009-09-26 23:07:09 +02:00
|
|
|
def __contentAcceptCB(self, stanza, content, error, action):
|
|
|
|
if self.accepted:
|
2009-10-03 22:40:12 +02:00
|
|
|
if len(self.remote_candidates) > 0:
|
|
|
|
self.p2pstream.set_remote_candidates(self.remote_candidates)
|
|
|
|
self.remote_candidates = []
|
2009-09-26 23:07:09 +02:00
|
|
|
#TODO: farsight.DIRECTION_BOTH only if senders='both'
|
|
|
|
self.p2pstream.set_property('direction', farsight.DIRECTION_BOTH)
|
|
|
|
self.session.content_negociated(self.media)
|
|
|
|
|
2009-09-13 11:02:49 +02:00
|
|
|
def __getRemoteCodecsCB(self, stanza, content, error, action):
|
|
|
|
''' Get peer codecs from what we get from peer. '''
|
2009-09-19 17:24:59 +02:00
|
|
|
if self.got_codecs:
|
|
|
|
return
|
2009-09-13 11:02:49 +02:00
|
|
|
|
|
|
|
codecs = []
|
|
|
|
for codec in content.getTag('description').iterTags('payload-type'):
|
|
|
|
c = farsight.Codec(int(codec['id']), codec['name'],
|
2009-09-16 22:35:11 +02:00
|
|
|
self.farsight_media, int(codec['clockrate']))
|
2009-09-13 11:02:49 +02:00
|
|
|
if 'channels' in codec:
|
|
|
|
c.channels = int(codec['channels'])
|
|
|
|
else:
|
|
|
|
c.channels = 1
|
2009-09-16 20:41:12 +02:00
|
|
|
c.optional_params = [(str(p['name']), str(p['value'])) for p in \
|
|
|
|
codec.iterTags('parameter')]
|
2009-09-13 11:02:49 +02:00
|
|
|
codecs.append(c)
|
|
|
|
|
2009-09-19 17:24:59 +02:00
|
|
|
if len(codecs) > 0:
|
|
|
|
#FIXME: Handle this case:
|
|
|
|
# glib.GError: There was no intersection between the remote codecs and
|
|
|
|
# the local ones
|
|
|
|
self.p2pstream.set_remote_codecs(codecs)
|
|
|
|
self.got_codecs = True
|
2009-09-13 11:02:49 +02:00
|
|
|
|
2009-09-19 17:24:59 +02:00
|
|
|
def iter_codecs(self):
|
|
|
|
codecs = self.p2psession.get_property('codecs')
|
2007-08-15 11:47:29 +02:00
|
|
|
for codec in codecs:
|
2009-09-19 17:24:59 +02:00
|
|
|
attrs = {'name': codec.encoding_name,
|
2009-09-16 20:41:12 +02:00
|
|
|
'id': codec.id,
|
|
|
|
'channels': codec.channels}
|
2009-09-19 17:24:59 +02:00
|
|
|
if codec.clock_rate:
|
|
|
|
attrs['clockrate'] = codec.clock_rate
|
2009-09-13 11:02:49 +02:00
|
|
|
if codec.optional_params:
|
2009-09-19 17:24:59 +02:00
|
|
|
payload = (xmpp.Node('parameter', {'name': name, 'value': value})
|
2009-09-16 20:41:12 +02:00
|
|
|
for name, value in codec.optional_params)
|
2009-09-19 17:24:59 +02:00
|
|
|
else: payload = ()
|
|
|
|
yield xmpp.Node('payload-type', attrs, payload)
|
2007-08-06 17:53:01 +02:00
|
|
|
|
2009-09-16 13:55:54 +02:00
|
|
|
def __stop(self, *things):
|
|
|
|
self.pipeline.set_state(gst.STATE_NULL)
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
self.__stop()
|
|
|
|
|
2009-09-25 19:32:13 +02:00
|
|
|
def destroy(self):
|
|
|
|
JingleContent.destroy(self)
|
|
|
|
self.p2pstream.disconnect_by_func(self._on_src_pad_added)
|
|
|
|
self.pipeline.get_bus().disconnect_by_func(self._on_gst_message)
|
|
|
|
|
2009-09-13 11:02:49 +02:00
|
|
|
|
|
|
|
class JingleVoIP(JingleRTPContent):
|
|
|
|
''' Jingle VoIP sessions consist of audio content transported
|
|
|
|
over an ICE UDP protocol. '''
|
|
|
|
def __init__(self, session, node=None):
|
2009-09-16 13:55:54 +02:00
|
|
|
JingleRTPContent.__init__(self, session, 'audio', node)
|
2009-09-19 17:24:59 +02:00
|
|
|
self.setup_stream()
|
2009-09-13 11:02:49 +02:00
|
|
|
|
|
|
|
|
2007-08-24 01:42:31 +02:00
|
|
|
''' Things to control the gstreamer's pipeline '''
|
2009-09-19 17:24:59 +02:00
|
|
|
def setup_stream(self):
|
|
|
|
JingleRTPContent.setup_stream(self)
|
2007-08-24 01:42:31 +02:00
|
|
|
|
2009-09-13 11:02:49 +02:00
|
|
|
# Configure SPEEX
|
2009-09-27 22:56:09 +02:00
|
|
|
# Workaround for psi (not needed since rev
|
|
|
|
# 147aedcea39b43402fe64c533d1866a25449888a):
|
|
|
|
# place 16kHz before 8kHz, as buggy psi versions will take in
|
|
|
|
# account only the first codec
|
|
|
|
|
2009-09-13 11:02:49 +02:00
|
|
|
codecs = [farsight.Codec(farsight.CODEC_ID_ANY, 'SPEEX',
|
2009-09-27 22:56:09 +02:00
|
|
|
farsight.MEDIA_TYPE_AUDIO, 16000),
|
2009-09-16 20:41:12 +02:00
|
|
|
farsight.Codec(farsight.CODEC_ID_ANY, 'SPEEX',
|
2009-09-27 22:56:09 +02:00
|
|
|
farsight.MEDIA_TYPE_AUDIO, 8000)]
|
2009-09-13 11:02:49 +02:00
|
|
|
self.p2psession.set_codec_preferences(codecs)
|
2007-08-24 01:42:31 +02:00
|
|
|
|
|
|
|
# the local parts
|
|
|
|
# TODO: use gconfaudiosink?
|
2009-09-13 11:02:49 +02:00
|
|
|
# sink = get_first_gst_element(['alsasink', 'osssink', 'autoaudiosink'])
|
2009-09-25 19:32:13 +02:00
|
|
|
self.sink = gst.element_factory_make('alsasink')
|
|
|
|
self.sink.set_property('sync', False)
|
2009-09-13 11:02:49 +02:00
|
|
|
#sink.set_property('latency-time', 20000)
|
|
|
|
#sink.set_property('buffer-time', 80000)
|
2007-08-24 01:42:31 +02:00
|
|
|
|
|
|
|
# TODO: use gconfaudiosrc?
|
2009-09-16 22:35:11 +02:00
|
|
|
src_mic = gst.element_factory_make('alsasrc')
|
|
|
|
src_mic.set_property('blocksize', 320)
|
2007-08-24 01:42:31 +02:00
|
|
|
|
|
|
|
self.mic_volume = gst.element_factory_make('volume')
|
2009-09-16 13:55:54 +02:00
|
|
|
self.mic_volume.set_property('volume', 1)
|
2007-08-24 01:42:31 +02:00
|
|
|
|
|
|
|
# link gst elements
|
2009-09-25 19:32:13 +02:00
|
|
|
self.pipeline.add(self.sink, src_mic, self.mic_volume)
|
2009-09-16 22:35:11 +02:00
|
|
|
src_mic.link(self.mic_volume)
|
2009-09-13 11:02:49 +02:00
|
|
|
|
2009-09-16 20:41:12 +02:00
|
|
|
self.mic_volume.get_pad('src').link(self.p2psession.get_property(
|
|
|
|
'sink-pad'))
|
2009-09-25 19:32:13 +02:00
|
|
|
self.p2pstream.connect('src-pad-added', self._on_src_pad_added)
|
2009-09-13 11:02:49 +02:00
|
|
|
|
|
|
|
# The following is needed for farsight to process ICE requests:
|
2009-09-16 13:55:54 +02:00
|
|
|
self.pipeline.set_state(gst.STATE_PLAYING)
|
2007-08-24 01:42:31 +02:00
|
|
|
|
2009-09-17 23:36:26 +02:00
|
|
|
|
2009-09-16 22:35:11 +02:00
|
|
|
class JingleVideo(JingleRTPContent):
|
|
|
|
def __init__(self, session, node=None):
|
|
|
|
JingleRTPContent.__init__(self, session, 'video', node)
|
2009-09-19 17:24:59 +02:00
|
|
|
self.setup_stream()
|
2009-09-16 22:35:11 +02:00
|
|
|
|
|
|
|
''' Things to control the gstreamer's pipeline '''
|
2009-09-19 17:24:59 +02:00
|
|
|
def setup_stream(self):
|
2009-09-16 22:35:11 +02:00
|
|
|
#TODO: Everything is not working properly:
|
|
|
|
# sometimes, one window won't show up,
|
|
|
|
# sometimes it'll freeze...
|
2009-09-19 17:24:59 +02:00
|
|
|
JingleRTPContent.setup_stream(self)
|
2009-09-16 22:35:11 +02:00
|
|
|
# the local parts
|
2009-09-25 19:32:13 +02:00
|
|
|
src_vid = gst.element_factory_make('videotestsrc')
|
2009-09-26 21:55:43 +02:00
|
|
|
src_vid.set_property('is-live', True)
|
2009-09-16 22:35:11 +02:00
|
|
|
videoscale = gst.element_factory_make('videoscale')
|
|
|
|
caps = gst.element_factory_make('capsfilter')
|
|
|
|
caps.set_property('caps', gst.caps_from_string('video/x-raw-yuv, width=320, height=240'))
|
|
|
|
colorspace = gst.element_factory_make('ffmpegcolorspace')
|
|
|
|
|
|
|
|
self.pipeline.add(src_vid, videoscale, caps, colorspace)
|
|
|
|
gst.element_link_many(src_vid, videoscale, caps, colorspace)
|
|
|
|
|
2009-09-25 19:32:13 +02:00
|
|
|
self.sink = gst.element_factory_make('xvimagesink')
|
|
|
|
self.pipeline.add(self.sink)
|
2009-09-16 22:35:11 +02:00
|
|
|
|
|
|
|
colorspace.get_pad('src').link(self.p2psession.get_property('sink-pad'))
|
2009-09-25 19:32:13 +02:00
|
|
|
self.p2pstream.connect('src-pad-added', self._on_src_pad_added)
|
2009-09-16 22:35:11 +02:00
|
|
|
|
|
|
|
# The following is needed for farsight to process ICE requests:
|
|
|
|
self.pipeline.set_state(gst.STATE_PLAYING)
|
2009-09-13 11:02:49 +02:00
|
|
|
|
2009-09-17 23:36:26 +02:00
|
|
|
|
2007-08-06 17:53:01 +02:00
|
|
|
class ConnectionJingle(object):
|
|
|
|
''' This object depends on that it is a part of Connection class. '''
|
|
|
|
def __init__(self):
|
|
|
|
# dictionary: (jid, sessionid) => JingleSession object
|
|
|
|
self.__sessions = {}
|
|
|
|
|
|
|
|
# dictionary: (jid, iq stanza id) => JingleSession object,
|
|
|
|
# one time callbacks
|
|
|
|
self.__iq_responses = {}
|
|
|
|
|
2009-09-19 17:24:59 +02:00
|
|
|
def add_jingle(self, jingle):
|
2007-08-06 17:53:01 +02:00
|
|
|
''' Add a jingle session to a jingle stanza dispatcher
|
|
|
|
jingle - a JingleSession object.
|
|
|
|
'''
|
2009-09-16 20:41:12 +02:00
|
|
|
self.__sessions[(jingle.peerjid, jingle.sid)] = jingle
|
2007-08-06 17:53:01 +02:00
|
|
|
|
2009-10-16 19:04:04 +02:00
|
|
|
def delete_jingle_session(self, peerjid, sid):
|
2007-08-06 17:53:01 +02:00
|
|
|
''' Remove a jingle session from a jingle stanza dispatcher '''
|
2009-10-16 19:04:04 +02:00
|
|
|
key = (peerjid, sid)
|
|
|
|
if key in self.__sessions:
|
|
|
|
#FIXME: Move this elsewhere?
|
|
|
|
for content in self.__sessions[key].contents.values():
|
|
|
|
content.destroy()
|
|
|
|
self.__sessions[key].callbacks = []
|
|
|
|
del self.__sessions[key]
|
2007-08-06 17:53:01 +02:00
|
|
|
|
2007-08-07 01:19:57 +02:00
|
|
|
def _JingleCB(self, con, stanza):
|
2007-08-06 17:53:01 +02:00
|
|
|
''' The jingle stanza dispatcher.
|
|
|
|
Route jingle stanza to proper JingleSession object,
|
|
|
|
or create one if it is a new session.
|
|
|
|
TODO: Also check if the stanza isn't an error stanza, if so
|
|
|
|
route it adequatelly.'''
|
|
|
|
|
|
|
|
# get data
|
2009-09-25 20:41:22 +02:00
|
|
|
jid = helpers.get_full_jid_from_iq(stanza)
|
2007-08-06 17:53:01 +02:00
|
|
|
id = stanza.getID()
|
|
|
|
|
|
|
|
if (jid, id) in self.__iq_responses.keys():
|
|
|
|
self.__iq_responses[(jid, id)].stanzaCB(stanza)
|
|
|
|
del self.__iq_responses[(jid, id)]
|
|
|
|
raise xmpp.NodeProcessed
|
|
|
|
|
|
|
|
jingle = stanza.getTag('jingle')
|
2007-08-07 01:19:57 +02:00
|
|
|
if not jingle: return
|
2007-08-06 17:53:01 +02:00
|
|
|
sid = jingle.getAttr('sid')
|
|
|
|
|
|
|
|
# do we need to create a new jingle object
|
|
|
|
if (jid, sid) not in self.__sessions:
|
2009-09-28 22:23:48 +02:00
|
|
|
#TODO: tie-breaking and other things...
|
2007-08-07 18:34:09 +02:00
|
|
|
newjingle = JingleSession(con=self, weinitiate=False, jid=jid, sid=sid)
|
2009-09-19 17:24:59 +02:00
|
|
|
self.add_jingle(newjingle)
|
2007-08-06 17:53:01 +02:00
|
|
|
|
|
|
|
# we already have such session in dispatcher...
|
2007-08-07 18:34:09 +02:00
|
|
|
self.__sessions[(jid, sid)].stanzaCB(stanza)
|
|
|
|
|
|
|
|
raise xmpp.NodeProcessed
|
2007-08-06 17:53:01 +02:00
|
|
|
|
2009-09-13 11:02:49 +02:00
|
|
|
def startVoIP(self, jid):
|
2009-09-25 19:32:13 +02:00
|
|
|
if self.get_jingle_session(jid, media='audio'):
|
2009-09-26 21:55:43 +02:00
|
|
|
return self.get_jingle_session(jid, media='audio').sid
|
2009-09-25 13:52:44 +02:00
|
|
|
jingle = self.get_jingle_session(jid, media='video')
|
|
|
|
if jingle:
|
|
|
|
jingle.add_content('voice', JingleVoIP(jingle))
|
|
|
|
else:
|
|
|
|
jingle = JingleSession(self, weinitiate=True, jid=jid)
|
|
|
|
self.add_jingle(jingle)
|
|
|
|
jingle.add_content('voice', JingleVoIP(jingle))
|
|
|
|
jingle.start_session()
|
2009-09-17 14:48:15 +02:00
|
|
|
return jingle.sid
|
2009-09-16 20:41:12 +02:00
|
|
|
|
2009-09-25 13:52:44 +02:00
|
|
|
def startVideoIP(self, jid):
|
2009-09-25 19:32:13 +02:00
|
|
|
if self.get_jingle_session(jid, media='video'):
|
2009-09-26 21:55:43 +02:00
|
|
|
return self.get_jingle_session(jid, media='video').sid
|
2009-09-25 13:52:44 +02:00
|
|
|
jingle = self.get_jingle_session(jid, media='audio')
|
|
|
|
if jingle:
|
|
|
|
jingle.add_content('video', JingleVideo(jingle))
|
|
|
|
else:
|
|
|
|
jingle = JingleSession(self, weinitiate=True, jid=jid)
|
|
|
|
self.add_jingle(jingle)
|
|
|
|
jingle.add_content('video', JingleVideo(jingle))
|
|
|
|
jingle.start_session()
|
|
|
|
return jingle.sid
|
|
|
|
|
|
|
|
def get_jingle_session(self, jid, sid=None, media=None):
|
|
|
|
if sid:
|
|
|
|
if (jid, sid) in self.__sessions:
|
|
|
|
return self.__sessions[(jid, sid)]
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
elif media:
|
2009-09-25 19:32:13 +02:00
|
|
|
if media not in ('audio', 'video'):
|
2009-09-25 13:52:44 +02:00
|
|
|
return None
|
|
|
|
for session in self.__sessions.values():
|
2009-09-25 19:32:13 +02:00
|
|
|
if session.peerjid == jid and session.get_content(media):
|
|
|
|
return session
|
|
|
|
|
2009-09-25 13:52:44 +02:00
|
|
|
return None
|