static void *v4l_thread(void *ptr){ V4lState *s=(V4lState*)ptr; int err=-1; ms_message("v4l_thread starting"); if (s->v4lv2){ #ifdef HAVE_LINUX_VIDEODEV2_H err=v4lv2_do_mmap(s); #endif }else{ err=v4l_do_mmap(s); } if (err<0){ ms_thread_exit(NULL); } while(s->run){ mblk_t *m; #ifdef HAVE_LINUX_VIDEODEV2_H if (s->v4lv2) m=v4lv2_grab_image(s); else #endif m=v4l_grab_image_mmap(s); if (m) { ms_mutex_lock(&s->mutex); putq(&s->rq,dupmsg(m)); ms_mutex_unlock(&s->mutex); } } v4l_do_munmap(s); ms_message("v4l_thread exited."); ms_thread_exit(NULL); }
/*the ticker thread function that executes the filters */ void * ms_ticker_run(void *arg) { uint64_t realtime; int64_t diff; MSTicker *s=(MSTicker*)arg; int lastlate=0; int precision=2; int late; precision = set_high_prio(s); s->ticks=1; ms_mutex_lock(&s->lock); s->orig=s->get_cur_time_ptr(s->get_cur_time_data); while(s->run){ s->ticks++; { #if TICKER_MEASUREMENTS MSTimeSpec begin,end;/*used to measure time spent in processing one tick*/ double iload; ms_get_cur_time(&begin); #endif run_graphs(s,s->execution_list,FALSE); #if TICKER_MEASUREMENTS ms_get_cur_time(&end); iload=100*((end.tv_sec-begin.tv_sec)*1000.0 + (end.tv_nsec-begin.tv_nsec)/1000000.0)/(double)s->interval; s->av_load=(smooth_coef*s->av_load)+((1.0-smooth_coef)*iload); #endif } s->time+=s->interval; while(1){ realtime=s->get_cur_time_ptr(s->get_cur_time_data)-s->orig; ms_mutex_unlock(&s->lock); diff=s->time-realtime; if (diff>0){ /* sleep until next tick */ sleepMs((int)diff); }else{ late=(int)-diff; if (late>s->interval*5 && late>lastlate){ ms_warning("%s: We are late of %d miliseconds.",s->name,late); } lastlate=late; break; /*exit the while loop */ } ms_mutex_lock(&s->lock); } ms_mutex_lock(&s->lock); } ms_mutex_unlock(&s->lock); unset_high_prio(precision); ms_message("%s thread exiting",s->name); ms_thread_exit(NULL); return NULL; }
/*the ticker thread function that executes the filters */ void * ms_ticker_run(void *arg) { MSTicker *s=(MSTicker*)arg; int lastlate=0; int precision=2; int late; precision = set_high_prio(s); s->thread_id = ms_thread_self(); s->ticks=1; s->orig=s->get_cur_time_ptr(s->get_cur_time_data); ms_mutex_lock(&s->lock); while(s->run){ uint64_t late_tick_time=0; s->ticks++; /*Step 1: run the graphs*/ { #if TICKER_MEASUREMENTS MSTimeSpec begin,end;/*used to measure time spent in processing one tick*/ double iload; ms_get_cur_time(&begin); #endif run_tasks(s); run_graphs(s,s->execution_list,FALSE); #if TICKER_MEASUREMENTS ms_get_cur_time(&end); iload=100*((end.tv_sec-begin.tv_sec)*1000.0 + (end.tv_nsec-begin.tv_nsec)/1000000.0)/(double)s->interval; s->av_load=(smooth_coef*s->av_load)+((1.0-smooth_coef)*iload); #endif } ms_mutex_unlock(&s->lock); /*Step 2: wait for next tick*/ s->time+=s->interval; late=s->wait_next_tick(s->wait_next_tick_data,s->time); if (late>s->interval*5 && late>lastlate){ ms_warning("%s: We are late of %d miliseconds.",s->name,late); late_tick_time=ms_get_cur_time_ms(); } lastlate=late; ms_mutex_lock(&s->lock); if (late_tick_time){ s->late_event.lateMs=late; s->late_event.time=late_tick_time; } s->late_event.current_late_ms = late; } ms_mutex_unlock(&s->lock); unset_high_prio(precision); ms_message("%s thread exiting",s->name); ms_thread_exit(NULL); s->thread_id = 0; return NULL; }
static void * ecc_thread(void *p) { EcCalibrator *ecc=(EcCalibrator*)p; ecc_init_filters(ecc); ecc_play_tones(ecc); ecc_deinit_filters(ecc); ms_thread_exit(NULL); return NULL; }
static void *msv4l2_thread(void *ptr){ V4l2State *s=(V4l2State*)ptr; int try=0; ms_message("msv4l2_thread starting"); if (s->fd==-1){ if( msv4l2_open(s)!=0){ ms_warning("msv4l2 could not be openned"); goto close; } } if (!s->configured && msv4l2_configure(s)!=0){ ms_warning("msv4l2 could not be configured"); goto close; } if (msv4l2_do_mmap(s)!=0) { ms_warning("msv4l2 do mmap"); goto close; } ms_message("V4L2 video capture started."); while(s->thread_run) { if (s->fd!=-1){ mblk_t *m; m=v4lv2_grab_image(s,50); if (m){ mblk_t *om=dupmsg(m); mblk_set_marker_info(om,(s->pix_fmt==MS_MJPEG)); ms_mutex_lock(&s->mutex); putq(&s->rq,om); ms_mutex_unlock(&s->mutex); } } } /*dequeue pending buffers so that we can properly unref them (avoids memleak )*/ while(s->queued && try<10){ v4l2_dequeue_ready_buffer(s,50); try++; } if (try==10) ms_warning("msv4l2: buffers not dequeued at exit !"); msv4l2_do_munmap(s); close: msv4l2_close(s); ms_message("msv4l2_thread exited."); ms_thread_exit(NULL); return NULL; } static void msv4l2_preprocess(MSFilter *f){ V4l2State *s=(V4l2State*)f->data; s->thread_run=TRUE; ms_thread_create(&s->thread,NULL,msv4l2_thread,s); s->th_frame_count=-1; s->mean_inter_frame=0; }
static void *v4l_thread(void *ptr){ V4lState *s=(V4lState*)ptr; int err=-1; ms_message("v4l_thread starting"); if (s->v4lv2){ #ifdef HAVE_LINUX_VIDEODEV2_H err=v4lv2_do_mmap(s); #endif }else{ err=v4l_do_mmap(s); } if (err<0){ ms_thread_exit(NULL); } while(s->run){ mblk_t *m; #ifdef HAVE_LINUX_VIDEODEV2_H if (s->v4lv2) m=v4lv2_grab_image(s); else #endif m=v4l_grab_image_mmap(s); if (s->vsize.width!=s->got_vsize.width){ if (m){ /* mblock was allocated by crop or pad! */ ms_mutex_lock(&s->mutex); putq(&s->rq,m); ms_mutex_unlock(&s->mutex); }else{ ms_error("grabbing failed !"); } } else if (m!=NULL) { mblk_t *dm=dupmsg(m); ms_mutex_lock(&s->mutex); putq(&s->rq,dm); ms_mutex_unlock(&s->mutex); } } v4l_do_munmap(s); ms_message("v4l_thread exited."); ms_thread_exit(NULL); return NULL; }
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; } }
static void *msv4l2_thread(void *ptr){ V4l2State *s=(V4l2State*)ptr; int err=-1; ms_message("msv4l2_thread starting"); if (s->fd!=-1) { ms_warning("msv4l2 file descriptor already openned fd:%d",s->fd); goto exit; } if( msv4l2_open(s)!=0){ ms_warning("msv4l2 could not be openned"); goto close; } if (!s->configured && msv4l2_configure(s)!=0){ ms_warning("msv4l2 could not be configured"); goto close; } if (msv4l2_do_mmap(s)!=0) { ms_warning("msv4l2 do mmap"); goto close; } ms_message("V4L2 video capture started."); while(s->thread_run) { mblk_t *m; if (s->fd!=-1){ mblk_t *m; m=v4lv2_grab_image(s); if (m){ mblk_t *om=dupb(m); mblk_set_marker_info(om,(s->pix_fmt==MS_MJPEG)); ms_mutex_lock(&s->mutex); putq(&s->rq,om); ms_mutex_unlock(&s->mutex); } } } ms_message("thread:%d",s->thread_run); munmap: msv4l2_do_munmap(s); close: msv4l2_close(s); exit: ms_message("msv4l2_thread exited."); s->fd = -1; ms_thread_exit(NULL); }
void * ms_ticker_run(void *arg) { uint64_t realtime; int64_t diff; MSTicker *s=(MSTicker*)arg; int lastlate=0; int precision=2; int late; precision = set_high_prio(); s->ticks=1; ms_mutex_lock(&s->lock); s->orig=s->get_cur_time_ptr(s->get_cur_time_data); while(s->run){ s->ticks++; run_graphs(s,s->execution_list,FALSE); s->time+=s->interval; while(1){ realtime=s->get_cur_time_ptr(s->get_cur_time_data)-s->orig; ms_mutex_unlock(&s->lock); diff=s->time-realtime; if (diff>0){ /* sleep until next tick */ sleepMs(diff); }else{ late=-diff; if (late>s->interval*5 && late>lastlate){ ms_warning("We are late of %d miliseconds.",late); } lastlate=late; break; /*exit the while loop */ } ms_mutex_lock(&s->lock); } ms_mutex_lock(&s->lock); } ms_mutex_unlock(&s->lock); unset_high_prio(precision); ms_message("MSTicker thread exiting"); ms_thread_exit(NULL); return NULL; }
static void * vfw_engine_thread(void *arg) { VfwEngine *s=(VfwEngine*)arg; MSG msg; while(s->thread_running) { BOOL fGotMessage; while((fGotMessage = PeekMessage(&msg, (HWND) s->capvideo, 0, 0, PM_REMOVE)) != 0) { TranslateMessage(&msg); DispatchMessage(&msg); } Sleep(10); } ms_thread_exit(NULL); return NULL; }
void * ms_ticker_run(void *arg) { MSTicker *s=(MSTicker*)arg; uint64_t realtime; int precision=2; UINT timerId; precision = set_high_prio(); s->TimeEvent = CreateEvent (NULL, FALSE, FALSE, NULL); s->ticks=1; ms_mutex_lock(&s->lock); s->orig=s->get_cur_time_ptr(s->get_cur_time_data); timerId = timeSetEvent (s->interval, precision, s->TimeEvent, 0, TIME_PERIODIC | TIME_CALLBACK_EVENT_SET); while(s->run){ DWORD err; s->ticks++; run_graphs(s,s->execution_list,FALSE); /* elapsed time since origin */ s->time = s->get_cur_time_ptr(s->get_cur_time_data)- s->orig; ms_mutex_unlock(&s->lock); err = WaitForSingleObject (s->TimeEvent, s->interval*1000 ); /* wake up each diff */ if (err==WAIT_FAILED) ms_message("WaitForSingleObject is failing"); ms_mutex_lock(&s->lock); } ms_mutex_unlock(&s->lock); timeKillEvent (timerId); CloseHandle (s->TimeEvent); s->TimeEvent=NULL; unset_high_prio(precision); ms_message("MSTicker thread exiting"); ms_thread_exit(NULL); return NULL; }
void* monitor(void* s) { UDT_transport *udt = (UDT_transport *)s; UDTSOCKET u = udt->udt_socket; UDT::TRACEINFO perf; ms_message("SendRate(Mb/s)\t\tRTT(ms)\tCWnd\tPktSndPeriod(us)\tRecvACK\tRecvNAK"); while (udt->run) { #ifndef WIN32 sleep(1); #else Sleep(1000); #endif if (UDT::ERROR == UDT::perfmon(u, &perf)) { ms_message("perfmon: %s",UDT::getlasterror().getErrorMessage()); break; } ms_message("%f\t\t%d\t%d\t%f\t\t\t%d\t%d", perf.mbpsSendRate, perf.msRTT, perf.pktCongestionWindow, perf.usPktSndPeriod, perf.pktRecvACK, perf.pktRecvNAK); } ms_thread_exit(NULL); #ifndef WIN32 return NULL; #else return 0; #endif }
static void* msandroid_write_cb(msandroid_sound_write_data* d) { jbyteArray write_buff; jmethodID write_id=0; jmethodID play_id=0; int min_size=-1; int count; int max_size=sndwrite_flush_threshold*(float)d->rate*(float)d->nchannels*2.0; int check_point_size=3*(float)d->rate*(float)d->nchannels*2.0; /*3 seconds*/ int nwrites=0; set_high_prio(); int buff_size = d->write_chunk_size; JNIEnv *jni_env = ms_get_jni_env(); // int write (byte[] audioData, int offsetInBytes, int sizeInBytes) write_id = jni_env->GetMethodID(d->audio_track_class,"write", "([BII)I"); if(write_id==0) { ms_error("cannot find AudioTrack.write() method"); goto end; } play_id = jni_env->GetMethodID(d->audio_track_class,"play", "()V"); if(play_id==0) { ms_error("cannot find AudioTrack.play() method"); goto end; } write_buff = jni_env->NewByteArray(buff_size); uint8_t tmpBuff[buff_size]; //start playing jni_env->CallVoidMethod(d->audio_track,play_id); ms_mutex_lock(&d->mutex); ms_bufferizer_flush(d->bufferizer); ms_mutex_unlock(&d->mutex); while(d->started) { int bufferizer_size; ms_mutex_lock(&d->mutex); min_size=-1; count=0; while((bufferizer_size = ms_bufferizer_get_avail(d->bufferizer)) >= d->write_chunk_size) { if (min_size==-1) min_size=bufferizer_size; else if (bufferizer_size<min_size) min_size=bufferizer_size; ms_bufferizer_read(d->bufferizer, tmpBuff, d->write_chunk_size); ms_mutex_unlock(&d->mutex); jni_env->SetByteArrayRegion(write_buff,0,d->write_chunk_size,(jbyte*)tmpBuff); int result = jni_env->CallIntMethod(d->audio_track,write_id,write_buff,0,d->write_chunk_size); d->writtenBytes+=result; if (result <= 0) { ms_error("write operation has failed [%i]",result); } nwrites++; ms_mutex_lock(&d->mutex); count+=d->write_chunk_size; if (count>check_point_size){ if (min_size > max_size) { ms_warning("we are late, flushing %i bytes",min_size); ms_bufferizer_skip_bytes(d->bufferizer,min_size); } count=0; } } if (d->started) { d->sleeping=true; ms_cond_wait(&d->cond,&d->mutex); d->sleeping=false; } ms_mutex_unlock(&d->mutex); } goto end; end: { ms_thread_exit(NULL); return NULL; } }