summaryrefslogtreecommitdiff
path: root/gst/fsrtpconference/fs-rtp-dtmf-sound-source.c
blob: d54674f5def4d82d641c0ca5ec78a46f20ae9c0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
 * Farstream - Farstream RTP DTMF Sound Source
 *
 * Copyright 2007-2009 Collabora Ltd.
 *  @author: Olivier Crete <olivier.crete@collabora.co.uk>
 * Copyright 2007-2009 Nokia Corp.
 *
 * fs-rtp-dtmf-sound-source.c - A Farstream RTP Sound Source gobject
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 */


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "fs-rtp-dtmf-sound-source.h"

#include <farstream/fs-conference.h>

#include "fs-rtp-conference.h"
#include "fs-rtp-discover-codecs.h"
#include "fs-rtp-codec-negotiation.h"
#include "fs-rtp-codec-specific.h"

#define GST_CAT_DEFAULT fsrtpconference_debug

/*
 * SECTION:fs-rtp-dtmf-sound-source
 * @short_description: Class to create the source of DTMF sounds
 *
 * This class is manages the DTMF Sound source and related matters
 *
 */


/* all privates variables are protected by the mutex */
struct _FsRtpDtmfSoundSourcePrivate {
  gboolean disposed;
};

G_DEFINE_TYPE(FsRtpDtmfSoundSource, fs_rtp_dtmf_sound_source,
    FS_TYPE_RTP_SPECIAL_SOURCE);

#define FS_RTP_DTMF_SOUND_SOURCE_GET_PRIVATE(o)                         \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), FS_TYPE_RTP_DTMF_SOUND_SOURCE,     \
   FsRtpDtmfSoundSourcePrivate))


static GstElement *
fs_rtp_dtmf_sound_source_build (FsRtpSpecialSource *source,
    GList *negotiated_codec_associations,
    FsCodec *selected_codec);


static FsCodec *fs_rtp_dtmf_sound_source_get_codec (
    FsRtpSpecialSourceClass *klass,
    GList *negotiated_codec_associations,
    FsCodec *selected_codec);


static void
fs_rtp_dtmf_sound_source_class_init (FsRtpDtmfSoundSourceClass *klass)
{
  FsRtpSpecialSourceClass *spsource_class = FS_RTP_SPECIAL_SOURCE_CLASS (klass);

  spsource_class->build = fs_rtp_dtmf_sound_source_build;
  spsource_class->get_codec = fs_rtp_dtmf_sound_source_get_codec;

  g_type_class_add_private (klass, sizeof (FsRtpDtmfSoundSourcePrivate));
}

static void
fs_rtp_dtmf_sound_source_init (FsRtpDtmfSoundSource *self)
{
  self->priv = FS_RTP_DTMF_SOUND_SOURCE_GET_PRIVATE (self);
}

static gboolean
_is_law_codec (CodecAssociation *ca, gpointer user_data)
{
  if (codec_association_is_valid_for_sending (ca, FALSE) &&
      (ca->codec->id == 0 || ca->codec->id == 8))
    return TRUE;
  else
    return FALSE;
}

/**
 * get_telephone_sound_codec:
 * @codecs: a #GList of #FsCodec
 *
 * Find the first occurence of PCMA or PCMU codecs
 *
 * Returns: The #FsCodec of type PCMA/U from the list or %NULL
 */
static FsCodec *
get_pcm_law_sound_codec (GList *codecs,
    gchar **encoder_name,
    gchar **payloader_name,
    CodecAssociation **out_ca)
{
  CodecAssociation *ca = NULL;

  ca = lookup_codec_association_custom (codecs, _is_law_codec, NULL);

  if (!ca)
    return NULL;

  if (ca->codec->id == 0)
  {
    if (encoder_name)
      *encoder_name = "mulawenc";
    if (payloader_name)
      *payloader_name = "rtppcmupay";
  }
  else if (ca->codec->id == 8)
  {
    if (encoder_name)
      *encoder_name = "alawenc";
    if (payloader_name)
      *payloader_name = "rtppcmapay";
  }

  if (out_ca)
    *out_ca = ca;

  return ca->send_codec;
}

