int main (int argc, char *argv[]) { GstElement *pipeline = NULL; GError* error = NULL; FILE* output_file = NULL; gst_init (&argc,&argv); if (argc != 3) { g_print ("usage: %s <gst-launch-like pipeline> <xml file_path>\n", argv[0]); exit (-1); } output_file = fopen (argv[2], "w"); if(!output_file){ g_warning("Error opening file[%s]", argv[2]); exit (-1); } pipeline = gst_parse_launch(argv[1], &error); if(error) g_warning("Warning [%s]", error->message); if(!pipeline){ g_warning("Error building pipeline [%s]", argv[1]); fclose(output_file); exit (-1); } /* write the bin to a file */ gst_xml_write_file (GST_ELEMENT (pipeline), output_file); fclose(output_file); gst_object_unref (GST_OBJECT (pipeline)); exit (0); }
int main(int argc, char * argv[]) { GMainLoop *loop; GstElement *pipeline, *sink, *mux; GstElement *vsrc[NR_PROG]; GstElement *asrc[NR_PROG]; GstElement *vparse[NR_PROG]; GstElement *vdemux[NR_PROG]; GstElement *aparse[NR_PROG]; GstPad *tl_pad, *pad; GstStructure *pm; GstBus *bus; FILE * xml_of; gchar vname[][60] = { "/Users/lyang/src/res/mpts.test/mpts110.mpv", "/Users/lyang/src/res/mpts.test/mpts120.mpv", "/Users/lyang/src/res/mpts.test/mpts130.mpv", "/Users/lyang/src/res/mpts.test/mpts140.mpv", "/Users/lyang/src/res/mpts.test/mpts150.mpv", "/Users/lyang/src/res/mpts.test/mpts160.mpv", "/Users/lyang/src/res/mpts.test/mpts170.mpv" }; gchar aname[][60] = { "/Users/lyang/src/res/mpts.test/mpts113.mpa", "/Users/lyang/src/res/mpts.test/mpts123.mpa", "/Users/lyang/src/res/mpts.test/mpts133.mpa", "/Users/lyang/src/res/mpts.test/mpts143.mpa", "/Users/lyang/src/res/mpts.test/mpts153.mpa", "/Users/lyang/src/res/mpts.test/mpts163.mpa", "/Users/lyang/src/res/mpts.test/mpts173.mpa" }; gchar dest_dir[60]; gchar dest_xml[60]; gint i; gst_init (&argc, &argv); loop = g_main_loop_new (NULL, FALSE); pipeline = gst_pipeline_new ("mpeg-ts-muxer"); mux = gst_element_factory_make ("mpegtsmux", "muxer"); sink = gst_element_factory_make ("filesink", "sink"); if (!pipeline || !mux || !sink) { g_printerr ( "Some element could not be created.\n"); return -1; } for(i = 0; i< NR_PROG; i++) { vsrc[i] = gst_element_factory_make ("filesrc", NULL); vdemux[i] = gst_element_factory_make ("mpegpsdemux", NULL); vparse[i] = gst_element_factory_make ("mpegvideoparse", NULL); asrc[i] = gst_element_factory_make ("filesrc", NULL); aparse[i] = gst_element_factory_make ("mp3parse", NULL); if (!vsrc[i] || !vparse[i] || !vdemux[i] ||!asrc[i] || !aparse[i]) { g_printerr ( "Some element could not be created. i=%d.\n", i); return -1; } } /* Setting paths */ for(i = 0; i< NR_PROG; i++) { g_object_set (G_OBJECT (vsrc[i]), "location", vname[i], NULL); g_object_set (G_OBJECT (asrc[i]), "location", aname[i], NULL); } sprintf (dest_dir, "/Users/lyang/src/res/mpts.test/mpts_%02d.ts", NR_PROG); g_object_set (G_OBJECT (sink), "location", dest_dir, NULL); /* construct the pipeline */ gst_bin_add_many (GST_BIN (pipeline), mux, sink, NULL); gst_element_link (mux, sink); for(i = 0; i< NR_PROG; i++) { gst_bin_add_many (GST_BIN (pipeline), vsrc[i], vdemux[i], vparse[i], NULL); gst_element_link (vsrc[i], vdemux[i]); g_signal_connect (vdemux[i], "pad-added", G_CALLBACK (on_pad_added), vparse[i]); gst_bin_add_many (GST_BIN (pipeline), asrc[i], aparse[i], NULL); gst_element_link (asrc[i], aparse[i]); } /* construct the program map */ pm = gst_structure_empty_new ("program_map"); /* Program 1 */ for(i = 0; i< NR_PROG; i++) { /* vparse <-> mux */ tl_pad = gst_element_get_static_pad (vparse[i], "src"); if (tl_pad == NULL) { g_printerr ("vparse[%d] src pad getting failed.\n", i); return -1; } pad = gst_element_get_compatible_pad (mux, tl_pad, NULL); gst_pad_link (tl_pad, pad); gst_structure_set (pm, gst_pad_get_name(pad), G_TYPE_INT, i, NULL); gst_object_unref (GST_OBJECT (tl_pad)); gst_object_unref (GST_OBJECT (pad)); /* aparse <-> mux */ tl_pad = gst_element_get_static_pad (aparse[i], "src"); if (tl_pad == NULL) { g_printerr ("aparse[%d] src pad getting failed.\n", i); return -1; } pad = gst_element_get_compatible_pad (mux, tl_pad, NULL); gst_pad_link (tl_pad, pad); gst_structure_set (pm, gst_pad_get_name(pad), G_TYPE_INT, i, NULL); gst_object_unref (GST_OBJECT (tl_pad)); gst_object_unref (GST_OBJECT (pad)); } /* set the program map */ g_object_set (G_OBJECT(mux), "prog-map", pm, NULL); bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline)); gst_bus_add_watch (bus, bus_call, loop); gst_object_unref (bus); /* Write the pipeline to XML */ sprintf (dest_xml, "/Users/lyang/src/res/mpts.test/mpts_%02d.xml", NR_PROG); xml_of = fopen (dest_xml, "w"); gst_xml_write_file (GST_ELEMENT (pipeline), xml_of); g_print ("Now playing: %s\n", dest_dir); gst_element_set_state (pipeline, GST_STATE_PLAYING); /* Run! */ g_print ("Running...\n"); g_main_loop_run (loop); /* Out of the main loop, clean up nicely */ g_print ("Returned, stopping playback\n"); gst_element_set_state (pipeline, GST_STATE_NULL); g_print ("Deleting pipeline\n"); gst_object_unref (GST_OBJECT (pipeline)); return 0; }
int main (int argc, char *argv[]) { int i, j; int num_channels; char buffer[20]; GList *input_channels; /* structure holding all the input channels */ input_channel_t *channel_in; GstElement *main_bin; GstElement *adder; GstElement *audiosink; GstPad *pad; /* to request pads for the adder */ gst_init (&argc, &argv); if (argc == 1) { g_print ("usage: %s <filename1> <filename2> <...>\n", argv[0]); exit (-1); } num_channels = argc - 1; /* set up output channel and main bin */ /* create adder */ adder = gst_element_factory_make ("adder", "adderel"); /* create an audio sink */ audiosink = gst_element_factory_make ("esdsink", "play_audio"); /* create main bin */ main_bin = gst_pipeline_new ("bin"); /* link adder and output to bin */ GST_INFO ("main: adding adder to bin"); gst_bin_add (GST_BIN (main_bin), adder); GST_INFO ("main: adding audiosink to bin"); gst_bin_add (GST_BIN (main_bin), audiosink); /* link adder and audiosink */ gst_pad_link (gst_element_get_pad (adder, "src"), gst_element_get_pad (audiosink, "sink")); /* start looping */ input_channels = NULL; for (i = 1; i < argc; ++i) { printf ("Opening channel %d from file %s...\n", i, argv[i]); channel_in = create_input_channel (i, argv[i]); input_channels = g_list_append (input_channels, channel_in); if (i > 1) gst_element_set_state (main_bin, GST_STATE_PAUSED); gst_bin_add (GST_BIN (main_bin), channel_in->pipe); /* request pads and link to adder */ GST_INFO ("requesting pad\n"); pad = gst_element_get_request_pad (adder, "sink%d"); printf ("\tGot new adder sink pad %s\n", gst_pad_get_name (pad)); sprintf (buffer, "channel%d", i); gst_pad_link (gst_element_get_pad (channel_in->pipe, buffer), pad); /* register a volume envelope */ printf ("\tregistering volume envelope...\n"); /* * this is the volenv : * each song gets a slot of 5 seconds, with a 5 second fadeout * at the end of that, all audio streams play simultaneously * at a level ensuring no distortion * example for three songs : * song1 : starts at full level, plays 5 seconds, faded out at 10 seconds, * sleep until 25, fade to end level at 30 * song2 : starts silent, fades in at 5 seconds, full blast at 10 seconds, * full level until 15, faded out at 20, sleep until 25, fade to end at 30 * song3 : starts muted, fades in from 15, full at 20, until 25, fade to end level */ if (i == 1) { /* first song gets special treatment for end style */ env_register_cp (channel_in->volenv, 0.0, 1.0); } else { env_register_cp (channel_in->volenv, 0.0, 0.0000001); /* start muted */ env_register_cp (channel_in->volenv, i * 10.0 - 15.0, 0.0000001); /* start fade in */ env_register_cp (channel_in->volenv, i * 10.0 - 10.0, 1.0); } env_register_cp (channel_in->volenv, i * 10.0 - 5.0, 1.0); /* end of full level */ if (i != num_channels) { env_register_cp (channel_in->volenv, i * 10.0, 0.0000001); /* fade to black */ env_register_cp (channel_in->volenv, num_channels * 10.0 - 5.0, 0.0000001); /* start fade in */ } env_register_cp (channel_in->volenv, num_channels * 10.0, 1.0 / num_channels); /* to end level */ #ifndef GST_DISABLE_LOADSAVE gst_xml_write_file (GST_ELEMENT (main_bin), fopen ("mixer.xml", "w")); #endif /* start playing */ gst_element_set_state (main_bin, GST_STATE_PLAYING); /* write out the schedule */ gst_scheduler_show (GST_ELEMENT_SCHEDULER (main_bin)); playing = TRUE; j = 0; /*printf ("main: start iterating from 0"); */ while (playing && j < 100) { /* printf ("main: iterating %d\n", j); */ gst_bin_iterate (GST_BIN (main_bin)); /*fprintf(stderr,"after iterate()\n"); */ ++j; } } printf ("main: all the channels are open\n"); while (playing) { gst_bin_iterate (GST_BIN (main_bin)); /*fprintf(stderr,"after iterate()\n"); */ } /* stop the bin */ gst_element_set_state (main_bin, GST_STATE_NULL); while (input_channels) { destroy_input_channel (input_channels->data); input_channels = g_list_next (input_channels); } g_list_free (input_channels); gst_object_unref (audiosink); gst_object_unref (main_bin); exit (0); }
input_channel_t * create_input_channel (int id, char *location) { /* create an input channel, reading from location * return a pointer to the channel * return NULL if failed */ input_channel_t *channel; char buffer[20]; /* hold the names */ /* GstAutoplug *autoplug; GstCaps *srccaps; */ GstElement *new_element; GstElement *decoder; GST_DEBUG ("c_i_p : creating channel with id %d for file %s", id, location); /* allocate channel */ channel = (input_channel_t *) malloc (sizeof (input_channel_t)); if (channel == NULL) { printf ("create_input_channel : could not allocate memory for channel !\n"); return NULL; } /* create channel */ GST_DEBUG ("c_i_p : creating pipeline"); sprintf (buffer, "pipeline%d", id); channel->pipe = gst_bin_new (buffer); g_assert (channel->pipe != NULL); /* create elements */ GST_DEBUG ("c_i_p : creating filesrc"); sprintf (buffer, "filesrc%d", id); channel->filesrc = gst_element_factory_make ("filesrc", buffer); g_assert (channel->filesrc != NULL); GST_DEBUG ("c_i_p : setting location"); g_object_set (G_OBJECT (channel->filesrc), "location", location, NULL); /* add filesrc to the bin before autoplug */ gst_bin_add (GST_BIN (channel->pipe), channel->filesrc); /* link signal to eos of filesrc */ g_signal_connect (G_OBJECT (channel->filesrc), "eos", G_CALLBACK (eos), NULL); #ifdef DEBUG printf ("DEBUG : c_i_p : creating volume envelope\n"); #endif sprintf (buffer, "volenv%d", id); channel->volenv = gst_element_factory_make ("volenv", buffer); g_assert (channel->volenv != NULL); /* autoplug the pipe */ #ifdef DEBUG printf ("DEBUG : c_i_p : getting srccaps\n"); #endif #ifdef WITH_BUG srccaps = gst_play_type_find (GST_BIN (channel->pipe), channel->filesrc); #endif #ifdef WITH_BUG2 { GstElement *pipeline; pipeline = gst_pipeline_new ("autoplug_pipeline"); gst_bin_add (GST_BIN (pipeline), channel->pipe); gst_element_set_state (pipeline, GST_STATE_PLAYING); gst_element_set_state (pipeline, GST_STATE_NULL); gst_bin_remove (GST_BIN (pipeline), channel->pipe); } #endif #ifdef AUTOPLUG if (!srccaps) { g_print ("could not autoplug, unknown media type...\n"); exit (-1); } #ifdef DEBUG printf ("DEBUG : c_i_p : creating autoplug\n"); #endif autoplug = gst_autoplug_factory_make ("static"); g_assert (autoplug != NULL); #ifdef DEBUG printf ("DEBUG : c_i_p : autoplugging\n"); #endif new_element = gst_autoplug_to_caps (autoplug, srccaps, gst_caps_new ("audio/raw", NULL), NULL); if (!new_element) { g_print ("could not autoplug, no suitable codecs found...\n"); exit (-1); } #else new_element = gst_bin_new ("autoplug_bin"); /* static plug, use mad plugin and assume mp3 input */ printf ("using static plugging for input channel\n"); decoder = gst_element_factory_make ("mad", "mpg123"); if (!decoder) { fprintf (stderr, "Could not get a decoder element !\n"); exit (1); } gst_bin_add (GST_BIN (new_element), decoder); gst_element_add_ghost_pad (new_element, gst_element_get_pad (decoder, "sink"), "sink"); gst_element_add_ghost_pad (new_element, gst_element_get_pad (decoder, "src"), "src_00"); #endif #ifndef GST_DISABLE_LOADSAVE gst_xml_write_file (GST_ELEMENT (new_element), fopen ("mixer.gst", "w")); #endif gst_bin_add (GST_BIN (channel->pipe), channel->volenv); gst_bin_add (GST_BIN (channel->pipe), new_element); gst_element_link_pads (channel->filesrc, "src", new_element, "sink"); gst_element_link_pads (new_element, "src_00", channel->volenv, "sink"); /* add a ghost pad */ sprintf (buffer, "channel%d", id); gst_element_add_ghost_pad (channel->pipe, gst_element_get_pad (channel->volenv, "src"), buffer); #ifdef DEBUG printf ("DEBUG : c_i_p : end function\n"); #endif return channel; }