captions-mssapi-stream.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. circlebuf_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. circlebuf_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. circlebuf_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. UNUSED_PARAMETER(bytes);
  114. return STG_E_INVALIDFUNCTION;
  115. }
  116. // IStream methods
  117. STDMETHODIMP CaptionStream::Seek(LARGE_INTEGER move, DWORD origin,
  118. ULARGE_INTEGER *new_pos)
  119. {
  120. debugfunc("%lld, %lx, new_pos", move, origin);
  121. UNUSED_PARAMETER(move);
  122. UNUSED_PARAMETER(origin);
  123. if (!new_pos)
  124. return E_POINTER;
  125. if (origin != SEEK_CUR || move.QuadPart != 0)
  126. return E_NOTIMPL;
  127. *new_pos = pos;
  128. return S_OK;
  129. }
  130. STDMETHODIMP CaptionStream::SetSize(ULARGE_INTEGER new_size)
  131. {
  132. debugfunc("%llu", new_size);
  133. UNUSED_PARAMETER(new_size);
  134. return STG_E_INVALIDFUNCTION;
  135. }
  136. STDMETHODIMP CaptionStream::CopyTo(IStream *stream, ULARGE_INTEGER bytes,
  137. ULARGE_INTEGER *read_bytes,
  138. ULARGE_INTEGER *written_bytes)
  139. {
  140. HRESULT hr;
  141. debugfunc("stream, %llu, read_bytes, written_bytes", bytes);
  142. if (!stream)
  143. return STG_E_INVALIDPOINTER;
  144. ULONG written = 0;
  145. if (bytes.QuadPart > (ULONGLONG)buf->size)
  146. bytes.QuadPart = (ULONGLONG)buf->size;
  147. lock_guard<mutex> lock(m);
  148. temp_buf.resize((size_t)bytes.QuadPart);
  149. circlebuf_peek_front(buf, &temp_buf[0], (size_t)bytes.QuadPart);
  150. hr = stream->Write(temp_buf.data(), (ULONG)bytes.QuadPart, &written);
  151. if (read_bytes)
  152. *read_bytes = bytes;
  153. if (written_bytes)
  154. written_bytes->QuadPart = written;
  155. return hr;
  156. }
  157. STDMETHODIMP CaptionStream::Commit(DWORD commit_flags)
  158. {
  159. debugfunc("%lx", commit_flags);
  160. UNUSED_PARAMETER(commit_flags);
  161. /* TODO? */
  162. return S_OK;
  163. }
  164. STDMETHODIMP CaptionStream::Revert(void)
  165. {
  166. debugfunc("");
  167. return S_OK;
  168. }
  169. STDMETHODIMP CaptionStream::LockRegion(ULARGE_INTEGER offset,
  170. ULARGE_INTEGER size, DWORD type)
  171. {
  172. debugfunc("%llu, %llu, %ld", offset, size, type);
  173. UNUSED_PARAMETER(offset);
  174. UNUSED_PARAMETER(size);
  175. UNUSED_PARAMETER(type);
  176. /* TODO? */
  177. return STG_E_INVALIDFUNCTION;
  178. }
  179. STDMETHODIMP CaptionStream::UnlockRegion(ULARGE_INTEGER offset,
  180. ULARGE_INTEGER size, DWORD type)
  181. {
  182. debugfunc("%llu, %llu, %ld", offset, size, type);
  183. UNUSED_PARAMETER(offset);
  184. UNUSED_PARAMETER(size);
  185. UNUSED_PARAMETER(type);
  186. /* TODO? */
  187. return STG_E_INVALIDFUNCTION;
  188. }
  189. static const wchar_t *stat_name = L"Caption stream";
  190. STDMETHODIMP CaptionStream::Stat(STATSTG *stg, DWORD flag)
  191. {
  192. debugfunc("stg, %lu", flag);
  193. if (!stg)
  194. return E_POINTER;
  195. lock_guard<mutex> lock(m);
  196. *stg = {};
  197. stg->type = STGTY_STREAM;
  198. stg->cbSize.QuadPart = (ULONGLONG)buf->size;
  199. if (flag == STATFLAG_DEFAULT) {
  200. size_t byte_size = (wcslen(stat_name) + 1) * sizeof(wchar_t);
  201. stg->pwcsName = (wchar_t *)CoTaskMemAlloc(byte_size);
  202. memcpy(stg->pwcsName, stat_name, byte_size);
  203. }
  204. return S_OK;
  205. }
  206. STDMETHODIMP CaptionStream::Clone(IStream **stream)
  207. {
  208. debugfunc("stream");
  209. *stream = nullptr;
  210. return E_NOTIMPL;
  211. }
  212. // ISpStreamFormat methods
  213. STDMETHODIMP CaptionStream::GetFormat(GUID *guid,
  214. WAVEFORMATEX **co_mem_wfex_out)
  215. {
  216. debugfunc("guid, co_mem_wfex_out");
  217. if (!guid || !co_mem_wfex_out)
  218. return E_POINTER;
  219. if (format.wFormatTag == 0) {
  220. *co_mem_wfex_out = nullptr;
  221. return S_OK;
  222. }
  223. void *wfex = CoTaskMemAlloc(sizeof(format));
  224. memcpy(wfex, &format, sizeof(format));
  225. *co_mem_wfex_out = (WAVEFORMATEX *)wfex;
  226. return S_OK;
  227. }
  228. // ISpAudio methods
  229. STDMETHODIMP CaptionStream::SetState(SPAUDIOSTATE state_, ULONGLONG)
  230. {
  231. debugfunc("%lu, reserved", state_);
  232. state = state_;
  233. return S_OK;
  234. }
  235. STDMETHODIMP CaptionStream::SetFormat(REFGUID guid_ref,
  236. const WAVEFORMATEX *wfex)
  237. {
  238. debugfunc("guid, wfex");
  239. if (!wfex)
  240. return E_INVALIDARG;
  241. if (guid_ref == SPDFID_WaveFormatEx) {
  242. lock_guard<mutex> lock(m);
  243. memcpy(&format, wfex, sizeof(format));
  244. if (!handler->reset_resampler(AUDIO_FORMAT_16BIT,
  245. wfex->nSamplesPerSec))
  246. return E_FAIL;
  247. /* 50 msec */
  248. DWORD size = format.nSamplesPerSec / 20;
  249. DWORD byte_size = size * format.nBlockAlign;
  250. circlebuf_reserve(buf, (size_t)byte_size);
  251. }
  252. return S_OK;
  253. }
  254. STDMETHODIMP CaptionStream::GetStatus(SPAUDIOSTATUS *status)
  255. {
  256. debugfunc("status");
  257. if (!status)
  258. return E_POINTER;
  259. /* TODO? */
  260. lock_guard<mutex> lock(m);
  261. *status = {};
  262. status->cbNonBlockingIO = (ULONG)buf->size;
  263. status->State = state;
  264. status->CurSeekPos = pos.QuadPart;
  265. status->CurDevicePos = write_pos;
  266. return S_OK;
  267. }
  268. STDMETHODIMP CaptionStream::SetBufferInfo(const SPAUDIOBUFFERINFO *buf_info_)
  269. {
  270. debugfunc("buf_info");
  271. /* TODO */
  272. buf_info = *buf_info_;
  273. return S_OK;
  274. }
  275. STDMETHODIMP CaptionStream::GetBufferInfo(SPAUDIOBUFFERINFO *buf_info_)
  276. {
  277. debugfunc("buf_info");
  278. if (!buf_info_)
  279. return E_POINTER;
  280. *buf_info_ = buf_info;
  281. return S_OK;
  282. }
  283. STDMETHODIMP CaptionStream::GetDefaultFormat(GUID *format,
  284. WAVEFORMATEX **co_mem_wfex_out)
  285. {
  286. debugfunc("format, co_mem_wfex_out");
  287. if (!format || !co_mem_wfex_out)
  288. return E_POINTER;
  289. void *wfex = CoTaskMemAlloc(sizeof(format));
  290. memcpy(wfex, &format, sizeof(format));
  291. *format = SPDFID_WaveFormatEx;
  292. *co_mem_wfex_out = (WAVEFORMATEX *)wfex;
  293. return S_OK;
  294. }
  295. STDMETHODIMP_(HANDLE) CaptionStream::EventHandle(void)
  296. {
  297. debugfunc("");
  298. return event;
  299. }
  300. STDMETHODIMP CaptionStream::GetVolumeLevel(ULONG *level)
  301. {
  302. debugfunc("level");
  303. if (!level)
  304. return E_POINTER;
  305. *level = vol;
  306. return S_OK;
  307. }
  308. STDMETHODIMP CaptionStream::SetVolumeLevel(ULONG level)
  309. {
  310. debugfunc("%lu", level);
  311. vol = level;
  312. return S_OK;
  313. }
  314. STDMETHODIMP CaptionStream::GetBufferNotifySize(ULONG *size)
  315. {
  316. debugfunc("size");
  317. if (!size)
  318. return E_POINTER;
  319. *size = notify_size;
  320. return S_OK;
  321. }
  322. STDMETHODIMP CaptionStream::SetBufferNotifySize(ULONG size)
  323. {
  324. debugfunc("%lu", size);
  325. notify_size = size;
  326. return S_OK;
  327. }