|
| 1 | +# SurfaceControl Implementation for GSYVideoPlayer |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This implementation adds SurfaceControl support to the Exo2PlayerManager for more efficient Surface switching, as suggested in androidx/media/issues/2733. |
| 6 | + |
| 7 | +## What is SurfaceControl? |
| 8 | + |
| 9 | +SurfaceControl is an Android API introduced in API level 29 (Android 10) that provides more efficient and atomic surface operations. It allows for: |
| 10 | + |
| 11 | +- Better performance when switching between surfaces |
| 12 | +- Atomic surface operations through transactions |
| 13 | +- Reduced visual artifacts during surface transitions |
| 14 | +- Improved overall video playback experience |
| 15 | + |
| 16 | +## Implementation Details |
| 17 | + |
| 18 | +### SurfaceControlHelper Class |
| 19 | + |
| 20 | +The `SurfaceControlHelper` class provides a compatibility wrapper that: |
| 21 | + |
| 22 | +1. **API Level Detection**: Automatically detects if SurfaceControl is available (API 29+) |
| 23 | +2. **Graceful Fallback**: Falls back to standard `setSurface()` for older API levels |
| 24 | +3. **Error Handling**: Handles SurfaceControl initialization failures gracefully |
| 25 | +4. **Resource Management**: Properly manages SurfaceControl.Transaction lifecycle |
| 26 | + |
| 27 | +### Key Components |
| 28 | + |
| 29 | +#### SurfaceSwitcher Interface |
| 30 | +```java |
| 31 | +public interface SurfaceSwitcher { |
| 32 | + void switchToSurface(Surface surface); |
| 33 | + void release(); |
| 34 | + boolean isUsingSurfaceControl(); |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +#### SurfaceControlSwitcher (API 29+) |
| 39 | +- Uses `SurfaceControl.Transaction` for atomic operations |
| 40 | +- Synchronized surface switching for thread safety |
| 41 | +- Automatic fallback on errors |
| 42 | + |
| 43 | +#### StandardSurfaceSwitcher (API < 29) |
| 44 | +- Uses traditional `setSurface()` method |
| 45 | +- Maintains compatibility with older devices |
| 46 | + |
| 47 | +## Integration |
| 48 | + |
| 49 | +### Exo2PlayerManager |
| 50 | +The main `Exo2PlayerManager` class has been updated to: |
| 51 | + |
| 52 | +- Initialize `SurfaceControlHelper` during player setup |
| 53 | +- Use SurfaceControl-based switching in `showDisplay()` method |
| 54 | +- Properly clean up resources in `release()` method |
| 55 | +- Provide `isUsingSurfaceControl()` for debugging |
| 56 | + |
| 57 | +### GSYExoPlayerManager |
| 58 | +The sample `GSYExoPlayerManager` class has also been updated with the same enhancements. |
| 59 | + |
| 60 | +## Usage Example |
| 61 | + |
| 62 | +```java |
| 63 | +// The SurfaceControl functionality is automatically enabled |
| 64 | +Exo2PlayerManager playerManager = new Exo2PlayerManager(); |
| 65 | +// ... initialize player ... |
| 66 | + |
| 67 | +// Check if SurfaceControl is being used |
| 68 | +boolean usingSurfaceControl = playerManager.isUsingSurfaceControl(); |
| 69 | +Log.i("Player", "Using SurfaceControl: " + usingSurfaceControl); |
| 70 | + |
| 71 | +// Surface switching happens automatically through showDisplay() |
| 72 | +// and will use SurfaceControl if available |
| 73 | +``` |
| 74 | + |
| 75 | +## Testing and Debugging |
| 76 | + |
| 77 | +### SurfaceControlTestUtils |
| 78 | +A utility class is provided for testing and debugging: |
| 79 | + |
| 80 | +```java |
| 81 | +// Test SurfaceControl support |
| 82 | +SurfaceControlTestUtils.testSurfaceControlSupport(playerManager); |
| 83 | + |
| 84 | +// Log surface switch operations |
| 85 | +SurfaceControlTestUtils.logSurfaceSwitch(playerManager, "TextureView"); |
| 86 | + |
| 87 | +// Get SurfaceControl availability info |
| 88 | +String info = SurfaceControlTestUtils.getSurfaceControlInfo(); |
| 89 | +``` |
| 90 | + |
| 91 | +### Logging |
| 92 | +The implementation includes verbose logging to help developers understand: |
| 93 | +- When SurfaceControl is successfully initialized |
| 94 | +- When fallback to standard switching occurs |
| 95 | +- Individual surface switch operations |
| 96 | + |
| 97 | +## Benefits |
| 98 | + |
| 99 | +1. **Performance**: Better surface switching performance on API 29+ devices |
| 100 | +2. **Compatibility**: Full backward compatibility with older Android versions |
| 101 | +3. **Reliability**: Graceful error handling and automatic fallbacks |
| 102 | +4. **Transparency**: No changes required to existing application code |
| 103 | + |
| 104 | +## Requirements |
| 105 | + |
| 106 | +- **Minimum API**: No change (same as original GSYVideoPlayer) |
| 107 | +- **Target API**: Enhanced functionality on API 29+ |
| 108 | +- **Dependencies**: Uses existing Media3/ExoPlayer dependencies |
| 109 | + |
| 110 | +## Backward Compatibility |
| 111 | + |
| 112 | +The implementation is fully backward compatible: |
| 113 | +- On devices with API < 29: Uses standard surface switching |
| 114 | +- On devices with API 29+: Uses SurfaceControl if available, falls back if needed |
| 115 | +- Existing applications require no code changes |
| 116 | +- All existing functionality is preserved |
0 commit comments