~emesene-team/emesene/master

« back to all changes in this revision

Viewing changes to emesene/e3/papylib/PapyConference.py

  • Committer: Riccardo (C10uD)
  • Date: 2012-07-01 16:54:34 UTC
  • Revision ID: git-v1:17ed298a3acb830f76aa2703351993cff749ed35
import2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# papyon - a python client library for Msn
 
4
#
 
5
# Copyright (C) 2009 Collabora Ltd.
 
6
#
 
7
# This program is free software; you can redistribute it and/or modify
 
8
# it under the terms of the GNU General Public License as published by
 
9
# the Free Software Foundation; either version 2 of the License, or
 
10
# (at your option) any later version.
 
11
#
 
12
# This program is distributed in the hope that it will be useful,
 
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
# GNU General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU General Public License
 
18
# along with this program; if not, write to the Free Software
 
19
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
20
 
 
21
from papyon.media import *
 
22
from papyon.event.media import *
 
23
 
 
24
import pygst
 
25
pygst.require('0.10')
 
26
 
 
27
try:
 
28
    import farstream as farsight
 
29
except ImportError: # Hello older systems
 
30
    import farsight
 
31
 
 
32
import gobject
 
33
import gst
 
34
import logging
 
35
 
 
36
logger = logging.getLogger("papylib.conference")
 
37
 
 
38
codecs_definitions = {
 
39
    "audio" : [
 
40
        (114, "x-msrta", farsight.MEDIA_TYPE_AUDIO, 16000),
 
41
        (111, "SIREN", farsight.MEDIA_TYPE_AUDIO, 16000),
 
42
        (112, "G7221", farsight.MEDIA_TYPE_AUDIO, 16000),
 
43
        (115, "x-msrta", farsight.MEDIA_TYPE_AUDIO, 8000),
 
44
        (116, "SIREN", farsight.MEDIA_TYPE_AUDIO, 8000),
 
45
        (4, "G723", farsight.MEDIA_TYPE_AUDIO, 8000),
 
46
        (8, "PCMA", farsight.MEDIA_TYPE_AUDIO, 8000),
 
47
        (0, "PCMU", farsight.MEDIA_TYPE_AUDIO, 8000),
 
48
        (97, "RED", farsight.MEDIA_TYPE_AUDIO, 8000),
 
49
        (101, "telephone-event", farsight.MEDIA_TYPE_AUDIO, 8000)
 
50
    ],
 
51
    "video" : [
 
52
        (121, "x-rtvc1", farsight.MEDIA_TYPE_VIDEO, 90000),
 
53
        (34, "H263", farsight.MEDIA_TYPE_VIDEO, 90000)
 
54
    ]
 
55
}
 
56
 
 
57
types = {
 
58
    0 : None,
 
59
    farsight.CANDIDATE_TYPE_HOST  : "host",
 
60
    farsight.CANDIDATE_TYPE_SRFLX : "srflx",
 
61
    farsight.CANDIDATE_TYPE_PRFLX : "prflx",
 
62
    farsight.CANDIDATE_TYPE_RELAY : "relay"
 
63
}
 
64
 
 
65
protos = {
 
66
    farsight.NETWORK_PROTOCOL_TCP : "TCP",
 
67
    farsight.NETWORK_PROTOCOL_UDP : "UDP"
 
68
}
 
69
 
 
70
media_names = {
 
71
    farsight.MEDIA_TYPE_AUDIO : "audio",
 
72
    farsight.MEDIA_TYPE_VIDEO : "video"
 
73
}
 
74
 
 
75
media_types = {
 
76
    "audio" : farsight.MEDIA_TYPE_AUDIO,
 
77
    "video" : farsight.MEDIA_TYPE_VIDEO
 
78
}
 
79
 
 
80
 
 
81
class Conference(gobject.GObject):
 
82
 
 
83
    def __init__(self):
 
84
        gobject.GObject.__init__(self)
 
85
 
 
86
    def set_source(self, source):
 
87
        pass
 
88
 
 
89
 
 
90
class MediaSessionHandler(MediaSessionEventInterface):
 
91
 
 
92
    def __init__(self, session, surface_buddy=None, surface_self=None):
 
93
        MediaSessionEventInterface.__init__(self, session)
 
94
        self.surface_buddy = surface_buddy
 
95
        self.surface_self = surface_self
 
96
 
 
97
        self._conference = None
 
98
        self._handlers = []
 
99
        self._setup()
 
