bool Encoder::init(void) { #ifdef CONFIG_AUDIO_CODEC switch (mAudioType) { #ifdef CONFIG_CODEC_LIBOPUS case AUDIO_TYPE_OPUS: { opus_enc_external_t ext = {0}; // params for opus encoding #if defined(CONFIG_OPUS_APPLICATION_VOIP) ext.applicationMode = OPUS_APPLICATION_VOIP; #elif defined(CONFIG_OPUS_APPLICATION_AUDIO) ext.applicationMode = OPUS_APPLICATION_AUDIO; #elif defined(CONFIG_OPUS_APPLICATION_RESTRICTED_LOWDELAY) ext.applicationMode = OPUS_APPLICATION_RESTRICTED_LOWDELAY; #else ext.applicationMode = OPUS_APPLICATION_AUDIO; #endif ext.complexity = CONFIG_OPUS_ENCODE_COMPLEXITY; ext.frameSizeMS = CONFIG_OPUS_ENCODE_FRAMESIZE; ext.bitrate = CONFIG_OPUS_ENCODE_BITRATE; ext.bandWidth = OPUS_AUTO; // allocate memory resource size_t inSamples = mSampleRate * mChannels * ext.frameSizeMS / MSEC_PER_SECOND; inputBuf = new signed short[inSamples]; outputBuf = new unsigned char[MAX_PACKET_SIZE]; // params for streaming ext.pOutputBuffer = outputBuf; ext.outputBufferMaxLength = sizeof(unsigned char) * MAX_PACKET_SIZE; ext.pInputBuffer = inputBuf; ext.inputBufferMaxLength = sizeof(signed short) * inSamples; // params about PCM source ext.inputChannels = mChannels; ext.inputSampleRate = mSampleRate; // Initialize encoder if (audio_encoder_init(&mEncoder, CONFIG_AUDIO_CODEC_RINGBUFFER_SIZE, AUDIO_TYPE_OPUS, &ext) != AUDIO_ENCODER_OK) { meddbg("Error! audio_encoder_init failed!\n"); return false; } return true; } #endif default: return false; } #else return false; #endif }
int main(int argc, char *argv[]) { /* setup termination signals handler */ signal(SIGINT, sig_handler); signal(SIGHUP, sig_handler); signal(SIGKILL, sig_handler); audio_capture_init(samplerate); atexit(audio_capture_terminate); audio_encoder_init(samplerate, opus_complexity, opus_bitrate); atexit(audio_encoder_terminate); udp_bcast_init(bcast_addr, port); atexit(udp_bcast_terminate); size_t element_size = (samplerate / 1000 * frame_length) * 2; queue_init(queue_elements_num, element_size); atexit(queue_terminate); pthread_t capt_th, enc_send_th; if (pthread_create(&enc_send_th, NULL, enc_send_thread, NULL)) { fprintf(stderr, "ERROR: unable to spawn encoding and sending thread\n"); exit(EXIT_FAILURE); } if (pthread_create(&capt_th, NULL, capture_thread, NULL)) { fprintf(stderr, "ERROR: unable to spawn capturing thread\n"); terminate_threads = true; pthread_join(enc_send_th, NULL); exit(EXIT_FAILURE); } printf("Capturing, encoding and sending Ctrl+C to terminate...\n"); struct timeval start_ts, cur_ts; gettimeofday(&start_ts, NULL); while (!app_terminate) { sleep(1); gettimeofday(&cur_ts, NULL); uint32_t time_delta_ms = (cur_ts.tv_sec - start_ts.tv_sec) * 1000 + (cur_ts.tv_usec - start_ts.tv_usec) / 1000; printf("%u.%03u capt:%"PRIu64" drop:%"PRIu64" enc: %"PRIu64"\n", time_delta_ms/1000, time_delta_ms % 1000, capt_buffers, capt_buffers_dropped, enc_buffers); } terminate_threads = true; pthread_join(capt_th, NULL); pthread_join(enc_send_th, NULL); printf("\n"); printf("Captured packets: %"PRIu64" Packets dropped at " "capture: %" PRIu64"\n", capt_buffers, capt_buffers_dropped); printf("Capture time (min/avg/max): %u/%u/%u microseconds\n", capt_min_time, (uint32_t)(capt_time/capt_buffers), capt_max_time); printf("Encoded packets: %"PRIu64"\n", enc_buffers); printf("Encoding time (min/avg/max): %u/%u/%u microseconds\n", enc_min_time, (uint32_t)(enc_time/enc_buffers), enc_max_time); /* OK */ exit(EXIT_SUCCESS); }