Example #1
0
void media_input_fifo_flush(media_input_fifo_t media_input_fifo0)
{
	while (!media_input_fifo_empty(media_input_fifo0)) {
		media_input_fifo_release_packet(media_input_fifo0);
	}
}
/** This receives user defined audio samples from local out stream and packetize
 *  them into specified AVB1722 transport packet.
 */
int avb1722_create_packet(unsigned char Buf0[],
		avb1722_Talker_StreamConfig_t *stream_info,
		ptp_time_info_mod64 *timeInfo,
		int time)
{
	int stream_id0 = stream_info->streamId[0];
	media_input_fifo_t *map = stream_info->map;

	// align packet 2 chars into the buffer so that samples are
	// word align for fast copying.
	unsigned char *Buf = &Buf0[2];
	unsigned int *dest = (unsigned int *) &Buf[(AVB_ETHERNET_HDR_SIZE + AVB_TP_HDR_SIZE + AVB_CIP_HDR_SIZE)];

	int dbc = stream_info->dbc_at_start_of_last_fifo_packet;
	int pkt_data_length = 0;

	// Check to see if there is something that can be transmitted.  If there is not, then we give up
	// transmitting this packet, because there may be other streams serviced by this thread which
	// can be serviced.  Since a packet on the wire is always shorter than a packet in the fifo,
	// we know that having a packet in the fifo is enough to transmit one on the wire.
	//
	// There is a slight issue here, that because wire packets are potentially shorter than fifo
	// packets, that we will occasionally not transmit when we could do. The period of this is
	// 1/(ceil(rate/8000)-(rate/8000))
    if (media_input_fifo_empty(map[0])) return 0;

	// Figure out if it is time to transmit a packet
	if (!stream_info->transmit_ok) {
		int elapsed = time - stream_info->last_transmit_time;
		if (elapsed < AVB1722_PACKET_PERIOD_TIMER_TICKS)
			return 0;

		stream_info->transmit_ok = 1;
	}


	// For all of the packets in the FIFO
	for (unsigned n=0; n<MAX_TS_PACKETS_PER_1722 && !media_input_fifo_empty(map[0]); ++n) {
		unsigned * data = media_input_fifo_get_packet(map[0]);

		// Adjust the timestamp from local to PTP
		*dest++ = local_timestamp_to_ptp_mod32(data[0], timeInfo) + stream_info->presentation_delay;

		// Copy the whole TS packet
		for (unsigned c=1; c<(192/4); ++c) {
			*dest++ = data[c];
		}

		// Update length
		pkt_data_length += 192;
		stream_info->dbc_at_start_of_last_fifo_packet += 8;

		media_input_fifo_release_packet(map[0]);
	}

	AVB1722_CIP_HeaderGen(Buf, dbc);

	// Update timestamp value and valid flag.
	AVB1722_AVBTP_HeaderGen(Buf, 0, 0, pkt_data_length, stream_info->sequence_number, stream_id0);

	stream_info->last_transmit_time = time;
	stream_info->transmit_ok = 0;
	stream_info->sequence_number++;

	return (AVB_ETHERNET_HDR_SIZE + AVB_TP_HDR_SIZE + AVB_CIP_HDR_SIZE + pkt_data_length);
}