~ubuntu-dev/mplayer/ubuntu-feisty

« back to all changes in this revision

Viewing changes to libmpdemux/asf_streaming.c

  • Committer: Reinhard Tartler
  • Date: 2006-07-08 08:45:33 UTC
  • Revision ID: siretart@tauware.de-20060708084533-dbc155bde7122e78
imported mplayer_0.99+1.0pre7try2+cvs20060117

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdlib.h>
 
3
#include <string.h>
 
4
#include <unistd.h>
 
5
#include <errno.h>
 
6
#include <limits.h>
 
7
 
 
8
#include "config.h"
 
9
#include "mp_msg.h"
 
10
#include "help_mp.h"
 
11
 
 
12
#ifndef HAVE_WINSOCK2
 
13
#define closesocket close
 
14
#else
 
15
#include <winsock2.h>
 
16
#endif
 
17
 
 
18
#include "url.h"
 
19
#include "http.h"
 
20
#include "asf.h"
 
21
 
 
22
#include "stream.h"
 
23
#include "demuxer.h"
 
24
 
 
25
#include "network.h"
 
26
 
 
27
#ifdef ARCH_X86
 
28
#define ASF_LOAD_GUID_PREFIX(guid)      (*(uint32_t *)(guid))
 
29
#else
 
30
#define ASF_LOAD_GUID_PREFIX(guid)      \
 
31
        ((guid)[3] << 24 | (guid)[2] << 16 | (guid)[1] << 8 | (guid)[0])
 
32
#endif
 
33
 
 
34
extern int verbose;
 
35
extern int network_bandwidth;
 
36
 
 
37
int asf_mmst_streaming_start( stream_t *stream );
 
38
static int asf_http_streaming_start(stream_t *stream, int *demuxer_type);
 
39
 
 
40
// We can try several protocol for asf streaming
 
41
// * first the UDP protcol, if there is a firewall, UDP
 
42
//   packets will not come back, so the mmsu will fail.
 
43
// * Then we can try TCP, but if there is a proxy for
 
44
//   internet connection, the TCP connection will not get
 
45
//   through
 
46
// * Then we can try HTTP.
 
47
// 
 
48
// Note: Using  WMP sequence  MMSU then MMST and then HTTP.
 
49
 
 
50
static int asf_streaming_start( stream_t *stream, int *demuxer_type) {
 
51
    char *proto = stream->streaming_ctrl->url->protocol;
 
52
    int fd = -1;
 
53
    int port = stream->streaming_ctrl->url->port;
 
54
 
 
55
    // Is protocol mms or mmsu?
 
56
    if (!strcasecmp(proto, "mmsu") || !strcasecmp(proto, "mms"))
 
57
    {
 
58
                mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/UDP...\n");
 
59
                //fd = asf_mmsu_streaming_start( stream );
 
60
                if( fd>-1 ) return fd; //mmsu support is not implemented yet - using this code
 
61
                mp_msg(MSGT_NETWORK,MSGL_V,"  ===> ASF/UDP failed\n");
 
62
                if( fd==-2 ) return -1;
 
63
        }
 
64
 
 
65
    //Is protocol mms or mmst?
 
66
    if (!strcasecmp(proto, "mmst") || !strcasecmp(proto, "mms"))
 
67
    {
 
68
                mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/TCP...\n");
 
69
                fd = asf_mmst_streaming_start( stream );
 
70
                stream->streaming_ctrl->url->port = port;
 
71
                if( fd>-1 ) return fd;
 
72
                mp_msg(MSGT_NETWORK,MSGL_V,"  ===> ASF/TCP failed\n");
 
73
                if( fd==-2 ) return -1;
 
74
        }
 
75
 
 
76
    //Is protocol http, http_proxy, or mms? 
 
77
    if (!strcasecmp(proto, "http_proxy") || !strcasecmp(proto, "http") ||
 
78
        !strcasecmp(proto, "mms") || !strcasecmp(proto, "mmshttp"))
 
79
    {
 
80
                mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/HTTP...\n");
 
81
                fd = asf_http_streaming_start( stream, demuxer_type );
 
82
                stream->streaming_ctrl->url->port = port;
 
83
                if( fd>-1 ) return fd;
 
84
                mp_msg(MSGT_NETWORK,MSGL_V,"  ===> ASF/HTTP failed\n");
 
85
                if( fd==-2 ) return -1;
 
86
        }
 
87
 
 
88
    //everything failed
 
89
        return -1;
 
90
}
 
91
 
 
92
static int asf_streaming(ASF_stream_chunck_t *stream_chunck, int *drop_packet ) {
 
93
/*      
 
94
printf("ASF stream chunck size=%d\n", stream_chunck->size);
 
95
printf("length: %d\n", length );
 
96
printf("0x%02X\n", stream_chunck->type );
 
97
*/
 
98
        if( drop_packet!=NULL ) *drop_packet = 0;
 
99
 
 
100
        if( stream_chunck->size<8 ) {
 
101
                mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_StreamChunkSize2Small, stream_chunck->size);
 
102
                return -1;
 
103
        }
 
104
        if( stream_chunck->size!=stream_chunck->size_confirm ) {
 
105
                mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_SizeConfirmMismatch, stream_chunck->size, stream_chunck->size_confirm);
 
106
                return -1;
 
107
        }
 
