4.でローカルに保存したHTMLをCefSharpを用いて表示させましたが、これに少し手を加えてWebRTCをやってみます。
1.これまでの以下1、2、3をおさらいして、新しくプロジェクトを作成します。
- WinFormsアプリを作成して、NuGetパッケージを使ってCefSharpを追加する。
- プラットフォーム構成を変更して、AnyCPUで使用できるようにする。
- CefSharpを使って、ローカルで動くWebアプリケーションを作る。
2.フォーム(Form1.cs)を少し変更します。
関数 InitializeChromium() 内に以下一行を追加します。
settings.CefCommandLineArgs.Add(“enable-media-stream”, “1”);
3.サンプルHTMLの index.html を少し変更します。
以下の一行とWebRTC用のJavaScriptを追加します。(下図の赤枠)
<video id=”myVideo” width=”400″ height=”300″ autoplay=”1″></video>
4.ここまで出来たら、実行してみます。カメラで撮った画面が映し出されたらOKです。
【サンプルHTML】
<!doctype html>
<html lang="jp">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>サンプル ハローワールド</title>
</head>
<body>
<h1>これはCefSharpのデモです</h1>
<video id="myVideo" width="400" height="300" autoplay="1"></video>
<br>
<button class="btn btn-info" onclick="cefCustomObject.showDevTools();">Open Chrome Dev Tools</button>
<button class="btn btn-primary" onclick="cefCustomObject.opencmd();">Open cmd.exe</button>
<script type="text/javascript">
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || window.navigator.mozGetUserMedia;
window.URL = window.URL || window.webkitURL;
let video = document.getElementById('myVideo');
let localStream = null;
navigator.getUserMedia({ video: true, audio: false },
function (stream) { // for success case
console.log(stream);
/* video.src = window.URL.createObjectURL(stream);
Chrome68以降で URL.createObjectURL が使えなくなったらしいので、
以下のsrcObjectプロパティを使うように修正 */
video.srcObject = stream;
},
function (err) { // for error case
console.log(err);
});
</script>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
【フォーム(Form1.cs)】
using System;
using System.IO;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
namespace WindowsFormsApp19
{
public partial class Form1 : Form
{
public ChromiumWebBrowser chromeBrowser;
public Form1()
{
InitializeComponent();
// Start the browser after initialize global component
InitializeChromium();
//For legacy biding we'll still have support for
CefSharpSettings.LegacyJavascriptBindingEnabled = true;
// Register an object in javascript named "cefCustomObject" with function of the CefCustomObject class :3
chromeBrowser.RegisterJsObject("cefCustomObject", new CefCustomObject(chromeBrowser, this));
}
private void Form1_Load(object sender, EventArgs e)
{
chromeBrowser.ShowDevTools();
}
public void InitializeChromium()
{
CefSettings settings = new CefSettings();
// Note that if you get an error or a white screen, you may be doing something wrong !
// Try to load a local file that you're sure that exists and give the complete path instead to test
// for example, replace page with a direct path instead :
// String page = @"C:\Users\SDkCarlos\Desktop\afolder\index.html";
String page = string.Format(@"{0}\sample_html\index.html", Application.StartupPath);
if (!File.Exists(page))
{
MessageBox.Show("Error The html file doesn't exists : " + page);
}
// Initialize cef with a command line argument
// In this case the enable-media-stream flag that allows you to access the camera and the microphone
settings.CefCommandLineArgs.Add("enable-media-stream", "1");
// Initialize cef with the provided settings
Cef.Initialize(settings);
// Create a browser component
chromeBrowser = new ChromiumWebBrowser(page);
// Add it to the form and fill it to the form window.
this.Controls.Add(chromeBrowser);
chromeBrowser.Dock = DockStyle.Fill;
// Allow the use of local resources in the browser
BrowserSettings browserSettings = new BrowserSettings();
browserSettings.FileAccessFromFileUrls = CefState.Enabled;
browserSettings.UniversalAccessFromFileUrls = CefState.Enabled;
chromeBrowser.BrowserSettings = browserSettings;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Cef.Shutdown();
}
}
}




