From 8bb2ecb06d7e994b4b5c94af831fb02c465ecb49 Mon Sep 17 00:00:00 2001 From: Benny Prijono Date: Wed, 2 May 2007 11:29:37 +0000 Subject: PJSUA-LIB was ported to Symbian and added simple Symbian app. Testing follows git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@1242 74dad513-b988-da41-8d7b-12977e46ad98 --- pjmedia/src/pjmedia/codec.c | 2 +- pjmedia/src/pjmedia/g711.c | 2 +- pjmedia/src/pjmedia/plc_steveu.c | 338 ------------- pjmedia/src/pjmedia/resample_resample.c | 52 +- pjmedia/src/pjmedia/symbian_sound.cpp | 873 ++++++++++++++++++++++++++++++++ 5 files changed, 926 insertions(+), 341 deletions(-) delete mode 100644 pjmedia/src/pjmedia/plc_steveu.c create mode 100644 pjmedia/src/pjmedia/symbian_sound.cpp (limited to 'pjmedia') diff --git a/pjmedia/src/pjmedia/codec.c b/pjmedia/src/pjmedia/codec.c index 593ca6f9..12ff4c02 100644 --- a/pjmedia/src/pjmedia/codec.c +++ b/pjmedia/src/pjmedia/codec.c @@ -322,7 +322,7 @@ pjmedia_codec_mgr_set_codec_priority(pjmedia_codec_mgr *mgr, pj_strnicmp2(codec_id, mgr->codec_desc[i].id, codec_id->slen) == 0) { - mgr->codec_desc[i].prio = prio; + mgr->codec_desc[i].prio = (pjmedia_codec_priority) prio; ++found; } } diff --git a/pjmedia/src/pjmedia/g711.c b/pjmedia/src/pjmedia/g711.c index da9b7586..0a8def01 100644 --- a/pjmedia/src/pjmedia/g711.c +++ b/pjmedia/src/pjmedia/g711.c @@ -372,7 +372,7 @@ static pj_status_t g711_dealloc_codec(pjmedia_codec_factory *factory, pjmedia_codec *codec ) { struct g711_private *priv = codec->codec_data; - int i; + int i = 0; PJ_ASSERT_RETURN(factory==&g711_factory.base, PJ_EINVAL); diff --git a/pjmedia/src/pjmedia/plc_steveu.c b/pjmedia/src/pjmedia/plc_steveu.c deleted file mode 100644 index 3326b748..00000000 --- a/pjmedia/src/pjmedia/plc_steveu.c +++ /dev/null @@ -1,338 +0,0 @@ -/* - * SpanDSP - a series of DSP components for telephony - * - * plc.c - * - * Written by Steve Underwood - * - * Copyright (C) 2004 Steve Underwood - * - * All rights reserved. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - * - * This version may be optionally licenced under the GNU LGPL licence. - * This version is disclaimed to DIGIUM for inclusion in the Asterisk project. - */ - -/*! \file */ - -#include -#include -#include -#include -#include -#include - -#include "plc_steveu.h" - -#if !defined(FALSE) -#define FALSE 0 -#endif -#if !defined(TRUE) -#define TRUE (!FALSE) -#endif - -#ifndef INT16_MAX -#define INT16_MAX (32767) -#endif - -#ifndef INT16_MIN -#define INT16_MIN (-32767-1) -#endif - -//#define PJ_HAS_RINT 1 - - -/* We do a straight line fade to zero volume in 50ms when we are filling in for missing data. */ -#define ATTENUATION_INCREMENT 0.0025 /* Attenuation per sample */ - -#define ms_to_samples(t) (((t)*SAMPLE_RATE)/1000) - - -#if defined(PJ_HAS_RINT) && PJ_HAS_RINT!=0 -#define RINT(d) rint(d) -#else -double RINT(double d) -{ - double f = floor(d); - double c = ceil(d); - - if (c-d > d-f) - return f; - else if (c-d < d-f) - return c; - else if (d >= 0) { - if (f/2==f) - return f; - else - return c; - } else { - if (c/2==c) - return c; - else - return f; - } -} -#endif - - -PJ_INLINE(pj_int16_t) fsaturate(double damp) -{ - if (damp > 32767.0) - return INT16_MAX; - else if (damp < -32768.0) - return INT16_MIN; - else { - return (pj_int16_t) RINT(damp); - } -} - -static void save_history(plc_state_t *s, pj_int16_t *buf, int len) -{ - if (len >= PLC_HISTORY_LEN) - { - /* Just keep the last part of the new data, starting at the beginning of the buffer */ - memcpy(s->history, buf + len - PLC_HISTORY_LEN, sizeof(pj_int16_t)*PLC_HISTORY_LEN); - s->buf_ptr = 0; - return; - } - if (s->buf_ptr + len > PLC_HISTORY_LEN) - { - /* Wraps around - must break into two sections */ - memcpy(s->history + s->buf_ptr, buf, sizeof(pj_int16_t)*(PLC_HISTORY_LEN - s->buf_ptr)); - len -= (PLC_HISTORY_LEN - s->buf_ptr); - memcpy(s->history, buf + (PLC_HISTORY_LEN - s->buf_ptr), sizeof(pj_int16_t)*len); - s->buf_ptr = len; - return; - } - /* Can use just one section */ - memcpy(s->history + s->buf_ptr, buf, sizeof(pj_int16_t)*len); - s->buf_ptr += len; -} -/*- End of function --------------------------------------------------------*/ - -static void normalise_history(plc_state_t *s) -{ - pj_int16_t tmp[PLC_HISTORY_LEN]; - - if (s->buf_ptr == 0) - return; - memcpy(tmp, s->history, sizeof(pj_int16_t)*s->buf_ptr); - memcpy(s->history, s->history + s->buf_ptr, sizeof(pj_int16_t)*(PLC_HISTORY_LEN - s->buf_ptr)); - memcpy(s->history + PLC_HISTORY_LEN - s->buf_ptr, tmp, sizeof(pj_int16_t)*s->buf_ptr); - s->buf_ptr = 0; -} -/*- End of function --------------------------------------------------------*/ - -PJ_INLINE(int) amdf_pitch(int min_pitch, int max_pitch, pj_int16_t amp[], int len) -{ - int i; - int j; - int acc; - int min_acc; - int pitch; - - pitch = min_pitch; - min_acc = INT_MAX; - for (i = max_pitch; i <= min_pitch; i++) - { - acc = 0; - for (j = 0; j < len; j++) - acc += abs(amp[i + j] - amp[j]); - if (acc < min_acc) - { - min_acc = acc; - pitch = i; - } - } - return pitch; -} -/*- End of function --------------------------------------------------------*/ - -int plc_rx(plc_state_t *s, pj_int16_t amp[], int len) -{ - int i; - /*int overlap_len;*/ - int pitch_overlap; - float old_step; - float new_step; - float old_weight; - float new_weight; - float gain; - - if (s->missing_samples) - { - /* Although we have a real signal, we need to smooth it to fit well - with the synthetic signal we used for the previous block */ - - /* The start of the real data is overlapped with the next 1/4 cycle - of the synthetic data. */ - pitch_overlap = s->pitch >> 2; - if (pitch_overlap > len) - pitch_overlap = len; - gain = 1.0 - s->missing_samples*ATTENUATION_INCREMENT; - if (gain < 0.0) - gain = 0.0; - new_step = 1.0/pitch_overlap; - old_step = new_step*gain; - new_weight = new_step; - old_weight = (1.0 - new_step)*gain; - for (i = 0; i < pitch_overlap; i++) - { - amp[i] = fsaturate(old_weight*s->pitchbuf[s->pitch_offset] + new_weight*amp[i]); - if (++s->pitch_offset >= s->pitch) - s->pitch_offset = 0; - new_weight += new_step; - old_weight -= old_step; - if (old_weight < 0.0) - old_weight = 0.0; - } - s->missing_samples = 0; - } - save_history(s, amp, len); - return len; -} -/*- End of function --------------------------------------------------------*/ - -int plc_fillin(plc_state_t *s, pj_int16_t amp[], int len) -{ - /*pj_int16_t tmp[PLC_PITCH_OVERLAP_MAX];*/ - int i; - int pitch_overlap; - float old_step; - float new_step; - float old_weight; - float new_weight; - float gain; - pj_int16_t *orig_amp; - int orig_len; - - orig_amp = amp; - orig_len = len; - if (s->missing_samples == 0) - { - /* As the gap in real speech starts we need to assess the last known pitch, - and prepare the synthetic data we will use for fill-in */ - normalise_history(s); - s->pitch = amdf_pitch(PLC_PITCH_MIN, PLC_PITCH_MAX, s->history + PLC_HISTORY_LEN - CORRELATION_SPAN - PLC_PITCH_MIN, CORRELATION_SPAN); - /* We overlap a 1/4 wavelength */ - pitch_overlap = s->pitch >> 2; - /* Cook up a single cycle of pitch, using a single of the real signal with 1/4 - cycle OLA'ed to make the ends join up nicely */ - /* The first 3/4 of the cycle is a simple copy */ - for (i = 0; i < s->pitch - pitch_overlap; i++) - s->pitchbuf[i] = s->history[PLC_HISTORY_LEN - s->pitch + i]; - /* The last 1/4 of the cycle is overlapped with the end of the previous cycle */ - new_step = 1.0/pitch_overlap; - new_weight = new_step; - for ( ; i < s->pitch; i++) - { - s->pitchbuf[i] = s->history[PLC_HISTORY_LEN - s->pitch + i]*(1.0 - new_weight) + s->history[PLC_HISTORY_LEN - 2*s->pitch + i]*new_weight; - new_weight += new_step; - } - /* We should now be ready to fill in the gap with repeated, decaying cycles - of what is in pitchbuf */ - - /* We need to OLA the first 1/4 wavelength of the synthetic data, to smooth - it into the previous real data. To avoid the need to introduce a delay - in the stream, reverse the last 1/4 wavelength, and OLA with that. */ - gain = 1.0; - new_step = 1.0/pitch_overlap; - old_step = new_step; - new_weight = new_step; - old_weight = 1.0 - new_step; - for (i = 0; i < pitch_overlap; i++) - { - amp[i] = fsaturate(old_weight*s->history[PLC_HISTORY_LEN - 1 - i] + new_weight*s->pitchbuf[i]); - new_weight += new_step; - old_weight -= old_step; - if (old_weight < 0.0) - old_weight = 0.0; - } - s->pitch_offset = i; - } - else - { - gain = 1.0 - s->missing_samples*ATTENUATION_INCREMENT; - i = 0; - } - for ( ; gain > 0.0 && i < len; i++) - { - amp[i] = (pj_int16_t)(s->pitchbuf[s->pitch_offset]*gain); - gain = gain - ATTENUATION_INCREMENT; - if (++s->pitch_offset >= s->pitch) - s->pitch_offset = 0; - } - for ( ; i < len; i++) - amp[i] = 0; - s->missing_samples += orig_len; - save_history(s, amp, len); - return len; -} -/*- End of function --------------------------------------------------------*/ - -plc_state_t *plc_init(plc_state_t *s) -{ - memset(s, 0, sizeof(*s)); - return s; -} -/*- End of function --------------------------------------------------------*/ - - -/* - * PJMEDIA specifics - */ -#include -#include -#include - -#define THIS_FILE "plc_steveu.c" - -struct steveu_plc -{ - plc_state_t state; - unsigned samples_per_frame; -}; - -void* pjmedia_plc_steveu_create(pj_pool_t *pool, unsigned c, unsigned f) -{ - struct steveu_plc *splc; - - PJ_ASSERT_RETURN(c==8000, NULL); - PJ_UNUSED_ARG(c); - - splc = pj_pool_alloc(pool, sizeof(struct steveu_plc)); - plc_init(&splc->state); - splc->samples_per_frame = f; - - return splc; -} - -void pjmedia_plc_steveu_save(void *obj, pj_int16_t *samples) -{ - struct steveu_plc *splc = obj; - plc_rx(&splc->state, samples, splc->samples_per_frame); -} - -void pjmedia_plc_steveu_generate(void *obj, pj_int16_t *samples) -{ - struct steveu_plc *splc = obj; - //PJ_LOG(5,(THIS_FILE, "PLC: generating lost frame")); - plc_fillin(&splc->state, samples, splc->samples_per_frame); -} - -/*- End of file ------------------------------------------------------------*/ - diff --git a/pjmedia/src/pjmedia/resample_resample.c b/pjmedia/src/pjmedia/resample_resample.c index 0dc755d9..9caaa31b 100644 --- a/pjmedia/src/pjmedia/resample_resample.c +++ b/pjmedia/src/pjmedia/resample_resample.c @@ -217,7 +217,57 @@ PJ_DEF(void) pjmedia_resample_destroy(pjmedia_resample *resample) #else /* PJMEDIA_HAS_LIBRESAMPLE */ -int pjmedia_resample_resample_excluded; +/* + * This is the configuration when sample rate conversion is disabled. + */ +struct pjmedia_resample +{ + unsigned samples_per_frame; +}; + +PJ_DEF(pj_status_t) pjmedia_resample_create( pj_pool_t *pool, + pj_bool_t high_quality, + pj_bool_t large_filter, + unsigned channel_count, + unsigned rate_in, + unsigned rate_out, + unsigned samples_per_frame, + pjmedia_resample **p_resample) +{ + pjmedia_resample *resample; + + PJ_ASSERT_RETURN(rate_in == rate_out, PJ_EINVALIDOP); + + PJ_UNUSED_ARG(high_quality); + PJ_UNUSED_ARG(large_filter); + PJ_UNUSED_ARG(channel_count); + PJ_UNUSED_ARG(rate_in); + PJ_UNUSED_ARG(rate_out); + + resample = PJ_POOL_ZALLOC_T(pool, pjmedia_resample); + resample->samples_per_frame = samples_per_frame; + + *p_resample = resample; + + return PJ_SUCCESS; +} + +PJ_DEF(void) pjmedia_resample_run( pjmedia_resample *resample, + const pj_int16_t *input, + pj_int16_t *output ) +{ + pjmedia_copy_samples(output, input, resample->samples_per_frame); +} + +PJ_DEF(unsigned) pjmedia_resample_get_input_size(pjmedia_resample *resample) +{ + return resample->samples_per_frame; +} + +PJ_DEF(void) pjmedia_resample_destroy(pjmedia_resample *resample) +{ + PJ_UNUSED_ARG(resample); +} #endif /* PJMEDIA_HAS_LIBRESAMPLE */ diff --git a/pjmedia/src/pjmedia/symbian_sound.cpp b/pjmedia/src/pjmedia/symbian_sound.cpp new file mode 100644 index 00000000..c2db57b5 --- /dev/null +++ b/pjmedia/src/pjmedia/symbian_sound.cpp @@ -0,0 +1,873 @@ +/* $Id$ */ +/* + * Copyright (C) 2003-2006 Benny Prijono + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include +#include +#include +#include +#include + + +/* + * This file provides sound implementation for Symbian Audio Streaming + * device. Application using this sound abstraction must link with: + * - mediaclientaudiostream.lib, and + * - mediaclientaudioinputstream.lib + */ +#include +#include +#include + + + +////////////////////////////////////////////////////////////////////////////// +// + +#define THIS_FILE "symbian_sound.cpp" +#define BYTES_PER_SAMPLE 2 +#define POOL_NAME "SymbianSound" +#define POOL_SIZE 512 +#define POOL_INC 512 + +static pjmedia_snd_dev_info symbian_snd_dev_info = +{ + "Symbian Sound Device", + 1, + 1, + 8000 +}; + +class CPjAudioInputEngine; +class CPjAudioOutputEngine; + +/* + * PJMEDIA Sound Stream instance + */ +struct pjmedia_snd_stream +{ + // Pool + pj_pool_t *pool; + + // Common settings. + unsigned clock_rate; + unsigned channel_count; + unsigned samples_per_frame; + + // Input stream + CPjAudioInputEngine *inEngine; + + // Output stream + CPjAudioOutputEngine *outEngine; +}; + +static pj_pool_factory *snd_pool_factory; + + +/* + * Convert clock rate to Symbian's TMdaAudioDataSettings capability. + */ +static TInt get_clock_rate_cap(unsigned clock_rate) +{ + switch (clock_rate) { + case 8000: return TMdaAudioDataSettings::ESampleRate8000Hz; + case 11025: return TMdaAudioDataSettings::ESampleRate11025Hz; + case 12000: return TMdaAudioDataSettings::ESampleRate12000Hz; + case 16000: return TMdaAudioDataSettings::ESampleRate16000Hz; + case 22050: return TMdaAudioDataSettings::ESampleRate22050Hz; + case 24000: return TMdaAudioDataSettings::ESampleRate24000Hz; + case 32000: return TMdaAudioDataSettings::ESampleRate32000Hz; + case 44100: return TMdaAudioDataSettings::ESampleRate44100Hz; + case 48000: return TMdaAudioDataSettings::ESampleRate48000Hz; + case 64000: return TMdaAudioDataSettings::ESampleRate64000Hz; + case 96000: return TMdaAudioDataSettings::ESampleRate96000Hz; + default: + return 0; + } +} + + +/* + * Convert number of channels into Symbian's TMdaAudioDataSettings capability. + */ +static TInt get_channel_cap(unsigned channel_count) +{ + switch (channel_count) { + case 1: return TMdaAudioDataSettings::EChannelsMono; + case 2: return TMdaAudioDataSettings::EChannelsStereo; + default: + return 0; + } +} + + +////////////////////////////////////////////////////////////////////////////// +// + +/* + * Implementation: Symbian Input Stream. + */ +class CPjAudioInputEngine : public MMdaAudioInputStreamCallback +{ +public: + enum State + { + STATE_INACTIVE, + STATE_ACTIVE, + }; + + ~CPjAudioInputEngine(); + + static CPjAudioInputEngine *NewL(pjmedia_snd_stream *parent_strm, + pjmedia_snd_rec_cb rec_cb, + void *user_data); + + static CPjAudioInputEngine *NewLC(pjmedia_snd_stream *parent_strm, + pjmedia_snd_rec_cb rec_cb, + void *user_data); + + pj_status_t StartRecord(); + void Stop(); + +public: + State state_; + pjmedia_snd_stream *parentStrm_; + pjmedia_snd_rec_cb recCb_; + void *userData_; + CMdaAudioInputStream *iInputStream_; + HBufC8 *iStreamBuffer_; + TPtr8 iFramePtr_; + TInt lastError_; + pj_uint32_t timeStamp_; + + CPjAudioInputEngine(pjmedia_snd_stream *parent_strm, + pjmedia_snd_rec_cb rec_cb, + void *user_data); + void ConstructL(); + +public: + virtual void MaiscOpenComplete(TInt aError); + virtual void MaiscBufferCopied(TInt aError, const TDesC8 &aBuffer); + virtual void MaiscRecordComplete(TInt aError); + +}; + + +CPjAudioInputEngine::CPjAudioInputEngine(pjmedia_snd_stream *parent_strm, + pjmedia_snd_rec_cb rec_cb, + void *user_data) + : state_(STATE_INACTIVE), parentStrm_(parent_strm), recCb_(rec_cb), + iInputStream_(NULL), iStreamBuffer_(NULL), iFramePtr_(NULL, 0), + userData_(user_data), lastError_(KErrNone), timeStamp_(0) +{ +} + +CPjAudioInputEngine::~CPjAudioInputEngine() +{ + Stop(); + delete iStreamBuffer_; +} + +void CPjAudioInputEngine::ConstructL() +{ + iStreamBuffer_ = HBufC8::NewMaxL(parentStrm_->samples_per_frame * + parentStrm_->channel_count * + BYTES_PER_SAMPLE); +} + +CPjAudioInputEngine *CPjAudioInputEngine::NewLC(pjmedia_snd_stream *parent, + pjmedia_snd_rec_cb rec_cb, + void *user_data) +{ + CPjAudioInputEngine* self = new (ELeave) CPjAudioInputEngine(parent, + rec_cb, + user_data); + CleanupStack::PushL(self); + self->ConstructL(); + return self; +} + +CPjAudioInputEngine *CPjAudioInputEngine::NewL(pjmedia_snd_stream *parent, + pjmedia_snd_rec_cb rec_cb, + void *user_data) +{ + CPjAudioInputEngine *self = NewLC(parent, rec_cb, user_data); + CleanupStack::Pop(self); + return self; +} + + +pj_status_t CPjAudioInputEngine::StartRecord() +{ + + // Ignore command if recording is in progress. + if (state_ == STATE_ACTIVE) + return PJ_SUCCESS; + + // According to Nokia's AudioStream example, some 2nd Edition, FP2 devices + // (such as Nokia 6630) require the stream to be reconstructed each time + // before calling Open() - otherwise the callback never gets called. + // For uniform behavior, lets just delete/re-create the stream for all + // devices. + + // Destroy existing stream. + if (iInputStream_) delete iInputStream_; + iInputStream_ = NULL; + + // Create the stream. + TRAPD(err, iInputStream_ = CMdaAudioInputStream::NewL(*this)); + if (err != KErrNone) + return PJ_RETURN_OS_ERROR(err); + + // Initialize settings. + TMdaAudioDataSettings iStreamSettings; + iStreamSettings.iChannels = get_channel_cap(parentStrm_->channel_count); + iStreamSettings.iSampleRate = get_clock_rate_cap(parentStrm_->clock_rate); + + pj_assert(iStreamSettings.iChannels != 0 && + iStreamSettings.iSampleRate != 0); + + // Create timeout timer to wait for Open to complete + RTimer timer; + TRequestStatus reqStatus; + TInt rc; + + rc = timer.CreateLocal(); + if (rc != KErrNone) { + delete iInputStream_; + iInputStream_ = NULL; + return PJ_RETURN_OS_ERROR(rc); + } + + PJ_LOG(4,(THIS_FILE, "Opening sound device for capture, " + "clock rate=%d, channel count=%d..", + parentStrm_->clock_rate, + parentStrm_->channel_count)); + + // Open stream. + lastError_ = KRequestPending; + iInputStream_->Open(&iStreamSettings); + + // Wait until callback is called. + if (lastError_ == KRequestPending) { + timer.After(reqStatus, 5 * 1000 * 1000); + + do { + User::WaitForAnyRequest(); + } while (lastError_==KRequestPending && reqStatus==KRequestPending); + + if (reqStatus==KRequestPending) + timer.Cancel(); + } + + // Close timer + timer.Close(); + + // Handle timeout + if (lastError_ == KRequestPending) { + iInputStream_->Stop(); + delete iInputStream_; + iInputStream_ = NULL; + return PJ_ETIMEDOUT; + } + else if (lastError_ != KErrNone) { + // Handle failure. + delete iInputStream_; + iInputStream_ = NULL; + return PJ_RETURN_OS_ERROR(lastError_); + } + + // Feed the first frame. + iFramePtr_ = iStreamBuffer_->Des(); + iInputStream_->ReadL(iFramePtr_); + + // Success + PJ_LOG(4,(THIS_FILE, "Sound capture started.")); + return PJ_SUCCESS; +} + + +void CPjAudioInputEngine::Stop() +{ + // If capture is in progress, stop it. + if (iInputStream_ && state_ == STATE_ACTIVE) { + lastError_ = KRequestPending; + iInputStream_->Stop(); + + // Wait until it's actually stopped + while (lastError_ == KRequestPending) + pj_thread_sleep(100); + } + + if (iInputStream_) { + delete iInputStream_; + iInputStream_ = NULL; + } + + state_ = STATE_INACTIVE; +} + + +void CPjAudioInputEngine::MaiscOpenComplete(TInt aError) +{ + lastError_ = aError; +} + +void CPjAudioInputEngine::MaiscBufferCopied(TInt aError, + const TDesC8 &aBuffer) +{ + lastError_ = aError; + if (aError != KErrNone) + return; + + // Call the callback. + recCb_(userData_, timeStamp_, (void*)aBuffer.Ptr(), aBuffer.Size()); + + // Increment timestamp. + timeStamp_ += (aBuffer.Size() * BYTES_PER_SAMPLE); + + // Record next frame + iFramePtr_ = iStreamBuffer_->Des(); + iInputStream_->ReadL(iFramePtr_); +} + + +void CPjAudioInputEngine::MaiscRecordComplete(TInt aError) +{ + lastError_ = aError; + state_ = STATE_INACTIVE; +} + + + +////////////////////////////////////////////////////////////////////////////// +// + +/* + * Implementation: Symbian Output Stream. + */ + +class CPjAudioOutputEngine : public MMdaAudioOutputStreamCallback +{ +public: + enum State + { + STATE_INACTIVE, + STATE_ACTIVE, + }; + + ~CPjAudioOutputEngine(); + + static CPjAudioOutputEngine *NewL(pjmedia_snd_stream *parent_strm, + pjmedia_snd_play_cb play_cb, + void *user_data); + + static CPjAudioOutputEngine *NewLC(pjmedia_snd_stream *parent_strm, + pjmedia_snd_play_cb rec_cb, + void *user_data); + + pj_status_t StartPlay(); + void Stop(); + +public: + State state_; + pjmedia_snd_stream *parentStrm_; + pjmedia_snd_play_cb playCb_; + void *userData_; + CMdaAudioOutputStream *iOutputStream_; + TUint8 *frameBuf_; + unsigned frameBufSize_; + TInt lastError_; + unsigned timestamp_; + + CPjAudioOutputEngine(pjmedia_snd_stream *parent_strm, + pjmedia_snd_play_cb play_cb, + void *user_data); + void ConstructL(); + + virtual void MaoscOpenComplete(TInt aError); + virtual void MaoscBufferCopied(TInt aError, const TDesC8& aBuffer); + virtual void MaoscPlayComplete(TInt aError); +}; + + +CPjAudioOutputEngine::CPjAudioOutputEngine(pjmedia_snd_stream *parent_strm, + pjmedia_snd_play_cb play_cb, + void *user_data) +: state_(STATE_INACTIVE), parentStrm_(parent_strm), playCb_(play_cb), + userData_(user_data), iOutputStream_(NULL), frameBuf_(NULL), + lastError_(KErrNone), timestamp_(0) +{ +} + + +void CPjAudioOutputEngine::ConstructL() +{ + frameBufSize_ = parentStrm_->samples_per_frame * + parentStrm_->channel_count * + BYTES_PER_SAMPLE; + frameBuf_ = new TUint8[frameBufSize_]; +} + +CPjAudioOutputEngine::~CPjAudioOutputEngine() +{ + Stop(); + delete [] frameBuf_; +} + +CPjAudioOutputEngine * +CPjAudioOutputEngine::NewLC(pjmedia_snd_stream *parent_strm, + pjmedia_snd_play_cb rec_cb, + void *user_data) +{ + CPjAudioOutputEngine* self = new (ELeave) CPjAudioOutputEngine(parent_strm, + rec_cb, + user_data); + CleanupStack::PushL(self); + self->ConstructL(); + return self; +} + +CPjAudioOutputEngine * +CPjAudioOutputEngine::NewL(pjmedia_snd_stream *parent_strm, + pjmedia_snd_play_cb play_cb, + void *user_data) +{ + CPjAudioOutputEngine *self = NewLC(parent_strm, play_cb, user_data); + CleanupStack::Pop(self); + return self; +} + +pj_status_t CPjAudioOutputEngine::StartPlay() +{ + // Ignore command if playing is in progress. + if (state_ == STATE_ACTIVE) + return PJ_SUCCESS; + + // Destroy existing stream. + if (iOutputStream_) delete iOutputStream_; + iOutputStream_ = NULL; + + // Create the stream + TRAPD(err, iOutputStream_ = CMdaAudioOutputStream::NewL(*this)); + if (err != KErrNone) + return PJ_RETURN_OS_ERROR(err); + + // Initialize settings. + TMdaAudioDataSettings iStreamSettings; + iStreamSettings.iChannels = get_channel_cap(parentStrm_->channel_count); + iStreamSettings.iSampleRate = get_clock_rate_cap(parentStrm_->clock_rate); + + pj_assert(iStreamSettings.iChannels != 0 && + iStreamSettings.iSampleRate != 0); + + PJ_LOG(4,(THIS_FILE, "Opening sound device for playback, " + "clock rate=%d, channel count=%d..", + parentStrm_->clock_rate, + parentStrm_->channel_count)); + + // Open stream. + lastError_ = KRequestPending; + iOutputStream_->Open(&iStreamSettings); + + // Wait until callback is called. + while (lastError_ == KRequestPending) + pj_thread_sleep(100); + + // Handle failure. + if (lastError_ != KErrNone) { + delete iOutputStream_; + iOutputStream_ = NULL; + return PJ_RETURN_OS_ERROR(lastError_); + } + + // Success + PJ_LOG(4,(THIS_FILE, "Sound playback started")); + return PJ_SUCCESS; + +} + +void CPjAudioOutputEngine::Stop() +{ + // Stop stream if it's playing + if (iOutputStream_ && state_ != STATE_INACTIVE) { + lastError_ = KRequestPending; + iOutputStream_->Stop(); + + // Wait until it's actually stopped + while (lastError_ == KRequestPending) + pj_thread_sleep(100); + } + + if (iOutputStream_) { + delete iOutputStream_; + iOutputStream_ = NULL; + } + + state_ = STATE_INACTIVE; +} + +void CPjAudioOutputEngine::MaoscOpenComplete(TInt aError) +{ + lastError_ = aError; + + if (aError==KErrNone) { + // output stream opened succesfully, set status to Active + state_ = STATE_ACTIVE; + + // set stream properties, 16bit 8KHz mono + TMdaAudioDataSettings iSettings; + iSettings.iChannels = get_channel_cap(parentStrm_->channel_count); + iSettings.iSampleRate = get_clock_rate_cap(parentStrm_->clock_rate); + + iOutputStream_->SetAudioPropertiesL(iSettings.iSampleRate, + iSettings.iChannels); + + // set volume to 1/4th of stream max volume + iOutputStream_->SetVolume(iOutputStream_->MaxVolume()/4); + + // set stream priority to normal and time sensitive + iOutputStream_->SetPriority(EPriorityNormal, + EMdaPriorityPreferenceTime); + + // Call callback to retrieve frame from upstream. + pj_status_t status; + status = playCb_(this->userData_, timestamp_, frameBuf_, + frameBufSize_); + if (status != PJ_SUCCESS) { + this->Stop(); + return; + } + + // Increment timestamp. + timestamp_ += (frameBufSize_ / BYTES_PER_SAMPLE); + + // issue WriteL() to write the first audio data block, + // subsequent calls to WriteL() will be issued in + // MMdaAudioOutputStreamCallback::MaoscBufferCopied() + // until whole data buffer is written. + TPtrC8 frame(frameBuf_, frameBufSize_); + iOutputStream_->WriteL(frame); + } +} + +void CPjAudioOutputEngine::MaoscBufferCopied(TInt aError, + const TDesC8& aBuffer) +{ + PJ_UNUSED_ARG(aBuffer); + + if (aError==KErrNone) { + // Buffer successfully written, feed another one. + + // Call callback to retrieve frame from upstream. + pj_status_t status; + status = playCb_(this->userData_, timestamp_, frameBuf_, + frameBufSize_); + if (status != PJ_SUCCESS) { + this->Stop(); + return; + } + + // Increment timestamp. + timestamp_ += (frameBufSize_ / BYTES_PER_SAMPLE); + + // Write to playback stream. + TPtrC8 frame(frameBuf_, frameBufSize_); + iOutputStream_->WriteL(frame); + + } else if (aError==KErrAbort) { + // playing was aborted, due to call to CMdaAudioOutputStream::Stop() + state_ = STATE_INACTIVE; + } else { + // error writing data to output + lastError_ = aError; + state_ = STATE_INACTIVE; + } +} + +void CPjAudioOutputEngine::MaoscPlayComplete(TInt aError) +{ + lastError_ = aError; + state_ = STATE_INACTIVE; +} + + +////////////////////////////////////////////////////////////////////////////// +// + + +/* + * Initialize sound subsystem. + */ +PJ_DEF(pj_status_t) pjmedia_snd_init(pj_pool_factory *factory) +{ + snd_pool_factory = factory; + return PJ_SUCCESS; +} + +/* + * Get device count. + */ +PJ_DEF(int) pjmedia_snd_get_dev_count(void) +{ + /* Always return 1 */ + return 1; +} + +/* + * Get device info. + */ +PJ_DEF(const pjmedia_snd_dev_info*) pjmedia_snd_get_dev_info(unsigned index) +{ + /* Always return the default sound device */ + PJ_ASSERT_RETURN(index==0, NULL); + return &symbian_snd_dev_info; +} + + + +/* + * Open sound recorder stream. + */ +PJ_DEF(pj_status_t) pjmedia_snd_open_rec( int index, + unsigned clock_rate, + unsigned channel_count, + unsigned samples_per_frame, + unsigned bits_per_sample, + pjmedia_snd_rec_cb rec_cb, + void *user_data, + pjmedia_snd_stream **p_snd_strm) +{ + pj_pool_t *pool; + pjmedia_snd_stream *strm; + + PJ_ASSERT_RETURN(index == 0, PJ_EINVAL); + PJ_ASSERT_RETURN(clock_rate && channel_count && samples_per_frame && + bits_per_sample && rec_cb && p_snd_strm, PJ_EINVAL); + + pool = pj_pool_create(snd_pool_factory, POOL_NAME, POOL_SIZE, POOL_INC, + NULL); + if (!pool) + return PJ_ENOMEM; + + strm = (pjmedia_snd_stream*) pj_pool_zalloc(pool, + sizeof(pjmedia_snd_stream)); + strm->pool = pool; + strm->clock_rate = clock_rate; + strm->channel_count = channel_count; + strm->samples_per_frame = samples_per_frame; + + TMdaAudioDataSettings settings; + TInt clockRateCap, channelCountCap; + + clockRateCap = get_clock_rate_cap(clock_rate); + channelCountCap = get_channel_cap(channel_count); + + PJ_ASSERT_RETURN(bits_per_sample == 16, PJ_EINVAL); + PJ_ASSERT_RETURN(clockRateCap != 0, PJ_EINVAL); + PJ_ASSERT_RETURN(channelCountCap != 0, PJ_EINVAL); + + // Create the input stream. + TRAPD(err, strm->inEngine = CPjAudioInputEngine::NewL(strm, rec_cb, + user_data)); + if (err != KErrNone) { + pj_pool_release(pool); + return PJ_RETURN_OS_ERROR(err); + } + + + // Done. + *p_snd_strm = strm; + return PJ_SUCCESS; +} + +PJ_DEF(pj_status_t) pjmedia_snd_open_player( int index, + unsigned clock_rate, + unsigned channel_count, + unsigned samples_per_frame, + unsigned bits_per_sample, + pjmedia_snd_play_cb play_cb, + void *user_data, + pjmedia_snd_stream **p_snd_strm ) +{ + pj_pool_t *pool; + pjmedia_snd_stream *strm; + + PJ_ASSERT_RETURN(index == 0, PJ_EINVAL); + PJ_ASSERT_RETURN(clock_rate && channel_count && samples_per_frame && + bits_per_sample && play_cb && p_snd_strm, PJ_EINVAL); + + pool = pj_pool_create(snd_pool_factory, POOL_NAME, POOL_SIZE, POOL_INC, + NULL); + if (!pool) + return PJ_ENOMEM; + + strm = (pjmedia_snd_stream*) pj_pool_zalloc(pool, + sizeof(pjmedia_snd_stream)); + strm->pool = pool; + strm->clock_rate = clock_rate; + strm->channel_count = channel_count; + strm->samples_per_frame = samples_per_frame; + + TMdaAudioDataSettings settings; + TInt clockRateCap, channelCountCap; + + clockRateCap = get_clock_rate_cap(clock_rate); + channelCountCap = get_channel_cap(channel_count); + + PJ_ASSERT_RETURN(bits_per_sample == 16, PJ_EINVAL); + PJ_ASSERT_RETURN(clockRateCap != 0, PJ_EINVAL); + PJ_ASSERT_RETURN(channelCountCap != 0, PJ_EINVAL); + + // Create the output stream. + TRAPD(err, strm->outEngine = CPjAudioOutputEngine::NewL(strm, play_cb, + user_data)); + if (err != KErrNone) { + pj_pool_release(pool); + return PJ_RETURN_OS_ERROR(err); + } + + // Done. + *p_snd_strm = strm; + return PJ_SUCCESS; +} + +PJ_DEF(pj_status_t) pjmedia_snd_open( int rec_id, + int play_id, + unsigned clock_rate, + unsigned channel_count, + unsigned samples_per_frame, + unsigned bits_per_sample, + pjmedia_snd_rec_cb rec_cb, + pjmedia_snd_play_cb play_cb, + void *user_data, + pjmedia_snd_stream **p_snd_strm) +{ + pj_pool_t *pool; + pjmedia_snd_stream *strm; + + PJ_ASSERT_RETURN(rec_id == 0 && play_id == 0, PJ_EINVAL); + PJ_ASSERT_RETURN(clock_rate && channel_count && samples_per_frame && + bits_per_sample && rec_cb && play_cb && p_snd_strm, + PJ_EINVAL); + + pool = pj_pool_create(snd_pool_factory, POOL_NAME, POOL_SIZE, POOL_INC, + NULL); + if (!pool) + return PJ_ENOMEM; + + strm = (pjmedia_snd_stream*) pj_pool_zalloc(pool, + sizeof(pjmedia_snd_stream)); + strm->pool = pool; + strm->clock_rate = clock_rate; + strm->channel_count = channel_count; + strm->samples_per_frame = samples_per_frame; + + TMdaAudioDataSettings settings; + TInt clockRateCap, channelCountCap; + + clockRateCap = get_clock_rate_cap(clock_rate); + channelCountCap = get_channel_cap(channel_count); + + PJ_ASSERT_RETURN(bits_per_sample == 16, PJ_EINVAL); + PJ_ASSERT_RETURN(clockRateCap != 0, PJ_EINVAL); + PJ_ASSERT_RETURN(channelCountCap != 0, PJ_EINVAL); + + // Create the output stream. + TRAPD(err, strm->outEngine = CPjAudioOutputEngine::NewL(strm, play_cb, + user_data)); + if (err != KErrNone) { + pj_pool_release(pool); + return PJ_RETURN_OS_ERROR(err); + } + + // Done. + *p_snd_strm = strm; + return PJ_SUCCESS; +} + + +PJ_DEF(pj_status_t) pjmedia_snd_stream_start(pjmedia_snd_stream *stream) +{ + pj_status_t status; + + PJ_ASSERT_RETURN(stream != NULL, PJ_EINVAL); + + if (stream->inEngine) { + status = stream->inEngine->StartRecord(); + if (status != PJ_SUCCESS) + return status; + } + + if (stream->outEngine) { + status = stream->outEngine->StartPlay(); + if (status != PJ_SUCCESS) + return status; + } + + return PJ_SUCCESS; +} + + +PJ_DEF(pj_status_t) pjmedia_snd_stream_stop(pjmedia_snd_stream *stream) +{ + PJ_ASSERT_RETURN(stream != NULL, PJ_EINVAL); + + if (stream->inEngine) { + stream->inEngine->Stop(); + } + + if (stream->outEngine) { + stream->outEngine->Stop(); + } + + return PJ_SUCCESS; +} + + +PJ_DEF(pj_status_t) pjmedia_snd_stream_close(pjmedia_snd_stream *stream) +{ + pj_pool_t *pool; + + PJ_ASSERT_RETURN(stream != NULL, PJ_EINVAL); + + if (stream->inEngine) { + delete stream->inEngine; + stream->inEngine = NULL; + } + + if (stream->outEngine) { + delete stream->outEngine; + stream->outEngine = NULL; + } + + pool = stream->pool; + if (pool) { + stream->pool = NULL; + pj_pool_release(pool); + } + + return PJ_SUCCESS; +} + + +PJ_DEF(pj_status_t) pjmedia_snd_deinit(void) +{ + /* Nothing to do */ + return PJ_SUCCESS; +} + -- cgit v1.2.3