Changeset 1425:e86a2cee8d4e in mediastreamer2
- Timestamp:
- Oct 14, 2011 6:10:00 PM (20 months ago)
- 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
- Files:
-
- 2 edited
-
src/msandroid.cpp (modified) (1 diff)
-
src/msandroid.cpp (modified) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/msandroid.cpp
r1420 r1425 28 28 #include <jni.h> 29 29 30 31 static const float sndwrite_flush_threshold=0.050; //ms 30 static const float sndwrite_flush_threshold=0.120; //ms 32 31 33 32 static void sound_read_setup(MSFilter *f); -
src/msandroid.cpp
r1424 r1425 20 20 */ 21 21 22 #include <sys/times.h> 23 #include <sys/resource.h> 22 24 #include "mediastreamer2/mssndcard.h" 23 25 #include "mediastreamer2/msfilter.h" … … 31 33 32 34 static void set_high_prio(void){ 33 struct sched_param param;34 35 int result=0; 36 /* struct sched_param param; 35 37 memset(¶m,0,sizeof(param)); 36 38 int policy=SCHED_OTHER; 37 param.sched_priority=sched_get_priority_max(policy); 38 if((result= pthread_setschedparam(pthread_self(),policy, ¶m))) {39 param.sched_priority=sched_get_priority_max(policy);*/ 40 if((result=setpriority(PRIO_PROCESS, gettid(), -19))) { 39 41 ms_warning("Set sched param failed with error code(%i)\n",result); 40 42 } else { 41 ms_ message("msandroid thread priority set to max");43 ms_error("msandroid thread priority set to max %x\n", -19); 42 44 } 43 45 } … … 189 191 jmethodID read_id=0; 190 192 jmethodID record_id=0; 193 jbyte* buf; 194 191 195 192 196 set_high_prio(); 193 197 194 198 JNIEnv *jni_env = ms_get_jni_env(); 199 200 ms_debug("read thread cb"); 195 201 record_id = jni_env->GetMethodID(d->audio_record_class,"startRecording", "()V"); 196 202 if(record_id==0) { … … 208 214 } 209 215 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 } 211 225 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); 214 229 m->b_wptr += nread; 215 230 ms_mutex_lock(&d->mutex); … … 220 235 goto end; 221 236 end: { 237 ms_debug("record thread finished"); 222 238 ms_thread_exit(NULL); 223 239 return 0; … … 252 268 } 253 269 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 255 273 256 274 if (d->buff_size > 0) { … … 280 298 d->audio_record = jni_env->NewObject(d->audio_record_class 281 299 ,constructor_id 282 , 1/*MIC*/300 ,7/*MIC*/ 283 301 ,d->rate 284 302 ,2/*CHANNEL_CONFIGURATION_MONO*/ … … 287 305 288 306 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 290 335 if (d->audio_record == 0) { 291 336 ms_error("cannot instanciate AudioRecord"); 292 337 return; 293 338 } 339 d->audio_record = jni_env->NewGlobalRef(d->audio_record); 294 340 295 341 d->started=true; 296 // start reader thread297 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 }302 342 } 303 343 … … 316 356 JNIEnv *jni_env = ms_get_jni_env(); 317 357 358 d->started = false; 359 if (d->thread_id !=0) ms_thread_join(d->thread_id,0); 360 318 361 //stop recording 319 362 stop_id = jni_env->GetMethodID(d->audio_record_class,"stop", "()V"); … … 323 366 } 324 367 325 d->started = false;326 if (d->thread_id !=0) ms_thread_join(d->thread_id,0);327 328 368 if (d->audio_record) { 329 369 jni_env->CallVoidMethod(d->audio_record,stop_id); … … 350 390 int nbytes=0.02*(float)d->rate*2.0*(float)d->nchannels; 351 391 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 352 400 // output a buffer only every 2 ticks + alpha 353 401 if ((f->ticker->time % 20)==0 || (f->ticker->time % 510)==0){ 402 int err; 354 403 mblk_t *om=allocb(nbytes,0); 355 int err;356 404 ms_mutex_lock(&d->mutex); 357 405 err=ms_bufferizer_read(&d->rb,om->b_wptr,nbytes); … … 465 513 int buff_size = d->getWriteBuffSize(); 466 514 JNIEnv *jni_env = ms_get_jni_env(); 515 ms_debug("write thread cb"); 467 516 468 517 // int write (byte[] audioData, int offsetInBytes, int sizeInBytes) … … 520 569 goto end; 521 570 end: { 571 ms_debug("finishing writer thread"); 522 572 ms_thread_exit(NULL); 523 573 return 0; … … 526 576 527 577 void msandroid_sound_write_preprocess(MSFilter *f){ 528 ms_debug(" andsnd_write_preprocess");578 ms_debug("write preprocess started"); 529 579 msandroid_sound_write_data *d=(msandroid_sound_write_data*)f->data; 530 580 jmethodID constructor_id=0; 531 581 532 int rc;533 582 jmethodID min_buff_size_id; 534 583 … … 584 633 } 585 634 586 587 // start reader thread588 635 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 }595 636 } 596 637 … … 647 688 void msandroid_sound_write_process(MSFilter *f){ 648 689 msandroid_sound_write_data *d=(msandroid_sound_write_data*)f->data; 690 mblk_t *m; 649 691 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 } 651 700 while((m=ms_queue_get(f->inputs[0]))!=NULL){ 652 701 if (d->started){
Note: See TracChangeset
for help on using the changeset viewer.
