StreamTracks.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "StreamTracks.h"
  6. #include "mozilla/Logging.h"
  7. #include <algorithm>
  8. namespace mozilla {
  9. extern LazyLogModule gMediaStreamGraphLog;
  10. #define STREAM_LOG(type, msg) MOZ_LOG(gMediaStreamGraphLog, type, msg)
  11. #ifdef DEBUG
  12. void
  13. StreamTracks::DumpTrackInfo() const
  14. {
  15. STREAM_LOG(LogLevel::Info, ("DumpTracks: mTracksKnownTime %lld", mTracksKnownTime));
  16. for (uint32_t i = 0; i < mTracks.Length(); ++i) {
  17. Track* track = mTracks[i];
  18. if (track->IsEnded()) {
  19. STREAM_LOG(LogLevel::Info, ("Track[%d] %d: ended", i, track->GetID()));
  20. } else {
  21. STREAM_LOG(LogLevel::Info, ("Track[%d] %d: %lld", i, track->GetID(),
  22. track->GetEnd()));
  23. }
  24. }
  25. }
  26. #endif
  27. StreamTime
  28. StreamTracks::GetEnd() const
  29. {
  30. StreamTime t = mTracksKnownTime;
  31. for (uint32_t i = 0; i < mTracks.Length(); ++i) {
  32. Track* track = mTracks[i];
  33. if (!track->IsEnded()) {
  34. t = std::min(t, track->GetEnd());
  35. }
  36. }
  37. return t;
  38. }
  39. StreamTime
  40. StreamTracks::GetAllTracksEnd() const
  41. {
  42. if (mTracksKnownTime < STREAM_TIME_MAX) {
  43. // A track might be added.
  44. return STREAM_TIME_MAX;
  45. }
  46. StreamTime t = 0;
  47. for (uint32_t i = 0; i < mTracks.Length(); ++i) {
  48. Track* track = mTracks[i];
  49. if (!track->IsEnded()) {
  50. return STREAM_TIME_MAX;
  51. }
  52. t = std::max(t, track->GetEnd());
  53. }
  54. return t;
  55. }
  56. StreamTracks::Track*
  57. StreamTracks::FindTrack(TrackID aID)
  58. {
  59. if (aID == TRACK_NONE || mTracks.IsEmpty()) {
  60. return nullptr;
  61. }
  62. // The tracks are sorted by ID. We can use a binary search.
  63. uint32_t left = 0, right = mTracks.Length() - 1;
  64. while (left <= right) {
  65. uint32_t middle = (left + right) / 2;
  66. if (mTracks[middle]->GetID() == aID) {
  67. return mTracks[middle];
  68. }
  69. if (mTracks[middle]->GetID() > aID) {
  70. if (middle == 0) {
  71. break;
  72. }
  73. right = middle - 1;
  74. } else {
  75. left = middle + 1;
  76. }
  77. }
  78. return nullptr;
  79. }
  80. void
  81. StreamTracks::ForgetUpTo(StreamTime aTime)
  82. {
  83. // Only prune if there is a reasonable chunk (50ms @ 48kHz) to forget, so we
  84. // don't spend too much time pruning segments.
  85. const StreamTime minChunkSize = 2400;
  86. if (aTime < mForgottenTime + minChunkSize) {
  87. return;
  88. }
  89. mForgottenTime = aTime;
  90. for (uint32_t i = 0; i < mTracks.Length(); ++i) {
  91. Track* track = mTracks[i];
  92. StreamTime forgetTo = std::min(track->GetEnd() - 1, aTime);
  93. track->ForgetUpTo(forgetTo);
  94. }
  95. }
  96. void
  97. StreamTracks::Clear()
  98. {
  99. mTracks.Clear();
  100. }
  101. } // namespace mozilla