summaryrefslogtreecommitdiff
path: root/cras/src/server/cras_rclient.c
blob: d2646e7548bcb1134e4df512a9b320d082491bba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <assert.h>
#include <stdlib.h>
#include <syslog.h>

#include "audio_thread.h"
#include "cras_apm_list.h"
#include "cras_bt_log.h"
#include "cras_capture_rclient.h"
#include "cras_config.h"
#include "cras_control_rclient.h"
#include "cras_dsp.h"
#include "cras_iodev.h"
#include "cras_iodev_list.h"
#include "cras_messages.h"
#include "cras_observer.h"
#include "cras_playback_rclient.h"
#include "cras_rclient.h"
#include "cras_rstream.h"
#include "cras_server_metrics.h"
#include "cras_system_state.h"
#include "cras_types.h"
#include "cras_unified_rclient.h"
#include "cras_util.h"
#include "stream_list.h"
#include "utlist.h"

/* Removes all streams that the client owns and destroys it. */
void cras_rclient_destroy(struct cras_rclient *client)
{
	client->ops->destroy(client);
}

/* Entry point for handling a message from the client.  Called from the main
 * server context. */
int cras_rclient_buffer_from_client(struct cras_rclient *client,
				    const uint8_t *buf, size_t buf_len,
				    int *fds, int num_fds)
{
	struct cras_server_message *msg = (struct cras_server_message *)buf;

	if (buf_len < sizeof(*msg))
		return -EINVAL;
	if (msg->length != buf_len)
		return -EINVAL;
	return client->ops->handle_message_from_client(client, msg, fds,
						       num_fds);
}

/* Sends a message to the client. */
int cras_rclient_send_message(const struct cras_rclient *client,
			      const struct cras_client_message *msg, int *fds,
			      unsigned int num_fds)
{
	return client->ops->send_message_to_client(client, msg, fds, num_fds);
}

static void cras_rclient_set_client_type(struct cras_rclient *client,
					 enum CRAS_CLIENT_TYPE client_type)
{
	client->client_type = client_type;
}

struct cras_rclient *cras_rclient_create(int fd, size_t id,
					 enum CRAS_CONNECTION_TYPE conn_type)
{
	struct cras_rclient *client;
	if (!cras_validate_connection_type(conn_type))
		goto error;

	switch (conn_type) {
	case CRAS_CONTROL:
		return cras_control_rclient_create(fd, id);
	case CRAS_PLAYBACK:
		return cras_playback_rclient_create(fd, id);
	case CRAS_CAPTURE:
		return cras_capture_rclient_create(fd, id);
	case CRAS_VMS_LEGACY:
		return cras_playback_rclient_create(fd, id);
	case CRAS_VMS_UNIFIED:
		return cras_unified_rclient_create(fd, id);
	case CRAS_PLUGIN_PLAYBACK:
		client = cras_playback_rclient_create(fd, id);
		cras_rclient_set_client_type(client, CRAS_CLIENT_TYPE_PLUGIN);
		return client;
	case CRAS_PLUGIN_UNIFIED:
		client = cras_unified_rclient_create(fd, id);
		cras_rclient_set_client_type(client, CRAS_CLIENT_TYPE_PLUGIN);
		return client;
	default:
		goto error;
	}

error:
	syslog(LOG_ERR, "unsupported connection type");
	return NULL;
}