Merge pipewire-pulseaudio subtree

This commit is contained in:
Arun Raghavan 2019-11-03 15:20:57 +01:00
commit 37a72b2531
41 changed files with 14044 additions and 0 deletions

View file

@ -0,0 +1,65 @@
/***
This file is part of PulseAudio.
Copyright 2009 Lennart Poettering
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <stdbool.h>
#include "internal.h"
#include "bitset.h"
void pa_bitset_set(pa_bitset_t *b, unsigned k, bool v) {
pa_assert(b);
if (v)
b[k >> 5] |= 1 << (k & 31);
else
b[k >> 5] &= ~((uint32_t) (1 << (k & 31)));
}
bool pa_bitset_get(const pa_bitset_t *b, unsigned k) {
return !!(b[k >> 5] & (1 << (k & 31)));
}
bool pa_bitset_equals(const pa_bitset_t *b, unsigned n, ...) {
va_list ap;
pa_bitset_t *a;
bool equal;
a = alloca(PA_BITSET_SIZE(n));
spa_memzero(a, PA_BITSET_SIZE(n));
va_start(ap, n);
for (;;) {
int j = va_arg(ap, int);
if (j < 0)
break;
pa_bitset_set(a, j, true);
}
va_end(ap);
equal = memcmp(a, b, PA_BITSET_SIZE(n)) == 0;
return equal;
}

View file

@ -0,0 +1,34 @@
#ifndef foopulsecorebitsethfoo
#define foopulsecorebitsethfoo
/***
This file is part of PulseAudio.
Copyright 2009 Lennart Poettering
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
***/
#include <inttypes.h>
#define PA_BITSET_ELEMENTS(n) (((n)+31)/32)
#define PA_BITSET_SIZE(n) (PA_BITSET_ELEMENTS(n)*4)
typedef uint32_t pa_bitset_t;
void pa_bitset_set(pa_bitset_t *b, unsigned k, bool v);
bool pa_bitset_get(const pa_bitset_t *b, unsigned k);
bool pa_bitset_equals(const pa_bitset_t *b, unsigned n, ...);
#endif

View file

@ -0,0 +1,851 @@
/***
This file is part of PulseAudio.
Copyright 2005-2006 Lennart Poettering
Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pulse/xmalloc.h>
#include <pulse/channelmap.h>
#include "internal.h"
#include "bitset.h"
#include "sample-util.h"
const char *const table[PA_CHANNEL_POSITION_MAX] = {
[PA_CHANNEL_POSITION_MONO] = "mono",
[PA_CHANNEL_POSITION_FRONT_CENTER] = "front-center",
[PA_CHANNEL_POSITION_FRONT_LEFT] = "front-left",
[PA_CHANNEL_POSITION_FRONT_RIGHT] = "front-right",
[PA_CHANNEL_POSITION_REAR_CENTER] = "rear-center",
[PA_CHANNEL_POSITION_REAR_LEFT] = "rear-left",
[PA_CHANNEL_POSITION_REAR_RIGHT] = "rear-right",
[PA_CHANNEL_POSITION_LFE] = "lfe",
[PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER] = "front-left-of-center",
[PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER] = "front-right-of-center",
[PA_CHANNEL_POSITION_SIDE_LEFT] = "side-left",
[PA_CHANNEL_POSITION_SIDE_RIGHT] = "side-right",
[PA_CHANNEL_POSITION_AUX0] = "aux0",
[PA_CHANNEL_POSITION_AUX1] = "aux1",
[PA_CHANNEL_POSITION_AUX2] = "aux2",
[PA_CHANNEL_POSITION_AUX3] = "aux3",
[PA_CHANNEL_POSITION_AUX4] = "aux4",
[PA_CHANNEL_POSITION_AUX5] = "aux5",
[PA_CHANNEL_POSITION_AUX6] = "aux6",
[PA_CHANNEL_POSITION_AUX7] = "aux7",
[PA_CHANNEL_POSITION_AUX8] = "aux8",
[PA_CHANNEL_POSITION_AUX9] = "aux9",
[PA_CHANNEL_POSITION_AUX10] = "aux10",
[PA_CHANNEL_POSITION_AUX11] = "aux11",
[PA_CHANNEL_POSITION_AUX12] = "aux12",
[PA_CHANNEL_POSITION_AUX13] = "aux13",
[PA_CHANNEL_POSITION_AUX14] = "aux14",
[PA_CHANNEL_POSITION_AUX15] = "aux15",
[PA_CHANNEL_POSITION_AUX16] = "aux16",
[PA_CHANNEL_POSITION_AUX17] = "aux17",
[PA_CHANNEL_POSITION_AUX18] = "aux18",
[PA_CHANNEL_POSITION_AUX19] = "aux19",
[PA_CHANNEL_POSITION_AUX20] = "aux20",
[PA_CHANNEL_POSITION_AUX21] = "aux21",
[PA_CHANNEL_POSITION_AUX22] = "aux22",
[PA_CHANNEL_POSITION_AUX23] = "aux23",
[PA_CHANNEL_POSITION_AUX24] = "aux24",
[PA_CHANNEL_POSITION_AUX25] = "aux25",
[PA_CHANNEL_POSITION_AUX26] = "aux26",
[PA_CHANNEL_POSITION_AUX27] = "aux27",
[PA_CHANNEL_POSITION_AUX28] = "aux28",
[PA_CHANNEL_POSITION_AUX29] = "aux29",
[PA_CHANNEL_POSITION_AUX30] = "aux30",
[PA_CHANNEL_POSITION_AUX31] = "aux31",
[PA_CHANNEL_POSITION_TOP_CENTER] = "top-center",
[PA_CHANNEL_POSITION_TOP_FRONT_CENTER] = "top-front-center",
[PA_CHANNEL_POSITION_TOP_FRONT_LEFT] = "top-front-left",
[PA_CHANNEL_POSITION_TOP_FRONT_RIGHT] = "top-front-right",
[PA_CHANNEL_POSITION_TOP_REAR_CENTER] = "top-rear-center",
[PA_CHANNEL_POSITION_TOP_REAR_LEFT] = "top-rear-left",
[PA_CHANNEL_POSITION_TOP_REAR_RIGHT] = "top-rear-right"
};
const char *const pretty_table[PA_CHANNEL_POSITION_MAX] = {
[PA_CHANNEL_POSITION_MONO] = N_("Mono"),
[PA_CHANNEL_POSITION_FRONT_CENTER] = N_("Front Center"),
[PA_CHANNEL_POSITION_FRONT_LEFT] = N_("Front Left"),
[PA_CHANNEL_POSITION_FRONT_RIGHT] = N_("Front Right"),
[PA_CHANNEL_POSITION_REAR_CENTER] = N_("Rear Center"),
[PA_CHANNEL_POSITION_REAR_LEFT] = N_("Rear Left"),
[PA_CHANNEL_POSITION_REAR_RIGHT] = N_("Rear Right"),
[PA_CHANNEL_POSITION_LFE] = N_("Subwoofer"),
[PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER] = N_("Front Left-of-center"),
[PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER] = N_("Front Right-of-center"),
[PA_CHANNEL_POSITION_SIDE_LEFT] = N_("Side Left"),
[PA_CHANNEL_POSITION_SIDE_RIGHT] = N_("Side Right"),
[PA_CHANNEL_POSITION_AUX0] = N_("Auxiliary 0"),
[PA_CHANNEL_POSITION_AUX1] = N_("Auxiliary 1"),
[PA_CHANNEL_POSITION_AUX2] = N_("Auxiliary 2"),
[PA_CHANNEL_POSITION_AUX3] = N_("Auxiliary 3"),
[PA_CHANNEL_POSITION_AUX4] = N_("Auxiliary 4"),
[PA_CHANNEL_POSITION_AUX5] = N_("Auxiliary 5"),
[PA_CHANNEL_POSITION_AUX6] = N_("Auxiliary 6"),
[PA_CHANNEL_POSITION_AUX7] = N_("Auxiliary 7"),
[PA_CHANNEL_POSITION_AUX8] = N_("Auxiliary 8"),
[PA_CHANNEL_POSITION_AUX9] = N_("Auxiliary 9"),
[PA_CHANNEL_POSITION_AUX10] = N_("Auxiliary 10"),
[PA_CHANNEL_POSITION_AUX11] = N_("Auxiliary 11"),
[PA_CHANNEL_POSITION_AUX12] = N_("Auxiliary 12"),
[PA_CHANNEL_POSITION_AUX13] = N_("Auxiliary 13"),
[PA_CHANNEL_POSITION_AUX14] = N_("Auxiliary 14"),
[PA_CHANNEL_POSITION_AUX15] = N_("Auxiliary 15"),
[PA_CHANNEL_POSITION_AUX16] = N_("Auxiliary 16"),
[PA_CHANNEL_POSITION_AUX17] = N_("Auxiliary 17"),
[PA_CHANNEL_POSITION_AUX18] = N_("Auxiliary 18"),
[PA_CHANNEL_POSITION_AUX19] = N_("Auxiliary 19"),
[PA_CHANNEL_POSITION_AUX20] = N_("Auxiliary 20"),
[PA_CHANNEL_POSITION_AUX21] = N_("Auxiliary 21"),
[PA_CHANNEL_POSITION_AUX22] = N_("Auxiliary 22"),
[PA_CHANNEL_POSITION_AUX23] = N_("Auxiliary 23"),
[PA_CHANNEL_POSITION_AUX24] = N_("Auxiliary 24"),
[PA_CHANNEL_POSITION_AUX25] = N_("Auxiliary 25"),
[PA_CHANNEL_POSITION_AUX26] = N_("Auxiliary 26"),
[PA_CHANNEL_POSITION_AUX27] = N_("Auxiliary 27"),
[PA_CHANNEL_POSITION_AUX28] = N_("Auxiliary 28"),
[PA_CHANNEL_POSITION_AUX29] = N_("Auxiliary 29"),
[PA_CHANNEL_POSITION_AUX30] = N_("Auxiliary 30"),
[PA_CHANNEL_POSITION_AUX31] = N_("Auxiliary 31"),
[PA_CHANNEL_POSITION_TOP_CENTER] = N_("Top Center"),
[PA_CHANNEL_POSITION_TOP_FRONT_CENTER] = N_("Top Front Center"),
[PA_CHANNEL_POSITION_TOP_FRONT_LEFT] = N_("Top Front Left"),
[PA_CHANNEL_POSITION_TOP_FRONT_RIGHT] = N_("Top Front Right"),
[PA_CHANNEL_POSITION_TOP_REAR_CENTER] = N_("Top Rear Center"),
[PA_CHANNEL_POSITION_TOP_REAR_LEFT] = N_("Top Rear Left"),
[PA_CHANNEL_POSITION_TOP_REAR_RIGHT] = N_("Top Rear Right")
};
SPA_EXPORT
pa_channel_map* pa_channel_map_init(pa_channel_map *m) {
unsigned c;
pa_assert(m);
m->channels = 0;
for (c = 0; c < PA_CHANNELS_MAX; c++)
m->map[c] = PA_CHANNEL_POSITION_INVALID;
return m;
}
SPA_EXPORT
pa_channel_map* pa_channel_map_init_mono(pa_channel_map *m) {
pa_assert(m);
pa_channel_map_init(m);
m->channels = 1;
m->map[0] = PA_CHANNEL_POSITION_MONO;
return m;
}
SPA_EXPORT
pa_channel_map* pa_channel_map_init_stereo(pa_channel_map *m) {
pa_assert(m);
pa_channel_map_init(m);
m->channels = 2;
m->map[0] = PA_CHANNEL_POSITION_LEFT;
m->map[1] = PA_CHANNEL_POSITION_RIGHT;
return m;
}
SPA_EXPORT
pa_channel_map* pa_channel_map_init_auto(pa_channel_map *m, unsigned channels, pa_channel_map_def_t def) {
pa_assert(m);
pa_assert(pa_channels_valid(channels));
pa_assert(def < PA_CHANNEL_MAP_DEF_MAX);
pa_channel_map_init(m);
m->channels = (uint8_t) channels;
switch (def) {
case PA_CHANNEL_MAP_AIFF:
/* This is somewhat compatible with RFC3551 */
switch (channels) {
case 1:
m->map[0] = PA_CHANNEL_POSITION_MONO;
return m;
case 6:
m->map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
m->map[1] = PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER;
m->map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
m->map[3] = PA_CHANNEL_POSITION_FRONT_RIGHT;
m->map[4] = PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
m->map[5] = PA_CHANNEL_POSITION_REAR_CENTER;
return m;
case 5:
m->map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
m->map[3] = PA_CHANNEL_POSITION_REAR_LEFT;
m->map[4] = PA_CHANNEL_POSITION_REAR_RIGHT;
/* Fall through */
case 2:
m->map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
m->map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
return m;
case 3:
m->map[0] = PA_CHANNEL_POSITION_LEFT;
m->map[1] = PA_CHANNEL_POSITION_RIGHT;
m->map[2] = PA_CHANNEL_POSITION_CENTER;
return m;
case 4:
m->map[0] = PA_CHANNEL_POSITION_LEFT;
m->map[1] = PA_CHANNEL_POSITION_CENTER;
m->map[2] = PA_CHANNEL_POSITION_RIGHT;
m->map[3] = PA_CHANNEL_POSITION_REAR_CENTER;
return m;
default:
return NULL;
}
case PA_CHANNEL_MAP_ALSA:
switch (channels) {
case 1:
m->map[0] = PA_CHANNEL_POSITION_MONO;
return m;
case 8:
m->map[6] = PA_CHANNEL_POSITION_SIDE_LEFT;
m->map[7] = PA_CHANNEL_POSITION_SIDE_RIGHT;
/* Fall through */
case 6:
m->map[5] = PA_CHANNEL_POSITION_LFE;
/* Fall through */
case 5:
m->map[4] = PA_CHANNEL_POSITION_FRONT_CENTER;
/* Fall through */
case 4:
m->map[2] = PA_CHANNEL_POSITION_REAR_LEFT;
m->map[3] = PA_CHANNEL_POSITION_REAR_RIGHT;
/* Fall through */
case 2:
m->map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
m->map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
return m;
default:
return NULL;
}
case PA_CHANNEL_MAP_AUX: {
unsigned i;
for (i = 0; i < channels; i++)
m->map[i] = PA_CHANNEL_POSITION_AUX0 + i;
return m;
}
case PA_CHANNEL_MAP_WAVEEX:
/* Following http://www.microsoft.com/whdc/device/audio/multichaud.mspx#EKLAC */
switch (channels) {
case 1:
m->map[0] = PA_CHANNEL_POSITION_MONO;
return m;
case 18:
m->map[15] = PA_CHANNEL_POSITION_TOP_REAR_LEFT;
m->map[16] = PA_CHANNEL_POSITION_TOP_REAR_CENTER;
m->map[17] = PA_CHANNEL_POSITION_TOP_REAR_RIGHT;
/* Fall through */
case 15:
m->map[12] = PA_CHANNEL_POSITION_TOP_FRONT_LEFT;
m->map[13] = PA_CHANNEL_POSITION_TOP_FRONT_CENTER;
m->map[14] = PA_CHANNEL_POSITION_TOP_FRONT_RIGHT;
/* Fall through */
case 12:
m->map[11] = PA_CHANNEL_POSITION_TOP_CENTER;
/* Fall through */
case 11:
m->map[9] = PA_CHANNEL_POSITION_SIDE_LEFT;
m->map[10] = PA_CHANNEL_POSITION_SIDE_RIGHT;
/* Fall through */
case 9:
m->map[8] = PA_CHANNEL_POSITION_REAR_CENTER;
/* Fall through */
case 8:
m->map[6] = PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER;
m->map[7] = PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
/* Fall through */
case 6:
m->map[4] = PA_CHANNEL_POSITION_REAR_LEFT;
m->map[5] = PA_CHANNEL_POSITION_REAR_RIGHT;
/* Fall through */
case 4:
m->map[3] = PA_CHANNEL_POSITION_LFE;
/* Fall through */
case 3:
m->map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
/* Fall through */
case 2:
m->map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
m->map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
return m;
default:
return NULL;
}
case PA_CHANNEL_MAP_OSS:
switch (channels) {
case 1:
m->map[0] = PA_CHANNEL_POSITION_MONO;
return m;
case 8:
m->map[6] = PA_CHANNEL_POSITION_REAR_LEFT;
m->map[7] = PA_CHANNEL_POSITION_REAR_RIGHT;
/* Fall through */
case 6:
m->map[4] = PA_CHANNEL_POSITION_SIDE_LEFT;
m->map[5] = PA_CHANNEL_POSITION_SIDE_RIGHT;
/* Fall through */
case 4:
m->map[3] = PA_CHANNEL_POSITION_LFE;
/* Fall through */
case 3:
m->map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
/* Fall through */
case 2:
m->map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
m->map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
return m;
default:
return NULL;
}
default:
pa_assert_not_reached();
}
}
SPA_EXPORT
pa_channel_map* pa_channel_map_init_extend(pa_channel_map *m, unsigned channels, pa_channel_map_def_t def) {
unsigned c;
pa_assert(m);
pa_assert(pa_channels_valid(channels));
pa_assert(def < PA_CHANNEL_MAP_DEF_MAX);
pa_channel_map_init(m);
for (c = channels; c > 0; c--) {
if (pa_channel_map_init_auto(m, c, def)) {
unsigned i = 0;
for (; c < channels; c++) {
m->map[c] = PA_CHANNEL_POSITION_AUX0 + i;
i++;
}
m->channels = (uint8_t) channels;
return m;
}
}
return NULL;
}
SPA_EXPORT
const char* pa_channel_position_to_string(pa_channel_position_t pos) {
if (pos < 0 || pos >= PA_CHANNEL_POSITION_MAX)
return NULL;
return table[pos];
}
SPA_EXPORT
const char* pa_channel_position_to_pretty_string(pa_channel_position_t pos) {
if (pos < 0 || pos >= PA_CHANNEL_POSITION_MAX)
return NULL;
pa_init_i18n();
return _(pretty_table[pos]);
}
SPA_EXPORT
int pa_channel_map_equal(const pa_channel_map *a, const pa_channel_map *b) {
unsigned c;
pa_assert(a);
pa_assert(b);
pa_return_val_if_fail(pa_channel_map_valid(a), 0);
if (PA_UNLIKELY(a == b))
return 1;
pa_return_val_if_fail(pa_channel_map_valid(b), 0);
if (a->channels != b->channels)
return 0;
for (c = 0; c < a->channels; c++)
if (a->map[c] != b->map[c])
return 0;
return 1;
}
SPA_EXPORT
char* pa_channel_map_snprint(char *s, size_t l, const pa_channel_map *map) {
unsigned channel;
bool first = true;
char *e;
pa_assert(s);
pa_assert(l > 0);
pa_assert(map);
pa_init_i18n();
if (!pa_channel_map_valid(map)) {
pa_snprintf(s, l, _("(invalid)"));
return s;
}
*(e = s) = 0;
for (channel = 0; channel < map->channels && l > 1; channel++) {
l -= pa_snprintf(e, l, "%s%s",
first ? "" : ",",
pa_channel_position_to_string(map->map[channel]));
e = strchr(e, 0);
first = false;
}
return s;
}
SPA_EXPORT
pa_channel_position_t pa_channel_position_from_string(const char *p) {
pa_channel_position_t i;
pa_assert(p);
/* Some special aliases */
if (pa_streq(p, "left"))
return PA_CHANNEL_POSITION_LEFT;
else if (pa_streq(p, "right"))
return PA_CHANNEL_POSITION_RIGHT;
else if (pa_streq(p, "center"))
return PA_CHANNEL_POSITION_CENTER;
else if (pa_streq(p, "subwoofer"))
return PA_CHANNEL_POSITION_SUBWOOFER;
for (i = 0; i < PA_CHANNEL_POSITION_MAX; i++)
if (pa_streq(p, table[i]))
return i;
return PA_CHANNEL_POSITION_INVALID;
}
SPA_EXPORT
pa_channel_map *pa_channel_map_parse(pa_channel_map *rmap, const char *s) {
pa_channel_map map;
char **tokens;
int i, n_tokens;
pa_assert(rmap);
pa_assert(s);
pa_channel_map_init(&map);
/* We don't need to match against the well known channel mapping
* "mono" here explicitly, because that can be understood as
* listing with one channel called "mono". */
if (pa_streq(s, "stereo")) {
map.channels = 2;
map.map[0] = PA_CHANNEL_POSITION_LEFT;
map.map[1] = PA_CHANNEL_POSITION_RIGHT;
goto finish;
} else if (pa_streq(s, "surround-21")) {
map.channels = 3;
map.map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
map.map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
map.map[2] = PA_CHANNEL_POSITION_LFE;
goto finish;
} else if (pa_streq(s, "surround-40")) {
map.channels = 4;
map.map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
map.map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
map.map[2] = PA_CHANNEL_POSITION_REAR_LEFT;
map.map[3] = PA_CHANNEL_POSITION_REAR_RIGHT;
goto finish;
} else if (pa_streq(s, "surround-41")) {
map.channels = 5;
map.map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
map.map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
map.map[2] = PA_CHANNEL_POSITION_REAR_LEFT;
map.map[3] = PA_CHANNEL_POSITION_REAR_RIGHT;
map.map[4] = PA_CHANNEL_POSITION_LFE;
goto finish;
} else if (pa_streq(s, "surround-50")) {
map.channels = 5;
map.map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
map.map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
map.map[2] = PA_CHANNEL_POSITION_REAR_LEFT;
map.map[3] = PA_CHANNEL_POSITION_REAR_RIGHT;
map.map[4] = PA_CHANNEL_POSITION_FRONT_CENTER;
goto finish;
} else if (pa_streq(s, "surround-51")) {
map.channels = 6;
map.map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
map.map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
map.map[2] = PA_CHANNEL_POSITION_REAR_LEFT;
map.map[3] = PA_CHANNEL_POSITION_REAR_RIGHT;
map.map[4] = PA_CHANNEL_POSITION_FRONT_CENTER;
map.map[5] = PA_CHANNEL_POSITION_LFE;
goto finish;
} else if (pa_streq(s, "surround-71")) {
map.channels = 8;
map.map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
map.map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
map.map[2] = PA_CHANNEL_POSITION_REAR_LEFT;
map.map[3] = PA_CHANNEL_POSITION_REAR_RIGHT;
map.map[4] = PA_CHANNEL_POSITION_FRONT_CENTER;
map.map[5] = PA_CHANNEL_POSITION_LFE;
map.map[6] = PA_CHANNEL_POSITION_SIDE_LEFT;
map.map[7] = PA_CHANNEL_POSITION_SIDE_RIGHT;
goto finish;
}
map.channels = 0;
tokens = pw_split_strv(s, ",", INT_MAX, &n_tokens);
for (i = 0; i < n_tokens; i++) {
pa_channel_position_t f;
if (map.channels >= PA_CHANNELS_MAX) {
pw_free_strv(tokens);
return NULL;
}
if ((f = pa_channel_position_from_string(tokens[i])) == PA_CHANNEL_POSITION_INVALID) {
pw_free_strv(tokens);
return NULL;
}
map.map[map.channels++] = f;
}
pw_free_strv(tokens);
finish:
if (!pa_channel_map_valid(&map))
return NULL;
*rmap = map;
return rmap;
}
SPA_EXPORT
int pa_channel_map_valid(const pa_channel_map *map) {
unsigned c;
pa_assert(map);
if (!pa_channels_valid(map->channels))
return 0;
for (c = 0; c < map->channels; c++)
if (map->map[c] < 0 || map->map[c] >= PA_CHANNEL_POSITION_MAX)
return 0;
return 1;
}
SPA_EXPORT
int pa_channel_map_compatible(const pa_channel_map *map, const pa_sample_spec *ss) {
pa_assert(map);
pa_assert(ss);
pa_return_val_if_fail(pa_channel_map_valid(map), 0);
pa_return_val_if_fail(pa_sample_spec_valid(ss), 0);
return map->channels == ss->channels;
}
SPA_EXPORT
int pa_channel_map_superset(const pa_channel_map *a, const pa_channel_map *b) {
pa_channel_position_mask_t am, bm;
pa_assert(a);
pa_assert(b);
pa_return_val_if_fail(pa_channel_map_valid(a), 0);
if (PA_UNLIKELY(a == b))
return 1;
pa_return_val_if_fail(pa_channel_map_valid(b), 0);
am = pa_channel_map_mask(a);
bm = pa_channel_map_mask(b);
return (bm & am) == bm;
}
SPA_EXPORT
int pa_channel_map_can_balance(const pa_channel_map *map) {
pa_channel_position_mask_t m;
pa_assert(map);
pa_return_val_if_fail(pa_channel_map_valid(map), 0);
m = pa_channel_map_mask(map);
return
(PA_CHANNEL_POSITION_MASK_LEFT & m) &&
(PA_CHANNEL_POSITION_MASK_RIGHT & m);
}
SPA_EXPORT
int pa_channel_map_can_fade(const pa_channel_map *map) {
pa_channel_position_mask_t m;
pa_assert(map);
pa_return_val_if_fail(pa_channel_map_valid(map), 0);
m = pa_channel_map_mask(map);
return
(PA_CHANNEL_POSITION_MASK_FRONT & m) &&
(PA_CHANNEL_POSITION_MASK_REAR & m);
}
SPA_EXPORT
int pa_channel_map_can_lfe_balance(const pa_channel_map *map) {
pa_channel_position_mask_t m;
pa_assert(map);
pa_return_val_if_fail(pa_channel_map_valid(map), 0);
m = pa_channel_map_mask(map);
return
(PA_CHANNEL_POSITION_MASK_LFE & m) &&
(PA_CHANNEL_POSITION_MASK_HFE & m);
}
SPA_EXPORT
const char* pa_channel_map_to_name(const pa_channel_map *map) {
pa_bitset_t in_map[PA_BITSET_ELEMENTS(PA_CHANNEL_POSITION_MAX)];
unsigned c;
pa_assert(map);
pa_return_val_if_fail(pa_channel_map_valid(map), NULL);
memset(in_map, 0, sizeof(in_map));
for (c = 0; c < map->channels; c++)
pa_bitset_set(in_map, map->map[c], true);
if (pa_bitset_equals(in_map, PA_CHANNEL_POSITION_MAX,
PA_CHANNEL_POSITION_MONO, -1))
return "mono";
if (pa_bitset_equals(in_map, PA_CHANNEL_POSITION_MAX,
PA_CHANNEL_POSITION_LEFT, PA_CHANNEL_POSITION_RIGHT, -1))
return "stereo";
if (pa_bitset_equals(in_map, PA_CHANNEL_POSITION_MAX,
PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT, -1))
return "surround-40";
if (pa_bitset_equals(in_map, PA_CHANNEL_POSITION_MAX,
PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
PA_CHANNEL_POSITION_LFE, -1))
return "surround-41";
if (pa_bitset_equals(in_map, PA_CHANNEL_POSITION_MAX,
PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
PA_CHANNEL_POSITION_FRONT_CENTER, -1))
return "surround-50";
if (pa_bitset_equals(in_map, PA_CHANNEL_POSITION_MAX,
PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
PA_CHANNEL_POSITION_FRONT_CENTER, PA_CHANNEL_POSITION_LFE, -1))
return "surround-51";
if (pa_bitset_equals(in_map, PA_CHANNEL_POSITION_MAX,
PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
PA_CHANNEL_POSITION_FRONT_CENTER, PA_CHANNEL_POSITION_LFE,
PA_CHANNEL_POSITION_SIDE_LEFT, PA_CHANNEL_POSITION_SIDE_RIGHT, -1))
return "surround-71";
return NULL;
}
SPA_EXPORT
const char* pa_channel_map_to_pretty_name(const pa_channel_map *map) {
pa_bitset_t in_map[PA_BITSET_ELEMENTS(PA_CHANNEL_POSITION_MAX)];
unsigned c;
pa_assert(map);
pa_return_val_if_fail(pa_channel_map_valid(map), NULL);
memset(in_map, 0, sizeof(in_map));
for (c = 0; c < map->channels; c++)
pa_bitset_set(in_map, map->map[c], true);
pa_init_i18n();
if (pa_bitset_equals(in_map, PA_CHANNEL_POSITION_MAX,
PA_CHANNEL_POSITION_MONO, -1))
return _("Mono");
if (pa_bitset_equals(in_map, PA_CHANNEL_POSITION_MAX,
PA_CHANNEL_POSITION_LEFT, PA_CHANNEL_POSITION_RIGHT, -1))
return _("Stereo");
if (pa_bitset_equals(in_map, PA_CHANNEL_POSITION_MAX,
PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT, -1))
return _("Surround 4.0");
if (pa_bitset_equals(in_map, PA_CHANNEL_POSITION_MAX,
PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
PA_CHANNEL_POSITION_LFE, -1))
return _("Surround 4.1");
if (pa_bitset_equals(in_map, PA_CHANNEL_POSITION_MAX,
PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
PA_CHANNEL_POSITION_FRONT_CENTER, -1))
return _("Surround 5.0");
if (pa_bitset_equals(in_map, PA_CHANNEL_POSITION_MAX,
PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
PA_CHANNEL_POSITION_FRONT_CENTER, PA_CHANNEL_POSITION_LFE, -1))
return _("Surround 5.1");
if (pa_bitset_equals(in_map, PA_CHANNEL_POSITION_MAX,
PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT,
PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT,
PA_CHANNEL_POSITION_FRONT_CENTER, PA_CHANNEL_POSITION_LFE,
PA_CHANNEL_POSITION_SIDE_LEFT, PA_CHANNEL_POSITION_SIDE_RIGHT, -1))
return _("Surround 7.1");
return NULL;
}
SPA_EXPORT
int pa_channel_map_has_position(const pa_channel_map *map, pa_channel_position_t p) {
unsigned c;
pa_return_val_if_fail(pa_channel_map_valid(map), 0);
pa_return_val_if_fail(p < PA_CHANNEL_POSITION_MAX, 0);
for (c = 0; c < map->channels; c++)
if (map->map[c] == p)
return 1;
return 0;
}
SPA_EXPORT
pa_channel_position_mask_t pa_channel_map_mask(const pa_channel_map *map) {
unsigned c;
pa_channel_position_mask_t r = 0;
pa_return_val_if_fail(pa_channel_map_valid(map), 0);
for (c = 0; c < map->channels; c++)
r |= PA_CHANNEL_POSITION_MASK(map->map[c]);
return r;
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,248 @@
/***
This file is part of PulseAudio.
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "core-format.h"
#include <pulse/def.h>
#include <pulse/xmalloc.h>
#include "internal.h"
SPA_EXPORT
int pa_format_info_get_sample_format(const pa_format_info *f, pa_sample_format_t *sf) {
int r;
char *sf_str;
pa_sample_format_t sf_local;
pa_assert(f);
pa_assert(sf);
r = pa_format_info_get_prop_string(f, PA_PROP_FORMAT_SAMPLE_FORMAT, &sf_str);
if (r < 0)
return r;
sf_local = pa_parse_sample_format(sf_str);
pa_xfree(sf_str);
if (!pa_sample_format_valid(sf_local)) {
pa_log_debug("Invalid sample format.");
return -PA_ERR_INVALID;
}
*sf = sf_local;
return 0;
}
SPA_EXPORT
int pa_format_info_get_rate(const pa_format_info *f, uint32_t *rate) {
int r;
int rate_local;
pa_assert(f);
pa_assert(rate);
r = pa_format_info_get_prop_int(f, PA_PROP_FORMAT_RATE, &rate_local);
if (r < 0)
return r;
if (!pa_sample_rate_valid(rate_local)) {
pa_log_debug("Invalid sample rate: %i", rate_local);
return -PA_ERR_INVALID;
}
*rate = rate_local;
return 0;
}
SPA_EXPORT
int pa_format_info_get_channels(const pa_format_info *f, uint8_t *channels) {
int r;
int channels_local;
pa_assert(f);
pa_assert(channels);
r = pa_format_info_get_prop_int(f, PA_PROP_FORMAT_CHANNELS, &channels_local);
if (r < 0)
return r;
if (!pa_channels_valid(channels_local)) {
pa_log_debug("Invalid channel count: %i", channels_local);
return -PA_ERR_INVALID;
}
*channels = channels_local;
return 0;
}
SPA_EXPORT
int pa_format_info_get_channel_map(const pa_format_info *f, pa_channel_map *map) {
int r;
char *map_str;
pa_assert(f);
pa_assert(map);
r = pa_format_info_get_prop_string(f, PA_PROP_FORMAT_CHANNEL_MAP, &map_str);
if (r < 0)
return r;
map = pa_channel_map_parse(map, map_str);
pa_xfree(map_str);
if (!map) {
pa_log_debug("Failed to parse channel map.");
return -PA_ERR_INVALID;
}
return 0;
}
SPA_EXPORT
pa_format_info *pa_format_info_from_sample_spec2(const pa_sample_spec *ss, const pa_channel_map *map, bool set_format,
bool set_rate, bool set_channels) {
pa_format_info *format = NULL;
pa_assert(ss);
format = pa_format_info_new();
format->encoding = PA_ENCODING_PCM;
if (set_format)
pa_format_info_set_sample_format(format, ss->format);
if (set_rate)
pa_format_info_set_rate(format, ss->rate);
if (set_channels) {
pa_format_info_set_channels(format, ss->channels);
if (map) {
if (map->channels != ss->channels) {
pa_log_debug("Channel map is incompatible with the sample spec.");
goto fail;
}
pa_format_info_set_channel_map(format, map);
}
}
return format;
fail:
if (format)
pa_format_info_free(format);
return NULL;
}
SPA_EXPORT
int pa_format_info_to_sample_spec2(const pa_format_info *f, pa_sample_spec *ss, pa_channel_map *map,
const pa_sample_spec *fallback_ss, const pa_channel_map *fallback_map) {
int r, r2;
pa_sample_spec ss_local;
pa_channel_map map_local;
pa_assert(f);
pa_assert(ss);
pa_assert(map);
pa_assert(fallback_ss);
pa_assert(fallback_map);
if (!pa_format_info_is_pcm(f))
return pa_format_info_to_sample_spec_fake(f, ss, map);
r = pa_format_info_get_sample_format(f, &ss_local.format);
if (r == -PA_ERR_NOENTITY)
ss_local.format = fallback_ss->format;
else if (r < 0)
return r;
pa_assert(pa_sample_format_valid(ss_local.format));
r = pa_format_info_get_rate(f, &ss_local.rate);
if (r == -PA_ERR_NOENTITY)
ss_local.rate = fallback_ss->rate;
else if (r < 0)
return r;
pa_assert(pa_sample_rate_valid(ss_local.rate));
r = pa_format_info_get_channels(f, &ss_local.channels);
r2 = pa_format_info_get_channel_map(f, &map_local);
if (r == -PA_ERR_NOENTITY && r2 >= 0)
ss_local.channels = map_local.channels;
else if (r == -PA_ERR_NOENTITY)
ss_local.channels = fallback_ss->channels;
else if (r < 0)
return r;
pa_assert(pa_channels_valid(ss_local.channels));
if (r2 >= 0 && map_local.channels != ss_local.channels) {
pa_log_debug("Channel map is not compatible with the sample spec.");
return -PA_ERR_INVALID;
}
if (r2 == -PA_ERR_NOENTITY) {
if (fallback_map->channels == ss_local.channels)
map_local = *fallback_map;
else
pa_channel_map_init_extend(&map_local, ss_local.channels, PA_CHANNEL_MAP_DEFAULT);
} else if (r2 < 0)
return r2;
pa_assert(pa_channel_map_valid(&map_local));
pa_assert(ss_local.channels == map_local.channels);
*ss = ss_local;
*map = map_local;
return 0;
}
SPA_EXPORT
int pa_format_info_to_sample_spec_fake(const pa_format_info *f, pa_sample_spec *ss, pa_channel_map *map) {
int rate;
pa_assert(f);
pa_assert(ss);
/* Note: When we add support for non-IEC61937 encapsulated compressed
* formats, this function should return a non-zero values for these. */
ss->format = PA_SAMPLE_S16LE;
ss->channels = 2;
if (map)
pa_channel_map_init_stereo(map);
pa_return_val_if_fail(pa_format_info_get_prop_int(f, PA_PROP_FORMAT_RATE, &rate) == 0, -PA_ERR_INVALID);
ss->rate = (uint32_t) rate;
if (f->encoding == PA_ENCODING_EAC3_IEC61937)
ss->rate *= 4;
return 0;
}

