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"
|
|
8
|
|
9
|
|
10 const char *temp_response =
|
|
11 "HTTP/1.0 200 OK\r\n"
|
|
12 "Date: Tue, 20 Mar 2001 11:40:35 GMT\r\n"
|
|
13 "Content-Type: application/octet-stream\r\n"
|
|
14 "Server: Cougar 4.1.0.3920\r\n"
|
|
15 "Cache-Control: no-cache\r\n"
|
|
16 "Pragma: no-cache, client-id=290092, features=\"broadcast\"\r\n"
|
|
17 /* "Pragma: no-cache\r\n"
|
|
18 "Pragma: client-id=290092\r\n"
|
|
19 "Pragma: features=\"broadcast\"\r\n"
|
|
20 */ "\r\n";
|
833
|
21
|
871
|
22 static ASF_StreamType_e stream_type = ASF_Unknown_e;
|
|
23
|
|
24
|
|
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
|
871
|
58 void
|
|
59 asf_stream_type(char *content_type, char *features) {
|
833
|
60 stream_type = ASF_Unknown_e;
|
|
61 if( !strcasecmp(content_type, "application/octet-stream") ) {
|
|
62 if( strstr(features, "broadcast")) {
|
871
|
63 printf("=====> Live stream\n");
|
833
|
64 stream_type = ASF_Live_e;
|
|
65 } else {
|
871
|
66 printf("=====> Prerecorded\n");
|
833
|
67 stream_type = ASF_Prerecorded_e;
|
|
68 }
|
|
69 } else {
|
871
|
70 if( (!strcasecmp(content_type, "audio/x-ms-wax")) ||
|
833
|
71 (!strcasecmp(content_type, "audio/x-ms-wma")) ||
|
|
72 (!strcasecmp(content_type, "video/x-ms-asf")) ||
|
|
73 (!strcasecmp(content_type, "video/x-ms-afs")) ||
|
|
74 (!strcasecmp(content_type, "video/x-ms-wvx")) ||
|
|
75 (!strcasecmp(content_type, "video/x-ms-wmv")) ||
|
|
76 (!strcasecmp(content_type, "video/x-ms-wma")) ) {
|
871
|
77 printf("=====> Redirector\n");
|
833
|
78 stream_type = ASF_Redirector_e;
|
|
79 } else {
|
871
|
80 printf("=====> unknown content-type: %s\n", content_type );
|
833
|
81 stream_type = ASF_Unknown_e;
|
|
82 }
|
|
83 }
|
|
84 }
|
871
|
85
|
|
86 //void asf_http_request(stream_t *stream, URL_t *url) {
|
|
87 void
|
|
88 asf_http_request(URL_t *url) {
|
|
89 HTTP_header_t *http_hdr;
|
|
90 char str[250];
|
|
91 char *request;
|
|
92 // int size;
|
|
93
|
|
94 int offset_hi=0, offset_lo=0, req_nb=1, length=0;
|
|
95 int asf_nb_stream=1;
|
|
96
|
|
97 // Common header for all requests.
|
|
98 http_hdr = http_new_header();
|
|
99 http_set_uri( http_hdr, url->file );
|
|
100 http_set_field( http_hdr, "Accept: */*" );
|
|
101 http_set_field( http_hdr, "User-Agent: NSPlayer/4.1.0.3856" );
|
|
102 sprintf( str, "Host: %s:%d", url->hostname, url->port );
|
|
103 http_set_field( http_hdr, str );
|
|
104 http_set_field( http_hdr, "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}" );
|
|
105 sprintf(str,
|
|
106 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=%u",
|
|
107 offset_hi, offset_lo, req_nb, length );
|
|
108 http_set_field( http_hdr, str );
|
|
109
|
|
110 switch( stream_type ) {
|
|
111 case ASF_Live_e:
|
|
112 case ASF_Prerecorded_e:
|
|
113 http_set_field( http_hdr, "Pragma: xPlayStrm=1" );
|
|
114 sprintf( str, "Pragma: stream-switch-count=%d", asf_nb_stream );
|
|
115 http_set_field( http_hdr, str );
|
|
116 http_set_field( http_hdr, "Pragma: stream-switch-entry=ffff:1:0" );
|
|
117 break;
|
|
118 case ASF_Redirector_e:
|
|
119 break;
|
|
120 case ASF_Unknown_e:
|
|
121 // First request goes here.
|
|
122 break;
|
|
123 default:
|
|
124 printf("Unknown asf stream type\n");
|
|
125 }
|
|
126
|
|
127 http_set_field( http_hdr, "Connection: Close" );
|
|
128 request = http_get_request( http_hdr );
|
|
129
|
|
130 printf("%s\n", request );
|
|
131
|
|
132 }
|
|
133
|
|
134 int
|
|
135 asf_http_parse_response( char *response, int length ) {
|
|
136 HTTP_header_t *http_hdr;
|
|
137 char *content_type, *pragma;
|
|
138 char features[64] = "\0";
|
|
139 int len;
|
|
140
|
|
141 http_hdr = http_new_response( response, length );
|
|
142
|
|
143 if( http_hdr->status_code!=200 ) {
|
|
144 printf("Server return %d:%s\n", http_hdr->status_code, http_hdr->reason_phrase);
|
|
145 return -1;
|
|
146 }
|
|
147
|
|
148 content_type = http_get_field( http_hdr, "Content-Type");
|
|
149
|
|
150 pragma = http_get_field( http_hdr, "Pragma");
|
|
151 do {
|
|
152 char *comma_ptr=NULL;
|
|
153 char *end;
|
|
154 // The pragma line can get severals attributes
|
|
155 // separeted with a comma ','.
|
|
156 do {
|
|
157 if( !strncasecmp( pragma, "features=", 9) ) {
|
|
158 pragma += 9;
|
|
159 end = strstr( pragma, "," );
|
|
160 if( end==NULL ) {
|
|
161 len = strlen(pragma);
|
|
162 }
|
|
163 len = MIN(end-pragma,sizeof(features));
|
|
164 strncpy( features, pragma, len );
|
|
165 features[len]='\0';
|
|
166 break;
|
|
167 }
|
|
168 comma_ptr = strstr( pragma, "," );
|
|
169 if( comma_ptr!=NULL ) {
|
|
170 pragma = comma_ptr+1;
|
|
171 if( pragma[0]==' ' ) pragma++;
|
|
172 }
|
|
173 } while( comma_ptr!=NULL );
|
|
174 pragma = http_get_next_field( http_hdr );
|
|
175 } while( pragma!=NULL );
|
|
176
|
|
177 asf_stream_type( content_type, features );
|
|
178
|
|
179 return 0;
|
|
180 }
|
|
181
|
|
182 #ifdef STREAMING_TEST
|
|
183 int main() {
|
|
184 URL_t *url = set_url("http://toto.com:12/coucou");
|
|
185 asf_http_request( url );
|
|
186 asf_http_parse_response( temp_response, strlen(temp_response) );
|
|
187 asf_http_request( url );
|
|
188 return 0;
|
|
189 }
|
|
190 #endif
|