null-output.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <util/threading.h>
  15. #include <obs-module.h>
  16. struct null_output {
  17. obs_output_t *output;
  18. pthread_t stop_thread;
  19. bool stop_thread_active;
  20. };
  21. static const char *null_output_getname(void *unused)
  22. {
  23. UNUSED_PARAMETER(unused);
  24. return "Null Encoding Output";
  25. }
  26. static void *null_output_create(obs_data_t *settings, obs_output_t *output)
  27. {
  28. struct null_output *context = bzalloc(sizeof(*context));
  29. context->output = output;
  30. UNUSED_PARAMETER(settings);
  31. return context;
  32. }
  33. static void null_output_destroy(void *data)
  34. {
  35. struct null_output *context = data;
  36. if (context->stop_thread_active)
  37. pthread_join(context->stop_thread, NULL);
  38. bfree(context);
  39. }
  40. static bool null_output_start(void *data)
  41. {
  42. struct null_output *context = data;
  43. if (!obs_output_can_begin_data_capture2(context->output))
  44. return false;
  45. if (!obs_output_initialize_encoders2(context->output))
  46. return false;
  47. if (context->stop_thread_active)
  48. pthread_join(context->stop_thread, NULL);
  49. obs_output_begin_data_capture2(context->output);
  50. return true;
  51. }
  52. static void *stop_thread(void *data)
  53. {
  54. struct null_output *context = data;
  55. obs_output_end_data_capture(context->output);
  56. context->stop_thread_active = false;
  57. return NULL;
  58. }
  59. static void null_output_stop(void *data, uint64_t ts)
  60. {
  61. struct null_output *context = data;
  62. UNUSED_PARAMETER(ts);
  63. context->stop_thread_active = pthread_create(&context->stop_thread,
  64. NULL, stop_thread,
  65. data) == 0;
  66. }
  67. static void null_output_data(void *data, struct encoder_packet *packet)
  68. {
  69. UNUSED_PARAMETER(data);
  70. UNUSED_PARAMETER(packet);
  71. }
  72. struct obs_output_info null_output_info = {
  73. .id = "null_output",
  74. .flags = OBS_OUTPUT_AV | OBS_OUTPUT_ENCODED,
  75. .get_name = null_output_getname,
  76. .create = null_output_create,
  77. .destroy = null_output_destroy,
  78. .start = null_output_start,
  79. .stop = null_output_stop,
  80. .encoded_packet = null_output_data,
  81. };