2020-07-08 08:06:16 +10:00
|
|
|
// 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 CefSharp.Enums;
|
|
|
|
|
using CefSharp.Structs;
|
2021-07-30 23:30:59 +03:00
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2020-07-08 08:06:16 +10:00
|
|
|
|
|
|
|
|
namespace CefSharp.Example.Handlers
|
|
|
|
|
{
|
2021-06-04 16:55:28 +10:00
|
|
|
public class AudioHandler : Handler.AudioHandler
|
2020-07-08 08:06:16 +10:00
|
|
|
{
|
|
|
|
|
private ChannelLayout channelLayout;
|
|
|
|
|
private int channelCount;
|
|
|
|
|
private int sampleRate;
|
2021-07-30 23:30:59 +03:00
|
|
|
private readonly FileStream rawAudioFile;
|
2020-07-08 08:06:16 +10:00
|
|
|
|
2021-07-30 23:30:59 +03:00
|
|
|
public AudioHandler(string path) : base()
|
2020-07-08 08:06:16 +10:00
|
|
|
{
|
2021-07-30 23:30:59 +03:00
|
|
|
// The output file with raw audio data (PCM, 32-bit, float) will be saved in this path
|
|
|
|
|
this.rawAudioFile = new FileStream(path, FileMode.Create, FileAccess.Write);
|
2020-07-08 08:06:16 +10:00
|
|
|
}
|
2021-07-30 23:30:59 +03:00
|
|
|
|
|
|
|
|
protected override bool GetAudioParameters(IWebBrowser chromiumWebBrowser, IBrowser browser, ref AudioParameters parameters)
|
2020-07-08 08:06:16 +10:00
|
|
|
{
|
2021-07-30 23:30:59 +03:00
|
|
|
// return true to activate audio stream capture
|
|
|
|
|
return true;
|
2021-06-04 16:55:28 +10:00
|
|
|
}
|
2020-07-08 08:06:16 +10:00
|
|
|
|
2021-06-04 16:55:28 +10:00
|
|
|
protected override void OnAudioStreamStarted(IWebBrowser chromiumWebBrowser, IBrowser browser, AudioParameters parameters, int channels)
|
|
|
|
|
{
|
|
|
|
|
this.channelLayout = parameters.ChannelLayout;
|
|
|
|
|
this.sampleRate = parameters.SampleRate;
|
|
|
|
|
this.channelCount = channels;
|
2020-07-08 08:06:16 +10:00
|
|
|
}
|
|
|
|
|
|
2021-06-04 16:55:28 +10:00
|
|
|
protected override void OnAudioStreamPacket(IWebBrowser chromiumWebBrowser, IBrowser browser, IntPtr data, int noOfFrames, long pts)
|
2020-07-08 08:06:16 +10:00
|
|
|
{
|
2021-07-30 23:30:59 +03:00
|
|
|
/*
|
|
|
|
|
* NOTE: data is an array representing the raw PCM data as a floating point type, i.e. 4-byte value(s)
|
|
|
|
|
* Based on noOfFrames and the channels value passed to IAudioHandler.OnAudioStreamStarted
|
|
|
|
|
* you can calculate the size of the data array in bytes.
|
|
|
|
|
*
|
|
|
|
|
* Audio data (PCM, 32-bit, float) will be save to rawAudioFile stream.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
unsafe
|
|
|
|
|
{
|
|
|
|
|
float** channelData = (float**)data.ToPointer();
|
|
|
|
|
int size = channelCount * noOfFrames * 4;
|
|
|
|
|
byte[] samples = new byte[size];
|
|
|
|
|
fixed (byte* pDestByte = samples)
|
|
|
|
|
{
|
|
|
|
|
float* pDest = (float*)pDestByte;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < noOfFrames; i++)
|
|
|
|
|
{
|
|
|
|
|
for (int c = 0; c < channelCount; c++)
|
|
|
|
|
{
|
|
|
|
|
*pDest++ = channelData[c][i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
rawAudioFile.Write(samples, 0, size);
|
|
|
|
|
}
|
2020-07-08 08:06:16 +10:00
|
|
|
}
|
|
|
|
|
|
2021-06-04 16:55:28 +10:00
|
|
|
protected override void OnAudioStreamStopped(IWebBrowser chromiumWebBrowser, IBrowser browser)
|
2020-07-08 08:06:16 +10:00
|
|
|
{
|
2021-06-04 16:55:28 +10:00
|
|
|
base.OnAudioStreamStopped(chromiumWebBrowser, browser);
|
2020-07-08 08:06:16 +10:00
|
|
|
}
|
|
|
|
|
|
2021-07-30 23:30:59 +03:00
|
|
|
protected override void OnAudioStreamError(IWebBrowser chromiumWebBrowser, IBrowser browser, string errorMessage)
|
|
|
|
|
{
|
|
|
|
|
base.OnAudioStreamError(chromiumWebBrowser, browser, errorMessage);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 16:55:28 +10:00
|
|
|
protected override void Dispose(bool disposing)
|
2020-07-08 08:06:16 +10:00
|
|
|
{
|
2021-07-30 23:30:59 +03:00
|
|
|
rawAudioFile.Close();
|
2021-06-04 16:55:28 +10:00
|
|
|
base.Dispose(disposing);
|
2020-07-08 08:06:16 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|