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