Пример #1
0
int
http_seek( stream_t *stream, off_t pos ) {
	HTTP_header_t *http_hdr = NULL;
	int fd;
	if( stream==NULL ) return 0;

	if( stream->fd>0 ) closesocket(stream->fd); // need to reconnect to seek in http-stream
	fd = http_send_request( stream->streaming_ctrl->url, pos );
	if( fd<0 ) return 0;

	http_hdr = http_read_response( fd );

	if( http_hdr==NULL ) return 0;

	if( mp_msg_test(MSGT_NETWORK,MSGL_V) )
		http_debug_hdr( http_hdr );

	switch( http_hdr->status_code ) {
		case 200:
		case 206: // OK
			mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") );
			mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") );
			if( http_hdr->body_size>0 ) {
				if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
					http_free( http_hdr );
					return -1;
				}
			}
			break;
		default:
			mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrServerReturned, http_hdr->status_code, http_hdr->reason_phrase );
			closesocket( fd );
			fd = -1;
	}
	stream->fd = fd;

	if( http_hdr ) {
		http_free( http_hdr );
		stream->streaming_ctrl->data = NULL;
	}

	stream->pos=pos;

	return 1;
}
Пример #2
0
static int get_media_packet (int s, int padding, streaming_ctrl_t *stream_ctrl) {
    unsigned char  pre_header[8];
    char           data[BUF_SIZE];

    if (!get_data (s, pre_header, 8)) {
        mp_tmsg(MSGT_NETWORK,MSGL_ERR,"pre-header read failed\n");
        return 0;
    }

//  for (i=0; i<8; i++)
//    mp_msg(MSGT_NETWORK,MSGL_INFO,"pre_header[%d] = %02x (%d)\n",
//	    i, pre_header[i], pre_header[i]);

    if (pre_header[4] == 0x04) {

        int packet_len;

        packet_len = (pre_header[7] << 8 | pre_header[6]) - 8;

//    mp_msg(MSGT_NETWORK,MSGL_INFO,"asf media packet detected, len=%d\n", packet_len);

        if (packet_len < 0 || packet_len > BUF_SIZE) {
            mp_tmsg(MSGT_NETWORK, MSGL_FATAL, "Invalid RTSP packet size, giving up.\n");
            return 0;
        }

        if (!get_data (s, data, packet_len)) {
            mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Media data read failed.\n");
            return 0;
        }

        streaming_bufferize(stream_ctrl, data, padding);

    } else {

        int32_t packet_len;
        int command;

        if (!get_data (s, (char*)&packet_len, 4)) {
            mp_tmsg(MSGT_NETWORK,MSGL_ERR,"packet_len read failed.\n");
            return 0;
        }

        packet_len = get_32 ((unsigned char*)&packet_len, 0) + 4;

        if (packet_len < 0 || packet_len > BUF_SIZE) {
            mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Invalid RTSP packet size, giving up.\n");
            return 0;
        }

        if (!get_data (s, data, packet_len)) {
            mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Command data read failed.\n");
            return 0;
        }

        if ( (pre_header[7] != 0xb0) || (pre_header[6] != 0x0b)
                || (pre_header[5] != 0xfa) || (pre_header[4] != 0xce) ) {

            mp_tmsg(MSGT_NETWORK,MSGL_ERR,"missing signature\n");
            return -1;
        }

        command = get_32 (data, 24) & 0xFFFF;

//    mp_msg(MSGT_NETWORK,MSGL_INFO,"\ncommand packet detected, len=%d  cmd=0x%X\n", packet_len, command);

        if (command == 0x1b)
            send_command (s, 0x1b, 0, 0, 0, data);
        else if (command == 0x1e) {
            mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Everything done. Thank you for downloading a media file containing proprietary and patented technology.\n");
            return 0;
        }
        else if (command == 0x21 ) {
            // Looks like it's new in WMS9
            // Unknown command, but ignoring it seems to work.
            return 0;
        }
        else if (command != 0x05) {
            mp_tmsg(MSGT_NETWORK,MSGL_ERR,"unknown command %02x\n",command);
            return -1;
        }
    }

//  mp_msg(MSGT_NETWORK,MSGL_INFO,"get media packet succ\n");

    return 1;
}
Пример #3
0
static int get_header (int s, uint8_t *header, streaming_ctrl_t *streaming_ctrl)
{
    unsigned char  pre_header[8];
    int            header_len;

    header_len = 0;

    while (1) {
        if (!get_data (s, pre_header, 8)) {
            mp_tmsg(MSGT_NETWORK,MSGL_ERR,"pre-header read failed\n");
            return 0;
        }
        if (pre_header[4] == 0x02) {

            int packet_len;

            packet_len = (pre_header[7] << 8 | pre_header[6]) - 8;

//      mp_msg(MSGT_NETWORK,MSGL_INFO,"asf header packet detected, len=%d\n", packet_len);

            if (packet_len < 0 || packet_len > HDR_BUF_SIZE - header_len) {
                mp_tmsg(MSGT_NETWORK, MSGL_FATAL, "Invalid header size, giving up.\n");
                return 0;
            }

            if (!get_data (s, &header[header_len], packet_len)) {
                mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Header data read failed.\n");
                return 0;
            }

            header_len += packet_len;

            if ( (header[header_len-1] == 1) && (header[header_len-2]==1)) {


                if( streaming_bufferize( streaming_ctrl, header, header_len )<0 ) {
                    return -1;
                }

                //	mp_msg(MSGT_NETWORK,MSGL_INFO,"get header packet finished\n");

                return header_len;

            }

        } else {

            int32_t packet_len;
            int command;
            char data[BUF_SIZE];

            if (!get_data (s, (char*)&packet_len, 4)) {
                mp_tmsg(MSGT_NETWORK,MSGL_ERR,"packet_len read failed.\n");
                return 0;
            }

            packet_len = get_32 ((unsigned char*)&packet_len, 0) + 4;

//      mp_msg(MSGT_NETWORK,MSGL_INFO,"command packet detected, len=%d\n", packet_len);

            if (packet_len < 0 || packet_len > BUF_SIZE) {
                mp_tmsg(MSGT_NETWORK, MSGL_FATAL,
                        "Invalid RTSP packet size, giving up.\n");
                return 0;
            }

            if (!get_data (s, data, packet_len)) {
                mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Command data read failed.\n");
                return 0;
            }

            command = get_32 (data, 24) & 0xFFFF;

//      mp_msg(MSGT_NETWORK,MSGL_INFO,"command: %02x\n", command);

            if (command == 0x1b)
                send_command (s, 0x1b, 0, 0, 0, data);

        }

//    mp_msg(MSGT_NETWORK,MSGL_INFO,"get header packet succ\n");
    }
}
Пример #4
0
int  http_seek(stream_t *stream, off_t pos) 
{

	HTTP_header_t * http_hdr = NULL;
	int fd = -1;

	if (stream == NULL)
	{
		OS_PRINTF("[%s][ERROR] stream == NULL!!!!\n",__func__);
		return 0;
	}
	
	//This if removed for network play issue fix, Gavin 2013-09-09
	//if (stream->fd > 0) closesocket(stream->fd); // need to reconnect to seek in http-stream
	//yliu add :for  fd = 0
#ifdef __LINUX__
	if (stream->fd > 0) 
	{
		closesocket(stream->fd); // need to reconnect to seek in http-stream
	}
#else
	if (stream->fd >= 0) 
	{
		closesocket(stream->fd);
		stream->fd = -1;
	}
#endif
      
	 if(is_file_seq_exit())
	 {
       
	     return -1;
	 }

	fd = http_send_request(stream->streaming_ctrl->url, pos);

	if (fd < 0) 
	{
		OS_PRINTF("[%s][ERROR] fail to send request  fd[%d]!!!!!!!!\n",__func__,fd);
		return 0;
	}

	http_hdr = http_read_response(fd);
       
	if (http_hdr == NULL) 
	{
	//yliu add:
	 closesocket(fd);
       fd = -1;
		OS_PRINTF("[%s][ERROR] fail to read response !!!!!!!!\n",__func__);
		return 0;
	}
      
      stream->streaming_ctrl->chunksize = http_hdr->chunksize;
	if (mp_msg_test(MSGT_NETWORK, MSGL_V))
	{
		http_debug_hdr(http_hdr);
	}

	switch (http_hdr->status_code) {
		case 200:
		case 206: // OK
			//mp_msg(MSGT_NETWORK, MSGL_V, "Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type"));
			//mp_msg(MSGT_NETWORK, MSGL_V, "Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length"));
			OS_PRINTF("[%s] Content-Type: [%s]\n",__func__,http_get_field(http_hdr, "Content-Type"));
			OS_PRINTF("[%s] Content-Length: [%s]\n",__func__,http_get_field(http_hdr, "Content-Length"));
                   //  yliu add :for reconnect
                   //if(pos == 0)
                    {
              	if(http_get_field(http_hdr, "Content-Length"))
			   stream->end_pos = atoll(http_get_field(http_hdr, "Content-Length"))+pos;
                   else
                      stream->end_pos = 0;
                    }
			OS_PRINTF("[%s] stream->end_pos: [%ld]\n",__func__,stream->end_pos);
			
			if (http_hdr->body_size > 0) {
				if (streaming_bufferize(stream->streaming_ctrl, http_hdr->body, http_hdr->body_size) < 0) {
					http_free(http_hdr);
                    closesocket(fd);
                    fd = -1;
					return -1;
				}
			}

			break;

			
		default:
			mp_msg(MSGT_NETWORK, MSGL_ERR, MSGTR_MPDEMUX_NW_ErrServerReturned, http_hdr->status_code, http_hdr->reason_phrase);
			closesocket(fd);
			fd = -1;
	}
	//add macro for consistency in Linux Version, yliu 2013-09-10
#if  0
	//This if added for network play issue fix, Gavin 2013-09-09
	if(stream->fd >= 0)
	{
		closesocket(stream->fd); // need to reconnect to seek in http-stream
		stream->fd = -1;
	}
#endif

	stream->fd = fd;

	if (http_hdr) {
		
		http_free(http_hdr);
		stream->streaming_ctrl->data = NULL;
		
	}

	stream->pos = pos;
	return 1;
}
Пример #5
0
static int get_media_packet (int s, int padding, streaming_ctrl_t *stream_ctrl) {
  unsigned char  pre_header[8];
  char           data[BUF_SIZE];

  if (!get_data (s, pre_header, 8)) {
    mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_MMST_PreHeaderReadFailed);
    return 0;
  }

//  for (i=0; i<8; i++)
//    mp_msg(MSGT_NETWORK,MSGL_INFO,"pre_header[%d] = %02x (%d)\n",
//	    i, pre_header[i], pre_header[i]);

  if (pre_header[4] == 0x04) {

    int packet_len;

    packet_len = (pre_header[7] << 8 | pre_header[6]) - 8;

//    mp_msg(MSGT_NETWORK,MSGL_INFO,"asf media packet detected, len=%d\n", packet_len);

    if (packet_len < 0 || packet_len > BUF_SIZE) {
      mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MPDEMUX_MMST_InvalidRTSPPacketSize);
      return 0;
    }

    if (!get_data (s, data, packet_len)) {
      mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_MMST_MediaDataReadFailed);
      return 0;
    }

    streaming_bufferize(stream_ctrl, data, padding);

  } else {

    int32_t packet_len;
    int command;

    if (!get_data (s, (char*)&packet_len, 4)) {
      mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_MMST_packet_lenReadFailed);
      return 0;
    }

    packet_len = get_32 ((unsigned char*)&packet_len, 0) + 4;

    if (packet_len < 0 || packet_len > BUF_SIZE) {
      mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MPDEMUX_MMST_InvalidRTSPPacketSize);
      return 0;
    }

    if (!get_data (s, data, packet_len)) {
      mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_MMST_CmdDataReadFailed);
      return 0;
    }

    if ( (pre_header[7] != 0xb0) || (pre_header[6] != 0x0b)
	 || (pre_header[5] != 0xfa) || (pre_header[4] != 0xce) ) {

      mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_MMST_MissingSignature);
      return -1;
    }

    command = get_32 (data, 24) & 0xFFFF;

