Example #1
0
/* API: Init factory */
static pj_status_t android_init(pjmedia_aud_dev_factory *f)
{
	int err;

	PJ_UNUSED_ARG(f);

	PJ_LOG(4,(THIS_FILE, "Android sound library initialized"));
	PJ_LOG(4,(THIS_FILE, "Sound device count=%d", android_get_dev_count(f)));

	return PJ_SUCCESS;
}
/* API: Init factory */
static pj_status_t android_init(pjmedia_aud_dev_factory *f)
{
    int mic_source;
    int state = 0;
    jthrowable exc;

    PJ_UNUSED_ARG(f);

    PJ_LOG(4,(THIS_FILE, "Android sound library initialized"));
    PJ_LOG(4,(THIS_FILE, "Sound device count=%d", android_get_dev_count(f)));

    JNIEnv *jni_env = 0;
    ATTACH_JVM(jni_env);
    jmethodID constructor_method = 0, method_id = 0;

    g_record_class = (jclass)jni_env->NewGlobalRef(jni_env->FindClass("android/media/AudioRecord"));
    if (g_record_class == 0)
    {
        PJ_LOG(2, (THIS_FILE, "zzc Not able to find audio record class"));
        goto on_error;
    }
    //Get pointer to the constructor
    constructor_method = jni_env->GetMethodID(g_record_class,"<init>", "(IIIII)V");
    if (constructor_method == 0)
    {
        PJ_LOG(2, (THIS_FILE, "zzc Not able to find audio record class constructor"));
        goto on_error;
    }

    mic_source = on_set_micro_source_wrapper();
    if(mic_source == 0)
    {
        mic_source = 1;
        char sdk_version[PROP_VALUE_MAX];
        __system_property_get("ro.build.version.sdk", sdk_version);

        pj_str_t pj_sdk_version = pj_str(sdk_version);
        int sdk_v = pj_strtoul(&pj_sdk_version);
        if(sdk_v >= 10)
        {
            mic_source = 7;
        }
    }
    PJ_LOG(3, (THIS_FILE, "zzc Use micro source : %d", mic_source));

    g_audio_record = jni_env->NewObject(g_record_class, constructor_method,
        1, // Mic input source:  1 = MIC / 7 = VOICE_COMMUNICATION
        16000,
        //2, // CHANNEL_CONFIGURATION_MONO
        16, // lxd CHANNEL_IN_MONO
        2,
        //6144
        16000//lxd
        );
    if (g_audio_record == 0)
    {
        PJ_LOG(1, (THIS_FILE, "zzc Not able to instantiate record class"));
        goto on_error;
    }
    exc = jni_env->ExceptionOccurred();
    if (exc)
    {
        jni_env->ExceptionDescribe();
        jni_env->ExceptionClear();
        PJ_LOG(2, (THIS_FILE, "zzc The micro source was probably not valid"));
        // Try to fallback on MIC source -- lazy failure
        if(mic_source != 1)
        {
            PJ_LOG(4, (THIS_FILE, "zzc Try default source"));
            g_audio_record = jni_env->NewObject(g_record_class, constructor_method,
            		1, // Mic input source:  1 = MIC / 7 = VOICE_COMMUNICATION
					16000,
					//2, // CHANNEL_CONFIGURATION_MONO
					16, // lxd CHANNEL_IN_MONO
					2,
					//6144
					16000//lxd
					);
            if (g_audio_record == 0)
            {
                PJ_LOG(1, (THIS_FILE, "zzc Not able to instantiate record class"));
                goto on_error;
            }
        }
        else
        {
            PJ_LOG(1, (THIS_FILE, "zzc Not able to instantiate record class"));
            goto on_error;
        }
    }
    // Check state
    method_id = jni_env->GetMethodID(g_record_class,"getState", "()I");
    state = jni_env->CallIntMethod(g_audio_record, method_id);
    if(state == 0){ /* STATE_UNINITIALIZED */
        // Try to fallback on MIC source -- lazy failure
        if(mic_source != 1){
            PJ_LOG(4, (THIS_FILE, "Try default source"));
            g_audio_record =  jni_env->NewObject(g_record_class, constructor_method,
            		1, // Mic input source:  1 = MIC / 7 = VOICE_COMMUNICATION
					16000,
					//2, // CHANNEL_CONFIGURATION_MONO
					16, // lxd CHANNEL_IN_MONO
					2,
					//6144
					16000//lxd
					);
            if (g_audio_record == 0) {
                PJ_LOG(1, (THIS_FILE, "Not able to instantiate record class"));
                goto on_error;
            }
        }else{
            PJ_LOG(1, (THIS_FILE, "Not able to instantiate record class"));
            goto on_error;
        }
    }

    g_audio_record = jni_env->NewGlobalRef(g_audio_record);

    return PJ_SUCCESS;

on_error:

    on_teardown_audio_wrapper();
    DETACH_JVM(jni_env);
    return PJMEDIA_ESNDINDEVID;
}