Esempio n. 1
0
File: alert.c Progetto: grpascal/GEO
int main(int argc, char **argv)
{
	int ret;
	int counter=0;
	int count=0;
	video_format_t fmt;
	int ch_count = 0, channel;
	uint16_t hight;
	int framerate = 0;
	int goplen = 0;
	video_channel_info_t info;
	int audio_threshold = 70;
	int current_audio_intensity = 0;
	int reg_id;	/* region number; used when alert is generated */
	int x_offt;	/* starting x offset */
	int y_offt;	/* starting y offset */
	int width;	/* width of region */
	int height;	/* height of region */
	int sensitivity;	/* threshold for motion detection for a macroblock */
	int trigger_percentage;	/* % of macroblocks to be in motion for alert to be generated */
	char adata;
	char mdata;
	
	//initialize audio part **************************
	ret = mxuvc_audio_init("alsa","device = MAX64380");
	if(ret<0)
		goto error;

	ret = mxuvc_audio_register_cb(AUD_CH2, audio_cb, NULL);
	if(ret < 0)
		goto error;
	
	//TBD
	ret = mxuvc_audio_set_format(AUD_CH2, AUD_FORMAT_AAC_RAW);
	if(ret < 0)
		goto error;
	
	ret = mxuvc_audio_set_samplerate(AUD_CH2, SAMP_FREQ);
	if(ret < 0)
		goto error;

	mxuvc_audio_set_volume(100);

	//initialize video part **************************
	ret = mxuvc_video_init("v4l2","dev_offset=0");

	if(ret < 0)
		return 1;

	/* Register callback functions */
	ret = mxuvc_video_register_cb(CH1, ch_cb, (void*)CH1);
	if(ret < 0)
		goto error;
	
	printf("\n");
	//get channel count of MUX channel
	ret = mxuvc_video_get_channel_count(&ch_count);
	printf("Total Channel count: %d\n",ch_count);
	//remove raw channel from count
	ch_count = ch_count - 1;

	for(channel=CH2 ; channel<ch_count; channel++)
	{
		ret = mxuvc_video_register_cb(channel, ch_cb, (void*)channel);
		if(ret < 0)
			goto error;
	}

	/*** enquire every channel capabilities ****/
	for(channel=CH1; channel<ch_count ; channel++)
	{
		mxuvc_video_get_channel_info(channel, &info);
		printf("\nCH%d Channel Config:\n",channel+1);
		print_format(info.format);
		printf("width %d height %d\n",info.width,info.height);
		printf("framerate %dfps\n",info.framerate);
		
		if(info.format == VID_FORMAT_H264_RAW || 
				info.format == VID_FORMAT_H264_TS)
		{
			printf("gop length %d\n",info.goplen);	
			printf("setting gop length to 30\n");
			ret = mxuvc_video_set_goplen(channel, 30);
			if(ret < 0)
				goto error;
			ret = mxuvc_video_get_goplen(channel, &goplen);
			if(ret < 0)
				goto error;
			printf("gop length %d\n",goplen);
			printf("profile %d\n",info.profile);
			printf("bitrate %d\n",info.bitrate);
		}
	
	}

	printf("\n");
	video_profile_t profile;
	ret = mxuvc_video_get_profile(CH1, &profile);
	if(ret < 0)
		goto error;
	printf("CH1 profile %d\n",profile);
	ret = mxuvc_video_set_profile(CH1, PROFILE_BASELINE);
	if(ret < 0)
		goto error;
	printf("changed profile to PROFILE_BASELINE\n");
	ret = mxuvc_video_get_profile(CH1, &profile);
	if(ret < 0)
		goto error;
	printf("CH1 profile %d\n",profile);
	
	/***** Start audio  streaming ************/
	ret = mxuvc_audio_start(AUD_CH2);
	if(ret < 0)
		goto error;
	/***** Start video streaming ************/
	for(channel=CH1; channel<ch_count ; channel++){
		/* Start streaming */
		ret = mxuvc_video_start(channel);
		if(ret < 0)
			goto error;
	}

	/***** alert ************/
	if (audio_threshold > 0){
		//init audio alarm 
		ret = mxuvc_alert_init();
		if(ret < 0)
			goto error;
		ret = mxuvc_alert_audio_enable(	aalarm_callback, (void *)&adata);

		if (ret){
			printf("mxuvc_alert_audio_enable() failed\n");
			goto error;
		}
		//motion alert
		ret = mxuvc_alert_motion_enable(malarm_callback, (void *)&mdata);
		if (ret){
			printf("mxuvc_alert_motion_enable() failed\n");
			goto error;
		}
		//set region
		reg_id = 0;
		x_offt = 0;
		y_offt = 0;
		width = 80;
		height = 45;
		sensitivity = 50;
		trigger_percentage = 100;
		ret = mxuvc_alert_motion_add_region(reg_id, x_offt, y_offt, width, 
						height);
		if (ret){
			printf("mxuvc_alert_motion_add_region() failed\n");
			goto error;
		}
		ret =  mxuvc_alert_motion_sensitivity_and_level(reg_id,sensitivity,
		        trigger_percentage);
		if (ret){
		    printf("mxuvc_alert_motion_sensitivity_and_level() failed\n");
		    goto error;
		}

		//enable audio alert again to test
		ret = mxuvc_alert_audio_set_threshold(50);

		if (ret){
		    printf("mxuvc_alert_audio_set_threshold() failed\n");
		    goto error;
		}
	}
	
	//sleep
	usleep(50000);

	/* Main 'loop' */
	if (argc > 1){
		counter = atoi(argv[1]);
	} else
		counter = 15;

	while(counter--) {
		if (!mxuvc_audio_alive() || !mxuvc_video_alive())
			goto error;
		printf("\r%i secs left", counter+1);

		//video_testing(counter);
		//get current audio intensity
		ret = mxuvc_get_current_audio_intensity(&current_audio_intensity);
		if(ret){
			printf("mxuvc_get_current_audio_intensity() failed\n");
			goto error;
		}else
			printf("Current audio intensity %ddB\n",current_audio_intensity);

		if(counter == 10){
			mxuvc_alert_audio_disable();
		}
		if(counter == 9)
			mxuvc_alert_motion_disable();

		if(counter == 8){
			ret = mxuvc_alert_audio_set_threshold(audio_threshold);
			if (ret){
			    printf("mxuvc_alert_audio_set_threshold() failed\n");
			    goto error;
			}
			ret = mxuvc_alert_audio_enable(	aalarm_callback, (void *)&adata);

			if (ret){
			    printf("mxuvc_alert_audio_enable() failed\n");
			    goto error;
			}
		}
		if(counter == 7){
			ret = mxuvc_alert_motion_enable(malarm_callback, (void *)&mdata);
			if (ret){
				printf("mxuvc_alert_motion_enable() failed\n");
				goto error;
			}
		}
		fflush(stdout);
		sleep(1);
	}	

	if (audio_threshold > 0){
		ret = mxuvc_alert_motion_remove_region(reg_id);
		if (ret){
			printf("mxuvc_alert_motion_remove_region() failed\n");
			goto error;
		}
		mxuvc_alert_audio_disable();
		mxuvc_alert_motion_disable();
		sleep(1);
	}
	mxuvc_alert_deinit();

	/* Stop audio streaming */
	ret = mxuvc_audio_stop(AUD_CH2);
	if(ret < 0)
		goto error;

	mxuvc_audio_deinit();
	
	/* Stop video streaming */
	for(channel=CH1; channel<ch_count ; channel++)
	{	
		/* Stop streaming */
		ret = mxuvc_video_stop(channel);
		if(ret < 0)
			goto error;
	}
	/* Deinitialize and exit */
	mxuvc_video_deinit();
	
	close_fds();
	
	return 0;

error:
	mxuvc_audio_deinit();

	close_fds();	

	printf("Failed\n");
	return 1;
}
int main(int argc, char **argv)
{
    int ret=0;
    int count=0;
    video_format_t fmt;
    int ch_count = 0;
    long channel;
    uint16_t width, hight;
    int framerate = 0;
    int goplen = 0;
    int value, comp_q;
    noise_filter_mode_t sel;
    white_balance_mode_t bal;
    pwr_line_freq_mode_t freq;
    zone_wb_set_t wb;
    camer_mode_t mode;
    /* Initialize camera */
    ret = mxuvc_video_init("v4l2","dev_offset=0");

    if (ret < 0)
        return 1;

    ret = mxuvc_get_camera_mode(&mode);
    if (ret < 0)
        return 1;
    printf("Camera mode %s\n",mode==IPCAM ? "IPCAM":"SKYPE");

    /* Register callback functions */
    ret = mxuvc_video_register_cb(CH1, ch_cb, (void*)CH1);
    if (ret < 0)
        goto error;

    printf("\n");
    //get channel count of MUX channel
    ret = mxuvc_video_get_channel_count(&ch_count);
    printf("Total Channel count: %d\n",ch_count);
    //remove raw channel from count
    ch_count = ch_count - 1;

    for (channel=CH2 ; channel<ch_count; channel++)
    {
        ret = mxuvc_video_register_cb(channel, ch_cb, (void*)channel);
        if (ret < 0)
            goto error;
    }

    printf("\n");
    for (channel=CH1; channel<ch_count ; channel++) {
        /* Start streaming */
        ret = mxuvc_video_start(channel);

        if (ret < 0)
            goto error;
    }

    usleep(5000);

    /* Main 'loop' */
    int counter;
    if (argc > 1) {
        counter = atoi(argv[1]);
    } else
        counter = 15;
    int chid =CH1;
    //while (counter--) {
    while (1) {
        if (!mxuvc_video_alive()) {
            goto error;
        }
        //printf("\r%i secs left\n", counter+1);
        // mxuvc_video_set_dewarp_params((video_channel_t) chid, 0, EMODE_WM_ZCL, (dewarp_params_t*) &eptzWM_ZclParam);
        chid++;
        fflush(stdout);
        sleep(1);
    }

    for (channel=CH1; channel<ch_count ; channel++)
    {
        /* Stop streaming */
        ret = mxuvc_video_stop(channel);
        if (ret < 0)
            goto error;
    }
    /* Deinitialize and exit */
    mxuvc_video_deinit();


    printf("Success\n");

    return 0;
error:
    mxuvc_video_deinit();
    printf("Failure\n");
    return 1;
}
int main(int argc, char **argv)
{
	int ret;
	int i, counter=0;
	int count=0;
	video_format_t fmt;
	int ch_count = 0;
	long channel;
	uint16_t hight;
	int framerate = 0;
	int goplen = 0;
	video_channel_info_t info;
        int timer = 0;
        int frame_num = 0;
	char adata;
	char mdata;
	char *file = NULL;
	int font_size;
        time_t curtime;
        struct tm *loc_time;
        char szDate[16] = {0};

        if ( argc < 2 ){
                printf("USAGE: sudo overlay_text <path to font file> <size of font> [enable timer 0/1(default 0)] [enable frame numbering 0/1(default 0)\n");
                return 0;
        }

	//initialize video part **************************
	ret = mxuvc_video_init("v4l2","dev_offset=0");

	if(ret < 0)
		return 1;
	/* Main 'loop' */
	if (argc > 1){
		file = argv[1];
	}

	if (argc > 2){
		font_size = atoi(argv[2]);	
	} else
		font_size = 8;
	

	if (argc > 3){
		timer = atoi(argv[3]);	
	} else
		timer = 0;

	if (argc > 4){
		frame_num = atoi(argv[4]);	
	} else
		frame_num = 0;

        printf("YOUR CONFIGURATION timer %d and frame number %d\n",timer,frame_num);

        curtime = time (NULL);
        loc_time = localtime (&curtime); 
        printf("%s", asctime (loc_time));

	snprintf(szDate, 11, "%04d-%02d-%02d ", (loc_time->tm_year+1900), (loc_time->tm_mon+1), loc_time->tm_mday);
	printf("Date = %s\n", szDate);

	ret = mxuvc_overlay_init();

    /* Register callback functions */
	ret = mxuvc_video_register_cb(CH1, ch_cb, (void*)CH1);
	if(ret < 0)
		goto error;
	
	printf("\n");
	//get channel count of MUX channel
	ret = mxuvc_video_get_channel_count(&ch_count);
	printf("Total Channel count: %d\n",ch_count);
	//remove raw channel from count
	ch_count = ch_count - 1;

	for(channel=CH2 ; channel<ch_count; channel++)
	{
		ret = mxuvc_video_register_cb(channel, ch_cb, (void*)channel);
		if(ret < 0)
			goto error;
	}
	for(channel=CH1; channel<ch_count ; channel++)
    { 
    	ret = mxuvc_overlay_load_font(channel, font_size, file);	
		if(ret < 0)
	  		return 1;
		printf("overlay init done for ch %ld\n", channel);
	}

	/*** enquire every channel capabilities ****/
	for(channel=CH1; channel<ch_count ; channel++)
	{
		mxuvc_video_get_channel_info(channel, &info);
		printf("\nCH%ld Channel Config:\n",channel+1);
		print_format(info.format);
		printf("width %d height %d\n",info.width,info.height);
		printf("framerate %dfps\n",info.framerate);
		
		if(info.format == VID_FORMAT_H264_RAW || 
				info.format == VID_FORMAT_H264_TS)
		{
			//printf("gop length %d\n",info.goplen);	
			//printf("setting gop length to 30\n");
			ret = mxuvc_video_set_goplen(channel, 30);
			if(ret < 0)
				goto error;
			ret = mxuvc_video_get_goplen(channel, &goplen);
			if(ret < 0)
				goto error;
			//printf("gop length %d\n",goplen);
			//printf("profile %d\n",info.profile);
			//printf("bitrate %d\n",info.bitrate);
		}
	
	}

	printf("\n");
	video_profile_t profile;
	ret = mxuvc_video_get_profile(CH1, &profile);
	if(ret < 0)
		goto error;
	printf("CH1 profile %d\n",profile);
	ret = mxuvc_video_set_profile(CH1, PROFILE_BASELINE);
	if(ret < 0)
		goto error;
	printf("changed profile to PROFILE_BASELINE\n");
	ret = mxuvc_video_get_profile(CH1, &profile);
	if(ret < 0)
		goto error;
	printf("CH1 profile %d\n",profile);

	/***** Start video streaming ************/
	for(channel=CH1; channel<ch_count ; channel++){
		/* Start streaming */
		ret = mxuvc_video_start(channel);
		if(ret < 0)
			goto error;
	}

	//sleep
	usleep(5000);
	counter = 20;
	overlay_text_params_t ovtext[NUM_MUX_VID_CHANNELS][NUM_OVERLAY_TEXT_IDX];
	overlay_text_params_t ovtime;
	overlay_time_t btime;

	for(i = 0; i < sizeof(ovtext)/sizeof(overlay_text_params_t); i++) {
		memset(&ovtext[0][i], 0x00, sizeof(overlay_text_params_t));
	}

	memset(&ovtime, 0x00, sizeof(overlay_text_params_t));

	while(counter--) {
		if (!mxuvc_video_alive())
			goto error;
		printf("%i secs left\n", counter+1);

		if(counter == 18)
		{
                
			for(channel=CH1; channel < ch_count; channel++)
			{
				int ch = channel;

				memset(&btime,   0x00, sizeof(overlay_time_t));

				ovtext[ch][0].xoff = 0;
				ovtext[ch][0].yoff = 0;
				ovtext[ch][0].idx = 0;
				ret = mxuvc_overlay_add_text(channel, &ovtext[ch][0], szText0, strlen(szText0));
					if(ret < 0)
						goto error;

				ovtext[ch][1].xoff = 0;
				ovtext[ch][1].yoff = 100; 
				ovtext[ch][1].idx = 1;
				ret = mxuvc_overlay_add_text(channel, &ovtext[ch][1], szText1, strlen(szText1));
					if(ret < 0)
						goto error;

				ovtext[ch][2].xoff = 0;
				ovtext[ch][2].yoff = 200;
				ovtext[ch][2].idx = 2;

				ret = mxuvc_overlay_add_text(channel, &ovtext[ch][2], szDate, strlen(szDate));
					if(ret < 0)
						goto error;

				ovtext[ch][3].xoff = 0;
				ovtext[ch][3].yoff = 300;
				ovtext[ch][3].idx = 3;

				ret = mxuvc_overlay_add_text(channel, &ovtext[ch][3], szText2, strlen(szText2));
					if(ret < 0)
						goto error;

				ovtext[ch][4].xoff = 0;
				ovtext[ch][4].yoff = 400;
				ovtext[ch][4].idx = 4;

				ret = mxuvc_overlay_add_text(channel, &ovtext[ch][4], szText2, strlen(szText2));
					if(ret < 0)
						goto error;

			}
		}

		if(counter == 15 && timer == 1 )
		{
		
			for(channel=CH1; channel < ch_count; channel++)
			{
				int ch = channel;
			
				ovtime.xoff = 0;
				ovtime.yoff = 500;

				btime.hh = loc_time->tm_hour;
				btime.mm = loc_time->tm_min;
				btime.ss = loc_time->tm_sec;
				btime.frame_num_enable = frame_num;

				ret = mxuvc_overlay_set_time(channel, &ovtime, &btime);
				if(ret < 0)
					goto error;
				ret = mxuvc_overlay_show_time(channel);
				if(ret < 0)
					goto error;
			}
		}


		if(counter == 5)
		{
			printf("Removing text\n");

			for(channel=CH1; channel<ch_count ; channel++)
			{

				int ch = channel;		
				printf("from ch: %d\n",ch);
				ret = mxuvc_overlay_remove_text(channel, ovtext[ch][0].idx);
				if(ret < 0)
					goto error;

				ret = mxuvc_overlay_remove_text(channel, ovtext[ch][1].idx);
				if(ret < 0)
					goto error;

				ret = mxuvc_overlay_remove_text(channel, ovtext[ch][2].idx);
				if(ret < 0)
					goto error;

				ret = mxuvc_overlay_remove_text(channel, ovtext[ch][3].idx);
				if(ret < 0)
					goto error;

				ret = mxuvc_overlay_remove_text(channel, ovtext[ch][4].idx);
				if(ret < 0)
					goto error;

				ret = mxuvc_overlay_hide_time(channel);
				if(ret < 0)
					goto error;
			}
		}

		fflush(stdout);
		sleep(1);
	}	
	ret = mxuvc_overlay_deinit();	
	if(ret < 0)
		goto error;

	/* Stop video streaming */
	for(channel=CH1; channel<ch_count ; channel++)
	{	
		/* Stop streaming */
		ret = mxuvc_video_stop(channel);
		if(ret < 0)
			goto error;
	}
	/* Deinitialize and exit */
	mxuvc_video_deinit();
	
	close_fds();
	
	return 0;

error:
	//mxuvc_audio_deinit();
	mxuvc_video_deinit();

	close_fds();	

	printf("Failed\n");
	return 1;
}
int main(int argc, char **argv)
{
	int ret;
	int counter=0;
	int count=0;
	video_format_t fmt;
	int ch_count = 0;
        long channel;
	int width, height, xoff, yoff;
	video_channel_info_t info;

	char adata;
	char mdata;
	char *file = NULL;

	if ( argc < 4 )
	{
    	printf("USAGE: sudo overlay_image <path to font file> [<imagewidth>(max 320)] [<imageheight>(max 240)] [<xoffset>(optional | default 0)] [<yoffset>(optional | default 0)]\n");
    	return 0;
    }


	//initialize video part **************************
	ret = mxuvc_video_init("v4l2","dev_offset=0");

	if(ret < 0)
		return 1;
	/* Main 'loop' */
	if (argc > 1){
		file = argv[1];
	}

	if (argc > 2){
		width = atoi(argv[2]);	
	} 

	if (argc > 3){
		height = atoi(argv[3]);
	}

	if (argc > 4){
		xoff = atoi(argv[4]);
	}
	else {
		xoff = 0;
	}

	if (argc > 5){
		yoff = atoi(argv[5]);
	}
	else {
		yoff = 0;
	}

	ret = mxuvc_overlay_init();
	if(ret < 0)
	  return 1;
	printf("overlay init done\n");
	

	/* Register callback functions */
	ret = mxuvc_video_register_cb(CH1, ch_cb, (void*)CH1);
	if(ret < 0)
		goto error;
	
	printf("\n");
	//get channel count of MUX channel
	ret = mxuvc_video_get_channel_count(&ch_count);
	printf("Total Channel count: %d\n",ch_count);
	//remove raw channel from count
	ch_count = ch_count - 1;

	for(channel=CH1 ; channel<ch_count; channel++)
	{
		ret = mxuvc_video_register_cb(channel, ch_cb, (void*)channel);
		if(ret < 0)
			goto error;
	}

	/***** Start video streaming ************/
	for(channel=CH1; channel<ch_count ; channel++){
		/* Start streaming */
		ret = mxuvc_video_start(channel);
		if(ret < 0)
			goto error;
	}

	//sleep
	usleep(5000);
	counter = 20;
	overlay_image_params_t ovimage[1];
	uint8_t alpha = 0x7F;

	while(counter--) {
		if (!mxuvc_video_alive())
			goto error;

		printf("%i secs left\n", counter+1);
		int ch=0;
		if(counter == 18)
		{
              
			int i;
			ovimage[ch].width = width;
			ovimage[ch].height = height;
			ovimage[ch].xoff = xoff;
			ovimage[ch].yoff = yoff;
			ovimage[ch].idx = 0;
			ovimage[ch].alpha = alpha;
			mxuvc_overlay_add_image(CH1, &ovimage[ch], file);
		}


		if(counter == 2)
		{
			ret = mxuvc_overlay_remove_image(CH1, ovimage[0].idx);
		}

		fflush(stdout);
		sleep(1);
	}	

	ret = mxuvc_overlay_deinit();	
	if(ret < 0)
		goto error;

	/* Stop video streaming */
	for(channel=CH1; channel<ch_count ; channel++)
	{	
		/* Stop streaming */
		ret = mxuvc_video_stop(channel);
		if(ret < 0)
			goto error;
	}
	/* Deinitialize and exit */
	mxuvc_video_deinit();
	
	close_fds();
	
	return 0;

error:
	//mxuvc_audio_deinit();
	mxuvc_video_deinit();

	close_fds();	

	printf("Failed\n");
	return 1;
}
int main(int argc, char **argv)
{
	int ret=0;
	int count=0;

	/* Initialize camera */
	ret = mxuvc_video_init("v4l2", "dev_offset=0");
	if(ret < 0)
		return 1;
#if 0
	ret = mxuvc_audio_init("alsa","device = Condor");
	if(ret < 0)
		return 1;
	/* Set audio settings*/
	ret = mxuvc_audio_set_samplerate(AUD_CH1, 24000);
	if(ret < 0)
		goto error;
#endif
	/* Set video settings */
	ret = mxuvc_video_set_format(CH_MAIN, VID_FORMAT_H264_RAW);
	if(ret < 0)
		goto error;
	ret = mxuvc_video_set_resolution(CH_MAIN, 1280, 720);
	if(ret < 0)
		goto error;
	ret = mxuvc_video_set_format(CH_PREVIEW, VID_FORMAT_MJPEG_RAW);
	if(ret < 0)
		goto error;
	ret = mxuvc_video_set_resolution(CH_PREVIEW, 640, 480);
	if(ret < 0)
		goto error;

	/* Register callback functions */
	ret = mxuvc_video_register_cb(CH_MAIN, main_cb, NULL);
	if(ret < 0)
		goto error;
	ret = mxuvc_video_register_cb(CH_PREVIEW, preview_cb, NULL);
	if(ret < 0)
		goto error;
#if 0
	ret = mxuvc_audio_register_cb(AUD_CH1,(mxuvc_audio_cb_t) audio_cb, NULL);
	if(ret < 0)
		goto error;

	/* Start streaming */
	ret = mxuvc_audio_start(AUD_CH1);
	if(ret < 0)
		goto error;
#endif	

	ret = mxuvc_video_start(CH_MAIN);
	if(ret < 0)
		goto error;
	ret = mxuvc_video_start(CH_PREVIEW);
	if(ret < 0)
		goto error;

	/* In case of Bulk mode: wait a few ms and force an iframe */
	usleep(50000);
	mxuvc_video_force_iframe(CH_MAIN);

	/* Main 'loop' */
	int counter;
	if (argc > 1){
		counter = atoi(argv[1]);
	} else
		counter = 15;

	while(counter--) {
		if( !mxuvc_video_alive() /*|| !mxuvc_audio_alive()*/)
			goto error;
		printf("\r%i secs left", counter+1);
		fflush(stdout);
		sleep(1);
	}
#if 0
	
	/* Stop audio/video streaming */
	ret = mxuvc_audio_stop(AUD_CH1);
	if(ret < 0)
		goto error;
#endif
	ret = mxuvc_video_stop(CH_MAIN);
	if(ret < 0)
		goto error;
	ret = mxuvc_video_stop(CH_PREVIEW);
	if(ret < 0)
		goto error;

	/* Deinitialize and exit */
	mxuvc_video_deinit();
	//mxuvc_audio_deinit();

	close_fds();

	printf("Success\n");

	return 0;
error:
	mxuvc_video_deinit();
	//mxuvc_audio_deinit();
	close_fds();
	printf("Failure\n");
	return 1;
}
int main(int argc, char **argv)
{
	int ret=0;
	int count=0;
	video_format_t fmt;
	int ch_count = 0;
	long channel;
	int framerate = 0;
	int goplen = 0;
	long ch;

	/* Initialize camera */
	ret = mxuvc_video_init("v4l2","dev_offset=0");

	if(ret < 0)
		return 1;
	ret = mxuvc_video_get_channel_count(&ch_count);
	printf("Total Channel count: %d\n",ch_count);
	ch = ch_count-1; // by default, we have RAW channel as the last channel in our design
	ret = mxuvc_video_get_resolution(ch, &nRawChW, &nRawChH);
	printf("Raw Channel: %4d x %4d\n", nRawChW, nRawChH);

	mbWidth  = (nRawChW + 15)/16;
	mbHeight = (nRawChH + 15)/16;
	mbStride = ((mbWidth + 7) & ~0x7);

	nMBStatSize = mbWidth * mbHeight;
	mbStatsOffset = mbStride * mbHeight;

	printf("MBW %d MBH %d MBS %d; MBStatSize=%d; mbStatOffset=%d\n", mbWidth, mbHeight, mbStride, nMBStatSize, mbStatsOffset);  

        LumaAvgMBData = malloc(sizeof(int)*nMBStatSize);
	
#ifdef CH1_CAPTURE
	/* Register callback functions */
	ret = mxuvc_video_register_cb(CH1, ch_cb, (void*)CH1);
	if(ret < 0)
		goto error;
#endif
	ret = mxuvc_video_register_cb(ch_count-1, ch_cb, (void*)ch);
	if(ret < 0)
		goto error;
	
	printf("\n");
	//get channel count of MUX channel
	ret = mxuvc_video_get_channel_count(&ch_count);
	printf("Total Channel count: %d\n",ch_count);
	//Commented following in order to use the default format set in firmware
	//ret = mxuvc_video_set_format(ch_count - 1, VID_FORMAT_GREY_RAW);

        STRUCT_Q_EPTZ_MODE_WM_ZCL eptzWM_ZclParam;
        eptzWM_ZclParam.Phi0   = 55;
        eptzWM_ZclParam.Rx     = 2;
        eptzWM_ZclParam.Ry     = 1;
        eptzWM_ZclParam.Rz     = 1;
        eptzWM_ZclParam.Gshift = 0;
        //mxuvc_video_set_dewarp_params((video_channel_t) ch_count - 1, 0, EMODE_WM_ZCL, (dewarp_params_t*) &eptzWM_ZclParam);
	//ret = mxuvc_video_set_resolution(ch_count - 1, 320, 240);
	//if(ret < 0)
	//	goto error;
#ifdef CH1_CAPTURE
        ret = mxuvc_video_start(CH1);
	if(ret < 0)
		goto error;
#endif
	usleep(5000);
 
	ret = mxuvc_video_start(ch_count - 1);
	if(ret < 0)
		goto error;

	usleep(5000);

	/* Main 'loop' */
	int counter;
	if (argc > 1){
		counter = atoi(argv[1]);
	} else
		counter = 15;

	while(counter--) {
		if(!mxuvc_video_alive()) {
			goto error;
		}
		printf("\r%i secs left", counter+1);


		fflush(stdout);
		sleep(1);
	}
#ifdef CH1_CAPTURE
	ret = mxuvc_video_stop(CH1);
	if(ret < 0)
		goto error;
#endif
	ret = mxuvc_video_stop(ch_count - 1);
	if(ret < 0)
		goto error;
	/* Deinitialize and exit */
	mxuvc_video_deinit();

	close_fds();

	printf("Success\n");

	return 0;
error:
	mxuvc_video_deinit();
	close_fds();
	printf("Failure\n");
	return 1;
}
int main(int argc, char **argv)
{
	int ret;
	int counter=0;
	int ch_count = 0;
	long channel;
	char data;
	int i2c_instance = 0; 
	int i2c_device_type = 0; 
	uint16_t i2c_device_addr = 0x20;
	uint16_t chip_id_reg = 0xff;
	uint16_t chip_id_reg_value = 0x40;
	int handle;
	uint32_t read_len = 0;
	uint16_t width, hight;
	int framerate = 0;
	int goplen = 0;
	int audio_threshold = 20;
	int current_audio_intensity = 0;
	uint16_t regval = 0;

	fd_aud = NULL;
	
	//initialize audio part **************************
	ret = mxuvc_audio_init("alsa","device = Condor");
	if(ret<0)
		goto error;

	ret = mxuvc_alert_init();
	if(ret < 0)
		goto error;
	
	ret = mxuvc_audio_register_cb(AUD_CH2,(mxuvc_audio_cb_t) audio_cb, NULL);
	if(ret < 0)
		goto error;
	
	ret = mxuvc_audio_set_samplerate(AUD_CH2, SAMP_FREQ);
	if(ret < 0)
		goto error;

	mxuvc_audio_set_volume(100);

	//initialize video part **************************
	ret = mxuvc_video_init("v4l2","dev_offset=0");

	if(ret < 0)
		return 1;

	/* Register callback functions */
	ret = mxuvc_video_register_cb(CH1, ch_cb, (void*)CH1);
	if(ret < 0)
		goto error;
	
	printf("\n");
	//get channel count of MUX channel
	ret = mxuvc_video_get_channel_count(&ch_count);
	printf("Total Channel count: %ld\n",ch_count);
	//remove raw channel from count
	ch_count = ch_count - 1;

	for(channel=CH2 ; channel<ch_count; channel++)
	{
		ret = mxuvc_video_register_cb(channel, ch_cb, (void*)channel);
		if(ret < 0)
			goto error;
	}

	/***** Start audio  streaming ************/
	ret = mxuvc_audio_start(AUD_CH2);
	if(ret < 0)
		goto error;

	/***** Start video streaming ************/
	for(channel=CH1; channel<ch_count ; channel++){
		/* Start streaming */
		ret = mxuvc_video_start(channel);
		if(ret < 0)
			goto error;
	}
	
	if (audio_threshold > 0){
		ret = mxuvc_alert_audio_set_threshold(audio_threshold);
		if (ret){
		    printf("mxuvc_alert_audio_set_threshold() failed\n");
		    goto error;
		}
		ret = mxuvc_alert_audio_register_cb(&aalarm_callback, (void *)&data);
		if (ret){
			printf("mxuvc_alert_audio_register_callback() failed\n");
			goto error;
		}

		ret = mxuvc_alert_audio_enable();
		if (ret){
			printf("mxuvc_alert_audio_enable() failed\n");
			goto error;
		}

	}
	
	//sleep
	usleep(5000);

	//start i2c notification
	handle = mxuvc_i2c_open(i2c_instance, i2c_device_type, i2c_device_addr, 
			chip_id_reg, chip_id_reg_value);
	if(handle <= 0){
		printf("err: mxuvc_i2c_open failed\n");
		goto error;	
	}
#if 0			
	ret = mxuvc_i2c_notification_read_register(handle, 0x10, &regval, 1);
	if(ret < 0){
		printf("err: mxuvc_i2c_notification_read_register failed\n");
		goto error;	
	}
	printf("regval %x\n",regval);

	ret = mxuvc_i2c_notification_write_register(handle, 0x10, 0x2, 1);
	if(ret < 0){
		printf("err: mxuvc_i2c_notification_write_register failed\n");
		goto error;	
	}
	ret = mxuvc_i2c_notification_read_register(handle, 0x10, &regval, 1);
	if(ret < 0){
		printf("err: mxuvc_i2c_notification_read_register failed\n");
		goto error;	
	}
	printf("regval %x\n",regval);
#endif
	//sleep(1);
	read_len = 1; //max 2 bytes can be read together
	ret = mxuvc_i2c_register_notification_callback(
					handle,
					i2c_notify_cb,
					2000, //polling time
					chip_id_reg,
					read_len);
	if(ret < 0){
		printf("err: mxuvc_i2c_register_notification_callback failed\n");
		goto error;	
	}
	ret = mxuvc_i2c_notification_add_threshold(
					handle,
					0x10,
					0x20);
	if(ret < 0){
		printf("err: mxuvc_i2c_notification_add_threshold failed\n");
		goto error;	
	}
	ret = mxuvc_i2c_notification_add_threshold(
					handle,
					0x50,
					0x60);
	if(ret < 0){
		printf("err: mxuvc_i2c_notification_add_threshold failed\n");
		goto error;	
	}
	/* Main 'loop' */
	if (argc > 1){
		counter = atoi(argv[1]);
	} else
		counter = 15;

	while(counter--) {
		if (!mxuvc_audio_alive() || !mxuvc_video_alive())
			goto error;

		printf("\r%i secs left", counter);

		if(counter==10){
			ret = mxuvc_i2c_notification_remove_threshold(
				handle,
				0x10,
				0x20);
			if(ret < 0){
				printf("err: mxuvc_i2c_notification_remove_threshold failed\n");
				goto error;	
			}	
		}

		fflush(stdout);
		sleep(1);
	}
	
	if (audio_threshold > 0){
		mxuvc_alert_audio_disable();
		sleep(1);
	}
	ret = mxuvc_i2c_close(handle);	

	mxuvc_alert_deinit();

	/* Stop audio streaming */
	ret = mxuvc_audio_stop(AUD_CH2);
	if(ret < 0)
		goto error;

	mxuvc_audio_deinit();
	/* Stop video streaming */
	for(channel=CH1; channel<ch_count ; channel++)
	{	
		/* Stop streaming */
		ret = mxuvc_video_stop(channel);
		if(ret < 0)
			goto error;
	}
	/* Deinitialize and exit */
	mxuvc_video_deinit();
	
	close_fds();
	
	return 0;

error:
	mxuvc_alert_deinit();
	mxuvc_audio_deinit();
	mxuvc_video_deinit();
	mxuvc_custom_control_deinit();

	close_fds();	

	printf("Failed\n");
	return 1;
}