aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml/docilealligator/infinityforreddit/videoautoplay/Playable.java
blob: 384277a3a8f98e6f486c4db883821413a38d90b1 (plain) (blame)
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
 * Copyright (c) 2018 Nam Nguyen, nam@ene.im
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ml.docilealligator.infinityforreddit.videoautoplay;

import androidx.annotation.FloatRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.media3.common.Metadata;
import androidx.media3.common.PlaybackException;
import androidx.media3.common.PlaybackParameters;
import androidx.media3.common.Player;
import androidx.media3.common.Timeline;
import androidx.media3.common.Tracks;
import androidx.media3.common.VideoSize;
import androidx.media3.common.text.Cue;
import androidx.media3.common.text.CueGroup;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.exoplayer.metadata.MetadataOutput;
import androidx.media3.exoplayer.source.MediaSource;
import androidx.media3.exoplayer.text.TextOutput;
import androidx.media3.ui.PlayerView;

import java.util.List;
import java.util.concurrent.CopyOnWriteArraySet;

import ml.docilealligator.infinityforreddit.videoautoplay.annotations.RemoveIn;
import ml.docilealligator.infinityforreddit.videoautoplay.media.PlaybackInfo;
import ml.docilealligator.infinityforreddit.videoautoplay.media.VolumeInfo;

/**
 * Define an interface to control a playback, specific for {@link ExoPlayer} and {@link PlayerView}.
 * <p>
 * This interface is designed to be reused across Config change. Implementation must not hold any
 * strong reference to Activity, and if it supports any kind of that, make sure to implicitly clean
 * it up.
 *
 * @author eneim
 * @since 3.4.0
 */

@SuppressWarnings("unused") //
public interface Playable {

    /**
     * Prepare the resource for a {@link ExoPlayer}. This method should:
     * - Request for new {@link ExoPlayer} instance if there is not a usable one.
     * - Configure {@link EventListener} for it.
     * - If there is non-trivial PlaybackInfo, update it to the ExoPlayer.
     * - If client request to prepare MediaSource, then prepare it.
     * <p>
     * This method must be called before {@link #setPlayerView(PlayerView)}.
     *
     * @param prepareSource if {@code true}, also prepare the MediaSource when preparing the Player,
     *                      if {@code false} just do nothing for the MediaSource.
     */
    void prepare(boolean prepareSource);

    /**
     * Set the {@link PlayerView} for this Playable. It is expected that a playback doesn't require a
     * UI, so this setup is optional. But it must be called after the ExoPlayer is prepared,
     * that is after {@link #prepare(boolean)} and before {@link #release()}.
     * <p>
     * Changing the PlayerView during playback is expected, though not always recommended, especially
     * on old Devices with low Android API.
     *
     * @param playerView the PlayerView to set to the ExoPlayer.
     */
    void setPlayerView(@Nullable PlayerView playerView);

    /**
     * Get current {@link PlayerView} of this Playable.
     *
     * @return current PlayerView instance of this Playable.
     */
    @Nullable
    PlayerView getPlayerView();

    /**
     * Start the playback. If the {@link MediaSource} is not prepared, then also prepare it.
     */
    void play();

    /**
     * Pause the playback.
     */
    void pause();

    /**
     * Reset all resource, so that the playback can start all over again. This is to cleanup the
     * playback for reuse. The ExoPlayer instance must be still usable without calling
     * {@link #prepare(boolean)}.
     */
    void reset();

    /**
     * Release all resource. After this, the ExoPlayer is released to the Player pool and the
     * Playable must call {@link #prepare(boolean)} again to use it again.
     */
    void release();

    /**
     * Get current {@link PlaybackInfo} of the playback.
     *
     * @return current PlaybackInfo of the playback.
     */
    @NonNull
    PlaybackInfo getPlaybackInfo();

    /**
     * Set the custom {@link PlaybackInfo} for this playback. This could suggest a seek.
     *
     * @param playbackInfo the PlaybackInfo to set for this playback.
     */
    void setPlaybackInfo(@NonNull PlaybackInfo playbackInfo);

    /**
     * Add a new {@link EventListener} to this Playable. As calling {@link #prepare(boolean)} also
     * triggers some internal events, this method should be called before {@link #prepare(boolean)} so
     * that Client could received them all.
     *
     * @param listener the EventListener to add, must be not {@code null}.
     */
    void addEventListener(@NonNull EventListener listener);

    /**
     * Remove an {@link EventListener} from this Playable.
     *
     * @param listener the EventListener to be removed. If null, nothing happens.
     */
    void removeEventListener(EventListener listener);

    /**
     * !This must only work if the Player in use is a {@link ToroExoPlayer}.
     */
    void addOnVolumeChangeListener(@NonNull ToroPlayer.OnVolumeChangeListener listener);

    void removeOnVolumeChangeListener(@Nullable ToroPlayer.OnVolumeChangeListener listener);

    /**
     * Check if current Playable is playing or not.
     *
     * @return {@code true} if this Playable is playing, {@code false} otherwise.
     */
    boolean isPlaying();

    /**
     * Change the volume of current playback.
     *
     * @param volume the volume value to be set. Must be a {@code float} of range from 0 to 1.
     * @deprecated use {@link #setVolumeInfo(VolumeInfo)} instead.
     */
    @RemoveIn(version = "3.6.0")
    @Deprecated
    //
    void setVolume(@FloatRange(from = 0.0, to = 1.0) float volume);

    /**
     * Obtain current volume value. The returned value is a {@code float} of range from 0 to 1.
     *
     * @return current volume value.
     * @deprecated use {@link #getVolumeInfo()} instead.
     */
    @RemoveIn(version = "3.6.0")
    @Deprecated  //
    @FloatRange(from = 0.0, to = 1.0)
    float getVolume();

