From 8db42a9187da583c957d433d086288621794e0c2 Mon Sep 17 00:00:00 2001 From: attermann Date: Tue, 31 Aug 2021 10:35:59 -0600 Subject: [PATCH] Added support for ulaw and alaw encoded wav files. --- src/audiofilters/msfileplayer.c | 54 ++++++++++++++++++++++++++++++--- src/voip/msmediaplayer.c | 2 +- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/audiofilters/msfileplayer.c b/src/audiofilters/msfileplayer.c index 65240ad12b..983129fe12 100644 --- a/src/audiofilters/msfileplayer.c +++ b/src/audiofilters/msfileplayer.c @@ -25,6 +25,7 @@ #include "waveheader.h" #include "mediastreamer2/msticker.h" #include "asyncrw.h" +#include "g711.h" #ifdef HAVE_PCAP #include @@ -39,6 +40,7 @@ struct _PlayerData{ int fd; MSAsyncReader *reader; MSPlayerState state; + int type; int rate; int nchannels; int hsize; @@ -71,6 +73,7 @@ static void player_init(MSFilter *f){ d->fd=-1; d->state=MSPlayerClosed; d->swap=FALSE; + d->type=WAVE_FORMAT_PCM; d->rate=8000; d->nchannels=1; d->samplesize=2; @@ -157,6 +160,7 @@ static int read_wav_header(PlayerData *d){ if (ret==-1) goto not_a_wav; + d->type=le_uint16(format_chunk->type); d->rate=le_uint32(format_chunk->rate); d->nchannels=le_uint16(format_chunk->channel); if (d->nchannels==0) goto not_a_wav; @@ -222,7 +226,7 @@ static int player_open(MSFilter *f, void *arg){ } d->current_pos_bytes = 0; ms_filter_notify_no_arg(f,MS_FILTER_OUTPUT_FMT_CHANGED); - ms_message("MSFilePlayer[%p]: %s opened: rate=%i,channel=%i, length=%i ms",f,file,d->rate,d->nchannels, d->duration); + ms_message("MSFilePlayer[%p]: %s opened: type=%i,rate=%i,channel=%i, length=%i ms",f,file,d->type,d->rate,d->nchannels, d->duration); return 0; } @@ -363,8 +367,30 @@ static void player_process(MSFilter *f){ mblk_set_cseq(om, pcap_seq); mblk_set_timestamp_info(om, f->ticker->time); mblk_set_marker_info(om,markbit); - ms_queue_put(f->outputs[0], om); - ms_message("Outputting RTP packet of size %i, seq=%u markbit=%i", bytes, pcap_seq, (int)markbit); + if (d->type == WAVE_FORMAT_MULAW) { + int dbytes = 2/d->samplesize*bytes; + mblk_t *dm=allocb(dbytes,0); + for(;om->b_rptrb_wptr;om->b_rptr++,dm->b_wptr+=2){ + *((int16_t*)(dm->b_wptr))=Snack_Mulaw2Lin(*om->b_rptr); + } + freemsg(om); + ms_queue_put(f->outputs[0],dm); + ms_message("Outputting RTP packet of size %i, seq=%u markbit=%i", dbytes, pcap_seq, (int)markbit); + } + else if (d->type == WAVE_FORMAT_ALAW) { + int dbytes = 2/d->samplesize*bytes; + mblk_t *dm=allocb(dbytes,0); + for(;om->b_rptrb_wptr;om->b_rptr++,dm->b_wptr+=2){ + *((int16_t*)(dm->b_wptr))=Snack_Alaw2Lin(*om->b_rptr); + } + freemsg(om); + ms_queue_put(f->outputs[0],dm); + ms_message("Outputting RTP packet of size %i, seq=%u markbit=%i", dbytes, pcap_seq, (int)markbit); + } + else { + ms_queue_put(f->outputs[0],om); + ms_message("Outputting RTP packet of size %i, seq=%u markbit=%i", bytes, pcap_seq, (int)markbit); + } } d->pcap_seq = pcap_seq; d->pcap_hdr = NULL; @@ -400,7 +426,27 @@ static void player_process(MSFilter *f){ mblk_set_timestamp_info(om,d->ts); d->ts+=nsamples; d->current_pos_bytes += bytes; - ms_queue_put(f->outputs[0],om); + if (d->type == WAVE_FORMAT_MULAW) { + int dbytes = 2/d->samplesize*bytes; + mblk_t *dm=allocb(dbytes,0); + for(;om->b_rptrb_wptr;om->b_rptr++,dm->b_wptr+=2){ + *((int16_t*)(dm->b_wptr))=Snack_Mulaw2Lin(*om->b_rptr); + } + freemsg(om); + ms_queue_put(f->outputs[0],dm); + } + else if (d->type == WAVE_FORMAT_ALAW) { + int dbytes = 2/d->samplesize*bytes; + mblk_t *dm=allocb(dbytes,0); + for(;om->b_rptrb_wptr;om->b_rptr++,dm->b_wptr+=2){ + *((int16_t*)(dm->b_wptr))=Snack_Alaw2Lin(*om->b_rptr); + } + freemsg(om); + ms_queue_put(f->outputs[0],dm); + } + else { + ms_queue_put(f->outputs[0],om); + } }else freemsg(om); if (errreader, d->hsize); diff --git a/src/voip/msmediaplayer.c b/src/voip/msmediaplayer.c index d7ae74eaf5..7bce284983 100644 --- a/src/voip/msmediaplayer.c +++ b/src/voip/msmediaplayer.c @@ -193,7 +193,7 @@ bool_t ms_media_player_open(MSMediaPlayer *obj, const char *filepath) { return FALSE; } close(fd); - if(wave_header_get_format_type(&header) != WAVE_FORMAT_PCM) { + if(wave_header_get_format_type(&header) != WAVE_FORMAT_PCM && wave_header_get_format_type(&header) != WAVE_FORMAT_MULAW && wave_header_get_format_type(&header) != WAVE_FORMAT_ALAW) { ms_error("Cannot open %s. Codec not supported", filepath); return FALSE; }