473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to capture sequence no. and time stamp of RTP/UDP packets

1
I am multicasting video stream using RTP (rtph264pay) and UDP (udpsink) from a source node. At the client end the video stream is decoded and displayed as they stream. I have coded this using gstreamer in c/c++. I now want to

1. At source, capture the sequence no., packet size (in bytes) and time stamp of each packet of the video stream being transmitted over the network.

2. At receiver, capture the sequence no. and time stamp of the received packets so as to measure delay and packet loss.

It would be great if someone could help me suggest a way to implement this in C/C++. I do not want to use gst debug since I have to do some processing with the video packets. Here is the code for Server and client.

Expand|Select|Wrap|Line Numbers
  1. SERVER 
  2.  
  3. #include<gst/gst.h>
  4.  
  5. static gboolean
  6. bus_call (GstBus *bus,
  7. GstMessage *msg,
  8. gpointer data)
  9. {
  10. GMainLoop *loop = (GMainLoop *) data;
  11. switch (GST_MESSAGE_TYPE (msg)) {
  12. case GST_MESSAGE_EOS:
  13. g_print ("End of video stream\n");
  14. g_main_loop_quit (loop);
  15. break;
  16. case GST_MESSAGE_ERROR: {
  17. gchar *debug;
  18. GError *error;
  19. gst_message_parse_error (msg, &error, &debug);
  20. g_free (debug);
  21. g_printerr ("Error: %s\n", error->message);
  22. g_error_free (error);
  23. g_main_loop_quit (loop);
  24. break;
  25. }
  26. default:
  27. break;
  28. }
  29. return TRUE;
  30. }
  31.  
  32. static void
  33. on_pad_added (GstElement *element,
  34. GstPad *pad,
  35. gpointer data)
  36. {
  37. GstPad *sinkpad;
  38. GstElement *decoder = (GstElement *) data;
  39. g_print ("Dynamic pad created, linking demuxer/decoder\n");
  40. sinkpad = gst_element_get_static_pad (decoder, "sink");
  41. gst_pad_link (pad, sinkpad);
  42. gst_object_unref (sinkpad);
  43. }
  44.  
  45.  
  46.  
  47. typedef struct Custom_Data{
  48.  
  49. GMainLoop *loop;
  50. GstElement *pipeline, *source, *demuxer, *decoder, *aratio, *encoder, *rtp, *usink, *que, *videoconvert, *videoscale_capsfilter, *videoscale;
  51. GstBus *bus;
  52. guint bus_watch_id;
  53. guint bitrate;
  54. GstCaps *c, *videoscalecaps;
  55.  
  56. }Custom_Data;
  57.  
  58.  
  59.  
  60. int
  61. main (int argc,
  62. char *argv[])
  63. {
  64.  
  65. Custom_Data Data;
  66.  
  67.  
  68. /* Initialisation */
  69. gst_init (&argc, &argv);
  70.  
  71.  
  72. Data.loop = g_main_loop_new (NULL, FALSE);
  73. /* Check input arguments */
  74. if (argc != 2) {
  75. g_printerr ("Usage: %s <cif filename>\n", argv[0]);
  76. return -1;
  77. }
  78.  
  79. Data.videoscalecaps =  gst_caps_from_string("video/x-raw, width=1024, height=768");
  80. Data.pipeline = gst_pipeline_new ("video-send");
  81. Data.source = gst_element_factory_make ("filesrc", "file-source");
  82. g_assert (Data.source);
  83. Data.demuxer = gst_element_factory_make ("qtdemux", "demuxer");
  84. g_assert (Data.demuxer);
  85. Data.que = gst_element_factory_make ("queue", "que");
  86. g_assert (Data.que);
  87. Data.decoder = gst_element_factory_make ("avdec_h264", "decoder");
  88. g_assert (Data.decoder);
  89. Data.videoscale = gst_element_factory_make ("videoscale", "scale");
  90. g_assert (Data.videoscale);
  91. Data.videoscale_capsfilter = gst_element_factory_make ("capsfilter", "videoscale_capsfilter");
  92. g_assert (Data.videoscale_capsfilter);
  93. Data.aratio=gst_element_factory_make ("aspectratiocrop", "aratio");
  94. g_assert (Data.aratio);
  95. Data.videoconvert = gst_element_factory_make ("videoconvert", "video-convert");
  96. g_assert (Data.videoconvert);
  97. Data.encoder = gst_element_factory_make ("x264enc", "encoder");
  98. g_assert (Data.encoder);
  99. Data.rtp = gst_element_factory_make ("rtph264pay", "rtp");
  100. g_assert (Data.rtp);
  101. Data.usink = gst_element_factory_make ("udpsink", "udp_sink");
  102. g_assert (Data.usink);
  103.  
  104. g_object_set (G_OBJECT (Data.source), "location", argv[1], NULL);
  105. g_object_set (G_OBJECT (Data.source), "do-timestamp", true, NULL);
  106. g_object_set (G_OBJECT (Data.aratio), "aspect-ratio", 4, 3, NULL);
  107. g_object_set (G_OBJECT (Data.encoder), "b-adapt", true, NULL);
  108. g_object_set (G_OBJECT (Data.usink), "host", 224.0.0.0, NULL);
  109. g_object_set (G_OBJECT (Data.usink), "port", 5007, NULL);
  110. g_object_set (G_OBJECT (Data.usink), "auto-multicast", TRUE, NULL);
  111. g_object_set( G_OBJECT ( Data.videoscale_capsfilter ),  "caps",  Data.videoscalecaps, NULL );
  112.  
  113. Data.bus = gst_pipeline_get_bus (GST_PIPELINE (Data.pipeline));
  114. Data.bus_watch_id = gst_bus_add_watch (Data.bus, bus_call, Data.loop);
  115. gst_object_unref (Data.bus);
  116.  
  117. gst_bin_add(GST_BIN (Data.pipeline), Data.source);
  118. gst_bin_add(GST_BIN (Data.pipeline), Data.demuxer);
  119. gst_bin_add(GST_BIN (Data.pipeline), Data.decoder);
  120. gst_bin_add(GST_BIN (Data.pipeline), Data.videoscale);
  121. gst_bin_add(GST_BIN (Data.pipeline), Data.videoscale_capsfilter);
  122. gst_bin_add(GST_BIN (Data.pipeline), Data.aratio);
  123. gst_bin_add(GST_BIN (Data.pipeline), Data.videoconvert);
  124. gst_bin_add(GST_BIN (Data.pipeline), Data.encoder);
  125. gst_bin_add(GST_BIN (Data.pipeline), Data.rtp);
  126. gst_bin_add(GST_BIN (Data.pipeline), Data.usink);
  127.  
  128. if(!gst_element_link (Data.source, Data.demuxer))
  129. {
  130. g_printerr ("Here is  the problem.\n");
  131. }
  132.  
  133. if(!gst_element_link_many (Data.decoder, Data.videoscale, Data.videoscale_capsfilter, Data.aratio, Data.videoconvert, Data.encoder, Data.rtp, Data.usink, NULL))
  134. {
  135. g_printerr ("Here is  the problem too.\n");
  136. }
  137.  
  138. g_signal_connect (Data.demuxer, "pad-added", G_CALLBACK (on_pad_added), Data.decoder);
  139.  
  140. g_print ("Now playing: %s\n", argv[1]);
  141.  
  142. gst_element_set_state (Data.pipeline, GST_STATE_PLAYING);
  143. g_main_loop_run (Data.loop);
  144. gst_element_set_state (Data.pipeline, GST_STATE_NULL);
  145. gst_object_unref (GST_OBJECT (Data.pipeline));
  146. g_source_remove (Data.bus_watch_id);
  147. g_main_loop_unref (Data.loop);
  148.  
  149. return 0;
  150.  
  151.  
  152.  
  153. CLIENT
  154.  
  155. #include<gst/gst.h>
  156.  
  157. static gboolean
  158. bus_call (GstBus *bus,
  159. GstMessage *msg,
  160. gpointer data)
  161. {
  162. GMainLoop *loop = (GMainLoop *) data;
  163. switch (GST_MESSAGE_TYPE (msg)) {
  164. case GST_MESSAGE_EOS:
  165. g_print ("End of stream\n");
  166. g_main_loop_quit (loop);
  167. break;
  168. case GST_MESSAGE_ERROR: {
  169. gchar *debug;
  170. GError *error;
  171. gst_message_parse_error (msg, &error, &debug);
  172. g_free (debug);
  173. g_printerr ("Error: %s\n", error->message);
  174. g_error_free (error);
  175. g_main_loop_quit (loop);
  176. break;
  177. }
  178. default:
  179. break;
  180. }
  181. return TRUE;
  182. }
  183.  
  184.  
  185. static void
  186. on_pad_added (GstElement *element,
  187. GstPad *pad,
  188. gpointer data)
  189. {
  190. GstPad *sinkpad;
  191. GstElement *decoder = (GstElement *) data;
  192. g_print ("Dynamic pad created, linking demuxer/decoder\n");
  193. sinkpad = gst_element_get_static_pad (decoder, "sink");
  194. gst_pad_link (pad, sinkpad);
  195. gst_object_unref (sinkpad);
  196. }
  197.  
  198. typedef struct _CustomData {
  199. GstElement *pipeline, *source, *rtp, *decoder, *sink, *videomixer, *videoconvert, *videoscale, *que, *filter; 
  200. GstState state;                 
  201. } CustomData;
  202.  
  203.  
  204.  
  205.  
  206. int main(int argc, char *argv[]) {
  207.  
  208.  
  209.   CustomData data;
  210.   GMainLoop *loop;
  211.   GstPad *pad;
  212.  
  213.  
  214.  
  215. GstCaps* caps, *filtercaps;
  216. GstBus *bus;
  217. GstStateChangeReturn ret;
  218.  
  219.  
  220.   gst_init (&argc, &argv);
  221.  caps = gst_caps_from_string( "application/x-rtp, media = (string) video, clock-rate = (int) 90000, encoding-name =(string) H264, Payload =(int)96");
  222.  
  223. loop = g_main_loop_new (NULL, FALSE);
  224.  
  225. if (argc != 1) {
  226. g_printerr ("Usage: %s <udpsrc>\n", argv[0]);
  227. return -1;
  228. }  
  229.  
  230. data.pipeline = gst_pipeline_new ("video-receive");
  231. data.source = gst_element_factory_make ("udpsrc", "udp-source");
  232. data.videoscale = gst_element_factory_make ("videoscale", "video-scale");
  233. data.videoconvert = gst_element_factory_make ("videoconvert", "video-convert");
  234. data.sink = gst_element_factory_make ("xvimagesink", "video-output");
  235. data.rtp = gst_element_factory_make ("rtph264depay", "rtp");
  236. data.decoder = gst_element_factory_make ("avdec_h264", "decoder");
  237. data.filter = gst_element_factory_make ("capsfilter", "filtercaps");
  238. data.que = gst_element_factory_make ("queue", "que");
  239.  
  240.  
  241. filtercaps = gst_caps_new_simple ("video/x-raw", "format", G_TYPE_STRING, "I420", "width", G_TYPE_INT, 200, "height", G_TYPE_INT, 200, NULL);
  242.  
  243. if (!data.pipeline|| !data.source || !data.rtp || !data.decoder || !data.sink)
  244. {
  245. g_printerr ("One element could not be created.\n");
  246. return -1;
  247. }
  248.  
  249.  
  250.  
  251. g_object_set (G_OBJECT (data.filter), "caps", filtercaps, NULL);
  252.  
  253.  
  254.  
  255.   bus = gst_element_get_bus (data.pipeline);
  256.   gst_bus_add_signal_watch (bus);
  257.   gst_object_unref (bus);
  258.  
  259.  
  260. g_object_set (G_OBJECT (data.source), "multicast group", 224.0.0.0, NULL);
  261. g_object_set (G_OBJECT (data.source), "port", 5007, NULL);
  262. g_object_set(G_OBJECT (data.source), "caps",  caps, NULL);
  263. g_object_set (G_OBJECT (data.source), "do-timestamp", true, NULL);
  264. g_object_set( G_OBJECT (data.sink),  "sync", FALSE, NULL);
  265.  
  266.  
  267. gst_bin_add_many (GST_BIN (data.pipeline), data.source, data.rtp, data.decoder, data.que, data.sink, NULL);
  268.  
  269. gst_element_link(data.source, data.decoder);
  270. gst_element_link_many(data.source, data.rtp, data.decoder, data.que, data.sink, NULL);
  271. g_signal_connect(data.decoder, "pad-added", G_CALLBACK (on_pad_added), data.sink);
  272. g_print ("Now playing...\n");//: %s\n", argv[1]);
  273. gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
  274. g_print ("Running...\n");
  275. g_main_loop_run (loop);
  276.   g_print ("Deleting pipeline\n");
  277. gst_object_unref (GST_OBJECT (data.pipeline));
  278.   g_main_loop_unref (loop);
  279.  
  280.   return 0;
  281. }
  282.  
Apr 30 '15 #1
0 1495

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Dave | last post by:
Hey folks, I'm moving from ASP to PHP and I must say that on using this for only a day it is MUCH simpler to get things going. Already have made a VERY basic blog site for myself in a very...
7
by: Don | last post by:
Hi all, With regards to the following, how do I append the datetimestamp to the filenames in the form? The files are processed using the PHP script that follows below. Thanks in advance,...
6
by: Mike Charney | last post by:
Is there a way to check a files date and time stamp from VBA in access. I have a need check a date stamp on a file that I am importing. Thanks in advance, Mike m charney at dunlap hospital...
3
by: phried1 | last post by:
I have created a form and inserted the following tables: Date Entered Time Entered Date Modified Time Modified Essentially how and where can I have these dates and times recorded so when the...
1
by: Susan Bricker | last post by:
Greetings. I have a report (actually all of my reports in an MDB) that I want to date/time stamp at the bottom. Previously, I had used the builtin function of Now(). I thought that would give...
4
by: SilentThunderer | last post by:
Hey folks, Let me start out by letting you know what I'm working with. I'm building an application in VB 2005 that is basically a userform that employees can use to "Clock in". The form...
2
by: Shane | last post by:
I'm writing a program for renaming my picture files, and I want to use the picture time stamp as a prefix to the file names. I can get the time stamp, but it is extraordinarily slow! I have about...
1
by: 6afraidbecause789 | last post by:
Hi - I am using a Date/Time Picker popup form for users to choose a a date and time for use on a separate entry form. The date/time on the entry form is actually entered automatically as a...
0
by: Edwin.Madari | last post by:
os.paht.gmtime(path) returns the last modification of path. check out http://docs.python.org/lib/module-os.path.html regards Edwin -----Original Message----- From:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.