4 * @file gmyth/gmyth_vlc.c
6 * @brief <p> GMythVLC library provides functions that
7 * interact with a VLC server running telnet interface.
9 * Copyright (C) 2007 INdT - Instituto Nokia de Tecnologia.
10 * @author Artur Duque de Souza <artur.souza@indt.org.br>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU Lesser General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include "gmyth_vlc.h"
35 #include "gmyth_debug.h"
36 #include "gmyth_socket.h"
38 /* static functions */
39 static int _socket_send(int socket, gchar* msg);
40 static int _socket_recv(int socket, gchar* buf);
42 /** Primitive function to send a message through the socket
45 * @param msg the message itself
46 * @return 0 if success
49 _socket_send(int socket, gchar* msg)
51 size_t size = strlen(msg) + 2; // (\n + \0)
52 gchar* final = (gchar *)g_malloc0(sizeof(gchar) * size);
53 g_snprintf(final, size, "%s\n", msg);
55 if (send(socket, final, strlen(final), 0) == -1)
56 perror("GMyth_VLC: send error");
63 /** Primitive function to receive a message through the socket
66 * @param buf Buffer to put the message
67 * @return 0 if success
70 _socket_recv(int socket, gchar* buf)
74 if ((numbytes=recv(socket, buf, BUFFER-1, 0)) == -1)
76 perror("GMyth_VLC: recv error");
80 buf[numbytes-1] = '\0';
85 /** Function that adds options to the output media
87 * @param vlc structure with options for vlc
88 * @param output the number of the output media
89 * @param kind the type of option we'll change
90 * @param the params for the option
91 * @return 0 if success
94 gmyth_vlc_setup_output(GMythVlc *vlc, int output, \
95 gchar* kind, gchar* opts)
99 size_t size = strlen(opts) + 25;
100 gchar* msg = g_malloc0(sizeof(gchar) * size);
101 g_snprintf(msg, size, "setup output%d %s %s", output,\
104 ret = _socket_send(vlc->sockfd, msg);
111 /** Function to clean the playlist
113 * @param vlc structure with options for vlc
114 * @param output the number of the output media
115 * @param file the file we want to insert in the playlist
116 * @return 0 if success
119 gmyth_vlc_clean_playlist(GMythVlc *vlc)
121 return _socket_send(vlc->sockfd, "del all");
125 /** Function to control the playlist
127 * @param vlc structure with options for vlc
128 * @param output the number of the output media
129 * @param command play, stop or pause(just for vod)
130 * @return 0 if success
133 gmyth_vlc_control_input(GMythVlc *vlc, int output, \
137 gchar* msg = g_malloc0(sizeof(gchar) * size);
138 g_snprintf(msg, size, "control output%d %s", output,\
141 int ret = _socket_send(vlc->sockfd, msg);
149 /** Function to insert an item in the playlist
151 * @param vlc structure with options for vlc
152 * @param output the number of the output media
153 * @param file the file we want to insert in the playlist
154 * @return 0 if success
157 gmyth_vlc_create_input(GMythVlc *vlc, int output,\
160 return gmyth_vlc_setup_output(vlc, output, "input",\
165 /** Function to create a channel in vlc
167 * @param vlc structure with options for vlc
168 * @param type the type of channel (broadcast, vod...)
170 * @return 0 if success
173 gmyth_vlc_create_channel(GMythVlc *vlc, gchar* type,\
174 int port, int vcodec)
178 gchar* msg = (gchar *)g_malloc0(sizeof(gchar) * size);
179 g_snprintf(msg, size, "new output%d %s enabled loop", \
180 vlc->n_outputs, type);
182 ret = _socket_send(vlc->sockfd, msg);
186 gmyth_vlc_setup_output(vlc, vlc->n_outputs, "option", \
191 msg = (gchar *)g_malloc0(sizeof(gchar) * size);
193 if ( vcodec == MPEG1 )
195 // Best transcode option for N800 (MP1V)
196 g_snprintf(msg, size, "#transcode{vcodec=mp1v,vb=384,"
197 "fps=25.0,scale=1,acodec=mpga,"
198 "ab=64,channels=1}:duplicate{dst="
199 "std{access=http,mux=mpeg1,dst="
203 // Best transcode option for N800 (THEORA)
204 g_snprintf(msg, size, "#transcode{vcodec=theo,vb=384,"
205 "fps=25.0,scale=1,acodec=vorb,"
206 "ab=64,channels=1}:duplicate{dst="
207 "std{access=http,mux=ogg,dst="
210 ret = gmyth_vlc_setup_output(vlc, vlc->n_outputs, "output", \
222 /** Function to connect to vlc on the backend
224 * @param vlc structure with options for vlc
225 * @param backend_info infos about the backend
226 * @param passwd the password for telnet interface
227 * @return 0 if success
230 gmyth_vlc_connect(GMythVlc *vlc, GMythBackendInfo *backend_info,\
231 gchar* passwd, int port)
235 if ((vlc->he=gethostbyname(backend_info->hostname)) == NULL)
237 herror("GMyth_VLC: gethostbyname error");
241 if ((vlc->sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
243 perror("GMyth_VLC: socket error");
248 vlc->their_addr.sin_family = AF_INET;
249 vlc->their_addr.sin_port = htons(port);
250 vlc->their_addr.sin_addr = *((struct in_addr *)vlc->he->h_addr);
251 memset(&(vlc->their_addr.sin_zero), '\0', 8);
253 if (connect(vlc->sockfd, (struct sockaddr *)&(vlc->their_addr),
254 sizeof(struct sockaddr)) == -1)
256 perror("GMyth_VLC: connect error. Check VLC's telnet interface");
261 // Receive the Password's Prompt
262 numbytes = _socket_recv(vlc->sockfd, vlc->buf);
264 // Send the Password. We don't have to
265 // care about passwords being sent in plain text
266 // because telnet protocol does it.
267 _socket_send(vlc->sockfd, passwd);
269 // Receive the Welcome msg
270 numbytes = _socket_recv(vlc->sockfd, vlc->buf);
272 if (strncmp(vlc->buf,"\r\nWrong password.", 17) == 0)
274 perror("Gmyth_VLC: passwd error. Check your passwd");
283 /** Function to disconnect from vlc
285 * @param vlc structure with options for vlc
286 * @param backend_info infos about the backend
287 * @return 0 if success
290 gmyth_vlc_disconnect(GMythVlc *vlc)
295 ret = gmyth_vlc_clean_playlist(vlc);
303 return close(vlc->sockfd);