View file

@ -0,0 +1,79 @@
#ifndef foocoreformathfoo
#define foocoreformathfoo
/***
This file is part of PulseAudio.
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
***/
#include <pulse/format.h>
#include <stdbool.h>
/* Gets the sample format stored in the format info. Returns a negative error
* code on failure. If the sample format property is not set at all, returns
* -PA_ERR_NOENTITY. */
int pa_format_info_get_sample_format(const pa_format_info *f, pa_sample_format_t *sf);
/* Gets the sample rate stored in the format info. Returns a negative error
* code on failure. If the sample rate property is not set at all, returns
* -PA_ERR_NOENTITY. */
int pa_format_info_get_rate(const pa_format_info *f, uint32_t *rate);
/* Gets the channel count stored in the format info. Returns a negative error
* code on failure. If the channels property is not set at all, returns
* -PA_ERR_NOENTITY. */
int pa_format_info_get_channels(const pa_format_info *f, uint8_t *channels);
/* Gets the channel map stored in the format info. Returns a negative error
* code on failure. If the channel map property is not set at all, returns
* -PA_ERR_NOENTITY. */
int pa_format_info_get_channel_map(const pa_format_info *f, pa_channel_map *map);
/* Convert a sample spec and an optional channel map to a new PCM format info
* object (remember to free it). If map is NULL, then the channel map will be
* left unspecified. If some fields of the sample spec should be ignored, pass
* false for set_format, set_rate and set_channels as appropriate, then those
* fields will be left unspecified. This function returns NULL if the input is
* invalid (for example, setting the sample rate was requested, but the rate
* in ss is invalid).
*
* pa_format_info_from_sample_spec() exists too. This "version 2" was created,
* because the original function doesn't provide the possibility of ignoring
* some of the sample spec fields. That functionality can't be added to the
* original function, because the function is a part of the public API and
* adding parameters to it would break the API. */
pa_format_info *pa_format_info_from_sample_spec2(const pa_sample_spec *ss, const pa_channel_map *map, bool set_format,
bool set_rate, bool set_channels);
/* Convert the format info into a sample spec and a channel map. If the format
* info doesn't contain some information, the fallback sample spec and channel
* map are used to populate the output.
*
* pa_format_info_to_sample_spec() exists too. This "version 2" was created,
* because the original function doesn't provide the possibility of specifying
* a fallback sample spec and channel map. That functionality can't be added to
* the original function, because the function is part of the public API and
* adding parameters to it would break the API. */
int pa_format_info_to_sample_spec2(const pa_format_info *f, pa_sample_spec *ss, pa_channel_map *map,
const pa_sample_spec *fallback_ss, const pa_channel_map *fallback_map);
/* For compressed formats. Converts the format info into a sample spec and a
* channel map that an ALSA device can use as its configuration parameters when
* playing back the compressed data. That is, the returned sample spec doesn't
* describe the audio content, but the device parameters. */
int pa_format_info_to_sample_spec_fake(const pa_format_info *f, pa_sample_spec *ss, pa_channel_map *map);
#endif

View file

