File: captions-mssapi-stream.cpp

package info (click to toggle)
obs-studio 22.0.3%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 23,052 kB
  • sloc: ansic: 134,708; cpp: 49,169; objc: 1,036; makefile: 829; sh: 410; python: 360
file content (408 lines) | stat: -rw-r--r-- 8,158 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include "captions-mssapi-stream.hpp"
#include "captions-mssapi.hpp"
#include <mmreg.h>
#include <util/windows/CoTaskMemPtr.hpp>
#include <util/threading.h>
#include <util/base.h>

using namespace std;

#if 0
#define debugfunc(format, ...) blog(LOG_DEBUG, "[Captions] %s(" format ")", \
		__FUNCTION__, ##__VA_ARGS__)
#else
#define debugfunc(format, ...)
#endif

CaptionStream::CaptionStream(DWORD samplerate_, mssapi_captions *handler_) :
	handler(handler_),
	samplerate(samplerate_),
	event(CreateEvent(nullptr, false, false, nullptr))
{
	buf_info.ulMsMinNotification = 50;
	buf_info.ulMsBufferSize = 500;
	buf_info.ulMsEventBias = 0;

	format.wFormatTag = WAVE_FORMAT_PCM;
	format.nChannels = 1;
	format.nSamplesPerSec = 16000;
	format.nAvgBytesPerSec = format.nSamplesPerSec * sizeof(uint16_t);
	format.nBlockAlign = 2;
	format.wBitsPerSample = 16;
	format.cbSize = sizeof(format);
}

void CaptionStream::Stop()
{
	{
		lock_guard<mutex> lock(m);
		circlebuf_free(buf);
	}

	cv.notify_one();
}

void CaptionStream::PushAudio(const void *data, size_t frames)
{
	bool ready = false;

	lock_guard<mutex> lock(m);
	circlebuf_push_back(buf, data, frames * sizeof(int16_t));
	write_pos += frames * sizeof(int16_t);

	if (wait_size && buf->size >= wait_size)
		ready = true;
	if (ready)
		cv.notify_one();
}

// IUnknown methods

STDMETHODIMP CaptionStream::QueryInterface(REFIID riid, void **ppv)
{
	if (riid == IID_IUnknown) {
		AddRef();
		*ppv = this;

	} else if (riid == IID_IStream) {
		AddRef();
		*ppv = (IStream*)this;

	} else if (riid == IID_ISpStreamFormat) {
		AddRef();
		*ppv = (ISpStreamFormat*)this;

	} else if (riid == IID_ISpAudio) {
		AddRef();
		*ppv = (ISpAudio*)this;

	} else {
		*ppv = nullptr;
		return E_NOINTERFACE;
	}

	return NOERROR;
}

STDMETHODIMP_(ULONG) CaptionStream::AddRef()
{
	return (ULONG)os_atomic_inc_long(&refs);
}

STDMETHODIMP_(ULONG) CaptionStream::Release()
{
	ULONG new_refs = (ULONG)os_atomic_dec_long(&refs);
	if (!new_refs)
		delete this;

	return new_refs;
}

// ISequentialStream methods

STDMETHODIMP CaptionStream::Read(void *data, ULONG bytes, ULONG *read_bytes)
{
	HRESULT hr = S_OK;
	size_t cur_size;

	debugfunc("data, %lu, read_bytes", bytes);
	if (!data)
		return STG_E_INVALIDPOINTER;

	{
		lock_guard<mutex> lock1(m);
		wait_size = bytes;
		cur_size = buf->size;
	}

	unique_lock<mutex> lock(m);

	if (bytes > cur_size)
		cv.wait(lock);

	if (bytes > (ULONG)buf->size) {
		bytes = (ULONG)buf->size;
		hr = S_FALSE;
	}
	if (bytes)
		circlebuf_pop_front(buf, data, bytes);
	if (read_bytes)
		*read_bytes = bytes;

	wait_size = 0;
	pos.QuadPart += bytes;
	return hr;
}

