dtmf: convert to bit accessors

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-good/-/merge_requests/949>
This commit is contained in:
Doug Nazar 2021-04-14 11:13:45 -04:00
parent c071cbbe30
commit 850a6f5f6f
3 changed files with 24 additions and 27 deletions

View file

@ -34,19 +34,9 @@
typedef struct typedef struct
{ {
unsigned event:8; /* Current DTMF event */ guint8 event; /* Current DTMF event */
#if G_BYTE_ORDER == G_LITTLE_ENDIAN guint8 volume; /* power level of the tone, in dBm0 */
unsigned volume:6; /* power level of the tone, in dBm0 */ guint16 duration; /* Duration of digit, in timestamp units */
unsigned r:1; /* Reserved-bit */
unsigned e:1; /* End-bit */
#elif G_BYTE_ORDER == G_BIG_ENDIAN
unsigned e:1; /* End-bit */
unsigned r:1; /* Reserved-bit */
unsigned volume:6; /* power level of the tone, in dBm0 */
#else
#error "G_BYTE_ORDER should be big or little endian."
#endif
unsigned duration:16; /* Duration of digit, in timestamp units */
} GstRTPDTMFPayload; } GstRTPDTMFPayload;
#endif /* __GST_RTP_DTMF_COMMON_H__ */ #endif /* __GST_RTP_DTMF_COMMON_H__ */

View file

