gstgsmenc.c 5.1 KB
Newer Older
1 2 3 4
/*
 * Farsight
 * GStreamer GSM encoder
 * Copyright (C) 2005 Philippe Khalaf <burger@speedy.org>
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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.
 */


23 24 25
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
26 27 28 29
#include <string.h>

#include "gstgsmenc.h"

30 31 32
GST_DEBUG_CATEGORY (gsmenc_debug);
#define GST_CAT_DEFAULT (gsmenc_debug)

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
33 34
/* elementfactory information */
GstElementDetails gst_gsmenc_details = {
35 36
  "GSM audio encoder",
  "Codec/Encoder/Audio",
37 38
  "Encodes GSM audio",
  "Philippe Khalaf <burger@speedy.org>",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
39 40 41
};

/* GSMEnc signals and args */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
42 43
enum
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
44 45 46 47
  /* FILL ME */
  LAST_SIGNAL
};

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
48 49
enum
{
50
  /* FILL ME */
51
  ARG_0
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
52 53
};

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
54 55 56
static void gst_gsmenc_base_init (gpointer g_class);
static void gst_gsmenc_class_init (GstGSMEnc * klass);
static void gst_gsmenc_init (GstGSMEnc * gsmenc);
57

58
static GstFlowReturn gst_gsmenc_chain (GstPad * pad, GstBuffer * buf);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
59 60

static GstElementClass *parent_class = NULL;
61

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
62 63 64 65 66 67 68 69
GType
gst_gsmenc_get_type (void)
{
  static GType gsmenc_type = 0;

  if (!gsmenc_type) {
    static const GTypeInfo gsmenc_info = {
      sizeof (GstGSMEncClass),
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
70
      gst_gsmenc_base_init,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
71 72 73 74 75 76 77 78
      NULL,
      (GClassInitFunc) gst_gsmenc_class_init,
      NULL,
      NULL,
      sizeof (GstGSMEnc),
      0,
      (GInstanceInitFunc) gst_gsmenc_init,
    };
79

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
80
    gsmenc_type =
81
        g_type_register_static (GST_TYPE_ELEMENT, "GstGSMEnc", &gsmenc_info, 0);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
82 83 84 85
  }
  return gsmenc_type;
}

David Schleef's avatar
David Schleef committed
86
static GstStaticPadTemplate gsmenc_src_template =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
87 88 89
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
90
    GST_STATIC_CAPS ("audio/x-gsm, " "rate = (int) 8000, " "channels = (int) 1")
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
91
    );
David Schleef's avatar
David Schleef committed
92 93

static GstStaticPadTemplate gsmenc_sink_template =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
94 95 96 97
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("audio/x-raw-int, "
98 99 100
        "endianness = (int) BYTE_ORDER, "
        "signed = (boolean) true, "
        "width = (int) 16, "
101
        "depth = (int) 16, " "rate = (int) 8000, " "channels = (int) 1")
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
102
    );
Iain Holmes's avatar
Iain Holmes committed
103 104 105 106 107 108

static void
gst_gsmenc_base_init (gpointer g_class)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);

David Schleef's avatar
David Schleef committed
109 110 111 112
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&gsmenc_sink_template));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&gsmenc_src_template));
Iain Holmes's avatar
Iain Holmes committed
113 114
  gst_element_class_set_details (element_class, &gst_gsmenc_details);
}
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
115

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
116
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
117
gst_gsmenc_class_init (GstGSMEnc * klass)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
118 119 120 121
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
122 123
  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
124 125 126

  parent_class = g_type_class_ref (GST_TYPE_ELEMENT);

127
  GST_DEBUG_CATEGORY_INIT (gsmenc_debug, "gsmenc", 0, "GSM Encoder");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
128 129 130
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
131
gst_gsmenc_init (GstGSMEnc * gsmenc)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
132 133
{
  /* create the sink and src pads */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
134 135 136
  gsmenc->sinkpad =
      gst_pad_new_from_template (gst_static_pad_template_get
      (&gsmenc_sink_template), "sink");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
137 138 139
  gst_element_add_pad (GST_ELEMENT (gsmenc), gsmenc->sinkpad);
  gst_pad_set_chain_function (gsmenc->sinkpad, gst_gsmenc_chain);

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
140 141 142
  gsmenc->srcpad =
      gst_pad_new_from_template (gst_static_pad_template_get
      (&gsmenc_src_template), "src");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
143 144 145
  gst_element_add_pad (GST_ELEMENT (gsmenc), gsmenc->srcpad);

  gsmenc->state = gsm_create ();
146 147
  // turn on WAN49 handling
  gint use_wav49 = 0;
148

149
  gsm_option (gsmenc->state, GSM_OPT_WAV49, &use_wav49);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
150

151
  gsmenc->adapter = gst_adapter_new ();
152

153
  gsmenc->next_ts = 0;
154 155 156 157 158 159 160
}

static GstFlowReturn
gst_gsmenc_chain (GstPad * pad, GstBuffer * buf)
{
  GstGSMEnc *gsmenc;
  gsm_signal *data;
161
  GstFlowReturn ret = GST_FLOW_OK;
162

163 164
  gsmenc = GST_GSMENC (gst_pad_get_parent (pad));
  gst_adapter_push (gsmenc->adapter, buf);
165

166
  while (gst_adapter_available (gsmenc->adapter) >= 320) {
167
    GstBuffer *outbuf;
168

169 170 171 172
    outbuf = gst_buffer_new_and_alloc (33 * sizeof (gsm_byte));
    GST_BUFFER_TIMESTAMP (outbuf) = gsmenc->next_ts;
    GST_BUFFER_DURATION (outbuf) = 20 * GST_MSECOND;
    gsmenc->next_ts += 20 * GST_MSECOND;
173

174 175
    // encode 160 16-bit samples into 33 bytes
    data = (gsm_signal *) gst_adapter_peek (gsmenc->adapter, 320);
176
    gsm_encode (gsmenc->state, data, (gsm_byte *) GST_BUFFER_DATA (outbuf));
177
    gst_adapter_flush (gsmenc->adapter, 320);
178

179 180 181
    gst_buffer_set_caps (outbuf, gst_pad_get_caps (gsmenc->srcpad));
    GST_DEBUG ("Pushing buffer of size %d", GST_BUFFER_SIZE (outbuf));
    //gst_util_dump_mem (GST_BUFFER_DATA(outbuf), GST_BUFFER_SIZE (outbuf));
182
    ret = gst_pad_push (gsmenc->srcpad, outbuf);
183 184
  }

185 186 187
  gst_object_unref (gsmenc);

  return ret;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
188
}