108
/*      
 
109
        printf("  type: 0x%02X\n", stream_chunck->type );
 
110
        printf("  size: %d (0x%02X)\n", stream_chunck->size, stream_chunck->size );
 
111
        printf("  sequence_number: 0x%04X\n", stream_chunck->sequence_number );
 
112
        printf("  unknown: 0x%02X\n", stream_chunck->unknown );
 
113
        printf("  size_confirm: 0x%02X\n", stream_chunck->size_confirm );
 
114
*/
 
115
        switch(stream_chunck->type) {
 
116
                case ASF_STREAMING_CLEAR:       // $C   Clear ASF configuration
 
117
                        mp_msg(MSGT_NETWORK,MSGL_V,"=====> Clearing ASF stream configuration!\n");
 
118
                        if( drop_packet!=NULL ) *drop_packet = 1;
 
119
                        return stream_chunck->size;
 
120
                        break;
 
121
                case ASF_STREAMING_DATA:        // $D   Data follows
 
122
//                      printf("=====> Data follows\n");
 
123
                        break;
 
124
                case ASF_STREAMING_END_TRANS:   // $E   Transfer complete
 
125
                        mp_msg(MSGT_NETWORK,MSGL_V,"=====> Transfer complete\n");
 
126
                        if( drop_packet!=NULL ) *drop_packet = 1;
 
127
                        return stream_chunck->size;
 
128
                        break;
 
129
                case ASF_STREAMING_HEADER:      // $H   ASF header chunk follows
 
130
                        mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF header chunk follows\n");
 
131
                        break;
 
132
                default:
 
133
                        mp_msg(MSGT_NETWORK,MSGL_V,"=====> Unknown stream type 0x%x\n", stream_chunck->type );
 
134
        }
 
135
        return stream_chunck->size+4;
 
136
}
 
137
 
 
138
extern int find_asf_guid(char *buf, const char *guid, int cur_pos, int buf_len);
 
139
extern const char asf_file_header_guid[];
 
140
extern const char asf_stream_header_guid[];
 
141
extern const char asf_stream_group_guid[];
 
142
extern int audio_id;
 
143
extern int video_id;
 
144
 
 
145
static void close_s(stream_t *stream) {
 
146
        close(stream->fd);
 
147
        stream->fd=-1;
 
148
}
 
149
 
 
150
static int max_idx(int s_count, int *s_rates, int bound) {
 
151
  int i, best = -1, rate = -1;
 
152
  for (i = 0; i < s_count; i++) {
 
153
    if (s_rates[i] > rate && s_rates[i] <= bound) {
 
154
      rate = s_rates[i];
 
155
      best = i;
 
156
    }
 
157
  }
 
158
  return best;
 
159
}
 
160
 
 
161
static int asf_streaming_parse_header(int fd, streaming_ctrl_t* streaming_ctrl) {
 
162
  ASF_header_t asfh;
 
163
  ASF_stream_chunck_t chunk;
 
164
  asf_http_streaming_ctrl_t* asf_ctrl = (asf_http_streaming_ctrl_t*) streaming_ctrl->data;
 
165
  char* buffer=NULL, *chunk_buffer=NULL;
 
166
  int i,r,size,pos = 0;
 
167
  int start;
 
168
  int buffer_size = 0;
 
169
  int chunk_size2read = 0;
 
170
  int bw = streaming_ctrl->bandwidth;
 
171
  int *v_rates = NULL, *a_rates = NULL;
 
172
  int v_rate = 0, a_rate = 0, a_idx = -1, v_idx = -1;
 
173
  
 
174
  if(asf_ctrl == NULL) return -1;
 
175
 
 
176
        // The ASF header can be in several network chunks. For example if the content description
 
177
        // is big, the ASF header will be split in 2 network chunk.
 
178
        // So we need to retrieve all the chunk before starting to parse the header.
 
179
  do {
 
180
          for( r=0; r < (int)sizeof(ASF_stream_chunck_t) ; ) {
 
181
                i = nop_streaming_read(fd,((char*)&chunk)+r,sizeof(ASF_stream_chunck_t) - r,streaming_ctrl);
 
182
                if(i <= 0) return -1;
 
183
                r += i;
 
184
          }
 
185
          // Endian handling of the stream chunk
 
186
          le2me_ASF_stream_chunck_t(&chunk);
 
187
          size = asf_streaming( &chunk, &r) - sizeof(ASF_stream_chunck_t);
 
188
          if(r) mp_msg(MSGT_NETWORK,MSGL_WARN,MSGTR_MPDEMUX_ASF_WarnDropHeader);
 
189
          if(size < 0){
 
190
            mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader);
 
191
                return -1;
 
192
          }
 
193
          if (chunk.type != ASF_STREAMING_HEADER) {
 
194
            mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_NoHeaderAtFirstChunk);
 
195
            return -1;
 
196
          }
 
197
          
 
198
          buffer = (char*) malloc(size+buffer_size);
 
199
          if(buffer == NULL) {
 
200
            mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MPDEMUX_ASF_BufferMallocFailed,size+buffer_size);
 
201
            return -1;
 
202
          }
 
203
          if( chunk_buffer!=NULL ) {
 
204
                memcpy( buffer, chunk_buffer, buffer_size );
 
205
                free( chunk_buffer );
 
206
          }
 
207
          chunk_buffer = buffer;
 
208
          buffer += buffer_size;
 
209
          buffer_size += size;
 
