gstrtpbvdepay.c 5.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* GStreamer
 * Copyright (C) <2009> 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
16 17
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
18 19
 */

Wim Taymans's avatar
Wim Taymans committed
20 21 22 23 24 25 26 27
/**
 * SECTION:element-rtpbvdepay
 * @see_also: rtpbvpay
 *
 * Extract BroadcomVoice audio from RTP packets according to RFC 4298.
 * For detailed information see: http://www.rfc-editor.org/rfc/rfc4298.txt
 */

28 29 30 31 32 33 34 35
#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

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

#include <gst/rtp/gstrtpbuffer.h>
36
#include <gst/audio/audio.h>
37
#include "gstrtpbvdepay.h"
38
#include "gstrtputils.h"
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

static GstStaticPadTemplate gst_rtp_bv_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) \"BV16\"; "
        "application/x-rtp, "
        "media = (string) \"audio\", "
        "clock-rate = (int) 16000, " "encoding-name = (string) \"BV32\"")
    );

static GstStaticPadTemplate gst_rtp_bv_depay_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("audio/x-bv, " "mode = (int) { 16, 32 }")
    );

Wim Taymans's avatar
Wim Taymans committed
60
static GstBuffer *gst_rtp_bv_depay_process (GstRTPBaseDepayload * depayload,
61
    GstRTPBuffer * rtp);
Wim Taymans's avatar
Wim Taymans committed
62
static gboolean gst_rtp_bv_depay_setcaps (GstRTPBaseDepayload * depayload,
63 64
    GstCaps * caps);

65
#define gst_rtp_bv_depay_parent_class parent_class
Wim Taymans's avatar
Wim Taymans committed
66
G_DEFINE_TYPE (GstRTPBVDepay, gst_rtp_bv_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
67 68

static void
69
gst_rtp_bv_depay_class_init (GstRTPBVDepayClass * klass)
70
{
71
  GstElementClass *gstelement_class;
Wim Taymans's avatar
Wim Taymans committed
72
  GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
73 74

  gstelement_class = (GstElementClass *) klass;
Wim Taymans's avatar
Wim Taymans committed
75
  gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
76

77 78 79 80
  gst_element_class_add_static_pad_template (gstelement_class,
      &gst_rtp_bv_depay_src_template);
  gst_element_class_add_static_pad_template (gstelement_class,
      &gst_rtp_bv_depay_sink_template);
81

82
  gst_element_class_set_static_metadata (gstelement_class,
Wim Taymans's avatar
Wim Taymans committed
83
      "RTP BroadcomVoice depayloader", "Codec/Depayloader/Network/RTP",
84 85
      "Extracts BroadcomVoice audio from RTP packets (RFC 4298)",
      "Wim Taymans <wim.taymans@collabora.co.uk>");
86

87
  gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_bv_depay_process;
Wim Taymans's avatar
Wim Taymans committed
88
  gstrtpbasedepayload_class->set_caps = gst_rtp_bv_depay_setcaps;
89 90 91
}

static void
92
gst_rtp_bv_depay_init (GstRTPBVDepay * rtpbvdepay)
93 94 95 96 97
{
  rtpbvdepay->mode = -1;
}

static gboolean
Wim Taymans's avatar
Wim Taymans committed
98
gst_rtp_bv_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
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
{
  GstRTPBVDepay *rtpbvdepay = GST_RTP_BV_DEPAY (depayload);
  GstCaps *srccaps;
  GstStructure *structure;
  const gchar *mode_str = NULL;
  gint mode, clock_rate, expected_rate;
  gboolean ret;

  structure = gst_caps_get_structure (caps, 0);

  mode_str = gst_structure_get_string (structure, "encoding-name");
  if (!mode_str)
    goto no_mode;

  if (!strcmp (mode_str, "BV16")) {
    mode = 16;
    expected_rate = 8000;
  } else if (!strcmp (mode_str, "BV32")) {
    mode = 32;
    expected_rate = 16000;
  } else
    goto invalid_mode;

  if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
    clock_rate = expected_rate;
  else if (clock_rate != expected_rate)
    goto wrong_rate;

  depayload->clock_rate = clock_rate;
  rtpbvdepay->mode = mode;

  srccaps = gst_caps_new_simple ("audio/x-bv",
      "mode", G_TYPE_INT, rtpbvdepay->mode, NULL);
Wim Taymans's avatar
Wim Taymans committed
132
  ret = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
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

  GST_DEBUG ("set caps on source: %" GST_PTR_FORMAT " (ret=%d)", srccaps, ret);
  gst_caps_unref (srccaps);

  return ret;

  /* ERRORS */
no_mode:
  {
    GST_ERROR_OBJECT (rtpbvdepay, "did not receive an encoding-name");
    return FALSE;
  }
invalid_mode:
  {
    GST_ERROR_OBJECT (rtpbvdepay,
        "invalid encoding-name, expected BV16 or BV32, got %s", mode_str);
    return FALSE;
  }
wrong_rate:
  {
    GST_ERROR_OBJECT (rtpbvdepay, "invalid clock-rate, expected %d, got %d",
        expected_rate, clock_rate);
    return FALSE;
  }
}

static GstBuffer *
160
gst_rtp_bv_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
161 162 163 164
{
  GstBuffer *outbuf;
  gboolean marker;

165
  marker = gst_rtp_buffer_get_marker (rtp);
166

167
  GST_DEBUG ("process : got %" G_GSIZE_FORMAT " bytes, mark %d ts %u seqn %d",
168 169
      gst_buffer_get_size (rtp->buffer), marker,
      gst_rtp_buffer_get_timestamp (rtp), gst_rtp_buffer_get_seq (rtp));
170

171
  outbuf = gst_rtp_buffer_get_payload_buffer (rtp);
172

173
  if (marker && outbuf) {
174 175
    /* mark start of talkspurt with RESYNC */
    GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
176 177
  }

178
  if (outbuf) {
179
    gst_rtp_drop_non_audio_meta (depayload, outbuf);
180 181
  }

182 183 184 185 186 187 188
  return outbuf;
}

gboolean
gst_rtp_bv_depay_plugin_init (GstPlugin * plugin)
{
  return gst_element_register (plugin, "rtpbvdepay",
189
      GST_RANK_SECONDARY, GST_TYPE_RTP_BV_DEPAY);
190
}