Remove references to global heap in SDL audio

Ref #1987
This commit is contained in:
Emery Hemingway 2017-05-29 21:02:22 -05:00 committed by Christian Helmuth
parent 697f179e21
commit b814188d7a

View File

@ -19,18 +19,19 @@
#include <base/log.h> #include <base/log.h>
#include <base/thread.h> #include <base/thread.h>
#include <audio_out_session/connection.h> #include <audio_out_session/connection.h>
#include <util/reconstructible.h>
enum { enum {
AUDIO_CHANNELS = 2, AUDIO_CHANNELS = 2,
}; };
using Genode::env;
using Genode::Allocator_avl; using Genode::Allocator_avl;
using Genode::Signal_context; using Genode::Signal_context;
using Genode::Signal_receiver; using Genode::Signal_receiver;
using Genode::log; using Genode::log;
using Genode::Hex; using Genode::Hex;
using Genode::Constructible;
static const char *channel_names[] = { "front left", "front right" }; static const char *channel_names[] = { "front left", "front right" };
static float volume = 1.0; static float volume = 1.0;
@ -57,7 +58,7 @@ extern "C" {
struct SDL_PrivateAudioData { struct SDL_PrivateAudioData {
Uint8 *mixbuf; Uint8 *mixbuf;
Uint32 mixlen; Uint32 mixlen;
Audio_out::Connection *audio[AUDIO_CHANNELS]; Constructible<Audio_out::Connection> audio[AUDIO_CHANNELS];
Audio_out::Packet *packet[AUDIO_CHANNELS]; Audio_out::Packet *packet[AUDIO_CHANNELS];
}; };
@ -113,7 +114,7 @@ static int GENODEAUD_Available(void)
static void GENODEAUD_DeleteDevice(SDL_AudioDevice *device) static void GENODEAUD_DeleteDevice(SDL_AudioDevice *device)
{ {
for (int channel = 0; channel < AUDIO_CHANNELS; channel++) for (int channel = 0; channel < AUDIO_CHANNELS; channel++)
destroy(env()->heap(), device->hidden->audio[channel]); device->hidden->audio[channel].destruct();
SDL_free(device->hidden); SDL_free(device->hidden);
SDL_free(device); SDL_free(device);
@ -152,16 +153,15 @@ static SDL_AudioDevice *GENODEAUD_CreateDevice(int devindex)
/* connect to 'Audio_out' service */ /* connect to 'Audio_out' service */
for (int channel = 0; channel < AUDIO_CHANNELS; channel++) { for (int channel = 0; channel < AUDIO_CHANNELS; channel++) {
try { try {
_this->hidden->audio[channel] = new (env()->heap()) _this->hidden->audio[channel].construct(
Audio_out::Connection(channel_names[channel], channel_names[channel], false, channel == 0 ? true : false);
false, channel == 0 ? true : false);
_this->hidden->audio[channel]->start(); _this->hidden->audio[channel]->start();
} }
catch(Genode::Service_denied) { catch(Genode::Service_denied) {
Genode::error("could not connect to 'Audio_out' service"); Genode::error("could not connect to 'Audio_out' service");
while(--channel > 0) while(--channel > 0)
destroy(env()->heap(), _this->hidden->audio[channel]); _this->hidden->audio[channel].destruct();
return NULL; return NULL;
} }
@ -181,18 +181,18 @@ AudioBootStrap GENODEAUD_bootstrap = {
static void GENODEAUD_WaitAudio(_THIS) static void GENODEAUD_WaitAudio(_THIS)
{ {
Audio_out::Connection *con = _this->hidden->audio[0]; Audio_out::Connection &con = *_this->hidden->audio[0];
Audio_out::Packet *p = _this->hidden->packet[0]; Audio_out::Packet *p = _this->hidden->packet[0];
unsigned const packet_pos = con->stream()->packet_position(p); unsigned const packet_pos = con.stream()->packet_position(p);
unsigned const play_pos = con->stream()->pos(); unsigned const play_pos = con.stream()->pos();
unsigned queued = packet_pos < play_pos unsigned queued = packet_pos < play_pos
? ((Audio_out::QUEUE_SIZE + packet_pos) - play_pos) ? ((Audio_out::QUEUE_SIZE + packet_pos) - play_pos)
: packet_pos - play_pos; : packet_pos - play_pos;
/* wait until there is only one packet left to play */ /* wait until there is only one packet left to play */
while (queued > 1) { while (queued > 1) {
con->wait_for_progress(); con.wait_for_progress();
queued--; queued--;
} }
} }
@ -203,7 +203,7 @@ static void GENODEAUD_PlayAudio(_THIS)
Audio_out::Connection *c[AUDIO_CHANNELS]; Audio_out::Connection *c[AUDIO_CHANNELS];
Audio_out::Packet *p[AUDIO_CHANNELS]; Audio_out::Packet *p[AUDIO_CHANNELS];
for (int channel = 0; channel < AUDIO_CHANNELS; channel++) { for (int channel = 0; channel < AUDIO_CHANNELS; channel++) {
c[channel] = _this->hidden->audio[channel]; c[channel] = &(*_this->hidden->audio[channel]);
p[channel] = _this->hidden->packet[channel]; p[channel] = _this->hidden->packet[channel];
} }