210
          
 
211
          for(r = 0; r < size;) {
 
212
            i = nop_streaming_read(fd,buffer+r,size-r,streaming_ctrl);
 
213
            if(i < 0) {
 
214
                    mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrReadingNetworkStream);
 
215
                    return -1;
 
216
            }
 
217
            r += i;
 
218
          }  
 
219
 
 
220
          if( chunk_size2read==0 ) {
 
221
                if(size < (int)sizeof(asfh)) {
 
222
                    mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrChunk2Small);
 
223
                    return -1;
 
224
                } else mp_msg(MSGT_NETWORK,MSGL_DBG2,"Got chunk\n");
 
225
                memcpy(&asfh,buffer,sizeof(asfh));
 
226
                le2me_ASF_header_t(&asfh);
 
227
                chunk_size2read = asfh.objh.size;
 
228
                mp_msg(MSGT_NETWORK,MSGL_DBG2,"Size 2 read=%d\n", chunk_size2read);
 
229
          }
 
230
  } while( buffer_size<chunk_size2read);
 
231
  buffer = chunk_buffer;
 
232
  size = buffer_size;
 
233
          
 
234
  if(asfh.cno > 256) {
 
235
    mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrSubChunkNumberInvalid);
 
236
    return -1;
 
237
  }
 
238
 
 
239
  start = sizeof(asfh);
 
240
  
 
241
  pos = find_asf_guid(buffer, asf_file_header_guid, start, size);
 
242
  if (pos >= 0) {
 
243
    ASF_file_header_t *fileh = (ASF_file_header_t *) &buffer[pos];
 
244
    pos += sizeof(ASF_file_header_t);
 
245
    if (pos > size) goto len_err_out;
 
246
      le2me_ASF_file_header_t(fileh);
 
247
/*
 
248
      if(fileh.packetsize != fileh.packetsize2) {
 
249
        printf("Error packetsize check don't match\n");
 
250
        return -1;
 
251
      }
 
252
*/
 
253
      asf_ctrl->packet_size = fileh->max_packet_size;
 
254
      // before playing. 
 
255
      // preroll: time in ms to bufferize before playing
 
256
      streaming_ctrl->prebuffer_size = (unsigned int)(((double)fileh->preroll/1000.0)*((double)fileh->max_bitrate/8.0));
 
257
  }
 
258
 
 
259
  pos = start;
 
260
  while ((pos = find_asf_guid(buffer, asf_stream_header_guid, pos, size)) >= 0)
 
261
  {
 
262
    ASF_stream_header_t *streamh = (ASF_stream_header_t *)&buffer[pos];
 
263
    pos += sizeof(ASF_stream_header_t);
 
264
    if (pos > size) goto len_err_out;
 
265
      le2me_ASF_stream_header_t(streamh);
 
266
      switch(ASF_LOAD_GUID_PREFIX(streamh->type)) {
 
267
      case 0xF8699E40 : // audio stream
 
268
        if(asf_ctrl->audio_streams == NULL){
 
269
          asf_ctrl->audio_streams = (int*)malloc(sizeof(int));
 
270
          asf_ctrl->n_audio = 1;
 
271
        } else {
 
272
          asf_ctrl->n_audio++;
 
273
          asf_ctrl->audio_streams = (int*)realloc(asf_ctrl->audio_streams,
 
274
                                                     asf_ctrl->n_audio*sizeof(int));
 
275
        }
 
276
        asf_ctrl->audio_streams[asf_ctrl->n_audio-1] = streamh->stream_no;
 
277
        break;
 
278
      case 0xBC19EFC0 : // video stream
 
279
        if(asf_ctrl->video_streams == NULL){
 
280
          asf_ctrl->video_streams = (int*)malloc(sizeof(int));
 
281
          asf_ctrl->n_video = 1;
 
282
        } else {
 
283
          asf_ctrl->n_video++;
 
284
          asf_ctrl->video_streams = (int*)realloc(asf_ctrl->video_streams,
 
285
                                                     asf_ctrl->n_video*sizeof(int));
 
286
        }
 
287
        asf_ctrl->video_streams[asf_ctrl->n_video-1] = streamh->stream_no;
 
288
        break;
 
289
      }
 
290
  }
 
291
 
 
292
  // always allocate to avoid lots of ifs later
 
293
  v_rates = calloc(asf_ctrl->n_video, sizeof(int));
 
294
  a_rates = calloc(asf_ctrl->n_audio, sizeof(int));
 
295
 
 
296
  pos = find_asf_guid(buffer, asf_stream_group_guid, start, size);
 
