More hacking

Add connection message for PORT_COMMAND
Add rtkit support to ask for realtime priority
work on stream states and improve negotiation
Rework of port linking works, keep separate state for realtime threads
and use message passing to update the state.
Don't try to link nodes that are removed.
Open the device in the ALSA monitor to detect source or sink
Implement send_command as async methods on the plugins, use async
replies to sync start and stop.
Work on alsa sink.
Implement async PAUSE/START on v4l2 src. move the STREAMON/OFF calls to
the mainloop because they have high latency, add the poll descriptors
from the data loop.
This commit is contained in:
Wim Taymans 2016-10-28 16:56:33 +02:00
parent 984375c0b2
commit 3f4ccaaea2
32 changed files with 1335 additions and 545 deletions

View file

@ -151,6 +151,15 @@ connection_parse_node_command (PinosConnection *conn, PinosControlCmdNodeCommand
cmd->command = SPA_MEMBER (p, SPA_PTR_TO_INT (cmd->command), SpaNodeCommand);
}
static void
connection_parse_port_command (PinosConnection *conn, PinosControlCmdPortCommand *cmd)
{
void *p = conn->in.data;
memcpy (cmd, p, sizeof (PinosControlCmdPortCommand));
if (cmd->command)
cmd->command = SPA_MEMBER (p, SPA_PTR_TO_INT (cmd->command), SpaNodeCommand);
}
static void *
connection_ensure_size (PinosConnection *conn, ConnectionBuffer *buf, size_t size)
{
@ -363,6 +372,27 @@ connection_add_node_command (PinosConnection *conn, PinosControlCmdNodeCommand *
memcpy (p, cm->command, cm->command->size);
}
static void
connection_add_port_command (PinosConnection *conn, PinosControlCmdPortCommand *cm)
{
size_t len;
void *p;
PinosControlCmdPortCommand *d;
/* calculate length */
len = sizeof (PinosControlCmdPortCommand);
len += cm->command->size;
p = connection_add_cmd (conn, PINOS_CONTROL_CMD_PORT_COMMAND, len);
memcpy (p, cm, sizeof (PinosControlCmdPortCommand));
d = p;
p = SPA_MEMBER (d, sizeof (PinosControlCmdPortCommand), void);
d->command = SPA_INT_TO_PTR (SPA_PTRDIFF (p, d));
memcpy (p, cm->command, cm->command->size);
}
static gboolean
refill_buffer (PinosConnection *conn, ConnectionBuffer *buf)
{
@ -595,6 +625,10 @@ pinos_connection_parse_cmd (PinosConnection *conn,
connection_parse_node_command (conn, command);
break;
case PINOS_CONTROL_CMD_PORT_COMMAND:
connection_parse_port_command (conn, command);
break;
case PINOS_CONTROL_CMD_INVALID:
return FALSE;
}
@ -742,6 +776,10 @@ pinos_connection_add_cmd (PinosConnection *conn,
connection_add_node_command (conn, command);
break;
case PINOS_CONTROL_CMD_PORT_COMMAND:
connection_add_port_command (conn, command);
break;
case PINOS_CONTROL_CMD_INVALID:
return FALSE;
}

View file

@ -49,6 +49,7 @@ typedef enum {
PINOS_CONTROL_CMD_SET_PROPERTY = 35,
PINOS_CONTROL_CMD_NODE_COMMAND = 36,
PINOS_CONTROL_CMD_PORT_COMMAND = 37,
/* both */
PINOS_CONTROL_CMD_ADD_MEM = 64,
@ -132,6 +133,12 @@ typedef struct {
SpaNodeCommand *command;
} PinosControlCmdNodeCommand;
/* PINOS_CONTROL_CMD_PORT_COMMAND */
typedef struct {
uint32_t port_id;
SpaNodeCommand *command;
} PinosControlCmdPortCommand;
/* PINOS_CONTROL_CMD_ADD_MEM */
typedef struct {
SpaDirection direction;

View file

@ -23,6 +23,8 @@
#include <gio/gio.h>
#include <glib-object.h>
#include <spa/include/spa/defs.h>
G_BEGIN_DECLS
/**
@ -59,9 +61,9 @@ const gchar * pinos_node_state_as_string (PinosNodeState state);
* The direction of a port
*/
typedef enum {
PINOS_DIRECTION_INVALID = 0,
PINOS_DIRECTION_INPUT = 1,
PINOS_DIRECTION_OUTPUT = 2
PINOS_DIRECTION_INVALID = SPA_DIRECTION_INVALID,
PINOS_DIRECTION_INPUT = SPA_DIRECTION_INPUT,
PINOS_DIRECTION_OUTPUT = SPA_DIRECTION_OUTPUT
} PinosDirection;
const gchar * pinos_direction_as_string (PinosDirection direction);

View file

@ -4,8 +4,9 @@ pinos_headers = [
'introspect.h',
'pinos.h',
'properties.h',
'stream.h',
'ringbuffer.h',
'rtkit.h',
'stream.h',
'subscribe.h',
'thread-mainloop.h',
]
@ -20,6 +21,7 @@ pinos_sources = [
'stream.c',
'pinos.c',
'ringbuffer.c',
'rtkit.c',
'subscribe.c',
'thread-mainloop.c',
gdbus_target,

104
pinos/client/rtkit.c Normal file
View file

@ -0,0 +1,104 @@
/* Pinos
* Copyright (C) 2015 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 <sys/syscall.h>
#include "rtkit.h"
static pid_t _gettid(void) {
return (pid_t) syscall(SYS_gettid);
}
gboolean
pinos_rtkit_make_realtime (GDBusConnection *system_bus,
pid_t thread,
gint priority,
GError **error)
{
GVariant *v;
if (thread == 0)
thread = _gettid();
v = g_dbus_connection_call_sync (system_bus,
RTKIT_SERVICE_NAME,
RTKIT_OBJECT_PATH,
"org.freedesktop.RealtimeKit1",
"MakeThreadRealtime",
g_variant_new ("(tu)",
(guint64) thread,
(guint32) priority),
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
error);
if (v)
g_variant_unref (v);
return v != NULL;
}
gboolean
pinos_rtkit_make_high_priority (GDBusConnection *system_bus,
pid_t thread,
gint nice_level,
GError **error)
{
GVariant *v;
if (thread == 0)
thread = _gettid();
v = g_dbus_connection_call_sync (system_bus,
RTKIT_SERVICE_NAME,
RTKIT_OBJECT_PATH,
"org.freedesktop.RealtimeKit1",
"MakeThreadHighPriority",
g_variant_new ("(tu)",
(guint64) thread,
(guint32) nice_level),
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
error);
if (v)
g_variant_unref (v);
return v != NULL;
}
int pinos_rtkit_get_max_realtime_priority (GDBusConnection *system_bus)
{
return 0;
}
int pinos_rtkit_get_min_nice_level (GDBusConnection *system_bus, int* min_nice_level)
{
return 0;
}
/* Return the maximum value of RLIMIT_RTTIME to set before attempting a
* realtime request. A negative value is an errno style error code.
*/
long long rtkit_get_rttime_usec_max (GDBusConnection *system_bus)
{
return 0;
}

68
pinos/client/rtkit.h Normal file
View file

@ -0,0 +1,68 @@
/* Pinos
* Copyright (C) 2015 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 __PINOS_RTKIT_H__
#define __PINOS_RTKIT_H__
#include <gio/gio.h>
#include <glib-object.h>
G_BEGIN_DECLS
#define RTKIT_SERVICE_NAME "org.freedesktop.RealtimeKit1"
#define RTKIT_OBJECT_PATH "/org/freedesktop/RealtimeKit1"
/* This is mostly equivalent to sched_setparam(thread, SCHED_RR, {
* .sched_priority = priority }). 'thread' needs to be a kernel thread
* id as returned by gettid(), not a pthread_t! If 'thread' is 0 the
* current thread is used.
*/
gboolean pinos_rtkit_make_realtime (GDBusConnection *system_bus,
pid_t thread,
gint priority,
GError **error);
/* This is mostly equivalent to setpriority(PRIO_PROCESS, thread,
* nice_level). 'thread' needs to be a kernel thread id as returned by
* gettid(), not a pthread_t! If 'thread' is 0 the current thread is
* used. */
gboolean pinos_rtkit_make_high_priority (GDBusConnection *system_bus,
pid_t thread,
gint nice_level,
GError **error);
/* Return the maximum value of realtime priority available. Realtime requests
* above this value will fail. A negative value is an errno style error code.
*/
int pinos_rtkit_get_max_realtime_priority (GDBusConnection *system_bus);
/* Retreive the minimum value of nice level available. High prio requests
* below this value will fail. The returned value is a negative errno
* style error code, or 0 on success.*/
int pinos_rtkit_get_min_nice_level (GDBusConnection *system_bus, int* min_nice_level);
/* Return the maximum value of RLIMIT_RTTIME to set before attempting a
* realtime request. A negative value is an errno style error code.
*/
long long rtkit_get_rttime_usec_max (GDBusConnection *system_bus);
G_END_DECLS
#endif /* __PINOS_RTKIT_H__ */

View file

@ -874,7 +874,7 @@ handle_node_command (PinosStream *stream,
break;
case SPA_NODE_COMMAND_PAUSE:
{
g_debug ("stream %p: pause", stream);
g_debug ("stream %p: pause %d", stream, seq);
add_state_change (stream, SPA_NODE_STATE_PAUSED);
add_async_complete (stream, seq, SPA_RESULT_OK);
@ -882,12 +882,12 @@ handle_node_command (PinosStream *stream,
if (!pinos_connection_flush (priv->conn))
g_warning ("stream %p: error writing connection", stream);
stream_set_state (stream, PINOS_STREAM_STATE_READY, NULL);
stream_set_state (stream, PINOS_STREAM_STATE_PAUSED, NULL);
break;
}
case SPA_NODE_COMMAND_START:
{
g_debug ("stream %p: start", stream);
g_debug ("stream %p: start %d", stream, seq);
add_state_change (stream, SPA_NODE_STATE_STREAMING);
add_async_complete (stream, seq, SPA_RESULT_OK);
@ -911,7 +911,6 @@ handle_node_command (PinosStream *stream,
g_warning ("stream %p: error writing connection", stream);
break;
}
case SPA_NODE_COMMAND_CLOCK_UPDATE:
{
SpaNodeCommandClockUpdate *cu = (SpaNodeCommandClockUpdate *) command;
@ -945,7 +944,7 @@ parse_connection (PinosStream *stream)
case PINOS_CONTROL_CMD_PORT_STATUS_CHANGE:
case PINOS_CONTROL_CMD_NODE_STATE_CHANGE:
case PINOS_CONTROL_CMD_PROCESS_BUFFER:
g_warning ("got unexpected connection %d", cmd);
g_warning ("got unexpected command %d", cmd);
break;
case PINOS_CONTROL_CMD_ADD_PORT:
@ -968,6 +967,7 @@ parse_connection (PinosStream *stream)
priv->pending_seq = p.seq;
g_object_notify (G_OBJECT (stream), "format");
stream_set_state (stream, PINOS_STREAM_STATE_READY, NULL);
break;
}
case PINOS_CONTROL_CMD_SET_PROPERTY:
@ -1114,6 +1114,11 @@ parse_connection (PinosStream *stream)
if (!pinos_connection_flush (conn))
g_warning ("stream %p: error writing connection", stream);
if (p.n_buffers)
stream_set_state (stream, PINOS_STREAM_STATE_PAUSED, NULL);
else
stream_set_state (stream, PINOS_STREAM_STATE_READY, NULL);
break;
}
case PINOS_CONTROL_CMD_NODE_EVENT:
@ -1136,7 +1141,15 @@ parse_connection (PinosStream *stream)
handle_node_command (stream, p.seq, p.command);
break;
}
case PINOS_CONTROL_CMD_PORT_COMMAND:
{
PinosControlCmdPortCommand p;
if (!pinos_connection_parse_cmd (conn, &p))
break;
break;
}
case PINOS_CONTROL_CMD_INVALID:
g_warning ("unhandled command %d", cmd);
break;
@ -1167,7 +1180,7 @@ parse_rtconnection (PinosStream *stream)
case PINOS_CONTROL_CMD_ADD_MEM:
case PINOS_CONTROL_CMD_USE_BUFFERS:
case PINOS_CONTROL_CMD_NODE_COMMAND:
g_warning ("got unexpected connection %d", cmd);
g_warning ("got unexpected command %d", cmd);
break;
case PINOS_CONTROL_CMD_PROCESS_BUFFER:
@ -1202,6 +1215,10 @@ parse_rtconnection (PinosStream *stream)
handle_rtnode_event (stream, p.event);
break;
}
case PINOS_CONTROL_CMD_PORT_COMMAND:
{
break;
}
}
}
@ -1344,7 +1361,7 @@ on_node_proxy (GObject *source_object,
do_node_init (stream);
stream_set_state (stream, PINOS_STREAM_STATE_READY, NULL);
stream_set_state (stream, PINOS_STREAM_STATE_CONFIGURE, NULL);
g_object_unref (stream);
return;
@ -1664,9 +1681,7 @@ pinos_stream_start (PinosStream *stream)
g_return_val_if_fail (PINOS_IS_STREAM (stream), FALSE);
priv = stream->priv;
g_return_val_if_fail (priv->state == PINOS_STREAM_STATE_READY, FALSE);
stream_set_state (stream, PINOS_STREAM_STATE_STARTING, NULL);
//g_return_val_if_fail (priv->state == PINOS_STREAM_STATE_PAUSED, FALSE);
g_main_context_invoke (priv->context->priv->context,
(GSourceFunc) do_start,
@ -1709,8 +1724,8 @@ pinos_stream_stop (PinosStream *stream)
static void
on_node_removed (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
GAsyncResult *res,
gpointer user_data)
{
PinosStream *stream = user_data;
PinosStreamPrivate *priv = stream->priv;

View file

@ -47,9 +47,10 @@ typedef enum {
PINOS_STREAM_STATE_ERROR = -1,
PINOS_STREAM_STATE_UNCONNECTED = 0,
PINOS_STREAM_STATE_CONNECTING = 1,
PINOS_STREAM_STATE_READY = 2,
PINOS_STREAM_STATE_STARTING = 3,
PINOS_STREAM_STATE_STREAMING = 4
PINOS_STREAM_STATE_CONFIGURE = 2,
PINOS_STREAM_STATE_READY = 3,
PINOS_STREAM_STATE_PAUSED = 4,
PINOS_STREAM_STATE_STREAMING = 5
} PinosStreamState;
const gchar * pinos_stream_state_as_string (PinosStreamState state);