@ -0,0 +1,50 @@
/* PipeWire
* Copyright (C) 2018 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <errno.h>
#include <spa/utils/defs.h>
#include <pulse/direction.h>
#define pa_init_i18n()
#define _(String) (String)
SPA_EXPORT
int pa_direction_valid(pa_direction_t direction)
{
if (direction != PA_DIRECTION_INPUT
&& direction != PA_DIRECTION_OUTPUT
&& direction != (PA_DIRECTION_INPUT | PA_DIRECTION_OUTPUT))
return 0;
return 1;
}
SPA_EXPORT
const char *pa_direction_to_string(pa_direction_t direction) {
pa_init_i18n();
if (direction == PA_DIRECTION_INPUT)
return _("input");
if (direction == PA_DIRECTION_OUTPUT)
return _("output");
if (direction == (PA_DIRECTION_INPUT | PA_DIRECTION_OUTPUT))
return _("bidirectional");
return _("invalid");
}

View file

@ -0,0 +1,75 @@
/* PipeWire
* Copyright (C) 2018 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <unistd.h>
#include <errno.h>
#include <spa/utils/defs.h>
#include <pulse/def.h>
#include <pulse/error.h>
#define N_(String) (String)
#define _(String) (String)
#define pa_init_i18n()
SPA_EXPORT
const char*pa_strerror(int error)
{
static const char* const errortab[PA_ERR_MAX] = {
[PA_OK] = N_("OK"),
[PA_ERR_ACCESS] = N_("Access denied"),
[PA_ERR_COMMAND] = N_("Unknown command"),
[PA_ERR_INVALID] = N_("Invalid argument"),
[PA_ERR_EXIST] = N_("Entity exists"),
[PA_ERR_NOENTITY] = N_("No such entity"),
[PA_ERR_CONNECTIONREFUSED] = N_("Connection refused"),
[PA_ERR_PROTOCOL] = N_("Protocol error"),
[PA_ERR_TIMEOUT] = N_("Timeout"),
[PA_ERR_AUTHKEY] = N_("No authentication key"),
[PA_ERR_INTERNAL] = N_("Internal error"),
[PA_ERR_CONNECTIONTERMINATED] = N_("Connection terminated"),
[PA_ERR_KILLED] = N_("Entity killed"),
[PA_ERR_INVALIDSERVER] = N_("Invalid server"),
[PA_ERR_MODINITFAILED] = N_("Module initialization failed"),
[PA_ERR_BADSTATE] = N_("Bad state"),
[PA_ERR_NODATA] = N_("No data"),
[PA_ERR_VERSION] = N_("Incompatible protocol version"),
[PA_ERR_TOOLARGE] = N_("Too large"),
[PA_ERR_NOTSUPPORTED] = N_("Not supported"),
[PA_ERR_UNKNOWN] = N_("Unknown error code"),
[PA_ERR_NOEXTENSION] = N_("No such extension"),
[PA_ERR_OBSOLETE] = N_("Obsolete functionality"),
[PA_ERR_NOTIMPLEMENTED] = N_("Missing implementation"),
[PA_ERR_FORKED] = N_("Client forked"),
[PA_ERR_IO] = N_("Input/Output error"),
[PA_ERR_BUSY] = N_("Device or resource busy")
};
pa_init_i18n();
if (error < 0)
error = -error;
if (error >= PA_ERR_MAX)
return NULL;
return _(errortab[error]);
}

View file

@ -0,0 +1,113 @@
/* PipeWire
* Copyright (C) 2018 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <errno.h>
#include <pipewire/log.h>
#include <pulse/ext-device-manager.h>
#include "internal.h"
SPA_EXPORT
pa_operation *pa_ext_device_manager_test(
pa_context *c,
pa_ext_device_manager_test_cb_t cb,
void *userdata)
{
pw_log_warn("Not Implemented");
return NULL;
}
SPA_EXPORT
pa_operation *pa_ext_device_manager_read(
pa_context *c,
pa_ext_device_manager_read_cb_t cb,
void *userdata)
{
pw_log_warn("Not Implemented");
return NULL;
}
SPA_EXPORT
pa_operation *pa_ext_device_manager_set_device_description(
pa_context *c,
const char* device,
const char* description,
pa_context_success_cb_t cb,
void *userdata)
{
pw_log_warn("Not Implemented");
return NULL;
}
SPA_EXPORT
pa_operation *pa_ext_device_manager_delete(
pa_context *c,
const char *const s[],
pa_context_success_cb_t cb,
void *userdata)
{
pw_log_warn("Not Implemented");
return NULL;
}
SPA_EXPORT
pa_operation *pa_ext_device_manager_enable_role_device_priority_routing(
pa_context *c,
int enable,
pa_context_success_cb_t cb,
void *userdata)
{
pw_log_warn("Not Implemented");
return NULL;
}
SPA_EXPORT
pa_operation *pa_ext_device_manager_reorder_devices_for_role(
pa_context *c,
const char* role,
const char** devices,
pa_context_success_cb_t cb,
void *userdata)
{
pw_log_warn("Not Implemented");
return NULL;
}
SPA_EXPORT
pa_operation *pa_ext_device_manager_subscribe(
pa_context *c,
int enable,
pa_context_success_cb_t cb,
void *userdata)
{
pw_log_warn("Not Implemented");
return NULL;
}
SPA_EXPORT
void pa_ext_device_manager_set_subscribe_cb(
pa_context *c,
pa_ext_device_manager_subscribe_cb_t cb,
void *userdata)
{
pw_log_warn("Not Implemented");
}

View file

@ -0,0 +1,194 @@
/* PipeWire
* Copyright (C) 2018 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <pipewire/log.h>
#include <pulse/ext-device-restore.h>
#include "internal.h"
#define EXT_VERSION 1
struct ext_data {
pa_context *context;
pa_ext_device_restore_test_cb_t test_cb;
pa_ext_device_restore_read_device_formats_cb_t read_cb;
pa_context_success_cb_t success_cb;
void *userdata;
};
static void restore_test(pa_operation *o, void *userdata)
{
struct ext_data *d = userdata;
if (d->test_cb)
d->test_cb(o->context, EXT_VERSION, d->userdata);
pa_operation_done(o);
}
SPA_EXPORT
pa_operation *pa_ext_device_restore_test(
pa_context *c,
pa_ext_device_restore_test_cb_t cb,
void *userdata)
{
pa_operation *o;
struct ext_data *d;
pa_assert(c);
pa_assert(c->refcount >= 1);
PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
o = pa_operation_new(c, NULL, restore_test, sizeof(struct ext_data));
d = o->userdata;
d->context = c;
d->test_cb = cb;
d->userdata = userdata;
pa_operation_sync(o);
return o;
}
static void on_success(pa_operation *o, void *userdata)
{
struct ext_data *d = userdata;
if (d->success_cb)
d->success_cb(o->context, PA_OK, d->userdata);
pa_operation_done(o);
}
SPA_EXPORT
pa_operation *pa_ext_device_restore_subscribe(
pa_context *c,
int enable,
pa_context_success_cb_t cb,
void *userdata)
{
pa_operation *o;
struct ext_data *d;
pa_assert(c);
pa_assert(c->refcount >= 1);
PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
o = pa_operation_new(c, NULL, on_success, sizeof(struct ext_data));
d = o->userdata;
d->context = c;
d->success_cb = cb;
d->userdata = userdata;
pa_operation_sync(o);
return o;
}
SPA_EXPORT
void pa_ext_device_restore_set_subscribe_cb(
pa_context *c,
pa_ext_device_restore_subscribe_cb_t cb,
void *userdata)
{
pw_log_warn("Not Implemented");
}
static void read_formats(pa_operation *o, void *userdata)
{
struct ext_data *d = userdata;
if (d->read_cb)
d->read_cb(o->context, NULL, 1, d->userdata);
pa_operation_done(o);
}
SPA_EXPORT
pa_operation *pa_ext_device_restore_read_formats_all(
pa_context *c,
pa_ext_device_restore_read_device_formats_cb_t cb,
void *userdata)
{
pa_operation *o;
struct ext_data *d;
pa_assert(c);
pa_assert(c->refcount >= 1);
PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
o = pa_operation_new(c, NULL, read_formats, sizeof(struct ext_data));
d = o->userdata;
d->context = c;
d->read_cb = cb;
d->userdata = userdata;
pa_operation_sync(o);
return o;
}
SPA_EXPORT
pa_operation *pa_ext_device_restore_read_formats(
pa_context *c,
pa_device_type_t type,
uint32_t idx,
pa_ext_device_restore_read_device_formats_cb_t cb,
void *userdata)
{
pa_operation *o;
struct ext_data *d;
pa_assert(c);
pa_assert(c->refcount >= 1);
PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
o = pa_operation_new(c, NULL, read_formats, sizeof(struct ext_data));
d = o->userdata;
d->context = c;
d->read_cb = cb;
d->userdata = userdata;
pa_operation_sync(o);
return o;
}
SPA_EXPORT
pa_operation *pa_ext_device_restore_save_formats(
pa_context *c,
pa_device_type_t type,
uint32_t idx,
uint8_t n_formats,
pa_format_info **formats,
pa_context_success_cb_t cb,
void *userdata)
{
pa_operation *o;
struct ext_data *d;
pa_assert(c);
pa_assert(c->refcount >= 1);
PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
o = pa_operation_new(c, NULL, on_success, sizeof(struct ext_data));
d = o->userdata;
d->context = c;
d->success_cb = cb;
d->userdata = userdata;
pa_operation_sync(o);
return o;
}

View file

@ -0,0 +1,201 @@
/* PipeWire
* Copyright (C) 2018 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <pipewire/pipewire.h>
#include <pulse/ext-stream-restore.h>
#include "internal.h"
#define EXT_VERSION 1
struct stream_data {
pa_context *context;
pa_ext_stream_restore_test_cb_t test_cb;
pa_ext_stream_restore_read_cb_t read_cb;
pa_context_success_cb_t success_cb;
void *userdata;
};
static void restore_test(pa_operation *o, void *userdata)
{
struct stream_data *d = userdata;
if (d->test_cb)
d->test_cb(o->context, EXT_VERSION, d->userdata);
pa_operation_done(o);
}
SPA_EXPORT
pa_operation *pa_ext_stream_restore_test(
pa_context *c,
pa_ext_stream_restore_test_cb_t cb,
void *userdata)
{
pa_operation *o;
struct stream_data *d;
pa_assert(c);
pa_assert(c->refcount >= 1);
PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
o = pa_operation_new(c, NULL, restore_test, sizeof(struct stream_data));
d = o->userdata;
d->context = c;
d->test_cb = cb;
d->userdata = userdata;
pa_operation_sync(o);
return o;
}
static void restore_read(pa_operation *o, void *userdata)
{
struct stream_data *d = userdata;
if (d->read_cb)
d->read_cb(o->context, NULL, 1, d->userdata);
pa_operation_done(o);
}
SPA_EXPORT
pa_operation *pa_ext_stream_restore_read(
pa_context *c,
pa_ext_stream_restore_read_cb_t cb,
void *userdata)
{
pa_operation *o;
struct stream_data *d;
pa_assert(c);
pa_assert(c->refcount >= 1);
PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
o = pa_operation_new(c, NULL, restore_read, sizeof(struct stream_data));
d = o->userdata;
d->context = c;
d->read_cb = cb;
d->userdata = userdata;
pa_operation_sync(o);
return o;
}
static void on_success(pa_operation *o, void *userdata)
{
struct stream_data *d = userdata;
if (d->success_cb)
d->success_cb(o->context, PA_OK, d->userdata);
pa_operation_done(o);
}
SPA_EXPORT
pa_operation *pa_ext_stream_restore_write(
pa_context *c,
pa_update_mode_t mode,
const pa_ext_stream_restore_info data[],
unsigned n,
int apply_immediately,
pa_context_success_cb_t cb,
void *userdata)
{
pa_operation *o;
struct stream_data *d;
pa_assert(c);
pa_assert(c->refcount >= 1);
PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
o = pa_operation_new(c, NULL, on_success, sizeof(struct stream_data));
d = o->userdata;
d->context = c;
d->success_cb = cb;
d->userdata = userdata;
pa_operation_sync(o);
return o;
}
/** Delete entries from the stream database. \since 0.9.12 */
SPA_EXPORT
pa_operation *pa_ext_stream_restore_delete(
pa_context *c,
const char *const s[],
pa_context_success_cb_t cb,
void *userdata)
{
pa_operation *o;
struct stream_data *d;
pa_assert(c);
pa_assert(c->refcount >= 1);
PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
o = pa_operation_new(c, NULL, on_success, sizeof(struct stream_data));
d = o->userdata;
d->context = c;
d->success_cb = cb;
d->userdata = userdata;
pa_operation_sync(o);
return o;
}
/** Subscribe to changes in the stream database. \since 0.9.12 */
SPA_EXPORT
pa_operation *pa_ext_stream_restore_subscribe(
pa_context *c,
int enable,
pa_context_success_cb_t cb,
void *userdata)
{
pa_operation *o;
struct stream_data *d;
pa_assert(c);
pa_assert(c->refcount >= 1);
PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
o = pa_operation_new(c, NULL, on_success, sizeof(struct stream_data));
d = o->userdata;
d->context = c;
d->success_cb = cb;
d->userdata = userdata;
pa_operation_sync(o);
return o;
}
/** Set the subscription callback that is called when
* pa_ext_stream_restore_subscribe() was called. \since 0.9.12 */
SPA_EXPORT
void pa_ext_stream_restore_set_subscribe_cb(
pa_context *c,
pa_ext_stream_restore_subscribe_cb_t cb,
void *userdata)
{
pw_log_warn("Not Implemented");
}

View file

@ -0,0 +1,707 @@
/***
This file is part of PulseAudio.
Copyright 2011 Intel Corporation
Copyright 2011 Collabora Multimedia
Copyright 2011 Arun Raghavan <arun.raghavan@collabora.co.uk>
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <pulse/xmalloc.h>
#include <pulse/format.h>
#include "core-format.h"
#include "json.h"
#include "internal.h"
#include "strbuf.h"
#define PA_JSON_MIN_KEY "min"
#define PA_JSON_MAX_KEY "max"
static int pa_format_info_prop_compatible(const char *one, const char *two);
static const char* const _encoding_str_table[]= {
[PA_ENCODING_PCM] = "pcm",
[PA_ENCODING_AC3_IEC61937] = "ac3-iec61937",
[PA_ENCODING_EAC3_IEC61937] = "eac3-iec61937",
[PA_ENCODING_MPEG_IEC61937] = "mpeg-iec61937",
[PA_ENCODING_DTS_IEC61937] = "dts-iec61937",
[PA_ENCODING_MPEG2_AAC_IEC61937] = "mpeg2-aac-iec61937",
[PA_ENCODING_ANY] = "any",
};
SPA_EXPORT
const char *pa_encoding_to_string(pa_encoding_t e) {
if (e < 0 || e >= PA_ENCODING_MAX)
return NULL;
return _encoding_str_table[e];
}
SPA_EXPORT
pa_encoding_t pa_encoding_from_string(const char *encoding) {
pa_encoding_t e;
for (e = PA_ENCODING_ANY; e < PA_ENCODING_MAX; e++)
if (pa_streq(_encoding_str_table[e], encoding))
return e;
return PA_ENCODING_INVALID;
}
SPA_EXPORT
pa_format_info* pa_format_info_new(void) {
pa_format_info *f = pa_xnew(pa_format_info, 1);
f->encoding = PA_ENCODING_INVALID;
f->plist = pa_proplist_new();
return f;
}
SPA_EXPORT
pa_format_info* pa_format_info_copy(const pa_format_info *src) {
pa_format_info *dest;
pa_assert(src);
dest = pa_xnew(pa_format_info, 1);
dest->encoding = src->encoding;
if (src->plist)
dest->plist = pa_proplist_copy(src->plist);
else
dest->plist = NULL;
return dest;
}
SPA_EXPORT
void pa_format_info_free(pa_format_info *f) {
pa_assert(f);
pa_proplist_free(f->plist);
pa_xfree(f);
}
SPA_EXPORT
int pa_format_info_valid(const pa_format_info *f) {
return (f->encoding >= 0 && f->encoding < PA_ENCODING_MAX && f->plist != NULL);
}
SPA_EXPORT
int pa_format_info_is_pcm(const pa_format_info *f) {
return f->encoding == PA_ENCODING_PCM;
}
SPA_EXPORT
char *pa_format_info_snprint(char *s, size_t l, const pa_format_info *f) {
char *tmp;
pa_assert(s);
pa_assert(l > 0);
pa_assert(f);
pa_init_i18n();
if (!pa_format_info_valid(f))
pa_snprintf(s, l, _("(invalid)"));
else {
tmp = pa_proplist_to_string_sep(f->plist, " ");
if (tmp[0])
pa_snprintf(s, l, "%s, %s", pa_encoding_to_string(f->encoding), tmp);
else
pa_snprintf(s, l, "%s", pa_encoding_to_string(f->encoding));
pa_xfree(tmp);
}
return s;
}
SPA_EXPORT
pa_format_info* pa_format_info_from_string(const char *str) {
pa_format_info *f = pa_format_info_new();
char *encoding = NULL, *properties = NULL;
size_t pos;
pos = strcspn(str, ",");
encoding = pa_xstrndup(str, pos);
f->encoding = pa_encoding_from_string(pa_strip(encoding));
if (f->encoding == PA_ENCODING_INVALID)
goto error;
if (pos != strlen(str)) {
pa_proplist *plist;
properties = pa_xstrdup(&str[pos+1]);
plist = pa_proplist_from_string(properties);
if (!plist)
goto error;
pa_proplist_free(f->plist);
f->plist = plist;
}
out:
if (encoding)
pa_xfree(encoding);
if (properties)
pa_xfree(properties);
return f;
error:
pa_format_info_free(f);
f = NULL;
goto out;
}
SPA_EXPORT
int pa_format_info_is_compatible(const pa_format_info *first, const pa_format_info *second) {
const char *key;
void *state = NULL;
pa_assert(first);
pa_assert(second);
if (first->encoding != second->encoding)
return false;
while ((key = pa_proplist_iterate(first->plist, &state))) {
const char *value_one, *value_two;
value_one = pa_proplist_gets(first->plist, key);
value_two = pa_proplist_gets(second->plist, key);
if (!value_two || !pa_format_info_prop_compatible(value_one, value_two))
return false;
}
return true;
}
SPA_EXPORT
pa_format_info* pa_format_info_from_sample_spec(const pa_sample_spec *ss, const pa_channel_map *map) {
char cm[PA_CHANNEL_MAP_SNPRINT_MAX];
pa_format_info *f;
pa_assert(ss && pa_sample_spec_valid(ss));
pa_assert(!map || pa_channel_map_valid(map));
f = pa_format_info_new();
f->encoding = PA_ENCODING_PCM;
pa_format_info_set_sample_format(f, ss->format);
pa_format_info_set_rate(f, ss->rate);
pa_format_info_set_channels(f, ss->channels);
if (map) {
pa_channel_map_snprint(cm, sizeof(cm), map);
pa_format_info_set_prop_string(f, PA_PROP_FORMAT_CHANNEL_MAP, cm);
}
return f;
}
/* For PCM streams */
SPA_EXPORT
int pa_format_info_to_sample_spec(const pa_format_info *f, pa_sample_spec *ss, pa_channel_map *map) {
pa_assert(f);
pa_assert(ss);
if (!pa_format_info_is_pcm(f))
return pa_format_info_to_sample_spec_fake(f, ss, map);
if (pa_format_info_get_sample_format(f, &ss->format) < 0)
return -PA_ERR_INVALID;
if (pa_format_info_get_rate(f, &ss->rate) < 0)
return -PA_ERR_INVALID;
if (pa_format_info_get_channels(f, &ss->channels) < 0)
return -PA_ERR_INVALID;
if (map && pa_format_info_get_channel_map(f, map) < 0)
return -PA_ERR_INVALID;
return 0;
}
SPA_EXPORT
pa_prop_type_t pa_format_info_get_prop_type(const pa_format_info *f, const char *key) {
const char *str;
pa_json_object *o;
const pa_json_object *o1;
pa_prop_type_t type;
pa_assert(f);
pa_assert(key);
str = pa_proplist_gets(f->plist, key);
if (!str)
return PA_PROP_TYPE_INVALID;
o = pa_json_parse(str);
if (!o)
return PA_PROP_TYPE_INVALID;
switch (pa_json_object_get_type(o)) {
case PA_JSON_TYPE_INT:
type = PA_PROP_TYPE_INT;
break;
case PA_JSON_TYPE_STRING:
type = PA_PROP_TYPE_STRING;
break;
case PA_JSON_TYPE_ARRAY:
if (pa_json_object_get_array_length(o) == 0) {
/* Unlikely, but let's account for this anyway. We need at
* least one element to figure out the array type. */
type = PA_PROP_TYPE_INVALID;
break;
}
o1 = pa_json_object_get_array_member(o, 0);
if (pa_json_object_get_type(o1) == PA_JSON_TYPE_INT)
type = PA_PROP_TYPE_INT_ARRAY;
else if (pa_json_object_get_type(o1) == PA_JSON_TYPE_STRING)
type = PA_PROP_TYPE_STRING_ARRAY;
else
type = PA_PROP_TYPE_INVALID;
break;
case PA_JSON_TYPE_OBJECT:
/* We actually know at this point that it's a int range, but let's
* confirm. */
if (!pa_json_object_get_object_member(o, PA_JSON_MIN_KEY)) {
type = PA_PROP_TYPE_INVALID;
break;
}
if (!pa_json_object_get_object_member(o, PA_JSON_MAX_KEY)) {
type = PA_PROP_TYPE_INVALID;
break;
}
type = PA_PROP_TYPE_INT_RANGE;
break;
default:
type = PA_PROP_TYPE_INVALID;
break;
}
pa_json_object_free(o);
return type;
}
SPA_EXPORT
int pa_format_info_get_prop_int(const pa_format_info *f, const char *key, int *v) {
const char *str;
pa_json_object *o;
pa_assert(f);
pa_assert(key);
pa_assert(v);
str = pa_proplist_gets(f->plist, key);
if (!str)
return -PA_ERR_NOENTITY;
o = pa_json_parse(str);
if (!o) {
pa_log_debug("Failed to parse format info property '%s'.", key);
return -PA_ERR_INVALID;
}
if (pa_json_object_get_type(o) != PA_JSON_TYPE_INT) {
pa_log_debug("Format info property '%s' type is not int.", key);
pa_json_object_free(o);
return -PA_ERR_INVALID;
}
*v = pa_json_object_get_int(o);
pa_json_object_free(o);
return 0;
}
SPA_EXPORT
int pa_format_info_get_prop_int_range(const pa_format_info *f, const char *key, int *min, int *max) {
const char *str;
pa_json_object *o;
const pa_json_object *o1;
int ret = -PA_ERR_INVALID;
pa_assert(f);
pa_assert(key);
pa_assert(min);
pa_assert(max);
str = pa_proplist_gets(f->plist, key);
if (!str)
return -PA_ERR_NOENTITY;
o = pa_json_parse(str);
if (!o) {
pa_log_debug("Failed to parse format info property '%s'.", key);
return -PA_ERR_INVALID;
}
if (pa_json_object_get_type(o) != PA_JSON_TYPE_OBJECT)
goto out;
if (!(o1 = pa_json_object_get_object_member(o, PA_JSON_MIN_KEY)) ||
(pa_json_object_get_type(o1) != PA_JSON_TYPE_INT))
goto out;
*min = pa_json_object_get_int(o1);
if (!(o1 = pa_json_object_get_object_member(o, PA_JSON_MAX_KEY)) ||
(pa_json_object_get_type(o1) != PA_JSON_TYPE_INT))
goto out;
*max = pa_json_object_get_int(o1);
ret = 0;
out:
if (ret < 0)
pa_log_debug("Format info property '%s' is not a valid int range.", key);
pa_json_object_free(o);
return ret;
}
SPA_EXPORT
int pa_format_info_get_prop_int_array(const pa_format_info *f, const char *key, int **values, int *n_values) {
const char *str;
pa_json_object *o;
const pa_json_object *o1;
int i, ret = -PA_ERR_INVALID;
pa_assert(f);
pa_assert(key);
pa_assert(values);
pa_assert(n_values);
str = pa_proplist_gets(f->plist, key);
if (!str)
return -PA_ERR_NOENTITY;
o = pa_json_parse(str);
if (!o) {
pa_log_debug("Failed to parse format info property '%s'.", key);
return -PA_ERR_INVALID;
}
if (pa_json_object_get_type(o) != PA_JSON_TYPE_ARRAY)
goto out;
*n_values = pa_json_object_get_array_length(o);
*values = pa_xnew(int, *n_values);
for (i = 0; i < *n_values; i++) {
o1 = pa_json_object_get_array_member(o, i);
if (pa_json_object_get_type(o1) != PA_JSON_TYPE_INT) {
goto out;
}
(*values)[i] = pa_json_object_get_int(o1);
}
ret = 0;
out:
if (ret < 0)
pa_log_debug("Format info property '%s' is not a valid int array.", key);
pa_json_object_free(o);
return ret;
}
SPA_EXPORT
int pa_format_info_get_prop_string(const pa_format_info *f, const char *key, char **v) {
const char *str = NULL;
pa_json_object *o;
pa_assert(f);
pa_assert(key);
pa_assert(v);
str = pa_proplist_gets(f->plist, key);
if (!str)
return -PA_ERR_NOENTITY;
o = pa_json_parse(str);
if (!o) {
pa_log_debug("Failed to parse format info property '%s'.", key);
return -PA_ERR_INVALID;
}
if (pa_json_object_get_type(o) != PA_JSON_TYPE_STRING) {
pa_log_debug("Format info property '%s' type is not string.", key);
pa_json_object_free(o);
return -PA_ERR_INVALID;
}
*v = pa_xstrdup(pa_json_object_get_string(o));
pa_json_object_free(o);
return 0;
}
SPA_EXPORT
int pa_format_info_get_prop_string_array(const pa_format_info *f, const char *key, char ***values, int *n_values) {
const char *str;
pa_json_object *o;
const pa_json_object *o1;
int i, ret = -PA_ERR_INVALID;
pa_assert(f);
pa_assert(key);
pa_assert(values);
pa_assert(n_values);
str = pa_proplist_gets(f->plist, key);
if (!str)
return -PA_ERR_NOENTITY;
o = pa_json_parse(str);
if (!o) {
pa_log_debug("Failed to parse format info property '%s'.", key);
return -PA_ERR_INVALID;
}
if (pa_json_object_get_type(o) != PA_JSON_TYPE_ARRAY)
goto out;
*n_values = pa_json_object_get_array_length(o);
*values = pa_xnew(char *, *n_values);
for (i = 0; i < *n_values; i++) {
o1 = pa_json_object_get_array_member(o, i);
if (pa_json_object_get_type(o1) != PA_JSON_TYPE_STRING) {
goto out;
}
(*values)[i] = pa_xstrdup(pa_json_object_get_string(o1));
}
ret = 0;
out:
if (ret < 0)
pa_log_debug("Format info property '%s' is not a valid string array.", key);
pa_json_object_free(o);
return ret;
}
SPA_EXPORT
void pa_format_info_free_string_array(char **values, int n_values) {
int i;
for (i = 0; i < n_values; i++)
pa_xfree(values[i]);
pa_xfree(values);
}
SPA_EXPORT
void pa_format_info_set_sample_format(pa_format_info *f, pa_sample_format_t sf) {
pa_format_info_set_prop_string(f, PA_PROP_FORMAT_SAMPLE_FORMAT, pa_sample_format_to_string(sf));
}
SPA_EXPORT
void pa_format_info_set_rate(pa_format_info *f, int rate) {
pa_format_info_set_prop_int(f, PA_PROP_FORMAT_RATE, rate);
}
SPA_EXPORT
void pa_format_info_set_channels(pa_format_info *f, int channels) {
pa_format_info_set_prop_int(f, PA_PROP_FORMAT_CHANNELS, channels);
}
SPA_EXPORT
void pa_format_info_set_channel_map(pa_format_info *f, const pa_channel_map *map) {
char map_str[PA_CHANNEL_MAP_SNPRINT_MAX];
pa_channel_map_snprint(map_str, sizeof(map_str), map);
pa_format_info_set_prop_string(f, PA_PROP_FORMAT_CHANNEL_MAP, map_str);
}
SPA_EXPORT
void pa_format_info_set_prop_int(pa_format_info *f, const char *key, int value) {
pa_assert(f);
pa_assert(key);
pa_proplist_setf(f->plist, key, "%d", value);
}
SPA_EXPORT
void pa_format_info_set_prop_int_array(pa_format_info *f, const char *key, const int *values, int n_values) {
pa_strbuf *buf;
char *str;
int i;
pa_assert(f);
pa_assert(key);
pa_assert(n_values > 0);
buf = pa_strbuf_new();
pa_strbuf_printf(buf, "[ %d", values[0]);
for (i = 1; i < n_values; i++)
pa_strbuf_printf(buf, ", %d", values[i]);
pa_strbuf_printf(buf, " ]");
str = pa_strbuf_to_string_free(buf);
pa_proplist_sets(f->plist, key, str);
pa_xfree (str);
}
SPA_EXPORT
void pa_format_info_set_prop_int_range(pa_format_info *f, const char *key, int min, int max) {
pa_assert(f);
pa_assert(key);
pa_proplist_setf(f->plist, key, "{ \"" PA_JSON_MIN_KEY "\": %d, \"" PA_JSON_MAX_KEY "\": %d }",
min, max);
}
SPA_EXPORT
void pa_format_info_set_prop_string(pa_format_info *f, const char *key, const char *value) {
pa_assert(f);
pa_assert(key);
pa_proplist_setf(f->plist, key, "\"%s\"", value);
}
SPA_EXPORT
void pa_format_info_set_prop_string_array(pa_format_info *f, const char *key, const char **values, int n_values) {
pa_strbuf *buf;
char *str;
int i;
pa_assert(f);
pa_assert(key);
buf = pa_strbuf_new();
pa_strbuf_printf(buf, "[ \"%s\"", values[0]);
for (i = 1; i < n_values; i++)
pa_strbuf_printf(buf, ", \"%s\"", values[i]);
pa_strbuf_printf(buf, " ]");
str = pa_strbuf_to_string_free(buf);
pa_proplist_sets(f->plist, key, str);
pa_xfree (str);
}
static bool pa_json_is_fixed_type(pa_json_object *o) {
switch(pa_json_object_get_type(o)) {
case PA_JSON_TYPE_OBJECT:
case PA_JSON_TYPE_ARRAY:
return false;
default:
return true;
}
}
static int pa_format_info_prop_compatible(const char *one, const char *two) {
pa_json_object *o1 = NULL, *o2 = NULL;
int i, ret = 0;
o1 = pa_json_parse(one);
if (!o1)
goto out;
o2 = pa_json_parse(two);
if (!o2)
goto out;
/* We don't deal with both values being non-fixed - just because there is no immediate need (FIXME) */
pa_return_val_if_fail(pa_json_is_fixed_type(o1) || pa_json_is_fixed_type(o2), false);
if (pa_json_is_fixed_type(o1) && pa_json_is_fixed_type(o2)) {
ret = pa_json_object_equal(o1, o2);
goto out;
}
if (pa_json_is_fixed_type(o1)) {
pa_json_object *tmp = o2;
o2 = o1;
o1 = tmp;
}
/* o2 is now a fixed type, and o1 is not */
if (pa_json_object_get_type(o1) == PA_JSON_TYPE_ARRAY) {
for (i = 0; i < pa_json_object_get_array_length(o1); i++) {
if (pa_json_object_equal(pa_json_object_get_array_member(o1, i), o2)) {
ret = 1;
break;
}
}
} else if (pa_json_object_get_type(o1) == PA_JSON_TYPE_OBJECT) {
/* o1 should be a range type */
int min, max, v;
const pa_json_object *o_min = NULL, *o_max = NULL;
if (pa_json_object_get_type(o2) != PA_JSON_TYPE_INT) {
/* We don't support non-integer ranges */
goto out;
}
if (!(o_min = pa_json_object_get_object_member(o1, PA_JSON_MIN_KEY)) ||
pa_json_object_get_type(o_min) != PA_JSON_TYPE_INT)
goto out;
if (!(o_max = pa_json_object_get_object_member(o1, PA_JSON_MAX_KEY)) ||
pa_json_object_get_type(o_max) != PA_JSON_TYPE_INT)
goto out;
v = pa_json_object_get_int(o2);
min = pa_json_object_get_int(o_min);
max = pa_json_object_get_int(o_max);
ret = v >= min && v <= max;
} else {
pa_log_warn("Got a format type that we don't support");
}
out:
if (o1)
pa_json_object_free(o1);
if (o2)
pa_json_object_free(o2);
return ret;
}

View file

@ -0,0 +1,437 @@
/* PipeWire
* Copyright (C) 2018 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __PIPEWIRE_PULSEAUDIO_INTERNAL_H__
#define __PIPEWIRE_PULSEAUDIO_INTERNAL_H__
#include <string.h>
#include <spa/utils/defs.h>
#include <spa/utils/hook.h>
#include <spa/utils/ringbuffer.h>
#include <spa/param/audio/format-utils.h>
#include <pulse/stream.h>
#include <pulse/format.h>
#include <pulse/subscribe.h>
#include <pulse/introspect.h>
#include <pulse/version.h>
#include <pipewire/array.h>
#include <pipewire/utils.h>
#include <pipewire/interfaces.h>
#include <pipewire/log.h>
/* Some PulseAudio API added const qualifiers in 13.0 */
#if PA_MAJOR >= 13
#define PA_CONST const
#else
#define PA_CONST
#endif
#define PA_MAX_FORMATS (PA_ENCODING_MAX)
#ifdef __cplusplus
extern "C" {
#endif
#define pa_streq(a,b) (!strcmp((a),(b)))
#define pa_strneq(a,b,n) (!strncmp((a),(b),(n)))
#define PA_UNLIKELY SPA_UNLIKELY
#define PA_LIKELY SPA_LIKELY
#define PA_MIN SPA_MIN
#define PA_MAX SPA_MAX
#define pa_assert spa_assert
#define pa_assert_se spa_assert
#define pa_return_val_if_fail spa_return_val_if_fail
#define pa_assert_not_reached spa_assert_not_reached
#define PA_INT_TYPE_SIGNED(type) (!!((type) 0 > (type) -1))
#define PA_INT_TYPE_HALF(type) ((type) 1 << (sizeof(type)*8 - 2))
#define PA_INT_TYPE_MAX(type) \
((type) (PA_INT_TYPE_SIGNED(type) \
? (PA_INT_TYPE_HALF(type) - 1 + PA_INT_TYPE_HALF(type)) \
: (type) -1))
#define PA_INT_TYPE_MIN(type) \
((type) (PA_INT_TYPE_SIGNED(type) \
? (-1 - PA_INT_TYPE_MAX(type)) \
: (type) 0))
#ifdef __GNUC__
#define PA_CLAMP_UNLIKELY(x, low, high) \
__extension__ ({ \
typeof(x) _x = (x); \
typeof(low) _low = (low); \
typeof(high) _high = (high); \
(PA_UNLIKELY(_x > _high) ? _high : (PA_UNLIKELY(_x < _low) ? _low : _x)); \
})
#else
#define PA_CLAMP_UNLIKELY(x, low, high) (PA_UNLIKELY((x) > (high)) ? (high) : (PA_UNLIKELY((x) < (low)) ? (low) : (x)))
#endif
#ifdef __GNUC__
#define PA_ROUND_DOWN(a, b) \
__extension__ ({ \
typeof(a) _a = (a); \
typeof(b) _b = (b); \
(_a / _b) * _b; \
})
#else
#define PA_ROUND_DOWN(a, b) (((a) / (b)) * (b))
#endif
#define pa_init_i18n()
#define _(String) (String)
#define N_(String) (String)
#define pa_snprintf snprintf
#define pa_strip(n) pw_strip(n,"\n\r \t")
#define pa_log pw_log_info
#define pa_log_debug pw_log_debug
#define pa_log_warn pw_log_warn
static inline void* PA_ALIGN_PTR(const void *p) {
return (void*) (((size_t) p) & ~(sizeof(void*) - 1));
}
/* Rounds up */
static inline size_t PA_ALIGN(size_t l) {
return ((l + sizeof(void*) - 1) & ~(sizeof(void*) - 1));
}
static inline const char *pa_strnull(const char *x) {
return x ? x : "(null)";
}
int pa_context_set_error(pa_context *c, int error);
#define PA_CHECK_VALIDITY(context, expression, error) \
do { \
if (!(expression)) { \
pw_log_trace("'%s' failed at %s:%u %s()", \
#expression, __FILE__, __LINE__, __func__); \
return -pa_context_set_error((context), (error)); \
} \
} while(false)
#define PA_CHECK_VALIDITY_RETURN_ANY(context, expression, error, value) \
do { \
if (!(expression)) { \
pw_log_trace("'%s' failed at %s:%u %s()", \
#expression, __FILE__, __LINE__, __func__); \
pa_context_set_error((context), (error)); \
return value; \
} \
} while(false)
#define PA_CHECK_VALIDITY_RETURN_NULL(context, expression, error) \
PA_CHECK_VALIDITY_RETURN_ANY(context, expression, error, NULL)
#define PA_FAIL(context, error) \
do { \
return -pa_context_set_error((context), (error)); \
} while(false)
#define PA_FAIL_RETURN_ANY(context, error, value) \
do { \
pa_context_set_error((context), (error)); \
return value; \
} while(false)
#define PA_FAIL_RETURN_NULL(context, error) \
PA_FAIL_RETURN_ANY(context, error, NULL)
struct pa_proplist {
struct pw_properties *props;
};
pa_proplist* pa_proplist_new_props(struct pw_properties *props);
pa_proplist* pa_proplist_new_dict(struct spa_dict *dict);
int pa_proplist_update_dict(pa_proplist *p, struct spa_dict *dict);
struct pa_io_event {
struct spa_source *source;
struct pa_mainloop *mainloop;
int fd;
pa_io_event_flags_t events;
pa_io_event_cb_t cb;
void *userdata;
pa_io_event_destroy_cb_t destroy;
};
struct pa_time_event {
struct spa_source *source;
struct pa_mainloop *mainloop;
pa_time_event_cb_t cb;
void *userdata;
pa_time_event_destroy_cb_t destroy;
};
struct pa_defer_event {
struct spa_source *source;
struct pa_mainloop *mainloop;
pa_defer_event_cb_t cb;
void *userdata;
pa_defer_event_destroy_cb_t destroy;
};
struct pa_mainloop {
struct pw_loop *loop;
struct spa_source *event;
pa_mainloop_api api;
bool quit;
int retval;
int timeout;
int n_events;
};
struct param {
struct spa_list link;
uint32_t id;
int seq;
void *param;
};
#define PA_IDX_FLAG_DSP 0x800000U
#define PA_IDX_MASK_DSP 0x7fffffU
struct global {
struct spa_list link;
uint32_t id;
uint32_t type;
struct pw_properties *props;
pa_context *context;
pa_subscription_mask_t mask;
pa_subscription_event_type_t event;
int priority_master;
int pending_seq;
int init:1;
int subscribed:1;
void *info;
pw_destroy_t destroy;
struct pw_proxy *proxy;
struct spa_hook proxy_listener;
struct spa_hook object_listener;
union {
/* for links */
struct {
struct global *src;
struct global *dst;
} link_info;
/* for sink/source */
struct {
uint32_t client_id;
uint32_t monitor;
float volume;
bool mute;
uint32_t n_channel_volumes;
float channel_volumes[SPA_AUDIO_MAX_CHANNELS];
} node_info;
struct {
uint32_t node_id;
} port_info;
/* for devices */
struct {
struct spa_list profiles;
uint32_t n_profiles;
uint32_t active_profile;
pa_card_info info;
} card_info;
struct {
pa_module_info info;
} module_info;
struct {
pa_client_info info;
} client_info;
};
};
struct pa_context {
int refcount;
uint32_t client_index;
struct pw_loop *loop;
struct pw_core *core;
struct pw_remote *remote;
struct spa_hook remote_listener;
struct pw_core_proxy *core_proxy;
struct spa_hook core_listener;
struct pw_core_info *core_info;
struct pw_registry_proxy *registry_proxy;
struct spa_hook registry_listener;
pa_proplist *proplist;
pa_mainloop_api *mainloop;
int error;
pa_context_state_t state;
pa_context_notify_cb_t state_callback;
void *state_userdata;
pa_context_event_cb_t event_callback;
void *event_userdata;
pa_context_subscribe_cb_t subscribe_callback;
void *subscribe_userdata;
pa_subscription_mask_t subscribe_mask;
struct spa_list globals;
struct spa_list streams;
struct spa_list operations;
int no_fail:1;
int disconnect:1;
};
struct global *pa_context_find_global(pa_context *c, uint32_t id);
struct global *pa_context_find_global_by_name(pa_context *c, uint32_t mask, const char *name);
struct global *pa_context_find_linked(pa_context *c, uint32_t id);
#define MAX_BUFFERS 64u
#define MASK_BUFFERS (MAX_BUFFERS-1)
struct pa_stream {
struct spa_list link;
int refcount;
struct pw_stream *stream;
struct spa_hook stream_listener;
pa_context *context;
pa_proplist *proplist;
pa_stream_direction_t direction;
pa_stream_state_t state;
pa_stream_flags_t flags;
bool disconnecting;
pa_sample_spec sample_spec;
pa_channel_map channel_map;
uint8_t n_formats;
pa_format_info *req_formats[PA_MAX_FORMATS];
pa_format_info *format;
uint32_t stream_index;
pa_buffer_attr buffer_attr;
uint32_t device_index;
char *device_name;
pa_timing_info timing_info;
uint32_t direct_on_input;
bool suspended:1;
bool corked:1;
bool timing_info_valid:1;
pa_stream_notify_cb_t state_callback;
void *state_userdata;
pa_stream_request_cb_t read_callback;
void *read_userdata;
pa_stream_request_cb_t write_callback;
void *write_userdata;
pa_stream_notify_cb_t overflow_callback;
void *overflow_userdata;
pa_stream_notify_cb_t underflow_callback;
void *underflow_userdata;
pa_stream_notify_cb_t latency_update_callback;
void *latency_update_userdata;
pa_stream_notify_cb_t moved_callback;
void *moved_userdata;
pa_stream_notify_cb_t suspended_callback;
void *suspended_userdata;
pa_stream_notify_cb_t started_callback;
void *started_userdata;
pa_stream_event_cb_t event_callback;
void *event_userdata;
pa_stream_notify_cb_t buffer_attr_callback;
void *buffer_attr_userdata;
int64_t offset;
struct pw_buffer *dequeued[MAX_BUFFERS];
struct spa_ringbuffer dequeued_ring;
size_t dequeued_size;
size_t maxsize;
struct spa_list pending;
struct pw_buffer *buffer;
uint32_t buffer_index;
void *buffer_data;
uint32_t buffer_size;
uint32_t buffer_offset;
uint32_t n_channel_volumes;
float channel_volumes[SPA_AUDIO_MAX_CHANNELS];
bool mute;
pa_operation *drain;
uint64_t queued;
};
void pa_stream_set_state(pa_stream *s, pa_stream_state_t st);
typedef void (*pa_operation_cb_t)(pa_operation *o, void *userdata);
struct pa_operation
{
struct spa_list link;
int refcount;
pa_context *context;
pa_stream *stream;
int seq;
pa_operation_state_t state;
pa_operation_cb_t callback;
void *userdata;
pa_operation_notify_cb_t state_callback;
void *state_userdata;
};
pa_operation *pa_operation_new(pa_context *c, pa_stream *s, pa_operation_cb_t cb, size_t userdata_size);
void pa_operation_done(pa_operation *o);
int pa_operation_sync(pa_operation *o);
#ifdef __cplusplus
}
#endif
#endif /* __PIPEWIRE_PULSEAUDIO_INTERNAL_H__ */

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,659 @@
/***
This file is part of PulseAudio.
Copyright 2016 Arun Raghavan <mail@arunraghavan.net>
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <math.h>
#include <pulse/xmalloc.h>
#include <pipewire/array.h>
#include <pipewire/properties.h>
#include "internal.h"
#include "json.h"
#include "strbuf.h"
#define MAX_NESTING_DEPTH 20 /* Arbitrary number to make sure we don't have a stack overflow */
typedef struct pa_json_item {
char *key;
pa_json_object *value;
} pa_json_item;
struct pa_json_object {
pa_json_type type;
union {
int int_value;
double double_value;
bool bool_value;
char *string_value;
struct pw_array values; /* objects */
};
};
static void clear_array(struct pw_array *array)
{
pa_json_object **value;
pw_array_for_each(value, array)
pa_json_object_free(*value);
pw_array_clear(array);
}
static void clear_item(pa_json_item *item)
{
free(item->key);
pa_json_object_free(item->value);
}
static void clear_object(struct pw_array *array)
{
pa_json_item *item;
pw_array_for_each(item, array)
clear_item(item);
pw_array_clear(array);
}
static const char* parse_value(const char *str, const char *end, pa_json_object **obj, unsigned int depth);
static pa_json_object* json_object_new(void) {
pa_json_object *obj;
obj = pa_xnew0(pa_json_object, 1);
return obj;
}
static bool is_whitespace(char c) {
return c == '\t' || c == '\n' || c == '\r' || c == ' ';
}
static bool is_digit(char c) {
return c >= '0' && c <= '9';
}
static bool is_end(const char c, const char *end) {
if (!end)
return c == '\0';
else {
while (*end) {
if (c == *end)
return true;
end++;
}
}
return false;
}
static const char* consume_string(const char *str, const char *expect) {
while (*expect) {
if (*str != *expect)
return NULL;
str++;
expect++;
}
return str;
}
static const char* parse_null(const char *str, pa_json_object *obj) {
str = consume_string(str, "null");
if (str)
obj->type = PA_JSON_TYPE_NULL;
return str;
}
static const char* parse_boolean(const char *str, pa_json_object *obj) {
const char *tmp;
tmp = consume_string(str, "true");
if (tmp) {
obj->type = PA_JSON_TYPE_BOOL;
obj->bool_value = true;
} else {
tmp = consume_string(str, "false");
if (str) {
obj->type = PA_JSON_TYPE_BOOL;
obj->bool_value = false;
}
}
return tmp;
}
static const char* parse_string(const char *str, pa_json_object *obj) {
pa_strbuf *buf = pa_strbuf_new();
str++; /* Consume leading '"' */
while (*str && *str != '"') {
if (*str != '\\') {
/* We only accept ASCII printable characters. */
if (*str < 0x20 || *str > 0x7E) {
pa_log("Invalid non-ASCII character: 0x%x", (unsigned int) *str);
goto error;
}
/* Normal character, juts consume */
pa_strbuf_putc(buf, *str);
} else {
/* Need to unescape */
str++;
switch (*str) {
case '"':
case '\\':
case '/':
pa_strbuf_putc(buf, *str);
break;
case 'b':
pa_strbuf_putc(buf, '\b' /* backspace */);
break;
case 'f':
pa_strbuf_putc(buf, '\f' /* form feed */);
break;
case 'n':
pa_strbuf_putc(buf, '\n' /* new line */);
break;
case 'r':
pa_strbuf_putc(buf, '\r' /* carriage return */);
break;
case 't':
pa_strbuf_putc(buf, '\t' /* horizontal tab */);
break;
case 'u':
pa_log("Unicode code points are currently unsupported");
goto error;
default:
pa_log("Unexepcted escape value: %c", *str);
goto error;
}
}
str++;
}
if (*str != '"') {
pa_log("Failed to parse remainder of string: %s", str);
goto error;
}
str++;
obj->type = PA_JSON_TYPE_STRING;
obj->string_value = pa_strbuf_to_string_free(buf);
return str;
error:
pa_strbuf_free(buf);
return NULL;
}
static const char* parse_number(const char *str, pa_json_object *obj) {
bool negative = false, has_fraction = false, has_exponent = false, valid = false;
unsigned int integer = 0;
unsigned int fraction = 0;
unsigned int fraction_digits = 0;
int exponent = 0;
if (*str == '-') {
negative = true;
str++;
}
if (*str == '0') {
valid = true;
str++;
goto fraction;
}
while (is_digit(*str)) {
valid = true;
if (integer > ((negative ? INT_MAX : UINT_MAX) / 10)) {
pa_log("Integer overflow while parsing number");
goto error;
}
integer = (integer * 10) + (*str - '0');
str++;
}
fraction:
if (!valid) {
pa_log("Missing digits while parsing number");
goto error;
}
if (*str == '.') {
has_fraction = true;
str++;
valid = false;
while (is_digit(*str)) {
valid = true;
if (fraction > (UINT_MAX / 10)) {
pa_log("Integer overflow while parsing fractional part of number");
goto error;
}
fraction = (fraction * 10) + (*str - '0');
fraction_digits++;
str++;
}
if (!valid) {
pa_log("No digit after '.' while parsing fraction");
goto error;
}
}
if (*str == 'e' || *str == 'E') {
bool exponent_negative = false;
has_exponent = true;
str++;
valid = false;
if (*str == '-') {
exponent_negative = true;
str++;
} else if (*str == '+')
str++;
while (is_digit(*str)) {
valid = true;
if (exponent > (INT_MAX / 10)) {
pa_log("Integer overflow while parsing exponent part of number");
goto error;
}
exponent = (exponent * 10) + (*str - '0');
str++;
}
if (!valid) {
pa_log("No digit in exponent while parsing fraction");
goto error;
}
if (exponent_negative)
exponent *= -1;
}
if (has_fraction || has_exponent) {
obj->type = PA_JSON_TYPE_DOUBLE;
obj->double_value =
(negative ? -1.0 : 1.0) * (integer + (double) fraction / pow(10, fraction_digits)) * pow(10, exponent);
} else {
obj->type = PA_JSON_TYPE_INT;
obj->int_value = (negative ? -1 : 1) * integer;
}
return str;
error:
return NULL;
}
static const char *parse_object(const char *str, pa_json_object *obj, unsigned int depth) {
pa_json_object *name = NULL, *value = NULL;
pa_json_item *item;
obj->values = PW_ARRAY_INIT(64);
while (*str != '}') {
str++; /* Consume leading '{' or ',' */
str = parse_value(str, ":", &name, depth + 1);
if (!str || pa_json_object_get_type(name) != PA_JSON_TYPE_STRING) {
pa_log("Could not parse key for object");
goto error;
}
/* Consume the ':' */
str++;
str = parse_value(str, ",}", &value, depth + 1);
if (!str) {
pa_log("Could not parse value for object");
goto error;
}
item = pw_array_add(&obj->values, sizeof(pa_json_item));
item->key = strdup(pa_json_object_get_string(name));
item->value = value;
pa_json_object_free(name);
name = NULL;
value = NULL;
}
/* Drop trailing '}' */
str++;
/* We now know the value was correctly parsed */
obj->type = PA_JSON_TYPE_OBJECT;
return str;
error:
clear_object(&obj->values);
if (name)
pa_json_object_free(name);
if (value)
pa_json_object_free(value);
return NULL;
}
static const char *parse_array(const char *str, pa_json_object *obj, unsigned int depth) {
pa_json_object *value;
obj->values = PW_ARRAY_INIT(64);
while (*str != ']') {
str++; /* Consume leading '[' or ',' */
/* Need to chew up whitespaces as a special case to deal with the
* possibility of an empty array */
while (is_whitespace(*str))
str++;
if (*str == ']')
break;
str = parse_value(str, ",]", &value, depth + 1);
if (!str) {
pa_log("Could not parse value for array");
goto error;
}
pw_array_add_ptr(&obj->values, value);
}
/* Drop trailing ']' */
str++;
/* We now know the value was correctly parsed */
obj->type = PA_JSON_TYPE_ARRAY;
return str;
error:
clear_array(&obj->values);
return NULL;
}
typedef enum {
JSON_PARSER_STATE_INIT,
JSON_PARSER_STATE_FINISH,
} json_parser_state;
static const char* parse_value(const char *str, const char *end, pa_json_object **obj, unsigned int depth) {
json_parser_state state = JSON_PARSER_STATE_INIT;
pa_json_object *o;
pa_assert(str != NULL);
o = json_object_new();
if (depth > MAX_NESTING_DEPTH) {
pa_log("Exceeded maximum permitted nesting depth of objects (%u)", MAX_NESTING_DEPTH);
goto error;
}
while (!is_end(*str, end)) {
switch (state) {
case JSON_PARSER_STATE_INIT:
if (is_whitespace(*str)) {
str++;
} else if (*str == 'n') {
str = parse_null(str, o);
state = JSON_PARSER_STATE_FINISH;
} else if (*str == 't' || *str == 'f') {
str = parse_boolean(str, o);
state = JSON_PARSER_STATE_FINISH;
} else if (*str == '"') {
str = parse_string(str, o);
state = JSON_PARSER_STATE_FINISH;
} else if (is_digit(*str) || *str == '-') {
str = parse_number(str, o);
state = JSON_PARSER_STATE_FINISH;
} else if (*str == '{') {
str = parse_object(str, o, depth);
state = JSON_PARSER_STATE_FINISH;
} else if (*str == '[') {
str = parse_array(str, o, depth);
state = JSON_PARSER_STATE_FINISH;
} else {
pa_log("Invalid JSON string: %s", str);
goto error;
}
if (!str)
goto error;
break;
case JSON_PARSER_STATE_FINISH:
/* Consume trailing whitespaces */
if (is_whitespace(*str)) {
str++;
} else {
goto error;
}
}
}
if (pa_json_object_get_type(o) == PA_JSON_TYPE_INIT) {
/* We didn't actually get any data */
pa_log("No data while parsing json string: '%s' till '%s'", str, pa_strnull(end));
goto error;
}
*obj = o;
return str;
error:
pa_json_object_free(o);
return NULL;
}
SPA_EXPORT
pa_json_object* pa_json_parse(const char *str) {
pa_json_object *obj;
str = parse_value(str, NULL, &obj, 0);
if (!str) {
pa_log("JSON parsing failed");
return NULL;
}
if (*str != '\0') {
pa_log("Unable to parse complete JSON string, remainder is: %s", str);
pa_json_object_free(obj);
return NULL;
}
return obj;
}
SPA_EXPORT
pa_json_type pa_json_object_get_type(const pa_json_object *obj) {
return obj->type;
}
SPA_EXPORT
void pa_json_object_free(pa_json_object *obj) {
switch (pa_json_object_get_type(obj)) {
case PA_JSON_TYPE_INIT:
case PA_JSON_TYPE_INT:
case PA_JSON_TYPE_DOUBLE:
case PA_JSON_TYPE_BOOL:
case PA_JSON_TYPE_NULL:
break;
case PA_JSON_TYPE_STRING:
pa_xfree(obj->string_value);
break;
case PA_JSON_TYPE_OBJECT:
clear_object(&obj->values);
break;
case PA_JSON_TYPE_ARRAY:
clear_array(&obj->values);
break;
default:
pa_assert_not_reached();
}
pa_xfree(obj);
}
SPA_EXPORT
int pa_json_object_get_int(const pa_json_object *o) {
pa_assert(pa_json_object_get_type(o) == PA_JSON_TYPE_INT);
return o->int_value;
}
SPA_EXPORT
double pa_json_object_get_double(const pa_json_object *o) {
pa_assert(pa_json_object_get_type(o) == PA_JSON_TYPE_DOUBLE);
return o->double_value;
}
SPA_EXPORT
bool pa_json_object_get_bool(const pa_json_object *o) {
pa_assert(pa_json_object_get_type(o) == PA_JSON_TYPE_BOOL);
return o->bool_value;
}
SPA_EXPORT
const char* pa_json_object_get_string(const pa_json_object *o) {
pa_assert(pa_json_object_get_type(o) == PA_JSON_TYPE_STRING);
return o->string_value;
}
SPA_EXPORT
const pa_json_object* pa_json_object_get_object_member(const pa_json_object *o, const char *name) {
pa_json_item *item;
pa_assert(pa_json_object_get_type(o) == PA_JSON_TYPE_OBJECT);
pw_array_for_each(item, &o->values) {
if (pa_streq(item->key, name))
return item->value;
}
return NULL;
}
SPA_EXPORT
int pa_json_object_get_array_length(const pa_json_object *o) {
pa_assert(pa_json_object_get_type(o) == PA_JSON_TYPE_ARRAY);
return pw_array_get_len(&o->values, const pa_json_object*);
}
SPA_EXPORT
const pa_json_object* pa_json_object_get_array_member(const pa_json_object *o, int index) {
pa_assert(pa_json_object_get_type(o) == PA_JSON_TYPE_ARRAY);
return pw_array_get_unchecked_s(&o->values, index, sizeof(pa_json_object*),
const pa_json_object);
}
SPA_EXPORT
bool pa_json_object_equal(const pa_json_object *o1, const pa_json_object *o2) {
int i;
if (pa_json_object_get_type(o1) != pa_json_object_get_type(o2))
return false;
switch (pa_json_object_get_type(o1)) {
case PA_JSON_TYPE_NULL:
return true;
case PA_JSON_TYPE_BOOL:
return o1->bool_value == o2->bool_value;
case PA_JSON_TYPE_INT:
return o1->int_value == o2->int_value;
case PA_JSON_TYPE_DOUBLE:
return PA_DOUBLE_IS_EQUAL(o1->double_value, o2->double_value);
case PA_JSON_TYPE_STRING:
return pa_streq(o1->string_value, o2->string_value);
case PA_JSON_TYPE_ARRAY:
if (pa_json_object_get_array_length(o1) != pa_json_object_get_array_length(o2))
return false;
for (i = 0; i < pa_json_object_get_array_length(o1); i++) {
if (!pa_json_object_equal(pa_json_object_get_array_member(o1, i),
pa_json_object_get_array_member(o2, i)))
return false;
}
return true;
case PA_JSON_TYPE_OBJECT: {
const pa_json_object *value;
const pa_json_item *item;
if (o1->values.size != o2->values.size)
return false;
pw_array_for_each(item, &o1->values) {
value = pa_json_object_get_object_member(o2, item->key);
if (!value || !pa_json_object_equal(item->value, value))
return false;
}
return true;
}
default:
pa_assert_not_reached();
}
}

View file

@ -0,0 +1,53 @@
/***
This file is part of PulseAudio.
Copyright 2016 Arun Raghavan <mail@arunraghavan.net>
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
***/
#include <stdbool.h>
#define PA_DOUBLE_IS_EQUAL(x, y) (((x) - (y)) < 0.000001 && ((x) - (y)) > -0.000001)
typedef enum {
PA_JSON_TYPE_INIT = 0,
PA_JSON_TYPE_NULL,
PA_JSON_TYPE_INT,
PA_JSON_TYPE_DOUBLE,
PA_JSON_TYPE_BOOL,
PA_JSON_TYPE_STRING,
PA_JSON_TYPE_ARRAY,
PA_JSON_TYPE_OBJECT,
} pa_json_type;
typedef struct pa_json_object pa_json_object;
pa_json_object* pa_json_parse(const char *str);
pa_json_type pa_json_object_get_type(const pa_json_object *obj);
void pa_json_object_free(pa_json_object *obj);
/* All pointer members that are returned are valid while the corresponding object is valid */
int pa_json_object_get_int(const pa_json_object *o);
double pa_json_object_get_double(const pa_json_object *o);
bool pa_json_object_get_bool(const pa_json_object *o);
const char* pa_json_object_get_string(const pa_json_object *o);
const pa_json_object* pa_json_object_get_object_member(const pa_json_object *o, const char *name);
int pa_json_object_get_array_length(const pa_json_object *o);
const pa_json_object* pa_json_object_get_array_member(const pa_json_object *o, int index);
bool pa_json_object_equal(const pa_json_object *o1, const pa_json_object *o2);

View file

@ -0,0 +1,120 @@
/* PipeWire
* Copyright (C) 2018 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <errno.h>
#include <spa/utils/result.h>
#include <pipewire/log.h>
#include <pipewire/loop.h>
#include <pulse/glib-mainloop.h>
#include <pulse/mainloop.h>
#include "internal.h"
struct source {
GSource base;
struct pw_loop *loop;
};
struct pa_glib_mainloop {
GMainContext *context;
pa_mainloop *loop;
struct source *source;
guint id;
};
static gboolean source_prepare (GSource *base, int *timeout)
{
*timeout = -1;
return FALSE;
}
static gboolean source_dispatch (GSource *source, GSourceFunc callback, gpointer user_data)
{
struct source *s = (struct source *) source;
int result;
pw_loop_enter (s->loop);
result = pw_loop_iterate (s->loop, 0);
pw_loop_leave (s->loop);
if (result < 0)
g_warning ("pipewire_loop_iterate failed: %s", spa_strerror (result));
return TRUE;
}
static GSourceFuncs source_funcs =
{
source_prepare,
NULL,
source_dispatch,
NULL,
NULL,
NULL,
};
SPA_EXPORT
pa_glib_mainloop *pa_glib_mainloop_new(GMainContext *c)
{
pa_glib_mainloop *loop;
loop = calloc(1, sizeof(pa_glib_mainloop));
if (loop == NULL)
goto error;
loop->context = c;
loop->loop = pa_mainloop_new();
if (loop->loop == NULL)
goto error_free;
loop->source = (struct source *) g_source_new(&source_funcs, sizeof(struct source));
loop->source->loop = loop->loop->loop;
g_source_add_unix_fd (&loop->source->base,
pw_loop_get_fd(loop->source->loop),
G_IO_IN | G_IO_ERR);
loop->id = g_source_attach (&loop->source->base, loop->context);
return loop;
error_free:
free(loop);
error:
return NULL;
}
SPA_EXPORT
void pa_glib_mainloop_free(pa_glib_mainloop* g)
{
g_source_destroy(&g->source->base);
pa_mainloop_free(g->loop);
free(g);
}
SPA_EXPORT
pa_mainloop_api* pa_glib_mainloop_get_api(pa_glib_mainloop *g)
{
return pa_mainloop_get_api(g->loop);
}

View file

@ -0,0 +1,114 @@
/* PipeWire
* Copyright (C) 2018 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <errno.h>
#include <spa/utils/list.h>
#include <spa/support/loop.h>
#include <pipewire/log.h>
#include <pipewire/loop.h>
#include <pulse/mainloop-signal.h>
#include "internal.h"
static pa_mainloop_api *api = NULL;
static struct spa_list signals;
static struct pw_loop *loop = NULL;
struct pa_signal_event {
struct spa_list link;
struct spa_source *source;
pa_signal_cb_t callback;
pa_signal_destroy_cb_t destroy;
void *userdata;
};
SPA_EXPORT
int pa_signal_init(pa_mainloop_api *a)
{
pa_assert(a);
pa_assert(!api);
api = a;
spa_list_init(&signals);
loop = a->userdata;
return 0;
}
SPA_EXPORT
void pa_signal_done(void)
{
pa_signal_event *ev, *t;
pa_assert(api);
spa_list_for_each_safe(ev, t, &signals, link)
pa_signal_free(ev);
spa_list_init(&signals);
api = NULL;
}
static void source_signal_func (void *data, int signal_number)
{
pa_signal_event *ev = data;
if (ev->callback)
ev->callback(api, ev, signal_number, ev->userdata);
}
SPA_EXPORT
pa_signal_event* pa_signal_new(int sig, pa_signal_cb_t callback, void *userdata)
{
pa_signal_event *ev;
pa_assert(sig > 0);
pa_assert(callback);
ev = calloc(1, sizeof(pa_signal_event));
ev->source = spa_loop_utils_add_signal(loop->utils, sig, source_signal_func, ev);
ev->callback = callback;
ev->userdata = userdata;
spa_list_append(&signals, &ev->link);
return ev;
}
SPA_EXPORT
void pa_signal_free(pa_signal_event *e)
{
pa_assert(e);
spa_list_remove(&e->link);
spa_loop_utils_destroy_source(loop->utils, e->source);
if (e->destroy)
e->destroy(api, e, e->userdata);
free(e);
}
SPA_EXPORT
void pa_signal_set_destroy(pa_signal_event *e, pa_signal_destroy_cb_t callback)
{
pa_assert(e);
e->destroy = callback;
}

View file

@ -0,0 +1,433 @@
/* PipeWire
* Copyright (C) 2018 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <errno.h>
#include <pipewire/log.h>
#include <pipewire/loop.h>
#include <pulse/mainloop.h>
#include <pulse/xmalloc.h>
#include "internal.h"
static void do_stop(void *data, uint64_t count)
{
struct pa_mainloop *this = data;
this->quit = true;
}
static uint32_t map_flags_to_spa(pa_io_event_flags_t flags) {
return (uint32_t)
((flags & PA_IO_EVENT_INPUT ? SPA_IO_IN : 0) |
(flags & PA_IO_EVENT_OUTPUT ? SPA_IO_OUT : 0) |
(flags & PA_IO_EVENT_ERROR ? SPA_IO_ERR : 0) |
(flags & PA_IO_EVENT_HANGUP ? SPA_IO_HUP : 0));
}
static pa_io_event_flags_t map_flags_from_spa(uint32_t flags) {
return (flags & SPA_IO_IN ? PA_IO_EVENT_INPUT : 0) |
(flags & SPA_IO_OUT ? PA_IO_EVENT_OUTPUT : 0) |
(flags & SPA_IO_ERR ? PA_IO_EVENT_ERROR : 0) |
(flags & SPA_IO_HUP ? PA_IO_EVENT_HANGUP : 0);
}
static void source_io_func(void *data, int fd, uint32_t mask)
{
pa_io_event *ev = data;
if (ev->cb)
ev->cb(&ev->mainloop->api, ev, ev->fd, map_flags_from_spa(mask), ev->userdata);
}
static pa_io_event* api_io_new(pa_mainloop_api*a, int fd, pa_io_event_flags_t events, pa_io_event_cb_t cb, void *userdata)
{
pa_mainloop *mainloop = SPA_CONTAINER_OF(a, pa_mainloop, api);
pa_io_event *ev;
pa_assert(a);
pa_assert(fd >= 0);
pa_assert(cb);
ev = calloc(1, sizeof(pa_io_event));
ev->source = pw_loop_add_io(mainloop->loop, fd,
map_flags_to_spa(events), false, source_io_func, ev);
ev->fd = fd;
ev->events = events;
ev->mainloop = mainloop;
ev->cb = cb;
ev->userdata = userdata;
return ev;
}
static void api_io_enable(pa_io_event* e, pa_io_event_flags_t events)
{
pa_assert(e);
if (e->events == events)
return;
e->events = events;
pw_loop_update_io(e->mainloop->loop, e->source, map_flags_to_spa(events));
}
static void api_io_free(pa_io_event* e)
{
pa_assert(e);
pw_loop_destroy_source(e->mainloop->loop, e->source);
if (e->destroy)
e->destroy(&e->mainloop->api, e, e->userdata);
free(e);
}
static void api_io_set_destroy(pa_io_event *e, pa_io_event_destroy_cb_t cb)
{
pa_assert(e);
e->destroy = cb;
}
static void source_timer_func(void *data, uint64_t expirations)
{
pa_time_event *ev = data;
struct timeval tv;
if (ev->cb)
ev->cb(&ev->mainloop->api, ev, &tv, ev->userdata);
}
static pa_time_event* api_time_new(pa_mainloop_api*a, const struct timeval *tv, pa_time_event_cb_t cb, void *userdata)
{
pa_mainloop *mainloop = SPA_CONTAINER_OF(a, pa_mainloop, api);
pa_time_event *ev;
struct timespec ts;
ev = calloc(1, sizeof(pa_time_event));
ev->source = pw_loop_add_timer(mainloop->loop, source_timer_func, ev);
ev->mainloop = mainloop;
ev->cb = cb;
ev->userdata = userdata;
if (tv == NULL) {
ts.tv_sec = 0;
ts.tv_nsec = 1;
}
else {
ts.tv_sec = tv->tv_sec;
ts.tv_nsec = tv->tv_usec * 1000LL;
}
pw_log_debug("new timer %p %ld %ld", ev, ts.tv_sec, ts.tv_nsec);
pw_loop_update_timer(mainloop->loop, ev->source, &ts, NULL, true);
return ev;
}
static void api_time_restart(pa_time_event* e, const struct timeval *tv)
{
struct timespec ts;
pa_assert(e);
if (tv == NULL) {
ts.tv_sec = 0;
ts.tv_nsec = 1;
}
else {
ts.tv_sec = tv->tv_sec;
ts.tv_nsec = tv->tv_usec * 1000LL;
}
pw_loop_update_timer(e->mainloop->loop, e->source, &ts, NULL, true);
}
static void api_time_free(pa_time_event* e)
{
pa_assert(e);
pw_loop_destroy_source(e->mainloop->loop, e->source);
if (e->destroy)
e->destroy(&e->mainloop->api, e, e->userdata);
free(e);
}
static void api_time_set_destroy(pa_time_event *e, pa_time_event_destroy_cb_t cb)
{
pa_assert(e);
e->destroy = cb;
}
static void source_idle_func(void *data)
{
pa_defer_event *ev = data;
if (ev->cb)
ev->cb(&ev->mainloop->api, ev, ev->userdata);
}
static pa_defer_event* api_defer_new(pa_mainloop_api*a, pa_defer_event_cb_t cb, void *userdata)
{
pa_mainloop *mainloop = SPA_CONTAINER_OF(a, pa_mainloop, api);
pa_defer_event *ev;
pa_assert(a);
pa_assert(cb);
ev = calloc(1, sizeof(pa_defer_event));
ev->source = pw_loop_add_idle(mainloop->loop, true, source_idle_func, ev);
ev->mainloop = mainloop;
ev->cb = cb;
ev->userdata = userdata;
return ev;
}
static void api_defer_enable(pa_defer_event* e, int b)
{
pa_assert(e);
pw_loop_enable_idle(e->mainloop->loop, e->source, b ? true : false);
}
static void api_defer_free(pa_defer_event* e)
{
pa_assert(e);
pw_loop_destroy_source(e->mainloop->loop, e->source);
if (e->destroy)
e->destroy(&e->mainloop->api, e, e->userdata);
free(e);
}
static void api_defer_set_destroy(pa_defer_event *e, pa_defer_event_destroy_cb_t cb)
{
pa_assert(e);
e->destroy = cb;
}
static void api_quit(pa_mainloop_api*a, int retval)
{
pa_mainloop *m = SPA_CONTAINER_OF(a, pa_mainloop, api);
m->quit = true;
m->retval = retval;
pa_mainloop_wakeup(m);
}
static const pa_mainloop_api api =
{
.io_new = api_io_new,
.io_enable = api_io_enable,
.io_free = api_io_free,
.io_set_destroy = api_io_set_destroy,
.time_new = api_time_new,
.time_restart = api_time_restart,
.time_free = api_time_free,
.time_set_destroy = api_time_set_destroy,
.defer_new = api_defer_new,
.defer_enable = api_defer_enable,
.defer_free = api_defer_free,
.defer_set_destroy = api_defer_set_destroy,
.quit = api_quit,
};
SPA_EXPORT
pa_mainloop *pa_mainloop_new(void)
{
pa_mainloop *loop;
loop = calloc(1, sizeof(pa_mainloop));
if (loop == NULL)
return NULL;
loop->loop = pw_loop_new(NULL);
if (loop->loop == NULL)
goto no_loop;
loop->event = pw_loop_add_event(loop->loop, do_stop, loop);
loop->api = api;
loop->api.userdata = loop->loop;
return loop;
no_loop:
free(loop);
return NULL;
}
SPA_EXPORT
void pa_mainloop_free(pa_mainloop* m)
{
pw_loop_destroy(m->loop);
free(m);
}
SPA_EXPORT
int pa_mainloop_prepare(pa_mainloop *m, int timeout)
{
if (m->quit)
return -2;
m->timeout = timeout;
m->n_events = -EIO;
return 0;
}
/** Execute the previously prepared poll. Returns a negative value on error.*/
SPA_EXPORT
int pa_mainloop_poll(pa_mainloop *m)
{
int res;
if (m->quit)
return -2;
res = pw_loop_iterate(m->loop, m->timeout);
if (res < 0) {
if (res == -EINTR)
res = 0;
}
m->n_events = res;
return res;
}
SPA_EXPORT
int pa_mainloop_dispatch(pa_mainloop *m)
{
if (m->quit)
return -2;
return m->n_events;
}
SPA_EXPORT
int pa_mainloop_get_retval(PA_CONST pa_mainloop *m)
{
return m->retval;
}
/** Run a single iteration of the main loop. This is a convenience function
for pa_mainloop_prepare(), pa_mainloop_poll() and pa_mainloop_dispatch().
Returns a negative value on error or exit request. If block is nonzero,
block for events if none are queued. Optionally return the return value as
specified with the main loop's quit() routine in the integer variable retval points
to. On success returns the number of sources dispatched in this iteration. */
SPA_EXPORT
int pa_mainloop_iterate(pa_mainloop *m, int block, int *retval)
{
int r;
pa_assert(m);
if ((r = pa_mainloop_prepare(m, block ? -1 : 0)) < 0)
goto quit;
if ((r = pa_mainloop_poll(m)) < 0)
goto quit;
if ((r = pa_mainloop_dispatch(m)) < 0)
goto quit;
return r;
quit:
if ((r == -2) && retval)
*retval = pa_mainloop_get_retval(m);
return r;
}
SPA_EXPORT
int pa_mainloop_run(pa_mainloop *m, int *retval)
{
int r;
while ((r = pa_mainloop_iterate(m, 1, retval)) >= 0)
;
if (r == -2)
return 1;
else
return -1;
}
SPA_EXPORT
pa_mainloop_api* pa_mainloop_get_api(pa_mainloop *m)
{
pa_assert(m);
return &m->api;
}
SPA_EXPORT
void pa_mainloop_quit(pa_mainloop *m, int retval)
{
pa_assert(m);
m->api.quit(&m->api, retval);
}
SPA_EXPORT
void pa_mainloop_wakeup(pa_mainloop *m)
{
pa_assert(m);
pw_loop_signal_event(m->loop, m->event);
}
SPA_EXPORT
void pa_mainloop_set_poll_func(pa_mainloop *m, pa_poll_func poll_func, void *userdata)
{
pw_log_warn("Not Implemented");
}
struct once_info {
void (*callback)(pa_mainloop_api*m, void *userdata);
void *userdata;
};
static void once_callback(pa_mainloop_api *m, pa_defer_event *e, void *userdata) {
struct once_info *i = userdata;
pa_assert(m);
pa_assert(i);
pa_assert(i->callback);
i->callback(m, i->userdata);
pa_assert(m->defer_free);
m->defer_free(e);
}
static void free_callback(pa_mainloop_api *m, pa_defer_event *e, void *userdata) {
struct once_info *i = userdata;
pa_assert(m);
pa_assert(i);
pa_xfree(i);
}
void pa_mainloop_api_once(pa_mainloop_api* m, void (*callback)(pa_mainloop_api *m, void *userdata), void *userdata) {
struct once_info *i;
pa_defer_event *e;
pa_assert(m);
pa_assert(callback);
pa_init_i18n();
i = pa_xnew(struct once_info, 1);
i->callback = callback;
i->userdata = userdata;
pa_assert(m->defer_new);
pa_assert_se(e = m->defer_new(m, once_callback, i));
m->defer_set_destroy(e, free_callback);
}

View file

@ -0,0 +1,386 @@
PULSE_0 {
global:
pa_ascii_filter;
pa_ascii_valid;
pa_bytes_per_second;
pa_bytes_snprint;
pa_bytes_to_usec;
pa_channel_map_can_balance;
pa_channel_map_can_fade;
pa_channel_map_can_lfe_balance;
pa_channel_map_compatible;
pa_channel_map_equal;
pa_channel_map_has_position;
pa_channel_map_init;
pa_channel_map_init_auto;
pa_channel_map_init_extend;
pa_channel_map_init_mono;
pa_channel_map_init_stereo;
pa_channel_map_mask;
pa_channel_map_parse;
pa_channel_map_snprint;
pa_channel_map_superset;
pa_channel_map_to_name;
pa_channel_map_to_pretty_name;
pa_channel_map_valid;
pa_channel_position_from_string;
pa_channel_position_to_pretty_string;
pa_channel_position_to_string;
pa_channels_valid;
pa_context_add_autoload;
pa_context_connect;
pa_context_disconnect;
pa_context_drain;
pa_context_errno;
pa_context_exit_daemon;
pa_context_get_autoload_info_by_index;
pa_context_get_autoload_info_by_name;
pa_context_get_autoload_info_list;
pa_context_get_card_info_by_index;
pa_context_get_card_info_by_name;
pa_context_get_card_info_list;
pa_context_get_client_info;
pa_context_get_client_info_list;
pa_context_get_index;
pa_context_get_module_info;
pa_context_get_module_info_list;
pa_context_get_protocol_version;
pa_context_get_sample_info_by_index;
pa_context_get_sample_info_by_name;
pa_context_get_sample_info_list;
pa_context_get_server;
pa_context_get_server_info;
pa_context_get_server_protocol_version;
pa_context_get_sink_info_by_index;
pa_context_get_sink_info_by_name;
pa_context_get_sink_info_list;
pa_context_get_sink_input_info;
pa_context_get_sink_input_info_list;
pa_context_get_source_info_by_index;
pa_context_get_source_info_by_name;
pa_context_get_source_info_list;
pa_context_get_source_output_info;
pa_context_get_source_output_info_list;
pa_context_set_port_latency_offset;
pa_context_get_state;
pa_context_get_tile_size;
pa_context_is_local;
pa_context_is_pending;
pa_context_kill_client;
pa_context_kill_sink_input;
pa_context_kill_source_output;
pa_context_load_cookie_from_file;
pa_context_load_module;
pa_context_move_sink_input_by_index;
pa_context_move_sink_input_by_name;
pa_context_move_source_output_by_index;
pa_context_move_source_output_by_name;
pa_context_new;
pa_context_new_with_proplist;
pa_context_play_sample;
pa_context_play_sample_with_proplist;
pa_context_proplist_remove;
pa_context_proplist_update;
pa_context_ref;
pa_context_remove_autoload_by_index;
pa_context_remove_autoload_by_name;
pa_context_remove_sample;
pa_context_rttime_new;
pa_context_rttime_restart;
pa_context_set_card_profile_by_index;
pa_context_set_card_profile_by_name;
pa_context_set_default_sink;
pa_context_set_default_source;
pa_context_set_event_callback;
pa_context_set_name;
pa_context_set_sink_input_mute;
pa_context_set_sink_input_volume;
pa_context_set_sink_mute_by_index;
pa_context_set_sink_mute_by_name;
pa_context_set_sink_port_by_index;
pa_context_set_sink_port_by_name;
pa_context_set_sink_volume_by_index;
pa_context_set_sink_volume_by_name;
pa_context_set_source_output_mute;
pa_context_set_source_output_volume;
pa_context_set_source_mute_by_index;
pa_context_set_source_mute_by_name;
pa_context_set_source_port_by_index;
pa_context_set_source_port_by_name;
pa_context_set_source_volume_by_index;
pa_context_set_source_volume_by_name;
pa_context_set_state_callback;
pa_context_set_subscribe_callback;
pa_context_stat;
pa_context_subscribe;
pa_context_suspend_sink_by_index;
pa_context_suspend_sink_by_name;
pa_context_suspend_source_by_index;
pa_context_suspend_source_by_name;
pa_context_unload_module;
pa_context_unref;
pa_cvolume_avg;
pa_cvolume_avg_mask;
pa_cvolume_channels_equal_to;
pa_cvolume_compatible;
pa_cvolume_compatible_with_channel_map;
pa_cvolume_dec;
pa_cvolume_equal;
pa_cvolume_get_balance;
pa_cvolume_get_fade;
pa_cvolume_get_lfe_balance;
pa_cvolume_get_position;
pa_cvolume_inc;
pa_cvolume_inc_clamp;
pa_cvolume_init;
pa_cvolume_max;
pa_cvolume_max_mask;
pa_cvolume_merge;
pa_cvolume_min;
pa_cvolume_min_mask;
pa_cvolume_remap;
pa_cvolume_scale;
pa_cvolume_scale_mask;
pa_cvolume_set;
pa_cvolume_set_balance;
pa_cvolume_set_fade;
pa_cvolume_set_lfe_balance;
pa_cvolume_set_position;
pa_cvolume_snprint;
pa_cvolume_snprint_verbose;
pa_cvolume_valid;
pa_direction_to_string;
pa_direction_valid;
pa_encoding_from_string;
pa_encoding_to_string;
pa_ext_device_manager_delete;
pa_ext_device_manager_enable_role_device_priority_routing;
pa_ext_device_manager_read;
pa_ext_device_manager_reorder_devices_for_role;
pa_ext_device_manager_set_device_description;
pa_ext_device_manager_set_subscribe_cb;
pa_ext_device_manager_subscribe;
pa_ext_device_manager_test;
pa_ext_device_restore_read_formats;
pa_ext_device_restore_read_formats_all;
pa_ext_device_restore_save_formats;
pa_ext_device_restore_set_subscribe_cb;
pa_ext_device_restore_subscribe;
pa_ext_device_restore_test;
pa_ext_stream_restore_delete;
pa_ext_stream_restore_read;
pa_ext_stream_restore_set_subscribe_cb;
pa_ext_stream_restore_subscribe;
pa_ext_stream_restore_test;
pa_ext_stream_restore_write;
pa_format_info_copy;
pa_format_info_free;
pa_format_info_from_string;
pa_format_info_from_sample_spec;
pa_format_info_get_prop_type;
pa_format_info_get_prop_int;
pa_format_info_get_prop_int_range;
pa_format_info_get_prop_int_array;
pa_format_info_get_prop_string;
pa_format_info_get_prop_string_array;
pa_format_info_free_string_array;
pa_format_info_is_compatible;
pa_format_info_is_pcm;
pa_format_info_new;
pa_format_info_set_channel_map;
pa_format_info_set_channels;
pa_format_info_set_prop_int;
pa_format_info_set_prop_int_array;
pa_format_info_set_prop_int_range;
pa_format_info_set_prop_string;
pa_format_info_set_prop_string_array;
pa_format_info_set_rate;
pa_format_info_set_sample_format;
pa_format_info_snprint;
pa_format_info_to_sample_spec;
pa_format_info_valid;
pa_frame_size;
pa_get_binary_name;
pa_get_fqdn;
pa_get_home_dir;
pa_get_host_name;
pa_get_library_version;
pa_gettimeofday;
pa_get_user_name;
pa_glib_mainloop_free;
pa_glib_mainloop_get_api;
pa_glib_mainloop_new;
pa_locale_to_utf8;
pa_mainloop_api_once;
pa_mainloop_dispatch;
pa_mainloop_free;
pa_mainloop_get_api;
pa_mainloop_get_retval;
pa_mainloop_iterate;
pa_mainloop_new;
pa_mainloop_poll;
pa_mainloop_prepare;
pa_mainloop_quit;
pa_mainloop_run;
pa_mainloop_set_poll_func;
pa_mainloop_wakeup;
pa_msleep;
pa_operation_cancel;
pa_operation_get_state;
pa_operation_ref;
pa_operation_set_state_callback;
pa_operation_unref;
pa_parse_sample_format;
pa_path_get_filename;
pa_proplist_clear;
pa_proplist_contains;
pa_proplist_copy;
pa_proplist_equal;
pa_proplist_free;
pa_proplist_from_string;
pa_proplist_get;
pa_proplist_gets;
pa_proplist_isempty;
pa_proplist_iterate;
pa_proplist_key_valid;
pa_proplist_new;
pa_proplist_set;
pa_proplist_setf;
pa_proplist_setp;
pa_proplist_sets;
pa_proplist_size;
pa_proplist_to_string;
pa_proplist_to_string_sep;
pa_proplist_unset;
pa_proplist_unset_many;
pa_proplist_update;
pa_rtclock_now;
pa_sample_format_is_be;
pa_sample_format_is_le;
pa_sample_format_to_string;
pa_sample_format_valid;
pa_sample_rate_valid;
pa_sample_size;
pa_sample_size_of_format;
pa_sample_spec_equal;
pa_sample_spec_init;
pa_sample_spec_snprint;
pa_sample_spec_valid;
pa_signal_done;
pa_signal_free;
pa_signal_init;
pa_signal_new;
pa_signal_set_destroy;
pa_simple_drain;
pa_simple_flush;
pa_simple_free;
pa_simple_get_latency;
pa_simple_new;
pa_simple_read;
pa_simple_write;
pa_stream_begin_write;
pa_stream_cancel_write;
pa_stream_connect_playback;
pa_stream_connect_record;
pa_stream_connect_upload;
pa_stream_cork;
pa_stream_disconnect;
pa_stream_drain;
pa_stream_drop;
pa_stream_finish_upload;
pa_stream_flush;
pa_stream_get_buffer_attr;
pa_stream_get_channel_map;
pa_stream_get_context;
pa_stream_get_device_index;
pa_stream_get_device_name;
pa_stream_get_format_info;
pa_stream_get_index;
pa_stream_get_latency;
pa_stream_get_monitor_stream;
pa_stream_get_sample_spec;
pa_stream_get_state;
pa_stream_get_time;
pa_stream_get_timing_info;
pa_stream_get_underflow_index;
pa_stream_is_corked;
pa_stream_is_suspended;
pa_stream_new;
pa_stream_new_extended;
pa_stream_new_with_proplist;
pa_stream_peek;
pa_stream_prebuf;
pa_stream_proplist_remove;
pa_stream_proplist_update;
pa_stream_readable_size;
pa_stream_ref;
pa_stream_set_buffer_attr;
pa_stream_set_buffer_attr_callback;
pa_stream_set_event_callback;
pa_stream_set_latency_update_callback;
pa_stream_set_monitor_stream;
pa_stream_set_moved_callback;
pa_stream_set_name;
pa_stream_set_overflow_callback;
pa_stream_set_read_callback;
pa_stream_set_started_callback;
pa_stream_set_state_callback;
pa_stream_set_suspended_callback;
pa_stream_set_underflow_callback;
pa_stream_set_write_callback;
pa_stream_trigger;
pa_stream_unref;
pa_stream_update_sample_rate;
pa_stream_update_timing_info;
pa_stream_writable_size;
pa_stream_write;
pa_stream_write_ext_free;
pa_strerror;
pa_sw_cvolume_divide;
pa_sw_cvolume_divide_scalar;
pa_sw_cvolume_multiply;
pa_sw_cvolume_multiply_scalar;
pa_sw_cvolume_snprint_dB;
pa_sw_volume_divide;
pa_sw_volume_from_dB;
pa_sw_volume_from_linear;
pa_sw_volume_multiply;
pa_sw_volume_snprint_dB;
pa_sw_volume_to_dB;
pa_sw_volume_to_linear;
pa_threaded_mainloop_accept;
pa_threaded_mainloop_free;
pa_threaded_mainloop_get_api;
pa_threaded_mainloop_get_retval;
pa_threaded_mainloop_in_thread;
pa_threaded_mainloop_lock;
pa_threaded_mainloop_new;
pa_threaded_mainloop_set_name;
pa_threaded_mainloop_signal;
pa_threaded_mainloop_start;
pa_threaded_mainloop_stop;
pa_threaded_mainloop_unlock;
pa_threaded_mainloop_wait;
pa_timeval_add;
pa_timeval_age;
pa_timeval_cmp;
pa_timeval_diff;
pa_timeval_load;
pa_timeval_store;
pa_timeval_sub;
pa_usec_to_bytes;
pa_utf8_filter;
pa_utf8_to_locale;
pa_utf8_valid;
pa_volume_snprint;
pa_volume_snprint_verbose;
pa_xfree;
pa_xmalloc;
pa_xmalloc0;
pa_xmemdup;
pa_xrealloc;
pa_xstrdup;
pa_xstrndup;
local:
*;
};

View file

@ -0,0 +1,65 @@
pipewire_pulseaudio_sources = [
'bitset.c',
'channelmap.c',
'context.c',
'core-format.c',
'direction.c',
'error.c',
'ext-device-manager.c',
'ext-device-restore.c',
'ext-stream-restore.c',
'format.c',
'introspect.c',
'json.c',
'mainloop.c',
'mainloop-signal.c',
'operation.c',
'proplist.c',
'rtclock.c',
'sample.c',
'scache.c',
'stream.c',
'strbuf.c',
'subscribe.c',
'thread-mainloop.c',
'timeval.c',
'utf8.c',
'util.c',
'version.c',
'volume.c',
'xmalloc.c',
'pipewire-pulseaudio.c',
]
pipewire_mainloop_glib_sources = [
'mainloop-glib.c',
]
pipewire_pulseaudio_c_args = [
'-DHAVE_CONFIG_H',
'-D_GNU_SOURCE',
'-DPIC',
]
mapfile = 'map-file'
vflag = '-Wl,--version-script,@0@/@1@'.format(meson.current_source_dir(), mapfile)
pipewire_pulseaudio = shared_library('pulse',
pipewire_pulseaudio_sources,
soversion : '0',
c_args : pipewire_pulseaudio_c_args,
link_args : vflag,
include_directories : [configinc],
dependencies : [pipewire_dep, pulseaudio_dep, mathlib],
install : false,
)
pipewire_pulseaudio = shared_library('pulse-mainloop-glib',
pipewire_mainloop_glib_sources,
soversion : '0',
c_args : pipewire_pulseaudio_c_args,
link_args : vflag,
include_directories : [configinc],
dependencies : [pipewire_dep, pulseaudio_dep, mathlib, glib_dep],
install : false,
)

View file

@ -0,0 +1,165 @@
/* PipeWire
* Copyright (C) 2018 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <errno.h>
#include <spa/utils/list.h>
#include <pipewire/log.h>
#include <pulse/operation.h>
#include "internal.h"
pa_operation *pa_operation_new(pa_context *c, pa_stream *s, pa_operation_cb_t cb, size_t userdata_size) {
pa_operation *o;
pa_assert(c);
o = calloc(1, sizeof(pa_operation) + userdata_size);
o->refcount = 1;
o->context = c;
o->stream = s ? pa_stream_ref(s) : NULL;
o->seq = SPA_ID_INVALID;
o->state = PA_OPERATION_RUNNING;
o->callback = cb;
o->userdata = SPA_MEMBER(o, sizeof(pa_operation), void);
spa_list_append(&c->operations, &o->link);
pa_operation_ref(o);
pw_log_debug("new %p", o);
return o;
}
int pa_operation_sync(pa_operation *o)
{
pa_context *c = o->context;
o->seq = pw_core_proxy_sync(c->core_proxy, 0, 0);
pw_log_debug("operation %p: sync %d", o, o->seq);
return 0;
}
SPA_EXPORT
pa_operation *pa_operation_ref(pa_operation *o)
{
pa_assert(o);
pa_assert(o->refcount >= 1);
o->refcount++;
return o;
}
static void operation_free(pa_operation *o)
{
pa_assert(!o->context);
pa_assert(!o->stream);
pw_log_debug("%p %d", o, o->seq);
free(o);
}
static void operation_unlink(pa_operation *o) {
pa_assert(o);
pw_log_debug("%p %d", o, o->seq);
if (o->context) {
pa_assert(o->refcount >= 2);
spa_list_remove(&o->link);
pa_operation_unref(o);
o->context = NULL;
}
if (o->stream)
pa_stream_unref(o->stream);
o->stream = NULL;
o->callback = NULL;
o->userdata = NULL;
o->state_callback = NULL;
o->state_userdata = NULL;
}
SPA_EXPORT
void pa_operation_unref(pa_operation *o)
{
pa_assert(o);
pa_assert(o->refcount >= 1);
if (--o->refcount == 0)
operation_free(o);
}
static void operation_set_state(pa_operation *o, pa_operation_state_t st) {
pa_assert(o);
pa_assert(o->refcount >= 1);
if (st == o->state)
return;
pa_operation_ref(o);
pw_log_debug("new state %p %d %d", o, o->seq, st);
o->state = st;
if (o->state_callback)
o->state_callback(o, o->state_userdata);
if ((o->state == PA_OPERATION_DONE) || (o->state == PA_OPERATION_CANCELED))
operation_unlink(o);
pa_operation_unref(o);
}
SPA_EXPORT
void pa_operation_cancel(pa_operation *o)
{
pa_assert(o);
pa_assert(o->refcount >= 1);
pw_log_debug("%p %d", o, o->seq);
operation_set_state(o, PA_OPERATION_CANCELED);
}
void pa_operation_done(pa_operation *o) {
pa_assert(o);
pa_assert(o->refcount >= 1);
operation_set_state(o, PA_OPERATION_DONE);
}
SPA_EXPORT
pa_operation_state_t pa_operation_get_state(PA_CONST pa_operation *o)
{
pa_assert(o);
pa_assert(o->refcount >= 1);
return o->state;
}
SPA_EXPORT
void pa_operation_set_state_callback(pa_operation *o, pa_operation_notify_cb_t cb, void *userdata)
{
pa_assert(o);
pa_assert(o->refcount >= 1);
if (o->state == PA_OPERATION_DONE || o->state == PA_OPERATION_CANCELED)
return;
o->state_callback = cb;
o->state_userdata = userdata;
}

View file

@ -0,0 +1,30 @@
/* PipeWire
* Copyright (C) 2018 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <pipewire/pipewire.h>
static void reg(void) __attribute__ ((constructor));
static void reg(void)
{
pw_init(NULL, NULL);
}

View file

@ -0,0 +1,372 @@
/* PipeWire
* Copyright (C) 2018 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <string.h>
#include <spa/debug/mem.h>
#include <pipewire/log.h>
#include <pipewire/properties.h>
#include <pulse/proplist.h>
#include "internal.h"
#include "strbuf.h"
pa_proplist* pa_proplist_new_dict(struct spa_dict *dict)
{
pa_proplist *p;
p = calloc(1, sizeof(struct pa_proplist));
if (p == NULL)
return NULL;
if (dict)
p->props = pw_properties_new_dict(dict);
else
p->props = pw_properties_new(NULL, NULL);
return p;
}
pa_proplist* pa_proplist_new_props(struct pw_properties *props)
{
return pa_proplist_new_dict(&props->dict);
}
SPA_EXPORT
pa_proplist* pa_proplist_new(void)
{
return pa_proplist_new_dict(NULL);
}
int pa_proplist_update_dict(pa_proplist *p, struct spa_dict *dict)
{
return pw_properties_update(p->props, dict);
}
SPA_EXPORT
void pa_proplist_free(pa_proplist* p)
{
pw_properties_free(p->props);
free(p);
}
SPA_EXPORT
int pa_proplist_key_valid(const char *key)
{
const char *p;
for (p = key; *p; p++)
if ((unsigned char) *p >= 128)
return 0;
if (strlen(key) < 1)
return 0;
return 1;
}
SPA_EXPORT
int pa_proplist_sets(pa_proplist *p, const char *key, const char *value)
{
pa_assert(p);
pa_assert(key);
pa_assert(value);
if (!pa_proplist_key_valid(key))
return -1;
pw_properties_set(p->props, key, value);
return 0;
}
SPA_EXPORT
int pa_proplist_setp(pa_proplist *p, const char *pair)
{
const char *t;
char *c;
int idx;
pa_assert(p);
pa_assert(pair);
if (!(t = strchr(pair, '=')))
return -1;
idx = pair - t;
c = strdup(pair);
c[idx] = 0;
pa_proplist_sets(p, c, &c[idx]+1);
free(c);
return 0;
}
SPA_EXPORT
int pa_proplist_setf(pa_proplist *p, const char *key, const char *format, ...)
{
va_list varargs;
va_start(varargs, format);
pw_properties_setva(p->props, key, format, varargs);
va_end(varargs);
return 0;
}
SPA_EXPORT
int pa_proplist_set(pa_proplist *p, const char *key, const void *data, size_t nbytes)
{
pa_assert(p);
pa_assert(key);
pa_assert(data || nbytes == 0);
if (!pa_proplist_key_valid(key))
return -1;
pw_properties_set(p->props, key, data);
return 0;
}
SPA_EXPORT
const char *pa_proplist_gets(PA_CONST pa_proplist *p, const char *key)
{
return pw_properties_get(p->props, key);
}
SPA_EXPORT
int pa_proplist_get(PA_CONST pa_proplist *p, const char *key, const void **data, size_t *nbytes)
{
const char *val;
spa_assert(p);
spa_assert(key);
val = pw_properties_get(p->props, key);
*data = val;
*nbytes = val ? strlen(val) : 0;
return 0;
}
SPA_EXPORT
void pa_proplist_update(pa_proplist *p, pa_update_mode_t mode, const pa_proplist *other)
{
uint32_t i;
spa_assert(p);
spa_assert(mode == PA_UPDATE_SET || mode == PA_UPDATE_MERGE || mode == PA_UPDATE_REPLACE);
spa_assert(other);
if (mode == PA_UPDATE_REPLACE) {
pa_proplist_update_dict(p, &other->props->dict);
return;
}
if (mode == PA_UPDATE_SET)
pa_proplist_clear(p);
for (i = 0; i < other->props->dict.n_items; i++) {
const struct spa_dict_item *oi;
oi = &other->props->dict.items[i];
if (pw_properties_get(p->props, oi->key) == NULL)
pw_properties_set(p->props, oi->key, oi->value);
}
}
SPA_EXPORT
int pa_proplist_unset(pa_proplist *p, const char *key)
{
spa_assert(p);
spa_assert(key);
if (!pa_proplist_key_valid(key))
return -1;
return pw_properties_set(p->props, key, NULL);
}
SPA_EXPORT
int pa_proplist_unset_many(pa_proplist *p, const char * const keys[])
{
const char * const * k;
int n = 0;
spa_assert(p);
spa_assert(keys);
for (k = keys; *k; k++)
if (!pa_proplist_key_valid(*k))
return -1;
for (k = keys; *k; k++)
if (pa_proplist_unset(p, *k) >= 0)
n++;
return n;
}
SPA_EXPORT
const char *pa_proplist_iterate(PA_CONST pa_proplist *p, void **state)
{
spa_assert(p);
spa_assert(state);
return pw_properties_iterate(p->props, state);
}
SPA_EXPORT
char *pa_proplist_to_string(PA_CONST pa_proplist *p)
{
spa_assert(p);
return pa_proplist_to_string_sep(p, ",");
}
SPA_EXPORT
char *pa_proplist_to_string_sep(PA_CONST pa_proplist *p, const char *sep)
{
const char *key;
void *state = NULL;
pa_strbuf *buf;
spa_assert(p);
spa_assert(sep);
buf = pa_strbuf_new();
while ((key = pa_proplist_iterate(p, &state))) {
const char *v;
const char *t;
if (!pa_strbuf_isempty(buf))
pa_strbuf_puts(buf, sep);
if ((v = pa_proplist_gets(p, key)) == NULL)
continue;
pa_strbuf_printf(buf, "%s = \"", key);
for (t = v;;) {
size_t h;
h = strcspn(t, "\"");
if (h > 0)
pa_strbuf_putsn(buf, t, h);
t += h;
if (*t == 0)
break;
pa_assert(*t == '"');
pa_strbuf_puts(buf, "\\\"");
t++;
}
pa_strbuf_puts(buf, "\"");
}
return pa_strbuf_to_string_free(buf);
}
SPA_EXPORT
pa_proplist *pa_proplist_from_string(const char *str)
{
spa_assert(str);
pw_log_warn("Not Implemented");
return NULL;
}
SPA_EXPORT
int pa_proplist_contains(PA_CONST pa_proplist *p, const char *key)
{
spa_assert(p);
spa_assert(key);
if (!pa_proplist_key_valid(key))
return -1;
if (pw_properties_get(p->props, key) == NULL)
return 0;
return 1;
}
SPA_EXPORT
void pa_proplist_clear(pa_proplist *p)
{
spa_assert(p);
pw_properties_clear(p->props);
}
SPA_EXPORT
pa_proplist* pa_proplist_copy(const pa_proplist *p)
{
pa_proplist *c;
spa_assert(p);
c = calloc(1, sizeof(struct pa_proplist));
if (c == NULL)
return NULL;
c->props = pw_properties_copy(p->props);
return c;
}
SPA_EXPORT
unsigned pa_proplist_size(PA_CONST pa_proplist *p)
{
spa_assert(p);
return p->props->dict.n_items;
}
SPA_EXPORT
int pa_proplist_isempty(PA_CONST pa_proplist *p)
{
spa_assert(p);
return p->props->dict.n_items == 0 ? 1 : 0;
}
SPA_EXPORT
int pa_proplist_equal(PA_CONST pa_proplist *a, PA_CONST pa_proplist *b)
{
uint32_t i;
spa_assert(a);
spa_assert(b);
if (a == b)
return 1;
if (pa_proplist_size(a) != pa_proplist_size(b))
return 0;
for (i = 0; i < a->props->dict.n_items; i++) {
const struct spa_dict_item *ai, *bi;
ai = &a->props->dict.items[i];
bi = spa_dict_lookup_item(&b->props->dict, ai->key);
if (bi == NULL || bi->value == NULL || ai->value == NULL)
return 0;
if (strcmp(ai->value, bi->value) != 0)
return 0;
}
return 1;
}

View file

@ -0,0 +1,38 @@
/* PipeWire
* Copyright (C) 2018 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <time.h>
#include <spa/utils/defs.h>
#include <pipewire/log.h>
#include <pulse/rtclock.h>
SPA_EXPORT
pa_usec_t pa_rtclock_now(void)
{
struct timespec ts;
pa_usec_t res;
clock_gettime(CLOCK_MONOTONIC, &ts);
res = (ts.tv_sec * SPA_USEC_PER_SEC) + (ts.tv_nsec / SPA_NSEC_PER_USEC);
return res;
}

View file

@ -0,0 +1,134 @@
#ifndef foosampleutilhfoo
#define foosampleutilhfoo
/***
This file is part of PulseAudio.
Copyright 2004-2006 Lennart Poettering
Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
***/
#include <inttypes.h>
#include <limits.h>
#include <pulse/gccmacro.h>
#include <pulse/sample.h>
#include <pulse/volume.h>
#include <pulse/channelmap.h>
size_t pa_frame_align(size_t l, const pa_sample_spec *ss) PA_GCC_PURE;
bool pa_frame_aligned(size_t l, const pa_sample_spec *ss) PA_GCC_PURE;
void pa_interleave(const void *src[], unsigned channels, void *dst, size_t ss, unsigned n);
void pa_deinterleave(const void *src, void *dst[], unsigned channels, size_t ss, unsigned n);
void pa_sample_clamp(pa_sample_format_t format, void *dst, size_t dstr, const void *src, size_t sstr, unsigned n);
static inline int32_t pa_mult_s16_volume(int16_t v, int32_t cv) {
#ifdef HAVE_FAST_64BIT_OPERATIONS
/* Multiply with 64 bit integers on 64 bit platforms */
return (v * (int64_t) cv) >> 16;
#else
/* Multiplying the 32 bit volume factor with the
* 16 bit sample might result in an 48 bit value. We
* want to do without 64 bit integers and hence do
* the multiplication independently for the HI and
* LO part of the volume. */
int32_t hi = cv >> 16;
int32_t lo = cv & 0xFFFF;
return ((v * lo) >> 16) + (v * hi);
#endif
}
pa_usec_t pa_bytes_to_usec_round_up(uint64_t length, const pa_sample_spec *spec);
size_t pa_usec_to_bytes_round_up(pa_usec_t t, const pa_sample_spec *spec);
typedef void (*pa_do_volume_func_t) (void *samples, const void *volumes, unsigned channels, unsigned length);
pa_do_volume_func_t pa_get_volume_func(pa_sample_format_t f);
void pa_set_volume_func(pa_sample_format_t f, pa_do_volume_func_t func);
size_t pa_convert_size(size_t size, const pa_sample_spec *from, const pa_sample_spec *to);
#define PA_CHANNEL_POSITION_MASK_LEFT \
(PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_LEFT) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_REAR_LEFT) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_SIDE_LEFT) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_FRONT_LEFT) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_REAR_LEFT)) \
#define PA_CHANNEL_POSITION_MASK_RIGHT \
(PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_RIGHT) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_REAR_RIGHT) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_SIDE_RIGHT) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_FRONT_RIGHT) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_REAR_RIGHT))
#define PA_CHANNEL_POSITION_MASK_CENTER \
(PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_CENTER) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_REAR_CENTER) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_CENTER) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_FRONT_CENTER) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_REAR_CENTER))
#define PA_CHANNEL_POSITION_MASK_FRONT \
(PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_LEFT) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_RIGHT) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_CENTER) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_FRONT_LEFT) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_FRONT_RIGHT) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_FRONT_CENTER))
#define PA_CHANNEL_POSITION_MASK_REAR \
(PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_REAR_LEFT) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_REAR_RIGHT) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_REAR_CENTER) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_REAR_LEFT) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_REAR_RIGHT) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_REAR_CENTER))
#define PA_CHANNEL_POSITION_MASK_LFE \
PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_LFE)
#define PA_CHANNEL_POSITION_MASK_HFE \
(PA_CHANNEL_POSITION_MASK_REAR | PA_CHANNEL_POSITION_MASK_FRONT \
| PA_CHANNEL_POSITION_MASK_LEFT | PA_CHANNEL_POSITION_MASK_RIGHT \
| PA_CHANNEL_POSITION_MASK_CENTER)
#define PA_CHANNEL_POSITION_MASK_SIDE_OR_TOP_CENTER \
(PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_SIDE_LEFT) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_SIDE_RIGHT) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_CENTER))
#define PA_CHANNEL_POSITION_MASK_TOP \
(PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_CENTER) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_FRONT_LEFT) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_FRONT_RIGHT) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_FRONT_CENTER) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_REAR_LEFT) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_REAR_RIGHT) \
| PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_REAR_CENTER))
#define PA_CHANNEL_POSITION_MASK_ALL \
((pa_channel_position_mask_t) (PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_MAX)-1))
#endif

