]> git.sesse.net Git - vlc/blob - modules/access/mms/mmsh.c
* modules/access/http.c,modules/access/mmsh,modules/demux/livedotcom.cpp: Disable...
[vlc] / modules / access / mms / mmsh.c
1 /*****************************************************************************
2  * mmsh.c:
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 #include "vlc_playlist.h"
33
34 #include "network.h"
35 #include "asf.h"
36 #include "buffer.h"
37
38 #include "mms.h"
39 #include "mmsh.h"
40
41 /* TODO:
42  *  - http_proxy
43  *  - authentication
44  */
45
46 /*****************************************************************************
47  * Local prototypes
48  *****************************************************************************/
49 int  E_(MMSHOpen)  ( access_t * );
50 void E_(MMSHClose) ( access_t * );
51
52 static int  Read( access_t *, uint8_t *, int );
53 static int  ReadRedirect( access_t *, uint8_t *, int );
54 static int  Seek( access_t *, int64_t );
55 static int  Control( access_t *, int, va_list );
56
57 static int  Describe( access_t  *, char **ppsz_location );
58 static int  Start( access_t *, int64_t );
59 static void Stop( access_t * );
60 static int  GetPacket( access_t *, chunk_t * );
61
62 /****************************************************************************
63  * Open: connect to ftp server and ask for file
64  ****************************************************************************/
65 int E_(MMSHOpen)( access_t *p_access )
66 {
67     access_sys_t    *p_sys;
68     char            *psz_location = NULL;
69
70     /* init p_sys */
71
72     /* Set up p_access */
73     p_access->pf_read = Read;
74     p_access->pf_block = NULL;
75     p_access->pf_control = Control;
76     p_access->pf_seek = Seek;
77     p_access->info.i_update = 0;
78     p_access->info.i_size = 0;
79     p_access->info.i_pos = 0;
80     p_access->info.b_eof = VLC_FALSE;
81     p_access->info.i_title = 0;
82     p_access->info.i_seekpoint = 0;
83     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
84     memset( p_sys, 0, sizeof( access_sys_t ) );
85     p_sys->i_proto= MMS_PROTO_HTTP;
86     p_sys->fd     = -1;
87     p_sys->i_start= 0;
88
89     /* open a tcp connection */
90     vlc_UrlParse( &p_sys->url, p_access->psz_path, 0 );
91     if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
92     {
93         msg_Err( p_access, "invalid host" );
94         vlc_UrlClean( &p_sys->url );
95         free( p_sys );
96         return VLC_EGENERIC;
97     }
98     if( p_sys->url.i_port <= 0 )
99         p_sys->url.i_port = 80;
100
101     if( Describe( p_access, &psz_location ) )
102     {
103         vlc_UrlClean( &p_sys->url );
104         free( p_sys );
105         return VLC_EGENERIC;
106     }
107     /* Handle redirection */
108     if( psz_location && *psz_location )
109     {
110         playlist_t * p_playlist = vlc_object_find( p_access, VLC_OBJECT_PLAYLIST, FIND_PARENT );
111
112         msg_Dbg( p_access, "redirection to %s", psz_location );
113
114         if( !p_playlist )
115         {
116             msg_Err( p_access, "redirection failed: can't find playlist" );
117             free( psz_location );
118             return VLC_EGENERIC;
119         }
120         p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
121         playlist_Add( p_playlist, psz_location, psz_location,
122                       PLAYLIST_INSERT | PLAYLIST_GO,
123                       p_playlist->i_index + 1 );
124         vlc_object_release( p_playlist );
125
126         free( psz_location );
127
128         p_access->pf_read = ReadRedirect;
129         return VLC_SUCCESS;
130     }
131
132     /* Start playing */
133     if( Start( p_access, 0 ) )
134     {
135         msg_Err( p_access, "cannot start stream" );
136         free( p_sys->p_header );
137         vlc_UrlClean( &p_sys->url );
138         free( p_sys );
139         return VLC_EGENERIC;
140     }
141
142     if( !p_sys->b_broadcast )
143     {
144         p_access->info.i_size = p_sys->asfh.i_file_size;
145     }
146
147     return VLC_SUCCESS;
148 }
149
150 /*****************************************************************************
151  * Close: free unused data structures
152  *****************************************************************************/
153 void E_( MMSHClose )( access_t *p_access )
154 {
155     access_sys_t *p_sys = p_access->p_sys;
156
157     Stop( p_access );
158     free( p_sys );
159 }
160
161 /*****************************************************************************
162  * Control:
163  *****************************************************************************/
164 static int Control( access_t *p_access, int i_query, va_list args )
165 {
166     access_sys_t *p_sys = p_access->p_sys;
167     vlc_bool_t   *pb_bool;
168     int          *pi_int;
169     int64_t      *pi_64;
170     int          i_int;
171
172     switch( i_query )
173     {
174         /* */
175         case ACCESS_CAN_SEEK:
176             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
177             *pb_bool = !p_sys->b_broadcast;
178             break;
179         case ACCESS_CAN_FASTSEEK:
180         case ACCESS_CAN_PAUSE:
181         case ACCESS_CAN_CONTROL_PACE:
182             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
183
184 #if 0       /* Disable for now until we have a clock synchro algo
185              * which works with something else than MPEG over UDP */
186             *pb_bool = VLC_FALSE;
187 #endif
188             *pb_bool = VLC_TRUE;
189             break;
190
191         /* */
192         case ACCESS_GET_MTU:
193             pi_int = (int*)va_arg( args, int * );
194             *pi_int = 3 * p_sys->asfh.i_min_data_packet_size;
195             break;
196
197         case ACCESS_GET_PTS_DELAY:
198             pi_64 = (int64_t*)va_arg( args, int64_t * );
199             *pi_64 = (int64_t)var_GetInteger( p_access, "mms-caching" ) * I64C(1000);
200             break;
201
202         case ACCESS_GET_PRIVATE_ID_STATE:
203             i_int = (int)va_arg( args, int );
204             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t );
205
206             if( i_int < 0 || i_int > 127 )
207                 return VLC_EGENERIC;
208             *pb_bool =  p_sys->asfh.stream[i_int].i_selected ? VLC_TRUE : VLC_FALSE;
209             break;
210
211         /* */
212         case ACCESS_SET_PAUSE_STATE:
213         case ACCESS_GET_TITLE_INFO:
214         case ACCESS_SET_TITLE:
215         case ACCESS_SET_SEEKPOINT:
216         case ACCESS_SET_PRIVATE_ID_STATE:
217             return VLC_EGENERIC;
218
219         default:
220             msg_Warn( p_access, "unimplemented query in control" );
221             return VLC_EGENERIC;
222
223     }
224     return VLC_SUCCESS;
225 }
226
227 /*****************************************************************************
228  * Seek: try to go at the right place
229  *****************************************************************************/
230 static int Seek( access_t *p_access, int64_t i_pos )
231 {
232     access_sys_t *p_sys = p_access->p_sys;
233     chunk_t      ck;
234     off_t        i_offset;
235     off_t        i_packet;
236
237     msg_Dbg( p_access, "seeking to "I64Fd, i_pos );
238
239     i_packet = ( i_pos - p_sys->i_header ) / p_sys->asfh.i_min_data_packet_size;
240     i_offset = ( i_pos - p_sys->i_header ) % p_sys->asfh.i_min_data_packet_size;
241
242     Stop( p_access );
243     Start( p_access, i_packet * p_sys->asfh.i_min_data_packet_size );
244
245     while( !p_access->b_die )
246     {
247         if( GetPacket( p_access, &ck ) )
248             break;
249
250         /* skip headers */
251         if( ck.i_type != 0x4824 )
252             break;
253
254         msg_Warn( p_access, "skipping header" );
255     }
256
257     p_access->info.i_pos = i_pos;
258     p_access->info.b_eof = VLC_FALSE;
259     p_sys->i_packet_used += i_offset;
260
261     return VLC_SUCCESS;
262 }
263
264 /*****************************************************************************
265  * Read:
266  *****************************************************************************/
267 static int ReadRedirect( access_t *p_access, uint8_t *p, int i_len )
268 {
269     return 0;
270 }
271
272 /*****************************************************************************
273  * Read:
274  *****************************************************************************/
275 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
276 {
277     access_sys_t *p_sys = p_access->p_sys;
278     size_t       i_copy;
279     size_t       i_data = 0;
280
281     if( p_access->info.b_eof )
282         return 0;
283
284     while( i_data < i_len )
285     {
286         if( p_access->info.i_pos < p_sys->i_start + p_sys->i_header )
287         {
288             int i_offset = p_access->info.i_pos - p_sys->i_start;
289             i_copy = __MIN( p_sys->i_header - i_offset, i_len - i_data );
290             memcpy( &p_buffer[i_data], &p_sys->p_header[i_offset], i_copy );
291
292             i_data += i_copy;
293             p_access->info.i_pos += i_copy;
294         }
295         else if( p_sys->i_packet_used < p_sys->i_packet_length )
296         {
297             i_copy = __MIN( p_sys->i_packet_length - p_sys->i_packet_used,
298                             i_len - i_data );
299
300             memcpy( &p_buffer[i_data],
301                     &p_sys->p_packet[p_sys->i_packet_used],
302                     i_copy );
303
304             i_data += i_copy;
305             p_sys->i_packet_used += i_copy;
306             p_access->info.i_pos += i_copy;
307         }
308         else if( p_sys->i_packet_length > 0 &&
309                  (int)p_sys->i_packet_used < p_sys->asfh.i_min_data_packet_size )
310         {
311             i_copy = __MIN( p_sys->asfh.i_min_data_packet_size - p_sys->i_packet_used,
312                             i_len - i_data );
313
314             memset( &p_buffer[i_data], 0, i_copy );
315
316             i_data += i_copy;
317             p_sys->i_packet_used += i_copy;
318             p_access->info.i_pos += i_copy;
319         }
320         else
321         {
322             chunk_t ck;
323             if( GetPacket( p_access, &ck ) )
324             {
325                 if( ck.i_type == 0x4524 && ck.i_sequence != 0 && p_sys->b_broadcast )
326                 {
327                     char *psz_location = NULL;
328
329                     p_sys->i_start = p_access->info.i_pos;
330
331                     msg_Dbg( p_access, "stoping the stream" );
332                     Stop( p_access );
333
334                     msg_Dbg( p_access, "describe the stream" );
335                     if( Describe( p_access, &psz_location ) )
336                     {
337                         msg_Err( p_access, "describe failed" );
338                         p_access->info.b_eof = VLC_TRUE;
339                         return 0;
340                     }
341                     if( Start( p_access, 0 ) )
342                     {
343                         msg_Err( p_access, "Start failed" );
344                         p_access->info.b_eof = VLC_TRUE;
345                         return 0;
346                     }
347                 }
348                 else
349                 {
350                     p_access->info.b_eof = VLC_TRUE;
351                     return 0;
352                 }
353             }
354             if( ck.i_type != 0x4424 )
355             {
356                 p_sys->i_packet_used = 0;
357                 p_sys->i_packet_length = 0;
358             }
359         }
360     }
361
362     return( i_data );
363 }
364
365 /*****************************************************************************
366  * Describe:
367  *****************************************************************************/
368 static int Describe( access_t  *p_access, char **ppsz_location )
369 {
370     access_sys_t *p_sys = p_access->p_sys;
371     char         *psz_location = NULL;
372     char         *psz;
373     int          i_code;
374
375     /* Reinit context */
376     p_sys->b_broadcast = VLC_TRUE;
377     p_sys->i_request_context = 1;
378     p_sys->i_packet_sequence = 0;
379     p_sys->i_packet_used = 0;
380     p_sys->i_packet_length = 0;
381     p_sys->p_packet = NULL;
382     E_( GenerateGuid )( &p_sys->guid );
383
384     if( ( p_sys->fd = net_OpenTCP( p_access, p_sys->url.psz_host,
385                                             p_sys->url.i_port ) ) < 0 )
386     {
387         msg_Err( p_access, "cannot connect to %s:%d", p_sys->url.psz_host, p_sys->url.i_port );
388         goto error;
389     }
390
391     /* send first request */
392     net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
393                 "GET %s HTTP/1.0\r\n"
394                 "Accept: */*\r\n"
395                 "User-Agent: NSPlayer/4.1.0.3856\r\n"
396                 "Host: %s:%d\r\n"
397                 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=0:0,request-context=%d,max-duration=0\r\n"
398                 "Pragma: xClientGUID={"GUID_FMT"}\r\n"
399                 "Connection: Close\r\n",
400                 ( p_sys->url.psz_path == NULL || *p_sys->url.psz_path == '\0' ) ? "/" : p_sys->url.psz_path,
401                 p_sys->url.psz_host, p_sys->url.i_port,
402                 p_sys->i_request_context++,
403                 GUID_PRINT( p_sys->guid ) );
404
405     if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "\r\n" ) < 0 )
406     {
407         msg_Err( p_access, "failed to send request" );
408         goto error;
409     }
410
411     /* Receive the http header */
412     if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL ) ) == NULL )
413     {
414         msg_Err( p_access, "failed to read answer" );
415         goto error;
416     }
417     if( strncmp( psz, "HTTP/1.", 7 ) )
418     {
419         msg_Err( p_access, "invalid HTTP reply '%s'", psz );
420         free( psz );
421         goto error;
422     }
423     i_code = atoi( &psz[9] );
424     if( i_code >= 400 )
425     {
426         msg_Err( p_access, "error: %s", psz );
427         free( psz );
428         goto error;
429     }
430
431     msg_Dbg( p_access, "HTTP reply '%s'", psz );
432     free( psz );
433     for( ;; )
434     {
435         char *psz = net_Gets( p_access, p_sys->fd, NULL );
436         char *p;
437
438         if( psz == NULL )
439         {
440             msg_Err( p_access, "failed to read answer" );
441             goto error;
442         }
443
444         if( *psz == '\0' )
445         {
446             free( psz );
447             break;
448         }
449
450         if( ( p = strchr( psz, ':' ) ) == NULL )
451         {
452             msg_Err( p_access, "malformed header line: %s", psz );
453             free( psz );
454             goto error;
455         }
456         *p++ = '\0';
457         while( *p == ' ' ) p++;
458
459         /* FIXME FIXME test Content-Type to see if it's a plain stream or an
460          * asx FIXME */
461         if( !strcasecmp( psz, "Pragma" ) )
462         {
463             if( strstr( p, "features" ) )
464             {
465                 /* FIXME, it is a bit badly done here ..... */
466                 if( strstr( p, "broadcast" ) )
467                 {
468                     msg_Dbg( p_access, "stream type = broadcast" );
469                     p_sys->b_broadcast = VLC_TRUE;
470                 }
471                 else if( strstr( p, "seekable" ) )
472                 {
473                     msg_Dbg( p_access, "stream type = seekable" );
474                     p_sys->b_broadcast = VLC_FALSE;
475                 }
476                 else
477                 {
478                     msg_Warn( p_access, "unknow stream types (%s)", p );
479                     p_sys->b_broadcast = VLC_FALSE;
480                 }
481             }
482         }
483         else if( !strcasecmp( psz, "Location" ) )
484         {
485             psz_location = strdup( p );
486         }
487
488         free( psz );
489     }
490
491     /* Handle the redirection */
492     if( ( i_code == 301 || i_code == 302 ||
493           i_code == 303 || i_code == 307 ) &&
494         psz_location && *psz_location )
495     {
496         msg_Dbg( p_access, "redirection to %s", psz_location );
497         net_Close( p_sys->fd ); p_sys->fd = -1;
498
499         *ppsz_location = psz_location;
500         return VLC_SUCCESS;
501     }
502
503     /* Read the asf header */
504     p_sys->i_header = 0;
505     p_sys->p_header = NULL;
506     for( ;; )
507     {
508         chunk_t ck;
509         if( GetPacket( p_access, &ck ) ||
510             ck.i_type != 0x4824 )
511         {
512             break;
513         }
514
515         if( ck.i_data > 0 )
516         {
517             p_sys->i_header += ck.i_data;
518             p_sys->p_header = realloc( p_sys->p_header, p_sys->i_header );
519             memcpy( &p_sys->p_header[p_sys->i_header - ck.i_data],
520                     ck.p_data, ck.i_data );
521         }
522     }
523     msg_Dbg( p_access, "complete header size=%d", p_sys->i_header );
524     if( p_sys->i_header <= 0 )
525     {
526         msg_Err( p_access, "header size == 0" );
527         goto error;
528     }
529     /* close this connection */
530     net_Close( p_sys->fd ); p_sys->fd = -1;
531
532     /* *** parse header and get stream and their id *** */
533     /* get all streams properties,
534      *
535      * TODO : stream bitrates properties(optional)
536      *        and bitrate mutual exclusion(optional) */
537     E_( asf_HeaderParse )( &p_sys->asfh,
538                            p_sys->p_header, p_sys->i_header );
539     msg_Dbg( p_access, "packet count=%lld packet size=%d",
540              p_sys->asfh.i_data_packets_count,
541              p_sys->asfh.i_min_data_packet_size );
542
543     E_( asf_StreamSelect)( &p_sys->asfh,
544                            var_CreateGetInteger( p_access, "mms-maxbitrate" ),
545                            var_CreateGetInteger( p_access, "mms-all" ),
546                            var_CreateGetInteger( p_access, "audio" ),
547                            var_CreateGetInteger( p_access, "video" ) );
548
549     return VLC_SUCCESS;
550
551 error:
552     if( p_sys->fd > 0 )
553     {
554         net_Close( p_sys->fd  );
555         p_sys->fd = -1;
556     }
557     return VLC_EGENERIC;
558 }
559
560 /*****************************************************************************
561  *
562  *****************************************************************************/
563 static int Start( access_t *p_access, off_t i_pos )
564 {
565     access_sys_t *p_sys = p_access->p_sys;
566     int  i_streams = 0;
567     int  i;
568     char *psz;
569
570     msg_Dbg( p_access, "starting stream" );
571
572     if( ( p_sys->fd = net_OpenTCP( p_access, p_sys->url.psz_host,
573                                             p_sys->url.i_port ) ) < 0 )
574     {
575         /* should not occur */
576         msg_Err( p_access, "cannot connect to the server" );
577         return VLC_EGENERIC;
578     }
579
580     for( i = 1; i < 128; i++ )
581     {
582         if( p_sys->asfh.stream[i].i_selected )
583         {
584             i_streams++;
585         }
586     }
587
588     if( i_streams <= 0 )
589     {
590         msg_Err( p_access, "no stream selected" );
591         return VLC_EGENERIC;
592     }
593     net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
594                 "GET %s HTTP/1.0\r\n"
595                 "Accept: */*\r\n"
596                 "User-Agent: NSPlayer/4.1.0.3856\r\n"
597                 "Host: %s:%d\r\n",
598                 ( p_sys->url.psz_path == NULL || *p_sys->url.psz_path == '\0' ) ? "/" : p_sys->url.psz_path,
599                 p_sys->url.psz_host, p_sys->url.i_port );
600     if( p_sys->b_broadcast )
601     {
602         net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
603                     "Pragma: no-cache,rate=1.000000,request-context=%d\r\n",
604                     p_sys->i_request_context++ );
605     }
606     else
607     {
608         net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
609                     "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=0\r\n",
610                     (uint32_t)((i_pos >> 32)&0xffffffff),
611                     (uint32_t)(i_pos&0xffffffff),
612                     p_sys->i_request_context++ );
613     }
614     net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
615                 "Pragma: xPlayStrm=1\r\n"
616                 "Pragma: xClientGUID={"GUID_FMT"}\r\n"
617                 "Pragma: stream-switch-count=%d\r\n"
618                 "Pragma: stream-switch-entry=",
619                 GUID_PRINT( p_sys->guid ),
620                 i_streams);
621
622     for( i = 1; i < 128; i++ )
623     {
624         if( p_sys->asfh.stream[i].i_cat != ASF_STREAM_UNKNOWN )
625         {
626             int i_select = 2;
627             if( p_sys->asfh.stream[i].i_selected )
628             {
629                 i_select = 0;
630             }
631
632             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
633                         "ffff:%d:%d ", i, i_select );
634         }
635     }
636     net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "\r\n" );
637     net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
638                 "Connection: Close\r\n" );
639
640     if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "\r\n" ) < 0 )
641     {
642         msg_Err( p_access, "failed to send request" );
643         return VLC_EGENERIC;
644     }
645
646     if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL ) ) == NULL )
647     {
648         msg_Err( p_access, "cannot read data" );
649         return VLC_EGENERIC;
650     }
651     if( atoi( &psz[9] ) >= 400 )
652     {
653         msg_Err( p_access, "error: %s", psz );
654         free( psz );
655         return VLC_EGENERIC;
656     }
657     msg_Dbg( p_access, "HTTP reply '%s'", psz );
658     free( psz );
659
660     /* FIXME check HTTP code */
661     for( ;; )
662     {
663         char *psz = net_Gets( p_access, p_sys->fd, NULL );
664         if( psz == NULL )
665         {
666             msg_Err( p_access, "cannot read data" );
667             return VLC_EGENERIC;
668         }
669         if( *psz == '\0' )
670         {
671             free( psz );
672             break;
673         }
674         msg_Dbg( p_access, "%s", psz );
675         free( psz );
676     }
677
678     p_sys->i_packet_used   = 0;
679     p_sys->i_packet_length = 0;
680
681     return VLC_SUCCESS;
682 }
683
684 /*****************************************************************************
685  *
686  *****************************************************************************/
687 static void Stop( access_t *p_access )
688 {
689     access_sys_t *p_sys = p_access->p_sys;
690
691     msg_Dbg( p_access, "closing stream" );
692     if( p_sys->fd > 0 )
693     {
694         net_Close( p_sys->fd );
695         p_sys->fd = -1;
696     }
697 }
698
699 /*****************************************************************************
700  *
701  *****************************************************************************/
702 static int GetPacket( access_t * p_access, chunk_t *p_ck )
703 {
704     access_sys_t *p_sys = p_access->p_sys;
705
706     /* chunk_t */
707     memset( p_ck, 0, sizeof( chunk_t ) );
708
709     /* Read the chunk header */
710     if( net_Read( p_access, p_sys->fd, NULL, p_sys->buffer, 12, VLC_TRUE ) < 12 )
711     {
712         /* msg_Err( p_access, "cannot read data" ); */
713         return VLC_EGENERIC;
714     }
715
716     p_ck->i_type      = GetWLE( p_sys->buffer);
717     p_ck->i_size      = GetWLE( p_sys->buffer + 2);
718     p_ck->i_sequence  = GetDWLE( p_sys->buffer + 4);
719     p_ck->i_unknown   = GetWLE( p_sys->buffer + 8);
720     p_ck->i_size2     = GetWLE( p_sys->buffer + 10);
721     p_ck->p_data      = p_sys->buffer + 12;
722     p_ck->i_data      = p_ck->i_size2 - 8;
723
724     if( p_ck->i_type == 0x4524 )   // Transfer complete
725     {
726         if( p_ck->i_sequence == 0 )
727         {
728             msg_Warn( p_access, "EOF" );
729             return VLC_EGENERIC;
730         }
731         else
732         {
733             msg_Warn( p_access, "Next stream follow but not supported" );
734             return VLC_EGENERIC;
735         }
736     }
737     else if( p_ck->i_type != 0x4824 && p_ck->i_type != 0x4424 )
738     {
739         msg_Err( p_access, "invalid chunk FATAL (0x%x)", p_ck->i_type );
740         return VLC_EGENERIC;
741     }
742
743     if( p_ck->i_data > 0 &&
744         net_Read( p_access, p_sys->fd, NULL, &p_sys->buffer[12], p_ck->i_data, VLC_TRUE ) < p_ck->i_data )
745     {
746         msg_Err( p_access, "cannot read data" );
747         return VLC_EGENERIC;
748     }
749
750     if( p_sys->i_packet_sequence != 0 &&
751         p_ck->i_sequence != p_sys->i_packet_sequence )
752     {
753         msg_Warn( p_access, "packet lost ? (%d != %d)", p_ck->i_sequence, p_sys->i_packet_sequence );
754     }
755
756     p_sys->i_packet_sequence = p_ck->i_sequence + 1;
757     p_sys->i_packet_used   = 0;
758     p_sys->i_packet_length = p_ck->i_data;
759     p_sys->p_packet        = p_ck->p_data;
760
761     return VLC_SUCCESS;
762 }