From c0d335c2f76f0edd538be59ff605aaedfc418086 Mon Sep 17 00:00:00 2001 From: "Castillo, Gerard" Date: Wed, 25 Sep 2013 11:23:41 +0200 Subject: [PATCH] source formatted --- src/rtp/rtpdec_h264.c | 264 ++++++------ src/rtp/rtpdec_h264.h | 3 +- src/video_capture/rtsp.c | 895 ++++++++++++++++++++------------------- src/video_capture/rtsp.h | 13 +- 4 files changed, 610 insertions(+), 565 deletions(-) diff --git a/src/rtp/rtpdec_h264.c b/src/rtp/rtpdec_h264.c index a8dda22a0..00170b522 100644 --- a/src/rtp/rtpdec_h264.c +++ b/src/rtp/rtpdec_h264.c @@ -53,162 +53,164 @@ #include "video_capture/rtsp.h" #include "rtp/pbuf.h" -struct recieved_data{ - uint32_t buffer_len;//[MAX_SUBSTREAMS]; +struct recieved_data { + uint32_t buffer_len; //[MAX_SUBSTREAMS]; //uint32_t buffer_num;//[MAX_SUBSTREAMS]; - char *frame_buffer;//[MAX_SUBSTREAMS]; + char *frame_buffer; //[MAX_SUBSTREAMS]; }; static const uint8_t start_sequence[] = { 0, 0, 0, 1 }; -int decode_frame_h264(struct coded_data *cdata, void *rx_data); +int +decode_frame_h264(struct coded_data *cdata, void *rx_data); -int decode_frame_h264(struct coded_data *cdata, void *rx_data) { - rtp_packet *pckt = NULL; - struct coded_data *orig = cdata; +int +decode_frame_h264(struct coded_data *cdata, void *rx_data) { + rtp_packet *pckt = NULL; + struct coded_data *orig = cdata; - uint8_t nal; - uint8_t type; + uint8_t nal; + uint8_t type; - int pass; - int total_length = 0; + int pass; + int total_length = 0; - char *dst = NULL; - int src_len; + char *dst = NULL; + int src_len; - struct recieved_data *buffers = (struct recieved_data *) rx_data; + struct recieved_data *buffers = (struct recieved_data *) rx_data; - for (pass = 0; pass < 2; pass++) { + for (pass = 0; pass < 2; pass++) { - if (pass > 0) { - cdata = orig; - buffers->buffer_len = total_length; - dst = buffers->frame_buffer + total_length; - } + if (pass > 0) { + cdata = orig; + buffers->buffer_len = total_length; + dst = buffers->frame_buffer + total_length; + } - while (cdata != NULL) { - pckt = cdata->data; + while (cdata != NULL) { + pckt = cdata->data; - if (pckt->pt != PT_H264) { - error_msg("Wrong Payload type: %u\n", pckt->pt); - return FALSE; - } + if (pckt->pt != PT_H264) { + error_msg("Wrong Payload type: %u\n", pckt->pt); + return FALSE; + } - nal = (uint8_t) pckt->data[0]; - type = nal & 0x1f; + nal = (uint8_t) pckt->data[0]; + type = nal & 0x1f; - if (type >= 1 && type <= 23) { - type = 1; - } + if (type >= 1 && type <= 23) { + type = 1; + } - const uint8_t *src = NULL; + const uint8_t *src = NULL; - switch (type) { - case 0: - case 1: - if (pass == 0) { - debug_msg("NAL type 1\n"); - total_length += sizeof(start_sequence) + pckt->data_len; - } else { - dst -= pckt->data_len + sizeof(start_sequence); - memcpy(dst, start_sequence, sizeof(start_sequence)); - memcpy(dst + sizeof(start_sequence), pckt->data, pckt->data_len); - unsigned char *dst2 = (unsigned char *)dst; - } - break; - case 24: - src = (const uint8_t *) pckt->data; - src_len = pckt->data_len; + switch (type) { + case 0: + case 1: + if (pass == 0) { + debug_msg("NAL type 1\n"); + total_length += sizeof(start_sequence) + pckt->data_len; + } else { + dst -= pckt->data_len + sizeof(start_sequence); + memcpy(dst, start_sequence, sizeof(start_sequence)); + memcpy(dst + sizeof(start_sequence), pckt->data, pckt->data_len); + unsigned char *dst2 = (unsigned char *)dst; + } + break; + case 24: + src = (const uint8_t *) pckt->data; + src_len = pckt->data_len; - src++; - src_len--; + src++; + src_len--; - while (src_len > 2) { - //TODO: Not properly tested - uint16_t nal_size; - memcpy(&nal_size, src, sizeof(uint16_t)); + while (src_len > 2) { + //TODO: Not properly tested + uint16_t nal_size; + memcpy(&nal_size, src, sizeof(uint16_t)); - src += 2; - src_len -= 2; + src += 2; + src_len -= 2; - if (nal_size <= src_len) { - if (pass == 0) { - total_length += sizeof(start_sequence) + nal_size; - } else { - dst -= nal_size + sizeof(start_sequence); - memcpy(dst, start_sequence, sizeof(start_sequence)); - memcpy(dst + sizeof(start_sequence), src, nal_size); - } - } else { - error_msg("NAL size exceeds length: %u %d\n", nal_size, src_len); - return FALSE; - } - src += nal_size; - src_len -= nal_size; + if (nal_size <= src_len) { + if (pass == 0) { + total_length += sizeof(start_sequence) + nal_size; + } else { + dst -= nal_size + sizeof(start_sequence); + memcpy(dst, start_sequence, sizeof(start_sequence)); + memcpy(dst + sizeof(start_sequence), src, nal_size); + } + } else { + error_msg("NAL size exceeds length: %u %d\n", nal_size, src_len); + return FALSE; + } + src += nal_size; + src_len -= nal_size; - if (src_len < 0) { - error_msg("Consumed more bytes than we got! (%d)\n", src_len); - return FALSE; - } - } - break; + if (src_len < 0) { + error_msg("Consumed more bytes than we got! (%d)\n", src_len); + return FALSE; + } + } + break; - case 25: - case 26: - case 27: - case 29: - error_msg("Unhandled NAL type\n"); - return FALSE; - case 28: - src = (const uint8_t *) pckt->data; - src_len = pckt->data_len; + case 25: + case 26: + case 27: + case 29: + error_msg("Unhandled NAL type\n"); + return FALSE; + case 28: + src = (const uint8_t *) pckt->data; + src_len = pckt->data_len; - src++; - src_len--; + src++; + src_len--; - if (src_len > 1) { - uint8_t fu_header = *src; - uint8_t start_bit = fu_header >> 7; - //uint8_t end_bit = (fu_header & 0x40) >> 6; - uint8_t nal_type = fu_header & 0x1f; - uint8_t reconstructed_nal; + if (src_len > 1) { + uint8_t fu_header = *src; + uint8_t start_bit = fu_header >> 7; + //uint8_t end_bit = (fu_header & 0x40) >> 6; + uint8_t nal_type = fu_header & 0x1f; + uint8_t reconstructed_nal; - // Reconstruct this packet's true nal; only the data follows. - /* The original nal forbidden bit and NRI are stored in this - * packet's nal. */ - reconstructed_nal = nal & 0xe0; - reconstructed_nal |= nal_type; + // Reconstruct this packet's true nal; only the data follows. + /* The original nal forbidden bit and NRI are stored in this + * packet's nal. */ + reconstructed_nal = nal & 0xe0; + reconstructed_nal |= nal_type; - // skip the fu_header - src++; - src_len--; + // skip the fu_header + src++; + src_len--; - if (pass == 0) { - if (start_bit) { - total_length += sizeof(start_sequence) + sizeof(reconstructed_nal) + src_len; - } else { - total_length += src_len; - } - } else { - if (start_bit) { - dst -= sizeof(start_sequence) + sizeof(reconstructed_nal) + src_len; - memcpy(dst, start_sequence, sizeof(start_sequence)); - memcpy(dst + sizeof(start_sequence), &reconstructed_nal, sizeof(reconstructed_nal)); - memcpy(dst + sizeof(start_sequence) + sizeof(reconstructed_nal), src, src_len); - } else { - dst -= src_len; - memcpy(dst, src, src_len); - } - } - } else { - error_msg("Too short data for FU-A H264 RTP packet\n"); - return FALSE; - } - break; - default: - error_msg("Unknown NAL type\n"); - return FALSE; - } - cdata = cdata->nxt; - } - } - return TRUE; + if (pass == 0) { + if (start_bit) { + total_length += sizeof(start_sequence) + sizeof(reconstructed_nal) + src_len; + } else { + total_length += src_len; + } + } else { + if (start_bit) { + dst -= sizeof(start_sequence) + sizeof(reconstructed_nal) + src_len; + memcpy(dst, start_sequence, sizeof(start_sequence)); + memcpy(dst + sizeof(start_sequence), &reconstructed_nal, sizeof(reconstructed_nal)); + memcpy(dst + sizeof(start_sequence) + sizeof(reconstructed_nal), src, src_len); + } else { + dst -= src_len; + memcpy(dst, src, src_len); + } + } + } else { + error_msg("Too short data for FU-A H264 RTP packet\n"); + return FALSE; + } + break; + default: + error_msg("Unknown NAL type\n"); + return FALSE; + } + cdata = cdata->nxt; + } + } + return TRUE; } diff --git a/src/rtp/rtpdec_h264.h b/src/rtp/rtpdec_h264.h index 2f1318707..be59eac81 100644 --- a/src/rtp/rtpdec_h264.h +++ b/src/rtp/rtpdec_h264.h @@ -44,6 +44,7 @@ #ifndef _RTP_DEC_H264_H #define _RTP_DEC_H264_H -int decode_frame_h264(struct coded_data *cdata, void *rx_data); +int +decode_frame_h264(struct coded_data *cdata, void *rx_data); #endif diff --git a/src/video_capture/rtsp.c b/src/video_capture/rtsp.c index a6ed56f4a..b28ccd1ce 100644 --- a/src/video_capture/rtsp.c +++ b/src/video_capture/rtsp.c @@ -79,13 +79,12 @@ struct coded_data; //TODO set lower initial video recv buffer size (to find the minimal) #define INITIAL_VIDEO_RECV_BUFFER_SIZE ((0.1*1920*1080)*110/100) //command line net.core setup: sysctl -w net.core.rmem_max=9123840 - //#define MAX_SUBSTREAMS 1 -struct recieved_data{ - uint32_t buffer_len;//[MAX_SUBSTREAMS]; +struct recieved_data { + uint32_t buffer_len; //[MAX_SUBSTREAMS]; //uint32_t buffer_num;//[MAX_SUBSTREAMS]; - char *frame_buffer;//[MAX_SUBSTREAMS]; + char *frame_buffer; //[MAX_SUBSTREAMS]; }; /* error handling macros */ @@ -102,158 +101,174 @@ exit(0); \ } /* send RTSP GET_PARAMETERS request */ -static void rtsp_get_parameters(CURL *curl, const char *uri); +static void +rtsp_get_parameters(CURL *curl, const char *uri); /* send RTSP OPTIONS request */ -static void rtsp_options(CURL *curl, const char *uri); +static void +rtsp_options(CURL *curl, const char *uri); /* send RTSP DESCRIBE request and write sdp response to a file */ -static void rtsp_describe(CURL *curl, const char *uri, const char *sdp_filename); +static void +rtsp_describe(CURL *curl, const char *uri, const char *sdp_filename); /* send RTSP SETUP request */ -static void rtsp_setup(CURL *curl, const char *uri, const char *transport); +static void +rtsp_setup(CURL *curl, const char *uri, const char *transport); /* send RTSP PLAY request */ -static void rtsp_play(CURL *curl, const char *uri, const char *range); +static void +rtsp_play(CURL *curl, const char *uri, const char *range); /* send RTSP TEARDOWN request */ -static void rtsp_teardown(CURL *curl, const char *uri); +static void +rtsp_teardown(CURL *curl, const char *uri); /* convert url into an sdp filename */ -static void get_sdp_filename(const char *url, char *sdp_filename); +static void +get_sdp_filename(const char *url, char *sdp_filename); /* scan sdp file for media control attribute */ -static void get_media_control_attribute(const char *sdp_filename, char *control); +static void +get_media_control_attribute(const char *sdp_filename, char *control); /* scan sdp file for incoming codec */ -static int set_codec_attribute_from_incoming_media(const char *sdp_filename, void *state); +static int +set_codec_attribute_from_incoming_media(const char *sdp_filename, void *state); -static int get_nals(const char *sdp_filename, char *nals); +static int +get_nals(const char *sdp_filename, char *nals); -static int init_rtsp(char* rtsp_uri, int rtsp_port,void *state, char* nals); +static int +init_rtsp(char* rtsp_uri, int rtsp_port, void *state, char* nals); -static int init_decompressor(void *state); +static int +init_decompressor(void *state); -static void * vidcap_rtsp_thread(void *args); -static void show_help(void); +static void * +vidcap_rtsp_thread(void *args); +static void +show_help(void); -FILE *F_video_rtsp=NULL; +FILE *F_video_rtsp = NULL; /** * @struct rtsp_state */ struct rtsp_state { - char *nals; - int nals_size; - char *data; //nals + data - uint32_t *in_codec; + char *nals; + int nals_size; + char *data; //nals + data + uint32_t *in_codec; - struct timeval t0,t; - int frames; - struct video_frame *frame; - struct tile *tile; + struct timeval t0, t; + int frames; + struct video_frame *frame; + struct tile *tile; // struct audio_frame audio; - int width; - int height; + int width; + int height; - struct recieved_data *rx_data; - bool new_frame; - bool pbuf_removed; + struct recieved_data *rx_data; + bool new_frame; + bool pbuf_removed; - bool decompress; + bool decompress; - struct rtp *device; - struct pdb *participants; - struct pdb_e *cp; - double rtcp_bw; - int ttl; - char *addr; - char *mcast_if; - struct timeval curr_time; - struct timeval timeout; - struct timeval prev_time; - struct timeval start_time; - int required_connections; - uint32_t timestamp; + struct rtp *device; + struct pdb *participants; + struct pdb_e *cp; + double rtcp_bw; + int ttl; + char *addr; + char *mcast_if; + struct timeval curr_time; + struct timeval timeout; + struct timeval prev_time; + struct timeval start_time; + int required_connections; + uint32_t timestamp; // int play_audio_frame; // struct timeval last_audio_time; // unsigned int grab_audio:1; - pthread_t rtsp_thread_id; //the worker_id + pthread_t rtsp_thread_id; //the worker_id pthread_mutex_t lock; pthread_cond_t worker_cv; volatile bool worker_waiting; pthread_cond_t boss_cv; volatile bool boss_waiting; - volatile bool should_exit; + volatile bool should_exit; - struct state_decompress *sd; - struct video_desc des; - char * out_frame; + struct state_decompress *sd; + struct video_desc des; + char * out_frame; - CURL *curl; - char *uri; - int port; - float fps; + CURL *curl; + char *uri; + int port; + float fps; }; - - -static void show_help(){ - printf("[rtsp] usage:\n"); - printf("\t-t rtsp::::[:]\n"); - printf("\t\t uri server without 'rtsp://' \n"); - printf("\t\t receiver port number \n"); - printf("\t\t receiver width number \n"); - printf("\t\t receiver height number \n"); - printf("\t\t receiver decompress boolean [true|false] - default: false - no decompression active\n\n"); +static void +show_help() { + printf("[rtsp] usage:\n"); + printf("\t-t rtsp::::[:]\n"); + printf("\t\t uri server without 'rtsp://' \n"); + printf("\t\t receiver port number \n"); + printf("\t\t receiver width number \n"); + printf("\t\t receiver height number \n"); + printf( + "\t\t receiver decompress boolean [true|false] - default: false - no decompression active\n\n"); } -static void * vidcap_rtsp_thread(void *arg) -{ - struct rtsp_state *s; - s = (struct rtsp_state *)arg; +static void * +vidcap_rtsp_thread(void *arg) { + struct rtsp_state *s; + s = (struct rtsp_state *) arg; - gettimeofday(&s->start_time, NULL); - gettimeofday(&s->prev_time, NULL); + gettimeofday(&s->start_time, NULL); + gettimeofday(&s->prev_time, NULL); - while(!s->should_exit){ - gettimeofday(&s->curr_time, NULL); - s->timestamp = tv_diff(s->curr_time, s->start_time) * 90000; + while (!s->should_exit) { + gettimeofday(&s->curr_time, NULL); + s->timestamp = tv_diff(s->curr_time, s->start_time) * 90000; - if(tv_diff(s->curr_time, s->prev_time) >= 30){ - rtsp_get_parameters(s->curl, s->uri); - gettimeofday(&s->prev_time, NULL); - } + if (tv_diff(s->curr_time, s->prev_time) >= 30) { + rtsp_get_parameters(s->curl, s->uri); + gettimeofday(&s->prev_time, NULL); + } - rtp_update(s->device, s->curr_time); - //TODO rtcp communication between ug and rtsp server? - //rtp_send_ctrl(s->device, s->timestamp, 0, s->curr_time); + rtp_update(s->device, s->curr_time); + //TODO rtcp communication between ug and rtsp server? + //rtp_send_ctrl(s->device, s->timestamp, 0, s->curr_time); - s->timeout.tv_sec = 0; - s->timeout.tv_usec = 10000; + s->timeout.tv_sec = 0; + s->timeout.tv_usec = 10000; - if(!rtp_recv_r(s->device, &s->timeout, s->timestamp)){ + if (!rtp_recv_r(s->device, &s->timeout, s->timestamp)) { pdb_iter_t it; s->cp = pdb_iter_init(s->participants, &it); s->pbuf_removed = true; - while (s->cp != NULL ) { - if (pbuf_decode(s->cp->playout_buffer, s->curr_time, decode_frame_h264, s->rx_data)) { + while (s->cp != NULL) { + if (pbuf_decode(s->cp->playout_buffer, s->curr_time, + decode_frame_h264, s->rx_data)) + { pthread_mutex_lock(&s->lock); { - while(s->new_frame && !s->should_exit) { - s->worker_waiting = true; - pthread_cond_wait(&s->worker_cv, &s->lock); - s->worker_waiting = false; - } - s->new_frame = true; + while (s->new_frame && !s->should_exit) { + s->worker_waiting = true; + pthread_cond_wait(&s->worker_cv, &s->lock); + s->worker_waiting = false; + } + s->new_frame = true; - if(s->boss_waiting) - pthread_cond_signal(&s->boss_cv); + if (s->boss_waiting) + pthread_cond_signal(&s->boss_cv); } pthread_mutex_unlock(&s->lock); } @@ -262,85 +277,92 @@ static void * vidcap_rtsp_thread(void *arg) } pdb_iter_done(&it); } - } - return NULL; + } + return NULL; } -struct video_frame *vidcap_rtsp_grab(void *state, struct audio_frame **audio){ - struct rtsp_state *s; - s = (struct rtsp_state *)state; +struct video_frame * +vidcap_rtsp_grab(void *state, struct audio_frame **audio) { + struct rtsp_state *s; + s = (struct rtsp_state *) state; - *audio = NULL; + *audio = NULL; pthread_mutex_lock(&s->lock); { - while(!s->new_frame) { - s->boss_waiting = true; - pthread_cond_wait(&s->boss_cv, &s->lock); - s->boss_waiting = false; - } + while (!s->new_frame) { + s->boss_waiting = true; + pthread_cond_wait(&s->boss_cv, &s->lock); + s->boss_waiting = false; + } - gettimeofday(&s->curr_time, NULL); + gettimeofday(&s->curr_time, NULL); - s->frame->tiles[0].data_len = s->rx_data->buffer_len; + s->frame->tiles[0].data_len = s->rx_data->buffer_len; - memcpy(s->data+s->nals_size,s->rx_data->frame_buffer,s->rx_data->buffer_len); + memcpy(s->data + s->nals_size, s->rx_data->frame_buffer, + s->rx_data->buffer_len); - memcpy(s->frame->tiles[0].data,s->data,s->rx_data->buffer_len + s->nals_size); - s->frame->tiles[0].data_len += s->nals_size; + memcpy(s->frame->tiles[0].data, s->data, + s->rx_data->buffer_len + s->nals_size); + s->frame->tiles[0].data_len += s->nals_size; - if(s->decompress){ - decompress_frame(s->sd,(unsigned char *) s->out_frame,(unsigned char *)s->frame->tiles[0].data,s->rx_data->buffer_len + s->nals_size,0); - s->frame->tiles[0].data = s->out_frame; //TODO memcpy? - s->frame->tiles[0].data_len = vc_get_linesize(s->des.width, UYVY)*s->des.height;//TODO reconfigurable - } - s->new_frame = false; + if (s->decompress) { + decompress_frame(s->sd, (unsigned char *) s->out_frame, + (unsigned char *) s->frame->tiles[0].data, + s->rx_data->buffer_len + s->nals_size, 0); + s->frame->tiles[0].data = s->out_frame; //TODO memcpy? + s->frame->tiles[0].data_len = vc_get_linesize(s->des.width, UYVY) + * s->des.height; //TODO reconfigurable + } + s->new_frame = false; - if(s->worker_waiting) { - pthread_cond_signal(&s->worker_cv); - } + if (s->worker_waiting) { + pthread_cond_signal(&s->worker_cv); + } } pthread_mutex_unlock(&s->lock); gettimeofday(&s->t, NULL); - double seconds = tv_diff(s->t, s->t0); - if (seconds >= 5) { - float fps = s->frames / seconds; - fprintf(stderr, "[rtsp capture] %d frames in %g seconds = %g FPS\n", - s->frames, seconds, fps); - s->t0 = s->t; - s->frames = 0; - //Threshold of 1fps in order to update fps parameter - if(fps > s->fps + 1 || fps < s->fps - 1){ - debug_msg("\n[rtsp] updating fps from rtsp server stream... now = %f , before = %f\n",fps,s->fps); - s->frame->fps = fps; - s->fps = fps; - } - } - s->frames++; + double seconds = tv_diff(s->t, s->t0); + if (seconds >= 5) { + float fps = s->frames / seconds; + fprintf(stderr, "[rtsp capture] %d frames in %g seconds = %g FPS\n", + s->frames, seconds, fps); + s->t0 = s->t; + s->frames = 0; + //Threshold of 1fps in order to update fps parameter + if (fps > s->fps + 1 || fps < s->fps - 1) { + debug_msg( + "\n[rtsp] updating fps from rtsp server stream... now = %f , before = %f\n",fps,s->fps); + s->frame->fps = fps; + s->fps = fps; + } + } + s->frames++; - return s->frame; + return s->frame; } - -void *vidcap_rtsp_init(const struct vidcap_params *params){ +void * +vidcap_rtsp_init(const struct vidcap_params *params) { struct rtsp_state *s; s = calloc(1, sizeof(struct rtsp_state)); if (!s) - return NULL; + return NULL; char *save_ptr = NULL; gettimeofday(&s->t0, NULL); - s->frames=0; + s->frames = 0; s->nals = malloc(1024); - s->addr="127.0.0.1"; + s->addr = "127.0.0.1"; s->device = NULL; s->rtcp_bw = 5 * 1024 * 1024; /* FIXME */ - s->ttl = 255; + s->ttl = 255; s->mcast_if = NULL; s->required_connections = 1; @@ -348,7 +370,8 @@ void *vidcap_rtsp_init(const struct vidcap_params *params){ s->timeout.tv_sec = 0; s->timeout.tv_usec = 10000; - s->device = (struct rtp *) malloc((s->required_connections) * sizeof(struct rtp *)); + s->device = (struct rtp *) malloc( + (s->required_connections) * sizeof(struct rtp *)); s->participants = pdb_init(); s->rx_data = malloc(sizeof(struct recieved_data)); @@ -362,107 +385,115 @@ void *vidcap_rtsp_init(const struct vidcap_params *params){ char *uri_tmp1; char *uri_tmp2; - if(vidcap_params_get_fmt(params) && strcmp(vidcap_params_get_fmt(params), "help") == 0) { + if (vidcap_params_get_fmt(params) + && strcmp(vidcap_params_get_fmt(params), "help") == 0) + { show_help(); - return &vidcap_init_noerr; - } - else{ - char *tmp = NULL; - fmt = strdup(vidcap_params_get_fmt(params)); - int i = 0; + return &vidcap_init_noerr; + } else { + char *tmp = NULL; + fmt = strdup(vidcap_params_get_fmt(params)); + int i = 0; - while((tmp = strtok_r(fmt, ":", &save_ptr))) { - switch (i) { - case 0: - if(tmp){ - tmp = strtok_r(NULL, ":", &save_ptr); - uri_tmp1 = malloc(strlen(tmp) + 32); - sprintf(uri_tmp1, "%s",tmp); - tmp = strtok_r(NULL, ":", &save_ptr); - uri_tmp2 = malloc(strlen(tmp) + 32); - sprintf(uri_tmp2, "%s",tmp); - s->uri = malloc(1024 + 32); - sprintf(s->uri, "rtsp:%s:%s",uri_tmp1,uri_tmp2); - }else{ - printf("\n[rtsp] Wrong format for uri! \n"); - show_help(); - exit(0); - } - break; - case 1: - if(tmp){ //TODO check if it's a number - s->port = atoi(tmp); - }else{ - printf("\n[rtsp] Wrong format for port! \n"); - show_help(); - exit(0); - } - break; - case 2: - if(tmp){ //TODO check if it's a number - s->width = atoi(tmp); - }else{ - printf("\n[rtsp] Wrong format for width! \n"); - show_help(); - exit(0); - } - break; - case 3: - if(tmp){ //TODO check if it's a number - s->height = atoi(tmp); - //Now checking if we have user and password parameters... - if(s->height == 0){ - int ntmp = 0; - ntmp = s->width; - s->width = s->port; - s->height = ntmp; - sprintf(s->uri, "rtsp:%s",uri_tmp1); - s->port = atoi(uri_tmp2); - if(tmp){ - if(strcmp(tmp,"true")==0) s->decompress = true; - else if (strcmp(tmp,"false")==0) s->decompress = false; - else{ - printf("\n[rtsp] Wrong format for boolean decompress flag! \n"); - show_help(); - exit(0); - } - }else continue; - } - }else{ - printf("\n[rtsp] Wrong format for height! \n"); - show_help(); - exit(0); - } - break; - case 4: - if(tmp){ - if(strcmp(tmp,"true")==0) s->decompress = true; - else if (strcmp(tmp,"false")==0) s->decompress = false; - else{ - printf("\n[rtsp] Wrong format for boolean decompress flag! \n"); + while ((tmp = strtok_r(fmt, ":", &save_ptr))) { + switch (i) { + case 0: + if (tmp) { + tmp = strtok_r(NULL, ":", &save_ptr); + uri_tmp1 = malloc(strlen(tmp) + 32); + sprintf(uri_tmp1, "%s", tmp); + tmp = strtok_r(NULL, ":", &save_ptr); + uri_tmp2 = malloc(strlen(tmp) + 32); + sprintf(uri_tmp2, "%s", tmp); + s->uri = malloc(1024 + 32); + sprintf(s->uri, "rtsp:%s:%s", uri_tmp1, uri_tmp2); + } else { + printf("\n[rtsp] Wrong format for uri! \n"); + show_help(); + exit(0); + } + break; + case 1: + if (tmp) { //TODO check if it's a number + s->port = atoi(tmp); + } else { + printf("\n[rtsp] Wrong format for port! \n"); + show_help(); + exit(0); + } + break; + case 2: + if (tmp) { //TODO check if it's a number + s->width = atoi(tmp); + } else { + printf("\n[rtsp] Wrong format for width! \n"); + show_help(); + exit(0); + } + break; + case 3: + if (tmp) { //TODO check if it's a number + s->height = atoi(tmp); + //Now checking if we have user and password parameters... + if (s->height == 0) { + int ntmp = 0; + ntmp = s->width; + s->width = s->port; + s->height = ntmp; + sprintf(s->uri, "rtsp:%s", uri_tmp1); + s->port = atoi(uri_tmp2); + if (tmp) { + if (strcmp(tmp, "true") == 0) + s->decompress = true; + else if (strcmp(tmp, "false") == 0) + s->decompress = false; + else { + printf( + "\n[rtsp] Wrong format for boolean decompress flag! \n"); show_help(); exit(0); - } - }else continue; - break; - case 5: - continue; - } - fmt = NULL; - ++i; - } + } + } else + continue; + } + } else { + printf("\n[rtsp] Wrong format for height! \n"); + show_help(); + exit(0); + } + break; + case 4: + if (tmp) { + if (strcmp(tmp, "true") == 0) + s->decompress = true; + else if (strcmp(tmp, "false") == 0) + s->decompress = false; + else { + printf( + "\n[rtsp] Wrong format for boolean decompress flag! \n"); + show_help(); + exit(0); + } + } else + continue; + break; + case 5: + continue; + } + fmt = NULL; + ++i; + } } //re-check parameters - if(s->height == 0){ + if (s->height == 0) { int ntmp = 0; ntmp = s->width; - s->width = (int)s->port; + s->width = (int) s->port; s->height = (int) ntmp; - sprintf(s->uri, "rtsp:%s",uri_tmp1); + sprintf(s->uri, "rtsp:%s", uri_tmp1); s->port = (int) atoi(uri_tmp2); } - debug_msg("[rtsp] selected flags:\n"); debug_msg("\t uri: %s\n",s->uri); debug_msg("\t port: %d\n", s->port); @@ -470,26 +501,29 @@ void *vidcap_rtsp_init(const struct vidcap_params *params){ debug_msg("\t height: %d\n",s->height); debug_msg("\t decompress: %d\n\n",s->decompress); - if(uri_tmp1!=NULL) free(uri_tmp1); - if(uri_tmp2!=NULL) free(uri_tmp2); + if (uri_tmp1 != NULL) + free(uri_tmp1); + if (uri_tmp2 != NULL) + free(uri_tmp2); - s->rx_data->frame_buffer = malloc(4*s->width*s->height); - s->data = malloc(4*s->width*s->height + s->nals_size); + s->rx_data->frame_buffer = malloc(4 * s->width * s->height); + s->data = malloc(4 * s->width * s->height + s->nals_size); s->frame = vf_alloc(1); s->tile = vf_get_tile(s->frame, 0); - vf_get_tile(s->frame, 0)->width=s->width; - vf_get_tile(s->frame, 0)->height=s->height; + vf_get_tile(s->frame, 0)->width = s->width; + vf_get_tile(s->frame, 0)->height = s->height; //TODO fps should be autodetected, now reset and controlled at vidcap_grab function - s->frame->fps=60; + s->frame->fps = 60; s->fps = 60; - s->frame->interlacing=PROGRESSIVE; + s->frame->interlacing = PROGRESSIVE; - s->frame->tiles[0].data = calloc(1, s->width*s->height); + s->frame->tiles[0].data = calloc(1, s->width * s->height); - s->should_exit = false; + s->should_exit = false; - s->device = rtp_init_if(NULL, s->mcast_if, s->port, 0, s->ttl, s->rtcp_bw, 0, rtp_recv_callback, (void *)s->participants, 0); + s->device = rtp_init_if(NULL, s->mcast_if, s->port, 0, s->ttl, s->rtcp_bw, + 0, rtp_recv_callback, (void *) s->participants, 0); if (s->device != NULL) { if (!rtp_set_option(s->device, RTP_OPT_WEAK_VALIDATION, 1)) { @@ -524,19 +558,20 @@ void *vidcap_rtsp_init(const struct vidcap_params *params){ s->boss_waiting = false; s->worker_waiting = false; - s->nals_size = init_rtsp(s->uri, s->port, s , s->nals); + s->nals_size = init_rtsp(s->uri, s->port, s, s->nals); - if(s->nals_size>=0) - memcpy(s->data,s->nals,s->nals_size); - else - return NULL; + if (s->nals_size >= 0) + memcpy(s->data, s->nals, s->nals_size); + else + return NULL; - if(s->decompress) { - if(init_decompressor(s)==0) return NULL; - s->frame->color_spec = UYVY; - } + if (s->decompress) { + if (init_decompressor(s) == 0) + return NULL; + s->frame->color_spec = UYVY; + } - pthread_create(&s->rtsp_thread_id, NULL , vidcap_rtsp_thread , s); + pthread_create(&s->rtsp_thread_id, NULL, vidcap_rtsp_thread, s); debug_msg("[rtsp] rtsp capture init done\n"); @@ -546,130 +581,136 @@ void *vidcap_rtsp_init(const struct vidcap_params *params){ /** * Initializes rtsp state and internal parameters */ -static int init_rtsp(char* rtsp_uri, int rtsp_port,void *state, char* nals) { - struct rtsp_state *s; - s = (struct rtsp_state *)state; - const char *range = "0.000-"; - int len_nals=-1; - CURLcode res; - debug_msg("\n[rtsp] request %s\n", VERSION_STR); - debug_msg(" Project web site: http://code.google.com/p/rtsprequest/\n"); - debug_msg(" Requires cURL V7.20 or greater\n\n"); - const char *url = rtsp_uri; - char *uri = malloc(strlen(url) + 32); - char *sdp_filename = malloc(strlen(url) + 32); - char *control = malloc(150 * sizeof(char *)); - memset(control,0,150 * sizeof(char *)); - char transport[256]; - bzero(transport, 256); - int port = rtsp_port; - get_sdp_filename(url, sdp_filename); - sprintf(transport, "RTP/AVP;unicast;client_port=%d-%d", port, port+1); +static int +init_rtsp(char* rtsp_uri, int rtsp_port, void *state, char* nals) { + struct rtsp_state *s; + s = (struct rtsp_state *) state; + const char *range = "0.000-"; + int len_nals = -1; + CURLcode res; + debug_msg("\n[rtsp] request %s\n", VERSION_STR); + debug_msg(" Project web site: http://code.google.com/p/rtsprequest/\n"); + debug_msg(" Requires cURL V7.20 or greater\n\n"); + const char *url = rtsp_uri; + char *uri = malloc(strlen(url) + 32); + char *sdp_filename = malloc(strlen(url) + 32); + char *control = malloc(150 * sizeof(char *)); + memset(control, 0, 150 * sizeof(char *)); + char transport[256]; + bzero(transport, 256); + int port = rtsp_port; + get_sdp_filename(url, sdp_filename); + sprintf(transport, "RTP/AVP;unicast;client_port=%d-%d", port, port + 1); - /* initialize curl */ - res = curl_global_init(CURL_GLOBAL_ALL); - if (res == CURLE_OK) { - curl_version_info_data *data = curl_version_info(CURLVERSION_NOW); - CURL *curl; - fprintf(stderr, "[rtsp] cURL V%s loaded\n", data->version); + /* initialize curl */ + res = curl_global_init(CURL_GLOBAL_ALL); + if (res == CURLE_OK) { + curl_version_info_data *data = curl_version_info(CURLVERSION_NOW); + CURL *curl; + fprintf(stderr, "[rtsp] cURL V%s loaded\n", data->version); - /* initialize this curl session */ - curl = curl_easy_init(); - if (curl != NULL) { - my_curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); //This tells curl not to use any functions that install signal handlers or cause signals to be sent to your process. - //my_curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, 1); - my_curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L); - my_curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L); - my_curl_easy_setopt(curl, CURLOPT_WRITEHEADER, stdout); - my_curl_easy_setopt(curl, CURLOPT_URL, url); + /* initialize this curl session */ + curl = curl_easy_init(); + if (curl != NULL) { + my_curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); //This tells curl not to use any functions that install signal handlers or cause signals to be sent to your process. + //my_curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, 1); + my_curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L); + my_curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L); + my_curl_easy_setopt(curl, CURLOPT_WRITEHEADER, stdout); + my_curl_easy_setopt(curl, CURLOPT_URL, url); - sprintf(uri, "%s", url); + sprintf(uri, "%s", url); - s->curl = curl; - s->uri = uri; + s->curl = curl; + s->uri = uri; - //TODO TO CHECK CONFIGURING ERRORS - //CURLOPT_ERRORBUFFER - //http://curl.haxx.se/libcurl/c/curl_easy_perform.html + //TODO TO CHECK CONFIGURING ERRORS + //CURLOPT_ERRORBUFFER + //http://curl.haxx.se/libcurl/c/curl_easy_perform.html - /* request server options */ - rtsp_options(curl, uri); - debug_msg("sdp_file: %s\n", sdp_filename); - /* request session description and write response to sdp file */ - rtsp_describe(curl, uri, sdp_filename); - debug_msg("sdp_file!!!!: %s\n", sdp_filename); - /* get media control attribute from sdp file */ - get_media_control_attribute(sdp_filename, control); + /* request server options */ + rtsp_options(curl, uri); + debug_msg("sdp_file: %s\n", sdp_filename); + /* request session description and write response to sdp file */ + rtsp_describe(curl, uri, sdp_filename); + debug_msg("sdp_file!!!!: %s\n", sdp_filename); + /* get media control attribute from sdp file */ + get_media_control_attribute(sdp_filename, control); - /* set incoming media codec attribute from sdp file */ - if (set_codec_attribute_from_incoming_media(sdp_filename, s) == 0) - return -1; + /* set incoming media codec attribute from sdp file */ + if (set_codec_attribute_from_incoming_media(sdp_filename, s) == 0) + return -1; - /* setup media stream */ - sprintf(uri, "%s/%s", url, control); - rtsp_setup(curl, uri, transport); + /* setup media stream */ + sprintf(uri, "%s/%s", url, control); + rtsp_setup(curl, uri, transport); - /* start playing media stream */ - sprintf(uri, "%s", url); - rtsp_play(curl, uri, range); + /* start playing media stream */ + sprintf(uri, "%s", url); + rtsp_play(curl, uri, range); - /* get start nal size attribute from sdp file */ - len_nals=get_nals(sdp_filename, nals); + /* get start nal size attribute from sdp file */ + len_nals = get_nals(sdp_filename, nals); - s->curl = curl; - s->uri = uri; - debug_msg("[rtsp] playing video from server...\n"); + s->curl = curl; + s->uri = uri; + debug_msg("[rtsp] playing video from server...\n"); - } else { - fprintf(stderr, "[rtsp] curl_easy_init() failed\n"); - } - curl_global_cleanup(); - } else { - fprintf(stderr, "[rtsp] curl_global_init(%s) failed: %d\n", "CURL_GLOBAL_ALL", res); - } - return len_nals; + } else { + fprintf(stderr, "[rtsp] curl_easy_init() failed\n"); + } + curl_global_cleanup(); + } else { + fprintf(stderr, "[rtsp] curl_global_init(%s) failed: %d\n", + "CURL_GLOBAL_ALL", res); + } + return len_nals; } /** * Initializes decompressor if required by decompress flag */ -static int init_decompressor(void *state) { - struct rtsp_state *sr; - sr = (struct rtsp_state *)state; +static int +init_decompressor(void *state) { + struct rtsp_state *sr; + sr = (struct rtsp_state *) state; - sr->sd = (struct state_decompress *) calloc(2, sizeof(struct state_decompress *)); - initialize_video_decompress(); + sr->sd = (struct state_decompress *) calloc(2, + sizeof(struct state_decompress *)); + initialize_video_decompress(); - if (decompress_is_available(LIBAVCODEC_MAGIC)) { - sr->sd = decompress_init(LIBAVCODEC_MAGIC); + if (decompress_is_available(LIBAVCODEC_MAGIC)) { + sr->sd = decompress_init(LIBAVCODEC_MAGIC); - sr->des.width = sr->width; - sr->des.height = sr->height; - sr->des.color_spec = sr->frame->color_spec; - sr->des.tile_count = 0; - sr->des.interlacing = PROGRESSIVE; + sr->des.width = sr->width; + sr->des.height = sr->height; + sr->des.color_spec = sr->frame->color_spec; + sr->des.tile_count = 0; + sr->des.interlacing = PROGRESSIVE; - decompress_reconfigure(sr->sd, sr->des, 16, 8, 0, vc_get_linesize(sr->des.width, UYVY), UYVY); - }else return 0; - sr->out_frame = malloc(sr->width*sr->height*4); - return 1; + decompress_reconfigure(sr->sd, sr->des, 16, 8, 0, + vc_get_linesize(sr->des.width, UYVY), UYVY); + } else + return 0; + sr->out_frame = malloc(sr->width * sr->height * 4); + return 1; } - -static void rtsp_get_parameters(CURL *curl, const char *uri) -{ +static void +rtsp_get_parameters(CURL *curl, const char *uri) { CURLcode res = CURLE_OK; debug_msg("\n[rtsp] GET_PARAMETERS %s\n", uri); my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri); - my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_GET_PARAMETER); + my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, + (long )CURL_RTSPREQ_GET_PARAMETER); my_curl_easy_perform(curl); } /** * send RTSP OPTIONS request */ -static void rtsp_options(CURL *curl, const char *uri) -{ +static void +rtsp_options(CURL *curl, const char *uri) { char control[1500], *user, *pass, *strtoken; user = malloc(1500); pass = malloc(1500); @@ -685,39 +726,37 @@ static void rtsp_options(CURL *curl, const char *uri) strtoken = strtok(control, ":"); memcpy(user, strtoken, strlen(strtoken)); strtoken = strtok(NULL, "@"); - if (strtoken == NULL){ - user = NULL; - pass = NULL; - } - else - memcpy(pass, strtoken, strlen(strtoken)); + if (strtoken == NULL) { + user = NULL; + pass = NULL; + } else + memcpy(pass, strtoken, strlen(strtoken)); if (user != NULL) - my_curl_easy_setopt(curl, CURLOPT_USERNAME, user); + my_curl_easy_setopt(curl, CURLOPT_USERNAME, user); if (pass != NULL) - my_curl_easy_setopt(curl, CURLOPT_PASSWORD, pass); + my_curl_easy_setopt(curl, CURLOPT_PASSWORD, pass); - my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_OPTIONS); + my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long )CURL_RTSPREQ_OPTIONS); my_curl_easy_perform(curl); } - /** * send RTSP DESCRIBE request and write sdp response to a file */ -static void rtsp_describe(CURL *curl, const char *uri, const char *sdp_filename) -{ +static void +rtsp_describe(CURL *curl, const char *uri, const char *sdp_filename) { CURLcode res = CURLE_OK; FILE *sdp_fp = fopen(sdp_filename, "wt"); debug_msg("\n[rtsp] DESCRIBE %s\n", uri); if (sdp_fp == NULL) { fprintf(stderr, "Could not open '%s' for writing\n", sdp_filename); sdp_fp = stdout; - } - else { + } else { debug_msg("Writing SDP to '%s'\n", sdp_filename); } my_curl_easy_setopt(curl, CURLOPT_WRITEDATA, sdp_fp); - my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_DESCRIBE); + my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, + (long )CURL_RTSPREQ_DESCRIBE); my_curl_easy_perform(curl); my_curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout); if (sdp_fp != stdout) { @@ -728,49 +767,47 @@ static void rtsp_describe(CURL *curl, const char *uri, const char *sdp_filename) /** * send RTSP SETUP request */ -static void rtsp_setup(CURL *curl, const char *uri, const char *transport) -{ +static void +rtsp_setup(CURL *curl, const char *uri, const char *transport) { CURLcode res = CURLE_OK; debug_msg("\n[rtsp] SETUP %s\n", uri); debug_msg("\t TRANSPORT %s\n", transport); my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri); my_curl_easy_setopt(curl, CURLOPT_RTSP_TRANSPORT, transport); - my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_SETUP); + my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long )CURL_RTSPREQ_SETUP); my_curl_easy_perform(curl); } - /** * send RTSP PLAY request */ -static void rtsp_play(CURL *curl, const char *uri, const char *range) -{ +static void +rtsp_play(CURL *curl, const char *uri, const char *range) { CURLcode res = CURLE_OK; debug_msg("\n[rtsp] PLAY %s\n", uri); my_curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri); //my_curl_easy_setopt(curl, CURLOPT_RANGE, range); //range not set because we want (right now) no limit range for streaming duration - my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_PLAY); + my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long )CURL_RTSPREQ_PLAY); my_curl_easy_perform(curl); } - /** * send RTSP TEARDOWN request */ -static void rtsp_teardown(CURL *curl, const char *uri) -{ +static void +rtsp_teardown(CURL *curl, const char *uri) { CURLcode res = CURLE_OK; debug_msg("\n[rtsp] TEARDOWN %s\n", uri); - my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_TEARDOWN); + my_curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, + (long )CURL_RTSPREQ_TEARDOWN); my_curl_easy_perform(curl); } - /** * convert url into an sdp filename */ -static void get_sdp_filename(const char *url, char *sdp_filename) -{ +static void +get_sdp_filename(const char *url, char *sdp_filename) { const char *s = strrchr(url, '/'); debug_msg("sdp_file get: %s\n", sdp_filename); @@ -785,9 +822,8 @@ static void get_sdp_filename(const char *url, char *sdp_filename) /** * scan sdp file for media control attribute */ -static void get_media_control_attribute(const char *sdp_filename, - char *control) -{ +static void +get_media_control_attribute(const char *sdp_filename, char *control) { int max_len = 1256; char *s = malloc(max_len); @@ -799,9 +835,9 @@ static void get_media_control_attribute(const char *sdp_filename, if (sdp_fp != NULL) { while (fgets(s, max_len - 2, sdp_fp) != NULL) { sscanf(s, " a = control: %s", track_ant); - if (strcmp(track_ant, "") != 0){ + if (strcmp(track_ant, "") != 0) { track = strstr(track_ant, "track"); - if(track != NULL) + if (track != NULL) break; } } @@ -815,14 +851,14 @@ static void get_media_control_attribute(const char *sdp_filename, /** * scan sdp file for incoming codec and set it */ -static int set_codec_attribute_from_incoming_media(const char *sdp_filename, void *state) -{ - struct rtsp_state *sr; - sr = (struct rtsp_state *)state; +static int +set_codec_attribute_from_incoming_media(const char *sdp_filename, void *state) { + struct rtsp_state *sr; + sr = (struct rtsp_state *) state; - int max_len = 1256; - char *pt = malloc(4*sizeof(char *)); - char *codec = malloc(32*sizeof(char *)); + int max_len = 1256; + char *pt = malloc(4 * sizeof(char *)); + char *codec = malloc(32 * sizeof(char *)); char *s = malloc(max_len); FILE *sdp_fp = fopen(sdp_filename, "rt"); @@ -847,51 +883,53 @@ static int set_codec_attribute_from_incoming_media(const char *sdp_filename, voi char *save_ptr = NULL; char *tmp; - tmp = strtok_r(codec, "/", &save_ptr); - if (!tmp) { - fprintf(stderr, "[rtsp] no codec atribute found into sdp file...\n"); - return 0; - } - sprintf((char *)sr->in_codec, "%s",tmp); + tmp = strtok_r(codec, "/", &save_ptr); + if (!tmp) { + fprintf(stderr, "[rtsp] no codec atribute found into sdp file...\n"); + return 0; + } + sprintf((char *) sr->in_codec, "%s", tmp); - if(memcmp(sr->in_codec,"H264",4)==0) - sr->frame->color_spec = H264; - else if(memcmp(sr->in_codec,"VP8",3)==0) - sr->frame->color_spec = VP8; - else - sr->frame->color_spec = RGBA; - return 1; + if (memcmp(sr->in_codec, "H264", 4) == 0) + sr->frame->color_spec = H264; + else if (memcmp(sr->in_codec, "VP8", 3) == 0) + sr->frame->color_spec = VP8; + else + sr->frame->color_spec = RGBA; + return 1; } -struct vidcap_type *vidcap_rtsp_probe(void){ +struct vidcap_type * +vidcap_rtsp_probe(void) { struct vidcap_type *vt; - vt = (struct vidcap_type *)malloc(sizeof(struct vidcap_type)); + vt = (struct vidcap_type *) malloc(sizeof(struct vidcap_type)); if (vt != NULL) { - vt->id = VIDCAP_RTSP_ID; - vt->name = "rtsp"; - vt->description = "Video capture from RTSP remote server"; + vt->id = VIDCAP_RTSP_ID; + vt->name = "rtsp"; + vt->description = "Video capture from RTSP remote server"; } return vt; } - -void vidcap_rtsp_done(void *state){ +void +vidcap_rtsp_done(void *state) { struct rtsp_state *s = state; s->should_exit = true; pthread_join(s->rtsp_thread_id, NULL); free(s->rx_data->frame_buffer); - free(s->data); + free(s->data); rtsp_teardown(s->curl, s->uri); - curl_easy_cleanup(s->curl); - s->curl = NULL; + curl_easy_cleanup(s->curl); + s->curl = NULL; - if(s->decompress) decompress_done(s->sd); - rtp_done(s->device); + if (s->decompress) + decompress_done(s->sd); + rtp_done(s->device); free(s); } @@ -899,8 +937,9 @@ void vidcap_rtsp_done(void *state){ /** * scan sdp file for media control attribute to generate nal starting bytes */ -static int get_nals(const char *sdp_filename, char *nals){ - int max_len = 1500 , len_nals = 0; +static int +get_nals(const char *sdp_filename, char *nals) { + int max_len = 1500, len_nals = 0; char *s = malloc(max_len); char *sprop; bzero(s, max_len); @@ -913,36 +952,36 @@ static int get_nals(const char *sdp_filename, char *nals){ gsize length; //gsize is an unsigned int. char *nal_aux, *nals_aux, *nal; nals_aux = malloc(max_len); - nals[0]=0x00; - nals[1]=0x00; - nals[2]=0x00; - nals[3]=0x01; + nals[0] = 0x00; + nals[1] = 0x00; + nals[2] = 0x00; + nals[3] = 0x01; len_nals = 4; - nal_aux = strstr(sprop, "=") ; + nal_aux = strstr(sprop, "="); nal_aux++; nal = strtok(nal_aux, ",;"); //convert base64 to hex nals_aux = g_base64_decode(nal, &length); - memcpy(nals+len_nals, nals_aux, length); + memcpy(nals + len_nals, nals_aux, length); len_nals += length; - while ((nal = strtok(NULL, ",;")) != NULL){ + while ((nal = strtok(NULL, ",;")) != NULL) { nals_aux = g_base64_decode(nal, &length); if (length) { //convert base64 to hex - nals[len_nals]=0x00; - nals[len_nals+1]=0x00; - nals[len_nals+2]=0x00; - nals[len_nals+3]=0x01; + nals[len_nals] = 0x00; + nals[len_nals + 1] = 0x00; + nals[len_nals + 2] = 0x00; + nals[len_nals + 3] = 0x01; len_nals += 4; - memcpy(nals+len_nals, nals_aux, length); + memcpy(nals + len_nals, nals_aux, length); len_nals += length; } //end if (length) { - }//end while ((nal = strtok(NULL, ",;")) != NULL){ - }//end if (sprop != NULL) { - }//end while (fgets(s, max_len - 2, sdp_fp) != NULL) { + } //end while ((nal = strtok(NULL, ",;")) != NULL){ + } //end if (sprop != NULL) { + } //end while (fgets(s, max_len - 2, sdp_fp) != NULL) { fclose(sdp_fp); - }//end if (sdp_fp != NULL) { + } //end if (sdp_fp != NULL) { free(s); return len_nals; diff --git a/src/video_capture/rtsp.h b/src/video_capture/rtsp.h index c99a42192..d018c90e4 100644 --- a/src/video_capture/rtsp.h +++ b/src/video_capture/rtsp.h @@ -40,11 +40,14 @@ extern "C" { #endif #define VIDCAP_RTSP_ID 0x45b3d828 //md5 hash of VIDCAP_RTSP_ID string == a208d26f519a2664a48781c845b3d828 - -struct vidcap_type *vidcap_rtsp_probe(void); -void *vidcap_rtsp_init(const struct vidcap_params *params); -void vidcap_rtsp_done(void *state); -struct video_frame *vidcap_rtsp_grab(void *state, struct audio_frame **audio); + struct vidcap_type * + vidcap_rtsp_probe(void); + void * + vidcap_rtsp_init(const struct vidcap_params *params); + void + vidcap_rtsp_done(void *state); + struct video_frame * + vidcap_rtsp_grab(void *state, struct audio_frame **audio); #endif