297
  if (pos >= 0) {
 
298
    // stream bitrate properties object
 
299
        int stream_count;
 
300
        char *ptr = &buffer[pos];
 
301
        
 
302
        mp_msg(MSGT_NETWORK, MSGL_V, "Stream bitrate properties object\n");
 
303
                stream_count = le2me_16(*(uint16_t*)ptr);
 
304
                ptr += sizeof(uint16_t);
 
305
                if (ptr > &buffer[size]) goto len_err_out;
 
306
                mp_msg(MSGT_NETWORK, MSGL_V, " stream count=[0x%x][%u]\n",
 
307
                        stream_count, stream_count );
 
308
                for( i=0 ; i<stream_count ; i++ ) {
 
309
                        uint32_t rate;
 
310
                        int id;
 
311
                        int j;
 
312
                        id = le2me_16(*(uint16_t*)ptr);
 
313
                        ptr += sizeof(uint16_t);
 
314
                        if (ptr > &buffer[size]) goto len_err_out;
 
315
                        memcpy(&rate, ptr, sizeof(uint32_t));// workaround unaligment bug on sparc
 
316
                        ptr += sizeof(uint32_t);
 
317
                        if (ptr > &buffer[size]) goto len_err_out;
 
318
                        rate = le2me_32(rate);
 
319
                        mp_msg(MSGT_NETWORK, MSGL_V,
 
320
                                "  stream id=[0x%x][%u]\n", id, id);
 
321
                        mp_msg(MSGT_NETWORK, MSGL_V,
 
322
                                "  max bitrate=[0x%x][%u]\n", rate, rate);
 
323
                        for (j = 0; j < asf_ctrl->n_video; j++) {
 
324
                          if (id == asf_ctrl->video_streams[j]) {
 
325
                            mp_msg(MSGT_NETWORK, MSGL_V, "  is video stream\n");
 
326
                            v_rates[j] = rate;
 
327
                            break;
 
328
                          }
 
329
                        }
 
330
                        for (j = 0; j < asf_ctrl->n_audio; j++) {
 
331
                          if (id == asf_ctrl->audio_streams[j]) {
 
332
                            mp_msg(MSGT_NETWORK, MSGL_V, "  is audio stream\n");
 
333
                            a_rates[j] = rate;
 
334
                            break;
 
335
                          }
 
336
                        }
 
337
                }
 
338
  }
 
339
  free(buffer);
 
340
 
 
341
  // automatic stream selection based on bandwidth
 
342
  if (bw == 0) bw = INT_MAX;
 
343
  mp_msg(MSGT_NETWORK, MSGL_V, "Max bandwidth set to %d\n", bw);
 
344
 
 
345
  if (asf_ctrl->n_audio) {
 
346
    // find lowest-bitrate audio stream
 
347
    a_rate = a_rates[0];
 
348
    a_idx = 0;
 
349
    for (i = 0; i < asf_ctrl->n_audio; i++) {
 
350
      if (a_rates[i] < a_rate) {
 
351
        a_rate = a_rates[i];
 
352
        a_idx = i;
 
353
      }
 
354
    }
 
355
    if (max_idx(asf_ctrl->n_video, v_rates, bw - a_rate) < 0) {
 
356
      // both audio and video are not possible, try video only next
 
357
      a_idx = -1;
 
358
      a_rate = 0;
 
359
    }
 
360
  }
 
361
  // find best video stream
 
362
  v_idx = max_idx(asf_ctrl->n_video, v_rates, bw - a_rate);
 
363
  if (v_idx >= 0)
 
364
    v_rate = v_rates[v_idx];
 
365
 
 
366
  // find best audio stream
 
367
  a_idx = max_idx(asf_ctrl->n_audio, a_rates, bw - v_rate);
 
368
    
 
369
  free(v_rates);
 
370
  free(a_rates);
 
371
 
 
372
  if (a_idx < 0 && v_idx < 0) {
 
373
    mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MPDEMUX_ASF_Bandwidth2SmallCannotPlay);
 
374
    return -1;
 
375
  }
 
376
 
 
377
  if (audio_id > 0)
 
378
    // a audio stream was forced
 
379
    asf_ctrl->audio_id = audio_id;
 
380
  else if (a_idx >= 0)
 
381
    asf_ctrl->audio_id = asf_ctrl->audio_streams[a_idx];
 
382
  else if (asf_ctrl->n_audio) {
 
383
    mp_msg(MSGT_NETWORK, MSGL_WARN, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedAudio);
 
384
    audio_id = -2;
 
385
  }
 
386
 
 
387
  if (video_id > 0)
 
388
    // a video stream was forced
 
389
    asf_ctrl->video_id = video_id;
 
390
  else if (v_idx >= 0)
 
391
    asf_ctrl->video_id = asf_ctrl->video_streams[v_idx];
 
392
  else if (asf_ctrl->n_video) {
 
393
    mp_msg(MSGT_NETWORK, MSGL_WARN, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedVideo);
 
394
    video_id = -2;
 
395
  }
 
396
 
 
397
  return 1;
 
398
 
 
399
len_err_out:
 
400
  mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MPDEMUX_ASF_InvalidLenInHeader);
 
401
  if (buffer) free(buffer);
 
402
  if (v_rates) free(v_rates);
 
403
  if (a_rates) free(a_rates);
 
404
  return -1;
 
405
}
 
406
 
 
407
static int asf_http_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *streaming_ctrl ) {
 
408
  static ASF_stream_chunck_t chunk;
 
409
  int read,chunk_size = 0;
 
410
  static int rest = 0, drop_chunk = 0, waiting = 0;
 
411
  asf_http_streaming_ctrl_t *asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data;
 
412
 
 
413
  while(1) {
 
414
    if (rest == 0 && waiting == 0) {
 
415
      read = 0;
 
416
      while(read < (int)sizeof(ASF_stream_chunck_t)){
 
417
        int r = nop_streaming_read( fd, ((char*)&chunk) + read, 
 
418
                                    sizeof(ASF_stream_chunck_t)-read, 
 
419
                                    streaming_ctrl );
 
420
        if(r <= 0){
 
421
          if( r < 0) 
 
422
            mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrReadingChunkHeader);
 
423
          return -1;
 
424
        }
 
425
        read += r;
 
426
      }
 
427
      
 
428
      // Endian handling of the stream chunk
 
429
      le2me_ASF_stream_chunck_t(&chunk);
 
430
      chunk_size = asf_streaming( &chunk, &drop_chunk );
 
431
      if(chunk_size < 0) {
 
432
        mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader);
 
433
        return -1;
 
434
      }
 
435
      chunk_size -= sizeof(ASF_stream_chunck_t);
 
436
        
 
437
      if(chunk.type != ASF_STREAMING_HEADER && (!drop_chunk)) {
 
438
        if (asf_http_ctrl->packet_size < chunk_size) {
 
439
          mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrChunkBiggerThanPacket);
 
440
          return -1;
 
441
        }
 
442
        waiting = asf_http_ctrl->packet_size;
 
443
      } else {
 
444
        waiting = chunk_size;
 
445
      }
 