//    mp_msg(MSGT_NETWORK,MSGL_INFO,"\ncommand packet detected, len=%d  cmd=0x%X\n", packet_len, command);

    if (command == 0x1b)
      send_command (s, 0x1b, 0, 0, 0, data);
    else if (command == 0x1e) {
      mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_MMST_PatentedTechnologyJoke);
      return 0;
    }
    else if (command == 0x21 ) {
	// Looks like it's new in WMS9
	// Unknown command, but ignoring it seems to work.
	return 0;
    }
    else if (command != 0x05) {
      mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_MMST_UnknownCmd,command);
      return -1;
    }
  }

//  mp_msg(MSGT_NETWORK,MSGL_INFO,"get media packet succ\n");

  return 1;
}
Пример #6
0
Файл: http.c Проект: zerix/mpv
static int nop_streaming_start( stream_t *stream ) {
	HTTP_header_t *http_hdr = NULL;
	char *next_url=NULL;
	int fd,ret;
	if( stream==NULL ) return -1;

	fd = stream->fd;
	if( fd<0 ) {
		fd = http_send_request( stream->streaming_ctrl->url, 0 );
		if( fd<0 ) return -1;
		http_hdr = http_read_response( fd );
		if( http_hdr==NULL ) return -1;

		switch( http_hdr->status_code ) {
			case 200: // OK
				mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") );
				mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") );
				if( http_hdr->body_size>0 ) {
					if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
						http_free( http_hdr );
						return -1;
					}
				}
				break;
			// Redirect
			case 301: // Permanently
			case 302: // Temporarily
			case 303: // See Other
			case 307: // Temporarily (since HTTP/1.1)
				ret=-1;
				next_url = http_get_field( http_hdr, "Location" );

				if (next_url != NULL) {
					mp_msg(MSGT_NETWORK,MSGL_STATUS,"Redirected: Using this url instead %s\n",next_url);
							stream->streaming_ctrl->url=url_new_with_proxy(next_url);
					ret=nop_streaming_start(stream); //recursively get streaming started
				} else {
					mp_msg(MSGT_NETWORK,MSGL_ERR,"Redirection failed\n");
					closesocket( fd );
					fd = -1;
				}
				return ret;
				break;
			case 401: //Authorization required
			case 403: //Forbidden
			case 404: //Not found
			case 500: //Server Error
			default:
				mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned code %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
				closesocket( fd );
				fd = -1;
				return -1;
				break;
		}
		stream->fd = fd;
	} else {
		http_hdr = (HTTP_header_t*)stream->streaming_ctrl->data;
		if( http_hdr->body_size>0 ) {
			if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
				http_free( http_hdr );
				stream->streaming_ctrl->data = NULL;
				return -1;
			}
		}
	}

	if( http_hdr ) {
		http_free( http_hdr );
		stream->streaming_ctrl->data = NULL;
	}

	stream->streaming_ctrl->streaming_read = nop_streaming_read;
	stream->streaming_ctrl->streaming_seek = nop_streaming_seek;
	stream->streaming_ctrl->status = streaming_playing_e;
        stream->streaming = true;
	return 0;
}
Пример #7
0
static int asf_http_streaming_start( stream_t *stream, int *demuxer_type ) {
	HTTP_header_t *http_hdr=NULL;
	URL_t *url = stream->streaming_ctrl->url;
	asf_http_streaming_ctrl_t *asf_http_ctrl;
	char buffer[BUFFER_SIZE];
	int i, ret;
	int fd = stream->fd;
	int done;
	int auth_retry = 0;

	asf_http_ctrl = malloc(sizeof(asf_http_streaming_ctrl_t));
	if( asf_http_ctrl==NULL ) {
		mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
		return -1;
	}
	asf_http_ctrl->streaming_type = ASF_Unknown_e;
	asf_http_ctrl->request = 1;
	asf_http_ctrl->audio_streams = asf_http_ctrl->video_streams = NULL;
	asf_http_ctrl->n_audio = asf_http_ctrl->n_video = 0;
	stream->streaming_ctrl->data = (void*)asf_http_ctrl;

	do {
		done = 1;
		if( fd>0 ) closesocket( fd );

		if( !strcasecmp( url->protocol, "http_proxy" ) ) {
			if( url->port==0 ) url->port = 8080;
		} else {
			if( url->port==0 ) url->port = 80;
		}
		fd = connect2Server( url->hostname, url->port, 1);
		if( fd<0 ) return fd;

		http_hdr = asf_http_request( stream->streaming_ctrl );
		mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request [%s]\n", http_hdr->buffer );
		for(i=0; i < (int)http_hdr->buffer_size ; ) {
			int r = send( fd, http_hdr->buffer+i, http_hdr->buffer_size-i, DEFAULT_SEND_FLAGS );
			if(r <0) {
				mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_SocketWriteError,strerror(errno));
				goto err_out;
			}
			i += r;
		}
		http_free( http_hdr );
		http_hdr = http_new_header();
		do {
			i = recv( fd, buffer, BUFFER_SIZE, 0 );
//printf("read: %d\n", i );
			if( i<=0 ) {
				perror("read");
				goto err_out;
			}
			http_response_append( http_hdr, buffer, i );
		} while( !http_is_header_entire( http_hdr ) );
		if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) {
			http_hdr->buffer[http_hdr->buffer_size]='\0';
			mp_msg(MSGT_NETWORK,MSGL_DBG2,"Response [%s]\n", http_hdr->buffer );
		}
		ret = asf_http_parse_response(asf_http_ctrl, http_hdr);
		if( ret<0 ) {
			mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_HeaderParseFailed);
			goto err_out;
		}
		switch( asf_http_ctrl->streaming_type ) {
			case ASF_Live_e:
			case ASF_Prerecorded_e:
			case ASF_PlainText_e:
				if( http_hdr->body_size>0 ) {
					if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
						goto err_out;
					}
				}
				if( asf_http_ctrl->request==1 ) {
					if( asf_http_ctrl->streaming_type!=ASF_PlainText_e ) {
						// First request, we only got the ASF header.
						ret = asf_streaming_parse_header(fd,stream->streaming_ctrl);
						if(ret < 0) goto err_out;
						if(asf_http_ctrl->n_audio == 0 && asf_http_ctrl->n_video == 0) {
							mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_NoStreamFound);
							goto err_out;
						}
						asf_http_ctrl->request++;
						done = 0;
					} else {
						done = 1;
					}
				}
				break;
			case ASF_Redirector_e:
				if( http_hdr->body_size>0 ) {
					if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
						goto err_out;
					}
				}
				*demuxer_type = DEMUXER_TYPE_PLAYLIST;
				done = 1;
				break;
			case ASF_Authenticate_e:
				if( http_authenticate( http_hdr, url, &auth_retry)<0 ) return -1;
				asf_http_ctrl->streaming_type = ASF_Unknown_e;
				done = 0;
				break;
			case ASF_Unknown_e:
			default:
				mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_UnknownASFStreamingType);
				goto err_out;
		}
	// Check if we got a redirect.
	} while(!done);

	stream->fd = fd;
	if( asf_http_ctrl->streaming_type==ASF_PlainText_e || asf_http_ctrl->streaming_type==ASF_Redirector_e ) {
		stream->streaming_ctrl->streaming_read = nop_streaming_read;
		stream->streaming_ctrl->streaming_seek = nop_streaming_seek;
	} else {
		stream->streaming_ctrl->streaming_read = asf_http_streaming_read;
		stream->streaming_ctrl->streaming_seek = asf_http_streaming_seek;
		stream->streaming_ctrl->buffering = 1;
	}
	stream->streaming_ctrl->status = streaming_playing_e;
	stream->close = close_s;

	http_free( http_hdr );
	return 0;

err_out:
	if (fd > 0)
		closesocket(fd);
	stream->fd = -1;
	http_free(http_hdr);
	return -1;
}