dtmf: convert to bit accessors
[platform/upstream/gstreamer.git] / gst / dtmf / gstrtpdtmfdepay.c
index 9f5ae92..a819467 100644 (file)
  *
  * 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.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
  */
 /**
  * SECTION:element-rtpdtmfdepay
+ * @title: rtpdtmfdepay
  * @see_also: rtpdtmfsrc, rtpdtmfmux
  *
  * This element takes RTP DTMF packets and produces sound. It also emits a
  * message on the #GstBus.
  *
- * The message is called "dtmf-event" and has the following fields
- * <informaltable>
- * <tgroup cols='4'>
- * <colspec colname='Name' />
- * <colspec colname='Type' />
- * <colspec colname='Possible values' />
- * <colspec colname='Purpose' />
- * <thead>
- * <row>
- * <entry>Name</entry>
- * <entry>GType</entry>
- * <entry>Possible values</entry>
- * <entry>Purpose</entry>
- * </row>
- * </thead>
- * <tbody>
- * <row>
- * <entry></entry>
- * <entry>G_TYPE_INT</entry>
- * <entry>0-1</entry>
- * <entry>Which of the two methods
- * specified in RFC 2833 to use. The value should be 0 for tones and 1 for
- * named events. Tones are specified by their frequencies and events are specied
- * by their number. This element currently only recognizes events.
- * Do not confuse with "method" which specified the output.
- * </entry>
- * </row>
- * <row>
- * <entry>number</entry>
- * <entry>G_TYPE_INT</entry>
- * <entry>0-16</entry>
- * <entry>The event number.</entry>
- * </row>
- * <row>
- * <entry>volume</entry>
- * <entry>G_TYPE_INT</entry>
- * <entry>0-36</entry>
- * <entry>This field describes the power level of the tone, expressed in dBm0
- * after dropping the sign. Power levels range from 0 to -63 dBm0. The range of
- * valid DTMF is from 0 to -36 dBm0.
- * </entry>
- * </row>
- * <row>
- * <entry>method</entry>
- * <entry>G_TYPE_INT</entry>
- * <entry>1</entry>
- * <entry>This field will always been 1 (ie RTP event) from this element.
- * </entry>
- * </row>
- * </tbody>
- * </tgroup>
- * </informaltable>
+ * The message is called "dtmf-event" and has the following fields:
+ *
+ * * `type` (G_TYPE_INT, 0-1): Which of the two methods
+ *   specified in RFC 2833 to use. The value should be 0 for tones and 1 for
+ *   named events. Tones are specified by their frequencies and events are specified
+ *   by their number. This element currently only recognizes events.
+ *   Do not confuse with "method" which specified the output.
+ *
+ * * `number` (G_TYPE_INT, 0-16): The event number.
+ *
+ * * `volume` (G_TYPE_INT, 0-36): This field describes the power level of the tone, expressed in dBm0
+ *   after dropping the sign. Power levels range from 0 to -63 dBm0. The range of
+ *   valid DTMF is from 0 to -36 dBm0.
+ *
+ * * `method` (G_TYPE_INT, 1): This field will always been 1 (ie RTP event) from this element.
  */
 
 #ifdef HAVE_CONFIG_H
-#  include "config.h"
+#include "config.h"
 #endif
 
+#include "gstrtpdtmfdepay.h"
+
 #include <string.h>
 #include <math.h>
 
+#include <gst/audio/audio.h>
+#include <gst/base/gstbitreader.h>
 #include <gst/rtp/gstrtpbuffer.h>
-#include "gstrtpdtmfdepay.h"
-
-#ifndef M_PI
-# define M_PI           3.14159265358979323846  /* pi */
-#endif
-
 
 #define DEFAULT_PACKET_INTERVAL  50     /* ms */
 #define MIN_PACKET_INTERVAL      10     /* ms */
 #define SAMPLE_RATE              8000
 #define SAMPLE_SIZE              16
 #define CHANNELS                 1
-#define MIN_EVENT                0
-#define MAX_EVENT                16
-#define MIN_VOLUME               0
-#define MAX_VOLUME               36
-#define MIN_INTER_DIGIT_INTERVAL 100
-#define MIN_PULSE_DURATION       250
 #define MIN_DUTY_CYCLE           (MIN_INTER_DIGIT_INTERVAL + MIN_PULSE_DURATION)
 
 #define MIN_UNIT_TIME            0
 
 typedef struct st_dtmf_key
 {
-  char *event_name;
-  int event_encoding;
   float low_frequency;
   float high_frequency;
 } DTMF_KEY;
 
 static const DTMF_KEY DTMF_KEYS[] = {
-  {"DTMF_KEY_EVENT_0", 0, 941, 1336},
-  {"DTMF_KEY_EVENT_1", 1, 697, 1209},
-  {"DTMF_KEY_EVENT_2", 2, 697, 1336},
-  {"DTMF_KEY_EVENT_3", 3, 697, 1477},
-  {"DTMF_KEY_EVENT_4", 4, 770, 1209},
-  {"DTMF_KEY_EVENT_5", 5, 770, 1336},
-  {"DTMF_KEY_EVENT_6", 6, 770, 1477},
-  {"DTMF_KEY_EVENT_7", 7, 852, 1209},
-  {"DTMF_KEY_EVENT_8", 8, 852, 1336},
-  {"DTMF_KEY_EVENT_9", 9, 852, 1477},
-  {"DTMF_KEY_EVENT_S", 10, 941, 1209},
-  {"DTMF_KEY_EVENT_P", 11, 941, 1477},
-  {"DTMF_KEY_EVENT_A", 12, 697, 1633},
-  {"DTMF_KEY_EVENT_B", 13, 770, 1633},
-  {"DTMF_KEY_EVENT_C", 14, 852, 1633},
-  {"DTMF_KEY_EVENT_D", 15, 941, 1633},
+  {941, 1336},
+  {697, 1209},
+  {697, 1336},
+  {697, 1477},
+  {770, 1209},
+  {770, 1336},
+  {770, 1477},
+  {852, 1209},
+  {852, 1336},
+  {852, 1477},
+  {941, 1209},
+  {941, 1477},
+  {697, 1633},
+  {770, 1633},
+  {852, 1633},
+  {941, 1633},
 };
 
 #define MAX_DTMF_EVENTS 16
@@ -169,8 +123,6 @@ GST_DEBUG_CATEGORY_STATIC (gst_rtp_dtmf_depay_debug);
 
 enum
 {
-
-
   /* FILL ME */
   LAST_SIGNAL
 };
@@ -182,21 +134,13 @@ enum
   PROP_MAX_DURATION
 };
 
-enum
-{
-  ARG_0
-};
-
 static GstStaticPadTemplate gst_rtp_dtmf_depay_src_template =
 GST_STATIC_PAD_TEMPLATE ("src",
     GST_PAD_SRC,
     GST_PAD_ALWAYS,
-    GST_STATIC_CAPS ("audio/x-raw-int, "
-        "width = (int) 16, "
-        "depth = (int) 16, "
-        "endianness = (int) " G_STRINGIFY (G_BYTE_ORDER) ", "
-        "signed = (boolean) true, "
-        "rate = (int) [0, MAX], " "channels = (int) 1")
+    GST_STATIC_CAPS ("audio/x-raw, "
+        "format = (string) \"" GST_AUDIO_NE (S16) "\", "
+        "rate = " GST_AUDIO_RATE_RANGE ", " "channels = (int) 1")
     );
 
 static GstStaticPadTemplate gst_rtp_dtmf_depay_sink_template =
@@ -210,49 +154,42 @@ GST_STATIC_PAD_TEMPLATE ("sink",
         "encoding-name = (string) \"TELEPHONE-EVENT\"")
     );
 
-GST_BOILERPLATE (GstRtpDTMFDepay, gst_rtp_dtmf_depay, GstBaseRTPDepayload,
-    GST_TYPE_BASE_RTP_DEPAYLOAD);
+G_DEFINE_TYPE (GstRtpDTMFDepay, gst_rtp_dtmf_depay,
+    GST_TYPE_RTP_BASE_DEPAYLOAD);
+GST_ELEMENT_REGISTER_DEFINE (rtpdtmfdepay, "rtpdtmfdepay", GST_RANK_MARGINAL,
+    GST_TYPE_RTP_DTMF_DEPAY);
 
 static void gst_rtp_dtmf_depay_set_property (GObject * object, guint prop_id,
     const GValue * value, GParamSpec * pspec);
 static void gst_rtp_dtmf_depay_get_property (GObject * object, guint prop_id,
     GValue * value, GParamSpec * pspec);
