aboutsummaryrefslogtreecommitdiff
path: root/src/contrib/service/conversation/speaker.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/contrib/service/conversation/speaker.c')
-rw-r--r--src/contrib/service/conversation/speaker.c189
1 files changed, 189 insertions, 0 deletions
diff --git a/src/contrib/service/conversation/speaker.c b/src/contrib/service/conversation/speaker.c
new file mode 100644
index 000000000..38eb1159c
--- /dev/null
+++ b/src/contrib/service/conversation/speaker.c
@@ -0,0 +1,189 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2013 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file conversation/speaker.c
23 * @brief API to access an audio speaker; provides access to hardware speakers
24 * @author Simon Dieterle
25 * @author Andreas Fuchs
26 * @author Christian Grothoff
27 */
28#include "platform.h"
29#include "gnunet_speaker_lib.h"
30#include "conversation.h"
31
32
33/**
34 * Internal data structures for the speaker.
35 */
36struct Speaker
37{
38 /**
39 * Our configuration.
40 */
41 const struct GNUNET_CONFIGURATION_Handle *cfg;
42
43 /**
44 * Handle for the playback helper
45 */
46 struct GNUNET_HELPER_Handle *playback_helper;
47};
48
49
50/**
51 * Function that enables a speaker.
52 *
53 * @param cls closure with the `struct Speaker`
54 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
55 */
56static int
57enable (void *cls)
58{
59 struct Speaker *spe = cls;
60 static char *playback_helper_argv[] = {
61 "gnunet-helper-audio-playback",
62 NULL
63 };
64
65 spe->playback_helper = GNUNET_HELPER_start (GNUNET_NO,
66 "gnunet-helper-audio-playback",
67 playback_helper_argv,
68 NULL,
69 NULL, spe);
70 if (NULL == spe->playback_helper)
71 {
72 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
73 _ ("Could not start playback audio helper.\n"));
74 return GNUNET_SYSERR;
75 }
76 return GNUNET_OK;
77}
78
79
80/**
81 * Function that disables a speaker.
82 *
83 * @param cls closure with the `struct Speaker`
84 */
85static void
86disable (void *cls)
87{
88 struct Speaker *spe = cls;
89
90 if (NULL == spe->playback_helper)
91 {
92 GNUNET_break (0);
93 return;
94 }
95 GNUNET_break (GNUNET_OK ==
96 GNUNET_HELPER_kill (spe->playback_helper, GNUNET_NO));
97 GNUNET_HELPER_destroy (spe->playback_helper);
98 spe->playback_helper = NULL;
99}
100
101
102/**
103 * Function to destroy a speaker.
104 *
105 * @param cls closure with the `struct Speaker`
106 */
107static void
108destroy (void *cls)
109{
110 struct Speaker *spe = cls;
111
112 if (NULL != spe->playback_helper)
113 disable (spe);
114}
115
116
117/**
118 * Function to cause a speaker to play audio data.
119 *
120 * @param cls clsoure with the `struct Speaker`
121 * @param data_size number of bytes in @a data
122 * @param data audio data to play, format is
123 * opaque to the API but should be OPUS.
124 */
125static void
126play (void *cls,
127 size_t data_size,
128 const void *data)
129{
130 struct Speaker *spe = cls;
131 char buf[sizeof(struct AudioMessage) + data_size];
132 struct AudioMessage *am;
133
134 if (NULL == spe->playback_helper)
135 {
136 GNUNET_break (0);
137 return;
138 }
139 am = (struct AudioMessage *) buf;
140 am->header.size = htons (sizeof(struct AudioMessage) + data_size);
141 am->header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO);
142 GNUNET_memcpy (&am[1], data, data_size);
143 (void) GNUNET_HELPER_send (spe->playback_helper,
144 &am->header,
145 GNUNET_NO,
146 NULL, NULL);
147}
148
149
150/**
151 * Create a speaker that corresponds to the speaker hardware
152 * of our system.
153 *
154 * @param cfg configuration to use
155 * @return NULL on error
156 */
157struct GNUNET_SPEAKER_Handle *
158GNUNET_SPEAKER_create_from_hardware (const struct
159 GNUNET_CONFIGURATION_Handle *cfg)
160{
161 struct GNUNET_SPEAKER_Handle *speaker;
162 struct Speaker *spe;
163
164 spe = GNUNET_new (struct Speaker);
165 spe->cfg = cfg;
166 speaker = GNUNET_new (struct GNUNET_SPEAKER_Handle);
167 speaker->cls = spe;
168 speaker->enable_speaker = &enable;
169 speaker->play = &play;
170 speaker->disable_speaker = &disable;
171 speaker->destroy_speaker = &destroy;
172 return speaker;
173}
174
175
176/**
177 * Destroy a speaker.
178 *
179 * @param speaker speaker to destroy
180 */
181void
182GNUNET_SPEAKER_destroy (struct GNUNET_SPEAKER_Handle *speaker)
183{
184 speaker->destroy_speaker (speaker->cls);
185 GNUNET_free (speaker);
186}
187
188
189/* end of speaker.c */