Check-in [298fb7cf26]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:improved tclwmf w.r.t. frame size and documentation
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 298fb7cf26db9049316c1939422f190abe8e24a7
User & Date: chw 2016-07-06 14:34:03.337
Original Comment: improvemed tclwmf w.r.t. frame size and documentation
Context
2016-07-06
20:15
fixed locking bug in tclwmf check-in: e360dfbef1 user: chw tags: trunk
14:34
improved tclwmf w.r.t. frame size and documentation check-in: 298fb7cf26 user: chw tags: trunk
14:32
addition to [0325db73aa] check-in: 79d7903284 user: chw tags: trunk
Changes
Unified Diff Ignore Whitespace Patch
Changes to undroid/tclwmf/demos/simple.tcl.
34
35
36
37
38
39
40
41

42

43
44
45

46
47
48
49
50
51
52
	set name [lindex [wmf devices] 0]
    }
    if {$name eq ""} {
	$but configure -text "No Camera"
	return
    }
    set dev [wmf open $name [list imagecb $but $img]]
    array set d [wmf parameters $dev]

    if {[info exists d(frame-size)] &&

	[scan $d(frame-size) %dx%d w h] == 2} {
	$img configure -width $w -height $h
	$img blank

    }
    $but configure -text "Start"
}

proc changemirror {w} {
    set dev [wmf info]
    if {$dev ne ""} {







|
>
|
>
|
|
|
>







34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
	set name [lindex [wmf devices] 0]
    }
    if {$name eq ""} {
	$but configure -text "No Camera"
	return
    }
    set dev [wmf open $name [list imagecb $but $img]]
    array set flist [wmf listformats $dev]
    set index [wmf format $dev]
    if {[info exists flist($index)]} {
	array set fmt $flist($index)
	if {[scan $fmt(frame-size) %dx%d w h] == 2} {
	    $img configure -width $w -height $h
	    $img blank
	}
    }
    $but configure -text "Start"
}

proc changemirror {w} {
    set dev [wmf info]
    if {$dev ne ""} {
Changes to undroid/tclwmf/doc/wmf.n.
297
298
299
300
301
302
303

304






305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322







323
324
325
326
327
328
329
.TP
\fBwmf devices\fR
.
Returns a list of device names which can be used for \fBwmf open\fR.
Each device adds two elements to the list: its symbolic link to be
used in \fBwmf open\fR and its friendly name for presentation.
.TP

.






\fBwmf image\fR \fIdevid\fR ?\fIphotoImage\fR?
.
Copies the most recent captured image of the device \fIdevid\fR into
the photo image identified by \fIphotoImage\fR and returns non-zero on
success or zero if no data transfer has taken place.
If \fIphotoImage\fR is omitted, a four element list is returned with the
first element being the image width, the second the image height,
the third the number of bytes per pixel, and the last the image's RGB
values with 3 bytes per pixel in red, green, blue order as a byte array.
In this case an error is indicated by throwing an exception.
.TP
\fBwmf info\fR ?\fIdevid\fR?
.
Returns information on open devices. If \fIdevid\fR is specified,
a list of two elements is returned, the first being the device symbolic
link and the second the image callback command for that device, i.e. the
same arguments which were used on \fBwmf open\fR. If \fIdevid\fR is omitted,
a list of \fIdevid\fRs, i.e. all currently opened devices is returned.







.TP
\fBwmf mirror\fR \fIdevid\fR ?\fIx y\fR?
.
Retrieves or sets flags to mirror captured images along the X or Y axis.
Parameters \fIx\fR and \fIy\fR if specified must be boolean values.
.TP
\fBwmf open\fR \fIdevname callback\fR







>

>
>
>
>
>
>


















>
>
>
>
>
>
>







297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
.TP
\fBwmf devices\fR
.
Returns a list of device names which can be used for \fBwmf open\fR.
Each device adds two elements to the list: its symbolic link to be
used in \fBwmf open\fR and its friendly name for presentation.
.TP
\fBwmf format\fR \fIdevid\fR ?\fIindex\fR?
.
Returns or changes the media format of the device identifed by \fIdevid\fR.
The optional parameter \fIindex\fR is an integer number giving the index of
the media format to be used as returned in \fBwmf listformats\fR. If omitted,
the currently active index is returned. Changing the media format is only
possible if the device is not capturing images.
.TP
\fBwmf image\fR \fIdevid\fR ?\fIphotoImage\fR?
.
Copies the most recent captured image of the device \fIdevid\fR into
the photo image identified by \fIphotoImage\fR and returns non-zero on
success or zero if no data transfer has taken place.
If \fIphotoImage\fR is omitted, a four element list is returned with the
first element being the image width, the second the image height,
the third the number of bytes per pixel, and the last the image's RGB
values with 3 bytes per pixel in red, green, blue order as a byte array.
In this case an error is indicated by throwing an exception.
.TP
\fBwmf info\fR ?\fIdevid\fR?
.
Returns information on open devices. If \fIdevid\fR is specified,
a list of two elements is returned, the first being the device symbolic
link and the second the image callback command for that device, i.e. the
same arguments which were used on \fBwmf open\fR. If \fIdevid\fR is omitted,
a list of \fIdevid\fRs, i.e. all currently opened devices is returned.
.TP
\fBwmf listformats\fR \fIdevid\fR
.
Returns a dictionary keyed by a media format index as integer with the
values being another dictionary with information about the frame size
and rate of that media format. The respective index can be used
in \fBwmf format\fR.
.TP
\fBwmf mirror\fR \fIdevid\fR ?\fIx y\fR?
.
Retrieves or sets flags to mirror captured images along the X or Y axis.
Parameters \fIx\fR and \fIy\fR if specified must be boolean values.
.TP
\fBwmf open\fR \fIdevname callback\fR
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359

360
361
362
363
364
365

366
367
368
369
370
371
372
373
374
375
\fIdevname\fR see the description of \fBwmf devices\fR.
.TP
\fBwmf orientation\fR \fIdevid\fR ?\fIdegrees\fR?
.
Retrieves or sets the orientation of captured images regarding image
rotation. \fIDegrees\fR if specified must be an integer number.
.TP
\fBwmf parameters\fR \fIdevid\fR ?\fIkey value ...\fR?
.
Returns or changes device parameters for the device identified by \fIdevid\fR
given as key-value pairs, e.g. \fBframe-size 320x240\fR will change the size
of captured images to width 320 and height 240. The command returns the
current device parameters (after the potential change, when keys and values
where given) as a key-value list which can be processed with \fBarray set\fR
or \fBdict get\fR. Currently, this command does nothing. 
.TP
\fBwmf start\fR \fIdevid\fR
Starts capturing images of the device identified by \fIdevid\fR. When
an image is ready, the callback command set on \fBwmf open\fR is
invoked.
.
.TP
\fBwmf state\fR \fIdevid\fR

Returns the image capture state of the device identified by \fIdevid\fR.
The result is the string \fBcapture\fR if the device is started or
\fBstop\fR if the device is stopped.
.
.TP
\fBwmf stop\fR \fIdevid\fR

Stops capturing images of the device identified by \fIdevid\fR.
.
.SH "SEE ALSO"
photo(n), image(n)
.SH KEYWORDS
files, image, photo, video, camera
'\" Local Variables:
'\" mode: nroff
'\" fill-column: 78
'\" End:







|

<
<
<
<
<
<
<
<



<


>



<


>










351
352
353
354
355
356
357
358
359








360
361
362

363
364
365
366
367
368

369
370
371
372
373
374
375
376
377
378
379
380
381
\fIdevname\fR see the description of \fBwmf devices\fR.
.TP
\fBwmf orientation\fR \fIdevid\fR ?\fIdegrees\fR?
.
Retrieves or sets the orientation of captured images regarding image
rotation. \fIDegrees\fR if specified must be an integer number.
.TP
\fBwmf start\fR \fIdevid\fR
.








Starts capturing images of the device identified by \fIdevid\fR. When
an image is ready, the callback command set on \fBwmf open\fR is
invoked.

.TP
\fBwmf state\fR \fIdevid\fR
.
Returns the image capture state of the device identified by \fIdevid\fR.
The result is the string \fBcapture\fR if the device is started or
\fBstop\fR if the device is stopped.

.TP
\fBwmf stop\fR \fIdevid\fR
.
Stops capturing images of the device identified by \fIdevid\fR.
.
.SH "SEE ALSO"
photo(n), image(n)
.SH KEYWORDS
files, image, photo, video, camera
'\" Local Variables:
'\" mode: nroff
'\" fill-column: 78
'\" End:
Changes to undroid/tclwmf/tclwmf.c.
128
129
130
131
132
133
134














135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151



152
153
154
155
156
157

158
159
160
161
162
163
164
    int maxLength;		/* Max. length of pixel buffer. */
    int ready;			/* Zero when free, positive when filled. */
    int fourcc;			/* Media format code. */
    int width, height;		/* Width and height in pixels. */
    int stride;			/* Line increment in bytes. */
} Frame;















/*
 * Control structure for capture.
 */

typedef struct WMFC {
    IMFMediaSource *mediaSrc;	/* Handle for device. */
    IMFSourceReader *srcReader;	/* Handle for capture. */
    Tcl_Interp *interp;		/* Interpreter for this object. */
    char devId[32];		/* Device id. */
    Tcl_DString devName;	/* Device name. */
    int cbCmdLen;               /* Initial length of callback command. */
    Tcl_DString cbCmd;          /* Callback command prefix. */
    int fourcc, compr;		/* FOURCC and compression. */
    int stride, width, height;	/* Image dimensions. */
    int mirror;			/* Image mirror flags. */
    int rotate;			/* Image rotation in degrees. */
    Tcl_WideInt counters[4];	/* Statistic counters. */



#ifdef USE_ASYNC_HANDLER
    Tcl_AsyncHandler async;	/* For signalling captured image. */
#else
    Tcl_ThreadId tid;		/* Thread identifier of interp. */
#endif
    SourceReaderCB srcb;	/* Callback interface object. */

    int frameReady;		/* Frame index with data or -1. */
    int frameQueued;		/* Next frame index to be filled. */
    Frame frame[2];		/* Frame buffers. */
} WMFC;

#ifndef USE_ASYNC_HANDLER








>
>
>
>
>
>
>
>
>
>
>
>
>
>












|




>
>
>






>







128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
    int maxLength;		/* Max. length of pixel buffer. */
    int ready;			/* Zero when free, positive when filled. */
    int fourcc;			/* Media format code. */
    int width, height;		/* Width and height in pixels. */
    int stride;			/* Line increment in bytes. */
} Frame;

