gstrtpg729depay.c 5.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* GStreamer
 *
 * 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
15 16
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
17 18 19 20 21 22 23 24 25 26 27 28
 */

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

#include <gst/rtp/gstrtpbuffer.h>

#include <stdlib.h>
#include <string.h>
#include "gstrtpg729depay.h"

29 30 31 32
GST_DEBUG_CATEGORY_STATIC (rtpg729depay_debug);
#define GST_CAT_DEFAULT (rtpg729depay_debug)


33 34 35 36 37 38 39 40 41 42 43 44 45
/* references:
 *
 * RFC 3551 (4.5.6)
 */

enum
{
  /* FILL ME */
  LAST_SIGNAL
};

enum
{
46
  PROP_0
47 48 49 50 51 52
};

/* input is an RTP packet
 *
 */
static GstStaticPadTemplate gst_rtp_g729_depay_sink_template =
53
    GST_STATIC_PAD_TEMPLATE ("sink",
54 55 56 57 58
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/x-rtp, "
        "media = (string) \"audio\", "
        "clock-rate = (int) 8000, "
59
        "encoding-name = (string) \"G729\"; "
60 61 62 63 64 65 66 67 68 69 70 71 72
        "application/x-rtp, "
        "media = (string) \"audio\", "
        "payload = (int) " GST_RTP_PAYLOAD_G729_STRING ", "
        "clock-rate = (int) 8000")
    );

static GstStaticPadTemplate gst_rtp_g729_depay_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("audio/G729, " "channels = (int) 1," "rate = (int) 8000")
    );

Wim Taymans's avatar
Wim Taymans committed
73
static gboolean gst_rtp_g729_depay_setcaps (GstRTPBaseDepayload * depayload,
74
    GstCaps * caps);
Wim Taymans's avatar
Wim Taymans committed
75
static GstBuffer *gst_rtp_g729_depay_process (GstRTPBaseDepayload * depayload,
76
    GstRTPBuffer * rtp);
77

Wim Taymans's avatar
Wim Taymans committed
78 79
#define gst_rtp_g729_depay_parent_class parent_class
G_DEFINE_TYPE (GstRtpG729Depay, gst_rtp_g729_depay,
Wim Taymans's avatar
Wim Taymans committed
80
    GST_TYPE_RTP_BASE_DEPAYLOAD);
81 82

static void
Wim Taymans's avatar
Wim Taymans committed
83
gst_rtp_g729_depay_class_init (GstRtpG729DepayClass * klass)
84
{
Wim Taymans's avatar
Wim Taymans committed
85
  GstElementClass *gstelement_class;
Wim Taymans's avatar
Wim Taymans committed
86
  GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
Wim Taymans's avatar
Wim Taymans committed
87 88 89

  GST_DEBUG_CATEGORY_INIT (rtpg729depay_debug, "rtpg729depay", 0,
      "G.729 RTP Depayloader");
90

Wim Taymans's avatar
Wim Taymans committed
91
  gstelement_class = (GstElementClass *) klass;
Wim Taymans's avatar
Wim Taymans committed
92
  gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
Wim Taymans's avatar
Wim Taymans committed
93 94

  gst_element_class_add_pad_template (gstelement_class,
95
      gst_static_pad_template_get (&gst_rtp_g729_depay_src_template));
Wim Taymans's avatar
Wim Taymans committed
96
  gst_element_class_add_pad_template (gstelement_class,
97 98
      gst_static_pad_template_get (&gst_rtp_g729_depay_sink_template));

99
  gst_element_class_set_static_metadata (gstelement_class,
Wim Taymans's avatar
Wim Taymans committed
100
      "RTP G.729 depayloader", "Codec/Depayloader/Network/RTP",
101 102
      "Extracts G.729 audio from RTP packets (RFC 3551)",
      "Laurent Glayal <spglegle@yahoo.fr>");
103

104
  gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_g729_depay_process;
Wim Taymans's avatar
Wim Taymans committed
105
  gstrtpbasedepayload_class->set_caps = gst_rtp_g729_depay_setcaps;
106 107 108
}

