gnunet_gst.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2016 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/gnunet_gst.c
  18. * @brief FIXME
  19. * @author Hark
  20. */
  21. #include "gnunet_gst_def.h"
  22. /**
  23. * Our configuration.
  24. */
  25. static struct GNUNET_CONFIGURATION_Handle *cfg;
  26. void
  27. dump_buffer (unsigned n, const unsigned char*buf)
  28. {
  29. const unsigned char *p, *end;
  30. unsigned i, j;
  31. end = buf + n;
  32. for (i = 0;; i += 16)
  33. {
  34. p = buf + i;
  35. for (j = 0; j < 16; j++)
  36. {
  37. fprintf (stderr, "%02X ", p[j]);
  38. if (p + j >= end)
  39. goto BREAKOUT;
  40. }
  41. fprintf (stderr, " ");
  42. p = buf + i;
  43. for (j = 0; j < 16; j++)
  44. {
  45. fprintf (stderr, "%c", isprint (p[j]) ? p[j] :
  46. '.');
  47. if (p + j >= end)
  48. goto BREAKOUT;
  49. }
  50. fprintf (stderr, "\n");
  51. }
  52. BREAKOUT:
  53. return;
  54. }
  55. /***
  56. * load gnunet configuration
  57. */
  58. void
  59. gg_load_configuration (GNUNET_gstData *d)
  60. {
  61. char *audiobackend_string;
  62. cfg = GNUNET_CONFIGURATION_create ();
  63. GNUNET_CONFIGURATION_load (cfg, "mediahelper.conf");
  64. GNUNET_CONFIGURATION_get_value_string (cfg, "MEDIAHELPER", "JACK_PP_IN",
  65. &d->jack_pp_in);
  66. GNUNET_CONFIGURATION_get_value_string (cfg, "MEDIAHELPER", "JACK_PP_OUT",
  67. &d->jack_pp_out);
  68. GNUNET_CONFIGURATION_get_value_string (cfg, "MEDIAHELPER", "AUDIOBACKEND",
  69. &audiobackend_string);
  70. // printf("abstring: %s \n", audiobackend_string);
  71. if (0 == strcasecmp (audiobackend_string, "AUTO"))
  72. {
  73. d->audiobackend = AUTO;
  74. }
  75. else if (0 == strcasecmp (audiobackend_string, "JACK"))
  76. {
  77. d->audiobackend = JACK;
  78. }
  79. else if (0 == strcasecmp (audiobackend_string, "ALSA"))
  80. {
  81. d->audiobackend = ALSA;
  82. }
  83. else if (0 == strcasecmp (audiobackend_string, "FAKE"))
  84. {
  85. d->audiobackend = FAKE;
  86. }
  87. else if (0 == strcasecmp (audiobackend_string, "TEST"))
  88. {
  89. d->audiobackend = TEST;
  90. }
  91. else
  92. {
  93. d->audiobackend = AUTO;
  94. }
  95. if (GNUNET_CONFIGURATION_get_value_yesno (cfg, "MEDIAHELPER",
  96. "REMOVESILENCE") == GNUNET_YES)
  97. {
  98. d->dropsilence = TRUE;
  99. }
  100. else
  101. {
  102. d->dropsilence = FALSE;
  103. }
  104. if (GNUNET_CONFIGURATION_get_value_yesno (cfg, "MEDIAHELPER",
  105. "NO_GN_HEADERS") == GNUNET_YES)
  106. {
  107. d->pure_ogg = TRUE;
  108. }
  109. else
  110. {
  111. d->pure_ogg = FALSE;
  112. }
  113. if (GNUNET_CONFIGURATION_get_value_yesno (cfg, "MEDIAHELPER", "USERTP") ==
  114. GNUNET_YES)
  115. {
  116. d->usertp = TRUE;
  117. }
  118. else
  119. {
  120. d->usertp = FALSE;
  121. }
  122. // GNUNET_CONFIGURATION_write(cfg, "mediahelper.conf");
  123. }
  124. static void
  125. write_data (const char *ptr, size_t msg_size)
  126. {
  127. ssize_t ret;
  128. size_t off;
  129. off = 0;
  130. while (off < msg_size)
  131. {
  132. ret = write (1, &ptr[off], msg_size - off);
  133. if (0 >= ret)
  134. {
  135. if (-1 == ret)
  136. GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "write");
  137. // quit (2);
  138. }
  139. off += ret;
  140. }
  141. }
  142. extern GstFlowReturn
  143. on_appsink_new_sample (GstElement *element, GNUNET_gstData *d)
  144. {
  145. // size of message including gnunet header
  146. size_t msg_size;
  147. GstSample *s;
  148. GstBuffer *b;
  149. GstMapInfo map;
  150. /*
  151. const GstStructure *si;
  152. char *si_str;
  153. GstCaps *s_caps;
  154. char *caps_str;
  155. */if (gst_app_sink_is_eos (GST_APP_SINK (element)))
  156. return GST_FLOW_OK;
  157. // pull sample from appsink
  158. s = gst_app_sink_pull_sample (GST_APP_SINK (element));
  159. if (s == NULL)
  160. return GST_FLOW_OK;
  161. if (! GST_IS_SAMPLE (s))
  162. return GST_FLOW_OK;
  163. b = gst_sample_get_buffer (s);
  164. GST_WARNING ("caps are %" GST_PTR_FORMAT, gst_sample_get_caps (s));
  165. gst_buffer_map (b, &map, GST_MAP_READ);
  166. size_t len;
  167. len = map.size;
  168. if (len > UINT16_MAX - sizeof(struct AudioMessage))
  169. {
  170. // this should never happen?
  171. printf ("GSTREAMER sample too big! \n");
  172. exit (20);
  173. len = UINT16_MAX - sizeof(struct AudioMessage);
  174. }
  175. msg_size = sizeof(struct AudioMessage) + len;
  176. // copy the data into audio_message
  177. GNUNET_memcpy (((char *) &(d->audio_message)[1]), map.data, len);
  178. (d->audio_message)->header.size = htons ((uint16_t) msg_size);
  179. if (d->pure_ogg)
  180. // write the audio_message without the gnunet headers
  181. write_data ((const char *) &(d->audio_message)[1], len);
  182. else
  183. write_data ((const char *) d->audio_message, msg_size);
  184. gst_sample_unref (s);
  185. return GST_FLOW_OK;
  186. }
  187. /***
  188. * Dump a pipeline graph
  189. */
  190. extern void
  191. pl_graph (GstElement *pipeline)
  192. {
  193. #ifdef IS_SPEAKER
  194. gst_debug_bin_to_dot_file_with_ts (GST_BIN (pipeline),
  195. GST_DEBUG_GRAPH_SHOW_ALL,
  196. "playback_helper.dot");
  197. #endif
  198. #ifdef IS_MIC
  199. gst_debug_bin_to_dot_file_with_ts (GST_BIN (pipeline),
  200. GST_DEBUG_GRAPH_SHOW_ALL,
  201. "record_helper.dot");
  202. #endif
  203. // load_configuration();
  204. }
  205. extern gboolean
  206. gnunet_gst_bus_call (GstBus *bus, GstMessage *msg, gpointer data)
  207. {
  208. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  209. "Bus message\n");
  210. switch (GST_MESSAGE_TYPE (msg))
  211. {
  212. case GST_MESSAGE_EOS:
  213. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  214. "End of stream\n");
  215. exit (10);
  216. break;
  217. case GST_MESSAGE_ERROR:
  218. {
  219. gchar *debug;
  220. GError *error;
  221. gst_message_parse_error (msg, &error, &debug);
  222. g_free (debug);
  223. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  224. "Error: %s\n",
  225. error->message);
  226. g_error_free (error);
  227. exit (10);
  228. break;
  229. }
  230. default:
  231. break;
  232. }
  233. return TRUE;
  234. }
  235. /* called when pipeline changes state */
  236. extern void
  237. state_changed_cb (GstBus *bus, GstMessage *msg, GNUNET_gstData *d)
  238. {
  239. GstState old_state, new_state, pending_state;
  240. gst_message_parse_state_changed (msg, &old_state, &new_state,
  241. &pending_state);
  242. switch (new_state)
  243. {
  244. case GST_STATE_READY:
  245. // printf("ready.... \n");
  246. // pl_graph(GST_ELEMENT(d->pipeline));
  247. break;
  248. case GST_STATE_PLAYING:
  249. // GST_LOG ("caps are %" GST_PTR_FORMAT, caps);
  250. // printf("Playing.... \n");
  251. pl_graph (GST_ELEMENT (d->pipeline));
  252. break;
  253. case GST_STATE_VOID_PENDING:
  254. // printf("void_pending.... \n");
  255. // pl_graph(GST_ELEMENT(d->pipeline));
  256. break;
  257. case GST_STATE_NULL:
  258. // printf("null.... \n");
  259. // pl_graph(GST_ELEMENT(d->pipeline));
  260. break;
  261. case GST_STATE_PAUSED:
  262. // printf("paused.... \n");
  263. // pl_graph(GST_ELEMENT(d->pipeline));
  264. break;
  265. }
  266. }
  267. static void
  268. application_cb (GstBus *bus, GstMessage *msg, GNUNET_gstData *data)
  269. {
  270. // printf("application cb");
  271. return;
  272. }
  273. static void
  274. error_cb (GstBus *bus, GstMessage *msg, GNUNET_gstData *data)
  275. {
  276. // printf("error cb");
  277. return;
  278. }
  279. static void
  280. eos_cb (GstBus *bus, GstMessage *msg, GNUNET_gstData *data)
  281. {
  282. // printf("eos cb");
  283. return;
  284. }
  285. extern void
  286. gg_setup_gst_bus (GNUNET_gstData *d)
  287. {
  288. GstBus *bus;
  289. bus = gst_element_get_bus (GST_ELEMENT (d->pipeline));
  290. gst_bus_add_signal_watch (bus);
  291. g_signal_connect (G_OBJECT (bus), "message::error", (GCallback) error_cb,
  292. d);
  293. g_signal_connect (G_OBJECT (bus), "message::eos", (GCallback) eos_cb,
  294. d);
  295. g_signal_connect (G_OBJECT (bus), "message::state-changed",
  296. (GCallback) state_changed_cb, d);
  297. g_signal_connect (G_OBJECT (bus), "message::application",
  298. (GCallback) application_cb, d);
  299. g_signal_connect (G_OBJECT (bus), "message::about-to-finish",
  300. (GCallback) application_cb, d);
  301. gst_object_unref (bus);
  302. }
  303. /*
  304. * take buffer from gstreamer and feed it to gnunet
  305. */
  306. /*
  307. extern int
  308. feed_buffer_to_gnunet (GNUNET_gstData * d)
  309. {
  310. GstSample *s;
  311. GstBuffer *b;
  312. GstMapInfo m;
  313. size_t len, msg_size;
  314. const char *ptr;
  315. int phase;
  316. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pulling...\n");
  317. s = gst_app_sink_pull_sample (GST_APP_SINK(d->appsink));
  318. if (NULL == s)
  319. {
  320. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pulled NULL\n");
  321. return OK;
  322. }
  323. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "...pulled!\n");
  324. const GstStructure *si;
  325. char *si_str;
  326. GstCaps *s_caps;
  327. char *caps_str;
  328. si = gst_sample_get_info (s);
  329. if (si)
  330. {
  331. si_str = gst_structure_to_string (si);
  332. if (si_str)
  333. {
  334. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got sample %s\n", si_str);
  335. g_free (si_str);
  336. }
  337. }
  338. else
  339. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got sample with no info\n");
  340. s_caps = gst_sample_get_caps (s);
  341. if (s_caps)
  342. {
  343. caps_str = gst_caps_to_string (s_caps);
  344. if (caps_str)
  345. {
  346. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got sample with caps %s\n", caps_str);
  347. g_free (caps_str);
  348. }
  349. }
  350. else
  351. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got sample with no caps\n");
  352. b = gst_sample_get_buffer (s);
  353. if (NULL == b || !gst_buffer_map (b, &m, GST_MAP_READ))
  354. {
  355. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got NULL buffer %p or failed to map the buffer\n", b);
  356. gst_sample_unref (s);
  357. return FAIL;
  358. }
  359. len = m.size;
  360. if (len > UINT16_MAX - sizeof (struct AudioMessage))
  361. {
  362. GNUNET_break (0);
  363. len = UINT16_MAX - sizeof (struct AudioMessage);
  364. }
  365. msg_size = sizeof (struct AudioMessage) + len;
  366. audio_message.header.size = htons ((uint16_t) msg_size);
  367. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  368. "Sending %u bytes of audio data\n", (unsigned int) msg_size);
  369. for (phase = 0; phase < 2; phase++)
  370. {
  371. size_t offset;
  372. size_t to_send;
  373. ssize_t ret;
  374. if (0 == phase && !d->pure_ogg)
  375. {
  376. //#ifdef DEBUG_RECORD_PURE_OGG
  377. // if (d->pure_ogg)
  378. // break;
  379. //#endif
  380. ptr = (const char *) &audio_message;
  381. to_send = sizeof (audio_message);
  382. }
  383. else
  384. {
  385. ptr = (const char *) m.data;
  386. to_send = len;
  387. }
  388. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  389. "Sending %u bytes on phase %d\n", (unsigned int) to_send, phase);
  390. for (offset = 0; offset < to_send; offset += ret)
  391. {
  392. ret = write (1, &ptr[offset], to_send - offset);
  393. if (0 >= ret)
  394. {
  395. if (-1 == ret)
  396. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  397. "Failed to write %u bytes at offset %u (total %u) in phase %d: %s\n",
  398. (unsigned int) to_send - offset, (unsigned int) offset,
  399. (unsigned int) (to_send + offset), phase, strerror (errno));
  400. // abort_send = 1;
  401. return FAIL;
  402. }
  403. }
  404. // if (abort_send)
  405. // break;
  406. }
  407. gst_buffer_unmap (b, &m);
  408. gst_sample_unref (s);
  409. }
  410. */
  411. extern int
  412. feed_buffer_to_gst (const char *audio, size_t b_len, GNUNET_gstData *d)
  413. {
  414. GstBuffer *b;
  415. gchar *bufspace;
  416. GstFlowReturn flow;
  417. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  418. "Feeding %u bytes to GStreamer\n",
  419. (unsigned int) b_len);
  420. bufspace = g_memdup (audio, b_len);
  421. b = gst_buffer_new_wrapped (bufspace, b_len);
  422. if (NULL == b)
  423. {
  424. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  425. "Failed to wrap a buffer\n");
  426. g_free (bufspace);
  427. return GNUNET_SYSERR;
  428. }
  429. if (GST_APP_SRC (d->appsrc) == NULL)
  430. exit (10);
  431. flow = gst_app_src_push_buffer (GST_APP_SRC (d->appsrc), b);
  432. /* They all return GNUNET_OK, because currently player stops when
  433. * data stops coming. This might need to be changed for the player
  434. * to also stop when pipeline breaks.
  435. */
  436. switch (flow)
  437. {
  438. case GST_FLOW_OK:
  439. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  440. "Fed %u bytes to the pipeline\n",
  441. (unsigned int) b_len);
  442. break;
  443. case GST_FLOW_FLUSHING:
  444. /* buffer was dropped, because pipeline state is not PAUSED or PLAYING */
  445. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  446. "Dropped a buffer\n");
  447. break;
  448. case GST_FLOW_EOS:
  449. /* end of stream */
  450. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  451. "EOS\n");
  452. break;
  453. default:
  454. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  455. "Unexpected push result\n");
  456. break;
  457. }
  458. return GNUNET_OK;
  459. }
  460. /**
  461. * debug making elements
  462. */
  463. extern GstElement *
  464. gst_element_factory_make_debug (gchar *factoryname, gchar *name)
  465. {
  466. GstElement *element;
  467. element = gst_element_factory_make (factoryname, name);
  468. if (element == NULL)
  469. {
  470. printf ("\n Failed to create element - type: %s name: %s \n", factoryname,
  471. name);
  472. exit (10);
  473. return element;
  474. }
  475. else
  476. {
  477. return element;
  478. }
  479. }
  480. /*
  481. static gboolean
  482. gst_element_link_many_debug(...)
  483. {
  484. va_list arguments;
  485. gst_element_link_many(argptr);
  486. }
  487. #define gst_element_link_many(...) \
  488. gst_element_link_many_debug(__VA_ARGS__)
  489. */
  490. extern void
  491. lf (char *msg)
  492. {
  493. printf ("linking elements failed: %s", msg);
  494. exit (10);
  495. }
  496. /***
  497. * used to set properties on autoaudiosink's chosen sink
  498. */
  499. static void
  500. autoaudiosink_child_added (GstChildProxy *child_proxy,
  501. GObject *object,
  502. gchar *name,
  503. gpointer user_data)
  504. {
  505. if (GST_IS_AUDIO_BASE_SRC (object))
  506. g_object_set (object,
  507. "buffer-time", (gint64) BUFFER_TIME,
  508. "latency-time", (gint64) LATENCY_TIME,
  509. NULL);
  510. }
  511. /***
  512. * used to set properties on autoaudiosource's chosen sink
  513. */
  514. static void
  515. autoaudiosource_child_added (GstChildProxy *child_proxy, GObject *object,
  516. gchar *name, gpointer user_data)
  517. {
  518. if (GST_IS_AUDIO_BASE_SRC (object))
  519. g_object_set (object, "buffer-time", (gint64) BUFFER_TIME, "latency-time",
  520. (gint64) LATENCY_TIME, NULL);
  521. }
  522. GstElement *
  523. get_pipeline (GstElement *element)
  524. {
  525. GstPipeline *p;
  526. p = GST_PIPELINE (gst_object_get_parent (GST_OBJECT (element)));
  527. return GST_ELEMENT (p);
  528. }
  529. static void
  530. decoder_ogg_pad_added (GstElement *element,
  531. GstPad *pad,
  532. gpointer data)
  533. {
  534. GstPad *sinkpad;
  535. GstElement *decoder = (GstElement *) data;
  536. printf ("==== ogg pad added callback \n");
  537. /* We can now link this pad with the opus-decoder sink pad */
  538. // pl_graph(get_pipeline(element));
  539. sinkpad = gst_element_get_static_pad (decoder, "sink");
  540. gst_pad_link (pad, sinkpad);
  541. gst_element_link_many (element, decoder, NULL);
  542. gst_object_unref (sinkpad);
  543. }
  544. int
  545. gnunet_read (GNUNET_gstData *d)
  546. {
  547. char readbuf[MAXLINE];
  548. int ret;
  549. printf ("read \n");
  550. ret = read (0, readbuf, sizeof(readbuf));
  551. if (0 > ret)
  552. {
  553. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  554. _ ("Read error from STDIN: %d %s\n"),
  555. ret, strerror (errno));
  556. return FAIL;
  557. }
  558. // toff += ret;
  559. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  560. "Received %d bytes of audio data\n",
  561. (int) ret);
  562. if (0 == ret)
  563. return FAIL;
  564. // #ifdef DEBUG_READ_PURE_OGG
  565. if (d->pure_ogg)
  566. {
  567. feed_buffer_to_gst (readbuf, ret, d);
  568. }
  569. else
  570. {
  571. // #endif
  572. GNUNET_MST_from_buffer (d->stdin_mst,
  573. readbuf,
  574. ret,
  575. GNUNET_NO,
  576. GNUNET_NO);
  577. }
  578. return 0;
  579. }
  580. /**
  581. * Message callback
  582. *
  583. * @param msg message we received.
  584. * @return #GNUNET_OK on success,
  585. * #GNUNET_NO to stop further processing due to disconnect (no error)
  586. * #GNUNET_SYSERR to stop further processing due to error
  587. */
  588. static int
  589. stdin_receiver (void *cls,
  590. const struct GNUNET_MessageHeader *msg)
  591. {
  592. struct AudioMessage *audio;
  593. size_t b_len;
  594. printf ("stdin receiver \n ");
  595. dump_buffer (sizeof(msg),
  596. (const unsigned char *) msg);
  597. switch (ntohs (msg->type))
  598. {
  599. case GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO:
  600. audio = (struct AudioMessage *) msg;
  601. b_len = ntohs (audio->header.size) - sizeof(struct AudioMessage);
  602. printf ("feeding buffer to gst \n ");
  603. feed_buffer_to_gst ((const char *) &audio[1], b_len, cls);
  604. break;
  605. default:
  606. printf ("No audio message: %u \n ", ntohs (msg->type));
  607. break;
  608. }
  609. return GNUNET_OK;
  610. }
  611. GstBin *
  612. get_app (GNUNET_gstData *d, int type)
  613. {
  614. GstBin *bin;
  615. GstPad *pad, *ghostpad;
  616. if (type == SOURCE)
  617. {
  618. bin = GST_BIN (gst_bin_new ("Gnunet appsrc"));
  619. GNUNET_assert (GNUNET_OK ==
  620. GNUNET_log_setup ("gnunet-helper-audio-playback",
  621. "WARNING",
  622. NULL));
  623. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  624. "Audio playback starts\n");
  625. printf (" creating appsrc \n ");
  626. // d->audio_message.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO);
  627. // d->audio_message = GNUNET_malloc (UINT16_MAX);
  628. // d->audio_message = (AudioMessage*)malloc(sizeof(struct AudioMessage));
  629. // d->audio_message = GNUNET_malloc(sizeof(struct AudioMessage));
  630. // d->audio_message.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO);
  631. d->stdin_mst = GNUNET_MST_create (&stdin_receiver, d);
  632. if (d->stdin_mst == NULL)
  633. printf ("stdin_mst = NULL");
  634. d->appsrc = gst_element_factory_make ("appsrc", "appsrc");
  635. gst_bin_add_many (bin, d->appsrc, NULL);
  636. // gst_element_link_many ( encoder, muxer, NULL);
  637. pad = gst_element_get_static_pad (d->appsrc, "src");
  638. ghostpad = gst_ghost_pad_new ("src", pad);
  639. }
  640. if (type == SINK)
  641. {
  642. bin = GST_BIN (gst_bin_new ("Gnunet appsink"));
  643. GNUNET_assert (GNUNET_OK ==
  644. GNUNET_log_setup ("gnunet-helper-audio-record",
  645. "WARNING",
  646. NULL));
  647. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  648. "Audio source starts\n");
  649. d->appsink = gst_element_factory_make ("appsink", "appsink");
  650. // Move this out of here!
  651. d->audio_message = GNUNET_malloc (UINT16_MAX);
  652. (d->audio_message)->header.type = htons (
  653. GNUNET_MESSAGE_TYPE_CONVERSATION_AUDIO);
  654. g_object_set (G_OBJECT (d->appsink), "emit-signals", TRUE, "sync", TRUE,
  655. NULL);
  656. g_signal_connect (d->appsink, "new-sample",
  657. G_CALLBACK (on_appsink_new_sample), &d);
  658. gst_bin_add_many (bin, d->appsink, NULL);
  659. // gst_element_link_many ( encoder, muxer, NULL);
  660. pad = gst_element_get_static_pad (d->appsink, "sink");
  661. ghostpad = gst_ghost_pad_new ("sink", pad);
  662. }
  663. /* set the bin pads */
  664. gst_pad_set_active (ghostpad, TRUE);
  665. gst_element_add_pad (GST_ELEMENT (bin), ghostpad);
  666. gst_object_unref (pad);
  667. return bin;
  668. }
  669. extern GstBin *
  670. get_coder (GNUNET_gstData *d, int type)
  671. {
  672. GstBin *bin;
  673. GstPad *srcpad, *sinkpad, *srcghostpad, *sinkghostpad;
  674. GstCaps *rtpcaps;
  675. GstElement *encoder, *muxer, *decoder, *demuxer, *jitterbuffer,
  676. *rtpcapsfilter;
  677. if (d->usertp == TRUE)
  678. {
  679. /*
  680. * application/x-rtp, media=(string)audio, clock-rate=(int)48000, encoding-name=(string)OPUS, sprop-maxcapturerate=(string)48000, sprop-stereo=(string)0, payload=(int)96, encoding-params=(string)2, ssrc=(uint)630297634, timestamp-offset=(uint)678334141, seqnum-offset=(uint)16938 */
  681. /*
  682. rtpcaps = gst_caps_new_simple ("application/x-rtp",
  683. "media", G_TYPE_STRING, "audio",
  684. "clock-rate", G_TYPE_INT, SAMPLING_RATE,
  685. "encoding-name", G_TYPE_STRING, "OPUS",
  686. "payload", G_TYPE_INT, 96,
  687. "sprop-stereo", G_TYPE_STRING, "0",
  688. "encoding-params", G_TYPE_STRING, "2",
  689. NULL);
  690. */ rtpcaps = gst_caps_new_simple ("application/x-rtp",
  691. "media", G_TYPE_STRING, "audio",
  692. "clock-rate", G_TYPE_INT, SAMPLING_RATE,
  693. "encoding-name", G_TYPE_STRING, "OPUS",
  694. "payload", G_TYPE_INT, 96,
  695. "sprop-stereo", G_TYPE_STRING, "0",
  696. "encoding-params", G_TYPE_STRING, "2",
  697. NULL);
  698. rtpcapsfilter = gst_element_factory_make ("capsfilter", "rtpcapsfilter");
  699. g_object_set (G_OBJECT (rtpcapsfilter),
  700. "caps", rtpcaps,
  701. NULL);
  702. gst_caps_unref (rtpcaps);
  703. }
  704. if (type == ENCODER)
  705. {
  706. bin = GST_BIN (gst_bin_new ("Gnunet audioencoder"));
  707. encoder = gst_element_factory_make ("opusenc", "opus-encoder");
  708. if (d->usertp == TRUE)
  709. {
  710. muxer = gst_element_factory_make ("rtpopuspay", "rtp-payloader");
  711. }
  712. else
  713. {
  714. muxer = gst_element_factory_make ("oggmux", "ogg-muxer");
  715. }
  716. g_object_set (G_OBJECT (encoder),
  717. /* "bitrate", 64000, */
  718. /* "bandwidth", OPUS_BANDWIDTH_FULLBAND, */
  719. "inband-fec", INBAND_FEC_MODE,
  720. "packet-loss-percentage", PACKET_LOSS_PERCENTAGE,
  721. "max-payload-size", MAX_PAYLOAD_SIZE,
  722. "audio", TRUE, /* VoIP, not audio */
  723. "frame-size", OPUS_FRAME_SIZE,
  724. NULL);
  725. if (d->usertp != TRUE)
  726. {
  727. g_object_set (G_OBJECT (muxer),
  728. "max-delay", OGG_MAX_DELAY,
  729. "max-page-delay", OGG_MAX_PAGE_DELAY,
  730. NULL);
  731. }
  732. gst_bin_add_many (bin, encoder, muxer, NULL);
  733. gst_element_link_many (encoder, muxer, NULL);
  734. sinkpad = gst_element_get_static_pad (encoder, "sink");
  735. sinkghostpad = gst_ghost_pad_new ("sink", sinkpad);
  736. srcpad = gst_element_get_static_pad (muxer, "src");
  737. srcghostpad = gst_ghost_pad_new ("src", srcpad);
  738. }
  739. if (type == DECODER)
  740. {
  741. bin = GST_BIN (gst_bin_new ("Gnunet audiodecoder"));
  742. // decoder
  743. if (d->usertp == TRUE)
  744. {
  745. demuxer = gst_element_factory_make ("rtpopusdepay", "ogg-demuxer");
  746. jitterbuffer = gst_element_factory_make ("rtpjitterbuffer",
  747. "rtpjitterbuffer");
  748. }
  749. else
  750. {
  751. demuxer = gst_element_factory_make ("oggdemux", "ogg-demuxer");
  752. }
  753. decoder = gst_element_factory_make ("opusdec", "opus-decoder");
  754. if (d->usertp == TRUE)
  755. {
  756. gst_bin_add_many (bin, rtpcapsfilter, jitterbuffer, demuxer, decoder,
  757. NULL);
  758. gst_element_link_many (rtpcapsfilter, jitterbuffer, demuxer, decoder,
  759. NULL);
  760. sinkpad = gst_element_get_static_pad (rtpcapsfilter, "sink");
  761. }
  762. else
  763. {
  764. gst_bin_add_many (bin, demuxer, decoder, NULL);
  765. g_signal_connect (demuxer,
  766. "pad-added",
  767. G_CALLBACK (decoder_ogg_pad_added),
  768. decoder);
  769. sinkpad = gst_element_get_static_pad (demuxer, "sink");
  770. }
  771. sinkghostpad = gst_ghost_pad_new ("sink", sinkpad);
  772. srcpad = gst_element_get_static_pad (decoder, "src");
  773. srcghostpad = gst_ghost_pad_new ("src", srcpad);
  774. }
  775. // add pads to the bin
  776. gst_pad_set_active (sinkghostpad, TRUE);
  777. gst_element_add_pad (GST_ELEMENT (bin), sinkghostpad);
  778. gst_pad_set_active (srcghostpad, TRUE);
  779. gst_element_add_pad (GST_ELEMENT (bin), srcghostpad);
  780. return bin;
  781. }
  782. extern GstBin *
  783. get_audiobin (GNUNET_gstData *d, int type)
  784. {
  785. GstBin *bin;
  786. GstElement *sink, *source, *queue, *conv, *resampler, *removesilence, *filter;
  787. GstPad *pad, *ghostpad;
  788. GstCaps *caps;
  789. if (type == SINK)
  790. {
  791. bin = GST_BIN (gst_bin_new ("Gnunet audiosink"));
  792. /* Create all the elements */
  793. if (d->dropsilence == TRUE)
  794. {
  795. queue = gst_element_factory_make ("queue", "queue");
  796. removesilence = gst_element_factory_make ("removesilence",
  797. "removesilence");
  798. }
  799. conv = gst_element_factory_make ("audioconvert", "converter");
  800. resampler = gst_element_factory_make ("audioresample", "resampler");
  801. if (d->audiobackend == AUTO)
  802. {
  803. sink = gst_element_factory_make ("autoaudiosink", "audiosink");
  804. g_signal_connect (sink, "child-added", G_CALLBACK (
  805. autoaudiosink_child_added), NULL);
  806. }
  807. if (d->audiobackend == ALSA)
  808. {
  809. sink = gst_element_factory_make ("alsaaudiosink", "audiosink");
  810. }
  811. if (d->audiobackend == JACK)
  812. {
  813. sink = gst_element_factory_make ("jackaudiosink", "audiosink");
  814. g_object_set (G_OBJECT (sink), "client-name", "gnunet", NULL);
  815. if (g_object_class_find_property
  816. (G_OBJECT_GET_CLASS (sink), "port-pattern"))
  817. {
  818. // char *portpattern = "system";
  819. g_object_set (G_OBJECT (sink), "port-pattern", d->jack_pp_out,
  820. NULL);
  821. }
  822. }
  823. if (d->audiobackend == FAKE)
  824. {
  825. sink = gst_element_factory_make ("fakesink", "audiosink");
  826. }
  827. g_object_set (sink,
  828. "buffer-time", (gint64) BUFFER_TIME,
  829. "latency-time", (gint64) LATENCY_TIME,
  830. NULL);
  831. if (d->dropsilence == TRUE)
  832. {
  833. // Do not remove silence by default
  834. g_object_set (removesilence, "remove", FALSE, NULL);
  835. g_object_set (queue, "max-size-buffers", 12, NULL);
  836. /*
  837. g_signal_connect (source,
  838. "need-data",
  839. G_CALLBACK(appsrc_need_data),
  840. NULL);
  841. g_signal_connect (source,
  842. "enough-data",
  843. G_CALLBACK(appsrc_enough_data),
  844. NULL);
  845. *//*
  846. g_signal_connect (queue,
  847. "notify::current-level-bytes",
  848. G_CALLBACK(queue_current_level),
  849. NULL);
  850. g_signal_connect (queue,
  851. "underrun",
  852. G_CALLBACK(queue_underrun),
  853. NULL);
  854. g_signal_connect (queue,
  855. "running",
  856. G_CALLBACK(queue_running),
  857. NULL);
  858. g_signal_connect (queue,
  859. "overrun",
  860. G_CALLBACK(queue_overrun),
  861. NULL);
  862. g_signal_connect (queue,
  863. "pushing",
  864. G_CALLBACK(queue_pushing),
  865. NULL);
  866. */ }
  867. gst_bin_add_many (bin, conv, resampler, sink, NULL);
  868. gst_element_link_many (conv, resampler, sink, NULL);
  869. if (d->dropsilence == TRUE)
  870. {
  871. gst_bin_add_many (bin, queue, removesilence, NULL);
  872. if (! gst_element_link_many (queue, removesilence, conv, NULL))
  873. lf ("queue, removesilence, conv ");
  874. pad = gst_element_get_static_pad (queue, "sink");
  875. }
  876. else
  877. {
  878. pad = gst_element_get_static_pad (conv, "sink");
  879. }
  880. ghostpad = gst_ghost_pad_new ("sink", pad);
  881. }
  882. else
  883. {
  884. // SOURCE
  885. bin = GST_BIN (gst_bin_new ("Gnunet audiosource"));
  886. // source = gst_element_factory_make("audiotestsrc", "audiotestsrcbla");
  887. if (d->audiobackend == AUTO)
  888. {
  889. source = gst_element_factory_make ("autoaudiosrc", "audiosource");
  890. }
  891. if (d->audiobackend == ALSA)
  892. {
  893. source = gst_element_factory_make ("alsasrc", "audiosource");
  894. }
  895. if (d->audiobackend == JACK)
  896. {
  897. source = gst_element_factory_make ("jackaudiosrc", "audiosource");
  898. }
  899. if (d->audiobackend == TEST)
  900. {
  901. source = gst_element_factory_make ("audiotestsrc", "audiosource");
  902. }
  903. filter = gst_element_factory_make ("capsfilter", "filter");
  904. conv = gst_element_factory_make ("audioconvert", "converter");
  905. resampler = gst_element_factory_make ("audioresample", "resampler");
  906. if (d->audiobackend == AUTO)
  907. {
  908. g_signal_connect (source, "child-added", G_CALLBACK (
  909. autoaudiosource_child_added), NULL);
  910. }
  911. else
  912. {
  913. if (GST_IS_AUDIO_BASE_SRC (source))
  914. g_object_set (source, "buffer-time", (gint64) BUFFER_TIME,
  915. "latency-time", (gint64) LATENCY_TIME, NULL);
  916. if (d->audiobackend == JACK)
  917. {
  918. g_object_set (G_OBJECT (source), "client-name", "gnunet", NULL);
  919. if (g_object_class_find_property
  920. (G_OBJECT_GET_CLASS (source), "port-pattern"))
  921. {
  922. char *portpattern = "moc";
  923. g_object_set (G_OBJECT (source), "port-pattern", portpattern,
  924. NULL);
  925. }
  926. }
  927. }
  928. caps = gst_caps_new_simple ("audio/x-raw",
  929. /* "format", G_TYPE_STRING, "S16LE", */
  930. /* "rate", G_TYPE_INT, SAMPLING_RATE,*/
  931. "channels", G_TYPE_INT, OPUS_CHANNELS,
  932. /* "layout", G_TYPE_STRING, "interleaved",*/
  933. NULL);
  934. g_object_set (G_OBJECT (filter),
  935. "caps", caps,
  936. NULL);
  937. gst_caps_unref (caps);
  938. gst_bin_add_many (bin, source, filter, conv, resampler, NULL);
  939. gst_element_link_many (source, filter, conv, resampler, NULL);
  940. pad = gst_element_get_static_pad (resampler, "src");
  941. /* pads */
  942. ghostpad = gst_ghost_pad_new ("src", pad);
  943. }
  944. /* set the bin pads */
  945. gst_pad_set_active (ghostpad, TRUE);
  946. gst_element_add_pad (GST_ELEMENT (bin), ghostpad);
  947. gst_object_unref (pad);
  948. return bin;
  949. }