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