rtmp-custom.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include <obs-module.h>
  2. #include <util/dstr.h>
  3. struct rtmp_custom {
  4. char *server, *key;
  5. bool use_auth;
  6. char *username, *password;
  7. };
  8. static const char *rtmp_custom_name(void *unused)
  9. {
  10. UNUSED_PARAMETER(unused);
  11. return obs_module_text("CustomStreamingServer");
  12. }
  13. static void rtmp_custom_update(void *data, obs_data_t *settings)
  14. {
  15. struct rtmp_custom *service = data;
  16. bfree(service->server);
  17. bfree(service->key);
  18. bfree(service->username);
  19. bfree(service->password);
  20. service->server = bstrdup(obs_data_get_string(settings, "server"));
  21. service->key = bstrdup(obs_data_get_string(settings, "key"));
  22. service->use_auth = obs_data_get_bool(settings, "use_auth");
  23. service->username = bstrdup(obs_data_get_string(settings, "username"));
  24. service->password = bstrdup(obs_data_get_string(settings, "password"));
  25. }
  26. static void rtmp_custom_destroy(void *data)
  27. {
  28. struct rtmp_custom *service = data;
  29. bfree(service->server);
  30. bfree(service->key);
  31. bfree(service->username);
  32. bfree(service->password);
  33. bfree(service);
  34. }
  35. static void *rtmp_custom_create(obs_data_t *settings, obs_service_t *service)
  36. {
  37. struct rtmp_custom *data = bzalloc(sizeof(struct rtmp_custom));
  38. rtmp_custom_update(data, settings);
  39. UNUSED_PARAMETER(service);
  40. return data;
  41. }
  42. static bool use_auth_modified(obs_properties_t *ppts, obs_property_t *p, obs_data_t *settings)
  43. {
  44. bool use_auth = obs_data_get_bool(settings, "use_auth");
  45. p = obs_properties_get(ppts, "username");
  46. obs_property_set_visible(p, use_auth);
  47. p = obs_properties_get(ppts, "password");
  48. obs_property_set_visible(p, use_auth);
  49. return true;
  50. }
  51. static obs_properties_t *rtmp_custom_properties(void *unused)
  52. {
  53. UNUSED_PARAMETER(unused);
  54. obs_properties_t *ppts = obs_properties_create();
  55. obs_property_t *p;
  56. obs_properties_add_text(ppts, "server", "URL", OBS_TEXT_DEFAULT);
  57. obs_properties_add_text(ppts, "key", obs_module_text("StreamKey"), OBS_TEXT_PASSWORD);
  58. p = obs_properties_add_bool(ppts, "use_auth", obs_module_text("UseAuth"));
  59. obs_properties_add_text(ppts, "username", obs_module_text("Username"), OBS_TEXT_DEFAULT);
  60. obs_properties_add_text(ppts, "password", obs_module_text("Password"), OBS_TEXT_PASSWORD);
  61. obs_property_set_modified_callback(p, use_auth_modified);
  62. return ppts;
  63. }
  64. static const char *rtmp_custom_url(void *data)
  65. {
  66. struct rtmp_custom *service = data;
  67. return service->server;
  68. }
  69. static const char *rtmp_custom_key(void *data)
  70. {
  71. struct rtmp_custom *service = data;
  72. return service->key;
  73. }
  74. static const char *rtmp_custom_username(void *data)
  75. {
  76. struct rtmp_custom *service = data;
  77. if (!service->use_auth)
  78. return NULL;
  79. return service->username;
  80. }
  81. static const char *rtmp_custom_password(void *data)
  82. {
  83. struct rtmp_custom *service = data;
  84. if (!service->use_auth)
  85. return NULL;
  86. return service->password;
  87. }
  88. #define RTMPS_PREFIX "rtmps://"
  89. #define SRT_PREFIX "srt://"
  90. #define RIST_PREFIX "rist://"
  91. static const char *rtmp_custom_get_protocol(void *data)
  92. {
  93. struct rtmp_custom *service = data;
  94. if (strncmp(service->server, RTMPS_PREFIX, strlen(RTMPS_PREFIX)) == 0)
  95. return "RTMPS";
  96. if (strncmp(service->server, SRT_PREFIX, strlen(SRT_PREFIX)) == 0)
  97. return "SRT";
  98. if (strncmp(service->server, RIST_PREFIX, strlen(RIST_PREFIX)) == 0)
  99. return "RIST";
  100. return "RTMP";
  101. }
  102. static void rtmp_custom_apply_settings(void *data, obs_data_t *video_settings, obs_data_t *audio_settings)
  103. {
  104. struct rtmp_custom *service = data;
  105. const char *protocol = rtmp_custom_get_protocol(service);
  106. bool has_mpegts = false;
  107. bool is_rtmp = false;
  108. if (strcmp(protocol, "SRT") == 0 || strcmp(protocol, "RIST") == 0)
  109. has_mpegts = true;
  110. if (strcmp(protocol, "RTMP") == 0 || strcmp(protocol, "RTMPS") == 0)
  111. is_rtmp = true;
  112. if (!is_rtmp && video_settings != NULL)
  113. obs_data_set_bool(video_settings, "repeat_headers", true);
  114. if (has_mpegts && audio_settings != NULL)
  115. obs_data_set_bool(audio_settings, "set_to_ADTS", true);
  116. }
  117. static const char *rtmp_custom_get_connect_info(void *data, uint32_t type)
  118. {
  119. switch ((enum obs_service_connect_info)type) {
  120. case OBS_SERVICE_CONNECT_INFO_SERVER_URL:
  121. return rtmp_custom_url(data);
  122. case OBS_SERVICE_CONNECT_INFO_STREAM_ID:
  123. return rtmp_custom_key(data);
  124. case OBS_SERVICE_CONNECT_INFO_USERNAME:
  125. return rtmp_custom_username(data);
  126. case OBS_SERVICE_CONNECT_INFO_PASSWORD:
  127. return rtmp_custom_password(data);
  128. case OBS_SERVICE_CONNECT_INFO_ENCRYPT_PASSPHRASE: {
  129. const char *protocol = rtmp_custom_get_protocol(data);
  130. if ((strcmp(protocol, "SRT") == 0))
  131. return rtmp_custom_password(data);
  132. else if ((strcmp(protocol, "RIST") == 0))
  133. return rtmp_custom_key(data);
  134. break;
  135. }
  136. case OBS_SERVICE_CONNECT_INFO_BEARER_TOKEN:
  137. return NULL;
  138. }
  139. return NULL;
  140. }
  141. static bool rtmp_custom_can_try_to_connect(void *data)
  142. {
  143. struct rtmp_custom *service = data;
  144. return (service->server != NULL && service->server[0] != '\0');
  145. }
  146. struct obs_service_info rtmp_custom_service = {
  147. .id = "rtmp_custom",
  148. .get_name = rtmp_custom_name,
  149. .create = rtmp_custom_create,
  150. .destroy = rtmp_custom_destroy,
  151. .update = rtmp_custom_update,
  152. .get_properties = rtmp_custom_properties,
  153. .get_protocol = rtmp_custom_get_protocol,
  154. .get_url = rtmp_custom_url,
  155. .get_key = rtmp_custom_key,
  156. .get_connect_info = rtmp_custom_get_connect_info,
  157. .get_username = rtmp_custom_username,
  158. .get_password = rtmp_custom_password,
  159. .apply_encoder_settings = rtmp_custom_apply_settings,
  160. .can_try_to_connect = rtmp_custom_can_try_to_connect,
  161. };