It seems that Microsoft Source reader is still quite difficult to use because of its needs. In fact, according to the example provided by Microsoft, You can normally debug the program.
The following example shows the source reader code. At the same time, the pointer is used in the hresult createvideodevicesource (imfmediasource ** ppsource) function to correctly export the ppsource, not null. Before debugging, use hresult createvideodevicesource (imfmediasource * ppsource). After the output, ppsource is still null. Later, the code was expanded without further consideration. Thanks to @ wuruifang's suggestion two days ago, I searched the internet and found that the pointer can make the output normal when the input parameter is null. So here we re-write a function (I was not sure why an error occurred while using the pointer, but now I do not ).
Main Code:
1 int _ tmain (INT argc, _ tchar * argv []) 2 {3 imfmediasource * ppsource = NULL; 4 createvideodevicesource (& ppsource ); 5 6 // hR = enumeratecaptureformats (ppsource); // This can show the formats the camera support. 7 // If (failed (HR) 8 // abort (); 9 hresult hr; 10 imfsourcereader * preader; 11 hR = mfcreatesourcereaderfrommediasource (ppsource, null, & preader ); 12 if (failed (HR) 13 abort (); 14 15 hr = Setdeviceformat (ppsource, 6); // I need to configure the camera to format 6. 16 if (failed (HR) 17 abort (); 18 19 processsamples (preader); 20 21 saferelles (& preader); 22 saferelease (& ppsource); 23 mfshutdown (); 24 couninitialize (); 25} 26 27 hresult processsamples (imfsourcereader * preader) 28 {29 hresult hR = s_ OK; 30 imfsample * psample = NULL; 31 size_t csamples = 0; 32 33 _ large_integer time _ Start;/* begin time */34 _ large_integer time_over;/* End Time */35 double dqfreq;/* timer frequence */36 large_integer F; /* timer frequence */37 queryperformancefrequency (& F); 38 dqfreq = (double) F. quadpart; 39 40 queryperformancecounter (& time_start); 41 42 43 bool quit = false; 44 While (! Quit) 45 {46 DWORD streamindex, flags; 47 Longlong lltimestamp; 48 49 hR = preader-> readsample (50 mf_source_reader_any_stream, // stream index. 51 0, // flags. 52 & streamindex, // es the actual stream index. 53 & flags, // elasticsearch Status flags. 54 & lltimestamp, // es the time stamp. 55 & psample // replace es the sample or null. 56); 57 58 If (failed (HR) 59 break; 60 61 If (flags & MF _ Source_readerf_endofstream) 62 {63 wprintf (L "\ tend of stream \ n"); 64 quit = true; 65} 66 67 If (psample) 68 {69 byte * data; 70 imfmediabuffer * buffer; 71 DWORD Max, current; 72 73 // printf ("csamples = % d \ n", csamples); 74 ++ csamples; 75 psample-> getbufferbyindex (0, & buffer); 76 buffer-> lock (& Data, & MAX, & Current); 77 78 // savebmp (data, csamples, imgwidth, imgheight); 79 80 buffer-> unlock (); 81 saferelease (& buffer); 82 83 queryperformancecounter (& time_over); // in order to find the frames per second of the camera. 84 double usedtime = (time_over.quadpart-time_start.quadpart)/dqfreq); 85 if (usedtime> 1) 86 {87 printf ("csamples = % d \ n", csamples ); 88 csamples = 0; 89 queryperformancecounter (& time_start); 90} 91} 92 saferelter (& psample); 93} 94 95 saferelease (& psample); 96 retu Rn hr; 97} 98 99 hresult createvideodevicesource (imfmediasource ** ppsource) 100 {101 hresult hr; 102 hR = coinitialize (null); 103 If (failed (HR )) 104 abort (); 105 hR = mfstartup (mf_version, mfstartup_nosocket); 106 If (failed (HR) 107 abort (); 108 109 * ppsource = NULL; 110 111 imfmediasource * psource = NULL; 112 imfattributes * pattributes = NULL; 113 imfactivate ** ppdevices = NULL; 114 115 // create an attri External Store to specify the enumeration parameters.116/* hresult */HR = mfcreateattributes (& pattributes, 1); 117 If (failed (HR) 118 abort (); 119 120 // source type: Video Capture devices121 hR = pattributes-> setguid (mf_devsource_attribute_source_type, 122 bytes); 123 If (failed (HR) 124 abort (); 125 126 // enumerate devices.127 uint32 count; 128 hR = mfenumdevices Ources (pattributes, & ppdevices, & COUNT); 129 If (failed (HR) 130 abort (); 131 If (COUNT = 0) 132 {133 hR = e_fail; 134 return hr; 135} 136 137 // create the media source object.138 hR = ppdevices [0]-> activateobject (iid_ppv_args (& psource); 139 If (failed (HR )) 140 abort (); 141 142 * ppsource = psource; 143 (* ppsource)-> addref (); 144 145 // release part146 saferelease (& pattributes ); 147 148 For (DWORD I = 0; I <Count; I ++) 149 {150 saferelease (& ppdevices [I]); 151} 152 cotaskmemfree (ppdevices); 153 saferelease (& psource); // not sure here, whether saferelease is required. 154 return hr; 155}
(BMP is saved in the program. Because cnblogs does not support BMP, it is converted to JPG ):
Complete code (Hyperlink not inserted, sorry) See: https://github.com/darkknightzh/Microsoft-Source-Reader
For more information, see ):
Microsoft: http://msdn.microsoft.com/en-us/library/windows/desktop/dd389281 (V = vs.85). aspx
Pointer pointer (see 4 floor, 14 Floor): http://bbs.csdn.net/topics/210076970
(Original) simple use of Microsoft Source Reader