Skip to content

Commit

Permalink
stability improvement is done to rtsp module
Browse files Browse the repository at this point in the history
now retries to re-connect to rtsp source instead of segmentation fault
  • Loading branch information
zhuzhihao committed Jan 15, 2014
1 parent 0b025b2 commit 61aad79
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 26 deletions.
4 changes: 1 addition & 3 deletions motion.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,7 @@ static void sig_handler(int signo)
if (cnt_list) {
i = -1;
while (cnt_list[++i]) {
if (cnt_list[i]->conf.snapshot_interval)
cnt_list[i]->snapshot = 1;

cnt_list[i]->snapshot = 1;
}
}
break;
Expand Down
5 changes: 5 additions & 0 deletions netcam.c
Original file line number Diff line number Diff line change
Expand Up @@ -2018,6 +2018,11 @@ static void *netcam_handler_loop(void *arg)
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, "%s: Trying to re-connect");

}
/* Attempt to re-connect to rtsp server */
else if (netcam->rtsp) {
MOTION_LOG(ERR, TYPE_NETCAM, NO_ERRNO, "%s: Trying to re-connect to rtsp server");
netcam_reconnect_rtsp(netcam);
}
continue;
}
/*
Expand Down
50 changes: 27 additions & 23 deletions netcam_rtsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,41 +204,26 @@ static void rtsp_free_context(struct rtsp_context *ctxt)

static int rtsp_connect(netcam_context_ptr netcam)
{
if (netcam->rtsp == NULL) {
netcam->rtsp = rtsp_new_context();

if (netcam->rtsp == NULL) {
MOTION_LOG(ALR, TYPE_NETCAM, NO_ERRNO, "%s: unable to create context(%s)", netcam->rtsp->path);
return -1;
}
}

// open the network connection
AVDictionary *opts = 0;
av_dict_set(&opts, "rtsp_transport", "tcp", 0);

int ret = avformat_open_input(&netcam->rtsp->format_context, netcam->rtsp->path, NULL, &opts);
if (ret < 0) {
MOTION_LOG(ALR, TYPE_NETCAM, NO_ERRNO, "%s: unable to open input(%s): %d - %s", netcam->rtsp->path, ret, av_err2str(ret));
rtsp_free_context(netcam->rtsp);
netcam->rtsp = NULL;
return -1;
}

// fill out stream information
ret = avformat_find_stream_info(netcam->rtsp->format_context, NULL);
if (ret < 0) {
MOTION_LOG(ALR, TYPE_NETCAM, NO_ERRNO, "%s: unable to find stream info: %d", ret);
rtsp_free_context(netcam->rtsp);
netcam->rtsp = NULL;
return -1;
}

ret = open_codec_context(&netcam->rtsp->video_stream_index, netcam->rtsp->format_context, AVMEDIA_TYPE_VIDEO);
if (ret < 0) {
MOTION_LOG(ALR, TYPE_NETCAM, NO_ERRNO, "%s: unable to open codec context: %d", ret);
rtsp_free_context(netcam->rtsp);
netcam->rtsp = NULL;
return -1;
}

Expand All @@ -252,12 +237,6 @@ static int rtsp_connect(netcam_context_ptr netcam)

static int netcam_read_rtsp_image(netcam_context_ptr netcam)
{
if (netcam->rtsp == NULL) {
if (rtsp_connect(netcam) < 0) {
return -1;
}
}

AVCodecContext *cc = netcam->rtsp->codec_context;
AVFormatContext *fc = netcam->rtsp->format_context;
netcam_buff_ptr buffer;
Expand Down Expand Up @@ -357,6 +336,7 @@ int netcam_setup_rtsp(netcam_context_ptr netcam, struct url_t *url)
{
struct context *cnt = netcam->cnt;
const char *ptr;
int ret;

netcam->caps.streaming = NCS_RTSP;
netcam->rtsp = rtsp_new_context();
Expand Down Expand Up @@ -407,10 +387,15 @@ int netcam_setup_rtsp(netcam_context_ptr netcam, struct url_t *url)
* The RTSP context should be all ready to attempt a connection with
* the server, so we try ....
*/
rtsp_connect(netcam);
ret = rtsp_connect(netcam);
if (ret < 0)
{
rtsp_free_context(netcam->rtsp);
netcam->rtsp = NULL;
return ret;
}

netcam->get_image = netcam_read_rtsp_image;

return 0;
}

Expand All @@ -421,3 +406,22 @@ void netcam_shutdown_rtsp(netcam_context_ptr netcam)
netcam->rtsp = NULL;
}
}

void netcam_reconnect_rtsp(netcam_context_ptr netcam)
{
if (!netcam->rtsp)
{
/* incorrect calling sequence */
return;
}

if (netcam->rtsp->format_context != NULL) {
avformat_close_input(&netcam->rtsp->format_context);
}

if (netcam->rtsp->codec_context != NULL) {
avcodec_close(netcam->rtsp->codec_context);
}

rtsp_connect(netcam);
}
1 change: 1 addition & 0 deletions netcam_rtsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ struct rtsp_context {

int netcam_setup_rtsp(netcam_context_ptr netcam, struct url_t *url);
void netcam_shutdown_rtsp(netcam_context_ptr netcam);
void netcam_reconnect_rtsp(netcam_context_ptr netcam);

0 comments on commit 61aad79

Please sign in to comment.