View file

@ -0,0 +1,337 @@
/***
This file is part of PulseAudio.
Copyright 2004-2006 Lennart Poettering
Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
Copyright 2018 Wim Taymans <wim.taymans@gmail.com>
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <string.h>
#include <spa/utils/defs.h>
#include <pulse/timeval.h>
#include <pulse/sample.h>
#define pa_init_i18n()
#define _(String) (String)
static const size_t size_table[] = {
[PA_SAMPLE_U8] = 1,
[PA_SAMPLE_ULAW] = 1,
[PA_SAMPLE_ALAW] = 1,
[PA_SAMPLE_S16LE] = 2,
[PA_SAMPLE_S16BE] = 2,
[PA_SAMPLE_FLOAT32LE] = 4,
[PA_SAMPLE_FLOAT32BE] = 4,
[PA_SAMPLE_S32LE] = 4,
[PA_SAMPLE_S32BE] = 4,
[PA_SAMPLE_S24LE] = 3,
[PA_SAMPLE_S24BE] = 3,
[PA_SAMPLE_S24_32LE] = 4,
[PA_SAMPLE_S24_32BE] = 4
};
SPA_EXPORT
size_t pa_sample_size_of_format(pa_sample_format_t f)
{
spa_assert(pa_sample_format_valid(f));
return size_table[f];
}
SPA_EXPORT
size_t pa_sample_size(const pa_sample_spec * spec)
{
spa_assert(spec);
spa_assert(pa_sample_spec_valid(spec));
return size_table[spec->format];
}
SPA_EXPORT
size_t pa_frame_size(const pa_sample_spec * spec)
{
spa_assert(spec);
spa_assert(pa_sample_spec_valid(spec));
return size_table[spec->format] * spec->channels;
}
SPA_EXPORT
size_t pa_bytes_per_second(const pa_sample_spec * spec)
{
spa_assert(spec);
spa_assert(pa_sample_spec_valid(spec));
return spec->rate * size_table[spec->format] * spec->channels;
}
SPA_EXPORT
pa_usec_t pa_bytes_to_usec(uint64_t length, const pa_sample_spec * spec)
{
spa_assert(spec);
spa_assert(pa_sample_spec_valid(spec));
return (((pa_usec_t)
(length / (size_table[spec->format] * spec->channels))
* PA_USEC_PER_SEC) / spec->rate);
}
SPA_EXPORT
size_t pa_usec_to_bytes(pa_usec_t t, const pa_sample_spec * spec)
{
spa_assert(spec);
spa_assert(pa_sample_spec_valid(spec));
return (size_t) (((t * spec->rate) / PA_USEC_PER_SEC)) *
(size_table[spec->format] * spec->channels);
}
SPA_EXPORT
pa_sample_spec *pa_sample_spec_init(pa_sample_spec * spec)
{
spa_assert(spec);
spec->format = PA_SAMPLE_INVALID;
spec->rate = 0;
spec->channels = 0;
return spec;
}
SPA_EXPORT
int pa_sample_format_valid(unsigned format)
{
return format < PA_SAMPLE_MAX;
}
SPA_EXPORT
int pa_sample_rate_valid(uint32_t rate)
{
/* The extra 1% is due to module-loopback: it temporarily sets
* a higher-than-nominal rate to get rid of excessive buffer
* latency */
return rate > 0 && rate <= PA_RATE_MAX * 101 / 100;
}
SPA_EXPORT
int pa_channels_valid(uint8_t channels)
{
return channels > 0 && channels <= PA_CHANNELS_MAX;
}
SPA_EXPORT
int pa_sample_spec_valid(const pa_sample_spec * spec)
{
spa_assert(spec);
if (SPA_UNLIKELY(!pa_sample_rate_valid(spec->rate) ||
!pa_channels_valid(spec->channels) ||
!pa_sample_format_valid(spec->format)))
return 0;
return 1;
}
SPA_EXPORT
int pa_sample_spec_equal(const pa_sample_spec * a, const pa_sample_spec * b)
{
spa_assert(a);
spa_assert(b);
spa_return_val_if_fail(pa_sample_spec_valid(a), 0);
if (SPA_UNLIKELY(a == b))
return 1;
spa_return_val_if_fail(pa_sample_spec_valid(b), 0);
return
(a->format == b->format) &&
(a->rate == b->rate) && (a->channels == b->channels);
}
SPA_EXPORT
const char *pa_sample_format_to_string(pa_sample_format_t f)
{
static const char *const table[] = {
[PA_SAMPLE_U8] = "u8",
[PA_SAMPLE_ALAW] = "aLaw",
[PA_SAMPLE_ULAW] = "uLaw",
[PA_SAMPLE_S16LE] = "s16le",
[PA_SAMPLE_S16BE] = "s16be",
[PA_SAMPLE_FLOAT32LE] = "float32le",
[PA_SAMPLE_FLOAT32BE] = "float32be",
[PA_SAMPLE_S32LE] = "s32le",
[PA_SAMPLE_S32BE] = "s32be",
[PA_SAMPLE_S24LE] = "s24le",
[PA_SAMPLE_S24BE] = "s24be",
[PA_SAMPLE_S24_32LE] = "s24-32le",
[PA_SAMPLE_S24_32BE] = "s24-32be",
};
if (!pa_sample_format_valid(f))
return NULL;
return table[f];
}
SPA_EXPORT
char *pa_sample_spec_snprint(char *s, size_t l, const pa_sample_spec * spec)
{
spa_assert(s);
spa_assert(l > 0);
spa_assert(spec);
pa_init_i18n();
if (!pa_sample_spec_valid(spec))
snprintf(s, l, _("(invalid)"));
else
snprintf(s, l, _("%s %uch %uHz"),
pa_sample_format_to_string(spec->format),
spec->channels, spec->rate);
return s;
}
SPA_EXPORT
char *pa_bytes_snprint(char *s, size_t l, unsigned v)
{
spa_assert(s);
spa_assert(l > 0);
pa_init_i18n();
if (v >= ((unsigned)1024) * 1024 * 1024)
snprintf(s, l, _("%0.1f GiB"),
((double)v) / 1024 / 1024 / 1024);
else if (v >= ((unsigned)1024) * 1024)
snprintf(s, l, _("%0.1f MiB"), ((double)v) / 1024 / 1024);
else if (v >= (unsigned)1024)
snprintf(s, l, _("%0.1f KiB"), ((double)v) / 1024);
else
snprintf(s, l, _("%u B"), (unsigned)v);
return s;
}
SPA_EXPORT
pa_sample_format_t pa_parse_sample_format(const char *format)
{
spa_assert(format);
if (strcasecmp(format, "s16le") == 0)
return PA_SAMPLE_S16LE;
else if (strcasecmp(format, "s16be") == 0)
return PA_SAMPLE_S16BE;
else if (strcasecmp(format, "s16ne") == 0
|| strcasecmp(format, "s16") == 0
|| strcasecmp(format, "16") == 0)
return PA_SAMPLE_S16NE;
else if (strcasecmp(format, "s16re") == 0)
return PA_SAMPLE_S16RE;
else if (strcasecmp(format, "u8") == 0 || strcasecmp(format, "8") == 0)
return PA_SAMPLE_U8;
else if (strcasecmp(format, "float32") == 0
|| strcasecmp(format, "float32ne") == 0
|| strcasecmp(format, "float") == 0)
return PA_SAMPLE_FLOAT32NE;
else if (strcasecmp(format, "float32re") == 0)
return PA_SAMPLE_FLOAT32RE;
else if (strcasecmp(format, "float32le") == 0)
return PA_SAMPLE_FLOAT32LE;
else if (strcasecmp(format, "float32be") == 0)
return PA_SAMPLE_FLOAT32BE;
else if (strcasecmp(format, "ulaw") == 0
|| strcasecmp(format, "mulaw") == 0)
return PA_SAMPLE_ULAW;
else if (strcasecmp(format, "alaw") == 0)
return PA_SAMPLE_ALAW;
else if (strcasecmp(format, "s32le") == 0)
return PA_SAMPLE_S32LE;
else if (strcasecmp(format, "s32be") == 0)
return PA_SAMPLE_S32BE;
else if (strcasecmp(format, "s32ne") == 0
|| strcasecmp(format, "s32") == 0
|| strcasecmp(format, "32") == 0)
return PA_SAMPLE_S32NE;
else if (strcasecmp(format, "s32re") == 0)
return PA_SAMPLE_S24RE;
else if (strcasecmp(format, "s24le") == 0)
return PA_SAMPLE_S24LE;
else if (strcasecmp(format, "s24be") == 0)
return PA_SAMPLE_S24BE;
else if (strcasecmp(format, "s24ne") == 0
|| strcasecmp(format, "s24") == 0
|| strcasecmp(format, "24") == 0)
return PA_SAMPLE_S24NE;
else if (strcasecmp(format, "s24re") == 0)
return PA_SAMPLE_S24RE;
else if (strcasecmp(format, "s24-32le") == 0)
return PA_SAMPLE_S24_32LE;
else if (strcasecmp(format, "s24-32be") == 0)
return PA_SAMPLE_S24_32BE;
else if (strcasecmp(format, "s24-32ne") == 0
|| strcasecmp(format, "s24-32") == 0)
return PA_SAMPLE_S24_32NE;
else if (strcasecmp(format, "s24-32re") == 0)
return PA_SAMPLE_S24_32RE;
return PA_SAMPLE_INVALID;
}
SPA_EXPORT
int pa_sample_format_is_le(pa_sample_format_t f)
{
spa_assert(pa_sample_format_valid(f));
switch (f) {
case PA_SAMPLE_S16LE:
case PA_SAMPLE_S24LE:
case PA_SAMPLE_S32LE:
case PA_SAMPLE_S24_32LE:
case PA_SAMPLE_FLOAT32LE:
return 1;
case PA_SAMPLE_S16BE:
case PA_SAMPLE_S24BE:
case PA_SAMPLE_S32BE:
case PA_SAMPLE_S24_32BE:
case PA_SAMPLE_FLOAT32BE:
return 0;
default:
return -1;
}
}
SPA_EXPORT
int pa_sample_format_is_be(pa_sample_format_t f)
{
int r;
if ((r = pa_sample_format_is_le(f)) < 0)
return r;
return !r;
}

