Skip to content

Commit

Permalink
Core - Re-Add IAudioHandler (#3153)
Browse files Browse the repository at this point in the history
Resolves #3152
  • Loading branch information
amaitland authored Jul 7, 2020
1 parent 9d4bc5d commit 8d25b71
Show file tree
Hide file tree
Showing 15 changed files with 458 additions and 4 deletions.
73 changes: 73 additions & 0 deletions CefSharp.Core/Internals/ClientAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,79 @@ namespace CefSharp
}
}

bool ClientAdapter::GetAudioParameters(CefRefPtr<CefBrowser> browser, CefAudioParameters & params)
{
auto handler = _browserControl->AudioHandler;

if (handler == nullptr)
{
return false;
}

auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
auto parameters = new AudioParameters((CefSharp::Enums::ChannelLayout)params.channel_layout, params.sample_rate, params.frames_per_buffer);

auto result = handler->GetAudioParameters(_browserControl, browserWrapper, *parameters);

if (result)
{
params.channel_layout = (cef_channel_layout_t)parameters->ChannelLayout;
params.sample_rate = parameters->SampleRate;
params.frames_per_buffer = parameters->FramesPerBuffer;
}

return result;
}

void ClientAdapter::OnAudioStreamStarted(CefRefPtr<CefBrowser> browser, const CefAudioParameters& params, int channels)
{
auto handler = _browserControl->AudioHandler;

if (handler != nullptr)
{
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());
AudioParameters parameters((CefSharp::Enums::ChannelLayout)params.channel_layout, params.sample_rate, params.frames_per_buffer);

handler->OnAudioStreamStarted(_browserControl, browserWrapper, parameters, channels);
}
}

void ClientAdapter::OnAudioStreamPacket(CefRefPtr<CefBrowser> browser, const float** data, int frames, int64 pts)
{
auto handler = _browserControl->AudioHandler;

if (handler != nullptr)
{
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());

handler->OnAudioStreamPacket(_browserControl, browserWrapper, IntPtr((void *)data), frames, pts);
}
}

void ClientAdapter::OnAudioStreamStopped(CefRefPtr<CefBrowser> browser)
{
auto handler = _browserControl->AudioHandler;

if (handler != nullptr)
{
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());

handler->OnAudioStreamStopped(_browserControl, browserWrapper);
}
}

void ClientAdapter::OnAudioStreamError(CefRefPtr<CefBrowser> browser, const CefString& message)
{
auto handler = _browserControl->AudioHandler;

if (handler != nullptr)
{
auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup());

handler->OnAudioStreamError(_browserControl, browserWrapper, StringUtils::ToClr(message));
}
}

bool ClientAdapter::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefProcessId source_process, CefRefPtr<CefProcessMessage> message)
{
auto handled = false;
Expand Down
11 changes: 10 additions & 1 deletion CefSharp.Core/Internals/ClientAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ namespace CefSharp
public CefDialogHandler,
public CefDragHandler,
public CefDownloadHandler,
public CefFindHandler
public CefFindHandler,
public CefAudioHandler
{
private:
gcroot<IWebBrowserInternal^> _browserControl;
Expand Down Expand Up @@ -94,6 +95,7 @@ namespace CefSharp
virtual DECL CefRefPtr<CefDialogHandler> GetDialogHandler() OVERRIDE { return this; }
virtual DECL CefRefPtr<CefDragHandler> GetDragHandler() OVERRIDE { return this; }
virtual DECL CefRefPtr<CefFindHandler> GetFindHandler() OVERRIDE { return this; }
virtual DECL CefRefPtr<CefAudioHandler> GetAudioHandler() OVERRIDE { return this; }
virtual DECL bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefProcessId source_process, CefRefPtr<CefProcessMessage> message) OVERRIDE;


Expand Down Expand Up @@ -185,6 +187,13 @@ namespace CefSharp
//CefFindHandler
virtual DECL void OnFindResult(CefRefPtr<CefBrowser> browser, int identifier, int count, const CefRect& selectionRect, int activeMatchOrdinal, bool finalUpdate) OVERRIDE;

//CefAudioHandler
virtual DECL bool GetAudioParameters(CefRefPtr<CefBrowser> browser, CefAudioParameters & params) OVERRIDE;
virtual DECL void OnAudioStreamStarted(CefRefPtr<CefBrowser> browser, const CefAudioParameters& params, int channels) OVERRIDE;
virtual DECL void OnAudioStreamPacket(CefRefPtr<CefBrowser> browser, const float** data, int frames, int64 pts) OVERRIDE;
virtual DECL void OnAudioStreamStopped(CefRefPtr<CefBrowser> browser) OVERRIDE;
virtual DECL void OnAudioStreamError(CefRefPtr<CefBrowser> browser, const CefString& message) OVERRIDE;

IMPLEMENT_REFCOUNTING(ClientAdapter);
};
}
Expand Down
1 change: 1 addition & 0 deletions CefSharp.Example/CefSharp.Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<Compile Include="Callback\RunFileDialogCallback.cs" />
<Compile Include="DevTools\DevToolsExtensions.cs" />
<Compile Include="DevTools\TaskMethodDevToolsMessageObserver.cs" />
<Compile Include="Handlers\AudioHandler.cs" />
<Compile Include="Handlers\ExampleResourceRequestHandler.cs" />
<Compile Include="Handlers\ExtensionHandler.cs" />
<Compile Include="JavascriptBinding\AsyncBoundObject.cs" />
Expand Down
47 changes: 47 additions & 0 deletions CefSharp.Example/Handlers/AudioHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright © 2019 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

using System;
using CefSharp.Enums;
using CefSharp.Structs;