446
 
 
447
    } else if (rest){
 
448
      chunk_size = rest;
 
449
      rest = 0;
 
450
    }
 
451
 
 
452
    read = 0;
 
453
    if ( waiting >= chunk_size) {
 
454
      if (chunk_size > size){
 
455
        rest = chunk_size - size;
 
456
        chunk_size = size;
 
457
      }
 
458
      while(read < chunk_size) {
 
459
        int got = nop_streaming_read( fd,buffer+read,chunk_size-read,streaming_ctrl );
 
460
        if(got <= 0) {
 
461
          if(got < 0)
 
462
            mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrReadingChunk);
 
463
          return -1;
 
464
        }
 
465
        read += got;
 
466
      }
 
467
      waiting -= read;
 
468
      if (drop_chunk) continue;
 
469
    }
 
470
    if (rest == 0 && waiting > 0 && size-read > 0) {
 
471
      int s = MIN(waiting,size-read);
 
472
      memset(buffer+read,0,s);
 
473
      waiting -= s;
 
474
      read += s;
 
475
    }
 
476
    break;
 
477
  }
 
478
 
 
479
  return read;
 
480
}
 
481
 
 
482
static int asf_http_streaming_seek( int fd, off_t pos, streaming_ctrl_t *streaming_ctrl ) {
 
483
        return -1;
 
484
        // to shut up gcc warning
 
485
        fd++;
 
486
        pos++;
 
487
        streaming_ctrl=NULL;
 
488
}
 
489
 
 
490
static int asf_header_check( HTTP_header_t *http_hdr ) {
 
491
        ASF_obj_header_t *objh;
 
492
        if( http_hdr==NULL ) return -1;
 
493
        if( http_hdr->body==NULL || http_hdr->body_size<sizeof(ASF_obj_header_t) ) return -1;
 
494
 
 
495
        objh = (ASF_obj_header_t*)http_hdr->body;
 
496
        if( ASF_LOAD_GUID_PREFIX(objh->guid)==0x75B22630 ) return 0;
 
497
        return -1;
 
498
}
 
499
 
 
500
static int asf_http_streaming_type(char *content_type, char *features, HTTP_header_t *http_hdr ) {
 
501
        if( content_type==NULL ) return ASF_Unknown_e;
 
502
        if(     !strcasecmp(content_type, "application/octet-stream") ||
 
503
                !strcasecmp(content_type, "application/vnd.ms.wms-hdr.asfv1") ||        // New in Corona, first request
 
504
                !strcasecmp(content_type, "application/x-mms-framed") ||                // New in Corana, second request
 
505
                !strcasecmp(content_type, "video/x-ms-asf")) {               
 
506
 
 
507
                if( strstr(features, "broadcast") ) {
 
508
                        mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Live stream\n");
 
509
                        return ASF_Live_e;
 
510
                } else {
 
511
                        mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Prerecorded\n");
 
512
                        return ASF_Prerecorded_e;
 
513
                }
 
514
        } else {
 
515
                // Ok in a perfect world, web servers should be well configured
 
516
                // so we could used mime type to know the stream type,
 
517
                // but guess what? All of them are not well configured.
 
518
                // So we have to check for an asf header :(, but it works :p
 
519
                if( http_hdr->body_size>sizeof(ASF_obj_header_t) ) {
 
520
                        if( asf_header_check( http_hdr )==0 ) {
 
521
                                mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n");
 
522
                                return ASF_PlainText_e;
 
523
                        } else if( (!strcasecmp(content_type, "text/html")) ) {
 
524
                                mp_msg(MSGT_NETWORK,MSGL_V,"=====> HTML, MPlayer is not a browser...yet!\n");
 
525
                                return ASF_Unknown_e;
 
526
                        } else {
 
527
                                mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Redirector\n");
 
528
                                return ASF_Redirector_e;
 
529
                        }
 
530
                } else {
 
531
                        if(     (!strcasecmp(content_type, "audio/x-ms-wax")) ||
 
532
                                (!strcasecmp(content_type, "audio/x-ms-wma")) ||
 
533
                                (!strcasecmp(content_type, "video/x-ms-asf")) ||
 
534
                                (!strcasecmp(content_type, "video/x-ms-afs")) ||
 
535
                                (!strcasecmp(content_type, "video/x-ms-wvx")) ||
 
536
                                (!strcasecmp(content_type, "video/x-ms-wmv")) ||
 
537
                                (!strcasecmp(content_type, "video/x-ms-wma")) ) {
 
538
                                mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ASFRedirector);
 
539
                                return ASF_Redirector_e;
 
540
                        } else if( !strcasecmp(content_type, "text/plain") ) {
 
541
                                mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n");
 
542
                                return ASF_PlainText_e;
 
543
                        } else {
 
544
                                mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF unknown content-type: %s\n", content_type );
 
545
                                return ASF_Unknown_e;
 
546
                        }
 
547
                }
 
548
        }
 
549
        return ASF_Unknown_e;
 
550
}
 
551
 
 
552
static HTTP_header_t *asf_http_request(streaming_ctrl_t *streaming_ctrl) {
 
553
        HTTP_header_t *http_hdr;
 
554
        URL_t *url = NULL;
 
555
        URL_t *server_url = NULL;
 
556
        asf_http_streaming_ctrl_t *asf_http_ctrl;
 
557
        char str[250];
 
558
        char *ptr;
 
559
        int i, enable;
 
560
 
 
561
        int offset_hi=0, offset_lo=0, length=0;
 
562
        int asf_nb_stream=0, stream_id;
 
563
 
 
564
        // Sanity check
 
565
        if( streaming_ctrl==NULL ) return NULL;
 
566
        url = streaming_ctrl->url;
 
567
        asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data;
 
568
        if( url==NULL || asf_http_ctrl==NULL ) return NULL;
 
569
 
 
570
        // Common header for all requests.
 
571
        http_hdr = http_new_header();
 
572
        http_set_field( http_hdr, "Accept: */*" );
 
573
        http_set_field( http_hdr, "User-Agent: NSPlayer/4.1.0.3856" );
 
574
        http_add_basic_authentication( http_hdr, url->username, url->password );
 
575
 
 
576
        // Check if we are using a proxy
 
577
        if( !strcasecmp( url->protocol, "http_proxy" ) ) {
 
578
                server_url = url_new( (url->file)+1 );
 
579
                if( server_url==NULL ) {
 
580
                        mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_InvalidProxyURL);
 
581
                        http_free( http_hdr );
 
582
                        return NULL;
 
583
                }
 
584
                http_set_uri( http_hdr, server_url->url );
 
585
                sprintf( str, "Host: %.220s:%d", server_url->hostname, server_url->port );
 
586
                url_free( server_url );
 
587
        } else {
 
588
                http_set_uri( http_hdr, url->file );
 
589
                sprintf( str, "Host: %.220s:%d", url->hostname, url->port );
 
590
        }
 
591
        
 
592
        http_set_field( http_hdr, str );
 
593
        http_set_field( http_hdr, "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}" );
 
594
        sprintf(str, 
 
595
                "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=%u",
 
596
                offset_hi, offset_lo, asf_http_ctrl->request, length );
 
597
        http_set_field( http_hdr, str );
 
598
 
 
599
        switch( asf_http_ctrl->streaming_type ) {
 
600
                case ASF_Live_e:
 
601
                case ASF_Prerecorded_e:
 
602
                        http_set_field( http_hdr, "Pragma: xPlayStrm=1" );
 
603
                        ptr = str;
 
604
                        ptr += sprintf( ptr, "Pragma: stream-switch-entry=");
 
605
                        if(asf_http_ctrl->n_audio > 0) {
 
606
                                for( i=0; i<asf_http_ctrl->n_audio ; i++ ) {
 
607
                                        stream_id = asf_http_ctrl->audio_streams[i];
 
608
                                        if(stream_id == asf_http_ctrl->audio_id) {
 
609
                                                enable = 0;
 
610
                                        } else {
 
611
                                                enable = 2;
 
612
                                                continue;
 
613
                                        }
 
614
                                        asf_nb_stream++;
 
615
                                        ptr += sprintf(ptr, "ffff:%d:%d ", stream_id, enable);
 
616
                                }
 
617
                        }
 
618
                        if(asf_http_ctrl->n_video > 0) {
 
619
                                for( i=0; i<asf_http_ctrl->n_video ; i++ ) {
 
620
                                        stream_id = asf_http_ctrl->video_streams[i];
 
621
                                        if(stream_id == asf_http_ctrl->video_id) {
 
622
                                                enable = 0;
 
623
                                        } else {
 
624
                                                enable = 2;
 
625
                                                continue;
 
626
                                        }
 
627
                                        asf_nb_stream++;
 
628
                                        ptr += sprintf(ptr, "ffff:%d:%d ", stream_id, enable);
 
629
                                }
 
630
                        }
 
631
                        http_set_field( http_hdr, str );
 
632
                        sprintf( str, "Pragma: stream-switch-count=%d", asf_nb_stream );
 
633
                        http_set_field( http_hdr, str );
 
634
                        break;
 
635
                case ASF_Redirector_e:
 
636
                        break;
 
637
                case ASF_Unknown_e:
 
638
                        // First request goes here.
 
639
                        break;
 
640
                default:
 
641
                        mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_UnknownASFStreamType);
 
642
        }
 
643
 
 
644
        http_set_field( http_hdr, "Connection: Close" );
 
645
        http_build_request( http_hdr );
 
646
 
 
647
        return http_hdr;
 
648
}
 
649
 
 
650
static int asf_http_parse_response(asf_http_streaming_ctrl_t *asf_http_ctrl, HTTP_header_t *http_hdr ) {
 
651
        char *content_type, *pragma;
 
652
        char features[64] = "\0";
 
653
        size_t len;
 
654
        if( http_response_parse(http_hdr)<0 ) {
 
655
                mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_Failed2ParseHTTPResponse);
 
656
                return -1;
 
657
        }
 
