/** * @brief Connect to the Chromecast * @param p_stream the sout_stream_t structure * @return the opened socket file descriptor or -1 on error */ static int connectChromecast(sout_stream_t *p_stream, char *psz_ipChromecast) { sout_stream_sys_t *p_sys = p_stream->p_sys; int fd = net_ConnectTCP(p_stream, psz_ipChromecast, CHROMECAST_CONTROL_PORT); if (fd < 0) return -1; p_sys->p_creds = vlc_tls_ClientCreate(VLC_OBJECT(p_stream)); if (p_sys->p_creds == NULL) { net_Close(fd); return -1; } p_sys->p_tls = vlc_tls_ClientSessionCreate(p_sys->p_creds, fd, psz_ipChromecast, "tcps", NULL, NULL); if (p_sys->p_tls == NULL) { vlc_tls_Delete(p_sys->p_creds); return -1; } return fd; }
static void Run(intf_thread_t *intf) { intf_sys_t *sys = intf->p_sys; int fd; int canc = vlc_savecancel(); RegisterCallbacks(intf); while(1) { msg_Info(intf, "Creating IRC connection..."); fd = net_ConnectTCP(VLC_OBJECT(intf), sys->server, 6667); if(fd == -1) { msg_Err(intf, "Error connecting to server"); return; } msg_Info(intf, "Connected to server"); /* initialize context */ sys->fd = fd; sys->line_loc = 0; SendBufferInit(intf); SendBufferAppend(intf, "NICK "); SendBufferAppend(intf, sys->nick); SendBufferAppend(intf, "\r\n"); SendBufferAppend(intf, "USER "); SendBufferAppend(intf, sys->nick); SendBufferAppend(intf, " 8 * vlc\r\n"); sys->playlist = pl_Get(intf); #ifdef STOP_HACK playlist_Pause(sys->playlist); input_thread_t * input = playlist_CurrentInput(sys->playlist); var_SetFloat(input, "position", 0.0); #endif EventLoop(fd, intf); free(sys->send_buffer->buffer); sleep(30); } free(sys); vlc_restorecancel(canc); }
static int Open (vlc_object_t *obj) { access_t *access = (access_t *)obj; vlc_url_t url; vlc_UrlParse (&url, access->psz_location, 0); int fd = net_ConnectTCP (obj, url.psz_host, url.i_port ? url.i_port : IPPORT_HTCPCP); if (fd == -1) { vlc_UrlClean (&url); return VLC_EGENERIC; } access_sys_t *sys = malloc (sizeof (*sys)); if (unlikely(sys == NULL)) goto error; sys->fd = fd; net_Printf (obj, fd, NULL, "BREW %s HTTP/1.1\r\n", url.psz_path ? url.psz_path : "/"); if (url.i_port) net_Printf (obj, fd, NULL, "Host: %s:%u\r\n", url.psz_host, url.i_port); else net_Printf (obj, fd, NULL, "Host: %s\r\n", url.psz_host); net_Printf (obj, fd, NULL, "User-Agent: "PACKAGE_NAME"/"PACKAGE_VERSION"\r\n" "Accept-Additions: \r\n" "Content-Type: application/coffee-pot-command\r\n" "Content-Length: 0\r\n" "\r\n"); vlc_UrlClean (&url); access->p_sys = sys; access->pf_read = Read; access->pf_seek = Seek; access->pf_control = Control; return VLC_SUCCESS; error: net_Close (fd); free (sys); vlc_UrlClean (&url); return VLC_EGENERIC; }
/***************************************************************************** * Network wrappers *****************************************************************************/ static int RtspConnect( void *p_userdata, char *psz_server, int i_port ) { access_t *p_access = (access_t *)p_userdata; access_sys_t *p_sys = p_access->p_sys; /* Open connection */ p_sys->fd = net_ConnectTCP( p_access, psz_server, i_port ); if( p_sys->fd < 0 ) { msg_Err( p_access, "cannot connect to %s:%d", psz_server, i_port ); dialog_Fatal( p_access, _("Connection failed"), _("VLC could not connect to \"%s:%d\"."), psz_server, i_port ); return VLC_EGENERIC; } return VLC_SUCCESS; }
static bool open_vnc_connection ( filter_t *p_filter ) { filter_sys_t *p_sys = p_filter->p_sys; msg_Dbg( p_filter, "Open socket to vnc server on %s:%u.", p_sys->psz_host, p_sys->i_port ); p_sys->i_socket = net_ConnectTCP( p_filter, p_sys->psz_host, p_sys->i_port ); if( p_sys->i_socket < 0 ) { msg_Err( p_filter, "Could not open socket" ); return false; } msg_Dbg( p_filter, "socket is open." ); return true; }
/***************************************************************************** * Open: open the rtmp connection *****************************************************************************/ static int Open( vlc_object_t *p_this ) { sout_access_out_t *p_access = (sout_access_out_t *) p_this; sout_access_out_sys_t *p_sys; char *psz, *p; int length_path, length_media_name; int i; if( !( p_sys = calloc ( 1, sizeof( sout_access_out_sys_t ) ) ) ) return VLC_ENOMEM; p_access->p_sys = p_sys; p_sys->p_thread = vlc_object_create( p_access, sizeof( rtmp_control_thread_t ) ); if( !p_sys->p_thread ) { free( p_sys ); return VLC_ENOMEM; } vlc_object_attach( p_sys->p_thread, p_access ); /* Parse URI - remove spaces */ p = psz = strdup( p_access->psz_path ); while( ( p = strchr( p, ' ' ) ) != NULL ) *p = '+'; vlc_UrlParse( &p_sys->p_thread->url, psz, 0 ); free( psz ); if( p_sys->p_thread->url.psz_host == NULL || *p_sys->p_thread->url.psz_host == '\0' ) { msg_Warn( p_access, "invalid host" ); goto error; } if( p_sys->p_thread->url.i_port <= 0 ) p_sys->p_thread->url.i_port = 1935; if ( p_sys->p_thread->url.psz_path == NULL ) { msg_Warn( p_access, "invalid path" ); goto error; } length_path = strlen( p_sys->p_thread->url.psz_path ); char* psz_tmp = strrchr( p_sys->p_thread->url.psz_path, '/' ); if( !psz_tmp ) goto error; length_media_name = strlen( psz_tmp ) - 1; p_sys->p_thread->psz_application = strndup( p_sys->p_thread->url.psz_path + 1, length_path - length_media_name - 2 ); p_sys->p_thread->psz_media = strdup( p_sys->p_thread->url.psz_path + ( length_path - length_media_name ) ); msg_Dbg( p_access, "rtmp: host='%s' port=%d path='%s'", p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port, p_sys->p_thread->url.psz_path ); if( p_sys->p_thread->url.psz_username && *p_sys->p_thread->url.psz_username ) { msg_Dbg( p_access, " user='******'", p_sys->p_thread->url.psz_username ); } /* Initialize thread variables */ p_sys->p_thread->b_error= 0; p_sys->p_thread->p_fifo_input = block_FifoNew(); p_sys->p_thread->p_empty_blocks = block_FifoNew(); p_sys->p_thread->has_audio = 0; p_sys->p_thread->has_video = 0; p_sys->p_thread->metadata_received = 0; p_sys->p_thread->first_media_packet = 1; p_sys->p_thread->flv_tag_previous_tag_size = 0x00000000; /* FLV_TAG_FIRST_PREVIOUS_TAG_SIZE */ p_sys->p_thread->flv_body = rtmp_body_new( -1 ); p_sys->p_thread->flv_length_body = 0; p_sys->p_thread->chunk_size_recv = 128; /* RTMP_DEFAULT_CHUNK_SIZE */ p_sys->p_thread->chunk_size_send = 128; /* RTMP_DEFAULT_CHUNK_SIZE */ for(i = 0; i < 64; i++) { memset( &p_sys->p_thread->rtmp_headers_recv[i], 0, sizeof( rtmp_packet_t ) ); p_sys->p_thread->rtmp_headers_send[i].length_header = -1; p_sys->p_thread->rtmp_headers_send[i].stream_index = -1; p_sys->p_thread->rtmp_headers_send[i].timestamp = -1; p_sys->p_thread->rtmp_headers_send[i].timestamp_relative = -1; p_sys->p_thread->rtmp_headers_send[i].length_encoded = -1; p_sys->p_thread->rtmp_headers_send[i].length_body = -1; p_sys->p_thread->rtmp_headers_send[i].content_type = -1; p_sys->p_thread->rtmp_headers_send[i].src_dst = -1; p_sys->p_thread->rtmp_headers_send[i].body = NULL; } vlc_cond_init( &p_sys->p_thread->wait ); vlc_mutex_init( &p_sys->p_thread->lock ); p_sys->p_thread->result_connect = 1; /* p_sys->p_thread->result_publish = only used on access */ p_sys->p_thread->result_play = 1; p_sys->p_thread->result_stop = 0; p_sys->p_thread->fd = -1; /* Open connection */ if( var_CreateGetBool( p_access, "rtmp-connect" ) > 0 ) { #if 0 p_sys->p_thread->fd = net_ConnectTCP( p_access, p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port ); #endif msg_Err( p_access, "to be implemented" ); goto error2; } else { int *p_fd_listen; p_sys->active = 0; p_fd_listen = net_ListenTCP( p_access, p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port ); if( p_fd_listen == NULL ) { msg_Warn( p_access, "cannot listen to %s port %i", p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port ); goto error2; } do p_sys->p_thread->fd = net_Accept( p_access, p_fd_listen ); while( p_sys->p_thread->fd == -1 ); net_ListenClose( p_fd_listen ); if( rtmp_handshake_passive( p_this, p_sys->p_thread->fd ) < 0 ) { msg_Err( p_access, "handshake passive failed"); goto error2; } } if( vlc_clone( &p_sys->p_thread->thread, ThreadControl, p_sys->p_thread, VLC_THREAD_PRIORITY_INPUT ) ) { msg_Err( p_access, "cannot spawn rtmp control thread" ); goto error2; } if( !p_sys->active ) { if( rtmp_connect_passive( p_sys->p_thread ) < 0 ) { msg_Err( p_access, "connect passive failed"); vlc_cancel( p_sys->p_thread->thread ); vlc_join( p_sys->p_thread->thread, NULL ); goto error2; } } p_access->pf_write = Write; p_access->pf_seek = Seek; return VLC_SUCCESS; error2: vlc_cond_destroy( &p_sys->p_thread->wait ); vlc_mutex_destroy( &p_sys->p_thread->lock ); free( p_sys->p_thread->psz_application ); free( p_sys->p_thread->psz_media ); if( p_sys->p_thread->fd != -1 ) net_Close( p_sys->p_thread->fd ); error: vlc_UrlClean( &p_sys->p_thread->url ); vlc_object_release( p_sys->p_thread ); free( p_sys ); return VLC_EGENERIC; }
/***************************************************************************** * Run : call Handshake() then submit songs *****************************************************************************/ static void *Run(void *data) { intf_thread_t *p_intf = data; uint8_t p_buffer[1024]; int canc = vlc_savecancel(); bool b_handshaked = false; /* data about audioscrobbler session */ mtime_t next_exchange = -1; /**< when can we send data */ unsigned int i_interval = 0; /**< waiting interval (secs)*/ intf_sys_t *p_sys = p_intf->p_sys; /* main loop */ for (;;) { vlc_restorecancel(canc); vlc_mutex_lock(&p_sys->lock); mutex_cleanup_push(&p_sys->lock); do vlc_cond_wait(&p_sys->wait, &p_sys->lock); while (mdate() < next_exchange); vlc_cleanup_run(); canc = vlc_savecancel(); /* handshake if needed */ if (!b_handshaked) { msg_Dbg(p_intf, "Handshaking with last.fm ..."); switch(Handshake(p_intf)) { case VLC_ENOMEM: goto out; case VLC_ENOVAR: /* username not set */ dialog_Fatal(p_intf, _("Last.fm username not set"), "%s", _("Please set a username or disable the " "audioscrobbler plugin, and restart VLC.\n" "Visit http://www.last.fm/join/ to get an account.")); goto out; case VLC_SUCCESS: msg_Dbg(p_intf, "Handshake successful :)"); b_handshaked = true; i_interval = 0; next_exchange = mdate(); break; case VLC_AUDIOSCROBBLER_EFATAL: msg_Warn(p_intf, "Exiting..."); goto out; case VLC_EGENERIC: default: /* protocol error : we'll try later */ HandleInterval(&next_exchange, &i_interval); break; } /* if handshake failed let's restart the loop */ if (!b_handshaked) continue; } msg_Dbg(p_intf, "Going to submit some data..."); char *psz_submit; if (asprintf(&psz_submit, "s=%s", p_sys->psz_auth_token) == -1) break; /* forge the HTTP POST request */ vlc_mutex_lock(&p_sys->lock); audioscrobbler_song_t *p_song; for (int i_song = 0 ; i_song < p_sys->i_songs ; i_song++) { char *psz_submit_song, *psz_submit_tmp; p_song = &p_sys->p_queue[i_song]; if (asprintf(&psz_submit_song, "&a%%5B%d%%5D=%s" "&t%%5B%d%%5D=%s" "&i%%5B%d%%5D=%u" "&o%%5B%d%%5D=P" "&r%%5B%d%%5D=" "&l%%5B%d%%5D=%d" "&b%%5B%d%%5D=%s" "&n%%5B%d%%5D=%s" "&m%%5B%d%%5D=%s", i_song, p_song->psz_a, i_song, p_song->psz_t, i_song, (unsigned)p_song->date, /* HACK: %ju (uintmax_t) unsupported on Windows */ i_song, i_song, i_song, p_song->i_l, i_song, p_song->psz_b, i_song, p_song->psz_n, i_song, p_song->psz_m ) == -1) { /* Out of memory */ vlc_mutex_unlock(&p_sys->lock); goto out; } psz_submit_tmp = psz_submit; if (asprintf(&psz_submit, "%s%s", psz_submit_tmp, psz_submit_song) == -1) { /* Out of memory */ free(psz_submit_tmp); free(psz_submit_song); vlc_mutex_unlock(&p_sys->lock); goto out; } free(psz_submit_song); free(psz_submit_tmp); } vlc_mutex_unlock(&p_sys->lock); int i_post_socket = net_ConnectTCP(p_intf, p_sys->p_submit_url.psz_host, p_sys->p_submit_url.i_port); if (i_post_socket == -1) { /* If connection fails, we assume we must handshake again */ HandleInterval(&next_exchange, &i_interval); b_handshaked = false; free(psz_submit); continue; } /* we transmit the data */ int i_net_ret = net_Printf(p_intf, i_post_socket, NULL, "POST %s HTTP/1.1\n" "Accept-Encoding: identity\n" "Content-length: %zu\n" "Connection: close\n" "Content-type: application/x-www-form-urlencoded\n" "Host: %s\n" "User-agent: VLC media player/"VERSION"\r\n" "\r\n" "%s\r\n" "\r\n", p_sys->p_submit_url.psz_path, strlen(psz_submit), p_sys->p_submit_url.psz_host, psz_submit ); free(psz_submit); if (i_net_ret == -1) { /* If connection fails, we assume we must handshake again */ HandleInterval(&next_exchange, &i_interval); b_handshaked = false; continue; } i_net_ret = net_Read(p_intf, i_post_socket, NULL, p_buffer, sizeof(p_buffer) - 1, false); if (i_net_ret <= 0) { /* if we get no answer, something went wrong : try again */ continue; } net_Close(i_post_socket); p_buffer[i_net_ret] = '\0'; char *failed = strstr((char *) p_buffer, "FAILED"); if (failed) { msg_Warn(p_intf, "%s", failed); HandleInterval(&next_exchange, &i_interval); continue; } if (strstr((char *) p_buffer, "BADSESSION")) { msg_Err(p_intf, "Authentication failed (BADSESSION), are you connected to last.fm with another program ?"); b_handshaked = false; HandleInterval(&next_exchange, &i_interval); continue; } if (strstr((char *) p_buffer, "OK")) { for (int i = 0; i < p_sys->i_songs; i++) DeleteSong(&p_sys->p_queue[i]); p_sys->i_songs = 0; i_interval = 0; next_exchange = mdate(); msg_Dbg(p_intf, "Submission successful!"); } else { msg_Err(p_intf, "Authentication failed, handshaking again (%s)", p_buffer); b_handshaked = false; HandleInterval(&next_exchange, &i_interval); } } out: vlc_restorecancel(canc); return NULL; }
static int vnc_connect( filter_t *p_filter ) { filter_sys_t *p_sys = p_filter->p_sys; int port = var_InheritInteger( p_filter, RMTOSD_CFG "port" ); int fd = net_ConnectTCP( p_filter, p_sys->psz_host, port ); if( fd == -1 ) { msg_Err( p_filter, "Could not connect to VNC host" ); return -1; } msg_Dbg( p_filter, "Reading protocol version" ); rfbProtocolVersionMsg pv; if ( !read_exact( p_filter, fd, pv, sz_rfbProtocolVersionMsg ) ) { msg_Err( p_filter, "Could not read version message" ); goto error; } pv[sz_rfbProtocolVersionMsg] = '\0'; /* pv size is sz_rfbProtocolVersionMsg+1 */ msg_Dbg( p_filter, "Server version is %s", pv ); strncpy(pv, "RFB 003.003\n", sz_rfbProtocolVersionMsg); if( !write_exact(p_filter, fd, pv, sz_rfbProtocolVersionMsg) ) { msg_Err( p_filter, "Could not write version message" ); goto error; } msg_Dbg( p_filter, "Reading authentication scheme" ); uint32_t i_authScheme; if( !read_exact( p_filter, fd, &i_authScheme, 4 ) ) { msg_Err( p_filter, "Could not read authentication scheme" ); goto error; } i_authScheme = htonl(i_authScheme); msg_Dbg( p_filter, "Authentication scheme = %x", i_authScheme ); if ( i_authScheme == rfbConnFailed ) { msg_Err( p_filter, "Connection rejected by server" ); goto error; } if (i_authScheme == rfbVncAuth) { unsigned char challenge[CHALLENGESIZE]; if ( !read_exact( p_filter, fd, challenge, CHALLENGESIZE ) ) { msg_Err( p_filter, "Could not read password challenge" ); goto error; } vnc_encrypt_bytes( challenge, p_sys->psz_passwd ); if( !write_exact(p_filter, fd, challenge, CHALLENGESIZE ) ) { msg_Err( p_filter, "Could not write password" ); goto error; } uint32_t i_authResult; if( !read_exact( p_filter, fd, &i_authResult, 4 ) ) { msg_Err( p_filter, "Could not read authentication result" ); goto error; } i_authResult = htonl(i_authResult); if (i_authResult != rfbVncAuthOK) { msg_Err( p_filter, "VNC authentication failed" ); goto error; } } msg_Dbg( p_filter, "Writing client init message" ); rfbClientInitMsg ci; ci.shared = 1; if( !write_exact( p_filter, fd, &ci, sz_rfbClientInitMsg ) ) { msg_Err( p_filter, "Could not write client init message" ); goto error; } msg_Dbg( p_filter, "Reading server init message" ); rfbServerInitMsg si; if( !read_exact( p_filter, fd, &si, sz_rfbServerInitMsg ) ) { msg_Err( p_filter, "Could not read server init message" ); goto error; } si.framebufferWidth = htons(si.framebufferWidth); si.framebufferHeight = htons(si.framebufferHeight); si.format.redMax = htons(si.format.redMax); si.format.greenMax = htons(si.format.greenMax); si.format.blueMax = htons(si.format.blueMax); p_sys->i_vnc_width = si.framebufferWidth; p_sys->i_vnc_height = si.framebufferHeight; msg_Dbg( p_filter, "Servers preferred pixelformat: " "%ux%u, R(%u),G(%u),B(%u), %u bit, depht=%u, %s", si.framebufferWidth, si.framebufferHeight, si.format.redMax, si.format.greenMax, si.format.blueMax, si.format.bitsPerPixel, si.format.depth, si.format.trueColour ? "TrueColor" : "Not-TrueColor"); uint32_t i_nameLength = htonl(si.nameLength); if( i_nameLength > MAX_VNC_SERVER_NAME_LENGTH ) { msg_Err( p_filter, "Server name too long" ); goto error; } char s_ServerName[MAX_VNC_SERVER_NAME_LENGTH+1]; msg_Dbg( p_filter, "Reading server name with size = %u", i_nameLength ); if( !read_exact( p_filter, fd, s_ServerName, i_nameLength ) ) { msg_Err( p_filter, "Could not read server name" ); goto error; } s_ServerName[i_nameLength] = '\0'; if( strcmp( s_ServerName, "VDR-OSD") == 0 ) { msg_Dbg( p_filter, "Server is a VDR" ); p_sys->b_alpha_from_vnc = true; } else { msg_Dbg( p_filter, "Server is a normal VNC" ); p_sys->b_alpha_from_vnc = false; } msg_Dbg( p_filter, "Server init message read properly" ); msg_Dbg( p_filter, "Server name is %s", s_ServerName ); msg_Dbg( p_filter, "Writing SetPixelFormat message" ); rfbSetPixelFormatMsg sp; sp.type = rfbSetPixelFormat; sp.pad1 = sp.pad2 = 0; sp.format.bitsPerPixel = 8; sp.format.depth = 8 ; sp.format.bigEndian = 1; sp.format.trueColour = 0; sp.format.redMax = htons(31); sp.format.greenMax = htons(31); sp.format.blueMax = htons(31); sp.format.redShift = 10; sp.format.greenShift = 5; sp.format.blueShift = 0; sp.format.pad1 = sp.format.pad2 = 0; if( !write_exact( p_filter, fd, &sp, sz_rfbSetPixelFormatMsg) ) { msg_Err( p_filter, "Could not write SetPixelFormat message" ); goto error; } msg_Dbg( p_filter, "Writing SetEncodings message" ); rfbSetEncodingsMsg se; se.type = rfbSetEncodings; se.pad = 0; se.nEncodings = htons( p_sys->b_alpha_from_vnc ? 3 : 2 ); if( !write_exact( p_filter, fd, &se, sz_rfbSetEncodingsMsg) ) { msg_Err( p_filter, "Could not write SetEncodings message begin" ); goto error; } uint32_t i_encoding; msg_Dbg( p_filter, "Writing SetEncodings rfbEncodingCopyRect" ); i_encoding = htonl(rfbEncodingCopyRect); if( !write_exact( p_filter, fd, &i_encoding, 4) ) { msg_Err( p_filter, "Could not write encoding type rfbEncodingCopyRect." ); goto error; } msg_Dbg( p_filter, "Writing SetEncodings rfbEncodingRRE" ); i_encoding = htonl(rfbEncodingRRE); if( !write_exact(p_filter, fd, &i_encoding, 4) ) { msg_Err( p_filter, "Could not write encoding type rfbEncodingRRE." ); goto error; } if( p_sys->b_alpha_from_vnc ) { msg_Dbg( p_filter, "Writing SetEncodings rfbEncSpecialUseAlpha" ); i_encoding = 0x00F0FFFF; /* rfbEncSpecialUseAlpha is 0xFFFFF000 * before we swap it */ if( !write_exact(p_filter, fd, &i_encoding, 4) ) { msg_Err( p_filter, "Could not write encoding type rfbEncSpecialUseAlpha." ); goto error; } } return fd; error: net_Close( fd ); return -1; }
/***************************************************************************** * Run : call Handshake() then submit songs *****************************************************************************/ static void Run( intf_thread_t *p_intf ) { char *psz_submit, *psz_submit_song, *psz_submit_tmp; int i_net_ret; int i_song; uint8_t p_buffer[1024]; char *p_buffer_pos; int i_post_socket; int canc = vlc_savecancel(); intf_sys_t *p_sys = p_intf->p_sys; /* main loop */ for( ;; ) { bool b_wait = false; vlc_restorecancel( canc ); vlc_mutex_lock( &p_sys->lock ); mutex_cleanup_push( &p_sys->lock ); if( mdate() < p_sys->next_exchange ) /* wait until we can resubmit, i.e. */ b_wait = vlc_cond_timedwait( &p_sys->wait, &p_sys->lock, p_sys->next_exchange ) == 0; else /* wait for data to submit */ /* we are signaled each time there is a song to submit */ vlc_cond_wait( &p_sys->wait, &p_sys->lock ); vlc_cleanup_run(); canc = vlc_savecancel(); if( b_wait ) continue; /* holding on until next_exchange */ /* handshake if needed */ if( !p_sys->b_handshaked ) { msg_Dbg( p_intf, "Handshaking with last.fm ..." ); switch( Handshake( p_intf ) ) { case VLC_ENOMEM: return; case VLC_ENOVAR: /* username not set */ dialog_Fatal( p_intf, _("Last.fm username not set"), "%s", _("Please set a username or disable the " "audioscrobbler plugin, and restart VLC.\n" "Visit http://www.last.fm/join/ to get an account.") ); return; case VLC_SUCCESS: msg_Dbg( p_intf, "Handshake successfull :)" ); p_sys->b_handshaked = true; p_sys->i_interval = 0; p_sys->next_exchange = mdate(); break; case VLC_AUDIOSCROBBLER_EFATAL: msg_Warn( p_intf, "Exiting..." ); return; case VLC_EGENERIC: default: /* protocol error : we'll try later */ HandleInterval( &p_sys->next_exchange, &p_sys->i_interval ); break; } /* if handshake failed let's restart the loop */ if( !p_sys->b_handshaked ) continue; } msg_Dbg( p_intf, "Going to submit some data..." ); /* The session may be invalid if there is a trailing \n */ char *psz_ln = strrchr( p_sys->psz_auth_token, '\n' ); if( psz_ln ) *psz_ln = '\0'; if( !asprintf( &psz_submit, "s=%s", p_sys->psz_auth_token ) ) { /* Out of memory */ return; } /* forge the HTTP POST request */ vlc_mutex_lock( &p_sys->lock ); audioscrobbler_song_t *p_song; for( i_song = 0 ; i_song < p_sys->i_songs ; i_song++ ) { p_song = &p_sys->p_queue[i_song]; if( !asprintf( &psz_submit_song, "&a%%5B%d%%5D=%s" "&t%%5B%d%%5D=%s" "&i%%5B%d%%5D=%u" "&o%%5B%d%%5D=P" "&r%%5B%d%%5D=" "&l%%5B%d%%5D=%d" "&b%%5B%d%%5D=%s" "&n%%5B%d%%5D=%s" "&m%%5B%d%%5D=%s", i_song, p_song->psz_a, i_song, p_song->psz_t, i_song, (unsigned)p_song->date, /* HACK: %ju (uintmax_t) unsupported on Windows */ i_song, i_song, i_song, p_song->i_l, i_song, p_song->psz_b, i_song, p_song->psz_n, i_song, p_song->psz_m ) ) { /* Out of memory */ vlc_mutex_unlock( &p_sys->lock ); return; } psz_submit_tmp = psz_submit; if( !asprintf( &psz_submit, "%s%s", psz_submit_tmp, psz_submit_song ) ) { /* Out of memory */ free( psz_submit_tmp ); free( psz_submit_song ); vlc_mutex_unlock( &p_sys->lock ); return; } free( psz_submit_song ); free( psz_submit_tmp ); } vlc_mutex_unlock( &p_sys->lock ); i_post_socket = net_ConnectTCP( p_intf, p_sys->psz_submit_host, p_sys->i_submit_port ); if ( i_post_socket == -1 ) { /* If connection fails, we assume we must handshake again */ HandleInterval( &p_sys->next_exchange, &p_sys->i_interval ); p_sys->b_handshaked = false; free( psz_submit ); continue; } /* we transmit the data */ i_net_ret = net_Printf( p_intf, i_post_socket, NULL, POST_REQUEST, p_sys->psz_submit_file, (unsigned)strlen( psz_submit ), p_sys->psz_submit_host, VERSION, psz_submit ); free( psz_submit ); if ( i_net_ret == -1 ) { /* If connection fails, we assume we must handshake again */ HandleInterval( &p_sys->next_exchange, &p_sys->i_interval ); p_sys->b_handshaked = false; continue; } i_net_ret = net_Read( p_intf, i_post_socket, NULL, p_buffer, 1023, false ); if ( i_net_ret <= 0 ) { /* if we get no answer, something went wrong : try again */ continue; } net_Close( i_post_socket ); p_buffer[i_net_ret] = '\0'; p_buffer_pos = strstr( ( char * ) p_buffer, "FAILED" ); if ( p_buffer_pos ) { msg_Warn( p_intf, "%s", p_buffer_pos ); HandleInterval( &p_sys->next_exchange, &p_sys->i_interval ); continue; } p_buffer_pos = strstr( ( char * ) p_buffer, "BADSESSION" ); if ( p_buffer_pos ) { msg_Err( p_intf, "Authentication failed (BADSESSION), are you connected to last.fm with another program ?" ); p_sys->b_handshaked = false; HandleInterval( &p_sys->next_exchange, &p_sys->i_interval ); continue; } p_buffer_pos = strstr( ( char * ) p_buffer, "OK" ); if ( p_buffer_pos ) { int i; for( i = 0; i < p_sys->i_songs; i++ ) DeleteSong( &p_sys->p_queue[i] ); p_sys->i_songs = 0; p_sys->i_interval = 0; p_sys->next_exchange = mdate(); msg_Dbg( p_intf, "Submission successful!" ); } else { msg_Err( p_intf, "Authentication failed, handshaking again (%s)", p_buffer ); p_sys->b_handshaked = false; HandleInterval( &p_sys->next_exchange, &p_sys->i_interval ); continue; } } vlc_restorecancel( canc ); }
/***************************************************************************** * Run : call Handshake() then submit items *****************************************************************************/ static void Run(intf_thread_t *p_intf) { msg_Dbg(p_intf, "Entering Run()"); uint8_t p_buffer[1024]; int parse_url_ret; int canc = vlc_savecancel(); char *psz_scrobbler_url; mtime_t next_exchange; /**< when can we send data */ unsigned int i_interval; /**< waiting interval (secs)*/ intf_sys_t *p_sys = p_intf->p_sys; psz_scrobbler_url = var_InheritString(p_intf, "mediascrobbler-url"); if(!psz_scrobbler_url) return; msg_Dbg(p_intf, "Scrobbler URL: %s", psz_scrobbler_url); /* main loop */ for (;;) { vlc_restorecancel(canc); vlc_mutex_lock(&p_sys->lock); mutex_cleanup_push(&p_sys->lock); msg_Dbg(p_intf, "Next exchange %lld (current: %lld)", next_exchange, mdate()); do { vlc_cond_wait(&p_sys->wait, &p_sys->lock); } while (mdate() < next_exchange); vlc_cleanup_run(); canc = vlc_savecancel(); msg_Dbg(p_intf, "Going to submit some data..."); char *psz_submit; if (asprintf(&psz_submit, "s=%s", psz_scrobbler_url) == -1) return; msg_Dbg(p_intf, "Going to parse URL (%s)", psz_scrobbler_url); parse_url_ret = ParseURL(psz_scrobbler_url, &p_sys->psz_submit_host, &p_sys->psz_submit_file, &p_sys->i_submit_port); if(parse_url_ret != VLC_SUCCESS) { msg_Err(p_intf, "Couldn't process URL, can't continue"); return; } msg_Dbg(p_intf, "Submit Host: %s", p_sys->psz_submit_host); msg_Dbg(p_intf, "Preparing to submit %d items", p_sys->i_items); /* forge the HTTP POST request */ vlc_mutex_lock(&p_sys->lock); mediascrobbler_item_t *p_item; for (int i_item = 0 ; i_item < p_sys->i_items ; i_item++) { char *psz_submit_item, *psz_submit_tmp; p_item = &p_sys->p_queue[i_item]; if (asprintf(&psz_submit_item, "&n%%5B%d%%5D=%s" "&u%%5B%d%%5D=%s" "&d%%5B%d%%5D=%ju" "&l%%5B%d%%5D=%d", i_item, p_item->psz_n, i_item, p_item->psz_u, i_item, p_item->date, i_item, p_item->i_l ) == -1) { /* Out of memory */ vlc_mutex_unlock(&p_sys->lock); return; } psz_submit_tmp = psz_submit; if (asprintf(&psz_submit, "%s%s", psz_submit_tmp, psz_submit_item) == -1) { /* Out of memory */ free(psz_submit_tmp); free(psz_submit_item); vlc_mutex_unlock(&p_sys->lock); return; } free(psz_submit_item); free(psz_submit_tmp); } vlc_mutex_unlock(&p_sys->lock); int i_post_socket = net_ConnectTCP(p_intf, p_sys->psz_submit_host, p_sys->i_submit_port); if (i_post_socket == -1) { msg_Warn(p_intf, "Couldn't talk to the API, waiting to try again. (%d)", i_interval); HandleInterval(&next_exchange, &i_interval); free(psz_submit); continue; } /* we transmit the data */ int i_net_ret = net_Printf(p_intf, i_post_socket, NULL, "POST %s HTTP/1.1\r\n" "Accept-Encoding: identity\r\n" "Content-length: %zu\r\n" "Connection: close\r\n" "Content-type: application/x-www-form-urlencoded\r\n" "Host: %s\r\n" "User-agent: VLC media player/"VERSION"\r\n" "\r\n" "%s\r\n" "\r\n", p_sys->psz_submit_file, strlen(psz_submit), p_sys->psz_submit_host, psz_submit ); free(psz_submit); if (i_net_ret == -1) { /* If connection fails, back off the timer, and try again */ HandleInterval(&next_exchange, &i_interval); continue; } i_net_ret = net_Read(p_intf, i_post_socket, NULL, p_buffer, sizeof(p_buffer) - 1, false); if (i_net_ret <= 0) { /* if we get no answer, something went wrong : try again */ continue; } net_Close(i_post_socket); p_buffer[i_net_ret] = '\0'; char *failed = strstr((char *) p_buffer, "FAILED"); if (failed) { msg_Warn(p_intf, "%s", failed); HandleInterval(&next_exchange, &i_interval); continue; } if (strstr((char *) p_buffer, "BADAPIKEY")) { msg_Err(p_intf, "Authentication failed (BADAPIKEY), are you sure your API key is valid?"); HandleInterval(&next_exchange, &i_interval); continue; } if (strstr((char *) p_buffer, "OK")) { for (int i = 0; i < p_sys->i_items; i++) DeleteItem(&p_sys->p_queue[i]); p_sys->i_items = 0; i_interval = 0; next_exchange = mdate(); msg_Dbg(p_intf, "Submission successful!"); } else { msg_Err(p_intf, "Authentication failed, trying again (%s)", p_buffer); HandleInterval(&next_exchange, &i_interval); } } vlc_restorecancel(canc); }
/***************************************************************************** * Run : call Handshake() then submit songs *****************************************************************************/ static void *Run(void *data) { intf_thread_t *p_intf = data; uint8_t p_buffer[1024]; int canc = vlc_savecancel(); bool b_handshaked = false; bool b_nowp_submission_ongoing = false; /* data about audioscrobbler session */ mtime_t next_exchange = 0; /**< when can we send data */ unsigned int i_interval = 0; /**< waiting interval (secs)*/ intf_sys_t *p_sys = p_intf->p_sys; /* main loop */ for (;;) { vlc_restorecancel(canc); mwait(next_exchange); vlc_mutex_lock(&p_sys->lock); mutex_cleanup_push(&p_sys->lock); while (p_sys->i_songs == 0 && p_sys->b_submit_nowp == false) vlc_cond_wait(&p_sys->wait, &p_sys->lock); vlc_cleanup_pop(); vlc_mutex_unlock(&p_sys->lock); canc = vlc_savecancel(); /* handshake if needed */ if (!b_handshaked) { msg_Dbg(p_intf, "Handshaking with last.fm ..."); switch(Handshake(p_intf)) { case VLC_ENOMEM: goto out; case VLC_ENOVAR: /* username not set */ vlc_dialog_display_error(p_intf, _("Last.fm username not set"), "%s", _("Please set a username or disable the " "audioscrobbler plugin, and restart VLC.\n" "Visit http://www.last.fm/join/ to get an account.")); goto out; case VLC_SUCCESS: msg_Dbg(p_intf, "Handshake successful :)"); b_handshaked = true; i_interval = 0; next_exchange = 0; break; case VLC_AUDIOSCROBBLER_EFATAL: msg_Warn(p_intf, "Exiting..."); goto out; case VLC_EGENERIC: default: /* protocol error : we'll try later */ HandleInterval(&next_exchange, &i_interval); break; } /* if handshake failed let's restart the loop */ if (!b_handshaked) continue; } msg_Dbg(p_intf, "Going to submit some data..."); char *psz_submit; vlc_url_t *url; char *psz_submit_song, *psz_submit_tmp; if (asprintf(&psz_submit, "s=%s", p_sys->psz_auth_token) == -1) break; /* forge the HTTP POST request */ vlc_mutex_lock(&p_sys->lock); if (p_sys->b_submit_nowp) { b_nowp_submission_ongoing = true; url = &p_sys->p_nowp_url; if (asprintf(&psz_submit_song, "&a=%s" "&t=%s" "&b=%s" "&l=%d" "&n=%s" "&m=%s", p_sys->p_current_song.psz_a, p_sys->p_current_song.psz_t, p_sys->p_current_song.psz_b ? p_sys->p_current_song.psz_b : "", p_sys->p_current_song.i_l, p_sys->p_current_song.psz_n ? p_sys->p_current_song.psz_n : "", p_sys->p_current_song.psz_m ? p_sys->p_current_song.psz_m : "" ) == -1) { /* Out of memory */ vlc_mutex_unlock(&p_sys->lock); goto out; } } else { url = &p_sys->p_submit_url; audioscrobbler_song_t *p_song; for (int i_song = 0 ; i_song < p_sys->i_songs ; i_song++) { p_song = &p_sys->p_queue[i_song]; if (asprintf(&psz_submit_song, "&a%%5B%d%%5D=%s" "&t%%5B%d%%5D=%s" "&i%%5B%d%%5D=%u" "&o%%5B%d%%5D=P" "&r%%5B%d%%5D=" "&l%%5B%d%%5D=%d" "&b%%5B%d%%5D=%s" "&n%%5B%d%%5D=%s" "&m%%5B%d%%5D=%s", i_song, p_song->psz_a, i_song, p_song->psz_t, i_song, (unsigned)p_song->date, /* HACK: %ju (uintmax_t) unsupported on Windows */ i_song, i_song, i_song, p_song->i_l, i_song, p_song->psz_b ? p_song->psz_b : "", i_song, p_song->psz_n ? p_song->psz_n : "", i_song, p_song->psz_m ? p_song->psz_m : "" ) == -1) { /* Out of memory */ vlc_mutex_unlock(&p_sys->lock); goto out; } } } psz_submit_tmp = psz_submit; int print_ret = asprintf(&psz_submit, "%s%s", psz_submit_tmp, psz_submit_song); free(psz_submit_tmp); free(psz_submit_song); vlc_mutex_unlock(&p_sys->lock); if (print_ret == -1) { /* Out of memory */ goto out; } int i_post_socket = net_ConnectTCP(p_intf, url->psz_host, url->i_port); if (i_post_socket == -1) { /* If connection fails, we assume we must handshake again */ HandleInterval(&next_exchange, &i_interval); b_handshaked = false; free(psz_submit); continue; } /* we transmit the data */ int i_net_ret = net_Printf(p_intf, i_post_socket, "POST %s HTTP/1.1\r\n" "Host: %s\r\n" "User-Agent: "PACKAGE_NAME"/"PACKAGE_VERSION"\r\n" "Connection: close\r\n" "Accept-Encoding: identity\r\n" "Content-Type: application/x-www-form-urlencoded\r\n" "Content-Length: %zu\r\n" "\r\n" "%s\r\n" "\r\n", url->psz_path, url->psz_host, strlen(psz_submit), psz_submit); free(psz_submit); if (i_net_ret == -1) { /* If connection fails, we assume we must handshake again */ HandleInterval(&next_exchange, &i_interval); b_handshaked = false; net_Close(i_post_socket); continue; } /* FIXME: this might wait forever */ struct pollfd ufd = { .fd = i_post_socket, .events = POLLIN }; while( poll( &ufd, 1, -1 ) == -1 ); /* FIXME: With TCP, you should never assume that a single read will * return the entire response... */ i_net_ret = recv(i_post_socket, p_buffer, sizeof(p_buffer) - 1, 0); if (i_net_ret <= 0) { /* if we get no answer, something went wrong : try again */ net_Close(i_post_socket); continue; } net_Close(i_post_socket); p_buffer[i_net_ret] = '\0'; char *failed = strstr((char *) p_buffer, "FAILED"); if (failed) { msg_Warn(p_intf, "%s", failed); HandleInterval(&next_exchange, &i_interval); continue; } if (strstr((char *) p_buffer, "BADSESSION")) { msg_Err(p_intf, "Authentication failed (BADSESSION), are you connected to last.fm with another program ?"); b_handshaked = false; HandleInterval(&next_exchange, &i_interval); continue; } if (strstr((char *) p_buffer, "OK")) { if (b_nowp_submission_ongoing) { b_nowp_submission_ongoing = false; p_sys->b_submit_nowp = false; } else { for (int i = 0; i < p_sys->i_songs; i++) DeleteSong(&p_sys->p_queue[i]); p_sys->i_songs = 0; } i_interval = 0; next_exchange = 0; msg_Dbg(p_intf, "Submission successful!"); } else { msg_Err(p_intf, "Authentication failed, handshaking again (%s)", p_buffer); b_handshaked = false; HandleInterval(&next_exchange, &i_interval); } } out: vlc_restorecancel(canc); return NULL; }