Skip to content

Commit

Permalink
screen_id, remove unsused
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Kiseliov committed Jul 24, 2013
1 parent 58da56c commit c7c0185
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 147 deletions.
95 changes: 49 additions & 46 deletions cameras.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,43 @@
#define REAL 2
#define STATS 3
#define CAM_INFO 4
#define SCREEN_PING 5

#define MP4_FILE 100
#define H264_FILE 101

struct camera {
int id;
char *url;
char *name;
int analize_frames;
int threshold;
int motion_delay;
int active;
AVFormatContext *context;
AVCodecContext *codec;
AVStream *input_stream;
AVFormatContext *output_context;
AVStream *output_stream;
AVIOInterruptCB int_cb;
int video_stream_index;
int id;
char *url;
char *name;
int analize_frames;
int threshold;
int motion_delay;
int active;

AVFormatContext *context;
AVCodecContext *codec;
AVStream *input_stream;
AVFormatContext *output_context;
AVStream *output_stream;
AVIOInterruptCB int_cb;
int video_stream_index;

int file_id;
time_t file_started_at;
time_t last_io;
time_t last_screenshot;
int file_id;
time_t file_started_at;
time_t last_io;
time_t last_screenshot;

l1* cam_consumers_list;
pthread_mutex_t consumers_lock;
l1* cam_consumers_list;
pthread_mutex_t consumers_lock;
};

struct motion_detection {
struct camera *cam;
struct SwsContext *img_convert_ctx;
IplImage *prev;
IplImage *cur;
IplImage *silh;
uint8_t* buffer;
struct camera *cam;
struct SwsContext *img_convert_ctx;
IplImage *prev;
IplImage *cur;
IplImage *silh;
uint8_t* buffer;
};