100
        for stream in session.streams:
 
101
            self.on_stream_added(stream)
 
102
 
 
103
    def _setup(self):
 
104
        self._pipeline = gst.Pipeline()
 
105
        bus = self._pipeline.get_bus()
 
106
        bus.add_signal_watch()
 
107
        bus.connect("message", self.on_bus_message)
 
108
        if self._session.type is MediaSessionType.WEBCAM_RECV:
 
109
            name = "fsmsncamrecvconference"
 
110
        elif self._session.type is MediaSessionType.WEBCAM_SEND:
 
111
            name = "fsmsncamsendconference"
 
112
        else:
 
113
            name = "fsrtpconference"
 
114
        self._conference = gst.element_factory_make(name)
 
115
        self._participant = self._conference.new_participant("")
 
116
        self._pipeline.add(self._conference)
 
117
        self._pipeline.set_state(gst.STATE_PLAYING)
 
118
        #FIXME Create FsElementAddedNotifier
 
119
 
 
120
    def on_stream_added(self, stream):
 
121
        logger.debug("Stream \"%s\" added" % stream.name)
 
122
        handler = MediaStreamHandler(stream, self.surface_buddy, self.surface_self)
 
123
        handler.setup(self._conference, self._pipeline, self._participant,
 
124
                self._session.type)
 
125
        self._handlers.append(handler)
 
126
        if self._session.type is MediaSessionType.WEBCAM_RECV or\
 
127
           self._session.type is MediaSessionType.WEBCAM_SEND:
 
128
            stream.set_local_codecs([])
 
129
 
 
130
    def on_bus_message(self, bus, msg):
 
131
        ret = gst.BUS_PASS
 
132
        if msg.type == gst.MESSAGE_ELEMENT:
 
133
            s = msg.structure
 
134
            if s.has_name("prepare-xwindow-id"):
 
135
                # Sets buddy's xid in our nice window.
 
136
                msg.src.set_xwindow_id(self.surface_buddy)
 
137
            if s.has_name("farsight-error"):
 
138
                logger.error("Farsight error : %s" % s['error-msg'])
 
139
            if s.has_name("farsight-codecs-changed"):
 
140
                logger.debug("Farsight codecs changed")
 
141
                ret = gst.BUS_DROP
 
142
                ready = s["session"].get_property("codecs-ready")
 
143
                if ready:
 
144
                    codecs = s["session"].get_property("codecs")
 
145
                    name = media_names[s["session"].get_property("media-type")]
 
146
                    stream = self._session.get_stream(name)
 
147
                    stream.set_local_codecs(convert_fs_codecs(codecs))
 
148
            if s.has_name("farsight-new-local-candidate"):
 
149
                logger.debug("New local candidate")
 
150
                ret = gst.BUS_DROP
 
151
                name = media_names[s["stream"].get_property("session").get_property("media-type")]
 
152
                candidate = convert_fs_candidate(s["candidate"])
 
153
                stream = self._session.get_stream(name)
 
154
                stream.new_local_candidate(candidate)
 
155
            if s.has_name("farsight-local-candidates-prepared"):
 
156
                logger.debug("Local candidates are prepared")
 
157
                ret = gst.BUS_DROP
 
158
                type = s["stream"].get_property("session").get_property("media-type")
 
159
                name = media_names[type]
 
160
                stream = self._session.get_stream(name)
 
161
                stream.local_candidates_prepared()
 
162
            if s.has_name("farsight-new-active-candidate-pair"):
 
163
                logger.debug("New active candidate pair")
 
164
                ret = gst.BUS_DROP
 
165
                type = s["stream"].get_property("session").get_property("media-type")
 
166
                name = media_names[type]
 
167
                stream = self._session.get_stream(name)
 
168
                local = s["local-candidate"]
 
169
                remote = s["remote-candidate"]
 
170
                local = convert_fs_candidate(local)
 
171
                remote = convert_fs_candidate(remote)
 
172
                stream.new_active_candidate_pair(local, remote)
 
173
        return ret
 
174
 
 
175
 
 
176
class MediaStreamHandler(MediaStreamEventInterface):
 
177
 
 
178
    def __init__(self, stream, surface_buddy=None, surface_self=None):
 
179
        MediaStreamEventInterface.__init__(self, stream)
 
180
 
 
181
        self.surface_buddy = surface_buddy
 
182
        self.surface_self = surface_self
 
183
 
 
184
    def setup(self, conference, pipeline, participant, type):
 
