mixer.c 5.4 KB
Newer Older
Wim Taymans's avatar
Wim Taymans committed
1 2
#include <stdlib.h>
#include <gst/gst.h>
3 4 5 6 7 8 9 10 11
#include "mixer.h"

#define DEBUG

/* function prototypes */

input_pipe_t*	create_input_pipe (int id, char* location);
void			destroy_input_pipe (input_pipe_t *pipe);

Wim Taymans's avatar
Wim Taymans committed
12 13 14 15

gboolean playing;

/* example based on helloworld by thomas@apestaart.org
16 17
   demonstrates the adder plugin and the volume envelope plugin 
   work in progress but do try it out */
Wim Taymans's avatar
Wim Taymans committed
18 19 20 21 22 23 24 25 26 27 28

/* eos will be called when the src element has an end of stream */
void eos(GstElement *element) 
{
  g_print("have eos, quitting\n");

  playing = FALSE;
}

int main(int argc,char *argv[]) 
{
29 30 31 32
  input_pipe_t *channel_in1;
  input_pipe_t *channel_in2;
  
  GstElement *main_bin;
Wim Taymans's avatar
Wim Taymans committed
33 34 35 36 37 38 39 40 41 42 43 44
  GstElement *adder;
  GstElement *audiosink;

  GstPad *pad; /* to request pads for the adder */

  gst_init(&argc,&argv);

  if (argc != 3) {
    g_print("usage: %s <filename1> <filename2>\n", argv[0]);
    exit(-1);
  }

45
  /* create input channels */
Wim Taymans's avatar
Wim Taymans committed
46

47 48
  channel_in1 = create_input_pipe (1, argv[1]);
  channel_in2 = create_input_pipe (2, argv[2]);
Wim Taymans's avatar
Wim Taymans committed
49 50 51 52 53 54 55 56 57 58 59


  /* create adder */
  adder = gst_elementfactory_make("adder", "adderel");

  /* create an audio sink */
  audiosink = gst_elementfactory_make("esdsink", "play_audio");

  /* now create main bin */
  main_bin = gst_bin_new("bin");

60 61
  gst_bin_add(GST_BIN(main_bin), channel_in1->pipe);
  gst_bin_add(GST_BIN(main_bin), channel_in2->pipe);
Wim Taymans's avatar
Wim Taymans committed
62 63 64 65 66 67 68
  gst_bin_add(GST_BIN(main_bin), adder);
  gst_bin_add(GST_BIN(main_bin), audiosink);

  /* request pads and connect to adder */

  pad = gst_element_request_pad_by_name (adder, "sink%d");
  g_print ("new pad %s\n", gst_pad_get_name (pad));
69
  gst_pad_connect (gst_element_get_pad (channel_in1->pipe, "channel1"), pad);
Wim Taymans's avatar
Wim Taymans committed
70 71
  pad = gst_element_request_pad_by_name (adder, "sink%d");
  g_print ("new pad %s\n", gst_pad_get_name (pad));
72
  gst_pad_connect (gst_element_get_pad (channel_in2->pipe, "channel2"), pad);
Wim Taymans's avatar
Wim Taymans committed
73 74 75 76 77 78 79 80

  /* connect adder and audiosink */

  gst_pad_connect(gst_element_get_pad(adder,"src"),
                  gst_element_get_pad(audiosink,"sink"));

  /* register the volume envelope */

81 82 83 84 85
  gtk_object_set(GTK_OBJECT(channel_in1->volenv), "controlpoint", "0:0.000001", NULL);
  gtk_object_set(GTK_OBJECT(channel_in1->volenv), "controlpoint", "5:0.000001", NULL);
  gtk_object_set(GTK_OBJECT(channel_in1->volenv), "controlpoint", "10:1", NULL);
  gtk_object_set(GTK_OBJECT(channel_in1->volenv), "controlpoint", "15:1", NULL);
  gtk_object_set(GTK_OBJECT(channel_in1->volenv), "controlpoint", "20:0.000001", NULL);
86

87 88 89 90 91
  gtk_object_set(GTK_OBJECT(channel_in2->volenv), "controlpoint", "0:1", NULL);
  gtk_object_set(GTK_OBJECT(channel_in2->volenv), "controlpoint", "5:1", NULL);
  gtk_object_set(GTK_OBJECT(channel_in2->volenv), "controlpoint", "10:0.000001", NULL);
  gtk_object_set(GTK_OBJECT(channel_in2->volenv), "controlpoint", "15:0.000001", NULL);
  gtk_object_set(GTK_OBJECT(channel_in2->volenv), "controlpoint", "20:1", NULL);
Wim Taymans's avatar
Wim Taymans committed
92 93 94 95 96 97 98 99 100 101 102 103 104

  /* start playing */
  gst_element_set_state(main_bin, GST_STATE_PLAYING);

  playing = TRUE;

  while (playing) {
    gst_bin_iterate(GST_BIN(main_bin));
  }

  /* stop the bin */
  gst_element_set_state(main_bin, GST_STATE_NULL);

105 106 107
  destroy_input_pipe (channel_in1);
  destroy_input_pipe (channel_in2);
  
Wim Taymans's avatar
Wim Taymans committed
108 109 110 111 112 113 114
  gst_object_destroy(GST_OBJECT(audiosink));

  gst_object_destroy(GST_OBJECT(main_bin));

  exit(0);
}

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
input_pipe_t*
create_input_pipe (int id, char* location)
{
  /* create an input pipeline, reading from location
   * return a pointer to the pipe
   * return NULL if failed
   */

  input_pipe_t *pipe;
  char buffer[20]; 		/* hold the names */


#ifdef DEBUG
  printf ("DEBUG : c_i_p : creating pipe with id %d for file %s\n",
  		  id, location);
#endif
  
  /* allocate pipe */

  pipe = (input_pipe_t *) malloc (sizeof (input_pipe_t));
  if (pipe == NULL)
  {
    printf ("create_input_pipe : could not allocate memory for pipe !\n");
    return NULL;
  }

  /* create pipe */

  pipe->pipe = gst_bin_new ("pipeline");
     
  /* create elements */

  sprintf (buffer, "disksrc%d", id);
  pipe->disksrc = gst_elementfactory_make ("disksrc", buffer);
  gtk_object_set(GTK_OBJECT(pipe->disksrc),"location", location, NULL);
/*  gtk_signal_connect(GTK_OBJECT(disksrc1),"eos",
                     GTK_SIGNAL_FUNC(eos),NULL);
*/
  sprintf (buffer, "decoder%d", id);
  pipe->decoder = gst_elementfactory_make("mad", buffer);
  sprintf (buffer, "volume%d", id);
  pipe->volenv = gst_elementfactory_make("volenv", buffer);

  gst_bin_add(GST_BIN(pipe->pipe), pipe->disksrc);
  gst_bin_add(GST_BIN(pipe->pipe), pipe->decoder);
  gst_bin_add(GST_BIN(pipe->pipe), pipe->volenv);

  /* connect elements */

  gst_pad_connect(gst_element_get_pad(pipe->disksrc,"src"),
                  gst_element_get_pad(pipe->decoder,"sink"));
  gst_pad_connect(gst_element_get_pad(pipe->decoder,"src"),
                  gst_element_get_pad(pipe->volenv,"sink"));

  /* add a ghost pad */
  sprintf (buffer, "channel%d", id);
  gst_element_add_ghost_pad (pipe->pipe, 
                             gst_element_get_pad (pipe->volenv, "src"), buffer);

#ifdef DEBUG
  printf ("DEBUG : c_i_p : end function\n");
#endif

  return pipe;
}

void
destroy_input_pipe (input_pipe_t *pipe)
{
  /* 
   * destroy an input pipeline
   */
   
#ifdef DEBUG
  printf ("DEBUG : d_i_p : start\n");
#endif
  
  /* destroy elements */

  gst_object_destroy (GST_OBJECT (pipe->disksrc));
  gst_object_destroy (GST_OBJECT (pipe->decoder));
  gst_object_destroy (GST_OBJECT (pipe->volenv));

  gst_object_destroy (GST_OBJECT (pipe->pipe));

  free (pipe);
}