| 6 |
| 7 | #include "AudioOutputAndroid.h" |
| 8 | #include "../../logging.h" |
| 9 | #include <cstdio> |
| 10 |
| 11 | extern JavaVM* sharedJVM; |
| 12 |
| 13 | using namespace tgvoip; |
| 14 | using namespace tgvoip::audio; |
| 15 |
| 16 | jmethodID AudioOutputAndroid::initMethod = nullptr; |
| 17 | jmethodID AudioOutputAndroid::releaseMethod = nullptr; |
| 18 | jmethodID AudioOutputAndroid::startMethod = nullptr; |
| 19 | jmethodID AudioOutputAndroid::stopMethod = nullptr; |
| 20 | jclass AudioOutputAndroid::jniClass = nullptr; |
| 21 |
| 22 | AudioOutputAndroid::AudioOutputAndroid() |
| 23 | { |
| 24 | JNIEnv* env = nullptr; |
| 25 | bool didAttach = false; |
| 26 | sharedJVM->GetEnv((void**)&env, JNI_VERSION_1_6); |
| 27 | if (!env) |
| 28 | { |
| 29 | sharedJVM->AttachCurrentThread(&env, nullptr); |
| 30 | didAttach = true; |
| 31 | } |
| 32 |
| 33 | jmethodID ctor = env->GetMethodID(jniClass, "<init>", "(J)V"); |
| 34 | jobject obj = env->NewObject(jniClass, ctor, (jlong)(intptr_t)this); |
| 35 | javaObject = env->NewGlobalRef(obj); |
| 36 |
| 37 | env->CallVoidMethod(javaObject, initMethod, 48000, 16, 1, 960 * 2); |
| 38 |
| 39 | if (didAttach) |
| 40 | { |
| 41 | sharedJVM->DetachCurrentThread(); |
| 42 | } |
| 43 | running = false; |
| 44 | } |
| 45 |
| 46 | AudioOutputAndroid::~AudioOutputAndroid() |
| 47 | { |
| 48 | JNIEnv* env = nullptr; |
| 49 | bool didAttach = false; |
| 50 | sharedJVM->GetEnv((void**)&env, JNI_VERSION_1_6); |
| 51 | if (!env) |
| 52 | { |
| 53 | sharedJVM->AttachCurrentThread(&env, nullptr); |
| 54 | didAttach = true; |
| 55 | } |
| 56 |
| 57 | env->CallVoidMethod(javaObject, releaseMethod); |
| 58 | env->DeleteGlobalRef(javaObject); |
| 59 | javaObject = nullptr; |
| 60 |
| 61 | if (didAttach) |
| 62 | { |
| 63 | sharedJVM->DetachCurrentThread(); |
| 64 | } |
| 65 | } |
| 66 |
| 67 | void AudioOutputAndroid::Start() |
| 68 | { |
| 69 | JNIEnv* env = nullptr; |
| 70 | bool didAttach = false; |
| 71 | sharedJVM->GetEnv((void**)&env, JNI_VERSION_1_6); |
| 72 | if (!env) |
| 73 | { |
| 74 | sharedJVM->AttachCurrentThread(&env, nullptr); |
| 75 | didAttach = true; |
| 76 | } |
| 77 |
| 78 | env->CallVoidMethod(javaObject, startMethod); |
| 79 |
| 80 | if (didAttach) |
| 81 | { |
| 82 | sharedJVM->DetachCurrentThread(); |
| 83 | } |
| 84 | running = true; |
| 85 | } |
| 86 |
| 87 | void AudioOutputAndroid::Stop() |
| 88 | { |
| 89 | running = false; |
| 90 | JNIEnv* env = nullptr; |
| 91 | bool didAttach = false; |
| 92 | sharedJVM->GetEnv((void**)&env, JNI_VERSION_1_6); |
| 93 | if (!env) |
| 94 | { |
| 95 | sharedJVM->AttachCurrentThread(&env, nullptr); |
| 96 | didAttach = true; |
| 97 | } |
| 98 |
| 99 | env->CallVoidMethod(javaObject, stopMethod); |
| 100 |
| 101 | if (didAttach) |
| 102 | { |
| 103 | sharedJVM->DetachCurrentThread(); |
| 104 | } |
| 105 | } |
| 106 |
| 107 | void AudioOutputAndroid::HandleCallback(JNIEnv* env, jbyteArray buffer) |
| 108 | { |
| 109 | if (!running) |
| 110 | return; |
| 111 | std::uint8_t* buf = reinterpret_cast<std::uint8_t*>(env->GetByteArrayElements(buffer, nullptr)); |
| 112 | std::size_t len = (std::size_t)env->GetArrayLength(buffer); |
| 113 | InvokeCallback(buf, len); |
| 114 | env->ReleaseByteArrayElements(buffer, (jbyte*)buf, 0); |
| 115 | } |
| 116 |
| 117 | bool AudioOutputAndroid::IsPlaying() |
| 118 | { |
| 119 | return running; |
| 120 | } |
| 121 |