/** * @brief Resume (or start) an RTP session * * @param session_gen The session to resume or start * @param range_gen Pointer tp @ref RTSP_Range to start with * * @todo This function should probably take care of starting eventual * libev events when the scheduler is replaced. * * This function is used by the PLAY method of RTSP to start or resume * a session; since a newly-created session starts as paused, this is * the only method available. * * The use of a pointer to double rather than a double itself is to * make it possible to pass this function straight to foreach * functions from glib. * * @internal This function should only be called from g_slist_foreach. */ static void rtp_session_resume(gpointer session_gen, gpointer range_gen) { RTP_session *session = (RTP_session*)session_gen; RTSP_Range *range = (RTSP_Range*)range_gen; fnc_log(FNC_LOG_VERBOSE, "Resuming session %p\n", session); session->range = range; session->start_seq = 1 + session->seq_no; session->start_rtptime = g_random_int(); session->send_time = 0.0; session->last_packet_send_time = time(NULL); /* Create the new thread pool for the read requests */ session->fill_pool = g_thread_pool_new(rtp_session_fill_cb, session, 1, true, NULL); /* Prefetch frames */ rtp_session_fill(session); ev_periodic_set(&session->transport.rtp_writer, range->playback_time - 0.05, 0, NULL); ev_periodic_start(session->srv->loop, &session->transport.rtp_writer); ev_io_start(session->srv->loop, &session->transport.rtcp_reader); }
static __inline__ void rtp_session_resume_stored(RTP_session *rtp_s) { GEvent *periodic; if (rtp_s->playing) return; if (rtp_s->track && rtp_s->track->properties.media_type != MP_video) {//只创建一路audio是一个bug. rtp_s->track->parent->audio_rtp = rtp_s; return; } if (!rtp_s->fill_pool) { rtp_s->fill_pool = g_thread_pool_new(rtp_session_fill_cb, rtp_s, 1, TRUE, NULL); rtp_session_fill(rtp_s); } if (!rtp_s->transport.rtp_writer) { periodic = g_event_new(sizeof(RTP_writer), -1, 0); g_event_set_timeout(periodic, 1); g_event_set_callback(periodic, rtp_timer_cb, rtp_s, on_rtp_periodic_destroy); rtp_s->transport.rtp_writer = periodic; ((RTP_writer*)periodic)->rtp_s = rtp_s; rtp_session_ref(rtp_s); g_scheduler_add(periodic, LOOP_WEIGHT_VIDEO); } rtp_s->playing = TRUE; }
/** * 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; }