int CameraDcam::findCameraByGuid(void) { /*----------------------------------------------------------------------- * get the camera nodes and describe them as we find them * Number of available ports is known globally. *-----------------------------------------------------------------------*/ //as the root node is not determined statically we may reset the bus in order to make a camera become //NOT a root node. //Uses generally the first camera found on bus. int portNum,resetNum, numNodes, foundCamerasOnBus; int resetBus = 1; nodeid_t * camera_nodes = NULL; for (resetNum=0; resetNum < MAX_RESETS && resetBus == 1; resetNum++) { resetBus = 0; foundCamerasOnBus = 0; for (portNum=0; portNum < firewireNumPorts; portNum++) { porthandle[portNum] = dc1394_create_handle(portNum);//handle for portNum if (porthandle[portNum]==NULL) { GDOS_WARNING("error: Unable to aquire handle for port %i.\n", portNum); } //each port can have multiple cameras attached. numNodes = 0; camera_nodes = dc1394_get_camera_nodes(porthandle[portNum], &numNodes, 0); //last parameter defines if output is given. //if any camera is found on this port... if (numNodes > 0) { GDOS_DBG_INFO("found %i cameras to port %i.\n",numNodes, portNum); //try to put them all at their position. int k; for (k = 0; k < numNodes; k++) { //try to get camera guid dc1394_camerainfo info; if (dc1394_get_camera_info(porthandle[portNum], camera_nodes[k], &info) == DC1394_SUCCESS) { //test if camera node is root, if so reset bus... if (camera_nodes[k] == raw1394_get_nodecount(porthandle[portNum])-1) { //reset and retry if root GDOS_WARNING("error: camera found as root node - resetting.\n"); raw1394_reset_bus(porthandle[portNum]); int node_pos = 0; for (node_pos=0; node_pos < 10; node_pos++) resetBus = 1; foundCamerasOnBus = foundCamerasOnBus - k; //no -1 as the array stqarts with 0 } GDOS_DBG_INFO("found camera with guid:%x \n", info.euid_64); //put camera in the global variable if ((cameraGuid == 0) || (cameraGuid == (int) info.euid_64)) { GDOS_DBG_INFO("using camera node:%i with guid:%x \n",camera_nodes[k], info.euid_64); camera_node = camera_nodes[k]; dc1394CameraPortNo = portNum; } foundCamerasOnBus = foundCamerasOnBus + 1; }//if camera_info }//for numCameras }//if numCamera > 0 else { GDOS_ERROR("No cameras found! (%d nodes on the bus)\n" " - could be you need to try a different 1394 device (modify code to fix)\n", numNodes ); } }//for firewireNumPorts }//for MAXRESETS GDOS_DBG_INFO("bus init complete found %i cameras.\n",foundCamerasOnBus); if (resetNum == MAX_RESETS-1) return FW_ERROR; return DC1394_SUCCESS; }
/***************************************************************************** * Open: *****************************************************************************/ static int Open( vlc_object_t *p_this ) { demux_t *p_demux = (demux_t*)p_this; demux_sys_t *p_sys; es_format_t fmt; int i; int i_width; int i_height; int result = 0; if( strncmp(p_demux->psz_access, "dc1394", 6) != 0 ) return VLC_EGENERIC; /* Set up p_demux */ p_demux->pf_demux = Demux; p_demux->pf_control = Control; p_demux->info.i_update = 0; p_demux->info.i_title = 0; p_demux->info.i_seekpoint = 0; p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) ); if( !p_sys ) return VLC_ENOMEM; memset( &fmt, 0, sizeof( es_format_t ) ); /* DEFAULTS */ p_sys->frame_size = MODE_640x480_YUV422; p_sys->width = 640; p_sys->height = 480; p_sys->frame_rate = FRAMERATE_30; p_sys->brightness = 200; p_sys->focus = 0; p_sys->dma_capture = DMA_ON; /* defaults to VIDEO1394 capture mode */ p_sys->fd_audio = -1; p_sys->fd_video = NULL; p_sys->camera_nodes = NULL; p_sys->selected_camera = 0; p_sys->dma_device = NULL; p_sys->selected_uid = 0; /* PROCESS INPUT OPTIONS */ if( process_options(p_demux) != VLC_SUCCESS ) { msg_Err( p_demux, "Bad MRL, please check the option line " "(MRL was: %s)", p_demux->psz_path ); free( p_sys ); p_demux->p_sys = NULL; return VLC_EGENERIC; } msg_Dbg( p_demux, "Selected camera %d", p_sys->selected_camera ); msg_Dbg( p_demux, "Selected uid 0x%llx", p_sys->selected_uid ); ScanCameras( p_sys, p_demux ); if( !p_sys->camera_nodes ) { msg_Err( p_demux, "No camera found !!" ); free( p_sys ); p_demux->p_sys = NULL; return VLC_EGENERIC; } if( p_sys->selected_uid ) { int found = 0; for( i=0; i < p_sys->num_cameras; i++ ) { if( p_sys->camera_nodes[i].uid == p_sys->selected_uid ) { p_sys->selected_camera = i; found++; break; } } if( !found ) { msg_Err( p_demux, "Can't find camera with uid : 0x%llx.", p_sys->selected_uid ); Close( p_this ); return VLC_EGENERIC; } } else if( p_sys->selected_camera >= p_sys->num_cameras ) { msg_Err( p_demux, "there are not this many cameras. (%d/%d)", p_sys->selected_camera, p_sys->num_cameras ); Close( p_this ); return VLC_EGENERIC; } p_sys->fd_video = dc1394_create_handle( p_sys->camera_nodes[p_sys->selected_camera].port ); if( (int)p_sys->fd_video < 0 ) { msg_Err( p_demux, "Can't init dc1394 handle" ); Close( p_this ); return VLC_EGENERIC; } /* get camera info */ result = dc1394_get_camera_info( p_sys->fd_video, p_sys->camera_nodes[p_sys->selected_camera].node, &p_sys->camera_info ); if( result != DC1394_SUCCESS ) { msg_Err( p_demux ,"unable to get camera info" ); Close( p_this ); return VLC_EGENERIC; } dc1394_print_camera_info( &p_sys->camera_info ); result = dc1394_get_camera_misc_info( p_sys->fd_video, p_sys->camera_nodes[p_sys->selected_camera].node, &p_sys->misc_info ); if( result != DC1394_SUCCESS ) { msg_Err( p_demux, "unable to get camera misc info" ); Close( p_this ); return VLC_EGENERIC; } /* init camera and set some video options */ result = dc1394_init_camera( p_sys->camera_info.handle, p_sys->camera_info.id ); if( result != DC1394_SUCCESS ) { msg_Err( p_demux, "unable to get init dc1394 camera" ); Close( p_this ); return VLC_EGENERIC; } if( p_sys->focus ) { result = dc1394_set_focus( p_sys->camera_info.handle, p_sys->camera_nodes[p_sys->selected_camera].node, p_sys->focus ); if( result != DC1394_SUCCESS ) { msg_Err( p_demux, "unable to set initial focus to %u", p_sys->focus ); } msg_Dbg( p_demux, "Initial focus set to %u", p_sys->focus ); } result = dc1394_set_brightness( p_sys->camera_info.handle, p_sys->camera_nodes[p_sys->selected_camera].node, p_sys->brightness ); if( result != DC1394_SUCCESS ) { msg_Err( p_demux, "unable to set init brightness to %d", p_sys->brightness); Close( p_this ); return VLC_EGENERIC; } result = dc1394_set_video_framerate( p_sys->camera_info.handle, p_sys->camera_nodes[p_sys->selected_camera].node, p_sys->frame_rate ); if( result != DC1394_SUCCESS ) { msg_Err( p_demux, "unable to set framerate to %d", p_sys->frame_rate ); Close( p_this ); return VLC_EGENERIC; } p_sys->misc_info.framerate = p_sys->frame_rate; result = dc1394_set_video_format( p_sys->camera_info.handle, p_sys->camera_nodes[p_sys->selected_camera].node, FORMAT_VGA_NONCOMPRESSED ); if( result != DC1394_SUCCESS ) { msg_Err( p_demux, "unable to set video format to VGA_NONCOMPRESSED" ); Close( p_this ); return VLC_EGENERIC; } p_sys->misc_info.format = FORMAT_VGA_NONCOMPRESSED; result = dc1394_set_video_mode( p_sys->camera_info.handle, p_sys->camera_nodes[p_sys->selected_camera].node, p_sys->frame_size ); if( result != DC1394_SUCCESS ) { msg_Err( p_demux, "unable to set video mode" ); Close( p_this ); return VLC_EGENERIC; } p_sys->misc_info.mode = p_sys->frame_size; /* reprobe everything */ result = dc1394_get_camera_info( p_sys->camera_info.handle, p_sys->camera_info.id, &p_sys->camera_info ); if( result != DC1394_SUCCESS ) { msg_Err( p_demux, "Could not get camera basic information!" ); Close( p_this ); return VLC_EGENERIC; } result = dc1394_get_camera_misc_info( p_sys->camera_info.handle, p_sys->camera_info.id, &p_sys->misc_info ); if( result != DC1394_SUCCESS ) { msg_Err( p_demux, "Could not get camera misc information!" ); Close( p_this ); return VLC_EGENERIC; } /* set iso_channel */ result = dc1394_set_iso_channel_and_speed( p_sys->camera_info.handle, p_sys->camera_info.id, p_sys->selected_camera, SPEED_400 ); if( result != DC1394_SUCCESS ) { msg_Err( p_demux, "Could not set iso channel!" ); Close( p_this ); return VLC_EGENERIC; } msg_Dbg( p_demux, "Using ISO channel %d", p_sys->misc_info.iso_channel ); p_sys->misc_info.iso_channel = p_sys->selected_camera; /* and setup capture */ if( p_sys->dma_capture ) { result = dc1394_dma_setup_capture( p_sys->camera_info.handle, p_sys->camera_info.id, p_sys->misc_info.iso_channel, p_sys->misc_info.format, p_sys->misc_info.mode, SPEED_400, p_sys->misc_info.framerate, 10, 0, p_sys->dma_device, &p_sys->camera ); if( result != DC1394_SUCCESS ) { msg_Err( p_demux ,"unable to setup camera" ); Close( p_this ); return VLC_EGENERIC; } } else { result = dc1394_setup_capture( p_sys->camera_info.handle, p_sys->camera_info.id, p_sys->misc_info.iso_channel, p_sys->misc_info.format, p_sys->misc_info.mode, SPEED_400, p_sys->misc_info.framerate, &p_sys->camera ); if( result != DC1394_SUCCESS) { msg_Err( p_demux ,"unable to setup camera" ); Close( p_this ); return VLC_EGENERIC; } } /* TODO - UYV444 chroma converter is missing, when it will be available * fourcc will become variable (and not just a fixed value for UYVY) */ i_width = p_sys->camera.frame_width; i_height = p_sys->camera.frame_height; if( picture_Setup( &p_sys->pic, VLC_CODEC_UYVY, i_width, i_height, i_width * VOUT_ASPECT_FACTOR / i_height ) ) { msg_Err( p_demux ,"unknown chroma" ); Close( p_this ); return VLC_EGENERIC; } es_format_Init( &fmt, VIDEO_ES, VLC_CODEC_UYVY ); fmt.video.i_width = i_width; fmt.video.i_height = i_height; msg_Dbg( p_demux, "added new video es %4.4s %dx%d", (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height ); p_sys->p_es_video = es_out_Add( p_demux->out, &fmt ); if( p_sys->audio_device ) { OpenAudioDev( p_demux ); if( p_sys->fd_audio >= 0 ) { es_format_t fmt; es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S16L ); /* FIXME: hmm, ?? */ fmt.audio.i_channels = p_sys->channels ? p_sys->channels : 1; fmt.audio.i_rate = p_sys->i_sample_rate; fmt.audio.i_bitspersample = 16; fmt.audio.i_blockalign = fmt.audio.i_channels * fmt.audio.i_bitspersample / 8; fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate * fmt.audio.i_bitspersample; msg_Dbg( p_demux, "new audio es %d channels %dHz", fmt.audio.i_channels, fmt.audio.i_rate ); p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt ); } } /* have the camera start sending us data */ result = dc1394_start_iso_transmission( p_sys->camera_info.handle, p_sys->camera_info.id ); if( result != DC1394_SUCCESS ) { msg_Err( p_demux, "unable to start camera iso transmission" ); if( p_sys->dma_capture ) { dc1394_dma_release_camera( p_sys->fd_video, &p_sys->camera ); } else { dc1394_release_camera( p_sys->fd_video, &p_sys->camera ); } Close( p_this ); return VLC_EGENERIC; } p_sys->misc_info.is_iso_on = DC1394_TRUE; return VLC_SUCCESS; }
bool linuxfwCamera::initCamera(int width, int height, bool colour) { if (cameraID < 0) return false; this->width = width; this->height = height; this->colour = colour; bytes = (colour?3:1); quadlet_t format; if (dc1394_query_supported_modes(handle, cameraID,FORMAT_VGA_NONCOMPRESSED,&format) !=DC1394_SUCCESS) { fprintf( stderr,"unable to query format\n"); } image_mode_ = 0; if (colour) { int yuv411 = 29; int rgb = 27; if (format & (0x1<<yuv411)) image_mode_=MODE_640x480_YUV411; else if (format & (0x1<<rgb)) image_mode_=MODE_640x480_RGB; } else { int mono = 26; int yuv411 = 29; if (format & (0x1<<mono)) image_mode_=MODE_640x480_MONO; else if (format & (0x1<<yuv411)) image_mode_=MODE_640x480_YUV411; } if(!image_mode_) return false; quadlet_t frames; if (dc1394_query_supported_framerates(handle, cameraID,FORMAT_VGA_NONCOMPRESSED, image_mode_, &frames) !=DC1394_SUCCESS) { fprintf( stderr,"unable query frame rate\n"); } fps = 60; while (fps>=15) { frame_rate_ = 0; int test_rate = 0; if (fps == 60) test_rate = FRAMERATE_60; else if (fps == 30) test_rate = FRAMERATE_30; else if (fps == 15) test_rate = FRAMERATE_15; if ((frames & (0x1<<(31-(test_rate-FRAMERATE_MIN))))) frame_rate_=test_rate; if(!frame_rate_) { fps=fps/2; } else break; } if(!frame_rate_) return false; printf("framerate\n"); char videoDevice[24] = ""; sprintf(videoDevice,"/dev/video1394/%d",dc1394_get_camera_port(handle)); // setup capture if (dc1394_dma_setup_capture(handle,camera_nodes[cameraID], 0, /* channel */ FORMAT_VGA_NONCOMPRESSED, image_mode_, SPEED_400, frame_rate_, 4, 0, videoDevice, &camera)==DC1394_SUCCESS) { use_dma=true; } else if (dc1394_setup_capture(handle,camera_nodes[cameraID], 0, /* channel */ FORMAT_VGA_NONCOMPRESSED, image_mode_, SPEED_400, frame_rate_, &camera)==DC1394_SUCCESS) { use_dma=false; } else { fprintf( stderr,"unable to setup camera -\n" "make sure that the video mode,framerate and format are supported by your camera\n"); dc1394_release_camera(handle,&camera); dc1394_destroy_handle(handle); dc1394_free_camera_nodes(camera_nodes); return false; } dc1394_camerainfo info; if (dc1394_get_camera_info(handle, cameraID, &info) !=DC1394_SUCCESS ) { //printf("could not read camera info\n"); sprintf(cameraName,"unknown camera"); } else { sprintf(cameraName,"%s, %s", info.vendor, info.model); } /*if (dc1394_set_video_framerate(handle,cameraID,frame_rate)!=DC1394_SUCCESS) { fprintf( stderr,"%i nope\n"); dc1394_release_camera(handle,&camera); //dc1394_destroy_handle(handle); //sdc1394_free_camera_nodes(camera_nodes); return false; }*/ dc1394_free_camera_nodes(camera_nodes); // set trigger mode /*if( dc1394_set_trigger_mode(handle, camera.node, TRIGGER_MODE_0) != DC1394_SUCCESS) { //fprintf( stderr, "unable to set camera trigger mode\n"); }*/ buffer = new unsigned char[width*height*bytes]; return true; }
/***************************************************************************** * ScanCameras *****************************************************************************/ static void ScanCameras( dc1394_sys *sys, demux_t *p_demux ) { struct raw1394_portinfo portinfo[MAX_IEEE1394_HOSTS]; raw1394handle_t tempFd; dc1394_camerainfo info; dc_camera *node_list = NULL; nodeid_t *nodes = NULL; int num_ports = 0; int num_cameras = 0; int nodecount; int i, n; memset( &portinfo, 0, sizeof(portinfo) ); msg_Dbg( p_demux, "Scanning for ieee1394 ports ..." ); tempFd = raw1394_new_handle(); if( !tempFd ) return VLC_EGENERIC; raw1394_get_port_info( tempFd, portinfo, MAX_IEEE1394_HOSTS); raw1394_destroy_handle( tempFd ); for( i=0; i < MAX_IEEE1394_HOSTS; i++ ) { /* check if port exists and has at least one node*/ if( !portinfo[i].nodes ) continue; nodes = NULL; nodecount = 0; tempFd = dc1394_create_handle( i ); /* skip this port if we can't obtain a valid handle */ if( !tempFd ) continue; msg_Dbg( p_demux, "Found ieee1394 port %d (%s) ... " "checking for camera nodes", i, portinfo[i].name ); num_ports++; nodes = dc1394_get_camera_nodes( tempFd, &nodecount, 0 ); if( nodecount ) { msg_Dbg( p_demux, "Found %d dc1394 cameras on port %d (%s)", nodecount, i, portinfo[i].name ); if( node_list ) node_list = realloc( node_list, sizeof(dc_camera) * (num_cameras+nodecount) ); else node_list = malloc( sizeof(dc_camera) * nodecount); for( n = 0; n < nodecount; n++ ) { int result = 0; result = dc1394_get_camera_info( tempFd, nodes[n], &info ); if( result == DC1394_SUCCESS ) { node_list[num_cameras+n].uid = info.euid_64; } node_list[num_cameras+n].node = nodes[n]; node_list[num_cameras+n].port = i; } num_cameras += nodecount; } else msg_Dbg( p_demux, "no cameras found on port %d (%s)", i, portinfo[i].name ); if( tempFd ) dc1394_destroy_handle( tempFd ); } sys->num_ports = num_ports; sys->num_cameras = num_cameras; sys->camera_nodes = node_list; }