Source code of Windows XP (NT5)
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.

165 lines
5.0 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. // INTEL Corporation Proprietary Information
  3. // This listing is supplied under the terms of a license agreement with Intel
  4. // Corporation and many not be copied nor disclosed except in accordance
  5. // with the terms of that agreement.
  6. // Copyright (c) 1995, 1996 Intel Corporation.
  7. //
  8. //
  9. // Module Name: g723snd.cpp
  10. // Environment: MSVC 4.0, OLE 2
  11. /////////////////////////////////////////////////////////////////////////////////
  12. #include "g723snd.h"
  13. G723_ppmSend::G723_ppmSend(IUnknown* pUnkOuter,
  14. IUnknown** ppUnkInner) :
  15. ppmSend(G723_PT, 0,
  16. 8000, pUnkOuter, ppUnkInner),
  17. m_dwLastTimeStamp(0)
  18. {
  19. }
  20. G723_ppmSend::~G723_ppmSend()
  21. {
  22. }
  23. IMPLEMENT_CREATEPROC(G723_ppmSend);
  24. //////////////////////////////////////////////////////////////////////////////////////////
  25. //SetMarkerBit: Determines whether to set the marker bit or not. lastPacket is TRUE if
  26. // this is the last packet of the frame; FALSE if not. With audio, we don't
  27. // don't care about fragmentation, just the start of a talkspurt.
  28. //////////////////////////////////////////////////////////////////////////////////////////
  29. BOOL G723_ppmSend::SetMarkerBit(BOOL lastPacket)
  30. {
  31. return m_markTalkSpurt;
  32. }
  33. //////////////////////////////////////////////////////////////////////////////////////////
  34. //MakeTimeStamp: Generate a time stamp based on the frequency specified in the Profile Spec.
  35. //////////////////////////////////////////////////////////////////////////////////////////
  36. DWORD G723_ppmSend::MakeTimeStamp(MsgDescriptor* pMsgDescrip,
  37. BOOL bStartStream,
  38. BOOL bUseInputTime)
  39. {
  40. #ifndef TIMESTAMP_OFF
  41. DWORD ThisTimeStamp;
  42. DWORD CurTime = timeGetTime();
  43. DWORD delta;
  44. DWORD epsilon;
  45. if (bUseInputTime) CurTime = pMsgDescrip->m_TimeStamp;
  46. // calculate the time span encoded in this packet
  47. delta = 240 * CountFrames((char *) pMsgDescrip->m_pBuffer, pMsgDescrip->m_Size) / (m_Frequency/1000);
  48. epsilon = delta/2;
  49. // init, do it here so it is set when we get the first packet
  50. // not at init time, they may be significantly different
  51. // Generate our first time stamp based on the current time.
  52. if (m_dwStartTime == 0)
  53. {
  54. // if the first packet we receive is a drop or silence then the delta will
  55. // be zero. We just won't do anything until we receive valid data.
  56. if (delta != 0)
  57. {
  58. m_dwStartTime = CurTime;
  59. m_dwLastTime = m_dwStartTime - delta;
  60. ThisTimeStamp = (((CurTime - m_dwStartTime) + (epsilon)) / delta) * delta * (m_Frequency/1000);
  61. }
  62. }
  63. else
  64. if (bStartStream)
  65. {
  66. // bStartStream will be set if this is the first packet after a break in a
  67. // data stream. We need to get our time stamps back on track, so we'll generate a time
  68. // based on the current time. This case can happen if for some reason the capture device
  69. // gets starved or we are in half duplex and we are switching to talk mode.
  70. if (delta != 0)
  71. {
  72. ThisTimeStamp = (((CurTime - m_dwStartTime) + (epsilon)) / delta) * delta * (m_Frequency/1000);
  73. }
  74. else
  75. {
  76. // we don't have a valid delta, so we'll make do with the previous delta
  77. ThisTimeStamp = (((CurTime - m_dwStartTime) + (epsilon)) / m_dwLastDelta) * m_dwLastDelta * (m_Frequency/1000);
  78. ThisTimeStamp -= m_dwLastDelta * (m_Frequency/1000);
  79. }
  80. }
  81. else
  82. {
  83. // if we are in a continuous audio data stream, then we just want to increment our timestamp
  84. // for this data packet. We don't want to use the current time because we don't know how long
  85. // it took from the time the data was acutally captured to the time we got it. We have to rely
  86. // on the person feeding us data to let us know more information about the data stream.
  87. ThisTimeStamp = m_dwLastTimeStamp + 240 * CountFrames((char *) pMsgDescrip->m_pBuffer, pMsgDescrip->m_Size);
  88. }
  89. m_dwLastTimeStamp = ThisTimeStamp;
  90. m_dwLastTime = CurTime;
  91. if (delta != 0)
  92. {
  93. m_dwLastDelta = delta;
  94. }
  95. return ThisTimeStamp;
  96. #else
  97. //I changed this because the times I was getting were widely spaced. When I was in debugging
  98. //mode.
  99. static DWORD CurTime = 0;
  100. CurTime++;
  101. #endif
  102. return CurTime;
  103. }
  104. int G723_ppmSend::CountFrames(char *ipBuffer, int len)
  105. {
  106. int Count = 0;
  107. int Delta = 0;
  108. int Length = len;
  109. LPBYTE pBuffer = (LPBYTE) ipBuffer;
  110. while(Length >= 4)
  111. {
  112. if (pBuffer[0] & 2) // silence frame
  113. {
  114. if (Length < 4) // invalid frame
  115. {
  116. break;
  117. }
  118. Delta = 4;
  119. //TTDBG(ghISRInst, TT_NOTIFY, "Dejitter::CountFrames - found silence frame");
  120. }
  121. else if (pBuffer[0] & 1) // 20 byte frame (5.3k)
  122. {
  123. if (Length < 20) // invalid frame
  124. {
  125. break;
  126. }
  127. Delta = 20;
  128. }
  129. else // 24 byte frame (6.3k)
  130. {
  131. if (Length < 24) // invalid frame
  132. {
  133. break;
  134. }
  135. Delta = 24;
  136. }
  137. Count++;
  138. pBuffer += Delta;
  139. Length -= Delta;
  140. }
  141. return Count;
  142. }