Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

546 lines
15 KiB

  1. /*++
  2. Copyright (c) 1995-1996 Microsoft Corporation
  3. Module Name:
  4. medistrm.h
  5. Abstract:
  6. Contains constants and class declarations for the abstract MediaStream object. A MediaStream
  7. represents a single unidirectional stream, such as a received Video channel.
  8. --*/
  9. #ifndef _MEDISTRM_H_
  10. #define _MEDISTRM_H_
  11. #include "dtmf.h"
  12. #include <pshpack8.h> /* Assume 8 byte packing throughout */
  13. class DataPump;
  14. class TxStream;
  15. class RxStream;
  16. class AcmFilter;
  17. class VcmFilter;
  18. class MediaControl;
  19. class BufferPool;
  20. class AudioPacket;
  21. class VideoPacket;
  22. class SendMediaStream : public IMediaChannel {
  23. friend class SendAudioStream;
  24. protected:
  25. DataPump *m_pDP;
  26. TxStream *m_SendStream;
  27. MediaControl *m_InMedia;
  28. UINT m_CaptureDevice; // device id used for recording
  29. UINT m_PreviousCaptureDevice; // device id used for recording
  30. IRTPSession *m_Net;
  31. IRTPSend *m_pRTPSend;
  32. BYTE m_RTPPayload; // payload type
  33. //BufferPool *m_NetBufferPool;
  34. DWORD m_SendTimestamp;
  35. DWORD m_SavedTickCount;
  36. DWORD m_ThreadFlags;
  37. DWORD m_DPFlags;
  38. BOOL m_fSending;
  39. MEDIA_FORMAT_ID m_PrevFormatId;
  40. DWORD m_dwDstSize;
  41. FLOWSPEC m_flowspec;
  42. HRESULT SetFlowSpec();
  43. HANDLE m_hCapturingThread;
  44. DWORD m_CaptureThId;
  45. CRITICAL_SECTION m_crsQos;
  46. // IQOS interface pointer and two resources requests: one for BW and one for CPU
  47. struct {
  48. int cResourceRequests;
  49. RESOURCEREQUEST aResourceRequest[2];
  50. } m_aRRq;
  51. // Performance statistics
  52. struct {
  53. DWORD dwMsCap; // Capture CPU usage (ms)
  54. DWORD dwMsComp; // Compression CPU usage (ms)
  55. DWORD dwBits; // Compressed audio or video frame size (bits)
  56. DWORD dwCount; // Number of video frames captured or audio packets recorded
  57. DWORD dwOldestTs; // Oldest QoS callback timestamp
  58. DWORD dwNewestTs; // Most recent QoS callback timestamp
  59. HKEY hPerfKey; // Handle to CPU perf data collection reg key on Win95/98
  60. DWORD dwSmoothedCPUUsage; // Previous CPU usage value - used to compute slow-varying average in CPU usage
  61. BOOL fWinNT; // Are we running on WinNT or Win95/98?
  62. struct { // Structure used to extract CPU usage performance on NT
  63. DWORD cbPerfData;
  64. PBYTE pbyPerfData;
  65. HANDLE hPerfData;
  66. LONGLONG llPerfTime100nSec;
  67. PLONGLONG pllCounterValue;
  68. DWORD dwProcessorIndex;
  69. DWORD dwPercentProcessorIndex;
  70. DWORD dwNumProcessors;
  71. } NtCPUUsage;
  72. } m_Stats;
  73. RTP_STATS m_RTPStats; // network stats
  74. public:
  75. SendMediaStream()
  76. {
  77. InitializeCriticalSection(&m_crsQos);
  78. };
  79. virtual ~SendMediaStream()
  80. {
  81. DeleteCriticalSection(&m_crsQos);
  82. }
  83. // Implementation of IMediaChannel::GetState
  84. STDMETHODIMP_(DWORD) GetState()
  85. {
  86. if (m_DPFlags & DPFLAG_STARTED_SEND) return MSSTATE_STARTED;
  87. else if (m_DPFlags & DPFLAG_CONFIGURED_SEND) return MSSTATE_CONFIGURED;
  88. else return MSSTATE_UNCONFIGURED;
  89. }
  90. virtual HRESULT Initialize(DataPump *) = 0;
  91. virtual DWORD Send() = 0;
  92. virtual void EndSend() = 0;
  93. virtual HRESULT STDMETHODCALLTYPE SetNetworkInterface(IUnknown *pUnknown);
  94. } ;
  95. class RecvMediaStream : public IMediaChannel
  96. {
  97. friend class DataPump;
  98. friend BOOL RTPRecvCallback(DWORD_PTR,WSABUF *);
  99. protected:
  100. DataPump *m_pDP;
  101. RxStream *m_RecvStream;
  102. MediaControl *m_OutMedia;
  103. UINT m_RenderingDevice; // device id used for playback
  104. IRTPSession *m_Net;
  105. IRTPRecv *m_pIRTPRecv;
  106. //BufferPool *m_NetBufferPool;
  107. DWORD m_ThreadFlags;
  108. DWORD m_DPFlags;
  109. BOOL m_fReceiving;
  110. DWORD m_PlaybackTimestamp; // last played sample
  111. HANDLE m_hRecvThreadStopEvent;
  112. HANDLE m_hRenderingThread;
  113. DWORD m_RenderingThId;
  114. UINT m_nRecvBuffersPending;
  115. DWORD m_dwSrcSize;
  116. FLOWSPEC m_flowspec;
  117. HRESULT SetFlowSpec();
  118. public:
  119. RecvMediaStream(){};
  120. virtual HRESULT Initialize(DataPump *) = 0;
  121. virtual BOOL IsEmpty() = 0;
  122. // Implementation of IMediaChannel::GetState
  123. STDMETHODIMP_(DWORD) GetState()
  124. {
  125. if (m_DPFlags & DPFLAG_STARTED_RECV) return MSSTATE_STARTED;
  126. else if (m_DPFlags & DPFLAG_CONFIGURED_RECV) return MSSTATE_CONFIGURED;
  127. else return MSSTATE_UNCONFIGURED;
  128. }
  129. virtual HRESULT RTPCallback(WSABUF *pWsaBuf, DWORD timestamp, UINT seq, UINT fMark)=0;
  130. virtual HRESULT GetCurrentPlayNTPTime(NTP_TS *)=0;
  131. virtual HRESULT StartRecv(HWND)=0;
  132. virtual HRESULT StopRecv()=0;
  133. virtual HRESULT STDMETHODCALLTYPE SetNetworkInterface(IUnknown *pUnknown);
  134. virtual HRESULT DTMFBeep() {return S_OK;}
  135. virtual HRESULT OnDTMFBeep() {return S_OK;}
  136. };
  137. class SendVideoStream : public SendMediaStream, public IVideoRender,
  138. public IVideoChannel
  139. {
  140. friend class DataPump;
  141. protected:
  142. CCaptureChain* m_pCaptureChain;
  143. VIDEOFORMATEX m_fDevSend;
  144. VIDEOFORMATEX m_fCodecOutput;
  145. RECT m_cliprect;
  146. DWORD m_maxfps;
  147. DWORD m_frametime;
  148. int *m_pTSTable; // NULL if table isn't used
  149. DWORD m_dwCurrentTSSetting;
  150. VcmFilter *m_pVideoFilter;
  151. IUnknown *m_pIUnknown; // Pointer to IUnkown from which we'll query the Stream Signal interface
  152. class MediaPacket *m_pNextPacketToRender; // current recv video frame
  153. UINT m_cRendering; // count of packets given out by GetFrame()
  154. HANDLE m_hRenderEvent; // IVideoRender event for recv notification
  155. LPFNFRAMEREADY m_pfFrameReadyCallback; // callback function
  156. CRITICAL_SECTION m_crs;
  157. CRITICAL_SECTION m_crsVidQoS; // Allows QoS thread to read the video statistics while capture and compression are running
  158. // the capture thread (and it's launch function)
  159. static DWORD CALLBACK StartCaptureThread(LPVOID pVoid);
  160. DWORD CapturingThread();
  161. HRESULT SendPacket(VideoPacket *pVP, UINT *puBytesSent);
  162. STDMETHODIMP_(void) UnConfigure(void);
  163. LONG m_lRefCount;
  164. public:
  165. SendVideoStream(): SendMediaStream(){m_Net=NULL; m_lRefCount=0; };
  166. virtual ~SendVideoStream();
  167. // IUnknown
  168. virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject);
  169. virtual ULONG STDMETHODCALLTYPE AddRef(void);
  170. virtual ULONG STDMETHODCALLTYPE Release(void);
  171. // IMediaChannel APIs
  172. // new version of Configure()
  173. HRESULT STDMETHODCALLTYPE Configure(
  174. BYTE *pFormat,
  175. UINT cbFormat,
  176. BYTE *pChannelParams,
  177. UINT cbParams,
  178. IUnknown *pUnknown);
  179. STDMETHODIMP Start(void);
  180. STDMETHODIMP Stop(void);
  181. HRESULT STDMETHODCALLTYPE SetNetworkInterface(IUnknown *pUnknown)
  182. {
  183. return SendMediaStream::SetNetworkInterface(pUnknown);
  184. }
  185. STDMETHODIMP_(DWORD) GetState()
  186. {
  187. return SendMediaStream::GetState();
  188. }
  189. HRESULT STDMETHODCALLTYPE SetMaxBitrate(UINT uMaxBitrate);
  190. // IVideoChannel
  191. virtual HRESULT __stdcall SetTemporalSpatialTradeOff(DWORD dwVal);
  192. virtual HRESULT __stdcall GetTemporalSpatialTradeOff(DWORD *pdwVal);
  193. virtual HRESULT __stdcall SendKeyFrame(void);
  194. virtual HRESULT __stdcall ShowDeviceDialog(DWORD dwFlags);
  195. virtual HRESULT __stdcall GetDeviceDialog(DWORD *pdwFlags);
  196. // IProperty methods
  197. STDMETHODIMP GetProperty(DWORD dwProp, PVOID pBuf, LPUINT pcbBuf);
  198. STDMETHODIMP SetProperty(DWORD dwProp, PVOID pBuf, UINT cbBuf);
  199. // IVideoRender methods
  200. STDMETHODIMP Init( DWORD_PTR dwUser, LPFNFRAMEREADY pfCallback);
  201. STDMETHODIMP Done(void);
  202. STDMETHODIMP GetFrame(FRAMECONTEXT* pfc);
  203. STDMETHODIMP ReleaseFrame(FRAMECONTEXT *pfc);
  204. // Other virtual methods
  205. virtual HRESULT Initialize(DataPump *);
  206. // Non virtual methods
  207. static HRESULT CALLBACK QosNotifyVideoCB(LPRESOURCEREQUESTLIST lpResourceRequestList, DWORD_PTR dwThis);
  208. void UnConfigureSendVideo(BOOL fNewDeviceSettings, BOOL fNewDevice);
  209. void StartCPUUsageCollection(void);
  210. BOOL GetCPUUsage(PDWORD pdwOverallCPUUsage);
  211. void StopCPUUsageCollection(void);
  212. BOOL SetTargetRates(DWORD dwTargetFrameRate, DWORD dwTargetBitrate);
  213. DWORD Send();
  214. void EndSend();
  215. };
  216. class RecvVideoStream : public RecvMediaStream, public IVideoRender {
  217. friend class DataPump;
  218. protected:
  219. VIDEOFORMATEX m_fDevRecv;
  220. RECT m_cliprect;
  221. class MediaPacket *m_pNextPacketToRender; // current recv video frame
  222. UINT m_cRendering; // count of packets given out by GetFrame()
  223. HANDLE m_hRenderEvent; // IVideoRender event for recv notification
  224. LPFNFRAMEREADY m_pfFrameReadyCallback; // callback function
  225. CRITICAL_SECTION m_crs;
  226. VcmFilter *m_pVideoFilter;
  227. IUnknown *m_pIUnknown; // Pointer to IUnkown from which we'll query the Stream Signal interface
  228. IStreamSignal *m_pIStreamSignal; // Pointer to I-Frame request interface
  229. CRITICAL_SECTION m_crsIStreamSignal; // Used to serialize access to the interface between Stop() and the RTP callback
  230. UINT m_ulLastSeq; // Last received RTP sequence number
  231. DWORD m_dwLastIFrameRequest; // When was the last I-frame request sent? Used to make sure we don't send requests too often
  232. BOOL m_fDiscontinuity; // Signals that a discontinuity (RTP packet lost or receive frame buffer overflow) was detected
  233. CRITICAL_SECTION m_crsVidQoS; // Allows QoS thread to read the video statistics while capture and compression are running
  234. static DWORD CALLBACK StartRenderingThread(PVOID pVoid);
  235. DWORD RenderingThread();
  236. STDMETHODIMP_(void) UnConfigure(void);
  237. LONG m_lRefCount;
  238. public:
  239. RecvVideoStream() : RecvMediaStream(){m_Net=NULL; m_lRefCount=0; };
  240. virtual ~RecvVideoStream();
  241. // IUnknown
  242. virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject);
  243. virtual ULONG STDMETHODCALLTYPE AddRef(void);
  244. virtual ULONG STDMETHODCALLTYPE Release(void);
  245. // IMediaChannel APIs
  246. HRESULT STDMETHODCALLTYPE Configure(
  247. BYTE *pFormat,
  248. UINT cbFormat,
  249. BYTE *pChannelParams,
  250. UINT cbParams,
  251. IUnknown *pUnknown);
  252. STDMETHODIMP Start(void);
  253. STDMETHODIMP Stop(void);
  254. HRESULT STDMETHODCALLTYPE SetMaxBitrate(UINT uMaxBitrate)
  255. {
  256. return E_NOTIMPL;
  257. }
  258. // IProperty methods
  259. STDMETHODIMP GetProperty(DWORD dwProp, PVOID pBuf, LPUINT pcbBuf);
  260. STDMETHODIMP SetProperty(DWORD dwProp, PVOID pBuf, UINT cbBuf);
  261. // IVideoRender methods
  262. STDMETHODIMP Init( DWORD_PTR dwUser, LPFNFRAMEREADY pfCallback);
  263. STDMETHODIMP Done(void);
  264. STDMETHODIMP GetFrame(FRAMECONTEXT* pfc);
  265. STDMETHODIMP ReleaseFrame(FRAMECONTEXT *pfc);
  266. // Other virtual methods
  267. virtual HRESULT Initialize(DataPump *);
  268. virtual BOOL IsEmpty();
  269. HRESULT GetCurrentPlayNTPTime(NTP_TS *);
  270. virtual HRESULT StartRecv(HWND);
  271. virtual HRESULT StopRecv();
  272. virtual HRESULT RTPCallback(WSABUF *pWsaBuf, DWORD timestamp, UINT seq, UINT fMark);
  273. // Non virtual methods
  274. };
  275. extern char LogScale[];
  276. class AudioSilenceDetector {
  277. private:
  278. UINT m_uManualSilenceLevel; // silence level in unit of 1/1000
  279. DWORD m_dwMaxStrength; // signal strength in units of 1/1000
  280. INT m_iSilenceLevel; // adaptive silence threshold
  281. INT m_iSilenceAvg; // scale factor 256
  282. INT m_iTalkAvg; // average strength of non-silent signal
  283. public:
  284. AudioSilenceDetector();
  285. void SetSilenceLevel(UINT level) {m_uManualSilenceLevel = level;}
  286. UINT GetSilenceLevel(void) {return m_uManualSilenceLevel;}
  287. UINT GetSignalStrength(void) {return LogScale[m_dwMaxStrength >> 8];}
  288. BOOL SilenceDetect(WORD strength);
  289. };
  290. class SendAudioStream : public SendMediaStream, public IAudioChannel, public IDTMFSend
  291. {
  292. friend class DataPump;
  293. private:
  294. WAVEFORMATEX m_fDevSend;
  295. WAVEFORMATEX m_wfCompressed;
  296. AcmFilter *m_pAudioFilter; // this will replace m_fSendFilter
  297. MMIOSRC m_mmioSrc;
  298. AudioSilenceDetector m_AudioMonitor;
  299. BOOL m_bAutoMix;
  300. static DWORD CALLBACK StartRecordingThread (LPVOID pVoid);
  301. DWORD RecordingThread();
  302. HRESULT SendPacket(AudioPacket *pAP, UINT *puBytesSent);
  303. STDMETHODIMP_(void) UnConfigure(void);
  304. LONG m_lRefCount;
  305. DTMFQueue *m_pDTMF;
  306. HRESULT __stdcall SendDTMF();
  307. public:
  308. SendAudioStream() : SendMediaStream(){m_Net=NULL;m_lRefCount=0;};
  309. virtual ~SendAudioStream();
  310. // IUnknown
  311. virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject);
  312. virtual ULONG STDMETHODCALLTYPE AddRef(void);
  313. virtual ULONG STDMETHODCALLTYPE Release(void);
  314. // IMediaChannel APIs
  315. // new version of Configure()
  316. HRESULT STDMETHODCALLTYPE Configure(
  317. BYTE *pFormat,
  318. UINT cbFormat,
  319. BYTE *pChannelParams,
  320. UINT cbParams,
  321. IUnknown *pUnknown);
  322. STDMETHODIMP Start(void);
  323. STDMETHODIMP Stop(void);
  324. HRESULT STDMETHODCALLTYPE SetNetworkInterface(IUnknown *pUnknown)
  325. {
  326. return SendMediaStream::SetNetworkInterface(pUnknown);
  327. }
  328. STDMETHODIMP_(DWORD) GetState()
  329. {
  330. return SendMediaStream::GetState();
  331. }
  332. HRESULT STDMETHODCALLTYPE SetMaxBitrate(UINT uMaxBitrate);
  333. // IAudioChannel
  334. STDMETHODIMP GetSignalLevel(UINT *pSignalStrength);
  335. // IDTMFSend
  336. virtual HRESULT __stdcall AddDigit(int nDigit);
  337. virtual HRESULT __stdcall ResetDTMF();
  338. // IProperty methods
  339. STDMETHODIMP GetProperty(DWORD dwProp, PVOID pBuf, LPUINT pcbBuf);
  340. STDMETHODIMP SetProperty(DWORD dwProp, PVOID pBuf, UINT cbBuf);
  341. // Other virtual methods
  342. virtual HRESULT Initialize(DataPump *pdp);
  343. // Non virtual methods
  344. static HRESULT CALLBACK QosNotifyAudioCB(LPRESOURCEREQUESTLIST lpResourceRequestList, DWORD_PTR dwThis);
  345. HRESULT OpenSrcFile (void);
  346. HRESULT CloseSrcFile (void);
  347. DWORD Send();
  348. void EndSend();
  349. };
  350. class RecvAudioStream : public RecvMediaStream, public IAudioChannel
  351. {
  352. friend class DataPump;
  353. private:
  354. WAVEFORMATEX m_fDevRecv;
  355. IAppAudioCap* m_pAudioCaps; // pointer to the audio capabilities object
  356. // mmio file operations
  357. MMIODEST m_mmioDest;
  358. AcmFilter *m_pAudioFilter; // this will replace m_fSendFilter
  359. AudioSilenceDetector m_AudioMonitor;
  360. CRITICAL_SECTION m_crsAudQoS; // Allows QoS thread to read the audio statistics while recording and compression are running
  361. static DWORD CALLBACK StartPlaybackThread(LPVOID pVoid);
  362. DWORD PlaybackThread();
  363. STDMETHODIMP_(void) UnConfigure(void);
  364. LONG m_lRefCount;
  365. virtual HRESULT DTMFBeep();
  366. public:
  367. RecvAudioStream() :RecvMediaStream(){m_Net=NULL;m_lRefCount=0;};
  368. virtual ~RecvAudioStream();
  369. // IUnknown
  370. virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject);
  371. virtual ULONG STDMETHODCALLTYPE AddRef(void);
  372. virtual ULONG STDMETHODCALLTYPE Release(void);
  373. // IMediaChannel APIs
  374. HRESULT STDMETHODCALLTYPE Configure(
  375. BYTE *pFormat,
  376. UINT cbFormat,
  377. BYTE *pChannelParams,
  378. UINT cbParams,
  379. IUnknown *pUnknown);
  380. STDMETHODIMP Start(void);
  381. STDMETHODIMP Stop(void);
  382. HRESULT STDMETHODCALLTYPE SetNetworkInterface(IUnknown *pUnknown)
  383. {
  384. return RecvMediaStream::SetNetworkInterface(pUnknown);
  385. }
  386. STDMETHODIMP_(DWORD) GetState()
  387. {
  388. return RecvMediaStream::GetState();
  389. }
  390. HRESULT STDMETHODCALLTYPE SetMaxBitrate(UINT uMaxBitrate)
  391. {
  392. return E_NOTIMPL;
  393. }
  394. // IAudioChannel
  395. STDMETHODIMP GetSignalLevel(UINT *pSignalStrength);
  396. // IProperty methods
  397. STDMETHODIMP GetProperty(DWORD dwProp, PVOID pBuf, LPUINT pcbBuf);
  398. STDMETHODIMP SetProperty(DWORD dwProp, PVOID pBuf, UINT cbBuf);
  399. // Other virtual inherited methods
  400. virtual HRESULT Initialize(DataPump *);
  401. virtual BOOL IsEmpty();
  402. HRESULT GetCurrentPlayNTPTime(NTP_TS *);
  403. virtual HRESULT StartRecv(HWND);
  404. virtual HRESULT StopRecv();
  405. virtual HRESULT RTPCallback(WSABUF *pWsaBuf, DWORD timestamp, UINT seq, UINT fMark);
  406. };
  407. #include <poppack.h> /* End byte packing */
  408. #endif // _MEDISTRM_H_
  409.