// Copyright © 2021 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.Handler
{
///
/// Inherit from this class to handle audio events
/// All methods will be called on the CEF UI thread
///
public class AudioHandler : IAudioHandler
{
private bool isDisposed;
///
/// Gets a value indicating this instance
/// has been disposed.
///
public bool IsDisposed
{
get { return isDisposed; }
}
///
bool IAudioHandler.GetAudioParameters(IWebBrowser chromiumWebBrowser, IBrowser browser, ref AudioParameters parameters)
{
return GetAudioParameters(chromiumWebBrowser, browser, ref parameters);
}
///
/// 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
protected virtual bool GetAudioParameters(IWebBrowser chromiumWebBrowser, IBrowser browser, ref AudioParameters parameters)
{
return false;
}
///
void IAudioHandler.OnAudioStreamStarted(IWebBrowser chromiumWebBrowser,
IBrowser browser,
AudioParameters parameters,
int channels)
{
OnAudioStreamStarted(chromiumWebBrowser, browser, parameters, channels);
}
///
/// Called on a browser audio capture thread when the browser starts streaming audio.
/// OnAudioStreamStopped 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
protected virtual void OnAudioStreamStarted(IWebBrowser chromiumWebBrowser,
IBrowser browser,
AudioParameters parameters,
int channels)
{
}
///
void IAudioHandler.OnAudioStreamPacket(IWebBrowser chromiumWebBrowser, IBrowser browser, IntPtr data, int noOfFrames, long pts)
{
OnAudioStreamPacket(chromiumWebBrowser, browser, data, noOfFrames, pts);
}
///
/// 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.
///
///
/// the browser object
/// 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
protected virtual void OnAudioStreamPacket(IWebBrowser chromiumWebBrowser, IBrowser browser, IntPtr data, int noOfFrames, long pts)
{
}
///
void IAudioHandler.OnAudioStreamStopped(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
OnAudioStreamStopped(chromiumWebBrowser, browser);
}
///
/// Called on the CEF UI thread when the stream has stopped. OnAudioStreamStopped will always be called after ;
/// both methods may be called multiple times for the same stream.
///
/// the ChromiumWebBrowser control
/// the browser object
protected virtual void OnAudioStreamStopped(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
}
///
void IAudioHandler.OnAudioStreamError(IWebBrowser chromiumWebBrowser, IBrowser browser, string errorMessage)
{
OnAudioStreamError(chromiumWebBrowser, browser, errorMessage);
}
///
/// 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
protected virtual void OnAudioStreamError(IWebBrowser chromiumWebBrowser, IBrowser browser, string errorMessage)
{
}
///
/// Releases unmanaged and managed resources
///
/// to release both managed and unmanaged resources; to release only unmanaged resources.
protected virtual void Dispose(bool disposing)
{
isDisposed = true;
}
///
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}