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