view src/mediastreamer/msringplayer.c @ 12116:e75ef7aa913e

[gaim-migrate @ 14416] " This patch implements a replacement for the queuing system from 1.x. It also obsoletes a previous patch [#1338873] I submitted to prioritize the unseen states in gtk conversations. The attached envelope.png is ripped from the msgunread.png already included in gaim. It should be dropped in the pixmaps directory (Makefile.am is updated accordingly in this patch). The two separate queuing preferences from 1.x, queuing messages while away and queuing all new messages (from docklet), are replaced with a single 3-way preference for conversations. The new preference is "Hide new IM conversations". This preference can be set to never, away and always. When a gtk conversation is created, it may be placed in a hidden conversation window instead of being placed normally. This decision is based upon the preference and possibly the away state of the account the conversation is being created for. This *will* effect conversations the user explicitly requests to be created, so in these cases the caller must be sure to present the conversation to the user, using gaim_gtkconv_present_conversation(). This is done already in gtkdialogs.c which handles creating conversations requested by the user from gaim proper (menus, double-clicking on budy in blist, etc.). The main advantage to not queuing messages is that the conversations exist, the message is written to the conversation (and logged if appropriate) and the unseen state is set on the conversation. This means no additional features are needed to track whether there are queued messages or not, just use the unseen state on conversations. Since conversations may not be visible (messages "queued"), gaim proper needs some notification that there are messages waiting. I opted for a menutray icon that shows up when an im conversation has an unseen message. Clicking this icon will focus (and show if hidden) the first conversation with an unseen message. This is essentially the same behavior of the docklet in cvs right now, except that the icon is only visible when there is a conversation with an unread message. The api that is added is flexible enough to allow either the docklet or the new blist menutray icon to be visible for conversations of any/all types and for unseen messages >= any state. Currently they are set to only IM conversations and only unseen states >= TEXT (system messages and no log messages will not trigger blinking the docklet or showing the blist tray icon), but these could be made preferences relatively easily in the future. Other plugins could probably benefit as well: gaim_gtk_conversations_get_first_unseen(). There is probably some limit to comment size, so I'll stop rambling now. If anyone has more questions/comments, catch me in #gaim, here or on gaim-devel." committer: Tailor Script <tailor@pidgin.im>
author Luke Schierer <lschiere@pidgin.im>
date Wed, 16 Nov 2005 18:17:01 +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;
}