rtmp-custom.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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,
  43. obs_data_t *settings)
  44. {
  45. bool use_auth = obs_data_get_bool(settings, "use_auth");
  46. p = obs_properties_get(ppts, "username");
  47. obs_property_set_visible(p, use_auth);
  48. p = obs_properties_get(ppts, "password");
  49. obs_property_set_visible(p, use_auth);
  50. return true;
  51. }
  52. static obs_properties_t *rtmp_custom_properties(void *unused)
  53. {
  54. UNUSED_PARAMETER(unused);
  55. obs_properties_t *ppts = obs_properties_create();
  56. obs_property_t *p;
  57. obs_properties_add_text(ppts, "server", "URL", OBS_TEXT_DEFAULT);
  58. obs_properties_add_text(ppts, "key", obs_module_text("StreamKey"),
  59. OBS_TEXT_PASSWORD);
  60. p = obs_properties_add_bool(ppts, "use_auth",
  61. obs_module_text("UseAuth"));
  62. obs_properties_add_text(ppts, "username", obs_module_text("Username"),
  63. OBS_TEXT_DEFAULT);
  64. obs_properties_add_text(ppts, "password", obs_module_text("Password"),
  65. OBS_TEXT_PASSWORD);
  66. obs_property_set_modified_callback(p, use_auth_modified);
  67. return ppts;
  68. }
  69. static const char *rtmp_custom_url(void *data)
  70. {
  71. struct rtmp_custom *service = data;
  72. return service->server;
  73. }
  74. static const char *rtmp_custom_key(void *data)
  75. {
  76. struct rtmp_custom *service = data;
  77. return service->key;
  78. }
  79. static const char *rtmp_custom_username(void *data)
  80. {
  81. struct rtmp_custom *service = data;
  82. if (!service->use_auth)
  83. return NULL;
  84. return service->username;
  85. }
  86. static const char *rtmp_custom_password(void *data)
  87. {
  88. struct rtmp_custom *service = data;
  89. if (!service->use_auth)
  90. return NULL;
  91. return service->password;
  92. }
  93. #define RTMPS_PREFIX "rtmps://"
  94. #define SRT_PREFIX "srt://"
  95. #define RIST_PREFIX "rist://"
  96. static const char *rtmp_custom_get_protocol(void *data)
  97. {
  98. struct rtmp_custom *service = data;
  99. if (strncmp(service->server, RTMPS_PREFIX, strlen(RTMPS_PREFIX)) == 0)
  100. return "RTMPS";
  101. if (strncmp(service->server, SRT_PREFIX, strlen(SRT_PREFIX)) == 0)
  102. return "SRT";
  103. if (strncmp(service->server, RIST_PREFIX, strlen(RIST_PREFIX)) == 0)
  104. return "RIST";
  105. return "RTMP";
  106. }
  107. static void rtmp_custom_apply_settings(void *data, obs_data_t *video_settings,
  108. obs_data_t *audio_settings)
  109. {
  110. struct rtmp_custom *service = data;
  111. const char *protocol = rtmp_custom_get_protocol(service);
  112. bool has_mpegts = false;
  113. bool is_rtmp = false;
  114. if (strcmp(protocol, "SRT") == 0 || strcmp(protocol, "RIST") == 0)
  115. has_mpegts = true;
  116. if (strcmp(protocol, "RTMP") == 0 || strcmp(protocol, "RTMPS") == 0)
  117. is_rtmp = true;
  118. if (!is_rtmp && video_settings != NULL)
  119. obs_data_set_bool(video_settings, "repeat_headers", true);
  120. if (has_mpegts && audio_settings != NULL)
  121. obs_data_set_bool(audio_settings, "set_to_ADTS", true);
  122. }
  123. static const char *rtmp_custom_get_connect_info(void *data, uint32_t type)
  124. {
  125. switch ((enum obs_service_connect_info)type) {
  126. case OBS_SERVICE_CONNECT_INFO_SERVER_URL:
  127. return rtmp_custom_url(data);
  128. case OBS_SERVICE_CONNECT_INFO_STREAM_ID:
  129. return rtmp_custom_key(data);
  130. case OBS_SERVICE_CONNECT_INFO_USERNAME:
  131. return rtmp_custom_username(data);
  132. case OBS_SERVICE_CONNECT_INFO_PASSWORD:
  133. return rtmp_custom_password(data);
  134. case OBS_SERVICE_CONNECT_INFO_ENCRYPT_PASSPHRASE: {
  135. const char *protocol = rtmp_custom_get_protocol(data);
  136. if ((strcmp(protocol, "SRT") == 0))
  137. return rtmp_custom_password(data);
  138. else if ((strcmp(protocol, "RIST") == 0))
  139. return rtmp_custom_key(data);
  140. break;
  141. }
  142. case OBS_SERVICE_CONNECT_INFO_BEARER_TOKEN:
  143. return NULL;
  144. }
  145. return NULL;
  146. }
  147. static bool rtmp_custom_can_try_to_connect(void *data)
  148. {
  149. struct rtmp_custom *service = data;
  150. return (service->server != NULL && service->server[0] != '\0');
  151. }
  152. struct obs_service_info rtmp_custom_service = {
  153. .id = "rtmp_custom",
  154. .get_name = rtmp_custom_name,
  155. .create = rtmp_custom_create,
  156. .destroy = rtmp_custom_destroy,
  157. .update = rtmp_custom_update,
  158. .get_properties = rtmp_custom_properties,
  159. .get_protocol = rtmp_custom_get_protocol,
  160. .get_url = rtmp_custom_url,
  161. .get_key = rtmp_custom_key,
  162. .get_connect_info = rtmp_custom_get_connect_info,
  163. .get_username = rtmp_custom_username,
  164. .get_password = rtmp_custom_password,
  165. .apply_encoder_settings = rtmp_custom_apply_settings,
  166. .can_try_to_connect = rtmp_custom_can_try_to_connect,
  167. };