185
        relays = []
 
186
        for r in self._stream.relays:
 
187
            relay = gst.Structure("relay")
 
188
            relay.set_value("username", r.username)
 
189
            relay.set_value("password", r.password)
 
190
            relay.set_value("ip", r.ip)
 
191
            relay.set_value("port", r.port, "uint")
 
192
            relays.append(relay)
 
193
 
 
194
        if type in (MediaSessionType.SIP, MediaSessionType.TUNNELED_SIP):
 
195
            if type is MediaSessionType.TUNNELED_SIP:
 
196
                compatibility_mode = 3
 
197
            else:
 
198
                compatibility_mode = 2
 
199
            params = {"stun-ip" : "64.14.48.28", "stun-port" : 3478,
 
200
                    "compatibility-mode" : compatibility_mode,
 
201
                    "controlling-mode": self._stream.created_locally,
 
202
                    "relay-info": relays}
 
203
        else:
 
204
            params = {}
 
205
        media_type = media_types[self._stream.name]
 
206
        self.fssession = conference.new_session(media_type)
 
207
        self.fssession.set_codec_preferences(build_codecs(self._stream.name))
 
208
        self.fsstream = self.fssession.new_stream(participant,
 
209
                self._stream.direction, "nice", params)
 
210
        self.fsstream.connect("src-pad-added", self.on_src_pad_added, pipeline)
 
211
        source = make_source(self._stream.name)
 
212
        pipeline.add(source)
 
213
 
 
214
        def on_src_message(bus, msg):
 
215
            if msg.structure is None:
 
216
                return
 
217
            if msg.structure.get_name() == "prepare-xwindow-id":
 
218
                msg.src.set_xwindow_id(self.surface_self)
 
219
                print "XID222222"
 
220
 
 
221
        #bus = pipeline.get_bus()
 
222
        #bus.add_signal_watch()
 
223
        #bus.enable_sync_message_emission()
 
224
        #bus.connect("sync-message::element", on_src_message)
 
225
 
 
226
        source.get_pad("src").link(self.fssession.get_property("sink-pad"))
 
227
        pipeline.set_state(gst.STATE_PLAYING)
 
228
 
 
229
    def on_stream_closed(self):
 
230
        del self.fsstream
 
231
 
 
232
    def on_remote_candidates_received(self, candidates):
 
233
        candidates = filter(lambda x: x.transport == "UDP", candidates)
 
234
        candidates = convert_media_candidates(candidates)
 
235
        self.fsstream.set_remote_candidates(candidates)
 
236
 
 
237
    def on_remote_codecs_received(self, codecs):
 
238
        codecs = convert_media_codecs(codecs, self._stream.name)
 
239
        self.fsstream.set_remote_codecs(codecs)
 
240
 
 
241
    def on_src_pad_added(self, stream, pad, codec, pipeline):
 
242
        sink = make_sink(self._stream.name)
 
243
        pipeline.add(sink)
 
244
        sink.set_state(gst.STATE_PLAYING)
 
245
        pad.link(sink.get_pad("sink"))
 
246
 
 
247
 
 
248
# Farsight utility functions
 
249
 
 
250
def create_notifier(pipeline, filename):
 
251
    notifier = farsight.ElementAddedNotifier()
 
252
    notifier.add(pipeline)
 
253
    notifier.set_properties_from_file(filename)
 
254
    return notifier
 
255
 
 
256
def convert_fs_candidate(fscandidate):
 
257
    candidate = MediaCandidate()
 
258
    candidate.ip = fscandidate.ip
 
259
    candidate.port = fscandidate.port
 
260
    candidate.foundation = fscandidate.foundation
 
261
    candidate.component_id = fscandidate.component_id
 
262
    candidate.transport = protos[fscandidate.proto]
 
263
    candidate.priority = int(fscandidate.priority)
 
264
    candidate.username = fscandidate.username
 
265
    candidate.password = fscandidate.password
 
266
    candidate.type = types[fscandidate.type]
 
267
    candidate.base_ip = fscandidate.base_ip
 
268
    candidate.base_port = fscandidate.base_port
 
269
    return candidate
 
270
 
 
271
def convert_media_candidates(candidates):
 
272
    fscandidates = []
 
273
    for candidate in candidates:
 
274
        proto = farsight.NETWORK_PROTOCOL_TCP
 
275
        if candidate.transport == "UDP":
 
