Loading voip/jni/rtp/AudioCodec.cpp +10 −8 Original line number Diff line number Diff line Loading @@ -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); Loading Loading @@ -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); Loading Loading @@ -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; } Loading voip/jni/rtp/AudioCodec.h +4 −2 Original line number Diff line number Diff line Loading @@ -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. Loading voip/jni/rtp/AudioGroup.cpp +34 −29 Original line number Diff line number Diff line Loading @@ -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); Loading Loading @@ -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) { Loading @@ -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; Loading @@ -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; } Loading Loading @@ -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; Loading @@ -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); Loading Loading @@ -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); } Loading Loading @@ -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}, Loading Loading
voip/jni/rtp/AudioCodec.cpp +10 −8 Original line number Diff line number Diff line Loading @@ -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); Loading Loading @@ -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); Loading Loading @@ -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; } Loading
voip/jni/rtp/AudioCodec.h +4 −2 Original line number Diff line number Diff line Loading @@ -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. Loading
voip/jni/rtp/AudioGroup.cpp +34 −29 Original line number Diff line number Diff line Loading @@ -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); Loading Loading @@ -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) { Loading @@ -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; Loading @@ -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; } Loading Loading @@ -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; Loading @@ -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); Loading Loading @@ -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); } Loading Loading @@ -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}, Loading