From 4033a67d0e99d422336574fc5c982d349632b117 Mon Sep 17 00:00:00 2001 From: Chia-chi Yeh Date: Thu, 16 Sep 2010 18:36:45 +0800 Subject: RTP: Update native part to reflect the API change. Change-Id: Ic2858920ad77d7312f2429f89ca509a481363431 --- voip/jni/rtp/AudioCodec.cpp | 18 +++++++------ voip/jni/rtp/AudioCodec.h | 6 +++-- voip/jni/rtp/AudioGroup.cpp | 63 ++++++++++++++++++++++++--------------------- 3 files changed, 48 insertions(+), 39 deletions(-) (limited to 'voip/jni') diff --git a/voip/jni/rtp/AudioCodec.cpp b/voip/jni/rtp/AudioCodec.cpp index ddd07fc..4d8d36c 100644 --- a/voip/jni/rtp/AudioCodec.cpp +++ b/voip/jni/rtp/AudioCodec.cpp @@ -36,9 +36,9 @@ int8_t gExponents[128] = { class UlawCodec : public AudioCodec { public: - bool set(int sampleRate, int sampleCount) { - mSampleCount = sampleCount; - return sampleCount > 0; + int set(int sampleRate, const char *fmtp) { + mSampleCount = sampleRate / 50; + return mSampleCount; } int encode(void *payload, int16_t *samples); int decode(int16_t *samples, void *payload, int length); @@ -89,9 +89,9 @@ AudioCodec *newUlawCodec() class AlawCodec : public AudioCodec { public: - bool set(int sampleRate, int sampleCount) { - mSampleCount = sampleCount; - return sampleCount > 0; + int set(int sampleRate, const char *fmtp) { + mSampleCount = sampleRate / 50; + return mSampleCount; } int encode(void *payload, int16_t *samples); int decode(int16_t *samples, void *payload, int length); @@ -152,8 +152,10 @@ AudioCodec *newAudioCodec(const char *codecName) { AudioCodecType *type = gAudioCodecTypes; while (type->name != NULL) { - if (strcmp(codecName, type->name) == 0) { - return type->create(); + if (strcasecmp(codecName, type->name) == 0) { + AudioCodec *codec = type->create(); + codec->name = type->name; + return codec; } ++type; } diff --git a/voip/jni/rtp/AudioCodec.h b/voip/jni/rtp/AudioCodec.h index 797494c..e389255 100644 --- a/voip/jni/rtp/AudioCodec.h +++ b/voip/jni/rtp/AudioCodec.h @@ -22,9 +22,11 @@ class AudioCodec { public: + const char *name; + // Needed by destruction through base class pointers. virtual ~AudioCodec() {} - // Returns true if initialization succeeds. - virtual bool set(int sampleRate, int sampleCount) = 0; + // Returns sampleCount or non-positive value if unsupported. + virtual int set(int sampleRate, const char *fmtp) = 0; // Returns the length of payload in bytes. virtual int encode(void *payload, int16_t *samples) = 0; // Returns the number of decoded samples. diff --git a/voip/jni/rtp/AudioGroup.cpp b/voip/jni/rtp/AudioGroup.cpp index 3433dcf..726e98b 100644 --- a/voip/jni/rtp/AudioGroup.cpp +++ b/voip/jni/rtp/AudioGroup.cpp @@ -77,7 +77,7 @@ public: AudioStream(); ~AudioStream(); bool set(int mode, int socket, sockaddr_storage *remote, - const char *codecName, int sampleRate, int sampleCount, + AudioCodec *codec, int sampleRate, int sampleCount, int codecType, int dtmfType); void sendDtmf(int event); @@ -140,7 +140,7 @@ AudioStream::~AudioStream() } bool AudioStream::set(int mode, int socket, sockaddr_storage *remote, - const char *codecName, int sampleRate, int sampleCount, + AudioCodec *codec, int sampleRate, int sampleCount, int codecType, int dtmfType) { if (mode < 0 || mode > LAST_MODE) { @@ -148,14 +148,6 @@ bool AudioStream::set(int mode, int socket, sockaddr_storage *remote, } mMode = mode; - if (codecName) { - mRemote = *remote; - mCodec = newAudioCodec(codecName); - if (!mCodec || !mCodec->set(sampleRate, sampleCount)) { - return false; - } - } - mCodecMagic = (0x8000 | codecType) << 16; mDtmfMagic = (dtmfType == -1) ? 0 : (0x8000 | dtmfType) << 16; @@ -181,11 +173,15 @@ bool AudioStream::set(int mode, int socket, sockaddr_storage *remote, mDtmfEvent = -1; mDtmfStart = 0; - // Only take over the socket when succeeded. + // Only take over these things when succeeded. mSocket = socket; + if (codec) { + mRemote = *remote; + mCodec = codec; + } LOGD("stream[%d] is configured as %s %dkHz %dms", mSocket, - (codecName ? codecName : "RAW"), mSampleRate, mInterval); + (codec ? codec->name : "RAW"), mSampleRate, mInterval); return true; } @@ -831,10 +827,9 @@ static jfieldID gMode; void add(JNIEnv *env, jobject thiz, jint mode, jint socket, jstring jRemoteAddress, jint remotePort, - jstring jCodecName, jint sampleRate, jint sampleCount, - jint codecType, jint dtmfType) + jstring jCodecSpec, jint dtmfType) { - const char *codecName = NULL; + AudioCodec *codec = NULL; AudioStream *stream = NULL; AudioGroup *group = NULL; @@ -842,33 +837,42 @@ void add(JNIEnv *env, jobject thiz, jint mode, sockaddr_storage remote; if (parse(env, jRemoteAddress, remotePort, &remote) < 0) { // Exception already thrown. - goto error; - } - if (sampleRate < 0 || sampleCount < 0 || codecType < 0 || codecType > 127) { - jniThrowException(env, "java/lang/IllegalArgumentException", NULL); - goto error; + return; } - if (!jCodecName) { - jniThrowNullPointerException(env, "codecName"); - goto error; + if (!jCodecSpec) { + jniThrowNullPointerException(env, "codecSpec"); + return; } - codecName = env->GetStringUTFChars(jCodecName, NULL); - if (!codecName) { + const char *codecSpec = env->GetStringUTFChars(jCodecSpec, NULL); + if (!codecSpec) { // Exception already thrown. + return; + } + + // Create audio codec. + int codecType = -1; + char codecName[16]; + int sampleRate = -1; + sscanf(codecSpec, "%d %[^/]%*c%d", &codecType, codecName, &sampleRate); + codec = newAudioCodec(codecName); + int sampleCount = (codec ? codec->set(sampleRate, codecSpec) : -1); + env->ReleaseStringUTFChars(jCodecSpec, codecSpec); + if (sampleCount <= 0) { + jniThrowException(env, "java/lang/IllegalStateException", + "cannot initialize audio codec"); goto error; } // Create audio stream. stream = new AudioStream; - if (!stream->set(mode, socket, &remote, codecName, sampleRate, sampleCount, + if (!stream->set(mode, socket, &remote, codec, sampleRate, sampleCount, codecType, dtmfType)) { jniThrowException(env, "java/lang/IllegalStateException", "cannot initialize audio stream"); - env->ReleaseStringUTFChars(jCodecName, codecName); goto error; } - env->ReleaseStringUTFChars(jCodecName, codecName); socket = -1; + codec = NULL; // Create audio group. group = (AudioGroup *)env->GetIntField(thiz, gNative); @@ -896,6 +900,7 @@ void add(JNIEnv *env, jobject thiz, jint mode, error: delete group; delete stream; + delete codec; close(socket); env->SetIntField(thiz, gNative, NULL); } @@ -930,7 +935,7 @@ void sendDtmf(JNIEnv *env, jobject thiz, jint event) } JNINativeMethod gMethods[] = { - {"add", "(IILjava/lang/String;ILjava/lang/String;IIII)V", (void *)add}, + {"add", "(IILjava/lang/String;ILjava/lang/String;I)V", (void *)add}, {"remove", "(I)V", (void *)remove}, {"setMode", "(I)V", (void *)setMode}, {"sendDtmf", "(I)V", (void *)sendDtmf}, -- cgit v1.1