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