/*
 * Media format.
 */

typedef struct {
    int index;			/* Index for activation. */
    int fourcc;			/* Four character code. */
    int width, height;		/* Width and height in pixels. */
    int stride;			/* Line increment in bytes. */
    UINT64 frameRate;		/* Rate (nominator/denominator). */
    UINT64 frameRateMin;	/* Min. rate (nominator/denominator). */
    UINT64 frameRateMax;	/* Max. rate (nominator/denominator). */
} MediaFmt;

/*
 * Control structure for capture.
 */

typedef struct WMFC {
    IMFMediaSource *mediaSrc;	/* Handle for device. */
    IMFSourceReader *srcReader;	/* Handle for capture. */
    Tcl_Interp *interp;		/* Interpreter for this object. */
    char devId[32];		/* Device id. */
    Tcl_DString devName;	/* Device name. */
    int cbCmdLen;               /* Initial length of callback command. */
    Tcl_DString cbCmd;          /* Callback command prefix. */
    int fourcc;			/* Media format code. */
    int stride, width, height;	/* Image dimensions. */
    int mirror;			/* Image mirror flags. */
    int rotate;			/* Image rotation in degrees. */
    Tcl_WideInt counters[4];	/* Statistic counters. */
    int useFmt;			/* MediaFmt index to use. */
    int numFmts;		/* Number formats in fmts. */
    MediaFmt *fmts;		/* Array of MediaFmts or NULL. */
#ifdef USE_ASYNC_HANDLER
    Tcl_AsyncHandler async;	/* For signalling captured image. */
#else
    Tcl_ThreadId tid;		/* Thread identifier of interp. */
#endif
    SourceReaderCB srcb;	/* Callback interface object. */
    int streamEnd;		/* Indicates capture error or EOF. */
    int frameReady;		/* Frame index with data or -1. */
    int frameQueued;		/* Next frame index to be filled. */
    Frame frame[2];		/* Frame buffers. */
} WMFC;

#ifndef USE_ASYNC_HANDLER

403
404
405
406
407
408
409
410
411
412
413




414
415
416
417
418
419
420

    if (!SUCCEEDED(hrStatus)) {
	hr = hrStatus;
    }
    EnterCriticalSection(&srcb->lock);
    wmfc = srcb->wmfc;
    if ((wmfc->frameQueued < 0) || !SUCCEEDED(hr)) {
	LeaveCriticalSection(&srcb->lock);
	return hr;
    }
    if (sample == NULL) {




	goto done;
    }
    wmfc->counters[0] += 1;
    index = wmfc->frameQueued;
    hr = sample->lpVtbl->ConvertToContiguousBuffer(sample, &mbuf);
    if (SUCCEEDED(hr)) {
	DWORD length = 0, maxLength = 0;







|
|


>
>
>
>







421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442

    if (!SUCCEEDED(hrStatus)) {
	hr = hrStatus;
    }
    EnterCriticalSection(&srcb->lock);
    wmfc = srcb->wmfc;
    if ((wmfc->frameQueued < 0) || !SUCCEEDED(hr)) {
	wmfc->streamEnd = -1;
	goto done;
    }
    if (sample == NULL) {
	if ((streamFlags & MF_SOURCE_READERF_ENDOFSTREAM) ==
	    MF_SOURCE_READERF_ENDOFSTREAM) {
	    wmfc->streamEnd = 1;
	}
	goto done;
    }
    wmfc->counters[0] += 1;
    index = wmfc->frameQueued;
    hr = sample->lpVtbl->ConvertToContiguousBuffer(sample, &mbuf);
    if (SUCCEEDED(hr)) {
	DWORD length = 0, maxLength = 0;
524
525
526
527
528
529
530




531

532
533
534
535
536
537
538
    int capture = (wmfc->async != NULL);
#else
    int capture = (wmfc->tid != NULL);
#endif

    Tcl_DStringSetLength(&wmfc->cbCmd, wmfc->cbCmdLen);
    Tcl_DStringAppendElement(&wmfc->cbCmd, wmfc->devId);




    Tcl_DStringAppendElement(&wmfc->cbCmd, capture ? "capture" : "stop");

    Tcl_Preserve((ClientData) interp);
    ret = Tcl_EvalEx(interp, Tcl_DStringValue(&wmfc->cbCmd),
		     Tcl_DStringLength(&wmfc->cbCmd), TCL_EVAL_GLOBAL);
    if (ret != TCL_OK) {
	StopCapture(wmfc);
	Tcl_AddErrorInfo(interp, "\n    (wmf event handler)");
 	Tcl_BackgroundError(interp);







>
>
>
>
|
>







546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
    int capture = (wmfc->async != NULL);
#else
    int capture = (wmfc->tid != NULL);
#endif

    Tcl_DStringSetLength(&wmfc->cbCmd, wmfc->cbCmdLen);
    Tcl_DStringAppendElement(&wmfc->cbCmd, wmfc->devId);
    if (wmfc->streamEnd) {
	Tcl_DStringAppendElement(&wmfc->cbCmd, (wmfc->streamEnd < 0) ?
				 "error" : "eof");
    } else {
	Tcl_DStringAppendElement(&wmfc->cbCmd, capture ? "capture" : "stop");
    }
    Tcl_Preserve((ClientData) interp);
    ret = Tcl_EvalEx(interp, Tcl_DStringValue(&wmfc->cbCmd),
		     Tcl_DStringLength(&wmfc->cbCmd), TCL_EVAL_GLOBAL);
    if (ret != TCL_OK) {
	StopCapture(wmfc);
	Tcl_AddErrorInfo(interp, "\n    (wmf event handler)");
 	Tcl_BackgroundError(interp);
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
#ifdef USE_ASYNC_HANDLER
	Tcl_AsyncDelete(wmfc->async);
	wmfc->async = NULL;
#else
	wmfc->tid = NULL;
#endif
    }
#ifdef USE_ASYNC_HANDLER
    if (bufok || dostop) {
	Tcl_CancelIdleCall(ImageCallback, clientData);
	Tcl_DoWhenIdle(ImageCallback, clientData);
    }
#else
    if (bufok || dostop) {
	ImageCallback((ClientData) wmfc);
    }
#endif
    return code;
}

/*
 *-------------------------------------------------------------------------
 *
 * SetFormat --
 *
 *	Set media source and source reader format given index.
 *	which was obtained by FindYUVMediaType().
 *
 *-------------------------------------------------------------------------
 */

static HRESULT
SetFormat(IMFMediaSource *mediaSrc, IMFSourceReader *srcReader, int index)
{







<

|
|

<
<
<
<
<








|
|







634
635
636
637
638
639
640

641
642
643
644





645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
#ifdef USE_ASYNC_HANDLER
	Tcl_AsyncDelete(wmfc->async);
	wmfc->async = NULL;
#else
	wmfc->tid = NULL;
#endif
    }

    if (bufok || dostop) {
	Tcl_CancelIdleCall(ImageCallback, (ClientData) wmfc);
	Tcl_DoWhenIdle(ImageCallback, (ClientData) wmfc);
    }





    return code;
}

/*
 *-------------------------------------------------------------------------
 *
 * SetFormat --
 *
 *	Set media source and source reader format given index
 *	which must be an index from the MediaFmts array.
 *
 *-------------------------------------------------------------------------
 */

static HRESULT
SetFormat(IMFMediaSource *mediaSrc, IMFSourceReader *srcReader, int index)
{
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703

704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723




724
725
726




727
728
729



730

731
732

733



734





735


736



737






738








739


740


741


742

743





744
745
746
747
748
749
750
751
752
753
754





755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774


775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832

833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
    }
    return hr;
}

