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.
17 * @file conversation/speaker.c
18 * @brief API to access an audio speaker; provides access to hardware speakers
19 * @author Simon Dieterle
20 * @author Andreas Fuchs
21 * @author Christian Grothoff
24 #include "gnunet_speaker_lib.h"
25 #include "conversation.h"
29 * Internal data structures for the speaker.
36 const struct GNUNET_CONFIGURATION_Handle *cfg;
39 * Handle for the playback helper
41 struct GNUNET_HELPER_Handle *playback_helper;
47 * Function that enables a speaker.
49 * @param cls closure with the `struct Speaker`
50 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
55 struct Speaker *spe = cls;
56 static char *playback_helper_argv[] =
58 "gnunet-helper-audio-playback",
62 spe->playback_helper = GNUNET_HELPER_start (GNUNET_NO,
63 "gnunet-helper-audio-playback",
67 if (NULL == spe->playback_helper)
69 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
70 _("Could not start playback audio helper.\n"));
78 * Function that disables a speaker.
80 * @param cls closure with the `struct Speaker`
85 struct Speaker *spe = cls;
87 if (NULL == spe->playback_helper)
92 GNUNET_break (GNUNET_OK ==
93 GNUNET_HELPER_kill (spe->playback_helper, GNUNET_NO));
94 GNUNET_HELPER_destroy (spe->playback_helper);
95 spe->playback_helper = NULL;
100 * Function to destroy a speaker.
102 * @param cls closure with the `struct Speaker`
107 struct Speaker *spe = cls;
109 if (NULL != spe->playback_helper)
115 * Function to cause a speaker to play audio data.
117 * @param cls clsoure with the `struct Speaker`
118 * @param data_size number of bytes in @a data
119 * @param data audio data to play, format is
120 * opaque to the API but should be OPUS.
127 struct Speaker *spe = cls;
128 char buf[sizeof (struct AudioMessage) + data_size];
129 struct AudioMessage *am;
131 if (NULL == spe->playback_helper)
136 am = (struct AudioMessage *) buf;
137 am->header.size = htons (sizeof (struct AudioMessage) + data_size);
138 am->header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO);
139 GNUNET_memcpy (&am[1], data, data_size);
140 (void) GNUNET_HELPER_send (spe->playback_helper,
148 * Create a speaker that corresponds to the speaker hardware
151 * @param cfg configuration to use
152 * @return NULL on error
154 struct GNUNET_SPEAKER_Handle *
155 GNUNET_SPEAKER_create_from_hardware (const struct GNUNET_CONFIGURATION_Handle *cfg)
157 struct GNUNET_SPEAKER_Handle *speaker;
160 spe = GNUNET_new (struct Speaker);
162 speaker = GNUNET_new (struct GNUNET_SPEAKER_Handle);
164 speaker->enable_speaker = &enable;
165 speaker->play = &play;
166 speaker->disable_speaker = &disable;
167 speaker->destroy_speaker = &destroy;
175 * @param speaker speaker to destroy
178 GNUNET_SPEAKER_destroy (struct GNUNET_SPEAKER_Handle *speaker)
180 speaker->destroy_speaker (speaker->cls);
181 GNUNET_free (speaker);
184 /* end of speaker.c */