| 1 | /* ======================================================================== *\
|
|---|
| 2 | !
|
|---|
| 3 | ! *
|
|---|
| 4 | ! * This file is part of MARS, the MAGIC Analysis and Reconstruction
|
|---|
| 5 | ! * Software. It is distributed to you in the hope that it can be a useful
|
|---|
| 6 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
|
|---|
| 7 | ! * It is distributed WITHOUT ANY WARRANTY.
|
|---|
| 8 | ! *
|
|---|
| 9 | ! * Permission to use, copy, modify and distribute this software and its
|
|---|
| 10 | ! * documentation for any purpose is hereby granted without fee,
|
|---|
| 11 | ! * provided that the above copyright notice appear in all copies and
|
|---|
| 12 | ! * that both that copyright notice and this permission notice appear
|
|---|
| 13 | ! * in supporting documentation. It is provided "as is" without express
|
|---|
| 14 | ! * or implied warranty.
|
|---|
| 15 | ! *
|
|---|
| 16 | !
|
|---|
| 17 | !
|
|---|
| 18 | ! Author(s): Thomas Bretz 1/2008 <mailto:tbretz@astro.uni-wuerzburg.de>
|
|---|
| 19 | !
|
|---|
| 20 | ! Copyright: MAGIC Software Development, 2000-2008
|
|---|
| 21 | !
|
|---|
| 22 | !
|
|---|
| 23 | \* ======================================================================== */
|
|---|
| 24 |
|
|---|
| 25 | /////////////////////////////////////////////////////////////////////////////
|
|---|
| 26 | //
|
|---|
| 27 | // MVideo
|
|---|
| 28 | //
|
|---|
| 29 | // Interface to Video4Linux at a simple level
|
|---|
| 30 | //
|
|---|
| 31 | /////////////////////////////////////////////////////////////////////////////
|
|---|
| 32 | #include "MVideo.h"
|
|---|
| 33 |
|
|---|
| 34 | // iostream
|
|---|
| 35 | #include <iostream>
|
|---|
| 36 |
|
|---|
| 37 | // open
|
|---|
| 38 | #include <sys/types.h>
|
|---|
| 39 | #include <sys/stat.h>
|
|---|
| 40 | #include <fcntl.h>
|
|---|
| 41 |
|
|---|
| 42 | #include <unistd.h> // usleep
|
|---|
| 43 | #include <errno.h> // errno
|
|---|
| 44 | #include <sys/mman.h> // mmap
|
|---|
| 45 | #include <sys/ioctl.h> // ioctl
|
|---|
| 46 |
|
|---|
| 47 | #include <TEnv.h>
|
|---|
| 48 | #include <TString.h>
|
|---|
| 49 |
|
|---|
| 50 | #include "MLog.h"
|
|---|
| 51 | #include "MLogManip.h"
|
|---|
| 52 |
|
|---|
| 53 | #undef DEBUG
|
|---|
| 54 |
|
|---|
| 55 | using namespace std;
|
|---|
| 56 |
|
|---|
| 57 | //ClassImp(MVideo);
|
|---|
| 58 |
|
|---|
| 59 | MVideoCtrl::MVideoCtrl(const v4l2_queryctrl &ctrl)
|
|---|
| 60 | {
|
|---|
| 61 | fId = ctrl.id;
|
|---|
| 62 | fName = (const char*)ctrl.name;
|
|---|
| 63 | fMinimum = ctrl.minimum;
|
|---|
| 64 | fMaximum = ctrl.maximum;
|
|---|
| 65 | fStep = ctrl.step;
|
|---|
| 66 | fDefault = ctrl.default_value;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | // -------------------------------------------------------------
|
|---|
| 70 | //
|
|---|
| 71 | // Constructor. Specify the device (e.g. "/dev/video") to be used
|
|---|
| 72 | //
|
|---|
| 73 | MVideo::MVideo(const char *path) : fPath(path), fFileDesc(-1), fMapBuffer(0)
|
|---|
| 74 | {
|
|---|
| 75 | Reset();
|
|---|
| 76 |
|
|---|
| 77 | fControls.SetOwner();
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | // -------------------------------------------------------------
|
|---|
| 81 | //
|
|---|
| 82 | // Internal function to reset the descriptors of the device
|
|---|
| 83 | //
|
|---|
| 84 | void MVideo::Reset()
|
|---|
| 85 | {
|
|---|
| 86 | memset(&fCaps, 0, sizeof(fCaps));
|
|---|
| 87 | memset(&fChannel, 0, sizeof(fChannel));
|
|---|
| 88 | memset(&fBuffer, 0, sizeof(fBuffer));
|
|---|
| 89 | memset(&fAbil, 0, sizeof(fAbil));
|
|---|
| 90 |
|
|---|
| 91 | fFileDesc = -1;
|
|---|
| 92 | fMapBuffer = 0;
|
|---|
| 93 | fChannel.channel = -1;
|
|---|
| 94 | fAbil.tuner = -1;
|
|---|
| 95 |
|
|---|
| 96 | fControls.Delete();
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | // -------------------------------------------------------------
|
|---|
| 100 | //
|
|---|
| 101 | // Mapper around ioctl for easier access to the device
|
|---|
| 102 | //
|
|---|
| 103 | int MVideo::Ioctl(int req, void *opt, bool allowirq, bool force) const
|
|---|
| 104 | {
|
|---|
| 105 | if (fFileDesc<0)
|
|---|
| 106 | {
|
|---|
| 107 | gLog << err << "ERROR - Ioctl: Device " << fPath << " not open." << endl;
|
|---|
| 108 | return -1;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | while (1)
|
|---|
| 112 | {
|
|---|
| 113 | // FIXME: This call is a possible source for a hangup
|
|---|
| 114 | const int rc = ioctl(fFileDesc, req, opt);
|
|---|
| 115 | if (rc>=0)
|
|---|
| 116 | return rc;
|
|---|
| 117 |
|
|---|
| 118 | // errno== 4: Interrupted system call (e.g. by alarm())
|
|---|
| 119 | // errno==16: Device or resource busy
|
|---|
| 120 | if (errno==4 || errno==16)
|
|---|
| 121 | {
|
|---|
| 122 | if (!allowirq && errno==4)
|
|---|
| 123 | return -4;
|
|---|
| 124 |
|
|---|
| 125 | gLog << err << "ERROR - MVideo::Ioctl 0x" << hex << req << ": errno=" << dec << errno << " - ";
|
|---|
| 126 | gLog << strerror(errno) << " (rc=" << rc << ")" << endl;
|
|---|
| 127 | usleep(10);
|
|---|
| 128 | continue;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | if (!force)
|
|---|
| 132 | {
|
|---|
| 133 | gLog << err << "ERROR - MVideo::Ioctl 0x" << hex << req << ": errno=" << dec << errno << " - ";
|
|---|
| 134 | gLog << strerror(errno) << " (rc=" << rc << ")" << endl;
|
|---|
| 135 | }
|
|---|
| 136 | return rc;
|
|---|
| 137 | }
|
|---|
| 138 | return -1;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | // -------------------------------------------------------------
|
|---|
| 142 | //
|
|---|
| 143 | // Read the capabilities of the device
|
|---|
| 144 | //
|
|---|
| 145 | Bool_t MVideo::GetCapabilities()
|
|---|
| 146 | {
|
|---|
| 147 | return Ioctl(VIDIOCGCAP, &fCaps)!=-1;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | // -------------------------------------------------------------
|
|---|
| 151 | //
|
|---|
| 152 | // Read the properties of the device
|
|---|
| 153 | //
|
|---|
| 154 | Bool_t MVideo::GetProperties()
|
|---|
| 155 | {
|
|---|
| 156 | return Ioctl(VIDIOCGPICT, &fProp)!=-1;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | // -------------------------------------------------------------
|
|---|
| 160 | //
|
|---|
| 161 | // Read the video standard
|
|---|
| 162 | //
|
|---|
| 163 | Bool_t MVideo::GetVideoStandard()
|
|---|
| 164 | {
|
|---|
| 165 | return Ioctl(VIDIOC_G_STD, &fVideoStandard)==-1;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | // -------------------------------------------------------------
|
|---|
| 169 | //
|
|---|
| 170 | // Read the abilities of the tuner
|
|---|
| 171 | //
|
|---|
| 172 | Bool_t MVideo::GetTunerAbilities()
|
|---|
| 173 | {
|
|---|
| 174 | fAbil.tuner = 0; // FIXME?
|
|---|
| 175 | return Ioctl(VIDIOCGTUNER, &fAbil)!=-1;
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | // -------------------------------------------------------------
|
|---|
| 179 | //
|
|---|
| 180 | // Enumerate (get) all controls from the device and store them
|
|---|
| 181 | // as MVideoCtrl in fControls, starting with the id given as
|
|---|
| 182 | // argument.
|
|---|
| 183 | //
|
|---|
| 184 | Bool_t MVideo::EnumerateControls(UInt_t id)
|
|---|
| 185 | {
|
|---|
| 186 | struct v4l2_queryctrl qctrl;
|
|---|
| 187 | memset(&qctrl, 0, sizeof(qctrl));
|
|---|
| 188 | qctrl.id = id;
|
|---|
| 189 |
|
|---|
| 190 | while (1)
|
|---|
| 191 | {
|
|---|
| 192 | if (Ioctl(VIDIOC_QUERYCTRL, &qctrl, true, true)==-1)
|
|---|
| 193 | break;
|
|---|
| 194 |
|
|---|
| 195 | if (qctrl.maximum<=qctrl.minimum)
|
|---|
| 196 | continue;
|
|---|
| 197 |
|
|---|
| 198 | fControls.Add(new MVideoCtrl(qctrl));
|
|---|
| 199 |
|
|---|
| 200 | qctrl.id++;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | return kTRUE;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | // -------------------------------------------------------------
|
|---|
| 207 | //
|
|---|
| 208 | // Enumerate (get) all basic and private controls from the
|
|---|
| 209 | // device and store them as MVideoCtrl in fControls.
|
|---|
| 210 | //
|
|---|
| 211 | Bool_t MVideo::EnumerateControls()
|
|---|
| 212 | {
|
|---|
| 213 | if (!EnumerateControls(V4L2_CID_BASE))
|
|---|
| 214 | return kFALSE;
|
|---|
| 215 | if (!EnumerateControls(V4L2_CID_PRIVATE_BASE))
|
|---|
| 216 | return kFALSE;
|
|---|
| 217 |
|
|---|
| 218 | return kTRUE;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | // -------------------------------------------------------------
|
|---|
| 222 | //
|
|---|
| 223 | // Reset a given control to it's default value as defined
|
|---|
| 224 | // by the device.
|
|---|
| 225 | //
|
|---|
| 226 | Bool_t MVideo::ResetControl(MVideoCtrl &vctrl) const
|
|---|
| 227 | {
|
|---|
| 228 | return WriteControl(vctrl, vctrl.fDefault);
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | // -------------------------------------------------------------
|
|---|
| 232 | //
|
|---|
| 233 | // Reset all enumereated device controls to their default.
|
|---|
| 234 | // The default is defined by the device iteself.
|
|---|
| 235 | //
|
|---|
| 236 | Bool_t MVideo::ResetControls() const
|
|---|
| 237 | {
|
|---|
| 238 | Bool_t rc = kTRUE;
|
|---|
| 239 |
|
|---|
| 240 | TIter Next(&fControls);
|
|---|
| 241 | MVideoCtrl *ctrl = 0;
|
|---|
| 242 | while ((ctrl=((MVideoCtrl*)Next())))
|
|---|
| 243 | if (!ResetControl(*ctrl))
|
|---|
| 244 | {
|
|---|
| 245 | gLog << err << "ERROR - Could not reset " << ctrl->fName << "." << endl;
|
|---|
| 246 | rc = kFALSE;
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | return rc;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | // -------------------------------------------------------------
|
|---|
| 253 | //
|
|---|
| 254 | // Read the value of the given control from the device
|
|---|
| 255 | // and store it back into the given MVideoCtrl.
|
|---|
| 256 | //
|
|---|
| 257 | Bool_t MVideo::ReadControl(MVideoCtrl &vctrl) const
|
|---|
| 258 | {
|
|---|
| 259 | struct v4l2_control ctrl = { vctrl.fId, 0 };
|
|---|
| 260 | if (Ioctl(VIDIOC_G_CTRL, &ctrl)==-1)
|
|---|
| 261 | return kFALSE;
|
|---|
| 262 |
|
|---|
| 263 | vctrl.fValue = ctrl.value;
|
|---|
| 264 |
|
|---|
| 265 | return kTRUE;
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | // -------------------------------------------------------------
|
|---|
| 269 | //
|
|---|
| 270 | // Write the given value into the given control of the device.
|
|---|
| 271 | // On success the value is stored in the given MVideoCtrl.
|
|---|
| 272 | //
|
|---|
| 273 | Bool_t MVideo::WriteControl(MVideoCtrl &vctrl, Int_t val) const
|
|---|
| 274 | {
|
|---|
| 275 | if (val<vctrl.fMinimum)
|
|---|
| 276 | {
|
|---|
| 277 | gLog << err << "ERROR - Value of " << val << " below minimum of " << vctrl.fMinimum << " for " << vctrl.fName << endl;
|
|---|
| 278 | return kFALSE;
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | if (val>vctrl.fMaximum)
|
|---|
| 282 | {
|
|---|
| 283 | gLog << err << "ERROR - Value of " << val << " above maximum of " << vctrl.fMaximum << " for " << vctrl.fName << endl;
|
|---|
| 284 | return kFALSE;
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | struct v4l2_control ctrl = { vctrl.fId, val };
|
|---|
| 288 | if (Ioctl(VIDIOC_S_CTRL, &ctrl)==-1)
|
|---|
| 289 | return kFALSE;
|
|---|
| 290 |
|
|---|
| 291 | vctrl.fValue = val;
|
|---|
| 292 |
|
|---|
| 293 | return kTRUE;
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | // -------------------------------------------------------------
|
|---|
| 297 | //
|
|---|
| 298 | // Set all controls from a TEnv. Note that all whitespaces
|
|---|
| 299 | // and colons in the control names (as defined by the name of
|
|---|
| 300 | // the MVideoCtrls stored in fControls) are replaced by
|
|---|
| 301 | // underscores.
|
|---|
| 302 | //
|
|---|
| 303 | Bool_t MVideo::SetControls(TEnv &env) const
|
|---|
| 304 | {
|
|---|
| 305 | Bool_t rc = kTRUE;
|
|---|
| 306 |
|
|---|
| 307 | TIter Next(&fControls);
|
|---|
| 308 | TObject *o = 0;
|
|---|
| 309 | while ((o=Next()))
|
|---|
| 310 | {
|
|---|
| 311 | if (!env.Defined(o->GetName()))
|
|---|
| 312 | continue;
|
|---|
| 313 |
|
|---|
| 314 | TString str = env.GetValue(o->GetName(), "");
|
|---|
| 315 | str = str.Strip(TString::kBoth);
|
|---|
| 316 | str.ReplaceAll(" ", "_");
|
|---|
| 317 | str.ReplaceAll(":", "_");
|
|---|
| 318 | if (str.IsNull())
|
|---|
| 319 | continue;
|
|---|
| 320 |
|
|---|
| 321 | MVideoCtrl &ctrl = *static_cast<MVideoCtrl*>(o);
|
|---|
| 322 |
|
|---|
| 323 | const Int_t val = str=="default" || str=="def" ?
|
|---|
| 324 | ctrl.fDefault : env.GetValue(o->GetName(), 0);
|
|---|
| 325 |
|
|---|
| 326 | if (!WriteControl(ctrl, val))
|
|---|
| 327 | rc = kFALSE;
|
|---|
| 328 | }
|
|---|
| 329 |
|
|---|
| 330 | return rc;
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 |
|
|---|
| 334 | // -------------------------------------------------------------
|
|---|
| 335 | //
|
|---|
| 336 | // Open channel ch of the device
|
|---|
| 337 | //
|
|---|
| 338 | Bool_t MVideo::Open(Int_t ch)
|
|---|
| 339 | {
|
|---|
| 340 | Bool_t rc = Init(ch);
|
|---|
| 341 | if (!rc)
|
|---|
| 342 | Close();
|
|---|
| 343 | return rc;
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | // -------------------------------------------------------------
|
|---|
| 347 | //
|
|---|
| 348 | // Open a channel of the device and retriev all necessary
|
|---|
| 349 | // informations from the driver. Initialize the shared
|
|---|
| 350 | // memory. Other access methods are not supported yet.
|
|---|
| 351 | //
|
|---|
| 352 | Bool_t MVideo::Init(Int_t channel)
|
|---|
| 353 | {
|
|---|
| 354 | if (IsOpen())
|
|---|
| 355 | {
|
|---|
| 356 | gLog << warn << "WARNING - Device " << fPath << " already open." << endl;
|
|---|
| 357 | return kTRUE;
|
|---|
| 358 | }
|
|---|
| 359 |
|
|---|
| 360 | gLog << all << "Opening " << fPath << "... " << flush;
|
|---|
| 361 | do
|
|---|
| 362 | {
|
|---|
| 363 | fFileDesc = open(fPath, O_RDWR);
|
|---|
| 364 | usleep(1);
|
|---|
| 365 | }
|
|---|
| 366 | while (errno==19 && fFileDesc==-1);
|
|---|
| 367 |
|
|---|
| 368 | if (fFileDesc == -1)
|
|---|
| 369 | {
|
|---|
| 370 | gLog << err << "ERROR: " << strerror(errno) << endl;
|
|---|
| 371 | return kFALSE;
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | gLog << "done (" << fFileDesc << ")." << endl;
|
|---|
| 375 |
|
|---|
| 376 | // Close device on exit
|
|---|
| 377 | if (fcntl(fFileDesc, F_SETFD, FD_CLOEXEC)<0)
|
|---|
| 378 | {
|
|---|
| 379 | gLog << err << "ERROR - Call to fnctl (F_SETFD, FD_CLOEXEC) failed." << endl;
|
|---|
| 380 | return kFALSE;
|
|---|
| 381 | }
|
|---|
| 382 |
|
|---|
| 383 | gLog << warn << "Setting video standard to PAL-N." << endl;
|
|---|
| 384 |
|
|---|
| 385 | fVideoStandard = V4L2_STD_PAL_N;
|
|---|
| 386 | if (Ioctl(VIDIOC_S_STD, &fVideoStandard)==-1)
|
|---|
| 387 | {
|
|---|
| 388 | gLog << err << "ERROR - Could not set video standard to PAL-N." << endl;
|
|---|
| 389 | return kFALSE;
|
|---|
| 390 | }
|
|---|
| 391 |
|
|---|
| 392 | if (!EnumerateControls())
|
|---|
| 393 | {
|
|---|
| 394 | gLog << err << "ERROR - Could not enumerate controls." << endl;
|
|---|
| 395 | return kFALSE;
|
|---|
| 396 | }
|
|---|
| 397 |
|
|---|
| 398 | if (!ResetControls())
|
|---|
| 399 | {
|
|---|
| 400 | gLog << err << "ERROR - Could not reset controls to default." << endl;
|
|---|
| 401 | return kFALSE;
|
|---|
| 402 | }
|
|---|
| 403 |
|
|---|
| 404 | if (!GetCapabilities())
|
|---|
| 405 | {
|
|---|
| 406 | gLog << err << "ERROR - Getting device capabilities failed." << endl;
|
|---|
| 407 | return kFALSE;
|
|---|
| 408 | }
|
|---|
| 409 |
|
|---|
| 410 | if (!SetChannel(channel))
|
|---|
| 411 | return kFALSE;
|
|---|
| 412 |
|
|---|
| 413 | if (!GetProperties())
|
|---|
| 414 | {
|
|---|
| 415 | gLog << err << "ERROR - Couldn't get picture properties." << endl;
|
|---|
| 416 | return kFALSE;
|
|---|
| 417 | }
|
|---|
| 418 | /*
|
|---|
| 419 | if (HasTuner())
|
|---|
| 420 | {
|
|---|
| 421 | if (!GetTunerAbilities())
|
|---|
| 422 | {
|
|---|
| 423 | gLog << err << "ERROR - Couldn't get tuner abilities." << endl;
|
|---|
| 424 | return kFALSE;
|
|---|
| 425 | }
|
|---|
| 426 | }
|
|---|
| 427 | */
|
|---|
| 428 | // get mmap grab buffer info
|
|---|
| 429 | if (Ioctl(VIDIOCGMBUF, &fBuffer)==-1)
|
|---|
| 430 | {
|
|---|
| 431 | gLog << err << "ERROR - Couldn't get info about memory map buffer." << endl;
|
|---|
| 432 | return kFALSE;
|
|---|
| 433 | }
|
|---|
| 434 |
|
|---|
| 435 | // map file (device) into memory
|
|---|
| 436 | fMapBuffer = (unsigned char*)mmap(0, fBuffer.size, PROT_READ|PROT_WRITE, MAP_SHARED, fFileDesc, 0);
|
|---|
| 437 | if (fMapBuffer == (void*)-1)
|
|---|
| 438 | {
|
|---|
| 439 | gLog << err << "ERROR - Couldn't map device buffer into memory." << endl;
|
|---|
| 440 | fMapBuffer = 0;
|
|---|
| 441 | return kFALSE;
|
|---|
| 442 | }
|
|---|
| 443 |
|
|---|
| 444 | Print();
|
|---|
| 445 |
|
|---|
| 446 | return kTRUE;
|
|---|
| 447 | }
|
|---|
| 448 |
|
|---|
| 449 | // -------------------------------------------------------------
|
|---|
| 450 | //
|
|---|
| 451 | // Close device. Free the shared memory
|
|---|
| 452 | //
|
|---|
| 453 | Int_t MVideo::Close()
|
|---|
| 454 | {
|
|---|
| 455 | // if (!IsOpen())
|
|---|
| 456 | // return kTRUE;
|
|---|
| 457 |
|
|---|
| 458 | Bool_t rc = kTRUE;
|
|---|
| 459 |
|
|---|
| 460 | gLog << all << "Closing " << fPath << " (" << fFileDesc << ")... " << flush;
|
|---|
| 461 | if (fFileDesc != -1)
|
|---|
| 462 | {
|
|---|
| 463 | if (close(fFileDesc)<0)
|
|---|
| 464 | {
|
|---|
| 465 | gLog << err << "ERROR!" << endl;
|
|---|
| 466 | rc = kFALSE;
|
|---|
| 467 | }
|
|---|
| 468 | fFileDesc = -1;
|
|---|
| 469 | }
|
|---|
| 470 | gLog << "done." << endl;
|
|---|
| 471 |
|
|---|
| 472 | // unmap device memory
|
|---|
| 473 | if (fMapBuffer)
|
|---|
| 474 | munmap(fMapBuffer, fBuffer.size);
|
|---|
| 475 |
|
|---|
| 476 | Reset();
|
|---|
| 477 |
|
|---|
| 478 | return rc;
|
|---|
| 479 | }
|
|---|
| 480 |
|
|---|
| 481 | // -------------------------------------------------------------
|
|---|
| 482 | //
|
|---|
| 483 | // Instruct hardware to start capture into framebuffer frame
|
|---|
| 484 | //
|
|---|
| 485 | Bool_t MVideo::CaptureStart(unsigned int frame) const
|
|---|
| 486 | {
|
|---|
| 487 | frame %= fBuffer.frames;
|
|---|
| 488 |
|
|---|
| 489 | struct video_mmap gb =
|
|---|
| 490 | {
|
|---|
| 491 | frame, // frame
|
|---|
| 492 | fCaps.maxheight, fCaps.maxwidth, // height, width
|
|---|
| 493 | VIDEO_PALETTE_RGB24 // palette
|
|---|
| 494 | };
|
|---|
| 495 |
|
|---|
| 496 | #ifdef DEBUG
|
|---|
| 497 | gLog << dbg << "CapturStart(" << frame << ")" << endl;
|
|---|
| 498 | #endif
|
|---|
| 499 |
|
|---|
| 500 | //
|
|---|
| 501 | // capture frame
|
|---|
| 502 | //
|
|---|
| 503 | if (Ioctl(VIDIOCMCAPTURE, &gb) != -1)
|
|---|
| 504 | return kTRUE;
|
|---|
| 505 |
|
|---|
| 506 | // if (errno == EAGAIN)
|
|---|
| 507 | gLog << err;
|
|---|
| 508 | gLog << "ERROR - Couldn't start capturing frame " << frame << "." << endl;
|
|---|
| 509 | gLog << " Maybe your card doesn't support VIDEO_PALETTE_RGB24." << endl;
|
|---|
| 510 |
|
|---|
| 511 | return kFALSE;
|
|---|
| 512 | }
|
|---|
| 513 |
|
|---|
| 514 | // -------------------------------------------------------------
|
|---|
| 515 | //
|
|---|
| 516 | // Wait until hardware has finished capture into framebuffer frame
|
|---|
| 517 | //
|
|---|
| 518 | Int_t MVideo::CaptureWait(unsigned int frame, unsigned char **ptr) const
|
|---|
| 519 | {
|
|---|
| 520 | frame %= fBuffer.frames;
|
|---|
| 521 |
|
|---|
| 522 | if (ptr)
|
|---|
| 523 | *ptr = NULL;
|
|---|
| 524 |
|
|---|
| 525 | const int SYNC_TIMEOUT = 1;
|
|---|
| 526 |
|
|---|
| 527 | #ifdef DEBUG
|
|---|
| 528 | gLog << dbg << "CaptureWait(" << frame << ")" << endl;
|
|---|
| 529 | #endif
|
|---|
| 530 |
|
|---|
| 531 | alarm(SYNC_TIMEOUT);
|
|---|
| 532 | const Int_t rc = Ioctl(VIDIOCSYNC, &frame, false);
|
|---|
| 533 | if (rc==-4)
|
|---|
| 534 | {
|
|---|
| 535 | //cout << "ERROR - Waiting for frame " << frame << " timed out." << endl;
|
|---|
| 536 | return kSKIP;
|
|---|
| 537 | }
|
|---|
| 538 | alarm(0);
|
|---|
| 539 |
|
|---|
| 540 | if (rc==-1)
|
|---|
| 541 | {
|
|---|
| 542 | gLog << err << "ERROR - Waiting for " << frame << " frame failed." << endl;
|
|---|
| 543 | return kFALSE;
|
|---|
| 544 | }
|
|---|
| 545 |
|
|---|
| 546 | if (ptr)
|
|---|
| 547 | *ptr = fMapBuffer+fBuffer.offsets[frame];
|
|---|
| 548 |
|
|---|
| 549 | return kTRUE;
|
|---|
| 550 | }
|
|---|
| 551 |
|
|---|
| 552 | // -------------------------------------------------------------
|
|---|
| 553 | //
|
|---|
| 554 | // Change the channel of a priviously opened device
|
|---|
| 555 | //
|
|---|
| 556 | Int_t MVideo::SetChannel(Int_t chan)
|
|---|
| 557 | {
|
|---|
| 558 | if (fChannel.channel==chan)
|
|---|
| 559 | return kSKIP;
|
|---|
| 560 |
|
|---|
| 561 | if (chan<0 || chan>=fCaps.channels)
|
|---|
| 562 | {
|
|---|
| 563 | gLog << err << "ERROR - Set channel " << chan << " out of range." << endl;
|
|---|
| 564 | return kFALSE;
|
|---|
| 565 | }
|
|---|
| 566 |
|
|---|
| 567 | // Switch to channel
|
|---|
| 568 | struct video_channel ch = { chan, "", 0, 0, 0, 0 };
|
|---|
| 569 | if (Ioctl(VIDIOCSCHAN, &ch)==-1)
|
|---|
| 570 | {
|
|---|
| 571 | gLog << err << "ERROR - Couldn't switch to channel " << chan << "." << endl;
|
|---|
| 572 | gLog << " You might need a bttv version > 0.5.13" << endl;
|
|---|
| 573 | return kFALSE;
|
|---|
| 574 | }
|
|---|
| 575 |
|
|---|
| 576 | // Get information about channel
|
|---|
| 577 | if (Ioctl(VIDIOCGCHAN, &ch)==-1)
|
|---|
| 578 | {
|
|---|
| 579 | gLog << err << "ERROR - Getting information for channel " << chan << " failed." << endl;
|
|---|
| 580 | return kFALSE;
|
|---|
| 581 | }
|
|---|
| 582 |
|
|---|
| 583 | memcpy(&fChannel, &ch, sizeof(fChannel));
|
|---|
| 584 |
|
|---|
| 585 | gLog << all << "Switched to channel " << chan << endl;
|
|---|
| 586 |
|
|---|
| 587 | return kTRUE;
|
|---|
| 588 | }
|
|---|
| 589 |
|
|---|
| 590 | // -------------------------------------------------------------
|
|---|
| 591 | //
|
|---|
| 592 | // Has the device capture capabilities?
|
|---|
| 593 | //
|
|---|
| 594 | Bool_t MVideo::CanCapture() const
|
|---|
| 595 | {
|
|---|
| 596 | return fCaps.type&VID_TYPE_CAPTURE;
|
|---|
| 597 | }
|
|---|
| 598 |
|
|---|
| 599 | // -------------------------------------------------------------
|
|---|
| 600 | //
|
|---|
| 601 | // Has a tuner
|
|---|
| 602 | //
|
|---|
| 603 | Bool_t MVideo::HasTuner() const
|
|---|
| 604 | {
|
|---|
| 605 | return fCaps.type&VID_TYPE_TUNER;
|
|---|
| 606 | }
|
|---|
| 607 |
|
|---|
| 608 | // -------------------------------------------------------------
|
|---|
| 609 | //
|
|---|
| 610 | // Returns the number of frame buffers which can be used
|
|---|
| 611 | //
|
|---|
| 612 | Int_t MVideo::GetNumBuffers() const
|
|---|
| 613 | {
|
|---|
| 614 | return fBuffer.frames;
|
|---|
| 615 | }
|
|---|
| 616 |
|
|---|
| 617 | // -------------------------------------------------------------
|
|---|
| 618 | //
|
|---|
| 619 | // Maximum width of the frame which can be captured
|
|---|
| 620 | //
|
|---|
| 621 | Int_t MVideo::GetWidth() const
|
|---|
| 622 | {
|
|---|
| 623 | return fCaps.maxwidth;
|
|---|
| 624 | }
|
|---|
| 625 |
|
|---|
| 626 | // -------------------------------------------------------------
|
|---|
| 627 | //
|
|---|
| 628 | // Maximum height of the frame which can be captured
|
|---|
| 629 | //
|
|---|
| 630 | Int_t MVideo::GetHeight() const
|
|---|
| 631 | {
|
|---|
| 632 | return fCaps.maxheight;
|
|---|
| 633 | }
|
|---|
| 634 |
|
|---|
| 635 | // -------------------------------------------------------------
|
|---|
| 636 | //
|
|---|
| 637 | // Return the device type as string
|
|---|
| 638 | //
|
|---|
| 639 | TString MVideo::GetDevType(int type) const
|
|---|
| 640 | {
|
|---|
| 641 | TString rc;
|
|---|
| 642 | if (CanCapture())
|
|---|
| 643 | rc += " capture";
|
|---|
| 644 | if (HasTuner())
|
|---|
| 645 | rc += " tuner";
|
|---|
| 646 | if (type&VID_TYPE_TELETEXT)
|
|---|
| 647 | rc += " teletext";
|
|---|
| 648 | if (type&VID_TYPE_OVERLAY)
|
|---|
| 649 | rc += " overlay";
|
|---|
| 650 | if (type&VID_TYPE_CHROMAKEY)
|
|---|
| 651 | rc += " chromakey";
|
|---|
| 652 | if (type&VID_TYPE_CLIPPING)
|
|---|
| 653 | rc += " clipping";
|
|---|
| 654 | if (type&VID_TYPE_FRAMERAM)
|
|---|
| 655 | rc += " frameram";
|
|---|
| 656 | if (type&VID_TYPE_SCALES)
|
|---|
| 657 | rc += " scales";
|
|---|
| 658 | if (type&VID_TYPE_MONOCHROME)
|
|---|
| 659 | rc += " monochrom";
|
|---|
| 660 | if (type&VID_TYPE_SUBCAPTURE)
|
|---|
| 661 | rc += " subcapature";
|
|---|
| 662 | return rc;
|
|---|
| 663 | }
|
|---|
| 664 |
|
|---|
| 665 | TString MVideo::GetTunerFlags(Int_t flags) const
|
|---|
| 666 | {
|
|---|
| 667 | TString rc;
|
|---|
| 668 | if (flags&VIDEO_TUNER_PAL)
|
|---|
| 669 | rc += " PAL";
|
|---|
| 670 | if (flags&VIDEO_TUNER_NTSC)
|
|---|
| 671 | rc += " NTSC";
|
|---|
| 672 | if (flags&VIDEO_TUNER_SECAM)
|
|---|
| 673 | rc += " SECAM";
|
|---|
| 674 | if (flags&VIDEO_TUNER_LOW)
|
|---|
| 675 | rc += " kHz";
|
|---|
| 676 | if (flags&VIDEO_TUNER_NORM)
|
|---|
| 677 | rc += " CanSetNorm";
|
|---|
| 678 | if (flags&VIDEO_TUNER_STEREO_ON)
|
|---|
| 679 | rc += " StereoOn";
|
|---|
| 680 | return rc;
|
|---|
| 681 | }
|
|---|
| 682 |
|
|---|
| 683 | TString MVideo::GetTunerMode(Int_t mode) const
|
|---|
| 684 | {
|
|---|
| 685 | switch (mode)
|
|---|
| 686 | {
|
|---|
| 687 | case VIDEO_MODE_PAL:
|
|---|
| 688 | return "PAL";
|
|---|
| 689 | case VIDEO_MODE_NTSC:
|
|---|
| 690 | return "NTSC";
|
|---|
| 691 | case VIDEO_MODE_SECAM:
|
|---|
| 692 | return "SECAM";
|
|---|
| 693 | case VIDEO_MODE_AUTO:
|
|---|
| 694 | return "AUTO";
|
|---|
| 695 | }
|
|---|
| 696 | return "undefined";
|
|---|
| 697 | }
|
|---|
| 698 |
|
|---|
| 699 | // -------------------------------------------------------------
|
|---|
| 700 | //
|
|---|
| 701 | // Return the channel flags as string
|
|---|
| 702 | //
|
|---|
| 703 | TString MVideo::GetChannelFlags(Int_t flags) const
|
|---|
| 704 | {
|
|---|
| 705 | TString rc = "video";
|
|---|
| 706 | if (flags&VIDEO_VC_TUNER)
|
|---|
| 707 | rc += " tuner";
|
|---|
| 708 | if (flags&VIDEO_VC_AUDIO)
|
|---|
| 709 | rc += " audio";
|
|---|
| 710 | // if (flags&VIDEO_VC_NORM)
|
|---|
| 711 | // rc += " normsetting";
|
|---|
| 712 | return rc;
|
|---|
| 713 | }
|
|---|
| 714 |
|
|---|
| 715 | // -------------------------------------------------------------
|
|---|
| 716 | //
|
|---|
| 717 | // Return the channel type as string
|
|---|
| 718 | //
|
|---|
| 719 | TString MVideo::GetChannelType(Int_t type) const
|
|---|
| 720 | {
|
|---|
| 721 | if (type&VIDEO_TYPE_TV)
|
|---|
| 722 | return "TV";
|
|---|
| 723 | if (type&VIDEO_TYPE_CAMERA)
|
|---|
| 724 | return "Camera";
|
|---|
| 725 | return "unknown";
|
|---|
| 726 | }
|
|---|
| 727 |
|
|---|
| 728 | // -------------------------------------------------------------
|
|---|
| 729 | //
|
|---|
| 730 | // Return the palette pal as string
|
|---|
| 731 | //
|
|---|
| 732 | TString MVideo::GetPalette(Int_t pal) const
|
|---|
| 733 | {
|
|---|
| 734 | switch (pal)
|
|---|
| 735 | {
|
|---|
| 736 | case VIDEO_PALETTE_GREY:
|
|---|
| 737 | return "VIDEO_PALETTE_GREY: Linear intensity grey scale";
|
|---|
| 738 | case VIDEO_PALETTE_HI240:
|
|---|
| 739 | return "VIDEO_PALETTE_HI240: BT848 8-bit color cube";
|
|---|
| 740 | case VIDEO_PALETTE_RGB565:
|
|---|
| 741 | return "VIDEO_PALETTE_RGB565: RGB565 packed into 16-bit words";
|
|---|
| 742 | case VIDEO_PALETTE_RGB555:
|
|---|
| 743 | return "VIDEO_PALETTE_RGB555: RGB555 packed into 16-bit words, top bit undefined";
|
|---|
| 744 | case VIDEO_PALETTE_RGB24:
|
|---|
| 745 | return "VIDEO_PALETTE_RGB24: RGB888 packed into 24-bit words";
|
|---|
| 746 | case VIDEO_PALETTE_RGB32:
|
|---|
| 747 | return "VIDEO_PALETTE_RGB32: RGB888 packed into the low three bytes of 32-bit words. Top bits undefined.";
|
|---|
| 748 | case VIDEO_PALETTE_YUV422:
|
|---|
| 749 | return "VIDEO_PALETTE_YUV422: Video style YUV422 - 8-bit packed, 4-bit Y, 2-bits U, 2-bits V";
|
|---|
| 750 | case VIDEO_PALETTE_YUYV:
|
|---|
| 751 | return "VIDEO_PALETTE_YUYV: YUYV";
|
|---|
| 752 | case VIDEO_PALETTE_UYVY:
|
|---|
| 753 | return "VIDEO_PALETTE_UYVY: UYVY";
|
|---|
| 754 | case VIDEO_PALETTE_YUV420:
|
|---|
| 755 | return "VIDEO_PALETTE_YUV420: YUV420";
|
|---|
| 756 | case VIDEO_PALETTE_YUV411:
|
|---|
| 757 | return "VIDEO_PALETTE_YUV411: YUV411";
|
|---|
| 758 | case VIDEO_PALETTE_RAW:
|
|---|
| 759 | return "VIDEO_PALETTE_RAW: Raw capture (Bt848)";
|
|---|
| 760 | case VIDEO_PALETTE_YUV422P:
|
|---|
| 761 | return "VIDEO_PALETTE_YUV422P: YUV 4:2:2 planar";
|
|---|
| 762 | case VIDEO_PALETTE_YUV411P:
|
|---|
| 763 | return "VIDEO_PALETTE_YUV411P: YUV 4:1:1 planar";
|
|---|
| 764 | }
|
|---|
| 765 | return "unknown";
|
|---|
| 766 | }
|
|---|
| 767 |
|
|---|
| 768 | // -------------------------------------------------------------
|
|---|
| 769 | //
|
|---|
| 770 | // Print informations about the device, the capabilities, the
|
|---|
| 771 | // channel and all available information
|
|---|
| 772 | //
|
|---|
| 773 | void MVideo::Print() const
|
|---|
| 774 | {
|
|---|
| 775 | gLog << all << dec;
|
|---|
| 776 |
|
|---|
| 777 | gLog << "Device " << fPath << " " << (IsOpen()?"open":"closed") << "." << endl;
|
|---|
| 778 |
|
|---|
| 779 | if (!IsOpen())
|
|---|
| 780 | return;
|
|---|
| 781 |
|
|---|
| 782 | gLog << " - Name: " << fCaps.name << endl;
|
|---|
| 783 | gLog << " - DevType: " << GetDevType(fCaps.type) << endl;
|
|---|
| 784 | gLog << " - Channels: " << fCaps.channels << endl;
|
|---|
| 785 | gLog << " - Audios: " << fCaps.audios << endl;
|
|---|
| 786 | gLog << " - Size: ";
|
|---|
| 787 | gLog << fCaps.minwidth << "x" << fCaps.minheight << " to ";
|
|---|
| 788 | gLog << fCaps.maxwidth << "x" << fCaps.maxheight << endl;
|
|---|
| 789 | gLog << endl;
|
|---|
| 790 | if (fChannel.channel>=0)
|
|---|
| 791 | {
|
|---|
| 792 | gLog << " - Channel: " << fChannel.channel << " (" << fChannel.name << ")" << endl;
|
|---|
| 793 | gLog << " - IsA: " << GetChannelType(fChannel.type) << " with " << GetChannelFlags(fChannel.flags) << " (" << fChannel.flags << ")" << endl;
|
|---|
| 794 | //if (fChannel.flags&VIDEO_VC_NORM)
|
|---|
| 795 | gLog << " - Norm: " << fChannel.norm << endl;
|
|---|
| 796 | gLog << endl;
|
|---|
| 797 | }
|
|---|
| 798 |
|
|---|
| 799 | if (fAbil.tuner>=0)
|
|---|
| 800 | {
|
|---|
| 801 | gLog << " - Tuner: " << fAbil.tuner << endl;
|
|---|
| 802 | gLog << " - Name: " << fAbil.name << endl;
|
|---|
| 803 | gLog << " - Tuner Range: " << fAbil.rangelow << " - " << fAbil.rangehigh << endl;
|
|---|
| 804 | gLog << " - Tuner flags: " << GetTunerFlags(fAbil.flags) << " (" << fAbil.flags << ")" << endl;
|
|---|
| 805 | gLog << " - Tuner mode: " << GetTunerMode(fAbil.mode) << " (" << fAbil.mode << ")" <<endl;
|
|---|
| 806 | gLog << " - Signal Strength: " << fAbil.signal << endl;
|
|---|
| 807 | }
|
|---|
| 808 |
|
|---|
| 809 | gLog << " - Brightness: " << fProp.brightness << endl;
|
|---|
| 810 | gLog << " - Hue: " << fProp.hue << endl;
|
|---|
| 811 | gLog << " - Color: " << fProp.colour << endl;
|
|---|
| 812 | gLog << " - Contrast: " << fProp.contrast << endl;
|
|---|
| 813 | gLog << " - Whiteness: " << fProp.whiteness << endl;
|
|---|
| 814 | gLog << " - Depth: " << fProp.depth << endl;
|
|---|
| 815 | gLog << " - Palette: " << GetPalette(fProp.palette) << " (" << fProp.palette << ")" << endl;
|
|---|
| 816 | gLog << endl;
|
|---|
| 817 |
|
|---|
| 818 | gLog << " - BufferSize: 0x" << hex << fBuffer.size << " (" << dec << fBuffer.frames << " frames)" << endl;
|
|---|
| 819 | gLog << " - Offsets: " << hex;
|
|---|
| 820 | for (int i=0; i<fBuffer.frames; i++)
|
|---|
| 821 | gLog << " 0x" << fBuffer.offsets[i];
|
|---|
| 822 | gLog << dec << endl;
|
|---|
| 823 |
|
|---|
| 824 | gLog << inf2 << "Controls:" << endl;
|
|---|
| 825 | fControls.Print();
|
|---|
| 826 | }
|
|---|
| 827 |
|
|---|
| 828 | /*
|
|---|
| 829 | void MVideo::SetPicPar(int bright, int hue, int contrast)
|
|---|
| 830 | {
|
|---|
| 831 | struct video_picture pict;
|
|---|
| 832 |
|
|---|
| 833 | Ioctl(VIDIOCGPICT, &pict); // get
|
|---|
| 834 |
|
|---|
| 835 | if (contrast != -1)
|
|---|
| 836 | pict.contrast = contrast;
|
|---|
| 837 |
|
|---|
| 838 | if (bright != -1)
|
|---|
| 839 | pict.brightness = bright;
|
|---|
| 840 |
|
|---|
| 841 | if (hue != -1)
|
|---|
| 842 | pict.hue = hue;
|
|---|
| 843 |
|
|---|
| 844 | Ioctl(VIDIOCSPICT, &pict); //set
|
|---|
| 845 | }
|
|---|
| 846 |
|
|---|
| 847 | void MVideo::GetPicPar(int *bright, int *hue, int *contrast)
|
|---|
| 848 | {
|
|---|
| 849 | struct video_picture pict;
|
|---|
| 850 |
|
|---|
| 851 | Ioctl(VIDIOCGPICT, &pict); // get
|
|---|
| 852 |
|
|---|
| 853 | *contrast = pict.contrast;
|
|---|
| 854 | *bright = pict.brightness;
|
|---|
| 855 | *hue = pict.hue;
|
|---|
| 856 | }
|
|---|
| 857 | */
|
|---|