/*
 *-------------------------------------------------------------------------
 *
 * FindYUVMediaType --
 *
 *	Find the first YUY2 or NV12 media type given media source.
 *	Returns index of the media type or -1 when nothing found.
 *
 *-------------------------------------------------------------------------
 */

static int
FindYUVMediaType(IMFMediaSource *mediaSrc)
{
    IMFPresentationDescriptor *pd = NULL;
    IMFStreamDescriptor *sd = NULL;
    IMFMediaTypeHandler *handler = NULL;
    IMFMediaType *mediaType = NULL;

    BOOL selected;
    DWORD i, count = 0;
    int found = -1;
    HRESULT hr;

    hr = mediaSrc->lpVtbl->CreatePresentationDescriptor(mediaSrc, &pd);
    if (!SUCCEEDED(hr)) {
	goto done;
    }
    hr = pd->lpVtbl->GetStreamDescriptorByIndex(pd, 0, &selected, &sd);
    if (!SUCCEEDED(hr)) {
	goto done;
    }
    hr = sd->lpVtbl->GetMediaTypeHandler(sd, &handler);
    if (!SUCCEEDED(hr)) {
	goto done;
    }
    hr = handler->lpVtbl->GetMediaTypeCount(handler, &count);
    if (!SUCCEEDED(hr)) {
	goto done;




    }
    for (i = 0; i < count; i++) {
	GUID subType = GUID_NULL;





	hr = handler->lpVtbl->GetMediaTypeByIndex(handler, i, &mediaType);
	if (!SUCCEEDED(hr)) {



	    break;

	}
	hr = mediaType->lpVtbl->GetGUID(mediaType, &MF_MT_SUBTYPE, &subType);

	if (mediaType != NULL) {



	    mediaType->lpVtbl->Release(mediaType);





	    mediaType = NULL;


	}



	if (SUCCEEDED(hr)) {






	    if ((subType.Data1 == FOURCC_NV12) ||








		(subType.Data1 == FOURCC_YUY2)) {


		found = i;


		break;


	    }

	}





    }
done:
    if (handler != NULL) {
	handler->lpVtbl->Release(handler);
    }
    if (sd != NULL) {
	sd->lpVtbl->Release(sd);
    }
    if (pd != NULL) {
	pd->lpVtbl->Release(pd);
    }





    return found;
}

/*
 *-------------------------------------------------------------------------
 *
 * GetFormat --
 *
 *	Get information about the media format (width, height etc.)
 *	and try to switch MJPG source to YUY2 or NV12 format.
 *
 *-------------------------------------------------------------------------
 */

static int
GetFormat(Tcl_Interp *interp, IMFMediaSource *mediaSrc,
	  IMFSourceReader *srcReader,
	  int *fourcc, int *compr, int *stride, int *width, int *height)
{
    HRESULT hr;


    IMFMediaType *mediaType = NULL;
    GUID subType = GUID_NULL;
    LONG lval = 0;
    UINT32 uval = 0;
    UINT64 wval = 0;
    UINT32 w = 0, h = 0;
    BOOL isCompressed = FALSE;
    int result = TCL_ERROR;

    hr = srcReader->lpVtbl->GetCurrentMediaType(srcReader,
		MF_SOURCE_READER_FIRST_VIDEO_STREAM, &mediaType);
    if (!SUCCEEDED(hr)) {
	Tcl_SetResult(interp, "cannot get media type from source reader",
		      TCL_STATIC);
	goto done;
    }
    mediaType->lpVtbl->IsCompressedFormat(mediaType, &isCompressed);
    *compr = isCompressed;
    hr = mediaType->lpVtbl->GetGUID(mediaType, &MF_MT_SUBTYPE, &subType);
    if (!SUCCEEDED(hr)) {
	Tcl_SetResult(interp, "cannot get MF_MT_SUBTYPE", TCL_STATIC);
	goto done;
    }
    if (subType.Data1 == FOURCC_MJPG) {
	/* try to switch from MJPG to YUY2 or NV12 */
	int index = FindYUVMediaType(mediaSrc);

	if (index < 0) {
	    Tcl_SetResult(interp, "no suitable media format found",
			  TCL_STATIC);
	    goto done;
	}
	hr = SetFormat(mediaSrc, srcReader, index);
	if (!SUCCEEDED(hr)) {
	    Tcl_SetResult(interp, "cannot switch to usable media format",
			  TCL_STATIC);
	    goto done;
	}
	mediaType->lpVtbl->Release(mediaType);
	mediaType = NULL;
	hr = srcReader->lpVtbl->GetCurrentMediaType(srcReader,
			MF_SOURCE_READER_FIRST_VIDEO_STREAM, &mediaType);
	if (!SUCCEEDED(hr)) {
	    Tcl_SetResult(interp, "cannot get media type from source reader",
			  TCL_STATIC);
	    goto done;
	}
	mediaType->lpVtbl->IsCompressedFormat(mediaType, &isCompressed);
	*compr = isCompressed;
	hr = mediaType->lpVtbl->GetGUID(mediaType, &MF_MT_SUBTYPE, &subType);
	if (!SUCCEEDED(hr)) {
	    Tcl_SetResult(interp, "cannot get MF_MT_SUBTYPE", TCL_STATIC);
	    goto done;
	}
    }
    if ((subType.Data1 != FOURCC_NV12) && (subType.Data1 != FOURCC_YUY2)) {
	Tcl_SetObjResult(interp,
		Tcl_ObjPrintf("unsupported fourcc 0x%08lX", subType.Data1));

	goto done;
    }
    *fourcc = subType.Data1;
    hr = mediaType->lpVtbl->GetUINT32(mediaType, &MF_MT_DEFAULT_STRIDE, &uval);
    if (!SUCCEEDED(hr)) {
	hr = mediaType->lpVtbl->GetUINT64(mediaType, &MF_MT_FRAME_SIZE, &wval);
	if (SUCCEEDED(hr)) {
	    w = wval >> 32;
	    h = wval;
	}
	if (!SUCCEEDED(hr)) {
	    Tcl_SetResult(interp, "cannot get MF_MT_FRAME_SIZE", TCL_STATIC);
	    goto done;
	}
	hr = MFGetStrideForBitmapInfoHeader(subType.Data1, w, &lval);
	if (!SUCCEEDED(hr)) {
	    Tcl_SetResult(interp, "cannot get stride", TCL_STATIC);
	    goto done;
	}
	mediaType->lpVtbl->SetUINT32(mediaType, &MF_MT_DEFAULT_STRIDE,
				     (UINT32) lval);
	*stride = lval;
	*width = w;
	*height = h;
    } else {
	*stride = uval;
	hr = mediaType->lpVtbl->GetUINT64(mediaType, &MF_MT_FRAME_SIZE, &wval);
	if (!SUCCEEDED(hr)) {
	    Tcl_SetResult(interp, "cannot MF_MT_FRAME_SIZE", TCL_STATIC);
	    goto done;
	}
	w = wval >> 32;
	h = wval;
	*width = w;
	*height = h;
    }
    result = TCL_OK;
done:
    if (mediaType != NULL) {
	mediaType->lpVtbl->Release(mediaType);
    }
    return result;
}







