another batch of connect->link fixes please let me know about issues and please refra...
[platform/upstream/gst-plugins-good.git] / ext / shout2 / gstshout2.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "gstshout2.h"
21 #include <stdlib.h>
22
23 /* elementfactory information */
24 static GstElementDetails shout2send_details = {
25   "An Icecast plugin",
26   "Sink/Network/Icecast2",
27   "LGPL",
28   "Sends data to an icecast server",
29   VERSION,
30   "Wim Taymans <wim.taymans@chello.be>, Pedro Corte-Real <typo@netcabo.pt>",
31   "(C) 2000",
32 };
33
34 unsigned int audio_format = 100;
35
36 /* Shout2send signals and args */
37 enum {
38   /* FILL ME */
39   LAST_SIGNAL
40 };
41
42 enum {
43   ARG_0,
44   ARG_IP,          /* the ip of the server */
45   ARG_PORT,        /* the encoder port number on the server */
46   ARG_PASSWORD,    /* the encoder password on the server */
47   ARG_PUBLIC,      /* is this stream public? */
48   ARG_NAME,        /* Name of the stream */
49   ARG_DESCRIPTION, /* Description of the stream */
50   ARG_GENRE,       /* Genre of the stream */
51
52   ARG_PROTOCOL,    /* Protocol to connect with */
53
54   ARG_MOUNT,       /* mountpoint of stream (icecast only) */
55   ARG_URL,         /* Url of stream (I'm guessing) */
56 };
57
58 static GstPadTemplate*
59 sink_template_factory (void)
60 {
61   static GstPadTemplate *template = NULL;
62   
63   if (!template) {
64     template = gst_pad_template_new (
65       "sink",
66       GST_PAD_SINK,
67       GST_PAD_ALWAYS,
68       gst_caps_new (
69         "shout2send_sink",
70         "application/x-ogg",
71         NULL),
72       gst_caps_new (
73         "shout2send_sink",
74         "audio/x-mp3",
75         NULL),
76       NULL);
77   }
78
79   return template;
80 }
81
82 static void                     gst_shout2send_class_init       (GstShout2sendClass *klass);
83 static void                     gst_shout2send_init             (GstShout2send *shout2send);
84
85 static void                     gst_shout2send_chain            (GstPad *pad, GstBuffer *buf);
86 static GstPadConnectReturn      gst_shout2send_connect         (GstPad *pad, GstCaps *caps);
87
88 static void                     gst_shout2send_set_property             (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
89 static void                     gst_shout2send_get_property             (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
90
91 static GstElementStateReturn    gst_shout2send_change_state     (GstElement *element);
92
93 static GstElementClass *parent_class = NULL;
94 /*static guint gst_shout2send_signals[LAST_SIGNAL] = { 0 }; */
95
96 #define GST_TYPE_SHOUT_PROTOCOL (gst_shout2send_protocol_get_type())
97 static GType
98 gst_shout2send_protocol_get_type (void) 
99 {
100   static GType shout2send_protocol_type = 0;
101   static GEnumValue shout2send_protocol[] = {
102     { SHOUT2SEND_PROTOCOL_ICE,        "1", "Ice Protocol"},
103     { SHOUT2SEND_PROTOCOL_XAUDIOCAST, "2", "Xaudiocast Protocol (icecast 1.3.x)"},
104     { SHOUT2SEND_PROTOCOL_ICY,        "3", "Icy Protocol (ShoutCast)"},
105     { SHOUT2SEND_PROTOCOL_HTTP,       "4", "Http Protocol (icecast 2.x)"},
106     {0, NULL, NULL},
107   };
108   if (!shout2send_protocol_type) {
109     shout2send_protocol_type = g_enum_register_static ("GstShout2SendProtocol", shout2send_protocol);
110   }
111   return shout2send_protocol_type;
112 }
113
114 GType
115 gst_shout2send_get_type(void)
116 {
117   static GType shout2send_type = 0;
118
119   if (!shout2send_type) {
120     static const GTypeInfo shout2send_info = {
121       sizeof(GstShout2sendClass),      NULL,
122       NULL,
123       (GClassInitFunc)gst_shout2send_class_init,
124       NULL,
125       NULL,
126       sizeof(GstShout2send),
127       0,
128       (GInstanceInitFunc)gst_shout2send_init,
129     };
130     shout2send_type = g_type_register_static(GST_TYPE_ELEMENT, "GstShout2send", &shout2send_info, 0);
131   }
132   return shout2send_type;
133 }
134
135 static void
136 gst_shout2send_class_init (GstShout2sendClass *klass)
137 {
138   GObjectClass *gobject_class;
139   GstElementClass *gstelement_class;
140
141   gobject_class = (GObjectClass*)klass;
142   gstelement_class = (GstElementClass*)klass;
143
144   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
145
146   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_IP,
147     g_param_spec_string("ip","ip","ip",
148                         NULL, G_PARAM_READWRITE)); /* CHECKME */
149   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_PORT,
150     g_param_spec_int("port","port","port",
151                      1,G_MAXUSHORT,8000,G_PARAM_READWRITE)); /* CHECKME */
152
153   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_PASSWORD,
154     g_param_spec_string("password","password","password",
155                         NULL, G_PARAM_READWRITE)); /* CHECKME */
156
157   /* metadata */
158   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_NAME,
159     g_param_spec_string("name","name","name",
160                         NULL, G_PARAM_READWRITE)); /* CHECKME */
161
162   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_DESCRIPTION,
163     g_param_spec_string("description","description","description",
164                         NULL, G_PARAM_READWRITE)); /* CHECKME */
165
166   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_GENRE,
167     g_param_spec_string("genre","genre","genre",
168                         NULL, G_PARAM_READWRITE)); /* CHECKME */
169
170   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PROTOCOL,
171     g_param_spec_enum ("protocol", "protocol", "Connection Protocol to use",
172                        GST_TYPE_SHOUT_PROTOCOL, SHOUT2SEND_PROTOCOL_HTTP, G_PARAM_READWRITE)); 
173
174
175   /* icecast only */
176   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_MOUNT,
177     g_param_spec_string("mount","mount","mount",
178                         NULL, G_PARAM_READWRITE)); /* CHECKME */
179
180   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_URL,
181     g_param_spec_string("url","url","url",
182                         NULL, G_PARAM_READWRITE)); /* CHECKME */
183   
184
185   
186   gobject_class->set_property = gst_shout2send_set_property;
187   gobject_class->get_property = gst_shout2send_get_property;
188
189   gstelement_class->change_state = gst_shout2send_change_state;
190 }
191
192 static void
193 gst_shout2send_init (GstShout2send *shout2send)
194 {
195   shout2send->sinkpad = gst_pad_new_from_template (sink_template_factory (), "sink");
196   gst_element_add_pad(GST_ELEMENT(shout2send),shout2send->sinkpad);
197   gst_pad_set_chain_function(shout2send->sinkpad,gst_shout2send_chain);
198
199   gst_pad_set_link_function (shout2send->sinkpad, gst_shout2send_connect);
200
201   shout2send->ip = g_strdup ("127.0.0.1");
202   shout2send->port = 8000;
203   shout2send->password = g_strdup ("hackme");
204   shout2send->name = g_strdup ("");
205   shout2send->description = g_strdup ("");
206   shout2send->genre = g_strdup ("");
207   shout2send->mount = g_strdup ("");
208   shout2send->url = g_strdup ("");
209   shout2send->protocol = SHOUT2SEND_PROTOCOL_HTTP;
210 }
211
212 static void
213 gst_shout2send_chain (GstPad *pad, GstBuffer *buf)
214 {
215   GstShout2send *shout2send;
216   glong ret;
217
218   g_return_if_fail(pad != NULL);
219   g_return_if_fail(GST_IS_PAD(pad));
220   g_return_if_fail(buf != NULL);
221
222   shout2send = GST_SHOUT2SEND (GST_OBJECT_PARENT (pad));
223
224   g_return_if_fail (shout2send != NULL);
225   g_return_if_fail (GST_IS_SHOUT2SEND (shout2send));
226
227   ret = shout_send (shout2send->conn, GST_BUFFER_DATA (buf),
228                                              GST_BUFFER_SIZE (buf));
229   if (ret != SHOUTERR_SUCCESS) {
230     g_warning ("send error: %s...\n", shout_get_error(shout2send->conn));
231   }
232
233   shout_sync (shout2send->conn);
234
235   gst_buffer_unref (buf);
236 }
237
238 static void
239 gst_shout2send_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
240 {
241   GstShout2send *shout2send;
242
243   /* it's not null if we got it, but it might not be ours */
244   g_return_if_fail(GST_IS_SHOUT2SEND(object));
245   shout2send = GST_SHOUT2SEND(object);
246
247   switch (prop_id) {
248
249   case ARG_IP:
250     if (shout2send->ip)
251       g_free (shout2send->ip);
252     shout2send->ip = g_strdup (g_value_get_string (value));
253     break;
254     
255   case ARG_PORT:
256     shout2send->port = g_value_get_int (value);
257     break;
258
259   case ARG_PASSWORD:
260     if (shout2send->password)
261       g_free (shout2send->password);
262     shout2send->password = g_strdup (g_value_get_string (value));
263     break;
264
265   case ARG_NAME:         /* Name of the stream */
266     if (shout2send->name)
267       g_free (shout2send->name);
268     shout2send->name = g_strdup (g_value_get_string (value));
269     break;
270
271   case ARG_DESCRIPTION: /* Description of the stream */
272     if (shout2send->description)
273       g_free (shout2send->description);
274     shout2send->description = g_strdup (g_value_get_string (value));
275     break;
276     
277   case ARG_GENRE:       /* Genre of the stream */
278     if (shout2send->genre)
279       g_free (shout2send->genre);
280     shout2send->genre = g_strdup (g_value_get_string (value));
281     break;
282     
283   case ARG_PROTOCOL:       /* protocol to connect with */
284     shout2send->protocol = g_value_get_enum (value);
285     break;  
286
287   case ARG_MOUNT:       /* mountpoint of stream (icecast only) */
288     if (shout2send->mount)
289       g_free (shout2send->mount);
290     shout2send->mount = g_strdup (g_value_get_string (value));
291     break;
292       
293   case ARG_URL:       /* Url of the stream (I'm guessing) */
294     if (shout2send->url)
295       g_free (shout2send->url);
296     shout2send->url = g_strdup (g_value_get_string (value));
297     break;
298     
299   default:
300     break;
301   }
302 }
303
304 static void
305 gst_shout2send_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
306 {
307   GstShout2send *shout2send;
308
309   /* it's not null if we got it, but it might not be ours */
310   g_return_if_fail(GST_IS_SHOUT2SEND(object));
311   shout2send = GST_SHOUT2SEND(object);
312
313   switch (prop_id) {
314   
315   case ARG_IP:
316     g_value_set_string (value, shout2send->ip);
317     break;
318   case ARG_PORT:
319     g_value_set_int (value, shout2send->port);
320     break;
321   case ARG_PASSWORD:
322     g_value_set_string (value, shout2send->password);
323     break;
324
325   case ARG_NAME:         /* Name of the stream */
326     g_value_set_string (value, shout2send->name);
327     break;
328
329   case ARG_DESCRIPTION: /* Description of the stream */
330     g_value_set_string (value, shout2send->description);
331     break;
332
333   case ARG_GENRE:       /* Genre of the stream */
334     g_value_set_string (value, shout2send->genre);
335     break;
336
337   case ARG_PROTOCOL:       /* protocol to connect with */
338     g_value_set_enum (value, shout2send->protocol);
339     break; 
340
341   case ARG_MOUNT:       /* mountpoint of stream (icecast only) */
342     g_value_set_string (value, shout2send->mount);
343     break;
344
345   case ARG_URL:       /* Url of stream (I'm guessing) */
346     g_value_set_string (value, shout2send->url);
347     break;
348       
349     
350   default:
351     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
352     break;
353   }
354 }
355
356 static GstPadConnectReturn
357 gst_shout2send_connect (GstPad *pad, GstCaps *caps)
358
359 {
360   
361   if (!strcmp(gst_caps_get_mime (caps), "audio/x-mp3"))
362     {
363       audio_format = SHOUT_FORMAT_MP3;
364       return GST_PAD_LINK_OK;
365     }
366
367   if (!strcmp(gst_caps_get_mime (caps), "application/x-ogg"))
368     {
369       audio_format = SHOUT_FORMAT_VORBIS;
370       return GST_PAD_LINK_OK;
371     }
372   else {
373     return GST_PAD_LINK_REFUSED;
374   }
375         
376 }
377
378 static GstElementStateReturn
379 gst_shout2send_change_state (GstElement *element)
380 {
381   GstShout2send *shout2send;
382
383   guint major, minor, micro;
384   gshort proto = 3;
385
386   gchar *version_string;
387
388   g_return_val_if_fail (GST_IS_SHOUT2SEND (element), GST_STATE_FAILURE);
389
390   shout2send = GST_SHOUT2SEND(element);
391
392   GST_DEBUG (0,"state pending %d", GST_STATE_PENDING (element));
393
394   /* if going down into NULL state, close the file if it's open */
395   switch (GST_STATE_TRANSITION (element)) {
396    case GST_STATE_NULL_TO_READY:
397      shout2send->conn = shout_new();
398
399      switch (shout2send->protocol) {
400      case SHOUT2SEND_PROTOCOL_ICE:
401        proto = SHOUT_PROTOCOL_ICE;
402        break;
403      case SHOUT2SEND_PROTOCOL_XAUDIOCAST:
404        proto = SHOUT_PROTOCOL_XAUDIOCAST;
405        break;
406      case SHOUT2SEND_PROTOCOL_ICY:
407        proto = SHOUT_PROTOCOL_ICY;
408        break;
409      case SHOUT2SEND_PROTOCOL_HTTP:
410        proto = SHOUT_PROTOCOL_HTTP;
411        break;
412      }
413
414      if (shout_set_protocol(shout2send->conn, proto) != SHOUTERR_SUCCESS)
415        {
416          g_error ("Error setting protocol: %s\n", shout_get_error(shout2send->conn));
417        }
418
419      /* --- FIXME: shout requires an ip, and fails if it is given a host. */
420      /* may want to put convert_to_ip(shout2send->ip) here */
421
422
423      if (shout_set_host(shout2send->conn, shout2send->ip) != SHOUTERR_SUCCESS)
424        {
425          g_error ("Error setting host: %s\n", shout_get_error(shout2send->conn)); 
426        }
427      /* --- */
428      
429      if (shout_set_port(shout2send->conn, shout2send->port) != SHOUTERR_SUCCESS)
430        {
431          g_error ("Error setting port: %s\n", shout_get_error(shout2send->conn)); 
432        } 
433
434      if(shout_set_password(shout2send->conn, shout2send->password) != SHOUTERR_SUCCESS)
435        {
436          g_error ("Error setting password: %s\n", shout_get_error(shout2send->conn)); 
437        }
438      
439      if (shout_set_name(shout2send->conn, shout2send->name) != SHOUTERR_SUCCESS)
440        {
441          g_error ("Error setting name: %s\n", shout_get_error(shout2send->conn)); 
442        } 
443
444      if (shout_set_description(shout2send->conn, shout2send->description) != SHOUTERR_SUCCESS)
445        {
446          g_error ("Error setting name: %s\n", shout_get_error(shout2send->conn)); 
447        } 
448
449      if (shout_set_genre(shout2send->conn, shout2send->genre) != SHOUTERR_SUCCESS)
450        {
451          g_error ("Error setting name: %s\n", shout_get_error(shout2send->conn)); 
452        } 
453
454      if (shout_set_mount(shout2send->conn, shout2send->mount) != SHOUTERR_SUCCESS)
455        {
456          g_error ("Error setting mount point: %s\n", shout_get_error(shout2send->conn)); 
457        }
458  
459      if (shout_set_user(shout2send->conn, "source") != SHOUTERR_SUCCESS)
460        {
461          g_error ("Error setting user: %s\n", shout_get_error(shout2send->conn)); 
462        }
463
464      gst_version(&major,&minor,&micro);
465      
466      version_string = g_strdup_printf("GStreamer %d.%d.%d", major,minor,micro);
467
468      if (shout_set_agent(shout2send->conn, version_string) != SHOUTERR_SUCCESS)
469        {
470          g_error ("Error setting agent: %s\n", shout_get_error(shout2send->conn)); 
471        }
472      
473      g_free (version_string);
474      
475
476      
477      break;
478   case GST_STATE_READY_TO_PAUSED:
479
480     /* This sets the format acording to the capabilities of what
481        we are being given as input. */
482
483     if (shout_set_format(shout2send->conn, audio_format) != SHOUTERR_SUCCESS)
484        {
485          g_error ("Error setting connection format: %s\n", shout_get_error(shout2send->conn)); 
486        } 
487
488     if (shout_open (shout2send->conn) == SHOUTERR_SUCCESS) {
489       g_print ("connected to server...\n");
490     }
491     else {
492       /* changed from g_warning, and included result code lookup. */
493       g_error ("couldn't connect to server...");
494       shout_close (shout2send->conn);
495       shout_free (shout2send->conn);
496       return GST_STATE_FAILURE;
497     }
498     break;
499   case GST_STATE_READY_TO_NULL:
500     shout_close (shout2send->conn);
501     shout_free (shout2send->conn);
502     break;
503   default:
504     break;
505   }
506   
507   /* if we haven't failed already, give the parent class a chance to ;-) */
508   if (GST_ELEMENT_CLASS (parent_class)->change_state)
509     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
510   
511   return GST_STATE_SUCCESS;
512 }
513
514 static gboolean
515 plugin_init (GModule *module, GstPlugin *plugin)
516 {
517   GstElementFactory *factory;
518
519   factory = gst_element_factory_new("shout2send", GST_TYPE_SHOUT2SEND, &shout2send_details);
520   g_return_val_if_fail(factory != NULL, FALSE);
521
522   gst_element_factory_add_pad_template (factory, sink_template_factory ());
523
524   gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
525
526   return TRUE;
527 }
528
529 GstPluginDesc plugin_desc = {
530   GST_VERSION_MAJOR,
531   GST_VERSION_MINOR,
532   "shout2send",
533   plugin_init
534 };
535