speaker.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2013 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  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. * Internal data structures for the speaker.
  28. */
  29. struct Speaker
  30. {
  31. /**
  32. * Our configuration.
  33. */
  34. const struct GNUNET_CONFIGURATION_Handle *cfg;
  35. /**
  36. * Handle for the playback helper
  37. */
  38. struct GNUNET_HELPER_Handle *playback_helper;
  39. };
  40. /**
  41. * Function that enables a speaker.
  42. *
  43. * @param cls closure with the `struct Speaker`
  44. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  45. */
  46. static int
  47. enable (void *cls)
  48. {
  49. struct Speaker *spe = cls;
  50. static char *playback_helper_argv[] = {
  51. "gnunet-helper-audio-playback",
  52. NULL
  53. };
  54. spe->playback_helper = GNUNET_HELPER_start (GNUNET_NO,
  55. "gnunet-helper-audio-playback",
  56. playback_helper_argv,
  57. NULL,
  58. NULL, spe);
  59. if (NULL == spe->playback_helper)
  60. {
  61. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  62. _ ("Could not start playback audio helper.\n"));
  63. return GNUNET_SYSERR;
  64. }
  65. return GNUNET_OK;
  66. }
  67. /**
  68. * Function that disables a speaker.
  69. *
  70. * @param cls closure with the `struct Speaker`
  71. */
  72. static void
  73. disable (void *cls)
  74. {
  75. struct Speaker *spe = cls;
  76. if (NULL == spe->playback_helper)
  77. {
  78. GNUNET_break (0);
  79. return;
  80. }
  81. GNUNET_break (GNUNET_OK ==
  82. GNUNET_HELPER_kill (spe->playback_helper, GNUNET_NO));
  83. GNUNET_HELPER_destroy (spe->playback_helper);
  84. spe->playback_helper = NULL;
  85. }
  86. /**
  87. * Function to destroy a speaker.
  88. *
  89. * @param cls closure with the `struct Speaker`
  90. */
  91. static void
  92. destroy (void *cls)
  93. {
  94. struct Speaker *spe = cls;
  95. if (NULL != spe->playback_helper)
  96. disable (spe);
  97. }
  98. /**
  99. * Function to cause a speaker to play audio data.
  100. *
  101. * @param cls clsoure with the `struct Speaker`
  102. * @param data_size number of bytes in @a data
  103. * @param data audio data to play, format is
  104. * opaque to the API but should be OPUS.
  105. */
  106. static void
  107. play (void *cls,
  108. size_t data_size,
  109. const void *data)
  110. {
  111. struct Speaker *spe = cls;
  112. char buf[sizeof(struct AudioMessage) + data_size];
  113. struct AudioMessage *am;
  114. if (NULL == spe->playback_helper)
  115. {
  116. GNUNET_break (0);
  117. return;
  118. }
  119. am = (struct AudioMessage *) buf;
  120. am->header.size = htons (sizeof(struct AudioMessage) + data_size);
  121. am->header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO);
  122. GNUNET_memcpy (&am[1], data, data_size);
  123. (void) GNUNET_HELPER_send (spe->playback_helper,
  124. &am->header,
  125. GNUNET_NO,
  126. NULL, NULL);
  127. }
  128. /**
  129. * Create a speaker that corresponds to the speaker hardware
  130. * of our system.
  131. *
  132. * @param cfg configuration to use
  133. * @return NULL on error
  134. */
  135. struct GNUNET_SPEAKER_Handle *
  136. GNUNET_SPEAKER_create_from_hardware (const struct
  137. GNUNET_CONFIGURATION_Handle *cfg)
  138. {
  139. struct GNUNET_SPEAKER_Handle *speaker;
  140. struct Speaker *spe;
  141. spe = GNUNET_new (struct Speaker);
  142. spe->cfg = cfg;
  143. speaker = GNUNET_new (struct GNUNET_SPEAKER_Handle);
  144. speaker->cls = spe;
  145. speaker->enable_speaker = &enable;
  146. speaker->play = &play;
  147. speaker->disable_speaker = &disable;
  148. speaker->destroy_speaker = &destroy;
  149. return speaker;
  150. }
  151. /**
  152. * Destroy a speaker.
  153. *
  154. * @param speaker speaker to destroy
  155. */
  156. void
  157. GNUNET_SPEAKER_destroy (struct GNUNET_SPEAKER_Handle *speaker)
  158. {
  159. speaker->destroy_speaker (speaker->cls);
  160. GNUNET_free (speaker);
  161. }
  162. /* end of speaker.c */