fileparser.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 "fileparser.hpp"
  19. #include <fstream>
  20. using namespace std;
  21. FileParser::FileParser(string directory, string extension, uint32_t samplesPerSecond, bool loop) {
  22. this->directory = directory;
  23. this->extension = extension;
  24. this->loop = loop;
  25. this->sampleDuration_us = 1000 * 1000 / samplesPerSecond;
  26. }
  27. FileParser::~FileParser() {
  28. stop();
  29. }
  30. void FileParser::start() {
  31. sampleTime_us = std::numeric_limits<uint64_t>::max() - sampleDuration_us + 1;
  32. loadNextSample();
  33. }
  34. void FileParser::stop() {
  35. sample = {};
  36. sampleTime_us = 0;
  37. counter = -1;
  38. }
  39. void FileParser::loadNextSample() {
  40. string frame_id = to_string(++counter);
  41. string url = directory + "/sample-" + frame_id + extension;
  42. ifstream source(url, ios_base::binary);
  43. if (!source) {
  44. if (loop && counter > 0) {
  45. loopTimestampOffset = sampleTime_us;
  46. counter = -1;
  47. loadNextSample();
  48. return;
  49. }
  50. sample = {};
  51. return;
  52. }
  53. vector<uint8_t> fileContents((std::istreambuf_iterator<char>(source)), std::istreambuf_iterator<char>());
  54. sample = *reinterpret_cast<vector<byte> *>(&fileContents);
  55. sampleTime_us += sampleDuration_us;
  56. }
  57. rtc::binary FileParser::getSample() {
  58. return sample;
  59. }
  60. uint64_t FileParser::getSampleTime_us() {
  61. return sampleTime_us;
  62. }
  63. uint64_t FileParser::getSampleDuration_us() {
  64. return sampleDuration_us;
  65. }