/** * Send pending RTP packets to a session. * * @param loop eventloop * @param w contains the session the RTP session for which to send the packets * @todo implement a saner ratecontrol */ static void rtp_write_cb(struct ev_loop *loop, ev_periodic *w, ATTR_UNUSED int revents) { RTP_session *session = w->data; Resource *resource = session->track->parent; MParserBuffer *buffer = NULL; ev_tstamp next_time = w->offset; #ifdef HAVE_METADATA if (session->metadata) cpd_send(session, now); #endif /* If there is no buffer, it means that either the producer * has been stopped (as we reached the end of stream) or that * there is no data for the consumer to read. If that's the * case we just give control back to the main loop for now. */ if ( bq_consumer_stopped(session->consumer) ) { /* If the producer has been stopped, we send the * finishing packets and go away. */ fnc_log(FNC_LOG_INFO, "[rtp] Stream Finished"); rtcp_send_sr(session, BYE); return; } /* Check whether we have enough extra frames to send. If we have * no extra frames we have a problem, since we're going to send * one packet at least. */ if (resource->eor) fnc_log(FNC_LOG_INFO, "[%s] end of resource %d packets to be fetched", session->track->properties.encoding_name, bq_consumer_unseen(session->consumer)); /* Get the current buffer, if there is enough data */ if ( !(buffer = bq_consumer_get(session->consumer)) ) { /* We wait a bit of time to get the data but before it is * expired. */ if (resource->eor) { fnc_log(FNC_LOG_INFO, "[rtp] Stream Finished"); rtcp_send_sr(session, BYE); return; } next_time += 0.01; // assumed to be enough } else { MParserBuffer *next; double delivery = buffer->delivery; double timestamp = buffer->timestamp; double duration = buffer->duration; gboolean marker = buffer->marker; rtp_packet_send(session, buffer); if (session->pkt_count % 29 == 1) rtcp_send_sr(session, SDES); if (bq_consumer_move(session->consumer)) { next = bq_consumer_get(session->consumer); if(delivery != next->delivery) { if (session->track->properties.media_source == MS_live) next_time += next->delivery - delivery; else next_time = session->range->playback_time - session->range->begin_time + next->delivery; } } else { if (marker) next_time += duration; } fnc_log(FNC_LOG_VERBOSE, "[%s] Now: %5.4f, cur %5.4f[%5.4f][%5.4f], next %5.4f %s\n", session->track->properties.encoding_name, ev_now(loop) - session->range->playback_time, delivery, timestamp, duration, next_time - session->range->playback_time, marker? "M" : " "); } ev_periodic_set(w, next_time, 0, NULL); ev_periodic_again(loop, w); rtp_session_fill(session); }
static gboolean rtp_timer_cb(GEvent *ev, gint revents, void *user_data) { RTP_session *session = (RTP_session*)user_data; Resource *resource = session->track->parent; MParserBuffer *buffer = NULL; ev_tstamp now, sleep_secs; gint to_send = MAX_SND_BLOCKSIZE; now = g_event_time_now_sync(ev); sleep_secs = .01; #ifdef HAVE_METADATA if (session->metadata) cpd_send(session, now); #endif if (!session->audio_rtp && resource->audio_rtp) {//Fixme, race against rtp_session_kill(resource->audio_rtp) session->audio_rtp = rtp_session_ref(resource->audio_rtp); } for (;;) { if (to_send <= 0) { break; } if (bq_consumer_stopped(session->consumer)) { fnc_log(FNC_LOG_INFO, "[rtp] Stream Finished"); rtcp_send_sr(session, BYE); return FALSE; } if (rtp_session_would_block(session)) { break; } if (!(buffer = bq_consumer_get(session->consumer))) { if (resource->eor) { fnc_log(FNC_LOG_INFO, "[rtp] Stream Finished"); rtp_packet_send_eof(session, (time_t)now); rtcp_send_sr(session, BYE); sleep_secs = 1; break; } sleep_secs = .005; break; } if (buffer->mtype != MP_video) { if (session->audio_rtp) { rtp_packet_send_rtp(session->audio_rtp, buffer, (time_t)now); } } else { rtp_packet_send_rtp(session, buffer, (time_t)now); } to_send -= buffer->data_size; if (!bq_consumer_move(session->consumer)) { break; } } g_event_mod_timer_sync(ev, sleep_secs); rtp_session_fill(session); return TRUE; }