@@ -805,6 +805,14 @@ static void vidcap_dshow_should_exit(void *state) {
805
805
}
806
806
807
807
static int vidcap_dshow_init (struct vidcap_params *params, void **state) {
808
+ #define HANDLE_ERR (res, msg, ...) \
809
+ do { \
810
+ if (res != S_OK) { \
811
+ MSG (ERROR, " vidcap_dshow_init: " msg " : %s\n " , \
812
+ __VA_ARGS__ __VA_OPT__ (, ) hresult_to_str (res)); \
813
+ goto error; \
814
+ } \
815
+ } while (0 )
808
816
struct vidcap_dshow_state *s;
809
817
HRESULT res;
810
818
@@ -873,69 +881,42 @@ static int vidcap_dshow_init(struct vidcap_params *params, void **state) {
873
881
<< get_friendly_name (s->moniker ) << " \n " ;
874
882
875
883
res = s->moniker ->BindToObject (NULL , NULL , IID_IBaseFilter, (void **) &s->captureFilter );
876
- if (res != S_OK) {
877
- log_msg (LOG_LEVEL_ERROR, MOD_NAME " vidcap_dshow_init: Cannot bind capture filter to device.\n " );
878
- goto error;
879
- }
884
+ HANDLE_ERR (res, " Cannot bind capture filter to device" );
880
885
881
886
res = s->filterGraph ->AddFilter (s->captureFilter , L" Capture filter" );
882
- if (res != S_OK) {
883
- log_msg (LOG_LEVEL_ERROR, MOD_NAME " vidcap_dshow_init: Cannot add capture filter to filter graph.\n " );
884
- goto error;
885
- }
887
+ HANDLE_ERR (res, " Cannot add capture filter to filter graph" );
886
888
887
889
res = s->graphBuilder ->FindInterface (&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video, s->captureFilter ,
888
890
IID_IAMStreamConfig, (void **) &s->streamConfig );
889
- if (res != S_OK) {
890
- log_msg (LOG_LEVEL_ERROR, MOD_NAME " vidcap_dshow_init: Cannot find interface for reading capture capabilites.\n " );
891
- goto error;
892
- }
891
+ HANDLE_ERR (res, " Cannot find interface for reading capture capabilites" );
893
892
894
893
// create instance of sample grabber
895
894
res = CoCreateInstance (CLSID_SampleGrabber, NULL , CLSCTX_INPROC_SERVER, IID_PPV_ARGS (&s->sampleGrabberFilter ));
896
- if (res != S_OK) {
897
- log_msg (LOG_LEVEL_ERROR, MOD_NAME " vidcap_dshow_init: Cannot create instance of sample grabber.\n " );
898
- goto error;
899
- }
895
+ HANDLE_ERR (res, " Cannot create instance of sample grabber" );
900
896
901
897
// add the sample grabber to filter graph
902
898
res = s->filterGraph ->AddFilter (s->sampleGrabberFilter , L" Sample Grabber" );
903
- if (res != S_OK) {
904
- log_msg (LOG_LEVEL_ERROR, MOD_NAME " vidcap_dshow_init: Cannot add sample grabber to filter graph.\n " );
905
- goto error;
906
- }
899
+ HANDLE_ERR (res, " Cannot add sample grabber to filter graph" );
907
900
908
901
// query the sample grabber filter for control interface
909
902
res = s->sampleGrabberFilter ->QueryInterface (IID_PPV_ARGS (&s->sampleGrabber ));
910
- if (res != S_OK) {
911
- log_msg (LOG_LEVEL_ERROR, MOD_NAME " vidcap_dshow_init: Cannot query sample grabber filter for control interface.\n " );
912
- goto error;
913
- }
903
+ HANDLE_ERR (res, " Cannot query sample grabber filter for control interface" );
914
904
915
905
// make the sample grabber buffer frames
916
906
res = s->sampleGrabber ->SetBufferSamples (TRUE );
917
- if (res != S_OK) {
918
- log_msg (LOG_LEVEL_ERROR, MOD_NAME " vidcap_dshow_init: Cannot set sample grabber to buffer samples.\n " );
919
- goto error;
920
- }
907
+ HANDLE_ERR (res, " Cannot set sample grabber to buffer samples" );
921
908
922
909
// set media type for sample grabber; this is not done a very detailed setup, because it would be unneccessarily complicated to do so
923
910
AM_MEDIA_TYPE sampleGrabberMT;
924
911
ZeroMemory (&sampleGrabberMT, sizeof (sampleGrabberMT));
925
912
sampleGrabberMT.majortype = MEDIATYPE_Video;
926
913
sampleGrabberMT.subtype = MEDIASUBTYPE_RGB24;
927
914
res = s->sampleGrabber ->SetMediaType (&sampleGrabberMT);
928
- if (res != S_OK) {
929
- log_msg (LOG_LEVEL_ERROR, MOD_NAME " vidcap_dshow_init: Cannot setup media type of grabber filter.\n " );
930
- goto error;
931
- }
915
+ HANDLE_ERR (res, " Cannot setup media type of grabber filter" );
932
916
933
917
int capCount, capSize;
934
918
res = s->streamConfig ->GetNumberOfCapabilities (&capCount, &capSize);
935
- if (res != S_OK) {
936
- log_msg (LOG_LEVEL_ERROR, MOD_NAME " vidcap_dshow_init: Cannot read number of capture capabilites.\n " );
937
- goto error;
938
- }
919
+ HANDLE_ERR (res, " Cannot read number of capture capabilites" );
939
920
if (capSize != sizeof (VIDEO_STREAM_CONFIG_CAPS)) {
940
921
log_msg (LOG_LEVEL_ERROR, MOD_NAME " vidcap_dshow_init: Unknown format of capture capabilites.\n " );
941
922
goto error;
@@ -954,10 +935,10 @@ static int vidcap_dshow_init(struct vidcap_params *params, void **state) {
954
935
goto error;
955
936
}
956
937
// Some other error occured
957
- if (res != S_OK) {
958
- log_msg (LOG_LEVEL_ERROR, MOD_NAME " vidcap_dshow_help: Cannot read stream capabilities #%d (index is correct). \n " , s-> modeNumber );
959
- goto error;
960
- }
938
+ HANDLE_ERR (
939
+ res,
940
+ " Cannot read stream capabilities #%d (index is correct) " ,
941
+ s-> modeNumber );
961
942
962
943
if (s->desc .color_spec != VIDEO_CODEC_NONE) {
963
944
MSG (WARNING, " Mode set explicitly, specified "
@@ -969,11 +950,8 @@ static int vidcap_dshow_init(struct vidcap_params *params, void **state) {
969
950
goto error;
970
951
}
971
952
res = s->sampleGrabber ->SetMediaType (mediaType);
972
- if (res != S_OK) {
973
- MSG (ERROR, " vidcap_dshow_init: Cannot setup media type "
974
- " of grabber filter." );
975
- goto error;
976
- }
953
+ HANDLE_ERR (res, " Cannot setup media type "
954
+ " of grabber filter" );
977
955
978
956
struct video_desc desc = vidcap_dshow_get_video_desc (mediaType);
979
957
s->desc .width = desc.width ;
@@ -1010,10 +988,7 @@ static int vidcap_dshow_init(struct vidcap_params *params, void **state) {
1010
988
1011
989
if (s->modeNumber < 0 ) { // mode number was not set by user directly
1012
990
s->streamConfig ->GetFormat (&mediaType);
1013
- if (res != S_OK) {
1014
- log_msg (LOG_LEVEL_ERROR, MOD_NAME " vidcap_dshow_init: Cannot get current capture format.\n " );
1015
- goto error;
1016
- }
991
+ HANDLE_ERR (res, " Cannot get current capture format" );
1017
992
switch (s->desc .color_spec ) {
1018
993
case BGR : mediaType->subtype = MEDIASUBTYPE_RGB24;
1019
994
break ;
@@ -1030,10 +1005,7 @@ static int vidcap_dshow_init(struct vidcap_params *params, void **state) {
1030
1005
infoHeader->AvgTimePerFrame = (REFERENCE_TIME) (1e7 / s->desc .fps );
1031
1006
}
1032
1007
res = s->streamConfig ->SetFormat (mediaType);
1033
- if (res != S_OK) {
1034
- log_msg (LOG_LEVEL_ERROR, MOD_NAME " vidcap_dshow_init: Cannot set capture format.\n " );
1035
- goto error;
1036
- }
1008
+ HANDLE_ERR (res, " Cannot set capture format" );
1037
1009
DeleteMediaType (mediaType);
1038
1010
1039
1011
if (s->convert_YUYV_RGB ) {
@@ -1059,25 +1031,16 @@ static int vidcap_dshow_init(struct vidcap_params *params, void **state) {
1059
1031
// Create null renderer discarding all incoming frames
1060
1032
res = CoCreateInstance (CLSID_NullRenderer, NULL , CLSCTX_INPROC_SERVER, IID_IBaseFilter,
1061
1033
(void **) &s->nullRenderer );
1062
- if (res != S_OK) {
1063
- log_msg (LOG_LEVEL_ERROR, MOD_NAME " vidcap_dshow_init: Cannot create NullRenderer.\n " );
1064
- goto error;
1065
- }
1034
+ HANDLE_ERR (res, " Cannot create NullRenderer" );
1066
1035
1067
1036
res = s->filterGraph ->AddFilter (s->nullRenderer , L" NullRenderer" );
1068
- if (res != S_OK) {
1069
- log_msg (LOG_LEVEL_ERROR, MOD_NAME " vidcap_dshow_init: Cannot add null renderer to filter graph.\n " );
1070
- goto error;
1071
- }
1037
+ HANDLE_ERR (res, " Cannot add null renderer to filter graph" );
1072
1038
1073
1039
IEnumPins *pinEnum;
1074
1040
IPin *pin;
1075
1041
1076
1042
res = s->captureFilter ->EnumPins (&pinEnum);
1077
- if (res != S_OK) {
1078
- log_msg (LOG_LEVEL_ERROR, MOD_NAME " vidcap_dshow_init: Error enumerating pins of capture filter.\n " );
1079
- goto error;
1080
- }
1043
+ HANDLE_ERR (res, " Error enumerating pins of capture filter" );
1081
1044
1082
1045
while (pinEnum->Next (1 , &pin, NULL ) == S_OK) {
1083
1046
res = ConnectFilters (s->filterGraph , pin, s->sampleGrabberFilter );
@@ -1086,17 +1049,10 @@ static int vidcap_dshow_init(struct vidcap_params *params, void **state) {
1086
1049
break ;
1087
1050
}
1088
1051
}
1089
- if (res != S_OK) {
1090
- MSG (ERROR, " vidcap_dshow_init: Cannot connect capture "
1091
- " filter to sample grabber: %s.\n " , hresult_to_str (res));
1092
- goto error;
1093
- }
1052
+ HANDLE_ERR (res, " Cannot connect capture filter to sample grabber" );
1094
1053
1095
1054
res = ConnectFilters (s->filterGraph , s->sampleGrabberFilter , s->nullRenderer );
1096
- if (res != S_OK) {
1097
- log_msg (LOG_LEVEL_ERROR, MOD_NAME " vidcap_dshow_init: Cannot connect sample grabber to null renderer.\n " );
1098
- goto error;
1099
- }
1055
+ HANDLE_ERR (res, " Cannot connect sample grabber to null renderer" );
1100
1056
1101
1057
s->SGCallback = new SampleGrabberCallback (s, &s->frames );
1102
1058
s->sampleGrabber ->SetCallback (s->SGCallback , 1 );
@@ -1111,10 +1067,7 @@ static int vidcap_dshow_init(struct vidcap_params *params, void **state) {
1111
1067
*/
1112
1068
1113
1069
res = s->filterGraph ->QueryInterface (IID_IMediaControl, (void **) &s->mediaControl );
1114
- if (res != S_OK) {
1115
- log_msg (LOG_LEVEL_ERROR, MOD_NAME " vidcap_dshow_init: Cannot find media control interface.\n " );
1116
- goto error;
1117
- }
1070
+ HANDLE_ERR (res, " Cannot find media control interface" );
1118
1071
1119
1072
FILTER_STATE fs;
1120
1073
res = s->mediaControl ->Run ();
@@ -1123,17 +1076,9 @@ static int vidcap_dshow_init(struct vidcap_params *params, void **state) {
1123
1076
break ;
1124
1077
}
1125
1078
}
1126
- if (res != S_OK) {
1127
- log_msg (LOG_LEVEL_ERROR, MOD_NAME " vidcap_dshow_init: Cannot run filter graph.\n " );
1128
- ErrorDescription (res);
1129
- goto error;
1130
- }
1079
+ HANDLE_ERR (res, " Cannot run filter graph" );
1131
1080
res = s->sampleGrabberFilter ->GetState (INFINITE, &fs);
1132
- if (res != S_OK) {
1133
- log_msg (LOG_LEVEL_ERROR, MOD_NAME " filter getstate error\n " );
1134
- ErrorDescription (res);
1135
- goto error;
1136
- }
1081
+ HANDLE_ERR (res, " filter getstate error" );
1137
1082
1138
1083
s->frame = vf_alloc_desc (s->desc );
1139
1084
register_should_exit_callback (vidcap_params_get_parent (params), vidcap_dshow_should_exit, s);
@@ -1144,6 +1089,7 @@ static int vidcap_dshow_init(struct vidcap_params *params, void **state) {
1144
1089
error:
1145
1090
cleanup (s);
1146
1091
return VIDCAP_INIT_FAIL;
1092
+ #undef HANDLE_ERR
1147
1093
}
1148
1094
1149
1095
static void vidcap_dshow_done (void *state) {
0 commit comments