00001
00010 #include "hrpsys/util/VectorConvert.h"
00011 #include "VideoCapture.h"
00012
00013
00014
00015 static const char* videocapture_spec[] =
00016 {
00017 "implementation_id", "VideoCapture",
00018 "type_name", "VideoCapture",
00019 "description", "video capture component",
00020 "version", HRPSYS_PACKAGE_VERSION,
00021 "vendor", "JSK",
00022 "category", "example",
00023 "activity_type", "DataFlowComponent",
00024 "max_instance", "10",
00025 "language", "C++",
00026 "lang_type", "compile",
00027
00028 "conf.default.initialMode", "continuous",
00029 "conf.default.devIds", "0",
00030 "conf.default.width", "640",
00031 "conf.default.height", "480",
00032 "conf.default.frameRate", "1",
00033
00034 ""
00035 };
00036
00037
00038 VideoCapture::VideoCapture(RTC::Manager* manager)
00039 : RTC::DataFlowComponentBase(manager),
00040
00041 m_MultiCameraImagesOut ("MultiCameraImages", m_MultiCameraImages),
00042 m_CameraImageOut ("CameraImage", m_CameraImage),
00043 m_CameraCaptureServicePort("CameraCaptureService"),
00044 m_CameraCaptureService(this),
00045
00046 m_mode(CONTINUOUS)
00047 {
00048 }
00049
00050 VideoCapture::~VideoCapture()
00051 {
00052 }
00053
00054
00055
00056 RTC::ReturnCode_t VideoCapture::onInitialize()
00057 {
00058 std::cout << m_profile.instance_name << ": onInitialize()" << std::endl;
00059
00060
00061
00062 bindParameter("initialMode", m_initialMode, "continuous");
00063 bindParameter("devIds", m_devIds, "0");
00064 bindParameter("width", m_width, "640");
00065 bindParameter("height", m_height, "480");
00066 bindParameter("frameRate", m_frameRate, "1");
00067
00068
00069
00070
00071
00072
00073
00074
00075 if (m_devIds.size() == 1){
00076 addOutPort ("CameraImage", m_CameraImageOut);
00077 }else{
00078 addOutPort ("MultiCameraImages", m_MultiCameraImagesOut);
00079 }
00080
00081
00082 m_CameraCaptureServicePort.registerProvider("service0", "CameraCaptureService", m_CameraCaptureService);
00083
00084
00085
00086
00087 addPort(m_CameraCaptureServicePort);
00088
00089
00090
00091 return RTC::RTC_OK;
00092 }
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 RTC::ReturnCode_t VideoCapture::onStartup(RTC::UniqueId ec_id)
00104 {
00105 return RTC::RTC_OK;
00106 }
00107
00108
00109
00110
00111
00112
00113
00114
00115 RTC::ReturnCode_t VideoCapture::onActivated(RTC::UniqueId ec_id)
00116 {
00117 std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
00118 m_tOld = (double)(coil::gettimeofday());
00119 if (m_initialMode == "continuous"){
00120 m_mode = CONTINUOUS;
00121 }else{
00122 m_mode = SLEEP;
00123 }
00124
00125 if (m_devIds.size() == 1){
00126 std::cout << "** devId:" << m_devIds[0] << std::endl;
00127 v4l_capture *cam = new v4l_capture ();
00128 if (cam->init(m_width, m_height, m_devIds[0]) != 0) return RTC::RTC_ERROR;
00129 m_cameras.push_back (cam);
00130 m_CameraImage.data.image.format = Img::CF_RGB;
00131 m_CameraImage.data.image.width = cam->getWidth ();
00132 m_CameraImage.data.image.height = cam->getHeight ();
00133 m_CameraImage.data.image.raw_data.length (cam->getWidth () * cam->getHeight () * 3);
00134 }else{
00135 m_MultiCameraImages.data.image_seq.length (m_devIds.size ());
00136 m_MultiCameraImages.data.camera_set_id = 0;
00137 for (unsigned int i = 0; i < m_devIds.size (); i++)
00138 {
00139 std::cout << "** devId:" << m_devIds[i] << std::endl;
00140 v4l_capture *cam = new v4l_capture ();
00141 cam->init(m_width, m_height, m_devIds[i]);
00142 m_cameras.push_back (cam);
00143 m_MultiCameraImages.data.image_seq[i].image.format = Img::CF_RGB;
00144 m_MultiCameraImages.data.image_seq[i].image.width = cam->getWidth ();
00145 m_MultiCameraImages.data.image_seq[i].image.height = cam->getHeight ();
00146 m_MultiCameraImages.data.image_seq[i].image.raw_data.length (cam->getWidth () * cam->getHeight () * 3);
00147 }
00148 }
00149
00150 return RTC::RTC_OK;
00151 }
00152
00153 RTC::ReturnCode_t VideoCapture::onDeactivated(RTC::UniqueId ec_id)
00154 {
00155 std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
00156 for (unsigned int i=0; i< m_cameras.size(); i++){
00157 delete m_cameras[i];
00158 }
00159 m_cameras.clear();
00160 return RTC::RTC_OK;
00161 }
00162
00163 RTC::ReturnCode_t VideoCapture::onExecute(RTC::UniqueId ec_id)
00164 {
00165
00166 capture();
00167
00168 double tNew = (double)(coil::gettimeofday());
00169 double dt = (double)(tNew - m_tOld);
00170 if (dt > 1.0/m_frameRate){
00171 m_tOld = tNew;
00172 }else{
00173 return RTC::RTC_OK;
00174 }
00175
00176 if (m_mode == SLEEP) return RTC::RTC_OK;
00177
00178 if (m_cameras.size() == 1){
00179 m_CameraImageOut.write();
00180 }else{
00181 m_MultiCameraImagesOut.write();
00182 }
00183
00184 if (m_mode == ONESHOT) m_mode = SLEEP;
00185
00186 return RTC::RTC_OK;
00187 }
00188
00189 void VideoCapture::capture()
00190 {
00191 if (m_cameras.size() == 1){
00192 m_CameraImage.error_code = 0;
00193 uchar *imgFrom = m_cameras[0]->capture();
00194 memcpy(m_CameraImage.data.image.raw_data.get_buffer(), imgFrom,
00195 m_CameraImage.data.image.raw_data.length() * sizeof(uchar));
00196 return;
00197 }else{
00198 m_MultiCameraImages.error_code = 0;
00199 for (unsigned int i = 0; i < m_cameras.size (); i++)
00200 {
00201 uchar *imgFrom = m_cameras[i]->capture();
00202 memcpy (m_MultiCameraImages.data.image_seq[i].image.raw_data.get_buffer(), imgFrom,
00203 m_MultiCameraImages.data.image_seq[i].image.raw_data.length() * sizeof (uchar));
00204 }
00205 }
00206 }
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243 void VideoCapture::take_one_frame()
00244 {
00245 m_mode = ONESHOT;
00246 }
00247
00248 void VideoCapture::start_continuous()
00249 {
00250 m_mode = CONTINUOUS;
00251 }
00252
00253 void VideoCapture::stop_continuous()
00254 {
00255 m_mode = SLEEP;
00256 }
00257
00258
00259 extern "C"
00260 {
00261
00262 void VideoCaptureInit(RTC::Manager* manager)
00263 {
00264 RTC::Properties profile(videocapture_spec);
00265 manager->registerFactory(profile,
00266 RTC::Create<VideoCapture>,
00267 RTC::Delete<VideoCapture>);
00268 }
00269
00270 };
00271
00272