-static GstBuffer *gst_rtp_dtmf_depay_process (GstBaseRTPDepayload * depayload,
+static GstBuffer *gst_rtp_dtmf_depay_process (GstRTPBaseDepayload * depayload,
     GstBuffer * buf);
-gboolean gst_rtp_dtmf_depay_setcaps (GstBaseRTPDepayload * filter,
+gboolean gst_rtp_dtmf_depay_setcaps (GstRTPBaseDepayload * filter,
     GstCaps * caps);
 
 static void
-gst_rtp_dtmf_depay_base_init (gpointer klass)
+gst_rtp_dtmf_depay_class_init (GstRtpDTMFDepayClass * klass)
 {
-  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
+  GObjectClass *gobject_class;
+  GstElementClass *gstelement_class;
+  GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
 
-  gst_element_class_add_pad_template (element_class,
-      gst_static_pad_template_get (&gst_rtp_dtmf_depay_src_template));
-  gst_element_class_add_pad_template (element_class,
-      gst_static_pad_template_get (&gst_rtp_dtmf_depay_sink_template));
+  gobject_class = G_OBJECT_CLASS (klass);
+  gstelement_class = GST_ELEMENT_CLASS (klass);
+  gstrtpbasedepayload_class = GST_RTP_BASE_DEPAYLOAD_CLASS (klass);
 
+  gst_element_class_add_static_pad_template (gstelement_class,
+      &gst_rtp_dtmf_depay_src_template);
+  gst_element_class_add_static_pad_template (gstelement_class,
+      &gst_rtp_dtmf_depay_sink_template);
 
   GST_DEBUG_CATEGORY_INIT (gst_rtp_dtmf_depay_debug,
       "rtpdtmfdepay", 0, "rtpdtmfdepay element");
-  gst_element_class_set_details_simple (element_class,
+  gst_element_class_set_static_metadata (gstelement_class,
       "RTP DTMF packet depayloader", "Codec/Depayloader/Network",
       "Generates DTMF Sound from telephone-event RTP packets",
       "Youness Alaoui <youness.alaoui@collabora.co.uk>");
-}
-
-static void
-gst_rtp_dtmf_depay_class_init (GstRtpDTMFDepayClass * klass)
-{
-  GObjectClass *gobject_class;
-  GstElementClass *gstelement_class;
-  GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
-
-  gobject_class = (GObjectClass *) klass;
-  gstelement_class = (GstElementClass *) klass;
-  gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
-
-  parent_class = g_type_class_peek_parent (klass);
 
   gobject_class->set_property =
       GST_DEBUG_FUNCPTR (gst_rtp_dtmf_depay_set_property);
@@ -262,24 +199,24 @@ gst_rtp_dtmf_depay_class_init (GstRtpDTMFDepayClass * klass)
   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_UNIT_TIME,
       g_param_spec_uint ("unit-time", "Duration unittime",
           "The smallest unit (ms) the duration must be a multiple of (0 disables it)",
-          MIN_UNIT_TIME, MAX_UNIT_TIME, DEFAULT_UNIT_TIME, G_PARAM_READWRITE));
+          MIN_UNIT_TIME, MAX_UNIT_TIME, DEFAULT_UNIT_TIME,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MAX_DURATION,
       g_param_spec_uint ("max-duration", "Maximum duration",
           "The maxumimum duration (ms) of the outgoing soundpacket. "
           "(0 = no limit)", 0, G_MAXUINT, DEFAULT_MAX_DURATION,
-          G_PARAM_READWRITE));
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
-  gstbasertpdepayload_class->process =
+  gstrtpbasedepayload_class->process =
       GST_DEBUG_FUNCPTR (gst_rtp_dtmf_depay_process);
-  gstbasertpdepayload_class->set_caps =
+  gstrtpbasedepayload_class->set_caps =
       GST_DEBUG_FUNCPTR (gst_rtp_dtmf_depay_setcaps);
 
 }
 
 static void
-gst_rtp_dtmf_depay_init (GstRtpDTMFDepay * rtpdtmfdepay,
-    GstRtpDTMFDepayClass * klass)
+gst_rtp_dtmf_depay_init (GstRtpDTMFDepay * rtpdtmfdepay)
 {
   rtpdtmfdepay->unit_time = DEFAULT_UNIT_TIME;
 }
@@ -327,52 +264,58 @@ gst_rtp_dtmf_depay_get_property (GObject * object, guint prop_id,
 }
 
 gboolean
-gst_rtp_dtmf_depay_setcaps (GstBaseRTPDepayload * filter, GstCaps * caps)
+gst_rtp_dtmf_depay_setcaps (GstRTPBaseDepayload * filter, GstCaps * caps)
 {
-  GstCaps *srccaps;
+  GstCaps *filtercaps, *srccaps;
   GstStructure *structure = gst_caps_get_structure (caps, 0);
   gint clock_rate = 8000;       /* default */
 
   gst_structure_get_int (structure, "clock-rate", &clock_rate);
   filter->clock_rate = clock_rate;
 
-  srccaps = gst_caps_new_simple ("audio/x-raw-int",
-      "width", G_TYPE_INT, 16,
-      "depth", G_TYPE_INT, 16,
-      "endianness", G_TYPE_INT, G_BYTE_ORDER,
-      "signed", G_TYPE_BOOLEAN, TRUE,
-      "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, clock_rate, NULL);
-  gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (filter), srccaps);
+  filtercaps =
+      gst_pad_get_pad_template_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (filter));
+
+  filtercaps = gst_caps_make_writable (filtercaps);
+  gst_caps_set_simple (filtercaps, "rate", G_TYPE_INT, clock_rate, NULL);
+
+  srccaps = gst_pad_peer_query_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (filter),
+      filtercaps);
+  gst_caps_unref (filtercaps);
+
+  gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (filter), srccaps);
   gst_caps_unref (srccaps);
 
   return TRUE;
 }
 
