int kr_streamer_destroy (kr_streamer_t **streamer) { int c; if ((streamer == NULL) || (*streamer == NULL)) { return -1; } krad_vpx_encoder_destroy (&(*streamer)->vpx_enc); kr_mkv_destroy (&(*streamer)->mkv); kr_audioport_disconnect((*streamer)->audioport); kr_audioport_destroy ((*streamer)->audioport); kr_videoport_deactivate ((*streamer)->videoport); kr_videoport_destroy ((*streamer)->videoport); kr_client_destroy (&(*streamer)->client); krad_vorbis_encoder_destroy (&(*streamer)->vorbis_enc); for (c = 0; c < (*streamer)->params->channels; c++) { krad_ringbuffer_free ((*streamer)->audio_ring[c]); } krad_ringbuffer_free ((*streamer)->frame_ring); krad_framepool_destroy (&(*streamer)->framepool); free (*streamer); *streamer = NULL; return 0; }
void audio_encoding_unit_destroy (void *arg) { krad_link_t *krad_link = (krad_link_t *)arg; kr_mixer_path_unlink(krad_link->krad_radio->mixer, krad_link->mixer_portgroup); int c; //unsigned char *vorbis_buffer; //int bytes; //int frames; if (krad_link->krad_vorbis != NULL) { // krad_vorbis_encoder_finish (krad_link->krad_vorbis); /* // DUPEY bytes = krad_vorbis_encoder_read (krad_link->krad_vorbis, &frames, &vorbis_buffer); while (bytes > 0) { krad_ringbuffer_write (krad_link->encoded_audio_ringbuffer, (char *)&bytes, 4); krad_ringbuffer_write (krad_link->encoded_audio_ringbuffer, (char *)&frames, 4); krad_ringbuffer_write (krad_link->encoded_audio_ringbuffer, (char *)vorbis_buffer, bytes); bytes = krad_vorbis_encoder_read (krad_link->krad_vorbis, &frames, &vorbis_buffer); } */ krad_vorbis_encoder_destroy (&krad_link->krad_vorbis); } if (krad_link->krad_flac != NULL) { krad_flac_encoder_destroy (krad_link->krad_flac); krad_link->krad_flac = NULL; } if (krad_link->krad_opus != NULL) { krad_opus_encoder_destroy (krad_link->krad_opus); krad_link->krad_opus = NULL; } close (krad_link->socketpair[0]); close (krad_link->socketpair[1]); for (c = 0; c < krad_link->channels; c++) { free (krad_link->samples[c]); free (krad_link->au_samples[c]); krad_ringbuffer_free (krad_link->audio_input_ringbuffer[c]); } free (krad_link->au_interleaved_samples); if (krad_link->codec != VORBIS) { free (krad_link->au_buffer); } printk ("Audio encoding thread exiting"); }
void krad_ebml_creator_destroy(krad_ebml_creator_t *krad_ebml_creator) { krad_ebml_creator_stop(krad_ebml_creator); kradaudio_destroy(krad_ebml_creator->audio); free(krad_ebml_creator->samples[0]); free(krad_ebml_creator->samples[1]); free(krad_ebml_creator->callback_samples[0]); free(krad_ebml_creator->callback_samples[1]); kradebml_write(krad_ebml_creator->ebml); kradebml_destroy(krad_ebml_creator->ebml); //kradopus_encoder_destroy(krad_ebml_creator->opus); krad_vorbis_encoder_destroy(krad_ebml_creator->vorbis); free(krad_ebml_creator); }
int main (int argc, char *argv[]) { krad_v4l2_vpx_display_test_t *display_test; krad_flac_t *krad_flac; krad_dirac_t *krad_dirac; krad_vpx_encoder_t *krad_vpx_encoder; //krad_vpx_decoder_t *krad_vpx_decoder; kradgui_t *kradgui; kradebml_t *ebml; cairo_surface_t *cst; cairo_t *cr; char *filename = "/home/oneman/kode/testmedia/capture/new_testfile4.webm"; int hud_width, hud_height; int hud_stride; int hud_byte_size; unsigned char *hud_data; struct SwsContext *sws_context; int fps; int videotrack; int audiotrack; char *device; krad_v4l2_t *kradv4l2; krad_sdl_opengl_display_t *krad_opengl_display; int width; int height; int count; //int first_frame; int read_composited; unsigned char *read_screen_buffer; unsigned char *dbuffer; unsigned char *dfbuffer; krad_audio_t *audio; krad_audio_api_t audio_api; krad_vorbis_t *krad_vorbis; hud_width = 320; hud_height = 240; read_composited = 1; //first_frame = 1; width = 640; height = 480; fps = 30; count = 0; int keyframe; int bitrate; void *frame = NULL; void *vpx_packet; int packet_size; int took; int video_codec; int audio_codec; int framenum; int bytes; bitrate = 1000; video_codec = 1; audio_codec = 1; if (argc < 2) { device = DEFAULT_DEVICE; printf("no device provided, using %s , to provide a device, example: krad_v4l2_test /dev/video0\n", device); } else { device = argv[1]; printf("Using %s\n", device); } display_test = calloc(1, sizeof(krad_v4l2_vpx_display_test_t)); pthread_rwlock_init(&display_test->ebml_write_lock, NULL); pthread_rwlock_rdlock(&display_test->ebml_write_lock); //display_test->kradtimer = kradtimer_create(); display_test->input_ringbuffer[0] = krad_ringbuffer_create (RINGBUFFER_SIZE); display_test->input_ringbuffer[1] = krad_ringbuffer_create (RINGBUFFER_SIZE); display_test->samples[0] = malloc(4 * 8192); display_test->samples[1] = malloc(4 * 8192); display_test->first_block = 1; display_test->audio_codec = audio_codec; dbuffer = calloc(1, 2000000); dfbuffer = calloc(1, 2000000); audio_api = JACK; //audio = kradaudio_create("krad v4l2 vpx display test", audio_api); krad_vorbis = krad_vorbis_encoder_create(2, 44100, 0.7); krad_flac = krad_flac_encoder_create(1, 44100, 16); read_screen_buffer = calloc(1, width * height * 4 * 4 * 4 * 4); sws_context = sws_getContext ( width, height, PIX_FMT_RGB32, width, height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL); ebml = kradebml_create(); //kradebml_open_output_stream(ebml, "192.168.1.2", 9080, "/teststream.webm", "secretkode"); kradebml_open_output_file(ebml, filename); if (video_codec == 1) { kradebml_header(ebml, "webm", APPVERSION); } else { kradebml_header(ebml, "matroska", APPVERSION); } if (video_codec == 1) { videotrack = kradebml_add_video_track(ebml, "V_VP8", 30, width, height); } if (video_codec == 2) { videotrack = kradebml_add_video_track(ebml, "V_DIRAC", 10, width, height); } if (audio_codec == 1) { audiotrack = kradebml_add_audio_track(ebml, "A_VORBIS", 44100, 2, krad_vorbis->header, krad_vorbis->headerpos); } if (audio_codec == 2) { bytes = krad_flac_encoder_read_min_header(krad_flac, dfbuffer); audiotrack = kradebml_add_audio_track(ebml, "A_FLAC", 44100, 1, dfbuffer, bytes); display_test->krad_flac = krad_flac; } kradebml_write(ebml); krad_opengl_display = krad_sdl_opengl_display_create(APPVERSION, width, height, width, height); //krad_opengl_display = krad_sdl_opengl_display_create(APPVERSION, 1920, 1080, width, height); krad_vpx_encoder = krad_vpx_encoder_create(width, height, bitrate); krad_dirac = krad_dirac_encoder_create(width, height); kradgui = kradgui_create(width, height); kradgui_add_item(kradgui, REEL_TO_REEL); hud_stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, hud_width); hud_byte_size = hud_stride * hud_height; hud_data = calloc (1, hud_byte_size); cst = cairo_image_surface_create_for_data (hud_data, CAIRO_FORMAT_ARGB32, hud_width, hud_height, hud_stride); krad_opengl_display->hud_width = hud_width; krad_opengl_display->hud_height = hud_height; krad_opengl_display->hud_data = hud_data; display_test->audiotrack = audiotrack; display_test->ebml = ebml; //display_test->audio = audio; display_test->krad_vorbis = krad_vorbis; display_test->kradgui = kradgui; kradv4l2 = kradv4l2_create(); kradv4l2_open(kradv4l2, device, width, height, fps); kradv4l2_start_capturing (kradv4l2); kradgui_reset_elapsed_time(kradgui); kradgui->render_timecode = 1; audio = kradaudio_create("krad v4l2 vpx display test", KINPUT, audio_api); display_test->audio = audio; pthread_create(&display_test->audio_encoding_thread, NULL, audio_encoding_thread, (void *)display_test); kradaudio_set_process_callback(audio, krad_v4l2_vpx_display_test_audio_callback, display_test); if (audio_api == JACK) { krad_jack_t *jack = (krad_jack_t *)audio->api; kradjack_connect_port(jack->jack_client, "firewire_pcm:001486af2e61ac6b_Unknown0_in", "krad v4l2 vpx display test:InputLeft"); kradjack_connect_port(jack->jack_client, "firewire_pcm:001486af2e61ac6b_Unknown0_in", "krad v4l2 vpx display test:InputRight"); } while (count < TEST_COUNT) { //kradtimer_start(display_test->kradtimer, "cycle"); cr = cairo_create(cst); kradgui->cr = cr; kradgui_render(kradgui); cairo_destroy(cr); frame = kradv4l2_read_frame_wait (kradv4l2); if (display_test->start_audio == 0) { display_test->start_audio = 1; usleep(200000); } if (video_codec == 2) { // memcpy(dfbuffer, frame, width * height + (((width * height) / 2) * 2)); } krad_vpx_convert_uyvy2yv12(krad_vpx_encoder->image, frame, width, height); ///krad_vpx_convert_frame_for_local_gl_display(krad_vpx_encoder); kradv4l2_frame_done (kradv4l2); krad_sdl_opengl_display_render(krad_opengl_display, krad_vpx_encoder->image->planes[0], krad_vpx_encoder->image->stride[0], krad_vpx_encoder->image->planes[1], krad_vpx_encoder->image->stride[1], krad_vpx_encoder->image->planes[2], krad_vpx_encoder->image->stride[2]); krad_sdl_opengl_draw_screen( krad_opengl_display ); if (read_composited) { krad_sdl_opengl_read_screen( krad_opengl_display, read_screen_buffer); rgb_to_yv12(sws_context, read_screen_buffer, width, height, krad_vpx_encoder->image->planes, krad_vpx_encoder->image->stride); vpx_img_flip(krad_vpx_encoder->image); } if (video_codec == 1) { count++; packet_size = krad_vpx_encoder_write(krad_vpx_encoder, (unsigned char **)&vpx_packet, &keyframe); //printf("packet size was %d\n", packet_size); if (read_composited) { vpx_img_flip(krad_vpx_encoder->image); } if (packet_size) { pthread_rwlock_wrlock(&display_test->ebml_write_lock); kradebml_add_video(ebml, videotrack, vpx_packet, packet_size, keyframe); //kradebml_write(ebml); pthread_rwlock_unlock (&display_test->ebml_write_lock); } } if (video_codec == 2) { // packet_size = krad_dirac_encode (krad_dirac, dfbuffer, dbuffer, &framenum, &took); packet_size = krad_dirac_encode (krad_dirac, krad_vpx_encoder->image->img_data, dbuffer, &framenum, &took); if (took == 1) { took = 0; count++; } if (packet_size > 0) { krad_dirac_packet_type(dbuffer[4]); //write(fd, buffer, len); printf("Encoded size is %d for frame %d\n", packet_size, framenum); } //printf("packet size was %d\n", packet_size); if (read_composited) { vpx_img_flip(krad_vpx_encoder->image); } keyframe = 0; if (packet_size) { pthread_rwlock_wrlock(&display_test->ebml_write_lock); kradebml_add_video(ebml, videotrack, dbuffer, packet_size, keyframe); //kradebml_write(ebml); pthread_rwlock_unlock (&display_test->ebml_write_lock); } } //kradtimer_finish_show(display_test->kradtimer); } display_test->start_audio = 0; if (video_codec == 2) { schro_encoder_end_of_stream (krad_dirac->encoder); // packet_size = krad_dirac_encode (krad_dirac, dfbuffer, dbuffer, &framenum, &took); while ((framenum != count - 1) && (framenum != 0)) { packet_size = krad_dirac_encode (krad_dirac, krad_vpx_encoder->image->img_data, dbuffer, &framenum, &took); if (packet_size > 0) { krad_dirac_packet_type(dbuffer[4]); //write(fd, buffer, len); printf("Encoded size is %d for frame %d\n", packet_size, framenum); } else { usleep(50000); } //printf("packet size was %d\n", packet_size); if (read_composited) { //vpx_img_flip(krad_vpx_encoder->image); } keyframe = 0; if (packet_size) { pthread_rwlock_wrlock(&display_test->ebml_write_lock); kradebml_add_video(ebml, videotrack, dbuffer, packet_size, keyframe); //kradebml_write(ebml); pthread_rwlock_unlock (&display_test->ebml_write_lock); } } } //display_test->start_audio = 0; printf("finish audio encoding\n"); usleep(2000000); display_test->stop_audio_encoding = 1; pthread_join(display_test->audio_encoding_thread, NULL); kradebml_destroy(ebml); kradv4l2_stop_capturing (kradv4l2); kradv4l2_close(kradv4l2); kradv4l2_destroy(kradv4l2); krad_dirac_encoder_destroy(krad_dirac); krad_vpx_encoder_destroy(krad_vpx_encoder); // must be before vorbis kradaudio_destroy(audio); krad_vorbis_encoder_destroy(krad_vorbis); krad_flac_encoder_destroy(krad_flac); free(display_test->samples[0]); free(display_test->samples[1]); krad_sdl_opengl_display_destroy(krad_opengl_display); kradgui_destroy(kradgui); free(read_screen_buffer); sws_freeContext (sws_context); krad_ringbuffer_free ( display_test->input_ringbuffer[0] ); krad_ringbuffer_free ( display_test->input_ringbuffer[1] ); pthread_rwlock_destroy(&display_test->ebml_write_lock); //kradtimer_destroy(display_test->kradtimer); free(display_test); free(dbuffer); free(dfbuffer); return 0; }
static void remuxcode (kr_mkv_t *mkv, char *file, char *file2) { int32_t ret; int32_t bytes_read; uint32_t track; uint64_t timecode; uint8_t *buffer; uint8_t *vbuffer; uint8_t *vbuffer2; uint32_t out_track; kr_mkv_t *new_mkv; int32_t keyframe; uint8_t flags; int32_t packets; krad_vpx_decoder_t *vpx_dec; krad_vpx_encoder_t *vpx_enc; kr_codec_hdr_t header; kr_medium_t *medium; kr_codeme_t *codeme; krad_vorbis_t *vorbis_dec; krad_vorbis_t *vorbis_enc; packets = 0; //vpxtest (); buffer = malloc (20000000); vbuffer = malloc (20000000); vbuffer2 = malloc (20000000); new_mkv = kr_mkv_create_file (file); if (new_mkv == NULL) { fprintf (stderr, "Could not open %s\n", file); exit (1); } printf ("Created file: %s\n", file); vpx_dec = krad_vpx_decoder_create (); vpx_enc = krad_vpx_encoder_create (1280, 720, 30000, 1000, 2200); out_track = kr_mkv_add_video_track (new_mkv, VP8, 30000, 1000, mkv->tracks[VIDEO_TRACK].width, mkv->tracks[VIDEO_TRACK].height); header.count = mkv->tracks[AUDIO_TRACK].headers; header.data[0] = mkv->tracks[AUDIO_TRACK].header[0]; header.sz[0] = mkv->tracks[AUDIO_TRACK].header_len[0]; header.data[1] = mkv->tracks[AUDIO_TRACK].header[1]; header.sz[1] = mkv->tracks[AUDIO_TRACK].header_len[1]; header.data[2] = mkv->tracks[AUDIO_TRACK].header[2]; header.sz[2] = mkv->tracks[AUDIO_TRACK].header_len[2]; vorbis_dec = krad_vorbis_decoder_create (&header); vorbis_enc = krad_vorbis_encoder_create (2, 48000, 0.4); kr_mkv_add_audio_track (new_mkv, VORBIS, 48000, 2, vorbis_enc->hdrdata, 3 + vorbis_enc->header.sz[0] + vorbis_enc->header.sz[1] + vorbis_enc->header.sz[2]); printf ("\n"); while ((bytes_read = kr_mkv_read_packet (mkv, &track, &timecode, &flags, buffer)) > 0) { printk ("Read packet %d track %d %d bytes", packets++, track, bytes_read); //fflush (stdout); if (flags == 0x80) { keyframe = 1; } else { keyframe = 0; } if (track == 1) { /* medium = kr_medium_kludge_create (); codeme = kr_codeme_kludge_create (); codeme->sz = bytes_read; memcpy (codeme->data, buffer, codeme->sz); ret = kr_vpx_decode (vpx_dec, medium, codeme); //printf ("Vpx dec ret was %d", ret); kr_codeme_kludge_destroy (&codeme); codeme = kr_codeme_kludge_create (); ret = kr_vpx_encode (vpx_enc, codeme, medium); //printf ("Vpx enc ret was %d", ret); kr_medium_kludge_destroy (&medium); bytes_read = codeme->sz; memcpy (buffer, codeme->data, codeme->sz); kr_codeme_kludge_destroy (&codeme); */ kr_mkv_add_video (new_mkv, out_track, buffer, bytes_read, keyframe); } if (track == 2) { medium = kr_medium_kludge_create (); codeme = kr_codeme_kludge_create (); codeme->sz = bytes_read; memcpy (codeme->data, buffer, codeme->sz); kr_vorbis_decode (vorbis_dec, medium, codeme); kr_codeme_kludge_destroy (&codeme); if (medium->a.count > 0) { do { codeme = kr_codeme_kludge_create (); ret = kr_vorbis_encode (vorbis_enc, codeme, medium); if (ret == 1) { kr_mkv_add_audio (new_mkv, 2, codeme->data, codeme->sz, codeme->count); } kr_medium_kludge_destroy (&medium); kr_codeme_kludge_destroy (&codeme); } while (ret == 1); } //} } } //----------------------------- krad_vorbis_decoder_destroy (&vorbis_dec); //new_mkv->tracks[VIDEO_TRACK].fps_numerator = 30000; kr_mkv_destroy (&mkv); mkv = kr_mkv_open_file (file2); printf ("FPS %d/%d RES: %dx%d\n", mkv->tracks[VIDEO_TRACK].fps_numerator, mkv->tracks[VIDEO_TRACK].fps_denominator, mkv->tracks[VIDEO_TRACK].width, mkv->tracks[VIDEO_TRACK].height); if (mkv == NULL) { fprintf (stderr, "Could not open %s\n", file2); exit (1); } header.count = mkv->tracks[AUDIO_TRACK].headers; header.data[0] = mkv->tracks[AUDIO_TRACK].header[0]; header.sz[0] = mkv->tracks[AUDIO_TRACK].header_len[0]; header.data[1] = mkv->tracks[AUDIO_TRACK].header[1]; header.sz[1] = mkv->tracks[AUDIO_TRACK].header_len[1]; header.data[2] = mkv->tracks[AUDIO_TRACK].header[2]; header.sz[2] = mkv->tracks[AUDIO_TRACK].header_len[2]; vorbis_dec = krad_vorbis_decoder_create (&header); while ((bytes_read = kr_mkv_read_packet (mkv, &track, &timecode, &flags, buffer)) > 0) { printk ("Read packet %d track %d %d bytes", packets++, track, bytes_read); //fflush (stdout); if (flags == 0x80) { keyframe = 1; } else { keyframe = 0; } if (track == 1) { /* medium = kr_medium_kludge_create (); codeme = kr_codeme_kludge_create (); codeme->sz = bytes_read; memcpy (codeme->data, buffer, codeme->sz); ret = kr_vpx_decode (vpx_dec, medium, codeme); //printf ("Vpx dec ret was %d", ret); kr_codeme_kludge_destroy (&codeme); codeme = kr_codeme_kludge_create (); ret = kr_vpx_encode (vpx_enc, codeme, medium); //printf ("Vpx enc ret was %d", ret); kr_medium_kludge_destroy (&medium); bytes_read = codeme->sz; memcpy (buffer, codeme->data, codeme->sz); kr_codeme_kludge_destroy (&codeme); */ kr_mkv_add_video (new_mkv, out_track, buffer, bytes_read, keyframe); } if (track == 2) { medium = kr_medium_kludge_create (); codeme = kr_codeme_kludge_create (); codeme->sz = bytes_read; memcpy (codeme->data, buffer, codeme->sz); kr_vorbis_decode (vorbis_dec, medium, codeme); kr_codeme_kludge_destroy (&codeme); if (medium->a.count > 0) { do { codeme = kr_codeme_kludge_create (); ret = kr_vorbis_encode (vorbis_enc, codeme, medium); if (ret == 1) { kr_mkv_add_audio (new_mkv, 2, codeme->data, codeme->sz, codeme->count); } kr_medium_kludge_destroy (&medium); kr_codeme_kludge_destroy (&codeme); } while (ret == 1); } //} } } medium = kr_medium_kludge_create (); codeme = kr_codeme_kludge_create (); ret = kr_vorbis_encode (vorbis_enc, codeme, medium); if (codeme->sz > 0) { printk ("Got 1 another sized %zu with %d samples", codeme->sz, codeme->count); kr_mkv_add_audio (new_mkv, 2, codeme->data, codeme->sz, codeme->count); } kr_codeme_kludge_destroy (&codeme); kr_medium_kludge_destroy (&medium); do { codeme = kr_codeme_kludge_create (); ret = kr_vorbis_encode (vorbis_enc, codeme, medium); if (ret == 1) { kr_mkv_add_audio (new_mkv, 2, codeme->data, codeme->sz, codeme->count); } kr_codeme_kludge_destroy (&codeme); } while (ret == 1); //---------------------------- printf ("\nDone.\n"); krad_vpx_encoder_destroy (&vpx_enc); krad_vpx_decoder_destroy (&vpx_dec); krad_vorbis_decoder_destroy (&vorbis_dec); krad_vorbis_encoder_destroy (&vorbis_enc); kr_mkv_destroy (&new_mkv); kr_mkv_destroy (&mkv); free (buffer); free (vbuffer); free (vbuffer2); }