CefAudioHandler.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. namespace Xilium.CefGlue
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. using Xilium.CefGlue.Interop;
  8. /// <summary>
  9. /// Implement this interface to handle audio events
  10. /// All methods will be called on the UI thread
  11. /// </summary>
  12. public abstract unsafe partial class CefAudioHandler
  13. {
  14. private int get_audio_parameters(cef_audio_handler_t* self, cef_browser_t* browser, cef_audio_parameters_t* @params)
  15. {
  16. CheckSelf(self);
  17. var mBrowser = CefBrowser.FromNative(browser);
  18. var mResult = GetAudioParameters(mBrowser, new CefAudioParameters(@params));
  19. return mResult ? 1 : 0;
  20. }
  21. /// <summary>
  22. /// Called on the UI thread to allow configuration of audio stream parameters.
  23. /// Return true to proceed with audio stream capture, or false to cancel it.
  24. /// All members of |params| can optionally be configured here, but they are
  25. /// also pre-filled with some sensible defaults.
  26. /// </summary>
  27. protected abstract bool GetAudioParameters(CefBrowser browser, CefAudioParameters parameters);
  28. private void on_audio_stream_started(cef_audio_handler_t* self, cef_browser_t* browser, cef_audio_parameters_t* @params, int channels)
  29. {
  30. CheckSelf(self);
  31. var mBrowser = CefBrowser.FromNative(browser);
  32. OnAudioStreamStarted(mBrowser, new CefAudioParameters(@params), channels);
  33. }
  34. /// <summary>
  35. /// Called on a browser audio capture thread when the browser starts
  36. /// streaming audio. OnAudioSteamStopped will always be called after
  37. /// OnAudioStreamStarted; both methods may be called multiple times
  38. /// for the same browser. |params| contains the audio parameters like
  39. /// sample rate and channel layout. |channels| is the number of channels.
  40. /// </summary>
  41. protected abstract void OnAudioStreamStarted(CefBrowser browser, in CefAudioParameters parameters, int channels);
  42. private void on_audio_stream_packet(cef_audio_handler_t* self, cef_browser_t* browser, float** data, int frames, long pts)
  43. {
  44. CheckSelf(self);
  45. var mBrowser = CefBrowser.FromNative(browser);
  46. OnAudioStreamPacket(mBrowser, (IntPtr)data, frames, pts);
  47. }
  48. /// <summary>
  49. /// Called on the audio stream thread when a PCM packet is received for the
  50. /// stream. |data| is an array representing the raw PCM data as a floating
  51. /// point type, i.e. 4-byte value(s). |frames| is the number of frames in the
  52. /// PCM packet. |pts| is the presentation timestamp (in milliseconds since the
  53. /// Unix Epoch) and represents the time at which the decompressed packet should
  54. /// be presented to the user. Based on |frames| and the |channel_layout| value
  55. /// passed to OnAudioStreamStarted you can calculate the size of the |data|
  56. /// array in bytes.
  57. ///
  58. /// |data| is |float**|, readonly!
  59. /// </summary>
  60. protected abstract void OnAudioStreamPacket(CefBrowser browser, IntPtr data, int frames, long pts);
  61. private void on_audio_stream_stopped(cef_audio_handler_t* self, cef_browser_t* browser)
  62. {
  63. CheckSelf(self);
  64. var mBrowser = CefBrowser.FromNative(browser);
  65. OnAudioStreamStopped(mBrowser);
  66. }
  67. /// <summary>
  68. /// Called on the UI thread when the stream has stopped. OnAudioSteamStopped
  69. /// will always be called after OnAudioStreamStarted; both methods may be
  70. /// called multiple times for the same stream.
  71. /// </summary>
  72. protected abstract void OnAudioStreamStopped(CefBrowser browser);
  73. private void on_audio_stream_error(cef_audio_handler_t* self, cef_browser_t* browser, cef_string_t* message)
  74. {
  75. CheckSelf(self);
  76. var mBrowser = CefBrowser.FromNative(browser);
  77. var mMessage = cef_string_t.ToString(message);
  78. OnAudioStreamError(mBrowser, mMessage);
  79. }
  80. /// <summary>
  81. /// Called on the UI or audio stream thread when an error occurred. During the
  82. /// stream creation phase this callback will be called on the UI thread while
  83. /// in the capturing phase it will be called on the audio stream thread. The
  84. /// stream will be stopped immediately.
  85. /// </summary>
  86. protected abstract void OnAudioStreamError(CefBrowser browser, string message);
  87. }
  88. }