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.

299 lines
7.7 KiB

  1. #include "precomp.h"
  2. // SHORT g_BeepSamples[] = {195,-4352,-14484,-8778,397,-1801,2376,12278,6830,-2053};
  3. SHORT g_BeepSamples[] = {195,-4352,-12484,-8778,397,-1801,2376,10278,6830,-2053};
  4. #define BL 1024
  5. #if 0
  6. DEFWAVEFORMAT g_wfDefList[] =
  7. {
  8. {WAVE_FORMAT_PCM, 1, 22050, 22050, 1, 8, 0},
  9. {WAVE_FORMAT_PCM, 1, 11025, 11025, 1, 8, 0},
  10. {WAVE_FORMAT_PCM, 1, 8000, 8000, 1, 8, 0},
  11. {WAVE_FORMAT_PCM, 1, 5510, 5510, 1, 8, 0},
  12. {WAVE_FORMAT_ADPCM, 1, 11025, 11025/2, BL, 4, 32, (BL-7)*2+2, 7, 0x0100,0x0000,0x0200,0xFF00,0x0000,0x0000,0x00C0,0x0040,0x00F0,0x0000,0x01CC,0xFF30,0x0188,0xFF18},
  13. {WAVE_FORMAT_ADPCM, 1, 8000, 8000/2, BL, 4, 32, (BL-7)*2+2, 7, 0x0100,0x0000,0x0200,0xFF00,0x0000,0x0000,0x00C0,0x0040,0x00F0,0x0000,0x01CC,0xFF30,0x0188,0xFF18},
  14. {WAVE_FORMAT_ADPCM, 1, 5510, 5510/2, BL, 4, 32, (BL-7)*2+2, 7, 0x0100,0x0000,0x0200,0xFF00,0x0000,0x0000,0x00C0,0x0040,0x00F0,0x0000,0x01CC,0xFF30,0x0188,0xFF18},
  15. {WAVE_FORMAT_PCM, 1, 11025, 11025, 1, 8, 0},
  16. };
  17. #endif
  18. DEFWAVEFORMAT g_wfDefList[] =
  19. {
  20. {WAVE_FORMAT_VOXWARE,1,8000, 16000, 2, 16, 0},
  21. {WAVE_FORMAT_PCM, 1, 8000, 8000, 1, 8, 0},
  22. {WAVE_FORMAT_PCM, 1, 5510, 5510, 1, 8, 0},
  23. {WAVE_FORMAT_ADPCM, 1, 8000, 4096, 256, 4, 32, 500, 7, 0x0100,0x0000,0x0200,0xFF00,0x0000,0x0000,0x00C0,0x0040,0x00F0,0x0000,0x01CC,0xFF30,0x0188,0xFF18},
  24. {WAVE_FORMAT_ADPCM, 1, 5510, 2755, 256, 4, 32, 500, 7, 0x0100,0x0000,0x0200,0xFF00,0x0000,0x0000,0x00C0,0x0040,0x00F0,0x0000,0x01CC,0xFF30,0x0188,0xFF18},
  25. {WAVE_FORMAT_GSM610,1, 8000, 1625, 65, 0, 2, 320, 240, 0},
  26. {WAVE_FORMAT_ALAW, 1, 8000, 8000, 1, 8, 0},
  27. {WAVE_FORMAT_PCM, 1, 11025, 11025, 1, 8, 0},
  28. {WAVE_FORMAT_PCM, 1, 8000, 16000, 2, 16, 0},
  29. };
  30. WAVEFORMATEX * GetDefWaveFormat ( int idx )
  31. {
  32. return ((idx < DWF_NumOfWaveFormats) ?
  33. (WAVEFORMATEX *) &g_wfDefList[idx] :
  34. (WAVEFORMATEX *) NULL);
  35. }
  36. ULONG GetWaveFormatSize ( PVOID pwf )
  37. {
  38. return (((WAVEFORMAT *) pwf)->wFormatTag == WAVE_FORMAT_PCM
  39. ? sizeof (PCMWAVEFORMAT)
  40. : sizeof (PCMWAVEFORMAT) + sizeof (WORD) + ((WAVEFORMATEX *) pwf)->cbSize);
  41. }
  42. BOOL IsSameWaveFormat ( PVOID pwf1, PVOID pwf2 )
  43. {
  44. UINT u1 = GetWaveFormatSize (pwf1);
  45. UINT u2 = GetWaveFormatSize (pwf2);
  46. BOOL fSame = FALSE;
  47. if (u1 == u2)
  48. {
  49. fSame = ! CompareMemory1 ((char *)pwf1, (char *)pwf2, u1);
  50. }
  51. return fSame;
  52. }
  53. void FillSilenceBuf ( WAVEFORMATEX *pwf, PBYTE pb, ULONG cb )
  54. {
  55. if (pwf && pb)
  56. {
  57. if ((pwf->wFormatTag == WAVE_FORMAT_PCM) && (pwf->wBitsPerSample == 8))
  58. {
  59. FillMemory (pb, cb, (BYTE) 0x80);
  60. }
  61. else
  62. {
  63. ZeroMemory (pb, cb);
  64. }
  65. }
  66. }
  67. void MakeDTMFBeep(WAVEFORMATEX *pwf, PBYTE pb, ULONG cb)
  68. {
  69. SHORT *pShort = (SHORT*)pb;
  70. int nBeepMod = sizeof (g_BeepSamples) / sizeof(g_BeepSamples[0]);
  71. int nIndex, nLoops = 0;
  72. BYTE bSample;
  73. if (pwf->wBitsPerSample == 16)
  74. {
  75. nLoops = cb / 2;
  76. for (nIndex=0; nIndex < nLoops; nIndex++)
  77. {
  78. pShort[nIndex] = g_BeepSamples[(nIndex % nBeepMod)];
  79. }
  80. }
  81. else
  82. {
  83. nLoops = cb;
  84. for (nIndex=0; nIndex < nLoops; nIndex++)
  85. {
  86. bSample = (g_BeepSamples[(nIndex % nBeepMod)] >> 8) & 0x00ff;
  87. bSample = bSample ^ 0x80;
  88. pb[nIndex] = bSample;
  89. }
  90. }
  91. }
  92. char CompareMemory1 ( char * p1, char * p2, UINT u )
  93. {
  94. char i;
  95. while (u--)
  96. {
  97. i = *p1++ - *p2++;
  98. if (i) return i;
  99. }
  100. return 0;
  101. }
  102. short CompareMemory2 ( short * p1, short * p2, UINT u )
  103. {
  104. short i;
  105. while (u--)
  106. {
  107. i = *p1++ - *p2++;
  108. if (i) return i;
  109. }
  110. return 0;
  111. }
  112. long CompareMemory4 ( long * p1, long * p2, UINT u )
  113. {
  114. long i;
  115. while (u--)
  116. {
  117. i = *p1++ - *p2++;
  118. if (i) return i;
  119. }
  120. return 0;
  121. }
  122. const DWORD WIN98GOLDBUILD = 1998;
  123. const DWORD WIN98GOLDMAJOR = 4;
  124. const DWORD WIN98GOLDMINOR = 10;
  125. inline bool ISWIN98GOLD()
  126. {
  127. OSVERSIONINFO osVersion;
  128. BOOL bRet;
  129. osVersion.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  130. bRet = GetVersionEx(&osVersion);
  131. if ( bRet &&
  132. ( (osVersion.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) &&
  133. (osVersion.dwMajorVersion == WIN98GOLDMAJOR) &&
  134. (osVersion.dwMinorVersion == WIN98GOLDMINOR) // &&
  135. // (osVersion.dwBuildNumber == WIN98GOLDBUILD)
  136. )
  137. )
  138. {
  139. return true;
  140. }
  141. return false;
  142. }
  143. // dwPacketSize is the size of the audio payload (excludes RTP header)
  144. // pwf represents the compressed audio format
  145. HRESULT InitAudioFlowspec(FLOWSPEC *pFlowspec, WAVEFORMATEX *pwf, DWORD dwPacketSize)
  146. {
  147. DWORD dwPacketOverhead = 0;
  148. OSVERSIONINFO osVersion;
  149. DWORD dwTotalSize;
  150. BOOL bRet;
  151. // on Windows 2000 and Win98 OSR, the FLOWSPEC needs to be set without regard
  152. // to the UDP/IP headers.
  153. // On Win98 Gold, FLOWSPEC needs to account for IP/UDP headers
  154. // no way to detect this without explicitly checking the version number
  155. if (ISWIN98GOLD())
  156. {
  157. dwPacketOverhead = IP_HEADER_SIZE + UDP_HEADER_SIZE;
  158. }
  159. dwTotalSize = dwPacketOverhead + dwPacketSize + sizeof(RTP_HDR);
  160. // rather than specify the exact minimum bandwidth we need for audio,
  161. // make it slightly larger (10%) to account for bursts and beginning
  162. // of talk spurts
  163. // peakbandwidth is set another increment above TR, and bucket size
  164. // is adjusted high as well
  165. pFlowspec->TokenRate = (dwTotalSize * pwf->nAvgBytesPerSec) / dwPacketSize;
  166. pFlowspec->TokenRate = (11 * pFlowspec->TokenRate) / 10;
  167. // peak bandwidth is an ADDITIONAL 10% greater than TokenRate, so it's
  168. // really a 21% increase over the theoretical minimum
  169. pFlowspec->PeakBandwidth = (11 * pFlowspec->TokenRate) / 10;
  170. pFlowspec->TokenBucketSize = dwTotalSize * 4;
  171. pFlowspec->MinimumPolicedSize = dwTotalSize;
  172. pFlowspec->MaxSduSize = pFlowspec->MinimumPolicedSize;
  173. pFlowspec->Latency = QOS_NOT_SPECIFIED;
  174. pFlowspec->DelayVariation = QOS_NOT_SPECIFIED;
  175. pFlowspec->ServiceType = SERVICETYPE_GUARANTEED;
  176. return S_OK;
  177. }
  178. HRESULT InitVideoFlowspec(FLOWSPEC *pFlowspec, DWORD dwMaxBitRate, DWORD dwMaxFrag, DWORD dwAvgPacketSize)
  179. {
  180. DWORD dwPacketOverhead = 0;
  181. // I-Frames will be fragmented into 3 or 4 packets
  182. // and will be about 1000 bytes each
  183. // P-Frames average about 250 - 500 bytes each
  184. // we'll assume the reservation is a NetMeeting to NetMeeting call
  185. // so there will be few I-Frames sent
  186. if (ISWIN98GOLD())
  187. {
  188. dwPacketOverhead = IP_HEADER_SIZE + UDP_HEADER_SIZE;
  189. }
  190. // for 28.8 modems, 11kbits/sec have already been allocated for audio
  191. // so only allocate 17kbits/sec for video. This means that some packets
  192. // will be "non-conforming" and may receive less than a best-effort services.
  193. // but this is believed to be better than having no RSVP/QOS at all
  194. // if this becomes an issue, then it may be better to have no RSVP/QOS at all
  195. // maxBitRate will be equal to 14400 (14.4 modem), 28000 (28.8 modem), 85000 (ISDN/DSL), or 621700 (LAN)
  196. // (* .70, so really it can be: 10080, 20160, 59500, 435190)
  197. if (dwMaxBitRate <= BW_144KBS_BITS)
  198. {
  199. dwMaxBitRate = 4000; // is it worth it to attempt QOS at 14.4 ?
  200. }
  201. else if (dwMaxBitRate <= BW_288KBS_BITS)
  202. {
  203. dwMaxBitRate = 17000;
  204. }
  205. pFlowspec->TokenRate = dwMaxBitRate / 8;
  206. pFlowspec->MaxSduSize = dwMaxFrag + dwPacketOverhead + sizeof(RTP_HDR);
  207. pFlowspec->MinimumPolicedSize = dwAvgPacketSize + dwPacketOverhead;
  208. pFlowspec->PeakBandwidth = (DWORD)(pFlowspec->TokenRate * 1.2);
  209. pFlowspec->TokenBucketSize = dwMaxFrag * 3;
  210. pFlowspec->Latency = QOS_NOT_SPECIFIED;
  211. pFlowspec->DelayVariation = QOS_NOT_SPECIFIED;
  212. pFlowspec->ServiceType = SERVICETYPE_CONTROLLEDLOAD;
  213. return S_OK;
  214. }