You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.0KB

  1. #include <math.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #if defined(OS_WINDOWS) || defined(_WIN32) || defined(_WIN64)
  6. #include <windows.h>
  7. #include <wincrypt.h>
  8. uint32_t rtp_ssrc(void)
  9. {
  10. uint32_t seed;
  11. HCRYPTPROV provider;
  12. seed = (uint32_t)rand();
  13. if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
  14. CryptGenRandom(provider, sizeof(seed), (PBYTE)&seed);
  15. CryptReleaseContext(provider, 0);
  16. }
  17. return seed;
  18. }
  19. #elif defined(OS_LINUX) || defined(OS_MAC)
  20. #include <stdio.h>
  21. #include <fcntl.h>
  22. #include <unistd.h>
  23. static int read_random(uint32_t *dst, const char *file)
  24. {
  25. int fd = open(file, O_RDONLY);
  26. int err = -1;
  27. if (fd == -1)
  28. return -1;
  29. err = (int)read(fd, dst, sizeof(*dst));
  30. close(fd);
  31. return err;
  32. }
  33. uint32_t rtp_ssrc(void)
  34. {
  35. uint32_t seed;
  36. if (read_random(&seed, "/dev/urandom") == sizeof(seed))
  37. return seed;
  38. if (read_random(&seed, "/dev/random") == sizeof(seed))
  39. return seed;
  40. return (uint32_t)rand();
  41. }
  42. #else
  43. uint32_t rtp_ssrc(void)
  44. {
  45. return rand();
  46. }
  47. #endif