2016-09-20 11:20:43 +02:00
|
|
|
/* Spa ALSA Sink
|
|
|
|
|
* Copyright (C) 2016 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 __SPA_ALSA_UTILS_H__
|
|
|
|
|
#define __SPA_ALSA_UTILS_H__
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
|
|
#include <asoundlib.h>
|
|
|
|
|
|
2017-03-24 11:40:58 +01:00
|
|
|
#include <spa/type-map.h>
|
2017-03-22 10:04:24 +01:00
|
|
|
#include <spa/clock.h>
|
2016-10-07 13:14:32 +02:00
|
|
|
#include <spa/log.h>
|
2016-11-09 12:57:51 +01:00
|
|
|
#include <spa/list.h>
|
2016-09-20 11:20:43 +02:00
|
|
|
#include <spa/node.h>
|
2017-05-22 13:06:18 +02:00
|
|
|
#include <spa/param-alloc.h>
|
2016-11-18 17:46:01 +01:00
|
|
|
#include <spa/loop.h>
|
2016-11-03 19:41:53 +01:00
|
|
|
#include <spa/ringbuffer.h>
|
2017-03-21 16:50:44 +01:00
|
|
|
#include <spa/audio/format-utils.h>
|
2017-02-24 09:28:18 +01:00
|
|
|
#include <spa/format-builder.h>
|
2016-09-20 11:20:43 +02:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
struct props
|
|
|
|
|
{
|
2016-09-20 11:20:43 +02:00
|
|
|
char device[64];
|
|
|
|
|
char device_name[128];
|
|
|
|
|
char card_name[128];
|
2017-04-03 16:54:44 +02:00
|
|
|
uint32_t min_latency;
|
2017-05-25 13:28:15 +02:00
|
|
|
};
|
2016-09-20 11:20:43 +02:00
|
|
|
|
2016-10-28 16:56:33 +02:00
|
|
|
#define MAX_BUFFERS 64
|
2016-09-29 18:18:59 +02:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
struct buffer
|
|
|
|
|
{
|
2017-05-25 13:28:15 +02:00
|
|
|
struct spa_buffer *outbuf;
|
|
|
|
|
struct spa_meta_header *h;
|
|
|
|
|
struct spa_meta_ringbuffer *rb;
|
2016-09-20 11:20:43 +02:00
|
|
|
bool outstanding;
|
2017-05-25 13:28:15 +02:00
|
|
|
struct spa_list link;
|
2016-09-20 11:20:43 +02:00
|
|
|
};
|
|
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
struct type
|
|
|
|
|
{
|
2016-10-07 13:14:32 +02:00
|
|
|
uint32_t node;
|
|
|
|
|
uint32_t clock;
|
2017-03-22 10:04:24 +01:00
|
|
|
uint32_t format;
|
2017-03-23 12:38:00 +01:00
|
|
|
uint32_t props;
|
|
|
|
|
uint32_t prop_device;
|
|
|
|
|
uint32_t prop_device_name;
|
|
|
|
|
uint32_t prop_card_name;
|
2017-04-03 16:54:44 +02:00
|
|
|
uint32_t prop_min_latency;
|
2017-05-25 13:28:15 +02:00
|
|
|
struct spa_type_meta meta;
|
|
|
|
|
struct spa_type_data data;
|
|
|
|
|
struct spa_type_media_type media_type;
|
|
|
|
|
struct spa_type_media_subtype media_subtype;
|
|
|
|
|
struct spa_type_media_subtype_audio media_subtype_audio;
|
|
|
|
|
struct spa_type_format_audio format_audio;
|
|
|
|
|
struct spa_type_audio_format audio_format;
|
|
|
|
|
struct spa_type_event_node event_node;
|
|
|
|
|
struct spa_type_command_node command_node;
|
|
|
|
|
struct spa_type_param_alloc_buffers param_alloc_buffers;
|
|
|
|
|
struct spa_type_param_alloc_meta_enable param_alloc_meta_enable;
|
|
|
|
|
};
|
2016-10-07 13:14:32 +02:00
|
|
|
|
2017-03-23 12:38:00 +01:00
|
|
|
static inline void
|
2017-05-25 13:28:15 +02:00
|
|
|
init_type (struct type *type, struct spa_type_map *map)
|
2017-03-23 12:38:00 +01:00
|
|
|
{
|
2017-03-24 11:40:58 +01:00
|
|
|
type->node = spa_type_map_get_id (map, SPA_TYPE__Node);
|
|
|
|
|
type->clock = spa_type_map_get_id (map, SPA_TYPE__Clock);
|
|
|
|
|
type->format = spa_type_map_get_id (map, SPA_TYPE__Format);
|
|
|
|
|
type->props = spa_type_map_get_id (map, SPA_TYPE__Props);
|
|
|
|
|
type->prop_device = spa_type_map_get_id (map, SPA_TYPE_PROPS__device);
|
|
|
|
|
type->prop_device_name = spa_type_map_get_id (map, SPA_TYPE_PROPS__deviceName);
|
|
|
|
|
type->prop_card_name = spa_type_map_get_id (map, SPA_TYPE_PROPS__cardName);
|
2017-04-03 16:54:44 +02:00
|
|
|
type->prop_min_latency = spa_type_map_get_id (map, SPA_TYPE_PROPS__minLatency);
|
2017-03-24 11:40:58 +01:00
|
|
|
|
2017-04-26 18:42:50 +02:00
|
|
|
spa_type_meta_map (map, &type->meta);
|
|
|
|
|
spa_type_data_map (map, &type->data);
|
2017-03-24 11:40:58 +01:00
|
|
|
spa_type_media_type_map (map, &type->media_type);
|
|
|
|
|
spa_type_media_subtype_map (map, &type->media_subtype);
|
|
|
|
|
spa_type_media_subtype_audio_map (map, &type->media_subtype_audio);
|
2017-03-24 13:11:57 +01:00
|
|
|
spa_type_format_audio_map (map, &type->format_audio);
|
2017-03-24 11:40:58 +01:00
|
|
|
spa_type_audio_format_map (map, &type->audio_format);
|
|
|
|
|
spa_type_event_node_map (map, &type->event_node);
|
|
|
|
|
spa_type_command_node_map (map, &type->command_node);
|
2017-05-22 13:06:18 +02:00
|
|
|
spa_type_param_alloc_buffers_map (map, &type->param_alloc_buffers);
|
|
|
|
|
spa_type_param_alloc_meta_enable_map (map, &type->param_alloc_meta_enable);
|
2017-05-26 08:05:01 +02:00
|
|
|
} struct state
|
2017-03-23 12:38:00 +01:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
{
|
2017-05-25 13:28:15 +02:00
|
|
|
struct spa_handle handle;
|
|
|
|
|
struct spa_node node;
|
|
|
|
|
struct spa_clock clock;
|
2016-09-20 11:20:43 +02:00
|
|
|
|
2016-10-25 11:15:15 +02:00
|
|
|
uint32_t seq;
|
|
|
|
|
|
2017-05-25 13:28:15 +02:00
|
|
|
struct type type;
|
|
|
|
|
struct spa_type_map *map;
|
|
|
|
|
struct spa_log *log;
|
|
|
|
|
struct spa_loop *main_loop;
|
|
|
|
|
struct spa_loop *data_loop;
|
2016-10-07 13:14:32 +02:00
|
|
|
|
2016-09-20 11:20:43 +02:00
|
|
|
snd_pcm_stream_t stream;
|
|
|
|
|
snd_output_t *output;
|
|
|
|
|
|
2017-05-25 13:28:15 +02:00
|
|
|
struct spa_node_callbacks callbacks;
|
2016-09-20 11:20:43 +02:00
|
|
|
void *user_data;
|
|
|
|
|
|
2017-02-28 10:48:53 +01:00
|
|
|
uint8_t props_buffer[1024];
|
2017-05-25 13:28:15 +02:00
|
|
|
struct props props;
|
2016-09-20 11:20:43 +02:00
|
|
|
|
|
|
|
|
bool opened;
|
|
|
|
|
snd_pcm_t *hndl;
|
|
|
|
|
|
|
|
|
|
bool have_format;
|
2017-05-25 13:28:15 +02:00
|
|
|
struct spa_audio_info current_format;
|
2017-02-24 09:28:18 +01:00
|
|
|
uint8_t format_buffer[1024];
|
|
|
|
|
|
2017-03-28 19:29:08 +02:00
|
|
|
snd_pcm_uframes_t buffer_frames;
|
|
|
|
|
snd_pcm_uframes_t period_frames;
|
2016-09-20 11:20:43 +02:00
|
|
|
snd_pcm_format_t format;
|
|
|
|
|
int rate;
|
|
|
|
|
int channels;
|
|
|
|
|
size_t frame_size;
|
|
|
|
|
|
2017-05-25 13:28:15 +02:00
|
|
|
struct spa_port_info info;
|
2017-05-22 13:06:18 +02:00
|
|
|
uint32_t params[3];
|
2017-03-17 11:58:09 +01:00
|
|
|
uint8_t params_buffer[1024];
|
2017-05-25 13:28:15 +02:00
|
|
|
struct spa_port_io *io;
|
2016-09-20 11:20:43 +02:00
|
|
|
|
2017-05-25 13:28:15 +02:00
|
|
|
struct buffer buffers[MAX_BUFFERS];
|
2016-09-20 11:20:43 +02:00
|
|
|
unsigned int n_buffers;
|
|
|
|
|
|
2017-05-25 13:28:15 +02:00
|
|
|
struct spa_list free;
|
|
|
|
|
struct spa_list ready;
|
2016-10-28 16:56:33 +02:00
|
|
|
size_t ready_offset;
|
2016-09-20 11:20:43 +02:00
|
|
|
|
2016-10-25 11:15:15 +02:00
|
|
|
bool started;
|
2017-05-25 13:28:15 +02:00
|
|
|
struct spa_source source;
|
2017-03-28 19:29:08 +02:00
|
|
|
int timerfd;
|
|
|
|
|
bool alsa_started;
|
|
|
|
|
int threshold;
|
2016-09-20 11:20:43 +02:00
|
|
|
|
2016-09-20 19:52:05 +02:00
|
|
|
int64_t sample_count;
|
2016-09-20 11:20:43 +02:00
|
|
|
int64_t last_ticks;
|
|
|
|
|
int64_t last_monotonic;
|
|
|
|
|
};
|
|
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
#define PROP(f,key,type,...) \
|
|
|
|
|
SPA_POD_PROP (f,key,0,type,1,__VA_ARGS__)
|
|
|
|
|
#define PROP_MM(f,key,type,...) \
|
|
|
|
|
SPA_POD_PROP (f,key,SPA_POD_PROP_RANGE_MIN_MAX,type,3,__VA_ARGS__)
|
|
|
|
|
#define PROP_U_MM(f,key,type,...) \
|
|
|
|
|
SPA_POD_PROP (f,key,SPA_POD_PROP_FLAG_UNSET | \
|
|
|
|
|
SPA_POD_PROP_RANGE_MIN_MAX,type,3,__VA_ARGS__)
|
|
|
|
|
#define PROP_EN(f,key,type,n,...) \
|
|
|
|
|
SPA_POD_PROP (f,key,SPA_POD_PROP_RANGE_ENUM,type,n,__VA_ARGS__)
|
|
|
|
|
#define PROP_U_EN(f,key,type,n,...) \
|
|
|
|
|
SPA_POD_PROP (f,key,SPA_POD_PROP_FLAG_UNSET | \
|
|
|
|
|
SPA_POD_PROP_RANGE_ENUM,type,n,__VA_ARGS__)
|
2017-04-04 19:44:00 +02:00
|
|
|
|
2017-05-25 13:28:15 +02:00
|
|
|
int
|
2017-05-26 08:05:01 +02:00
|
|
|
spa_alsa_enum_format (struct state *state,
|
|
|
|
|
struct spa_format **format,
|
|
|
|
|
const struct spa_format *filter, uint32_t index);
|
2017-04-04 19:44:00 +02:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
int spa_alsa_set_format (struct state *state, struct spa_audio_info *info,
|
|
|
|
|
uint32_t flags);
|
2016-09-20 11:20:43 +02:00
|
|
|
|
2017-05-25 13:28:15 +02:00
|
|
|
int spa_alsa_start (struct state *state, bool xrun_recover);
|
|
|
|
|
int spa_alsa_pause (struct state *state, bool xrun_recover);
|
|
|
|
|
int spa_alsa_close (struct state *state);
|
2016-09-20 11:20:43 +02:00
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
2017-05-26 08:05:01 +02:00
|
|
|
} /* extern "C" */
|
2016-09-20 11:20:43 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#endif /* __SPA_ALSA_UTILS_H__ */
|