stream.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #ifdef _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. #else
  34. #include <unistd.h>
  35. #endif
  36. Stream::Stream(std::shared_ptr<StreamSource> video, std::shared_ptr<StreamSource> audio):
  37. std::enable_shared_from_this<Stream>(), video(video), audio(audio) { }
  38. Stream::~Stream() {
  39. stop();
  40. }
  41. std::pair<std::shared_ptr<StreamSource>, Stream::StreamSourceType> Stream::unsafePrepareForSample() {
  42. std::shared_ptr<StreamSource> ss;
  43. StreamSourceType sst;
  44. uint64_t nextTime;
  45. if (audio->getSampleTime_us() < video->getSampleTime_us()) {
  46. ss = audio;
  47. sst = StreamSourceType::Audio;
  48. nextTime = audio->getSampleTime_us();
  49. } else {
  50. ss = video;
  51. sst = StreamSourceType::Video;
  52. nextTime = video->getSampleTime_us();
  53. }
  54. auto currentTime = currentTimeInMicroSeconds();
  55. auto elapsed = currentTime - startTime;
  56. if (nextTime > elapsed) {
  57. auto waitTime = nextTime - elapsed;
  58. mutex.unlock();
  59. usleep(waitTime);
  60. mutex.lock();
  61. }
  62. return {ss, sst};
  63. }
  64. void Stream::sendSample() {
  65. std::lock_guard lock(mutex);
  66. if (!isRunning) {
  67. return;
  68. }
  69. auto ssSST = unsafePrepareForSample();
  70. auto ss = ssSST.first;
  71. auto sst = ssSST.second;
  72. auto sample = ss->getSample();
  73. sampleHandler(sst, ss->getSampleTime_us(), sample);
  74. ss->loadNextSample();
  75. dispatchQueue.dispatch([this]() {
  76. this->sendSample();
  77. });
  78. }
  79. void Stream::onSample(std::function<void (StreamSourceType, uint64_t, rtc::binary)> handler) {
  80. sampleHandler = handler;
  81. }
  82. void Stream::start() {
  83. std::lock_guard lock(mutex);
  84. if (isRunning) {
  85. return;
  86. }
  87. _isRunning = true;
  88. startTime = currentTimeInMicroSeconds();
  89. audio->start();
  90. video->start();
  91. dispatchQueue.dispatch([this]() {
  92. this->sendSample();
  93. });
  94. }
  95. void Stream::stop() {
  96. std::lock_guard lock(mutex);
  97. if (!isRunning) {
  98. return;
  99. }
  100. _isRunning = false;
  101. dispatchQueue.removePending();
  102. audio->stop();
  103. video->stop();
  104. };