static void receiver_process(MSFilter * f) { ReceiverData *d = (ReceiverData *) f->data; mblk_t *m; uint32_t timestamp; if (d->session == NULL) return; if (d->reset_jb){ ms_message("Reseting jitter buffer"); rtp_session_resync(d->session); d->reset_jb=FALSE; } if (d->starting){ PayloadType *pt=rtp_profile_get_payload( rtp_session_get_profile(d->session), rtp_session_get_recv_payload_type(d->session)); if (pt && pt->type!=PAYLOAD_VIDEO) rtp_session_flush_sockets(d->session); d->starting=FALSE; } timestamp = (uint32_t) (f->ticker->time * (d->rate/1000)); while ((m = rtp_session_recvm_with_ts(d->session, timestamp)) != NULL) { mblk_set_timestamp_info(m, rtp_get_timestamp(m)); mblk_set_marker_info(m, rtp_get_markbit(m)); mblk_set_cseq(m, rtp_get_seqnumber(m)); rtp_get_payload(m,&m->b_rptr); ms_queue_put(f->outputs[0], m); } }
static void receiver_process(MSFilter * f) { ReceiverData *d = (ReceiverData *) f->data; mblk_t *m; uint32_t timestamp; if (d->session == NULL) return; timestamp = (uint32_t) (f->ticker->time * (d->rate/1000)); while ((m = rtp_session_recvm_with_ts(d->session, timestamp)) != NULL) { mblk_set_timestamp_info(m, rtp_get_timestamp(m)); mblk_set_marker_info(m, rtp_get_markbit(m)); mblk_set_payload_type(m, rtp_get_payload_type(m)); rtp_get_payload(m,&m->b_rptr); ms_queue_put(f->outputs[0], m); } }
void * ph_audio_in_thread(void *_p) { phmstream_t *stream = (phmstream_t *) _p; int pos_whdr=0; int have_more; int timestamp = 0; int ts_inc; int len; phcodec_t *codec = stream->codec; struct timeval tvBRecv; struct timeval tvARecv; MMRESULT mr = NOERROR; int gotSome = 0; boostPriority(1); while (stream->running) { int length; mblk_t *mp; WAVEHDR *whp = &stream->waveHdrOut[pos_whdr]; ts_inc = 0; /* FIXME How bizarre !!! */ if (stream->payload==3) length = 260; /* 280 ?? */ else length = 160; #if USE_CODECS length = codec->encoded_framesize; #endif /* !USE_CODECS */ gettimeofday(&tvBRecv, NULL); if ( (mp=rtp_session_recvm_with_ts(stream->rtp_session,timestamp))!=NULL) { gettimeofday(&tvARecv, NULL); tvsub(&tvARecv, &tvBRecv); if (tvARecv.tv_usec > 160000) fprintf(stderr, "Rtp session received too long (%i ms)\n", tvARecv.tv_usec / 1000); len=mp->b_cont->b_wptr-mp->b_cont->b_rptr; if (!stream->running) break; #if USE_CODECS len = codec->decode(stream->decoder_ctx, mp->b_cont->b_rptr, len, whp->lpData, 512); freemsg(mp); if (len <= 0) continue; gotSome = 1; ts_inc += len/2; #else /* !USE_CODEC */ if (stream->payload==8) /* A-Law */ alaw_dec(mp->b_cont->b_rptr, whp->lpData, len); if (stream->payload==0) /* Mu-Law */ mulaw_dec(mp->b_cont->b_rptr, whp->lpData, len); ts_inc += len; freemsg(mp); #endif /* !USE_CODECS */ whp->dwBufferLength = len; #if DO_ECHO_CAN_WITHRESET if (stream->pcm_need_resync) { waveInReset(stream->hWaveIn); waveInStart(stream->hWaveIn); } #endif mr = waveOutWrite(stream->hWaveOut, whp, sizeof(*whp)); #if DO_ECHO_CAN store_pcm(stream, whp->lpData, len); #endif /*! DO_ECHO_CAN */ pos_whdr++; if (pos_whdr==USED_OUT_BUFFERS) pos_whdr=0; /* loop over the prepared blocks */ if (mr != MMSYSERR_NOERROR) switch (mr) { case MMSYSERR_INVALHANDLE : fprintf(stderr, "__call_free: waveOutWrite: 0x%i MMSYSERR_INVALHANDLE\n", mr); break; case MMSYSERR_NODRIVER : fprintf(stderr, "__call_free: waveOutWrite: 0x%i MMSYSERR_NODRIVER\n", mr); break; case MMSYSERR_NOMEM : fprintf(stderr, "__call_free: waveOutWrite: 0x%i MMSYSERR_NOMEM\n", mr); break; case WAVERR_UNPREPARED : fprintf(stderr, "__call_free: waveOutWrite: 0x%i WAVERR_UNPREPARED\n", mr); break; case WAVERR_STILLPLAYING : fprintf(stderr, "__call_free: waveOutWrite: 0x%i WAVERR_STILLPLAYING\n", mr); default : fprintf(stderr, "__call_free: waveOutWrite: 0x%i\n", mr); } } else { rtp_session_resync(stream->rtp_session, timestamp); Sleep(10); } timestamp += ts_inc; } return NULL; }
int main(int argc, char **argv) { MastTool* tool = NULL; RtpProfile* profile = &av_profile; PayloadType* pt = NULL; FILE* output = NULL; mblk_t* packet = NULL; int ts_diff = 0; int ts = 0; // Create an RTP session tool = new MastTool( MAST_TOOL_NAME, RTP_SESSION_RECVONLY ); tool->enable_scheduling(); // Parse the command line arguments // and configure the session parse_cmd_line( argc, argv, tool ); // Recieve an initial packet packet = tool->wait_for_rtp_packet(); if (packet == NULL) MAST_FATAL("Failed to receive an initial packet"); // Lookup the payload type pt = rtp_profile_get_payload( profile, rtp_get_payload_type( packet ) ); if (pt == NULL) MAST_FATAL( "Payload type %d isn't registered with oRTP", rtp_get_payload_type( packet ) ); fprintf(stderr, "Payload type: %s\n", payload_type_get_rtpmap( pt )); // Work out the duration of the packet ts_diff = mast_rtp_packet_duration( packet ); MAST_DEBUG("ts_diff = %d", ts_diff); // Open the output file output = open_output_file( g_filename ); if (output==NULL) MAST_FATAL( "failed to open output file" ); // We can free the packet now freemsg( packet ); // Setup signal handlers mast_setup_signals(); // The main loop while( mast_still_running() ) { // Read in a packet packet = rtp_session_recvm_with_ts( tool->get_session(), ts ); if (packet==NULL) { MAST_DEBUG( "packet is NULL" ); } else { int data_len = mast_rtp_packet_size( packet ); if (data_len==0) { MAST_WARNING("Failed to get size of packet's payload"); } else { unsigned char* data_ptr = packet->b_cont->b_rptr; int bytes_written = 0; // Skip the extra header for MPA payload if (rtp_get_payload_type( packet ) == RTP_MPEG_AUDIO_PT) { data_ptr += 4; data_len -= 4; } // Update the timestamp difference ts_diff = mast_rtp_packet_duration( packet ); MAST_DEBUG("ts_diff = %d", ts_diff); MAST_DEBUG("data_len = %d", data_len); // Write to disk bytes_written = fwrite( data_ptr, 1, data_len, output ); if (bytes_written != data_len) { MAST_ERROR("Failed to write data to disk: %s", strerror(errno) ); break; } } } // Increment the timestamp for the next packet ts += ts_diff; } // Close output file if (fclose( output )) { MAST_ERROR("Failed to close output file:\n%s", strerror(errno)); } // Close RTP session delete tool; // Success return 0; }
void *os_sound_start_thread(void *_ca) { jcall_t *ca = (jcall_t*)_ca; char data_in[512]; #ifdef USE_PCM char data_in_dec[1024]; #endif int have_more = 0; int timestamp = 0; int i; mblk_t *mp; struct timeval pkt_in_time, pkt_out_time, sleeptime; struct timespec sleepns; printf("rtp reading thread started\n"); while (ca->enable_audio != -1) { #if !MINIMIZE_COPYING do { memset(data_in, 0, 160); i = rtp_session_recv_with_ts(ca->rtp_session, data_in, 160, timestamp, &have_more); if (i>0) { #ifdef USE_PCM if (ca->payload==8) /* A-Law */ alaw_dec(data_in, data_in_dec, 160); if (ca->payload==0) /* Mu-Law */ mulaw_dec(data_in, data_in_dec, 160); write(fd, data_in_dec, i*2); #else write(fd, data_in, i); #endif } } while(have_more); #else /* MINIMIZE_COPYING */ if ( (mp=rtp_session_recvm_with_ts(ca->rtp_session,timestamp))!=NULL) { int len=mp->b_cont->b_wptr-mp->b_cont->b_rptr; gettimeofday(&pkt_in_time, 0); if (ca->enable_audio != -1) { #ifdef USE_PCM if (ca->payload==8) /* A-Law */ alaw_dec(mp->b_cont->b_rptr, data_in_dec, len); if (ca->payload==0) /* Mu-Law */ mulaw_dec(mp->b_cont->b_rptr, data_in_dec, len); write(fd, data_in_dec, len*2); #else write(fd, mp->b_cont->b_rptr, len); #endif } if (ca->enable_audio == -1) dbgbpt1(); freemsg(mp); gettimeofday(&pkt_out_time, 0); /* compute the time we spent processing the packet */ tvsub(&pkt_out_time, &pkt_in_time); } #endif sleeptime.tv_usec = 10000; sleeptime.tv_sec = 0; tvsub(&sleeptime, &pkt_out_time); TIMEVAL_TO_TIMESPEC(&sleeptime, &sleepns); #if !BLOCKING_MODE if (ca->enable_audio != -1) nanosleep(&sleepns, 0); #endif timestamp += 160; } printf("rtp reading thread stopping\n"); dbgbpt2(); return NULL; }