@ -54,6 +54,7 @@
#include <math.h> #include <math.h>
#include <gst/audio/audio.h> #include <gst/audio/audio.h>
#include <gst/base/gstbitreader.h>
#include <gst/rtp/gstrtpbuffer.h> #include <gst/rtp/gstrtpbuffer.h>
#define DEFAULT_PACKET_INTERVAL 50 /* ms */ #define DEFAULT_PACKET_INTERVAL 50 /* ms */
@ -357,7 +358,7 @@ gst_rtp_dtmf_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
GstRtpDTMFDepay *rtpdtmfdepay = NULL; GstRtpDTMFDepay *rtpdtmfdepay = NULL;
GstBuffer *outbuf = NULL; GstBuffer *outbuf = NULL;
gint payload_len; guint payload_len;
guint8 *payload = NULL; guint8 *payload = NULL;
guint32 timestamp; guint32 timestamp;
GstRTPDTMFPayload dtmf_payload; GstRTPDTMFPayload dtmf_payload;
@ -365,6 +366,7 @@ gst_rtp_dtmf_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
GstStructure *structure = NULL; GstStructure *structure = NULL;
GstMessage *dtmf_message = NULL; GstMessage *dtmf_message = NULL;
GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT; GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
GstBitReader bitreader;
rtpdtmfdepay = GST_RTP_DTMF_DEPAY (depayload); rtpdtmfdepay = GST_RTP_DTMF_DEPAY (depayload);
@ -373,10 +375,14 @@ gst_rtp_dtmf_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
payload_len = gst_rtp_buffer_get_payload_len (&rtpbuffer); payload_len = gst_rtp_buffer_get_payload_len (&rtpbuffer);
payload = gst_rtp_buffer_get_payload (&rtpbuffer); payload = gst_rtp_buffer_get_payload (&rtpbuffer);
if (payload_len != sizeof (GstRTPDTMFPayload)) if (payload_len != 4)
goto bad_packet; 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) if (dtmf_payload.event > MAX_EVENT)
goto bad_packet; goto bad_packet;
@ -385,8 +391,6 @@ gst_rtp_dtmf_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
timestamp = gst_rtp_buffer_get_timestamp (&rtpbuffer); timestamp = gst_rtp_buffer_get_timestamp (&rtpbuffer);
dtmf_payload.duration = g_ntohs (dtmf_payload.duration);
/* clip to whole units of unit_time */ /* clip to whole units of unit_time */
if (rtpdtmfdepay->unit_time) { if (rtpdtmfdepay->unit_time) {
guint unit_time_clock = guint unit_time_clock =

View file

@ -85,6 +85,7 @@
#include <glib.h> #include <glib.h>
#include "gstrtpdtmfsrc.h" #include "gstrtpdtmfsrc.h"
#include <gst/base/gstbitwriter.h>
#define GST_RTP_DTMF_TYPE_EVENT 1 #define GST_RTP_DTMF_TYPE_EVENT 1
#define DEFAULT_PTIME 40 /* ms */ #define DEFAULT_PTIME 40 /* ms */
@ -537,8 +538,6 @@ gst_rtp_dtmf_prepare_rtp_headers (GstRTPDTMFSrc * dtmfsrc,
/* Only the very first packet gets a marker */ /* Only the very first packet gets a marker */
if (dtmfsrc->first_packet) { if (dtmfsrc->first_packet) {
gst_rtp_buffer_set_marker (rtpbuf, TRUE); gst_rtp_buffer_set_marker (rtpbuf, TRUE);
} else if (dtmfsrc->last_packet) {
dtmfsrc->payload->e = 1;
} }
dtmfsrc->seqnum++; dtmfsrc->seqnum++;
@ -552,10 +551,12 @@ static GstBuffer *
gst_rtp_dtmf_src_create_next_rtp_packet (GstRTPDTMFSrc * dtmfsrc) gst_rtp_dtmf_src_create_next_rtp_packet (GstRTPDTMFSrc * dtmfsrc)
{ {
GstBuffer *buf; GstBuffer *buf;
GstRTPDTMFPayload *payload;
GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT; GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
GstBitWriter bitwriter;
guint8 *payload;
guint8 end = dtmfsrc->last_packet ? 0x02 : 0;
buf = gst_rtp_buffer_new_allocate (sizeof (GstRTPDTMFPayload), 0, 0); buf = gst_rtp_buffer_new_allocate (4, 0, 0);
gst_rtp_buffer_map (buf, GST_MAP_READWRITE, &rtpbuffer); gst_rtp_buffer_map (buf, GST_MAP_READWRITE, &rtpbuffer);
@ -569,12 +570,14 @@ gst_rtp_dtmf_src_create_next_rtp_packet (GstRTPDTMFSrc * dtmfsrc)
GST_BUFFER_DURATION (buf) = dtmfsrc->ptime * GST_MSECOND; GST_BUFFER_DURATION (buf) = dtmfsrc->ptime * GST_MSECOND;
GST_BUFFER_PTS (buf) = dtmfsrc->timestamp; GST_BUFFER_PTS (buf) = dtmfsrc->timestamp;
payload = (GstRTPDTMFPayload *) gst_rtp_buffer_get_payload (&rtpbuffer); payload = gst_rtp_buffer_get_payload (&rtpbuffer);
/* copy payload and convert to network-byte order */ memset (payload, 0, 4);
memmove (payload, dtmfsrc->payload, sizeof (GstRTPDTMFPayload)); gst_bit_writer_init_with_data (&bitwriter, payload, 4, FALSE);
gst_bit_writer_put_bits_uint8 (&bitwriter, dtmfsrc->payload->event, 8);
payload->duration = g_htons (payload->duration); gst_bit_writer_put_bits_uint8 (&bitwriter, end, 2);
gst_bit_writer_put_bits_uint8 (&bitwriter, dtmfsrc->payload->volume, 6);
gst_bit_writer_put_bits_uint16 (&bitwriter, dtmfsrc->payload->duration, 16);
if (dtmfsrc->redundancy_count <= 1 && dtmfsrc->last_packet) { if (dtmfsrc->redundancy_count <= 1 && dtmfsrc->last_packet) {
GstClockTime inter_digit_interval = MIN_INTER_DIGIT_INTERVAL; GstClockTime inter_digit_interval = MIN_INTER_DIGIT_INTERVAL;