11
11
import android .net .Uri ;
12
12
import android .view .Surface ;
13
13
import androidx .annotation .NonNull ;
14
+ import androidx .annotation .Nullable ;
14
15
import androidx .annotation .OptIn ;
15
16
import androidx .annotation .VisibleForTesting ;
16
17
import androidx .media3 .common .AudioAttributes ;
42
43
import java .util .List ;
43
44
import java .util .Map ;
44
45
45
- // API marked with UnstableApi is safe to use, it is only delicate when targeting stable ABI
46
- // https://developer.android.com/media/media3/exoplayer/migration-guide#unstableapi
47
- @ OptIn (markerClass = UnstableApi .class )
48
46
final class VideoPlayer {
49
47
private static final String FORMAT_SS = "ss" ;
50
48
private static final String FORMAT_DASH = "dash" ;
@@ -90,7 +88,7 @@ final class VideoPlayer {
90
88
91
89
MediaSource mediaSource = buildMediaSource (uri , dataSourceFactory , formatHint );
92
90
93
- exoPlayer . setMediaSource ( mediaSource );
91
+ unstableSetMediaSource ( exoPlayer , mediaSource );
94
92
exoPlayer .prepare ();
95
93
96
94
setUpVideoPlayer (exoPlayer , new QueuingEventSink ());
@@ -121,11 +119,8 @@ public void buildHttpDataSourceFactory(@NonNull Map<String, String> httpHeaders)
121
119
? httpHeaders .get (USER_AGENT )
122
120
: "ExoPlayer" ;
123
121
124
- httpDataSourceFactory .setUserAgent (userAgent ).setAllowCrossProtocolRedirects (true );
125
-
126
- if (httpHeadersNotEmpty ) {
127
- httpDataSourceFactory .setDefaultRequestProperties (httpHeaders );
128
- }
122
+ unstableUpdateDataSourceFactory (
123
+ httpDataSourceFactory , httpHeaders , userAgent , httpHeadersNotEmpty );
129
124
}
130
125
131
126
private MediaSource buildMediaSource (
@@ -152,26 +147,7 @@ private MediaSource buildMediaSource(
152
147
break ;
153
148
}
154
149
}
155
- switch (type ) {
156
- case C .CONTENT_TYPE_SS :
157
- return new SsMediaSource .Factory (
158
- new DefaultSsChunkSource .Factory (mediaDataSourceFactory ), mediaDataSourceFactory )
159
- .createMediaSource (MediaItem .fromUri (uri ));
160
- case C .CONTENT_TYPE_DASH :
161
- return new DashMediaSource .Factory (
162
- new DefaultDashChunkSource .Factory (mediaDataSourceFactory ), mediaDataSourceFactory )
163
- .createMediaSource (MediaItem .fromUri (uri ));
164
- case C .CONTENT_TYPE_HLS :
165
- return new HlsMediaSource .Factory (mediaDataSourceFactory )
166
- .createMediaSource (MediaItem .fromUri (uri ));
167
- case C .CONTENT_TYPE_OTHER :
168
- return new ProgressiveMediaSource .Factory (mediaDataSourceFactory )
169
- .createMediaSource (MediaItem .fromUri (uri ));
170
- default :
171
- {
172
- throw new IllegalStateException ("Unsupported type: " + type );
173
- }
174
- }
150
+ return unstableBuildMediaSource (uri , mediaDataSourceFactory , type );
175
151
}
176
152
177
153
private void setUpVideoPlayer (ExoPlayer exoPlayer , QueuingEventSink eventSink ) {
@@ -309,15 +285,15 @@ void sendInitialized() {
309
285
event .put ("event" , "initialized" );
310
286
event .put ("duration" , exoPlayer .getDuration ());
311
287
312
- if ( exoPlayer . getVideoFormat () != null ) {
313
- Format videoFormat = exoPlayer . getVideoFormat ();
288
+ Format videoFormat = unstableGetVideoFormat ( exoPlayer );
289
+ if ( videoFormat != null ) {
314
290
int width = videoFormat .width ;
315
291
int height = videoFormat .height ;
316
- int rotationDegrees = videoFormat . rotationDegrees ;
292
+ int rotationDegrees = unstableGetRotationDegrees ( videoFormat ) ;
317
293
// Switch the width/height if video was taken in portrait mode
318
294
if (rotationDegrees == 90 || rotationDegrees == 270 ) {
319
- width = exoPlayer . getVideoFormat () .height ;
320
- height = exoPlayer . getVideoFormat () .width ;
295
+ width = videoFormat .height ;
296
+ height = videoFormat .width ;
321
297
}
322
298
event .put ("width" , width );
323
299
event .put ("height" , height );
@@ -348,4 +324,58 @@ void dispose() {
348
324
exoPlayer .release ();
349
325
}
350
326
}
327
+
328
+ @ OptIn (markerClass = UnstableApi .class )
329
+ private static void unstableSetMediaSource (ExoPlayer exoPlayer , MediaSource mediaSource ) {
330
+ exoPlayer .setMediaSource (mediaSource );
331
+ }
332
+
333
+ @ OptIn (markerClass = UnstableApi .class )
334
+ private static void unstableUpdateDataSourceFactory (
335
+ DefaultHttpDataSource .Factory factory ,
336
+ @ NonNull Map <String , String > httpHeaders ,
337
+ String userAgent ,
338
+ boolean httpHeadersNotEmpty ) {
339
+ factory .setUserAgent (userAgent ).setAllowCrossProtocolRedirects (true );
340
+
341
+ if (httpHeadersNotEmpty ) {
342
+ factory .setDefaultRequestProperties (httpHeaders );
343
+ }
344
+ }
345
+
346
+ @ OptIn (markerClass = UnstableApi .class )
347
+ private static MediaSource unstableBuildMediaSource (
348
+ Uri uri , DataSource .Factory mediaDataSourceFactory , int type ) {
349
+ switch (type ) {
350
+ case C .CONTENT_TYPE_SS :
351
+ return new SsMediaSource .Factory (
352
+ new DefaultSsChunkSource .Factory (mediaDataSourceFactory ), mediaDataSourceFactory )
353
+ .createMediaSource (MediaItem .fromUri (uri ));
354
+ case C .CONTENT_TYPE_DASH :
355
+ return new DashMediaSource .Factory (
356
+ new DefaultDashChunkSource .Factory (mediaDataSourceFactory ), mediaDataSourceFactory )
357
+ .createMediaSource (MediaItem .fromUri (uri ));
358
+ case C .CONTENT_TYPE_HLS :
359
+ return new HlsMediaSource .Factory (mediaDataSourceFactory )
360
+ .createMediaSource (MediaItem .fromUri (uri ));
361
+ case C .CONTENT_TYPE_OTHER :
362
+ return new ProgressiveMediaSource .Factory (mediaDataSourceFactory )
363
+ .createMediaSource (MediaItem .fromUri (uri ));
364
+ default :
365
+ {
366
+ throw new IllegalStateException ("Unsupported type: " + type );
367
+ }
368
+ }
369
+ }
370
+
371
+ @ OptIn (markerClass = UnstableApi .class )
372
+ @ Nullable
373
+ private static Format unstableGetVideoFormat (ExoPlayer exoPlayer ) {
374
+ return exoPlayer .getVideoFormat ();
375
+ }
376
+
377
+ @ OptIn (markerClass = UnstableApi .class )
378
+ private static int unstableGetRotationDegrees (Format videoFormat ) {
379
+ return videoFormat .rotationDegrees ;
380
+ }
351
381
}
0 commit comments