Exemplo n.º 1
0
int krad_transmitter_transmission_add_data_opt (krad_transmission_t *krad_transmission, uint8_t *buffer, int length, int sync) {

    int ret;
    ret = 0;

    while (krad_transmission->new_data == 1) {
        usleep(1000);
    }

    if ((krad_transmission->ready == 0) && (krad_transmission->header_len > 0) && (length > 0)) {
        ret = krad_ringbuffer_write (krad_transmission->ringbuffer, (char *)buffer, length);
        krad_transmission->ready = 1;
        krad_transmission->new_sync_point = 1;
        krad_transmission->new_data = 1;

        printk ("Krad Transmitter: transmission %s added now ready!",
                krad_transmission->sysname);
        return ret;
    } else {
        ret = krad_ringbuffer_write (krad_transmission->ringbuffer, (char *)buffer, length);
        krad_transmission->new_sync_point = sync;
        krad_transmission->new_data = 1;
        return ret;
    }
}
Exemplo n.º 2
0
int krad_opus_decoder_write (krad_opus_t *krad_opus, unsigned char *buffer, int length) {

	int i;
	int frames_decoded;

  frames_decoded = 0;

	krad_opus->opus_decoder_error = opus_multistream_decode_float (krad_opus->decoder,
																  buffer,
																  length,
																  krad_opus->interleaved_samples,
																  2880 * 2,
																  0);

	if (krad_opus->opus_decoder_error < 0) {
		failfast ("Krad Opus decoder error: %d\n", krad_opus->opus_decoder_error);
	} else {
		frames_decoded = krad_opus->opus_decoder_error;
	}

	for (i = 0; i < frames_decoded; i++) {
		krad_opus->samples[0][i] = krad_opus->interleaved_samples[i * 2 + 0];
		krad_opus->samples[1][i] = krad_opus->interleaved_samples[i * 2 + 1];
	}


	krad_ringbuffer_write (krad_opus->ringbuf[0], (char *)krad_opus->samples[0], (frames_decoded * 4) );
	krad_ringbuffer_write (krad_opus->ringbuf[1], (char *)krad_opus->samples[1], (frames_decoded * 4) );


	return 0;

}
void krad_v4l2_vpx_display_test_audio_callback(int frames, void *userdata) {

	krad_v4l2_vpx_display_test_t *display_test = (krad_v4l2_vpx_display_test_t *)userdata;
	
	kradaudio_read (display_test->audio, 0, (char *)display_test->samples[0], frames * 4 );
	kradaudio_read (display_test->audio, 1, (char *)display_test->samples[1], frames * 4 );

	if (display_test->start_audio == 1) {
		krad_ringbuffer_write(display_test->input_ringbuffer[0], (char *)display_test->samples[0], frames * 4);
		krad_ringbuffer_write(display_test->input_ringbuffer[1], (char *)display_test->samples[1], frames * 4);
	}
}
Exemplo n.º 4
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;
	
}
Exemplo n.º 5
0
int audioport_process (uint32_t nframes, void *arg) {

  int c;
  float *buffer;
  kr_streamer_t *streamer;

  streamer = (kr_streamer_t *)arg;

  for (c = 0; c < streamer->params->channels; c++) {
    buffer = kr_audioport_get_buffer (streamer->audioport, c);
    krad_ringbuffer_write (streamer->audio_ring[c],
                           (char *)buffer,
                           nframes * 4);
  }

  return 0;
}
Exemplo n.º 6
0
static void kradpulse_capture_cb(pa_stream *stream, size_t length, void *userdata) {

    krad_pulse_t *kradpulse = (krad_pulse_t *)userdata;

    pa_usec_t usec;
    int neg;
    const void *samples;
    int c, s;

    pa_stream_get_latency(stream, &usec, &neg);

    //printf("  latency %8d us wanted %d frames\n", (int)usec, length / 4 / 2 );

    pa_stream_peek(stream, &samples, &length);

    if ((krad_ringbuffer_write_space (kradpulse->kradaudio->input_ringbuffer[1]) >= length / 2 ) && (krad_ringbuffer_write_space (kradpulse->kradaudio->input_ringbuffer[0]) >= length / 2 )) {


        memcpy(kradpulse->capture_interleaved_samples, samples, length);

        pa_stream_drop(stream);

        for (s = 0; s < length / 4 / 2; s++) {
            for (c = 0; c < 2; c++) {
                kradpulse->capture_samples[c][s] = kradpulse->capture_interleaved_samples[s * 2 + c];
            }
        }

        for (c = 0; c < 2; c++) {
            krad_ringbuffer_write (kradpulse->kradaudio->input_ringbuffer[c], (char *)kradpulse->capture_samples[c], (length / 2) );
        }


        for (c = 0; c < 2; c++) {
            compute_peak(kradpulse->kradaudio, KINPUT, &kradpulse->capture_interleaved_samples[c], c, length / 4 / 2 , 1);
        }


    }

    if (kradpulse->kradaudio->process_callback != NULL) {
        kradpulse->kradaudio->process_callback(length / 4 / 2, kradpulse->kradaudio->userdata);
    }

}
Exemplo n.º 7
0
int new_frame (void *buffer, void *user) {

  kr_streamer_t *streamer;
  krad_frame_t *frame;

  streamer = (kr_streamer_t *)user;

  frame = krad_framepool_getframe (streamer->framepool);

  if (frame != NULL) {

    memcpy (frame->pixels,
            buffer,
            streamer->frame_size);

    krad_ringbuffer_write (streamer->frame_ring,
                          (char *)&frame,
                          sizeof(krad_frame_t *));
  }

  streamer->frames++;

  return 0;
}
Exemplo n.º 8
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;
}
Exemplo n.º 9
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;

}
Exemplo n.º 10
0
int krad_opus_encoder_write (krad_opus_t *krad_opus, int channel, char *buffer, int buffer_length) {
	return krad_ringbuffer_write (krad_opus->ringbuf[channel - 1], buffer, buffer_length );
}