Gstreamer test udpsink udpsrc to play MP3 files

Source: Internet
Author: User
Tags gstreamer

Send: Send

GST-launch filesrc location =/root/Media/test.pdf! Mad! Audioconvert! Audio/X-raw-int, channels = 1, depth = 16, width = 16, rate = 44100! Rtpl16pay! Udpsink host = 127.0.0.1 Port = 5000

Receive: Recv

Gst-launch-0.10 udpsrc Port = 5000! "Application/X-RTP, media = (string) audio, clock-rate = (INT) 44100, width = 16, Height = 16 ,\
Encoding-name = (string) L16, encoding-Params = (string) 1, channels = (INT) 1, channel-positions = (INT) 1, payload = (INT) 96 "! Gstrtpjitterbuffer do-lost = true! Rtpl16depay! Audioconvert! Alsasink sync = false

You can add one music file by yourself. Please try it !!!

C language programming files will be added slowly in the future. Please give me some advice !!! For details, refer !!!

C language programming has finally been implemented, and I am not very familiar with the principle. If there is anything unreasonable, you are welcome to correct and make progress together:

Sending part:

Server-sender.c

#include <string.h>#include <math.h>#include <gst/gst.h>#define DEST_HOST "127.0.0.1"#define AUDIO_CAPS "audio/x-raw-int,channels=1,depth=16,width=16, rate=44100"intmain (int argc,char *argv[]){GstElement *source,*maddecoder,*audioconv;GstElement *rtpbin,*rtpsink,*rtppay;GstElement *pipeline;GMainLoop *loop;GstCaps *caps;GstPad *srcpad,*sinkpad;gst_init(&argc,&argv);pipeline = gst_pipeline_new(NULL);g_assert(pipeline);source = gst_element_factory_make("filesrc","source");g_assert (pipeline);maddecoder=gst_element_factory_make("mad","maddecoder");g_assert (maddecoder);audioconv=gst_element_factory_make("audioconvert","audioconv");g_assert (audioconv);/*caps=gst_caps_new_simple("audio/x-raw-int","channels",G_TYPE_INT,1,"depth",G_TYPE_INT,16,"width",G_TYPE_INT,15,"rate",GST_TYPE_LIST,44100,NULL);*/rtppay=gst_element_factory_make("rtpL16pay","rtppay");g_assert (rtppay);g_object_set(G_OBJECT(source),"location","/root/Media/test.mp3",NULL);gst_bin_add_many (GST_BIN (pipeline),source,maddecoder,audioconv,rtppay,NULL);caps=gst_caps_from_string(AUDIO_CAPS);if(!gst_element_link_many(source,maddecoder,audioconv,NULL)){g_error("Failed to link ");}if(!gst_element_link_filtered(audioconv,rtppay,caps))        {                 g_error("Failed to link caps");        }        gst_caps_unref(caps);rtpbin=gst_element_factory_make("gstrtpbin","rtpbin");g_assert(rtpbin);gst_bin_add(GST_BIN(pipeline),rtpbin);rtpsink=gst_element_factory_make ("udpsink","rtpsink");g_assert(rtpsink);g_object_set(rtpsink,"port",5000,"host","127.0.0.1",NULL);gst_bin_add_many(GST_BIN(pipeline),rtpsink,NULL);sinkpad = gst_element_get_request_pad (rtpbin, "send_rtp_sink_0");  srcpad = gst_element_get_static_pad (rtppay, "src");  if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK)    g_error ("Failed to link audio payloader to rtpbin");  gst_object_unref (srcpad);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);g_print("starting sender pipeline\n");gst_element_set_state(pipeline,GST_STATE_PLAYING);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;}

Receiving part:

Client-recv.c

#include <string.h>#include <math.h>#include <gst/gst.h>#define AUDIO_CAPS "application/x-rtp,media=(string)audio, clock-rate=(int)44100, width=16, height=16,encoding-name=(string)L16, encoding-params=(string)1, channels=(int)1, channel-positions=(int)1, payload=(int)96"//"application/x-rtp,media=(string)audio,clock-rate=(int)8000,encoding-name=(string)PCMA"static voidprint_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 voidon_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);  print_source_stats (isrc);  /* get the remote source that sent us RTCP */  g_signal_emit_by_name (session, "get-source-by-ssrc", ssrc, &osrc);  print_source_stats (osrc);}static voidpad_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);}int main (int argc,char *argv[]){GstElement *rtpbin,*rtpsrc,*buffer,*rtppay,*audioconver, *audiosink;GstElement *pipeline;GMainLoop *loop;GstCaps *caps;GstPadLinkReturn lres;GstPad *srcpad,*sinkpad;gst_init(&argc,&argv);pipeline=gst_pipeline_new(NULL);g_assert (pipeline);rtpsrc=gst_element_factory_make("udpsrc","rtpsrc");g_assert (rtpsrc);g_object_set (rtpsrc,"port",5000,NULL);caps=gst_caps_from_string(AUDIO_CAPS);g_object_set(rtpsrc,"caps",caps,NULL);gst_caps_unref(caps);gst_bin_add_many(GST_BIN (pipeline),rtpsrc,NULL);rtppay=gst_element_factory_make("rtpL16depay","rtppay");g_assert (rtppay);audioconver=gst_element_factory_make("audioconvert","audioconver");g_assert (audioconver);audiosink=gst_element_factory_make("alsasink","audiosink");g_assert (audiosink);gst_bin_add_many (GST_BIN(pipeline),rtppay,audioconver,audiosink,NULL);gboolean res=gst_element_link_many(rtppay,audioconver,audiosink,NULL);g_assert(res==TRUE);g_object_set (audiosink, "sync", FALSE, NULL);rtpbin=gst_element_factory_make("gstrtpbin","rtpbin");g_assert(rtpbin);gst_bin_add(GST_BIN(pipeline),rtpbin);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);g_signal_connect (rtpbin, "pad-added", G_CALLBACK (pad_added_cb), rtppay);  /* give some stats when we receive RTCP */  //g_signal_connect (rtpbin, "on-ssrc-active", G_CALLBACK (on_ssrc_active_cb), //     rtppay);g_print ("starting receiver pipeline\n");        gst_element_set_state (pipeline, GST_STATE_PLAYING);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);  gst_object_unref (pipeline);  return 0;}

I hope you can support it. I suggest you continue to transfer files and videos, and transfer the camera data. Come on !!!!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.