📄 stream_cddb.c
字号:
http_hdr->body_size = 0; return cddb_write_cache(cddb_data); default: mp_msg(MSGT_DEMUX, MSGL_FIXME, MSGTR_MPDEMUX_CDDB_UnhandledCode); } return 0;}intcddb_request_titles(cddb_data_t *cddb_data) { char command[1024]; sprintf( command, "cddb+read+%s+%08lx", cddb_data->category, cddb_data->disc_id); return cddb_http_request(command, cddb_read_parse, cddb_data); }intcddb_parse_matches_list(HTTP_header_t *http_hdr, cddb_data_t *cddb_data) { char album_title[100]; char *ptr = NULL; int ret; ptr = strstr(http_hdr->body, "\n"); if( ptr==NULL ) { mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_MPDEMUX_CDDB_UnableToFindEOL); return -1; } ptr++; // We have a list of exact/inexact matches, so which one do we use? // So let's take the first one. ret = sscanf(ptr, "%99s %08lx %99s", cddb_data->category, &(cddb_data->disc_id), album_title); if( ret!=3 ) { mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_ParseError); return -1; } ptr = strstr(http_hdr->body, album_title); if( ptr!=NULL ) { char *ptr2; int len; ptr2 = strstr(ptr, "\n"); if( ptr2==NULL ) { len = (http_hdr->body_size)-(ptr-(http_hdr->body)); } else { len = ptr2-ptr+1; } strncpy(album_title, ptr, len); album_title[len-2]='\0'; } mp_msg(MSGT_DEMUX, MSGL_STATUS, MSGTR_MPDEMUX_CDDB_ParseOKFoundAlbumTitle, album_title); return 0;}intcddb_query_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data) { char album_title[100]; char *ptr = NULL; int ret, status; ret = sscanf( http_hdr->body, "%d ", &status); if( ret!=1 ) { mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_ParseError); return -1; } switch(status) { case 200: // Found exact match ret = sscanf(http_hdr->body, "%d %99s %08lx %99s", &status, cddb_data->category, &(cddb_data->disc_id), album_title); if( ret!=4 ) { mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_ParseError); return -1; } ptr = strstr(http_hdr->body, album_title); if( ptr!=NULL ) { char *ptr2; int len; ptr2 = strstr(ptr, "\n"); if( ptr2==NULL ) { len = (http_hdr->body_size)-(ptr-(http_hdr->body)); } else { len = ptr2-ptr+1; } strncpy(album_title, ptr, len); album_title[len-2]='\0'; } mp_msg(MSGT_DEMUX, MSGL_STATUS, MSGTR_MPDEMUX_CDDB_ParseOKFoundAlbumTitle, album_title); return cddb_request_titles(cddb_data); case 202: // No match found mp_msg(MSGT_DEMUX, MSGL_WARN, MSGTR_MPDEMUX_CDDB_AlbumNotFound); break; case 210: // Found exact matches, list follows cddb_parse_matches_list(http_hdr, cddb_data); return cddb_request_titles(cddb_data);/*body=[210 Found exact matches, list follows (until terminating `.')misc c711930d Santana / Supernaturalrock c711930d Santana / Supernaturalblues c711930d Santana / Supernatural.]*/ case 211: // Found inexact matches, list follows cddb_parse_matches_list(http_hdr, cddb_data); return cddb_request_titles(cddb_data); case 500: mp_msg(MSGT_DEMUX, MSGL_FIXME, MSGTR_MPDEMUX_CDDB_ServerReturnsCommandSyntaxErr); break; default: mp_msg(MSGT_DEMUX, MSGL_FIXME, MSGTR_MPDEMUX_CDDB_UnhandledCode); } return -1;}intcddb_proto_level_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data) { int max; int ret, status; char *ptr; ret = sscanf( http_hdr->body, "%d ", &status); if( ret!=1 ) { mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_ParseError); return -1; } switch(status) { case 210: ptr = strstr(http_hdr->body, "max proto:"); if( ptr==NULL ) { mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_ParseError); return -1; } ret = sscanf(ptr, "max proto: %d", &max); if( ret!=1 ) { mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_ParseError); return -1; } cddb_data->freedb_proto_level = max; return 0; default: mp_msg(MSGT_DEMUX, MSGL_FIXME, MSGTR_MPDEMUX_CDDB_UnhandledCode); } return -1;}intcddb_get_proto_level(cddb_data_t *cddb_data) { return cddb_http_request("stat", cddb_proto_level_parse, cddb_data);}intcddb_freedb_sites_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data) { int ret, status; ret = sscanf( http_hdr->body, "%d ", &status); if( ret!=1 ) { mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_ParseError); return -1; } switch(status) { case 210: // TODO: Parse the sites ret = cddb_data->anonymous; // For gcc complaining about unused parameter. return 0; case 401: mp_msg(MSGT_DEMUX, MSGL_FIXME, MSGTR_MPDEMUX_CDDB_NoSitesInfoAvailable); break; default: mp_msg(MSGT_DEMUX, MSGL_FIXME, MSGTR_MPDEMUX_CDDB_UnhandledCode); } return -1;}intcddb_get_freedb_sites(cddb_data_t *cddb_data) { return cddb_http_request("sites", cddb_freedb_sites_parse, cddb_data);}voidcddb_create_hello(cddb_data_t *cddb_data) { char host_name[51]; char *user_name; if( cddb_data->anonymous ) { // Default is anonymous /* Note from Eduardo Pérez Ureta <eperez@it.uc3m.es> : * We don't send current user/host name in hello to prevent spam. * Software that sends this is considered spyware * that most people don't like. */ user_name = "anonymous"; strcpy(host_name, "localhost"); } else { if( gethostname(host_name, 50)<0 ) { strcpy(host_name, "localhost"); } user_name = getenv("LOGNAME"); } sprintf( cddb_data->cddb_hello, "&hello=%s+%s+%s+%s", user_name, host_name, "MPlayer", VERSION );}int cddb_retrieve(cddb_data_t *cddb_data) { char offsets[1024], command[1024]; char *ptr; unsigned int i, time_len; int ret; ptr = offsets; for( i=0; i<cddb_data->tracks ; i++ ) { ptr += sprintf(ptr, "%d+", cdtoc[i].frame ); if (ptr-offsets > sizeof offsets - 40) break; } ptr[0]=0; time_len = (cdtoc[cddb_data->tracks].frame)/75; cddb_data->freedb_server = DEFAULT_FREEDB_SERVER; cddb_data->freedb_proto_level = 1; cddb_data->xmcd_file = NULL; cddb_create_hello(cddb_data); if( cddb_get_proto_level(cddb_data)<0 ) { mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_MPDEMUX_CDDB_FailedToGetProtocolLevel); return -1; } //cddb_get_freedb_sites(&cddb_data); sprintf(command, "cddb+query+%08lx+%d+%s%d", cddb_data->disc_id, cddb_data->tracks, offsets, time_len ); ret = cddb_http_request(command, cddb_query_parse, cddb_data); if( ret<0 ) return -1; if( cddb_data->cache_dir!=NULL ) { free(cddb_data->cache_dir); } return 0;}intcddb_resolve(const char *dev, char **xmcd_file) { char cddb_cache_dir[] = DEFAULT_CACHE_DIR; char *home_dir = NULL; cddb_data_t cddb_data; if (cdtoc_last_track <= 0) { cdtoc_last_track = read_toc(dev); if (cdtoc_last_track < 0) { mp_msg(MSGT_OPEN, MSGL_ERR, MSGTR_MPDEMUX_CDDB_FailedToOpenDevice, dev); return -1; } } cddb_data.tracks = cdtoc_last_track; cddb_data.disc_id = cddb_discid(cddb_data.tracks); cddb_data.anonymous = 1; // Don't send user info by default // Check if there is a CD in the drive // FIXME: That's not really a good way to check if( cddb_data.disc_id==0 ) { mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_MPDEMUX_CDDB_NoCDInDrive); return -1; } home_dir = getenv("HOME");#ifdef __MINGW32__ if( home_dir==NULL ) home_dir = getenv("USERPROFILE"); if( home_dir==NULL ) home_dir = getenv("HOMEPATH"); // Last resort, store the cddb cache in the mplayer directory if( home_dir==NULL ) home_dir = (char *)get_path("");#endif if( home_dir==NULL ) { cddb_data.cache_dir = NULL; } else { cddb_data.cache_dir = malloc(strlen(home_dir)+strlen(cddb_cache_dir)+1); if( cddb_data.cache_dir==NULL ) { mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_MemAllocFailed); return -1; } sprintf(cddb_data.cache_dir, "%s%s", home_dir, cddb_cache_dir ); } // Check for a cached file if( cddb_read_cache(&cddb_data)<0 ) { // No Cache found if( cddb_retrieve(&cddb_data)<0 ) { return -1; } } if( cddb_data.xmcd_file!=NULL ) {// printf("%s\n", cddb_data.xmcd_file ); *xmcd_file = cddb_data.xmcd_file; return 0; } return -1;}/******************************************************************************************************************* * * xmcd parser * *******************************************************************************************************************/char*xmcd_parse_dtitle(cd_info_t *cd_info, char *line) { char *ptr, *album; ptr = strstr(line, "DTITLE="); if( ptr!=NULL ) { ptr += 7; album = strstr(ptr, "/"); if( album==NULL ) return NULL; cd_info->album = malloc(strlen(album+2)+1); if( cd_info->album==NULL ) { return NULL; } strcpy( cd_info->album, album+2 ); album--; album[0] = '\0'; cd_info->artist = malloc(strlen(ptr)+1); if( cd_info->artist==NULL ) { return NULL; } strcpy( cd_info->artist, ptr ); } return ptr;}char*xmcd_parse_dgenre(cd_info_t *cd_info, char *line) { char *ptr; ptr = strstr(line, "DGENRE="); if( ptr!=NULL ) { ptr += 7; cd_info->genre = malloc(strlen(ptr)+1); if( cd_info->genre==NULL ) { return NULL; } strcpy( cd_info->genre, ptr ); } return ptr;}char*xmcd_parse_ttitle(cd_info_t *cd_info, char *line) { unsigned int track_nb; unsigned long sec, off; char *ptr; ptr = strstr(line, "TTITLE"); if( ptr!=NULL ) { ptr += 6; // Here we point to the track number track_nb = atoi(ptr); ptr = strstr(ptr, "="); if( ptr==NULL ) return NULL; ptr++; sec = cdtoc[track_nb].frame; off = cdtoc[track_nb+1].frame-sec+1; cd_info_add_track( cd_info, ptr, track_nb+1, (unsigned int)(off/(60*75)), (unsigned int)((off/75)%60), (unsigned int)(off%75), sec, off ); } return ptr;}cd_info_t*cddb_parse_xmcd(char *xmcd_file) { cd_info_t *cd_info = NULL; int length, pos = 0; char *ptr, *ptr2; unsigned int audiolen; if( xmcd_file==NULL ) return NULL; cd_info = cd_info_new(); if( cd_info==NULL ) { return NULL; } length = strlen(xmcd_file); ptr = xmcd_file; while( ptr!=NULL && pos<length ) { // Read a line ptr2 = ptr; while( ptr2[0]!='\0' && ptr2[0]!='\r' && ptr2[0]!='\n' ) ptr2++; if( ptr2[0]=='\0' ) { break; } ptr2[0] = '\0'; // Ignore comments if( ptr[0]!='#' ) { // Search for the album title if( xmcd_parse_dtitle(cd_info, ptr) ); // Search for the genre else if( xmcd_parse_dgenre(cd_info, ptr) ); // Search for a track title else if( xmcd_parse_ttitle(cd_info, ptr) ) audiolen++; // <-- audiolen++ to shut up gcc warning } if( ptr2[1]=='\n' ) ptr2++; pos = (ptr2+1)-ptr; ptr = ptr2+1; } audiolen = cdtoc[cd_info->nb_tracks].frame-cdtoc[0].frame; cd_info->min = (unsigned int)(audiolen/(60*75)); cd_info->sec = (unsigned int)((audiolen/75)%60); cd_info->msec = (unsigned int)(audiolen%75); return cd_info;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -