7f336d632b5d79483e7bbc846f4e94efb4cd66bb
[oweals/gnunet.git] / src / conversation / speaker.c
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
19 /**
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
25  */
26 #include "platform.h"
27 #include "gnunet_speaker_lib.h"
28 #include "conversation.h"
29
30
31 /**
32  * Internal data structures for the speaker.
33  */
34 struct Speaker
35 {
36   /**
37    * Our configuration.
38    */
39   const struct GNUNET_CONFIGURATION_Handle *cfg;
40
41   /**
42    * Handle for the playback helper
43    */
44   struct GNUNET_HELPER_Handle *playback_helper;
45
46 };
47
48
49 /**
50  * Function that enables a speaker.
51  *
52  * @param cls closure with the `struct Speaker`
53  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
54  */
55 static int
56 enable (void *cls)
57 {
58   struct Speaker *spe = cls;
59   static char *playback_helper_argv[] =
60   {
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  */
85 static void
86 disable (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  */
107 static void
108 destroy (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  */
125 static void
126 play (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  */
157 struct GNUNET_SPEAKER_Handle *
158 GNUNET_SPEAKER_create_from_hardware (const struct GNUNET_CONFIGURATION_Handle *cfg)
159 {
160   struct GNUNET_SPEAKER_Handle *speaker;
161   struct Speaker *spe;
162
163   spe = GNUNET_new (struct Speaker);
164   spe->cfg = cfg;
165   speaker = GNUNET_new (struct GNUNET_SPEAKER_Handle);
166   speaker->cls = spe;
167   speaker->enable_speaker = &enable;
168   speaker->play = &play;
169   speaker->disable_speaker = &disable;
170   speaker->destroy_speaker = &destroy;
171   return speaker;
172 }
173
174
175 /**
176  * Destroy a speaker.
177  *
178  * @param speaker speaker to destroy
179  */
180 void
181 GNUNET_SPEAKER_destroy (struct GNUNET_SPEAKER_Handle *speaker)
182 {
183   speaker->destroy_speaker (speaker->cls);
184   GNUNET_free (speaker);
185 }
186
187 /* end of speaker.c */