Mercurial > mplayer.hg
annotate libmpdemux/asf_streaming.c @ 2557:6a74454ea121
some cleanup
author | arpi |
---|---|
date | Tue, 30 Oct 2001 17:04:59 +0000 |
parents | 66837325b929 |
children | 6b6fa2be9b97 |
rev | line source |
---|---|
871 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
833 | 3 #include <string.h> |
1430 | 4 #include <unistd.h> |
833 | 5 |
2555
66837325b929
config.h cleanup, few things added to steram/demuxer headers
arpi
parents:
2489
diff
changeset
|
6 #include "config.h" |
66837325b929
config.h cleanup, few things added to steram/demuxer headers
arpi
parents:
2489
diff
changeset
|
7 |
871 | 8 #include "url.h" |
9 #include "http.h" | |
1001 | 10 #include "asf.h" |
905 | 11 #include "network.h" |
871 | 12 |
1001 | 13 #include "stream.h" |
1340
d4f1e8d0e591
demuxer struct access code temporary disabled - FIXME
arpi
parents:
1001
diff
changeset
|
14 //#include "demuxer.h" |
871 | 15 |
1340
d4f1e8d0e591
demuxer struct access code temporary disabled - FIXME
arpi
parents:
1001
diff
changeset
|
16 //extern demuxer_t *demuxer; |
833 | 17 |
905 | 18 static ASF_StreamType_e streaming_type = ASF_Unknown_e; |
871 | 19 |
1001 | 20 int |
21 asf_http_streaming_read( streaming_ctrl_t *streaming_ctrl ) { | |
22 char *buffer; | |
23 int drop_packet; | |
24 int ret; | |
2489
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
25 printf("asf_http_streaming_read\n"); |
1001 | 26 ret = asf_streaming( streaming_ctrl->buffer->buffer, streaming_ctrl->buffer->length, &drop_packet ); |
27 printf("ret: %d\n", ret); | |
28 if( ret<0 ) return -1; | |
29 if( ret>streaming_ctrl->buffer->length ) return 0; | |
30 buffer = (char*)malloc(ret); | |
31 if( buffer==NULL ) { | |
32 printf("Memory allocation failed\n"); | |
33 return -1; | |
34 } | |
35 printf("buffer length: %d\n", streaming_ctrl->buffer->length ); | |
36 net_fifo_pop( streaming_ctrl->buffer, buffer, ret ); | |
37 printf(" pop: 0x%02X\n", *((unsigned int*)buffer) ); | |
38 printf("buffer length: %d\n", streaming_ctrl->buffer->length ); | |
39 printf("0x%02X\n", *((unsigned int*)(buffer+sizeof(ASF_stream_chunck_t))) ); | |
40 if( !drop_packet ) { | |
41 write( streaming_ctrl->fd_pipe_in, buffer+sizeof(ASF_stream_chunck_t), ret-sizeof(ASF_stream_chunck_t) ); | |
42 } | |
43 free( buffer ); | |
44 return ret; | |
45 } | |
46 | |
2489
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
47 int |
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
48 asf_http_read( streaming_ctrl_t *streaming_ctrl ) { |
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
49 char *buffer; |
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
50 unsigned int length = streaming_ctrl->buffer->length; |
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
51 |
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
52 buffer = (char*)malloc(length); |
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
53 if( buffer==NULL ) { |
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
54 printf("Memory allocation failed\n"); |
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
55 return -1; |
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
56 } |
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
57 |
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
58 net_fifo_pop( streaming_ctrl->buffer, buffer, length ); |
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
59 |
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
60 write( streaming_ctrl->fd_pipe_in, buffer, length ); |
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
61 |
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
62 free( buffer ); |
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
63 return length; |
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
64 } |
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
65 |
1001 | 66 int |
67 asf_streaming(char *data, int length, int *drop_packet ) { | |
833 | 68 ASF_stream_chunck_t *stream_chunck=(ASF_stream_chunck_t*)data; |
871 | 69 printf("ASF stream chunck size=%d\n", stream_chunck->size); |
1001 | 70 printf("length: %d\n", length ); |
71 printf("0x%02X\n", stream_chunck->type ); | |
72 | |
73 if( drop_packet!=NULL ) *drop_packet = 0; | |
74 if( data==NULL || length<=0 ) return -1; | |
871 | 75 |
76 if( stream_chunck->size<8 ) { | |
77 printf("Ahhhh, stream_chunck size is too small: %d\n", stream_chunck->size); | |
1001 | 78 return -1; |
871 | 79 } |
80 if( stream_chunck->size!=stream_chunck->size_confirm ) { | |
81 printf("size_confirm mismatch!: %d %d\n", stream_chunck->size, stream_chunck->size_confirm); | |
1001 | 82 return -1; |
871 | 83 } |
833 | 84 |
1001 | 85 printf(" type: 0x%02X\n", stream_chunck->type ); |
86 printf(" size: %d (0x%02X)\n", stream_chunck->size, stream_chunck->size ); | |
87 printf(" sequence_number: 0x%04X\n", stream_chunck->sequence_number ); | |
88 printf(" unknown: 0x%02X\n", stream_chunck->unknown ); | |
89 printf(" size_confirm: 0x%02X\n", stream_chunck->size_confirm ); | |
90 | |
91 | |
833 | 92 switch(stream_chunck->type) { |
93 case 0x4324: // Clear ASF configuration | |
871 | 94 printf("=====> Clearing ASF stream configuration!\n"); |
1001 | 95 if( drop_packet!=NULL ) *drop_packet = 1; |
96 return stream_chunck->size; | |
833 | 97 break; |
98 case 0x4424: // Data follows | |
871 | 99 printf("=====> Data follows\n"); |
833 | 100 break; |
101 case 0x4524: // Transfer complete | |
871 | 102 printf("=====> Transfer complete\n"); |
1001 | 103 if( drop_packet!=NULL ) *drop_packet = 1; |
104 return stream_chunck->size; | |
833 | 105 break; |
106 case 0x4824: // ASF header chunk follows | |
871 | 107 printf("=====> ASF header chunk follows\n"); |
833 | 108 break; |
109 default: | |
871 | 110 printf("=====> Unknown stream type 0x%x\n", stream_chunck->type ); |
833 | 111 } |
1001 | 112 return stream_chunck->size+4; |
833 | 113 } |
114 | |
905 | 115 int |
116 asf_http_streaming_type(char *content_type, char *features) { | |
117 if( content_type==NULL ) return ASF_Unknown_e; | |
833 | 118 if( !strcasecmp(content_type, "application/octet-stream") ) { |
905 | 119 if( features==NULL ) { |
1001 | 120 printf("=====> ASF Prerecorded\n"); |
905 | 121 return ASF_Prerecorded_e; |
122 } else if( strstr(features, "broadcast")) { | |
1001 | 123 printf("=====> ASF Live stream\n"); |
905 | 124 return ASF_Live_e; |
833 | 125 } else { |
1001 | 126 printf("=====> ASF Prerecorded\n"); |
905 | 127 return ASF_Prerecorded_e; |
833 | 128 } |
129 } else { | |
871 | 130 if( (!strcasecmp(content_type, "audio/x-ms-wax")) || |
833 | 131 (!strcasecmp(content_type, "audio/x-ms-wma")) || |
132 (!strcasecmp(content_type, "video/x-ms-asf")) || | |
133 (!strcasecmp(content_type, "video/x-ms-afs")) || | |
134 (!strcasecmp(content_type, "video/x-ms-wvx")) || | |
135 (!strcasecmp(content_type, "video/x-ms-wmv")) || | |
136 (!strcasecmp(content_type, "video/x-ms-wma")) ) { | |
1001 | 137 printf("=====> ASF Redirector\n"); |
905 | 138 return ASF_Redirector_e; |
2489
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
139 } else if( !strcasecmp(content_type, "text/plain") ) { |
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
140 printf("=====> ASF Plain text\n"); |
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
141 return ASF_PlainText_e; |
833 | 142 } else { |
1001 | 143 printf("=====> ASF unknown content-type: %s\n", content_type ); |
905 | 144 return ASF_Unknown_e; |
833 | 145 } |
146 } | |
905 | 147 return ASF_Unknown_e; |
833 | 148 } |
871 | 149 |
905 | 150 HTTP_header_t * |
871 | 151 asf_http_request(URL_t *url) { |
152 HTTP_header_t *http_hdr; | |
153 char str[250]; | |
1001 | 154 char *ptr; |
871 | 155 char *request; |
1001 | 156 int i; |
871 | 157 |
158 int offset_hi=0, offset_lo=0, req_nb=1, length=0; | |
1001 | 159 int asf_nb_stream; |
871 | 160 |
161 // Common header for all requests. | |
162 http_hdr = http_new_header(); | |
163 http_set_uri( http_hdr, url->file ); | |
164 http_set_field( http_hdr, "Accept: */*" ); | |
165 http_set_field( http_hdr, "User-Agent: NSPlayer/4.1.0.3856" ); | |
166 sprintf( str, "Host: %s:%d", url->hostname, url->port ); | |
167 http_set_field( http_hdr, str ); | |
168 http_set_field( http_hdr, "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}" ); | |
169 sprintf(str, | |
170 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=%u", | |
171 offset_hi, offset_lo, req_nb, length ); | |
172 http_set_field( http_hdr, str ); | |
173 | |
905 | 174 switch( streaming_type ) { |
871 | 175 case ASF_Live_e: |
176 case ASF_Prerecorded_e: | |
177 http_set_field( http_hdr, "Pragma: xPlayStrm=1" ); | |
1001 | 178 ptr = str; |
179 ptr += sprintf( ptr, "Pragma: stream-switch-entry="); | |
1340
d4f1e8d0e591
demuxer struct access code temporary disabled - FIXME
arpi
parents:
1001
diff
changeset
|
180 |
d4f1e8d0e591
demuxer struct access code temporary disabled - FIXME
arpi
parents:
1001
diff
changeset
|
181 // FIXME: why do you need demuxer here? if you really need it, pass it as |
d4f1e8d0e591
demuxer struct access code temporary disabled - FIXME
arpi
parents:
1001
diff
changeset
|
182 // parameter. -- A'rpi |
d4f1e8d0e591
demuxer struct access code temporary disabled - FIXME
arpi
parents:
1001
diff
changeset
|
183 |
d4f1e8d0e591
demuxer struct access code temporary disabled - FIXME
arpi
parents:
1001
diff
changeset
|
184 #if 0 |
1001 | 185 for( i=0, asf_nb_stream=0 ; i<256 ; i++ ) { |
186 // FIXME START | |
187 if( demuxer==NULL ) { | |
188 ptr += sprintf( ptr, " ffff:1:0" ); | |
189 asf_nb_stream = 1; | |
190 break; | |
191 } | |
192 // FIXME END | |
193 if( demuxer->a_streams[i] ) { | |
194 ptr += sprintf( ptr, " ffff:%d:0", i ); | |
195 asf_nb_stream++; | |
196 } | |
197 if( demuxer->v_streams[i] ) { | |
198 ptr += sprintf( ptr, " ffff:%d:0", i ); | |
199 asf_nb_stream++; | |
200 } | |
201 } | |
1340
d4f1e8d0e591
demuxer struct access code temporary disabled - FIXME
arpi
parents:
1001
diff
changeset
|
202 #endif |
1001 | 203 http_set_field( http_hdr, str ); |
871 | 204 sprintf( str, "Pragma: stream-switch-count=%d", asf_nb_stream ); |
205 http_set_field( http_hdr, str ); | |
206 break; | |
207 case ASF_Redirector_e: | |
208 break; | |
209 case ASF_Unknown_e: | |
210 // First request goes here. | |
211 break; | |
212 default: | |
213 printf("Unknown asf stream type\n"); | |
214 } | |
215 | |
216 http_set_field( http_hdr, "Connection: Close" ); | |
905 | 217 http_build_request( http_hdr ); |
871 | 218 |
905 | 219 return http_hdr; |
871 | 220 } |
221 | |
222 int | |
905 | 223 asf_http_parse_response( HTTP_header_t *http_hdr ) { |
871 | 224 char *content_type, *pragma; |
225 char features[64] = "\0"; | |
226 int len; | |
905 | 227 if( http_response_parse(http_hdr)<0 ) { |
228 printf("Failed to parse HTTP response\n"); | |
229 return -1; | |
230 } | |
871 | 231 if( http_hdr->status_code!=200 ) { |
232 printf("Server return %d:%s\n", http_hdr->status_code, http_hdr->reason_phrase); | |
233 return -1; | |
234 } | |
235 | |
236 content_type = http_get_field( http_hdr, "Content-Type"); | |
237 | |
238 pragma = http_get_field( http_hdr, "Pragma"); | |
905 | 239 while( pragma!=NULL ) { |
871 | 240 char *comma_ptr=NULL; |
241 char *end; | |
242 // The pragma line can get severals attributes | |
243 // separeted with a comma ','. | |
244 do { | |
245 if( !strncasecmp( pragma, "features=", 9) ) { | |
246 pragma += 9; | |
247 end = strstr( pragma, "," ); | |
248 if( end==NULL ) { | |
249 len = strlen(pragma); | |
250 } | |
251 len = MIN(end-pragma,sizeof(features)); | |
252 strncpy( features, pragma, len ); | |
253 features[len]='\0'; | |
254 break; | |
255 } | |
256 comma_ptr = strstr( pragma, "," ); | |
257 if( comma_ptr!=NULL ) { | |
258 pragma = comma_ptr+1; | |
259 if( pragma[0]==' ' ) pragma++; | |
260 } | |
261 } while( comma_ptr!=NULL ); | |
262 pragma = http_get_next_field( http_hdr ); | |
905 | 263 } |
264 | |
265 streaming_type = asf_http_streaming_type( content_type, features ); | |
2489
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
266 /* |
905 | 267 if( http_hdr->body_size>0 ) { |
1001 | 268 asf_streaming( http_hdr->body, http_hdr->body_size, NULL); |
905 | 269 } |
2489
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
270 */ |
871 | 271 return 0; |
272 } | |
273 | |
905 | 274 URL_t * |
275 asf_http_ASX_redirect( HTTP_header_t *http_hdr ) { | |
276 URL_t *url_redirect=NULL; | |
277 printf("=========>> ASX parser not yet implemented <<==========\n"); | |
278 | |
279 printf("ASX=[%s]\n", http_hdr->body ); | |
280 | |
281 return url_redirect; | |
871 | 282 } |
905 | 283 |
284 int | |
1001 | 285 asf_http_streaming_start( streaming_ctrl_t *streaming_ctrl ) { |
905 | 286 HTTP_header_t *http_hdr=NULL; |
287 URL_t *url_next=NULL; | |
1001 | 288 URL_t *url = *(streaming_ctrl->url); |
905 | 289 char buffer[BUFFER_SIZE]; |
290 int i; | |
1001 | 291 int fd = streaming_ctrl->fd_net; |
905 | 292 int done=1; |
1001 | 293 |
294 streaming_type = ASF_Live_e; | |
905 | 295 do { |
296 if( fd>0 ) close( fd ); | |
1001 | 297 |
905 | 298 fd = connect2Server( url->hostname, url->port ); |
299 if( fd<0 ) return -1; | |
300 | |
301 http_hdr = asf_http_request( url ); | |
1001 | 302 printf("[%s]\n", http_hdr->buffer ); |
905 | 303 write( fd, http_hdr->buffer, http_hdr->buffer_size ); |
1001 | 304 // http_free( http_hdr ); |
905 | 305 |
306 http_hdr = http_new_header(); | |
307 do { | |
1001 | 308 i = readFromServer( fd, buffer, BUFFER_SIZE ); |
905 | 309 printf("read: %d\n", i ); |
1001 | 310 if( i<0 ) { |
311 perror("read"); | |
312 http_free( http_hdr ); | |
313 return -1; | |
314 } | |
905 | 315 http_response_append( http_hdr, buffer, i ); |
2489
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
316 } while( !http_is_header_entire( http_hdr ) ); |
905 | 317 //http_hdr->buffer[http_hdr->buffer_len]='\0'; |
318 //printf("[%s]\n", http_hdr->buffer ); | |
319 if( asf_http_parse_response(http_hdr)<0 ) { | |
320 printf("Failed to parse header\n"); | |
1001 | 321 http_free( http_hdr ); |
905 | 322 return -1; |
323 } | |
1001 | 324 switch( streaming_type ) { |
905 | 325 case ASF_Live_e: |
326 case ASF_Prerecorded_e: | |
2489
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
327 case ASF_PlainText_e: |
1001 | 328 if( http_hdr->body_size>0 ) { |
329 net_fifo_push( streaming_ctrl->buffer, http_hdr->body, http_hdr->body_size ); | |
330 } else { | |
331 ASF_stream_chunck_t *ptr; | |
332 int ret; | |
333 i = readFromServer( fd, buffer, sizeof(ASF_stream_chunck_t) ); | |
905 | 334 printf("read: %d\n", i ); |
1001 | 335 ret = asf_streaming( buffer, i, NULL ); |
336 net_fifo_push( streaming_ctrl->buffer, buffer, i ); | |
337 ptr = (ASF_stream_chunck_t*)buffer; | |
338 if( ret==ptr->size ) { | |
339 } | |
905 | 340 } |
1001 | 341 // done = 0; |
905 | 342 break; |
343 case ASF_Redirector_e: | |
344 url_next = asf_http_ASX_redirect( http_hdr ); | |
345 if( url_next==NULL ) { | |
346 printf("Failed to parse ASX file\n"); | |
347 close(fd); | |
1001 | 348 http_free( http_hdr ); |
905 | 349 return -1; |
350 } | |
351 if( url_next->port==0 ) url_next->port=80; | |
352 url_free( url ); | |
353 url = url_next; | |
1001 | 354 *(streaming_ctrl->url) = url_next; |
905 | 355 url_next = NULL; |
356 break; | |
357 case ASF_Unknown_e: | |
358 default: | |
359 printf("Unknown ASF streaming type\n"); | |
360 close(fd); | |
1001 | 361 http_free( http_hdr ); |
905 | 362 return -1; |
363 } | |
364 | |
365 // Check if we got a redirect. | |
366 } while(!done); | |
367 | |
1001 | 368 streaming_ctrl->fd_net = fd; |
2489
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
369 if( streaming_type==ASF_PlainText_e ) { |
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
370 streaming_ctrl->streaming_read = asf_http_read; |
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
371 } else { |
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
372 streaming_ctrl->streaming_read = asf_http_streaming_read; |
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
373 } |
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
374 streaming_ctrl->prebuffer_size = 20000; |
1001 | 375 streaming_ctrl->buffering = 1; |
376 streaming_ctrl->status = streaming_playing_e; | |
377 | |
378 http_free( http_hdr ); | |
905 | 379 return fd; |
380 } | |
381 |