From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49783) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z2H4N-0001vr-Cq for qemu-devel@nongnu.org; Tue, 09 Jun 2015 06:47:49 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Z2H4J-0004f2-8b for qemu-devel@nongnu.org; Tue, 09 Jun 2015 06:47:47 -0400 Received: from mx1.redhat.com ([209.132.183.28]:52877) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z2H4I-0004eT-UX for qemu-devel@nongnu.org; Tue, 09 Jun 2015 06:47:43 -0400 From: Gerd Hoffmann Date: Tue, 9 Jun 2015 12:47:29 +0200 Message-Id: <1433846851-20552-11-git-send-email-kraxel@redhat.com> In-Reply-To: <1433846851-20552-1-git-send-email-kraxel@redhat.com> References: <1433846851-20552-1-git-send-email-kraxel@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PULL 10/12] dsoundaudio: do not use global variables List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: "Vassili Karpov (malc)" , Gerd Hoffmann , =?UTF-8?q?K=C5=91v=C3=A1g=C3=B3=2C=20Zolt=C3=A1n?= From: K=C5=91v=C3=A1g=C3=B3, Zolt=C3=A1n Signed-off-by: K=C5=91v=C3=A1g=C3=B3, Zolt=C3=A1n Signed-off-by: Gerd Hoffmann --- audio/dsound_template.h | 18 ++++---- audio/dsoundaudio.c | 106 ++++++++++++++++++++++++++++--------------= ------ 2 files changed, 74 insertions(+), 50 deletions(-) diff --git a/audio/dsound_template.h b/audio/dsound_template.h index 98276fb..85ba858 100644 --- a/audio/dsound_template.h +++ b/audio/dsound_template.h @@ -67,7 +67,8 @@ static int glue (dsound_lock_, TYPE) ( LPVOID *p2p, DWORD *blen1p, DWORD *blen2p, - int entire + int entire, + dsound *s ) { HRESULT hr; @@ -75,13 +76,14 @@ static int glue (dsound_lock_, TYPE) ( LPVOID p1 =3D NULL, p2 =3D NULL; DWORD blen1 =3D 0, blen2 =3D 0; DWORD flag; + DSoundConf *conf =3D &s->conf; =20 #ifdef DSBTYPE_IN flag =3D entire ? DSCBLOCK_ENTIREBUFFER : 0; #else flag =3D entire ? DSBLOCK_ENTIREBUFFER : 0; #endif - for (i =3D 0; i < conf.lock_retries; ++i) { + for (i =3D 0; i < conf->lock_retries; ++i) { hr =3D glue (IFACE, _Lock) ( buf, pos, @@ -96,7 +98,7 @@ static int glue (dsound_lock_, TYPE) ( if (FAILED (hr)) { #ifndef DSBTYPE_IN if (hr =3D=3D DSERR_BUFFERLOST) { - if (glue (dsound_restore_, TYPE) (buf)) { + if (glue (dsound_restore_, TYPE) (buf, s)) { dsound_logerr (hr, "Could not lock " NAME "\n"); goto fail; } @@ -110,7 +112,7 @@ static int glue (dsound_lock_, TYPE) ( break; } =20 - if (i =3D=3D conf.lock_retries) { + if (i =3D=3D conf->lock_retries) { dolog ("%d attempts to lock " NAME " failed\n", i); goto fail; } @@ -183,9 +185,10 @@ static int dsound_init_out(HWVoiceOut *hw, struct au= dsettings *as, { int err; HRESULT hr; - dsound *s =3D &glob_dsound; + dsound *s =3D drv_opaque; WAVEFORMATEX wfx; struct audsettings obt_as; + DSoundConf *conf =3D &s->conf; #ifdef DSBTYPE_IN const char *typ =3D "ADC"; DSoundVoiceIn *ds =3D (DSoundVoiceIn *) hw; @@ -212,7 +215,7 @@ static int dsound_init_out(HWVoiceOut *hw, struct aud= settings *as, bd.dwSize =3D sizeof (bd); bd.lpwfxFormat =3D &wfx; #ifdef DSBTYPE_IN - bd.dwBufferBytes =3D conf.bufsize_in; + bd.dwBufferBytes =3D conf->bufsize_in; hr =3D IDirectSoundCapture_CreateCaptureBuffer ( s->dsound_capture, &bd, @@ -221,7 +224,7 @@ static int dsound_init_out(HWVoiceOut *hw, struct aud= settings *as, ); #else bd.dwFlags =3D DSBCAPS_STICKYFOCUS | DSBCAPS_GETCURRENTPOSITION2; - bd.dwBufferBytes =3D conf.bufsize_out; + bd.dwBufferBytes =3D conf->bufsize_out; hr =3D IDirectSound_CreateSoundBuffer ( s->dsound, &bd, @@ -271,6 +274,7 @@ static int dsound_init_out(HWVoiceOut *hw, struct aud= settings *as, ); } hw->samples =3D bc.dwBufferBytes >> hw->info.shift; + ds->s =3D s; =20 #ifdef DEBUG_DSOUND dolog ("caps %ld, desc %ld\n", diff --git a/audio/dsoundaudio.c b/audio/dsoundaudio.c index e2d89fd..c8b09e2 100644 --- a/audio/dsoundaudio.c +++ b/audio/dsoundaudio.c @@ -41,7 +41,7 @@ =20 /* #define DEBUG_DSOUND */ =20 -static struct { +typedef struct { int lock_retries; int restore_retries; int getstatus_retries; @@ -50,33 +50,22 @@ static struct { int bufsize_out; struct audsettings settings; int latency_millis; -} conf =3D { - .lock_retries =3D 1, - .restore_retries =3D 1, - .getstatus_retries =3D 1, - .set_primary =3D 0, - .bufsize_in =3D 16384, - .bufsize_out =3D 16384, - .settings.freq =3D 44100, - .settings.nchannels =3D 2, - .settings.fmt =3D AUD_FMT_S16, - .latency_millis =3D 10 -}; +} DSoundConf; =20 typedef struct { LPDIRECTSOUND dsound; LPDIRECTSOUNDCAPTURE dsound_capture; LPDIRECTSOUNDBUFFER dsound_primary_buffer; struct audsettings settings; + DSoundConf conf; } dsound; =20 -static dsound glob_dsound; - typedef struct { HWVoiceOut hw; LPDIRECTSOUNDBUFFER dsound_buffer; DWORD old_pos; int first_time; + dsound *s; #ifdef DEBUG_DSOUND DWORD old_ppos; DWORD played; @@ -88,6 +77,7 @@ typedef struct { HWVoiceIn hw; int first_time; LPDIRECTSOUNDCAPTUREBUFFER dsound_capture_buffer; + dsound *s; } DSoundVoiceIn; =20 static void dsound_log_hresult (HRESULT hr) @@ -281,12 +271,12 @@ static void print_wave_format (WAVEFORMATEX *wfx) } #endif =20 -static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb) +static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb, dsound *s) { HRESULT hr; int i; =20 - for (i =3D 0; i < conf.restore_retries; ++i) { + for (i =3D 0; i < s->conf.restore_retries; ++i) { hr =3D IDirectSoundBuffer_Restore (dsb); =20 switch (hr) { @@ -311,12 +301,13 @@ static int dsound_restore_out (LPDIRECTSOUNDBUFFER = dsb) #include "dsound_template.h" #undef DSBTYPE_IN =20 -static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *status= p) +static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *status= p, + dsound *s) { HRESULT hr; int i; =20 - for (i =3D 0; i < conf.getstatus_retries; ++i) { + for (i =3D 0; i < s->conf.getstatus_retries; ++i) { hr =3D IDirectSoundBuffer_GetStatus (dsb, statusp); if (FAILED (hr)) { dsound_logerr (hr, "Could not get playback buffer status\n")= ; @@ -324,7 +315,7 @@ static int dsound_get_status_out (LPDIRECTSOUNDBUFFER= dsb, DWORD *statusp) } =20 if (*statusp & DSERR_BUFFERLOST) { - if (dsound_restore_out (dsb)) { + if (dsound_restore_out (dsb, s)) { return -1; } continue; @@ -376,7 +367,8 @@ static void dsound_write_sample (HWVoiceOut *hw, uint= 8_t *dst, int dst_len) hw->rpos =3D pos % hw->samples; } =20 -static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb= ) +static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb= , + dsound *s) { int err; LPVOID p1, p2; @@ -389,7 +381,8 @@ static void dsound_clear_sample (HWVoiceOut *hw, LPDI= RECTSOUNDBUFFER dsb) hw->samples << hw->info.shift, &p1, &p2, &blen1, &blen2, - 1 + 1, + s ); if (err) { return; @@ -435,6 +428,7 @@ static int dsound_open (dsound *s) WAVEFORMATEX wfx; DSBUFFERDESC dsbd; HWND hwnd; + DSoundConf *conf =3D &s->conf; =20 hwnd =3D GetForegroundWindow (); hr =3D IDirectSound_SetCooperativeLevel ( @@ -449,11 +443,11 @@ static int dsound_open (dsound *s) return -1; } =20 - if (!conf.set_primary) { + if (!conf->set_primary) { return 0; } =20 - err =3D waveformat_from_audio_settings (&wfx, &conf.settings); + err =3D waveformat_from_audio_settings (&wfx, &conf->settings); if (err) { return -1; } @@ -514,6 +508,7 @@ static int dsound_ctl_out (HWVoiceOut *hw, int cmd, .= ..) DWORD status; DSoundVoiceOut *ds =3D (DSoundVoiceOut *) hw; LPDIRECTSOUNDBUFFER dsb =3D ds->dsound_buffer; + dsound *s =3D ds->s; =20 if (!dsb) { dolog ("Attempt to control voice without a buffer\n"); @@ -522,7 +517,7 @@ static int dsound_ctl_out (HWVoiceOut *hw, int cmd, .= ..) =20 switch (cmd) { case VOICE_ENABLE: - if (dsound_get_status_out (dsb, &status)) { + if (dsound_get_status_out (dsb, &status, s)) { return -1; } =20 @@ -531,7 +526,7 @@ static int dsound_ctl_out (HWVoiceOut *hw, int cmd, .= ..) return 0; } =20 - dsound_clear_sample (hw, dsb); + dsound_clear_sample (hw, dsb, s); =20 hr =3D IDirectSoundBuffer_Play (dsb, 0, 0, DSBPLAY_LOOPING); if (FAILED (hr)) { @@ -541,7 +536,7 @@ static int dsound_ctl_out (HWVoiceOut *hw, int cmd, .= ..) break; =20 case VOICE_DISABLE: - if (dsound_get_status_out (dsb, &status)) { + if (dsound_get_status_out (dsb, &status, s)) { return -1; } =20 @@ -578,6 +573,8 @@ static int dsound_run_out (HWVoiceOut *hw, int live) DWORD wpos, ppos, old_pos; LPVOID p1, p2; int bufsize; + dsound *s =3D ds->s; + DSoundConf *conf =3D &s->conf; =20 if (!dsb) { dolog ("Attempt to run empty with playback buffer\n"); @@ -600,14 +597,14 @@ static int dsound_run_out (HWVoiceOut *hw, int live= ) len =3D live << hwshift; =20 if (ds->first_time) { - if (conf.latency_millis) { + if (conf->latency_millis) { DWORD cur_blat; =20 cur_blat =3D audio_ring_dist (wpos, ppos, bufsize); ds->first_time =3D 0; old_pos =3D wpos; old_pos +=3D - millis_to_bytes (&hw->info, conf.latency_millis) - cur_b= lat; + millis_to_bytes (&hw->info, conf->latency_millis) - cur_= blat; old_pos %=3D bufsize; old_pos &=3D ~hw->info.align; } @@ -663,7 +660,8 @@ static int dsound_run_out (HWVoiceOut *hw, int live) len, &p1, &p2, &blen1, &blen2, - 0 + 0, + s ); if (err) { return 0; @@ -766,6 +764,7 @@ static int dsound_run_in (HWVoiceIn *hw) DWORD cpos, rpos; LPVOID p1, p2; int hwshift; + dsound *s =3D ds->s; =20 if (!dscb) { dolog ("Attempt to run without capture buffer\n"); @@ -820,7 +819,8 @@ static int dsound_run_in (HWVoiceIn *hw) &p2, &blen1, &blen2, - 0 + 0, + s ); if (err) { return 0; @@ -843,12 +843,26 @@ static int dsound_run_in (HWVoiceIn *hw) return decr; } =20 +static DSoundConf glob_conf =3D { + .lock_retries =3D 1, + .restore_retries =3D 1, + .getstatus_retries =3D 1, + .set_primary =3D 0, + .bufsize_in =3D 16384, + .bufsize_out =3D 16384, + .settings.freq =3D 44100, + .settings.nchannels =3D 2, + .settings.fmt =3D AUD_FMT_S16, + .latency_millis =3D 10 +}; + static void dsound_audio_fini (void *opaque) { HRESULT hr; dsound *s =3D opaque; =20 if (!s->dsound) { + g_free(s); return; } =20 @@ -859,6 +873,7 @@ static void dsound_audio_fini (void *opaque) s->dsound =3D NULL; =20 if (!s->dsound_capture) { + g_free(s); return; } =20 @@ -867,17 +882,21 @@ static void dsound_audio_fini (void *opaque) dsound_logerr (hr, "Could not release DirectSoundCapture\n"); } s->dsound_capture =3D NULL; + + g_free(s); } =20 static void *dsound_audio_init (void) { int err; HRESULT hr; - dsound *s =3D &glob_dsound; + dsound *s =3D g_malloc0(sizeof(dsound)); =20 + s->conf =3D glob_conf; hr =3D CoInitialize (NULL); if (FAILED (hr)) { dsound_logerr (hr, "Could not initialize COM\n"); + g_free(s); return NULL; } =20 @@ -890,6 +909,7 @@ static void *dsound_audio_init (void) ); if (FAILED (hr)) { dsound_logerr (hr, "Could not create DirectSound instance\n"); + g_free(s); return NULL; } =20 @@ -901,7 +921,7 @@ static void *dsound_audio_init (void) if (FAILED (hr)) { dsound_logerr (hr, "Could not release DirectSound\n"); } - s->dsound =3D NULL; + g_free(s); return NULL; } =20 @@ -941,61 +961,61 @@ static struct audio_option dsound_options[] =3D { { .name =3D "LOCK_RETRIES", .tag =3D AUD_OPT_INT, - .valp =3D &conf.lock_retries, + .valp =3D &glob_conf.lock_retries, .descr =3D "Number of times to attempt locking the buffer" }, { .name =3D "RESTOURE_RETRIES", .tag =3D AUD_OPT_INT, - .valp =3D &conf.restore_retries, + .valp =3D &glob_conf.restore_retries, .descr =3D "Number of times to attempt restoring the buffer" }, { .name =3D "GETSTATUS_RETRIES", .tag =3D AUD_OPT_INT, - .valp =3D &conf.getstatus_retries, + .valp =3D &glob_conf.getstatus_retries, .descr =3D "Number of times to attempt getting status of the buf= fer" }, { .name =3D "SET_PRIMARY", .tag =3D AUD_OPT_BOOL, - .valp =3D &conf.set_primary, + .valp =3D &glob_conf.set_primary, .descr =3D "Set the parameters of primary buffer" }, { .name =3D "LATENCY_MILLIS", .tag =3D AUD_OPT_INT, - .valp =3D &conf.latency_millis, + .valp =3D &glob_conf.latency_millis, .descr =3D "(undocumented)" }, { .name =3D "PRIMARY_FREQ", .tag =3D AUD_OPT_INT, - .valp =3D &conf.settings.freq, + .valp =3D &glob_conf.settings.freq, .descr =3D "Primary buffer frequency" }, { .name =3D "PRIMARY_CHANNELS", .tag =3D AUD_OPT_INT, - .valp =3D &conf.settings.nchannels, + .valp =3D &glob_conf.settings.nchannels, .descr =3D "Primary buffer number of channels (1 - mono, 2 - ste= reo)" }, { .name =3D "PRIMARY_FMT", .tag =3D AUD_OPT_FMT, - .valp =3D &conf.settings.fmt, + .valp =3D &glob_conf.settings.fmt, .descr =3D "Primary buffer format" }, { .name =3D "BUFSIZE_OUT", .tag =3D AUD_OPT_INT, - .valp =3D &conf.bufsize_out, + .valp =3D &glob_conf.bufsize_out, .descr =3D "(undocumented)" }, { .name =3D "BUFSIZE_IN", .tag =3D AUD_OPT_INT, - .valp =3D &conf.bufsize_in, + .valp =3D &glob_conf.bufsize_in, .descr =3D "(undocumented)" }, { /* End of list */ } --=20 1.8.3.1