/TMessagesProj/src/main/java/xyz/pwrtelegram/messenger/exoplayer/TimeRange.java

https://github.com/pwrtelegram/pwrtelegram-app · Java · 203 lines · 120 code · 25 blank · 58 comment · 30 complexity · f1be96a2084f8ff34777a6b797f30786 MD5 · raw file

  1. /*
  2. * Copyright (C) 2014 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package xyz.pwrtelegram.messenger.exoplayer;
  17. import xyz.pwrtelegram.messenger.exoplayer.util.Clock;
  18. import android.os.SystemClock;
  19. /**
  20. * A container to store a start and end time in microseconds.
  21. */
  22. public interface TimeRange {
  23. /**
  24. * Whether the range is static, meaning repeated calls to {@link #getCurrentBoundsMs(long[])}
  25. * or {@link #getCurrentBoundsUs(long[])} will return identical results.
  26. *
  27. * @return Whether the range is static.
  28. */
  29. public boolean isStatic();
  30. /**
  31. * Returns the start and end times (in milliseconds) of the TimeRange in the provided array,
  32. * or creates a new one.
  33. *
  34. * @param out An array to store the start and end times; can be null.
  35. * @return An array containing the start time (index 0) and end time (index 1) in milliseconds.
  36. */
  37. public long[] getCurrentBoundsMs(long[] out);
  38. /**
  39. * Returns the start and end times (in microseconds) of the TimeRange in the provided array,
  40. * or creates a new one.
  41. *
  42. * @param out An array to store the start and end times; can be null.
  43. * @return An array containing the start time (index 0) and end time (index 1) in microseconds.
  44. */
  45. public long[] getCurrentBoundsUs(long[] out);
  46. /**
  47. * A static {@link TimeRange}.
  48. */
  49. public static final class StaticTimeRange implements TimeRange {
  50. private final long startTimeUs;
  51. private final long endTimeUs;
  52. /**
  53. * @param startTimeUs The beginning of the range.
  54. * @param endTimeUs The end of the range.
  55. */
  56. public StaticTimeRange(long startTimeUs, long endTimeUs) {
  57. this.startTimeUs = startTimeUs;
  58. this.endTimeUs = endTimeUs;
  59. }
  60. @Override
  61. public boolean isStatic() {
  62. return true;
  63. }
  64. @Override
  65. public long[] getCurrentBoundsMs(long[] out) {
  66. out = getCurrentBoundsUs(out);
  67. out[0] /= 1000;
  68. out[1] /= 1000;
  69. return out;
  70. }
  71. @Override
  72. public long[] getCurrentBoundsUs(long[] out) {
  73. if (out == null || out.length < 2) {
  74. out = new long[2];
  75. }
  76. out[0] = startTimeUs;
  77. out[1] = endTimeUs;
  78. return out;
  79. }
  80. @Override
  81. public int hashCode() {
  82. int result = 17;
  83. result = 31 * result + (int) startTimeUs;
  84. result = 31 * result + (int) endTimeUs;
  85. return result;
  86. }
  87. @Override
  88. public boolean equals(Object obj) {
  89. if (obj == this) {
  90. return true;
  91. }
  92. if (obj == null || getClass() != obj.getClass()) {
  93. return false;
  94. }
  95. StaticTimeRange other = (StaticTimeRange) obj;
  96. return other.startTimeUs == startTimeUs
  97. && other.endTimeUs == endTimeUs;
  98. }
  99. }
  100. /**
  101. * A dynamic {@link TimeRange}.
  102. */
  103. public static final class DynamicTimeRange implements TimeRange {
  104. private final long minStartTimeUs;
  105. private final long maxEndTimeUs;
  106. private final long elapsedRealtimeAtStartUs;
  107. private final long bufferDepthUs;
  108. private final Clock systemClock;
  109. /**
  110. * @param minStartTimeUs A lower bound on the beginning of the range.
  111. * @param maxEndTimeUs An upper bound on the end of the range.
  112. * @param elapsedRealtimeAtStartUs The value of {@link SystemClock#elapsedRealtime()},
  113. * multiplied by 1000, corresponding to a media time of zero.
  114. * @param bufferDepthUs The buffer depth of the media, or -1.
  115. * @param systemClock A system clock.
  116. */
  117. public DynamicTimeRange(long minStartTimeUs, long maxEndTimeUs, long elapsedRealtimeAtStartUs,
  118. long bufferDepthUs, Clock systemClock) {
  119. this.minStartTimeUs = minStartTimeUs;
  120. this.maxEndTimeUs = maxEndTimeUs;
  121. this.elapsedRealtimeAtStartUs = elapsedRealtimeAtStartUs;
  122. this.bufferDepthUs = bufferDepthUs;
  123. this.systemClock = systemClock;
  124. }
  125. @Override
  126. public boolean isStatic() {
  127. return false;
  128. }
  129. @Override
  130. public long[] getCurrentBoundsMs(long[] out) {
  131. out = getCurrentBoundsUs(out);
  132. out[0] /= 1000;
  133. out[1] /= 1000;
  134. return out;
  135. }
  136. @Override
  137. public long[] getCurrentBoundsUs(long[] out) {
  138. if (out == null || out.length < 2) {
  139. out = new long[2];
  140. }
  141. // Don't allow the end time to be greater than the total elapsed time.
  142. long currentEndTimeUs = Math.min(maxEndTimeUs,
  143. (systemClock.elapsedRealtime() * 1000) - elapsedRealtimeAtStartUs);
  144. long currentStartTimeUs = minStartTimeUs;
  145. if (bufferDepthUs != -1) {
  146. // Don't allow the start time to be less than the current end time minus the buffer depth.
  147. currentStartTimeUs = Math.max(currentStartTimeUs,
  148. currentEndTimeUs - bufferDepthUs);
  149. }
  150. out[0] = currentStartTimeUs;
  151. out[1] = currentEndTimeUs;
  152. return out;
  153. }
  154. @Override
  155. public int hashCode() {
  156. int result = 17;
  157. result = 31 * result + (int) minStartTimeUs;
  158. result = 31 * result + (int) maxEndTimeUs;
  159. result = 31 * result + (int) elapsedRealtimeAtStartUs;
  160. result = 31 * result + (int) bufferDepthUs;
  161. return result;
  162. }
  163. @Override
  164. public boolean equals(Object obj) {
  165. if (obj == this) {
  166. return true;
  167. }
  168. if (obj == null || getClass() != obj.getClass()) {
  169. return false;
  170. }
  171. DynamicTimeRange other = (DynamicTimeRange) obj;
  172. return other.minStartTimeUs == minStartTimeUs
  173. && other.maxEndTimeUs == maxEndTimeUs
  174. && other.elapsedRealtimeAtStartUs == elapsedRealtimeAtStartUs
  175. && other.bufferDepthUs == bufferDepthUs;
  176. }
  177. }
  178. }