gstrtpilbcpay.c 6.68 KB
Newer Older
1
/* GStreamer
2
 * Copyright (C) <2006> Philippe Khalaf <burger@speedy.org>
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
16 17
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
18 19 20
 */

#ifdef HAVE_CONFIG_H
21
#  include "config.h"
22 23
#endif

24
#include <stdlib.h>
25
#include <gst/rtp/gstrtpbuffer.h>
26
#include "gstrtpilbcpay.h"
27

28
GST_DEBUG_CATEGORY_STATIC (rtpilbcpay_debug);
29 30
#define GST_CAT_DEFAULT (rtpilbcpay_debug)

31
static GstStaticPadTemplate gst_rtp_ilbc_pay_sink_template =
32 33 34 35 36 37
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("audio/x-iLBC, " "mode = (int) {20, 30}")
    );

38
static GstStaticPadTemplate gst_rtp_ilbc_pay_src_template =
39 40 41 42 43 44 45
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/x-rtp, "
        "media = (string) \"audio\", "
        "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
        "clock-rate = (int) 8000, "
46 47
        "encoding-name = (string) \"ILBC\", "
        "mode = (string) { \"20\", \"30\" }")
48 49
    );

50

Wim Taymans's avatar
Wim Taymans committed
51
static GstCaps *gst_rtp_ilbc_pay_sink_getcaps (GstRTPBasePayload * payload,
52
    GstPad * pad, GstCaps * filter);
Wim Taymans's avatar
Wim Taymans committed
53
static gboolean gst_rtp_ilbc_pay_sink_setcaps (GstRTPBasePayload * payload,
54 55
    GstCaps * caps);

56 57
#define gst_rtp_ilbc_pay_parent_class parent_class
G_DEFINE_TYPE (GstRTPILBCPay, gst_rtp_ilbc_pay,
Wim Taymans's avatar
Wim Taymans committed
58
    GST_TYPE_RTP_BASE_AUDIO_PAYLOAD);
59 60

static void
61
gst_rtp_ilbc_pay_class_init (GstRTPILBCPayClass * klass)
62
{
63
  GstElementClass *gstelement_class;
Wim Taymans's avatar
Wim Taymans committed
64
  GstRTPBasePayloadClass *gstrtpbasepayload_class;
65 66 67

  GST_DEBUG_CATEGORY_INIT (rtpilbcpay_debug, "rtpilbcpay", 0,
      "iLBC audio RTP payloader");
68

69
  gstelement_class = (GstElementClass *) klass;
Wim Taymans's avatar
Wim Taymans committed
70
  gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
71

72 73 74 75
  gst_element_class_add_static_pad_template (gstelement_class,
      &gst_rtp_ilbc_pay_sink_template);
  gst_element_class_add_static_pad_template (gstelement_class,
      &gst_rtp_ilbc_pay_src_template);
76

77
  gst_element_class_set_static_metadata (gstelement_class, "RTP iLBC Payloader",
Wim Taymans's avatar
Wim Taymans committed
78
      "Codec/Payloader/Network/RTP",
79 80
      "Packetize iLBC audio streams into RTP packets",
      "Philippe Kalaf <philippe.kalaf@collabora.co.uk>");
81

Wim Taymans's avatar
Wim Taymans committed
82 83
  gstrtpbasepayload_class->set_caps = gst_rtp_ilbc_pay_sink_setcaps;
  gstrtpbasepayload_class->get_caps = gst_rtp_ilbc_pay_sink_getcaps;
84 85 86
}

static void
87
gst_rtp_ilbc_pay_init (GstRTPILBCPay * rtpilbcpay)
88
{
Wim Taymans's avatar
Wim Taymans committed
89 90
  GstRTPBasePayload *rtpbasepayload;
  GstRTPBaseAudioPayload *rtpbaseaudiopayload;
91

Wim Taymans's avatar
Wim Taymans committed
92 93
  rtpbasepayload = GST_RTP_BASE_PAYLOAD (rtpilbcpay);
  rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (rtpilbcpay);
94 95 96

  /* we don't set the payload type, it should be set by the application using
   * the pt property or the default 96 will be used */
Wim Taymans's avatar
Wim Taymans committed
97
  rtpbasepayload->clock_rate = 8000;
98 99 100

  rtpilbcpay->mode = -1;

Wim Taymans's avatar
Wim Taymans committed
101 102
  /* tell rtpbaseaudiopayload that this is a frame based codec */
  gst_rtp_base_audio_payload_set_frame_based (rtpbaseaudiopayload);
103 104 105
}

static gboolean
Wim Taymans's avatar
Wim Taymans committed
106
gst_rtp_ilbc_pay_sink_setcaps (GstRTPBasePayload * rtpbasepayload,
107
    GstCaps * caps)