namespace CefSharp.Example.Handlers
{
public class AudioHandler : IAudioHandler
{
private ChannelLayout channelLayout;
private int channelCount;
private int sampleRate;

bool IAudioHandler.GetAudioParameters(IWebBrowser chromiumWebBrowser, IBrowser browser, ref AudioParameters parameters)
{
//Cancel Capture
return false;
}

void IAudioHandler.OnAudioStreamError(IWebBrowser chromiumWebBrowser, IBrowser browser, string errorMessage)
{

}

void IAudioHandler.OnAudioStreamPacket(IWebBrowser chromiumWebBrowser, IBrowser browser, IntPtr data, int noOfFrames, long pts)
{
//NOTE: data is an array representing the raw PCM data as a floating point type, i.e. 4-byte value(s)
//Based on and the channelLayout value passed to IAudioHandler.OnAudioStreamStarted
//you can calculate the size of the data array in bytes.
}

void IAudioHandler.OnAudioStreamStarted(IWebBrowser chromiumWebBrowser, IBrowser browser, AudioParameters parameters, int channels)
{
this.channelLayout = parameters.ChannelLayout;
this.sampleRate = parameters.SampleRate;
this.channelCount = channels;
}

void IAudioHandler.OnAudioStreamStopped(IWebBrowser chromiumWebBrowser, IBrowser browser)
{

}
}
}
6 changes: 4 additions & 2 deletions CefSharp.OffScreen/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,15 @@ public bool IsDisposed
/// </summary>
/// <value>The find handler.</value>
public IFindHandler FindHandler { get; set; }

/// <summary>
/// Implement <see cref="IAudioHandler" /> to handle audio events.
/// </summary>
public IAudioHandler AudioHandler { get; set; }
/// <summary>
/// Implement <see cref="IAccessibilityHandler" /> to handle events related to accessibility.
/// </summary>
/// <value>The accessibility handler.</value>
public IAccessibilityHandler AccessibilityHandler { get; set; }

/// <summary>
/// Event handler that will get called when the resource load for a navigation fails or is canceled.
/// It's important to note this event is fired on a CEF UI thread, which by default is not the same as your application UI
Expand Down
2 changes: 2 additions & 0 deletions CefSharp.WinForms.Example/BrowserTabUserControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public BrowserTabUserControl(Action<string, int?> openNewTab, string url, bool m
browser.RequestHandler = new WinFormsRequestHandler(openNewTab);
browser.JsDialogHandler = new JsDialogHandler();
browser.DownloadHandler = new DownloadHandler();
browser.AudioHandler = new AudioHandler();

if (multiThreadedMessageLoopEnabled)
{
browser.KeyboardHandler = new KeyboardHandler();
Expand Down
5 changes: 5 additions & 0 deletions CefSharp.WinForms/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@ public IRequestContext RequestContext
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), DefaultValue(null)]
public IFindHandler FindHandler { get; set; }
/// <summary>
/// Implement <see cref="IAudioHandler" /> to handle audio events.
/// </summary>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), DefaultValue(null)]
public IAudioHandler AudioHandler { get; set; }
/// <summary>
/// The <see cref="IFocusHandler" /> for this ChromiumWebBrowser.
/// </summary>
/// <value>The focus handler.</value>
Expand Down
1 change: 1 addition & 0 deletions CefSharp.Wpf.Example/Views/BrowserTabView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public BrowserTabView()
downloadHandler.OnBeforeDownloadFired += OnBeforeDownloadFired;
downloadHandler.OnDownloadUpdatedFired += OnDownloadUpdatedFired;
browser.DownloadHandler = downloadHandler;
browser.AudioHandler = new AudioHandler();

//Read an embedded bitmap into a memory stream then register it as a resource you can then load custom://cefsharp/images/beach.jpg
var beachImageStream = new MemoryStream();
Expand Down
5 changes: 4 additions & 1 deletion CefSharp.Wpf/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,10 @@ public IRequestContext RequestContext
/// </summary>
/// <value>The find handler.</value>
public IFindHandler FindHandler { get; set; }

/// <summary>
/// Implement <see cref="IAudioHandler" /> to handle audio events.
/// </summary>
public IAudioHandler AudioHandler { get; set; }
/// <summary>
/// Implement <see cref="IAccessibilityHandler" /> to handle events related to accessibility.
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions CefSharp/CefSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
<Compile Include="ResourceRequestHandlerFactory.cs" />
<Compile Include="ResourceRequestHandlerFactoryItem.cs" />
<Compile Include="Enums\AlphaType.cs" />
<Compile Include="Enums\ChannelLayout.cs" />
<Compile Include="Enums\PointerType.cs" />
<Compile Include="Enums\TextInputMode.cs" />
<Compile Include="Enums\TouchEventType.cs" />
Expand All @@ -146,6 +147,7 @@
<Compile Include="Handler\ICookieAccessFilter.cs" />
<Compile Include="Handler\IResourceRequestHandler.cs" />
<Compile Include="IApp.cs" />
<Compile Include="Handler\IAudioHandler.cs" />
<Compile Include="IExtension.cs" />
<Compile Include="Internals\IMethodRunnerQueue.cs" />
<Compile Include="Internals\ConcurrentMethodRunnerQueue.cs" />
Expand All @@ -163,6 +165,7 @@
<Compile Include="RenderProcess\V8Exception.cs" />
<Compile Include="RequestContextExtensions.cs" />
<Compile Include="ResponseFilter\StreamResponseFilter.cs" />
<Compile Include="Structs\AudioParamaters.cs" />
<Compile Include="Structs\TouchEvent.cs" />
<Compile Include="V8Extension.cs" />
<Compile Include="CefLibraryHandle.cs" />
Expand Down
Loading

0 comments on commit 8d25b71

Please sign in to comment.