+ changes for new float caps without slope/intercept + some category changes for...
[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, GstBuffer *buf);
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, GstBuffer *buf)
221 {
222   GstShout2send *shout2send;
223   glong ret;
224
225   g_return_if_fail(pad != NULL);
226   g_return_if_fail(GST_IS_PAD(pad));
227   g_return_if_fail(buf != NULL);
228
229   shout2send = GST_SHOUT2SEND (GST_OBJECT_PARENT (pad));
230
231   g_return_if_fail (shout2send != NULL);
232   g_return_if_fail (GST_IS_SHOUT2SEND (shout2send));
233
234   ret = shout_send (shout2send->conn, GST_BUFFER_DATA (buf),
235                                              GST_BUFFER_SIZE (buf));
236   if (ret != SHOUTERR_SUCCESS) {
237     g_warning ("send error: %s...\n", shout_get_error(shout2send->conn));
238   }
239
240   shout_sync (shout2send->conn);
241
242   gst_buffer_unref (buf);
243 }
244
245 static void
246 gst_shout2send_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
247 {
248   GstShout2send *shout2send;
249
250   /* it's not null if we got it, but it might not be ours */
251   g_return_if_fail(GST_IS_SHOUT2SEND(object));
252   shout2send = GST_SHOUT2SEND(object);
253
254   switch (prop_id) {
255
256   case ARG_IP:
257     if (shout2send->ip)
258       g_free (shout2send->ip);
259     shout2send->ip = g_strdup (g_value_get_string (value));
260     break;
261     
262   case ARG_PORT:
263     shout2send->port = g_value_get_int (value);
264     break;
265
266   case ARG_PASSWORD:
267     if (shout2send->password)
268       g_free (shout2send->password);
269     shout2send->password = g_strdup (g_value_get_string (value));
270     break;
271
272   case ARG_NAME:         /* Name of the stream */
273     if (shout2send->name)
274       g_free (shout2send->name);
275     shout2send->name = g_strdup (g_value_get_string (value));
276     break;
277
278   case ARG_DESCRIPTION: /* Description of the stream */
279     if (shout2send->description)
280       g_free (shout2send->description);
281     shout2send->description = g_strdup (g_value_get_string (value));
282     break;
283     
284   case ARG_GENRE:       /* Genre of the stream */
285     if (shout2send->genre)
286       g_free (shout2send->genre);
287     shout2send->genre = g_strdup (g_value_get_string (value));
288     break;
289     
290   case ARG_PROTOCOL:       /* protocol to connect with */
291     shout2send->protocol = g_value_get_enum (value);
292     break;  
293
294   case ARG_MOUNT:       /* mountpoint of stream (icecast only) */
295     if (shout2send->mount)
296       g_free (shout2send->mount);
297     shout2send->mount = g_strdup (g_value_get_string (value));
298     break;
299       
300   case ARG_URL:       /* Url of the stream (I'm guessing) */
301     if (shout2send->url)
302       g_free (shout2send->url);
303     shout2send->url = g_strdup (g_value_get_string (value));
304     break;
305     
306   default:
307     break;
308   }
309 }
310
311 static void
312 gst_shout2send_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
313 {
314   GstShout2send *shout2send;
315
316   /* it's not null if we got it, but it might not be ours */
317   g_return_if_fail(GST_IS_SHOUT2SEND(object));
318   shout2send = GST_SHOUT2SEND(object);
319
320   switch (prop_id) {
321   
322   case ARG_IP:
323     g_value_set_string (value, shout2send->ip);
324     break;
325   case ARG_PORT:
326     g_value_set_int (value, shout2send->port);
327     break;
328   case ARG_PASSWORD:
329     g_value_set_string (value, shout2send->password);
330     break;
331
332   case ARG_NAME:         /* Name of the stream */
333     g_value_set_string (value, shout2send->name);
334     break;
335
336   case ARG_DESCRIPTION: /* Description of the stream */
337     g_value_set_string (value, shout2send->description);
338     break;
339
340   case ARG_GENRE:       /* Genre of the stream */
341     g_value_set_string (value, shout2send->genre);
342     break;
343
344   case ARG_PROTOCOL:       /* protocol to connect with */
345     g_value_set_enum (value, shout2send->protocol);
346     break; 
347
348   case ARG_MOUNT:       /* mountpoint of stream (icecast only) */
349     g_value_set_string (value, shout2send->mount);
350     break;
351
352   case ARG_URL:       /* Url of stream (I'm guessing) */
353     g_value_set_string (value, shout2send->url);
354     break;
355       
356     
357   default:
358     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
359     break;
360   }
361 }
362
363 static GstPadLinkReturn
364 gst_shout2send_connect (GstPad *pad, GstCaps *caps)
365
366 {
367   
368   if (!strcmp(gst_caps_get_mime (caps), "audio/mpeg"))
369     {
370       audio_format = SHOUT_FORMAT_MP3;
371       return GST_PAD_LINK_OK;
372     }
373
374   if (!strcmp(gst_caps_get_mime (caps), "application/ogg"))
375     {
376       audio_format = SHOUT_FORMAT_VORBIS;
377       return GST_PAD_LINK_OK;
378     }
379   else {
380     return GST_PAD_LINK_REFUSED;
381   }
382         
383 }
384
385 static GstElementStateReturn
386 gst_shout2send_change_state (GstElement *element)
387 {
388   GstShout2send *shout2send;
389
390   guint major, minor, micro;
391   gshort proto = 3;
392
393   gchar *version_string;
394
395   g_return_val_if_fail (GST_IS_SHOUT2SEND (element), GST_STATE_FAILURE);
396
397   shout2send = GST_SHOUT2SEND(element);
398
399   GST_DEBUG ("state pending %d", GST_STATE_PENDING (element));
400
401   /* if going down into NULL state, close the file if it's open */
402   switch (GST_STATE_TRANSITION (element)) {
403    case GST_STATE_NULL_TO_READY:
404      shout2send->conn = shout_new();
405
406      switch (shout2send->protocol) {
407      case SHOUT2SEND_PROTOCOL_ICE:
408        proto = SHOUT_PROTOCOL_ICE;
409        break;
410      case SHOUT2SEND_PROTOCOL_XAUDIOCAST:
411        proto = SHOUT_PROTOCOL_XAUDIOCAST;
412        break;
413      case SHOUT2SEND_PROTOCOL_ICY:
414        proto = SHOUT_PROTOCOL_ICY;
415        break;
416      case SHOUT2SEND_PROTOCOL_HTTP:
417        proto = SHOUT_PROTOCOL_HTTP;
418        break;
419      }
420
421      if (shout_set_protocol(shout2send->conn, proto) != SHOUTERR_SUCCESS)
422        {
423          g_error ("Error setting protocol: %s\n", shout_get_error(shout2send->conn));
424        }
425
426      /* --- FIXME: shout requires an ip, and fails if it is given a host. */
427      /* may want to put convert_to_ip(shout2send->ip) here */
428
429
430      if (shout_set_host(shout2send->conn, shout2send->ip) != SHOUTERR_SUCCESS)
431        {
432          g_error ("Error setting host: %s\n", shout_get_error(shout2send->conn)); 
433        }
434      /* --- */
435      
436      if (shout_set_port(shout2send->conn, shout2send->port) != SHOUTERR_SUCCESS)
437        {
438          g_error ("Error setting port: %s\n", shout_get_error(shout2send->conn)); 
439        } 
440
441      if(shout_set_password(shout2send->conn, shout2send->password) != SHOUTERR_SUCCESS)
442        {
443          g_error ("Error setting password: %s\n", shout_get_error(shout2send->conn)); 
444        }
445      
446      if (shout_set_name(shout2send->conn, shout2send->name) != SHOUTERR_SUCCESS)
447        {
448          g_error ("Error setting name: %s\n", shout_get_error(shout2send->conn)); 
449        } 
450
451      if (shout_set_description(shout2send->conn, shout2send->description) != SHOUTERR_SUCCESS)
452        {
453          g_error ("Error setting name: %s\n", shout_get_error(shout2send->conn)); 
454        } 
455
456      if (shout_set_genre(shout2send->conn, shout2send->genre) != SHOUTERR_SUCCESS)
457        {
458          g_error ("Error setting name: %s\n", shout_get_error(shout2send->conn)); 
459        } 
460
461      if (shout_set_mount(shout2send->conn, shout2send->mount) != SHOUTERR_SUCCESS)
462        {
463          g_error ("Error setting mount point: %s\n", shout_get_error(shout2send->conn)); 
464        }
465  
466      if (shout_set_user(shout2send->conn, "source") != SHOUTERR_SUCCESS)
467        {
468          g_error ("Error setting user: %s\n", shout_get_error(shout2send->conn)); 
469        }
470
471      gst_version(&major,&minor,&micro);
472      
473      version_string = g_strdup_printf("GStreamer %d.%d.%d", major,minor,micro);
474
475      if (shout_set_agent(shout2send->conn, version_string) != SHOUTERR_SUCCESS)
476        {
477          g_error ("Error setting agent: %s\n", shout_get_error(shout2send->conn)); 
478        }
479      
480      g_free (version_string);
481      
482
483      
484      break;
485   case GST_STATE_READY_TO_PAUSED:
486
487     /* This sets the format acording to the capabilities of what
488        we are being given as input. */
489
490     if (shout_set_format(shout2send->conn, audio_format) != SHOUTERR_SUCCESS)
491        {
492          g_error ("Error setting connection format: %s\n", shout_get_error(shout2send->conn)); 
493        } 
494
495     if (shout_open (shout2send->conn) == SHOUTERR_SUCCESS) {
496       g_print ("connected to server...\n");
497     }
498     else {
499       g_warning ("Couldn't connect to server: %s", shout_get_error(shout2send->conn));
500       shout_close (shout2send->conn);
501       shout_free (shout2send->conn);
502       return GST_STATE_FAILURE;
503     }
504     break;
505   case GST_STATE_PAUSED_TO_READY:
506     shout_close (shout2send->conn);
507     shout_free (shout2send->conn);
508     break;
509   default:
510     break;
511   }
512   
513   /* if we haven't failed already, give the parent class a chance to ;-) */
514   if (GST_ELEMENT_CLASS (parent_class)->change_state)
515     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
516   
517   return GST_STATE_SUCCESS;
518 }
519
520 static gboolean
521 plugin_init (GModule *module, GstPlugin *plugin)
522 {
523   GstElementFactory *factory;
524
525   factory = gst_element_factory_new("shout2send", GST_TYPE_SHOUT2SEND, &shout2send_details);
526   g_return_val_if_fail(factory != NULL, FALSE);
527
528   gst_element_factory_add_pad_template (factory, sink_template_factory ());
529
530   gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
531
532   return TRUE;
533 }
534
535 GstPluginDesc plugin_desc = {
536   GST_VERSION_MAJOR,
537   GST_VERSION_MINOR,
538   "shout2send",
539   plugin_init
540 };
541