2 This file is part of GNUnet
3 (C) 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
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
29 #include "gnunet_speaker_lib.h"
30 #include "conversation.h"
34 * Internal data structures for the speaker.
41 const struct GNUNET_CONFIGURATION_Handle *cfg;
44 * Handle for the playback helper
46 struct GNUNET_HELPER_Handle *playback_helper;
52 * Function that enables a speaker.
54 * @param cls closure with the `struct Speaker`
55 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
60 struct Speaker *spe = cls;
61 static char *playback_helper_argv[] =
63 "gnunet-helper-audio-playback",
67 spe->playback_helper = GNUNET_HELPER_start (GNUNET_NO,
68 "gnunet-helper-audio-playback",
72 if (NULL == spe->playback_helper)
74 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
75 _("Could not start playback audio helper.\n"));
83 * Function that disables a speaker.
85 * @param cls closure with the `struct Speaker`
90 struct Speaker *spe = cls;
92 if (NULL == spe->playback_helper)
97 GNUNET_break (GNUNET_OK ==
98 GNUNET_HELPER_kill (spe->playback_helper, GNUNET_NO));
99 GNUNET_HELPER_destroy (spe->playback_helper);
100 spe->playback_helper = NULL;
105 * Function to destroy a speaker.
107 * @param cls closure with the `struct Speaker`
112 struct Speaker *spe = cls;
114 if (NULL != spe->playback_helper)
120 * Function to cause a speaker to play audio data.
122 * @param cls clsoure with the `struct Speaker`
123 * @param data_size number of bytes in @a data
124 * @param data audio data to play, format is
125 * opaque to the API but should be OPUS.
132 struct Speaker *spe = cls;
133 char buf[sizeof (struct AudioMessage) + data_size];
134 struct AudioMessage *am;
136 if (NULL == spe->playback_helper)
141 am = (struct AudioMessage *) buf;
142 am->header.size = htons (sizeof (struct AudioMessage) + data_size);
143 am->header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO);
144 memcpy (&am[1], data, data_size);
145 (void) GNUNET_HELPER_send (spe->playback_helper,
153 * Create a speaker that corresponds to the speaker hardware
156 * @param cfg configuration to use
157 * @return NULL on error
159 struct GNUNET_SPEAKER_Handle *
160 GNUNET_SPEAKER_create_from_hardware (const struct GNUNET_CONFIGURATION_Handle *cfg)
162 struct GNUNET_SPEAKER_Handle *speaker;
165 spe = GNUNET_new (struct Speaker);
167 speaker = GNUNET_new (struct GNUNET_SPEAKER_Handle);
169 speaker->enable_speaker = &enable;
170 speaker->play = &play;
171 speaker->disable_speaker = &disable;
172 speaker->destroy_speaker = &destroy;
180 * @param speaker speaker to destroy
183 GNUNET_SPEAKER_destroy (struct GNUNET_SPEAKER_Handle *speaker)
185 speaker->destroy_speaker (speaker->cls);
186 GNUNET_free (speaker);
189 /* end of speaker.c */