static gboolean
_check_element_factory (gchar *name)
{
  GstElementFactory *fact = NULL;

  g_return_val_if_fail (name, FALSE);

  fact = gst_element_factory_find (name);
  if (fact)
    gst_object_unref (fact);

  return (fact != NULL);
}

static CodecAssociation *
_get_main_codec_association (GList *codec_associations, FsCodec *codec)
{
  CodecAssociation *ca = lookup_codec_association_by_codec_for_sending (
      codec_associations, codec);

  if (ca && codec_association_is_valid_for_sending (ca, TRUE) &&
      codec_blueprint_has_factory (ca->blueprint, FS_DIRECTION_SEND))
    return ca;
  else
    return NULL;
}

static FsCodec *
fs_rtp_dtmf_sound_source_get_codec (FsRtpSpecialSourceClass *klass,
    GList *negotiated_codec_associations,
    FsCodec *selected_codec)
{
  FsCodec *codec = NULL;
  gchar *encoder_name = NULL;
  gchar *payloader_name = NULL;
  CodecAssociation *ca;

  if (selected_codec->media_type != FS_MEDIA_TYPE_AUDIO)
    return NULL;

  if (!_check_element_factory ("dtmfsrc"))
    return NULL;

  if (selected_codec->clock_rate == 8000)
  {
    codec = get_pcm_law_sound_codec (negotiated_codec_associations,
        &encoder_name, &payloader_name, NULL);
    if (codec) {
      if (!_check_element_factory (encoder_name))
        return NULL;
      if (!_check_element_factory (payloader_name))
        return NULL;
      return codec;
    }
  }

  ca = _get_main_codec_association (negotiated_codec_associations,
      selected_codec);

  if (ca)
    return ca->send_codec;
  else
    return NULL;
}

