{"id":149,"date":"2020-08-02T21:44:24","date_gmt":"2020-08-02T18:14:24","guid":{"rendered":"https:\/\/iapi.ir\/?p=149"},"modified":"2020-08-02T21:44:25","modified_gmt":"2020-08-02T18:14:25","slug":"callhandler","status":"publish","type":"post","link":"https:\/\/iapi.ir\/index.php\/2020\/08\/02\/callhandler\/","title":{"rendered":"CallHandler"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>using System;\nusing Ozeki.Media.MediaHandlers;\nusing Ozeki.VoIP;\nusing Ozeki.VoIP.SDK;\n\n\nnamespace ozeki.voip.sip.client\n{\n    \/\/These classes is just an example how to use OZEKI VoIP SIP SDK, what dll is added to the References in Solution Explorer\n    \/\/Feel free to use the other components of this SDK\n    \/\/For More information please visit : http:\/\/www.voip-sip-sdk.com and http:\/\/www.ozekiphone.com\/ozeki-voip-sip-client-997.html\n    class CallHandlerSample\n    {\n        private ISoftPhone softPhone;\n        private IPhoneLine phoneLine;\n        private RegState phoneLineInformation;\n        private IPhoneCall call;\n        private readonly Microphone microphone;\n        private readonly Speaker speaker;\n        private readonly MediaConnector connector;\n        private readonly PhoneCallAudioSender mediaSender;\n        private readonly PhoneCallAudioReceiver mediaReceiver;\n\n        \/\/\/ &lt;summary>\n        \/\/\/ Event triggered when the registered softphone has called\n        \/\/\/ &lt;\/summary>\n        public event EventHandler&lt;VoIPEventArgs&lt;IPhoneCall>> IncomingCallReceived;\n\n        \/\/\/ &lt;summary>\n        \/\/\/ Event the softphone has successfully registered\n        \/\/\/ &lt;\/summary>\n        public event EventHandler RegistrationSucceded;\n\n        \/\/\/ &lt;summary>\n        \/\/\/ Handler of making call and receiving call\n        \/\/\/ &lt;\/summary>\n        \/\/\/ &lt;param name=\"registerName\">The SIP ID what will registered into your PBX&lt;\/param>\n        \/\/\/ &lt;param name=\"domainHost\">The address of your PBX&lt;\/param>\n        public CallHandlerSample(string registerName, string domainHost)\n        {\n            microphone = Microphone.GetDefaultDevice();\n            speaker = Speaker.GetDefaultDevice();\n            connector = new MediaConnector();\n            mediaSender = new PhoneCallAudioSender();\n            mediaReceiver = new PhoneCallAudioReceiver();\n\n            InitializeSoftPhone(registerName, domainHost);\n        }\n\n\n        \/\/\/ &lt;summary>\n        \/\/\/It initializes a softphone object with a SIP PBX, and it is for requisiting a SIP account that is nedded for a SIP PBX service. It registers this SIP\n        \/\/\/account to the SIP PBX through an \u2019IphoneLine\u2019 object which represents the telephoneline. \n        \/\/\/If the telephone registration is successful we have a call ready softphone. In this example the softphone can be reached by dialing the registername.\n        \/\/\/ &lt;\/summary>\n        \/\/\/ &lt;param name=\"registerName\">The SIP ID what will registered into your PBX&lt;\/param>\n        \/\/\/ &lt;param name=\"domainHost\">The address of your PBX&lt;\/param>\n        private void InitializeSoftPhone(string registerName, string domainHost)\n        {\n            try\n            {\n                softPhone = SoftPhoneFactory.CreateSoftPhone(5700, 5800);\n                softPhone.IncomingCall += softPhone_IncomingCall;\n                phoneLine = softPhone.CreatePhoneLine(new SIPAccount(true, registerName, registerName, registerName, registerName, domainHost, 5060));\n                phoneLine.RegistrationStateChanged += phoneLine_PhoneLineInformation;\n\n                softPhone.RegisterPhoneLine(phoneLine);\n            }\n            catch (Exception ex)\n            {\n                Console.WriteLine(\"You didn't give your local IP adress, so the program won't run properly.\\n {0}\", ex.Message);\n            }\n        }\n\n        \/\/\/ &lt;summary>\n        \/\/\/ Create and start the call to the dialed number\n        \/\/\/ &lt;\/summary>\n        \/\/\/ &lt;param name=\"dialedNumber\">&lt;\/param>\n        public void Call(string dialedNumber)\n        {\n            if (phoneLineInformation != RegState.RegistrationSucceeded)\n            {\n                Console.WriteLine(\"Phone line state is not valid!\");\n                return;\n            }\n\n            if (string.IsNullOrEmpty(dialedNumber))\n                return;\n\n            if (call != null)\n                return;\n\n            call = softPhone.CreateCallObject(phoneLine, dialedNumber);\n            WireUpCallEvents();\n            call.Start();\n        }\n\n        \/\/\/ &lt;summary>\n        \/\/\/ Occurs when phone line state has changed.\n        \/\/\/ &lt;\/summary>\n        \/\/\/ &lt;param name=\"sender\">&lt;\/param>\n        \/\/\/ &lt;param name=\"e\">&lt;\/param>\n        private void phoneLine_PhoneLineInformation(object sender, RegistrationStateChangedArgs e)\n        {\n            phoneLineInformation = e.State;\n            Console.WriteLine(\"Register name:\" + ((IPhoneLine)sender).SIPAccount.RegisterName);\n\n            if (e.State == RegState.RegistrationSucceeded)\n            {\n                Console.WriteLine(\"Registration succeeded. Online.\");\n                OnRegistrationSucceded();\n            }\n            else\n            {\n                Console.WriteLine(\"Current state:\" + e.State);\n            }\n        }\n\n        \/\/\/ &lt;summary>\n        \/\/\/ Occurs when an incoming call request has received.\n        \/\/\/ &lt;\/summary>\n        \/\/\/ &lt;param name=\"sender\">&lt;\/param>\n        \/\/\/ &lt;param name=\"e\">&lt;\/param>\n        private void softPhone_IncomingCall(object sender, VoIPEventArgs&lt;IPhoneCall> e)\n        {\n            Console.WriteLine(\"Incoming call from {0}\", e.Item.DialInfo);\n            call = e.Item;\n            WireUpCallEvents();\n            OnIncomingCallReceived(e.Item);\n        }\n\n        \/\/\/ &lt;summary>\n        \/\/\/ Occurs when the phone call state has changed.\n        \/\/\/ &lt;\/summary>\n        \/\/\/ &lt;param name=\"sender\">&lt;\/param>\n        \/\/\/ &lt;param name=\"e\">&lt;\/param>\n        private void call_CallStateChanged(object sender, CallStateChangedArgs e)\n        {\n            Console.WriteLine(\"Call state changed: \" + e.State);\n\n            switch (e.State)\n            {\n                case CallState.InCall:\n                    ConnectDevicesToCall();\n                    break;\n                case CallState.Completed:\n                    DisconnectDevicesFromCall();\n                    WireDownCallEvents();\n                    call = null;\n                    break;\n                case CallState.Cancelled:\n                    WireDownCallEvents();\n                    call = null;\n                    break;\n            }\n        }\n\n        private void OnRegistrationSucceded()\n        {\n            var handler = RegistrationSucceded;\n\n            if (handler != null)\n                handler(this, EventArgs.Empty);\n        }\n\n        private void OnIncomingCallReceived(IPhoneCall item)\n        {\n            var handler = IncomingCallReceived;\n\n            if (handler != null)\n                handler(this, new VoIPEventArgs&lt;IPhoneCall>(item));\n        }\n\n        \/\/\/ &lt;summary>\n        \/\/\/ Connecting the microphone and speaker to the call\n        \/\/\/ &lt;\/summary>\n        private void ConnectDevicesToCall()\n        {\n            if (microphone != null)\n                microphone.Start();\n            connector.Connect(microphone, mediaSender);\n\n            if (speaker != null)\n                speaker.Start();\n            connector.Connect(mediaReceiver, speaker);\n\n            mediaSender.AttachToCall(call);\n            mediaReceiver.AttachToCall(call);\n        }\n\n        \/\/\/ &lt;summary>\n        \/\/\/ Disconnecting the microphone and speaker from the call\n        \/\/\/ &lt;\/summary>\n        private void DisconnectDevicesFromCall()\n        {\n            if (microphone != null)\n                microphone.Stop();\n            connector.Disconnect(microphone, mediaSender);\n\n            if (speaker != null)\n                speaker.Stop();\n            connector.Disconnect(mediaReceiver, speaker);\n\n            mediaSender.Detach();\n            mediaReceiver.Detach();\n        }\n\n        \/\/\/ &lt;summary>\n        \/\/\/  It signs up to the necessary events of a call transact.\n        \/\/\/ &lt;\/summary>\n        private void WireUpCallEvents()\n        {\n            if (call != null)\n            {\n                call.CallStateChanged += ( call_CallStateChanged );\n            }\n        }\n\n        \/\/\/ &lt;summary>\n        \/\/\/ It signs down from the necessary events of a call transact.\n        \/\/\/ &lt;\/summary>\n        private void WireDownCallEvents()\n        {\n            if (call != null)\n            {\n                call.CallStateChanged -= (call_CallStateChanged);\n            }\n        }\n\n        ~CallHandlerSample()\n        {\n            if (softPhone != null)\n                softPhone.Close();\n            WireDownCallEvents();\n        }\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-149","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>CallHandler - IAPI.IR<\/title>\n<meta name=\"robots\" content=\"index, follow\" \/>\n<meta name=\"googlebot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta name=\"bingbot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/iapi.ir\/index.php\/2020\/08\/02\/callhandler\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"CallHandler - IAPI.IR\" \/>\n<meta property=\"og:url\" content=\"https:\/\/iapi.ir\/index.php\/2020\/08\/02\/callhandler\/\" \/>\n<meta property=\"og:site_name\" content=\"IAPI.IR\" \/>\n<meta property=\"article:published_time\" content=\"2020-08-02T18:14:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-08-02T18:14:25+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/iapi.ir\/#website\",\"url\":\"https:\/\/iapi.ir\/\",\"name\":\"IAPI.IR\",\"description\":\"\\u0622\\u0645\\u0648\\u0632\\u0634 \\u0647\\u0627\\u06cc \\u0628\\u0631\\u0646\\u0627\\u0645\\u0647 \\u0646\\u0648\\u06cc\\u0633\\u06cc \\u0648 \\u0627\\u0644\\u06a9\\u062a\\u0631\\u0648\\u0646\\u06cc\\u06a9 \\u0648 \\u0627\\u0644\\u06a9\\u062a\\u0631\\u0648\\u0645\\u06a9\\u0627\\u0646\\u06cc\\u06a9\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/iapi.ir\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/iapi.ir\/index.php\/2020\/08\/02\/callhandler\/#webpage\",\"url\":\"https:\/\/iapi.ir\/index.php\/2020\/08\/02\/callhandler\/\",\"name\":\"CallHandler - IAPI.IR\",\"isPartOf\":{\"@id\":\"https:\/\/iapi.ir\/#website\"},\"datePublished\":\"2020-08-02T18:14:24+00:00\",\"dateModified\":\"2020-08-02T18:14:25+00:00\",\"author\":{\"@id\":\"https:\/\/iapi.ir\/#\/schema\/person\/5757676943db35b416789ba141d9f76a\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/iapi.ir\/index.php\/2020\/08\/02\/callhandler\/\"]}]},{\"@type\":[\"Person\"],\"@id\":\"https:\/\/iapi.ir\/#\/schema\/person\/5757676943db35b416789ba141d9f76a\",\"name\":\"jPFldBRmCc\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/iapi.ir\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/f9a5bbf8f5b1847b1faba943c87797fb01dbb36a584debe6aa7c8fb65ef9eeb9?s=96&d=mm&r=g\",\"caption\":\"jPFldBRmCc\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/iapi.ir\/index.php\/wp-json\/wp\/v2\/posts\/149","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iapi.ir\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iapi.ir\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iapi.ir\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iapi.ir\/index.php\/wp-json\/wp\/v2\/comments?post=149"}],"version-history":[{"count":1,"href":"https:\/\/iapi.ir\/index.php\/wp-json\/wp\/v2\/posts\/149\/revisions"}],"predecessor-version":[{"id":150,"href":"https:\/\/iapi.ir\/index.php\/wp-json\/wp\/v2\/posts\/149\/revisions\/150"}],"wp:attachment":[{"href":"https:\/\/iapi.ir\/index.php\/wp-json\/wp\/v2\/media?parent=149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iapi.ir\/index.php\/wp-json\/wp\/v2\/categories?post=149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iapi.ir\/index.php\/wp-json\/wp\/v2\/tags?post=149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}