65b6e90f96530d8531d9fa05c47aa5e840508367
[platform/upstream/gstreamer.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   "Shout2send",
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_MOUNT,       /* mountpoint of stream (icecast only) */
53   ARG_URL,         /* Url of stream (I'm guessing) */
54 };
55
56 static GstPadTemplate*
57 sink_template_factory (void)
58 {
59   static GstPadTemplate *template = NULL;
60   
61   if (!template) {
62     template = gst_pad_template_new (
63       "sink",
64       GST_PAD_SINK,
65       GST_PAD_ALWAYS,
66       gst_caps_new (
67         "shout2send_sink",
68         "application/x-ogg",
69         NULL),
70       gst_caps_new (
71         "shout2send_sink",
72         "audio/x-mp3",
73         NULL),
74       NULL);
75   }
76
77   return template;
78 }
79
80 static void                     gst_shout2send_class_init       (GstShout2sendClass *klass);
81 static void                     gst_shout2send_init             (GstShout2send *shout2send);
82
83 static void                     gst_shout2send_chain            (GstPad *pad, GstBuffer *buf);
84 static GstPadConnectReturn      gst_shout2send_connect         (GstPad *pad, GstCaps *caps);
85
86 static void                     gst_shout2send_set_property             (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
87 static void                     gst_shout2send_get_property             (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
88
89 static GstElementStateReturn    gst_shout2send_change_state     (GstElement *element);
90
91 static GstElementClass *parent_class = NULL;
92 /*static guint gst_shout2send_signals[LAST_SIGNAL] = { 0 }; */
93
94 GType
95 gst_shout2send_get_type(void)
96 {
97   static GType shout2send_type = 0;
98
99   if (!shout2send_type) {
100     static const GTypeInfo shout2send_info = {
101       sizeof(GstShout2sendClass),      NULL,
102       NULL,
103       (GClassInitFunc)gst_shout2send_class_init,
104       NULL,
105       NULL,
106       sizeof(GstShout2send),
107       0,
108       (GInstanceInitFunc)gst_shout2send_init,
109     };
110     shout2send_type = g_type_register_static(GST_TYPE_ELEMENT, "GstShout2send", &shout2send_info, 0);
111   }
112   return shout2send_type;
113 }
114
115 static void
116 gst_shout2send_class_init (GstShout2sendClass *klass)
117 {
118   GObjectClass *gobject_class;
119   GstElementClass *gstelement_class;
120
121   gobject_class = (GObjectClass*)klass;
122   gstelement_class = (GstElementClass*)klass;
123
124   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
125
126   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_IP,
127     g_param_spec_string("ip","ip","ip",
128                         NULL, G_PARAM_READWRITE)); /* CHECKME */
129   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_PORT,
130     g_param_spec_int("port","port","port",
131                      1,G_MAXUSHORT,8000,G_PARAM_READWRITE)); /* CHECKME */
132
133   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_PASSWORD,
134     g_param_spec_string("password","password","password",
135                         NULL, G_PARAM_READWRITE)); /* CHECKME */
136
137   /* metadata */
138   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_NAME,
139     g_param_spec_string("name","name","name",
140                         NULL, G_PARAM_READWRITE)); /* CHECKME */
141
142   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_DESCRIPTION,
143     g_param_spec_string("description","description","description",
144                         NULL, G_PARAM_READWRITE)); /* CHECKME */
145
146   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_GENRE,
147     g_param_spec_string("genre","genre","genre",
148                         NULL, G_PARAM_READWRITE)); /* CHECKME */
149
150   /* icecast only */
151   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_MOUNT,
152     g_param_spec_string("mount","mount","mount",
153                         NULL, G_PARAM_READWRITE)); /* CHECKME */
154
155   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_URL,
156     g_param_spec_string("url","url","url",
157                         NULL, G_PARAM_READWRITE)); /* CHECKME */
158   
159
160   
161   gobject_class->set_property = gst_shout2send_set_property;
162   gobject_class->get_property = gst_shout2send_get_property;
163
164   gstelement_class->change_state = gst_shout2send_change_state;
165 }
166
167 static void
168 gst_shout2send_init (GstShout2send *shout2send)
169 {
170   shout2send->sinkpad = gst_pad_new_from_template (sink_template_factory (), "sink");
171   gst_element_add_pad(GST_ELEMENT(shout2send),shout2send->sinkpad);
172   gst_pad_set_chain_function(shout2send->sinkpad,gst_shout2send_chain);
173
174   gst_pad_set_connect_function (shout2send->sinkpad, gst_shout2send_connect);
175
176   shout2send->ip = g_strdup ("127.0.0.1");
177   shout2send->port = 8000;
178   shout2send->password = g_strdup ("hackme");
179   shout2send->name = g_strdup ("");
180   shout2send->description = g_strdup ("");
181   shout2send->genre = g_strdup ("");
182   shout2send->mount = g_strdup ("");
183   shout2send->url = g_strdup ("");
184 }
185
186 static void
187 gst_shout2send_chain (GstPad *pad, GstBuffer *buf)
188 {
189   GstShout2send *shout2send;
190   glong ret;
191
192   g_return_if_fail(pad != NULL);
193   g_return_if_fail(GST_IS_PAD(pad));
194   g_return_if_fail(buf != NULL);
195
196   shout2send = GST_SHOUT2SEND (GST_OBJECT_PARENT (pad));
197
198   g_return_if_fail (shout2send != NULL);
199   g_return_if_fail (GST_IS_SHOUT2SEND (shout2send));
200
201   ret = shout_send (shout2send->conn, GST_BUFFER_DATA (buf),
202                                              GST_BUFFER_SIZE (buf));
203   if (ret != SHOUTERR_SUCCESS) {
204     g_warning ("send error: %s...\n", shout_get_error(shout2send->conn));
205   }
206
207   shout_sync (shout2send->conn);
208
209   gst_buffer_unref (buf);
210 }
211
212 static void
213 gst_shout2send_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
214 {
215   GstShout2send *shout2send;
216
217   /* it's not null if we got it, but it might not be ours */
218   g_return_if_fail(GST_IS_SHOUT2SEND(object));
219   shout2send = GST_SHOUT2SEND(object);
220
221   switch (prop_id) {
222
223   case ARG_IP:
224     if (shout2send->ip)
225       g_free (shout2send->ip);
226     shout2send->ip = g_strdup (g_value_get_string (value));
227     break;
228     
229   case ARG_PORT:
230     shout2send->port = g_value_get_int (value);
231     break;
232
233   case ARG_PASSWORD:
234     if (shout2send->password)
235       g_free (shout2send->password);
236     shout2send->password = g_strdup (g_value_get_string (value));
237     break;
238
239   case ARG_NAME:         /* Name of the stream */
240     if (shout2send->name)
241       g_free (shout2send->name);
242     shout2send->name = g_strdup (g_value_get_string (value));
243     break;
244
245   case ARG_DESCRIPTION: /* Description of the stream */
246     if (shout2send->description)
247       g_free (shout2send->description);
248     shout2send->description = g_strdup (g_value_get_string (value));
249     break;
250     
251   case ARG_GENRE:       /* Genre of the stream */
252     if (shout2send->genre)
253       g_free (shout2send->genre);
254     shout2send->genre = g_strdup (g_value_get_string (value));
255     break;
256     
257   case ARG_MOUNT:       /* mountpoint of stream (icecast only) */
258     if (shout2send->mount)
259       g_free (shout2send->mount);
260     shout2send->mount = g_strdup (g_value_get_string (value));
261     break;
262       
263   case ARG_URL:       /* Url of the stream (I'm guessing) */
264     if (shout2send->url)
265       g_free (shout2send->url);
266     shout2send->url = g_strdup (g_value_get_string (value));
267     break;
268     
269   default:
270     break;
271   }
272 }
273
274 static void
275 gst_shout2send_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
276 {
277   GstShout2send *shout2send;
278
279   /* it's not null if we got it, but it might not be ours */
280   g_return_if_fail(GST_IS_SHOUT2SEND(object));
281   shout2send = GST_SHOUT2SEND(object);
282
283   switch (prop_id) {
284   
285   case ARG_IP:
286     g_value_set_string (value, shout2send->ip);
287     break;
288   case ARG_PORT:
289     g_value_set_int (value, shout2send->port);
290     break;
291   case ARG_PASSWORD:
292     g_value_set_string (value, shout2send->password);
293     break;
294
295   case ARG_NAME:         /* Name of the stream */
296     g_value_set_string (value, shout2send->name);
297     break;
298
299   case ARG_DESCRIPTION: /* Description of the stream */
300     g_value_set_string (value, shout2send->description);
301     break;
302
303   case ARG_GENRE:       /* Genre of the stream */
304     g_value_set_string (value, shout2send->genre);
305     break;
306
307   case ARG_MOUNT:       /* mountpoint of stream (icecast only) */
308     g_value_set_string (value, shout2send->mount);
309     break;
310
311   case ARG_URL:       /* Url of stream (I'm guessing) */
312     g_value_set_string (value, shout2send->url);
313     break;
314       
315     
316   default:
317     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
318     break;
319   }
320 }
321
322 static GstPadConnectReturn
323 gst_shout2send_connect (GstPad *pad, GstCaps *caps)
324
325 {
326   
327   if (!strcmp(gst_caps_get_mime (caps), "audio/x-mp3"))
328     {
329       audio_format = SHOUT_FORMAT_MP3;
330       return GST_PAD_CONNECT_OK;
331     }
332
333   if (!strcmp(gst_caps_get_mime (caps), "application/x-ogg"))
334     {
335       audio_format = SHOUT_FORMAT_VORBIS;
336       return GST_PAD_CONNECT_OK;
337     }
338   else {
339     return GST_PAD_CONNECT_REFUSED;
340   }
341         
342 }
343
344 static GstElementStateReturn
345 gst_shout2send_change_state (GstElement *element)
346 {
347   GstShout2send *shout2send;
348
349   guint major, minor, micro;
350
351   gchar *version_string;
352
353   g_return_val_if_fail (GST_IS_SHOUT2SEND (element), GST_STATE_FAILURE);
354
355   shout2send = GST_SHOUT2SEND(element);
356
357   GST_DEBUG (0,"state pending %d", GST_STATE_PENDING (element));
358
359   /* if going down into NULL state, close the file if it's open */
360   switch (GST_STATE_TRANSITION (element)) {
361    case GST_STATE_NULL_TO_READY:
362      shout2send->conn = shout_new();
363
364      /* There are other choices for protocols (_ICE, _XAUDIOCAST and _ICY)
365         adding a property to set this might be in order */
366      
367      if (shout_set_protocol(shout2send->conn, SHOUT_PROTOCOL_HTTP) != SHOUTERR_SUCCESS)
368        {
369          g_error ("Error setting protocol: %s\n", shout_get_error(shout2send->conn));
370        }
371
372      /* --- FIXME: shout requires an ip, and fails if it is given a host. */
373      /* may want to put convert_to_ip(shout2send->ip) here */
374
375
376      if (shout_set_host(shout2send->conn, shout2send->ip) != SHOUTERR_SUCCESS)
377        {
378          g_error ("Error setting host: %s\n", shout_get_error(shout2send->conn)); 
379        }
380      /* --- */
381      
382      if (shout_set_port(shout2send->conn, shout2send->port) != SHOUTERR_SUCCESS)
383        {
384          g_error ("Error setting port: %s\n", shout_get_error(shout2send->conn)); 
385        } 
386      
387      if(shout_set_password(shout2send->conn, shout2send->password) != SHOUTERR_SUCCESS)
388        {
389          g_error ("Error setting password: %s\n", shout_get_error(shout2send->conn)); 
390        }  
391      
392      if (shout_set_name(shout2send->conn, shout2send->name) != SHOUTERR_SUCCESS)
393        {
394          g_error ("Error setting name: %s\n", shout_get_error(shout2send->conn)); 
395        } 
396
397      if (shout_set_description(shout2send->conn, shout2send->description) != SHOUTERR_SUCCESS)
398        {
399          g_error ("Error setting name: %s\n", shout_get_error(shout2send->conn)); 
400        } 
401
402      if (shout_set_genre(shout2send->conn, shout2send->genre) != SHOUTERR_SUCCESS)
403        {
404          g_error ("Error setting name: %s\n", shout_get_error(shout2send->conn)); 
405        } 
406
407      if (shout_set_mount(shout2send->conn, shout2send->mount) != SHOUTERR_SUCCESS)
408        {
409          g_error ("Error setting mount point: %s\n", shout_get_error(shout2send->conn)); 
410        }
411  
412      if (shout_set_user(shout2send->conn, "source") != SHOUTERR_SUCCESS)
413        {
414          g_error ("Error setting user: %s\n", shout_get_error(shout2send->conn)); 
415        }
416
417      gst_version(&major,&minor,&micro);
418      
419      version_string = g_strdup_printf("GStreamer %d.%d.%d", major,minor,micro);
420
421      if (shout_set_agent(shout2send->conn, version_string) != SHOUTERR_SUCCESS)
422        {
423          g_error ("Error setting agent: %s\n", shout_get_error(shout2send->conn)); 
424        }
425      
426      g_free (version_string);
427      
428
429      
430      break;
431   case GST_STATE_READY_TO_PAUSED:
432
433     /* This sets the format acording to the capabilities of what
434        we are being given as input. */
435
436     if (shout_set_format(shout2send->conn, audio_format) != SHOUTERR_SUCCESS)
437        {
438          g_error ("Error setting connection format: %s\n", shout_get_error(shout2send->conn)); 
439        } 
440
441     if (shout_open (shout2send->conn) == SHOUTERR_SUCCESS) {
442       g_print ("connected to server...\n");
443     }
444     else {
445       /* changed from g_warning, and included result code lookup. */
446       g_error ("couldn't connect to server...");
447       shout_close (shout2send->conn);
448       shout_free (shout2send->conn);
449       return GST_STATE_FAILURE;
450     }
451     break;
452   case GST_STATE_READY_TO_NULL:
453     shout_close (shout2send->conn);
454     shout_free (shout2send->conn);
455     break;
456   default:
457     break;
458   }
459   
460   /* if we haven't failed already, give the parent class a chance to ;-) */
461   if (GST_ELEMENT_CLASS (parent_class)->change_state)
462     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
463   
464   return GST_STATE_SUCCESS;
465 }
466
467 static gboolean
468 plugin_init (GModule *module, GstPlugin *plugin)
469 {
470   GstElementFactory *factory;
471
472   factory = gst_element_factory_new("shout2send", GST_TYPE_SHOUT2SEND, &shout2send_details);
473   g_return_val_if_fail(factory != NULL, FALSE);
474
475   gst_element_factory_add_pad_template (factory, sink_template_factory ());
476
477   gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
478
479   return TRUE;
480 }
481
482 GstPluginDesc plugin_desc = {
483   GST_VERSION_MAJOR,
484   GST_VERSION_MINOR,
485   "shout2send",
486   plugin_init
487 };
488