void avi_destroy_context(avi_Context* AVI) { //clean up io_destroy_writer(AVI->writer); avi_RIFF* riff = avi_get_last_riff(AVI); while(riff->previous != NULL) //from end to start { avi_RIFF* prev_riff = riff->previous; free(riff); riff = prev_riff; AVI->riff_list_size--; } free(riff); //free the last one; destroy_stream_list(AVI->stream_list, &AVI->stream_list_size); //free avi_Context free(AVI); }
int main(int argc, char **argv) { int stop_at = 0; char *yuv_path; if (argc == 2) { yuv_path = argv[1]; } else if (argc == 3) { yuv_path = argv[1]; stop_at = atoi(argv[2]); } else { printf("usage: %s input [max frames]\n", argv[0]); return -1; } printf("[test] initializing streams list\n"); printf("[test] init_stream_list\n"); stream_list_t *streams = init_stream_list(); printf("[test] init_stream\n"); stream_data_t *stream = init_stream(VIDEO, OUTPUT, 0, ACTIVE, "i2catrocks"); printf("[test] set_stream_video_data\n"); printf("[test] add_stream\n"); add_stream(streams, stream); printf("[test] initializing transmitter\n"); transmitter_t *transmitter = init_transmitter(streams, 20.0); start_transmitter(transmitter); rtsp_serv_t *server; server = malloc(sizeof(rtsp_serv_t)); server->port = 8554; server->streams = streams; server->transmitter = transmitter; init_encoder(stream->video); c_start_server(server); c_update_server(server); // Stuff ... AVFormatContext *pformat_ctx = avformat_alloc_context(); AVCodecContext codec_ctx; int video_stream = -1; av_register_all(); int width = 1280; int height = 534; load_video(yuv_path, pformat_ctx, &codec_ctx, &video_stream); uint8_t *b1 = (uint8_t *)av_malloc(avpicture_get_size(codec_ctx.pix_fmt, codec_ctx.width, codec_ctx.height)*sizeof(uint8_t)); int counter = 0; struct timeval a, b; video_data_frame_t *decoded_frame; while(1) { gettimeofday(&a, NULL); int ret = read_frame(pformat_ctx, video_stream, &codec_ctx, b1); if (stop_at > 0 && counter == stop_at) { break; } if (ret == 0) { counter++; decoded_frame = curr_in_frame(stream->video->decoded_frames); if (decoded_frame == NULL){ continue; } decoded_frame->buffer_len = vc_get_linesize(width, RGB)*height; memcpy(decoded_frame->buffer, b1, decoded_frame->buffer_len); put_frame(stream->video->decoded_frames); } else { break; } gettimeofday(&b, NULL); long diff = (b.tv_sec - a.tv_sec)*1000000 + b.tv_usec - a.tv_usec; if (diff < 40000) { usleep(40000 - diff); } else { usleep(0); } } debug_msg(" deallocating resources and terminating threads\n"); av_free(pformat_ctx); av_free(b1); debug_msg(" done!\n"); stop_transmitter(transmitter); destroy_stream_list(streams); return 0; }
int main() { fprintf(stderr, "Starting audio_receiver test...\n"); // Open files to write audio if ((F_audio1 = fopen(name_audio1, "wb")) == NULL) { perror(name_audio1); exit(errno); } if ((F_audio2 = fopen(name_audio2, "wb")) == NULL) { perror(name_audio2); exit(errno); } // General pourpouse variables. time_t start, stop; audio_frame2 *audio_frame; // Receiver configuration stream_list_t *video_stream_list = init_stream_list(); // Not used stream_list_t *audio_stream_list = init_stream_list(); receiver_t *receiver = init_receiver(video_stream_list, audio_stream_list, 5004, 5006); // First stream and participant configuration participant_data_t *p1 = init_participant(1, INPUT, NULL, 0); stream_data_t *stream1 = init_stream(AUDIO, INPUT, rand(), I_AWAIT, 25.0, "Stream1"); add_participant_stream(stream1, p1); add_stream(receiver->audio_stream_list, stream1); fprintf(stderr, " ·Stream1 configuration: 1 bps, 32000Hz, 1 channel, mulaw\n"); ap_config(stream1->audio, 1, 32000, 1, AC_MULAW); ap_worker_start(stream1->audio); // Second stream and participant configuration participant_data_t *p2 = init_participant(2, INPUT, NULL, 0); stream_data_t *stream2 = init_stream(AUDIO, INPUT, rand(), I_AWAIT, 25.0, "Stream2"); add_participant_stream(stream2, p2); add_stream(receiver->audio_stream_list, stream2); fprintf(stderr, " ·Stream2 configuration: 1 bps, 8000Hz, 1 channel, mulaw\n"); ap_config(stream2->audio, 1, 8000, 1, AC_MULAW); ap_worker_start(stream2->audio); if (start_receiver(receiver)) { fprintf(stderr, " ·Receiver started!\n"); #ifdef STREAM1 // STREAM1 recording block fprintf(stderr, " ·Waiting for audio_frame2 data\n"); while (stream1->audio->decoded_cq->level == CIRCULAR_QUEUE_EMPTY) { #ifdef QUEUE_PRINT print_cq_status(stream1->audio->decoded_cq, "wait stream1"); #endif } #ifdef QUEUE_PRINT print_cq_status(stream1->audio->decoded_cq, "continue stream1"); #endif fprintf(stderr, " ·Copying to file... "); start = time(NULL); stop = start + RECORD_TIME; while (time(NULL) < stop) { // RECORD_TIME seconds loop audio_frame = cq_get_front(stream1->audio->decoded_cq); if (audio_frame != NULL) { fwrite(audio_frame->data[0], audio_frame->data_len[0], 1, F_audio1); cq_remove_bag(stream1->audio->decoded_cq); } } fprintf(stderr, "Done!\n"); #endif //STREAM1 #ifdef STREAM2 // STREAM2 recording block fprintf(stderr, " ·Waiting for audio_frame2 data\n"); while (stream2->audio->decoded_cq->level == CIRCULAR_QUEUE_EMPTY) { #ifdef QUEUE_PRINT print_cq_status(stream2->audio->decoded_cq, "wait stream2"); #endif } #ifdef QUEUE_PRINT print_cq_status(stream2->audio->decoded_cq, "continue stream2"); #endif fprintf(stderr, " ·Copying to file... "); start = time(NULL); stop = start + RECORD_TIME; while (time(NULL) < stop) { // RECORD_TIME seconds loop audio_frame = cq_get_front(stream2->audio->decoded_cq); if (audio_frame != NULL) { fwrite(audio_frame->data[0], audio_frame->data_len[0], 1, F_audio2); cq_remove_bag(stream2->audio->decoded_cq); } } fprintf(stderr, "Done!\n"); #endif //STREAM2 // Finish and destroy objects stop_receiver(receiver); destroy_receiver(receiver); fprintf(stderr, " ·Receiver stopped\n"); destroy_stream_list(video_stream_list); destroy_stream_list(audio_stream_list); } if (fclose(F_audio1) != 0) { perror(name_audio1); exit(-1); } if (fclose(F_audio2) != 0) { perror(name_audio2); exit(-1); } fprintf(stderr, "Finished\n"); }
int main() { fprintf(stderr, "Starting audio_rec_trans test (max %i seconds)\n", LIVE_TIME); fprintf(stderr, "Issue kill -10 %i to dinamically add a couple of streams.\n", getpid()); // Attach the handlers to the signals and prepare block stuff. signal(SIGINT, finish_handler); signal(SIGALRM, finish_handler); signal(SIGUSR1, action_handler); // Start live time alarm alarm(LIVE_TIME); // Receiver startup fprintf(stderr, " ·Configuring receiver (listen at %i)\n", RECEIVER_AUDIO_PORT); receiver = init_receiver(init_stream_list(), init_stream_list(), RECEIVER_VIDEO_PORT, RECEIVER_AUDIO_PORT); start_receiver(receiver); add_receiver_entity(); // Transmitter startup fprintf(stderr, " ·Configuring transmitter\n"); transmitter = init_transmitter(init_stream_list(), init_stream_list(), 25.0); start_transmitter(transmitter); add_transmitter_entity(TRANSMITTER_IP_1, TRANSMITTER_PORT_1); // Temporal variables and initializations stream_data_t *in_stream_couple1, *out_stream_couple1, *in_stream_couple2, *out_stream_couple2; bool cross = false; bool verbose = true; time_t lap_time = time(NULL) + SWITCH_TIME; // Main loop fprintf(stderr, " ·Forwarding audio and switching the streams every %i seconds...\n", SWITCH_TIME); while(!stop) { // Lap time control if (time(NULL) > lap_time) { lap_time = time(NULL) + SWITCH_TIME; cross = !cross; verbose = true; fprintf(stderr, "Done!\n"); } // Cross sending control if (!cross) { in_stream_couple1 = receiver->audio_stream_list->first; out_stream_couple1 = transmitter->audio_stream_list->first; in_stream_couple2 = in_stream_couple1->next; out_stream_couple2 = out_stream_couple1->next; } else { in_stream_couple1 = receiver->audio_stream_list->first; out_stream_couple1 = transmitter->audio_stream_list->last; in_stream_couple2 = in_stream_couple1->next; out_stream_couple2 = out_stream_couple1->prev; } // Forward audio from receiver to transmitter if (in_stream_couple2 == NULL || out_stream_couple2 == NULL) { // One couple case (first to first). if (verbose) { sprintf(msg, " ·Sending %s -> %s ", in_stream_couple1->stream_name, out_stream_couple1->stream_name); } audio_frame_forward(in_stream_couple1, out_stream_couple1); } else { // Two couples case (first to first, next to next). if (verbose) { sprintf(msg, " ·Sending %s -> %s and %s -> %s ", in_stream_couple1->stream_name, out_stream_couple1->stream_name, in_stream_couple2->stream_name, out_stream_couple2->stream_name); } audio_frame_forward(in_stream_couple1, out_stream_couple1); audio_frame_forward(in_stream_couple2, out_stream_couple2); } // Print information message if (verbose) { fprintf(stderr, "%s", msg); verbose = false; } //Try to not send all the audio suddently. usleep(SEND_TIME); } fprintf(stderr, "Done!\n"); // Finish and destroy receiver objects stop_receiver(receiver); destroy_stream_list(receiver->video_stream_list); destroy_stream_list(receiver->audio_stream_list); destroy_receiver(receiver); fprintf(stderr, " ·Receiver stopped\n"); // Finish and destroy transmitter objects stop_transmitter(transmitter); destroy_stream_list(transmitter->video_stream_list); destroy_stream_list(transmitter->audio_stream_list); destroy_transmitter(transmitter); fprintf(stderr, " ·Transmitter stopped\n"); fprintf(stderr, "Finished\n"); }