1 /* Copyright 2020 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
6 #include <fcntl.h>
7 #include <stdint.h>
8 #include <sys/mman.h>
9 #include <sys/types.h>
10 #include <syslog.h>
11
12 #include "cras_audio_area.h"
13 #include "cras_config.h"
14 #include "cras_messages.h"
15 #include "cras_rclient.h"
16 #include "cras_rstream.h"
17 #include "cras_rstream_config.h"
18 #include "cras_server_metrics.h"
19 #include "cras_shm.h"
20 #include "cras_types.h"
21 #include "cras_system_state.h"
22
cras_rstream_config_init(struct cras_rclient * client,cras_stream_id_t stream_id,enum CRAS_STREAM_TYPE stream_type,enum CRAS_CLIENT_TYPE client_type,enum CRAS_STREAM_DIRECTION direction,uint32_t dev_idx,uint32_t flags,uint32_t effects,const struct cras_audio_format * format,size_t buffer_frames,size_t cb_threshold,int * audio_fd,int * client_shm_fd,size_t client_shm_size,const uint64_t buffer_offsets[2],struct cras_rstream_config * stream_config)23 void cras_rstream_config_init(
24 struct cras_rclient *client, cras_stream_id_t stream_id,
25 enum CRAS_STREAM_TYPE stream_type, enum CRAS_CLIENT_TYPE client_type,
26 enum CRAS_STREAM_DIRECTION direction, uint32_t dev_idx, uint32_t flags,
27 uint32_t effects, const struct cras_audio_format *format,
28 size_t buffer_frames, size_t cb_threshold, int *audio_fd,
29 int *client_shm_fd, size_t client_shm_size,
30 const uint64_t buffer_offsets[2],
31 struct cras_rstream_config *stream_config)
32 {
33 stream_config->stream_id = stream_id;
34 stream_config->stream_type = stream_type;
35 stream_config->client_type = client_type;
36 stream_config->direction = direction;
37 stream_config->dev_idx = dev_idx;
38 stream_config->flags = flags;
39 stream_config->effects = effects;
40 stream_config->format = format;
41 stream_config->buffer_frames = buffer_frames;
42 stream_config->cb_threshold = cb_threshold;
43 stream_config->audio_fd = *audio_fd;
44 *audio_fd = -1;
45 stream_config->client_shm_fd = *client_shm_fd;
46 *client_shm_fd = -1;
47 stream_config->client_shm_size = client_shm_size;
48 stream_config->buffer_offsets[0] = buffer_offsets[0];
49 stream_config->buffer_offsets[1] = buffer_offsets[1];
50 stream_config->client = client;
51 }
52
cras_rstream_config_init_with_message(struct cras_rclient * client,const struct cras_connect_message * msg,int * aud_fd,int * client_shm_fd,const struct cras_audio_format * remote_fmt)53 struct cras_rstream_config cras_rstream_config_init_with_message(
54 struct cras_rclient *client, const struct cras_connect_message *msg,
55 int *aud_fd, int *client_shm_fd,
56 const struct cras_audio_format *remote_fmt)
57 {
58 struct cras_rstream_config stream_config;
59
60 const uint64_t buffer_offsets[2] = { msg->buffer_offsets[0],
61 msg->buffer_offsets[1] };
62 cras_rstream_config_init(client, msg->stream_id, msg->stream_type,
63 msg->client_type, msg->direction, msg->dev_idx,
64 msg->flags, msg->effects, remote_fmt,
65 msg->buffer_frames, msg->cb_threshold, aud_fd,
66 client_shm_fd, msg->client_shm_size,
67 buffer_offsets, &stream_config);
68 return stream_config;
69 }
70
cras_rstream_config_cleanup(struct cras_rstream_config * stream_config)71 void cras_rstream_config_cleanup(struct cras_rstream_config *stream_config)
72 {
73 if (stream_config->audio_fd >= 0)
74 close(stream_config->audio_fd);
75 if (stream_config->client_shm_fd >= 0)
76 close(stream_config->client_shm_fd);
77 }
78