[pulseaudio-discuss] [PATCH] revive solaris module
Finn Thain
fthain at telegraphics.com.au
Fri Mar 6 21:48:10 PST 2009
On Wed, 4 Mar 2009, Lennart Poettering wrote:
[snip]
> > This patch disables link map/library versioning unless ld is GNU ld.
> > Another approach for solaris would be to use that linker's -M option,
> > but I couldn't make that work (due to undefined mainloop, browse and
> > simple symbols when linking pacat. I can post the errors if anyone is
> > intested.)
>
> The linking in PA is a bit weird since we have a cyclic dependency
> between libpulse and libpulsecommon which however is not explicit.
Could that affect the pacat link somehow?
What are the implications for client apps that link with the non-versioned
libraries I've been building on solaris?
[snip]
> > struct userdata {
> > pa_core *core;
> > @@ -87,15 +92,24 @@ struct userdata {
> >
> > pa_memchunk memchunk;
> >
> > - unsigned int page_size;
> > -
> > uint32_t frame_size;
> > - uint32_t buffer_size;
> > - unsigned int written_bytes, read_bytes;
> > + int32_t buffer_size;
> > + volatile uint64_t written_bytes, read_bytes;
> > + pa_mutex *written_bytes_lock;
>
> Hmm, we generally try do do things without locking in PA. This smells as
> if it was solvable using atomic ints as well.
>
> Actually, looking at this again I get the impression these mutex are
> completely unnecessary here. All functions that lock these mutexes are
> called from the IO thread so no locking should be nessary.
>
> Please don't use volatile here. I am pretty sure it is a misuse. Also
> see http://kernel.org/doc/Documentation/volatile-considered-harmful.txt
> which applies here too I think.
OK, I've removed the locks. For some reason I thought that the get_latency
function was called from two different threads.
> > +static void sink_set_volume(pa_sink *s) {
> > + struct userdata *u;
> > + audio_info_t info;
> > +
> > + pa_assert_se(u = s->userdata);
> > +
> > + if (u->fd >= 0) {
> > + AUDIO_INITINFO(&info);
> > +
> > + info.play.gain = pa_cvolume_avg(&s->virtual_volume) * AUDIO_MAX_GAIN / PA_VOLUME_NORM;
> > + assert(info.play.gain <= AUDIO_MAX_GAIN);
>
> I'd prefer if you'd use pa_cvolume_max here instead of pa_cvolume_avg()
> because this makes the volume independant of the balance.
>
> > - info.play.error = 0;
> > + info.play.gain = pa_cvolume_avg(&s->virtual_volume) * AUDIO_MAX_GAIN / PA_VOLUME_NORM;
> > + assert(info.play.gain <= AUDIO_MAX_GAIN);
>
> Same here. (i.e. for the source)
Done and done.
> > + if (u->sink->thread_info.rewind_requested)
> > + pa_sink_process_rewind(u->sink, 0);
>
> This is correct.
>
> >
> > err = ioctl(u->fd, AUDIO_GETINFO, &info);
> > pa_assert(err >= 0);
>
> Hmm, if at all this should be pa_assert_se(), not pa_assert() (so that
> it is not defined away by -DNDEBUG). However I'd prefer if the error
> would be could correctly. (I see that this code is not yours, but
> still...)
Done.
> > + case EINTR:
> > + break;
>
> I think you should simply try again in this case...
Done.
> > + case EAGAIN:
> > + u->buffer_size = u->buffer_size * 18 / 25;
> > + u->buffer_size -= u->buffer_size % u->frame_size;
> > + u->buffer_size = PA_MAX(u->buffer_size, (int32_t)MIN_BUFFER_SIZE);
> > + pa_sink_set_max_request(u->sink, u->buffer_size);
> > + pa_log("EAGAIN. Buffer size is now %u bytes (%llu buffered)", u->buffer_size, buffered_bytes);
> > + break;
>
> Hmm, care to explain this?
EAGAIN happens when the user requests a buffer size that is too large for
the STREAMS layer to accept. We end up looping with EAGAIN every time we
try to write out the rest of the buffer, which burns enough CPU time to
trip the CPU limit.
So, I reduce the buffer size with each EAGAIN. This gets us reasonably
close to the largest usable buffer size. (Perhaps there's a better way to
determine what that limit is, but I don't know how.)
> > +
> > + pa_rtpoll_set_timer_absolute(u->rtpoll, xtime0 + pa_bytes_to_usec(buffered_bytes / 2, &u->sink->sample_spec));
> > + } else {
> > + pa_rtpoll_set_timer_disabled(u->rtpoll);
> > }
>
> Hmm, you schedule audio via timers? Is that a good idea?
Perhaps not. I won't know until I test on more hardware.
But, given that we have rt priority and high resolution timers on solaris,
I think it is OK in theory...
The reason I used a timer was to minimise CPU usage and avoid the CPU
limit. Recall that getting woken up by poll is not an option for playback
unfortunately. We can arrange for a signal when the FD becomes writable,
but that throws out the whole buffer size concept, which acts to reduce
latency.
> That really only makes sense if you have to deal with large buffers and
> support rewinding.
I've implemented rewind support, but I'm still not sure that I have
understood the concept; I take it that we "rewind" (from the point-of-view
of the renderer, not the sink) so that some rendered but as yet unplayed
portion of the memblock/buffers can then be rendered again?
> Please keep in mind that the system clock and the sound card clock
> deviate. If you use the system timers to do PCM scheduling ou might need
> a pa_smoother object that is able to estimate the deviation for you.
Actually, in an earlier version I did use a smoother (after reading about
that in the wiki). But because of the non-monotonic sample counter (bug?)
I decided that it probably wasn't worth the added complexity so I removed
it. I'll put the smoother back if I can figure out the problem with the
sample counter.
>
> > + u->frame_size = pa_frame_size(&ss);
> >
> > - if ((fd = open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), mode | O_NONBLOCK)) < 0)
> > + u->buffer_size = 16384;
>
> It would appear more appropriate to me if the buffer size is adjusted by
> the sample spec used.
Done.
> One last thing: it would probably be a good idea to allocate a pa_card
> object and attach the sink and the source to it.
It is possible to open /dev/audio twice by loading the solaris module
twice -- once for the sink (passing record=0) and once for source (passing
playback=0), thus giving seperate threads/LWPs for source and sink. It
might be misleading to allocate two cards in that situation?
> Right now pa_cards are mostly useful for switching profiles but even if
> you do not allow switching profiles on-the-fly it is of some value to
> find out via the cards object which source belongs to which sink.
>
> Otherwise I am happy!
>
> Thanks for your patch! I'd be thankful if you could fix the issues
> pointed out and prepare another patch on top of current git!
No problem. Patch follows. It also includes a portability fix for
pa_realpath and a fix for a bug in the pa_signal_new() error path that
causes signal data be freed if you attempt to register the same signal
twice.
> I hope I answered all your questions,
Your answers were very helpful, thanks.
Finn
>
> Lennart
>
>
diff --git a/src/modules/module-solaris.c b/src/modules/module-solaris.c
index 995b3c6..b8c5cac 100644
--- a/src/modules/module-solaris.c
+++ b/src/modules/module-solaris.c
@@ -75,7 +75,7 @@ PA_MODULE_USAGE(
"format=<sample format> "
"channels=<number of channels> "
"rate=<sample rate> "
- "buffer_size=<record buffer size> "
+ "buffer_length=<milliseconds> "
"channel_map=<channel map>");
PA_MODULE_LOAD_ONCE(FALSE);
@@ -94,8 +94,7 @@ struct userdata {
uint32_t frame_size;
int32_t buffer_size;
- volatile uint64_t written_bytes, read_bytes;
- pa_mutex *written_bytes_lock;
+ uint64_t written_bytes, read_bytes;
char *device_name;
int mode;
@@ -107,9 +106,8 @@ struct userdata {
uint32_t play_samples_msw, record_samples_msw;
uint32_t prev_playback_samples, prev_record_samples;
- pa_mutex *sample_counter_lock;
- size_t min_request;
+ int32_t minimum_request;
};
static const char* const valid_modargs[] = {
@@ -118,7 +116,7 @@ static const char* const valid_modargs[] = {
"device",
"record",
"playback",
- "buffer_size",
+ "buffer_length",
"format",
"rate",
"channels",
@@ -127,13 +125,9 @@ static const char* const valid_modargs[] = {
};
#define DEFAULT_DEVICE "/dev/audio"
-#define MIN_BUFFER_SIZE (640)
-#define MAX_RENDER_HZ (300)
-/* This render rate limit implies a minimum latency, but without it we waste too much CPU time in the
- * IO thread. The maximum render rate and minimum latency (or minimum buffer size) are unachievable on
- * common hardware anyway. Note that MIN_BUFFER_SIZE * MAX_RENDER_HZ >= 4 * 48000 Bps.
- */
+#define MAX_RENDER_HZ (300)
+/* This render rate limit imposes a minimum latency, but without it we waste too much CPU time. */
static uint64_t get_playback_buffered_bytes(struct userdata *u) {
audio_info_t info;
@@ -142,8 +136,6 @@ static uint64_t get_playback_buffered_bytes(struct userdata *u) {
pa_assert(u->sink);
- pa_mutex_lock(u->sample_counter_lock);
-
err = ioctl(u->fd, AUDIO_GETINFO, &info);
pa_assert(err >= 0);
@@ -159,8 +151,6 @@ static uint64_t get_playback_buffered_bytes(struct userdata *u) {
u->prev_playback_samples = info.play.samples;
played_bytes = (((uint64_t)u->play_samples_msw << 32) + info.play.samples) * u->frame_size;
- pa_mutex_unlock(u->sample_counter_lock);
-
return u->written_bytes - played_bytes;
}
@@ -171,11 +161,9 @@ static pa_usec_t sink_get_latency(struct userdata *u, pa_sample_spec *ss) {
pa_assert(ss);
if (u->fd >= 0) {
- pa_mutex_lock(u->written_bytes_lock);
r = pa_bytes_to_usec(get_playback_buffered_bytes(u), ss);
if (u->memchunk.memblock)
r += pa_bytes_to_usec(u->memchunk.length, ss);
- pa_mutex_unlock(u->written_bytes_lock);
}
return r;
}
@@ -487,7 +475,7 @@ static void sink_set_volume(pa_sink *s) {
if (u->fd >= 0) {
AUDIO_INITINFO(&info);
- info.play.gain = pa_cvolume_avg(&s->virtual_volume) * AUDIO_MAX_GAIN / PA_VOLUME_NORM;
+ info.play.gain = pa_cvolume_max(&s->virtual_volume) * AUDIO_MAX_GAIN / PA_VOLUME_NORM;
assert(info.play.gain <= AUDIO_MAX_GAIN);
if (ioctl(u->fd, AUDIO_SETINFO, &info) < 0) {
@@ -523,7 +511,7 @@ static void source_set_volume(pa_source *s) {
if (u->fd >= 0) {
AUDIO_INITINFO(&info);
- info.play.gain = pa_cvolume_avg(&s->virtual_volume) * AUDIO_MAX_GAIN / PA_VOLUME_NORM;
+ info.play.gain = pa_cvolume_max(&s->virtual_volume) * AUDIO_MAX_GAIN / PA_VOLUME_NORM;
assert(info.play.gain <= AUDIO_MAX_GAIN);
if (ioctl(u->fd, AUDIO_SETINFO, &info) < 0) {
@@ -580,6 +568,25 @@ static void sink_get_mute(pa_sink *s) {
}
}
+static void process_rewind(struct userdata *u) {
+ size_t rewind_nbytes;
+
+ pa_assert(u);
+
+ /* Figure out how much we shall rewind and reset the counter */
+ rewind_nbytes = u->sink->thread_info.rewind_nbytes;
+ u->sink->thread_info.rewind_nbytes = 0;
+
+ if (rewind_nbytes > 0) {
+ pa_log_debug("Requested to rewind %lu bytes.", (unsigned long) rewind_nbytes);
+ rewind_nbytes = PA_MIN(u->memchunk.length, rewind_nbytes);
+ u->memchunk.length -= rewind_nbytes;
+ pa_log_debug("Rewound %lu bytes.", (unsigned long) rewind_nbytes);
+ }
+
+ pa_sink_process_rewind(u->sink, rewind_nbytes);
+}
+
static void thread_func(void *userdata) {
struct userdata *u = userdata;
unsigned short revents = 0;
@@ -604,10 +611,13 @@ static void thread_func(void *userdata) {
uint64_t buffered_bytes;
if (u->sink->thread_info.rewind_requested)
- pa_sink_process_rewind(u->sink, 0);
+ process_rewind(u);
err = ioctl(u->fd, AUDIO_GETINFO, &info);
- pa_assert(err >= 0);
+ if (err < 0) {
+ pa_log("AUDIO_GETINFO ioctl failed: %s", pa_cstrerror(errno));
+ goto fail;
+ }
if (info.play.error) {
pa_log_debug("buffer under-run!");
@@ -635,7 +645,7 @@ static void thread_func(void *userdata) {
len = u->buffer_size - buffered_bytes;
len -= len % u->frame_size;
- if (len < u->min_request)
+ if (len < (size_t)u->minimum_request)
break;
if (u->memchunk.length < len)
@@ -648,12 +658,16 @@ static void thread_func(void *userdata) {
if (w <= 0) {
switch (errno) {
case EINTR:
- break;
+ continue;
case EAGAIN:
+ /* If the buffer_size is too big, we get EAGAIN. Avoiding that limit by trial and error
+ * is not ideal, but I don't know how to get the system to tell me what the limit is.
+ */
u->buffer_size = u->buffer_size * 18 / 25;
u->buffer_size -= u->buffer_size % u->frame_size;
- u->buffer_size = PA_MAX(u->buffer_size, (int32_t)MIN_BUFFER_SIZE);
+ u->buffer_size = PA_MAX(u->buffer_size, 2 * u->minimum_request);
pa_sink_set_max_request(u->sink, u->buffer_size);
+ pa_sink_set_max_rewind(u->sink, u->buffer_size);
pa_log("EAGAIN. Buffer size is now %u bytes (%llu buffered)", u->buffer_size, buffered_bytes);
break;
default:
@@ -663,10 +677,8 @@ static void thread_func(void *userdata) {
} else {
pa_assert(w % u->frame_size == 0);
- pa_mutex_lock(u->written_bytes_lock);
u->written_bytes += w;
u->memchunk.length -= w;
- pa_mutex_unlock(u->written_bytes_lock);
u->memchunk.index += w;
if (u->memchunk.length <= 0) {
@@ -797,6 +809,7 @@ int pa__init(pa_module *m) {
pa_sample_spec ss;
pa_channel_map map;
pa_modargs *ma = NULL;
+ uint32_t buffer_length_msec;
int fd;
pa_sink_new_data sink_new_data;
pa_source_new_data source_new_data;
@@ -822,8 +835,6 @@ int pa__init(pa_module *m) {
}
u = pa_xnew0(struct userdata, 1);
- u->sample_counter_lock = pa_mutex_new(FALSE, FALSE);
- u->written_bytes_lock = pa_mutex_new(FALSE, FALSE);
/*
* For a process (or several processes) to use the same audio device for both
@@ -839,13 +850,15 @@ int pa__init(pa_module *m) {
}
u->frame_size = pa_frame_size(&ss);
- u->buffer_size = 16384;
- if (pa_modargs_get_value_s32(ma, "buffer_size", &u->buffer_size) < 0) {
- pa_log("failed to parse buffer size argument");
+ u->minimum_request = pa_usec_to_bytes(PA_USEC_PER_SEC / MAX_RENDER_HZ, &ss);
+
+ buffer_length_msec = 100;
+ if (pa_modargs_get_value_u32(ma, "buffer_length", &buffer_length_msec) < 0) {
+ pa_log("failed to parse buffer_length argument");
goto fail;
}
- u->buffer_size -= u->buffer_size % u->frame_size;
- if (u->buffer_size < (int32_t)MIN_BUFFER_SIZE) {
+ u->buffer_size = pa_usec_to_bytes(1000 * buffer_length_msec, &ss);
+ if (u->buffer_size < 2 * u->minimum_request) {
pa_log("supplied buffer size argument is too small");
goto fail;
}
@@ -946,16 +959,18 @@ int pa__init(pa_module *m) {
u->sink->set_mute = sink_set_mute;
u->sink->refresh_volume = u->sink->refresh_muted = TRUE;
- u->sink->thread_info.max_request = u->buffer_size;
- u->min_request = pa_usec_to_bytes(PA_USEC_PER_SEC / MAX_RENDER_HZ, &ss);
+ pa_sink_set_max_request(u->sink, u->buffer_size);
+ pa_sink_set_max_rewind(u->sink, u->buffer_size);
} else
u->sink = NULL;
pa_assert(u->source || u->sink);
u->sig = pa_signal_new(SIGPOLL, sig_callback, u);
- pa_assert(u->sig);
- ioctl(u->fd, I_SETSIG, S_MSG);
+ if (u->sig)
+ ioctl(u->fd, I_SETSIG, S_MSG);
+ else
+ pa_log_warn("could not register SIGPOLL handler");
if (!(u->thread = pa_thread_new(thread_func, u))) {
pa_log("Failed to create thread.");
@@ -1010,8 +1025,10 @@ void pa__done(pa_module *m) {
if (!(u = m->userdata))
return;
- ioctl(u->fd, I_SETSIG, 0);
- pa_signal_free(u->sig);
+ if (u->sig) {
+ ioctl(u->fd, I_SETSIG, 0);
+ pa_signal_free(u->sig);
+ }
if (u->sink)
pa_sink_unlink(u->sink);
@@ -1044,9 +1061,6 @@ void pa__done(pa_module *m) {
if (u->fd >= 0)
close(u->fd);
- pa_mutex_free(u->written_bytes_lock);
- pa_mutex_free(u->sample_counter_lock);
-
pa_xfree(u->device_name);
pa_xfree(u);
diff --git a/src/pulse/mainloop-signal.c b/src/pulse/mainloop-signal.c
index 52f11c8..3dc7439 100644
--- a/src/pulse/mainloop-signal.c
+++ b/src/pulse/mainloop-signal.c
@@ -170,7 +170,7 @@ pa_signal_event* pa_signal_new(int sig, pa_signal_cb_t _callback, void *userdata
for (e = signals; e; e = e->next)
if (e->sig == sig)
- goto fail;
+ return NULL;
e = pa_xnew(pa_signal_event, 1);
e->sig = sig;
@@ -196,8 +196,7 @@ pa_signal_event* pa_signal_new(int sig, pa_signal_cb_t _callback, void *userdata
return e;
fail:
- if (e)
- pa_xfree(e);
+ pa_xfree(e);
return NULL;
}
diff --git a/src/pulsecore/core-util.c b/src/pulsecore/core-util.c
index 0d243ee..cee6545 100644
--- a/src/pulsecore/core-util.c
+++ b/src/pulsecore/core-util.c
@@ -2608,7 +2608,7 @@ char *pa_unescape(char *p) {
}
char *pa_realpath(const char *path) {
- char *r, *t;
+ char *r, *t, *path_buf;
pa_assert(path);
/* We want only abolsute paths */
@@ -2617,17 +2617,16 @@ char *pa_realpath(const char *path) {
return NULL;
}
-#ifndef __GLIBC__
-#error "It's not clear whether this system supports realpath(..., NULL) like GNU libc does. If it doesn't we need a private version of realpath() here."
-#endif
-
- if (!(r = realpath(path, NULL)))
+ path_buf = pa_xmalloc(PATH_MAX);
+ if (!(r = realpath(path, path_buf))) {
+ pa_xfree(path_buf);
return NULL;
+ }
/* We copy this here in case our pa_xmalloc() is not implemented
* on top of libc malloc() */
t = pa_xstrdup(r);
- pa_xfree(r);
+ pa_xfree(path_buf);
return t;
}
More information about the pulseaudio-discuss
mailing list