• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <gst/gst.h>
2 #include <gst/mpegts/mpegts.h>
3 
4 /* 45s stream
5  * Send scte-35 NULL packets every 5s
6  * Use PID 123 for SCTE-35 */
7 #define PIPELINE_STR "videotestsrc is-live=True num-buffers=1350 ! video/x-raw,framerate=30/1 ! x264enc tune=zerolatency ! queue ! mpegtsmux name=mux scte-35-pid=123 scte-35-null-interval=450000 ! filesink location=test-scte.ts"
8 
9 static void
_on_bus_message(GstBus * bus,GstMessage * message,GMainLoop * mainloop)10 _on_bus_message (GstBus * bus, GstMessage * message, GMainLoop * mainloop)
11 {
12   switch (GST_MESSAGE_TYPE (message)) {
13     case GST_MESSAGE_ERROR:
14     case GST_MESSAGE_EOS:
15       g_main_loop_quit (mainloop);
16       break;
17     default:
18       break;
19   }
20 }
21 
22 static void
send_splice(GstElement * mux,gboolean out)23 send_splice (GstElement * mux, gboolean out)
24 {
25   GstMpegtsSCTESIT *sit;
26   GstMpegtsSection *section;
27 
28   g_print ("Sending Splice %s event\n", out ? "Out" : "In");
29 
30   /* Splice is at 5s for 30s */
31   if (out)
32     sit = gst_mpegts_scte_splice_out_new (1, 5 * GST_SECOND, 30 * GST_SECOND);
33   else
34     sit = gst_mpegts_scte_splice_in_new (2, 35 * GST_SECOND);
35 
36   section = gst_mpegts_section_from_scte_sit (sit, 123);
37   gst_mpegts_section_send_event (section, mux);
38   gst_mpegts_section_unref (section);
39 }
40 
41 static gboolean
send_splice_in(GstElement * mux)42 send_splice_in (GstElement * mux)
43 {
44   send_splice (mux, FALSE);
45 
46   return G_SOURCE_REMOVE;
47 }
48 
49 static gboolean
send_splice_out(GstElement * mux)50 send_splice_out (GstElement * mux)
51 {
52   send_splice (mux, TRUE);
53 
54   /* In 30s send the splice-in one */
55   g_timeout_add_seconds (30, (GSourceFunc) send_splice_in, mux);
56 
57   return G_SOURCE_REMOVE;
58 }
59 
60 int
main(int argc,char ** argv)61 main (int argc, char **argv)
62 {
63   GstElement *pipeline = NULL;
64   GError *error = NULL;
65   GstBus *bus;
66   GMainLoop *mainloop;
67   GstElement *mux;
68 
69   gst_init (&argc, &argv);
70   gst_mpegts_initialize ();
71 
72   pipeline = gst_parse_launch (PIPELINE_STR, &error);
73   if (error) {
74     g_print ("pipeline could not be constructed: %s\n", error->message);
75     g_clear_error (&error);
76     return 1;
77   }
78 
79   mainloop = g_main_loop_new (NULL, FALSE);
80 
81   mux = gst_bin_get_by_name (GST_BIN (pipeline), "mux");
82   /* Send splice-out 1s in */
83   g_timeout_add_seconds (1, (GSourceFunc) send_splice_out, mux);
84   gst_object_unref (mux);
85 
86   /* Put a bus handler */
87   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
88   gst_bus_add_signal_watch (bus);
89   g_signal_connect (bus, "message", (GCallback) _on_bus_message, mainloop);
90 
91   /* Start pipeline */
92   gst_element_set_state (pipeline, GST_STATE_PLAYING);
93   g_main_loop_run (mainloop);
94 
95   gst_element_set_state (pipeline, GST_STATE_NULL);
96 
97   gst_object_unref (pipeline);
98   gst_object_unref (bus);
99 
100   return 0;
101 }
102