view test-v22/rtp_tx.c @ 14:f96153d15889

sipout-test-voice: implement play from file
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 11 May 2024 22:10:38 -0800
parents 3c5734b88c20
children
line wrap: on
line source

/*
 * In this module we implement outgoing RTP stream generation.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <spandsp.h>
#include "../include/tmgw_const.h"
#include "../include/rtp_defs.h"
#include "../include/pstn_defs.h"
#include "../libutil/osmo_bits.h"

extern struct sockaddr_in rtp_local_addr, rtp_remote_addr;
extern int rtp_udp_fd, rtcp_udp_fd, pcma_selected;
extern struct timeval cur_event_time;

static uint32_t rtp_ssrc;
static uint32_t rtp_out_ts;
static uint16_t rtp_out_seq;

static g711_state_t *g711_enc_state;

void
assign_rtpout_ssrc()
{
	rtp_ssrc = cur_event_time.tv_sec ^ cur_event_time.tv_usec ^ getpid();
}

void
init_pcm_tx()
{
	g711_enc_state = g711_init(NULL, pcma_selected ? G711_ALAW : G711_ULAW);
	if (!g711_enc_state) {
		fprintf(stderr, "error: g711_init() failed!\n");
		exit(1);
	}
}

void
generate_rtp_packet()
{
	struct rtp_packet pkt;
	socklen_t addrlen;
	int16_t linear[FRAME_20MS];

	pkt.v_p_x_cc = 0x80;
	pkt.m_pt = pcma_selected ? PSTN_CODEC_PCMA : PSTN_CODEC_PCMU;
	pkt.seq = htons(rtp_out_seq++);
	pkt.tstamp = htonl(rtp_out_ts);
	rtp_out_ts += FRAME_20MS;
	pkt.ssrc = rtp_ssrc;
	fill_tx_frame(linear);
	g711_encode(g711_enc_state, pkt.payload, linear, FRAME_20MS);
	addrlen = sizeof(struct sockaddr_in);
	sendto(rtp_udp_fd, &pkt, RTP_PACKET_SIZE_PSTN, 0,
		(struct sockaddr *) &rtp_remote_addr, addrlen);
}