|

<
|




|
|





>

|
|

















>
>
>
>



>
>
>
>



>
>
>
|
>


>
|
>
>
>
|
>
>
>
>
>
|
>
>
|
>
>
>
|
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
|
>
>
|
>
>
|
>
>

>

>
>
>
>
>











>
>
>
>
>
|














|
<
<


>
>



<


<





|



<
<


<
<
<
<
<
<
<
<
|
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<



|
>


|
<
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
<
|
|
|
<
<
<
<
<
<
<
<
<
<
<
<







703
704
705
706
707
708
709
710
711

712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848


849
850
851
852
853
854
855

856
857

858
859
860
861
862
863
864
865
866


867
868








869

870























871
872
873
874
875
876
877
878


879
880
881
882
883
884
885
886
887
888
889
890
891
892


893
894
895












896
897
898
899
900
901
902
    }
    return hr;
}

/*
 *-------------------------------------------------------------------------
 *
 * GetFormatList --
 *

 *	Return allocated array of usable formats.
 *
 *-------------------------------------------------------------------------
 */

static MediaFmt *
GetFormatList(IMFMediaSource *mediaSrc, int *numFmtsRet)
{
    IMFPresentationDescriptor *pd = NULL;
    IMFStreamDescriptor *sd = NULL;
    IMFMediaTypeHandler *handler = NULL;
    IMFMediaType *mediaType = NULL;
    MediaFmt *fmts = NULL;
    BOOL selected;
    DWORD count = 0;
    int i, numFmts = 0;
    HRESULT hr;

    hr = mediaSrc->lpVtbl->CreatePresentationDescriptor(mediaSrc, &pd);
    if (!SUCCEEDED(hr)) {
	goto done;
    }
    hr = pd->lpVtbl->GetStreamDescriptorByIndex(pd, 0, &selected, &sd);
    if (!SUCCEEDED(hr)) {
	goto done;
    }
    hr = sd->lpVtbl->GetMediaTypeHandler(sd, &handler);
    if (!SUCCEEDED(hr)) {
	goto done;
    }
    hr = handler->lpVtbl->GetMediaTypeCount(handler, &count);
    if (!SUCCEEDED(hr)) {
	goto done;
    }
    fmts = (MediaFmt *) attemptckalloc(sizeof (MediaFmt) * count);
    if (fmts == NULL) {
	goto done;
    }
    for (i = 0; i < count; i++) {
	GUID subType = GUID_NULL;
	UINT64 wval;
	LONG lval;
	int w, h;
	BOOL isCompressed = TRUE;

	hr = handler->lpVtbl->GetMediaTypeByIndex(handler, i, &mediaType);
	if (!SUCCEEDED(hr)) {
	    goto skip;
	}
	mediaType->lpVtbl->IsCompressedFormat(mediaType, &isCompressed);
	if (isCompressed) {
	    goto skip;
	}
	hr = mediaType->lpVtbl->GetGUID(mediaType, &MF_MT_SUBTYPE, &subType);
	if (SUCCEEDED(hr) && ((subType.Data1 == FOURCC_NV12) ||
			      (subType.Data1 == FOURCC_YUY2))) {
	    fmts[numFmts].index = i;
	    fmts[numFmts].fourcc = subType.Data1;
	    wval = 0;
	    hr = mediaType->lpVtbl->GetUINT64(mediaType, &MF_MT_FRAME_SIZE,
					      &wval);
	    if (!SUCCEEDED(hr)) {
		continue;
	    }
	    w = wval >> 32;
	    h = wval;
	    if ((w == 0) || (h == 0)) {
		continue;
	    }
	    fmts[numFmts].width = w;
	    fmts[numFmts].height = h;
	    hr = MFGetStrideForBitmapInfoHeader(subType.Data1, w, &lval);
	    if (!SUCCEEDED(hr)) {
		continue;
	    }
	    fmts[numFmts].stride = lval;
	    wval = 0;
	    hr = mediaType->lpVtbl->GetUINT64(mediaType, &MF_MT_FRAME_RATE,
					      &wval);
	    if (!SUCCEEDED(hr)) {
		continue;
	    }
	    fmts[numFmts].frameRate = wval;
	    fmts[numFmts].frameRateMin = fmts[numFmts].frameRateMax = wval;
	    wval = 0;
	    hr = mediaType->lpVtbl->GetUINT64(mediaType,
					      &MF_MT_FRAME_RATE_RANGE_MIN,
					      &wval);
	    if (SUCCEEDED(hr)) {
		fmts[numFmts].frameRateMin = wval;
	    }
	    wval = 0;
	    hr = mediaType->lpVtbl->GetUINT64(mediaType,
					      &MF_MT_FRAME_RATE_RANGE_MAX,
					      &wval);
	    if (SUCCEEDED(hr)) {
		fmts[numFmts].frameRateMax = wval;
	    }
	    ++numFmts;
	}
skip:
	if (mediaType != NULL) {
	    mediaType->lpVtbl->Release(mediaType);
	    mediaType = NULL;
	}
    }
done:
    if (handler != NULL) {
	handler->lpVtbl->Release(handler);
    }
    if (sd != NULL) {
	sd->lpVtbl->Release(sd);
    }
    if (pd != NULL) {
	pd->lpVtbl->Release(pd);
    }
    if ((numFmts <= 0) && (fmts != NULL)) {
	ckfree((char *) fmts);
	fmts = NULL;
    }
    *numFmtsRet = numFmts;
    return fmts;
}

/*
 *-------------------------------------------------------------------------
 *
 * GetFormat --
 *
 *	Get information about the media format (width, height etc.)
 *	and try to switch MJPG source to YUY2 or NV12 format.
 *
 *-------------------------------------------------------------------------
 */

static int
GetFormat(WMFC *wmfc)


{
    HRESULT hr;
    Tcl_Interp *interp = wmfc->interp;
    IMFSourceReader *srcReader = wmfc->srcReader;
    IMFMediaType *mediaType = NULL;
    GUID subType = GUID_NULL;
    LONG lval = 0;

    UINT64 wval = 0;
    UINT32 w = 0, h = 0;

    int result = TCL_ERROR;

    hr = srcReader->lpVtbl->GetCurrentMediaType(srcReader,
		MF_SOURCE_READER_FIRST_VIDEO_STREAM, &mediaType);
    if (!SUCCEEDED(hr)) {
	Tcl_SetResult(interp, "error getting media type from source reader",
		      TCL_STATIC);
	goto done;
    }


    hr = mediaType->lpVtbl->GetGUID(mediaType, &MF_MT_SUBTYPE, &subType);
    if (!SUCCEEDED(hr)) {








	Tcl_SetResult(interp, "error getting media format", TCL_STATIC);

	goto done;























    }
    if ((subType.Data1 != FOURCC_NV12) && (subType.Data1 != FOURCC_YUY2)) {
	Tcl_SetObjResult(interp,
			 Tcl_ObjPrintf("unsupported format code 0x%08lx",
				       subType.Data1));
	goto done;
    }
    wmfc->fourcc = subType.Data1;


    hr = mediaType->lpVtbl->GetUINT64(mediaType, &MF_MT_FRAME_SIZE, &wval);
    if (SUCCEEDED(hr)) {
	w = wval >> 32;
	h = wval;
    }
    if (!SUCCEEDED(hr)) {
	Tcl_SetResult(interp, "error getting frame size", TCL_STATIC);
	goto done;
    }
    hr = MFGetStrideForBitmapInfoHeader(subType.Data1, w, &lval);
    if (!SUCCEEDED(hr)) {
	Tcl_SetResult(interp, "error getting stride", TCL_STATIC);
	goto done;
    }


    wmfc->stride = lval;
    wmfc->width = w;
    wmfc->height = h;












    result = TCL_OK;
done:
    if (mediaType != NULL) {
	mediaType->lpVtbl->Release(mediaType);
    }
    return result;
}
894
895
896
897
898
899
900
901
902
903
904
905
906

907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926



927


928



929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947

948
949
950
951
952
953
954
    IMFAttributes *attr = NULL;
    HRESULT hr;
    int doread = 0;

    if (wmfc->srcReader == NULL) {
	hr = MFCreateAttributes(&attr, 1);
	if (!SUCCEEDED(hr)) {
	    Tcl_SetResult(interp, "cannot create WMF attributes", TCL_STATIC);
	    return TCL_ERROR;
	}
	wmfc->counters[0] = wmfc->counters[1] = 0;
	wmfc->counters[2] = wmfc->counters[3] = 0;
	wmfc->frame[0].ready = wmfc->frame[1].ready = 0;

	wmfc->frameReady = wmfc->frameQueued = -1;
	wmfc->srcb.isrcb.lpVtbl = &SourceReaderVtbl;
	wmfc->srcb.refCount = 1;
	wmfc->srcb.wmfc = wmfc;
	hr = attr->lpVtbl->SetUnknown(attr,
				      &MF_SOURCE_READER_ASYNC_CALLBACK,
				      (struct IUnknown *) &wmfc->srcb);
	hr = MFCreateSourceReaderFromMediaSource(wmfc->mediaSrc, attr,
						 &wmfc->srcReader);
	if (attr != NULL) {
	    attr->lpVtbl->Release(attr);
	}
	if (!SUCCEEDED(hr)) {
	    if (wmfc->srcReader != NULL) {
		wmfc->srcReader->lpVtbl->Release(wmfc->srcReader);
		wmfc->srcReader = NULL;
	    }
	    Tcl_SetResult(interp, "cannot create source reader", TCL_STATIC);
	    return TCL_ERROR;
	}



	if (GetFormat(interp, wmfc->mediaSrc, wmfc->srcReader,


		      &wmfc->fourcc, &wmfc->compr,



		      &wmfc->stride, &wmfc->width, &wmfc->height) != TCL_OK) {
 	    wmfc->srcReader->lpVtbl->Release(wmfc->srcReader);
	    wmfc->srcReader = NULL;
	    return TCL_ERROR;
	}
    }
    EnterCriticalSection(&wmfc->srcb.lock);
#ifdef USE_ASYNC_HANDLER
    if (wmfc->async == NULL) {
	wmfc->async = Tcl_AsyncCreate(SignalBuffer, (ClientData) wmfc);
	doread = 1;
    }
#else
    if (wmfc->tid == NULL) {
	wmfc->tid = Tcl_GetCurrentThread();
	doread = 1;
    }
#endif
    if (doread) {

	wmfc->counters[0] = wmfc->counters[1] = 0;
	wmfc->counters[2] = wmfc->counters[3] = 0;
	wmfc->frameQueued = wmfc->frameReady ? 0 : 1;
	wmfc->frame[wmfc->frameQueued].ready = 0;
	hr = wmfc->srcReader->lpVtbl->ReadSample(wmfc->srcReader,
						 MF_SOURCE_READER_ANY_STREAM,
						 0, NULL, NULL, NULL, NULL);







|





>

















|


>
>
>
|
>
>
|
>
>
>
|


















>







921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
    IMFAttributes *attr = NULL;
    HRESULT hr;
    int doread = 0;

    if (wmfc->srcReader == NULL) {
	hr = MFCreateAttributes(&attr, 1);
	if (!SUCCEEDED(hr)) {
	    Tcl_SetResult(interp, "error creating WMF attributes", TCL_STATIC);
	    return TCL_ERROR;
	}
	wmfc->counters[0] = wmfc->counters[1] = 0;
	wmfc->counters[2] = wmfc->counters[3] = 0;
	wmfc->frame[0].ready = wmfc->frame[1].ready = 0;
	wmfc->streamEnd = 0;
	wmfc->frameReady = wmfc->frameQueued = -1;
	wmfc->srcb.isrcb.lpVtbl = &SourceReaderVtbl;
	wmfc->srcb.refCount = 1;
	wmfc->srcb.wmfc = wmfc;
	hr = attr->lpVtbl->SetUnknown(attr,
				      &MF_SOURCE_READER_ASYNC_CALLBACK,
				      (struct IUnknown *) &wmfc->srcb);
	hr = MFCreateSourceReaderFromMediaSource(wmfc->mediaSrc, attr,
						 &wmfc->srcReader);
	if (attr != NULL) {
	    attr->lpVtbl->Release(attr);
	}
	if (!SUCCEEDED(hr)) {
	    if (wmfc->srcReader != NULL) {
		wmfc->srcReader->lpVtbl->Release(wmfc->srcReader);
		wmfc->srcReader = NULL;
	    }
	    Tcl_SetResult(interp, "error creating source reader", TCL_STATIC);
	    return TCL_ERROR;
	}
	if (wmfc->useFmt < 0) {
	    wmfc->useFmt = wmfc->fmts[0].index;
	}
	hr = SetFormat(wmfc->mediaSrc, wmfc->srcReader, wmfc->useFmt);
	if (!SUCCEEDED(hr)) {
 	    wmfc->srcReader->lpVtbl->Release(wmfc->srcReader);
	    wmfc->srcReader = NULL;
	    Tcl_SetResult(interp, "error setting media format", TCL_STATIC);
	    return TCL_ERROR;
	}
	if (GetFormat(wmfc) != TCL_OK) {
 	    wmfc->srcReader->lpVtbl->Release(wmfc->srcReader);
	    wmfc->srcReader = NULL;
	    return TCL_ERROR;
	}
    }
    EnterCriticalSection(&wmfc->srcb.lock);
#ifdef USE_ASYNC_HANDLER
    if (wmfc->async == NULL) {
	wmfc->async = Tcl_AsyncCreate(SignalBuffer, (ClientData) wmfc);
	doread = 1;
    }
#else
    if (wmfc->tid == NULL) {
	wmfc->tid = Tcl_GetCurrentThread();
	doread = 1;
    }
#endif
    if (doread) {
	wmfc->streamEnd = 0;
	wmfc->counters[0] = wmfc->counters[1] = 0;
	wmfc->counters[2] = wmfc->counters[3] = 0;
	wmfc->frameQueued = wmfc->frameReady ? 0 : 1;
	wmfc->frame[wmfc->frameQueued].ready = 0;
	hr = wmfc->srcReader->lpVtbl->ReadSample(wmfc->srcReader,
						 MF_SOURCE_READER_ANY_STREAM,
						 0, NULL, NULL, NULL, NULL);
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
#endif
	}
    } else {
	hr = S_OK;
    }
    LeaveCriticalSection(&wmfc->srcb.lock);
    if (!SUCCEEDED(hr)) {
	Tcl_SetResult(interp, "cannot initiate read from source reader",
		      TCL_STATIC);
	return TCL_ERROR;
    }
    return TCL_OK;
}

