2 This file is part of GNUnet
3 Copyright (C) 2013 GNUnet e.V.
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.
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.
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/>.
20 * @file conversation/speaker.c
21 * @brief API to access an audio speaker; provides access to hardware speakers
22 * @author Simon Dieterle
23 * @author Andreas Fuchs
24 * @author Christian Grothoff
27 #include "gnunet_speaker_lib.h"
28 #include "conversation.h"
32 * Internal data structures for the speaker.
39 const struct GNUNET_CONFIGURATION_Handle *cfg;
42 * Handle for the playback helper
44 struct GNUNET_HELPER_Handle *playback_helper;
50 * Function that enables a speaker.
52 * @param cls closure with the `struct Speaker`
53 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
58 struct Speaker *spe = cls;
59 static char *playback_helper_argv[] =
61 "gnunet-helper-audio-playback",
65 spe->playback_helper = GNUNET_HELPER_start (GNUNET_NO,
66 "gnunet-helper-audio-playback",
70 if (NULL == spe->playback_helper)
72 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
73 _("Could not start playback audio helper.\n"));
81 * Function that disables a speaker.
83 * @param cls closure with the `struct Speaker`
88 struct Speaker *spe = cls;
90 if (NULL == spe->playback_helper)
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;
103 * Function to destroy a speaker.
105 * @param cls closure with the `struct Speaker`
110 struct Speaker *spe = cls;
112 if (NULL != spe->playback_helper)
118 * Function to cause a speaker to play audio data.
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.
130 struct Speaker *spe = cls;
131 char buf[sizeof (struct AudioMessage) + data_size];
132 struct AudioMessage *am;
134 if (NULL == spe->playback_helper)
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,
151 * Create a speaker that corresponds to the speaker hardware
154 * @param cfg configuration to use
155 * @return NULL on error
157 struct GNUNET_SPEAKER_Handle *
158 GNUNET_SPEAKER_create_from_hardware (const struct GNUNET_CONFIGURATION_Handle *cfg)
160 struct GNUNET_SPEAKER_Handle *speaker;
163 spe = GNUNET_new (struct Speaker);
165 speaker = GNUNET_new (struct GNUNET_SPEAKER_Handle);
167 speaker->enable_speaker = &enable;
168 speaker->play = &play;
169 speaker->disable_speaker = &disable;
170 speaker->destroy_speaker = &destroy;
178 * @param speaker speaker to destroy
181 GNUNET_SPEAKER_destroy (struct GNUNET_SPEAKER_Handle *speaker)
183 speaker->destroy_speaker (speaker->cls);
184 GNUNET_free (speaker);
187 /* end of speaker.c */