A sample project demonstrating how to implement WebRTC in Android WebView with H.265 video codec support. This project shows a practical approach to handling WebRTC permissions, codec configuration, and WebView setup in modern Kotlin Android applications.
This project demonstrates:
- How to enable H.265 (HEVC) codec in WebRTC connections via WebView
- Modern approach to runtime permission handling using Activity Result API
- Robust permission dialog handling with retry mechanisms
- Elegant Kotlin implementation with scope functions
Google Chrome can enable H.265 support in WebRTC through command-line flags:
--enable-features=WebRtcAllowH265Send,WebRtcAllowH265Receive
--force-fieldtrials=WebRTC-Video-H26xPacketBuffer/Enabled/
However, enabling these features in Android WebView is not straightforward. This project shows a working implementation that:
- Uses JavaScript injection instead of reflection on internal APIs
- Provides a maintainable, future-proof approach
- Uses modern permission handling for camera and microphone access
- Android API level 26+ (Android 8.0 Oreo or higher)
- Kotlin 2.1.0
- AndroidX
dependencies {
implementation 'androidx.webkit:webkit:1.13.0'
implementation 'com.google.android.material:material:1.12.0'
}
WebViewH265Handler.kt
: Enables H.265 support in WebRTC connectionsWebRtcPermissionHandler.kt
: Handles permissions required for WebRTCWebRtcDemoActivity.kt
: Sample activity implementing WebRTC with H.265
Instead of using potentially unreliable reflection on internal WebView APIs, this project:
- Injects JavaScript code that modifies WebRTC API calls
- Configures getUserMedia and RTCPeerConnection to prefer H.265
- Uses only public WebView APIs for better compatibility
// Example of how it's applied to a WebView instance
WebViewH265Handler.applyToWebView(webView)
The project demonstrates two approaches:
- Modern (recommended): Using Activity Result API
- Legacy: Using onRequestPermissionsResult
The implementation handles:
- Initial permission requests
- Permission denials with retry options
- Permanent denials with settings navigation
- Permission rationale dialogs
class WebRtcDemoActivity : AppCompatActivity() {
private lateinit var webView: WebView
private val testUrl = "https://webrtc.github.io/samples/src/content/getusermedia/resolution/"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_webrtc_demo)
// Initialize WebView
findViewById<WebView>(R.id.webview)?.let {
webView = it
setupWebRtcWebView()
}
}
private fun setupWebRtcWebView() {
// Set up WebView with permission handling
WebRtcPermissionHandler.setupWebRtcWebView(
activity = this,
webView = webView,
url = testUrl,
// Show custom dialogs, handle permission denials
// ...
)
}
}
You can test the WebRTC implementation with the default testing URL or any other WebRTC service. The code is pre-configured to work with:
This page will attempt to access your camera and display the video feed, allowing you to verify that permissions and H.265 support are working.
- No reflection on internal APIs: Uses only supported public APIs
- Better UX for permission handling: Clear dialogs with retry options
- Modern Kotlin approach: Uses scope functions for cleaner code
- Material Design dialogs: Consistent with modern Android UI
To use this code in your own project:
- Copy the
WebViewH265Handler.kt
andWebRtcPermissionHandler.kt
files - Update your AndroidManifest.xml to include camera and microphone permissions
- Implement the permission handling in your Activity or Fragment
- Configure your WebView to use the H.265 handler
- H.265 support depends on the underlying WebView implementation
- Not all devices support H.265 hardware encoding/decoding
- Some browser-specific WebRTC features might not be available
MIT License
Copyright (c) 2025
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.