871
|
1 #include <stdio.h>
|
|
2 #include <stdlib.h>
|
833
|
3 #include <string.h>
|
|
4
|
871
|
5 #include "asf.h"
|
|
6 #include "url.h"
|
|
7 #include "http.h"
|
905
|
8 #include "network.h"
|
871
|
9
|
905
|
10 #define BUFFER_SIZE 2048
|
871
|
11
|
|
12 const char *temp_response =
|
|
13 "HTTP/1.0 200 OK\r\n"
|
|
14 "Date: Tue, 20 Mar 2001 11:40:35 GMT\r\n"
|
|
15 "Content-Type: application/octet-stream\r\n"
|
|
16 "Server: Cougar 4.1.0.3920\r\n"
|
|
17 "Cache-Control: no-cache\r\n"
|
|
18 "Pragma: no-cache, client-id=290092, features=\"broadcast\"\r\n"
|
|
19 /* "Pragma: no-cache\r\n"
|
|
20 "Pragma: client-id=290092\r\n"
|
|
21 "Pragma: features=\"broadcast\"\r\n"
|
|
22 */ "\r\n";
|
833
|
23
|
905
|
24 static ASF_StreamType_e streaming_type = ASF_Unknown_e;
|
871
|
25
|
|
26 void
|
|
27 asf_streaming(char *data, int length) {
|
833
|
28 ASF_stream_chunck_t *stream_chunck=(ASF_stream_chunck_t*)data;
|
871
|
29 printf("ASF stream chunck size=%d\n", stream_chunck->size);
|
|
30
|
|
31 if( stream_chunck->size<8 ) {
|
|
32 printf("Ahhhh, stream_chunck size is too small: %d\n", stream_chunck->size);
|
|
33 return;
|
|
34 }
|
|
35 if( stream_chunck->size!=stream_chunck->size_confirm ) {
|
|
36 printf("size_confirm mismatch!: %d %d\n", stream_chunck->size, stream_chunck->size_confirm);
|
|
37 return;
|
|
38 }
|
833
|
39
|
|
40 switch(stream_chunck->type) {
|
|
41 case 0x4324: // Clear ASF configuration
|
871
|
42 printf("=====> Clearing ASF stream configuration!\n");
|
833
|
43 break;
|
|
44 case 0x4424: // Data follows
|
871
|
45 printf("=====> Data follows\n");
|
833
|
46 break;
|
|
47 case 0x4524: // Transfer complete
|
871
|
48 printf("=====> Transfer complete\n");
|
833
|
49 break;
|
|
50 case 0x4824: // ASF header chunk follows
|
871
|
51 printf("=====> ASF header chunk follows\n");
|
833
|
52 break;
|
|
53 default:
|
871
|
54 printf("=====> Unknown stream type 0x%x\n", stream_chunck->type );
|
833
|
55 }
|
|
56 }
|
|
57
|
905
|
58 int
|
|
59 asf_http_streaming_type(char *content_type, char *features) {
|
|
60 if( content_type==NULL ) return ASF_Unknown_e;
|
833
|
61 if( !strcasecmp(content_type, "application/octet-stream") ) {
|
905
|
62 if( features==NULL ) {
|
|
63 printf("=====> Prerecorded\n");
|
|
64 return ASF_Prerecorded_e;
|
|
65 } else if( strstr(features, "broadcast")) {
|
871
|
66 printf("=====> Live stream\n");
|
905
|
67 return ASF_Live_e;
|
833
|
68 } else {
|
871
|
69 printf("=====> Prerecorded\n");
|
905
|
70 return ASF_Prerecorded_e;
|
833
|
71 }
|
|
72 } else {
|
871
|
73 if( (!strcasecmp(content_type, "audio/x-ms-wax")) ||
|
833
|
74 (!strcasecmp(content_type, "audio/x-ms-wma")) ||
|
|
75 (!strcasecmp(content_type, "video/x-ms-asf")) ||
|
|
76 (!strcasecmp(content_type, "video/x-ms-afs")) ||
|
|
77 (!strcasecmp(content_type, "video/x-ms-wvx")) ||
|
|
78 (!strcasecmp(content_type, "video/x-ms-wmv")) ||
|
|
79 (!strcasecmp(content_type, "video/x-ms-wma")) ) {
|
871
|
80 printf("=====> Redirector\n");
|
905
|
81 return ASF_Redirector_e;
|
833
|
82 } else {
|
871
|
83 printf("=====> unknown content-type: %s\n", content_type );
|
905
|
84 return ASF_Unknown_e;
|
833
|
85 }
|
|
86 }
|
905
|
87 return ASF_Unknown_e;
|
833
|
88 }
|
871
|
89
|
|
90 //void asf_http_request(stream_t *stream, URL_t *url) {
|
905
|
91 HTTP_header_t *
|
871
|
92 asf_http_request(URL_t *url) {
|
|
93 HTTP_header_t *http_hdr;
|
|
94 char str[250];
|
|
95 char *request;
|
|
96
|
|
97 int offset_hi=0, offset_lo=0, req_nb=1, length=0;
|
905
|
98 int asf_nb_stream=1; // FIXME
|
871
|
99
|
|
100 // Common header for all requests.
|
|
101 http_hdr = http_new_header();
|
|
102 http_set_uri( http_hdr, url->file );
|
|
103 http_set_field( http_hdr, "Accept: */*" );
|
|
104 http_set_field( http_hdr, "User-Agent: NSPlayer/4.1.0.3856" );
|
|
105 sprintf( str, "Host: %s:%d", url->hostname, url->port );
|
|
106 http_set_field( http_hdr, str );
|
|
107 http_set_field( http_hdr, "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}" );
|
|
108 sprintf(str,
|
|
109 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=%u",
|
|
110 offset_hi, offset_lo, req_nb, length );
|
|
111 http_set_field( http_hdr, str );
|
|
112
|
905
|
113 switch( streaming_type ) {
|
871
|
114 case ASF_Live_e:
|
|
115 case ASF_Prerecorded_e:
|
|
116 http_set_field( http_hdr, "Pragma: xPlayStrm=1" );
|
|
117 sprintf( str, "Pragma: stream-switch-count=%d", asf_nb_stream );
|
|
118 http_set_field( http_hdr, str );
|
905
|
119 http_set_field( http_hdr, "Pragma: stream-switch-entry=ffff:1:0" ); // FIXME
|
871
|
120 break;
|
|
121 case ASF_Redirector_e:
|
|
122 break;
|
|
123 case ASF_Unknown_e:
|
|
124 // First request goes here.
|
|
125 break;
|
|
126 default:
|
|
127 printf("Unknown asf stream type\n");
|
|
128 }
|
|
129
|
|
130 http_set_field( http_hdr, "Connection: Close" );
|
905
|
131 http_build_request( http_hdr );
|
871
|
132
|
905
|
133 return http_hdr;
|
871
|
134 }
|
|
135
|
|
136 int
|
905
|
137 asf_http_parse_response( HTTP_header_t *http_hdr ) {
|
871
|
138 char *content_type, *pragma;
|
|
139 char features[64] = "\0";
|
|
140 int len;
|
905
|
141 if( http_response_parse(http_hdr)<0 ) {
|
|
142 printf("Failed to parse HTTP response\n");
|
|
143 return -1;
|
|
144 }
|
871
|
145 if( http_hdr->status_code!=200 ) {
|
|
146 printf("Server return %d:%s\n", http_hdr->status_code, http_hdr->reason_phrase);
|
|
147 return -1;
|
|
148 }
|
|
149
|
|
150 content_type = http_get_field( http_hdr, "Content-Type");
|
|
151
|
|
152 pragma = http_get_field( http_hdr, "Pragma");
|
905
|
153 while( pragma!=NULL ) {
|
871
|
154 char *comma_ptr=NULL;
|
|
155 char *end;
|
|
156 // The pragma line can get severals attributes
|
|
157 // separeted with a comma ','.
|
|
158 do {
|
|
159 if( !strncasecmp( pragma, "features=", 9) ) {
|
|
160 pragma += 9;
|
|
161 end = strstr( pragma, "," );
|
|
162 if( end==NULL ) {
|
|
163 len = strlen(pragma);
|
|
164 }
|
|
165 len = MIN(end-pragma,sizeof(features));
|
|
166 strncpy( features, pragma, len );
|
|
167 features[len]='\0';
|
|
168 break;
|
|
169 }
|
|
170 comma_ptr = strstr( pragma, "," );
|
|
171 if( comma_ptr!=NULL ) {
|
|
172 pragma = comma_ptr+1;
|
|
173 if( pragma[0]==' ' ) pragma++;
|
|
174 }
|
|
175 } while( comma_ptr!=NULL );
|
|
176 pragma = http_get_next_field( http_hdr );
|
905
|
177 }
|
|
178
|
|
179 streaming_type = asf_http_streaming_type( content_type, features );
|
871
|
180
|
905
|
181 if( http_hdr->body_size>0 ) {
|
|
182 asf_streaming( http_hdr->body, http_hdr->body_size );
|
|
183 }
|
871
|
184
|
|
185 return 0;
|
|
186 }
|
|
187
|
905
|
188 URL_t *
|
|
189 asf_http_ASX_redirect( HTTP_header_t *http_hdr ) {
|
|
190 URL_t *url_redirect=NULL;
|
|
191 printf("=========>> ASX parser not yet implemented <<==========\n");
|
|
192
|
|
193 printf("ASX=[%s]\n", http_hdr->body );
|
|
194
|
|
195 return url_redirect;
|
871
|
196 }
|
905
|
197
|
|
198 int
|
|
199 asf_http_streaming_start( URL_t **url_ref ) {
|
|
200 HTTP_header_t *http_hdr=NULL;
|
|
201 URL_t *url_next=NULL;
|
|
202 URL_t *url=*url_ref;
|
|
203 char buffer[BUFFER_SIZE];
|
|
204 int i;
|
|
205 int fd=-1;
|
|
206 int done=1;
|
|
207 do {
|
|
208 if( fd>0 ) close( fd );
|
|
209 fd = connect2Server( url->hostname, url->port );
|
|
210 if( fd<0 ) return -1;
|
|
211
|
|
212 http_hdr = asf_http_request( url );
|
|
213 //printf("[%s]\n", http_hdr->buffer );
|
|
214 write( fd, http_hdr->buffer, http_hdr->buffer_size );
|
|
215 http_free( http_hdr );
|
|
216
|
|
217 http_hdr = http_new_header();
|
|
218 do {
|
|
219 i = read( fd, buffer, BUFFER_SIZE );
|
|
220 printf("read: %d\n", i );
|
|
221 http_response_append( http_hdr, buffer, i );
|
|
222 } while( !http_is_header_entired( http_hdr ) );
|
|
223 //http_hdr->buffer[http_hdr->buffer_len]='\0';
|
|
224 //printf("[%s]\n", http_hdr->buffer );
|
|
225 if( asf_http_parse_response(http_hdr)<0 ) {
|
|
226 printf("Failed to parse header\n");
|
|
227 return -1;
|
|
228 }
|
|
229
|
|
230 switch(streaming_type) {
|
|
231 case ASF_Live_e:
|
|
232 case ASF_Prerecorded_e:
|
|
233 if( http_hdr->body_size==0 ) {
|
|
234 i = read( fd, buffer, BUFFER_SIZE );
|
|
235 printf("read: %d\n", i );
|
|
236 asf_streaming( buffer, i );
|
|
237 }
|
|
238 break;
|
|
239 case ASF_Redirector_e:
|
|
240 url_next = asf_http_ASX_redirect( http_hdr );
|
|
241 if( url_next==NULL ) {
|
|
242 printf("Failed to parse ASX file\n");
|
|
243 close(fd);
|
|
244 return -1;
|
|
245 }
|
|
246 if( url_next->port==0 ) url_next->port=80;
|
|
247 url_free( url );
|
|
248 url = url_next;
|
|
249 *url_ref = url_next;
|
|
250 url_next = NULL;
|
|
251 break;
|
|
252 case ASF_Unknown_e:
|
|
253 default:
|
|
254 printf("Unknown ASF streaming type\n");
|
|
255 close(fd);
|
|
256 return -1;
|
|
257 }
|
|
258
|
|
259 // Check if we got a redirect.
|
|
260 } while(!done);
|
|
261
|
|
262 return fd;
|
|
263 }
|
|
264
|