captions-stream.cpp 8.1 KB

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