Mercurial > pidgin
view src/mediastreamer/msringplayer.c @ 12360:3153661f4d5c
[gaim-migrate @ 14664]
Faceprint is concerned about 2 things:
1)some of the random colors are very close together.
as best we can tell, there are two ways to fix this
1a) for each proposed color, iterate the entire list of selected
colors, looking to ensure that it is not too close to any of them.
this is an O(n^2) operation, with n >= 220 (the current number of
colors we look for)
1b) iterate the entire set of possible colors, skipping ahead by some
guess (rather than iterating by 1). this is an O(n^3) operation,
where n is 65535/(whatever we skip ahead by). This is not only a
more expensive operation, but because of the nature of the color
list, it is not _necessarily_ going to yield more predictable
results, skipping ahead 5 (or any other number) does not necessarily
guarantee that you've skipped 5 very similar colors.
2) as you can see, either solution to #1 is potentially a resource hog.
#1a is a random delay, #1b is inherently expensive. How often #1a will
exceed the bound #1b, if ever, is unknown.
rather than doing either of these, we settled on a middle course: a .h
file has been created containing a set of colors. currently the set we
were previously hard coded to. Gaim will search that list for usable
colors and start randomly looking only if that list does not contain
sufficient usable colors. ideally this list would be generated to have
colors that are known to be a "safe" distance appart, that is colors that
you can tell appart. and Ideally it would have a (small) multiple of the
number of colors we are searching for. This should ensure that IF we go
to randomly searching, we need do so only for a few colors.
Right now I have no good way to generate a "safe" list of colors though.
committer: Tailor Script <tailor@pidgin.im>
author | Luke Schierer <lschiere@pidgin.im> |
---|---|
date | Mon, 05 Dec 2005 21:46:47 +0000 |
parents | 1c771536a032 |
children |
line wrap: on
line source
/* The mediastreamer library aims at providing modular media processing and I/O for linphone, but also for any telephony application. Copyright (C) 2001 Simon MORLAT simon.morlat@linphone.org This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "msringplayer.h" #include "mssync.h" #include <unistd.h> #include <fcntl.h> #include <sys/types.h> #include <string.h> #include <errno.h> #include <stdlib.h> #include "waveheader.h" #define WAVE_HEADER_OFFSET sizeof(wave_header_t) enum { PLAY_RING, PLAY_SILENCE}; static int supported_freq[6]={8000,11025,16000,22050,32000,44100}; gint freq_is_supported(gint freq){ int i; for (i=0;i<6;i++){ if (abs(supported_freq[i]-freq)<50) return supported_freq[i]; } return 0; } static MSRingPlayerClass *ms_ring_player_class=NULL; /** * ms_ring_player_new: * @name: The path to the 16-bit 8khz raw file to be played as a ring. * @seconds: The number of seconds that separates two rings. * * Allocates a new MSRingPlayer object. * * * Returns: a pointer the the object, NULL if name could not be open. */ MSFilter * ms_ring_player_new(char *name, gint seconds) { MSRingPlayer *r; int fd=-1; if ((name!=NULL) && (strlen(name)!=0)) { fd=open(name,O_RDONLY); if (fd<0) { g_warning("ms_ring_player_new: failed to open %s.\n",name); return NULL; } }else { g_warning("ms_ring_player_new: Bad file name"); return NULL; } r=g_new(MSRingPlayer,1); ms_ring_player_init(r); if (ms_ring_player_class==NULL) { ms_ring_player_class=g_new(MSRingPlayerClass,1); ms_ring_player_class_init(ms_ring_player_class); } MS_FILTER(r)->klass=MS_FILTER_CLASS(ms_ring_player_class); r->fd=fd; r->silence=seconds; r->freq=8000; if (strstr(name,".wav")!=NULL){ wave_header_t header; int freq,freq2; /* read the header */ read(fd,&header,sizeof(wave_header_t)); freq=wave_header_get_rate(&header); if ((freq2=freq_is_supported(freq))>0){ r->freq=freq2; }else { g_warning("Unsupported sampling rate %i",freq); r->freq=8000; } r->channel=wave_header_get_channel(&header); lseek(fd,WAVE_HEADER_OFFSET,SEEK_SET); #ifdef WORDS_BIGENDIAN r->need_swap=1; #else r->need_swap=0; #endif } ms_ring_player_set_property(r, MS_FILTER_PROPERTY_FREQ,&r->freq); r->state=PLAY_RING; return(MS_FILTER(r)); } /* FOR INTERNAL USE*/ void ms_ring_player_init(MSRingPlayer *r) { ms_filter_init(MS_FILTER(r)); MS_FILTER(r)->outfifos=r->foutputs; MS_FILTER(r)->outqueues=r->qoutputs; memset(r->foutputs,0,sizeof(MSFifo*)*MS_RING_PLAYER_MAX_OUTPUTS); memset(r->qoutputs,0,sizeof(MSQueue*)*MS_RING_PLAYER_MAX_OUTPUTS); r->fd=-1; r->current_pos=0; r->need_swap=0; r->sync=NULL; } gint ms_ring_player_set_property(MSRingPlayer *f,MSFilterProperty prop, void *value) { switch(prop){ case MS_FILTER_PROPERTY_FREQ: f->rate=((gint*)value)[0]*2; f->silence_bytes=f->silence*f->rate; if (f->sync!=NULL) f->gran=(f->rate*f->sync->interval/1000)*2; break; case MS_FILTER_PROPERTY_BITRATE: case MS_FILTER_PROPERTY_CHANNELS: case MS_FILTER_PROPERTY_FMTP: default: break; } return 0; } gint ms_ring_player_get_property(MSRingPlayer *f,MSFilterProperty prop, void *value) { switch(prop){ case MS_FILTER_PROPERTY_FREQ: ((gint*)value)[0]=f->freq; break; case MS_FILTER_PROPERTY_CHANNELS: ((gint*)value)[0]=f->channel; break; case MS_FILTER_PROPERTY_BITRATE: case MS_FILTER_PROPERTY_FMTP: default: break; } return 0; } gint ms_ring_player_get_sample_freq(MSRingPlayer *obj){ return obj->freq; } void ms_ring_player_class_init(MSRingPlayerClass *klass) { ms_filter_class_init(MS_FILTER_CLASS(klass)); ms_filter_class_set_name(MS_FILTER_CLASS(klass),"ringplay"); ms_filter_class_set_attr(MS_FILTER_CLASS(klass),FILTER_IS_SOURCE); MS_FILTER_CLASS(klass)->max_foutputs=MS_RING_PLAYER_MAX_OUTPUTS; MS_FILTER_CLASS(klass)->max_qoutputs=MS_RING_PLAYER_MAX_OUTPUTS; MS_FILTER_CLASS(klass)->w_maxgran=MS_RING_PLAYER_DEF_GRAN; MS_FILTER_CLASS(klass)->setup=(MSFilterSetupFunc)ms_ring_player_setup; MS_FILTER_CLASS(klass)->destroy=(MSFilterDestroyFunc)ms_ring_player_destroy; MS_FILTER_CLASS(klass)->process=(MSFilterProcessFunc)ms_ring_player_process; MS_FILTER_CLASS(klass)->set_property=(MSFilterPropertyFunc)ms_ring_player_set_property; MS_FILTER_CLASS(klass)->get_property=(MSFilterPropertyFunc)ms_ring_player_get_property; } void ms_ring_player_process(MSRingPlayer *r) { MSFifo *f; gint err; gint processed=0; gint gran=r->gran; char *p; g_return_if_fail(gran>0); /* process output fifos*/ f=r->foutputs[0]; ms_fifo_get_write_ptr(f,gran,(void**)&p); g_return_if_fail(p!=NULL); for (processed=0;processed<gran;){ switch(r->state){ case PLAY_RING: err=read(r->fd,&p[processed],gran-processed); if (err<0) { memset(&p[processed],0,gran-processed); processed=gran; g_warning("ms_ring_player_process: failed to read: %s.\n",strerror(errno)); return; } else if (err<gran) {/* end of file */ r->current_pos=r->silence_bytes; lseek(r->fd,WAVE_HEADER_OFFSET,SEEK_SET); r->state=PLAY_SILENCE; ms_filter_notify_event(MS_FILTER(r),MS_RING_PLAYER_END_OF_RING_EVENT,NULL); } if (r->need_swap) swap_buffer(&p[processed],err); processed+=err; break; case PLAY_SILENCE: err=gran-processed; if (r->current_pos>err){ memset(&p[processed],0,err); r->current_pos-=gran; processed=gran; }else{ memset(&p[processed],0,r->current_pos); processed+=r->current_pos; r->state=PLAY_RING; } break; } } } /** * ms_ring_player_destroy: * @obj: A valid MSRingPlayer object. * * Destroy a MSRingPlayer object. * * */ void ms_ring_player_destroy( MSRingPlayer *obj) { if (obj->fd!=0) close(obj->fd); g_free(obj); } void ms_ring_player_setup(MSRingPlayer *r,MSSync *sync) { r->sync=sync; r->gran=(r->rate*r->sync->interval/1000)*r->channel; }