Skip to content

Commit 604bbf9

Browse files
committed
dshow: generic error handling in init
Handle eventual error generically, currently only in init. Print also error description (will be perhaps just a code for DShow for now).
1 parent 1fa19a6 commit 604bbf9

File tree

1 file changed

+34
-88
lines changed

1 file changed

+34
-88
lines changed

src/video_capture/DirectShowGrabber.cpp

Lines changed: 34 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,14 @@ static void vidcap_dshow_should_exit(void *state) {
805805
}
806806

807807
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)
808816
struct vidcap_dshow_state *s;
809817
HRESULT res;
810818

@@ -873,69 +881,42 @@ static int vidcap_dshow_init(struct vidcap_params *params, void **state) {
873881
<< get_friendly_name(s->moniker) << "\n";
874882

875883
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");
880885

881886
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");
886888

887889
res = s->graphBuilder->FindInterface(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video, s->captureFilter,
888890
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");
893892

894893
// create instance of sample grabber
895894
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");
900896

901897
// add the sample grabber to filter graph
902898
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");
907900

908901
// query the sample grabber filter for control interface
909902
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");
914904

915905
// make the sample grabber buffer frames
916906
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");
921908

922909
// set media type for sample grabber; this is not done a very detailed setup, because it would be unneccessarily complicated to do so
923910
AM_MEDIA_TYPE sampleGrabberMT;
924911
ZeroMemory(&sampleGrabberMT, sizeof(sampleGrabberMT));
925912
sampleGrabberMT.majortype = MEDIATYPE_Video;
926913
sampleGrabberMT.subtype = MEDIASUBTYPE_RGB24;
927914
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");
932916

933917
int capCount, capSize;
934918
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");
939920
if (capSize != sizeof(VIDEO_STREAM_CONFIG_CAPS)) {
940921
log_msg(LOG_LEVEL_ERROR, MOD_NAME "vidcap_dshow_init: Unknown format of capture capabilites.\n");
941922
goto error;
@@ -954,10 +935,10 @@ static int vidcap_dshow_init(struct vidcap_params *params, void **state) {
954935
goto error;
955936
}
956937
// 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);
961942

962943
if (s->desc.color_spec != VIDEO_CODEC_NONE) {
963944
MSG(WARNING, "Mode set explicitly, specified "
@@ -969,11 +950,8 @@ static int vidcap_dshow_init(struct vidcap_params *params, void **state) {
969950
goto error;
970951
}
971952
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");
977955

978956
struct video_desc desc = vidcap_dshow_get_video_desc(mediaType);
979957
s->desc.width = desc.width;
@@ -1010,10 +988,7 @@ static int vidcap_dshow_init(struct vidcap_params *params, void **state) {
1010988

1011989
if (s->modeNumber < 0) { // mode number was not set by user directly
1012990
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");
1017992
switch (s->desc.color_spec) {
1018993
case BGR : mediaType->subtype = MEDIASUBTYPE_RGB24;
1019994
break;
@@ -1030,10 +1005,7 @@ static int vidcap_dshow_init(struct vidcap_params *params, void **state) {
10301005
infoHeader->AvgTimePerFrame = (REFERENCE_TIME) (1e7 / s->desc.fps);
10311006
}
10321007
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");
10371009
DeleteMediaType(mediaType);
10381010

10391011
if (s->convert_YUYV_RGB) {
@@ -1059,25 +1031,16 @@ static int vidcap_dshow_init(struct vidcap_params *params, void **state) {
10591031
// Create null renderer discarding all incoming frames
10601032
res = CoCreateInstance(CLSID_NullRenderer, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter,
10611033
(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");
10661035

10671036
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");
10721038

10731039
IEnumPins *pinEnum;
10741040
IPin *pin;
10751041

10761042
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");
10811044

10821045
while (pinEnum->Next(1, &pin, NULL) == S_OK) {
10831046
res = ConnectFilters(s->filterGraph, pin, s->sampleGrabberFilter);
@@ -1086,17 +1049,10 @@ static int vidcap_dshow_init(struct vidcap_params *params, void **state) {
10861049
break;
10871050
}
10881051
}
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");
10941053

10951054
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");
11001056

11011057
s->SGCallback = new SampleGrabberCallback(s, &s->frames);
11021058
s->sampleGrabber->SetCallback(s->SGCallback, 1);
@@ -1111,10 +1067,7 @@ static int vidcap_dshow_init(struct vidcap_params *params, void **state) {
11111067
*/
11121068

11131069
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");
11181071

11191072
FILTER_STATE fs;
11201073
res = s->mediaControl->Run();
@@ -1123,17 +1076,9 @@ static int vidcap_dshow_init(struct vidcap_params *params, void **state) {
11231076
break;
11241077
}
11251078
}
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");
11311080
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");
11371082

11381083
s->frame = vf_alloc_desc(s->desc);
11391084
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) {
11441089
error:
11451090
cleanup(s);
11461091
return VIDCAP_INIT_FAIL;
1092+
#undef HANDLE_ERR
11471093
}
11481094

11491095
static void vidcap_dshow_done(void *state) {

0 commit comments

Comments
 (0)