Loading voip/jni/rtp/AmrCodec.cpp +12 −8 Original line number Original line Diff line number Diff line Loading @@ -52,7 +52,7 @@ public: int set(int sampleRate, const char *fmtp); int set(int sampleRate, const char *fmtp); int encode(void *payload, int16_t *samples); int encode(void *payload, int16_t *samples); int decode(int16_t *samples, void *payload, int length); int decode(int16_t *samples, int count, void *payload, int length); private: private: void *mEncoder; void *mEncoder; Loading Loading @@ -128,7 +128,7 @@ int AmrCodec::encode(void *payload, int16_t *samples) return length; return length; } } int AmrCodec::decode(int16_t *samples, void *payload, int length) int AmrCodec::decode(int16_t *samples, int count, void *payload, int length) { { unsigned char *bytes = (unsigned char *)payload; unsigned char *bytes = (unsigned char *)payload; Frame_Type_3GPP type; Frame_Type_3GPP type; Loading Loading @@ -213,7 +213,7 @@ public: } } int encode(void *payload, int16_t *samples); int encode(void *payload, int16_t *samples); int decode(int16_t *samples, void *payload, int length); int decode(int16_t *samples, int count, void *payload, int length); private: private: void *mEncoder; void *mEncoder; Loading @@ -239,20 +239,24 @@ int GsmEfrCodec::encode(void *payload, int16_t *samples) return -1; return -1; } } int GsmEfrCodec::decode(int16_t *samples, void *payload, int length) int GsmEfrCodec::decode(int16_t *samples, int count, void *payload, int length) { { unsigned char *bytes = (unsigned char *)payload; unsigned char *bytes = (unsigned char *)payload; if (length == 31 && (bytes[0] >> 4) == 0x0C) { int n = 0; while (n + 160 <= count && length >= 31 && (bytes[0] >> 4) == 0x0C) { for (int i = 0; i < 30; ++i) { for (int i = 0; i < 30; ++i) { bytes[i] = (bytes[i] << 4) | (bytes[i + 1] >> 4); bytes[i] = (bytes[i] << 4) | (bytes[i + 1] >> 4); } } bytes[30] <<= 4; bytes[30] <<= 4; if (AMRDecode(mDecoder, AMR_122, bytes, samples, MIME_IETF) == 31) { if (AMRDecode(mDecoder, AMR_122, bytes, &samples[n], MIME_IETF) != 31) { return 160; break; } } n += 160; length -= 31; bytes += 31; } } return -1; return n; } } } // namespace } // namespace Loading voip/jni/rtp/AudioCodec.h +1 −1 Original line number Original line Diff line number Diff line Loading @@ -30,7 +30,7 @@ public: // Returns the length of payload in bytes. // Returns the length of payload in bytes. virtual int encode(void *payload, int16_t *samples) = 0; virtual int encode(void *payload, int16_t *samples) = 0; // Returns the number of decoded samples. // Returns the number of decoded samples. virtual int decode(int16_t *samples, void *payload, int length) = 0; virtual int decode(int16_t *samples, int count, void *payload, int length) = 0; }; }; AudioCodec *newAudioCodec(const char *codecName); AudioCodec *newAudioCodec(const char *codecName); Loading voip/jni/rtp/AudioGroup.cpp +11 −10 Original line number Original line Diff line number Diff line Loading @@ -395,7 +395,8 @@ void AudioStream::decode(int tick) mLatencyTimer = tick; mLatencyTimer = tick; } } if (mBufferTail - mBufferHead > BUFFER_SIZE - mInterval) { int count = (BUFFER_SIZE - (mBufferTail - mBufferHead)) * mSampleRate; if (count < mSampleCount) { // Buffer overflow. Drop the packet. // Buffer overflow. Drop the packet. LOGV("stream[%d] buffer overflow", mSocket); LOGV("stream[%d] buffer overflow", mSocket); recv(mSocket, &c, 1, MSG_DONTWAIT); recv(mSocket, &c, 1, MSG_DONTWAIT); Loading @@ -403,19 +404,18 @@ void AudioStream::decode(int tick) } } // Receive the packet and decode it. // Receive the packet and decode it. int16_t samples[mSampleCount]; int16_t samples[count]; int length = 0; if (!mCodec) { if (!mCodec) { // Special case for device stream. // Special case for device stream. length = recv(mSocket, samples, sizeof(samples), count = recv(mSocket, samples, sizeof(samples), MSG_TRUNC | MSG_DONTWAIT) >> 1; MSG_TRUNC | MSG_DONTWAIT) >> 1; } else { } else { __attribute__((aligned(4))) uint8_t buffer[2048]; __attribute__((aligned(4))) uint8_t buffer[2048]; sockaddr_storage remote; sockaddr_storage remote; socklen_t len = sizeof(remote); socklen_t addrlen = sizeof(remote); length = recvfrom(mSocket, buffer, sizeof(buffer), int length = recvfrom(mSocket, buffer, sizeof(buffer), MSG_TRUNC | MSG_DONTWAIT, (sockaddr *)&remote, &len); MSG_TRUNC | MSG_DONTWAIT, (sockaddr *)&remote, &addrlen); // Do we need to check SSRC, sequence, and timestamp? They are not // Do we need to check SSRC, sequence, and timestamp? They are not // reliable but at least they can be used to identify duplicates? // reliable but at least they can be used to identify duplicates? Loading @@ -433,14 +433,15 @@ void AudioStream::decode(int tick) } } length -= offset; length -= offset; if (length >= 0) { if (length >= 0) { length = mCodec->decode(samples, &buffer[offset], length); length = mCodec->decode(samples, count, &buffer[offset], length); } } if (length > 0 && mFixRemote) { if (length > 0 && mFixRemote) { mRemote = remote; mRemote = remote; mFixRemote = false; mFixRemote = false; } } count = length; } } if (length <= 0) { if (count <= 0) { LOGV("stream[%d] decoder error", mSocket); LOGV("stream[%d] decoder error", mSocket); return; return; } } Loading @@ -462,7 +463,7 @@ void AudioStream::decode(int tick) // Append to the jitter buffer. // Append to the jitter buffer. int tail = mBufferTail * mSampleRate; int tail = mBufferTail * mSampleRate; for (int i = 0; i < mSampleCount; ++i) { for (int i = 0; i < count; ++i) { mBuffer[tail & mBufferMask] = samples[i]; mBuffer[tail & mBufferMask] = samples[i]; ++tail; ++tail; } } Loading voip/jni/rtp/G711Codec.cpp +10 −4 Original line number Original line Diff line number Diff line Loading @@ -39,7 +39,7 @@ public: return mSampleCount; return mSampleCount; } } int encode(void *payload, int16_t *samples); int encode(void *payload, int16_t *samples); int decode(int16_t *samples, void *payload, int length); int decode(int16_t *samples, int count, void *payload, int length); private: private: int mSampleCount; int mSampleCount; }; }; Loading @@ -64,9 +64,12 @@ int UlawCodec::encode(void *payload, int16_t *samples) return mSampleCount; return mSampleCount; } } int UlawCodec::decode(int16_t *samples, void *payload, int length) int UlawCodec::decode(int16_t *samples, int count, void *payload, int length) { { int8_t *ulaws = (int8_t *)payload; int8_t *ulaws = (int8_t *)payload; if (length > count) { length = count; } for (int i = 0; i < length; ++i) { for (int i = 0; i < length; ++i) { int ulaw = ~ulaws[i]; int ulaw = ~ulaws[i]; int exponent = (ulaw >> 4) & 0x07; int exponent = (ulaw >> 4) & 0x07; Loading @@ -87,7 +90,7 @@ public: return mSampleCount; return mSampleCount; } } int encode(void *payload, int16_t *samples); int encode(void *payload, int16_t *samples); int decode(int16_t *samples, void *payload, int length); int decode(int16_t *samples, int count, void *payload, int length); private: private: int mSampleCount; int mSampleCount; }; }; Loading @@ -111,9 +114,12 @@ int AlawCodec::encode(void *payload, int16_t *samples) return mSampleCount; return mSampleCount; } } int AlawCodec::decode(int16_t *samples, void *payload, int length) int AlawCodec::decode(int16_t *samples, int count, void *payload, int length) { { int8_t *alaws = (int8_t *)payload; int8_t *alaws = (int8_t *)payload; if (length > count) { length = count; } for (int i = 0; i < length; ++i) { for (int i = 0; i < length; ++i) { int alaw = alaws[i] ^ 0x55; int alaw = alaws[i] ^ 0x55; int exponent = (alaw >> 4) & 0x07; int exponent = (alaw >> 4) & 0x07; Loading voip/jni/rtp/GsmCodec.cpp +10 −6 Original line number Original line Diff line number Diff line Loading @@ -44,7 +44,7 @@ public: } } int encode(void *payload, int16_t *samples); int encode(void *payload, int16_t *samples); int decode(int16_t *samples, void *payload, int length); int decode(int16_t *samples, int count, void *payload, int length); private: private: gsm mEncode; gsm mEncode; Loading @@ -57,13 +57,17 @@ int GsmCodec::encode(void *payload, int16_t *samples) return 33; return 33; } } int GsmCodec::decode(int16_t *samples, void *payload, int length) int GsmCodec::decode(int16_t *samples, int count, void *payload, int length) { { if (length == 33 && unsigned char *bytes = (unsigned char *)payload; gsm_decode(mDecode, (unsigned char *)payload, samples) == 0) { int n = 0; return 160; while (n + 160 <= count && length >= 33 && gsm_decode(mDecode, bytes, &samples[n]) == 0) { n += 160; length -= 33; bytes += 33; } } return -1; return n; } } } // namespace } // namespace Loading Loading
voip/jni/rtp/AmrCodec.cpp +12 −8 Original line number Original line Diff line number Diff line Loading @@ -52,7 +52,7 @@ public: int set(int sampleRate, const char *fmtp); int set(int sampleRate, const char *fmtp); int encode(void *payload, int16_t *samples); int encode(void *payload, int16_t *samples); int decode(int16_t *samples, void *payload, int length); int decode(int16_t *samples, int count, void *payload, int length); private: private: void *mEncoder; void *mEncoder; Loading Loading @@ -128,7 +128,7 @@ int AmrCodec::encode(void *payload, int16_t *samples) return length; return length; } } int AmrCodec::decode(int16_t *samples, void *payload, int length) int AmrCodec::decode(int16_t *samples, int count, void *payload, int length) { { unsigned char *bytes = (unsigned char *)payload; unsigned char *bytes = (unsigned char *)payload; Frame_Type_3GPP type; Frame_Type_3GPP type; Loading Loading @@ -213,7 +213,7 @@ public: } } int encode(void *payload, int16_t *samples); int encode(void *payload, int16_t *samples); int decode(int16_t *samples, void *payload, int length); int decode(int16_t *samples, int count, void *payload, int length); private: private: void *mEncoder; void *mEncoder; Loading @@ -239,20 +239,24 @@ int GsmEfrCodec::encode(void *payload, int16_t *samples) return -1; return -1; } } int GsmEfrCodec::decode(int16_t *samples, void *payload, int length) int GsmEfrCodec::decode(int16_t *samples, int count, void *payload, int length) { { unsigned char *bytes = (unsigned char *)payload; unsigned char *bytes = (unsigned char *)payload; if (length == 31 && (bytes[0] >> 4) == 0x0C) { int n = 0; while (n + 160 <= count && length >= 31 && (bytes[0] >> 4) == 0x0C) { for (int i = 0; i < 30; ++i) { for (int i = 0; i < 30; ++i) { bytes[i] = (bytes[i] << 4) | (bytes[i + 1] >> 4); bytes[i] = (bytes[i] << 4) | (bytes[i + 1] >> 4); } } bytes[30] <<= 4; bytes[30] <<= 4; if (AMRDecode(mDecoder, AMR_122, bytes, samples, MIME_IETF) == 31) { if (AMRDecode(mDecoder, AMR_122, bytes, &samples[n], MIME_IETF) != 31) { return 160; break; } } n += 160; length -= 31; bytes += 31; } } return -1; return n; } } } // namespace } // namespace Loading
voip/jni/rtp/AudioCodec.h +1 −1 Original line number Original line Diff line number Diff line Loading @@ -30,7 +30,7 @@ public: // Returns the length of payload in bytes. // Returns the length of payload in bytes. virtual int encode(void *payload, int16_t *samples) = 0; virtual int encode(void *payload, int16_t *samples) = 0; // Returns the number of decoded samples. // Returns the number of decoded samples. virtual int decode(int16_t *samples, void *payload, int length) = 0; virtual int decode(int16_t *samples, int count, void *payload, int length) = 0; }; }; AudioCodec *newAudioCodec(const char *codecName); AudioCodec *newAudioCodec(const char *codecName); Loading
voip/jni/rtp/AudioGroup.cpp +11 −10 Original line number Original line Diff line number Diff line Loading @@ -395,7 +395,8 @@ void AudioStream::decode(int tick) mLatencyTimer = tick; mLatencyTimer = tick; } } if (mBufferTail - mBufferHead > BUFFER_SIZE - mInterval) { int count = (BUFFER_SIZE - (mBufferTail - mBufferHead)) * mSampleRate; if (count < mSampleCount) { // Buffer overflow. Drop the packet. // Buffer overflow. Drop the packet. LOGV("stream[%d] buffer overflow", mSocket); LOGV("stream[%d] buffer overflow", mSocket); recv(mSocket, &c, 1, MSG_DONTWAIT); recv(mSocket, &c, 1, MSG_DONTWAIT); Loading @@ -403,19 +404,18 @@ void AudioStream::decode(int tick) } } // Receive the packet and decode it. // Receive the packet and decode it. int16_t samples[mSampleCount]; int16_t samples[count]; int length = 0; if (!mCodec) { if (!mCodec) { // Special case for device stream. // Special case for device stream. length = recv(mSocket, samples, sizeof(samples), count = recv(mSocket, samples, sizeof(samples), MSG_TRUNC | MSG_DONTWAIT) >> 1; MSG_TRUNC | MSG_DONTWAIT) >> 1; } else { } else { __attribute__((aligned(4))) uint8_t buffer[2048]; __attribute__((aligned(4))) uint8_t buffer[2048]; sockaddr_storage remote; sockaddr_storage remote; socklen_t len = sizeof(remote); socklen_t addrlen = sizeof(remote); length = recvfrom(mSocket, buffer, sizeof(buffer), int length = recvfrom(mSocket, buffer, sizeof(buffer), MSG_TRUNC | MSG_DONTWAIT, (sockaddr *)&remote, &len); MSG_TRUNC | MSG_DONTWAIT, (sockaddr *)&remote, &addrlen); // Do we need to check SSRC, sequence, and timestamp? They are not // Do we need to check SSRC, sequence, and timestamp? They are not // reliable but at least they can be used to identify duplicates? // reliable but at least they can be used to identify duplicates? Loading @@ -433,14 +433,15 @@ void AudioStream::decode(int tick) } } length -= offset; length -= offset; if (length >= 0) { if (length >= 0) { length = mCodec->decode(samples, &buffer[offset], length); length = mCodec->decode(samples, count, &buffer[offset], length); } } if (length > 0 && mFixRemote) { if (length > 0 && mFixRemote) { mRemote = remote; mRemote = remote; mFixRemote = false; mFixRemote = false; } } count = length; } } if (length <= 0) { if (count <= 0) { LOGV("stream[%d] decoder error", mSocket); LOGV("stream[%d] decoder error", mSocket); return; return; } } Loading @@ -462,7 +463,7 @@ void AudioStream::decode(int tick) // Append to the jitter buffer. // Append to the jitter buffer. int tail = mBufferTail * mSampleRate; int tail = mBufferTail * mSampleRate; for (int i = 0; i < mSampleCount; ++i) { for (int i = 0; i < count; ++i) { mBuffer[tail & mBufferMask] = samples[i]; mBuffer[tail & mBufferMask] = samples[i]; ++tail; ++tail; } } Loading
voip/jni/rtp/G711Codec.cpp +10 −4 Original line number Original line Diff line number Diff line Loading @@ -39,7 +39,7 @@ public: return mSampleCount; return mSampleCount; } } int encode(void *payload, int16_t *samples); int encode(void *payload, int16_t *samples); int decode(int16_t *samples, void *payload, int length); int decode(int16_t *samples, int count, void *payload, int length); private: private: int mSampleCount; int mSampleCount; }; }; Loading @@ -64,9 +64,12 @@ int UlawCodec::encode(void *payload, int16_t *samples) return mSampleCount; return mSampleCount; } } int UlawCodec::decode(int16_t *samples, void *payload, int length) int UlawCodec::decode(int16_t *samples, int count, void *payload, int length) { { int8_t *ulaws = (int8_t *)payload; int8_t *ulaws = (int8_t *)payload; if (length > count) { length = count; } for (int i = 0; i < length; ++i) { for (int i = 0; i < length; ++i) { int ulaw = ~ulaws[i]; int ulaw = ~ulaws[i]; int exponent = (ulaw >> 4) & 0x07; int exponent = (ulaw >> 4) & 0x07; Loading @@ -87,7 +90,7 @@ public: return mSampleCount; return mSampleCount; } } int encode(void *payload, int16_t *samples); int encode(void *payload, int16_t *samples); int decode(int16_t *samples, void *payload, int length); int decode(int16_t *samples, int count, void *payload, int length); private: private: int mSampleCount; int mSampleCount; }; }; Loading @@ -111,9 +114,12 @@ int AlawCodec::encode(void *payload, int16_t *samples) return mSampleCount; return mSampleCount; } } int AlawCodec::decode(int16_t *samples, void *payload, int length) int AlawCodec::decode(int16_t *samples, int count, void *payload, int length) { { int8_t *alaws = (int8_t *)payload; int8_t *alaws = (int8_t *)payload; if (length > count) { length = count; } for (int i = 0; i < length; ++i) { for (int i = 0; i < length; ++i) { int alaw = alaws[i] ^ 0x55; int alaw = alaws[i] ^ 0x55; int exponent = (alaw >> 4) & 0x07; int exponent = (alaw >> 4) & 0x07; Loading
voip/jni/rtp/GsmCodec.cpp +10 −6 Original line number Original line Diff line number Diff line Loading @@ -44,7 +44,7 @@ public: } } int encode(void *payload, int16_t *samples); int encode(void *payload, int16_t *samples); int decode(int16_t *samples, void *payload, int length); int decode(int16_t *samples, int count, void *payload, int length); private: private: gsm mEncode; gsm mEncode; Loading @@ -57,13 +57,17 @@ int GsmCodec::encode(void *payload, int16_t *samples) return 33; return 33; } } int GsmCodec::decode(int16_t *samples, void *payload, int length) int GsmCodec::decode(int16_t *samples, int count, void *payload, int length) { { if (length == 33 && unsigned char *bytes = (unsigned char *)payload; gsm_decode(mDecode, (unsigned char *)payload, samples) == 0) { int n = 0; return 160; while (n + 160 <= count && length >= 33 && gsm_decode(mDecode, bytes, &samples[n]) == 0) { n += 160; length -= 33; bytes += 33; } } return -1; return n; } } } // namespace } // namespace Loading