Commit ed525406 authored by sletz's avatar sletz
Browse files

Beautifull code.

git-svn-id: http://subversion.jackaudio.org/jack/jack2/trunk/jackmp@4230 0c269be4-1314-0410-8aa9-9f06e86f4224
parent 68cc7070
......@@ -130,7 +130,7 @@ int get_sample_size (int bitdepth)
//JN: why? is this for buffer sizes before or after encoding?
//JN: if the former, why not int16_t, if the latter, shouldn't it depend on -c N?
if( bitdepth == CELT_MODE )
return sizeof( unsigned char );
return sizeof( unsigned char );
return sizeof (int32_t);
}
......@@ -328,7 +328,6 @@ cache_packet_add_fragment (cache_packet *pack, char *packet_buf, int rcv_len)
return;
}
if (fragment_nr == 0)
{
memcpy (pack->packet_buf, packet_buf, rcv_len);
......@@ -374,7 +373,6 @@ netjack_poll_deadline (int sockfd, jack_time_t deadline)
int timeout;
#endif
jack_time_t now = jack_get_time();
if( now >= deadline )
return 0;
......@@ -389,7 +387,6 @@ netjack_poll_deadline (int sockfd, jack_time_t deadline)
timeout = lrintf( (float)(deadline - now) / 1000.0 );
#endif
fds.fd = sockfd;
fds.events = POLLIN;
......@@ -600,24 +597,24 @@ packet_cache_retreive_packet_pointer( packet_cache *pcache, jack_nframes_t frame
for (i = 0; i < pcache->size; i++) {
if (pcache->packets[i].valid && (pcache->packets[i].framecnt == framecnt)) {
cpack = &(pcache->packets[i]);
cpack = &(pcache->packets[i]);
break;
}
}
}
if( cpack == NULL ) {
//printf( "retreive packet: %d....not found\n", framecnt );
return -1;
//printf( "retreive packet: %d....not found\n", framecnt );
return -1;
}
if( !cache_packet_is_complete( cpack ) ) {
return -1;
return -1;
}
// ok. cpack is the one we want and its complete.
*packet_buf = cpack->packet_buf;
if( timestamp )
*timestamp = cpack->recv_timestamp;
*timestamp = cpack->recv_timestamp;
pcache->last_framecnt_retreived_valid = 1;
pcache->last_framecnt_retreived = framecnt;
......@@ -634,18 +631,18 @@ packet_cache_release_packet( packet_cache *pcache, jack_nframes_t framecnt )
for (i = 0; i < pcache->size; i++) {
if (pcache->packets[i].valid && (pcache->packets[i].framecnt == framecnt)) {
cpack = &(pcache->packets[i]);
cpack = &(pcache->packets[i]);
break;
}
}
}
if( cpack == NULL ) {
//printf( "retreive packet: %d....not found\n", framecnt );
return -1;
//printf( "retreive packet: %d....not found\n", framecnt );
return -1;
}
if( !cache_packet_is_complete( cpack ) ) {
return -1;
return -1;
}
cache_packet_reset (cpack);
......@@ -661,13 +658,13 @@ packet_cache_get_fill( packet_cache *pcache, jack_nframes_t expected_framecnt )
for (i = 0; i < pcache->size; i++)
{
cache_packet *cpack = &(pcache->packets[i]);
cache_packet *cpack = &(pcache->packets[i]);
if (cpack->valid && cache_packet_is_complete( cpack ))
if( cpack->framecnt >= expected_framecnt )
num_packets_before_us += 1;
if( cpack->framecnt >= expected_framecnt )
num_packets_before_us += 1;
}
return 100.0 * (float)num_packets_before_us / (float)( pcache->size ) ;
return 100.0 * (float)num_packets_before_us / (float)( pcache->size );
}
// Returns 0 when no valid packet is inside the cache.
......@@ -680,29 +677,29 @@ packet_cache_get_next_available_framecnt( packet_cache *pcache, jack_nframes_t e
for (i = 0; i < pcache->size; i++)
{
cache_packet *cpack = &(pcache->packets[i]);
//printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
cache_packet *cpack = &(pcache->packets[i]);
//printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
if (!cpack->valid || !cache_packet_is_complete( cpack )) {
//printf( "invalid\n" );
continue;
}
//printf( "invalid\n" );
continue;
}
if( cpack->framecnt < expected_framecnt )
continue;
if( cpack->framecnt < expected_framecnt )
continue;
if( (cpack->framecnt - expected_framecnt) > best_offset ) {
continue;
if( (cpack->framecnt - expected_framecnt) > best_offset ) {
continue;
}
best_offset = cpack->framecnt - expected_framecnt;
retval = 1;
if( best_offset == 0 )
if (best_offset == 0)
break;
}
if( retval && framecnt )
*framecnt = expected_framecnt + best_offset;
if (retval && framecnt)
*framecnt = expected_framecnt + best_offset;
return retval;
}
......@@ -716,12 +713,12 @@ packet_cache_get_highest_available_framecnt( packet_cache *pcache, jack_nframes_
for (i = 0; i < pcache->size; i++)
{
cache_packet *cpack = &(pcache->packets[i]);
//printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
cache_packet *cpack = &(pcache->packets[i]);
//printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
if (!cpack->valid || !cache_packet_is_complete( cpack )) {
//printf( "invalid\n" );
continue;
//printf( "invalid\n" );
continue;
}
if (cpack->framecnt < best_value) {
......@@ -732,8 +729,8 @@ packet_cache_get_highest_available_framecnt( packet_cache *pcache, jack_nframes_
retval = 1;
}
if( retval && framecnt )
*framecnt = best_value;
if (retval && framecnt)
*framecnt = best_value;
return retval;
}
......@@ -748,15 +745,15 @@ packet_cache_find_latency( packet_cache *pcache, jack_nframes_t expected_framecn
for (i = 0; i < pcache->size; i++)
{
cache_packet *cpack = &(pcache->packets[i]);
//printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
cache_packet *cpack = &(pcache->packets[i]);
//printf( "p%d: valid=%d, frame %d\n", i, cpack->valid, cpack->framecnt );
if (!cpack->valid || !cache_packet_is_complete( cpack )) {
//printf( "invalid\n" );
continue;
//printf( "invalid\n" );
continue;
}
if( (cpack->framecnt - expected_framecnt) < best_offset ) {
if ((cpack->framecnt - expected_framecnt) < best_offset) {
continue;
}
......@@ -766,8 +763,8 @@ packet_cache_find_latency( packet_cache *pcache, jack_nframes_t expected_framecn
if( best_offset == 0 )
break;
}
if( retval && framecnt )
*framecnt = JACK_MAX_FRAMES - best_offset;
if (retval && framecnt)
*framecnt = JACK_MAX_FRAMES - best_offset;
return retval;
}
......@@ -786,18 +783,18 @@ netjack_sendto (int sockfd, char *packet_buf, int pkt_size, int flags, struct so
int fragment_payload_size = mtu - sizeof (jacknet_packet_header);
if (pkt_size <= mtu) {
int err;
pkthdr = (jacknet_packet_header *) packet_buf;
int err;
pkthdr = (jacknet_packet_header *) packet_buf;
pkthdr->fragment_nr = htonl (0);
err = sendto(sockfd, packet_buf, pkt_size, flags, addr, addr_size);
if( err<0 ) {
//printf( "error in send\n" );
perror( "send" );
}
if( err<0 ) {
//printf( "error in send\n" );
perror( "send" );
}
}
else
{
int err;
int err;
// Copy the packet header to the tx pack first.
memcpy(tx_packet, packet_buf, sizeof (jacknet_packet_header));
......@@ -819,14 +816,13 @@ netjack_sendto (int sockfd, char *packet_buf, int pkt_size, int flags, struct so
// sendto(last_pack_size);
err = sendto(sockfd, tx_packet, last_payload_size + sizeof(jacknet_packet_header), flags, addr, addr_size);
if( err<0 ) {
//printf( "error in send\n" );
perror( "send" );
}
if( err<0 ) {
//printf( "error in send\n" );
perror( "send" );
}
}
}
void
decode_midi_buffer (uint32_t *buffer_uint32, unsigned int buffer_size_uint32, jack_default_audio_sample_t* buf)
{
......@@ -907,8 +903,8 @@ render_payload_to_jack_ports_float ( void *packet_payload, jack_nframes_t net_pe
uint32_t *packet_bufX = (uint32_t *)packet_payload;
if( !packet_payload )
return;
if (!packet_payload)
return;
while (node != NULL)
{
......@@ -951,19 +947,19 @@ render_payload_to_jack_ports_float ( void *packet_payload, jack_nframes_t net_pe
else
#endif
{
if( dont_htonl_floats )
{
memcpy( buf, packet_bufX, net_period_down*sizeof(jack_default_audio_sample_t));
}
else
{
for (i = 0; i < net_period_down; i++)
{
val.i = packet_bufX[i];
val.i = ntohl (val.i);
buf[i] = val.f;
}
}
if( dont_htonl_floats )
{
memcpy( buf, packet_bufX, net_period_down*sizeof(jack_default_audio_sample_t));
}
else
{
for (i = 0; i < net_period_down; i++)
{
val.i = packet_bufX[i];
val.i = ntohl (val.i);
buf[i] = val.f;
}
}
}
}
else if (jack_port_is_midi (porttype))
......@@ -1031,19 +1027,19 @@ render_jack_ports_to_payload_float (JSList *playback_ports, JSList *playback_src
else
#endif
{
if( dont_htonl_floats )
{
memcpy( packet_bufX, buf, net_period_up*sizeof(jack_default_audio_sample_t) );
}
else
{
for (i = 0; i < net_period_up; i++)
{
val.f = buf[i];
val.i = htonl (val.i);
packet_bufX[i] = val.i;
}
}
if( dont_htonl_floats )
{
memcpy( packet_bufX, buf, net_period_up*sizeof(jack_default_audio_sample_t) );
}
else
{
for (i = 0; i < net_period_up; i++)
{
val.f = buf[i];
val.i = htonl (val.i);
packet_bufX[i] = val.i;
}
}
}
}
else if (jack_port_is_midi (porttype))
......@@ -1218,8 +1214,8 @@ render_payload_to_jack_ports_8bit (void *packet_payload, jack_nframes_t net_peri
int8_t *packet_bufX = (int8_t *)packet_payload;
if( !packet_payload )
return;
if (!packet_payload)
return;
while (node != NULL)
{
......
......@@ -139,24 +139,24 @@ alloc_ports (int n_capture_audio, int n_playback_audio, int n_capture_midi, int
printf( "jack_netsource: cannot register %s port\n", buf);
break;
}
if( bitdepth == 1000 ) {
#if HAVE_CELT
#if HAVE_CELT_API_0_11
CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate( client ), jack_get_buffer_size(client), NULL );
capture_srcs = jack_slist_append(capture_srcs, celt_decoder_create_custom( celt_mode, 1, NULL ) );
#elif HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate( client ), jack_get_buffer_size(client), NULL );
capture_srcs = jack_slist_append(capture_srcs, celt_decoder_create( celt_mode, 1, NULL ) );
#else
CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate( client ), 1, jack_get_buffer_size(client), NULL );
capture_srcs = jack_slist_append(capture_srcs, celt_decoder_create( celt_mode ) );
#endif
#endif
} else {
#if HAVE_SAMPLERATE
capture_srcs = jack_slist_append (capture_srcs, src_new (SRC_LINEAR, 1, NULL));
#endif
}
if (bitdepth == 1000) {
#if HAVE_CELT
#if HAVE_CELT_API_0_11
CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate( client ), jack_get_buffer_size(client), NULL );
capture_srcs = jack_slist_append(capture_srcs, celt_decoder_create_custom( celt_mode, 1, NULL ) );
#elif HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate( client ), jack_get_buffer_size(client), NULL );
capture_srcs = jack_slist_append(capture_srcs, celt_decoder_create( celt_mode, 1, NULL ) );
#else
CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate( client ), 1, jack_get_buffer_size(client), NULL );
capture_srcs = jack_slist_append(capture_srcs, celt_decoder_create( celt_mode ) );
#endif
#endif
} else {
#if HAVE_SAMPLERATE
capture_srcs = jack_slist_append (capture_srcs, src_new (SRC_LINEAR, 1, NULL));
#endif
}
capture_ports = jack_slist_append (capture_ports, port);
}
......@@ -185,25 +185,25 @@ alloc_ports (int n_capture_audio, int n_playback_audio, int n_capture_midi, int
printf ("jack_netsource: cannot register %s port\n", buf);
break;
}
if( bitdepth == 1000 ) {
#if HAVE_CELT
#if HAVE_CELT_API_0_11
CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate (client), jack_get_buffer_size(client), NULL );
playback_srcs = jack_slist_append(playback_srcs, celt_encoder_create_custom( celt_mode, 1, NULL ) );
#elif HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate (client), jack_get_buffer_size(client), NULL );
playback_srcs = jack_slist_append(playback_srcs, celt_encoder_create( celt_mode, 1, NULL ) );
#else
CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate (client), 1, jack_get_buffer_size(client), NULL );
playback_srcs = jack_slist_append(playback_srcs, celt_encoder_create( celt_mode ) );
#endif
#endif
} else {
#if HAVE_SAMPLERATE
playback_srcs = jack_slist_append (playback_srcs, src_new (SRC_LINEAR, 1, NULL));
#endif
}
playback_ports = jack_slist_append (playback_ports, port);
if( bitdepth == 1000 ) {
#if HAVE_CELT
#if HAVE_CELT_API_0_11
CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate (client), jack_get_buffer_size(client), NULL );
playback_srcs = jack_slist_append(playback_srcs, celt_encoder_create_custom( celt_mode, 1, NULL ) );
#elif HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate (client), jack_get_buffer_size(client), NULL );
playback_srcs = jack_slist_append(playback_srcs, celt_encoder_create( celt_mode, 1, NULL ) );
#else
CELTMode *celt_mode = celt_mode_create( jack_get_sample_rate (client), 1, jack_get_buffer_size(client), NULL );
playback_srcs = jack_slist_append(playback_srcs, celt_encoder_create( celt_mode ) );
#endif
#endif
} else {
#if HAVE_SAMPLERATE
playback_srcs = jack_slist_append (playback_srcs, src_new (SRC_LINEAR, 1, NULL));
#endif
}
playback_ports = jack_slist_append (playback_ports, port);
}
/* Allocate midi playback channels */
......@@ -232,7 +232,7 @@ sync_cb (jack_transport_state_t state, jack_position_t *pos, void *arg)
int retval = sync_state;
if (! state_connected) {
return 1;
return 1;
}
if (latency_count) {
latency_count--;
......@@ -281,14 +281,13 @@ process (jack_nframes_t nframes, void *arg)
jack_time_t packet_recv_timestamp;
if( bitdepth == 1000 )
net_period = (factor * jack_get_buffer_size(client) * 1024 / jack_get_sample_rate(client) / 8)&(~1) ;
net_period = (factor * jack_get_buffer_size(client) * 1024 / jack_get_sample_rate(client) / 8)&(~1) ;
else
net_period = (float) nframes / (float) factor;
net_period = (float) nframes / (float) factor;
rx_bufsize = get_sample_size (bitdepth) * capture_channels * net_period + sizeof (jacknet_packet_header);
tx_bufsize = get_sample_size (bitdepth) * playback_channels * net_period + sizeof (jacknet_packet_header);
/* Allocate a buffer where both In and Out Buffer will fit */
packet_buf_tx = alloca (tx_bufsize);
......@@ -352,29 +351,29 @@ process (jack_nframes_t nframes, void *arg)
if( reply_port )
input_fd = insockfd;
input_fd = insockfd;
else
input_fd = outsockfd;
input_fd = outsockfd;
// for latency == 0 we can poll.
if( (latency == 0) || (freewheeling!=0) ) {
jack_time_t deadline = jack_get_time() + 1000000 * jack_get_buffer_size(client)/jack_get_sample_rate(client);
// Now loop until we get the right packet.
while(1) {
jack_nframes_t got_frame;
if ( ! netjack_poll_deadline( input_fd, deadline ) )
break;
packet_cache_drain_socket(packcache, input_fd);
if (packet_cache_get_next_available_framecnt( packcache, framecnt - latency, &got_frame ))
if( got_frame == (framecnt - latency) )
break;
}
jack_time_t deadline = jack_get_time() + 1000000 * jack_get_buffer_size(client)/jack_get_sample_rate(client);
// Now loop until we get the right packet.
while(1) {
jack_nframes_t got_frame;
if ( ! netjack_poll_deadline( input_fd, deadline ) )
break;
packet_cache_drain_socket(packcache, input_fd);
if (packet_cache_get_next_available_framecnt( packcache, framecnt - latency, &got_frame ))
if( got_frame == (framecnt - latency) )
break;
}
} else {
// normally:
// only drain socket.
packet_cache_drain_socket(packcache, input_fd);
// normally:
// only drain socket.
packet_cache_drain_socket(packcache, input_fd);
}
size = packet_cache_retreive_packet_pointer( packcache, framecnt - latency, (char**)&rx_packet_ptr, rx_bufsize, &packet_recv_timestamp );
......@@ -382,15 +381,15 @@ process (jack_nframes_t nframes, void *arg)
* to the JACK ports so it can be played. */
if (size == rx_bufsize)
{
uint32_t *packet_buf_rx = rx_packet_ptr;
jacknet_packet_header *pkthdr_rx = (jacknet_packet_header *) packet_buf_rx;
packet_bufX = packet_buf_rx + sizeof (jacknet_packet_header) / sizeof (jack_default_audio_sample_t);
// calculate how much time there would have been, if this packet was sent at the deadline.
uint32_t *packet_buf_rx = rx_packet_ptr;
jacknet_packet_header *pkthdr_rx = (jacknet_packet_header *) packet_buf_rx;
packet_bufX = packet_buf_rx + sizeof (jacknet_packet_header) / sizeof (jack_default_audio_sample_t);
// calculate how much time there would have been, if this packet was sent at the deadline.
int recv_time_offset = (int) (jack_get_time() - packet_recv_timestamp);
packet_header_ntoh (pkthdr_rx);
deadline_goodness = recv_time_offset - (int)pkthdr_rx->latency;
//printf( "deadline goodness = %d ---> off: %d\n", deadline_goodness, recv_time_offset );
int recv_time_offset = (int) (jack_get_time() - packet_recv_timestamp);
packet_header_ntoh (pkthdr_rx);
deadline_goodness = recv_time_offset - (int)pkthdr_rx->latency;
//printf( "deadline goodness = %d ---> off: %d\n", deadline_goodness, recv_time_offset );
if (cont_miss)
{
......@@ -400,26 +399,26 @@ process (jack_nframes_t nframes, void *arg)
render_payload_to_jack_ports (bitdepth, packet_bufX, net_period,
capture_ports, capture_srcs, nframes, dont_htonl_floats);
state_currentframe = framecnt;
state_recv_packet_queue_time = recv_time_offset;
state_connected = 1;
state_currentframe = framecnt;
state_recv_packet_queue_time = recv_time_offset;
state_connected = 1;
sync_state = pkthdr_rx->sync_state;
packet_cache_release_packet( packcache, framecnt - latency );
packet_cache_release_packet( packcache, framecnt - latency );
}
/* Second alternative : we've received something that's not
* as big as expected or we missed a packet. We render silence
* to the ouput ports */
else
{
jack_nframes_t latency_estimate;
if( packet_cache_find_latency( packcache, framecnt, &latency_estimate ) )
//if( (state_latency == 0) || (latency_estimate < state_latency) )
state_latency = latency_estimate;
jack_nframes_t latency_estimate;
if( packet_cache_find_latency( packcache, framecnt, &latency_estimate ) )
//if( (state_latency == 0) || (latency_estimate < state_latency) )
state_latency = latency_estimate;
// Set the counters up.
state_currentframe = framecnt;
//state_latency = framecnt - pkthdr->framecnt;
state_netxruns += 1;
// Set the counters up.
state_currentframe = framecnt;
//state_latency = framecnt - pkthdr->framecnt;
state_netxruns += 1;
//printf ("Frame %d \tPacket missed or incomplete (expected: %d bytes, got: %d bytes)\n", framecnt, rx_bufsize, size);
//printf ("Frame %d \tPacket missed or incomplete\n", framecnt);
......@@ -440,7 +439,7 @@ process (jack_nframes_t nframes, void *arg)
chn++;
}
}
if( latency != 0 ) {
if (latency != 0) {
/* reset packet_bufX... */
packet_bufX = packet_buf_tx + sizeof (jacknet_packet_header) / sizeof (jack_default_audio_sample_t);
......@@ -630,15 +629,15 @@ main (int argc, char *argv[])
case 'b':
bitdepth = atoi (optarg);
break;
case 'c':
case 'c':
#if HAVE_CELT
bitdepth = 1000;
bitdepth = 1000;
factor = atoi (optarg);
#else
printf( "not built with celt supprt\n" );
printf( "not built with celt support\n" );
exit(10);
#endif
break;
break;
case 'm':
mtu = atoi (optarg);
break;
......@@ -679,24 +678,24 @@ main (int argc, char *argv[])
outsockfd = socket (AF_INET, SOCK_DGRAM, 0);
insockfd = socket (AF_INET, SOCK_DGRAM, 0);
if( (outsockfd == -1) || (insockfd == -1) ) {
if ((outsockfd == -1) || (insockfd == -1)) {
fprintf (stderr, "cant open sockets\n" );
return 1;
}
init_sockaddr_in ((struct sockaddr_in *) &destaddr, peer_ip, peer_port);
if(bind_port) {
if (bind_port) {
init_sockaddr_in ((struct sockaddr_in *) &bindaddr, NULL, bind_port);
if( bind (outsockfd, &bindaddr, sizeof (bindaddr)) ) {
fprintf (stderr, "bind failure\n" );
}
fprintf (stderr, "bind failure\n" );
}
}
if(reply_port)
if (reply_port)
{
init_sockaddr_in ((struct sockaddr_in *) &bindaddr, NULL, reply_port);
if( bind (insockfd, &bindaddr, sizeof (bindaddr)) ) {
fprintf (stderr, "bind failure\n" );
}
fprintf (stderr, "bind failure\n" );
}
}