/TMessagesProj/src/main/java/com/google/android/exoplayer2/trackselection/RandomTrackSelection.java

https://github.com/TelePlusDev/TelePlus-Android · Java · 132 lines · 71 code · 18 blank · 43 comment · 8 complexity · cd57b2f6d9ead8d88b3c620dc30a6b81 MD5 · raw file

  1. /*
  2. * Copyright (C) 2016 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 com.google.android.exoplayer2.trackselection;
  17. import android.os.SystemClock;
  18. import android.support.annotation.Nullable;
  19. import com.google.android.exoplayer2.C;
  20. import com.google.android.exoplayer2.source.TrackGroup;
  21. import com.google.android.exoplayer2.upstream.BandwidthMeter;
  22. import java.util.Random;
  23. /**
  24. * A {@link TrackSelection} whose selected track is updated randomly.
  25. */
  26. public final class RandomTrackSelection extends BaseTrackSelection {
  27. /**
  28. * Factory for {@link RandomTrackSelection} instances.
  29. */
  30. public static final class Factory implements TrackSelection.Factory {
  31. private final Random random;
  32. public Factory() {
  33. random = new Random();
  34. }
  35. /**
  36. * @param seed A seed for the {@link Random} instance used by the factory.
  37. */
  38. public Factory(int seed) {
  39. random = new Random(seed);
  40. }
  41. @Override
  42. public RandomTrackSelection createTrackSelection(
  43. TrackGroup group, BandwidthMeter bandwidthMeter, int... tracks) {
  44. return new RandomTrackSelection(group, tracks, random);
  45. }
  46. }
  47. private final Random random;
  48. private int selectedIndex;
  49. /**
  50. * @param group The {@link TrackGroup}. Must not be null.
  51. * @param tracks The indices of the selected tracks within the {@link TrackGroup}. Must not be
  52. * null or empty. May be in any order.
  53. */
  54. public RandomTrackSelection(TrackGroup group, int... tracks) {
  55. super(group, tracks);
  56. random = new Random();
  57. selectedIndex = random.nextInt(length);
  58. }
  59. /**
  60. * @param group The {@link TrackGroup}. Must not be null.
  61. * @param tracks The indices of the selected tracks within the {@link TrackGroup}. Must not be
  62. * null or empty. May be in any order.
  63. * @param seed A seed for the {@link Random} instance used to update the selected track.
  64. */
  65. public RandomTrackSelection(TrackGroup group, int[] tracks, long seed) {
  66. this(group, tracks, new Random(seed));
  67. }
  68. /**
  69. * @param group The {@link TrackGroup}. Must not be null.
  70. * @param tracks The indices of the selected tracks within the {@link TrackGroup}. Must not be
  71. * null or empty. May be in any order.
  72. * @param random A source of random numbers.
  73. */
  74. public RandomTrackSelection(TrackGroup group, int[] tracks, Random random) {
  75. super(group, tracks);
  76. this.random = random;
  77. selectedIndex = random.nextInt(length);
  78. }
  79. @Override
  80. public void updateSelectedTrack(long playbackPositionUs, long bufferedDurationUs,
  81. long availableDurationUs) {
  82. // Count the number of non-blacklisted formats.
  83. long nowMs = SystemClock.elapsedRealtime();
  84. int nonBlacklistedFormatCount = 0;
  85. for (int i = 0; i < length; i++) {
  86. if (!isBlacklisted(i, nowMs)) {
  87. nonBlacklistedFormatCount++;
  88. }
  89. }
  90. selectedIndex = random.nextInt(nonBlacklistedFormatCount);
  91. if (nonBlacklistedFormatCount != length) {
  92. // Adjust the format index to account for blacklisted formats.
  93. nonBlacklistedFormatCount = 0;
  94. for (int i = 0; i < length; i++) {
  95. if (!isBlacklisted(i, nowMs) && selectedIndex == nonBlacklistedFormatCount++) {
  96. selectedIndex = i;
  97. return;
  98. }
  99. }
  100. }
  101. }
  102. @Override
  103. public int getSelectedIndex() {
  104. return selectedIndex;
  105. }
  106. @Override
  107. public int getSelectionReason() {
  108. return C.SELECTION_REASON_ADAPTIVE;
  109. }
  110. @Override
  111. public @Nullable Object getSelectionData() {
  112. return null;
  113. }
  114. }