static void
Wim Taymans's avatar
Wim Taymans committed
109
gst_rtp_g729_depay_init (GstRtpG729Depay * rtpg729depay)
110
{
Wim Taymans's avatar
Wim Taymans committed
111
  GstRTPBaseDepayload *depayload;
112

Wim Taymans's avatar
Wim Taymans committed
113
  depayload = GST_RTP_BASE_DEPAYLOAD (rtpg729depay);
114

Wim Taymans's avatar
Wim Taymans committed
115
  gst_pad_use_fixed_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload));
116 117 118
}

static gboolean
Wim Taymans's avatar
Wim Taymans committed
119
gst_rtp_g729_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
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
{
  GstStructure *structure;
  GstCaps *srccaps;
  GstRtpG729Depay *rtpg729depay;
  const gchar *params;
  gint clock_rate, channels;
  gboolean ret;

  rtpg729depay = GST_RTP_G729_DEPAY (depayload);

  structure = gst_caps_get_structure (caps, 0);

  if (!(params = gst_structure_get_string (structure, "encoding-params")))
    channels = 1;
  else {
    channels = atoi (params);
  }

  if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
    clock_rate = 8000;

  if (channels != 1)
    goto wrong_channels;

  if (clock_rate != 8000)
    goto wrong_clock_rate;

147 148
  depayload->clock_rate = clock_rate;

149 150
  srccaps = gst_caps_new_simple ("audio/G729",
      "channels", G_TYPE_INT, channels, "rate", G_TYPE_INT, clock_rate, NULL);
Wim Taymans's avatar
Wim Taymans committed
151
  ret = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
152 153 154 155 156 157 158
  gst_caps_unref (srccaps);

  return ret;

  /* ERRORS */
wrong_channels:
  {
159
    GST_DEBUG_OBJECT (rtpg729depay, "expected 1 channel, got %d", channels);
160 161 162 163 164 165 166 167 168 169 170
    return FALSE;
  }
wrong_clock_rate:
  {
    GST_DEBUG_OBJECT (rtpg729depay, "expected 8000 clock-rate, got %d",
        clock_rate);
    return FALSE;
  }
}

static GstBuffer *
171
gst_rtp_g729_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
172 173 174 175
{
  GstRtpG729Depay *rtpg729depay;
  GstBuffer *outbuf = NULL;
  gint payload_len;
176
  gboolean marker;
177 178 179

  rtpg729depay = GST_RTP_G729_DEPAY (depayload);

180
  payload_len = gst_rtp_buffer_get_payload_len (rtp);
181 182 183 184 185 186 187 188

  /* At least 2 bytes (CNG from G729 Annex B) */
  if (payload_len < 2) {
    GST_ELEMENT_WARNING (rtpg729depay, STREAM, DECODE,
        (NULL), ("G729 RTP payload too small (%d)", payload_len));
    goto bad_packet;
  }

189
  GST_LOG_OBJECT (rtpg729depay, "payload len %d", payload_len);
190 191

  if ((payload_len % 10) == 2) {
192
    GST_LOG_OBJECT (rtpg729depay, "G729 payload contains CNG frame");
193 194
  }

195 196
  outbuf = gst_rtp_buffer_get_payload_buffer (rtp);
  marker = gst_rtp_buffer_get_marker (rtp);
197 198 199

  if (marker) {
    /* marker bit starts talkspurt */
200
    GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
201
  }
202

203
  GST_LOG_OBJECT (depayload, "pushing buffer of size %" G_GSIZE_FORMAT,
Wim Taymans's avatar
Wim Taymans committed
204
      gst_buffer_get_size (outbuf));
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

  return outbuf;

  /* ERRORS */
bad_packet:
  {
    /* no fatal error */
    return NULL;
  }
}

gboolean
gst_rtp_g729_depay_plugin_init (GstPlugin * plugin)
{
  return gst_element_register (plugin, "rtpg729depay",
220
      GST_RANK_SECONDARY, GST_TYPE_RTP_G729_DEPAY);
221
}