Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdint.h> |
| 19 | #include <string.h> |
| 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <sys/epoll.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/socket.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/time.h> |
| 27 | #include <time.h> |
| 28 | #include <arpa/inet.h> |
| 29 | #include <netinet/in.h> |
| 30 | |
| 31 | #define LOG_TAG "AudioGroup" |
| 32 | #include <cutils/atomic.h> |
| 33 | #include <utils/Log.h> |
| 34 | #include <utils/Errors.h> |
| 35 | #include <utils/RefBase.h> |
| 36 | #include <utils/threads.h> |
| 37 | #include <utils/SystemClock.h> |
| 38 | #include <media/AudioSystem.h> |
| 39 | #include <media/AudioRecord.h> |
| 40 | #include <media/AudioTrack.h> |
| 41 | #include <media/mediarecorder.h> |
| 42 | |
| 43 | #include "jni.h" |
| 44 | #include "JNIHelp.h" |
| 45 | |
| 46 | #include "AudioCodec.h" |
Chia-chi Yeh | a8a1009 | 2010-10-05 01:17:13 +0800 | [diff] [blame] | 47 | #include "EchoSuppressor.h" |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 48 | |
| 49 | extern int parse(JNIEnv *env, jstring jAddress, int port, sockaddr_storage *ss); |
| 50 | |
| 51 | namespace { |
| 52 | |
| 53 | using namespace android; |
| 54 | |
| 55 | int gRandom = -1; |
| 56 | |
| 57 | // We use a circular array to implement jitter buffer. The simplest way is doing |
| 58 | // a modulo operation on the index while accessing the array. However modulo can |
| 59 | // be expensive on some platforms, such as ARM. Thus we round up the size of the |
| 60 | // array to the nearest power of 2 and then use bitwise-and instead of modulo. |
Chia-chi Yeh | 3520bd4 | 2010-09-30 13:48:07 +0800 | [diff] [blame] | 61 | // Currently we make it 512ms long and assume packet interval is 40ms or less. |
| 62 | // The first 80ms is the place where samples get mixed. The rest 432ms is the |
| 63 | // real jitter buffer. For a stream at 8000Hz it takes 8192 bytes. These numbers |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 64 | // are chosen by experiments and each of them can be adjusted as needed. |
| 65 | |
Chia-chi Yeh | 3cf7137 | 2011-01-04 19:10:06 +0800 | [diff] [blame] | 66 | // Originally a stream does not send packets when it is receive-only or there is |
| 67 | // nothing to mix. However, this causes some problems with certain firewalls and |
| 68 | // proxies. A firewall might remove a port mapping when there is no outgoing |
| 69 | // packet for a preiod of time, and a proxy might wait for incoming packets from |
| 70 | // both sides before start forwarding. To solve these problems, we send out a |
| 71 | // silence packet on the stream for every second. It should be good enough to |
| 72 | // keep the stream alive with relatively low resources. |
| 73 | |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 74 | // Other notes: |
| 75 | // + We use elapsedRealtime() to get the time. Since we use 32bit variables |
| 76 | // instead of 64bit ones, comparison must be done by subtraction. |
| 77 | // + Sampling rate must be multiple of 1000Hz, and packet length must be in |
| 78 | // milliseconds. No floating points. |
| 79 | // + If we cannot get enough CPU, we drop samples and simulate packet loss. |
| 80 | // + Resampling is not done yet, so streams in one group must use the same rate. |
Chia-chi Yeh | 3520bd4 | 2010-09-30 13:48:07 +0800 | [diff] [blame] | 81 | // For the first release only 8000Hz is supported. |
| 82 | |
| 83 | #define BUFFER_SIZE 512 |
| 84 | #define HISTORY_SIZE 80 |
| 85 | #define MEASURE_PERIOD 2000 |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 86 | |
| 87 | class AudioStream |
| 88 | { |
| 89 | public: |
| 90 | AudioStream(); |
| 91 | ~AudioStream(); |
| 92 | bool set(int mode, int socket, sockaddr_storage *remote, |
Chia-chi Yeh | 4033a67 | 2010-09-16 18:36:45 +0800 | [diff] [blame] | 93 | AudioCodec *codec, int sampleRate, int sampleCount, |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 94 | int codecType, int dtmfType); |
| 95 | |
| 96 | void sendDtmf(int event); |
| 97 | bool mix(int32_t *output, int head, int tail, int sampleRate); |
| 98 | void encode(int tick, AudioStream *chain); |
| 99 | void decode(int tick); |
| 100 | |
Chia-chi Yeh | 53aa6ef | 2010-11-30 13:10:31 +0800 | [diff] [blame] | 101 | private: |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 102 | enum { |
| 103 | NORMAL = 0, |
| 104 | SEND_ONLY = 1, |
| 105 | RECEIVE_ONLY = 2, |
| 106 | LAST_MODE = 2, |
| 107 | }; |
| 108 | |
| 109 | int mMode; |
| 110 | int mSocket; |
| 111 | sockaddr_storage mRemote; |
| 112 | AudioCodec *mCodec; |
| 113 | uint32_t mCodecMagic; |
| 114 | uint32_t mDtmfMagic; |
Chia-chi Yeh | fe52989 | 2010-09-30 02:42:27 +0800 | [diff] [blame] | 115 | bool mFixRemote; |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 116 | |
| 117 | int mTick; |
| 118 | int mSampleRate; |
| 119 | int mSampleCount; |
| 120 | int mInterval; |
Chia-chi Yeh | 3cf7137 | 2011-01-04 19:10:06 +0800 | [diff] [blame] | 121 | int mKeepAlive; |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 122 | |
| 123 | int16_t *mBuffer; |
| 124 | int mBufferMask; |
| 125 | int mBufferHead; |
| 126 | int mBufferTail; |
Chia-chi Yeh | 3520bd4 | 2010-09-30 13:48:07 +0800 | [diff] [blame] | 127 | int mLatencyTimer; |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 128 | int mLatencyScore; |
| 129 | |
| 130 | uint16_t mSequence; |
| 131 | uint32_t mTimestamp; |
| 132 | uint32_t mSsrc; |
| 133 | |
| 134 | int mDtmfEvent; |
| 135 | int mDtmfStart; |
| 136 | |
| 137 | AudioStream *mNext; |
| 138 | |
| 139 | friend class AudioGroup; |
| 140 | }; |
| 141 | |
| 142 | AudioStream::AudioStream() |
| 143 | { |
| 144 | mSocket = -1; |
| 145 | mCodec = NULL; |
| 146 | mBuffer = NULL; |
| 147 | mNext = NULL; |
| 148 | } |
| 149 | |
| 150 | AudioStream::~AudioStream() |
| 151 | { |
| 152 | close(mSocket); |
| 153 | delete mCodec; |
| 154 | delete [] mBuffer; |
| 155 | LOGD("stream[%d] is dead", mSocket); |
| 156 | } |
| 157 | |
| 158 | bool AudioStream::set(int mode, int socket, sockaddr_storage *remote, |
Chia-chi Yeh | 4033a67 | 2010-09-16 18:36:45 +0800 | [diff] [blame] | 159 | AudioCodec *codec, int sampleRate, int sampleCount, |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 160 | int codecType, int dtmfType) |
| 161 | { |
| 162 | if (mode < 0 || mode > LAST_MODE) { |
| 163 | return false; |
| 164 | } |
| 165 | mMode = mode; |
| 166 | |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 167 | mCodecMagic = (0x8000 | codecType) << 16; |
| 168 | mDtmfMagic = (dtmfType == -1) ? 0 : (0x8000 | dtmfType) << 16; |
| 169 | |
| 170 | mTick = elapsedRealtime(); |
| 171 | mSampleRate = sampleRate / 1000; |
| 172 | mSampleCount = sampleCount; |
| 173 | mInterval = mSampleCount / mSampleRate; |
| 174 | |
| 175 | // Allocate jitter buffer. |
Chia-chi Yeh | 3520bd4 | 2010-09-30 13:48:07 +0800 | [diff] [blame] | 176 | for (mBufferMask = 8; mBufferMask < mSampleRate; mBufferMask <<= 1); |
| 177 | mBufferMask *= BUFFER_SIZE; |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 178 | mBuffer = new int16_t[mBufferMask]; |
| 179 | --mBufferMask; |
| 180 | mBufferHead = 0; |
| 181 | mBufferTail = 0; |
Chia-chi Yeh | 3520bd4 | 2010-09-30 13:48:07 +0800 | [diff] [blame] | 182 | mLatencyTimer = 0; |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 183 | mLatencyScore = 0; |
| 184 | |
| 185 | // Initialize random bits. |
| 186 | read(gRandom, &mSequence, sizeof(mSequence)); |
| 187 | read(gRandom, &mTimestamp, sizeof(mTimestamp)); |
| 188 | read(gRandom, &mSsrc, sizeof(mSsrc)); |
| 189 | |
| 190 | mDtmfEvent = -1; |
| 191 | mDtmfStart = 0; |
| 192 | |
Chia-chi Yeh | 4033a67 | 2010-09-16 18:36:45 +0800 | [diff] [blame] | 193 | // Only take over these things when succeeded. |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 194 | mSocket = socket; |
Chia-chi Yeh | 4033a67 | 2010-09-16 18:36:45 +0800 | [diff] [blame] | 195 | if (codec) { |
| 196 | mRemote = *remote; |
| 197 | mCodec = codec; |
Chia-chi Yeh | fe52989 | 2010-09-30 02:42:27 +0800 | [diff] [blame] | 198 | |
| 199 | // Here we should never get an private address, but some buggy proxy |
| 200 | // servers do give us one. To solve this, we replace the address when |
| 201 | // the first time we successfully decode an incoming packet. |
| 202 | mFixRemote = false; |
| 203 | if (remote->ss_family == AF_INET) { |
| 204 | unsigned char *address = |
| 205 | (unsigned char *)&((sockaddr_in *)remote)->sin_addr; |
| 206 | if (address[0] == 10 || |
| 207 | (address[0] == 172 && (address[1] >> 4) == 1) || |
| 208 | (address[0] == 192 && address[1] == 168)) { |
| 209 | mFixRemote = true; |
| 210 | } |
| 211 | } |
Chia-chi Yeh | 4033a67 | 2010-09-16 18:36:45 +0800 | [diff] [blame] | 212 | } |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 213 | |
Chia-chi Yeh | 21ae1ad | 2010-09-30 16:07:44 +0800 | [diff] [blame] | 214 | LOGD("stream[%d] is configured as %s %dkHz %dms mode %d", mSocket, |
| 215 | (codec ? codec->name : "RAW"), mSampleRate, mInterval, mMode); |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 216 | return true; |
| 217 | } |
| 218 | |
| 219 | void AudioStream::sendDtmf(int event) |
| 220 | { |
| 221 | if (mDtmfMagic != 0) { |
| 222 | mDtmfEvent = event << 24; |
| 223 | mDtmfStart = mTimestamp + mSampleCount; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | bool AudioStream::mix(int32_t *output, int head, int tail, int sampleRate) |
| 228 | { |
| 229 | if (mMode == SEND_ONLY) { |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | if (head - mBufferHead < 0) { |
| 234 | head = mBufferHead; |
| 235 | } |
| 236 | if (tail - mBufferTail > 0) { |
| 237 | tail = mBufferTail; |
| 238 | } |
| 239 | if (tail - head <= 0) { |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | head *= mSampleRate; |
| 244 | tail *= mSampleRate; |
| 245 | |
| 246 | if (sampleRate == mSampleRate) { |
| 247 | for (int i = head; i - tail < 0; ++i) { |
| 248 | output[i - head] += mBuffer[i & mBufferMask]; |
| 249 | } |
| 250 | } else { |
| 251 | // TODO: implement resampling. |
| 252 | return false; |
| 253 | } |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | void AudioStream::encode(int tick, AudioStream *chain) |
| 258 | { |
| 259 | if (tick - mTick >= mInterval) { |
| 260 | // We just missed the train. Pretend that packets in between are lost. |
| 261 | int skipped = (tick - mTick) / mInterval; |
| 262 | mTick += skipped * mInterval; |
| 263 | mSequence += skipped; |
| 264 | mTimestamp += skipped * mSampleCount; |
Chia-chi Yeh | 21ae1ad | 2010-09-30 16:07:44 +0800 | [diff] [blame] | 265 | LOGV("stream[%d] skips %d packets", mSocket, skipped); |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | tick = mTick; |
| 269 | mTick += mInterval; |
| 270 | ++mSequence; |
| 271 | mTimestamp += mSampleCount; |
| 272 | |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 273 | // If there is an ongoing DTMF event, send it now. |
Chia-chi Yeh | 3cf7137 | 2011-01-04 19:10:06 +0800 | [diff] [blame] | 274 | if (mMode != RECEIVE_ONLY && mDtmfEvent != -1) { |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 275 | int duration = mTimestamp - mDtmfStart; |
| 276 | // Make sure duration is reasonable. |
| 277 | if (duration >= 0 && duration < mSampleRate * 100) { |
| 278 | duration += mSampleCount; |
| 279 | int32_t buffer[4] = { |
| 280 | htonl(mDtmfMagic | mSequence), |
| 281 | htonl(mDtmfStart), |
| 282 | mSsrc, |
| 283 | htonl(mDtmfEvent | duration), |
| 284 | }; |
| 285 | if (duration >= mSampleRate * 100) { |
| 286 | buffer[3] |= htonl(1 << 23); |
| 287 | mDtmfEvent = -1; |
| 288 | } |
| 289 | sendto(mSocket, buffer, sizeof(buffer), MSG_DONTWAIT, |
| 290 | (sockaddr *)&mRemote, sizeof(mRemote)); |
| 291 | return; |
| 292 | } |
| 293 | mDtmfEvent = -1; |
| 294 | } |
| 295 | |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 296 | int32_t buffer[mSampleCount + 3]; |
Chia-chi Yeh | 3cf7137 | 2011-01-04 19:10:06 +0800 | [diff] [blame] | 297 | int16_t samples[mSampleCount]; |
| 298 | if (mMode == RECEIVE_ONLY) { |
| 299 | if ((mTick ^ mKeepAlive) >> 10 == 0) { |
| 300 | return; |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 301 | } |
Chia-chi Yeh | 3cf7137 | 2011-01-04 19:10:06 +0800 | [diff] [blame] | 302 | mKeepAlive = mTick; |
| 303 | memset(samples, 0, sizeof(samples)); |
| 304 | } else { |
| 305 | // Mix all other streams. |
| 306 | bool mixed = false; |
| 307 | memset(buffer, 0, sizeof(buffer)); |
| 308 | while (chain) { |
| 309 | if (chain != this && |
| 310 | chain->mix(buffer, tick - mInterval, tick, mSampleRate)) { |
| 311 | mixed = true; |
| 312 | } |
| 313 | chain = chain->mNext; |
| 314 | } |
| 315 | |
| 316 | if (mixed) { |
| 317 | // Saturate into 16 bits. |
| 318 | for (int i = 0; i < mSampleCount; ++i) { |
| 319 | int32_t sample = buffer[i]; |
| 320 | if (sample < -32768) { |
| 321 | sample = -32768; |
| 322 | } |
| 323 | if (sample > 32767) { |
| 324 | sample = 32767; |
| 325 | } |
| 326 | samples[i] = sample; |
| 327 | } |
| 328 | } else { |
| 329 | if ((mTick ^ mKeepAlive) >> 10 == 0) { |
| 330 | return; |
| 331 | } |
| 332 | mKeepAlive = mTick; |
| 333 | memset(samples, 0, sizeof(samples)); |
Chia-chi Yeh | 21ae1ad | 2010-09-30 16:07:44 +0800 | [diff] [blame] | 334 | LOGV("stream[%d] no data", mSocket); |
repo sync | 7a69aef | 2010-09-23 05:46:01 +0800 | [diff] [blame] | 335 | } |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 336 | } |
| 337 | |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 338 | if (!mCodec) { |
| 339 | // Special case for device stream. |
| 340 | send(mSocket, samples, sizeof(samples), MSG_DONTWAIT); |
| 341 | return; |
| 342 | } |
| 343 | |
Chia-chi Yeh | 3cf7137 | 2011-01-04 19:10:06 +0800 | [diff] [blame] | 344 | // Cook the packet and send it out. |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 345 | buffer[0] = htonl(mCodecMagic | mSequence); |
| 346 | buffer[1] = htonl(mTimestamp); |
| 347 | buffer[2] = mSsrc; |
| 348 | int length = mCodec->encode(&buffer[3], samples); |
| 349 | if (length <= 0) { |
Chia-chi Yeh | 21ae1ad | 2010-09-30 16:07:44 +0800 | [diff] [blame] | 350 | LOGV("stream[%d] encoder error", mSocket); |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 351 | return; |
| 352 | } |
| 353 | sendto(mSocket, buffer, length + 12, MSG_DONTWAIT, (sockaddr *)&mRemote, |
| 354 | sizeof(mRemote)); |
| 355 | } |
| 356 | |
| 357 | void AudioStream::decode(int tick) |
| 358 | { |
| 359 | char c; |
| 360 | if (mMode == SEND_ONLY) { |
| 361 | recv(mSocket, &c, 1, MSG_DONTWAIT); |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | // Make sure mBufferHead and mBufferTail are reasonable. |
Chia-chi Yeh | 3520bd4 | 2010-09-30 13:48:07 +0800 | [diff] [blame] | 366 | if ((unsigned int)(tick + BUFFER_SIZE - mBufferHead) > BUFFER_SIZE * 2) { |
| 367 | mBufferHead = tick - HISTORY_SIZE; |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 368 | mBufferTail = mBufferHead; |
| 369 | } |
| 370 | |
Chia-chi Yeh | 3520bd4 | 2010-09-30 13:48:07 +0800 | [diff] [blame] | 371 | if (tick - mBufferHead > HISTORY_SIZE) { |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 372 | // Throw away outdated samples. |
Chia-chi Yeh | 3520bd4 | 2010-09-30 13:48:07 +0800 | [diff] [blame] | 373 | mBufferHead = tick - HISTORY_SIZE; |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 374 | if (mBufferTail - mBufferHead < 0) { |
| 375 | mBufferTail = mBufferHead; |
| 376 | } |
| 377 | } |
| 378 | |
Chia-chi Yeh | 3520bd4 | 2010-09-30 13:48:07 +0800 | [diff] [blame] | 379 | // Adjust the jitter buffer if the latency keeps larger than two times of the |
| 380 | // packet interval in the past two seconds. |
| 381 | int score = mBufferTail - tick - mInterval * 2; |
| 382 | if (mLatencyScore > score) { |
| 383 | mLatencyScore = score; |
| 384 | } |
| 385 | if (mLatencyScore <= 0) { |
| 386 | mLatencyTimer = tick; |
| 387 | mLatencyScore = score; |
| 388 | } else if (tick - mLatencyTimer >= MEASURE_PERIOD) { |
Chia-chi Yeh | 21ae1ad | 2010-09-30 16:07:44 +0800 | [diff] [blame] | 389 | LOGV("stream[%d] reduces latency of %dms", mSocket, mLatencyScore); |
Chia-chi Yeh | 3520bd4 | 2010-09-30 13:48:07 +0800 | [diff] [blame] | 390 | mBufferTail -= mLatencyScore; |
| 391 | mLatencyTimer = tick; |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 392 | } |
| 393 | |
Chia-chi Yeh | 3520bd4 | 2010-09-30 13:48:07 +0800 | [diff] [blame] | 394 | if (mBufferTail - mBufferHead > BUFFER_SIZE - mInterval) { |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 395 | // Buffer overflow. Drop the packet. |
Chia-chi Yeh | 21ae1ad | 2010-09-30 16:07:44 +0800 | [diff] [blame] | 396 | LOGV("stream[%d] buffer overflow", mSocket); |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 397 | recv(mSocket, &c, 1, MSG_DONTWAIT); |
| 398 | return; |
| 399 | } |
| 400 | |
| 401 | // Receive the packet and decode it. |
| 402 | int16_t samples[mSampleCount]; |
| 403 | int length = 0; |
| 404 | if (!mCodec) { |
| 405 | // Special case for device stream. |
| 406 | length = recv(mSocket, samples, sizeof(samples), |
| 407 | MSG_TRUNC | MSG_DONTWAIT) >> 1; |
| 408 | } else { |
| 409 | __attribute__((aligned(4))) uint8_t buffer[2048]; |
Chia-chi Yeh | fe52989 | 2010-09-30 02:42:27 +0800 | [diff] [blame] | 410 | sockaddr_storage remote; |
| 411 | socklen_t len = sizeof(remote); |
Chung-yih Wang | bd22942 | 2010-09-23 23:23:11 +0800 | [diff] [blame] | 412 | |
Chia-chi Yeh | fe52989 | 2010-09-30 02:42:27 +0800 | [diff] [blame] | 413 | length = recvfrom(mSocket, buffer, sizeof(buffer), |
| 414 | MSG_TRUNC | MSG_DONTWAIT, (sockaddr *)&remote, &len); |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 415 | |
| 416 | // Do we need to check SSRC, sequence, and timestamp? They are not |
Chia-chi Yeh | b879032 | 2010-08-19 18:26:53 +0800 | [diff] [blame] | 417 | // reliable but at least they can be used to identify duplicates? |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 418 | if (length < 12 || length > (int)sizeof(buffer) || |
| 419 | (ntohl(*(uint32_t *)buffer) & 0xC07F0000) != mCodecMagic) { |
Chia-chi Yeh | 21ae1ad | 2010-09-30 16:07:44 +0800 | [diff] [blame] | 420 | LOGV("stream[%d] malformed packet", mSocket); |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 421 | return; |
| 422 | } |
| 423 | int offset = 12 + ((buffer[0] & 0x0F) << 2); |
| 424 | if ((buffer[0] & 0x10) != 0) { |
| 425 | offset += 4 + (ntohs(*(uint16_t *)&buffer[offset + 2]) << 2); |
| 426 | } |
| 427 | if ((buffer[0] & 0x20) != 0) { |
| 428 | length -= buffer[length - 1]; |
| 429 | } |
| 430 | length -= offset; |
| 431 | if (length >= 0) { |
| 432 | length = mCodec->decode(samples, &buffer[offset], length); |
| 433 | } |
Chia-chi Yeh | fe52989 | 2010-09-30 02:42:27 +0800 | [diff] [blame] | 434 | if (length > 0 && mFixRemote) { |
| 435 | mRemote = remote; |
| 436 | mFixRemote = false; |
| 437 | } |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 438 | } |
Chia-chi Yeh | fe52989 | 2010-09-30 02:42:27 +0800 | [diff] [blame] | 439 | if (length <= 0) { |
Chia-chi Yeh | 21ae1ad | 2010-09-30 16:07:44 +0800 | [diff] [blame] | 440 | LOGV("stream[%d] decoder error", mSocket); |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 441 | return; |
| 442 | } |
| 443 | |
| 444 | if (tick - mBufferTail > 0) { |
Chia-chi Yeh | 3520bd4 | 2010-09-30 13:48:07 +0800 | [diff] [blame] | 445 | // Buffer underrun. Reset the jitter buffer. |
Chia-chi Yeh | 21ae1ad | 2010-09-30 16:07:44 +0800 | [diff] [blame] | 446 | LOGV("stream[%d] buffer underrun", mSocket); |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 447 | if (mBufferTail - mBufferHead <= 0) { |
Chia-chi Yeh | 3520bd4 | 2010-09-30 13:48:07 +0800 | [diff] [blame] | 448 | mBufferHead = tick + mInterval; |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 449 | mBufferTail = mBufferHead; |
| 450 | } else { |
Chia-chi Yeh | 3520bd4 | 2010-09-30 13:48:07 +0800 | [diff] [blame] | 451 | int tail = (tick + mInterval) * mSampleRate; |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 452 | for (int i = mBufferTail * mSampleRate; i - tail < 0; ++i) { |
| 453 | mBuffer[i & mBufferMask] = 0; |
| 454 | } |
Chia-chi Yeh | 3520bd4 | 2010-09-30 13:48:07 +0800 | [diff] [blame] | 455 | mBufferTail = tick + mInterval; |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | |
| 459 | // Append to the jitter buffer. |
| 460 | int tail = mBufferTail * mSampleRate; |
| 461 | for (int i = 0; i < mSampleCount; ++i) { |
| 462 | mBuffer[tail & mBufferMask] = samples[i]; |
| 463 | ++tail; |
| 464 | } |
| 465 | mBufferTail += mInterval; |
| 466 | } |
| 467 | |
| 468 | //------------------------------------------------------------------------------ |
| 469 | |
| 470 | class AudioGroup |
| 471 | { |
| 472 | public: |
| 473 | AudioGroup(); |
| 474 | ~AudioGroup(); |
| 475 | bool set(int sampleRate, int sampleCount); |
| 476 | |
| 477 | bool setMode(int mode); |
| 478 | bool sendDtmf(int event); |
| 479 | bool add(AudioStream *stream); |
| 480 | bool remove(int socket); |
| 481 | |
Chia-chi Yeh | 53aa6ef | 2010-11-30 13:10:31 +0800 | [diff] [blame] | 482 | private: |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 483 | enum { |
| 484 | ON_HOLD = 0, |
| 485 | MUTED = 1, |
| 486 | NORMAL = 2, |
Chia-chi Yeh | d87be27 | 2011-01-06 17:43:24 +0800 | [diff] [blame] | 487 | ECHO_SUPPRESSION = 3, |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 488 | LAST_MODE = 3, |
| 489 | }; |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 490 | |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 491 | AudioStream *mChain; |
| 492 | int mEventQueue; |
| 493 | volatile int mDtmfEvent; |
| 494 | |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 495 | int mMode; |
| 496 | int mSampleRate; |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 497 | int mSampleCount; |
| 498 | int mDeviceSocket; |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 499 | |
| 500 | class NetworkThread : public Thread |
| 501 | { |
| 502 | public: |
| 503 | NetworkThread(AudioGroup *group) : Thread(false), mGroup(group) {} |
| 504 | |
| 505 | bool start() |
| 506 | { |
| 507 | if (run("Network", ANDROID_PRIORITY_AUDIO) != NO_ERROR) { |
| 508 | LOGE("cannot start network thread"); |
| 509 | return false; |
| 510 | } |
| 511 | return true; |
| 512 | } |
| 513 | |
| 514 | private: |
| 515 | AudioGroup *mGroup; |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 516 | bool threadLoop(); |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 517 | }; |
| 518 | sp<NetworkThread> mNetworkThread; |
| 519 | |
| 520 | class DeviceThread : public Thread |
| 521 | { |
| 522 | public: |
| 523 | DeviceThread(AudioGroup *group) : Thread(false), mGroup(group) {} |
| 524 | |
| 525 | bool start() |
| 526 | { |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 527 | if (run("Device", ANDROID_PRIORITY_AUDIO) != NO_ERROR) { |
| 528 | LOGE("cannot start device thread"); |
| 529 | return false; |
| 530 | } |
| 531 | return true; |
| 532 | } |
| 533 | |
| 534 | private: |
| 535 | AudioGroup *mGroup; |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 536 | bool threadLoop(); |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 537 | }; |
| 538 | sp<DeviceThread> mDeviceThread; |
| 539 | }; |
| 540 | |
| 541 | AudioGroup::AudioGroup() |
| 542 | { |
| 543 | mMode = ON_HOLD; |
| 544 | mChain = NULL; |
| 545 | mEventQueue = -1; |
| 546 | mDtmfEvent = -1; |
| 547 | mDeviceSocket = -1; |
| 548 | mNetworkThread = new NetworkThread(this); |
| 549 | mDeviceThread = new DeviceThread(this); |
| 550 | } |
| 551 | |
| 552 | AudioGroup::~AudioGroup() |
| 553 | { |
| 554 | mNetworkThread->requestExitAndWait(); |
| 555 | mDeviceThread->requestExitAndWait(); |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 556 | close(mEventQueue); |
| 557 | close(mDeviceSocket); |
| 558 | while (mChain) { |
| 559 | AudioStream *next = mChain->mNext; |
| 560 | delete mChain; |
| 561 | mChain = next; |
| 562 | } |
| 563 | LOGD("group[%d] is dead", mDeviceSocket); |
| 564 | } |
| 565 | |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 566 | bool AudioGroup::set(int sampleRate, int sampleCount) |
| 567 | { |
| 568 | mEventQueue = epoll_create(2); |
| 569 | if (mEventQueue == -1) { |
| 570 | LOGE("epoll_create: %s", strerror(errno)); |
| 571 | return false; |
| 572 | } |
| 573 | |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 574 | mSampleRate = sampleRate; |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 575 | mSampleCount = sampleCount; |
| 576 | |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 577 | // Create device socket. |
| 578 | int pair[2]; |
| 579 | if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair)) { |
| 580 | LOGE("socketpair: %s", strerror(errno)); |
| 581 | return false; |
| 582 | } |
| 583 | mDeviceSocket = pair[0]; |
| 584 | |
| 585 | // Create device stream. |
| 586 | mChain = new AudioStream; |
| 587 | if (!mChain->set(AudioStream::NORMAL, pair[1], NULL, NULL, |
| 588 | sampleRate, sampleCount, -1, -1)) { |
| 589 | close(pair[1]); |
| 590 | LOGE("cannot initialize device stream"); |
| 591 | return false; |
| 592 | } |
| 593 | |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 594 | // Give device socket a reasonable timeout. |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 595 | timeval tv; |
| 596 | tv.tv_sec = 0; |
Chia-chi Yeh | 557b04d | 2010-09-08 09:56:02 +0800 | [diff] [blame] | 597 | tv.tv_usec = 1000 * sampleCount / sampleRate * 500; |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 598 | if (setsockopt(pair[0], SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv))) { |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 599 | LOGE("setsockopt: %s", strerror(errno)); |
| 600 | return false; |
| 601 | } |
| 602 | |
| 603 | // Add device stream into event queue. |
| 604 | epoll_event event; |
| 605 | event.events = EPOLLIN; |
| 606 | event.data.ptr = mChain; |
| 607 | if (epoll_ctl(mEventQueue, EPOLL_CTL_ADD, pair[1], &event)) { |
| 608 | LOGE("epoll_ctl: %s", strerror(errno)); |
| 609 | return false; |
| 610 | } |
| 611 | |
| 612 | // Anything else? |
| 613 | LOGD("stream[%d] joins group[%d]", pair[1], pair[0]); |
| 614 | return true; |
| 615 | } |
| 616 | |
| 617 | bool AudioGroup::setMode(int mode) |
| 618 | { |
| 619 | if (mode < 0 || mode > LAST_MODE) { |
| 620 | return false; |
| 621 | } |
Chia-chi Yeh | d87be27 | 2011-01-06 17:43:24 +0800 | [diff] [blame] | 622 | if (mode == ECHO_SUPPRESSION && AudioSystem::getParameters( |
| 623 | 0, String8("ec_supported")) == "ec_supported=yes") { |
| 624 | mode = NORMAL; |
| 625 | } |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 626 | if (mMode == mode) { |
| 627 | return true; |
| 628 | } |
| 629 | |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 630 | mDeviceThread->requestExitAndWait(); |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 631 | LOGD("group[%d] switches from mode %d to %d", mDeviceSocket, mMode, mode); |
| 632 | mMode = mode; |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 633 | return (mode == ON_HOLD) || mDeviceThread->start(); |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | bool AudioGroup::sendDtmf(int event) |
| 637 | { |
| 638 | if (event < 0 || event > 15) { |
| 639 | return false; |
| 640 | } |
| 641 | |
| 642 | // DTMF is rarely used, so we try to make it as lightweight as possible. |
| 643 | // Using volatile might be dodgy, but using a pipe or pthread primitives |
| 644 | // or stop-set-restart threads seems too heavy. Will investigate later. |
| 645 | timespec ts; |
| 646 | ts.tv_sec = 0; |
| 647 | ts.tv_nsec = 100000000; |
| 648 | for (int i = 0; mDtmfEvent != -1 && i < 20; ++i) { |
| 649 | nanosleep(&ts, NULL); |
| 650 | } |
| 651 | if (mDtmfEvent != -1) { |
| 652 | return false; |
| 653 | } |
| 654 | mDtmfEvent = event; |
| 655 | nanosleep(&ts, NULL); |
| 656 | return true; |
| 657 | } |
| 658 | |
| 659 | bool AudioGroup::add(AudioStream *stream) |
| 660 | { |
| 661 | mNetworkThread->requestExitAndWait(); |
| 662 | |
| 663 | epoll_event event; |
| 664 | event.events = EPOLLIN; |
| 665 | event.data.ptr = stream; |
| 666 | if (epoll_ctl(mEventQueue, EPOLL_CTL_ADD, stream->mSocket, &event)) { |
| 667 | LOGE("epoll_ctl: %s", strerror(errno)); |
| 668 | return false; |
| 669 | } |
| 670 | |
| 671 | stream->mNext = mChain->mNext; |
| 672 | mChain->mNext = stream; |
| 673 | if (!mNetworkThread->start()) { |
| 674 | // Only take over the stream when succeeded. |
| 675 | mChain->mNext = stream->mNext; |
| 676 | return false; |
| 677 | } |
| 678 | |
| 679 | LOGD("stream[%d] joins group[%d]", stream->mSocket, mDeviceSocket); |
| 680 | return true; |
| 681 | } |
| 682 | |
| 683 | bool AudioGroup::remove(int socket) |
| 684 | { |
| 685 | mNetworkThread->requestExitAndWait(); |
| 686 | |
| 687 | for (AudioStream *stream = mChain; stream->mNext; stream = stream->mNext) { |
| 688 | AudioStream *target = stream->mNext; |
| 689 | if (target->mSocket == socket) { |
Chia-chi Yeh | b879032 | 2010-08-19 18:26:53 +0800 | [diff] [blame] | 690 | if (epoll_ctl(mEventQueue, EPOLL_CTL_DEL, socket, NULL)) { |
| 691 | LOGE("epoll_ctl: %s", strerror(errno)); |
| 692 | return false; |
| 693 | } |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 694 | stream->mNext = target->mNext; |
| 695 | LOGD("stream[%d] leaves group[%d]", socket, mDeviceSocket); |
| 696 | delete target; |
| 697 | break; |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | // Do not start network thread if there is only one stream. |
| 702 | if (!mChain->mNext || !mNetworkThread->start()) { |
| 703 | return false; |
| 704 | } |
| 705 | return true; |
| 706 | } |
| 707 | |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 708 | bool AudioGroup::NetworkThread::threadLoop() |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 709 | { |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 710 | AudioStream *chain = mGroup->mChain; |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 711 | int tick = elapsedRealtime(); |
| 712 | int deadline = tick + 10; |
| 713 | int count = 0; |
| 714 | |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 715 | for (AudioStream *stream = chain; stream; stream = stream->mNext) { |
Chia-chi Yeh | 3520bd4 | 2010-09-30 13:48:07 +0800 | [diff] [blame] | 716 | if (tick - stream->mTick >= 0) { |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 717 | stream->encode(tick, chain); |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 718 | } |
| 719 | if (deadline - stream->mTick > 0) { |
| 720 | deadline = stream->mTick; |
| 721 | } |
| 722 | ++count; |
| 723 | } |
| 724 | |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 725 | int event = mGroup->mDtmfEvent; |
| 726 | if (event != -1) { |
| 727 | for (AudioStream *stream = chain; stream; stream = stream->mNext) { |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 728 | stream->sendDtmf(event); |
| 729 | } |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 730 | mGroup->mDtmfEvent = -1; |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | deadline -= tick; |
| 734 | if (deadline < 1) { |
| 735 | deadline = 1; |
| 736 | } |
| 737 | |
| 738 | epoll_event events[count]; |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 739 | count = epoll_wait(mGroup->mEventQueue, events, count, deadline); |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 740 | if (count == -1) { |
| 741 | LOGE("epoll_wait: %s", strerror(errno)); |
| 742 | return false; |
| 743 | } |
| 744 | for (int i = 0; i < count; ++i) { |
| 745 | ((AudioStream *)events[i].data.ptr)->decode(tick); |
| 746 | } |
| 747 | |
| 748 | return true; |
| 749 | } |
| 750 | |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 751 | bool AudioGroup::DeviceThread::threadLoop() |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 752 | { |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 753 | int mode = mGroup->mMode; |
| 754 | int sampleRate = mGroup->mSampleRate; |
| 755 | int sampleCount = mGroup->mSampleCount; |
| 756 | int deviceSocket = mGroup->mDeviceSocket; |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 757 | |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 758 | // Find out the frame count for AudioTrack and AudioRecord. |
| 759 | int output = 0; |
| 760 | int input = 0; |
| 761 | if (AudioTrack::getMinFrameCount(&output, AudioSystem::VOICE_CALL, |
| 762 | sampleRate) != NO_ERROR || output <= 0 || |
| 763 | AudioRecord::getMinFrameCount(&input, sampleRate, |
| 764 | AudioSystem::PCM_16_BIT, 1) != NO_ERROR || input <= 0) { |
| 765 | LOGE("cannot compute frame count"); |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 766 | return false; |
| 767 | } |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 768 | LOGD("reported frame count: output %d, input %d", output, input); |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 769 | |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 770 | if (output < sampleCount * 2) { |
| 771 | output = sampleCount * 2; |
| 772 | } |
| 773 | if (input < sampleCount * 2) { |
| 774 | input = sampleCount * 2; |
| 775 | } |
| 776 | LOGD("adjusted frame count: output %d, input %d", output, input); |
| 777 | |
| 778 | // Initialize AudioTrack and AudioRecord. |
| 779 | AudioTrack track; |
| 780 | AudioRecord record; |
| 781 | if (track.set(AudioSystem::VOICE_CALL, sampleRate, AudioSystem::PCM_16_BIT, |
Chia-chi Yeh | d87be27 | 2011-01-06 17:43:24 +0800 | [diff] [blame] | 782 | AudioSystem::CHANNEL_OUT_MONO, output) != NO_ERROR || record.set( |
| 783 | AUDIO_SOURCE_VOICE_COMMUNICATION, sampleRate, AudioSystem::PCM_16_BIT, |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 784 | AudioSystem::CHANNEL_IN_MONO, input) != NO_ERROR) { |
| 785 | LOGE("cannot initialize audio device"); |
| 786 | return false; |
| 787 | } |
| 788 | LOGD("latency: output %d, input %d", track.latency(), record.latency()); |
| 789 | |
Chia-chi Yeh | a8a1009 | 2010-10-05 01:17:13 +0800 | [diff] [blame] | 790 | // Initialize echo canceler. |
Chia-chi Yeh | 8a68b52 | 2010-10-21 23:39:35 +0800 | [diff] [blame] | 791 | EchoSuppressor echo(sampleCount, |
Chia-chi Yeh | a8a1009 | 2010-10-05 01:17:13 +0800 | [diff] [blame] | 792 | (track.latency() + record.latency()) * sampleRate / 1000); |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 793 | |
| 794 | // Give device socket a reasonable buffer size. |
| 795 | setsockopt(deviceSocket, SOL_SOCKET, SO_RCVBUF, &output, sizeof(output)); |
| 796 | setsockopt(deviceSocket, SOL_SOCKET, SO_SNDBUF, &output, sizeof(output)); |
| 797 | |
| 798 | // Drain device socket. |
| 799 | char c; |
| 800 | while (recv(deviceSocket, &c, 1, MSG_DONTWAIT) == 1); |
| 801 | |
Chia-chi Yeh | 67ecb5b | 2010-10-01 08:20:09 +0800 | [diff] [blame] | 802 | // Start AudioRecord before AudioTrack. This prevents AudioTrack from being |
| 803 | // disabled due to buffer underrun while waiting for AudioRecord. |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 804 | if (mode != MUTED) { |
| 805 | record.start(); |
Chia-chi Yeh | 67ecb5b | 2010-10-01 08:20:09 +0800 | [diff] [blame] | 806 | int16_t one; |
| 807 | record.read(&one, sizeof(one)); |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 808 | } |
Chia-chi Yeh | 67ecb5b | 2010-10-01 08:20:09 +0800 | [diff] [blame] | 809 | track.start(); |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 810 | |
| 811 | while (!exitPending()) { |
| 812 | int16_t output[sampleCount]; |
| 813 | if (recv(deviceSocket, output, sizeof(output), 0) <= 0) { |
| 814 | memset(output, 0, sizeof(output)); |
| 815 | } |
| 816 | |
| 817 | int16_t input[sampleCount]; |
| 818 | int toWrite = sampleCount; |
| 819 | int toRead = (mode == MUTED) ? 0 : sampleCount; |
| 820 | int chances = 100; |
| 821 | |
| 822 | while (--chances > 0 && (toWrite > 0 || toRead > 0)) { |
| 823 | if (toWrite > 0) { |
| 824 | AudioTrack::Buffer buffer; |
| 825 | buffer.frameCount = toWrite; |
| 826 | |
| 827 | status_t status = track.obtainBuffer(&buffer, 1); |
| 828 | if (status == NO_ERROR) { |
| 829 | int offset = sampleCount - toWrite; |
| 830 | memcpy(buffer.i8, &output[offset], buffer.size); |
| 831 | toWrite -= buffer.frameCount; |
| 832 | track.releaseBuffer(&buffer); |
| 833 | } else if (status != TIMED_OUT && status != WOULD_BLOCK) { |
| 834 | LOGE("cannot write to AudioTrack"); |
Chia-chi Yeh | 67ecb5b | 2010-10-01 08:20:09 +0800 | [diff] [blame] | 835 | return true; |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 836 | } |
| 837 | } |
| 838 | |
| 839 | if (toRead > 0) { |
| 840 | AudioRecord::Buffer buffer; |
Chia-chi Yeh | 67ecb5b | 2010-10-01 08:20:09 +0800 | [diff] [blame] | 841 | buffer.frameCount = toRead; |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 842 | |
| 843 | status_t status = record.obtainBuffer(&buffer, 1); |
| 844 | if (status == NO_ERROR) { |
Chia-chi Yeh | 67ecb5b | 2010-10-01 08:20:09 +0800 | [diff] [blame] | 845 | int offset = sampleCount - toRead; |
| 846 | memcpy(&input[offset], buffer.i8, buffer.size); |
| 847 | toRead -= buffer.frameCount; |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 848 | record.releaseBuffer(&buffer); |
| 849 | } else if (status != TIMED_OUT && status != WOULD_BLOCK) { |
| 850 | LOGE("cannot read from AudioRecord"); |
Chia-chi Yeh | 67ecb5b | 2010-10-01 08:20:09 +0800 | [diff] [blame] | 851 | return true; |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | if (chances <= 0) { |
Chia-chi Yeh | 67ecb5b | 2010-10-01 08:20:09 +0800 | [diff] [blame] | 857 | LOGW("device loop timeout"); |
| 858 | while (recv(deviceSocket, &c, 1, MSG_DONTWAIT) == 1); |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | if (mode != MUTED) { |
| 862 | if (mode == NORMAL) { |
| 863 | send(deviceSocket, input, sizeof(input), MSG_DONTWAIT); |
| 864 | } else { |
Chia-chi Yeh | a8a1009 | 2010-10-05 01:17:13 +0800 | [diff] [blame] | 865 | echo.run(output, input); |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 866 | send(deviceSocket, input, sizeof(input), MSG_DONTWAIT); |
| 867 | } |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 868 | } |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 869 | } |
Chia-chi Yeh | 9083c84 | 2010-09-29 05:19:44 +0800 | [diff] [blame] | 870 | return false; |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | //------------------------------------------------------------------------------ |
| 874 | |
| 875 | static jfieldID gNative; |
| 876 | static jfieldID gMode; |
| 877 | |
Chia-chi Yeh | b879032 | 2010-08-19 18:26:53 +0800 | [diff] [blame] | 878 | void add(JNIEnv *env, jobject thiz, jint mode, |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 879 | jint socket, jstring jRemoteAddress, jint remotePort, |
Chia-chi Yeh | 4033a67 | 2010-09-16 18:36:45 +0800 | [diff] [blame] | 880 | jstring jCodecSpec, jint dtmfType) |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 881 | { |
Chia-chi Yeh | 4033a67 | 2010-09-16 18:36:45 +0800 | [diff] [blame] | 882 | AudioCodec *codec = NULL; |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 883 | AudioStream *stream = NULL; |
| 884 | AudioGroup *group = NULL; |
| 885 | |
| 886 | // Sanity check. |
| 887 | sockaddr_storage remote; |
| 888 | if (parse(env, jRemoteAddress, remotePort, &remote) < 0) { |
| 889 | // Exception already thrown. |
Chia-chi Yeh | 4033a67 | 2010-09-16 18:36:45 +0800 | [diff] [blame] | 890 | return; |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 891 | } |
Chia-chi Yeh | 4033a67 | 2010-09-16 18:36:45 +0800 | [diff] [blame] | 892 | if (!jCodecSpec) { |
| 893 | jniThrowNullPointerException(env, "codecSpec"); |
| 894 | return; |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 895 | } |
Chia-chi Yeh | 4033a67 | 2010-09-16 18:36:45 +0800 | [diff] [blame] | 896 | const char *codecSpec = env->GetStringUTFChars(jCodecSpec, NULL); |
| 897 | if (!codecSpec) { |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 898 | // Exception already thrown. |
Chia-chi Yeh | 4033a67 | 2010-09-16 18:36:45 +0800 | [diff] [blame] | 899 | return; |
| 900 | } |
| 901 | |
| 902 | // Create audio codec. |
| 903 | int codecType = -1; |
| 904 | char codecName[16]; |
| 905 | int sampleRate = -1; |
Chia-chi Yeh | 3cf7137 | 2011-01-04 19:10:06 +0800 | [diff] [blame] | 906 | sscanf(codecSpec, "%d %15[^/]%*c%d", &codecType, codecName, &sampleRate); |
Chia-chi Yeh | 4033a67 | 2010-09-16 18:36:45 +0800 | [diff] [blame] | 907 | codec = newAudioCodec(codecName); |
| 908 | int sampleCount = (codec ? codec->set(sampleRate, codecSpec) : -1); |
| 909 | env->ReleaseStringUTFChars(jCodecSpec, codecSpec); |
| 910 | if (sampleCount <= 0) { |
| 911 | jniThrowException(env, "java/lang/IllegalStateException", |
| 912 | "cannot initialize audio codec"); |
Chia-chi Yeh | b879032 | 2010-08-19 18:26:53 +0800 | [diff] [blame] | 913 | goto error; |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | // Create audio stream. |
| 917 | stream = new AudioStream; |
Chia-chi Yeh | 4033a67 | 2010-09-16 18:36:45 +0800 | [diff] [blame] | 918 | if (!stream->set(mode, socket, &remote, codec, sampleRate, sampleCount, |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 919 | codecType, dtmfType)) { |
| 920 | jniThrowException(env, "java/lang/IllegalStateException", |
| 921 | "cannot initialize audio stream"); |
| 922 | goto error; |
| 923 | } |
| 924 | socket = -1; |
Chia-chi Yeh | 4033a67 | 2010-09-16 18:36:45 +0800 | [diff] [blame] | 925 | codec = NULL; |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 926 | |
| 927 | // Create audio group. |
| 928 | group = (AudioGroup *)env->GetIntField(thiz, gNative); |
| 929 | if (!group) { |
| 930 | int mode = env->GetIntField(thiz, gMode); |
| 931 | group = new AudioGroup; |
| 932 | if (!group->set(8000, 256) || !group->setMode(mode)) { |
| 933 | jniThrowException(env, "java/lang/IllegalStateException", |
| 934 | "cannot initialize audio group"); |
| 935 | goto error; |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | // Add audio stream into audio group. |
| 940 | if (!group->add(stream)) { |
| 941 | jniThrowException(env, "java/lang/IllegalStateException", |
| 942 | "cannot add audio stream"); |
| 943 | goto error; |
| 944 | } |
| 945 | |
| 946 | // Succeed. |
| 947 | env->SetIntField(thiz, gNative, (int)group); |
Chia-chi Yeh | b879032 | 2010-08-19 18:26:53 +0800 | [diff] [blame] | 948 | return; |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 949 | |
| 950 | error: |
| 951 | delete group; |
| 952 | delete stream; |
Chia-chi Yeh | 4033a67 | 2010-09-16 18:36:45 +0800 | [diff] [blame] | 953 | delete codec; |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 954 | close(socket); |
| 955 | env->SetIntField(thiz, gNative, NULL); |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | void remove(JNIEnv *env, jobject thiz, jint socket) |
| 959 | { |
| 960 | AudioGroup *group = (AudioGroup *)env->GetIntField(thiz, gNative); |
| 961 | if (group) { |
| 962 | if (socket == -1 || !group->remove(socket)) { |
| 963 | delete group; |
| 964 | env->SetIntField(thiz, gNative, NULL); |
| 965 | } |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | void setMode(JNIEnv *env, jobject thiz, jint mode) |
| 970 | { |
| 971 | AudioGroup *group = (AudioGroup *)env->GetIntField(thiz, gNative); |
| 972 | if (group && !group->setMode(mode)) { |
| 973 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 974 | } |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | void sendDtmf(JNIEnv *env, jobject thiz, jint event) |
| 978 | { |
| 979 | AudioGroup *group = (AudioGroup *)env->GetIntField(thiz, gNative); |
| 980 | if (group && !group->sendDtmf(event)) { |
| 981 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | JNINativeMethod gMethods[] = { |
Chia-chi Yeh | 53aa6ef | 2010-11-30 13:10:31 +0800 | [diff] [blame] | 986 | {"nativeAdd", "(IILjava/lang/String;ILjava/lang/String;I)V", (void *)add}, |
| 987 | {"nativeRemove", "(I)V", (void *)remove}, |
| 988 | {"nativeSetMode", "(I)V", (void *)setMode}, |
| 989 | {"nativeSendDtmf", "(I)V", (void *)sendDtmf}, |
Chia-chi Yeh | 4c5d28c | 2010-08-06 14:12:05 +0800 | [diff] [blame] | 990 | }; |
| 991 | |
| 992 | } // namespace |
| 993 | |
| 994 | int registerAudioGroup(JNIEnv *env) |
| 995 | { |
| 996 | gRandom = open("/dev/urandom", O_RDONLY); |
| 997 | if (gRandom == -1) { |
| 998 | LOGE("urandom: %s", strerror(errno)); |
| 999 | return -1; |
| 1000 | } |
| 1001 | |
| 1002 | jclass clazz; |
| 1003 | if ((clazz = env->FindClass("android/net/rtp/AudioGroup")) == NULL || |
| 1004 | (gNative = env->GetFieldID(clazz, "mNative", "I")) == NULL || |
| 1005 | (gMode = env->GetFieldID(clazz, "mMode", "I")) == NULL || |
| 1006 | env->RegisterNatives(clazz, gMethods, NELEM(gMethods)) < 0) { |
| 1007 | LOGE("JNI registration failed"); |
| 1008 | return -1; |
| 1009 | } |
| 1010 | return 0; |
| 1011 | } |