108 109
{
  GstRTPILBCPay *rtpilbcpay;
Wim Taymans's avatar
Wim Taymans committed
110
  GstRTPBaseAudioPayload *rtpbaseaudiopayload;
111 112
  gboolean ret;
  gint mode;
113
  gchar *mode_str;
114 115 116
  GstStructure *structure;
  const char *payload_name;

Wim Taymans's avatar
Wim Taymans committed
117 118
  rtpilbcpay = GST_RTP_ILBC_PAY (rtpbasepayload);
  rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (rtpbasepayload);
119 120 121 122

  structure = gst_caps_get_structure (caps, 0);

  payload_name = gst_structure_get_name (structure);
123
  if (g_ascii_strcasecmp ("audio/x-iLBC", payload_name))
124 125
    goto wrong_caps;

126 127 128 129 130 131
  if (!gst_structure_get_int (structure, "mode", &mode))
    goto no_mode;

  if (mode != 20 && mode != 30)
    goto wrong_mode;

Wim Taymans's avatar
Wim Taymans committed
132
  gst_rtp_base_payload_set_options (rtpbasepayload, "audio", TRUE, "ILBC",
Wim Taymans's avatar
Wim Taymans committed
133
      8000);
134
  /* set options for this frame based audio codec */
Wim Taymans's avatar
Wim Taymans committed
135
  gst_rtp_base_audio_payload_set_frame_options (rtpbaseaudiopayload,
136
      mode, mode == 30 ? 50 : 38);
137

138
  mode_str = g_strdup_printf ("%d", mode);
139
  ret =
Wim Taymans's avatar
Wim Taymans committed
140
      gst_rtp_base_payload_set_outcaps (rtpbasepayload, "mode", G_TYPE_STRING,
141 142 143
      mode_str, NULL);
  g_free (mode_str);

144 145 146 147 148 149 150 151 152 153 154 155 156 157
  if (mode != rtpilbcpay->mode && rtpilbcpay->mode != -1)
    goto mode_changed;

  rtpilbcpay->mode = mode;

  return ret;

  /* ERRORS */
wrong_caps:
  {
    GST_ERROR_OBJECT (rtpilbcpay, "expected audio/x-iLBC, received %s",
        payload_name);
    return FALSE;
  }
158 159 160 161 162 163 164 165 166 167
no_mode:
  {
    GST_ERROR_OBJECT (rtpilbcpay, "did not receive a mode");
    return FALSE;
  }
wrong_mode:
  {
    GST_ERROR_OBJECT (rtpilbcpay, "mode must be 20 or 30, received %d", mode);
    return FALSE;
  }
168 169
mode_changed:
  {
170 171
    GST_ERROR_OBJECT (rtpilbcpay, "Mode has changed from %d to %d! "
        "Mode cannot change while streaming", rtpilbcpay->mode, mode);
172 173 174 175
    return FALSE;
  }
}

176 177 178
/* we return the padtemplate caps with the mode field fixated to a value if we
 * can */
static GstCaps *
Wim Taymans's avatar
Wim Taymans committed
179
gst_rtp_ilbc_pay_sink_getcaps (GstRTPBasePayload * rtppayload, GstPad * pad,
180
    GstCaps * filter)
181 182 183 184 185
{
  GstCaps *otherpadcaps;
  GstCaps *caps;

  otherpadcaps = gst_pad_get_allowed_caps (rtppayload->srcpad);
Wim Taymans's avatar
Wim Taymans committed
186
  caps = gst_pad_get_pad_template_caps (pad);
187 188 189 190 191 192 193 194 195 196 197 198 199 200

  if (otherpadcaps) {
    if (!gst_caps_is_empty (otherpadcaps)) {
      GstStructure *structure;
      const gchar *mode_str;
      gint mode;

      structure = gst_caps_get_structure (otherpadcaps, 0);

      /* parse mode, if we can */
      mode_str = gst_structure_get_string (structure, "mode");
      if (mode_str) {
        mode = strtol (mode_str, NULL, 10);
        if (mode == 20 || mode == 30) {
Wim Taymans's avatar
Wim Taymans committed
201
          caps = gst_caps_make_writable (caps);
202 203 204 205 206 207 208
          structure = gst_caps_get_structure (caps, 0);
          gst_structure_set (structure, "mode", G_TYPE_INT, mode, NULL);
        }
      }
    }
    gst_caps_unref (otherpadcaps);
  }
209 210 211 212 213 214 215 216 217 218 219

  if (filter) {
    GstCaps *tmp;

    GST_DEBUG_OBJECT (rtppayload, "Intersect %" GST_PTR_FORMAT " and filter %"
        GST_PTR_FORMAT, caps, filter);
    tmp = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
    gst_caps_unref (caps);
    caps = tmp;
  }

220 221 222
  return caps;
}

223 224 225 226
gboolean
gst_rtp_ilbc_pay_plugin_init (GstPlugin * plugin)
{
  return gst_element_register (plugin, "rtpilbcpay",
227
      GST_RANK_SECONDARY, GST_TYPE_RTP_ILBC_PAY);
228
}