static GstElement *
fs_rtp_dtmf_sound_source_build (FsRtpSpecialSource *source,
    GList *negotiated_codec_associations,
    FsCodec *selected_codec)
{
  FsCodec *telephony_codec = NULL;
  GstCaps *caps = NULL;
  GstPad *pad = NULL;
  GstElement *dtmfsrc = NULL;
  GstElement *capsfilter = NULL;
  GstPad *ghostpad = NULL;
  GstElement *bin = NULL;
  GstElement *encoder = NULL;
  GstElement *payloader = NULL;
  gchar *encoder_name = NULL;
  gchar *payloader_name = NULL;
  CodecAssociation *ca = NULL;


  if (selected_codec->clock_rate == 8000)
    telephony_codec = get_pcm_law_sound_codec (negotiated_codec_associations,
        &encoder_name, &payloader_name, &ca);

  if (!telephony_codec)
  {
    ca = _get_main_codec_association (negotiated_codec_associations,
        selected_codec);
    if (ca)
      telephony_codec = ca->send_codec;
  }

  g_return_val_if_fail (telephony_codec, NULL);

  source->codec = fs_codec_copy (telephony_codec);

  GST_DEBUG ("Creating dtmf sound source for " FS_CODEC_FORMAT,
      FS_CODEC_ARGS (telephony_codec));

  bin = gst_bin_new (NULL);

  dtmfsrc = gst_element_factory_make ("dtmfsrc", NULL);
  if (!dtmfsrc)
  {
    GST_ERROR ("Could not make rtpdtmfsrc");
    goto error;
  }
  if (!gst_bin_add (GST_BIN (bin), dtmfsrc))
  {
    GST_ERROR ("Could not add rtpdtmfsrc to bin");
    gst_object_unref (dtmfsrc);
    goto error;
  }

  capsfilter = gst_element_factory_make ("capsfilter", NULL);
  if (!capsfilter)
  {
    GST_ERROR ("Could not make capsfilter");
    goto error;
  }
  if (!gst_bin_add (GST_BIN (bin), capsfilter))
  {
    GST_ERROR ("Could not add capsfilter to bin");
    gst_object_unref (capsfilter);
    goto error;
  }

  caps = fs_codec_to_gst_caps (telephony_codec);
  g_object_set (capsfilter, "caps", caps, NULL);
  {
    gchar *str = gst_caps_to_string (caps);
    GST_DEBUG ("Using caps %s for dtmf", str);
    g_free (str);
  }
  gst_caps_unref (caps);

  pad = gst_element_get_static_pad (capsfilter, "src");
  if (!pad)
  {
    GST_ERROR ("Could not get \"src\" pad from capsfilter");
    goto error;
  }
  ghostpad = gst_ghost_pad_new ("src", pad);
  if (!ghostpad)
  {
    GST_ERROR ("Could not create a ghostpad for capsfilter src pad"
        " for dtmfsrc");
    goto error;
  }
  if (!gst_element_add_pad (bin, ghostpad))
  {
    GST_ERROR ("Could not get \"src\" ghostpad to dtmf sound source bin");
    gst_object_unref (pad);
    goto error;
  }
  gst_object_unref (pad);


  if (ca)
  {
    gchar *codec_bin_name = g_strdup_printf ("dtmf_send_codecbin_%d",
        telephony_codec->id);
    GError *error = NULL;
    GstElement *codecbin = create_codec_bin_from_blueprint (
        telephony_codec, ca->blueprint, codec_bin_name, FS_DIRECTION_SEND,
        &error);

    if (!codecbin)
    {
      GST_ERROR ("Could not make %s: %s", codec_bin_name,
          error ? error->message : "No error message!");
      g_clear_error (&error);
      g_free (codec_bin_name);
      goto error;
    }

    if (!gst_bin_add (GST_BIN (bin), codecbin))
    {
      GST_ERROR ("Could not add %s to bin", codec_bin_name);
      gst_object_unref (codecbin);
      g_free (codec_bin_name);
      goto error;
    }

    if (!gst_element_link_pads (dtmfsrc, "src", codecbin, "sink"))
    {
      GST_ERROR ("Could not link the rtpdtmfsrc and %s", codec_bin_name);
      g_free (codec_bin_name);
      goto error;
    }

    if (!gst_element_link_pads (codecbin, "src", capsfilter, "sink"))
    {
      GST_ERROR ("Could not link the %s and its capsfilter", codec_bin_name);
      g_free (codec_bin_name);
      goto error;
    }

    g_free (codec_bin_name);
  }
  else
  {
    encoder = gst_element_factory_make (encoder_name, NULL);
    if (!encoder)
    {
      GST_ERROR ("Could not make %s", encoder_name);
      goto error;
    }
    if (!gst_bin_add (GST_BIN (bin), encoder))
    {
      GST_ERROR ("Could not add %s to bin", encoder_name);
      gst_object_unref (encoder);
      goto error;
    }

    if (!gst_element_link_pads (dtmfsrc, "src", encoder, "sink"))
    {
      GST_ERROR ("Could not link the rtpdtmfsrc and %s", encoder_name);
      goto error;
    }

    payloader = gst_element_factory_make (payloader_name, NULL);
    if (!payloader)
    {
      GST_ERROR ("Could not make %s", payloader_name);
      goto error;
    }
    if (!gst_bin_add (GST_BIN (bin), payloader))
    {
      GST_ERROR ("Could not add %s to bin", payloader_name);
      gst_object_unref (payloader);
      goto error;
    }

    if (!gst_element_link_pads (encoder, "src", payloader, "sink"))
    {
      GST_ERROR ("Could not link the %s and %s", encoder_name, payloader_name);
      goto error;
    }

    if (!gst_element_link_pads (payloader, "src", capsfilter, "sink"))
    {
      GST_ERROR ("Could not link the %s and its capsfilter", payloader_name);
      goto error;
    }
  }

  return bin;

 error:
  gst_object_unref (bin);

  return NULL;
}