-static void
+static GstBuffer *
 gst_dtmf_src_generate_tone (GstRtpDTMFDepay * rtpdtmfdepay,
-    GstRTPDTMFPayload payload, GstBuffer * buffer)
+    GstRTPDTMFPayload payload)
 {
+  GstBuffer *buf;
+  GstMapInfo map;
   gint16 *p;
   gint tone_size;
   double i = 0;
   double amplitude, f1, f2;
   double volume_factor;
   DTMF_KEY key = DTMF_KEYS[payload.event];
-  guint32 clock_rate = 8000 /* default */ ;
-  GstBaseRTPDepayload *depayload = GST_BASE_RTP_DEPAYLOAD (rtpdtmfdepay);
+  guint32 clock_rate;
+  GstRTPBaseDepayload *depayload = GST_RTP_BASE_DEPAYLOAD (rtpdtmfdepay);
   gint volume;
+  static GstAllocationParams params = { 0, 1, 0, 0, };
 
   clock_rate = depayload->clock_rate;
 
   /* Create a buffer for the tone */
   tone_size = (payload.duration * SAMPLE_SIZE * CHANNELS) / 8;
-  GST_BUFFER_SIZE (buffer) = tone_size;
-  GST_BUFFER_MALLOCDATA (buffer) = g_malloc (tone_size);
-  GST_BUFFER_DATA (buffer) = GST_BUFFER_MALLOCDATA (buffer);
-  GST_BUFFER_DURATION (buffer) = payload.duration * GST_SECOND / clock_rate;
+  buf = gst_buffer_new_allocate (NULL, tone_size, &params);
+  GST_BUFFER_DURATION (buf) = payload.duration * GST_SECOND / clock_rate;
   volume = payload.volume;
 
-  p = (gint16 *) GST_BUFFER_MALLOCDATA (buffer);
+  gst_buffer_map (buf, &map, GST_MAP_WRITE);
+  p = (gint16 *) map.data;
 
   volume_factor = pow (10, (-volume) / 20);
 
@@ -402,45 +345,51 @@ gst_dtmf_src_generate_tone (GstRtpDTMFDepay * rtpdtmfdepay,
 
     (rtpdtmfdepay->sample)++;
   }
+
+  gst_buffer_unmap (buf, &map);
+
+  return buf;
 }
 
 
 static GstBuffer *
