FLVCOM.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "FLVCOM.h"
  2. #include "AMFDispatch.h"
  3. FLVCOM flvCOM;
  4. extern bool mute;
  5. static HANDLE DuplicateCurrentThread()
  6. {
  7. HANDLE fakeHandle = GetCurrentThread();
  8. HANDLE copiedHandle = 0;
  9. HANDLE processHandle = GetCurrentProcess();
  10. DuplicateHandle(processHandle, fakeHandle, processHandle, &copiedHandle, 0, FALSE, DUPLICATE_SAME_ACCESS);
  11. return copiedHandle;
  12. }
  13. enum
  14. {
  15. DISP_FLV_REGISTER_CALLBACK,
  16. DISP_FLV_UNREGISTER_CALLBACK,
  17. DISP_FLV_SETMUTE,
  18. };
  19. #define CHECK_ID(str, id) if (wcscmp(rgszNames[i], L##str) == 0) { rgdispid[i] = id; continue; }
  20. HRESULT FLVCOM::GetIDsOfNames(REFIID riid, OLECHAR FAR* FAR* rgszNames, unsigned int cNames, LCID lcid, DISPID FAR* rgdispid)
  21. {
  22. bool unknowns = false;
  23. for (unsigned int i = 0;i != cNames;i++)
  24. {
  25. CHECK_ID("RegisterCallback", DISP_FLV_REGISTER_CALLBACK)
  26. CHECK_ID("UnregisterCallback", DISP_FLV_UNREGISTER_CALLBACK)
  27. CHECK_ID("SetMute", DISP_FLV_SETMUTE)
  28. rgdispid[i] = DISPID_UNKNOWN;
  29. unknowns = true;
  30. }
  31. if (unknowns)
  32. return DISP_E_UNKNOWNNAME;
  33. else
  34. return S_OK;
  35. }
  36. HRESULT FLVCOM::GetTypeInfo(unsigned int itinfo, LCID lcid, ITypeInfo FAR* FAR* pptinfo)
  37. {
  38. return E_NOTIMPL;
  39. }
  40. HRESULT FLVCOM::GetTypeInfoCount(unsigned int FAR * pctinfo)
  41. {
  42. return E_NOTIMPL;
  43. }
  44. HRESULT FLVCOM::Invoke(DISPID dispid, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, EXCEPINFO FAR * pexecinfo, unsigned int FAR *puArgErr)
  45. {
  46. if (pvarResult)
  47. VariantInit(pvarResult);
  48. switch (dispid)
  49. {
  50. case DISP_FLV_SETMUTE:
  51. {
  52. if (pdispparams->rgvarg[0].boolVal)
  53. mute = true;
  54. else
  55. mute = false;
  56. }
  57. return S_OK;
  58. case DISP_FLV_REGISTER_CALLBACK:
  59. {
  60. IDispatch *callback = pdispparams->rgvarg[0].pdispVal;
  61. callbacks.push_back(DispatchCallbackInfo(callback, GetCurrentThreadId(), DuplicateCurrentThread()));
  62. return S_OK;
  63. }
  64. break;
  65. case DISP_FLV_UNREGISTER_CALLBACK:
  66. {
  67. IDispatch *callback = pdispparams->rgvarg[0].pdispVal;
  68. size_t numCallbacks = callbacks.size();
  69. while (numCallbacks--)
  70. {
  71. if (callbacks[numCallbacks].dispatch == callback)
  72. {
  73. CloseHandle(callbacks[numCallbacks].threadHandle);
  74. callbacks.erase(callbacks.begin() + numCallbacks);
  75. }
  76. }
  77. return S_OK;
  78. }
  79. break;
  80. }
  81. return DISP_E_MEMBERNOTFOUND;
  82. }
  83. STDMETHODIMP FLVCOM::QueryInterface(REFIID riid, PVOID *ppvObject)
  84. {
  85. if (!ppvObject)
  86. return E_POINTER;
  87. else if (IsEqualIID(riid, IID_IDispatch))
  88. *ppvObject = (IDispatch *)this;
  89. else if (IsEqualIID(riid, IID_IUnknown))
  90. *ppvObject = this;
  91. else
  92. {
  93. *ppvObject = NULL;
  94. return E_NOINTERFACE;
  95. }
  96. AddRef();
  97. return S_OK;
  98. }
  99. ULONG FLVCOM::AddRef(void)
  100. {
  101. return 0;
  102. }
  103. ULONG FLVCOM::Release(void)
  104. {
  105. return 0;
  106. }
  107. static void CallDispatchMethod(IDispatch *dispatch, DISPPARAMS &params, OLECHAR *name)
  108. {
  109. try
  110. {
  111. unsigned int ret;
  112. DISPID dispid;
  113. if (SUCCEEDED(dispatch->GetIDsOfNames(IID_NULL, &name, 1, LOCALE_SYSTEM_DEFAULT, &dispid)))
  114. dispatch->Invoke(dispid, GUID_NULL, 0, DISPATCH_METHOD, &params, 0, 0, &ret);
  115. }
  116. catch (...)
  117. {}
  118. }
  119. struct APCdata
  120. {
  121. IDispatch *disp;
  122. wchar_t *name;
  123. AMFDispatch *amf;
  124. };
  125. static VOID CALLBACK MetadataAPC(ULONG_PTR param)
  126. {
  127. APCdata *data = (APCdata *)param;
  128. DISPPARAMS params;
  129. VARIANT argument;
  130. params.cArgs = 1;
  131. params.cNamedArgs = 0;
  132. params.rgdispidNamedArgs = 0;
  133. params.rgvarg = &argument;
  134. VariantInit(&argument);
  135. V_VT(&argument) = VT_DISPATCH;
  136. V_DISPATCH(&argument) = data->amf;
  137. CallDispatchMethod(data->disp, params, data->name);
  138. data->disp->Release();
  139. data->amf->Release();
  140. free(data->name);
  141. delete data;
  142. }
  143. void FLVCOM::MetadataCallback(FLVMetadata::Tag *tag)
  144. {
  145. if (!callbacks.empty())
  146. {
  147. AMFDispatch *disp = new AMFDispatch(tag->parameters); // we're newing this we can refcount
  148. DWORD curThreadId = GetCurrentThreadId();
  149. for (size_t i = 0;i != callbacks.size();i++)
  150. {
  151. APCdata *data = new APCdata;
  152. data->disp = callbacks[i].dispatch;
  153. data->disp->AddRef();
  154. data->name = _wcsdup(tag->name.str);
  155. data->amf = disp;
  156. data->amf->AddRef();
  157. if (curThreadId == callbacks[i].threadId)
  158. MetadataAPC((ULONG_PTR)data);
  159. else
  160. {
  161. if (callbacks[i].threadHandle)
  162. QueueUserAPC(MetadataAPC, callbacks[i].threadHandle, (ULONG_PTR)data);
  163. }
  164. }
  165. disp->Release();
  166. }
  167. }