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(¤t_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; 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, ®val, 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, ®val, 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; }
/* Initialize the audio path */ int mxuvc_audio_init(const char *backend, const char *options) { RECORD("\"%s\", \"%s\"", backend, options); struct libusb_device *dev = NULL; int ret=0, i, config; uint16_t vendor_id=0xdead, product_id=0xbeef; char *str=NULL, *opt, *value; int audio_sampling_rate; TRACE("Initializing the audio\n"); /* Check that the correct video backend was requested*/ if(strncmp(backend, "libusb-uac", 10)) { ERROR(-1, "The audio backend requested (%s) does not match " "the implemented one (libusb-uac)", backend); } /* Set init parameters to their default values */ packets_per_transfer = PACKETS_PER_TRANSFER_DEFAULT; num_transfers = NUM_TRANSFERS_DEFAULT; audio_duration_ms = AUDIO_DURATION_MS_DEFAULT; audio_sampling_rate = AUDIO_SAMPLING_RATE_DEFAULT; /* Copy the options string to a new buffer since next_opt() needs * non const strings and options could be a const string */ if(options != NULL) { str = (char*)malloc(strlen(options)+1); strncpy(str, options, strlen(options)); *(str + strlen(options)) = '\0'; } /* Get backend option from the option string */ ret = next_opt(str, &opt, &value); while(ret == 0) { if(strncmp(opt, "vid", 3) == 0) { vendor_id = (uint16_t) strtoul(value, NULL, 16); } else if(strncmp(opt, "pid", 3) == 0) { product_id = (uint16_t) strtoul(value, NULL, 16); } else if(strncmp(opt, "packets_per_transfer", 19) == 0) { packets_per_transfer = (unsigned int) strtoul(value, NULL, 10); } else if(strncmp(opt, "num_transfers", 12) == 0) { num_transfers = (unsigned int) strtoul(value, NULL, 10); } else if(strncmp(opt, "audio_duration_ms", 17) == 0) { audio_duration_ms = (unsigned int) strtoul(value, NULL, 10); } else if (strncmp (opt, "audio_sampling_rate", 19) == 0) { audio_sampling_rate = (unsigned int) strtoul (value, NULL, 10); } else { WARNING("Unrecognized option: '%s'", opt); } ret = next_opt(NULL, &opt, &value); } /* Display the values we are going to use */ TRACE("Using vid = 0x%x\n", vendor_id); TRACE("Using pid = 0x%x\n", product_id); TRACE("Using packets_per_transfer = %i\n", packets_per_transfer); TRACE("Using num_transfers = %i\n", num_transfers); TRACE("Using audio_duration_ms = %i\n", audio_duration_ms); TRACE("Using audio_sampling_rate = %i\n", audio_sampling_rate); /* Free the memory allocated to parse 'options' */ if(str) free(str); /* Initialize the backend */ aud_started = 0; audio_disconnected = 0; ret = init_libusb(&audio_ctx); if(ret < 0) return -1; audio_hdl = libusb_open_device_with_vid_pid(audio_ctx, vendor_id, product_id); CHECK_ERROR(audio_hdl == NULL, -1, "Could not open USB device " "%x:%x", vendor_id, product_id); dev = libusb_get_device(audio_hdl); if(dev == NULL) { printf("Unexpected error: libusb_get_device returned a NULL " "pointer."); mxuvc_audio_deinit(); return -1; } /* Get active USB configuration */ libusb_get_configuration(audio_hdl, &config); /* Parse USB decriptors from active USB configuration * to get all the UVC/UAC info needed */ ret = aparse_usb_config(dev, config); if(ret < 0){ mxuvc_audio_deinit(); return -1; } /* Initialize audio */ /* Claim audio control interface */ /* Check if a kernel driver is active on the audio control interface */ ret = libusb_kernel_driver_active(audio_hdl, aud_cfg.ctrlif); if(ret < 0) printf("Error: libusb_kernel_driver_active failed %d\n", ret); if(ret == 1) { TRACE("Detach the kernel driver...\n"); /* If kernel driver is active, detach it so that we can claim * the interface */ ret = libusb_detach_kernel_driver(audio_hdl, aud_cfg.ctrlif); if(ret < 0) printf("Error: libusb_detach_kernel_driver failed " "%d\n", ret); } /* Claim audio control interface */ ret = libusb_claim_interface(audio_hdl, aud_cfg.ctrlif); if(ret < 0) { printf("Error: libusb_claim_interface failed %d\n", ret); } /* Claim audio streaming interface */ /* Check if a kernel driver is active on the audio interface */ ret = libusb_kernel_driver_active(audio_hdl, aud_cfg.interface); if(ret < 0) printf("Error: libusb_kernel_driver_active failed %d\n", ret); if(ret == 1) { TRACE("Detach the kernel driver...\n"); /* If kernel driver is active, detach it so that we can claim * the interface */ ret = libusb_detach_kernel_driver(audio_hdl, aud_cfg.interface); if(ret < 0) printf("Error: libusb_detach_kernel_driver failed " "%d\n",ret); } /* Claim audio streaming interface */ ret = libusb_claim_interface(audio_hdl, aud_cfg.interface); if(ret < 0) { printf("Error: libusb_claim_interface failed %d\n",ret); } /* Select sampling rate */ for(i=0;i<MAX_AUD_FMTS;i++) { if(aud_cfg.format[i].samFr == audio_sampling_rate){ aud_cfg.fmt_idx = i; break; } CHECK_ERROR(i == MAX_AUD_FMTS-1, -1, "Unable to set the sampling rate to %i", audio_sampling_rate); } /* Map default UAC format to Audio format */ cur_aud_format = AUD_FORMAT_PCM_RAW; /* Get min, max and real unit id for ctrl */ AUDIO_CTRL *ctrl = uac_controls; int16_t min = 0, max = 0; uint16_t res = 0; while(ctrl->id != CTRL_NONE) { switch(ctrl->unit) { TRACE(">>>>>id:%d unit:%d\n", ctrl->id,ctrl->unit); case FEATURE: ctrl->unit = aud_cfg.ctrl_feature; break; default: ERROR(-1, "Unsupported control unit (%i) for " "audio control %i", ctrl->unit, ctrl->id); } if (ctrl->id == CTRL_MUTE) { ctrl++; continue; } ret = get_ctrl(ctrl->id, GET_MIN, (void*) &min); CHECK_ERROR(ret < 0, -1, "Unable to get min (GET_MIN) for audio " "control: id=%i, cs=%i, cn=%i.", ctrl->id, ctrl->cs, ctrl->cn); ctrl->min = min; ret = get_ctrl(ctrl->id, GET_MAX, (void*) &max); CHECK_ERROR(ret < 0, -1, "Unable to get max (GET_MAX) for audio " "control: id=%i, cs=%i, cn=%i.", ctrl->id, ctrl->cs, ctrl->cn); ctrl->max = max; ret = get_ctrl(ctrl->id, GET_RES, (void*) &res); CHECK_ERROR(ret < 0, -1, "Unable to get res (GET_RES) for audio " "control: id=%i, cs=%i, cn=%i.", ctrl->id, ctrl->cs, ctrl->cn); ctrl->res = res; ctrl++; } /* Register removal USB event*/ register_libusb_removal_cb((libusb_pollfd_removed_cb) audio_removed, audio_hdl); /* Start event thread/loop */ ret = start_libusb_events(); if(ret < 0) return -1; audio_initialized = 1; return 0; }
int main(int argc, char **argv) { int ret; int counter=0; int bitrate; int aec_enable = 0; if ( argc < 2 ){ printf("USAGE: capture_audmux <aec_enable 0/1> [sampling freq 8000|16000|24000] [no:of seconds to run the test]\n"); return 0; } 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; 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; mxuvc_custom_control_init(); if(argv[1] != NULL) aec_enable = atoi(argv[1]); if(aec_enable) { ret = mxuvc_custom_control_enable_aec(); if(ret < 0) goto error; } else { ret = mxuvc_custom_control_disable_aec(); if(ret < 0) goto error; } //TBD ret = mxuvc_audio_set_format(AUD_CH2, AUD_FORMAT_AAC_RAW); if(ret < 0) goto error; if(aec_enable){ ret = mxuvc_custom_control_set_audio_codec_samplerate(BOARD_SAMPLE_RATE); if(ret < 0) goto error; } if(argc > 2) samp_freq = atoi(argv[2]); if(samp_freq == 8000 || samp_freq == 16000 || samp_freq == 24000){ ret = mxuvc_audio_set_samplerate(AUD_CH2, samp_freq); if(ret < 0) goto error; }else printf("ERR: Unsupported sampling frequency %d\n",samp_freq); mxuvc_audio_set_volume(100); if(ret < 0) goto error; ret = mxuvc_audio_get_bitrate(&bitrate); if(ret < 0) goto error; printf("audio bitrate %d\n",bitrate); sleep(1); /* Main 'loop' */ if(argc > 3){ counter = atoi(argv[3]); } else{ counter = 15; } ret = mxuvc_audio_start(AUD_CH2); if(ret < 0) goto error; while(counter--) { if (!mxuvc_audio_alive()) goto error; printf("\r%i secs left", counter+1); fflush(stdout); sleep(1); /* uncomment to test Mute/Unmute */ /* if (counter >= 15) mxuvc_set_mic_mute(1); else mxuvc_set_mic_mute(0);*/ } /* Stop audio streaming */ ret = mxuvc_audio_stop(AUD_CH2); if(ret < 0) goto error; mxuvc_audio_deinit(); mxuvc_video_deinit(); mxuvc_custom_control_deinit(); close_fds(); return 0; error: mxuvc_audio_deinit(); close_fds(); printf("Failed\n"); return 1; }