Skip to content

This repository demonstrates a practical implementation of WebRTC in Android WebView with H.265 video codec support. It provides a clean, Kotlin-based solution for handling camera/microphone permissions and enabling H.265 in WebRTC connections.

Notifications You must be signed in to change notification settings

mbobiosio/Android265WebView

Repository files navigation

WebRTC with H.265 Support for Android WebView

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.

Overview

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

Why This Project?

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:

  1. Uses JavaScript injection instead of reflection on internal APIs
  2. Provides a maintainable, future-proof approach
  3. Uses modern permission handling for camera and microphone access

Requirements

  • 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'
}

Project Structure

  • WebViewH265Handler.kt: Enables H.265 support in WebRTC connections
  • WebRtcPermissionHandler.kt: Handles permissions required for WebRTC
  • WebRtcDemoActivity.kt: Sample activity implementing WebRTC with H.265

How It Works

H.265 Support Implementation

Instead of using potentially unreliable reflection on internal WebView APIs, this project:

  1. Injects JavaScript code that modifies WebRTC API calls
  2. Configures getUserMedia and RTCPeerConnection to prefer H.265
  3. Uses only public WebView APIs for better compatibility
// Example of how it's applied to a WebView instance
WebViewH265Handler.applyToWebView(webView)

Permission Handling

The project demonstrates two approaches:

  1. Modern (recommended): Using Activity Result API
  2. Legacy: Using onRequestPermissionsResult

The implementation handles:

  • Initial permission requests
  • Permission denials with retry options
  • Permanent denials with settings navigation
  • Permission rationale dialogs

Sample Usage

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
            // ...
        )
    }
}

Testing

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.

Key Features

  1. No reflection on internal APIs: Uses only supported public APIs
  2. Better UX for permission handling: Clear dialogs with retry options
  3. Modern Kotlin approach: Uses scope functions for cleaner code
  4. Material Design dialogs: Consistent with modern Android UI

Adapting For Your Project

To use this code in your own project:

  1. Copy the WebViewH265Handler.kt and WebRtcPermissionHandler.kt files
  2. Update your AndroidManifest.xml to include camera and microphone permissions
  3. Implement the permission handling in your Activity or Fragment
  4. Configure your WebView to use the H.265 handler

Limitations

  • 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

License

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.

About

This repository demonstrates a practical implementation of WebRTC in Android WebView with H.265 video codec support. It provides a clean, Kotlin-based solution for handling camera/microphone permissions and enabling H.265 in WebRTC connections.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages