rosfran@41
|
1 |
/**
|
rosfran@68
|
2 |
* GMyth Library
|
rosfran@68
|
3 |
*
|
rosfran@68
|
4 |
* @file gmyth/gmyth_file_transfer.c
|
rosfran@68
|
5 |
*
|
rosfran@68
|
6 |
* @brief <p> GMythFileTransfer deals with the file streaming media remote/local
|
rosfran@68
|
7 |
* transfering to the MythTV frontend.
|
rosfran@68
|
8 |
*
|
rosfran@68
|
9 |
* Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
|
rosfran@68
|
10 |
* @author Rosfran Lins Borges <rosfran.borges@indt.org.br>
|
rosfran@68
|
11 |
*
|
rosfran@68
|
12 |
*//*
|
rosfran@68
|
13 |
*
|
rosfran@68
|
14 |
* This program is free software; you can redistribute it and/or modify
|
rosfran@68
|
15 |
* it under the terms of the GNU Lesser General Public License as published by
|
rosfran@68
|
16 |
* the Free Software Foundation; either version 2 of the License, or
|
rosfran@68
|
17 |
* (at your option) any later version.
|
rosfran@68
|
18 |
*
|
rosfran@68
|
19 |
* This program is distributed in the hope that it will be useful,
|
rosfran@68
|
20 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
rosfran@68
|
21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
rosfran@68
|
22 |
* GNU General Public License for more details.
|
rosfran@68
|
23 |
*
|
rosfran@68
|
24 |
* You should have received a copy of the GNU Lesser General Public License
|
rosfran@68
|
25 |
* along with this program; if not, write to the Free Software
|
rosfran@68
|
26 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
rosfran@68
|
27 |
*
|
rosfran@68
|
28 |
* GStreamer MythTV plug-in properties:
|
rosfran@41
|
29 |
* - location (backend server hostname/URL) [ex.: myth://192.168.1.73:28722/1000_1092091.nuv]
|
rosfran@41
|
30 |
* - path (qurl - remote file to be opened)
|
rosfran@68
|
31 |
* - port number *
|
rosfran@41
|
32 |
*/
|
leo_sobral@213
|
33 |
|
leo_sobral@213
|
34 |
#ifdef HAVE_CONFIG_H
|
leo_sobral@213
|
35 |
#include "config.h"
|
leo_sobral@213
|
36 |
#endif
|
rosfran@41
|
37 |
|
rosfran@42
|
38 |
#include "gmyth_file_transfer.h"
|
rosfran@216
|
39 |
#include "gmyth_livetv.h"
|
rosfran@216
|
40 |
#include "gmyth_util.h"
|
rosfran@216
|
41 |
#include "gmyth_socket.h"
|
rosfran@216
|
42 |
#include "gmyth_stringlist.h"
|
rosfran@216
|
43 |
#include "gmyth_debug.h"
|
rosfran@216
|
44 |
#include "gmyth_uri.h"
|
rosfran@216
|
45 |
#include "gmyth_marshal.h"
|
rosfran@216
|
46 |
|
rosfran@41
|
47 |
#include <unistd.h>
|
rosfran@41
|
48 |
#include <glib.h>
|
rosfran@41
|
49 |
|
rosfran@41
|
50 |
#include <arpa/inet.h>
|
rosfran@41
|
51 |
#include <sys/types.h>
|
rosfran@41
|
52 |
#include <sys/socket.h>
|
rosfran@41
|
53 |
#include <netdb.h>
|
rosfran@41
|
54 |
#include <errno.h>
|
rosfran@41
|
55 |
#include <stdlib.h>
|
melunko@117
|
56 |
#include <assert.h>
|
rosfran@41
|
57 |
|
melunko@112
|
58 |
#define GMYTHTV_QUERY_HEADER "QUERY_FILETRANSFER "
|
rosfran@41
|
59 |
|
rosfran@41
|
60 |
/* default values to the file transfer parameters */
|
rosfran@97
|
61 |
#define GMYTHTV_USER_READ_AHEAD TRUE
|
rosfran@97
|
62 |
#define GMYTHTV_RETRIES -1
|
rosfran@54
|
63 |
#define GMYTHTV_FILE_SIZE 0
|
rosfran@41
|
64 |
|
rosfran@177
|
65 |
#define GMYTHTV_BUFFER_SIZE 8*1024
|
rosfran@41
|
66 |
|
rosfran@54
|
67 |
#define GMYTHTV_VERSION 30
|
rosfran@41
|
68 |
|
rosfran@41
|
69 |
#define GMYTHTV_TRANSFER_MAX_WAITS 700
|
rosfran@41
|
70 |
|
rosfran@41
|
71 |
#ifdef GMYTHTV_ENABLE_DEBUG
|
rosfran@54
|
72 |
#define GMYTHTV_ENABLE_DEBUG 1
|
rosfran@41
|
73 |
#else
|
rosfran@41
|
74 |
#undef GMYTHTV_ENABLE_DEBUG
|
rosfran@41
|
75 |
#endif
|
rosfran@41
|
76 |
|
rosfran@41
|
77 |
/* this NDEBUG is to maintain compatibility with GMyth library */
|
rosfran@41
|
78 |
#ifndef NDEBUG
|
rosfran@54
|
79 |
#define GMYTHTV_ENABLE_DEBUG 1
|
rosfran@41
|
80 |
#endif
|
rosfran@41
|
81 |
|
rosfran@41
|
82 |
enum myth_sock_types {
|
rosfran@41
|
83 |
GMYTH_PLAYBACK_TYPE = 0,
|
rosfran@41
|
84 |
GMYTH_MONITOR_TYPE,
|
rosfran@41
|
85 |
GMYTH_FILETRANSFER_TYPE,
|
rosfran@41
|
86 |
GMYTH_RINGBUFFER_TYPE
|
rosfran@41
|
87 |
};
|
rosfran@41
|
88 |
|
rosfran@216
|
89 |
#define GMYTH_FILE_TRANSFER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GMYTH_FILE_TRANSFER_TYPE, GMythFileTransferPrivate))
|
rosfran@216
|
90 |
|
rosfran@216
|
91 |
struct _GMythFileTransferPrivate {
|
rosfran@216
|
92 |
|
rosfran@216
|
93 |
GMythLiveTV *livetv;
|
rosfran@216
|
94 |
|
rosfran@216
|
95 |
gboolean do_next_program_chain;
|
rosfran@216
|
96 |
|
rosfran@216
|
97 |
};
|
rosfran@115
|
98 |
|
rosfran@41
|
99 |
static void gmyth_file_transfer_class_init (GMythFileTransferClass *klass);
|
rosfran@41
|
100 |
static void gmyth_file_transfer_init (GMythFileTransfer *object);
|
rosfran@41
|
101 |
|
rosfran@41
|
102 |
static void gmyth_file_transfer_dispose (GObject *object);
|
rosfran@41
|
103 |
static void gmyth_file_transfer_finalize (GObject *object);
|
rosfran@41
|
104 |
|
rosfran@216
|
105 |
static void gmyth_file_transfer_program_info_changed( GMythFileTransfer *transfer,
|
rosfran@232
|
106 |
gint msg_code, gpointer livetv_transfer );
|
rosfran@216
|
107 |
|
melunko@112
|
108 |
static gboolean gmyth_connect_to_backend (GMythFileTransfer *transfer);
|
rosfran@41
|
109 |
|
rosfran@41
|
110 |
void gmyth_file_transfer_close( GMythFileTransfer *transfer );
|
rosfran@41
|
111 |
|
rosfran@97
|
112 |
static gboolean myth_control_acquire_context( gboolean do_wait );
|
rosfran@97
|
113 |
|
rosfran@97
|
114 |
static gboolean myth_control_release_context( );
|
rosfran@97
|
115 |
|
rosfran@41
|
116 |
G_DEFINE_TYPE(GMythFileTransfer, gmyth_file_transfer, G_TYPE_OBJECT)
|
rosfran@41
|
117 |
|
rosfran@41
|
118 |
static void
|
rosfran@41
|
119 |
gmyth_file_transfer_class_init (GMythFileTransferClass *klass)
|
rosfran@41
|
120 |
{
|
rosfran@41
|
121 |
GObjectClass *gobject_class;
|
rosfran@216
|
122 |
GMythFileTransferClass *gtransfer_class;
|
rosfran@41
|
123 |
|
rosfran@41
|
124 |
gobject_class = (GObjectClass *) klass;
|
rosfran@216
|
125 |
gtransfer_class = (GMythFileTransferClass *) gobject_class;
|
rosfran@41
|
126 |
|
rosfran@41
|
127 |
gobject_class->dispose = gmyth_file_transfer_dispose;
|
rosfran@41
|
128 |
gobject_class->finalize = gmyth_file_transfer_finalize;
|
rosfran@216
|
129 |
|
rosfran@216
|
130 |
g_type_class_add_private (gobject_class, sizeof (GMythFileTransferPrivate));
|
rosfran@216
|
131 |
|
rosfran@233
|
132 |
gtransfer_class->program_info_changed_handler = gmyth_file_transfer_program_info_changed;
|
rosfran@233
|
133 |
|
rosfran@216
|
134 |
gtransfer_class->program_info_changed_handler_signal_id =
|
rosfran@233
|
135 |
g_signal_new ( "program-info-changed",
|
rosfran@216
|
136 |
G_TYPE_FROM_CLASS (gtransfer_class),
|
rosfran@216
|
137 |
G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS,
|
rosfran@216
|
138 |
0,
|
rosfran@216
|
139 |
NULL,
|
rosfran@216
|
140 |
NULL,
|
rosfran@216
|
141 |
gmyth_marshal_VOID__INT_POINTER,
|
rosfran@216
|
142 |
G_TYPE_NONE,
|
rosfran@216
|
143 |
2,
|
rosfran@216
|
144 |
G_TYPE_INT,
|
rosfran@233
|
145 |
G_TYPE_POINTER );
|
rosfran@216
|
146 |
|
rosfran@41
|
147 |
}
|
rosfran@41
|
148 |
|
melunko@112
|
149 |
static void
|
melunko@112
|
150 |
gmyth_file_transfer_init (GMythFileTransfer *transfer)
|
rosfran@41
|
151 |
{
|
melunko@112
|
152 |
g_return_if_fail( transfer != NULL );
|
melunko@112
|
153 |
|
melunko@112
|
154 |
transfer->readposition = 0;
|
melunko@112
|
155 |
transfer->filesize = GMYTHTV_FILE_SIZE;
|
rosfran@216
|
156 |
|
rosfran@216
|
157 |
transfer->priv = GMYTH_FILE_TRANSFER_GET_PRIVATE(transfer);
|
rosfran@216
|
158 |
|
rosfran@216
|
159 |
transfer->priv->do_next_program_chain = FALSE;
|
rosfran@216
|
160 |
transfer->priv->livetv = NULL;
|
melunko@112
|
161 |
|
melunko@112
|
162 |
transfer->control_sock = NULL;
|
melunko@112
|
163 |
transfer->sock = NULL;
|
rosfran@177
|
164 |
|
rosfran@220
|
165 |
transfer->mutex = g_mutex_new();
|
rosfran@220
|
166 |
|
rosfran@233
|
167 |
g_signal_connect ( G_OBJECT (transfer), "program-info-changed",
|
rosfran@233
|
168 |
(GCallback)(GMYTH_FILE_TRANSFER_GET_CLASS(transfer)->program_info_changed_handler),
|
rosfran@233
|
169 |
NULL );
|
rosfran@233
|
170 |
|
rosfran@41
|
171 |
}
|
rosfran@41
|
172 |
|
rosfran@41
|
173 |
static void
|
rosfran@41
|
174 |
gmyth_file_transfer_dispose (GObject *object)
|
rosfran@41
|
175 |
{
|
rosfran@192
|
176 |
GMythFileTransfer *transfer = GMYTH_FILE_TRANSFER (object);
|
rosfran@192
|
177 |
|
rosfran@220
|
178 |
if ( transfer->mutex != NULL )
|
rosfran@220
|
179 |
{
|
rosfran@220
|
180 |
g_mutex_free( transfer->mutex );
|
rosfran@220
|
181 |
transfer->mutex = NULL;
|
rosfran@220
|
182 |
}
|
rosfran@220
|
183 |
|
rosfran@192
|
184 |
if ( transfer->control_sock != NULL )
|
rosfran@192
|
185 |
{
|
rosfran@192
|
186 |
g_object_unref( transfer->control_sock );
|
rosfran@192
|
187 |
transfer->control_sock = NULL;
|
rosfran@192
|
188 |
}
|
rosfran@192
|
189 |
|
rosfran@192
|
190 |
if ( transfer->sock != NULL )
|
rosfran@192
|
191 |
{
|
rosfran@192
|
192 |
g_object_unref( transfer->sock );
|
rosfran@192
|
193 |
transfer->sock = NULL;
|
renatofilho@187
|
194 |
}
|
rosfran@203
|
195 |
|
rosfran@192
|
196 |
if ( transfer->filename != NULL )
|
rosfran@192
|
197 |
{
|
rosfran@192
|
198 |
g_free( transfer->filename );
|
rosfran@192
|
199 |
transfer->filename = NULL;
|
renatofilho@187
|
200 |
}
|
renatofilho@187
|
201 |
|
rosfran@41
|
202 |
G_OBJECT_CLASS (gmyth_file_transfer_parent_class)->dispose (object);
|
rosfran@41
|
203 |
}
|
rosfran@41
|
204 |
|
rosfran@101
|
205 |
static void
|
rosfran@41
|
206 |
gmyth_file_transfer_finalize (GObject *object)
|
rosfran@41
|
207 |
{
|
rosfran@41
|
208 |
g_signal_handlers_destroy (object);
|
rosfran@41
|
209 |
|
rosfran@41
|
210 |
G_OBJECT_CLASS (gmyth_file_transfer_parent_class)->finalize (object);
|
rosfran@41
|
211 |
}
|
rosfran@41
|
212 |
|
melunko@112
|
213 |
// fixme: do we need the card_id????
|
melunko@112
|
214 |
GMythFileTransfer*
|
rosfran@177
|
215 |
gmyth_file_transfer_new ( const GMythBackendInfo *backend_info)
|
rosfran@41
|
216 |
{
|
renatofilho@166
|
217 |
GMythFileTransfer *transfer = GMYTH_FILE_TRANSFER (g_object_new (GMYTH_FILE_TRANSFER_TYPE, NULL));
|
rosfran@177
|
218 |
|
rosfran@177
|
219 |
transfer->backend_info = (GMythBackendInfo *)backend_info;
|
renatofilho@187
|
220 |
g_object_ref (transfer->backend_info);
|
rosfran@41
|
221 |
|
rosfran@177
|
222 |
return transfer;
|
rosfran@177
|
223 |
}
|
rosfran@177
|
224 |
|
rosfran@177
|
225 |
GMythFileTransfer*
|
renatofilho@187
|
226 |
gmyth_file_transfer_new_with_uri (const gchar* uri_str)
|
rosfran@177
|
227 |
{
|
rosfran@177
|
228 |
GMythFileTransfer *transfer = GMYTH_FILE_TRANSFER (g_object_new (GMYTH_FILE_TRANSFER_TYPE, NULL));
|
melunko@158
|
229 |
|
melunko@201
|
230 |
transfer->backend_info = gmyth_backend_info_new_with_uri (uri_str);
|
renatofilho@187
|
231 |
|
rosfran@41
|
232 |
return transfer;
|
rosfran@41
|
233 |
}
|
rosfran@41
|
234 |
|
rosfran@41
|
235 |
gboolean
|
renatofilho@187
|
236 |
gmyth_file_transfer_open (GMythFileTransfer *transfer, const gchar* filename)
|
rosfran@41
|
237 |
{
|
rosfran@41
|
238 |
gboolean ret = TRUE;
|
rosfran@177
|
239 |
|
renatofilho@202
|
240 |
g_return_val_if_fail (transfer != NULL, FALSE);
|
renatofilho@202
|
241 |
g_return_val_if_fail (filename != NULL, FALSE);
|
rosfran@177
|
242 |
|
renatofilho@202
|
243 |
if (transfer->filename != NULL)
|
rosfran@184
|
244 |
{
|
renatofilho@202
|
245 |
g_free (transfer->filename);
|
renatofilho@202
|
246 |
transfer->filename = NULL;
|
rosfran@184
|
247 |
}
|
renatofilho@202
|
248 |
|
renatofilho@202
|
249 |
transfer->filename = g_strdup (filename);
|
rosfran@184
|
250 |
|
rosfran@41
|
251 |
/* configure the control socket */
|
melunko@112
|
252 |
if (transfer->control_sock == NULL) {
|
renatofilho@143
|
253 |
if (!gmyth_connect_to_backend (transfer)) {
|
renatofilho@187
|
254 |
gmyth_debug ("Connection to backend failed (Control Socket).\n");
|
rosfran@41
|
255 |
ret = FALSE;
|
rosfran@41
|
256 |
}
|
rosfran@41
|
257 |
} else {
|
rosfran@41
|
258 |
g_warning("Remote transfer control socket already created.\n");
|
rosfran@41
|
259 |
}
|
rosfran@177
|
260 |
|
rosfran@205
|
261 |
gmyth_debug ("Got file with size = %lld.\n", transfer->filesize);
|
rosfran@205
|
262 |
|
rosfran@41
|
263 |
return ret;
|
rosfran@41
|
264 |
|
rosfran@41
|
265 |
}
|
rosfran@41
|
266 |
|
rosfran@115
|
267 |
static gboolean
|
melunko@112
|
268 |
gmyth_connect_to_backend (GMythFileTransfer *transfer)
|
rosfran@41
|
269 |
{
|
renatofilho@143
|
270 |
GString *base_str = NULL;
|
rosfran@177
|
271 |
GString *hostname = NULL;
|
melunko@158
|
272 |
GMythStringList *strlist = NULL;
|
renatofilho@143
|
273 |
gboolean ret = TRUE;
|
rosfran@41
|
274 |
|
melunko@112
|
275 |
g_return_val_if_fail (transfer != NULL, FALSE );
|
rosfran@41
|
276 |
|
renatofilho@143
|
277 |
base_str = g_string_new ("");
|
rosfran@41
|
278 |
|
melunko@112
|
279 |
/* Creates the control socket */
|
renatofilho@143
|
280 |
if (transfer->control_sock != NULL) {
|
renatofilho@143
|
281 |
g_object_unref (transfer->control_sock);
|
renatofilho@143
|
282 |
transfer->control_sock = NULL;
|
renatofilho@143
|
283 |
}
|
renatofilho@143
|
284 |
|
melunko@276
|
285 |
transfer->control_sock = gmyth_socket_new();
|
rosfran@41
|
286 |
|
melunko@112
|
287 |
// Connects the socket, send Mythtv ANN command and verify Mythtv protocol version
|
melunko@112
|
288 |
if (!gmyth_socket_connect_to_backend (transfer->control_sock,
|
renatofilho@187
|
289 |
transfer->backend_info->hostname, transfer->backend_info->port, TRUE)) {
|
rosfran@41
|
290 |
|
melunko@112
|
291 |
g_object_unref (transfer->control_sock);
|
melunko@112
|
292 |
transfer->control_sock = NULL;
|
melunko@112
|
293 |
return FALSE;
|
melunko@112
|
294 |
}
|
melunko@112
|
295 |
|
melunko@112
|
296 |
/* Creates the data socket */
|
renatofilho@143
|
297 |
if (transfer->sock != NULL) {
|
renatofilho@143
|
298 |
g_object_unref (transfer->sock);
|
renatofilho@143
|
299 |
transfer->sock = NULL;
|
renatofilho@143
|
300 |
}
|
renatofilho@143
|
301 |
|
melunko@276
|
302 |
transfer->sock = gmyth_socket_new ();
|
renatofilho@187
|
303 |
gmyth_socket_connect (transfer->sock, transfer->backend_info->hostname, transfer->backend_info->port);
|
rosfran@41
|
304 |
|
melunko@112
|
305 |
strlist = gmyth_string_list_new();
|
melunko@112
|
306 |
hostname = gmyth_socket_get_local_hostname();
|
renatofilho@131
|
307 |
gmyth_debug( "[%s] MythTV version (from backend) = %d.\n", __FUNCTION__, transfer->control_sock->mythtv_version );
|
rosfran@115
|
308 |
if ( transfer->control_sock->mythtv_version > 26 )
|
rosfran@115
|
309 |
g_string_printf( base_str, "ANN FileTransfer %s 1 -1", hostname->str);
|
rosfran@115
|
310 |
else
|
rosfran@115
|
311 |
g_string_printf( base_str, "ANN FileTransfer %s", hostname->str);
|
rosfran@97
|
312 |
|
melunko@112
|
313 |
gmyth_string_list_append_string (strlist, base_str );
|
renatofilho@191
|
314 |
gmyth_string_list_append_char_array (strlist, transfer->filename);
|
rosfran@41
|
315 |
|
melunko@112
|
316 |
gmyth_socket_write_stringlist (transfer->sock, strlist );
|
melunko@112
|
317 |
gmyth_socket_read_stringlist (transfer->sock, strlist );
|
rosfran@101
|
318 |
|
melunko@112
|
319 |
/* file identification used in future file transfer requests to backend */
|
melunko@112
|
320 |
transfer->file_id = gmyth_string_list_get_int( strlist, 1 );
|
rosfran@41
|
321 |
|
melunko@112
|
322 |
/* Myth URI stream file size - decoded using two 8-bytes sequences (64 bits/long long types) */
|
melunko@112
|
323 |
transfer->filesize = gmyth_util_decode_long_long( strlist, 2 );
|
rosfran@41
|
324 |
|
renatofilho@131
|
325 |
gmyth_debug ( "[%s] ***** Received: recordernum = %d, filesize = %" G_GUINT64_FORMAT "\n", __FUNCTION__,
|
melunko@112
|
326 |
transfer->file_id, transfer->filesize );
|
rosfran@41
|
327 |
|
melunko@112
|
328 |
if (transfer->filesize < 0 ) {
|
renatofilho@131
|
329 |
gmyth_debug ( "[%s] Got filesize equals to %llu is lesser than 0 [invalid stream file]\n", __FUNCTION__, transfer->filesize );
|
renatofilho@143
|
330 |
g_object_unref (transfer->sock);
|
melunko@112
|
331 |
transfer->sock = NULL;
|
renatofilho@143
|
332 |
ret = FALSE;
|
renatofilho@143
|
333 |
goto cleanup;
|
rosfran@41
|
334 |
}
|
melunko@112
|
335 |
|
renatofilho@143
|
336 |
cleanup:
|
rosfran@41
|
337 |
|
rosfran@41
|
338 |
if ( strlist != NULL )
|
rosfran@41
|
339 |
g_object_unref( strlist );
|
rosfran@41
|
340 |
|
melunko@112
|
341 |
g_string_free (base_str, TRUE);
|
melunko@112
|
342 |
g_string_free (hostname, TRUE);
|
rosfran@41
|
343 |
|
renatofilho@143
|
344 |
return ret;
|
rosfran@41
|
345 |
}
|
rosfran@41
|
346 |
|
rosfran@233
|
347 |
void
|
rosfran@233
|
348 |
gmyth_file_transfer_emit_program_info_changed_signal ( GMythFileTransfer *transfer, gint msg_code,
|
rosfran@233
|
349 |
gpointer live_tv )
|
rosfran@233
|
350 |
{
|
rosfran@233
|
351 |
/*
|
rosfran@233
|
352 |
g_signal_emit_by_name ( G_OBJECT(transfer),
|
rosfran@233
|
353 |
"program-info-changed",
|
rosfran@233
|
354 |
msg_code, live_tv );
|
rosfran@233
|
355 |
*/
|
rosfran@233
|
356 |
|
rosfran@233
|
357 |
gmyth_debug( "Calling signal handler... [FILE_TRANSFER]" );
|
rosfran@233
|
358 |
|
rosfran@233
|
359 |
g_signal_emit ( transfer,
|
rosfran@233
|
360 |
GMYTH_FILE_TRANSFER_GET_CLASS (transfer)->program_info_changed_handler_signal_id,
|
rosfran@233
|
361 |
0, /* details */
|
rosfran@233
|
362 |
msg_code, live_tv );
|
rosfran@233
|
363 |
|
rosfran@233
|
364 |
}
|
rosfran@233
|
365 |
|
melunko@112
|
366 |
gboolean
|
melunko@112
|
367 |
gmyth_file_transfer_is_open (GMythFileTransfer *transfer)
|
rosfran@41
|
368 |
{
|
melunko@112
|
369 |
GMythStringList *strlist;
|
melunko@112
|
370 |
GString *query;
|
rosfran@41
|
371 |
|
melunko@112
|
372 |
g_return_val_if_fail (transfer->control_sock != NULL, FALSE);
|
melunko@112
|
373 |
g_return_val_if_fail (transfer->sock != NULL, FALSE);
|
rosfran@41
|
374 |
|
melunko@112
|
375 |
strlist = gmyth_string_list_new();
|
melunko@112
|
376 |
query = g_string_new (GMYTHTV_QUERY_HEADER);
|
melunko@112
|
377 |
g_string_append_printf (query, "%d", transfer->file_id );
|
rosfran@41
|
378 |
|
melunko@112
|
379 |
gmyth_string_list_append_string (strlist, query );
|
melunko@112
|
380 |
gmyth_string_list_append_char_array( strlist, "IS_OPEN" );
|
rosfran@41
|
381 |
|
melunko@112
|
382 |
gmyth_socket_write_stringlist( transfer->control_sock, strlist );
|
melunko@112
|
383 |
gmyth_socket_read_stringlist( transfer->control_sock, strlist );
|
rosfran@41
|
384 |
|
melunko@112
|
385 |
g_string_free (query, TRUE);
|
melunko@112
|
386 |
g_object_unref (strlist);
|
rosfran@41
|
387 |
|
melunko@112
|
388 |
return ( strlist != NULL && gmyth_string_list_get_int( strlist, 0 ) == 1 );
|
rosfran@41
|
389 |
}
|
rosfran@41
|
390 |
|
melunko@112
|
391 |
void
|
rosfran@41
|
392 |
gmyth_file_transfer_close( GMythFileTransfer *transfer )
|
rosfran@41
|
393 |
{
|
rosfran@41
|
394 |
GMythStringList *strlist;
|
melunko@112
|
395 |
GString *query;
|
rosfran@41
|
396 |
|
melunko@112
|
397 |
g_return_if_fail (transfer->control_sock != NULL);
|
rosfran@41
|
398 |
|
rosfran@41
|
399 |
strlist = gmyth_string_list_new( );
|
melunko@112
|
400 |
query = g_string_new (GMYTHTV_QUERY_HEADER);
|
melunko@112
|
401 |
g_string_append_printf( query, "%d", transfer->file_id);
|
rosfran@41
|
402 |
|
melunko@112
|
403 |
gmyth_string_list_append_string( strlist, query );
|
rosfran@41
|
404 |
gmyth_string_list_append_char_array( strlist, "DONE" );
|
rosfran@41
|
405 |
|
melunko@112
|
406 |
if ( gmyth_socket_sendreceive_stringlist(transfer->control_sock, strlist) <= 0 ) {
|
melunko@112
|
407 |
// fixme: time out???
|
rosfran@41
|
408 |
g_printerr( "Remote file timeout.\n" );
|
rosfran@41
|
409 |
}
|
rosfran@41
|
410 |
|
melunko@112
|
411 |
g_string_free (query, TRUE);
|
melunko@112
|
412 |
g_object_unref (strlist);
|
melunko@112
|
413 |
|
melunko@112
|
414 |
if (transfer->sock) {
|
rosfran@41
|
415 |
g_object_unref( transfer->sock );
|
rosfran@41
|
416 |
transfer->sock = NULL;
|
rosfran@41
|
417 |
}
|
rosfran@41
|
418 |
|
melunko@112
|
419 |
if (transfer->control_sock) {
|
rosfran@41
|
420 |
g_object_unref( transfer->control_sock );
|
rosfran@41
|
421 |
transfer->control_sock = NULL;
|
rosfran@41
|
422 |
}
|
rosfran@177
|
423 |
|
rosfran@41
|
424 |
}
|
rosfran@41
|
425 |
|
rosfran@41
|
426 |
gint64
|
rosfran@41
|
427 |
gmyth_file_transfer_seek(GMythFileTransfer *transfer, guint64 pos, gint whence)
|
rosfran@41
|
428 |
{
|
melunko@112
|
429 |
GMythStringList *strlist = gmyth_string_list_new();
|
melunko@112
|
430 |
GString *query;
|
rosfran@41
|
431 |
|
melunko@112
|
432 |
g_return_val_if_fail (transfer->sock != NULL, -1);
|
melunko@112
|
433 |
g_return_val_if_fail (transfer->control_sock != NULL, -1);
|
rosfran@41
|
434 |
|
melunko@112
|
435 |
strlist = gmyth_string_list_new();
|
melunko@112
|
436 |
query = g_string_new (GMYTHTV_QUERY_HEADER);
|
melunko@112
|
437 |
g_string_append_printf (query, "%d", transfer->file_id);
|
rosfran@97
|
438 |
|
rosfran@97
|
439 |
myth_control_acquire_context( TRUE );
|
rosfran@41
|
440 |
|
melunko@112
|
441 |
gmyth_string_list_append_string( strlist, query );
|
rosfran@41
|
442 |
gmyth_string_list_append_char_array( strlist, "SEEK" );
|
rosfran@41
|
443 |
gmyth_string_list_append_uint64( strlist, pos );
|
rosfran@41
|
444 |
|
rosfran@41
|
445 |
gmyth_string_list_append_int( strlist, whence );
|
rosfran@41
|
446 |
|
rosfran@41
|
447 |
if (pos > 0 )
|
rosfran@41
|
448 |
gmyth_string_list_append_uint64( strlist, pos );
|
rosfran@41
|
449 |
else
|
rosfran@41
|
450 |
gmyth_string_list_append_uint64( strlist, transfer->readposition );
|
rosfran@41
|
451 |
|
rosfran@41
|
452 |
gmyth_socket_sendreceive_stringlist( transfer->control_sock, strlist );
|
rosfran@41
|
453 |
|
rosfran@41
|
454 |
gint64 retval = gmyth_string_list_get_int64(strlist, 0);
|
rosfran@41
|
455 |
transfer->readposition = retval;
|
renatofilho@131
|
456 |
gmyth_debug ( "[%s] got reading position pointer from the streaming = %lld\n",
|
rosfran@41
|
457 |
__FUNCTION__, retval );
|
rosfran@41
|
458 |
|
rosfran@97
|
459 |
myth_control_release_context( );
|
rosfran@41
|
460 |
|
rosfran@41
|
461 |
return retval;
|
rosfran@41
|
462 |
}
|
rosfran@41
|
463 |
|
rosfran@97
|
464 |
static gboolean
|
rosfran@221
|
465 |
myth_control_acquire_context( gboolean do_wait )
|
rosfran@97
|
466 |
{
|
rosfran@97
|
467 |
gboolean ret = TRUE;
|
rosfran@140
|
468 |
//guint max_iter = 50;
|
rosfran@97
|
469 |
|
rosfran@97
|
470 |
//g_mutex_lock( mutex );
|
rosfran@97
|
471 |
|
rosfran@97
|
472 |
//while ( !has_io_access )
|
rosfran@97
|
473 |
// g_cond_wait( io_watcher_cond, mutex );
|
rosfran@97
|
474 |
|
rosfran@97
|
475 |
//has_io_access = FALSE;
|
rosfran@97
|
476 |
|
rosfran@97
|
477 |
//myth_control_acquire_context (FALSE);
|
rosfran@140
|
478 |
|
rosfran@140
|
479 |
/*
|
rosfran@97
|
480 |
if ( do_wait ) {
|
rosfran@120
|
481 |
while ( --max_iter > 0 && !g_main_context_wait( io_watcher_context, io_watcher_cond, mutex ) )
|
rosfran@97
|
482 |
ret = FALSE;
|
rosfran@97
|
483 |
} else if ( !g_main_context_acquire( io_watcher_context ) )
|
rosfran@97
|
484 |
ret = FALSE;
|
rosfran@140
|
485 |
*/
|
rosfran@140
|
486 |
|
rosfran@140
|
487 |
//g_static_mutex_lock( &st_mutex );
|
rosfran@97
|
488 |
|
rosfran@97
|
489 |
return ret;
|
rosfran@97
|
490 |
}
|
rosfran@97
|
491 |
|
rosfran@97
|
492 |
static gboolean
|
rosfran@97
|
493 |
myth_control_release_context( )
|
rosfran@97
|
494 |
{
|
rosfran@97
|
495 |
gboolean ret = TRUE;
|
rosfran@140
|
496 |
|
rosfran@140
|
497 |
//g_static_mutex_unlock( &st_mutex );
|
rosfran@97
|
498 |
|
rosfran@140
|
499 |
//g_main_context_release( io_watcher_context );
|
rosfran@140
|
500 |
|
rosfran@140
|
501 |
//g_main_context_wakeup( io_watcher_context );
|
rosfran@97
|
502 |
|
rosfran@97
|
503 |
//has_io_access = TRUE;
|
rosfran@97
|
504 |
|
rosfran@97
|
505 |
//g_cond_broadcast( io_watcher_cond );
|
rosfran@97
|
506 |
|
rosfran@140
|
507 |
//g_mutex_unlock( mutex );
|
rosfran@97
|
508 |
|
rosfran@97
|
509 |
return ret;
|
rosfran@97
|
510 |
}
|
rosfran@97
|
511 |
|
rosfran@41
|
512 |
gint
|
rosfran@115
|
513 |
gmyth_file_transfer_read(GMythFileTransfer *transfer, GByteArray *data, gint size, gboolean read_unlimited)
|
rosfran@41
|
514 |
{
|
rosfran@177
|
515 |
gint bytes_sent = 0;
|
rosfran@41
|
516 |
gsize bytes_read = 0;
|
melunko@112
|
517 |
gsize total_read = 0;
|
melunko@112
|
518 |
|
melunko@112
|
519 |
GError *error = NULL;
|
melunko@112
|
520 |
|
rosfran@41
|
521 |
GIOChannel *io_channel;
|
rosfran@41
|
522 |
GIOChannel *io_channel_control;
|
rosfran@41
|
523 |
|
rosfran@41
|
524 |
GIOCondition io_cond;
|
rosfran@41
|
525 |
GIOCondition io_cond_control;
|
rosfran@220
|
526 |
GIOStatus io_status = G_IO_STATUS_NORMAL, io_status_control = G_IO_STATUS_NORMAL;
|
rosfran@220
|
527 |
|
rosfran@220
|
528 |
gboolean ret = TRUE;
|
rosfran@41
|
529 |
|
melunko@112
|
530 |
GString *query;
|
rosfran@115
|
531 |
|
rosfran@41
|
532 |
g_return_val_if_fail ( data != NULL, -2 );
|
rosfran@101
|
533 |
|
rosfran@41
|
534 |
io_channel = transfer->sock->sd_io_ch;
|
rosfran@41
|
535 |
io_channel_control = transfer->control_sock->sd_io_ch;
|
rosfran@41
|
536 |
|
rosfran@41
|
537 |
io_status = g_io_channel_set_encoding( io_channel, NULL, &error );
|
rosfran@41
|
538 |
if ( io_status == G_IO_STATUS_NORMAL )
|
renatofilho@131
|
539 |
gmyth_debug ( "[%s] Setting encoding to binary data socket).\n", __FUNCTION__ );
|
rosfran@41
|
540 |
|
rosfran@41
|
541 |
io_cond = g_io_channel_get_buffer_condition( io_channel );
|
rosfran@41
|
542 |
|
rosfran@41
|
543 |
io_cond_control = g_io_channel_get_buffer_condition( io_channel );
|
melunko@112
|
544 |
if ( transfer->sock == NULL || ( io_status == G_IO_STATUS_ERROR ) ) {
|
rosfran@41
|
545 |
g_printerr( "gmyth_file_transfer_read(): Called with no raw socket.\n" );
|
rosfran@177
|
546 |
//exit(0); // fixme remove this
|
melunko@112
|
547 |
return -1;
|
rosfran@41
|
548 |
}
|
rosfran@41
|
549 |
|
melunko@112
|
550 |
if ( transfer->control_sock == NULL || ( io_status_control == G_IO_STATUS_ERROR ) ) {
|
rosfran@41
|
551 |
g_printerr( "gmyth_file_transfer_read(): Called with no control socket.\n" );
|
rosfran@177
|
552 |
//exit(0); // fixme remove this
|
melunko@112
|
553 |
return -1;
|
rosfran@41
|
554 |
}
|
rosfran@41
|
555 |
|
melunko@112
|
556 |
query = g_string_new (GMYTHTV_QUERY_HEADER);
|
melunko@112
|
557 |
g_string_append_printf ( query, "%d", transfer->file_id );
|
renatofilho@131
|
558 |
gmyth_debug ("[%s] Transfer_query = %s\n", __FUNCTION__, query->str );
|
rosfran@41
|
559 |
|
rosfran@177
|
560 |
/* send requests to the maximum number of REQUEST_BLOCK messages */
|
rosfran@197
|
561 |
guint max_tries = 5;
|
rosfran@177
|
562 |
|
rosfran@140
|
563 |
myth_control_acquire_context( TRUE );
|
rosfran@140
|
564 |
|
rosfran@177
|
565 |
while (total_read == 0 && --max_tries > 0) {
|
renatofilho@143
|
566 |
GMythStringList *strlist = gmyth_string_list_new();
|
melunko@112
|
567 |
|
melunko@112
|
568 |
gmyth_string_list_append_char_array( strlist, query->str );
|
rosfran@101
|
569 |
gmyth_string_list_append_char_array( strlist, "REQUEST_BLOCK" );
|
melunko@112
|
570 |
gmyth_string_list_append_int( strlist, size );
|
melunko@112
|
571 |
|
melunko@112
|
572 |
// Request the block to the backend
|
melunko@112
|
573 |
gmyth_socket_write_stringlist( transfer->control_sock, strlist );
|
melunko@112
|
574 |
|
melunko@112
|
575 |
// Receives the backand answer
|
rosfran@101
|
576 |
gmyth_socket_read_stringlist( transfer->control_sock, strlist );
|
rosfran@101
|
577 |
|
rosfran@220
|
578 |
if ( strlist != NULL && gmyth_string_list_length( strlist ) > 0 )
|
rosfran@220
|
579 |
{
|
renatofilho@143
|
580 |
bytes_sent = gmyth_string_list_get_int( strlist, 0 ); // -1 on backend error
|
renatofilho@143
|
581 |
gmyth_debug ( "[%s] got SENT buffer message = %d\n", __FUNCTION__, bytes_sent );
|
rosfran@89
|
582 |
|
rosfran@237
|
583 |
if ( bytes_sent >= 0 )
|
rosfran@220
|
584 |
{
|
rosfran@220
|
585 |
gchar *data_buffer = g_new0 ( gchar, bytes_sent );
|
rosfran@220
|
586 |
while ( 0 != bytes_sent )
|
rosfran@220
|
587 |
{
|
rosfran@220
|
588 |
io_status = g_io_channel_read_chars( io_channel, data_buffer + total_read,
|
rosfran@220
|
589 |
(gsize) bytes_sent, &bytes_read, &error );
|
rosfran@220
|
590 |
|
rosfran@220
|
591 |
total_read += bytes_read;
|
rosfran@220
|
592 |
bytes_sent -= bytes_read;
|
rosfran@220
|
593 |
|
rosfran@220
|
594 |
/* append new data to the increasing byte array */
|
rosfran@220
|
595 |
data = g_byte_array_append (data, (const guint8*)data_buffer, bytes_read);
|
rosfran@220
|
596 |
gmyth_debug ("Total transfer data read: %d\n", total_read);
|
rosfran@220
|
597 |
}
|
rosfran@220
|
598 |
g_free (data_buffer);
|
rosfran@220
|
599 |
} /* if */
|
rosfran@302
|
600 |
} else if ( !(transfer->priv != NULL && transfer->priv->livetv != NULL &&
|
rosfran@302
|
601 |
transfer->priv->do_next_program_chain) ) {
|
rosfran@177
|
602 |
total_read = GMYTHTV_FILE_TRANSFER_READ_ERROR;
|
rosfran@177
|
603 |
g_object_unref (strlist);
|
rosfran@302
|
604 |
strlist = NULL;
|
rosfran@177
|
605 |
break;
|
melunko@112
|
606 |
}
|
renatofilho@143
|
607 |
g_object_unref (strlist);
|
renatofilho@143
|
608 |
strlist = NULL;
|
melunko@112
|
609 |
}
|
rosfran@140
|
610 |
|
rosfran@234
|
611 |
if ( bytes_sent == 0 && max_tries == 0 )
|
rosfran@234
|
612 |
{
|
rosfran@234
|
613 |
gmyth_debug( "Trying to move to the next program chain..." );
|
rosfran@234
|
614 |
transfer->priv = GMYTH_FILE_TRANSFER_GET_PRIVATE(transfer);
|
rosfran@234
|
615 |
|
rosfran@234
|
616 |
if ( transfer->priv != NULL && transfer->priv->livetv != NULL &&
|
rosfran@234
|
617 |
transfer->priv->do_next_program_chain )
|
rosfran@234
|
618 |
{
|
rosfran@234
|
619 |
|
rosfran@234
|
620 |
total_read = GMYTHTV_FILE_TRANSFER_NEXT_PROG_CHAIN;
|
rosfran@234
|
621 |
|
rosfran@234
|
622 |
g_mutex_lock( transfer->mutex );
|
rosfran@234
|
623 |
|
rosfran@234
|
624 |
ret = gmyth_livetv_next_program_chain( transfer->priv->livetv );
|
rosfran@234
|
625 |
|
rosfran@234
|
626 |
g_mutex_unlock( transfer->mutex );
|
rosfran@234
|
627 |
|
rosfran@234
|
628 |
if ( !ret )
|
rosfran@234
|
629 |
gmyth_debug( "Cannot change to the next program chain!" );
|
rosfran@234
|
630 |
else
|
rosfran@234
|
631 |
gmyth_debug( "OK!!! MOVED to the next program chain [%s]!",
|
rosfran@234
|
632 |
(gmyth_tvchain_get_id( transfer->priv->livetv->tvchain ))->str );
|
rosfran@234
|
633 |
}
|
rosfran@234
|
634 |
|
rosfran@234
|
635 |
} /* if */
|
rosfran@234
|
636 |
|
rosfran@140
|
637 |
myth_control_release_context( );
|
renatofilho@143
|
638 |
g_string_free (query, TRUE);
|
rosfran@41
|
639 |
|
renatofilho@143
|
640 |
if ( error != NULL ) {
|
renatofilho@143
|
641 |
gmyth_debug ("Cleaning-up ERROR: %s [msg = %s, code = %d]\n", __FUNCTION__, error->message,
|
renatofilho@143
|
642 |
error->code);
|
renatofilho@143
|
643 |
g_error_free (error);
|
renatofilho@143
|
644 |
}
|
melunko@112
|
645 |
|
rosfran@216
|
646 |
if ( total_read > 0 )
|
rosfran@220
|
647 |
transfer->readposition += total_read;
|
rosfran@177
|
648 |
|
melunko@112
|
649 |
return total_read;
|
rosfran@41
|
650 |
}
|
rosfran@41
|
651 |
|
rosfran@216
|
652 |
static void
|
rosfran@216
|
653 |
gmyth_file_transfer_program_info_changed( GMythFileTransfer *transfer,
|
rosfran@232
|
654 |
gint msg_code, gpointer livetv_transfer )
|
rosfran@216
|
655 |
{
|
rosfran@233
|
656 |
GMythLiveTV *livetv = GMYTH_LIVETV( livetv_transfer );
|
rosfran@216
|
657 |
|
rosfran@220
|
658 |
gmyth_debug( "Program info changed! ( file transfer orig. = %p, ptr. = [%s], user data = [%s] )", transfer,
|
rosfran@220
|
659 |
livetv_transfer != NULL ? "[NOT NULL]" : "[NULL]", livetv != NULL ? "[NOT NULL]" : "[NULL]" );
|
rosfran@216
|
660 |
|
rosfran@220
|
661 |
if ( livetv != NULL && transfer == livetv->file_transfer )
|
rosfran@216
|
662 |
{
|
rosfran@216
|
663 |
gmyth_debug( "YES, the requested program info movement on the LiveTV transfer is authentical!" );
|
rosfran@216
|
664 |
}
|
rosfran@216
|
665 |
|
rosfran@216
|
666 |
transfer->priv = GMYTH_FILE_TRANSFER_GET_PRIVATE(transfer);
|
rosfran@216
|
667 |
|
rosfran@216
|
668 |
transfer->priv->livetv = livetv;
|
rosfran@216
|
669 |
transfer->priv->do_next_program_chain = TRUE;
|
rosfran@216
|
670 |
|
rosfran@216
|
671 |
//g_object_unref( transfer );
|
rosfran@216
|
672 |
}
|
rosfran@216
|
673 |
|
melunko@112
|
674 |
gboolean
|
rosfran@41
|
675 |
gmyth_file_transfer_settimeout( GMythFileTransfer *transfer, gboolean fast )
|
rosfran@41
|
676 |
{
|
rosfran@41
|
677 |
|
rosfran@41
|
678 |
GMythStringList *strlist = NULL;
|
rosfran@41
|
679 |
|
melunko@112
|
680 |
g_return_val_if_fail (transfer->sock != NULL, FALSE);
|
melunko@112
|
681 |
g_return_val_if_fail (transfer->control_sock != NULL, FALSE);
|
rosfran@41
|
682 |
|
melunko@112
|
683 |
// if ( transfer->timeoutisfast == fast )
|
melunko@112
|
684 |
// return;
|
rosfran@41
|
685 |
|
rosfran@41
|
686 |
strlist = gmyth_string_list_new();
|
melunko@112
|
687 |
gmyth_string_list_append_char_array( strlist, GMYTHTV_QUERY_HEADER );
|
rosfran@41
|
688 |
gmyth_string_list_append_char_array( strlist, "SET_TIMEOUT" );
|
rosfran@41
|
689 |
gmyth_string_list_append_int( strlist, fast );
|
rosfran@41
|
690 |
|
rosfran@41
|
691 |
gmyth_socket_write_stringlist( transfer->control_sock, strlist );
|
rosfran@41
|
692 |
gmyth_socket_read_stringlist( transfer->control_sock, strlist );
|
rosfran@41
|
693 |
|
melunko@112
|
694 |
// transfer->timeoutisfast = fast;
|
rosfran@41
|
695 |
|
melunko@112
|
696 |
return TRUE;
|
rosfran@41
|
697 |
}
|
rosfran@41
|
698 |
|
melunko@117
|
699 |
|
melunko@117
|
700 |
guint64
|
melunko@117
|
701 |
gmyth_file_transfer_get_filesize (GMythFileTransfer *transfer)
|
melunko@117
|
702 |
{
|
renatofilho@206
|
703 |
g_return_val_if_fail (transfer != NULL, 0);
|
melunko@117
|
704 |
return transfer->filesize;
|
melunko@117
|
705 |
}
|