glitch in the license text detected by hyazinthe, thank you!
[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
16 /**
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
22  */
23 #include "platform.h"
24 #include "gnunet_speaker_lib.h"
25 #include "conversation.h"
26
27
28 /**
29  * Internal data structures for the speaker.
30  */
31 struct Speaker
32 {
33   /**
34    * Our configuration.
35    */
36   const struct GNUNET_CONFIGURATION_Handle *cfg;
37
38   /**
39    * Handle for the playback helper
40    */
41   struct GNUNET_HELPER_Handle *playback_helper;
42
43 };
44
45
46 /**
47  * Function that enables a speaker.
48  *
49  * @param cls closure with the `struct Speaker`
50  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
51  */
52 static int
53 enable (void *cls)
54 {
55   struct Speaker *spe = cls;
56   static char *playback_helper_argv[] =
57   {
58     "gnunet-helper-audio-playback",
59     NULL
60   };
61
62   spe->playback_helper = GNUNET_HELPER_start (GNUNET_NO,
63                                               "gnunet-helper-audio-playback",
64                                               playback_helper_argv,
65                                               NULL,
66                                               NULL, spe);
67   if (NULL == spe->playback_helper)
68   {
69     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
70                 _("Could not start playback audio helper.\n"));
71     return GNUNET_SYSERR;
72   }
73   return GNUNET_OK;
74 }
75
76
77 /**
78  * Function that disables a speaker.
79  *
80  * @param cls closure with the `struct Speaker`
81  */
82 static void
83 disable (void *cls)
84 {
85   struct Speaker *spe = cls;
86
87   if (NULL == spe->playback_helper)
88   {
89     GNUNET_break (0);
90     return;
91   }
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;
96 }
97
98
99 /**
100  * Function to destroy a speaker.
101  *
102  * @param cls closure with the `struct Speaker`
103  */
104 static void
105 destroy (void *cls)
106 {
107   struct Speaker *spe = cls;
108
109   if (NULL != spe->playback_helper)
110     disable (spe);
111 }
112
113
114 /**
115  * Function to cause a speaker to play audio data.
116  *
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.
121  */
122 static void
123 play (void *cls,
124       size_t data_size,
125       const void *data)
126 {
127   struct Speaker *spe = cls;
128   char buf[sizeof (struct AudioMessage) + data_size];
129   struct AudioMessage *am;
130
131   if (NULL == spe->playback_helper)
132   {
133     GNUNET_break (0);
134     return;
135   }
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,
141                              &am->header,
142                              GNUNET_NO,
143                              NULL, NULL);
144 }
145
146
147 /**
148  * Create a speaker that corresponds to the speaker hardware
149  * of our system.
150  *
151  * @param cfg configuration to use
152  * @return NULL on error
153  */
154 struct GNUNET_SPEAKER_Handle *
155 GNUNET_SPEAKER_create_from_hardware (const struct GNUNET_CONFIGURATION_Handle *cfg)
156 {
157   struct GNUNET_SPEAKER_Handle *speaker;
158   struct Speaker *spe;
159
160   spe = GNUNET_new (struct Speaker);
161   spe->cfg = cfg;
162   speaker = GNUNET_new (struct GNUNET_SPEAKER_Handle);
163   speaker->cls = spe;
164   speaker->enable_speaker = &enable;
165   speaker->play = &play;
166   speaker->disable_speaker = &disable;
167   speaker->destroy_speaker = &destroy;
168   return speaker;
169 }
170
171
172 /**
173  * Destroy a speaker.
174  *
175  * @param speaker speaker to destroy
176  */
177 void
178 GNUNET_SPEAKER_destroy (struct GNUNET_SPEAKER_Handle *speaker)
179 {
180   speaker->destroy_speaker (speaker->cls);
181   GNUNET_free (speaker);
182 }
183
184 /* end of speaker.c */