Changeset 1425:e86a2cee8d4e in mediastreamer2


Ignore:
Timestamp:
Oct 14, 2011 6:10:00 PM (20 months ago)
Author:
Nikita Kozlov <nikita@…>
Branch:
default
Parents:
1424:443b67039206 (diff), 1423:a1f364f0fd13 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Tags:
android_beta
Message:

merge

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/msandroid.cpp

    r1420 r1425  
    2828#include <jni.h> 
    2929 
    30  
    31 static const float sndwrite_flush_threshold=0.050;      //ms 
     30static const float sndwrite_flush_threshold=0.120;      //ms 
    3231 
    3332static void sound_read_setup(MSFilter *f); 
  • src/msandroid.cpp

    r1424 r1425  
    2020 */ 
    2121 
     22#include <sys/times.h> 
     23#include <sys/resource.h> 
    2224#include "mediastreamer2/mssndcard.h" 
    2325#include "mediastreamer2/msfilter.h" 
     
    3133 
    3234static void set_high_prio(void){ 
    33         struct sched_param param; 
    3435        int result=0; 
     36/*      struct sched_param param; 
    3537        memset(&param,0,sizeof(param)); 
    3638        int policy=SCHED_OTHER; 
    37         param.sched_priority=sched_get_priority_max(policy); 
    38         if((result=pthread_setschedparam(pthread_self(),policy, &param))) { 
     39        param.sched_priority=sched_get_priority_max(policy);*/ 
     40        if((result=setpriority(PRIO_PROCESS, gettid(), -19))) { 
    3941                ms_warning("Set sched param failed with error code(%i)\n",result); 
    4042        } else { 
    41                 ms_message("msandroid thread priority set to max"); 
     43                ms_error("msandroid thread priority set to max %x\n", -19); 
    4244        } 
    4345} 
     
    189191        jmethodID read_id=0; 
    190192        jmethodID record_id=0; 
     193        jbyte* buf; 
     194 
    191195 
    192196        set_high_prio(); 
    193197 
    194198        JNIEnv *jni_env = ms_get_jni_env(); 
     199 
     200        ms_debug("read thread cb"); 
    195201        record_id = jni_env->GetMethodID(d->audio_record_class,"startRecording", "()V"); 
    196202        if(record_id==0) { 
     
    208214        } 
    209215 
    210         while (d->started && (nread=jni_env->CallIntMethod(d->audio_record,read_id,d->read_buff,0, d->read_chunk_size))>0) { 
     216        buf = jni_env->GetByteArrayElements(d->read_buff, 0); 
     217 
     218        while (d->started){  
     219                bzero(buf, d->read_chunk_size); 
     220                nread=jni_env->CallIntMethod(d->audio_record,read_id,d->read_buff,0, d->read_chunk_size); 
     221                if (nread <= 0 || nread != d->read_chunk_size) { 
     222                        ms_error("read failed with %d", nread); 
     223                        continue; 
     224                } 
    211225                m = allocb(nread,0); 
    212                 jni_env->GetByteArrayRegion(d->read_buff, 0,nread, (jbyte*)m->b_wptr); 
    213                 //ms_error("%i octets read",nread); 
     226                //jni_env->GetByteArrayRegion(d->read_buff, 0,nread, (jbyte*)m->b_wptr); 
     227                memcpy((jbyte*)m->b_wptr, buf, nread); 
     228        //      ms_error("%i octets read\n begin %x",nread, *m->b_wptr); 
    214229                m->b_wptr += nread; 
    215230                ms_mutex_lock(&d->mutex); 
     
    220235        goto end; 
    221236        end: { 
     237        ms_debug("record thread finished"); 
    222238        ms_thread_exit(NULL); 
    223239        return 0; 
     
    252268        } 
    253269        d->buff_size = jni_env->CallStaticIntMethod(d->audio_record_class,min_buff_size_id,d->rate,2/*CHANNEL_CONFIGURATION_MONO*/,2/*  ENCODING_PCM_16BIT */); 
    254         d->read_chunk_size = d->buff_size/2; 
     270        if (d->buff_size <= 4096) d->buff_size = 4096*3/2; 
     271        d->read_chunk_size = 0.02*(float)d->rate*2.0*(float)d->nchannels; 
     272 
    255273 
    256274        if (d->buff_size > 0) { 
     
    280298        d->audio_record =  jni_env->NewObject(d->audio_record_class 
    281299                        ,constructor_id 
    282                         ,1/*MIC*/ 
     300                        ,7/*MIC*/ 
    283301                        ,d->rate 
    284302                        ,2/*CHANNEL_CONFIGURATION_MONO*/ 
     
    287305 
    288306 
    289         d->audio_record = jni_env->NewGlobalRef(d->audio_record); 
     307        jmethodID  state_method_id = jni_env->GetMethodID(d->audio_record_class,"getState", "()I"); 
     308        int state = jni_env->CallIntMethod(d->audio_record, state_method_id); 
     309        if(state == 0){ /* STATE_UNINITIALIZED */ 
     310                 
     311                jmethodID release_method_id = jni_env->GetMethodID(d->audio_record_class,"release", "()V"); 
     312                jni_env->CallIntMethod(d->audio_record, release_method_id); 
     313                d->audio_record =  jni_env->NewObject(d->audio_record_class 
     314                                ,constructor_id 
     315                                ,1/*MIC*/ 
     316                                ,d->rate 
     317                                ,2/*CHANNEL_CONFIGURATION_MONO*/ 
     318                                ,2/*  ENCODING_PCM_16BIT */ 
     319                                ,d->buff_size); 
     320 
     321                state = jni_env->CallIntMethod(d->audio_record, state_method_id); 
     322 
     323                if (state == 0){ 
     324                        jni_env->CallIntMethod(d->audio_record, release_method_id); 
     325                        ms_error("cannot instanciate audio record"); 
     326                        return; 
     327                } 
     328                ms_debug("audio record ok with dev %d bis", 1); 
     329 
     330        }else 
     331                ms_debug("audio record ok with dev %d", 1); 
     332 
     333 
     334 
    290335        if (d->audio_record == 0) { 
    291336                ms_error("cannot instanciate AudioRecord"); 
    292337                return; 
    293338        } 
     339        d->audio_record = jni_env->NewGlobalRef(d->audio_record); 
    294340 
    295341        d->started=true; 
    296         // start reader thread 
    297         rc = ms_thread_create(&d->thread_id, 0, (void*(*)(void*))msandroid_read_cb, d); 
    298         if (rc){ 
    299                 ms_error("cannot create read thread return code  is [%i]", rc); 
    300                 d->started=false; 
    301         } 
    302342} 
    303343 
     
    316356        JNIEnv *jni_env = ms_get_jni_env(); 
    317357 
     358        d->started = false; 
     359        if (d->thread_id !=0) ms_thread_join(d->thread_id,0); 
     360 
    318361        //stop recording 
    319362        stop_id = jni_env->GetMethodID(d->audio_record_class,"stop", "()V"); 
     
    323366        } 
    324367 
    325         d->started = false; 
    326         if (d->thread_id !=0) ms_thread_join(d->thread_id,0); 
    327  
    328368        if (d->audio_record) { 
    329369                jni_env->CallVoidMethod(d->audio_record,stop_id); 
     
    350390        int nbytes=0.02*(float)d->rate*2.0*(float)d->nchannels; 
    351391 
     392        if (d->thread_id == 0) { 
     393                int rc = ms_thread_create(&d->thread_id, 0, (void*(*)(void*))msandroid_read_cb, d); 
     394                if (rc){ 
     395                        ms_error("cannot create read thread return code  is [%i]", rc); 
     396                        d->started=false; 
     397                } 
     398        } 
     399 
    352400        // output a buffer only every 2 ticks + alpha 
    353401        if ((f->ticker->time % 20)==0 || (f->ticker->time % 510)==0){ 
     402                int err; 
    354403                mblk_t *om=allocb(nbytes,0); 
    355                 int err; 
    356404                ms_mutex_lock(&d->mutex); 
    357405                err=ms_bufferizer_read(&d->rb,om->b_wptr,nbytes); 
     
    465513        int buff_size = d->getWriteBuffSize(); 
    466514        JNIEnv *jni_env = ms_get_jni_env(); 
     515        ms_debug("write thread cb"); 
    467516 
    468517        // int write  (byte[] audioData, int offsetInBytes, int sizeInBytes) 
     
    520569        goto end; 
    521570        end: { 
     571                ms_debug("finishing writer thread"); 
    522572                ms_thread_exit(NULL); 
    523573                return 0; 
     
    526576 
    527577void msandroid_sound_write_preprocess(MSFilter *f){ 
    528         ms_debug("andsnd_write_preprocess"); 
     578        ms_debug("write preprocess started"); 
    529579        msandroid_sound_write_data *d=(msandroid_sound_write_data*)f->data; 
    530580        jmethodID constructor_id=0; 
    531581 
    532         int rc; 
    533582        jmethodID min_buff_size_id; 
    534583 
     
    584633        } 
    585634 
    586  
    587         // start reader thread 
    588635        d->started = true; 
    589         rc = ms_thread_create(&d->thread_id, 0, (void*(*)(void*))msandroid_write_cb, d); 
    590         if (rc){ 
    591                 ms_error("cannot create write thread return code  is [%i]", rc); 
    592                 d->started = false; 
    593                 return; 
    594         } 
    595636} 
    596637 
     
    647688void msandroid_sound_write_process(MSFilter *f){ 
    648689        msandroid_sound_write_data *d=(msandroid_sound_write_data*)f->data; 
     690        mblk_t *m; 
    649691         
    650         mblk_t *m; 
     692        if (d->thread_id == 0) { 
     693                int rc = ms_thread_create(&d->thread_id, 0, (void*(*)(void*))msandroid_write_cb, d); 
     694                if (rc){ 
     695                        ms_error("cannot create write thread return code  is [%i]", rc); 
     696                        d->started = false; 
     697                        return; 
     698                } 
     699        } 
    651700        while((m=ms_queue_get(f->inputs[0]))!=NULL){ 
    652701                if (d->started){ 
Note: See TracChangeset for help on using the changeset viewer.