View file

@ -0,0 +1,62 @@
/* PipeWire
* Copyright (C) 2018 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <pipewire/log.h>
#include <pulse/scache.h>
#include "internal.h"
SPA_EXPORT
int pa_stream_connect_upload(pa_stream *s, size_t length)
{
pw_log_warn("Not Implemented");
return 0;
}
SPA_EXPORT
int pa_stream_finish_upload(pa_stream *s)
{
pw_log_warn("Not Implemented");
return 0;
}
SPA_EXPORT
pa_operation* pa_context_remove_sample(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata)
{
pw_log_warn("Not Implemented");
return NULL;
}
SPA_EXPORT
pa_operation* pa_context_play_sample(pa_context *c, const char *name, const char *dev,
pa_volume_t volume, pa_context_success_cb_t cb, void *userdata)
{
pw_log_warn("Not Implemented");
return NULL;
}
SPA_EXPORT
pa_operation* pa_context_play_sample_with_proplist(pa_context *c, const char *name,
const char *dev, pa_volume_t volume, PA_CONST pa_proplist *proplist,
pa_context_play_sample_cb_t cb, void *userdata)
{
pw_log_warn("Not Implemented");
return NULL;
}

View file

@ -0,0 +1,193 @@
/***
This file is part of PulseAudio.
Copyright 2004-2006 Lennart Poettering
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <pulse/xmalloc.h>
#include "internal.h"
#include "strbuf.h"
/* A chunk of the linked list that makes up the string */
struct chunk {
struct chunk *next;
size_t length;
};
#define CHUNK_TO_TEXT(c) ((char*) (c) + PA_ALIGN(sizeof(struct chunk)))
struct pa_strbuf {
size_t length;
struct chunk *head, *tail;
};
pa_strbuf *pa_strbuf_new(void) {
pa_strbuf *sb;
sb = pa_xnew(pa_strbuf, 1);
sb->length = 0;
sb->head = sb->tail = NULL;
return sb;
}
void pa_strbuf_free(pa_strbuf *sb) {
pa_assert(sb);
while (sb->head) {
struct chunk *c = sb->head;
sb->head = sb->head->next;
pa_xfree(c);
}
pa_xfree(sb);
}
/* Make a C string from the string buffer. The caller has to free
* string with pa_xfree(). */
char *pa_strbuf_to_string(pa_strbuf *sb) {
char *t, *e;
struct chunk *c;
pa_assert(sb);
e = t = pa_xmalloc(sb->length+1);
for (c = sb->head; c; c = c->next) {
pa_assert((size_t) (e-t) <= sb->length);
memcpy(e, CHUNK_TO_TEXT(c), c->length);
e += c->length;
}
/* Trailing NUL */
*e = 0;
pa_assert(e == t+sb->length);
return t;
}
/* Combination of pa_strbuf_free() and pa_strbuf_to_string() */
char *pa_strbuf_to_string_free(pa_strbuf *sb) {
char *t;
pa_assert(sb);
t = pa_strbuf_to_string(sb);
pa_strbuf_free(sb);
return t;
}
/* Append a string to the string buffer */
void pa_strbuf_puts(pa_strbuf *sb, const char *t) {
pa_assert(sb);
pa_assert(t);
pa_strbuf_putsn(sb, t, strlen(t));
}
/* Append a character to the string buffer */
void pa_strbuf_putc(pa_strbuf *sb, char c) {
pa_assert(sb);
pa_strbuf_putsn(sb, &c, 1);
}
/* Append a new chunk to the linked list */
static void append(pa_strbuf *sb, struct chunk *c) {
pa_assert(sb);
pa_assert(c);
if (sb->tail) {
pa_assert(sb->head);
sb->tail->next = c;
} else {
pa_assert(!sb->head);
sb->head = c;
}
sb->tail = c;
sb->length += c->length;
c->next = NULL;
}
/* Append up to l bytes of a string to the string buffer */
void pa_strbuf_putsn(pa_strbuf *sb, const char *t, size_t l) {
struct chunk *c;
pa_assert(sb);
pa_assert(t);
if (!l)
return;
c = pa_xmalloc(PA_ALIGN(sizeof(struct chunk)) + l);
c->length = l;
memcpy(CHUNK_TO_TEXT(c), t, l);
append(sb, c);
}
/* Append a printf() style formatted string to the string buffer. */
/* The following is based on an example from the GNU libc documentation */
size_t pa_strbuf_printf(pa_strbuf *sb, const char *format, ...) {
size_t size = 100;
struct chunk *c = NULL;
pa_assert(sb);
pa_assert(format);
for(;;) {
va_list ap;
int r;
c = pa_xrealloc(c, PA_ALIGN(sizeof(struct chunk)) + size);
va_start(ap, format);
r = vsnprintf(CHUNK_TO_TEXT(c), size, format, ap);
CHUNK_TO_TEXT(c)[size-1] = 0;
va_end(ap);
if (r > -1 && (size_t) r < size) {
c->length = (size_t) r;
append(sb, c);
return (size_t) r;
}
if (r > -1) /* glibc 2.1 */
size = (size_t) r+1;
else /* glibc 2.0 */
size *= 2;
}
}
bool pa_strbuf_isempty(pa_strbuf *sb) {
pa_assert(sb);
return sb->length <= 0;
}