struct in_out_cpy {
Expand All @@ -69,27 +68,31 @@ struct in_out_cpy {
};

struct screen {
int type;
int ncams;
int tmpl_size;
int active;
struct camera **cams;
unsigned int session_id;
time_t timestamp;
AVFormatContext *rtp_context;
AVStream *rtp_stream;
AVPicture combined_picture;
pthread_t worker_thread;
pthread_mutex_t combined_picture_lock;
struct in_out_cpy *io;
time_t last_activity;
int type;
int ncams;
int tmpl_size;
int active;
char *screen_id;
struct camera **cams;
time_t timestamp;
AVFormatContext *rtp_context;
AVStream *rtp_stream;
AVPicture combined_picture;
pthread_t worker_thread;
pthread_mutex_t combined_picture_lock;
struct in_out_cpy *io;
};

struct cam_consumer {
struct screen *screen;
int position;
AVPicture picture;
struct SwsContext *sws_context;
struct screen *screen;
int position;
AVPicture picture;
struct SwsContext *sws_context;
};

struct rtsp_session {
char *session_id;
struct screen *screen;
};

void error(const char *msg);
Expand Down
45 changes: 16 additions & 29 deletions control_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ static int nfds = 1, rc;
static int timeout = 5000;
static int reads[MAX_CLIENTS];
static char buffer[BUF_SIZE * MAX_CLIENTS];
static unsigned int session_id = 0;

static time_t last_disk_space_check = 0;

Expand Down Expand Up @@ -65,8 +64,8 @@ static struct camera** get_cams_by_ids(int *cam_ids, int ncams) {

int find_screen_func(void *value, void *arg) {
struct screen *screen = value;
int session_id = *((int *)arg);
if(screen->session_id == session_id)
char* screen_id = (char *)arg;
if(strcpy(screen->screen_id, screen_id) == 0)
return 1;
else
return 0;
Expand All @@ -88,9 +87,9 @@ static void parse_command(char *buf, int client_fd, int *close_conn) {
int type = 0;
int ncams = 0;
int *cam_ids = NULL;
int ssid = 0;
int tmpl_size = 1;
time_t timestamp = 0;
char *screen_id = NULL;

struct screen *screen = NULL;
struct camera *cam;
Expand Down Expand Up @@ -118,8 +117,6 @@ static void parse_command(char *buf, int client_fd, int *close_conn) {
type = STATS;
else if(strcmp(string_value, "cam_info") == 0)
type = CAM_INFO;
else if(strcmp(string_value, "screen_ping") == 0)
type = SCREEN_PING;
} else if (strcmp(key, "cam_ids") == 0) {
ncams = value->u.array.length;
cam_ids = (int*)malloc(ncams * sizeof(int));
Expand All @@ -129,8 +126,9 @@ static void parse_command(char *buf, int client_fd, int *close_conn) {
}
} else if (strcmp(key, "timestamp") == 0) {
timestamp = value->u.integer;
} else if (strcmp(key, "session_id") == 0) {
ssid = value->u.integer;
} else if (strcmp(key, "screen_id") == 0) {
screen_id = (char*)malloc(strlen(value->u.string.ptr)+1);
strcpy(screen_id, value->u.string.ptr);
} else if (strcmp(key, "template") == 0) {
tmpl_size = value->u.integer;
}
Expand All @@ -140,30 +138,26 @@ static void parse_command(char *buf, int client_fd, int *close_conn) {
switch(type) {
case ARCHIVE:
case REAL:
if(ssid == 0) {
session_id += 1;
ssid = session_id;

if(screen_id == NULL) {
screen = (struct screen *)malloc(sizeof(struct screen));
screen->type = type;
screen->ncams = ncams;
screen->session_id = ssid;
screen->screen_id = random_string(8);
screen->tmpl_size = tmpl_size;
screen->cams = get_cams_by_ids(cam_ids, ncams);
screen->timestamp = timestamp;\
screen->rtp_context = NULL;
screen->last_activity = time(NULL);
screen->active = 1;
screen->active = 0;
screen->io = NULL;

if(screen_init(screen) < 0) {
sprintf(buf, "{ \"error\": \"Screen initialization failed\" }\n");
} else {
sprintf(buf, "{ \"session_id\": %d, \"width\": %d, \"height\": %d }\n",
screen->session_id, screen->rtp_stream->codec->width, screen->rtp_stream->codec->height);
sprintf(buf, "{ \"screen_id\": %s, \"width\": %d, \"height\": %d }\n",
screen->screen_id, screen->rtp_stream->codec->width, screen->rtp_stream->codec->height);
}
} else {
screen = (struct screen *) l1_find(&screens, &screens_lock, &find_screen_func, &ssid);
screen = (struct screen *) l1_find(&screens, &screens_lock, &find_screen_func, screen_id);
if(screen == NULL) {
sprintf(buf, "{ \"error\": \"Screen not found\" }\n");
} else {
Expand All @@ -174,8 +168,8 @@ static void parse_command(char *buf, int client_fd, int *close_conn) {
if(screen_open_video_file(screen) < 0) {
sprintf(buf, "{ \"error\": \"Moment not found\" }\n");
} else {
sprintf(buf, "{ \"session_id\": %d, \"width\": %d, \"height\": %d }\n",
screen->session_id, screen->rtp_stream->codec->width, screen->rtp_stream->codec->height);
sprintf(buf, "{ \"screen_id\": %s, \"width\": %d, \"height\": %d }\n",
screen->screen_id, screen->rtp_stream->codec->width, screen->rtp_stream->codec->height);
}
}
}
Expand All @@ -201,15 +195,6 @@ static void parse_command(char *buf, int client_fd, int *close_conn) {
cam->input_stream->r_frame_rate.den, cam->input_stream->r_frame_rate.num);
}
break;
case SCREEN_PING:
screen = (struct screen *) l1_find(&screens, &screens_lock, &find_screen_func, &ssid);
if(screen == NULL) {
sprintf(buf, "{ \"error\": \"Screen not found\" }\n");
} else {
screen->last_activity = time(NULL);
sprintf(buf, "{ \"ok\": \"ok\" }\n");
}
break;
default:
sprintf(buf, "{ \"error\": \"Command not found\" }\n");
break;
Expand All @@ -219,6 +204,8 @@ static void parse_command(char *buf, int client_fd, int *close_conn) {

if(cam_ids != NULL)
free(cam_ids);
if(screen_id != NULL)
free(screen_id);
}

void control_socket_init() {
Expand Down
2 changes: 1 addition & 1 deletion file_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void* copy_input_to_output(void *ptr) {
return NULL;
}

void* multiple_cameras_thread(void * ptr) {
void* multiple_cameras_thread(void *ptr) {
struct screen *screen = (struct screen *)ptr;
AVCodecContext *c = screen->rtp_stream->codec;
AVFrame *frame;
Expand Down
2 changes: 2 additions & 0 deletions recorder_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ void *recorder_thread(void *ptr) {
pthread_mutex_lock(&cam->consumers_lock);
for(l1 *p = cam->cam_consumers_list; p != NULL; p = p->next) {
struct cam_consumer *consumer = (struct cam_consumer *)p->value;
if(!consumer->screen->active)
continue;

if(consumer->screen->tmpl_size == 1) {
packet.stream_index = consumer->screen->rtp_stream->id;
Expand Down
99 changes: 65 additions & 34 deletions rtsp_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ static void rtsp_server_loop();
/*
static l1 *sessions = NULL;
static pthread_mutex_t sessions_lock;
struct session {
char *session_id;
struct screen *screen;
};
*/

void* start_rtsp_server(void *ptr) {
Expand Down Expand Up @@ -189,7 +184,7 @@ static void parse_command(char *buf, int len, int client_fd, int *close_conn) {
char out[128];
char *req = buf;
char *url, *sdp=NULL, *header;
char *transport, *session;
char *transport, *session, *screen_id;
int method, CSeq = 0;
int c, h = 0;

Expand Down Expand Up @@ -221,7 +216,10 @@ static void parse_command(char *buf, int len, int client_fd, int *close_conn) {
while(*req != ' ') req += 1;
*req = '\0';

printf("url: <%s>\n", url);
screen_id = url;

printf("url: <%s>, screen_id: %s\n", url, screen_id);


while(*req != '\n') req += 1;
req += 1;
Expand Down Expand Up @@ -258,33 +256,42 @@ static void parse_command(char *buf, int len, int client_fd, int *close_conn) {

sprintf(out, "CSeq: %d\n", CSeq);

if(method == OPTIONS) {
snd(client_fd, "RTSP/1.0 200 OK\n", close_conn);
snd(client_fd, out, close_conn);
snd(client_fd, "Public: OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE\n\n", close_conn);
} else if (method == DESCRIBE) {
snd(client_fd, "RTSP/1.0 200 OK\n", close_conn);
snd(client_fd, out, close_conn);
snd(client_fd, "Content-Type: application/sdp\n", close_conn);

//parse url
//find screen
//sdp = create_sdp(screen);
sprintf(out, "Content-Length: %d\n\n", strlen(sdp));
snd(client_fd, out, close_conn);
snd(client_fd, sdp, close_conn);
//free(sdp);
} else if (method == SETUP) {
snd(client_fd, "RTSP/1.0 200 OK\n", close_conn);
snd(client_fd, out, close_conn);
sprintf(out, "Session: %d\n", 23456789);
snd(client_fd, out, close_conn);
if(strcmp(transport, "RTP/AVP;unicast;") != 0) {
printf("Transport not supported\n");
}
snd(client_fd, "Transport: RTP/AVP;unicast;\n", close_conn);
} else if (method == PLAY) {
printf("Session = %s\n", session);
switch(method) {
case OPTIONS:
snd(client_fd, "RTSP/1.0 200 OK\n", close_conn);
snd(client_fd, out, close_conn);
snd(client_fd, "Public: OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE\n\n", close_conn);
break;
case DESCRIBE:
snd(client_fd, "RTSP/1.0 200 OK\n", close_conn);
snd(client_fd, out, close_conn);
snd(client_fd, "Content-Type: application/sdp\n", close_conn);

if(strncasecmp(url, "rtsp://", 7) != 0) {
printf("bad url\n");
}
//find screen
//sdp = create_sdp(screen);
sprintf(out, "Content-Length: %d\n\n", strlen(sdp));
snd(client_fd, out, close_conn);
snd(client_fd, sdp, close_conn);
//free(sdp);
break;
case SETUP:
snd(client_fd, "RTSP/1.0 200 OK\n", close_conn);
snd(client_fd, out, close_conn);
session = random_string(8);
sprintf(out, "Session: %s\n", session);
snd(client_fd, out, close_conn);
if(strcmp(transport, "RTP/AVP;unicast;") != 0) {
printf("Transport not supported\n");
}
snd(client_fd, "Transport: RTP/AVP;unicast;\n", close_conn);
free(session);
break;
case PLAY:
printf("Session = %s\n", session);
break;
}
}

Expand All @@ -295,3 +302,27 @@ static void snd(int fd, char *buffer, int *close_conn) {
*close_conn = 1;
}
}

/* PLAY+SETUP
screen = (struct screen *) l1_find(&screens, &screens_lock, &find_screen_func, screen_id);
// all
rtp_context = screen->rtp_context;
if((ret = avio_open(&(rtp_context->pb), rtp_context->filename, AVIO_FLAG_WRITE)) < 0) {
avformat_free_context(rtp_context);
av_err_msg("avio_open", ret);
return -1;
}
screen->active = 1;
// file
pthread_t copy_input_to_output_thread;
if(pthread_create(&copy_input_to_output_thread, NULL, copy_input_to_output, io) < 0)
error("pthread_create");
if(pthread_detach(copy_input_to_output_thread) < 0)
error("pthread_detach");
// multiple
if(pthread_create(&screen->worker_thread, NULL, multiple_cameras_thread, screen) < 0)
error("pthread_create");
*/
Loading

0 comments on commit c7c0185

Please sign in to comment.