GNUnet  0.10.x
speaker.c
Go to the documentation of this file.
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  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
28 #include "platform.h"
29 #include "gnunet_speaker_lib.h"
30 #include "conversation.h"
31 
32 
36 struct Speaker
37 {
42 
47 
48 };
49 
50 
57 static int
58 enable (void *cls)
59 {
60  struct Speaker *spe = cls;
61  static char *playback_helper_argv[] =
62  {
63  "gnunet-helper-audio-playback",
64  NULL
65  };
66 
68  "gnunet-helper-audio-playback",
69  playback_helper_argv,
70  NULL,
71  NULL, spe);
72  if (NULL == spe->playback_helper)
73  {
75  _("Could not start playback audio helper.\n"));
76  return GNUNET_SYSERR;
77  }
78  return GNUNET_OK;
79 }
80 
81 
87 static void
88 disable (void *cls)
89 {
90  struct Speaker *spe = cls;
91 
92  if (NULL == spe->playback_helper)
93  {
94  GNUNET_break (0);
95  return;
96  }
100  spe->playback_helper = NULL;
101 }
102 
103 
109 static void
110 destroy (void *cls)
111 {
112  struct Speaker *spe = cls;
113 
114  if (NULL != spe->playback_helper)
115  disable (spe);
116 }
117 
118 
127 static void
128 play (void *cls,
129  size_t data_size,
130  const void *data)
131 {
132  struct Speaker *spe = cls;
133  char buf[sizeof (struct AudioMessage) + data_size];
134  struct AudioMessage *am;
135 
136  if (NULL == spe->playback_helper)
137  {
138  GNUNET_break (0);
139  return;
140  }
141  am = (struct AudioMessage *) buf;
142  am->header.size = htons (sizeof (struct AudioMessage) + data_size);
144  GNUNET_memcpy (&am[1], data, data_size);
145  (void) GNUNET_HELPER_send (spe->playback_helper,
146  &am->header,
147  GNUNET_NO,
148  NULL, NULL);
149 }
150 
151 
159 struct GNUNET_SPEAKER_Handle *
161 {
163  struct Speaker *spe;
164 
165  spe = GNUNET_new (struct Speaker);
166  spe->cfg = cfg;
167  speaker = GNUNET_new (struct GNUNET_SPEAKER_Handle);
168  speaker->cls = spe;
169  speaker->enable_speaker = &enable;
170  speaker->play = &play;
171  speaker->disable_speaker = &disable;
172  speaker->destroy_speaker = &destroy;
173  return speaker;
174 }
175 
176 
182 void
184 {
185  speaker->destroy_speaker (speaker->cls);
186  GNUNET_free (speaker);
187 }
188 
189 /* end of speaker.c */
int GNUNET_HELPER_kill(struct GNUNET_HELPER_Handle *h, int soft_kill)
Sends termination signal to the helper process.
Definition: helper.c:180
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO.
Definition: conversation.h:60
The handle to a helper process.
Definition: helper.c:80
struct GNUNET_HELPER_Handle * GNUNET_HELPER_start(int with_control_pipe, const char *binary_name, char *const binary_argv[], GNUNET_MessageTokenizerCallback cb, GNUNET_HELPER_ExceptionCallback exp_cb, void *cb_cls)
Starts a helper and begins reading from it.
Definition: helper.c:491
constants for network protocols
struct GNUNET_SPEAKER_Handle * GNUNET_SPEAKER_create_from_hardware(const struct GNUNET_CONFIGURATION_Handle *cfg)
Create a speaker that corresponds to the speaker hardware of our system.
Definition: speaker.c:160
GNUNET_SPEAKER_PlayCallback play
Play audio.
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
GNUNET_SPEAKER_DisableCallback disable_speaker
Turn the speaker off.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
Internal data structures for the speaker.
Definition: speaker.c:36
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
#define GNUNET_memcpy(dst, src, n)
Message to transmit the audio (between client and helpers).
Definition: conversation.h:55
static int enable(void *cls)
Function that enables a speaker.
Definition: speaker.c:58
static char buf[2048]
struct GNUNET_HELPER_Handle * playback_helper
Handle for the playback helper.
Definition: speaker.c:46
GNUNET_SPEAKER_EnableCallback enable_speaker
Turn on the speaker.
void * cls
Closure for the callbacks.
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
void GNUNET_SPEAKER_destroy(struct GNUNET_SPEAKER_Handle *speaker)
Destroy a speaker.
Definition: speaker.c:183
void GNUNET_HELPER_destroy(struct GNUNET_HELPER_Handle *h)
Free&#39;s the resources occupied by the helper handle.
Definition: helper.c:530
configuration data
Definition: configuration.c:85
struct GNUNET_HELPER_SendHandle * GNUNET_HELPER_send(struct GNUNET_HELPER_Handle *h, const struct GNUNET_MessageHeader *msg, int can_drop, GNUNET_HELPER_Continuation cont, void *cont_cls)
Send an message to the helper.
Definition: helper.c:660
#define GNUNET_log(kind,...)
static void destroy(void *cls)
Function to destroy a speaker.
Definition: speaker.c:110
#define GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO
Message to transmit the audio between helper and speaker/microphone library.
static void disable(void *cls)
Function that disables a speaker.
Definition: speaker.c:88
uint32_t data
The data value.
static size_t data_size
Number of bytes in data.
static void play(void *cls, size_t data_size, const void *data)
Function to cause a speaker to play audio data.
Definition: speaker.c:128
A speaker is a device that can play or record audio data.
static struct GNUNET_SPEAKER_Handle * speaker
Handle to the speaker.
#define GNUNET_free(ptr)
Wrapper around free.
GNUNET_SPEAKER_DestroyCallback destroy_speaker
Destroy the speaker.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: speaker.c:41