276
            proto = farsight.NETWORK_PROTOCOL_UDP
 
277
        type = 0
 
278
        for k,v in types.iteritems():
 
279
            if v == candidate.type:
 
280
                type = k
 
281
        fscandidate = farsight.Candidate()
 
282
        fscandidate.foundation = candidate.foundation
 
283
        fscandidate.ip = candidate.ip
 
284
        fscandidate.port = candidate.port
 
285
        fscandidate.component_id = candidate.component_id
 
286
        fscandidate.proto = proto
 
287
        fscandidate.type = type
 
288
        fscandidate.username = candidate.username
 
289
        fscandidate.password = candidate.password
 
290
        fscandidate.priority = int(candidate.priority)
 
291
        fscandidates.append(fscandidate)
 
292
    return fscandidates
 
293
 
 
294
def build_codecs(type):
 
295
    codecs = []
 
296
    for args in codecs_definitions[type]:
 
297
        codec = farsight.Codec(*args)
 
298
        codecs.append(codec)
 
299
    return codecs
 
300
 
 
301
def convert_fs_codecs(fscodecs):
 
302
    codecs = []
 
303
    for fscodec in fscodecs:
 
304
        codec = MediaCodec()
 
305
        codec.payload = fscodec.id
 
306
        codec.encoding = fscodec.encoding_name
 
307
        codec.clockrate = fscodec.clock_rate
 
308
        codec.params = dict(fscodec.optional_params)
 
309
        codecs.append(codec)
 
310
    return codecs
 
311
 
 
312
def convert_media_codecs(codecs, name):
 
313
    fscodecs = []
 
314
    media_type = media_types[name]
 
315
    for codec in codecs:
 
316
        fscodec = farsight.Codec(
 
317
            codec.payload,
 
318
            codec.encoding,
 
319
            media_type,
 
320
            codec.clockrate)
 
321
        fscodec.optional_params = codec.params.items()
 
322
        fscodecs.append(fscodec)
 
323
    return fscodecs
 
324
 
 
325
 
 
326
# GStreamer utility functions
 
327
 
 
328
def make_source(media_name):
 
329
    func = globals()["make_%s_source" % media_name]
 
330
    return func()
 
331
 
 
332
def make_sink(media_name):
 
333
    func = globals()["make_%s_sink" % media_name]
 
334
    return func()
 
335
 
 
336
# TODO: FIXME: Make this work, and then make a nice gui for configuration.
 
337
def make_audio_source(name="pulsesrc"): #was: "audiotestsrc"
 
338
    element = gst.element_factory_make(name)
 
339
    #element.set_property("is-live", True)
 
340
    return element
 
341
 
 
342
def make_audio_sink(async=False):
 
343
    return gst.element_factory_make("autoaudiosink")
 
344
 
 
345
# TODO: FIXME: Make this work, and then make a nice gui for configuration.
 
346
def make_video_source(name="v4l2src"):
 
347
    "Make a bin with a video source in it, defaulting to first webcamera "
 
348
    bin = gst.Bin("videosrc")
 
349
    src = gst.element_factory_make(name, name)
 
350
    bin.add(src)
 
351
    filter = gst.element_factory_make("capsfilter")
 
352
    filter.set_property("caps", gst.Caps("video/x-raw-yuv , width=[300,1000] , height=[200,500], framerate=[20/1,100/1]"))
 
353
    bin.add(filter)
 
354
    src.link(filter)
 
355
    videoscale = gst.element_factory_make("videoscale")
 
356
    bin.add(videoscale)
 
357
    filter.link(videoscale)
 
358
    bin.add_pad(gst.GhostPad("src", videoscale.get_pad("src")))
 
359
    return bin
 
360
 
 
361
def make_video_sink(async=False):
 
362
    "Make a bin with a video sink in it, that will be displayed on xid."
 
363
    bin = gst.Bin("videosink")
 
364
    sink = gst.element_factory_make("autovideosink", "videosink")
 
365
    #sink.set_property("sync", async)
 
366
    bin.add(sink)
 
367
    colorspace = gst.element_factory_make("ffmpegcolorspace")
 
368
    bin.add(colorspace)
 
369
    videoscale = gst.element_factory_make("videoscale")
 
370
    bin.add(videoscale)
 
371
    videoscale.link(colorspace)
 
372
    colorspace.link(sink)
 
373
    bin.add_pad(gst.GhostPad("sink", videoscale.get_pad("sink")))
 
374
 
 
375
    return bin