// %BANNER_BEGIN%
// ---------------------------------------------------------------------
// %COPYRIGHT_BEGIN%
// Copyright (c) (2018-2022) Magic Leap, Inc. All Rights Reserved.
// Use of this file is governed by the Software License Agreement, located here: https://www.magicleap.com/software-license-agreement-ml2
// Terms and conditions applicable to third-party materials accompanying this distribution may also be found in the top-level NOTICE file appearing herein.
// %COPYRIGHT_END%
// ---------------------------------------------------------------------
// %BANNER_END%
namespace UnityEngine.XR.MagicLeap
{
using System;
using System.Runtime.InteropServices;
using UnityEngine.XR.MagicLeap.Native;
///
/// MLWebRTC class contains the API to interface with the
/// WebRTC C API.
///
public partial class MLWebRTC
{
///
/// Class that represents a source used by the MLWebRTC API.
///
public partial class Source
{
///
/// Native bindings for the MLWebRTC.Source struct.
///
internal class NativeBindings : MagicLeapNativeBindings
{
///
/// Creates the local source that links to the user's MLCamera.
///
/// The handle to the local source to return to the caller.
///
/// MLResult.Result will be MLResult.Code.Ok if the local source was successfully created.
/// MLResult.Result will be MLResult.Code.PermissionDenied if necessary permission is missing.
/// MLResult.Result will be MLResult.Code.UnspecifiedFailure if failed due to other internal error.
///
[DllImport(MLWebRTCDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern MLResult.Code MLWebRTCSourceCreateLocalSourceForCamera(in MLCamera.NativeBindings.MLCameraConnectContext inputContext, out ulong sourceHandle);
///
/// Creates the local source with the specified track name that links to the user's microphone.
///
/// Track name
/// The handle to the local source to return to the caller.
///
/// MLResult.Result will be MLResult.Code.Ok if the local source was successfully created.
/// MLResult.Result will be MLResult.Code.PermissionDenied if necessary permission is missing.
/// MLResult.Result will be MLResult.Code.UnspecifiedFailure if failed due to other internal error.
///
[DllImport(MLWebRTCDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern MLResult.Code MLWebRTCSourceCreateLocalSourceForMicrophoneEx([MarshalAs(UnmanagedType.LPStr)] string trackName, out ulong sourceHandle);
///
/// Creates the local source with the specified parameters.
///
/// Audio source parameters
/// The handle to the local source to return to the caller.
///
/// MLResult.Result will be MLResult.Code.Ok if the local source was successfully created.
/// MLResult.Result will be MLResult.Code.PermissionDenied if necessary permission is missing.
/// MLResult.Result will be MLResult.Code.UnspecifiedFailure if failed due to other internal error.
///
[DllImport(MLWebRTCDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern MLResult.Code MLWebRTCSourceCreateAppDefinedAudioSourceEx(ref MLWebRTCAppDefinedSourceParams sourceParams, out ulong sourceHandle);
///
/// Checks if an audio source is currently enabled.
///
/// The handle of the source.
/// True if source is enabled.
///
/// MLResult.Result will be MLResult.Code.Ok if the audio source status was queried successfully.
/// MLResult.Result will be MLResult.Code.PermissionDenied if necessary permission is missing.
/// MLResult.Result will be MLResult.Code.UnspecifiedFailure if failed due to other internal error.
///
[DllImport(MLWebRTCDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern MLResult.Code MLWebRTCSourceIsEnabled(ulong sourceHandle, [MarshalAs(UnmanagedType.I1)] out bool enabled);
///
/// Enables or disables a audio source.
///
/// The handle of the audio source.
/// Sets the audio source to be enabled or disabled.
///
/// MLResult.Result will be MLResult.Code.Ok if the audio source was enabled/disabled successfully.
/// MLResult.Result will be MLResult.Code.PermissionDenied if necessary permission is missing.
/// MLResult.Result will be MLResult.Code.UnspecifiedFailure if failed due to other internal error.
///
[DllImport(MLWebRTCDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern MLResult.Code MLWebRTCSourceSetEnabled(ulong sourceHandle, [MarshalAs(UnmanagedType.I1)] bool enabled);
///
/// Checks if an video source is currently enabled.
///
/// The handle of the video source.
/// Type of the source.
///
/// MLResult.Result will be MLResult.Code.Ok if the video source status was queried successfully.
/// MLResult.Result will be MLResult.Code.PermissionDenied if necessary permission is missing.
/// MLResult.Result will be MLResult.Code.UnspecifiedFailure if failed due to other internal error.
///
[DllImport(MLWebRTCDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern MLResult.Code MLWebRTCSourceGetType(ulong sourceHandle, out MLWebRTC.MediaStream.Track.Type sourceType);
///
/// Gets the track Id of a source, call MLWebRTCSourceReleaseTrackId after.
///
/// The handle of the media source.
/// Double-pointer to the unmanaged trackId string.
///
/// MLResult.Result will be MLResult.Code.Ok if destroying all handles was successful.
/// MLResult.Result will be MLResult.Code.MismatchingHandle if an incorrect handle was sent.
/// MLResult.Result will be MLResult.Code.UnspecifiedFailure if failed due to other internal error.
///
[DllImport(MLWebRTCDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern MLResult.Code MLWebRTCSourceGetTrackId(ulong sourceHandle, out IntPtr trackIdPtr);
///
/// Releases the memory created when calling MLWebRTCSourceGetTrackId.
///
/// The handle of the data channel.
/// Pointer to the unmanaged trackId string.
///
/// MLResult.Result will be MLResult.Code.Ok if destroying all handles was successful.
/// MLResult.Result will be MLResult.Code.MismatchingHandle if an incorrect handle was sent.
/// MLResult.Result will be MLResult.Code.UnspecifiedFailure if failed due to other internal error.
///
[DllImport(MLWebRTCDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern MLResult.Code MLWebRTCSourceReleaseTrackId(ulong sourceHandle, IntPtr trackId);
///
/// Destroys the local source.
///
/// The handle to the local source to destroy.
///
/// MLResult.Result will be MLResult.Code.Ok if the local source was successfully destroyed.
/// MLResult.Result will be MLResult.Code.PermissionDenied if necessary permission is missing.
/// MLResult.Result will be MLResult.Code.UnspecifiedFailure if failed due to other internal error.
///
[DllImport(MLWebRTCDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern MLResult.Code MLWebRTCSourceDestroy(ulong sourceHandle);
///
/// The native representation of an MLWebRTC source.
///
[StructLayout(LayoutKind.Sequential)]
public struct MLWebRTCSource
{
///
/// Version of the struct.
///
public uint Version;
///
/// Type of the struct.
///
public MLWebRTC.MediaStream.Track.Type Type;
///
/// Handle of the struct.
///
public ulong Handle;
///
/// Gets an MLWebRTC.Source object from the data of this object.
///
public MLWebRTC.MediaStream.Track Data
{
get
{
string trackId = string.Empty;
MLResult.Code resultCode = NativeBindings.MLWebRTCSourceGetTrackId(this.Handle, out IntPtr trackIdPtr);
if (MLResult.DidNativeCallSucceed(resultCode, nameof(NativeBindings.MLWebRTCSourceGetTrackId)))
{
if (trackIdPtr != IntPtr.Zero)
{
trackId = Marshal.PtrToStringAnsi(trackIdPtr);
resultCode = NativeBindings.MLWebRTCSourceReleaseTrackId(this.Handle, trackIdPtr);
MLResult.DidNativeCallSucceed(resultCode, nameof(NativeBindings.MLWebRTCSourceReleaseTrackId));
}
}
MLWebRTC.MediaStream.Track track = new MLWebRTC.MediaStream.Track(trackId)
{
Handle = this.Handle,
TrackType = this.Type
};
return track;
}
}
///
/// Creates and returns an initialized version of this struct from a native MLWebRTCSource object.
///
/// An initialized version of this struct.
public static MLWebRTCSource Create()
{
MLWebRTCSource source = new MLWebRTCSource();
source.Version = 1;
source.Handle = MagicLeapNativeBindings.InvalidHandle;
return source;
}
}
[StructLayout(LayoutKind.Sequential)]
public struct MLWebRTCAppDefinedSourceParams
{
public uint Version;
public IntPtr Callbacks;
[MarshalAs(UnmanagedType.LPStr)]
public string TrackName;
public static MLWebRTCAppDefinedSourceParams Create(string trackName)
{
MLWebRTCAppDefinedSourceParams sourceParams = new MLWebRTCAppDefinedSourceParams();
sourceParams.Version = 1;
sourceParams.Callbacks = IntPtr.Zero;
sourceParams.TrackName = trackName;
return sourceParams;
}
public static MLWebRTCAppDefinedSourceParams Create(string trackName, AppDefinedSource.NativeBindings.MLWebRTCAppDefinedSourceEventCallbacks callbacks)
{
MLWebRTCAppDefinedSourceParams sourceParams = new MLWebRTCAppDefinedSourceParams();
sourceParams.Version = 1;
IntPtr callbacksPtr = Marshal.AllocHGlobal(Marshal.SizeOf(callbacks));
Marshal.StructureToPtr(callbacks, callbacksPtr, false);
sourceParams.Callbacks = callbacksPtr;
sourceParams.TrackName = trackName;
return sourceParams;
}
}
}
}
}
}