From 8d25b714904762ee6cf9edbb778dcc2f76204bfa Mon Sep 17 00:00:00 2001 From: Alex Maitland Date: Wed, 8 Jul 2020 08:06:16 +1000 Subject: [PATCH] Core - Re-Add IAudioHandler (#3153) Resolves #3152 --- CefSharp.Core/Internals/ClientAdapter.cpp | 73 +++++++ CefSharp.Core/Internals/ClientAdapter.h | 11 +- CefSharp.Example/CefSharp.Example.csproj | 1 + CefSharp.Example/Handlers/AudioHandler.cs | 47 +++++ CefSharp.OffScreen/ChromiumWebBrowser.cs | 6 +- .../BrowserTabUserControl.cs | 2 + CefSharp.WinForms/ChromiumWebBrowser.cs | 5 + .../Views/BrowserTabView.xaml.cs | 1 + CefSharp.Wpf/ChromiumWebBrowser.cs | 5 +- CefSharp/CefSharp.csproj | 3 + CefSharp/Enums/ChannelLayout.cs | 187 ++++++++++++++++++ CefSharp/Handler/IAudioHandler.cs | 73 +++++++ CefSharp/IWebBrowser.cs | 5 + .../Internals/InternalWebBrowserExtensions.cs | 1 + CefSharp/Structs/AudioParamaters.cs | 42 ++++ 15 files changed, 458 insertions(+), 4 deletions(-) create mode 100644 CefSharp.Example/Handlers/AudioHandler.cs create mode 100644 CefSharp/Enums/ChannelLayout.cs create mode 100644 CefSharp/Handler/IAudioHandler.cs create mode 100644 CefSharp/Structs/AudioParamaters.cs diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index 951dc4f07..1a3b5004a 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -992,6 +992,79 @@ namespace CefSharp } } + bool ClientAdapter::GetAudioParameters(CefRefPtr 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 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 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 browser) + { + auto handler = _browserControl->AudioHandler; + + if (handler != nullptr) + { + auto browserWrapper = GetBrowserWrapper(browser->GetIdentifier(), browser->IsPopup()); + + handler->OnAudioStreamStopped(_browserControl, browserWrapper); + } + } + + void ClientAdapter::OnAudioStreamError(CefRefPtr 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 browser, CefRefPtr frame, CefProcessId source_process, CefRefPtr message) { auto handled = false; diff --git a/CefSharp.Core/Internals/ClientAdapter.h b/CefSharp.Core/Internals/ClientAdapter.h index 72f1b0bdc..dd58981b6 100644 --- a/CefSharp.Core/Internals/ClientAdapter.h +++ b/CefSharp.Core/Internals/ClientAdapter.h @@ -32,7 +32,8 @@ namespace CefSharp public CefDialogHandler, public CefDragHandler, public CefDownloadHandler, - public CefFindHandler + public CefFindHandler, + public CefAudioHandler { private: gcroot _browserControl; @@ -94,6 +95,7 @@ namespace CefSharp virtual DECL CefRefPtr GetDialogHandler() OVERRIDE { return this; } virtual DECL CefRefPtr GetDragHandler() OVERRIDE { return this; } virtual DECL CefRefPtr GetFindHandler() OVERRIDE { return this; } + virtual DECL CefRefPtr GetAudioHandler() OVERRIDE { return this; } virtual DECL bool OnProcessMessageReceived(CefRefPtr browser, CefRefPtr frame, CefProcessId source_process, CefRefPtr message) OVERRIDE; @@ -185,6 +187,13 @@ namespace CefSharp //CefFindHandler virtual DECL void OnFindResult(CefRefPtr browser, int identifier, int count, const CefRect& selectionRect, int activeMatchOrdinal, bool finalUpdate) OVERRIDE; + //CefAudioHandler + virtual DECL bool GetAudioParameters(CefRefPtr browser, CefAudioParameters & params) OVERRIDE; + virtual DECL void OnAudioStreamStarted(CefRefPtr browser, const CefAudioParameters& params, int channels) OVERRIDE; + virtual DECL void OnAudioStreamPacket(CefRefPtr browser, const float** data, int frames, int64 pts) OVERRIDE; + virtual DECL void OnAudioStreamStopped(CefRefPtr browser) OVERRIDE; + virtual DECL void OnAudioStreamError(CefRefPtr browser, const CefString& message) OVERRIDE; + IMPLEMENT_REFCOUNTING(ClientAdapter); }; } diff --git a/CefSharp.Example/CefSharp.Example.csproj b/CefSharp.Example/CefSharp.Example.csproj index 6dcedbe0d..c55c2c1ac 100644 --- a/CefSharp.Example/CefSharp.Example.csproj +++ b/CefSharp.Example/CefSharp.Example.csproj @@ -77,6 +77,7 @@ + diff --git a/CefSharp.Example/Handlers/AudioHandler.cs b/CefSharp.Example/Handlers/AudioHandler.cs new file mode 100644 index 000000000..50b345e79 --- /dev/null +++ b/CefSharp.Example/Handlers/AudioHandler.cs @@ -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) + { + + } + } +} diff --git a/CefSharp.OffScreen/ChromiumWebBrowser.cs b/CefSharp.OffScreen/ChromiumWebBrowser.cs index e64803592..9c04b4de4 100644 --- a/CefSharp.OffScreen/ChromiumWebBrowser.cs +++ b/CefSharp.OffScreen/ChromiumWebBrowser.cs @@ -186,13 +186,15 @@ namespace CefSharp.OffScreen /// /// The find handler. public IFindHandler FindHandler { get; set; } - + /// + /// Implement to handle audio events. + /// + public IAudioHandler AudioHandler { get; set; } /// /// Implement to handle events related to accessibility. /// /// The accessibility handler. public IAccessibilityHandler AccessibilityHandler { get; set; } - /// /// 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 diff --git a/CefSharp.WinForms.Example/BrowserTabUserControl.cs b/CefSharp.WinForms.Example/BrowserTabUserControl.cs index 0574f4ff1..91bf07b93 100644 --- a/CefSharp.WinForms.Example/BrowserTabUserControl.cs +++ b/CefSharp.WinForms.Example/BrowserTabUserControl.cs @@ -39,6 +39,8 @@ namespace CefSharp.WinForms.Example browser.RequestHandler = new WinFormsRequestHandler(openNewTab); browser.JsDialogHandler = new JsDialogHandler(); browser.DownloadHandler = new DownloadHandler(); + browser.AudioHandler = new AudioHandler(); + if (multiThreadedMessageLoopEnabled) { browser.KeyboardHandler = new KeyboardHandler(); diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs index 32dc538aa..46ac719fa 100644 --- a/CefSharp.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.WinForms/ChromiumWebBrowser.cs @@ -268,6 +268,11 @@ namespace CefSharp.WinForms [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), DefaultValue(null)] public IFindHandler FindHandler { get; set; } /// + /// Implement to handle audio events. + /// + [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), DefaultValue(null)] + public IAudioHandler AudioHandler { get; set; } + /// /// The for this ChromiumWebBrowser. /// /// The focus handler. diff --git a/CefSharp.Wpf.Example/Views/BrowserTabView.xaml.cs b/CefSharp.Wpf.Example/Views/BrowserTabView.xaml.cs index 4704cdcdd..106cf47a9 100644 --- a/CefSharp.Wpf.Example/Views/BrowserTabView.xaml.cs +++ b/CefSharp.Wpf.Example/Views/BrowserTabView.xaml.cs @@ -91,6 +91,7 @@ namespace CefSharp.Wpf.Example.Views 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(); diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs index 846d6f34a..3d3ce28f8 100644 --- a/CefSharp.Wpf/ChromiumWebBrowser.cs +++ b/CefSharp.Wpf/ChromiumWebBrowser.cs @@ -290,7 +290,10 @@ namespace CefSharp.Wpf /// /// The find handler. public IFindHandler FindHandler { get; set; } - + /// + /// Implement to handle audio events. + /// + public IAudioHandler AudioHandler { get; set; } /// /// Implement to handle events related to accessibility. /// diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index e778dfa72..15fe4bc2f 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -128,6 +128,7 @@ + @@ -146,6 +147,7 @@ + @@ -163,6 +165,7 @@ + diff --git a/CefSharp/Enums/ChannelLayout.cs b/CefSharp/Enums/ChannelLayout.cs new file mode 100644 index 000000000..826b6fcbd --- /dev/null +++ b/CefSharp/Enums/ChannelLayout.cs @@ -0,0 +1,187 @@ +// 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. + +namespace CefSharp.Enums +{ + /// + /// Enumerates the various representations of the ordering of audio channels. + /// Logged to UMA, so never reuse a value, always add new/greater ones! + /// See media\base\channel_layout.h + /// + public enum ChannelLayout + { + /// + /// None + /// + LayoutNone = 0, + + /// + /// Unsupported + /// + LayoutUnsupported = 1, + + /// + /// Front C + /// + LayoutMono = 2, + + /// + /// Front L, Front R + /// + LayoutStereo = 3, + + /// + /// Front L, Front R, Back C + /// + Layout2_1 = 4, + + /// + /// Front L, Front R, Front C + /// + LayoutSurround = 5, + + /// + /// Front L, Front R, Front C, Back C + /// + Layout4_0 = 6, + + /// + /// Front L, Front R, Side L, Side R + /// + Layout2_2 = 7, + + /// + /// Front L, Front R, Back L, Back R + /// + LayoutQuad = 8, + + /// + /// Front L, Front R, Front C, Side L, Side R + /// + Layout5_0 = 9, + + /// + /// Front L, Front R, Front C, LFE, Side L, Side R + /// + Layout5_1 = 10, + + /// + /// Front L, Front R, Front C, Back L, Back R + /// + Layout5_0Back = 11, + + /// + /// Front L, Front R, Front C, LFE, Back L, Back R + /// + Layout5_1Back = 12, + + /// + /// Front L, Front R, Front C, Side L, Side R, Back L, Back R + /// + Layout7_0 = 13, + + /// + /// Front L, Front R, Front C, LFE, Side L, Side R, Back L, Back R + /// + Layout7_1 = 14, + + /// + /// Front L, Front R, Front C, LFE, Side L, Side R, Front LofC, Front RofC + /// + Layout7_1Wide = 15, + + /// + /// Stereo L, Stereo R + /// + LayoutStereoDownMix = 16, + + /// + /// Stereo L, Stereo R, LFE + /// + Layout2Point1 = 17, + + /// + /// Stereo L, Stereo R, Front C, LFE + /// + Layout3_1 = 18, + + /// + /// Stereo L, Stereo R, Front C, Rear C, LFE + /// + Layout4_1 = 19, + + /// + /// Stereo L, Stereo R, Front C, Side L, Side R, Back C + /// + Layout6_0 = 20, + + /// + /// Stereo L, Stereo R, Side L, Side R, Front LofC, Front RofC + /// + Layout6_0Front = 21, + + /// + /// Stereo L, Stereo R, Front C, Rear L, Rear R, Rear C + /// + LayoutHexagonal = 22, + + /// + /// Stereo L, Stereo R, Front C, LFE, Side L, Side R, Rear Center + /// + Layout6_1 = 23, + + /// + /// Stereo L, Stereo R, Front C, LFE, Back L, Back R, Rear Center + /// + Layout6_1Back = 24, + + /// + /// Stereo L, Stereo R, Side L, Side R, Front LofC, Front RofC, LFE + /// + Layout6_1Front = 25, + + /// + /// Front L, Front R, Front C, Side L, Side R, Front LofC, Front RofC + /// + Layout7_0Front = 26, + + /// + /// Front L, Front R, Front C, LFE, Back L, Back R, Front LofC, Front RofC + /// + Layout7_1WideBack = 27, + + /// + /// Front L, Front R, Front C, Side L, Side R, Rear L, Back R, Back C. + /// + LayoutOctagonal = 28, + + /// + /// Channels are not explicitly mapped to speakers. + /// + LayoutDiscrete = 29, + + /// + /// Front L, Front R, Front C. Front C contains the keyboard mic audio. This + /// layout is only intended for input for WebRTC. The Front C channel + /// is stripped away in the WebRTC audio input pipeline and never seen outside + /// of that. + /// + LayoutStereoKeyboardAndMic = 30, + + /// + /// Front L, Front R, Side L, Side R, LFE + /// + Layout4_1QuadSize = 31, + + /// + /// Actual channel layout is specified in the bitstream and the actual channel + /// count is unknown at Chromium media pipeline level (useful for audio + /// pass-through mode). + /// + LayoutBitstream = 32, + + // Max value, must always equal the largest entry ever logged. + LayoutMax = LayoutBitstream + } +} diff --git a/CefSharp/Handler/IAudioHandler.cs b/CefSharp/Handler/IAudioHandler.cs new file mode 100644 index 000000000..12722cecf --- /dev/null +++ b/CefSharp/Handler/IAudioHandler.cs @@ -0,0 +1,73 @@ +// 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.Structs; + +namespace CefSharp +{ + /// + /// Implement this interface to handle audio events + /// All methods will be called on the CEF UI thread + /// + public interface IAudioHandler + { + /// + /// Called on the CEF UI thread to allow configuration of audio stream parameters. + /// Audio stream paramaters can optionally be configured by modifying + /// + /// the ChromiumWebBrowser control + /// the browser object + /// audio stream parameters can optionally be configured here, they are + /// pre-filled with some sensible defaults. + /// Return true to proceed with audio stream capture, or false to cancel it + bool GetAudioParameters(IWebBrowser chromiumWebBrowser, IBrowser browser, ref AudioParameters parameters); + + /// + /// Called on a browser audio capture thread when the browser starts streaming audio. + /// OnAudioSteamStopped will always be called after OnAudioStreamStarted; both methods may be called multiple + /// times for the same browser. + /// + /// the ChromiumWebBrowser control + /// the browser object + /// contains the audio parameters like sample rate and channel layout. + /// Changing the param values will have no effect here. + /// is the number of channels + void OnAudioStreamStarted(IWebBrowser chromiumWebBrowser, + IBrowser browser, + AudioParameters parameters, + int channels); + + /// + /// Called on the audio stream thread when a PCM packet is received for the stream. + /// Based on and the value passed to + /// you can calculate the size of the array in bytes. + /// + /// + /// is an array representing the raw PCM data as a floating point type, i.e. 4-byte value(s). + /// is the number of frames in the PCM packet + /// is the presentation timestamp (in milliseconds since the Unix Epoch) + /// and represents the time at which the decompressed packet should be presented to the user + void OnAudioStreamPacket(IWebBrowser chromiumWebBrowser, IBrowser browser, IntPtr data, int noOfFrames, long pts); + + /// + /// Called on the CEF UI thread when the stream has stopped. OnAudioSteamStopped will always be called after ; + /// both methods may be called multiple times for the same stream. + /// + /// the ChromiumWebBrowser control + /// the browser object + void OnAudioStreamStopped(IWebBrowser chromiumWebBrowser, IBrowser browser); + + /// + /// Called on the CEF UI thread or audio stream thread when an error occurred. During the + /// stream creation phase this callback will be called on the UI thread while + /// in the capturing phase it will be called on the audio stream thread. The + /// stream will be stopped immediately. + /// + /// the ChromiumWebBrowser control + /// the browser object + /// error message + void OnAudioStreamError(IWebBrowser chromiumWebBrowser, IBrowser browser, string errorMessage); + } +} diff --git a/CefSharp/IWebBrowser.cs b/CefSharp/IWebBrowser.cs index 393e4ad6c..508ebdf7c 100644 --- a/CefSharp/IWebBrowser.cs +++ b/CefSharp/IWebBrowser.cs @@ -173,6 +173,11 @@ namespace CefSharp /// The find handler. IFindHandler FindHandler { get; set; } + /// + /// Implement to handle audio events. + /// + IAudioHandler AudioHandler { get; set; } + /// /// A flag that indicates whether the WebBrowser is initialized (true) or not (false). /// diff --git a/CefSharp/Internals/InternalWebBrowserExtensions.cs b/CefSharp/Internals/InternalWebBrowserExtensions.cs index e9e85930c..a4bafa589 100644 --- a/CefSharp/Internals/InternalWebBrowserExtensions.cs +++ b/CefSharp/Internals/InternalWebBrowserExtensions.cs @@ -8,6 +8,7 @@ namespace CefSharp.Internals { internal static void SetHandlersToNullExceptLifeSpan(this IWebBrowserInternal browser) { + browser.AudioHandler = null; browser.DialogHandler = null; browser.FindHandler = null; browser.RequestHandler = null; diff --git a/CefSharp/Structs/AudioParamaters.cs b/CefSharp/Structs/AudioParamaters.cs new file mode 100644 index 000000000..13cf5c710 --- /dev/null +++ b/CefSharp/Structs/AudioParamaters.cs @@ -0,0 +1,42 @@ +// Copyright © 2020 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 CefSharp.Enums; + +namespace CefSharp.Structs +{ + /// + /// Structure representing the audio parameters for setting up the audio handler. + /// + public struct AudioParameters + { + /// + /// Layout of the audio channels + /// + public ChannelLayout ChannelLayout { get; set; } + + /// + /// Sample rate + /// + public int SampleRate { get; set; } + + /// + /// Number of frames per buffer + /// + public int FramesPerBuffer { get; set; } + + /// + /// Init with default values + /// + /// channel layout + /// sample rate + /// frames per buffer + public AudioParameters(ChannelLayout channelLayout, int sampleRate, int framesPerBuffer) + { + ChannelLayout = channelLayout; + SampleRate = sampleRate; + FramesPerBuffer = framesPerBuffer; + } + } +}