STDMETHODIMP CaptionStream::Write(const void *, ULONG bytes,
		ULONG*)
{
	debugfunc("data, %lu, written_bytes", bytes);
	UNUSED_PARAMETER(bytes);

	return STG_E_INVALIDFUNCTION;
}

// IStream methods

STDMETHODIMP CaptionStream::Seek(LARGE_INTEGER move, DWORD origin,
		ULARGE_INTEGER *new_pos)
{
	debugfunc("%lld, %lx, new_pos", move, origin);
	UNUSED_PARAMETER(move);
	UNUSED_PARAMETER(origin);

	if (!new_pos)
		return E_POINTER;

	if (origin != SEEK_CUR || move.QuadPart != 0)
		return E_NOTIMPL;

	*new_pos = pos;
	return S_OK;
}

STDMETHODIMP CaptionStream::SetSize(ULARGE_INTEGER new_size)
{
	debugfunc("%llu", new_size);
	UNUSED_PARAMETER(new_size);
	return STG_E_INVALIDFUNCTION;
}

STDMETHODIMP CaptionStream::CopyTo(IStream *stream, ULARGE_INTEGER bytes,
		ULARGE_INTEGER *read_bytes,
		ULARGE_INTEGER *written_bytes)
{
	HRESULT hr;

	debugfunc("stream, %llu, read_bytes, written_bytes", bytes);

	if (!stream)
		return STG_E_INVALIDPOINTER;

	ULONG written = 0;
	if (bytes.QuadPart > (ULONGLONG)buf->size)
		bytes.QuadPart = (ULONGLONG)buf->size;

	lock_guard<mutex> lock(m);
	temp_buf.resize((size_t)bytes.QuadPart);
	circlebuf_peek_front(buf, &temp_buf[0], (size_t)bytes.QuadPart);

	hr = stream->Write(temp_buf.data(), (ULONG)bytes.QuadPart, &written);

	if (read_bytes)
		*read_bytes = bytes;
	if (written_bytes)
		written_bytes->QuadPart = written;

	return hr;
}

STDMETHODIMP CaptionStream::Commit(DWORD commit_flags)
{
	debugfunc("%lx", commit_flags);
	UNUSED_PARAMETER(commit_flags);
	/* TODO? */
	return S_OK;
}

STDMETHODIMP CaptionStream::Revert(void)
{
	debugfunc("");
	return S_OK;
}

STDMETHODIMP CaptionStream::LockRegion(ULARGE_INTEGER offset,
		ULARGE_INTEGER size, DWORD type)
{
	debugfunc("%llu, %llu, %ld", offset, size, type);
	UNUSED_PARAMETER(offset);
	UNUSED_PARAMETER(size);
	UNUSED_PARAMETER(type);
	/* TODO? */
	return STG_E_INVALIDFUNCTION;
}

STDMETHODIMP CaptionStream::UnlockRegion(ULARGE_INTEGER offset,
		ULARGE_INTEGER size, DWORD type)
{
	debugfunc("%llu, %llu, %ld", offset, size, type);
	UNUSED_PARAMETER(offset);
	UNUSED_PARAMETER(size);
	UNUSED_PARAMETER(type);
	/* TODO? */
	return STG_E_INVALIDFUNCTION;
}

static const wchar_t *stat_name = L"Caption stream";

STDMETHODIMP CaptionStream::Stat(STATSTG *stg, DWORD flag)
{
	debugfunc("stg, %lu", flag);

	if (!stg)
		return E_POINTER;

	lock_guard<mutex> lock(m);
	*stg = {};
	stg->type = STGTY_STREAM;
	stg->cbSize.QuadPart = (ULONGLONG)buf->size;

	if (flag == STATFLAG_DEFAULT) {
		stg->pwcsName = (wchar_t*)CoTaskMemAlloc(sizeof(stat_name));
		memcpy(stg->pwcsName, stat_name, sizeof(stat_name));
	}

	return S_OK;
}

STDMETHODIMP CaptionStream::Clone(IStream **stream)
{
	debugfunc("stream");
	*stream = nullptr;
	return E_NOTIMPL;
}