/*







|







999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
#endif
	}
    } else {
	hr = S_OK;
    }
    LeaveCriticalSection(&wmfc->srcb.lock);
    if (!SUCCEEDED(hr)) {
	Tcl_SetResult(interp, "error starting read from source reader",
		      TCL_STATIC);
	return TCL_ERROR;
    }
    return TCL_OK;
}

/*
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000

1001
1002
1003

1004
1005
1006



1007




1008
1009

1010
1011
1012
1013
1014
1015
1016
 *-------------------------------------------------------------------------
 */

static int
StopCapture(WMFC *wmfc)
{
#ifdef USE_ASYNC_HANDLER
    int docancel = 0;

    EnterCriticalSection(&wmfc->srcb.lock);
    if (wmfc->async != NULL) {
        Tcl_AsyncDelete(wmfc->async);
        wmfc->async = NULL;
	docancel = 1;
    }
    wmfc->frameQueued = -1;
    LeaveCriticalSection(&wmfc->srcb.lock);

    if (docancel) {
	Tcl_CancelIdleCall(ImageCallback, (ClientData) wmfc);
    }

#else
    EnterCriticalSection(&wmfc->srcb.lock);
    wmfc->tid = NULL;



    wmfc->frameQueued = -1;




    LeaveCriticalSection(&wmfc->srcb.lock);
#endif

    return TCL_OK;
}

/*
 *-------------------------------------------------------------------------
 *
 * GetImage --







<
<




<


|
>
|
<

>



>
>
>

>
>
>
>


>







1021
1022
1023
1024
1025
1026
1027


1028
1029
1030
1031

1032
1033
1034
1035
1036

1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
 *-------------------------------------------------------------------------
 */

static int
StopCapture(WMFC *wmfc)
{
#ifdef USE_ASYNC_HANDLER


    EnterCriticalSection(&wmfc->srcb.lock);
    if (wmfc->async != NULL) {
        Tcl_AsyncDelete(wmfc->async);
        wmfc->async = NULL;

    }
    wmfc->frameQueued = -1;
    if (wmfc->srcReader != NULL) {
	wmfc->srcReader->lpVtbl->Flush(wmfc->srcReader,
				       MF_SOURCE_READER_ALL_STREAMS);

    }
    LeaveCriticalSection(&wmfc->srcb.lock);
#else
    EnterCriticalSection(&wmfc->srcb.lock);
    wmfc->tid = NULL;
    if (wmfc->streamEnd < 0) {
	wmfc->streamEnd = 0;
    }
    wmfc->frameQueued = -1;
    if (wmfc->srcReader != NULL) {
	wmfc->srcReader->lpVtbl->Flush(wmfc->srcReader,
				       MF_SOURCE_READER_ALL_STREAMS);
    }
    LeaveCriticalSection(&wmfc->srcb.lock);
#endif
    Tcl_CancelIdleCall(ImageCallback, (ClientData) wmfc);
    return TCL_OK;
}

