Created
January 8, 2018 04:26
-
-
Save kyv/5f3873299fdbd46dd2e4c600f44923f4 to your computer and use it in GitHub Desktop.
empeded opus rtp stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* opusStream | |
* Copyright (C) 2009 Kevin Brown <kevin@ki-ai.org> | |
* | |
* 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., 51 Franklin St, Fifth Floor, | |
* Boston, MA 02110-1301, USA. | |
*/ | |
/* | |
* Based on gstreamer examples, this | |
* streams opus over rtp to a reciever | |
* and blinks a led with audio intensity | |
* | |
* Originally written for the imx233, it depends on gpio-mmap, | |
* which you can find more about here: | |
* https://www.jann.cc/2013/05/04/imx233_olinuxino_current_state.html#gpio | |
* | |
*/ | |
#define GLIB_DISABLE_DEPRECATION_WARNINGS | |
#include <string.h> | |
#include <math.h> | |
#include <stdio.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
#include "gpio-mmap.h" | |
#include <gst/gst.h> | |
/* the caps of the sender RTP stream. This is usually negotiated out of band with | |
* SDP or RTSP. */ | |
#define AUDIO_CAPS "application/x-rtp, media=(string)audio, clock-rate=(int)48000, encoding-name=(string)X-GST-OPUS-DRAFT-SPITTKA-00" | |
#define AUDIO_DEPAY "rtpopusdepay" | |
#define AUDIO_DEC "opusdec" | |
#define AUDIO_SINK "alsasink" | |
/* change this to send the RTP data and RTCP to another host */ | |
#define RX_HOST "xx.xx.xx.xx" | |
#define AUDIO_SRC "alsasrc" | |
/* #define AUDIO_SRC "audiotestsrc" */ | |
/* the encoder and payloader elements */ | |
#define AUDIO_ENC "opusenc" | |
#define AUDIO_PAY "rtpopuspay" | |
/* the destination machine to send RTCP to. This is the address of the sender and | |
* is used to send back the RTCP reports of this receiver. If the data is sent | |
* from another machine, change this address. */ | |
#define TX_HOST "xx.xx.xx.xx" | |
/* print the stats of a source */ | |
//static void | |
//rx_print_source_stats (GObject * source) | |
//{ | |
// GstStructure *stats; | |
// gchar *str; | |
// | |
// g_return_if_fail (source != NULL); | |
// | |
// /* get the source stats */ | |
// g_object_get (source, "stats", &stats, NULL); | |
// | |
// /* simply dump the stats structure */ | |
// str = gst_structure_to_string (stats); | |
// g_print ("source stats: %s\n", str); | |
// | |
// gst_structure_free (stats); | |
// g_free (str); | |
//} | |
static void | |
print_source_stats (GObject * source) | |
{ | |
GstStructure *stats; | |
gchar *str; | |
/* get the source stats */ | |
g_object_get (source, "stats", &stats, NULL); | |
/* simply dump the stats structure */ | |
str = gst_structure_to_string (stats); | |
g_print ("source stats: %s\n", str); | |
gst_structure_free (stats); | |
g_free (str); | |
} | |
/* will be called when rtpbin signals on-ssrc-active. It means that an RTCP | |
* packet was received from another source. */ | |
static void | |
on_ssrc_active_cb (GstElement * rtpbin, guint sessid, guint ssrc, | |
GstElement * depay) | |
{ | |
GObject *session, *isrc, *osrc; | |
g_print ("got RTCP from session %u, SSRC %u\n", sessid, ssrc); | |
/* get the right session */ | |
g_signal_emit_by_name (rtpbin, "get-internal-session", sessid, &session); | |
/* get the internal source (the SSRC allocated to us, the receiver */ | |
g_object_get (session, "internal-source", &isrc, NULL); | |
//rx_print_source_stats (isrc); | |
/* get the remote source that sent us RTCP */ | |
g_signal_emit_by_name (session, "get-source-by-ssrc", ssrc, &osrc); | |
//rx_print_source_stats (osrc); | |
} | |
/* this function is called every second and dumps the RTP manager stats */ | |
static gboolean | |
print_stats (GstElement * rtpbin) | |
{ | |
GObject *session; | |
GValueArray *arr; | |
GValue *val; | |
guint i; | |
g_print ("***********************************\n"); | |
/* get session 0 */ | |
g_signal_emit_by_name (rtpbin, "get-internal-session", 0, &session); | |
/* print all the sources in the session, this includes the internal source */ | |
g_object_get (session, "sources", &arr, NULL); | |
for (i = 0; i < arr->n_values; i++) { | |
GObject *source; | |
val = g_value_array_get_nth (arr, i); | |
source = g_value_get_object (val); | |
print_source_stats (source); | |
} | |
g_value_array_free (arr); | |
g_object_unref (session); | |
return TRUE; | |
} | |
/* will be called when rtpbin has validated a payload that we can depayload */ | |
static void | |
pad_added_cb (GstElement * rtpbin, GstPad * new_pad, GstElement * depay) | |
{ | |
GstPad *sinkpad; | |
GstPadLinkReturn lres; | |
g_print ("new payload on pad: %s\n", GST_PAD_NAME (new_pad)); | |
sinkpad = gst_element_get_static_pad (depay, "sink"); | |
g_assert (sinkpad); | |
lres = gst_pad_link (new_pad, sinkpad); | |
g_assert (lres == GST_PAD_LINK_OK); | |
gst_object_unref (sinkpad); | |
} | |
static int | |
led_set (int value) { | |
gpio_output(2,1); //bank 2 bit 1 = GPIO65 the LED on board | |
GPIO_WRITE_PIN(65, value); | |
} | |
static gboolean | |
level_message_handler (GstBus * bus, GstMessage * message, gpointer data) | |
{ | |
if (message->type == GST_MESSAGE_ELEMENT) { | |
const GstStructure *s = gst_message_get_structure (message); | |
const gchar *name = gst_structure_get_name (s); | |
if (strcmp (name, "level") == 0) { | |
gint channels; | |
GstClockTime endtime; | |
//gdouble rms_dB, peak_dB, decay_dB; | |
gdouble rms_dB; | |
gdouble rms; | |
const GValue *array_val; | |
const GValue *value; | |
GValueArray *rms_arr, *peak_arr, *decay_arr; | |
gint i; | |
char brightness; | |
if (!gst_structure_get_clock_time (s, "endtime", &endtime)) | |
g_warning ("Could not parse endtime"); | |
/* the values are packed into GValueArrays with the value per channel */ | |
array_val = gst_structure_get_value (s, "rms"); | |
rms_arr = (GValueArray *) g_value_get_boxed (array_val); | |
array_val = gst_structure_get_value (s, "peak"); | |
peak_arr = (GValueArray *) g_value_get_boxed (array_val); | |
array_val = gst_structure_get_value (s, "decay"); | |
decay_arr = (GValueArray *) g_value_get_boxed (array_val); | |
/* we can get the number of channels as the length of any of the value | |
* arrays */ | |
channels = rms_arr->n_values; | |
//g_print ("endtime: %" GST_TIME_FORMAT ", channels: %d\n", | |
// GST_TIME_ARGS (endtime), channels); | |
for (i = 0; i < channels; ++i) { | |
//g_print ("channel %d\n", i); | |
value = g_value_array_get_nth (rms_arr, i); | |
rms_dB = g_value_get_double (value); | |
value = g_value_array_get_nth (peak_arr, i); | |
//peak_dB = g_value_get_double (value); | |
value = g_value_array_get_nth (decay_arr, i); | |
//decay_dB = g_value_get_double (value); | |
//g_print (" RMS: %f dB, peak: %f dB, decay: %f dB\n", | |
// rms_dB, peak_dB, decay_dB); | |
/* converting from dB to normal gives us a value between 0.0 and 1.0 */ | |
rms = pow (10, rms_dB / 20); | |
if ( rms > 0.25 ) | |
{ | |
brightness = 1; | |
} | |
else | |
{ | |
brightness = 0; | |
} | |
led_set(brightness); | |
// g_print (" normalized rms value: %f\n", rms); | |
} | |
} | |
} | |
/* we handled the message we want, and ignored the ones we didn't want. | |
* so the core can unref the message for us */ | |
return TRUE; | |
} | |
int | |
init_rx () | |
{ | |
GstElement *rtpbin, *rtpsrc, *rtcpsrc, *rtcpsink; | |
GstElement *audiodepay, *audiodec, *audiores, *audioconv, *level, | |
*audiorate, *audiodynamic, *audiosink; | |
GstElement *pipeline; | |
GMainLoop *loop; | |
GstCaps *caps; | |
GstBus *bus; | |
guint watch_id; | |
gboolean res; | |
GstPadLinkReturn lres; | |
GstPad *srcpad, *sinkpad; | |
gpio_map(); | |
/* always init first */ | |
gst_init (NULL, NULL); | |
/* the pipeline to hold everything */ | |
pipeline = gst_pipeline_new (NULL); | |
g_assert (pipeline); | |
/* the udp src and source we will use for RTP and RTCP */ | |
rtpsrc = gst_element_factory_make ("udpsrc", "rtpsrc"); | |
g_assert (rtpsrc); | |
g_object_set (rtpsrc, "port", 5002, NULL); | |
/* we need to set caps on the udpsrc for the RTP data */ | |
caps = gst_caps_from_string (AUDIO_CAPS); | |
g_object_set (rtpsrc, "caps", caps, NULL); | |
gst_caps_unref (caps); | |
rtcpsrc = gst_element_factory_make ("udpsrc", "rtcpsrc"); | |
g_assert (rtcpsrc); | |
g_object_set (rtcpsrc, "port", 5003, NULL); | |
rtcpsink = gst_element_factory_make ("udpsink", "rtcpsink"); | |
g_assert (rtcpsink); | |
g_object_set (rtcpsink, "port", 5007, "host", TX_HOST, NULL); | |
/* no need for synchronisation or preroll on the RTCP sink */ | |
g_object_set (rtcpsink, "async", FALSE, "sync", FALSE, NULL); | |
gst_bin_add_many (GST_BIN (pipeline), rtpsrc, rtcpsrc, rtcpsink, NULL); | |
/* the depayloading and decoding */ | |
audiodepay = gst_element_factory_make (AUDIO_DEPAY, "audiodepay"); | |
g_assert (audiodepay); | |
audiodec = gst_element_factory_make (AUDIO_DEC, "audiodec"); | |
g_assert (audiodec); | |
/* the audio playback and format conversion */ | |
audioconv = gst_element_factory_make ("audioconvert", "audioconv"); | |
g_assert (audioconv); | |
level = gst_element_factory_make ("level", NULL); | |
g_assert (level); | |
g_object_set (level, "post-messages", TRUE, "interval", 15000000, NULL); | |
audiorate = gst_element_factory_make ("audiorate", NULL); | |
g_assert (audiorate); | |
audiores = gst_element_factory_make ("audioresample", "audiores"); | |
g_assert (audiores); | |
audiodynamic = gst_element_factory_make ("audiodynamic", "audiodynamic"); | |
g_assert (audiodynamic); | |
audiosink = gst_element_factory_make (AUDIO_SINK, "audiosink"); | |
g_assert (audiosink); | |
g_object_set (audiosink, "sync", TRUE, "buffer-time", 100000, NULL); | |
/* add depayloading and playback to the pipeline and link */ | |
gst_bin_add_many (GST_BIN (pipeline), audiodepay, audiodec, audioconv, | |
level, audiores, audiorate, audiodynamic, audiosink, NULL); | |
res = gst_element_link_many (audiodepay, audiodec, audioconv, level, | |
audiores, audiorate, audiodynamic, audiosink, NULL); | |
g_assert (res == TRUE); | |
/* the rtpbin element */ | |
rtpbin = gst_element_factory_make ("rtpbin", "rtpbin"); | |
g_assert (rtpbin); | |
g_object_set (rtpbin, "latency", 16, "do-lost", TRUE, "buffer-mode", 1, | |
NULL); | |
gst_bin_add (GST_BIN (pipeline), rtpbin); | |
/* now link all to the rtpbin, start by getting an RTP sinkpad for session 0 */ | |
srcpad = gst_element_get_static_pad (rtpsrc, "src"); | |
sinkpad = gst_element_get_request_pad (rtpbin, "recv_rtp_sink_0"); | |
lres = gst_pad_link (srcpad, sinkpad); | |
g_assert (lres == GST_PAD_LINK_OK); | |
gst_object_unref (srcpad); | |
/* get an RTCP sinkpad in session 0 */ | |
srcpad = gst_element_get_static_pad (rtcpsrc, "src"); | |
sinkpad = gst_element_get_request_pad (rtpbin, "recv_rtcp_sink_0"); | |
lres = gst_pad_link (srcpad, sinkpad); | |
g_assert (lres == GST_PAD_LINK_OK); | |
gst_object_unref (srcpad); | |
gst_object_unref (sinkpad); | |
/* get an RTCP srcpad for sending RTCP back to the sender */ | |
srcpad = gst_element_get_request_pad (rtpbin, "send_rtcp_src_0"); | |
sinkpad = gst_element_get_static_pad (rtcpsink, "sink"); | |
lres = gst_pad_link (srcpad, sinkpad); | |
g_assert (lres == GST_PAD_LINK_OK); | |
gst_object_unref (sinkpad); | |
/* the RTP pad that we have to connect to the depayloader will be created | |
* dynamically so we connect to the pad-added signal, pass the depayloader as | |
* user_data so that we can link to it. */ | |
g_signal_connect (rtpbin, "pad-added", G_CALLBACK (pad_added_cb), audiodepay); | |
/* give some stats when we receive RTCP */ | |
g_signal_connect (rtpbin, "on-ssrc-active", G_CALLBACK (on_ssrc_active_cb), | |
audiodepay); | |
/* set the pipeline to playing */ | |
g_print ("starting receiver pipeline\n"); | |
printf ("sender is at %s \n", TX_HOST); | |
gst_element_set_state (pipeline, GST_STATE_PLAYING); | |
bus = gst_element_get_bus (pipeline); | |
watch_id = gst_bus_add_watch (bus, level_message_handler, NULL); | |
/* we need to run a GLib main loop to get the messages */ | |
loop = g_main_loop_new (NULL, FALSE); | |
g_main_loop_run (loop); | |
g_print ("stopping receiver pipeline\n"); | |
gst_element_set_state (pipeline, GST_STATE_NULL); | |
g_source_remove (watch_id); | |
gst_object_unref (pipeline); | |
return 0; | |
} | |
int | |
init_tx () | |
{ | |
GstElement *audiosrc, *audioconv, *audiores, *audioenc, *audiopay; | |
GstElement *rtpbin, *rtpsink, *rtcpsink, *rtcpsrc; | |
GstElement *pipeline; | |
GMainLoop *loop; | |
GstPad *srcpad, *sinkpad; | |
/* always init first */ | |
gst_init (NULL, NULL); | |
/* the pipeline to hold everything */ | |
pipeline = gst_pipeline_new (NULL); | |
g_assert (pipeline); | |
/* the audio capture and format conversion */ | |
audiosrc = gst_element_factory_make (AUDIO_SRC, "audiosrc"); | |
g_assert (audiosrc); | |
g_object_set (audiosrc, "buffer-time", 40000, NULL); | |
audioconv = gst_element_factory_make ("audioconvert", "audioconv"); | |
g_assert (audioconv); | |
audiores = gst_element_factory_make ("audioresample", "audiores"); | |
g_assert (audiores); | |
/* the encoding and payloading */ | |
audioenc = gst_element_factory_make (AUDIO_ENC, "audioenc"); | |
g_assert (audioenc); | |
g_object_set (audioenc, "audio", TRUE, "complexity", 2, "bitrate", | |
128000, "frame-size", 10, "max-payload-size", 2048, NULL); | |
audiopay = gst_element_factory_make (AUDIO_PAY, "audiopay"); | |
g_assert (audiopay); | |
/* add capture and payloading to the pipeline and link */ | |
gst_bin_add_many (GST_BIN (pipeline), audiosrc, audioconv, audiores, | |
audioenc, audiopay, NULL); | |
if (!gst_element_link_many (audiosrc, audioconv, audiores, audioenc, | |
audiopay, NULL)) { | |
g_error ("Failed to link audiosrc, audioconv, audioresample, " | |
"audio encoder and audio payloader"); | |
} | |
/* the rtpbin element */ | |
rtpbin = gst_element_factory_make ("rtpbin", "rtpbin"); | |
g_assert (rtpbin); | |
gst_bin_add (GST_BIN (pipeline), rtpbin); | |
/* the udp sinks and source we will use for RTP and RTCP */ | |
rtpsink = gst_element_factory_make ("udpsink", "rtpsink"); | |
g_assert (rtpsink); | |
g_object_set (rtpsink, "port", 5002, "host", RX_HOST, NULL); | |
rtcpsink = gst_element_factory_make ("udpsink", "rtcpsink"); | |
g_assert (rtcpsink); | |
g_object_set (rtcpsink, "port", 5003, "host", RX_HOST, NULL); | |
/* no need for synchronisation or preroll on the RTCP sink */ | |
g_object_set (rtcpsink, "async", FALSE, "sync", FALSE, NULL); | |
rtcpsrc = gst_element_factory_make ("udpsrc", "rtcpsrc"); | |
g_assert (rtcpsrc); | |
g_object_set (rtcpsrc, "port", 5007, NULL); | |
gst_bin_add_many (GST_BIN (pipeline), rtpsink, rtcpsink, rtcpsrc, NULL); | |
/* now link all to the rtpbin, start by getting an RTP sinkpad for session 0 */ | |
sinkpad = gst_element_get_request_pad (rtpbin, "send_rtp_sink_0"); | |
srcpad = gst_element_get_static_pad (audiopay, "src"); | |
if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK) | |
g_error ("Failed to link audio payloader to rtpbin"); | |
gst_object_unref (srcpad); | |
/* get the RTP srcpad that was created when we requested the sinkpad above and | |
* link it to the rtpsink sinkpad*/ | |
srcpad = gst_element_get_static_pad (rtpbin, "send_rtp_src_0"); | |
sinkpad = gst_element_get_static_pad (rtpsink, "sink"); | |
if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK) | |
g_error ("Failed to link rtpbin to rtpsink"); | |
gst_object_unref (srcpad); | |
gst_object_unref (sinkpad); | |
/* get an RTCP srcpad for sending RTCP to the receiver */ | |
srcpad = gst_element_get_request_pad (rtpbin, "send_rtcp_src_0"); | |
sinkpad = gst_element_get_static_pad (rtcpsink, "sink"); | |
if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK) | |
g_error ("Failed to link rtpbin to rtcpsink"); | |
gst_object_unref (sinkpad); | |
/* we also want to receive RTCP, request an RTCP sinkpad for session 0 and | |
* link it to the srcpad of the udpsrc for RTCP */ | |
srcpad = gst_element_get_static_pad (rtcpsrc, "src"); | |
sinkpad = gst_element_get_request_pad (rtpbin, "recv_rtcp_sink_0"); | |
if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK) | |
g_error ("Failed to link rtcpsrc to rtpbin"); | |
gst_object_unref (srcpad); | |
/* set the pipeline to playing */ | |
g_print ("starting sender pipeline\n"); | |
printf ("reciever is at %s \n", RX_HOST); | |
gst_element_set_state (pipeline, GST_STATE_PLAYING); | |
/* print stats every second */ | |
g_timeout_add_seconds (1, (GSourceFunc) print_stats, rtpbin); | |
/* we need to run a GLib main loop to get the messages */ | |
loop = g_main_loop_new (NULL, FALSE); | |
g_main_loop_run (loop); | |
g_print ("stopping sender pipeline\n"); | |
gst_element_set_state (pipeline, GST_STATE_NULL); | |
return 0; | |
} | |
int | |
main (int argc, char *argv[]) | |
{ | |
if ( strcmp(argv[1], "tx" ) == 0 ) | |
{ | |
init_tx(argc, argv); | |
} | |
if ( strcmp(argv[1], "rx" ) == 0 ) | |
{ | |
init_rx(argc, argv); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment