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

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