]> git.sesse.net Git - vlc/blob - modules/access/mms/mmsh.c
* all: better support for MBR mms stream (display only received streams).
[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             *pb_bool = VLC_FALSE;
184             break;
185
186         /* */
187         case ACCESS_GET_MTU:
188             pi_int = (int*)va_arg( args, int * );
189             *pi_int = 3 * p_sys->asfh.i_min_data_packet_size;
190             break;
191
192         case ACCESS_GET_PTS_DELAY:
193             pi_64 = (int64_t*)va_arg( args, int64_t * );
194             *pi_64 = (int64_t)var_GetInteger( p_access, "mms-caching" ) * I64C(1000);
195             break;
196
197         case ACCESS_GET_PRIVATE_ID_STATE:
198             i_int = (int)va_arg( args, int );
199             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t );
200
201             if( i_int < 0 || i_int > 127 )
202                 return VLC_EGENERIC;
203             *pb_bool =  p_sys->asfh.stream[i_int].i_selected ? VLC_TRUE : VLC_FALSE;
204             break;
205
206         /* */
207         case ACCESS_SET_PAUSE_STATE:
208         case ACCESS_GET_TITLE_INFO:
209         case ACCESS_SET_TITLE:
210         case ACCESS_SET_SEEKPOINT:
211         case ACCESS_SET_PRIVATE_ID_STATE:
212             return VLC_EGENERIC;
213
214         default:
215             msg_Warn( p_access, "unimplemented query in control" );
216             return VLC_EGENERIC;
217
218     }
219     return VLC_SUCCESS;
220 }
221
222 /*****************************************************************************
223  * Seek: try to go at the right place
224  *****************************************************************************/
225 static int Seek( access_t *p_access, int64_t i_pos )
226 {
227     access_sys_t *p_sys = p_access->p_sys;
228     chunk_t      ck;
229     off_t        i_offset;
230     off_t        i_packet;
231
232     msg_Dbg( p_access, "seeking to "I64Fd, i_pos );
233
234     i_packet = ( i_pos - p_sys->i_header ) / p_sys->asfh.i_min_data_packet_size;
235     i_offset = ( i_pos - p_sys->i_header ) % p_sys->asfh.i_min_data_packet_size;
236
237     Stop( p_access );
238     Start( p_access, i_packet * p_sys->asfh.i_min_data_packet_size );
239
240     while( !p_access->b_die )
241     {
242         if( GetPacket( p_access, &ck ) )
243             break;
244
245         /* skip headers */
246         if( ck.i_type != 0x4824 )
247             break;
248
249         msg_Warn( p_access, "skipping header" );
250     }
251
252     p_access->info.i_pos = i_pos;
253     p_access->info.b_eof = VLC_FALSE;
254     p_sys->i_packet_used += i_offset;
255
256     return VLC_SUCCESS;
257 }
258
259 /*****************************************************************************
260  * Read:
261  *****************************************************************************/
262 static int ReadRedirect( access_t *p_access, uint8_t *p, int i_len )
263 {
264     return 0;
265 }
266
267 /*****************************************************************************
268  * Read:
269  *****************************************************************************/
270 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
271 {
272     access_sys_t *p_sys = p_access->p_sys;
273     size_t       i_copy;
274     size_t       i_data = 0;
275
276     if( p_access->info.b_eof )
277         return 0;
278
279     while( i_data < i_len )
280     {
281         if( p_access->info.i_pos < p_sys->i_start + p_sys->i_header )
282         {
283             int i_offset = p_access->info.i_pos - p_sys->i_start;
284             i_copy = __MIN( p_sys->i_header - i_offset, i_len - i_data );
285             memcpy( &p_buffer[i_data], &p_sys->p_header[i_offset], i_copy );
286
287             i_data += i_copy;
288             p_access->info.i_pos += i_copy;
289         }
290         else if( p_sys->i_packet_used < p_sys->i_packet_length )
291         {
292             i_copy = __MIN( p_sys->i_packet_length - p_sys->i_packet_used,
293                             i_len - i_data );
294
295             memcpy( &p_buffer[i_data],
296                     &p_sys->p_packet[p_sys->i_packet_used],
297                     i_copy );
298
299             i_data += i_copy;
300             p_sys->i_packet_used += i_copy;
301             p_access->info.i_pos += i_copy;
302         }
303         else if( p_sys->i_packet_length > 0 &&
304                  (int)p_sys->i_packet_used < p_sys->asfh.i_min_data_packet_size )
305         {
306             i_copy = __MIN( p_sys->asfh.i_min_data_packet_size - p_sys->i_packet_used,
307                             i_len - i_data );
308
309             memset( &p_buffer[i_data], 0, i_copy );
310
311             i_data += i_copy;
312             p_sys->i_packet_used += i_copy;
313             p_access->info.i_pos += i_copy;
314         }
315         else
316         {
317             chunk_t ck;
318             if( GetPacket( p_access, &ck ) )
319             {
320                 if( ck.i_type == 0x4524 && ck.i_sequence != 0 && p_sys->b_broadcast )
321                 {
322                     char *psz_location = NULL;
323
324                     p_sys->i_start = p_access->info.i_pos;
325
326                     msg_Dbg( p_access, "stoping the stream" );
327                     Stop( p_access );
328
329                     msg_Dbg( p_access, "describe the stream" );
330                     if( Describe( p_access, &psz_location ) )
331                     {
332                         msg_Err( p_access, "describe failed" );
333                         p_access->info.b_eof = VLC_TRUE;
334                         return 0;
335                     }
336                     if( Start( p_access, 0 ) )
337                     {
338                         msg_Err( p_access, "Start failed" );
339                         p_access->info.b_eof = VLC_TRUE;
340                         return 0;
341                     }
342                 }
343                 else
344                 {
345                     p_access->info.b_eof = VLC_TRUE;
346                     return 0;
347                 }
348             }
349             if( ck.i_type != 0x4424 )
350             {
351                 p_sys->i_packet_used = 0;
352                 p_sys->i_packet_length = 0;
353             }
354         }
355     }
356
357     return( i_data );
358 }
359
360 /*****************************************************************************
361  * Describe:
362  *****************************************************************************/
363 static int Describe( access_t  *p_access, char **ppsz_location )
364 {
365     access_sys_t *p_sys = p_access->p_sys;
366     char         *psz_location = NULL;
367     char         *psz;
368     int          i_code;
369
370     /* Reinit context */
371     p_sys->b_broadcast = VLC_TRUE;
372     p_sys->i_request_context = 1;
373     p_sys->i_packet_sequence = 0;
374     p_sys->i_packet_used = 0;
375     p_sys->i_packet_length = 0;
376     p_sys->p_packet = NULL;
377     E_( GenerateGuid )( &p_sys->guid );
378
379     if( ( p_sys->fd = net_OpenTCP( p_access, p_sys->url.psz_host,
380                                             p_sys->url.i_port ) ) < 0 )
381     {
382         msg_Err( p_access, "cannot connect to %s:%d", p_sys->url.psz_host, p_sys->url.i_port );
383         goto error;
384     }
385
386     /* send first request */
387     net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
388                 "GET %s HTTP/1.0\r\n"
389                 "Accept: */*\r\n"
390                 "User-Agent: NSPlayer/4.1.0.3856\r\n"
391                 "Host: %s:%d\r\n"
392                 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=0:0,request-context=%d,max-duration=0\r\n"
393                 "Pragma: xClientGUID={"GUID_FMT"}\r\n"
394                 "Connection: Close\r\n",
395                 ( p_sys->url.psz_path == NULL || *p_sys->url.psz_path == '\0' ) ? "/" : p_sys->url.psz_path,
396                 p_sys->url.psz_host, p_sys->url.i_port,
397                 p_sys->i_request_context++,
398                 GUID_PRINT( p_sys->guid ) );
399
400     if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "\r\n" ) < 0 )
401     {
402         msg_Err( p_access, "failed to send request" );
403         goto error;
404     }
405
406     /* Receive the http header */
407     if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL ) ) == NULL )
408     {
409         msg_Err( p_access, "failed to read answer" );
410         goto error;
411     }
412     if( strncmp( psz, "HTTP/1.", 7 ) )
413     {
414         msg_Err( p_access, "invalid HTTP reply '%s'", psz );
415         free( psz );
416         goto error;
417     }
418     i_code = atoi( &psz[9] );
419     if( i_code >= 400 )
420     {
421         msg_Err( p_access, "error: %s", psz );
422         free( psz );
423         goto error;
424     }
425
426     msg_Dbg( p_access, "HTTP reply '%s'", psz );
427     free( psz );
428     for( ;; )
429     {
430         char *psz = net_Gets( p_access, p_sys->fd, NULL );
431         char *p;
432
433         if( psz == NULL )
434         {
435             msg_Err( p_access, "failed to read answer" );
436             goto error;
437         }
438
439         if( *psz == '\0' )
440         {
441             free( psz );
442             break;
443         }
444
445         if( ( p = strchr( psz, ':' ) ) == NULL )
446         {
447             msg_Err( p_access, "malformed header line: %s", psz );
448             free( psz );
449             goto error;
450         }
451         *p++ = '\0';
452         while( *p == ' ' ) p++;
453
454         /* FIXME FIXME test Content-Type to see if it's a plain stream or an
455          * asx FIXME */
456         if( !strcasecmp( psz, "Pragma" ) )
457         {
458             if( strstr( p, "features" ) )
459             {
460                 /* FIXME, it is a bit badly done here ..... */
461                 if( strstr( p, "broadcast" ) )
462                 {
463                     msg_Dbg( p_access, "stream type = broadcast" );
464                     p_sys->b_broadcast = VLC_TRUE;
465                 }
466                 else if( strstr( p, "seekable" ) )
467                 {
468                     msg_Dbg( p_access, "stream type = seekable" );
469                     p_sys->b_broadcast = VLC_FALSE;
470                 }
471                 else
472                 {
473                     msg_Warn( p_access, "unknow stream types (%s)", p );
474                     p_sys->b_broadcast = VLC_FALSE;
475                 }
476             }
477         }
478         else if( !strcasecmp( psz, "Location" ) )
479         {
480             psz_location = strdup( p );
481         }
482
483         free( psz );
484     }
485
486     /* Handle the redirection */
487     if( ( i_code == 301 || i_code == 302 ||
488           i_code == 303 || i_code == 307 ) &&
489         psz_location && *psz_location )
490     {
491         msg_Dbg( p_access, "redirection to %s", psz_location );
492         net_Close( p_sys->fd ); p_sys->fd = -1;
493
494         *ppsz_location = psz_location;
495         return VLC_SUCCESS;
496     }
497
498     /* Read the asf header */
499     p_sys->i_header = 0;
500     p_sys->p_header = NULL;
501     for( ;; )
502     {
503         chunk_t ck;
504         if( GetPacket( p_access, &ck ) ||
505             ck.i_type != 0x4824 )
506         {
507             break;
508         }
509
510         if( ck.i_data > 0 )
511         {
512             p_sys->i_header += ck.i_data;
513             p_sys->p_header = realloc( p_sys->p_header, p_sys->i_header );
514             memcpy( &p_sys->p_header[p_sys->i_header - ck.i_data],
515                     ck.p_data, ck.i_data );
516         }
517     }
518     msg_Dbg( p_access, "complete header size=%d", p_sys->i_header );
519     if( p_sys->i_header <= 0 )
520     {
521         msg_Err( p_access, "header size == 0" );
522         goto error;
523     }
524     /* close this connection */
525     net_Close( p_sys->fd ); p_sys->fd = -1;
526
527     /* *** parse header and get stream and their id *** */
528     /* get all streams properties,
529      *
530      * TODO : stream bitrates properties(optional)
531      *        and bitrate mutual exclusion(optional) */
532     E_( asf_HeaderParse )( &p_sys->asfh,
533                            p_sys->p_header, p_sys->i_header );
534     msg_Dbg( p_access, "packet count=%lld packet size=%d",
535              p_sys->asfh.i_data_packets_count,
536              p_sys->asfh.i_min_data_packet_size );
537
538     E_( asf_StreamSelect)( &p_sys->asfh,
539                            var_CreateGetInteger( p_access, "mms-maxbitrate" ),
540                            var_CreateGetInteger( p_access, "mms-all" ),
541                            var_CreateGetInteger( p_access, "audio" ),
542                            var_CreateGetInteger( p_access, "video" ) );
543
544     return VLC_SUCCESS;
545
546 error:
547     if( p_sys->fd > 0 )
548     {
549         net_Close( p_sys->fd  );
550         p_sys->fd = -1;
551     }
552     return VLC_EGENERIC;
553 }
554
555 /*****************************************************************************
556  *
557  *****************************************************************************/
558 static int Start( access_t *p_access, off_t i_pos )
559 {
560     access_sys_t *p_sys = p_access->p_sys;
561     int  i_streams = 0;
562     int  i;
563     char *psz;
564
565     msg_Dbg( p_access, "starting stream" );
566
567     if( ( p_sys->fd = net_OpenTCP( p_access, p_sys->url.psz_host,
568                                             p_sys->url.i_port ) ) < 0 )
569     {
570         /* should not occur */
571         msg_Err( p_access, "cannot connect to the server" );
572         return VLC_EGENERIC;
573     }
574
575     for( i = 1; i < 128; i++ )
576     {
577         if( p_sys->asfh.stream[i].i_selected )
578         {
579             i_streams++;
580         }
581     }
582
583     if( i_streams <= 0 )
584     {
585         msg_Err( p_access, "no stream selected" );
586         return VLC_EGENERIC;
587     }
588     net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
589                 "GET %s HTTP/1.0\r\n"
590                 "Accept: */*\r\n"
591                 "User-Agent: NSPlayer/4.1.0.3856\r\n"
592                 "Host: %s:%d\r\n",
593                 ( p_sys->url.psz_path == NULL || *p_sys->url.psz_path == '\0' ) ? "/" : p_sys->url.psz_path,
594                 p_sys->url.psz_host, p_sys->url.i_port );
595     if( p_sys->b_broadcast )
596     {
597         net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
598                     "Pragma: no-cache,rate=1.000000,request-context=%d\r\n",
599                     p_sys->i_request_context++ );
600     }
601     else
602     {
603         net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
604                     "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=0\r\n",
605                     (uint32_t)((i_pos >> 32)&0xffffffff),
606                     (uint32_t)(i_pos&0xffffffff),
607                     p_sys->i_request_context++ );
608     }
609     net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
610                 "Pragma: xPlayStrm=1\r\n"
611                 "Pragma: xClientGUID={"GUID_FMT"}\r\n"
612                 "Pragma: stream-switch-count=%d\r\n"
613                 "Pragma: stream-switch-entry=",
614                 GUID_PRINT( p_sys->guid ),
615                 i_streams);
616
617     for( i = 1; i < 128; i++ )
618     {
619         if( p_sys->asfh.stream[i].i_cat != ASF_STREAM_UNKNOWN )
620         {
621             int i_select = 2;
622             if( p_sys->asfh.stream[i].i_selected )
623             {
624                 i_select = 0;
625             }
626
627             net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
628                         "ffff:%d:%d ", i, i_select );
629         }
630     }
631     net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "\r\n" );
632     net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
633                 "Connection: Close\r\n" );
634
635     if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "\r\n" ) < 0 )
636     {
637         msg_Err( p_access, "failed to send request" );
638         return VLC_EGENERIC;
639     }
640
641     if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL ) ) == NULL )
642     {
643         msg_Err( p_access, "cannot read data" );
644         return VLC_EGENERIC;
645     }
646     if( atoi( &psz[9] ) >= 400 )
647     {
648         msg_Err( p_access, "error: %s", psz );
649         free( psz );
650         return VLC_EGENERIC;
651     }
652     msg_Dbg( p_access, "HTTP reply '%s'", psz );
653     free( psz );
654
655     /* FIXME check HTTP code */
656     for( ;; )
657     {
658         char *psz = net_Gets( p_access, p_sys->fd, NULL );
659         if( psz == NULL )
660         {
661             msg_Err( p_access, "cannot read data" );
662             return VLC_EGENERIC;
663         }
664         if( *psz == '\0' )
665         {
666             free( psz );
667             break;
668         }
669         msg_Dbg( p_access, "%s", psz );
670         free( psz );
671     }
672
673     p_sys->i_packet_used   = 0;
674     p_sys->i_packet_length = 0;
675
676     return VLC_SUCCESS;
677 }
678
679 /*****************************************************************************
680  *
681  *****************************************************************************/
682 static void Stop( access_t *p_access )
683 {
684     access_sys_t *p_sys = p_access->p_sys;
685
686     msg_Dbg( p_access, "closing stream" );
687     if( p_sys->fd > 0 )
688     {
689         net_Close( p_sys->fd );
690         p_sys->fd = -1;
691     }
692 }
693
694 /*****************************************************************************
695  *
696  *****************************************************************************/
697 static int GetPacket( access_t * p_access, chunk_t *p_ck )
698 {
699     access_sys_t *p_sys = p_access->p_sys;
700
701     /* chunk_t */
702     memset( p_ck, 0, sizeof( chunk_t ) );
703
704     /* Read the chunk header */
705     if( net_Read( p_access, p_sys->fd, NULL, p_sys->buffer, 12, VLC_TRUE ) < 12 )
706     {
707         /* msg_Err( p_access, "cannot read data" ); */
708         return VLC_EGENERIC;
709     }
710
711     p_ck->i_type      = GetWLE( p_sys->buffer);
712     p_ck->i_size      = GetWLE( p_sys->buffer + 2);
713     p_ck->i_sequence  = GetDWLE( p_sys->buffer + 4);
714     p_ck->i_unknown   = GetWLE( p_sys->buffer + 8);
715     p_ck->i_size2     = GetWLE( p_sys->buffer + 10);
716     p_ck->p_data      = p_sys->buffer + 12;
717     p_ck->i_data      = p_ck->i_size2 - 8;
718
719     if( p_ck->i_type == 0x4524 )   // Transfer complete
720     {
721         if( p_ck->i_sequence == 0 )
722         {
723             msg_Warn( p_access, "EOF" );
724             return VLC_EGENERIC;
725         }
726         else
727         {
728             msg_Warn( p_access, "Next stream follow but not supported" );
729             return VLC_EGENERIC;
730         }
731     }
732     else if( p_ck->i_type != 0x4824 && p_ck->i_type != 0x4424 )
733     {
734         msg_Err( p_access, "invalid chunk FATAL (0x%x)", p_ck->i_type );
735         return VLC_EGENERIC;
736     }
737
738     if( p_ck->i_data > 0 &&
739         net_Read( p_access, p_sys->fd, NULL, &p_sys->buffer[12], p_ck->i_data, VLC_TRUE ) < p_ck->i_data )
740     {
741         msg_Err( p_access, "cannot read data" );
742         return VLC_EGENERIC;
743     }
744
745     if( p_sys->i_packet_sequence != 0 &&
746         p_ck->i_sequence != p_sys->i_packet_sequence )
747     {
748         msg_Warn( p_access, "packet lost ? (%d != %d)", p_ck->i_sequence, p_sys->i_packet_sequence );
749     }
750
751     p_sys->i_packet_sequence = p_ck->i_sequence + 1;
752     p_sys->i_packet_used   = 0;
753     p_sys->i_packet_length = p_ck->i_data;
754     p_sys->p_packet        = p_ck->p_data;
755
756     return VLC_SUCCESS;
757 }