View file

@ -0,0 +1,39 @@
#ifndef foostrbufhfoo
#define foostrbufhfoo
/***
This file is part of PulseAudio.
Copyright 2004-2006 Lennart Poettering
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
***/
#include <spa/utils/defs.h>
typedef struct pa_strbuf pa_strbuf;
pa_strbuf *pa_strbuf_new(void);
void pa_strbuf_free(pa_strbuf *sb);
char *pa_strbuf_to_string(pa_strbuf *sb);
char *pa_strbuf_to_string_free(pa_strbuf *sb);
size_t pa_strbuf_printf(pa_strbuf *sb, const char *format, ...) PA_GCC_PRINTF_ATTR(2,3);
void pa_strbuf_puts(pa_strbuf *sb, const char *t);
void pa_strbuf_putsn(pa_strbuf *sb, const char *t, size_t m);
void pa_strbuf_putc(pa_strbuf *sb, char c);
bool pa_strbuf_isempty(pa_strbuf *sb);
#endif

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,43 @@
/* PipeWire
* Copyright (C) 2018 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <pipewire/log.h>
#include <pulse/subscribe.h>
#include "internal.h"
struct subscribe_data
{
pa_context_success_cb_t cb;
void *userdata;
};
SPA_EXPORT
void pa_context_set_subscribe_callback(pa_context *c, pa_context_subscribe_cb_t cb, void *userdata)
{
pa_assert(c);
pa_assert(c->refcount >= 1);
if (c->state == PA_CONTEXT_TERMINATED || c->state == PA_CONTEXT_FAILED)
return;
c->subscribe_callback = cb;
c->subscribe_userdata = userdata;
}

View file

@ -0,0 +1,131 @@
/* PipeWire
* Copyright (C) 2018 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <pipewire/log.h>
#include <pipewire/thread-loop.h>
#include <pulse/mainloop.h>
#include <pulse/thread-mainloop.h>
#include "internal.h"
struct pa_threaded_mainloop
{
pa_mainloop *loop;
struct pw_thread_loop *tloop;
};
SPA_EXPORT
pa_threaded_mainloop *pa_threaded_mainloop_new(void)
{
pa_threaded_mainloop *m;
m = calloc(1, sizeof(pa_threaded_mainloop));
if (m == NULL)
return NULL;
m->loop = pa_mainloop_new();
if (m->loop == NULL)
goto no_mem;
m->tloop = pw_thread_loop_new(m->loop->loop, NULL);
if (m->tloop == NULL)
goto no_mem;
return m;
no_mem:
if (m->loop)
pa_mainloop_free(m->loop);
free(m);
return NULL;
}
SPA_EXPORT
void pa_threaded_mainloop_free(pa_threaded_mainloop* m)
{
pw_thread_loop_destroy(m->tloop);
pa_mainloop_free(m->loop);
free(m);
}
SPA_EXPORT
int pa_threaded_mainloop_start(pa_threaded_mainloop *m)
{
return pw_thread_loop_start(m->tloop);
}
SPA_EXPORT
void pa_threaded_mainloop_stop(pa_threaded_mainloop *m)
{
pw_thread_loop_stop(m->tloop);
}
SPA_EXPORT
void pa_threaded_mainloop_lock(pa_threaded_mainloop *m)
{
pw_thread_loop_lock(m->tloop);
}
SPA_EXPORT
void pa_threaded_mainloop_unlock(pa_threaded_mainloop *m)
{
pw_thread_loop_unlock(m->tloop);
}
SPA_EXPORT
void pa_threaded_mainloop_wait(pa_threaded_mainloop *m)
{
pw_thread_loop_wait(m->tloop);
}
SPA_EXPORT
void pa_threaded_mainloop_signal(pa_threaded_mainloop *m, int wait_for_accept)
{
pw_thread_loop_signal(m->tloop, wait_for_accept);
}
SPA_EXPORT
void pa_threaded_mainloop_accept(pa_threaded_mainloop *m)
{
pw_thread_loop_accept(m->tloop);
}
SPA_EXPORT
int pa_threaded_mainloop_get_retval(PA_CONST pa_threaded_mainloop *m)
{
return pa_mainloop_get_retval(m->loop);
}
SPA_EXPORT
pa_mainloop_api* pa_threaded_mainloop_get_api(pa_threaded_mainloop*m)
{
return pa_mainloop_get_api(m->loop);
}
SPA_EXPORT
int pa_threaded_mainloop_in_thread(pa_threaded_mainloop *m)
{
return pw_thread_loop_in_thread(m->tloop);
}
SPA_EXPORT
void pa_threaded_mainloop_set_name(pa_threaded_mainloop *m, const char *name)
{
}

View file

@ -0,0 +1,220 @@
/***
This file is part of PulseAudio.
Copyright 2004-2006 Lennart Poettering
Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stddef.h>
#include <sys/time.h>
#ifdef HAVE_WINDOWS_H
#include <windows.h>
#endif
#include <pulse/timeval.h>
#include "internal.h"
#define HAVE_GETTIMEOFDAY
SPA_EXPORT
struct timeval *pa_gettimeofday(struct timeval *tv) {
pa_assert(tv);
#if defined(OS_IS_WIN32)
/*
* Copied from implementation by Steven Edwards (LGPL).
* Found on wine mailing list.
*/
#if defined(_MSC_VER) || defined(__BORLANDC__)
#define EPOCHFILETIME (116444736000000000i64)
#else
#define EPOCHFILETIME (116444736000000000LL)
#endif
{
FILETIME ft;
LARGE_INTEGER li;
int64_t t;
GetSystemTimeAsFileTime(&ft);
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;
t = li.QuadPart; /* In 100-nanosecond intervals */
t -= EPOCHFILETIME; /* Offset to the Epoch time */
t /= 10; /* In microseconds */
tv->tv_sec = (time_t) (t / PA_USEC_PER_SEC);
tv->tv_usec = (suseconds_t) (t % PA_USEC_PER_SEC);
}
#elif defined(HAVE_GETTIMEOFDAY)
pa_assert_se(gettimeofday(tv, NULL) == 0);
#else
#error "Platform lacks gettimeofday() or equivalent function."
#endif
return tv;
}
SPA_EXPORT
pa_usec_t pa_timeval_diff(const struct timeval *a, const struct timeval *b) {
pa_usec_t r;
pa_assert(a);
pa_assert(b);
/* Check which is the earlier time and swap the two arguments if required. */
if (PA_UNLIKELY(pa_timeval_cmp(a, b) < 0)) {
const struct timeval *c;
c = a;
a = b;
b = c;
}
/* Calculate the second difference*/
r = ((pa_usec_t) a->tv_sec - (pa_usec_t) b->tv_sec) * PA_USEC_PER_SEC;
/* Calculate the microsecond difference */
if (a->tv_usec > b->tv_usec)
r += (pa_usec_t) a->tv_usec - (pa_usec_t) b->tv_usec;
else if (a->tv_usec < b->tv_usec)
r -= (pa_usec_t) b->tv_usec - (pa_usec_t) a->tv_usec;
return r;
}
SPA_EXPORT
int pa_timeval_cmp(const struct timeval *a, const struct timeval *b) {
pa_assert(a);
pa_assert(b);
if (a->tv_sec < b->tv_sec)
return -1;
if (a->tv_sec > b->tv_sec)
return 1;
if (a->tv_usec < b->tv_usec)
return -1;
if (a->tv_usec > b->tv_usec)
return 1;
return 0;
}
SPA_EXPORT
pa_usec_t pa_timeval_age(const struct timeval *tv) {
struct timeval now;
pa_assert(tv);
return pa_timeval_diff(pa_gettimeofday(&now), tv);
}
SPA_EXPORT
struct timeval* pa_timeval_add(struct timeval *tv, pa_usec_t v) {
time_t secs;
pa_assert(tv);
secs = (time_t) (v/PA_USEC_PER_SEC);
if (PA_UNLIKELY(tv->tv_sec > PA_INT_TYPE_MAX(time_t) - secs))
goto overflow;
tv->tv_sec += secs;
v -= (pa_usec_t) secs * PA_USEC_PER_SEC;
tv->tv_usec += (suseconds_t) v;
/* Normalize */
while ((pa_usec_t) tv->tv_usec >= PA_USEC_PER_SEC) {
if (PA_UNLIKELY(tv->tv_sec >= PA_INT_TYPE_MAX(time_t)))
goto overflow;
tv->tv_sec++;
tv->tv_usec -= (suseconds_t) PA_USEC_PER_SEC;
}
return tv;
overflow:
tv->tv_sec = PA_INT_TYPE_MAX(time_t);
tv->tv_usec = (suseconds_t) (PA_USEC_PER_SEC-1);
return tv;
}
SPA_EXPORT
struct timeval* pa_timeval_sub(struct timeval *tv, pa_usec_t v) {
time_t secs;
pa_assert(tv);
secs = (time_t) (v/PA_USEC_PER_SEC);
if (PA_UNLIKELY(tv->tv_sec < secs))
goto underflow;
tv->tv_sec -= secs;
v -= (pa_usec_t) secs * PA_USEC_PER_SEC;
if (tv->tv_usec >= (suseconds_t) v)
tv->tv_usec -= (suseconds_t) v;
else {
if (PA_UNLIKELY(tv->tv_sec <= 0))
goto underflow;
tv->tv_sec --;
tv->tv_usec += (suseconds_t) (PA_USEC_PER_SEC - v);
}
return tv;
underflow:
tv->tv_sec = 0;
tv->tv_usec = 0;
return tv;
}
SPA_EXPORT
struct timeval* pa_timeval_store(struct timeval *tv, pa_usec_t v) {
pa_assert(tv);
if (PA_UNLIKELY(v == PA_USEC_INVALID)) {
tv->tv_sec = PA_INT_TYPE_MAX(time_t);
tv->tv_usec = (suseconds_t) (PA_USEC_PER_SEC-1);
return tv;
}
tv->tv_sec = (time_t) (v / PA_USEC_PER_SEC);
tv->tv_usec = (suseconds_t) (v % PA_USEC_PER_SEC);
return tv;
}
SPA_EXPORT
pa_usec_t pa_timeval_load(const struct timeval *tv) {
if (PA_UNLIKELY(!tv))
return PA_USEC_INVALID;
return
(pa_usec_t) tv->tv_sec * PA_USEC_PER_SEC +
(pa_usec_t) tv->tv_usec;
}

