aboutsummaryrefslogtreecommitdiff
path: root/src/cljs/gnunet_web/webrtc.cljs
blob: d43292e03ee298429615f9777053bedd2c6f095c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
;; webrtc.cljs - WebRTC functions for gnunet-web
;; Copyright (C) 2018  David Barksdale <amatus@amat.us>
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

(ns gnunet-web.webrtc)

(def rtc-config
  (clj->js
    {:ice-servers [{:url "stun:stun.l.google.com:19302"}]}))

(def data-channel-config
  (clj->js
    {:ordered false
     :maxRetransmits 0
     :negotiated true
     :id 1}))

(defn peer-connect
  [message-port offer]
  (.debug js/console "peer-connect called with offer:" offer)
  (let [connection (js/RTCPeerConnection. rtc-config)
        channel (.createDataChannel connection
                 "data"
                 data-channel-config)]
    (set! (.-onopen channel)
      nil)
    (set! (.-onmessage channel)
      nil)
    (if (empty? offer)
      (.then (.createOffer connection)
        (fn [offer]
          (.debug js/console "created offer:" offer)
          (.setLocalDescription connection offer)
          (.postMessage message-port
            (js-obj "type" "offer"
                    "sdp" (.-sdp offer)))))
      (.then (.setRemoteDescription connection
                          (js-obj "type" "offer"
                            "sdp" offer))
        (fn []
          (.then (.createAnswer connection)
            (fn [answer]
              (.debug js/console "created answer:" answer)
              (.setLocalDescription connection answer)
              (.postMessage message-port
                (js-obj "type" "answer"
                  "sdp" (.-sdp answer))))))))
    (set! (.-onmessage message-port)
      (fn [e]
        (.debug js/console "got webrtc message-channel message" e)
        (let [data (.-data e)]
          (condp = (.-type data)
            "answer" (.then (.setRemoteDescription connection
                              (js-obj "type" "answer"
                                "sdp" (.-sdp data)))
                        (fn []))))))))