コード例 #1
0
ファイル: alsa.c プロジェクト: TheBaobabTeam/linphone-android
void alsa_read_process(MSFilter *obj){
	AlsaReadData *ad=(AlsaReadData*)obj->data;
	int samples=(128*ad->rate)/8000;
	int err;
	mblk_t *om=NULL;
	if (ad->handle==NULL && ad->pcmdev!=NULL && !ad->read_started){
		ad->read_started=TRUE;
		ad->handle=alsa_open_r(ad->pcmdev,16,ad->nchannels==2,ad->rate);
		if (ad->handle){
			ad->read_samples=0;
			ms_ticker_set_time_func(obj->ticker,(uint64_t (*)(void*))ms_ticker_synchronizer_get_corrected_time, ad->ticker_synchronizer);
		}
	}
	if (ad->handle==NULL) return;
	while (alsa_can_read(ad->handle)>=samples){

		int size=samples*2*ad->nchannels;
		om=allocb(size,0);
		if ((err=alsa_read(ad->handle,om->b_wptr,samples))<=0) {
			ms_warning("Fail to read samples");
			freemsg(om);
			return;
		}
		ad->read_samples+=err;
		size=err*2*ad->nchannels;
		om->b_wptr+=size;
		compute_timespec(ad);

		/*ms_message("alsa_read_process: Outputing %i bytes",size);*/
		ms_queue_put(obj->outputs[0],om);
	}
}
コード例 #2
0
static void* msandroid_read_cb(msandroid_sound_read_data* d) {
    mblk_t *m;
    int nread;
    jmethodID read_id=0;
    jmethodID record_id=0;

    set_high_prio();

    //JNIEnv *jni_env = ms_get_jni_env();
    JNIEnv *jni_env = NULL;
    JavaVM *jvm = ms_get_jvm();
    if (jvm->AttachCurrentThread(&jni_env, NULL)!=0) {
        ms_fatal("AttachCurrentThread() failed !");
        goto end;
    }
    record_id = jni_env->GetMethodID(d->audio_record_class,"startRecording", "()V");
    if(record_id==0) {
        ms_error("cannot find AudioRecord.startRecording() method");
        goto end;
    }
    //start recording
    ms_message("Start recording");
    jni_env->CallVoidMethod(d->audio_record,record_id);

    // int read (byte[] audioData, int offsetInBytes, int sizeInBytes)
    read_id = jni_env->GetMethodID(d->audio_record_class,"read", "([BII)I");
    if(read_id==0) {
        ms_error("cannot find AudioRecord.read() method");
        goto end;
    }

    while (d->started && (nread=jni_env->CallIntMethod(d->audio_record,read_id,d->read_buff,0, d->read_chunk_size))>0) {
        m = allocb(nread,0);
        jni_env->GetByteArrayRegion(d->read_buff, 0,nread, (jbyte*)m->b_wptr);
        //ms_error("%i octets read",nread);
        m->b_wptr += nread;
        d->read_samples+=nread/(2*d->nchannels);
        compute_timespec(d);
        ms_mutex_lock(&d->mutex);
        ms_bufferizer_put (&d->rb,m);
        ms_mutex_unlock(&d->mutex);
    };

    goto end;
end: {
        jvm->DetachCurrentThread();
        ms_thread_exit(NULL);
        return 0;
    }
}