/*
 *-------------------------------------------------------------------------
 *
 * GetImage --
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
{
    WMFI *wmfi = (WMFI *) clientData;
    WMFC *wmfc;
    Tcl_HashEntry *hPtr;
    int ret = TCL_OK, command;

    static const char *cmdNames[] = {
	"close", "counters", "devices", "image", "info",
	"mirror", "open", "orientation", "parameters",
	"start", "state", "stop", NULL
    };
    enum cmdCode {
	CMD_close, CMD_counters, CMD_devices, CMD_image, CMD_info,
	CMD_mirror, CMD_open, CMD_orientation, CMD_parameters,
	CMD_start, CMD_state, CMD_stop
    };

    if (objc < 2) {
	Tcl_WrongNumArgs(interp, 1, objv, "option ...");
	return TCL_ERROR;
    }







|
|



|
|







1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
{
    WMFI *wmfi = (WMFI *) clientData;
    WMFC *wmfc;
    Tcl_HashEntry *hPtr;
    int ret = TCL_OK, command;

    static const char *cmdNames[] = {
	"close", "counters", "devices", "format", "image",
	"info",	"listformats", "mirror", "open", "orientation",
	"start", "state", "stop", NULL
    };
    enum cmdCode {
	CMD_close, CMD_counters, CMD_devices, CMD_format, CMD_image,
	CMD_info, CMD_listformats, CMD_mirror, CMD_open, CMD_orientation,
	CMD_start, CMD_state, CMD_stop
    };

    if (objc < 2) {
	Tcl_WrongNumArgs(interp, 1, objv, "option ...");
	return TCL_ERROR;
    }
1508
1509
1510
1511
1512
1513
1514



1515
1516
1517
1518
1519
1520
1521
	    ckfree((char *) wmfc->frame[0].data);
	}
	if (wmfc->frame[1].data != NULL) {
	    ckfree((char *) wmfc->frame[1].data);
	}
	Tcl_DStringFree(&wmfc->devName);
	Tcl_DStringFree(&wmfc->cbCmd);



	DeleteCriticalSection(&wmfc->srcb.lock);
	ckfree((char *) wmfc);
	break;
    }

    case CMD_counters: {
	if (objc != 3) {







>
>
>







1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
	    ckfree((char *) wmfc->frame[0].data);
	}
	if (wmfc->frame[1].data != NULL) {
	    ckfree((char *) wmfc->frame[1].data);
	}
	Tcl_DStringFree(&wmfc->devName);
	Tcl_DStringFree(&wmfc->cbCmd);
	if (wmfc->fmts != NULL) {
	    ckfree((char *) wmfc->fmts);
	}
	DeleteCriticalSection(&wmfc->srcb.lock);
	ckfree((char *) wmfc);
	break;
    }

    case CMD_counters: {
	if (objc != 3) {
1543
1544
1545
1546
1547
1548
1549































































1550
1551
1552
1553
1554
1555
1556
	if (objc != 2) {
	    Tcl_WrongNumArgs(interp, 2, objv, NULL);
	    return TCL_ERROR;
	}
	ret = ListDevices(interp);
	break;
    }
































































    case CMD_image: {
	if ((objc < 3) || (objc > 4)) {
	    Tcl_WrongNumArgs(interp, 2, objv, "devid ?photoImage?");
	    return TCL_ERROR;
	}
	hPtr = Tcl_FindHashEntry(&wmfi->wmfc, Tcl_GetString(objv[2]));







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
	if (objc != 2) {
	    Tcl_WrongNumArgs(interp, 2, objv, NULL);
	    return TCL_ERROR;
	}
	ret = ListDevices(interp);
	break;
    }

    case CMD_format: {
	if ((objc < 3) || (objc > 4)) {
	    Tcl_WrongNumArgs(interp, 2, objv, "devid ?number?");
	    return TCL_ERROR;
	}
	hPtr = Tcl_FindHashEntry(&wmfi->wmfc, Tcl_GetString(objv[2]));
	if (hPtr == NULL) {
	    goto devNotFound;
	}
	wmfc = (WMFC *) Tcl_GetHashValue(hPtr);
	if (objc > 3) {
	    int i, k, fmtOk = 0, isRunning = 0;

	    if (Tcl_GetIntFromObj(interp, objv[3], &k) != TCL_OK) {
		return TCL_ERROR;
	    }
#ifdef USE_ASYNC_HANDLER
	    isRunning = wmfc->async != NULL;
#else
	    isRunning = wmfc->tid != NULL;
#endif
	    if (isRunning) {
		Tcl_SetResult(interp, "capture still running", TCL_STATIC);
		return TCL_ERROR;
	    }
	    for (i = 0; i < wmfc->numFmts; i++) {
		if (wmfc->fmts[i].index == k) {
		    HRESULT hr;

		    if (wmfc->srcReader == NULL) {
			wmfc->useFmt = k;
			fmtOk = 1;
			break;
		    }
		    hr = SetFormat(wmfc->mediaSrc, wmfc->srcReader, k);
		    if (SUCCEEDED(hr)) {
			if (GetFormat(wmfc) == TCL_OK) {
			    wmfc->useFmt = k;
			    fmtOk = 1;
			} else {
			    fmtOk = -1;
			}
		    } else {
			Tcl_SetObjResult(interp,
				 Tcl_ObjPrintf("set format %d failed", k));
			fmtOk = -1;
		    }
		    break;
		}
	    }
	    if (fmtOk == 0) {
		Tcl_SetObjResult(interp,
				 Tcl_ObjPrintf("format %d not found", k));
	    }
	    ret = (fmtOk <= 0) ? TCL_ERROR : TCL_OK;
	} else {
	    int i = (wmfc->useFmt < 0) ? wmfc->fmts[0].index : wmfc->useFmt;

	    Tcl_SetObjResult(interp, Tcl_NewIntObj(i));
	}
 	break;
    }

    case CMD_image: {
	if ((objc < 3) || (objc > 4)) {
	    Tcl_WrongNumArgs(interp, 2, objv, "devid ?photoImage?");
	    return TCL_ERROR;
	}
	hPtr = Tcl_FindHashEntry(&wmfi->wmfc, Tcl_GetString(objv[2]));
1592
1593
1594
1595
1596
1597
1598



























































1599
1600
1601
1602
1603
1604
1605
		Tcl_SetObjResult(interp, Tcl_NewListObj(2, r));
	    } else {
		goto devNotFound;
	    }
	}
	break;
    }




























































    case CMD_mirror: {
	int x, y;

	if ((objc != 3) && (objc != 5)) {
	    Tcl_WrongNumArgs(interp, 3, objv, "?x y?");
	    return TCL_ERROR;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
		Tcl_SetObjResult(interp, Tcl_NewListObj(2, r));
	    } else {
		goto devNotFound;
	    }
	}
	break;
    }

    case CMD_listformats: {
	Tcl_Obj *dict;
	int i;

	if ((objc < 3) || (objc % 2 == 0)) {
	    Tcl_WrongNumArgs(interp, 2, objv, "devid ?key value ...?");
	    return TCL_ERROR;
	}
	hPtr = Tcl_FindHashEntry(&wmfi->wmfc, Tcl_GetString(objv[2]));
	if (hPtr == NULL) {
	    goto devNotFound;
	}
	wmfc = (WMFC *) Tcl_GetHashValue(hPtr);
	dict = Tcl_NewDictObj();
	for (i = 0; i < wmfc->numFmts; i++) {
	    Tcl_DString ds;
	    char buffer[64];

	    Tcl_DStringInit(&ds);
	    Tcl_DStringAppendElement(&ds, "frame-size");
	    sprintf(buffer, "%dx%d", wmfc->fmts[i].width,
		    wmfc->fmts[i].height);
	    Tcl_DStringAppendElement(&ds, buffer);
	    Tcl_DStringAppendElement(&ds, "stride");
	    sprintf(buffer, "%d", wmfc->fmts[i].stride);
	    Tcl_DStringAppendElement(&ds, buffer);
	    Tcl_DStringAppendElement(&ds, "fourcc");
	    switch (wmfc->fmts[i].fourcc) {
	    case FOURCC_NV12:
		Tcl_DStringAppendElement(&ds, "NV12");
		break;
	    case FOURCC_YUY2:
		Tcl_DStringAppendElement(&ds, "YUY2");
		break;
	    default:
		Tcl_DStringAppendElement(&ds, "????");
		break;
	    }
	    Tcl_DStringAppendElement(&ds, "frame-rate");
	    sprintf(buffer, "%d/%d", (int) (wmfc->fmts[i].frameRate >> 32),
		    (int) (wmfc->fmts[i].frameRate & 0xffffffff));
	    Tcl_DStringAppendElement(&ds, buffer);
	    Tcl_DStringAppendElement(&ds, "frame-rate-min");
	    sprintf(buffer, "%d/%d", (int) (wmfc->fmts[i].frameRateMin >> 32),
		    (int) (wmfc->fmts[i].frameRateMin & 0xffffffff));
	    Tcl_DStringAppendElement(&ds, buffer);
	    Tcl_DStringAppendElement(&ds, "frame-rate-max");
	    sprintf(buffer, "%d/%d", (int) (wmfc->fmts[i].frameRateMax >> 32),
		    (int) (wmfc->fmts[i].frameRateMax & 0xffffffff));
	    Tcl_DStringAppendElement(&ds, buffer);
	    Tcl_DictObjPut(NULL, dict, Tcl_NewIntObj(wmfc->fmts[i].index),
			   Tcl_NewStringObj(Tcl_DStringValue(&ds),
					    Tcl_DStringLength(&ds)));
	    Tcl_DStringFree(&ds);
	}
	Tcl_SetObjResult(interp, dict);
 	break;
    }

    case CMD_mirror: {
	int x, y;

	if ((objc != 3) && (objc != 5)) {
	    Tcl_WrongNumArgs(interp, 3, objv, "?x y?");
	    return TCL_ERROR;
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633

1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653







1654
1655
1656
1657
1658
1659

1660
1661
1662
1663
1664
1665
1666
1667
1668



1669
1670
1671
1672
1673
1674
1675
	    Tcl_SetObjResult(interp, Tcl_NewListObj(2, list));
	}
	break;
    }

    case CMD_open: {
	char *devName;
	int isNew;
	IMFMediaSource *mediaSrc;
	Tcl_HashSearch search;


	if (objc != 4) {
	    Tcl_WrongNumArgs(interp, 2, objv, "device callback");
	    return TCL_ERROR;
	}
	devName = Tcl_GetString(objv[2]);
	hPtr = Tcl_FirstHashEntry(&wmfi->wmfc, &search);
	while (hPtr != NULL) {
	    wmfc = (WMFC *) Tcl_GetHashValue(hPtr);
	    if (strcmp(Tcl_DStringValue(&wmfc->devName), devName) == 0) {
		Tcl_SetObjResult(interp,
			Tcl_ObjPrintf("\"%s\" is already opened", devName));
		return TCL_ERROR;
	    }
	    hPtr = Tcl_NextHashEntry(&search);
	}
	mediaSrc = GetSource(devName);
	if (mediaSrc == NULL) {
	    Tcl_SetObjResult(interp,
		Tcl_ObjPrintf("cannot open \"%s\"", devName));







	    return TCL_ERROR;
	}
	wmfc = (WMFC *) ckalloc(sizeof (WMFC));
	memset(wmfc, 0, sizeof (WMFC));
	wmfc->mediaSrc = mediaSrc;
	wmfc->srcReader = NULL;

	wmfc->frameReady = wmfc->frameQueued = -1;
        wmfc->mirror = 0;
        wmfc->rotate = 0;
        wmfc->interp = interp;
	InitializeCriticalSection(&wmfc->srcb.lock);
 	Tcl_DStringInit(&wmfc->devName);
	Tcl_DStringAppend(&wmfc->devName, devName, -1);
	Tcl_DStringInit(&wmfc->cbCmd);
	Tcl_DStringAppend(&wmfc->cbCmd, Tcl_GetString(objv[3]), -1);



	wmfc->cbCmdLen = Tcl_DStringLength(&wmfc->cbCmd);
	sprintf(wmfc->devId, "wmfdev%d", wmfi->idCount++);
	hPtr = Tcl_CreateHashEntry(&wmfi->wmfc, wmfc->devId, &isNew);
	Tcl_SetHashValue(hPtr, (ClientData) wmfc);
	Tcl_SetObjResult(interp, Tcl_NewStringObj(wmfc->devId, -1));
	break;
    }







|


>



















|
>
>
>
>
>
>
>






>









>
>
>







1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
	    Tcl_SetObjResult(interp, Tcl_NewListObj(2, list));
	}
	break;
    }

    case CMD_open: {
	char *devName;
	int isNew, numFmts = 0;
	IMFMediaSource *mediaSrc;
	Tcl_HashSearch search;
	MediaFmt *fmts = NULL;

	if (objc != 4) {
	    Tcl_WrongNumArgs(interp, 2, objv, "device callback");
	    return TCL_ERROR;
	}
	devName = Tcl_GetString(objv[2]);
	hPtr = Tcl_FirstHashEntry(&wmfi->wmfc, &search);
	while (hPtr != NULL) {
	    wmfc = (WMFC *) Tcl_GetHashValue(hPtr);
	    if (strcmp(Tcl_DStringValue(&wmfc->devName), devName) == 0) {
		Tcl_SetObjResult(interp,
			Tcl_ObjPrintf("\"%s\" is already opened", devName));
		return TCL_ERROR;
	    }
	    hPtr = Tcl_NextHashEntry(&search);
	}
	mediaSrc = GetSource(devName);
	if (mediaSrc == NULL) {
	    Tcl_SetObjResult(interp,
		Tcl_ObjPrintf("error opening \"%s\"", devName));
	    return TCL_ERROR;
	}
	fmts = GetFormatList(mediaSrc, &numFmts);
	if (fmts == NULL) {
	    mediaSrc->lpVtbl->Release(mediaSrc);
	    Tcl_SetObjResult(interp,
		Tcl_ObjPrintf("error getting format list for \"%s\"", devName));
	    return TCL_ERROR;
	}
	wmfc = (WMFC *) ckalloc(sizeof (WMFC));
	memset(wmfc, 0, sizeof (WMFC));
	wmfc->mediaSrc = mediaSrc;
	wmfc->srcReader = NULL;
	wmfc->streamEnd = 0;
	wmfc->frameReady = wmfc->frameQueued = -1;
        wmfc->mirror = 0;
        wmfc->rotate = 0;
        wmfc->interp = interp;
	InitializeCriticalSection(&wmfc->srcb.lock);
 	Tcl_DStringInit(&wmfc->devName);
	Tcl_DStringAppend(&wmfc->devName, devName, -1);
	Tcl_DStringInit(&wmfc->cbCmd);
	Tcl_DStringAppend(&wmfc->cbCmd, Tcl_GetString(objv[3]), -1);
	wmfc->useFmt = -1;
	wmfc->numFmts = numFmts;
	wmfc->fmts = fmts;
	wmfc->cbCmdLen = Tcl_DStringLength(&wmfc->cbCmd);
	sprintf(wmfc->devId, "wmfdev%d", wmfi->idCount++);
	hPtr = Tcl_CreateHashEntry(&wmfi->wmfc, wmfc->devId, &isNew);
	Tcl_SetHashValue(hPtr, (ClientData) wmfc);
	Tcl_SetObjResult(interp, Tcl_NewStringObj(wmfc->devId, -1));
	break;
    }
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733


1734
1735
1736
1737
1738
1739
1740
1741
1742



1743
1744
1745
1746
1747
1748
1749


1750
1751
1752
1753
1754
1755
1756
	    }
	} else {
	    Tcl_SetObjResult(interp, Tcl_NewIntObj(wmfc->rotate));
	}
	break;
    }

    case CMD_parameters: {
	if ((objc < 3) || (objc % 2 == 0)) {
	    Tcl_WrongNumArgs(interp, 2, objv, "devid ?key value ...?");
	    return TCL_ERROR;
	}
 	break;
    }

    case CMD_start: {
	if (objc != 3) {
	    Tcl_WrongNumArgs(interp, 2, objv, "devid");
	    return TCL_ERROR;
	}
	hPtr = Tcl_FindHashEntry(&wmfi->wmfc, Tcl_GetString(objv[2]));
	if (hPtr == NULL) {
	    goto devNotFound;
	}
	wmfc = (WMFC *) Tcl_GetHashValue(hPtr);
	ret = StartCapture(wmfc);
	break;
    }

    case CMD_state: {


	if (objc != 3) {
	    Tcl_WrongNumArgs(interp, 2, objv, "devid");
	    return TCL_ERROR;
	}
	hPtr = Tcl_FindHashEntry(&wmfi->wmfc, Tcl_GetString(objv[2]));
	if (hPtr == NULL) {
	    goto devNotFound;
	}
	wmfc = (WMFC *) Tcl_GetHashValue(hPtr);



	Tcl_SetResult(interp,
#ifdef USE_ASYNC_HANDLER
		      (wmfc->async != NULL)
#else
		      (wmfc->tid != NULL)
#endif
		      ? "capture" : "stop", TCL_STATIC);


	break;
    }

    case CMD_stop: {
	if (objc != 3) {
	    Tcl_WrongNumArgs(interp, 2, objv, "devid");
	    return TCL_ERROR;







<
<
<
<
<
<
<
<















>
>









>
>
>
|

|

|

|
>
>







1884
1885
1886
1887
1888
1889
1890








1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
	    }
	} else {
	    Tcl_SetObjResult(interp, Tcl_NewIntObj(wmfc->rotate));
	}
	break;
    }









    case CMD_start: {
	if (objc != 3) {
	    Tcl_WrongNumArgs(interp, 2, objv, "devid");
	    return TCL_ERROR;
	}
	hPtr = Tcl_FindHashEntry(&wmfi->wmfc, Tcl_GetString(objv[2]));
	if (hPtr == NULL) {
	    goto devNotFound;
	}
	wmfc = (WMFC *) Tcl_GetHashValue(hPtr);
	ret = StartCapture(wmfc);
	break;
    }

    case CMD_state: {
	char *state;

	if (objc != 3) {
	    Tcl_WrongNumArgs(interp, 2, objv, "devid");
	    return TCL_ERROR;
	}
	hPtr = Tcl_FindHashEntry(&wmfi->wmfc, Tcl_GetString(objv[2]));
	if (hPtr == NULL) {
	    goto devNotFound;
	}
	wmfc = (WMFC *) Tcl_GetHashValue(hPtr);
	if (wmfc->streamEnd) {
	    state = (wmfc->streamEnd < 0) ? "error" : "eof";
	} else {
	    state =
#ifdef USE_ASYNC_HANDLER
		(wmfc->async != NULL)
#else
		(wmfc->tid != NULL)
#endif
		? "capture" : "stop";
	}
	Tcl_SetResult(interp, state, TCL_STATIC);
	break;
    }

    case CMD_stop: {
	if (objc != 3) {
	    Tcl_WrongNumArgs(interp, 2, objv, "devid");
	    return TCL_ERROR;