stream.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * libdatachannel streamer example
  3. * Copyright (c) 2020 Filip Klembara (in2core)
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "stream.hpp"
  19. #include "helpers.hpp"
  20. #if _WIN32
  21. // taken from https://stackoverflow.com/questions/5801813/c-usleep-is-obsolete-workarounds-for-windows-mingw
  22. #include <windows.h>
  23. void usleep(__int64 usec)
  24. {
  25. HANDLE timer;
  26. LARGE_INTEGER ft;
  27. ft.QuadPart = -(10*usec); // Convert to 100 nanosecond interval, negative value indicates relative time
  28. timer = CreateWaitableTimer(NULL, TRUE, NULL);
  29. SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0);
  30. WaitForSingleObject(timer, INFINITE);
  31. CloseHandle(timer);
  32. }
  33. #endif
  34. void StreamSource::stop() {
  35. sampleTime_us = 0;
  36. sample = {};
  37. }
  38. StreamSource::~StreamSource() {
  39. stop();
  40. }
  41. Stream::Stream(std::shared_ptr<StreamSource> video, std::shared_ptr<StreamSource> audio): std::enable_shared_from_this<Stream>(), video(video), audio(audio) { }
  42. Stream::~Stream() {
  43. stop();
  44. }
  45. std::pair<std::shared_ptr<StreamSource>, Stream::StreamSourceType> Stream::unsafePrepareForSample() {
  46. std::shared_ptr<StreamSource> ss;
  47. StreamSourceType sst;
  48. uint64_t nextTime;
  49. if (audio->getSampleTime_us() < video->getSampleTime_us()) {
  50. ss = audio;
  51. sst = StreamSourceType::Audio;
  52. nextTime = audio->getSampleTime_us();
  53. } else {
  54. ss = video;
  55. sst = StreamSourceType::Video;
  56. nextTime = video->getSampleTime_us();
  57. }
  58. auto currentTime = currentTimeInMicroSeconds();
  59. auto elapsed = currentTime - startTime;
  60. if (nextTime > elapsed) {
  61. auto waitTime = nextTime - elapsed;
  62. mutex.unlock();
  63. usleep(waitTime);
  64. mutex.lock();
  65. }
  66. return {ss, sst};
  67. }
  68. void Stream::sendSample() {
  69. std::lock_guard lock(mutex);
  70. if (!isRunning) {
  71. return;
  72. }
  73. auto ssSST = unsafePrepareForSample();
  74. auto ss = ssSST.first;
  75. auto sst = ssSST.second;
  76. auto sample = ss->getSample();
  77. sampleHandler(sst, ss->getSampleTime_us(), sample);
  78. ss->loadNextSample();
  79. dispatchQueue.dispatch([this]() {
  80. this->sendSample();
  81. });
  82. }
  83. void Stream::onSample(std::function<void (StreamSourceType, uint64_t, rtc::binary)> handler) {
  84. sampleHandler = handler;
  85. }
  86. void Stream::start() {
  87. std::lock_guard lock(mutex);
  88. if (isRunning) {
  89. return;
  90. }
  91. _isRunning = true;
  92. startTime = currentTimeInMicroSeconds();
  93. audio->start();
  94. video->start();
  95. dispatchQueue.dispatch([this]() {
  96. this->sendSample();
  97. });
  98. }
  99. void Stream::stop() {
  100. std::lock_guard lock(mutex);
  101. if (!isRunning) {
  102. return;
  103. }
  104. _isRunning = false;
  105. dispatchQueue.removePending();
  106. audio->stop();
  107. video->stop();
  108. };