-gst_rtp_dtmf_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
+gst_rtp_dtmf_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
 {
 
   GstRtpDTMFDepay *rtpdtmfdepay = NULL;
   GstBuffer *outbuf = NULL;
-  gint payload_len;
+  guint payload_len;
   guint8 *payload = NULL;
   guint32 timestamp;
   GstRTPDTMFPayload dtmf_payload;
   gboolean marker;
   GstStructure *structure = NULL;
   GstMessage *dtmf_message = NULL;
+  GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
+  GstBitReader bitreader;
 
   rtpdtmfdepay = GST_RTP_DTMF_DEPAY (depayload);
 
-  if (!gst_rtp_buffer_validate (buf))
-    goto bad_packet;
+  gst_rtp_buffer_map (buf, GST_MAP_READ, &rtpbuffer);
 
-  payload_len = gst_rtp_buffer_get_payload_len (buf);
-  payload = gst_rtp_buffer_get_payload (buf);
+  payload_len = gst_rtp_buffer_get_payload_len (&rtpbuffer);
+  payload = gst_rtp_buffer_get_payload (&rtpbuffer);
 
-  if (payload_len != sizeof (GstRTPDTMFPayload))
+  if (payload_len != 4)
     goto bad_packet;
 
-  memcpy (&dtmf_payload, payload, sizeof (GstRTPDTMFPayload));
+  gst_bit_reader_init (&bitreader, payload, payload_len);
+  gst_bit_reader_get_bits_uint8 (&bitreader, &dtmf_payload.event, 8);
+  gst_bit_reader_skip (&bitreader, 2);
+  gst_bit_reader_get_bits_uint8 (&bitreader, &dtmf_payload.volume, 6);
+  gst_bit_reader_get_bits_uint16 (&bitreader, &dtmf_payload.duration, 16);
 
   if (dtmf_payload.event > MAX_EVENT)
     goto bad_packet;
 
+  marker = gst_rtp_buffer_get_marker (&rtpbuffer);
 
-  marker = gst_rtp_buffer_get_marker (buf);
-
-  timestamp = gst_rtp_buffer_get_timestamp (buf);
-
-  dtmf_payload.duration = g_ntohs (dtmf_payload.duration);
+  timestamp = gst_rtp_buffer_get_timestamp (&rtpbuffer);
 
   /* clip to whole units of unit_time */
   if (rtpdtmfdepay->unit_time) {
@@ -479,7 +428,7 @@ gst_rtp_dtmf_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
     rtpdtmfdepay->sample = 0;
     rtpdtmfdepay->previous_ts = timestamp;
     rtpdtmfdepay->previous_duration = dtmf_payload.duration;
-    rtpdtmfdepay->first_gst_ts = GST_BUFFER_TIMESTAMP (buf);
+    rtpdtmfdepay->first_gst_ts = GST_BUFFER_PTS (buf);
 
     structure = gst_structure_new ("dtmf-event",
         "number", G_TYPE_INT, dtmf_payload.event,
@@ -515,11 +464,10 @@ gst_rtp_dtmf_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
 
   /* If late or duplicate packet (like the redundant end packet). Ignore */
   if (dtmf_payload.duration > 0) {
-    outbuf = gst_buffer_new ();
-    gst_dtmf_src_generate_tone (rtpdtmfdepay, dtmf_payload, outbuf);
+    outbuf = gst_dtmf_src_generate_tone (rtpdtmfdepay, dtmf_payload);
 
 
-    GST_BUFFER_TIMESTAMP (outbuf) = rtpdtmfdepay->first_gst_ts +
+    GST_BUFFER_PTS (outbuf) = rtpdtmfdepay->first_gst_ts +
         (rtpdtmfdepay->previous_duration - dtmf_payload.duration) *
         GST_SECOND / depayload->clock_rate;
     GST_BUFFER_OFFSET (outbuf) =
@@ -534,18 +482,16 @@ gst_rtp_dtmf_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
 
   }
 
-  return outbuf;
+  gst_rtp_buffer_unmap (&rtpbuffer);
 
+  return outbuf;
 
 bad_packet:
   GST_ELEMENT_WARNING (rtpdtmfdepay, STREAM, DECODE,
       ("Packet did not validate"), (NULL));
-  return NULL;
-}
 
-gboolean
-gst_rtp_dtmf_depay_plugin_init (GstPlugin * plugin)
-{
-  return gst_element_register (plugin, "rtpdtmfdepay",
-      GST_RANK_MARGINAL, GST_TYPE_RTP_DTMF_DEPAY);
+  if (rtpbuffer.buffer != NULL)
+    gst_rtp_buffer_unmap (&rtpbuffer);
+
+  return NULL;
 }