Example #1
0
int krad_opus_decoder_read (krad_opus_t *krad_opus, int channel, char *buffer, int buffer_length) {

	
	int resample_process_size = 512;

	krad_opus->ret = krad_ringbuffer_peek (krad_opus->resampled_ringbuf[channel - 1], (char *)buffer, buffer_length );

	if (krad_opus->ret >= buffer_length) {
		krad_ringbuffer_read_advance (krad_opus->resampled_ringbuf[channel - 1], buffer_length );
		return krad_opus->ret;
	} else {

		while (krad_ringbuffer_read_space (krad_opus->resampled_ringbuf[channel - 1]) < buffer_length) {

			if (krad_ringbuffer_read_space (krad_opus->ringbuf[channel - 1]) >= resample_process_size * 4 ) {

				krad_opus->ret = krad_ringbuffer_peek (krad_opus->ringbuf[channel - 1],
											  (char *)krad_opus->read_samples[channel - 1],
											          (resample_process_size * 4) );
			
				krad_opus->src_data[channel - 1].data_in = krad_opus->read_samples[channel - 1];
				krad_opus->src_data[channel - 1].input_frames = resample_process_size;
				krad_opus->src_data[channel - 1].data_out = krad_opus->resampled_samples[channel - 1];
				krad_opus->src_data[channel - 1].output_frames = 2048;
				krad_opus->src_error[channel - 1] = src_process (krad_opus->src_resampler[channel - 1],
																&krad_opus->src_data[channel - 1]);
				if (krad_opus->src_error[channel - 1] != 0) {
					failfast ("krad_opus_read_audio src resampler error: %s\n",
							  src_strerror(krad_opus->src_error[channel - 1]));
				}
				
				krad_ringbuffer_read_advance (krad_opus->ringbuf[channel - 1],
											 (krad_opus->src_data[channel - 1].input_frames_used * 4) );

				krad_opus->ret = krad_ringbuffer_write (krad_opus->resampled_ringbuf[channel - 1],
				                               (char *)krad_opus->resampled_samples[channel - 1],
				                                       (krad_opus->src_data[channel - 1].output_frames_gen * 4) );

				if (krad_ringbuffer_read_space (krad_opus->resampled_ringbuf[channel - 1]) >= buffer_length ) {
					return krad_ringbuffer_read (krad_opus->resampled_ringbuf[channel - 1], buffer, buffer_length );
				}

			} else {
				return 0;
			}
		
		}

		
	}
	
	return 0;
	
}
Example #2
0
static void *krad_transmitter_transmission_thread (void *arg) {

    krad_transmission_t *krad_transmission = (krad_transmission_t *)arg;

    krad_system_set_thread_name ("kr_tx_txmtr");

    int r;
    int e;
    int ret;
    int wait_time;
    uint64_t last_position;
    uint32_t new_bytes_avail;
    uint32_t space_avail;
    uint32_t drop_count;
    krad_transmission_receiver_t *krad_transmission_receiver;

    r = 0;
    e = 0;
    ret = 0;
    wait_time = 2;
    last_position = 0;
    new_bytes_avail = 0;
    space_avail = 0;
    drop_count = 0;
    krad_transmission_receiver = NULL;

    printk ("Krad Transmitter: transmission thread starting for %s", krad_transmission->sysname);

    while (krad_transmission->active == 1) {

        if (krad_transmission->new_data == 1) {

            if (krad_transmission->new_sync_point == 1) {
                krad_transmission->sync_point = krad_transmission->position;
                krad_transmission->new_sync_point = 0;
            }

            space_avail = krad_ringbuffer_write_space (krad_transmission->transmission_ringbuffer);
            new_bytes_avail = krad_ringbuffer_read_space (krad_transmission->ringbuffer);

            if (space_avail < new_bytes_avail) {
                drop_count = new_bytes_avail - space_avail;
                krad_ringbuffer_read_advance (krad_transmission->transmission_ringbuffer, drop_count);
            }

            krad_ringbuffer_read (krad_transmission->ringbuffer,
                                  (char *)krad_transmission->transmission_buffer,
                                  new_bytes_avail);

            krad_ringbuffer_write (krad_transmission->transmission_ringbuffer,
                                   (char *)krad_transmission->transmission_buffer,
                                   new_bytes_avail);

            krad_transmission->position += new_bytes_avail;
            if (krad_transmission->position > DEFAULT_RING_SIZE) {
                krad_transmission->horizon = krad_transmission->position - DEFAULT_RING_SIZE;
            }

            krad_ringbuffer_get_read_vector (krad_transmission->transmission_ringbuffer,
                                             &krad_transmission->tx_vec[0]);
            krad_transmission->new_data = 0;
        }

        ret = epoll_wait (krad_transmission->connections_efd, krad_transmission->transmission_events,
                          KRAD_TRANSMITTER_MAXEVENTS, wait_time);

        if (ret < 0) {
            if ((ret < 0) && (errno == EINTR)) {
                continue;
            }
            printke ("Krad Transmitter: Failed on epoll wait %s", strerror(errno));
            krad_transmission->active = 2;
            break;
        }

        if (ret > 0) {
            for (e = 0; e < ret; e++) {

                krad_transmission_receiver =
                    (krad_transmission_receiver_t *)krad_transmission->transmission_events[e].data.ptr;

                if (krad_transmission_receiver->noob == 1) {
                    krad_transmission_receiver->noob = 0;
                    krad_transmission->receivers++;
                }

                if ((krad_transmission->transmission_events[e].events & EPOLLERR) ||
                        (krad_transmission->transmission_events[e].events & EPOLLHUP) ||
                        (krad_transmission->transmission_events[e].events & EPOLLHUP)) {

                    if (krad_transmission->transmission_events[e].events & EPOLLHUP) {
                        printke ("Krad Transmitter: transmitter transmission receiver connection hangup");
                    }
                    if (krad_transmission->transmission_events[e].events & EPOLLERR) {
                        printke ("Krad Transmitter: transmitter transmission receiver connection error");
                    }
                    if (krad_transmission->transmission_events[e].events & EPOLLHUP) {
                        printk ("Krad Transmitter: client disconnected %d", krad_transmission_receiver->fd);
                    }
                    krad_transmitter_receiver_destroy (krad_transmission_receiver);
                    continue;
                }

                if (krad_transmission->transmission_events[e].events & EPOLLOUT) {
                    krad_transmitter_transmission_transmit (krad_transmission, krad_transmission_receiver);
                }
            }
        }

        if ((last_position != krad_transmission->position) && (krad_transmission->ready_receiver_count > 0)) {
            last_position = krad_transmission->position;
            for (r = 0; r < TOTAL_RECEIVERS; r++) {
                if ((krad_transmission->krad_transmitter->krad_transmission_receivers[r].krad_transmission == krad_transmission) && (krad_transmission->krad_transmitter->krad_transmission_receivers[r].active == 1) && (krad_transmission->krad_transmitter->krad_transmission_receivers[r].ready == 1)) {
                    krad_transmission_receiver = &krad_transmission->krad_transmitter->krad_transmission_receivers[r];
                    krad_transmitter_transmission_transmit (krad_transmission, krad_transmission_receiver);
                }
            }
        }
    }

    krad_transmission->active = 3;

    printk ("Krad Transmitter: transmission thread exiting for %s", krad_transmission->sysname);
    return NULL;
}
Example #3
0
int krad_opus_encoder_read (krad_opus_t *krad_opus, unsigned char *buffer, int *nframes) {

	int ready;
	int bytes;
	int resp;
	int s, c;

	while (krad_ringbuffer_read_space (krad_opus->ringbuf[krad_opus->channels - 1]) >= 512 * 4 ) {
		   
		for (c = 0; c < krad_opus->channels; c++) {

			krad_opus->ret = krad_ringbuffer_peek (krad_opus->ringbuf[c], (char *)krad_opus->samples[c], (512 * 4) );
			
			krad_opus->src_data[c].data_in = krad_opus->samples[c];
			krad_opus->src_data[c].input_frames = 512;
			krad_opus->src_data[c].data_out = krad_opus->resampled_samples[c];
			krad_opus->src_data[c].output_frames = 2048;
			krad_opus->src_error[c] = src_process (krad_opus->src_resampler[c], &krad_opus->src_data[c]);
			if (krad_opus->src_error[c] != 0) {
				failfast ("Krad Opus Encoder: src resampler error: %s\n", src_strerror(krad_opus->src_error[c]));
			}
			krad_ringbuffer_read_advance (krad_opus->ringbuf[c], (krad_opus->src_data[c].input_frames_used * 4) );
			krad_opus->ret = krad_ringbuffer_write (krad_opus->resampled_ringbuf[c],
										   (char *)krad_opus->resampled_samples[c],
										           (krad_opus->src_data[c].output_frames_gen * 4) );
		}
	}
	
	if (krad_opus->new_bitrate != krad_opus->bitrate) {
		krad_opus->bitrate = krad_opus->new_bitrate;
		resp = opus_multistream_encoder_ctl (krad_opus->encoder, OPUS_SET_BITRATE(krad_opus->bitrate));
		if (resp != OPUS_OK) {
			failfast ("Krad Opus Encoder: bitrate request failed %s\n", opus_strerror (resp));
		} else {
			printk  ("Krad Opus Encoder: set opus bitrate %d\n", krad_opus->bitrate);
		}
	}
	
	if (krad_opus->new_frame_size != krad_opus->frame_size) {
		krad_opus->frame_size = krad_opus->new_frame_size;
		printk ("Krad Opus Encoder: frame size is now %d\n", krad_opus->frame_size);		
	}
	
	if (krad_opus->new_complexity != krad_opus->complexity) {
		krad_opus->complexity = krad_opus->new_complexity;
		resp = opus_multistream_encoder_ctl (krad_opus->encoder, OPUS_SET_COMPLEXITY(krad_opus->complexity));
		if (resp != OPUS_OK) {
			failfast ("Krad Opus Encoder: complexity request failed %s. \n", opus_strerror(resp));
		} else {
			printk ("Krad Opus Encoder: set opus complexity %d\n", krad_opus->complexity);
		}
	}	

	if (krad_opus->new_signal != krad_opus->signal) {
		krad_opus->signal = krad_opus->new_signal;
		resp = opus_multistream_encoder_ctl (krad_opus->encoder, OPUS_SET_SIGNAL(krad_opus->signal));
		if (resp != OPUS_OK) {
			failfast ("Krad Opus Encoder: signal request failed %s\n", opus_strerror(resp));
		} else {
			printk ("Krad Opus Encoder: set opus signal mode %d\n", krad_opus->signal);
		}
	}
	
	if (krad_opus->new_bandwidth != krad_opus->bandwidth) {
		krad_opus->bandwidth = krad_opus->new_bandwidth;
		resp = opus_multistream_encoder_ctl (krad_opus->encoder, OPUS_SET_BANDWIDTH(krad_opus->bandwidth));
		if (resp != OPUS_OK) {
			failfast ("Krad Opus Encoder: bandwidth request failed %s\n", opus_strerror(resp));
		} else {
			printk ("Krad Opus Encoder: Set Opus bandwidth mode %d\n", krad_opus->bandwidth);
		}
	}
		
	ready = 1;
	
	for (c = 0; c < krad_opus->channels; c++) {
		if (krad_ringbuffer_read_space (krad_opus->resampled_ringbuf[c]) < krad_opus->frame_size * 4) {
			ready = 0;
		}
	}

	if (ready == 1) {

		for (c = 0; c < krad_opus->channels; c++) {
			krad_opus->ret = krad_ringbuffer_read (krad_opus->resampled_ringbuf[c],
										  (char *)krad_opus->resampled_samples[c],
										          (krad_opus->frame_size * 4) );
		}

		for (s = 0; s < krad_opus->frame_size; s++) {
			for (c = 0; c < krad_opus->channels; c++) {
				krad_opus->interleaved_resampled_samples[s * krad_opus->channels + c] = krad_opus->resampled_samples[c][s];
			}
		}

		bytes = opus_multistream_encode_float (krad_opus->encoder,
											   krad_opus->interleaved_resampled_samples,
											   krad_opus->frame_size,
											   buffer,
											   krad_opus->frame_size * 2);

		if (bytes < 0) {
			failfast ("Krad Opus Encoding failed: %s.", opus_strerror (bytes));
		}

		*nframes = krad_opus->frame_size;

		return bytes;
	}
	
	return 0;

}