658
        switch( http_hdr->status_code ) {
 
659
                case 200:
 
660
                        break;
 
661
                case 401: // Authentication required
 
662
                        return ASF_Authenticate_e;
 
663
                default:
 
664
                        mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ServerReturn, http_hdr->status_code, http_hdr->reason_phrase);
 
665
                        return -1;
 
666
        }
 
667
 
 
668
        content_type = http_get_field( http_hdr, "Content-Type");
 
669
//printf("Content-Type: [%s]\n", content_type);
 
670
 
 
671
        pragma = http_get_field( http_hdr, "Pragma");
 
672
        while( pragma!=NULL ) {
 
673
                char *comma_ptr=NULL;
 
674
                char *end;
 
675
//printf("Pragma: [%s]\n", pragma );
 
676
                // The pragma line can get severals attributes 
 
677
                // separeted with a comma ','.
 
678
                do {
 
679
                        if( !strncasecmp( pragma, "features=", 9) ) {
 
680
                                pragma += 9;
 
681
                                end = strstr( pragma, "," );
 
682
                                if( end==NULL ) {
 
683
                                  size_t s = strlen(pragma);
 
684
                                  if(s > sizeof(features)) {
 
685
                                    mp_msg(MSGT_NETWORK,MSGL_WARN,MSGTR_MPDEMUX_ASF_ASFHTTPParseWarnCuttedPragma,pragma,s,sizeof(features));
 
686
                                    len = sizeof(features);
 
687
                                  } else {                                 
 
688
                                    len = s;
 
689
                                  }
 
690
                                } else { 
 
691
                                  len = MIN((unsigned int)(end-pragma),sizeof(features));
 
692
                                }
 
693
                                strncpy( features, pragma, len );
 
694
                                features[len]='\0';
 
695
                                break;
 
696
                        }
 
697
                        comma_ptr = strstr( pragma, "," );
 
698
                        if( comma_ptr!=NULL ) {
 
699
                                pragma = comma_ptr+1;
 
700
                                if( pragma[0]==' ' ) pragma++;
 
701
                        }
 
702
                } while( comma_ptr!=NULL );
 
703
                pragma = http_get_next_field( http_hdr );
 
704
        }
 
705
        asf_http_ctrl->streaming_type = asf_http_streaming_type( content_type, features, http_hdr );
 
706
        return 0;
 
707
}
 
708
 
 
709
static int asf_http_streaming_start( stream_t *stream, int *demuxer_type ) {
 
710
        HTTP_header_t *http_hdr=NULL;
 
711
        URL_t *url = stream->streaming_ctrl->url;
 
712
        asf_http_streaming_ctrl_t *asf_http_ctrl;
 
713
        char buffer[BUFFER_SIZE];
 
714
        int i, ret;
 
715
        int fd = stream->fd;
 
716
        int done;
 
717
        int auth_retry = 0;
 
718
 
 
719
        asf_http_ctrl = (asf_http_streaming_ctrl_t*)malloc(sizeof(asf_http_streaming_ctrl_t));
 
720
        if( asf_http_ctrl==NULL ) {
 
721
                mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
 
722
                return -1;
 
723
        }
 
724
        asf_http_ctrl->streaming_type = ASF_Unknown_e;
 
725
        asf_http_ctrl->request = 1;
 
726
        asf_http_ctrl->audio_streams = asf_http_ctrl->video_streams = NULL;
 
727
        asf_http_ctrl->n_audio = asf_http_ctrl->n_video = 0;
 
728
        stream->streaming_ctrl->data = (void*)asf_http_ctrl;
 
729
 
 
730
        do {
 
731
                done = 1;
 
732
                if( fd>0 ) closesocket( fd );
 
733
 
 
734
                if( !strcasecmp( url->protocol, "http_proxy" ) ) {
 
735
                        if( url->port==0 ) url->port = 8080;
 
736
                } else {
 
737
                        if( url->port==0 ) url->port = 80;
 
738
                }
 
739
                fd = connect2Server( url->hostname, url->port, 1);
 
740
                if( fd<0 ) return fd;
 
741
 
 
742
                http_hdr = asf_http_request( stream->streaming_ctrl );
 
743
                mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request [%s]\n", http_hdr->buffer );
 
744
                for(i=0; i < (int)http_hdr->buffer_size ; ) {
 
745
                        int r = send( fd, http_hdr->buffer+i, http_hdr->buffer_size-i, 0 );
 
746
                        if(r <0) {
 
747
                                mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_SocketWriteError,strerror(errno));
 
748
                                return -1;
 
749
                        }
 
750
                        i += r;
 
751
                }       
 
752
                http_free( http_hdr );
 
753
                http_hdr = http_new_header();
 
754
                do {
 
755
                        i = recv( fd, buffer, BUFFER_SIZE, 0 );
 
756
//printf("read: %d\n", i );
 
757
                        if( i<=0 ) {
 
758
                                perror("read");
 
759
                                http_free( http_hdr );
 
760
                                return -1;
 
761
                        }
 
762
                        http_response_append( http_hdr, buffer, i );
 
763
                } while( !http_is_header_entire( http_hdr ) );
 
764
                if( verbose>0 ) {
 
765
                        http_hdr->buffer[http_hdr->buffer_size]='\0';
 
766
                        mp_msg(MSGT_NETWORK,MSGL_DBG2,"Response [%s]\n", http_hdr->buffer );
 
767
                }
 
768
                ret = asf_http_parse_response(asf_http_ctrl, http_hdr);
 
769
                if( ret<0 ) {
 
770
                        mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_HeaderParseFailed);
 
771
                        http_free( http_hdr );
 
772
                        return -1;
 
773
                }
 
