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