1
# -*- coding: utf-8 -*-
3
# papyon - a python client library for Msn
5
# Copyright (C) 2009 Collabora Ltd.
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.
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.
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
21
from papyon.media import *
22
from papyon.event.media import *
28
import farstream as farsight
29
except ImportError: # Hello older systems
36
logger = logging.getLogger("papylib.conference")
38
codecs_definitions = {
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)
52
(121, "x-rtvc1", farsight.MEDIA_TYPE_VIDEO, 90000),
53
(34, "H263", farsight.MEDIA_TYPE_VIDEO, 90000)
59
farsight.CANDIDATE_TYPE_HOST : "host",
60
farsight.CANDIDATE_TYPE_SRFLX : "srflx",
61
farsight.CANDIDATE_TYPE_PRFLX : "prflx",
62
farsight.CANDIDATE_TYPE_RELAY : "relay"
66
farsight.NETWORK_PROTOCOL_TCP : "TCP",
67
farsight.NETWORK_PROTOCOL_UDP : "UDP"
71
farsight.MEDIA_TYPE_AUDIO : "audio",
72
farsight.MEDIA_TYPE_VIDEO : "video"
76
"audio" : farsight.MEDIA_TYPE_AUDIO,
77
"video" : farsight.MEDIA_TYPE_VIDEO
81
class Conference(gobject.GObject):
84
gobject.GObject.__init__(self)
86
def set_source(self, source):
90
class MediaSessionHandler(MediaSessionEventInterface):
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
97
self._conference = None
100
for stream in session.streams:
101
self.on_stream_added(stream)
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"
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
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,
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([])
130
def on_bus_message(self, bus, msg):
132
if msg.type == gst.MESSAGE_ELEMENT:
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")
142
ready = s["session"].get_property("codecs-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")
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")
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")
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)
176
class MediaStreamHandler(MediaStreamEventInterface):
178
def __init__(self, stream, surface_buddy=None, surface_self=None):
179
MediaStreamEventInterface.__init__(self, stream)
181
self.surface_buddy = surface_buddy
182
self.surface_self = surface_self
184
def setup(self, conference, pipeline, participant, type):
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")
194
if type in (MediaSessionType.SIP, MediaSessionType.TUNNELED_SIP):
195
if type is MediaSessionType.TUNNELED_SIP:
196
compatibility_mode = 3
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}
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)
214
def on_src_message(bus, msg):
215
if msg.structure is None:
217
if msg.structure.get_name() == "prepare-xwindow-id":
218
msg.src.set_xwindow_id(self.surface_self)
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)
226
source.get_pad("src").link(self.fssession.get_property("sink-pad"))
227
pipeline.set_state(gst.STATE_PLAYING)
229
def on_stream_closed(self):
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)
237
def on_remote_codecs_received(self, codecs):
238
codecs = convert_media_codecs(codecs, self._stream.name)
239
self.fsstream.set_remote_codecs(codecs)
241
def on_src_pad_added(self, stream, pad, codec, pipeline):
242
sink = make_sink(self._stream.name)
244
sink.set_state(gst.STATE_PLAYING)
245
pad.link(sink.get_pad("sink"))
248
# Farsight utility functions
250
def create_notifier(pipeline, filename):
251
notifier = farsight.ElementAddedNotifier()
252
notifier.add(pipeline)
253
notifier.set_properties_from_file(filename)
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
271
def convert_media_candidates(candidates):
273
for candidate in candidates:
274
proto = farsight.NETWORK_PROTOCOL_TCP
275
if candidate.transport == "UDP":
276
proto = farsight.NETWORK_PROTOCOL_UDP
278
for k,v in types.iteritems():
279
if v == candidate.type:
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)
294
def build_codecs(type):
296
for args in codecs_definitions[type]:
297
codec = farsight.Codec(*args)
301
def convert_fs_codecs(fscodecs):
303
for fscodec in fscodecs:
305
codec.payload = fscodec.id
306
codec.encoding = fscodec.encoding_name
307
codec.clockrate = fscodec.clock_rate
308
codec.params = dict(fscodec.optional_params)
312
def convert_media_codecs(codecs, name):
314
media_type = media_types[name]
316
fscodec = farsight.Codec(
321
fscodec.optional_params = codec.params.items()
322
fscodecs.append(fscodec)
326
# GStreamer utility functions
328
def make_source(media_name):
329
func = globals()["make_%s_source" % media_name]
332
def make_sink(media_name):
333
func = globals()["make_%s_sink" % media_name]
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)
342
def make_audio_sink(async=False):
343
return gst.element_factory_make("autoaudiosink")
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)
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]"))
355
videoscale = gst.element_factory_make("videoscale")
357
filter.link(videoscale)
358
bin.add_pad(gst.GhostPad("src", videoscale.get_pad("src")))
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)
367
colorspace = gst.element_factory_make("ffmpegcolorspace")
369
videoscale = gst.element_factory_make("videoscale")
371
videoscale.link(colorspace)
372
colorspace.link(sink)
373
bin.add_pad(gst.GhostPad("sink", videoscale.get_pad("sink")))