gstrtpgsmenc.c 4.84 KB
Newer Older
Zeeshan Ali's avatar
Zeeshan Ali committed
1 2
/* GStreamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3
 * Copyright (C) <2005> Zeeshan Ali <zeenix@gmail.com>
Zeeshan Ali's avatar
Zeeshan Ali committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * 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
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

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

25
#include <stdlib.h>
Zeeshan Ali's avatar
Zeeshan Ali committed
26
#include <string.h>
27 28
#include <gst/rtp/gstrtpbuffer.h>

Zeeshan Ali's avatar
Zeeshan Ali committed
29 30 31 32 33
#include "gstrtpgsmenc.h"

/* elementfactory information */
static GstElementDetails gst_rtpgsmenc_details = {
  "RTP GSM Audio Encoder",
34
  "Codec/Encoder/Network",
35
  "Encodes GSM audio into a RTP packet",
36
  "Zeeshan Ali <zeenix@gmail.com>"
Zeeshan Ali's avatar
Zeeshan Ali committed
37 38
};

David Schleef's avatar
David Schleef committed
39
static GstStaticPadTemplate gst_rtpgsmenc_sink_template =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
40
GST_STATIC_PAD_TEMPLATE ("sink",
David Schleef's avatar
David Schleef committed
41 42
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
43
    GST_STATIC_CAPS ("audio/x-gsm, " "rate = (int) 8000, " "channels = (int) 1")
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
44
    );
David Schleef's avatar
David Schleef committed
45 46

static GstStaticPadTemplate gst_rtpgsmenc_src_template =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
47
GST_STATIC_PAD_TEMPLATE ("src",
David Schleef's avatar
David Schleef committed
48 49
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
50 51
    GST_STATIC_CAPS ("application/x-rtp, "
        "media = (string) \"audio\", "
52
        "payload = (int) " GST_RTP_PAYLOAD_GSM_STRING ", "
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
53
        "clock-rate = (int) 8000, " "encoding-name = (string) \"GSM\"")
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
54
    );
David Schleef's avatar
David Schleef committed
55

56 57 58 59 60
static gboolean gst_rtpgsmenc_setcaps (GstBaseRTPPayload * payload,
    GstCaps * caps);
static GstFlowReturn gst_rtpgsmenc_handle_buffer (GstBaseRTPPayload * payload,
    GstBuffer * buffer);

61 62
GST_BOILERPLATE (GstRTPGSMEnc, gst_rtpgsmenc, GstBaseRTPPayload,
    GST_TYPE_BASE_RTP_PAYLOAD);
Zeeshan Ali's avatar
Zeeshan Ali committed
63

Ronald S. Bultje's avatar
Ronald S. Bultje committed
64
static void
65
gst_rtpgsmenc_base_init (gpointer klass)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
66 67 68 69
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);

  gst_element_class_add_pad_template (element_class,
David Schleef's avatar
David Schleef committed
70
      gst_static_pad_template_get (&gst_rtpgsmenc_sink_template));
Ronald S. Bultje's avatar
Ronald S. Bultje committed
71
  gst_element_class_add_pad_template (element_class,
David Schleef's avatar
David Schleef committed
72
      gst_static_pad_template_get (&gst_rtpgsmenc_src_template));
Ronald S. Bultje's avatar
Ronald S. Bultje committed
73 74 75
  gst_element_class_set_details (element_class, &gst_rtpgsmenc_details);
}

Zeeshan Ali's avatar
Zeeshan Ali committed
76
static void
77
gst_rtpgsmenc_class_init (GstRTPGSMEncClass * klass)
Zeeshan Ali's avatar
Zeeshan Ali committed
78 79 80
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;
81
  GstBaseRTPPayloadClass *gstbasertppayload_class;
Zeeshan Ali's avatar
Zeeshan Ali committed
82 83 84

  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;
85
  gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
Zeeshan Ali's avatar
Zeeshan Ali committed
86

87
  parent_class = g_type_class_ref (GST_TYPE_BASE_RTP_PAYLOAD);
Zeeshan Ali's avatar
Zeeshan Ali committed
88

89 90
  gstbasertppayload_class->set_caps = gst_rtpgsmenc_setcaps;
  gstbasertppayload_class->handle_buffer = gst_rtpgsmenc_handle_buffer;
Zeeshan Ali's avatar
Zeeshan Ali committed
91 92 93
}

