/library-core/src/main/java/com/google/android/exoplayer2/source/AdaptiveMediaSourceEventListener.java

https://github.com/ocwvar/DarkPurple · Java · 317 lines · 153 code · 28 blank · 136 comment · 14 complexity · e266964e30ff73fde6ddbc915eb53076 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.source;
  17. import android.os.Handler;
  18. import android.os.SystemClock;
  19. import com.google.android.exoplayer2.C;
  20. import com.google.android.exoplayer2.Format;
  21. import com.google.android.exoplayer2.upstream.DataSpec;
  22. import com.google.android.exoplayer2.util.Assertions;
  23. import java.io.IOException;
  24. /**
  25. * Interface for callbacks to be notified of adaptive {@link MediaSource} events.
  26. */
  27. public interface AdaptiveMediaSourceEventListener {
  28. /**
  29. * Called when a load begins.
  30. *
  31. * @param dataSpec Defines the data being loaded.
  32. * @param dataType One of the {@link C} {@code DATA_TYPE_*} constants defining the type of data
  33. * being loaded.
  34. * @param trackType One of the {@link C} {@code TRACK_TYPE_*} constants if the data corresponds
  35. * to media of a specific type. {@link C#TRACK_TYPE_UNKNOWN} otherwise.
  36. * @param trackFormat The format of the track to which the data belongs. Null if the data does
  37. * not belong to a track.
  38. * @param trackSelectionReason One of the {@link C} {@code SELECTION_REASON_*} constants if the
  39. * data belongs to a track. {@link C#SELECTION_REASON_UNKNOWN} otherwise.
  40. * @param trackSelectionData Optional data associated with the selection of the track to which the
  41. * data belongs. Null if the data does not belong to a track.
  42. * @param mediaStartTimeMs The start time of the media being loaded, or {@link C#TIME_UNSET} if
  43. * the load is not for media data.
  44. * @param mediaEndTimeMs The end time of the media being loaded, or {@link C#TIME_UNSET} if the
  45. * load is not for media data.
  46. * @param elapsedRealtimeMs The value of {@link SystemClock#elapsedRealtime} when the load began.
  47. */
  48. void onLoadStarted(DataSpec dataSpec, int dataType, int trackType, Format trackFormat,
  49. int trackSelectionReason, Object trackSelectionData, long mediaStartTimeMs,
  50. long mediaEndTimeMs, long elapsedRealtimeMs);
  51. /**
  52. * Called when a load ends.
  53. *
  54. * @param dataSpec Defines the data being loaded.
  55. * @param dataType One of the {@link C} {@code DATA_TYPE_*} constants defining the type of data
  56. * being loaded.
  57. * @param trackType One of the {@link C} {@code TRACK_TYPE_*} constants if the data corresponds
  58. * to media of a specific type. {@link C#TRACK_TYPE_UNKNOWN} otherwise.
  59. * @param trackFormat The format of the track to which the data belongs. Null if the data does
  60. * not belong to a track.
  61. * @param trackSelectionReason One of the {@link C} {@code SELECTION_REASON_*} constants if the
  62. * data belongs to a track. {@link C#SELECTION_REASON_UNKNOWN} otherwise.
  63. * @param trackSelectionData Optional data associated with the selection of the track to which the
  64. * data belongs. Null if the data does not belong to a track.
  65. * @param mediaStartTimeMs The start time of the media being loaded, or {@link C#TIME_UNSET} if
  66. * the load is not for media data.
  67. * @param mediaEndTimeMs The end time of the media being loaded, or {@link C#TIME_UNSET} if the
  68. * load is not for media data.
  69. * @param elapsedRealtimeMs The value of {@link SystemClock#elapsedRealtime} when the load ended.
  70. * @param loadDurationMs The duration of the load.
  71. * @param bytesLoaded The number of bytes that were loaded.
  72. */
  73. void onLoadCompleted(DataSpec dataSpec, int dataType, int trackType, Format trackFormat,
  74. int trackSelectionReason, Object trackSelectionData, long mediaStartTimeMs,
  75. long mediaEndTimeMs, long elapsedRealtimeMs, long loadDurationMs, long bytesLoaded);
  76. /**
  77. * Called when a load is canceled.
  78. *
  79. * @param dataSpec Defines the data being loaded.
  80. * @param dataType One of the {@link C} {@code DATA_TYPE_*} constants defining the type of data
  81. * being loaded.
  82. * @param trackType One of the {@link C} {@code TRACK_TYPE_*} constants if the data corresponds
  83. * to media of a specific type. {@link C#TRACK_TYPE_UNKNOWN} otherwise.
  84. * @param trackFormat The format of the track to which the data belongs. Null if the data does
  85. * not belong to a track.
  86. * @param trackSelectionReason One of the {@link C} {@code SELECTION_REASON_*} constants if the
  87. * data belongs to a track. {@link C#SELECTION_REASON_UNKNOWN} otherwise.
  88. * @param trackSelectionData Optional data associated with the selection of the track to which the
  89. * data belongs. Null if the data does not belong to a track.
  90. * @param mediaStartTimeMs The start time of the media being loaded, or {@link C#TIME_UNSET} if
  91. * the load is not for media data.
  92. * @param mediaEndTimeMs The end time of the media being loaded, or {@link C#TIME_UNSET} if the
  93. * load is not for media data.
  94. * @param elapsedRealtimeMs The value of {@link SystemClock#elapsedRealtime} when the load was
  95. * canceled.
  96. * @param loadDurationMs The duration of the load up to the point at which it was canceled.
  97. * @param bytesLoaded The number of bytes that were loaded prior to cancelation.
  98. */
  99. void onLoadCanceled(DataSpec dataSpec, int dataType, int trackType, Format trackFormat,
  100. int trackSelectionReason, Object trackSelectionData, long mediaStartTimeMs,
  101. long mediaEndTimeMs, long elapsedRealtimeMs, long loadDurationMs, long bytesLoaded);
  102. /**
  103. * Called when a load error occurs.
  104. * <p>
  105. * The error may or may not have resulted in the load being canceled, as indicated by the
  106. * {@code wasCanceled} parameter. If the load was canceled, {@link #onLoadCanceled} will
  107. * <em>not</em> be called in addition to this method.
  108. *
  109. * @param dataSpec Defines the data being loaded.
  110. * @param dataType One of the {@link C} {@code DATA_TYPE_*} constants defining the type of data
  111. * being loaded.
  112. * @param trackType One of the {@link C} {@code TRACK_TYPE_*} constants if the data corresponds
  113. * to media of a specific type. {@link C#TRACK_TYPE_UNKNOWN} otherwise.
  114. * @param trackFormat The format of the track to which the data belongs. Null if the data does
  115. * not belong to a track.
  116. * @param trackSelectionReason One of the {@link C} {@code SELECTION_REASON_*} constants if the
  117. * data belongs to a track. {@link C#SELECTION_REASON_UNKNOWN} otherwise.
  118. * @param trackSelectionData Optional data associated with the selection of the track to which the
  119. * data belongs. Null if the data does not belong to a track.
  120. * @param mediaStartTimeMs The start time of the media being loaded, or {@link C#TIME_UNSET} if
  121. * the load is not for media data.
  122. * @param mediaEndTimeMs The end time of the media being loaded, or {@link C#TIME_UNSET} if the
  123. * load is not for media data.
  124. * @param elapsedRealtimeMs The value of {@link SystemClock#elapsedRealtime} when the error
  125. * occurred.
  126. * @param loadDurationMs The duration of the load up to the point at which the error occurred.
  127. * @param bytesLoaded The number of bytes that were loaded prior to the error.
  128. * @param error The load error.
  129. * @param wasCanceled Whether the load was canceled as a result of the error.
  130. */
  131. void onLoadError(DataSpec dataSpec, int dataType, int trackType, Format trackFormat,
  132. int trackSelectionReason, Object trackSelectionData, long mediaStartTimeMs,
  133. long mediaEndTimeMs, long elapsedRealtimeMs, long loadDurationMs, long bytesLoaded,
  134. IOException error, boolean wasCanceled);
  135. /**
  136. * Called when data is removed from the back of a media buffer, typically so that it can be
  137. * re-buffered in a different format.
  138. *
  139. * @param trackType The type of the media. One of the {@link C} {@code TRACK_TYPE_*} constants.
  140. * @param mediaStartTimeMs The start time of the media being discarded.
  141. * @param mediaEndTimeMs The end time of the media being discarded.
  142. */
  143. void onUpstreamDiscarded(int trackType, long mediaStartTimeMs, long mediaEndTimeMs);
  144. /**
  145. * Called when a downstream format change occurs (i.e. when the format of the media being read
  146. * from one or more {@link SampleStream}s provided by the source changes).
  147. *
  148. * @param trackType The type of the media. One of the {@link C} {@code TRACK_TYPE_*} constants.
  149. * @param trackFormat The format of the track to which the data belongs. Null if the data does
  150. * not belong to a track.
  151. * @param trackSelectionReason One of the {@link C} {@code SELECTION_REASON_*} constants if the
  152. * data belongs to a track. {@link C#SELECTION_REASON_UNKNOWN} otherwise.
  153. * @param trackSelectionData Optional data associated with the selection of the track to which the
  154. * data belongs. Null if the data does not belong to a track.
  155. * @param mediaTimeMs The media time at which the change occurred.
  156. */
  157. void onDownstreamFormatChanged(int trackType, Format trackFormat, int trackSelectionReason,
  158. Object trackSelectionData, long mediaTimeMs);
  159. /**
  160. * Dispatches events to a {@link AdaptiveMediaSourceEventListener}.
  161. */
  162. final class EventDispatcher {
  163. private final Handler handler;
  164. private final AdaptiveMediaSourceEventListener listener;
  165. private final long mediaTimeOffsetMs;
  166. public EventDispatcher(Handler handler, AdaptiveMediaSourceEventListener listener) {
  167. this(handler, listener, 0);
  168. }
  169. public EventDispatcher(Handler handler, AdaptiveMediaSourceEventListener listener,
  170. long mediaTimeOffsetMs) {
  171. this.handler = listener != null ? Assertions.checkNotNull(handler) : null;
  172. this.listener = listener;
  173. this.mediaTimeOffsetMs = mediaTimeOffsetMs;
  174. }
  175. public EventDispatcher copyWithMediaTimeOffsetMs(long mediaTimeOffsetMs) {
  176. return new EventDispatcher(handler, listener, mediaTimeOffsetMs);
  177. }
  178. public void loadStarted(DataSpec dataSpec, int dataType, long elapsedRealtimeMs) {
  179. loadStarted(dataSpec, dataType, C.TRACK_TYPE_UNKNOWN, null, C.SELECTION_REASON_UNKNOWN,
  180. null, C.TIME_UNSET, C.TIME_UNSET, elapsedRealtimeMs);
  181. }
  182. public void loadStarted(final DataSpec dataSpec, final int dataType, final int trackType,
  183. final Format trackFormat, final int trackSelectionReason, final Object trackSelectionData,
  184. final long mediaStartTimeUs, final long mediaEndTimeUs, final long elapsedRealtimeMs) {
  185. if (listener != null) {
  186. handler.post(new Runnable() {
  187. @Override
  188. public void run() {
  189. listener.onLoadStarted(dataSpec, dataType, trackType, trackFormat, trackSelectionReason,
  190. trackSelectionData, adjustMediaTime(mediaStartTimeUs),
  191. adjustMediaTime(mediaEndTimeUs), elapsedRealtimeMs);
  192. }
  193. });
  194. }
  195. }
  196. public void loadCompleted(DataSpec dataSpec, int dataType, long elapsedRealtimeMs,
  197. long loadDurationMs, long bytesLoaded) {
  198. loadCompleted(dataSpec, dataType, C.TRACK_TYPE_UNKNOWN, null, C.SELECTION_REASON_UNKNOWN,
  199. null, C.TIME_UNSET, C.TIME_UNSET, elapsedRealtimeMs, loadDurationMs, bytesLoaded);
  200. }
  201. public void loadCompleted(final DataSpec dataSpec, final int dataType, final int trackType,
  202. final Format trackFormat, final int trackSelectionReason, final Object trackSelectionData,
  203. final long mediaStartTimeUs, final long mediaEndTimeUs, final long elapsedRealtimeMs,
  204. final long loadDurationMs, final long bytesLoaded) {
  205. if (listener != null) {
  206. handler.post(new Runnable() {
  207. @Override
  208. public void run() {
  209. listener.onLoadCompleted(dataSpec, dataType, trackType, trackFormat,
  210. trackSelectionReason, trackSelectionData, adjustMediaTime(mediaStartTimeUs),
  211. adjustMediaTime(mediaEndTimeUs), elapsedRealtimeMs, loadDurationMs, bytesLoaded);
  212. }
  213. });
  214. }
  215. }
  216. public void loadCanceled(DataSpec dataSpec, int dataType, long elapsedRealtimeMs,
  217. long loadDurationMs, long bytesLoaded) {
  218. loadCanceled(dataSpec, dataType, C.TRACK_TYPE_UNKNOWN, null, C.SELECTION_REASON_UNKNOWN,
  219. null, C.TIME_UNSET, C.TIME_UNSET, elapsedRealtimeMs, loadDurationMs, bytesLoaded);
  220. }
  221. public void loadCanceled(final DataSpec dataSpec, final int dataType, final int trackType,
  222. final Format trackFormat, final int trackSelectionReason, final Object trackSelectionData,
  223. final long mediaStartTimeUs, final long mediaEndTimeUs, final long elapsedRealtimeMs,
  224. final long loadDurationMs, final long bytesLoaded) {
  225. if (listener != null) {
  226. handler.post(new Runnable() {
  227. @Override
  228. public void run() {
  229. listener.onLoadCanceled(dataSpec, dataType, trackType, trackFormat,
  230. trackSelectionReason, trackSelectionData, adjustMediaTime(mediaStartTimeUs),
  231. adjustMediaTime(mediaEndTimeUs), elapsedRealtimeMs, loadDurationMs, bytesLoaded);
  232. }
  233. });
  234. }
  235. }
  236. public void loadError(DataSpec dataSpec, int dataType, long elapsedRealtimeMs,
  237. long loadDurationMs, long bytesLoaded, IOException error, boolean wasCanceled) {
  238. loadError(dataSpec, dataType, C.TRACK_TYPE_UNKNOWN, null, C.SELECTION_REASON_UNKNOWN,
  239. null, C.TIME_UNSET, C.TIME_UNSET, elapsedRealtimeMs, loadDurationMs, bytesLoaded,
  240. error, wasCanceled);
  241. }
  242. public void loadError(final DataSpec dataSpec, final int dataType, final int trackType,
  243. final Format trackFormat, final int trackSelectionReason, final Object trackSelectionData,
  244. final long mediaStartTimeUs, final long mediaEndTimeUs, final long elapsedRealtimeMs,
  245. final long loadDurationMs, final long bytesLoaded, final IOException error,
  246. final boolean wasCanceled) {
  247. if (listener != null) {
  248. handler.post(new Runnable() {
  249. @Override
  250. public void run() {
  251. listener.onLoadError(dataSpec, dataType, trackType, trackFormat, trackSelectionReason,
  252. trackSelectionData, adjustMediaTime(mediaStartTimeUs),
  253. adjustMediaTime(mediaEndTimeUs), elapsedRealtimeMs, loadDurationMs, bytesLoaded,
  254. error, wasCanceled);
  255. }
  256. });
  257. }
  258. }
  259. public void upstreamDiscarded(final int trackType, final long mediaStartTimeUs,
  260. final long mediaEndTimeUs) {
  261. if (listener != null) {
  262. handler.post(new Runnable() {
  263. @Override
  264. public void run() {
  265. listener.onUpstreamDiscarded(trackType, adjustMediaTime(mediaStartTimeUs),
  266. adjustMediaTime(mediaEndTimeUs));
  267. }
  268. });
  269. }
  270. }
  271. public void downstreamFormatChanged(final int trackType, final Format trackFormat,
  272. final int trackSelectionReason, final Object trackSelectionData,
  273. final long mediaTimeUs) {
  274. if (listener != null) {
  275. handler.post(new Runnable() {
  276. @Override
  277. public void run() {
  278. listener.onDownstreamFormatChanged(trackType, trackFormat, trackSelectionReason,
  279. trackSelectionData, adjustMediaTime(mediaTimeUs));
  280. }
  281. });
  282. }
  283. }
  284. private long adjustMediaTime(long mediaTimeUs) {
  285. long mediaTimeMs = C.usToMs(mediaTimeUs);
  286. return mediaTimeMs == C.TIME_UNSET ? C.TIME_UNSET : mediaTimeOffsetMs + mediaTimeMs;
  287. }
  288. }
  289. }