PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mod/languages/mod_managed/managed/ManagedSession.cs

https://github.com/ppanhoto/Freeswitch-mod_mp4
C# | 230 lines | 148 code | 23 blank | 59 comment | 21 complexity | 781d909f1a99bb26f90fb9c28aa07592 MD5 | raw file
  1. /*
  2. * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application - mod_managed
  3. * Copyright (C) 2008, Michael Giagnocavo <mgg@giagnocavo.net>
  4. *
  5. * Version: MPL 1.1
  6. *
  7. * The contents of this file are subject to the Mozilla Public License Version
  8. * 1.1 (the "License"); you may not use this file except in compliance with
  9. * the License. You may obtain a copy of the License at
  10. * http://www.mozilla.org/MPL/
  11. *
  12. * Software distributed under the License is distributed on an "AS IS" basis,
  13. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  14. * for the specific language governing rights and limitations under the
  15. * License.
  16. *
  17. * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application - mod_managed
  18. *
  19. * The Initial Developer of the Original Code is
  20. * Michael Giagnocavo <mgg@giagnocavo.net>
  21. * Portions created by the Initial Developer are Copyright (C)
  22. * the Initial Developer. All Rights Reserved.
  23. *
  24. * Contributor(s):
  25. *
  26. * Michael Giagnocavo <mgg@giagnocavo.net>
  27. *
  28. * ManagedSession.cs -- ManagedSession additional functions
  29. *
  30. */
  31. using System;
  32. using System.Collections.Generic;
  33. using System.Linq;
  34. using System.Text;
  35. using System.Runtime.InteropServices;
  36. namespace FreeSWITCH.Native
  37. {
  38. // switch_status_t ManagedSession::run_dtmf_callback(void *input, switch_input_type_t itype)
  39. // But, process_callback_result is used to turn a string into a switch_status_t
  40. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  41. delegate string DtmfCallback(IntPtr input, Native.switch_input_type_t itype);
  42. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  43. delegate void CdeclAction();
  44. // This callback is used for originate
  45. [UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
  46. public delegate switch_status_t switch_state_handler_t_delegate(IntPtr sessionPtr);
  47. public partial class ManagedSession
  48. {
  49. // SWITCH_DECLARE(void) InitManagedSession(ManagedSession *session, MonoObject *dtmfDelegate, MonoObject *hangupDelegate)
  50. [DllImport("mod_managed.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
  51. static extern void InitManagedSession(IntPtr sessionPtr, DtmfCallback dtmfDelegate, CdeclAction hangupDelegate);
  52. /// <summary>Initializes the native ManagedSession. Called after Originate completes successfully .</summary>
  53. internal void Initialize()
  54. {
  55. if (allocated == 0) {
  56. throw new InvalidOperationException("Cannot initialize a ManagedSession until it is allocated (originated successfully).");
  57. }
  58. // P/Invoke generated function pointers stick around until the delegate is collected
  59. // By sticking the delegates in fields, their lifetime won't be less than the session
  60. // So we don't need to worry about GCHandles and all that....
  61. // Info here: http://blogs.msdn.com/cbrumme/archive/2003/05/06/51385.aspx
  62. this._inputCallbackRef = inputCallback;
  63. this._hangupCallbackRef = hangupCallback;
  64. InitManagedSession(ManagedSession.getCPtr(this).Handle, this._inputCallbackRef, this._hangupCallbackRef);
  65. this._variables = new ChannelVariables(this);
  66. }
  67. DtmfCallback _inputCallbackRef;
  68. CdeclAction _hangupCallbackRef;
  69. /// <summary>Function to execute when this session hangs up.</summary>
  70. public Action HangupFunction { get; set; }
  71. void hangupCallback()
  72. {
  73. Log.WriteLine(LogLevel.Debug, "AppFunction is in hangupCallback.");
  74. try {
  75. var f = HangupFunction;
  76. if (f != null) f();
  77. }
  78. catch (Exception ex) {
  79. Log.WriteLine(LogLevel.Warning, "Exception in hangupCallback: {0}", ex.ToString());
  80. }
  81. }
  82. public Func<Char, TimeSpan, string> DtmfReceivedFunction { get; set; }
  83. public Func<Native.Event, string> EventReceivedFunction { get; set; }
  84. string inputCallback(IntPtr input, Native.switch_input_type_t inputType)
  85. {
  86. try {
  87. switch (inputType) {
  88. case FreeSWITCH.Native.switch_input_type_t.SWITCH_INPUT_TYPE_DTMF:
  89. using (var dtmf = new Native.switch_dtmf_t(input, false)) {
  90. return dtmfCallback(dtmf);
  91. }
  92. case FreeSWITCH.Native.switch_input_type_t.SWITCH_INPUT_TYPE_EVENT:
  93. using (var swevt = new Native.switch_event(input, false)) {
  94. return eventCallback(swevt);
  95. }
  96. default:
  97. return "";
  98. }
  99. } catch (Exception ex) {
  100. Log.WriteLine(LogLevel.Error, "InputCallback threw exception: " + ex.ToString());
  101. return "-ERR InputCallback Exception: " + ex.Message;
  102. }
  103. }
  104. string dtmfCallback(Native.switch_dtmf_t dtmf) {
  105. var f = DtmfReceivedFunction;
  106. return f == null ? ""
  107. : f(((char)(byte)dtmf.digit), TimeSpan.FromMilliseconds(dtmf.duration));
  108. }
  109. string eventCallback(Native.switch_event swevt) {
  110. using (var evt = new FreeSWITCH.Native.Event(swevt, 0)) {
  111. var f = EventReceivedFunction;
  112. return f == null ? ""
  113. : f(evt);
  114. }
  115. }
  116. [Obsolete("Use static Originate method.", false)]
  117. public bool Originate(CoreSession aLegSession, string destination, TimeSpan timeout) {
  118. var res = 0 == this.originate(aLegSession, destination, (int)timeout.TotalMilliseconds, null);
  119. if (res) {
  120. this.Initialize();
  121. }
  122. return res;
  123. }
  124. // Creating these function pointers is a two-stage process.
  125. // The delegate needs to be stored so it doesn't get GC'd, so we can't just return GetFunctionPointerForDelegate right away.
  126. /// <summary>Wraps a nice handler into a delegate suitable for reverse P/Invoke. This only currently works well for hangup/reporting handlers.</summary>
  127. public static switch_state_handler_t_delegate CreateStateHandlerDelegate(Action<ManagedSession> handler) {
  128. // We create a ManagedSession on top of the session so callbacks can use it "nicely"
  129. // Then we sort of dispose it.
  130. switch_state_handler_t_delegate del = ptr => {
  131. using (var sess = new ManagedSession(new SWIGTYPE_p_switch_core_session(ptr, false))) {
  132. handler(sess);
  133. return switch_status_t.SWITCH_STATUS_SUCCESS;
  134. }
  135. };
  136. return del;
  137. }
  138. public static SWIGTYPE_p_f_p_switch_core_session__switch_status_t WrapStateHandlerDelegate(switch_state_handler_t_delegate del) {
  139. return new SWIGTYPE_p_f_p_switch_core_session__switch_status_t(Marshal.GetFunctionPointerForDelegate(del), false);
  140. }
  141. // These are needed on the ManagedSession bleg, so they don't get GC'd
  142. // while the B Leg is still around
  143. switch_state_handler_t_delegate originate_onhangup_delegate;
  144. switch_state_handler_t_delegate originate_ondestroy_delegate;
  145. switch_state_handler_table originate_table;
  146. GCHandle originate_keepalive_handle; // Make sure the B Leg is not collected and disposed until we run ondestroy
  147. switch_status_t originate_ondestroy_method(IntPtr channelPtr) {
  148. // CS_DESTROY lets the bleg be collected
  149. // and frees originate_table memory
  150. // Note that this (bleg ManagedSession) is invalid
  151. // to touch right now - the unmanaged memory has already been free'd
  152. if (this.originate_keepalive_handle.IsAllocated) {
  153. this.originate_keepalive_handle.Free(); // GC can now collect this bleg
  154. }
  155. if (this.originate_table != null) {
  156. this.originate_table.Dispose();
  157. this.originate_table = null;
  158. }
  159. return switch_status_t.SWITCH_STATUS_SUCCESS;
  160. }
  161. /// <summary>
  162. /// Performs originate. Returns ManagedSession on success, null on failure.
  163. /// onHangup is called as a state handler, after the channel is truly hungup (CS_REPORTING).
  164. /// </summary>
  165. public static ManagedSession OriginateHandleHangup(CoreSession aLegSession, string destination, TimeSpan timeout, Action<ManagedSession> onHangup) {
  166. var bleg = new ManagedSession();
  167. bleg.originate_ondestroy_delegate = bleg.originate_ondestroy_method;
  168. bleg.originate_onhangup_delegate = CreateStateHandlerDelegate(sess_b => {
  169. if (onHangup != null) {
  170. onHangup(sess_b);
  171. }
  172. });
  173. bleg.originate_table = new switch_state_handler_table();
  174. bleg.originate_table.on_reporting = WrapStateHandlerDelegate(bleg.originate_onhangup_delegate);
  175. bleg.originate_table.on_destroy = WrapStateHandlerDelegate(bleg.originate_ondestroy_delegate);
  176. bleg.originate_table.flags = (int)switch_state_handler_flag_t.SSH_FLAG_STICKY;
  177. var res = 0 == bleg.originate(aLegSession, destination, (int)timeout.TotalSeconds, bleg.originate_table);
  178. bleg.originate_keepalive_handle = GCHandle.Alloc(bleg, GCHandleType.Normal); // Prevent GC from eating the bleg
  179. if (res) {
  180. bleg.Initialize();
  181. return bleg;
  182. } else {
  183. // Dispose to free the lock
  184. // The bleg lives on with its unmanaged memory freed
  185. // Until CS_DESTROY gets called
  186. bleg.Dispose();
  187. return null;
  188. }
  189. }
  190. // Convenience
  191. public bool IsAvailable {
  192. get { return this.Ready(); }
  193. }
  194. public Guid Uuid {
  195. get {
  196. if (allocated == 0) throw new InvalidOperationException("Session has not been initialized.");
  197. return new Guid(this.GetUuid());
  198. }
  199. }
  200. public switch_call_cause_t CallCause {
  201. get {
  202. if (allocated == 0) throw new InvalidOperationException("Session has not been initialized.");
  203. return freeswitch.switch_channel_get_cause(this.channel);
  204. }
  205. }
  206. }
  207. }