View file

@ -0,0 +1,295 @@
/***
This file is part of PulseAudio.
Copyright 2006 Lennart Poettering
Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
***/
/* This file is based on the GLIB utf8 validation functions. The
* original license text follows. */
/* gutf8.c - Operations on UTF-8 strings.
*
* Copyright (C) 1999 Tom Tromey
* Copyright (C) 2000 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#ifdef HAVE_ICONV
#include <iconv.h>
#endif
#include <pulse/utf8.h>
#include <pulse/xmalloc.h>
#include "internal.h"
#define FILTER_CHAR '_'
static inline bool is_unicode_valid(uint32_t ch) {
if (ch >= 0x110000) /* End of unicode space */
return false;
if ((ch & 0xFFFFF800) == 0xD800) /* Reserved area for UTF-16 */
return false;
if ((ch >= 0xFDD0) && (ch <= 0xFDEF)) /* Reserved */
return false;
if ((ch & 0xFFFE) == 0xFFFE) /* BOM (Byte Order Mark) */
return false;
return true;
}
static inline bool is_continuation_char(uint8_t ch) {
if ((ch & 0xc0) != 0x80) /* 10xxxxxx */
return false;
return true;
}
static inline void merge_continuation_char(uint32_t *u_ch, uint8_t ch) {
*u_ch <<= 6;
*u_ch |= ch & 0x3f;
}
static char* utf8_validate(const char *str, char *output) {
uint32_t val = 0;
uint32_t min = 0;
const uint8_t *p, *last;
int size;
uint8_t *o;
pa_assert(str);
o = (uint8_t*) output;
for (p = (const uint8_t*) str; *p; p++) {
if (*p < 128) {
if (o)
*o = *p;
} else {
last = p;
if ((*p & 0xe0) == 0xc0) { /* 110xxxxx two-char seq. */
size = 2;
min = 128;
val = (uint32_t) (*p & 0x1e);
goto ONE_REMAINING;
} else if ((*p & 0xf0) == 0xe0) { /* 1110xxxx three-char seq.*/
size = 3;
min = (1 << 11);
val = (uint32_t) (*p & 0x0f);
goto TWO_REMAINING;
} else if ((*p & 0xf8) == 0xf0) { /* 11110xxx four-char seq */
size = 4;
min = (1 << 16);
val = (uint32_t) (*p & 0x07);
} else
goto error;
p++;
if (!is_continuation_char(*p))
goto error;
merge_continuation_char(&val, *p);
TWO_REMAINING:
p++;
if (!is_continuation_char(*p))
goto error;
merge_continuation_char(&val, *p);
ONE_REMAINING:
p++;
if (!is_continuation_char(*p))
goto error;
merge_continuation_char(&val, *p);
if (val < min)
goto error;
if (!is_unicode_valid(val))
goto error;
if (o) {
memcpy(o, last, (size_t) size);
o += size;
}
continue;
error:
if (o) {
*o = FILTER_CHAR;
p = last; /* We retry at the next character */
} else
goto failure;
}
if (o)
o++;
}
if (o) {
*o = '\0';
return output;
}
return (char*) str;
failure:
return NULL;
}
SPA_EXPORT
char* pa_utf8_valid (const char *str) {
return utf8_validate(str, NULL);
}
SPA_EXPORT
char* pa_utf8_filter (const char *str) {
char *new_str;
pa_assert(str);
new_str = pa_xmalloc(strlen(str) + 1);
return utf8_validate(str, new_str);
}
#ifdef HAVE_ICONV
static char* iconv_simple(const char *str, const char *to, const char *from) {
char *new_str;
size_t len, inlen;
iconv_t cd;
ICONV_CONST char *inbuf;
char *outbuf;
size_t res, inbytes, outbytes;
pa_assert(str);
pa_assert(to);
pa_assert(from);
cd = iconv_open(to, from);
if (cd == (iconv_t)-1)
return NULL;
inlen = len = strlen(str) + 1;
new_str = pa_xmalloc(len);
for (;;) {
inbuf = (ICONV_CONST char*) str; /* Brain dead prototype for iconv() */
inbytes = inlen;
outbuf = new_str;
outbytes = len;
res = iconv(cd, &inbuf, &inbytes, &outbuf, &outbytes);
if (res != (size_t)-1)
break;
if (errno != E2BIG) {
pa_xfree(new_str);
new_str = NULL;
break;
}
pa_assert(inbytes != 0);
len += inbytes;
new_str = pa_xrealloc(new_str, len);
}
iconv_close(cd);
return new_str;
}
SPA_EXPORT
char* pa_utf8_to_locale (const char *str) {
return iconv_simple(str, "", "UTF-8");
}
SPA_EXPORT
char* pa_locale_to_utf8 (const char *str) {
return iconv_simple(str, "UTF-8", "");
}
#else
SPA_EXPORT
char* pa_utf8_to_locale (const char *str) {
pa_assert(str);
return pa_ascii_filter(str);
}
SPA_EXPORT
char* pa_locale_to_utf8 (const char *str) {
pa_assert(str);
if (pa_utf8_valid(str))
return pa_xstrdup(str);
return NULL;
}
#endif
SPA_EXPORT
char *pa_ascii_valid(const char *str) {
const char *p;
pa_assert(str);
for (p = str; *p; p++)
if ((unsigned char) *p >= 128)
return NULL;
return (char*) str;
}
SPA_EXPORT
char *pa_ascii_filter(const char *str) {
char *r, *s, *d;
pa_assert(str);
r = pa_xstrdup(str);
for (s = r, d = r; *s; s++)
if ((unsigned char) *s < 128)
*(d++) = *s;
*d = 0;
return r;
}

View file

@ -0,0 +1,84 @@
/* PipeWire
* Copyright (C) 2018 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <time.h>
#include <pipewire/log.h>
#include <pipewire/pipewire.h>
#include <pulse/util.h>
#define PA_PATH_SEP_CHAR '/'
SPA_EXPORT
char *pa_get_user_name(char *s, size_t l)
{
return strncpy(s, pw_get_user_name(), l);
}
SPA_EXPORT
char *pa_get_host_name(char *s, size_t l)
{
return strncpy(s, pw_get_host_name(), l);
}
SPA_EXPORT
char *pa_get_fqdn(char *s, size_t l)
{
return strncpy(s, pw_get_host_name(), l);
}
SPA_EXPORT
char *pa_get_home_dir(char *s, size_t l)
{
pw_log_warn("Not Implemented");
return NULL;
}
SPA_EXPORT
char *pa_get_binary_name(char *s, size_t l)
{
return strncpy(s, pw_get_prgname(), l);
}
SPA_EXPORT
char *pa_path_get_filename(const char *p)
{
char *fn;
if (!p)
return NULL;
if ((fn = strrchr(p, PA_PATH_SEP_CHAR)))
return fn+1;
return (char*) p;
}
SPA_EXPORT
int pa_msleep(unsigned long t)
{
struct timespec ts;
ts.tv_sec = (time_t) (t / SPA_MSEC_PER_SEC);
ts.tv_nsec = (long) ((t % SPA_MSEC_PER_SEC) * SPA_NSEC_PER_MSEC);
return nanosleep(&ts, NULL);
}

View file

@ -0,0 +1,29 @@
/* PipeWire
* Copyright (C) 2018 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <spa/utils/defs.h>
#include <pulse/version.h>
SPA_EXPORT
const char* pa_get_library_version(void)
{
return pa_get_headers_version();
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,122 @@
/* PipeWire
* Copyright (C) 2018 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <unistd.h>
#include <errno.h>
#include <spa/utils/defs.h>
#include <pulse/xmalloc.h>
#define MAX_ALLOC_SIZE (1024*1024*96) /* 96MB */
static void oom(void) {
static const char e[] = "Not enough memory\n";
write(STDERR_FILENO, e, sizeof(e)-1);
#ifdef SIGQUIT
raise(SIGQUIT);
#endif
_exit(1);
}
SPA_EXPORT
void* pa_xmalloc(size_t l)
{
void *p;
spa_assert(l > 0);
spa_assert(l < MAX_ALLOC_SIZE);
if (!(p = malloc(l)))
oom();
return p;
}
SPA_EXPORT
void *pa_xmalloc0(size_t l)
{
void *p;
spa_assert(l > 0);
spa_assert(l < MAX_ALLOC_SIZE);
if (!(p = calloc(1, l)))
oom();
return p;
}
SPA_EXPORT
void *pa_xrealloc(void *ptr, size_t size)
{
void *p;
spa_assert(size > 0);
spa_assert(size < MAX_ALLOC_SIZE);
if (!(p = realloc(ptr, size)))
oom();
return p;
}
SPA_EXPORT
void pa_xfree(void *p)
{
int saved_errno;
if (!p)
return;
saved_errno = errno;
free(p);
errno = saved_errno;
}
SPA_EXPORT
char *pa_xstrdup(const char *s)
{
if (!s)
return NULL;
return pa_xmemdup(s, strlen(s)+1);
}
SPA_EXPORT
char *pa_xstrndup(const char *s, size_t l)
{
char *e, *r;
if (!s)
return NULL;
if ((e = memchr(s, 0, l)))
return pa_xmemdup(s, (size_t) (e-s+1));
r = pa_xmalloc(l+1);
memcpy(r, s, l);
r[l] = 0;
return r;
}
SPA_EXPORT
void* pa_xmemdup(const void *p, size_t l)
{
if (!p)
return NULL;
else {
char *r = pa_xmalloc(l);
memcpy(r, p, l);
return r;
}
}