static void
94
gst_rtpgsmenc_init (GstRTPGSMEnc * rtpgsmenc, GstRTPGSMEncClass * klass)
Zeeshan Ali's avatar
Zeeshan Ali committed
95
{
96 97
  GST_BASE_RTP_PAYLOAD (rtpgsmenc)->clock_rate = 8000;
  GST_BASE_RTP_PAYLOAD_PT (rtpgsmenc) = GST_RTP_PAYLOAD_GSM;
Zeeshan Ali's avatar
Zeeshan Ali committed
98 99
}

100 101
static gboolean
gst_rtpgsmenc_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
Zeeshan Ali's avatar
Zeeshan Ali committed
102
{
103
  const char *stname;
David Schleef's avatar
David Schleef committed
104
  GstStructure *structure;
Zeeshan Ali's avatar
Zeeshan Ali committed
105

David Schleef's avatar
David Schleef committed
106 107
  structure = gst_caps_get_structure (caps, 0);

108 109 110 111 112
  stname = gst_structure_get_name (structure);

  if (0 == strcmp ("audio/x-gsm", stname)) {
    gst_basertppayload_set_options (payload, "audio", FALSE, "GSM", 8000);
  } else {
113
    return FALSE;
114
  }
Zeeshan Ali's avatar
Zeeshan Ali committed
115

116
  gst_basertppayload_set_outcaps (payload, NULL);
Zeeshan Ali's avatar
Zeeshan Ali committed
117

118
  return TRUE;
Zeeshan Ali's avatar
Zeeshan Ali committed
119 120
}

121 122 123
static GstFlowReturn
gst_rtpgsmenc_handle_buffer (GstBaseRTPPayload * basepayload,
    GstBuffer * buffer)
Zeeshan Ali's avatar
Zeeshan Ali committed
124
{
125
  GstRTPGSMEnc *rtpgsmenc;
126
  guint size, payload_len;
Zeeshan Ali's avatar
Zeeshan Ali committed
127
  GstBuffer *outbuf;
128 129 130
  guint8 *payload, *data;
  GstClockTime timestamp;
  GstFlowReturn ret;
Zeeshan Ali's avatar
Zeeshan Ali committed
131

132
  rtpgsmenc = GST_RTP_GSM_ENC (basepayload);
Zeeshan Ali's avatar
Zeeshan Ali committed
133

134 135
  size = GST_BUFFER_SIZE (buffer);
  timestamp = GST_BUFFER_TIMESTAMP (buffer);
Zeeshan Ali's avatar
Zeeshan Ali committed
136

137 138
  /* FIXME, only one GSM frame per RTP packet for now */
  payload_len = size;
Zeeshan Ali's avatar
Zeeshan Ali committed
139

140 141
  outbuf = gst_rtpbuffer_new_allocate (payload_len, 0, 0);
  /* FIXME, assert for now */
142
  g_assert (payload_len <= GST_BASE_RTP_PAYLOAD_MTU (rtpgsmenc));
Zeeshan Ali's avatar
Zeeshan Ali committed
143

144 145
  /* copy timestamp */
  GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
Zeeshan Ali's avatar
Zeeshan Ali committed
146

147 148
  /* get payload */
  payload = gst_rtpbuffer_get_payload (outbuf);
Zeeshan Ali's avatar
Zeeshan Ali committed
149

150
  data = GST_BUFFER_DATA (buffer);
Zeeshan Ali's avatar
Zeeshan Ali committed
151

152 153
  /* copy data in payload */
  memcpy (&payload[0], data, size);
Zeeshan Ali's avatar
Zeeshan Ali committed
154

155
  gst_buffer_unref (buffer);
Zeeshan Ali's avatar
Zeeshan Ali committed
156

157 158
  GST_DEBUG ("gst_rtpgsmenc_chain: pushing buffer of size %d",
      GST_BUFFER_SIZE (outbuf));
Zeeshan Ali's avatar
Zeeshan Ali committed
159

160
  ret = gst_basertppayload_push (basepayload, outbuf);
Zeeshan Ali's avatar
Zeeshan Ali committed
161

162
  return ret;
Zeeshan Ali's avatar
Zeeshan Ali committed
163 164 165
}

gboolean
Ronald S. Bultje's avatar
Ronald S. Bultje committed
166
gst_rtpgsmenc_plugin_init (GstPlugin * plugin)
Zeeshan Ali's avatar
Zeeshan Ali committed
167
{
Ronald S. Bultje's avatar
Ronald S. Bultje committed
168
  return gst_element_register (plugin, "rtpgsmenc",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
169
      GST_RANK_NONE, GST_TYPE_RTP_GSM_ENC);
Zeeshan Ali's avatar
Zeeshan Ali committed
170
}