captions-mssapi-stream.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. #include "captions-mssapi-stream.hpp"
  2. #include "captions-mssapi.hpp"
  3. #include <mmreg.h>
  4. #include <util/windows/CoTaskMemPtr.hpp>
  5. #include <util/threading.h>
  6. #include <util/base.h>
  7. using namespace std;
  8. #if 0
  9. #define debugfunc(format, ...) \
  10. blog(LOG_DEBUG, "[Captions] %s(" format ")", __FUNCTION__, \
  11. ##__VA_ARGS__)
  12. #else
  13. #define debugfunc(format, ...)
  14. #endif
  15. CaptionStream::CaptionStream(DWORD samplerate_, mssapi_captions *handler_)
  16. : handler(handler_),
  17. samplerate(samplerate_),
  18. event(CreateEvent(nullptr, false, false, nullptr))
  19. {
  20. buf_info.ulMsMinNotification = 50;
  21. buf_info.ulMsBufferSize = 500;
  22. buf_info.ulMsEventBias = 0;
  23. format.wFormatTag = WAVE_FORMAT_PCM;
  24. format.nChannels = 1;
  25. format.nSamplesPerSec = 16000;
  26. format.nAvgBytesPerSec = format.nSamplesPerSec * sizeof(uint16_t);
  27. format.nBlockAlign = 2;
  28. format.wBitsPerSample = 16;
  29. format.cbSize = sizeof(format);
  30. }
  31. void CaptionStream::Stop()
  32. {
  33. {
  34. lock_guard<mutex> lock(m);
  35. deque_free(buf);
  36. }
  37. cv.notify_one();
  38. }
  39. void CaptionStream::PushAudio(const void *data, size_t frames)
  40. {
  41. bool ready = false;
  42. lock_guard<mutex> lock(m);
  43. deque_push_back(buf, data, frames * sizeof(int16_t));
  44. write_pos += frames * sizeof(int16_t);
  45. if (wait_size && buf->size >= wait_size)
  46. ready = true;
  47. if (ready)
  48. cv.notify_one();
  49. }
  50. // IUnknown methods
  51. STDMETHODIMP CaptionStream::QueryInterface(REFIID riid, void **ppv)
  52. {
  53. if (riid == IID_IUnknown) {
  54. AddRef();
  55. *ppv = this;
  56. } else if (riid == IID_IStream) {
  57. AddRef();
  58. *ppv = (IStream *)this;
  59. } else if (riid == IID_ISpStreamFormat) {
  60. AddRef();
  61. *ppv = (ISpStreamFormat *)this;
  62. } else if (riid == IID_ISpAudio) {
  63. AddRef();
  64. *ppv = (ISpAudio *)this;
  65. } else {
  66. *ppv = nullptr;
  67. return E_NOINTERFACE;
  68. }
  69. return NOERROR;
  70. }
  71. STDMETHODIMP_(ULONG) CaptionStream::AddRef()
  72. {
  73. return (ULONG)os_atomic_inc_long(&refs);
  74. }
  75. STDMETHODIMP_(ULONG) CaptionStream::Release()
  76. {
  77. ULONG new_refs = (ULONG)os_atomic_dec_long(&refs);
  78. if (!new_refs)
  79. delete this;
  80. return new_refs;
  81. }
  82. // ISequentialStream methods
  83. STDMETHODIMP CaptionStream::Read(void *data, ULONG bytes, ULONG *read_bytes)
  84. {
  85. HRESULT hr = S_OK;
  86. size_t cur_size;
  87. debugfunc("data, %lu, read_bytes", bytes);
  88. if (!data)
  89. return STG_E_INVALIDPOINTER;
  90. {
  91. lock_guard<mutex> lock1(m);
  92. wait_size = bytes;
  93. cur_size = buf->size;
  94. }
  95. unique_lock<mutex> lock(m);
  96. if (bytes > cur_size)
  97. cv.wait(lock);
  98. if (bytes > (ULONG)buf->size) {
  99. bytes = (ULONG)buf->size;
  100. hr = S_FALSE;
  101. }
  102. if (bytes)
  103. deque_pop_front(buf, data, bytes);
  104. if (read_bytes)
  105. *read_bytes = bytes;
  106. wait_size = 0;
  107. pos.QuadPart += bytes;
  108. return hr;
  109. }
  110. STDMETHODIMP CaptionStream::Write(const void *, ULONG bytes, ULONG *)
  111. {
  112. debugfunc("data, %lu, written_bytes", bytes);
  113. return STG_E_INVALIDFUNCTION;
  114. }
  115. // IStream methods
  116. STDMETHODIMP CaptionStream::Seek(LARGE_INTEGER move, DWORD origin,
  117. ULARGE_INTEGER *new_pos)
  118. {
  119. debugfunc("%lld, %lx, new_pos", move, origin);
  120. if (!new_pos)
  121. return E_POINTER;
  122. if (origin != SEEK_CUR || move.QuadPart != 0)
  123. return E_NOTIMPL;
  124. *new_pos = pos;
  125. return S_OK;
  126. }
  127. STDMETHODIMP CaptionStream::SetSize(ULARGE_INTEGER new_size)
  128. {
  129. debugfunc("%llu", new_size);
  130. return STG_E_INVALIDFUNCTION;
  131. }
  132. STDMETHODIMP CaptionStream::CopyTo(IStream *stream, ULARGE_INTEGER bytes,
  133. ULARGE_INTEGER *read_bytes,
  134. ULARGE_INTEGER *written_bytes)
  135. {
  136. HRESULT hr;
  137. debugfunc("stream, %llu, read_bytes, written_bytes", bytes);
  138. if (!stream)
  139. return STG_E_INVALIDPOINTER;
  140. ULONG written = 0;
  141. if (bytes.QuadPart > (ULONGLONG)buf->size)
  142. bytes.QuadPart = (ULONGLONG)buf->size;
  143. lock_guard<mutex> lock(m);
  144. temp_buf.resize((size_t)bytes.QuadPart);
  145. deque_peek_front(buf, &temp_buf[0], (size_t)bytes.QuadPart);
  146. hr = stream->Write(temp_buf.data(), (ULONG)bytes.QuadPart, &written);
  147. if (read_bytes)
  148. *read_bytes = bytes;
  149. if (written_bytes)
  150. written_bytes->QuadPart = written;
  151. return hr;
  152. }
  153. STDMETHODIMP CaptionStream::Commit(DWORD commit_flags)
  154. {
  155. debugfunc("%lx", commit_flags);
  156. /* TODO? */
  157. return S_OK;
  158. }
  159. STDMETHODIMP CaptionStream::Revert(void)
  160. {
  161. debugfunc("");
  162. return S_OK;
  163. }
  164. STDMETHODIMP CaptionStream::LockRegion(ULARGE_INTEGER offset,
  165. ULARGE_INTEGER size, DWORD type)
  166. {
  167. debugfunc("%llu, %llu, %ld", offset, size, type);
  168. /* TODO? */
  169. return STG_E_INVALIDFUNCTION;
  170. }
  171. STDMETHODIMP CaptionStream::UnlockRegion(ULARGE_INTEGER offset,
  172. ULARGE_INTEGER size, DWORD type)
  173. {
  174. debugfunc("%llu, %llu, %ld", offset, size, type);
  175. /* TODO? */
  176. return STG_E_INVALIDFUNCTION;
  177. }
  178. static const wchar_t *stat_name = L"Caption stream";
  179. STDMETHODIMP CaptionStream::Stat(STATSTG *stg, DWORD flag)
  180. {
  181. debugfunc("stg, %lu", flag);
  182. if (!stg)
  183. return E_POINTER;
  184. lock_guard<mutex> lock(m);
  185. *stg = {};
  186. stg->type = STGTY_STREAM;
  187. stg->cbSize.QuadPart = (ULONGLONG)buf->size;
  188. if (flag == STATFLAG_DEFAULT) {
  189. size_t byte_size = (wcslen(stat_name) + 1) * sizeof(wchar_t);
  190. stg->pwcsName = (wchar_t *)CoTaskMemAlloc(byte_size);
  191. memcpy(stg->pwcsName, stat_name, byte_size);
  192. }
  193. return S_OK;
  194. }
  195. STDMETHODIMP CaptionStream::Clone(IStream **stream)
  196. {
  197. debugfunc("stream");
  198. *stream = nullptr;
  199. return E_NOTIMPL;
  200. }
  201. // ISpStreamFormat methods
  202. STDMETHODIMP CaptionStream::GetFormat(GUID *guid,
  203. WAVEFORMATEX **co_mem_wfex_out)
  204. {
  205. debugfunc("guid, co_mem_wfex_out");
  206. if (!guid || !co_mem_wfex_out)
  207. return E_POINTER;
  208. if (format.wFormatTag == 0) {
  209. *co_mem_wfex_out = nullptr;
  210. return S_OK;
  211. }
  212. void *wfex = CoTaskMemAlloc(sizeof(format));
  213. memcpy(wfex, &format, sizeof(format));
  214. *co_mem_wfex_out = (WAVEFORMATEX *)wfex;
  215. return S_OK;
  216. }
  217. // ISpAudio methods
  218. STDMETHODIMP CaptionStream::SetState(SPAUDIOSTATE state_, ULONGLONG)
  219. {
  220. debugfunc("%lu, reserved", state_);
  221. state = state_;
  222. return S_OK;
  223. }
  224. STDMETHODIMP CaptionStream::SetFormat(REFGUID guid_ref,
  225. const WAVEFORMATEX *wfex)
  226. {
  227. debugfunc("guid, wfex");
  228. if (!wfex)
  229. return E_INVALIDARG;
  230. if (guid_ref == SPDFID_WaveFormatEx) {
  231. lock_guard<mutex> lock(m);
  232. memcpy(&format, wfex, sizeof(format));
  233. if (!handler->reset_resampler(AUDIO_FORMAT_16BIT,
  234. wfex->nSamplesPerSec))
  235. return E_FAIL;
  236. /* 50 msec */
  237. DWORD size = format.nSamplesPerSec / 20;
  238. DWORD byte_size = size * format.nBlockAlign;
  239. deque_reserve(buf, (size_t)byte_size);
  240. }
  241. return S_OK;
  242. }
  243. STDMETHODIMP CaptionStream::GetStatus(SPAUDIOSTATUS *status)
  244. {
  245. debugfunc("status");
  246. if (!status)
  247. return E_POINTER;
  248. /* TODO? */
  249. lock_guard<mutex> lock(m);
  250. *status = {};
  251. status->cbNonBlockingIO = (ULONG)buf->size;
  252. status->State = state;
  253. status->CurSeekPos = pos.QuadPart;
  254. status->CurDevicePos = write_pos;
  255. return S_OK;
  256. }
  257. STDMETHODIMP CaptionStream::SetBufferInfo(const SPAUDIOBUFFERINFO *buf_info_)
  258. {
  259. debugfunc("buf_info");
  260. /* TODO */
  261. buf_info = *buf_info_;
  262. return S_OK;
  263. }
  264. STDMETHODIMP CaptionStream::GetBufferInfo(SPAUDIOBUFFERINFO *buf_info_)
  265. {
  266. debugfunc("buf_info");
  267. if (!buf_info_)
  268. return E_POINTER;
  269. *buf_info_ = buf_info;
  270. return S_OK;
  271. }
  272. STDMETHODIMP CaptionStream::GetDefaultFormat(GUID *format,
  273. WAVEFORMATEX **co_mem_wfex_out)
  274. {
  275. debugfunc("format, co_mem_wfex_out");
  276. if (!format || !co_mem_wfex_out)
  277. return E_POINTER;
  278. void *wfex = CoTaskMemAlloc(sizeof(format));
  279. memcpy(wfex, &format, sizeof(format));
  280. *format = SPDFID_WaveFormatEx;
  281. *co_mem_wfex_out = (WAVEFORMATEX *)wfex;
  282. return S_OK;
  283. }
  284. STDMETHODIMP_(HANDLE) CaptionStream::EventHandle(void)
  285. {
  286. debugfunc("");
  287. return event;
  288. }
  289. STDMETHODIMP CaptionStream::GetVolumeLevel(ULONG *level)
  290. {
  291. debugfunc("level");
  292. if (!level)
  293. return E_POINTER;
  294. *level = vol;
  295. return S_OK;
  296. }
  297. STDMETHODIMP CaptionStream::SetVolumeLevel(ULONG level)
  298. {
  299. debugfunc("%lu", level);
  300. vol = level;
  301. return S_OK;
  302. }
  303. STDMETHODIMP CaptionStream::GetBufferNotifySize(ULONG *size)
  304. {
  305. debugfunc("size");
  306. if (!size)
  307. return E_POINTER;
  308. *size = notify_size;
  309. return S_OK;
  310. }
  311. STDMETHODIMP CaptionStream::SetBufferNotifySize(ULONG size)
  312. {
  313. debugfunc("%lu", size);
  314. notify_size = size;
  315. return S_OK;
  316. }