gstrtpg723depay.c 5.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* GStreamer
 *
 * Copyright (C) <2010> Wim Taymans <wim.taymans@gmail.com>
 *
 * 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
17 18
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
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
 */

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

#include <gst/rtp/gstrtpbuffer.h>

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

GST_DEBUG_CATEGORY_STATIC (rtpg723depay_debug);
#define GST_CAT_DEFAULT (rtpg723depay_debug)


/* references:
 *
 * RFC 3551 (4.5.3)
 */

enum
{
  /* FILL ME */
  LAST_SIGNAL
};

enum
{
48
  PROP_0
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
};

/* input is an RTP packet
 *
 */
static GstStaticPadTemplate gst_rtp_g723_depay_sink_template =
    GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/x-rtp, "
        "media = (string) \"audio\", "
        "clock-rate = (int) 8000, "
        "encoding-name = (string) \"G723\"; "
        "application/x-rtp, "
        "media = (string) \"audio\", "
        "payload = (int) " GST_RTP_PAYLOAD_G723_STRING ", "
        "clock-rate = (int) 8000")
    );

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

75
static gboolean gst_rtp_g723_depay_setcaps (GstRTPBaseDepayload * depayload,
76
    GstCaps * caps);
77
static GstBuffer *gst_rtp_g723_depay_process (GstRTPBaseDepayload * depayload,
78
    GstRTPBuffer * rtp);
79

Wim Taymans's avatar
Wim Taymans committed
80 81
#define gst_rtp_g723_depay_parent_class parent_class
G_DEFINE_TYPE (GstRtpG723Depay, gst_rtp_g723_depay,
82
    GST_TYPE_RTP_BASE_DEPAYLOAD);
83 84

static void
Wim Taymans's avatar
Wim Taymans committed
85
gst_rtp_g723_depay_class_init (GstRtpG723DepayClass * klass)
86
{
Wim Taymans's avatar
Wim Taymans committed
87
  GstElementClass *gstelement_class;
88
  GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
Wim Taymans's avatar
Wim Taymans committed
89 90 91

  GST_DEBUG_CATEGORY_INIT (rtpg723depay_debug, "rtpg723depay", 0,
      "G.723 RTP Depayloader");
92

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

96 97 98 99
  gst_element_class_add_static_pad_template (gstelement_class,
      &gst_rtp_g723_depay_src_template);
  gst_element_class_add_static_pad_template (gstelement_class,
      &gst_rtp_g723_depay_sink_template);
100

101
  gst_element_class_set_static_metadata (gstelement_class,
Wim Taymans's avatar
Wim Taymans committed
102
      "RTP G.723 depayloader", "Codec/Depayloader/Network/RTP",
103 104
      "Extracts G.723 audio from RTP packets (RFC 3551)",
      "Wim Taymans <wim.taymans@gmail.com>");
105

106
  gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_g723_depay_process;
107
  gstrtpbasedepayload_class->set_caps = gst_rtp_g723_depay_setcaps;
108 109 110
}

static void
Wim Taymans's avatar
Wim Taymans committed
111
gst_rtp_g723_depay_init (GstRtpG723Depay * rtpg723depay)
112
{
113
  GstRTPBaseDepayload *depayload;
114

115
  depayload = GST_RTP_BASE_DEPAYLOAD (rtpg723depay);
116

117
  gst_pad_use_fixed_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload));
118 119 120
}

static gboolean
121
gst_rtp_g723_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
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
{
  GstStructure *structure;
  GstCaps *srccaps;
  GstRtpG723Depay *rtpg723depay;
  const gchar *params;
  gint clock_rate, channels;
  gboolean ret;

  rtpg723depay = GST_RTP_G723_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;

  depayload->clock_rate = clock_rate;

  srccaps = gst_caps_new_simple ("audio/G723",
      "channels", G_TYPE_INT, channels, "rate", G_TYPE_INT, clock_rate, NULL);
153
  ret = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
  gst_caps_unref (srccaps);

  return ret;

  /* ERRORS */
wrong_channels:
  {
    GST_DEBUG_OBJECT (rtpg723depay, "expected 1 channel, got %d", channels);
    return FALSE;
  }
wrong_clock_rate:
  {
    GST_DEBUG_OBJECT (rtpg723depay, "expected 8000 clock-rate, got %d",
        clock_rate);
    return FALSE;
  }
}


static GstBuffer *
174
gst_rtp_g723_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
175 176 177 178 179 180 181 182
{
  GstRtpG723Depay *rtpg723depay;
  GstBuffer *outbuf = NULL;
  gint payload_len;
  gboolean marker;

  rtpg723depay = GST_RTP_G723_DEPAY (depayload);

183
  payload_len = gst_rtp_buffer_get_payload_len (rtp);
184 185 186 187 188 189 190

  /* At least 4 bytes */
  if (payload_len < 4)
    goto too_small;

  GST_LOG_OBJECT (rtpg723depay, "payload len %d", payload_len);

191 192
  outbuf = gst_rtp_buffer_get_payload_buffer (rtp);
  marker = gst_rtp_buffer_get_marker (rtp);
193 194 195

  if (marker) {
    /* marker bit starts talkspurt */
196
    GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
197 198
  }

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

  return outbuf;

  /* ERRORS */
too_small:
  {
    GST_ELEMENT_WARNING (rtpg723depay, STREAM, DECODE,
        (NULL), ("G723 RTP payload too small (%d)", payload_len));
    goto bad_packet;
  }
bad_packet:
  {
    /* no fatal error */
    return NULL;
  }
}

gboolean
gst_rtp_g723_depay_plugin_init (GstPlugin * plugin)
{
  return gst_element_register (plugin, "rtpg723depay",
222
      GST_RANK_SECONDARY, GST_TYPE_RTP_G723_DEPAY);
223
}