static gboolean gst_wasapi_src_start (GstBaseSrc * src) { GstWasapiSrc *self = GST_WASAPI_SRC (src); gboolean res = FALSE; IAudioClient *client = NULL; IAudioClock *client_clock = NULL; guint64 client_clock_freq = 0; IAudioCaptureClient *capture_client = NULL; HRESULT hr; if (!gst_wasapi_util_get_default_device_client (GST_ELEMENT (self), TRUE, self->rate, self->buffer_time, self->period_time, 0, &client, &self->latency)) goto beach; hr = IAudioClient_GetService (client, &IID_IAudioClock, &client_clock); if (hr != S_OK) { GST_ERROR_OBJECT (self, "IAudioClient::GetService (IID_IAudioClock) " "failed"); goto beach; } hr = IAudioClock_GetFrequency (client_clock, &client_clock_freq); if (hr != S_OK) { GST_ERROR_OBJECT (self, "IAudioClock::GetFrequency () failed"); goto beach; } hr = IAudioClient_GetService (client, &IID_IAudioCaptureClient, &capture_client); if (hr != S_OK) { GST_ERROR_OBJECT (self, "IAudioClient::GetService " "(IID_IAudioCaptureClient) failed"); goto beach; } hr = IAudioClient_Start (client); if (hr != S_OK) { GST_ERROR_OBJECT (self, "IAudioClient::Start failed"); goto beach; } self->client = client; self->client_clock = client_clock; self->client_clock_freq = client_clock_freq; self->capture_client = capture_client; res = TRUE; beach: if (!res) { if (capture_client != NULL) IUnknown_Release (capture_client); if (client_clock != NULL) IUnknown_Release (client_clock); if (client != NULL) IUnknown_Release (client); } return res; }
static void test_clock(void) { HRESULT hr; IAudioClient *ac; IAudioClock *acl; IAudioRenderClient *arc; UINT64 freq, pos, pcpos, last; BYTE *data; WAVEFORMATEX *pwfx; hr = IMMDevice_Activate(dev, &IID_IAudioClient, CLSCTX_INPROC_SERVER, NULL, (void**)&ac); ok(hr == S_OK, "Activation failed with %08x\n", hr); if(hr != S_OK) return; hr = IAudioClient_GetMixFormat(ac, &pwfx); ok(hr == S_OK, "GetMixFormat failed: %08x\n", hr); if(hr != S_OK) return; hr = IAudioClient_Initialize(ac, AUDCLNT_SHAREMODE_SHARED, 0, 5000000, 0, pwfx, NULL); ok(hr == S_OK, "Initialize failed: %08x\n", hr); hr = IAudioClient_GetService(ac, &IID_IAudioClock, (void**)&acl); ok(hr == S_OK, "GetService(IAudioClock) failed: %08x\n", hr); hr = IAudioClock_GetFrequency(acl, &freq); ok(hr == S_OK, "GetFrequency failed: %08x\n", hr); hr = IAudioClock_GetPosition(acl, NULL, NULL); ok(hr == E_POINTER, "GetPosition wrong error: %08x\n", hr); pcpos = 0; hr = IAudioClock_GetPosition(acl, &pos, &pcpos); ok(hr == S_OK, "GetPosition failed: %08x\n", hr); ok(pos == 0, "GetPosition returned non-zero pos before being started\n"); ok(pcpos != 0, "GetPosition returned zero pcpos\n"); last = pos; hr = IAudioClient_GetService(ac, &IID_IAudioRenderClient, (void**)&arc); ok(hr == S_OK, "GetService(IAudioRenderClient) failed: %08x\n", hr); hr = IAudioRenderClient_GetBuffer(arc, pwfx->nSamplesPerSec / 2., &data); ok(hr == S_OK, "GetBuffer failed: %08x\n", hr); hr = IAudioRenderClient_ReleaseBuffer(arc, pwfx->nSamplesPerSec / 2., AUDCLNT_BUFFERFLAGS_SILENT); ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr); hr = IAudioClock_GetPosition(acl, &pos, NULL); ok(hr == S_OK, "GetPosition failed: %08x\n", hr); ok(pos == 0, "GetPosition returned non-zero pos before being started\n"); hr = IAudioClient_Start(ac); ok(hr == S_OK, "Start failed: %08x\n", hr); Sleep(100); hr = IAudioClock_GetPosition(acl, &pos, NULL); ok(hr == S_OK, "GetPosition failed: %08x\n", hr); ok(pos > 0, "Position should have been further along...\n"); last = pos; hr = IAudioClient_Stop(ac); ok(hr == S_OK, "Stop failed: %08x\n", hr); hr = IAudioClock_GetPosition(acl, &pos, NULL); ok(hr == S_OK, "GetPosition failed: %08x\n", hr); ok(pos >= last, "Position should have been further along...\n"); last = pos; hr = IAudioClient_Start(ac); ok(hr == S_OK, "Start failed: %08x\n", hr); Sleep(100); hr = IAudioClient_Stop(ac); ok(hr == S_OK, "Stop failed: %08x\n", hr); hr = IAudioClock_GetPosition(acl, &pos, NULL); ok(hr == S_OK, "GetPosition failed: %08x\n", hr); ok(pos >= last, "Position should have been further along...\n"); last = pos; hr = IAudioClock_GetPosition(acl, &pos, NULL); ok(hr == S_OK, "GetPosition failed: %08x\n", hr); ok(pos == last, "Position should have been further along...\n"); hr = IAudioClient_Reset(ac); ok(hr == S_OK, "Reset failed: %08x\n", hr); hr = IAudioClock_GetPosition(acl, &pos, NULL); ok(hr == S_OK, "GetPosition failed: %08x\n", hr); ok(pos == 0, "GetPosition returned non-zero pos after Reset\n"); last = pos; hr = IAudioRenderClient_GetBuffer(arc, pwfx->nSamplesPerSec / 2., &data); ok(hr == S_OK, "GetBuffer failed: %08x\n", hr); hr = IAudioRenderClient_ReleaseBuffer(arc, pwfx->nSamplesPerSec / 2., AUDCLNT_BUFFERFLAGS_SILENT); ok(hr == S_OK, "ReleaseBuffer failed: %08x\n", hr); hr = IAudioClock_GetPosition(acl, &pos, NULL); ok(hr == S_OK, "GetPosition failed: %08x\n", hr); ok(pos == 0, "GetPosition returned non-zero pos after Reset\n"); last = pos; hr = IAudioClient_Start(ac); ok(hr == S_OK, "Start failed: %08x\n", hr); Sleep(100); hr = IAudioClock_GetPosition(acl, &pos, NULL); ok(hr == S_OK, "GetPosition failed: %08x\n", hr); ok(pos > last, "Position should have been further along...\n"); hr = IAudioClient_Stop(ac); ok(hr == S_OK, "Stop failed: %08x\n", hr); hr = IAudioClock_GetPosition(acl, &pos, NULL); ok(hr == S_OK, "GetPosition failed: %08x\n", hr); ok(pos >= last, "Position should have been further along...\n"); IAudioClock_Release(acl); IAudioClient_Release(ac); }
/** * Perform mixing for a Direct Sound device. That is, go through all the * secondary buffers (the sound bites currently playing) and mix them in * to the primary buffer (the device buffer). */ static void DSOUND_PerformMix(DirectSoundDevice *device) { UINT64 clock_pos, clock_freq, pos_bytes; UINT delta_frags; HRESULT hr; TRACE("(%p)\n", device); /* **** */ EnterCriticalSection(&device->mixlock); hr = IAudioClock_GetFrequency(device->clock, &clock_freq); if(FAILED(hr)){ WARN("GetFrequency failed: %08x\n", hr); LeaveCriticalSection(&device->mixlock); return; } hr = IAudioClock_GetPosition(device->clock, &clock_pos, NULL); if(FAILED(hr)){ WARN("GetCurrentPadding failed: %08x\n", hr); LeaveCriticalSection(&device->mixlock); return; } pos_bytes = (clock_pos * device->pwfx->nSamplesPerSec * device->pwfx->nBlockAlign) / clock_freq; delta_frags = (pos_bytes - device->last_pos_bytes) / device->fraglen; if(delta_frags > 0){ device->pwplay += delta_frags; device->pwplay %= device->helfrags; device->pwqueue -= delta_frags; device->last_pos_bytes = pos_bytes - (pos_bytes % device->fraglen); } if (device->priolevel != DSSCL_WRITEPRIMARY) { BOOL recover = FALSE, all_stopped = FALSE; DWORD playpos, writepos, writelead, maxq, frag, prebuff_max, prebuff_left, size1, size2, mixplaypos, mixplaypos2; LPVOID buf1, buf2; int nfiller; /* the sound of silence */ nfiller = device->pwfx->wBitsPerSample == 8 ? 128 : 0; /* get the position in the primary buffer */ if (DSOUND_PrimaryGetPosition(device, &playpos, &writepos) != 0){ LeaveCriticalSection(&(device->mixlock)); return; } TRACE("primary playpos=%d, writepos=%d, clrpos=%d, mixpos=%d, buflen=%d\n", playpos,writepos,device->playpos,device->mixpos,device->buflen); assert(device->playpos < device->buflen); mixplaypos = DSOUND_bufpos_to_mixpos(device, device->playpos); mixplaypos2 = DSOUND_bufpos_to_mixpos(device, playpos); /* calc maximum prebuff */ prebuff_max = (device->prebuf * device->fraglen); if (playpos + prebuff_max >= device->helfrags * device->fraglen) prebuff_max += device->buflen - device->helfrags * device->fraglen; /* check how close we are to an underrun. It occurs when the writepos overtakes the mixpos */ prebuff_left = DSOUND_BufPtrDiff(device->buflen, device->mixpos, playpos); writelead = DSOUND_BufPtrDiff(device->buflen, writepos, playpos); /* check for underrun. underrun occurs when the write position passes the mix position * also wipe out just-played sound data */ if((prebuff_left > prebuff_max) || (device->state == STATE_STOPPED) || (device->state == STATE_STARTING)){ if (device->state == STATE_STOPPING || device->state == STATE_PLAYING) WARN("Probable buffer underrun\n"); else TRACE("Buffer starting or buffer underrun\n"); /* recover mixing for all buffers */ recover = TRUE; /* reset mix position to write position */ device->mixpos = writepos; ZeroMemory(device->mix_buffer, device->mix_buffer_len); ZeroMemory(device->buffer, device->buflen); } else if (playpos < device->playpos) { buf1 = device->buffer + device->playpos; buf2 = device->buffer; size1 = device->buflen - device->playpos; size2 = playpos; FillMemory(device->mix_buffer + mixplaypos, device->mix_buffer_len - mixplaypos, 0); FillMemory(device->mix_buffer, mixplaypos2, 0); FillMemory(buf1, size1, nfiller); if (playpos && (!buf2 || !size2)) FIXME("%d: (%d, %d)=>(%d, %d) There should be an additional buffer here!!\n", __LINE__, device->playpos, device->mixpos, playpos, writepos); FillMemory(buf2, size2, nfiller); } else { buf1 = device->buffer + device->playpos; buf2 = NULL; size1 = playpos - device->playpos; size2 = 0; FillMemory(device->mix_buffer + mixplaypos, mixplaypos2 - mixplaypos, 0); FillMemory(buf1, size1, nfiller); } device->playpos = playpos; /* find the maximum we can prebuffer from current write position */ maxq = (writelead < prebuff_max) ? (prebuff_max - writelead) : 0; TRACE("prebuff_left = %d, prebuff_max = %dx%d=%d, writelead=%d\n", prebuff_left, device->prebuf, device->fraglen, prebuff_max, writelead); /* do the mixing */ frag = DSOUND_MixToPrimary(device, writepos, maxq, recover, &all_stopped); if (frag + writepos > device->buflen) { DWORD todo = device->buflen - writepos; device->normfunction(device->mix_buffer + DSOUND_bufpos_to_mixpos(device, writepos), device->buffer + writepos, todo); device->normfunction(device->mix_buffer, device->buffer, frag - todo); } else device->normfunction(device->mix_buffer + DSOUND_bufpos_to_mixpos(device, writepos), device->buffer + writepos, frag); /* update the mix position, taking wrap-around into account */ device->mixpos = writepos + frag; device->mixpos %= device->buflen; /* update prebuff left */ prebuff_left = DSOUND_BufPtrDiff(device->buflen, device->mixpos, playpos); /* check if have a whole fragment */ if (prebuff_left >= device->fraglen){ /* update the wave queue */ DSOUND_WaveQueue(device, FALSE); /* buffers are full. start playing if applicable */ if(device->state == STATE_STARTING){ TRACE("started primary buffer\n"); if(DSOUND_PrimaryPlay(device) != DS_OK){ WARN("DSOUND_PrimaryPlay failed\n"); } else{ /* we are playing now */ device->state = STATE_PLAYING; } } /* buffers are full. start stopping if applicable */ if(device->state == STATE_STOPPED){ TRACE("restarting primary buffer\n"); if(DSOUND_PrimaryPlay(device) != DS_OK){ WARN("DSOUND_PrimaryPlay failed\n"); } else{ /* start stopping again. as soon as there is no more data, it will stop */ device->state = STATE_STOPPING; } } } /* if device was stopping, its for sure stopped when all buffers have stopped */ else if((all_stopped == TRUE) && (device->state == STATE_STOPPING)){ TRACE("All buffers have stopped. Stopping primary buffer\n"); device->state = STATE_STOPPED; /* stop the primary buffer now */ DSOUND_PrimaryStop(device); } } else { DSOUND_WaveQueue(device, TRUE); /* in the DSSCL_WRITEPRIMARY mode, the app is totally in charge... */ if (device->state == STATE_STARTING) { if (DSOUND_PrimaryPlay(device) != DS_OK) WARN("DSOUND_PrimaryPlay failed\n"); else device->state = STATE_PLAYING; } else if (device->state == STATE_STOPPING) { if (DSOUND_PrimaryStop(device) != DS_OK) WARN("DSOUND_PrimaryStop failed\n"); else device->state = STATE_STOPPED; } } LeaveCriticalSection(&(device->mixlock)); /* **** */ }