-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestFairyExoPlayerAnalyticsListener.java
348 lines (288 loc) · 10.8 KB
/
TestFairyExoPlayerAnalyticsListener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package com.testfairy.tvtest.testfairy;
import android.os.Handler;
import android.view.Surface;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.PlaybackParameters;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.analytics.AnalyticsListener;
import com.google.android.exoplayer2.audio.AudioAttributes;
import com.google.android.exoplayer2.decoder.DecoderCounters;
import com.google.android.exoplayer2.metadata.Metadata;
import com.google.android.exoplayer2.source.MediaSourceEventListener;
import com.google.android.exoplayer2.source.TrackGroupArray;
import com.google.android.exoplayer2.trackselection.TrackSelectionArray;
import com.google.android.exoplayer2.upstream.HttpDataSource;
import com.testfairy.TestFairy;
import java.io.IOException;
import java.util.Locale;
public class TestFairyExoPlayerAnalyticsListener implements AnalyticsListener {
private final Handler handler = new Handler();
private final ExoPlayer player;
private Runnable currentPositionTracker;
private long lastKnownPlaybackPercent = -1;
public TestFairyExoPlayerAnalyticsListener(ExoPlayer player) {
this.player = player;
}
private void registerCurrentPositionTracker() {
unRegisterCurrentPositionTracker();
currentPositionTracker = new Runnable() {
@Override
public void run() {
long currentPosition = player.getCurrentPosition();
long percent = (currentPosition * 100) / player.getDuration();
if (lastKnownPlaybackPercent != percent) {
lastKnownPlaybackPercent = percent;
TestFairy.addEvent(String.format(Locale.ENGLISH, "Playback position %d%%", percent));
}
handler.postDelayed(currentPositionTracker, 100);
}
};
handler.postDelayed(currentPositionTracker, 100);
}
private void unRegisterCurrentPositionTracker() {
if (currentPositionTracker != null) {
handler.removeCallbacks(currentPositionTracker);
currentPositionTracker = null;
}
}
@Override
public void onPlayerStateChanged(EventTime eventTime, boolean playWhenReady,
int playbackState) {
if (playWhenReady && playbackState == Player.STATE_READY) {
if (currentPositionTracker == null) {
registerCurrentPositionTracker();
}
TestFairy.addEvent("Playback is playing");
} else if (playWhenReady) {
unRegisterCurrentPositionTracker();
if (player.getDuration() != 0 && player.getDuration() <= player.getCurrentPosition()) {
TestFairy.addEvent("Playback has completed");
} else {
TestFairy.addEvent("Playback is buffering or paused automatically");
}
} else {
unRegisterCurrentPositionTracker();
TestFairy.addEvent("Playback is paused");
}
}
@Override
public void onTimelineChanged(EventTime eventTime, int reason) {
}
@Override
public void onPositionDiscontinuity(EventTime eventTime, int reason) {
switch (reason) {
case SimpleExoPlayer.DISCONTINUITY_REASON_PERIOD_TRANSITION:
TestFairy.addEvent("Video stutters due to period transition");
break;
case SimpleExoPlayer.DISCONTINUITY_REASON_SEEK:
TestFairy.addEvent("Video stutters due to a seek");
break;
case SimpleExoPlayer.DISCONTINUITY_REASON_SEEK_ADJUSTMENT:
TestFairy.addEvent("Video stutters due to seek adjustment");
break;
case SimpleExoPlayer.DISCONTINUITY_REASON_AD_INSERTION:
TestFairy.addEvent("Video stutters due to an inserted ad");
break;
case SimpleExoPlayer.DISCONTINUITY_REASON_INTERNAL:
TestFairy.addEvent("Video stutters due to an internal problem");
break;
}
}
@Override
public void onSeekStarted(EventTime eventTime) {
}
@Override
public void onSeekProcessed(EventTime eventTime) {
long currentPosition = player.getCurrentPosition();
long percent = (currentPosition * 100) / player.getDuration();
lastKnownPlaybackPercent = percent;
TestFairy
.addEvent(String.format(Locale.ENGLISH, "Playback seeks to position %d%%", percent));
}
@Override
public void onPlaybackParametersChanged(EventTime eventTime,
PlaybackParameters playbackParameters) {
}
@Override
public void onRepeatModeChanged(EventTime eventTime, int repeatMode) {
switch (repeatMode) {
case ExoPlayer.REPEAT_MODE_OFF:
TestFairy.addEvent("Repeat mode has been changed to OFF");
case ExoPlayer.REPEAT_MODE_ONE:
TestFairy.addEvent("Repeat mode has been changed to ONE");
case ExoPlayer.REPEAT_MODE_ALL:
TestFairy.addEvent("Repeat mode has been changed to ALL");
break;
}
}
@Override
public void onShuffleModeChanged(EventTime eventTime, boolean shuffleModeEnabled) {
if (shuffleModeEnabled) {
TestFairy.addEvent("Shuffle mode is enabled");
} else {
TestFairy.addEvent("Shuffle mode is disabled");
}
}
@Override
public void onLoadingChanged(EventTime eventTime, boolean isLoading) {
}
@Override
public void onPlayerError(EventTime eventTime, ExoPlaybackException error) {
if (error.type == ExoPlaybackException.TYPE_SOURCE) {
IOException cause = error.getSourceException();
if (cause instanceof HttpDataSource.HttpDataSourceException) {
// An HTTP error occurred.
HttpDataSource.HttpDataSourceException httpError = (HttpDataSource.HttpDataSourceException) cause;
// It's possible to find out more about the error both by casting and by
// querying the cause.
if (httpError instanceof HttpDataSource.InvalidResponseCodeException) {
// Cast to InvalidResponseCodeException and retrieve the response code,
// message and headers.
HttpDataSource.InvalidResponseCodeException ex = (HttpDataSource.InvalidResponseCodeException) httpError;
TestFairy.addEvent(
String.format(Locale.ENGLISH, "Http error during playback - %d", ex.responseCode));
TestFairy.logThrowable(cause);
} else {
// Try calling httpError.getCause() to retrieve the underlying cause,
// although note that it may be null.
TestFairy.addEvent("Http error during playback before response");
Throwable innerCause = httpError.getCause();
if (innerCause != null) {
TestFairy.logThrowable(innerCause);
} else {
TestFairy.logThrowable(cause);
}
}
} else {
TestFairy.addEvent(String.format(Locale.ENGLISH, "Player error - %s", error.toString()));
TestFairy.logThrowable(cause);
}
}
}
@Override
public void onTracksChanged(EventTime eventTime, TrackGroupArray trackGroups,
TrackSelectionArray trackSelections) {
lastKnownPlaybackPercent = -1;
TestFairy.addEvent("A new video has been loaded");
}
@Override
public void onLoadStarted(EventTime eventTime,
MediaSourceEventListener.LoadEventInfo loadEventInfo,
MediaSourceEventListener.MediaLoadData mediaLoadData) {
}
@Override
public void onLoadCompleted(EventTime eventTime,
MediaSourceEventListener.LoadEventInfo loadEventInfo,
MediaSourceEventListener.MediaLoadData mediaLoadData) {
TestFairy.addEvent(
String.format(Locale.ENGLISH, "Playback is buffering %d%%", player.getBufferedPercentage())
);
}
@Override
public void onLoadCanceled(EventTime eventTime,
MediaSourceEventListener.LoadEventInfo loadEventInfo,
MediaSourceEventListener.MediaLoadData mediaLoadData) {
}
@Override
public void onLoadError(EventTime eventTime,
MediaSourceEventListener.LoadEventInfo loadEventInfo,
MediaSourceEventListener.MediaLoadData mediaLoadData, IOException error,
boolean wasCanceled) {
TestFairy.addEvent("Error during loading");
TestFairy.logThrowable(error);
}
@Override
public void onDownstreamFormatChanged(EventTime eventTime,
MediaSourceEventListener.MediaLoadData mediaLoadData) {
}
@Override
public void onUpstreamDiscarded(EventTime eventTime,
MediaSourceEventListener.MediaLoadData mediaLoadData) {
}
@Override
public void onMediaPeriodCreated(EventTime eventTime) {
}
@Override
public void onMediaPeriodReleased(EventTime eventTime) {
}
@Override
public void onReadingStarted(EventTime eventTime) {
}
@Override
public void onBandwidthEstimate(EventTime eventTime, int totalLoadTimeMs,
long totalBytesLoaded, long bitrateEstimate) {
}
@Override
public void onSurfaceSizeChanged(EventTime eventTime, int width, int height) {
}
@Override
public void onMetadata(EventTime eventTime, Metadata metadata) {
}
@Override
public void onDecoderEnabled(EventTime eventTime, int trackType,
DecoderCounters decoderCounters) {
}
@Override
public void onDecoderInitialized(EventTime eventTime, int trackType, String decoderName,
long initializationDurationMs) {
}
@Override
public void onDecoderInputFormatChanged(EventTime eventTime, int trackType, Format format) {
}
@Override
public void onDecoderDisabled(EventTime eventTime, int trackType,
DecoderCounters decoderCounters) {
}
@Override
public void onAudioSessionId(EventTime eventTime, int audioSessionId) {
}
@Override
public void onAudioAttributesChanged(EventTime eventTime, AudioAttributes audioAttributes) {
}
@Override
public void onVolumeChanged(EventTime eventTime, float volume) {
TestFairy.addEvent(String.format(Locale.ENGLISH, "Volume level has changed to %s",
Float.valueOf(volume * 100f).intValue()));
}
@Override
public void onAudioUnderrun(EventTime eventTime, int bufferSize, long bufferSizeMs,
long elapsedSinceLastFeedMs) {
}
@Override
public void onDroppedVideoFrames(EventTime eventTime, int droppedFrames, long elapsedMs) {
TestFairy.addEvent(
String.format(Locale.ENGLISH, "Video has dropped %d frames in %dms", droppedFrames,
elapsedMs));
}
@Override
public void onVideoSizeChanged(EventTime eventTime, int width, int height,
int unappliedRotationDegrees, float pixelWidthHeightRatio) {
}
@Override
public void onRenderedFirstFrame(EventTime eventTime, @Nullable Surface surface) {
}
@Override
public void onDrmSessionAcquired(EventTime eventTime) {
}
@Override
public void onDrmKeysLoaded(EventTime eventTime) {
}
@Override
public void onDrmSessionManagerError(EventTime eventTime, Exception error) {
TestFairy.addEvent("Drm session manager error occured");
TestFairy.logThrowable(error);
}
@Override
public void onDrmKeysRestored(EventTime eventTime) {
}
@Override
public void onDrmKeysRemoved(EventTime eventTime) {
}
@Override
public void onDrmSessionReleased(EventTime eventTime) {
}
}