Esempio n. 1
0
/**
 * 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);
}
Esempio n. 2
0
static gboolean
rtp_lwrite_cb(GEvent *ev, gint revents, void *user_data)
{
#define XBUFLEN   256
	RTP_session *rtp_s = (RTP_session*)user_data;
	Resource *resource = rtp_s->track->parent;
	MParserBuffer *buffer;
	gchar x[XBUFLEN];
	gint n, fd = g_event_fd(ev);
	ev_tstamp now;

	while ( TRUE )
	{
		n = read(fd, x, XBUFLEN);
		if (n > 0)
			continue;
			
		if (n < 0 && errno == EAGAIN)
			break;

		rc_log(
			RC_LOG_WARN,
			"[rtp] writer stopped, something wrong with rtp->fd[0]."
		);

		rtcp_send_sr(rtp_s, BYE);
		return FALSE;
	}	

	if (G_UNLIKELY(bq_consumer_stopped(rtp_s->consumer)))
	{
	    rc_log(FNC_LOG_INFO, "[rtp] one live stream finished.");
		rtcp_send_sr(rtp_s, BYE);
	    return FALSE;
	}

	while ( TRUE )
	{	
		if (rtp_session_would_block(rtp_s))
		{
			break;
		}

		if (!(buffer = bq_consumer_get(rtp_s->consumer)))
		{
			if (resource->eor)
	        {
	            fnc_log(FNC_LOG_INFO, "[rtp] Stream Finished"); 
				rtp_packet_send_eof(rtp_s, (time_t)now);
				rtcp_send_sr(rtp_s, BYE);
	      		return FALSE;
	        }
			break;
		}

		if (buffer == LS_EOF)
		{
            fnc_log(FNC_LOG_INFO, "[rtp] Stream Finished"); 
			rtp_packet_send_eof(rtp_s, (time_t)now);
			rtcp_send_sr(rtp_s, BYE);
      		return FALSE;					
		}

		now = g_event_time_now_sync(ev);
		rtp_packet_send(rtp_s, buffer, (time_t)now);

		if (!bq_consumer_move(rtp_s->consumer))
			break;
	}

	return TRUE;
}