    /**
     * Update playback's volume.
     *
     * @param volumeInfo the {@link VolumeInfo} to update to.
     * @return {@code true} if current Volume info is updated, {@code false} otherwise.
     */
    boolean setVolumeInfo(@NonNull VolumeInfo volumeInfo);

    /**
     * Get current {@link VolumeInfo}.
     */
    @NonNull
    VolumeInfo getVolumeInfo();

    /**
     * Same as {@link Player#setPlaybackParameters(PlaybackParameters)}
     */
    void setParameters(@Nullable PlaybackParameters parameters);

    /**
     * Same as {@link Player#getPlaybackParameters()}
     */
    @Nullable
    PlaybackParameters getParameters();

    void addErrorListener(@NonNull ToroPlayer.OnErrorListener listener);

    void removeErrorListener(@Nullable ToroPlayer.OnErrorListener listener);

    // Combine necessary interfaces.
    @UnstableApi
    interface EventListener extends Player.Listener, TextOutput, MetadataOutput {

        @Override
        default void onCues(@NonNull List<Cue> cues) {

        }

        @Override
        default void onCues(@NonNull CueGroup cueGroup) {

        }

        @Override
        default void onMetadata(@NonNull Metadata metadata) {

        }
    }

    /**
     * Default empty implementation
     */
    @UnstableApi
    class DefaultEventListener implements EventListener {
        @Override
        public void onTimelineChanged(@NonNull Timeline timeline, int reason) {

        }

        @Override
        public void onTracksChanged(@NonNull Tracks tracks) {

        }

        @Override
        public void onIsLoadingChanged(boolean isLoading) {

        }

        @Override
        public void onPlaybackStateChanged(int playbackState) {

        }

        @Override
        public void onRepeatModeChanged(int repeatMode) {

        }

        @Override
        public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {

        }

        @Override
        public void onPlayerError(@NonNull PlaybackException error) {

        }

        @Override
        public void onPositionDiscontinuity(@NonNull Player.PositionInfo oldPosition, @NonNull Player.PositionInfo newPosition, int reason) {

        }

        @Override
        public void onPlaybackParametersChanged(@NonNull PlaybackParameters playbackParameters) {

        }

        @Override
        public void onVideoSizeChanged(@NonNull VideoSize videoSize) {
            EventListener.super.onVideoSizeChanged(videoSize);
        }

        @Override
        public void onRenderedFirstFrame() {

        }

        @Override
        public void onCues(@NonNull CueGroup cueGroup) {
            EventListener.super.onCues(cueGroup);
        }

        @Override
        public void onMetadata(@NonNull Metadata metadata) {

        }
    }

    /**
     * List of EventListener
     */
    @UnstableApi
    class EventListeners extends CopyOnWriteArraySet<EventListener> implements EventListener {

        EventListeners() {
        }

        @Override
        public void onEvents(@NonNull Player player, @NonNull Player.Events events) {
            for (EventListener eventListener : this) {
                eventListener.onEvents(player, events);
            }
        }

        @Override
        public void onVideoSizeChanged(@NonNull VideoSize videoSize) {
            for (EventListener eventListener : this) {
                eventListener.onVideoSizeChanged(videoSize);
            }
        }

        @Override
        public void onRenderedFirstFrame() {
            for (EventListener eventListener : this) {
                eventListener.onRenderedFirstFrame();
            }
        }

        @Override
        public void onTimelineChanged(@NonNull Timeline timeline, int reason) {
            for (EventListener eventListener : this) {
                eventListener.onTimelineChanged(timeline, reason);
            }
        }

        @Override
        public void onTracksChanged(@NonNull Tracks tracks) {
            for (EventListener eventListener : this) {
                eventListener.onTracksChanged(tracks);
            }
        }

        @Override
        public void onIsLoadingChanged(boolean isLoading) {
            for (EventListener eventListener : this) {
                eventListener.onIsLoadingChanged(isLoading);
            }
        }

        @Override
        public void onPlaybackStateChanged(int playbackState) {
            for (EventListener eventListener : this) {
                eventListener.onPlaybackStateChanged(playbackState);
            }
        }

        @Override
        public void onRepeatModeChanged(int repeatMode) {
            for (EventListener eventListener : this) {
                eventListener.onRepeatModeChanged(repeatMode);
            }
        }

        @Override
        public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {
            for (EventListener eventListener : this) {
                eventListener.onShuffleModeEnabledChanged(shuffleModeEnabled);
            }
        }

        @Override
        public void onPlayerError(@NonNull PlaybackException error) {
            for (EventListener eventListener : this) {
                eventListener.onPlayerError(error);
            }
        }

        @Override
        public void onPositionDiscontinuity(@NonNull Player.PositionInfo oldPosition, @NonNull Player.PositionInfo newPosition, int reason) {
            for (EventListener eventListener : this) {
                eventListener.onPositionDiscontinuity(oldPosition, newPosition, reason);
            }
        }

        @Override
        public void onPlaybackParametersChanged(@NonNull PlaybackParameters playbackParameters) {
            for (EventListener eventListener : this) {
                eventListener.onPlaybackParametersChanged(playbackParameters);
            }
        }

        @Override
        public void onCues(@NonNull CueGroup cueGroup) {
            for (EventListener eventListener : this) {
                eventListener.onCues(cueGroup);
            }
        }

        @Override
        public void onMetadata(@NonNull Metadata metadata) {
            for (EventListener eventListener : this) {
                eventListener.onMetadata(metadata);
            }
        }
    }
}