/TMessagesProj/src/main/java/org/telegram/messenger/exoplayer/StandaloneMediaClock.java

https://gitlab.com/gossiks/bormotushek · Java · 76 lines · 30 code · 11 blank · 35 comment · 2 complexity · f0db2961989aeec1de345823056215b6 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 org.telegram.messenger.exoplayer;
  17. import android.os.SystemClock;
  18. /**
  19. * A standalone {@link MediaClock}. The clock can be started, stopped and its time can be set and
  20. * retrieved. When started, this clock is based on {@link SystemClock#elapsedRealtime()}.
  21. */
  22. /* package */ final class StandaloneMediaClock implements MediaClock {
  23. private boolean started;
  24. /**
  25. * The media time when the clock was last set or stopped.
  26. */
  27. private long positionUs;
  28. /**
  29. * The difference between {@link SystemClock#elapsedRealtime()} and {@link #positionUs}
  30. * when the clock was last set or started.
  31. */
  32. private long deltaUs;
  33. /**
  34. * Starts the clock. Does nothing if the clock is already started.
  35. */
  36. public void start() {
  37. if (!started) {
  38. started = true;
  39. deltaUs = elapsedRealtimeMinus(positionUs);
  40. }
  41. }
  42. /**
  43. * Stops the clock. Does nothing if the clock is already stopped.
  44. */
  45. public void stop() {
  46. if (started) {
  47. positionUs = elapsedRealtimeMinus(deltaUs);
  48. started = false;
  49. }
  50. }
  51. /**
  52. * @param timeUs The position to set in microseconds.
  53. */
  54. public void setPositionUs(long timeUs) {
  55. this.positionUs = timeUs;
  56. deltaUs = elapsedRealtimeMinus(timeUs);
  57. }
  58. @Override
  59. public long getPositionUs() {
  60. return started ? elapsedRealtimeMinus(deltaUs) : positionUs;
  61. }
  62. private long elapsedRealtimeMinus(long toSubtractUs) {
  63. return SystemClock.elapsedRealtime() * 1000 - toSubtractUs;
  64. }
  65. }