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 {
41 
46 };
47 
48 
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 
66  "gnunet-helper-audio-playback",
67  playback_helper_argv,
68  NULL,
69  NULL, spe);
70  if (NULL == spe->playback_helper)
71  {
73  _("Could not start playback audio helper.\n"));
74  return GNUNET_SYSERR;
75  }
76  return GNUNET_OK;
77 }
78 
79 
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  }
98  spe->playback_helper = NULL;
99 }
100 
101 
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 
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);
142  GNUNET_memcpy(&am[1], data, data_size);
144  &am->header,
145  GNUNET_NO,
146  NULL, NULL);
147 }
148 
149 
157 struct GNUNET_SPEAKER_Handle *
159 {
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 
180 void
182 {
183  speaker->destroy_speaker(speaker->cls);
184  GNUNET_free(speaker);
185 }
186 
187 /* 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:175
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO.
Definition: conversation.h:59
The handle to a helper process.
Definition: helper.c:77
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:487
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:158
GNUNET_SPEAKER_PlayCallback play
Play audio.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
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:181
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
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:56
static char buf[2048]
struct GNUNET_HELPER_Handle * playback_helper
Handle for the playback helper.
Definition: speaker.c:45
GNUNET_SPEAKER_EnableCallback enable_speaker
Turn on the speaker.
void * cls
Closure for the callbacks.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
void GNUNET_SPEAKER_destroy(struct GNUNET_SPEAKER_Handle *speaker)
Destroy a speaker.
Definition: speaker.c:181
void GNUNET_HELPER_destroy(struct GNUNET_HELPER_Handle *h)
Free&#39;s the resources occupied by the helper handle.
Definition: helper.c:526
configuration data
Definition: configuration.c:83
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:653
#define GNUNET_log(kind,...)
static void destroy(void *cls)
Function to destroy a speaker.
Definition: speaker.c:108
#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:86
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:126
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:40