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