// ISpStreamFormat methods

STDMETHODIMP CaptionStream::GetFormat(GUID *guid,
		WAVEFORMATEX **co_mem_wfex_out)
{
	debugfunc("guid, co_mem_wfex_out");

	if (!guid || !co_mem_wfex_out)
		return E_POINTER;

	if (format.wFormatTag == 0) {
		*co_mem_wfex_out = nullptr;
		return S_OK;
	}

	void *wfex = CoTaskMemAlloc(sizeof(format));
	memcpy(wfex, &format, sizeof(format));

	*co_mem_wfex_out = (WAVEFORMATEX*)wfex;
	return S_OK;
}

// ISpAudio methods

STDMETHODIMP CaptionStream::SetState(SPAUDIOSTATE state_, ULONGLONG)
{
	debugfunc("%lu, reserved", state_);
	state = state_;
	return S_OK;
}

STDMETHODIMP CaptionStream::SetFormat(REFGUID guid_ref,
		const WAVEFORMATEX *wfex)
{
	debugfunc("guid, wfex");
	if (!wfex)
		return E_INVALIDARG;

	if (guid_ref == SPDFID_WaveFormatEx) {
		lock_guard<mutex> lock(m);
		memcpy(&format, wfex, sizeof(format));
		if (!handler->reset_resampler(AUDIO_FORMAT_16BIT,
				wfex->nSamplesPerSec))
			return E_FAIL;

		/* 50 msec */
		DWORD size = format.nSamplesPerSec / 20;
		DWORD byte_size = size * format.nBlockAlign;
		circlebuf_reserve(buf, (size_t)byte_size);
	}
	return S_OK;
}

STDMETHODIMP CaptionStream::GetStatus(SPAUDIOSTATUS *status)
{
	debugfunc("status");

	if (!status)
		return E_POINTER;

	/* TODO? */
	lock_guard<mutex> lock(m);
	*status = {};
	status->cbNonBlockingIO = (ULONG)buf->size;
	status->State = state;
	status->CurSeekPos = pos.QuadPart;
	status->CurDevicePos = write_pos;
	return S_OK;
}

STDMETHODIMP CaptionStream::SetBufferInfo(const SPAUDIOBUFFERINFO *buf_info_)
{
	debugfunc("buf_info");

	/* TODO */
	buf_info = *buf_info_;
	return S_OK;
}

STDMETHODIMP CaptionStream::GetBufferInfo(SPAUDIOBUFFERINFO *buf_info_)
{
	debugfunc("buf_info");
	if (!buf_info_)
		return E_POINTER;

	*buf_info_ = buf_info;
	return S_OK;
}

STDMETHODIMP CaptionStream::GetDefaultFormat(GUID *format,
		WAVEFORMATEX **co_mem_wfex_out)
{
	debugfunc("format, co_mem_wfex_out");

	if (!format || !co_mem_wfex_out)
		return E_POINTER;

	void *wfex = CoTaskMemAlloc(sizeof(format));
	memcpy(wfex, &format, sizeof(format));

	*format = SPDFID_WaveFormatEx;
	*co_mem_wfex_out = (WAVEFORMATEX*)wfex;
	return S_OK;
}

STDMETHODIMP_(HANDLE) CaptionStream::EventHandle(void)
{
	debugfunc("");
	return event;
}

STDMETHODIMP CaptionStream::GetVolumeLevel(ULONG *level)
{
	debugfunc("level");
	if (!level)
		return E_POINTER;

	*level = vol;
	return S_OK;
}

STDMETHODIMP CaptionStream::SetVolumeLevel(ULONG level)
{
	debugfunc("%lu", level);
	vol = level;
	return S_OK;
}

STDMETHODIMP CaptionStream::GetBufferNotifySize(ULONG *size)
{
	debugfunc("size");
	if (!size)
		return E_POINTER;
	*size = notify_size;
	return S_OK;
}

STDMETHODIMP CaptionStream::SetBufferNotifySize(ULONG size)
{
	debugfunc("%lu", size);
	notify_size = size;
	return S_OK;
}