/references/ExoPlayer/library/core/src/main/java/com/google/android/exoplayer2/source/MediaSourceEventListener.java

https://github.com/Zeus64/alcinoe · Java · 491 lines · 329 code · 23 blank · 139 comment · 26 complexity · 47f83f6b1e93dd7cbbfa86a4f5f8f91f MD5 · raw file

  1. /*
  2. * Copyright (C) 2017 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 android.support.annotation.Nullable;
  20. import com.google.android.exoplayer2.C;
  21. import com.google.android.exoplayer2.Format;
  22. import com.google.android.exoplayer2.Player;
  23. import com.google.android.exoplayer2.upstream.DataSpec;
  24. import com.google.android.exoplayer2.util.Assertions;
  25. import java.io.IOException;
  26. /** Interface for callbacks to be notified of {@link MediaSource} events. */
  27. public interface MediaSourceEventListener {
  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 to
  35. * 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 not
  37. * 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(
  49. DataSpec dataSpec,
  50. int dataType,
  51. int trackType,
  52. Format trackFormat,
  53. int trackSelectionReason,
  54. Object trackSelectionData,
  55. long mediaStartTimeMs,
  56. long mediaEndTimeMs,
  57. long elapsedRealtimeMs);
  58. /**
  59. * Called when a load ends.
  60. *
  61. * @param dataSpec Defines the data being loaded.
  62. * @param dataType One of the {@link C} {@code DATA_TYPE_*} constants defining the type of data
  63. * being loaded.
  64. * @param trackType One of the {@link C} {@code TRACK_TYPE_*} constants if the data corresponds to
  65. * media of a specific type. {@link C#TRACK_TYPE_UNKNOWN} otherwise.
  66. * @param trackFormat The format of the track to which the data belongs. Null if the data does not
  67. * belong to a track.
  68. * @param trackSelectionReason One of the {@link C} {@code SELECTION_REASON_*} constants if the
  69. * data belongs to a track. {@link C#SELECTION_REASON_UNKNOWN} otherwise.
  70. * @param trackSelectionData Optional data associated with the selection of the track to which the
  71. * data belongs. Null if the data does not belong to a track.
  72. * @param mediaStartTimeMs The start time of the media being loaded, or {@link C#TIME_UNSET} if
  73. * the load is not for media data.
  74. * @param mediaEndTimeMs The end time of the media being loaded, or {@link C#TIME_UNSET} if the
  75. * load is not for media data.
  76. * @param elapsedRealtimeMs The value of {@link SystemClock#elapsedRealtime} when the load ended.
  77. * @param loadDurationMs The duration of the load.
  78. * @param bytesLoaded The number of bytes that were loaded.
  79. */
  80. void onLoadCompleted(
  81. DataSpec dataSpec,
  82. int dataType,
  83. int trackType,
  84. Format trackFormat,
  85. int trackSelectionReason,
  86. Object trackSelectionData,
  87. long mediaStartTimeMs,
  88. long mediaEndTimeMs,
  89. long elapsedRealtimeMs,
  90. long loadDurationMs,
  91. long bytesLoaded);
  92. /**
  93. * Called when a load is canceled.
  94. *
  95. * @param dataSpec Defines the data being loaded.
  96. * @param dataType One of the {@link C} {@code DATA_TYPE_*} constants defining the type of data
  97. * being loaded.
  98. * @param trackType One of the {@link C} {@code TRACK_TYPE_*} constants if the data corresponds to
  99. * media of a specific type. {@link C#TRACK_TYPE_UNKNOWN} otherwise.
  100. * @param trackFormat The format of the track to which the data belongs. Null if the data does not
  101. * belong to a track.
  102. * @param trackSelectionReason One of the {@link C} {@code SELECTION_REASON_*} constants if the
  103. * data belongs to a track. {@link C#SELECTION_REASON_UNKNOWN} otherwise.
  104. * @param trackSelectionData Optional data associated with the selection of the track to which the
  105. * data belongs. Null if the data does not belong to a track.
  106. * @param mediaStartTimeMs The start time of the media being loaded, or {@link C#TIME_UNSET} if
  107. * the load is not for media data.
  108. * @param mediaEndTimeMs The end time of the media being loaded, or {@link C#TIME_UNSET} if the
  109. * load is not for media data.
  110. * @param elapsedRealtimeMs The value of {@link SystemClock#elapsedRealtime} when the load was
  111. * canceled.
  112. * @param loadDurationMs The duration of the load up to the point at which it was canceled.
  113. * @param bytesLoaded The number of bytes that were loaded prior to cancelation.
  114. */
  115. void onLoadCanceled(
  116. DataSpec dataSpec,
  117. int dataType,
  118. int trackType,
  119. Format trackFormat,
  120. int trackSelectionReason,
  121. Object trackSelectionData,
  122. long mediaStartTimeMs,
  123. long mediaEndTimeMs,
  124. long elapsedRealtimeMs,
  125. long loadDurationMs,
  126. long bytesLoaded);
  127. /**
  128. * Called when a load error occurs.
  129. *
  130. * <p>The error may or may not have resulted in the load being canceled, as indicated by the
  131. * {@code wasCanceled} parameter. If the load was canceled, {@link #onLoadCanceled} will
  132. * <em>not</em> be called in addition to this method.
  133. *
  134. * <p>This method being called does not indicate that playback has failed, or that it will fail.
  135. * The player may be able to recover from the error and continue. Hence applications should
  136. * <em>not</em> implement this method to display a user visible error or initiate an application
  137. * level retry ({@link Player.EventListener#onPlayerError} is the appropriate place to implement
  138. * such behavior). This method is called to provide the application with an opportunity to log the
  139. * error if it wishes to do so.
  140. *
  141. * @param dataSpec Defines the data being loaded.
  142. * @param dataType One of the {@link C} {@code DATA_TYPE_*} constants defining the type of data
  143. * being loaded.
  144. * @param trackType One of the {@link C} {@code TRACK_TYPE_*} constants if the data corresponds to
  145. * media of a specific type. {@link C#TRACK_TYPE_UNKNOWN} otherwise.
  146. * @param trackFormat The format of the track to which the data belongs. Null if the data does not
  147. * belong to a track.
  148. * @param trackSelectionReason One of the {@link C} {@code SELECTION_REASON_*} constants if the
  149. * data belongs to a track. {@link C#SELECTION_REASON_UNKNOWN} otherwise.
  150. * @param trackSelectionData Optional data associated with the selection of the track to which the
  151. * data belongs. Null if the data does not belong to a track.
  152. * @param mediaStartTimeMs The start time of the media being loaded, or {@link C#TIME_UNSET} if
  153. * the load is not for media data.
  154. * @param mediaEndTimeMs The end time of the media being loaded, or {@link C#TIME_UNSET} if the
  155. * load is not for media data.
  156. * @param elapsedRealtimeMs The value of {@link SystemClock#elapsedRealtime} when the error
  157. * occurred.
  158. * @param loadDurationMs The duration of the load up to the point at which the error occurred.
  159. * @param bytesLoaded The number of bytes that were loaded prior to the error.
  160. * @param error The load error.
  161. * @param wasCanceled Whether the load was canceled as a result of the error.
  162. */
  163. void onLoadError(
  164. DataSpec dataSpec,
  165. int dataType,
  166. int trackType,
  167. Format trackFormat,
  168. int trackSelectionReason,
  169. Object trackSelectionData,
  170. long mediaStartTimeMs,
  171. long mediaEndTimeMs,
  172. long elapsedRealtimeMs,
  173. long loadDurationMs,
  174. long bytesLoaded,
  175. IOException error,
  176. boolean wasCanceled);
  177. /**
  178. * Called when data is removed from the back of a media buffer, typically so that it can be
  179. * re-buffered in a different format.
  180. *
  181. * @param trackType The type of the media. One of the {@link C} {@code TRACK_TYPE_*} constants.
  182. * @param mediaStartTimeMs The start time of the media being discarded.
  183. * @param mediaEndTimeMs The end time of the media being discarded.
  184. */
  185. void onUpstreamDiscarded(int trackType, long mediaStartTimeMs, long mediaEndTimeMs);
  186. /**
  187. * Called when a downstream format change occurs (i.e. when the format of the media being read
  188. * from one or more {@link SampleStream}s provided by the source changes).
  189. *
  190. * @param trackType The type of the media. One of the {@link C} {@code TRACK_TYPE_*} constants.
  191. * @param trackFormat The format of the track to which the data belongs. Null if the data does not
  192. * belong to a track.
  193. * @param trackSelectionReason One of the {@link C} {@code SELECTION_REASON_*} constants if the
  194. * data belongs to a track. {@link C#SELECTION_REASON_UNKNOWN} otherwise.
  195. * @param trackSelectionData Optional data associated with the selection of the track to which the
  196. * data belongs. Null if the data does not belong to a track.
  197. * @param mediaTimeMs The media time at which the change occurred.
  198. */
  199. void onDownstreamFormatChanged(
  200. int trackType,
  201. Format trackFormat,
  202. int trackSelectionReason,
  203. Object trackSelectionData,
  204. long mediaTimeMs);
  205. /** Dispatches events to a {@link MediaSourceEventListener}. */
  206. final class EventDispatcher {
  207. @Nullable private final Handler handler;
  208. @Nullable private final MediaSourceEventListener listener;
  209. private final long mediaTimeOffsetMs;
  210. public EventDispatcher(@Nullable Handler handler, @Nullable MediaSourceEventListener listener) {
  211. this(handler, listener, 0);
  212. }
  213. public EventDispatcher(
  214. @Nullable Handler handler,
  215. @Nullable MediaSourceEventListener listener,
  216. long mediaTimeOffsetMs) {
  217. this.handler = listener != null ? Assertions.checkNotNull(handler) : null;
  218. this.listener = listener;
  219. this.mediaTimeOffsetMs = mediaTimeOffsetMs;
  220. }
  221. public EventDispatcher copyWithMediaTimeOffsetMs(long mediaTimeOffsetMs) {
  222. return new EventDispatcher(handler, listener, mediaTimeOffsetMs);
  223. }
  224. public void loadStarted(DataSpec dataSpec, int dataType, long elapsedRealtimeMs) {
  225. loadStarted(
  226. dataSpec,
  227. dataType,
  228. C.TRACK_TYPE_UNKNOWN,
  229. null,
  230. C.SELECTION_REASON_UNKNOWN,
  231. null,
  232. C.TIME_UNSET,
  233. C.TIME_UNSET,
  234. elapsedRealtimeMs);
  235. }
  236. public void loadStarted(
  237. final DataSpec dataSpec,
  238. final int dataType,
  239. final int trackType,
  240. final Format trackFormat,
  241. final int trackSelectionReason,
  242. final Object trackSelectionData,
  243. final long mediaStartTimeUs,
  244. final long mediaEndTimeUs,
  245. final long elapsedRealtimeMs) {
  246. if (listener != null && handler != null) {
  247. handler.post(
  248. new Runnable() {
  249. @Override
  250. public void run() {
  251. listener.onLoadStarted(
  252. dataSpec,
  253. dataType,
  254. trackType,
  255. trackFormat,
  256. trackSelectionReason,
  257. trackSelectionData,
  258. adjustMediaTime(mediaStartTimeUs),
  259. adjustMediaTime(mediaEndTimeUs),
  260. elapsedRealtimeMs);
  261. }
  262. });
  263. }
  264. }
  265. public void loadCompleted(
  266. DataSpec dataSpec,
  267. int dataType,
  268. long elapsedRealtimeMs,
  269. long loadDurationMs,
  270. long bytesLoaded) {
  271. loadCompleted(
  272. dataSpec,
  273. dataType,
  274. C.TRACK_TYPE_UNKNOWN,
  275. null,
  276. C.SELECTION_REASON_UNKNOWN,
  277. null,
  278. C.TIME_UNSET,
  279. C.TIME_UNSET,
  280. elapsedRealtimeMs,
  281. loadDurationMs,
  282. bytesLoaded);
  283. }
  284. public void loadCompleted(
  285. final DataSpec dataSpec,
  286. final int dataType,
  287. final int trackType,
  288. final Format trackFormat,
  289. final int trackSelectionReason,
  290. final Object trackSelectionData,
  291. final long mediaStartTimeUs,
  292. final long mediaEndTimeUs,
  293. final long elapsedRealtimeMs,
  294. final long loadDurationMs,
  295. final long bytesLoaded) {
  296. if (listener != null && handler != null) {
  297. handler.post(
  298. new Runnable() {
  299. @Override
  300. public void run() {
  301. listener.onLoadCompleted(
  302. dataSpec,
  303. dataType,
  304. trackType,
  305. trackFormat,
  306. trackSelectionReason,
  307. trackSelectionData,
  308. adjustMediaTime(mediaStartTimeUs),
  309. adjustMediaTime(mediaEndTimeUs),
  310. elapsedRealtimeMs,
  311. loadDurationMs,
  312. bytesLoaded);
  313. }
  314. });
  315. }
  316. }
  317. public void loadCanceled(
  318. DataSpec dataSpec,
  319. int dataType,
  320. long elapsedRealtimeMs,
  321. long loadDurationMs,
  322. long bytesLoaded) {
  323. loadCanceled(
  324. dataSpec,
  325. dataType,
  326. C.TRACK_TYPE_UNKNOWN,
  327. null,
  328. C.SELECTION_REASON_UNKNOWN,
  329. null,
  330. C.TIME_UNSET,
  331. C.TIME_UNSET,
  332. elapsedRealtimeMs,
  333. loadDurationMs,
  334. bytesLoaded);
  335. }
  336. public void loadCanceled(
  337. final DataSpec dataSpec,
  338. final int dataType,
  339. final int trackType,
  340. final Format trackFormat,
  341. final int trackSelectionReason,
  342. final Object trackSelectionData,
  343. final long mediaStartTimeUs,
  344. final long mediaEndTimeUs,
  345. final long elapsedRealtimeMs,
  346. final long loadDurationMs,
  347. final long bytesLoaded) {
  348. if (listener != null && handler != null) {
  349. handler.post(
  350. new Runnable() {
  351. @Override
  352. public void run() {
  353. listener.onLoadCanceled(
  354. dataSpec,
  355. dataType,
  356. trackType,
  357. trackFormat,
  358. trackSelectionReason,
  359. trackSelectionData,
  360. adjustMediaTime(mediaStartTimeUs),
  361. adjustMediaTime(mediaEndTimeUs),
  362. elapsedRealtimeMs,
  363. loadDurationMs,
  364. bytesLoaded);
  365. }
  366. });
  367. }
  368. }
  369. public void loadError(
  370. DataSpec dataSpec,
  371. int dataType,
  372. long elapsedRealtimeMs,
  373. long loadDurationMs,
  374. long bytesLoaded,
  375. IOException error,
  376. boolean wasCanceled) {
  377. loadError(
  378. dataSpec,
  379. dataType,
  380. C.TRACK_TYPE_UNKNOWN,
  381. null,
  382. C.SELECTION_REASON_UNKNOWN,
  383. null,
  384. C.TIME_UNSET,
  385. C.TIME_UNSET,
  386. elapsedRealtimeMs,
  387. loadDurationMs,
  388. bytesLoaded,
  389. error,
  390. wasCanceled);
  391. }
  392. public void loadError(
  393. final DataSpec dataSpec,
  394. final int dataType,
  395. final int trackType,
  396. final Format trackFormat,
  397. final int trackSelectionReason,
  398. final Object trackSelectionData,
  399. final long mediaStartTimeUs,
  400. final long mediaEndTimeUs,
  401. final long elapsedRealtimeMs,
  402. final long loadDurationMs,
  403. final long bytesLoaded,
  404. final IOException error,
  405. final boolean wasCanceled) {
  406. if (listener != null && handler != null) {
  407. handler.post(
  408. new Runnable() {
  409. @Override
  410. public void run() {
  411. listener.onLoadError(
  412. dataSpec,
  413. dataType,
  414. trackType,
  415. trackFormat,
  416. trackSelectionReason,
  417. trackSelectionData,
  418. adjustMediaTime(mediaStartTimeUs),
  419. adjustMediaTime(mediaEndTimeUs),
  420. elapsedRealtimeMs,
  421. loadDurationMs,
  422. bytesLoaded,
  423. error,
  424. wasCanceled);
  425. }
  426. });
  427. }
  428. }
  429. public void upstreamDiscarded(
  430. final int trackType, final long mediaStartTimeUs, final long mediaEndTimeUs) {
  431. if (listener != null && handler != null) {
  432. handler.post(
  433. new Runnable() {
  434. @Override
  435. public void run() {
  436. listener.onUpstreamDiscarded(
  437. trackType, adjustMediaTime(mediaStartTimeUs), adjustMediaTime(mediaEndTimeUs));
  438. }
  439. });
  440. }
  441. }
  442. public void downstreamFormatChanged(
  443. final int trackType,
  444. final Format trackFormat,
  445. final int trackSelectionReason,
  446. final Object trackSelectionData,
  447. final long mediaTimeUs) {
  448. if (listener != null && handler != null) {
  449. handler.post(
  450. new Runnable() {
  451. @Override
  452. public void run() {
  453. listener.onDownstreamFormatChanged(
  454. trackType,
  455. trackFormat,
  456. trackSelectionReason,
  457. trackSelectionData,
  458. adjustMediaTime(mediaTimeUs));
  459. }
  460. });
  461. }
  462. }
  463. private long adjustMediaTime(long mediaTimeUs) {
  464. long mediaTimeMs = C.usToMs(mediaTimeUs);
  465. return mediaTimeMs == C.TIME_UNSET ? C.TIME_UNSET : mediaTimeOffsetMs + mediaTimeMs;
  466. }
  467. }
  468. }