rtmp-custom.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 FTL_PREFIX "ftl://"
  95. #define SRT_PREFIX "srt://"
  96. #define RIST_PREFIX "rist://"
  97. static const char *rtmp_custom_get_protocol(void *data)
  98. {
  99. struct rtmp_custom *service = data;
  100. if (strncmp(service->server, RTMPS_PREFIX, strlen(RTMPS_PREFIX)) == 0)
  101. return "RTMPS";
  102. if (strncmp(service->server, FTL_PREFIX, strlen(FTL_PREFIX)) == 0)
  103. return "FTL";
  104. if (strncmp(service->server, SRT_PREFIX, strlen(SRT_PREFIX)) == 0)
  105. return "SRT";
  106. if (strncmp(service->server, RIST_PREFIX, strlen(RIST_PREFIX)) == 0)
  107. return "RIST";
  108. return "RTMP";
  109. }
  110. #define RTMP_PROTOCOL "rtmp"
  111. static void rtmp_custom_apply_settings(void *data, obs_data_t *video_settings,
  112. obs_data_t *audio_settings)
  113. {
  114. struct rtmp_custom *service = data;
  115. if (service->server != NULL && video_settings != NULL &&
  116. strncmp(service->server, RTMP_PROTOCOL, strlen(RTMP_PROTOCOL)) !=
  117. 0) {
  118. obs_data_set_bool(video_settings, "repeat_headers", true);
  119. obs_data_set_bool(audio_settings, "set_to_ADTS", true);
  120. }
  121. }
  122. static const char *rtmp_custom_get_connect_info(void *data, uint32_t type)
  123. {
  124. switch ((enum obs_service_connect_info)type) {
  125. case OBS_SERVICE_CONNECT_INFO_SERVER_URL:
  126. return rtmp_custom_url(data);
  127. case OBS_SERVICE_CONNECT_INFO_STREAM_ID:
  128. return rtmp_custom_key(data);
  129. case OBS_SERVICE_CONNECT_INFO_USERNAME:
  130. return rtmp_custom_username(data);
  131. case OBS_SERVICE_CONNECT_INFO_PASSWORD:
  132. return rtmp_custom_password(data);
  133. case OBS_SERVICE_CONNECT_INFO_ENCRYPT_PASSPHRASE: {
  134. const char *protocol = rtmp_custom_get_protocol(data);
  135. if ((strcmp(protocol, "SRT") == 0))
  136. return rtmp_custom_password(data);
  137. else if ((strcmp(protocol, "RIST") == 0))
  138. return rtmp_custom_key(data);
  139. break;
  140. }
  141. }
  142. return NULL;
  143. }
  144. static bool rtmp_custom_can_try_to_connect(void *data)
  145. {
  146. struct rtmp_custom *service = data;
  147. return (service->server != NULL && service->server[0] != '\0');
  148. }
  149. struct obs_service_info rtmp_custom_service = {
  150. .id = "rtmp_custom",
  151. .get_name = rtmp_custom_name,
  152. .create = rtmp_custom_create,
  153. .destroy = rtmp_custom_destroy,
  154. .update = rtmp_custom_update,
  155. .get_properties = rtmp_custom_properties,
  156. .get_protocol = rtmp_custom_get_protocol,
  157. .get_url = rtmp_custom_url,
  158. .get_key = rtmp_custom_key,
  159. .get_connect_info = rtmp_custom_get_connect_info,
  160. .get_username = rtmp_custom_username,
  161. .get_password = rtmp_custom_password,
  162. .apply_encoder_settings = rtmp_custom_apply_settings,
  163. .can_try_to_connect = rtmp_custom_can_try_to_connect,
  164. };