[telepathy-butterfly/master] Emits signal on streams events

Louis-Francis Ratté-Boulianne louis-francis.ratte-boulianne at collabora.co.uk
Thu Sep 10 06:48:12 PDT 2009


---
 butterfly/channel/media.py         |   16 +++++++++++++---
 butterfly/media/session_handler.py |   19 ++++++++++---------
 butterfly/media/stream_handler.py  |   29 ++++++++++++++++++++++-------
 3 files changed, 45 insertions(+), 19 deletions(-)

diff --git a/butterfly/channel/media.py b/butterfly/channel/media.py
index fdf4105..f050dae 100644
--- a/butterfly/channel/media.py
+++ b/butterfly/channel/media.py
@@ -54,7 +54,7 @@ class ButterflyMediaChannel(
         self._call = call
         self._handle = handle
 
-        self._session_handler = ButterflySessionHandler(self._conn, self, call)
+        self._session_handler = ButterflySessionHandler(self._conn, self, call.media_session)
         self.NewSessionHandler(self._session_handler, self._session_handler.subtype)
 
         self.GroupFlagsChanged(telepathy.CHANNEL_GROUP_FLAG_CAN_REMOVE, 0)
@@ -85,6 +85,8 @@ class ButterflyMediaChannel(
         streams = dbus.Array([], signature="a(uuuuuu)")
         for type in types:
             handler = self._session_handler.CreateStream(type, 3)
+            handler.connect("state-changed", self.on_stream_state_changed)
+            handler.connect("error", self.on_stream_error)
             streams.append((handler.id, self._handle, handler.type,
                 handler.state, handler.direction, handler.pending_send))
         self._call.invite()
@@ -155,6 +157,8 @@ class ButterflyMediaChannel(
     def on_stream_created(self, stream):
         print "Media Stream created upon peer request"
         handler = self._session_handler.HandleStream(stream)
+        handler.connect("state-changed", self.on_stream_state_changed)
+        handler.connect("error", self.on_stream_error)
 
     #papyon.event.media.MediaSessionEventInterface
     def on_stream_added(self, stream):
@@ -178,8 +182,14 @@ class ButterflyMediaChannel(
             print "%s is now offline, closing channel" % contact
             self.Close()
 
-    def on_stream_state_changed(self, id, state):
-        self.StreamStateChanged(id, state)
+    #StreamHandler event
+    def on_stream_error(self, handler, error, message):
+        self.StreamError(handler.id, error, message)
+        self._call.media_session.remove_stream(handler.stream)
+
+    #StreamHandler event
+    def on_stream_state_changed(self, handler, state):
+        self.StreamStateChanged(handler.id, state)
 
     @async
     def __add_initial_participants(self):
diff --git a/butterfly/media/session_handler.py b/butterfly/media/session_handler.py
index ffca974..6137995 100644
--- a/butterfly/media/session_handler.py
+++ b/butterfly/media/session_handler.py
@@ -32,13 +32,12 @@ from papyon.sip.media import MediaSessionType
 __all__ = ['ButterflySessionHandler']
 
 class ButterflySessionHandler (telepathy.server.MediaSessionHandler):
-    def __init__(self, connection, channel, call):
+    def __init__(self, connection, channel, session):
         self._conn = connection
-        self._channel = channel
-        self._call = call
+        self._session = session
         self._stream_handlers = {}
         self._next_stream_id = 0
-        self._type = call.media_session.type
+        self._type = session.type
         self._subtype = self._type is MediaSessionType.WEBCAM and "msn" or "rtp"
         self._ready = False
         self._pending_handlers = []
@@ -93,7 +92,7 @@ class ButterflySessionHandler (telepathy.server.MediaSessionHandler):
             media_type = "audio"
         else:
             media_type = "video"
-        stream = self._call.media_session.add_stream(media_type, direction, True)
+        stream = self._session.create_stream(media_type, direction, True)
         handler = self.HandleStream(stream)
         self._session.add_pending_stream(stream)
         return handler
@@ -104,8 +103,6 @@ class ButterflySessionHandler (telepathy.server.MediaSessionHandler):
         self._stream_handlers[handler.id] = handler
         return handler
 
-    def RemoveStream(self, id):
-        del self._stream_handlers[id]
     def NewStream(self, stream=None, handler=None):
         if handler is None:
             handler = self.FindStream(stream)
@@ -117,5 +114,9 @@ class ButterflySessionHandler (telepathy.server.MediaSessionHandler):
         self.NewStreamHandler(path, handler.id, handler.type, handler.direction)
         return handler
 
-    def on_stream_state_changed(self, id, state):
-        self._channel.on_stream_state_changed(id, state)
+    def RemoveStream(self, id):
+        print "Session remove stream handler", id
+        if id in self._stream_handlers:
+            handler = self._stream_handlers[id]
+            handler.remove_from_connection()
+            del self._stream_handlers[id]
diff --git a/butterfly/media/stream_handler.py b/butterfly/media/stream_handler.py
index 6a566c0..7937b40 100644
--- a/butterfly/media/stream_handler.py
+++ b/butterfly/media/stream_handler.py
@@ -41,11 +41,12 @@ class ButterflyStreamHandler (
     def __init__(self, connection, session, stream):
         self._id = session.next_stream_id
         path = session.get_stream_path(self._id)
-
+        print "New stream %i" % self._id
         self._conn = connection
         self._session = session
         self._stream = stream
         self._interfaces = set()
+        self._callbacks = {}
 
         self._state = 1
         self._direction = stream.direction
@@ -89,6 +90,10 @@ class ButterflyStreamHandler (
         return self._state
 
     @property
+    def stream(self):
+        return self._stream
+
+    @property
     def created_locally(self):
         return self._stream.controlling
 
@@ -122,13 +127,21 @@ class ButterflyStreamHandler (
         self._direction = direction
         self._pending_send = pending_send
 
+    def connect(self, signal, cb):
+        self._callbacks.setdefault(signal, []).append(cb)
+
+    def emit(self, signal, *args):
+        callbacks = self._callbacks.get(signal, [])
+        for cb in callbacks:
+            cb(self, *args)
+
     def Ready(self, codecs):
-        print "StreamReady : ", codecs
+        print "Stream %i is ready" % self._id
         webcam = (self._session.type is MediaSessionType.WEBCAM)
 
-        if self._remote_candidates is not None:
+        if self._remote_candidates:
             self.SetRemoteCandidateList(self._remote_candidates)
-        if self._remote_codecs is not None and not webcam:
+        if self._remote_codecs:
             self.SetRemoteCodecs(self._remote_codecs)
 
         self.SetStreamPlaying(self._direction &
@@ -137,16 +150,16 @@ class ButterflyStreamHandler (
                 telepathy.MEDIA_STREAM_DIRECTION_SEND)
 
         if self.created_locally or webcam:
-            print "Set local codecs"
             self.SetLocalCodecs(codecs)
 
     def StreamState(self, state):
         print "StreamState : ", state
         self._state = state
-        self._session.on_stream_state_changed(self.id, state)
+        self.emit("state-changed", state)
 
     def Error(self, code, message):
         print "StreamError - %i - %s" % (code, message)
+        self.emit("error", code, message)
         self.Close()
 
     def NewNativeCandidate(self, id, transports):
@@ -195,7 +208,9 @@ class ButterflyStreamHandler (
 
     #papyon.event.MediaStreamEventInterface
     def on_stream_closed(self):
-        print "Stream Closed"
+        print "Stream %i closed" % self._id
+        self._state = 0
+        self.emit("state-changed", self._state)
         self.Close()
 
     def convert_sdp_codecs(self, codecs):
-- 
1.5.6.5




More information about the telepathy-commits mailing list