| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | #define _WIN32_DCOM |
|---|
| 122 | #include <stdio.h> |
|---|
| 123 | #include <assert.h> |
|---|
| 124 | #include <stdlib.h> |
|---|
| 125 | #include "config.h" |
|---|
| 126 | #include <windows.h> |
|---|
| 127 | |
|---|
| 128 | #include "debug.h" |
|---|
| 129 | #include "grabber.h" |
|---|
| 130 | #include "device-input.h" |
|---|
| 131 | #include "module.h" |
|---|
| 132 | #include "yuv_convert.h" |
|---|
| 133 | |
|---|
| 134 | #include "grabber-win32DS.h" |
|---|
| 135 | |
|---|
| 136 | #ifndef VIDE0_FOR_WINDOWS |
|---|
| 137 | static DirectShowScanner findDirectShowDevices; |
|---|
| 138 | #endif |
|---|
| 139 | |
|---|
| 140 | static const GUID MEDIASUBTYPE_I420 = |
|---|
| 141 | {0x30323449, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}}; |
|---|
| 142 | |
|---|
| 143 | #define NAMEBUF_LEN 200 |
|---|
| 144 | |
|---|
| 145 | IBaseFilter *pCaptureFilter[NUM_DEVS]; |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | STDMETHODIMP Callback::SampleCB(double sampleTime, IMediaSample *pSample) { |
|---|
| 151 | return E_NOTIMPL; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | STDMETHODIMP Callback::BufferCB(double sampleTime, BYTE *pBuffer, long bufferLen) { |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | WaitForSingleObject(grabber->cb_mutex_, INFINITE); |
|---|
| 159 | grabber->capture(pBuffer, bufferLen); |
|---|
| 160 | ReleaseMutex(grabber->cb_mutex_); |
|---|
| 161 | return S_OK; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | STDMETHODIMP Callback::QueryInterface(REFIID riid, void **ppvObject) { |
|---|
| 167 | if (NULL == ppvObject) |
|---|
| 168 | return E_POINTER; |
|---|
| 169 | if (riid == __uuidof(IUnknown)) |
|---|
| 170 | *ppvObject = static_cast<IUnknown*>(this); |
|---|
| 171 | else if (riid == __uuidof(ISampleGrabberCB)) |
|---|
| 172 | *ppvObject = static_cast<ISampleGrabberCB*>(this); |
|---|
| 173 | else |
|---|
| 174 | return E_NOTIMPL; |
|---|
| 175 | AddRef(); |
|---|
| 176 | return S_OK; |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | DirectShowGrabber::DirectShowGrabber(IBaseFilter *filt, const char * cformat, const char *nick) { |
|---|
| 183 | HRESULT hr; |
|---|
| 184 | WCHAR nameBufW[NAMEBUF_LEN]; |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | debug_msg("new DirectShowGrabber()\n"); |
|---|
| 192 | svideoPort = -1; |
|---|
| 193 | compositePort = -1; |
|---|
| 194 | decimate_ = 2; |
|---|
| 195 | cb_mutex_=0; |
|---|
| 196 | crossbar_ = crossbarCursor_ = NULL; |
|---|
| 197 | pNullBaseFilter_=0; |
|---|
| 198 | pSampleGrabber_=0; |
|---|
| 199 | pGrabberBaseFilter_=0; |
|---|
| 200 | pMediaControl_=0; |
|---|
| 201 | pGraph_=0; |
|---|
| 202 | pBuild_=0; |
|---|
| 203 | dwRegister_=0; |
|---|
| 204 | pCaptureFilter_ = filt; |
|---|
| 205 | pXBar_ = NULL; |
|---|
| 206 | pFilter_ = NULL; |
|---|
| 207 | capturing_=0; |
|---|
| 208 | max_fps_ = 30; |
|---|
| 209 | |
|---|
| 210 | if(!strcmp(cformat, "420")) |
|---|
| 211 | cformat_ = CF_420; |
|---|
| 212 | if(!strcmp(cformat, "422")) |
|---|
| 213 | cformat_ = CF_422; |
|---|
| 214 | if(!strcmp(cformat, "cif")) |
|---|
| 215 | cformat_ = CF_CIF; |
|---|
| 216 | |
|---|
| 217 | have_I420_ = false; |
|---|
| 218 | have_UYVY_ = false; |
|---|
| 219 | have_YUY2_ = false; |
|---|
| 220 | |
|---|
| 221 | setport("external-in"); |
|---|
| 222 | |
|---|
| 223 | basewidth_ = NTSC_BASE_WIDTH; |
|---|
| 224 | baseheight_ = NTSC_BASE_HEIGHT; |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | cb_mutex_ = CreateMutex(NULL, FALSE, NULL); |
|---|
| 238 | |
|---|
| 239 | callback_ = new Callback(); |
|---|
| 240 | callback_->grabber = this; |
|---|
| 241 | debug_msg("DirectShowGrabber::DirectShowGrabber(): callback created, grabber set\n"); |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER, |
|---|
| 246 | IID_ICaptureGraphBuilder2, (void **)&pBuild_); |
|---|
| 247 | |
|---|
| 248 | if (FAILED(hr)) { |
|---|
| 249 | Grabber::status_=-1; |
|---|
| 250 | return; |
|---|
| 251 | } |
|---|
| 252 | debug_msg("DirectShowGrabber::DirectShowGrabber(): graph builder interface acquired\n"); |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, |
|---|
| 256 | IID_IGraphBuilder, (void **)&pGraph_); |
|---|
| 257 | if (FAILED(hr)) { |
|---|
| 258 | Grabber::status_=-1; |
|---|
| 259 | return; |
|---|
| 260 | } |
|---|
| 261 | debug_msg("DirectShowGrabber::DirectShowGrabber(): graph instance acquired\n"); |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | hr = pBuild_->SetFiltergraph(pGraph_); |
|---|
| 265 | |
|---|
| 266 | if (FAILED(hr)) { |
|---|
| 267 | Grabber::status_=-1; |
|---|
| 268 | return; |
|---|
| 269 | } |
|---|
| 270 | debug_msg("DirectShowGrabber::DirectShowGrabber(): graph associated with builder\n"); |
|---|
| 271 | |
|---|
| 272 | nameBufW[0] = '\0'; |
|---|
| 273 | hr = MultiByteToWideChar(CP_ACP, 0, nick, -1, nameBufW, NAMEBUF_LEN); |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | hr = pGraph_->AddFilter(pCaptureFilter_, nameBufW); |
|---|
| 278 | |
|---|
| 279 | debug_msg("DirectShowGrabber::DirectShowGrabber(): capture filter added to graph: %d\n", hr); |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | hr = CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER, |
|---|
| 291 | IID_IBaseFilter, (LPVOID *)&pGrabberBaseFilter_); |
|---|
| 292 | |
|---|
| 293 | debug_msg("DirectShowGrabber::DirectShowGrabber(): grabber base filter instance acquired: %d\n", hr); |
|---|
| 294 | |
|---|
| 295 | hr = pGrabberBaseFilter_->QueryInterface(IID_ISampleGrabber, (void**)&pSampleGrabber_); |
|---|
| 296 | |
|---|
| 297 | debug_msg("DirectShowGrabber::DirectShowGrabber(): Sample Grabber interface acquired\n"); |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | hr = pSampleGrabber_->SetOneShot(FALSE); |
|---|
| 301 | hr = pSampleGrabber_->SetCallback(callback_, 1); |
|---|
| 302 | hr = pGraph_->AddFilter(pGrabberBaseFilter_,L"VicSampleGrabber"); |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | hr = CoCreateInstance(CLSID_NullRenderer, NULL, CLSCTX_INPROC_SERVER, |
|---|
| 306 | IID_IBaseFilter, (LPVOID *)&pNullBaseFilter_); |
|---|
| 307 | |
|---|
| 308 | if (FAILED(hr)) { |
|---|
| 309 | Grabber::status_=-1; |
|---|
| 310 | return; |
|---|
| 311 | } |
|---|
| 312 | debug_msg("DirectShowGrabber::DirectShowGrabber(): Null Renderer interface acquired\n"); |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | hr = pGraph_->AddFilter(pNullBaseFilter_,L"VicNullRenderer"); |
|---|
| 316 | if (FAILED(hr)) { |
|---|
| 317 | Grabber::status_=-1; |
|---|
| 318 | return; |
|---|
| 319 | } |
|---|
| 320 | debug_msg("DirectShowGrabber::DirectShowGrabber(): Null Renderer added to graph\n"); |
|---|
| 321 | |
|---|
| 322 | if (!getCaptureCapabilities()) { |
|---|
| 323 | Grabber::status_=-1; |
|---|
| 324 | return; |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | setCaptureOutputFormat(); |
|---|
| 328 | |
|---|
| 329 | if (findCrossbar(pCaptureFilter_)) { |
|---|
| 330 | routeCrossbar(); |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | ZeroMemory(&mt_, sizeof(AM_MEDIA_TYPE)); |
|---|
| 334 | mt_.majortype = MEDIATYPE_Video; |
|---|
| 335 | |
|---|
| 336 | if (have_I420_) { |
|---|
| 337 | mt_.subtype = MEDIASUBTYPE_I420; |
|---|
| 338 | } else if (have_UYVY_) { |
|---|
| 339 | mt_.subtype = MEDIASUBTYPE_UYVY; |
|---|
| 340 | } else if (have_YUY2_) { |
|---|
| 341 | mt_.subtype = MEDIASUBTYPE_YUY2; |
|---|
| 342 | } |
|---|
| 343 | |
|---|
| 344 | hr = pSampleGrabber_->SetMediaType(&mt_); |
|---|
| 345 | |
|---|
| 346 | |
|---|
| 347 | |
|---|
| 348 | hr = pGraph_->QueryInterface(IID_IMediaControl, (void **)&pMediaControl_); |
|---|
| 349 | |
|---|
| 350 | if (FAILED(hr)) { |
|---|
| 351 | Grabber::status_=-1; |
|---|
| 352 | return; |
|---|
| 353 | } |
|---|
| 354 | debug_msg("DirectShowGrabber::DirectShowGrabber(): graph media control interface acquired\n"); |
|---|
| 355 | |
|---|
| 356 | IMoniker * pMoniker = NULL; |
|---|
| 357 | IRunningObjectTable *pROT = NULL; |
|---|
| 358 | |
|---|
| 359 | |
|---|
| 360 | |
|---|
| 361 | |
|---|
| 362 | if (SUCCEEDED(GetRunningObjectTable(0, &pROT))) { |
|---|
| 363 | const size_t STRING_LENGTH = 256; |
|---|
| 364 | |
|---|
| 365 | WCHAR wsz[STRING_LENGTH]; |
|---|
| 366 | swprintf(wsz, STRING_LENGTH, L"FilterGraph %08x pid %08x", (DWORD_PTR)pGraph_, GetCurrentProcessId()); |
|---|
| 367 | |
|---|
| 368 | HRESULT hr = CreateItemMoniker(L"!", wsz, &pMoniker); |
|---|
| 369 | if (SUCCEEDED(hr)) { |
|---|
| 370 | hr = pROT->Register(ROTFLAGS_REGISTRATIONKEEPSALIVE, pGraph_, pMoniker, &dwRegister_); |
|---|
| 371 | pMoniker->Release(); |
|---|
| 372 | } |
|---|
| 373 | pROT->Release(); |
|---|
| 374 | } |
|---|
| 375 | } |
|---|
| 376 | |
|---|
| 377 | |
|---|
| 378 | |
|---|
| 379 | DirectShowGrabber::~DirectShowGrabber() { |
|---|
| 380 | HRESULT hr; |
|---|
| 381 | |
|---|
| 382 | debug_msg("~DirectShowGrabber()\n"); |
|---|
| 383 | |
|---|
| 384 | |
|---|
| 385 | if (capturing_) hr = pMediaControl_->Stop(); |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | CloseHandle(cb_mutex_); |
|---|
| 389 | |
|---|
| 390 | |
|---|
| 391 | IRunningObjectTable *pROT; |
|---|
| 392 | if (SUCCEEDED(GetRunningObjectTable(0, &pROT))) { |
|---|
| 393 | pROT->Revoke(dwRegister_); |
|---|
| 394 | pROT->Release(); |
|---|
| 395 | } |
|---|
| 396 | |
|---|
| 397 | |
|---|
| 398 | callback_->Release(); |
|---|
| 399 | |
|---|
| 400 | pNullBaseFilter_->Release(); |
|---|
| 401 | pSampleGrabber_->Release(); |
|---|
| 402 | pGrabberBaseFilter_->Release(); |
|---|
| 403 | pMediaControl_->Release(); |
|---|
| 404 | pGraph_->Release(); |
|---|
| 405 | pBuild_->Release(); |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | |
|---|
| 409 | |
|---|
| 410 | bool DirectShowGrabber::findCrossbar(IBaseFilter *pCapF) { |
|---|
| 411 | HRESULT hr; |
|---|
| 412 | |
|---|
| 413 | debug_msg("DirectShowGrabber::FindCrossbar()...\n"); |
|---|
| 414 | |
|---|
| 415 | hr = pBuild_->FindInterface(&LOOK_UPSTREAM_ONLY, NULL, pCapF, IID_IAMCrossbar, |
|---|
| 416 | (void**)&pXBar_); |
|---|
| 417 | |
|---|
| 418 | if ( SUCCEEDED(hr) ) { |
|---|
| 419 | addCrossbar(pXBar_); |
|---|
| 420 | hr = pXBar_->QueryInterface(IID_IBaseFilter, (void**)&pFilter_); |
|---|
| 421 | if ( SUCCEEDED(hr) ) { |
|---|
| 422 | debug_msg("DirectShowGrabber::FindCrossbar()...Found and added\n"); |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | return TRUE; |
|---|
| 426 | } |
|---|
| 427 | } |
|---|
| 428 | return FALSE; |
|---|
| 429 | } |
|---|
| 430 | |
|---|
| 431 | |
|---|
| 432 | |
|---|
| 433 | void DirectShowGrabber::addCrossbar(IAMCrossbar *xbar) { |
|---|
| 434 | Crossbar *pCross; |
|---|
| 435 | |
|---|
| 436 | debug_msg("DirectShowGrabber::addCrossbar()\n"); |
|---|
| 437 | |
|---|
| 438 | pCross = new Crossbar(xbar); |
|---|
| 439 | |
|---|
| 440 | if( crossbar_ == NULL ) { |
|---|
| 441 | crossbar_ = pCross; |
|---|
| 442 | } |
|---|
| 443 | else { |
|---|
| 444 | crossbarCursor_->next = pCross; |
|---|
| 445 | } |
|---|
| 446 | crossbarCursor_ = pCross; |
|---|
| 447 | } |
|---|
| 448 | |
|---|
| 449 | |
|---|
| 450 | |
|---|
| 451 | void DirectShowGrabber::routeCrossbar() { |
|---|
| 452 | HRESULT hr; |
|---|
| 453 | long output = -1; |
|---|
| 454 | long input = -1; |
|---|
| 455 | int videoDecoderPort = -1; |
|---|
| 456 | int port; |
|---|
| 457 | long related; |
|---|
| 458 | long pinType; |
|---|
| 459 | IAMCrossbar *xb; |
|---|
| 460 | |
|---|
| 461 | if( crossbar_ == NULL ) return; |
|---|
| 462 | |
|---|
| 463 | xb = crossbar_->getXBar(); |
|---|
| 464 | |
|---|
| 465 | xb->get_IsRoutedTo(0, &input); |
|---|
| 466 | debug_msg("DirectShowGrabber::routeCrossbar(): pin %d is routed to output pin 0\n", input); |
|---|
| 467 | |
|---|
| 468 | hr = xb->get_PinCounts(&output, &input); |
|---|
| 469 | |
|---|
| 470 | for( int i = 0; i < input; ++i ) { |
|---|
| 471 | xb->get_CrossbarPinInfo(TRUE, i, &related, &pinType); |
|---|
| 472 | if( pinType == PhysConn_Video_SVideo ) { |
|---|
| 473 | svideoPort = i; |
|---|
| 474 | } |
|---|
| 475 | if( pinType == PhysConn_Video_Composite ) { |
|---|
| 476 | compositePort = i; |
|---|
| 477 | } |
|---|
| 478 | } |
|---|
| 479 | for( int i = 0; i < output; ++i ) { |
|---|
| 480 | xb->get_CrossbarPinInfo(FALSE, i, &related, &pinType); |
|---|
| 481 | if( pinType == PhysConn_Video_VideoDecoder ) { |
|---|
| 482 | videoDecoderPort = i; |
|---|
| 483 | break; |
|---|
| 484 | } |
|---|
| 485 | } |
|---|
| 486 | if(strcmp(input_port_, "S-Video")==0){ |
|---|
| 487 | port = svideoPort; |
|---|
| 488 | }else if(strcmp(input_port_, "Composite")==0){ |
|---|
| 489 | port = compositePort; |
|---|
| 490 | }else{ |
|---|
| 491 | port = 0; |
|---|
| 492 | } |
|---|
| 493 | |
|---|
| 494 | if( xb->CanRoute(videoDecoderPort, port) == S_FALSE ) |
|---|
| 495 | debug_msg("DirectShowGrabber::routeCrossbar(): cannot route input pin %d to output pin %d\n", port, videoDecoderPort); |
|---|
| 496 | else { |
|---|
| 497 | debug_msg("DirectShowGrabber::routeCrossbar() routing pin %d to pin %d\n", port, videoDecoderPort); |
|---|
| 498 | hr = xb->Route(videoDecoderPort, port); |
|---|
| 499 | |
|---|
| 500 | } |
|---|
| 501 | |
|---|
| 502 | xb->get_IsRoutedTo(0, &input); |
|---|
| 503 | debug_msg("DirectShowGrabber::routeCrossbar(): pin %d is now routed to output pin 0\n", input); |
|---|
| 504 | } |
|---|
| 505 | |
|---|
| 506 | |
|---|
| 507 | |
|---|
| 508 | void DirectShowGrabber::start() { |
|---|
| 509 | HRESULT hr; |
|---|
| 510 | |
|---|
| 511 | setsize(); |
|---|
| 512 | setCaptureOutputFormat(); |
|---|
| 513 | hr = pBuild_->RenderStream(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video, |
|---|
| 514 | pCaptureFilter_, pGrabberBaseFilter_, pNullBaseFilter_); |
|---|
| 515 | if (SUCCEEDED(hr) ) |
|---|
| 516 | debug_msg("DirectShowGrabber::DirectShowGrabber(): builder render stream\n"); |
|---|
| 517 | else { |
|---|
| 518 | debug_msg("DirectShowGrabber::DirectShowGrabber(): FAILED to builder render stream: %x\n", hr); |
|---|
| 519 | |
|---|
| 520 | |
|---|
| 521 | |
|---|
| 522 | |
|---|
| 523 | } |
|---|
| 524 | |
|---|
| 525 | WaitForSingleObject(cb_mutex_, INFINITE); |
|---|
| 526 | |
|---|
| 527 | capturing_ = 1; |
|---|
| 528 | last_frame_ = NULL; |
|---|
| 529 | |
|---|
| 530 | debug_msg("DirectShowGrabber::start(): starting capture graph...\n"); |
|---|
| 531 | |
|---|
| 532 | |
|---|
| 533 | hr = pMediaControl_->Run(); |
|---|
| 534 | if (SUCCEEDED(hr) ) |
|---|
| 535 | debug_msg("DirectShowGrabber::DirectShowGrabber(): Graph set to Run\n"); |
|---|
| 536 | |
|---|
| 537 | |
|---|
| 538 | |
|---|
| 539 | Grabber::start(); |
|---|
| 540 | ReleaseMutex(cb_mutex_); |
|---|
| 541 | Grabber::timeout(); |
|---|
| 542 | } |
|---|
| 543 | |
|---|
| 544 | |
|---|
| 545 | |
|---|
| 546 | void DirectShowGrabber::stop() { |
|---|
| 547 | HRESULT hr; |
|---|
| 548 | |
|---|
| 549 | debug_msg("DirectShowGrabber::stop() thread=%x\n", GetCurrentThreadId()); |
|---|
| 550 | |
|---|
| 551 | if (capturing_) { |
|---|
| 552 | hr = pMediaControl_->Stop(); |
|---|
| 553 | } |
|---|
| 554 | |
|---|
| 555 | ReleaseMutex(cb_mutex_); |
|---|
| 556 | |
|---|
| 557 | capturing_ = 0; |
|---|
| 558 | last_frame_ = 0; |
|---|
| 559 | |
|---|
| 560 | Grabber::stop(); |
|---|
| 561 | } |
|---|
| 562 | |
|---|
| 563 | |
|---|
| 564 | |
|---|
| 565 | void DirectShowGrabber::fps(int f) { |
|---|
| 566 | if (f <= 0) |
|---|
| 567 | f = 1; |
|---|
| 568 | else if (u_int(f) > max_fps_) |
|---|
| 569 | f = max_fps_; |
|---|
| 570 | |
|---|
| 571 | Grabber::fps(f); |
|---|
| 572 | } |
|---|
| 573 | |
|---|
| 574 | void DirectShowGrabber::setsize() { |
|---|
| 575 | |
|---|
| 576 | if (max_width_ >= D1_BASE_WIDTH){ |
|---|
| 577 | max_width_ = D1_BASE_WIDTH; |
|---|
| 578 | max_height_ = D1_BASE_HEIGHT; |
|---|
| 579 | } |
|---|
| 580 | |
|---|
| 581 | if (decimate_ == 1){ |
|---|
| 582 | width_ = max_width_; |
|---|
| 583 | height_ = max_height_; |
|---|
| 584 | } else { |
|---|
| 585 | width_ = basewidth_ / decimate_; |
|---|
| 586 | height_ = baseheight_ / decimate_; |
|---|
| 587 | } |
|---|
| 588 | |
|---|
| 589 | debug_msg("DirectShowGrabber::setsize: %dx%d\n", width_, height_); |
|---|
| 590 | |
|---|
| 591 | switch (cformat_) { |
|---|
| 592 | case CF_CIF: |
|---|
| 593 | set_size_cif(width_, height_); |
|---|
| 594 | break; |
|---|
| 595 | case CF_420: |
|---|
| 596 | set_size_420(width_, height_); |
|---|
| 597 | break; |
|---|
| 598 | case CF_422: |
|---|
| 599 | set_size_cif(width_, height_); |
|---|
| 600 | break; |
|---|
| 601 | } |
|---|
| 602 | |
|---|
| 603 | allocref(); |
|---|
| 604 | } |
|---|
| 605 | |
|---|
| 606 | |
|---|
| 607 | |
|---|
| 608 | void DirectShowGrabber::capture(BYTE *frameBuf, long bufLen) { |
|---|
| 609 | last_frame_ = frameBuf; |
|---|
| 610 | |
|---|
| 611 | } |
|---|
| 612 | |
|---|
| 613 | |
|---|
| 614 | |
|---|
| 615 | int DirectShowGrabber::grab() { |
|---|
| 616 | int rval; |
|---|
| 617 | |
|---|
| 618 | |
|---|
| 619 | |
|---|
| 620 | |
|---|
| 621 | |
|---|
| 622 | |
|---|
| 623 | |
|---|
| 624 | WaitForSingleObject(cb_mutex_, INFINITE); |
|---|
| 625 | |
|---|
| 626 | if( last_frame_ == NULL || capturing_ == 0 ) { |
|---|
| 627 | ReleaseMutex(cb_mutex_); |
|---|
| 628 | return FALSE; |
|---|
| 629 | } |
|---|
| 630 | switch (cformat_) { |
|---|
| 631 | case CF_420: |
|---|
| 632 | case CF_CIF: |
|---|
| 633 | if (have_I420_) |
|---|
| 634 | planarYUYV420_to_planarYUYV420((char *)frame_, outw_, outh_, (char *)last_frame_, inw_, inh_); |
|---|
| 635 | else if (have_UYVY_) |
|---|
| 636 | packedUYVY422_to_planarYUYV420((char *)frame_, outw_, outh_, (char *)last_frame_, inw_, inh_); |
|---|
| 637 | else if (have_YUY2_) |
|---|
| 638 | packedYUYV422_to_planarYUYV420((char *)frame_, outw_, outh_, (char *)last_frame_, inw_, inh_); |
|---|
| 639 | break; |
|---|
| 640 | |
|---|
| 641 | case CF_422: |
|---|
| 642 | if (have_I420_) |
|---|
| 643 | planarYUYV420_to_planarYUYV422((char *)frame_, outw_, outh_, (char *)last_frame_, inw_, inh_); |
|---|
| 644 | else if (have_UYVY_) |
|---|
| 645 | packedUYVY422_to_planarYUYV422((char *)frame_, outw_, outh_, (char *)last_frame_, inw_, inh_); |
|---|
| 646 | else if (have_YUY2_) |
|---|
| 647 | packedYUYV422_to_planarYUYV422((char *)frame_, outw_, outh_, (char *)last_frame_, inw_, inh_); |
|---|
| 648 | break; |
|---|
| 649 | } |
|---|
| 650 | |
|---|
| 651 | last_frame_ = NULL; |
|---|
| 652 | |
|---|
| 653 | suppress(frame_); |
|---|
| 654 | saveblks(frame_); |
|---|
| 655 | YuvFrame f(media_ts(), frame_, crvec_, outw_, outh_); |
|---|
| 656 | |
|---|
| 657 | rval = (target_->consume(&f)); |
|---|
| 658 | |
|---|
| 659 | |
|---|
| 660 | ReleaseMutex(cb_mutex_); |
|---|
| 661 | |
|---|
| 662 | return rval; |
|---|
| 663 | } |
|---|
| 664 | |
|---|
| 665 | |
|---|
| 666 | |
|---|
| 667 | void DirectShowGrabber::setport(const char *port) { |
|---|
| 668 | |
|---|
| 669 | debug_msg("DirectShowGrabber::setport: %s thread=%x\n", port, GetCurrentThreadId()); |
|---|
| 670 | strcpy(input_port_, port); |
|---|
| 671 | routeCrossbar(); |
|---|
| 672 | |
|---|
| 673 | } |
|---|
| 674 | |
|---|
| 675 | |
|---|
| 676 | void FreeMediaType(AM_MEDIA_TYPE& mt) |
|---|
| 677 | { |
|---|
| 678 | if (mt.cbFormat != 0) { |
|---|
| 679 | CoTaskMemFree((PVOID)mt.pbFormat); |
|---|
| 680 | |
|---|
| 681 | |
|---|
| 682 | mt.cbFormat = 0; |
|---|
| 683 | mt.pbFormat = NULL; |
|---|
| 684 | } |
|---|
| 685 | if (mt.pUnk != NULL) { |
|---|
| 686 | mt.pUnk->Release(); |
|---|
| 687 | mt.pUnk = NULL; |
|---|
| 688 | } |
|---|
| 689 | } |
|---|
| 690 | |
|---|
| 691 | |
|---|
| 692 | |
|---|
| 693 | |
|---|
| 694 | |
|---|
| 695 | void DeleteMediaType( AM_MEDIA_TYPE *pmt) |
|---|
| 696 | { |
|---|
| 697 | |
|---|
| 698 | |
|---|
| 699 | if (pmt == NULL) { |
|---|
| 700 | return; |
|---|
| 701 | } |
|---|
| 702 | |
|---|
| 703 | FreeMediaType(*pmt); |
|---|
| 704 | CoTaskMemFree((PVOID)pmt); |
|---|
| 705 | } |
|---|
| 706 | |
|---|
| 707 | |
|---|
| 708 | int DirectShowGrabber::getCaptureCapabilities() { |
|---|
| 709 | IAMStreamConfig *pConfig; |
|---|
| 710 | AM_MEDIA_TYPE *pmtConfig; |
|---|
| 711 | int iCount; |
|---|
| 712 | int iSize; |
|---|
| 713 | VIDEO_STREAM_CONFIG_CAPS scc; |
|---|
| 714 | HRESULT hr; |
|---|
| 715 | VIDEOINFOHEADER *pVih; |
|---|
| 716 | |
|---|
| 717 | pConfig = NULL; |
|---|
| 718 | hr = pBuild_->FindInterface(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video, |
|---|
| 719 | pCaptureFilter_, IID_IAMStreamConfig, (void**)&pConfig); |
|---|
| 720 | if (FAILED(hr)) { |
|---|
| 721 | return FALSE; |
|---|
| 722 | } |
|---|
| 723 | |
|---|
| 724 | max_width_ = 0; |
|---|
| 725 | max_height_ = 0; |
|---|
| 726 | min_width_ = 0xFFFF; |
|---|
| 727 | min_height_ = 0xFFFF; |
|---|
| 728 | iCount = iSize = 0; |
|---|
| 729 | hr = pConfig->GetNumberOfCapabilities(&iCount, &iSize); |
|---|
| 730 | |
|---|
| 731 | |
|---|
| 732 | if ( iSize == sizeof(VIDEO_STREAM_CONFIG_CAPS) ) { |
|---|
| 733 | |
|---|
| 734 | for (int iFormat = 0; iFormat < iCount; iFormat++) { |
|---|
| 735 | hr = pConfig->GetStreamCaps(iFormat, &pmtConfig, (BYTE *)&scc); |
|---|
| 736 | |
|---|
| 737 | if( SUCCEEDED(hr) ) { |
|---|
| 738 | if ((pmtConfig->majortype == MEDIATYPE_Video) && |
|---|
| 739 | (pmtConfig->formattype == FORMAT_VideoInfo) && |
|---|
| 740 | (pmtConfig->cbFormat >= sizeof (VIDEOINFOHEADER)) && |
|---|
| 741 | (pmtConfig->pbFormat != NULL)) { |
|---|
| 742 | if(scc.MaxOutputSize.cx > max_width_){ |
|---|
| 743 | max_width_ = scc.MaxOutputSize.cx; |
|---|
| 744 | max_height_ = scc.MaxOutputSize.cy; |
|---|
| 745 | } |
|---|
| 746 | if(scc.MinOutputSize.cx < min_width_){ |
|---|
| 747 | min_width_ = scc.MinOutputSize.cx; |
|---|
| 748 | min_height_ = scc.MinOutputSize.cy; |
|---|
| 749 | } |
|---|
| 750 | if (pmtConfig->subtype == MEDIASUBTYPE_I420) { |
|---|
| 751 | have_I420_ = true; |
|---|
| 752 | } else if (pmtConfig->subtype == MEDIASUBTYPE_UYVY) { |
|---|
| 753 | have_UYVY_ = true; |
|---|
| 754 | } else if (pmtConfig->subtype == MEDIASUBTYPE_YUY2) { |
|---|
| 755 | have_YUY2_ = true; |
|---|
| 756 | } |
|---|
| 757 | |
|---|
| 758 | debug_msg("Windows GDI BITMAPINFOHEADER follows:\n"); |
|---|
| 759 | pVih = (VIDEOINFOHEADER *)pmtConfig->pbFormat; |
|---|
| 760 | debug_msg("biWidth= %d\n", pVih->bmiHeader.biWidth); |
|---|
| 761 | debug_msg("biHeight= %d\n", pVih->bmiHeader.biHeight); |
|---|
| 762 | debug_msg("biSize= %d\n", pVih->bmiHeader.biSize); |
|---|
| 763 | debug_msg("biPlanes= %d\n", pVih->bmiHeader.biPlanes); |
|---|
| 764 | debug_msg("biBitCount= %d\n", pVih->bmiHeader.biBitCount); |
|---|
| 765 | debug_msg("biCompression= %d\n", pVih->bmiHeader.biCompression); |
|---|
| 766 | debug_msg("biSizeImage= %d\n", pVih->bmiHeader.biSizeImage); |
|---|
| 767 | debug_msg("biXPelsPerMeter=%d\n", pVih->bmiHeader.biXPelsPerMeter); |
|---|
| 768 | debug_msg("biYPelsPerMeter=%d\n", pVih->bmiHeader.biYPelsPerMeter); |
|---|
| 769 | } |
|---|
| 770 | DeleteMediaType(pmtConfig); |
|---|
| 771 | } |
|---|
| 772 | } |
|---|
| 773 | } |
|---|
| 774 | pConfig->Release(); |
|---|
| 775 | if (max_width_>0) |
|---|
| 776 | return TRUE; |
|---|
| 777 | |
|---|
| 778 | return FALSE; |
|---|
| 779 | } |
|---|
| 780 | |
|---|
| 781 | void DirectShowGrabber::setCaptureOutputFormat() { |
|---|
| 782 | IAMStreamConfig *pConfig; |
|---|
| 783 | int iCount; |
|---|
| 784 | int iSize; |
|---|
| 785 | int curr_w=0; |
|---|
| 786 | int curr_h=0; |
|---|
| 787 | int temp_w, temp_h; |
|---|
| 788 | VIDEOINFOHEADER *pVih; |
|---|
| 789 | VIDEO_STREAM_CONFIG_CAPS scc; |
|---|
| 790 | AM_MEDIA_TYPE *pmtConfig; |
|---|
| 791 | int formatSet; |
|---|
| 792 | HRESULT hr; |
|---|
| 793 | |
|---|
| 794 | |
|---|
| 795 | |
|---|
| 796 | debug_msg("DirectShowGrabber::setCaptureOutputFormat(): enter...\n"); |
|---|
| 797 | |
|---|
| 798 | formatSet = 0; |
|---|
| 799 | pConfig = NULL; |
|---|
| 800 | hr = pBuild_->FindInterface(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video, |
|---|
| 801 | pCaptureFilter_, IID_IAMStreamConfig, (void**)&pConfig); |
|---|
| 802 | if (FAILED(hr)) { |
|---|
| 803 | debug_msg("Failed to FindInterface\n"); |
|---|
| 804 | Grabber::status_=-1; |
|---|
| 805 | return; |
|---|
| 806 | } |
|---|
| 807 | |
|---|
| 808 | debug_msg("DirectShowGrabber::setCaptureOutputFormat(): IAMStreamConfig interface acquired\n"); |
|---|
| 809 | |
|---|
| 810 | iCount = iSize = 0; |
|---|
| 811 | hr = pConfig->GetNumberOfCapabilities(&iCount, &iSize); |
|---|
| 812 | |
|---|
| 813 | |
|---|
| 814 | if ( iSize == sizeof(VIDEO_STREAM_CONFIG_CAPS) ) { |
|---|
| 815 | GUID mediasubtype = MEDIASUBTYPE_NULL; |
|---|
| 816 | |
|---|
| 817 | switch (cformat_) { |
|---|
| 818 | case CF_420: |
|---|
| 819 | case CF_CIF: |
|---|
| 820 | if (have_I420_) |
|---|
| 821 | mediasubtype = MEDIASUBTYPE_I420; |
|---|
| 822 | else if (have_UYVY_) |
|---|
| 823 | mediasubtype = MEDIASUBTYPE_UYVY; |
|---|
| 824 | else if (have_YUY2_) |
|---|
| 825 | mediasubtype = MEDIASUBTYPE_YUY2; |
|---|
| 826 | break; |
|---|
| 827 | |
|---|
| 828 | case CF_422: |
|---|
| 829 | if (have_I420_) |
|---|
| 830 | mediasubtype = MEDIASUBTYPE_I420; |
|---|
| 831 | else if (have_UYVY_) |
|---|
| 832 | mediasubtype = MEDIASUBTYPE_UYVY; |
|---|
| 833 | else if (have_YUY2_) |
|---|
| 834 | mediasubtype = MEDIASUBTYPE_YUY2; |
|---|
| 835 | break; |
|---|
| 836 | } |
|---|
| 837 | for (int iFormat = 0; iFormat < iCount; iFormat++) { |
|---|
| 838 | hr = pConfig->GetStreamCaps(iFormat, &pmtConfig, (BYTE *)&scc); |
|---|
| 839 | |
|---|
| 840 | |
|---|
| 841 | if( SUCCEEDED(hr) ) { |
|---|
| 842 | if ((pmtConfig->majortype == MEDIATYPE_Video) && |
|---|
| 843 | (pmtConfig->subtype == mediasubtype || mediasubtype == MEDIASUBTYPE_NULL) && |
|---|
| 844 | (pmtConfig->formattype == FORMAT_VideoInfo) && |
|---|
| 845 | (pmtConfig->cbFormat >= sizeof (VIDEOINFOHEADER)) && |
|---|
| 846 | (pmtConfig->pbFormat != NULL) |
|---|
| 847 | |
|---|
| 848 | ){ |
|---|
| 849 | |
|---|
| 850 | if ((abs(width_ - scc.MaxOutputSize.cx) + abs(height_ - scc.MaxOutputSize.cy))< |
|---|
| 851 | (abs(width_ - curr_w) +abs(height_ - curr_h))) { |
|---|
| 852 | |
|---|
| 853 | pVih = (VIDEOINFOHEADER *)pmtConfig->pbFormat; |
|---|
| 854 | |
|---|
| 855 | |
|---|
| 856 | |
|---|
| 857 | |
|---|
| 858 | |
|---|
| 859 | |
|---|
| 860 | |
|---|
| 861 | |
|---|
| 862 | debug_msg("fps_= %d, AvgTimePerFrame: %d\n", fps_, pVih->AvgTimePerFrame); |
|---|
| 863 | |
|---|
| 864 | debug_msg("Windows GDI BITMAPINFOHEADER follows:\n"); |
|---|
| 865 | debug_msg("biWidth= %d\n", pVih->bmiHeader.biWidth); |
|---|
| 866 | debug_msg("biHeight= %d\n", pVih->bmiHeader.biHeight); |
|---|
| 867 | debug_msg("biSize= %d\n", pVih->bmiHeader.biSize); |
|---|
| 868 | debug_msg("biPlanes= %d\n", pVih->bmiHeader.biPlanes); |
|---|
| 869 | debug_msg("biBitCount= %d\n", pVih->bmiHeader.biBitCount); |
|---|
| 870 | debug_msg("biCompression= %d\n", pVih->bmiHeader.biCompression); |
|---|
| 871 | debug_msg("biSizeImage= %d\n", pVih->bmiHeader.biSizeImage); |
|---|
| 872 | debug_msg("biXPelsPerMeter=%d\n", pVih->bmiHeader.biXPelsPerMeter); |
|---|
| 873 | debug_msg("biYPelsPerMeter=%d\n", pVih->bmiHeader.biYPelsPerMeter); |
|---|
| 874 | debug_msg("biClrUsed= %d\n", pVih->bmiHeader.biClrUsed); |
|---|
| 875 | debug_msg("biClrImportant= %d\n", pVih->bmiHeader.biClrImportant); |
|---|
| 876 | |
|---|
| 877 | temp_w = pVih->bmiHeader.biWidth; |
|---|
| 878 | temp_h = pVih->bmiHeader.biHeight; |
|---|
| 879 | pVih->bmiHeader.biWidth = width_; |
|---|
| 880 | pVih->bmiHeader.biHeight = height_; |
|---|
| 881 | hr = pConfig->SetFormat(pmtConfig); |
|---|
| 882 | if (SUCCEEDED(hr)) { |
|---|
| 883 | curr_w = width_; |
|---|
| 884 | curr_h = height_; |
|---|
| 885 | formatSet = 1; |
|---|
| 886 | debug_msg("Set(wxh): %dx%d, and Got: %dx%d\n",width_,height_, pVih->bmiHeader.biWidth, pVih->bmiHeader.biHeight); |
|---|
| 887 | break; |
|---|
| 888 | } else { |
|---|
| 889 | if ((temp_w < width_) && (temp_h < height_)) { |
|---|
| 890 | pVih->bmiHeader.biWidth = temp_w; |
|---|
| 891 | pVih->bmiHeader.biHeight = temp_h; |
|---|
| 892 | hr = pConfig->SetFormat(pmtConfig); |
|---|
| 893 | if (SUCCEEDED(hr)) { |
|---|
| 894 | curr_w = temp_w; |
|---|
| 895 | curr_h = temp_h; |
|---|
| 896 | formatSet = 1; |
|---|
| 897 | debug_msg("Set(wxh): %dx%d, and Got: %dx%d\n",width_,height_, pVih->bmiHeader.biWidth, pVih->bmiHeader.biHeight); |
|---|
| 898 | } |
|---|
| 899 | } else { |
|---|
| 900 | debug_msg("Failed to Set format this time - trying again\n"); |
|---|
| 901 | } |
|---|
| 902 | } |
|---|
| 903 | } |
|---|
| 904 | } |
|---|
| 905 | DeleteMediaType(pmtConfig); |
|---|
| 906 | } |
|---|
| 907 | } |
|---|
| 908 | } |
|---|
| 909 | pConfig->Release(); |
|---|
| 910 | |
|---|
| 911 | if ( formatSet ) { |
|---|
| 912 | if ( (curr_w != width_) || (curr_h != height_ )) { |
|---|
| 913 | width_ = curr_w; |
|---|
| 914 | height_ = curr_h; |
|---|
| 915 | debug_msg("DirectShowGrabber::setCaptureOutputFormat: format set to near res: %dx%d\n",width_,height_); |
|---|
| 916 | } else |
|---|
| 917 | debug_msg("DirectShowGrabber::setCaptureOutputFormat: format set\n"); |
|---|
| 918 | } |
|---|
| 919 | else |
|---|
| 920 | debug_msg("DirectShowGrabber::setCaptureOutputFormat: format not set\n"); |
|---|
| 921 | } |
|---|
| 922 | |
|---|
| 923 | |
|---|
| 924 | |
|---|
| 925 | int DirectShowGrabber::command(int argc, const char* const* argv) { |
|---|
| 926 | |
|---|
| 927 | if (argc == 3) { |
|---|
| 928 | if (strcmp(argv[1], "decimate") == 0) { |
|---|
| 929 | u_int dec = (u_int)atoi(argv[2]); |
|---|
| 930 | Tcl& tcl = Tcl::instance(); |
|---|
| 931 | if (dec <= 0) { |
|---|
| 932 | tcl.resultf("%s: divide by zero", argv[0]); |
|---|
| 933 | return (TCL_ERROR); |
|---|
| 934 | } |
|---|
| 935 | debug_msg("DirectShowGrabber::command: decimate=%d (dec)=%d\n", dec, decimate_); |
|---|
| 936 | if (dec != decimate_) { |
|---|
| 937 | decimate_ = dec; |
|---|
| 938 | if (running_) { |
|---|
| 939 | stop(); |
|---|
| 940 | setsize(); |
|---|
| 941 | setCaptureOutputFormat(); |
|---|
| 942 | start(); |
|---|
| 943 | } else{ |
|---|
| 944 | setsize(); |
|---|
| 945 | setCaptureOutputFormat(); |
|---|
| 946 | } |
|---|
| 947 | } |
|---|
| 948 | return (TCL_OK); |
|---|
| 949 | } else if (strcmp(argv[1], "port") == 0) { |
|---|
| 950 | setport(argv[2]); |
|---|
| 951 | return (TCL_OK); |
|---|
| 952 | } else if (strcmp(argv[1], "type") == 0) { |
|---|
| 953 | if (strcmp(argv[2], "auto") == 0) |
|---|
| 954 | ; |
|---|
| 955 | else if (strcmp(argv[2], "pal") == 0) { |
|---|
| 956 | basewidth_ = CIF_BASE_WIDTH; |
|---|
| 957 | baseheight_ = CIF_BASE_HEIGHT; |
|---|
| 958 | } else if (strcmp(argv[2], "ntsc") == 0) { |
|---|
| 959 | basewidth_ = NTSC_BASE_WIDTH; |
|---|
| 960 | baseheight_ = NTSC_BASE_HEIGHT; |
|---|
| 961 | } |
|---|
| 962 | if (running_) { |
|---|
| 963 | stop(); |
|---|
| 964 | setsize(); |
|---|
| 965 | setCaptureOutputFormat(); |
|---|
| 966 | start(); |
|---|
| 967 | } else { |
|---|
| 968 | setsize(); |
|---|
| 969 | setCaptureOutputFormat(); |
|---|
| 970 | } |
|---|
| 971 | return (TCL_OK); |
|---|
| 972 | } else if (strcmp(argv[1], "useconfig") ==0) { |
|---|
| 973 | if (strcmp(argv[2], "1") == 0) |
|---|
| 974 | useconfig_=1; |
|---|
| 975 | if (strcmp(argv[2], "0") == 0) |
|---|
| 976 | useconfig_=0; |
|---|
| 977 | } |
|---|
| 978 | } |
|---|
| 979 | return (Grabber::command(argc, argv)); |
|---|
| 980 | |
|---|
| 981 | } |
|---|
| 982 | |
|---|
| 983 | |
|---|
| 984 | |
|---|
| 985 | |
|---|
| 986 | |
|---|
| 987 | DirectShowDevice::DirectShowDevice(char *friendlyName, IBaseFilter *pCapFilt) : InputDevice(friendlyName) { |
|---|
| 988 | |
|---|
| 989 | attri_ = new char[128]; |
|---|
| 990 | attri_[0] = 0; |
|---|
| 991 | |
|---|
| 992 | debug_msg("new DirectShowDevice(): friendlyName=%s\n", friendlyName); |
|---|
| 993 | pDirectShowFilter_ = pCapFilt; |
|---|
| 994 | DirectShowGrabber o(pDirectShowFilter_, "420", friendlyName); |
|---|
| 995 | |
|---|
| 996 | strcat(attri_, "format { 420 422 cif } size { "); |
|---|
| 997 | |
|---|
| 998 | if (o.minWidth() > (CIF_BASE_WIDTH / 2)) { |
|---|
| 999 | strcat(attri_, "large"); |
|---|
| 1000 | } else if (o.maxWidth() < NTSC_BASE_WIDTH) { |
|---|
| 1001 | strcat(attri_, "small cif"); |
|---|
| 1002 | } else { |
|---|
| 1003 | strcat(attri_, "small cif large"); |
|---|
| 1004 | } |
|---|
| 1005 | |
|---|
| 1006 | strcat(attri_, " } type { pal ntsc } port { "); |
|---|
| 1007 | if(o.hasSVideo() || o.hasComposite()){ |
|---|
| 1008 | if(o.hasSVideo()){ |
|---|
| 1009 | strcat(attri_, "S-Video "); |
|---|
| 1010 | } |
|---|
| 1011 | if(o.hasComposite()){ |
|---|
| 1012 | strcat(attri_, "Composite "); |
|---|
| 1013 | } |
|---|
| 1014 | }else{ |
|---|
| 1015 | strcat(attri_, "external-in "); |
|---|
| 1016 | } |
|---|
| 1017 | |
|---|
| 1018 | strcat(attri_, "} "); |
|---|
| 1019 | attributes_ = attri_; |
|---|
| 1020 | } |
|---|
| 1021 | |
|---|
| 1022 | DirectShowDevice::~DirectShowDevice(){ |
|---|
| 1023 | |
|---|
| 1024 | pDirectShowFilter_->Release(); |
|---|
| 1025 | delete attri_; |
|---|
| 1026 | } |
|---|
| 1027 | |
|---|
| 1028 | |
|---|
| 1029 | int DirectShowDevice::command(int argc, const char* const* argv) { |
|---|
| 1030 | Tcl& tcl = Tcl::instance(); |
|---|
| 1031 | if ((argc == 3) && (strcmp(argv[1], "open") == 0)) { |
|---|
| 1032 | TclObject* o = 0; |
|---|
| 1033 | |
|---|
| 1034 | o = directShowGrabber_ = new DirectShowGrabber(pDirectShowFilter_, argv[2]); |
|---|
| 1035 | if (o != 0) |
|---|
| 1036 | Tcl::instance().result(o->name()); |
|---|
| 1037 | return (TCL_OK); |
|---|
| 1038 | } |
|---|
| 1039 | return (InputDevice::command(argc, argv)); |
|---|
| 1040 | } |
|---|
| 1041 | |
|---|
| 1042 | |
|---|
| 1043 | |
|---|
| 1044 | |
|---|
| 1045 | DirectShowScanner::DirectShowScanner():pMoniker_(0) |
|---|
| 1046 | { |
|---|
| 1047 | ICreateDevEnum *pDevEnum = 0; |
|---|
| 1048 | int hr; |
|---|
| 1049 | int devNum; |
|---|
| 1050 | char nameBuf[NAMEBUF_LEN]; |
|---|
| 1051 | |
|---|
| 1052 | |
|---|
| 1053 | |
|---|
| 1054 | debug_msg("new DirectShowScanner()\n"); |
|---|
| 1055 | |
|---|
| 1056 | |
|---|
| 1057 | hr=CoInitializeEx(NULL,COINIT_MULTITHREADED); |
|---|
| 1058 | if (FAILED(hr)) { |
|---|
| 1059 | debug_msg("Failed COM subsystem initialisation.\n"); |
|---|
| 1060 | return; |
|---|
| 1061 | } |
|---|
| 1062 | |
|---|
| 1063 | |
|---|
| 1064 | hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, (LPVOID*)&pDevEnum); |
|---|
| 1065 | if (FAILED(hr)) { |
|---|
| 1066 | debug_msg("Failed to Create a helper object to find the DS capture devices.\n"); |
|---|
| 1067 | CoUninitialize(); |
|---|
| 1068 | return; |
|---|
| 1069 | } |
|---|
| 1070 | |
|---|
| 1071 | IEnumMoniker *pEnum = 0; |
|---|
| 1072 | IPropertyBag *pPropBag = 0; |
|---|
| 1073 | VARIANT varName; |
|---|
| 1074 | CLSID clsid; |
|---|
| 1075 | |
|---|
| 1076 | |
|---|
| 1077 | hr = pDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEnum, 0); |
|---|
| 1078 | |
|---|
| 1079 | if (FAILED(hr) || pEnum == 0) { |
|---|
| 1080 | debug_msg("Failed to Get an enumerator over DS video capture filters.\n"); |
|---|
| 1081 | CoUninitialize(); |
|---|
| 1082 | return; |
|---|
| 1083 | } |
|---|
| 1084 | |
|---|
| 1085 | |
|---|
| 1086 | for( devNum=0; devNum < NUM_DEVS; ++devNum) { |
|---|
| 1087 | if ( pEnum->Next(1, &pMoniker_, NULL) == S_OK ) { |
|---|
| 1088 | |
|---|
| 1089 | hr = pMoniker_->BindToStorage(0, 0, IID_IPropertyBag, (void **)&pPropBag); |
|---|
| 1090 | if (FAILED(hr)) { |
|---|
| 1091 | debug_msg("Failed to Get propbag bound to storage on DS dev: %d\n", devNum); |
|---|
| 1092 | continue; |
|---|
| 1093 | } |
|---|
| 1094 | |
|---|
| 1095 | debug_msg("propbag bound to storage ok= %d\n", hr); |
|---|
| 1096 | |
|---|
| 1097 | VariantInit(&varName); |
|---|
| 1098 | hr = pPropBag->Read(L"FriendlyName", &varName, 0); |
|---|
| 1099 | if (FAILED(hr)) { |
|---|
| 1100 | debug_msg("Failed to Get friendly name read on DS dev: %d\n", devNum); |
|---|
| 1101 | VariantClear(&varName); |
|---|
| 1102 | pPropBag->Release(); |
|---|
| 1103 | continue; |
|---|
| 1104 | } |
|---|
| 1105 | |
|---|
| 1106 | debug_msg("friendly name read ok= %d\n", hr); |
|---|
| 1107 | |
|---|
| 1108 | |
|---|
| 1109 | |
|---|
| 1110 | |
|---|
| 1111 | nameBuf[0] = '\0'; |
|---|
| 1112 | WideCharToMultiByte(CP_ACP, 0, varName.bstrVal, -1, nameBuf, NAMEBUF_LEN, NULL, NULL); |
|---|
| 1113 | |
|---|
| 1114 | |
|---|
| 1115 | debug_msg("DirectShowScanner::DirectShowScanner(): found nameBuf/FriendlyName=%s\n", nameBuf); |
|---|
| 1116 | |
|---|
| 1117 | |
|---|
| 1118 | |
|---|
| 1119 | hr = pMoniker_->BindToObject(0, 0, IID_IBaseFilter, (void **)&pCaptureFilter[devNum]); |
|---|
| 1120 | if (FAILED(hr)) { |
|---|
| 1121 | debug_msg("Failed to Get friendly name read on DS dev: %d\n", devNum); |
|---|
| 1122 | VariantClear(&varName); |
|---|
| 1123 | pPropBag->Release(); |
|---|
| 1124 | continue; |
|---|
| 1125 | } |
|---|
| 1126 | debug_msg("capture filter bound ok= [%d} %s\n", hr, nameBuf); |
|---|
| 1127 | pCaptureFilter[devNum]->GetClassID(&clsid); |
|---|
| 1128 | VariantClear(&varName); |
|---|
| 1129 | if (!IsEqualGUID(clsid,CLSID_VfwCapture)) { |
|---|
| 1130 | pMoniker_->AddRef(); |
|---|
| 1131 | debug_msg("Adding capture filter %d\n", hr); |
|---|
| 1132 | devs_[devNum] = new DirectShowDevice(strdup(nameBuf), pCaptureFilter[devNum]); |
|---|
| 1133 | } else { |
|---|
| 1134 | debug_msg("discarding an apparent VFW device= %s\n", nameBuf); |
|---|
| 1135 | devs_[devNum] = NULL; |
|---|
| 1136 | pMoniker_->Release(); |
|---|
| 1137 | } |
|---|
| 1138 | pPropBag->Release(); |
|---|
| 1139 | } |
|---|
| 1140 | } |
|---|
| 1141 | |
|---|
| 1142 | |
|---|
| 1143 | pEnum->Release(); |
|---|
| 1144 | pDevEnum->Release(); |
|---|
| 1145 | } |
|---|
| 1146 | |
|---|
| 1147 | |
|---|
| 1148 | |
|---|
| 1149 | DirectShowScanner::~DirectShowScanner() { |
|---|
| 1150 | |
|---|
| 1151 | CoUninitialize(); |
|---|
| 1152 | debug_msg("~DirectShowScanner()\n"); |
|---|
| 1153 | } |
|---|