# What is the proper order of operations for starting and ending WebRTC sessions?

**URL:** <https://forum.magicleap.cloud/t/what-is-the-proper-order-of-operations-for-starting-and-ending-webrtc-sessions/1028>\
**Category:** WebRTC\
**Tags:** development, unity\
**Created:** [October 5, 2022, 3:40pm UTC](https://forum.magicleap.cloud/t/what-is-the-proper-order-of-operations-for-starting-and-ending-webrtc-sessions/1028 "2022-10-05T15:40:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![colden.prime](https://avatars.discourse-cdn.com/v4/letter/c/6bbea6/32.png) [@colden.prime](https://forum.magicleap.cloud/u/colden.prime)\
**Post date:** [October 5, 2022, 3:40pm UTC](https://forum.magicleap.cloud/t/what-is-the-proper-order-of-operations-for-starting-and-ending-webrtc-sessions/1028/1 "2022-10-05T15:40:24Z")

</div>

I am wondering what is the proper order of operations for starting and ending WebRTC sessions so that the camera/connection are properly instantiated and disposed.

From the example code and my own implementations, the order appears to be:

Startup:

1. Create webrtc connection - MLWebRTC.PeerConnection.CreateRemote()
2. Create and connect to camera - MLCamera.CreateAndConnect()
3. Create local video source - MLWebRTC.MLCameraVideoSource.CreateLocal()
4. Create local media stream - MLWebRTC.MediaStream.CreateWithAppDefinedVideoTrack()
5. Add audio/video tracks to webrtc connection - MLWebRTC.PeerConnection.AddLocalTrack()
6. Create and add data channel to webrtc connection - MLWebRTC.DataChannel.CreateLocal()
7. Create offers - MLWebRTC.PeerConnection.CreateOffer()
8. _Wait for answers..._
9. Set remote answer - MLWebRTC.PeerConnection.SetRemoteAnswer
10. Consume ices candidates - MLWebRTC.PeerConnection.AddRemoteIceCandidate()
11. _Wait for connection complete event..._
12. Enable audio/video tracks - MLWebRTC.MediaStream.Track.SetEnabled(true)

Shutdown:

1. Disable audio/video tracks - MLWebRTC.MediaStream.Track.SetEnabled(false)
2. Destroy local video source - MLWebRTC.MLCameraVideoSource.DestroyLocal()
3. _Wait for local video source to stop capturing..._
4. Disconnect camera - MLCamera.Disconnect()
5. Destroy local media stream - MLWebRTC.MediaStream.DestroyLocal()
6. Destroy data channel - MLWebRTC.DataChannel.Destroy()
7. Destroy webrtc connection - MLWebRTC.PeerConnection.Destroy()

Note: Occasionally, when shutting down a session, the green camera indicator in the upper left remains active and I am unable to start future sessions until I kill my application and/or restart the device.

**So my questions are:**

- Are all of these operations necessary for startup/shutdown?
- Is this the correct order for operations?
- Are any additional wait intervals required to prevent these operations for conflicting with each other?

**ML2 OS version** : B3E.220818.12-R.085  
**MLSDK version** : 0.53.3

---

<div class="post-metadata">

**Author:** ![kbabilinski](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.magicleap.cloud/kbabilinski/32/19_2.png) [@kbabilinski](https://forum.magicleap.cloud/u/kbabilinski)\
**Post date:** [October 11, 2022, 9:48pm UTC](https://forum.magicleap.cloud/t/what-is-the-proper-order-of-operations-for-starting-and-ending-webrtc-sessions/1028/2 "2022-10-11T21:48:05Z")

</div>

Hi @colden.prime,

That sequence looks correct to me. I asked for an additional pair of eyes to investigate the problem you are having. I'll report back as soon as I get more information.

---

<div class="post-metadata">

**Author:** ![kbabilinski](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.magicleap.cloud/kbabilinski/32/19_2.png) [@kbabilinski](https://forum.magicleap.cloud/u/kbabilinski)\
**Post date:** [October 20, 2022, 5:19pm UTC](https://forum.magicleap.cloud/t/what-is-the-proper-order-of-operations-for-starting-and-ending-webrtc-sessions/1028/3 "2022-10-20T17:19:29Z")

</div>

@colden.prime we are investigating an issue on the OS level that prevents applications from receiving the Application Quit or Application Focus events. I will let you know as soon as I have more information.