774
                switch( asf_http_ctrl->streaming_type ) {
 
775
                        case ASF_Live_e:
 
776
                        case ASF_Prerecorded_e:
 
777
                        case ASF_PlainText_e:
 
778
                                if( http_hdr->body_size>0 ) {
 
779
                                        if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
 
780
                                                http_free( http_hdr );
 
781
                                                return -1;
 
782
                                        }
 
783
                                }
 
784
                                if( asf_http_ctrl->request==1 ) {
 
785
                                        if( asf_http_ctrl->streaming_type!=ASF_PlainText_e ) {
 
786
                                                // First request, we only got the ASF header.
 
787
                                                ret = asf_streaming_parse_header(fd,stream->streaming_ctrl);
 
788
                                                if(ret < 0) return -1;
 
789
                                                if(asf_http_ctrl->n_audio == 0 && asf_http_ctrl->n_video == 0) {
 
790
                                                        mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_NoStreamFound);
 
791
                                                        return -1;
 
792
                                                }
 
793
                                                asf_http_ctrl->request++;
 
794
                                                done = 0;
 
795
                                        } else {
 
796
                                                done = 1;
 
797
                                        }
 
798
                                }
 
799
                                break;
 
800
                        case ASF_Redirector_e:
 
801
                                if( http_hdr->body_size>0 ) {
 
802
                                        if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
 
803
                                                http_free( http_hdr );
 
804
                                                return -1;
 
805
                                        }
 
806
                                }
 
807
                                *demuxer_type = DEMUXER_TYPE_PLAYLIST;
 
808
                                done = 1;
 
809
                                break;
 
810
                        case ASF_Authenticate_e:
 
811
                                if( http_authenticate( http_hdr, url, &auth_retry)<0 ) return -1;
 
812
                                asf_http_ctrl->streaming_type = ASF_Unknown_e;
 
813
                                done = 0;
 
814
                                break;
 
815
                        case ASF_Unknown_e:
 
816
                        default:
 
817
                                mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_UnknownASFStreamingType);
 
818
                                closesocket(fd);
 
819
                                http_free( http_hdr );
 
820
                                return -1;
 
821
                }
 
822
        // Check if we got a redirect.  
 
823
        } while(!done);
 
824
 
 
825
        stream->fd = fd;
 
826
        if( asf_http_ctrl->streaming_type==ASF_PlainText_e || asf_http_ctrl->streaming_type==ASF_Redirector_e ) {
 
827
                stream->streaming_ctrl->streaming_read = nop_streaming_read;
 
828
                stream->streaming_ctrl->streaming_seek = nop_streaming_seek;
 
829
        } else {
 
830
                stream->streaming_ctrl->streaming_read = asf_http_streaming_read;
 
831
                stream->streaming_ctrl->streaming_seek = asf_http_streaming_seek;
 
832
                stream->streaming_ctrl->buffering = 1;
 
833
        }
 
834
        stream->streaming_ctrl->status = streaming_playing_e;
 
835
        stream->close = close_s;
 
836
 
 
837
        http_free( http_hdr );
 
838
        return 0;
 
839
}
 
840
 
 
841
static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
 
842
        URL_t *url;
 
843
 
 
844
        stream->streaming_ctrl = streaming_ctrl_new();
 
845
        if( stream->streaming_ctrl==NULL ) {
 
846
                return STREAM_ERROR;
 
847
        }
 
848
        stream->streaming_ctrl->bandwidth = network_bandwidth;
 
849
        url = url_new(stream->url);
 
850
        stream->streaming_ctrl->url = check4proxies(url);
 
851
        url_free(url);
 
852
        
 
853
        mp_msg(MSGT_OPEN, MSGL_INFO, MSGTR_MPDEMUX_ASF_InfoStreamASFURL, stream->url);
 
854
        if((!strncmp(stream->url, "http", 4)) && (*file_format!=DEMUXER_TYPE_ASF && *file_format!=DEMUXER_TYPE_UNKNOWN)) {
 
855
                streaming_ctrl_free(stream->streaming_ctrl);
 
856
                stream->streaming_ctrl = NULL;
 
857
                return STREAM_UNSUPORTED;
 
858
        }
 
859
 
 
860
        if(asf_streaming_start(stream, file_format) < 0) {
 
861
                mp_msg(MSGT_OPEN, MSGL_ERR, MSGTR_MPDEMUX_ASF_StreamingFailed);
 
862
                streaming_ctrl_free(stream->streaming_ctrl);
 
863
                stream->streaming_ctrl = NULL;
 
864
                return STREAM_UNSUPORTED;
 
865
        }
 
866
        
 
867
        *file_format = DEMUXER_TYPE_ASF;
 
868
        stream->type = STREAMTYPE_STREAM;
 
869
        fixup_network_stream_cache(stream);
 
870
        return STREAM_OK;
 
871
}
 
872
 
 
873
stream_info_t stream_info_asf = {
 
874
  "mms and mms over http streaming",
 
875
  "null",
 
876
  "Bertrand, Reimar Doeffinger, Albeu",
 
877
  "originally based on work by Majormms (is that code still there?)",
 
878
  open_s,
 
879
  {"mms", "mmsu", "mmst", "http", "http_proxy", "mmshttp", NULL},
 
880
  NULL,
 
881
  0 // Urls are an option string
 
882
};
 
883