From 951ccef12ded25a18cb02e745923e4f9af577b32 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Wed, 11 May 2016 18:51:34 -0400 Subject: [PATCH 01/94] made namespace for web rtc call views --- src/braid/ui/views/call.cljs | 9 +++++++++ src/braid/ui/views/pills.cljs | 1 + 2 files changed, 10 insertions(+) create mode 100644 src/braid/ui/views/call.cljs diff --git a/src/braid/ui/views/call.cljs b/src/braid/ui/views/call.cljs new file mode 100644 index 000000000..04e3a93ad --- /dev/null +++ b/src/braid/ui/views/call.cljs @@ -0,0 +1,9 @@ +(ns braid.ui.views.call + (:require [reagent.core :as r] + [reagent.impl.util :refer [extract-props]])) + +(defn caller-tag-view []) + +(defn calling-panel-view []) + +(defn call-interface-view []) diff --git a/src/braid/ui/views/pills.cljs b/src/braid/ui/views/pills.cljs index 74b926926..2029c097c 100644 --- a/src/braid/ui/views/pills.cljs +++ b/src/braid/ui/views/pills.cljs @@ -1,6 +1,7 @@ (ns braid.ui.views.pills (:require [reagent.core :as r] [reagent.impl.util :refer [extract-props]] + [braid.ui.views.call :refer [caller-tag-view]] [chat.client.routes :as routes] [chat.client.dispatcher :refer [dispatch!]] [chat.client.views.helpers :refer [id->color]] From 37195f2fcf8f413fae007d977b243f8d703a5d26 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Thu, 12 May 2016 15:21:30 -0400 Subject: [PATCH 02/94] Make the call tag/panel view and setup dispatch method to start calls --- src/braid/ui/views/call.cljs | 17 +++++++++++++++-- src/braid/ui/views/pages/user.cljs | 4 +++- src/braid/ui/views/pills.cljs | 3 ++- src/chat/client/dispatcher.cljs | 3 +++ 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/braid/ui/views/call.cljs b/src/braid/ui/views/call.cljs index 04e3a93ad..a776182cc 100644 --- a/src/braid/ui/views/call.cljs +++ b/src/braid/ui/views/call.cljs @@ -1,8 +1,21 @@ (ns braid.ui.views.call (:require [reagent.core :as r] - [reagent.impl.util :refer [extract-props]])) + [reagent.impl.util :refer [extract-props]] + [chat.client.dispatcher :refer [dispatch!]])) -(defn caller-tag-view []) +(defn caller-tag-view + [user] + (fn [] + [:div + [:h3 "Call"] + [:a.button {:on-click + (fn [_] + (dispatch! :start-call user))} + "Audio"] + [:a.button {:on-click + (fn [_] + (dispatch! :start-call user))} + "Video"]])) (defn calling-panel-view []) diff --git a/src/braid/ui/views/pages/user.cljs b/src/braid/ui/views/pages/user.cljs index 11926b7b8..24aa3fef1 100644 --- a/src/braid/ui/views/pages/user.cljs +++ b/src/braid/ui/views/pages/user.cljs @@ -1,5 +1,6 @@ (ns braid.ui.views.pages.user (:require [reagent.ratom :include-macros true :refer-macros [reaction]] + [braid.ui.views.call :refer [caller-tag-view]] [braid.ui.views.pills :refer [user-pill-view]] [braid.ui.views.new-thread :refer [new-thread-view]] [braid.ui.views.threads :refer [threads-view]] @@ -38,6 +39,7 @@ [:img.avatar {:src @user-avatar-url}] [:p "One day, a profile will be here."] [:p "Currently only showing your open threads that mention this user."] - [:p "Soon, you will see all recent threads this user has participated in."]]] + [:p "Soon, you will see all recent threads this user has participated in."] + [caller-tag-view @user]]] [threads-view {:new-thread-args {:mentioned-ids [(@user :id)]} :threads @sorted-threads}]]))) diff --git a/src/braid/ui/views/pills.cljs b/src/braid/ui/views/pills.cljs index 2029c097c..191c2f1b2 100644 --- a/src/braid/ui/views/pills.cljs +++ b/src/braid/ui/views/pills.cljs @@ -66,7 +66,6 @@ :component-will-receive-props (fn [_ [_ new-user-id]] (reset! user-id-atom new-user-id)) - :reagent-render (fn [user-id] (let [path (routes/user-page-path {:group-id @open-group-id @@ -74,6 +73,8 @@ color (id->color user-id)] [:a.user.pill {:class (str (case @user-status :online "on" "off") (when @admin? " admin")) + :on-hover (fn [e] + (.log js/console "call?")) :title (when @admin? "admin") :tabIndex -1 :style {:background-color color diff --git a/src/chat/client/dispatcher.cljs b/src/chat/client/dispatcher.cljs index 76a9fb984..6b2838a55 100644 --- a/src/chat/client/dispatcher.cljs +++ b/src/chat/client/dispatcher.cljs @@ -269,6 +269,9 @@ (doseq [id open-thread-ids] (dispatch! :hide-thread {:thread-id id})))) +(defmethod dispatch! :start-call [_ {:keys [id nickname]}] + ) + (defn check-client-version [server-checksum] (when (not= (aget js/window "checksum") server-checksum) (store/display-error! :client-out-of-date "Client out of date - please refresh"))) From 5c2f6d5b5594d328db724c0610ec5e2dfddc9c47 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Thu, 12 May 2016 15:59:40 -0400 Subject: [PATCH 03/94] Tweaked dispatch method to take call data packet --- src/braid/ui/views/call.cljs | 8 +++++--- src/braid/ui/views/pages/user.cljs | 2 +- src/chat/client/dispatcher.cljs | 4 ++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/braid/ui/views/call.cljs b/src/braid/ui/views/call.cljs index a776182cc..34c621377 100644 --- a/src/braid/ui/views/call.cljs +++ b/src/braid/ui/views/call.cljs @@ -4,17 +4,19 @@ [chat.client.dispatcher :refer [dispatch!]])) (defn caller-tag-view - [user] + [user-id] (fn [] [:div [:h3 "Call"] [:a.button {:on-click (fn [_] - (dispatch! :start-call user))} + (dispatch! :start-call + (assoc {} :id user-id :call-type "audio")))} "Audio"] [:a.button {:on-click (fn [_] - (dispatch! :start-call user))} + (dispatch! :start-call + (assoc {} :id user-id :call-type "video")))} "Video"]])) (defn calling-panel-view []) diff --git a/src/braid/ui/views/pages/user.cljs b/src/braid/ui/views/pages/user.cljs index 24aa3fef1..e8ddfaf5f 100644 --- a/src/braid/ui/views/pages/user.cljs +++ b/src/braid/ui/views/pages/user.cljs @@ -40,6 +40,6 @@ [:p "One day, a profile will be here."] [:p "Currently only showing your open threads that mention this user."] [:p "Soon, you will see all recent threads this user has participated in."] - [caller-tag-view @user]]] + [caller-tag-view (@user :id)]]] [threads-view {:new-thread-args {:mentioned-ids [(@user :id)]} :threads @sorted-threads}]]))) diff --git a/src/chat/client/dispatcher.cljs b/src/chat/client/dispatcher.cljs index 6b2838a55..088d3074b 100644 --- a/src/chat/client/dispatcher.cljs +++ b/src/chat/client/dispatcher.cljs @@ -269,8 +269,8 @@ (doseq [id open-thread-ids] (dispatch! :hide-thread {:thread-id id})))) -(defmethod dispatch! :start-call [_ {:keys [id nickname]}] - ) +(defmethod dispatch! :start-call [_ {:keys [id call-type]}] + (println call-type "call to" id)) (defn check-client-version [server-checksum] (when (not= (aget js/window "checksum") server-checksum) From 8d49760d59822b5d0fc94b8db17c4283d04a8f5f Mon Sep 17 00:00:00 2001 From: 10plusY Date: Thu, 12 May 2016 16:33:41 -0400 Subject: [PATCH 04/94] Tweak call method flow to use caller and callee ids --- src/braid/ui/views/call.cljs | 10 +++++++--- src/braid/ui/views/pages/user.cljs | 4 +++- src/chat/client/dispatcher.cljs | 5 +++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/braid/ui/views/call.cljs b/src/braid/ui/views/call.cljs index 34c621377..a48c86a97 100644 --- a/src/braid/ui/views/call.cljs +++ b/src/braid/ui/views/call.cljs @@ -4,19 +4,23 @@ [chat.client.dispatcher :refer [dispatch!]])) (defn caller-tag-view - [user-id] + [caller-id callee-id] (fn [] [:div [:h3 "Call"] [:a.button {:on-click (fn [_] (dispatch! :start-call - (assoc {} :id user-id :call-type "audio")))} + (assoc {} :caller-id caller-id + :callee-id callee-id + :call-type "audio")))} "Audio"] [:a.button {:on-click (fn [_] (dispatch! :start-call - (assoc {} :id user-id :call-type "video")))} + (assoc {} :caller-id caller-id + :callee-id callee-id + :call-type "video")))} "Video"]])) (defn calling-panel-view []) diff --git a/src/braid/ui/views/pages/user.cljs b/src/braid/ui/views/pages/user.cljs index e8ddfaf5f..fdb8f6e37 100644 --- a/src/braid/ui/views/pages/user.cljs +++ b/src/braid/ui/views/pages/user.cljs @@ -40,6 +40,8 @@ [:p "One day, a profile will be here."] [:p "Currently only showing your open threads that mention this user."] [:p "Soon, you will see all recent threads this user has participated in."] - [caller-tag-view (@user :id)]]] + (when (not= @current-user-id (@user :id)) + [:div.call + [caller-tag-view @current-user-id (@user :id)]])]] [threads-view {:new-thread-args {:mentioned-ids [(@user :id)]} :threads @sorted-threads}]]))) diff --git a/src/chat/client/dispatcher.cljs b/src/chat/client/dispatcher.cljs index 088d3074b..e83e7326e 100644 --- a/src/chat/client/dispatcher.cljs +++ b/src/chat/client/dispatcher.cljs @@ -269,8 +269,9 @@ (doseq [id open-thread-ids] (dispatch! :hide-thread {:thread-id id})))) -(defmethod dispatch! :start-call [_ {:keys [id call-type]}] - (println call-type "call to" id)) +(defmethod dispatch! :start-call + [_ {:keys [caller-id callee-id call-type]}] + (println call-type "call from" caller-id "to" callee-id)) (defn check-client-version [server-checksum] (when (not= (aget js/window "checksum") server-checksum) From 3b681999dc19679b329541a6ad09585e8e4303cb Mon Sep 17 00:00:00 2001 From: 10plusY Date: Thu, 12 May 2016 17:47:59 -0400 Subject: [PATCH 05/94] Add socket event to start call --- src/chat/client/dispatcher.cljs | 5 ++--- src/chat/server/sync.clj | 4 ++++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/chat/client/dispatcher.cljs b/src/chat/client/dispatcher.cljs index e83e7326e..4b2d9ab25 100644 --- a/src/chat/client/dispatcher.cljs +++ b/src/chat/client/dispatcher.cljs @@ -269,9 +269,8 @@ (doseq [id open-thread-ids] (dispatch! :hide-thread {:thread-id id})))) -(defmethod dispatch! :start-call - [_ {:keys [caller-id callee-id call-type]}] - (println call-type "call from" caller-id "to" callee-id)) +(defmethod dispatch! :start-call [_ {:keys [caller-id callee-id call-type] :as args}] + (sync/chsk-send! [:chat/make-call args])) (defn check-client-version [server-checksum] (when (not= (aget js/window "checksum") server-checksum) diff --git a/src/chat/server/sync.clj b/src/chat/server/sync.clj index 54fb830ee..ee81cfafd 100644 --- a/src/chat/server/sync.clj +++ b/src/chat/server/sync.clj @@ -371,6 +371,10 @@ (db/group-set! group-id :avatar avatar) (broadcast-group-change group-id [:group/new-avatar [group-id avatar]])))))) +(defmethod event-msg-handler :chat/make-call + [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] + #_(println "socket data" ?data)) + (defmethod event-msg-handler :session/start [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] (when-let [user-id (get-in ring-req [:session :user-id])] From f674fff5b86ef9eb1706f970bf779fa1b03542ee Mon Sep 17 00:00:00 2001 From: 10plusY Date: Thu, 12 May 2016 19:51:59 -0400 Subject: [PATCH 06/94] Setup socket event to handle call request on client side, fix initial dispatch method --- src/braid/ui/views/call.cljs | 24 +++++++++++------------- src/braid/ui/views/pages/user.cljs | 2 +- src/chat/client/dispatcher.cljs | 8 ++++++-- src/chat/server/sync.clj | 4 +++- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/braid/ui/views/call.cljs b/src/braid/ui/views/call.cljs index a48c86a97..d7fcb177e 100644 --- a/src/braid/ui/views/call.cljs +++ b/src/braid/ui/views/call.cljs @@ -4,23 +4,21 @@ [chat.client.dispatcher :refer [dispatch!]])) (defn caller-tag-view - [caller-id callee-id] + [callee-id] (fn [] [:div [:h3 "Call"] - [:a.button {:on-click - (fn [_] - (dispatch! :start-call - (assoc {} :caller-id caller-id - :callee-id callee-id - :call-type "audio")))} + [:a.button + {:on-click + (fn [_] + (dispatch! :start-call (assoc {} :callee-id callee-id + :call-type "audio")))} "Audio"] - [:a.button {:on-click - (fn [_] - (dispatch! :start-call - (assoc {} :caller-id caller-id - :callee-id callee-id - :call-type "video")))} + [:a.button + {:on-click + (fn [_] + (dispatch! :start-call (assoc {} :callee-id callee-id + :call-type "video")))} "Video"]])) (defn calling-panel-view []) diff --git a/src/braid/ui/views/pages/user.cljs b/src/braid/ui/views/pages/user.cljs index fdb8f6e37..109a73fb0 100644 --- a/src/braid/ui/views/pages/user.cljs +++ b/src/braid/ui/views/pages/user.cljs @@ -42,6 +42,6 @@ [:p "Soon, you will see all recent threads this user has participated in."] (when (not= @current-user-id (@user :id)) [:div.call - [caller-tag-view @current-user-id (@user :id)]])]] + [caller-tag-view (@user :id)]])]] [threads-view {:new-thread-args {:mentioned-ids [(@user :id)]} :threads @sorted-threads}]]))) diff --git a/src/chat/client/dispatcher.cljs b/src/chat/client/dispatcher.cljs index 4b2d9ab25..c9c186f24 100644 --- a/src/chat/client/dispatcher.cljs +++ b/src/chat/client/dispatcher.cljs @@ -269,8 +269,8 @@ (doseq [id open-thread-ids] (dispatch! :hide-thread {:thread-id id})))) -(defmethod dispatch! :start-call [_ {:keys [caller-id callee-id call-type] :as args}] - (sync/chsk-send! [:chat/make-call args])) +(defmethod dispatch! :start-call [_ call-data] + (sync/chsk-send! [:chat/make-call call-data])) (defn check-client-version [server-checksum] (when (not= (aget js/window "checksum") server-checksum) @@ -356,3 +356,7 @@ [[_ message]] (println "Got notification " message) (notify/notify {:msg (:content message)})) + +(defmethod sync/event-handler :chat/accept-call + [[_ [caller-id call-type]]] + #_(println call-type "call from" caller-id)) diff --git a/src/chat/server/sync.clj b/src/chat/server/sync.clj index ee81cfafd..9922bac54 100644 --- a/src/chat/server/sync.clj +++ b/src/chat/server/sync.clj @@ -373,7 +373,9 @@ (defmethod event-msg-handler :chat/make-call [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] - #_(println "socket data" ?data)) + (let [{:keys [callee-id call-type]} ?data + caller-id (get-in ring-req [:session :user-id])] + (chsk-send! callee-id [:chat/accept-call [caller-id call-type]]))) (defmethod event-msg-handler :session/start [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] From c5a5b23184738a59b035d939ce2c97ab04a985cd Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 13 May 2016 13:00:46 -0400 Subject: [PATCH 07/94] Add store key for calls and call schema --- src/braid/common/schema.cljc | 7 +++++++ src/chat/client/store.cljs | 6 ++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/braid/common/schema.cljc b/src/braid/common/schema.cljc index 73be820ae..ea1818dd0 100644 --- a/src/braid/common/schema.cljc +++ b/src/braid/common/schema.cljc @@ -88,3 +88,10 @@ :invitee-email s/Str :group-id s/Uuid :group-name s/Str}) + +(def Call + {:id s/Uuid + :type s/Str + :source-id s/Uuid + :target-id s/Uuid + :status s/Str}) diff --git a/src/chat/client/store.cljs b/src/chat/client/store.cljs index 693d6a22e..cc69ad16e 100644 --- a/src/chat/client/store.cljs +++ b/src/chat/client/store.cljs @@ -26,7 +26,8 @@ :unread-count 0} :user {:open-thread-ids #{} :subscribed-tag-ids #{}} - :new-thread-id (uuid/make-random-squuid)})) + :new-thread-id (uuid/make-random-squuid) + :calls {}})) (def AppState {:login-state (s/enum :auth-check :login-form :ws-connect :app) @@ -49,7 +50,8 @@ :unread-count s/Int} :user {:open-thread-ids #{s/Uuid} :subscribed-tag-ids #{s/Uuid}} - :new-thread-id s/Uuid}) + :new-thread-id s/Uuid + :calls {s/Uuid app-schema/Call}}) (def check-app-state! (s/validator AppState)) From 5602017f75c1febda17c16f6f35e20fd43eb1b9a Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 13 May 2016 13:45:36 -0400 Subject: [PATCH 08/94] Create methods for adding call to store and update call status --- src/chat/client/store.cljs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/chat/client/store.cljs b/src/chat/client/store.cljs index cc69ad16e..c71675b5c 100644 --- a/src/chat/client/store.cljs +++ b/src/chat/client/store.cljs @@ -400,4 +400,10 @@ current-group-id))))))] open-threads)) +; calls +(defn add-call! [call] + (transact! [:calls] #(assoc % (:id call) call))) + +(defn update-call-status! [call-id status] + (transact! [:calls call-id :status] (constantly status))) From 12f3a815bbb53ceaacd4709080f9617e2a49167f Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 13 May 2016 14:56:29 -0400 Subject: [PATCH 09/94] Tweak socket event to receive callee-id --- src/chat/client/dispatcher.cljs | 10 +++++++--- src/chat/client/schema.cljs | 8 ++++++++ src/chat/server/sync.clj | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/chat/client/dispatcher.cljs b/src/chat/client/dispatcher.cljs index c9c186f24..25825f4ee 100644 --- a/src/chat/client/dispatcher.cljs +++ b/src/chat/client/dispatcher.cljs @@ -357,6 +357,10 @@ (println "Got notification " message) (notify/notify {:msg (:content message)})) -(defmethod sync/event-handler :chat/accept-call - [[_ [caller-id call-type]]] - #_(println call-type "call from" caller-id)) +(defmethod sync/event-handler :chat/receive-call + [[_ [caller-id callee-id call-type]]] + (let [call (schema/make-call {:type call-type + :source-id caller-id + :target-id callee-id + :status "created"})] + (store/add-call! call))) diff --git a/src/chat/client/schema.cljs b/src/chat/client/schema.cljs index 322e2a6c0..09bedc308 100644 --- a/src/chat/client/schema.cljs +++ b/src/chat/client/schema.cljs @@ -32,3 +32,11 @@ {:id (or (data :id) (uuid/make-random-squuid)) :invitee-email (data :invitee-email) :group-id (data :group-id)}) + + +(defn make-call [data] + {:id (or (data :id) (uuid/make-random-squuid)) + :type (data :type) + :source-id (data :source-id) + :target-id (data :target-id) + :status (data :status)}) diff --git a/src/chat/server/sync.clj b/src/chat/server/sync.clj index 9922bac54..ce0819bea 100644 --- a/src/chat/server/sync.clj +++ b/src/chat/server/sync.clj @@ -375,7 +375,7 @@ [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] (let [{:keys [callee-id call-type]} ?data caller-id (get-in ring-req [:session :user-id])] - (chsk-send! callee-id [:chat/accept-call [caller-id call-type]]))) + (chsk-send! callee-id [:chat/receive-call [caller-id callee-id call-type]]))) (defmethod event-msg-handler :session/start [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] From 639355424fa526984a49c2a1e7aa7506b5e9dd75 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 13 May 2016 15:43:11 -0400 Subject: [PATCH 10/94] Register subscriptions to state's calls and call attributes --- src/braid/common/state.cljs | 9 +++++++++ src/chat/client/reagent_adapter.cljs | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/src/braid/common/state.cljs b/src/braid/common/state.cljs index 028347f1b..49808dd2e 100644 --- a/src/braid/common/state.cljs +++ b/src/braid/common/state.cljs @@ -292,3 +292,12 @@ (defn get-preference [state [_ pref]] (reaction (get-in @state [:preferences pref]))) + +(defn get-calls + [state _] + (reaction (@state :calls))) + +(defn get-call-status? + ([state [_ call-id]] get-call-status? state nil call-id) + ([state _ call-id] + (reaction (get-in @state [:calls call-id :status])))) diff --git a/src/chat/client/reagent_adapter.cljs b/src/chat/client/reagent_adapter.cljs index f88557de2..e1dd3c55f 100644 --- a/src/chat/client/reagent_adapter.cljs +++ b/src/chat/client/reagent_adapter.cljs @@ -170,6 +170,14 @@ :user-preference state/get-preference) +(register-sub + :calls + state/get-calls) + +(register-sub + :call-status? + state/get-call-status?) + (defn subscribe ([v] (let [key-v (first v) From 28c3fd48bd281e78a69e322a497a3c3fc2f79312 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 13 May 2016 19:53:29 -0400 Subject: [PATCH 11/94] Create calling pane view, render incoming call views --- src/braid/common/state.cljs | 2 +- src/braid/ui/views/call.cljs | 31 +++++++++++++++++++++++++----- src/braid/ui/views/pages/user.cljs | 3 +-- src/braid/ui/views/pills.cljs | 1 - src/chat/client/dispatcher.cljs | 5 ++++- src/chat/client/store.cljs | 1 + 6 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/braid/common/state.cljs b/src/braid/common/state.cljs index 49808dd2e..9377ba6d4 100644 --- a/src/braid/common/state.cljs +++ b/src/braid/common/state.cljs @@ -295,7 +295,7 @@ (defn get-calls [state _] - (reaction (@state :calls))) + (reaction (vals (@state :calls)))) (defn get-call-status? ([state [_ call-id]] get-call-status? state nil call-id) diff --git a/src/braid/ui/views/call.cljs b/src/braid/ui/views/call.cljs index d7fcb177e..698aa12ac 100644 --- a/src/braid/ui/views/call.cljs +++ b/src/braid/ui/views/call.cljs @@ -1,13 +1,18 @@ (ns braid.ui.views.call (:require [reagent.core :as r] [reagent.impl.util :refer [extract-props]] - [chat.client.dispatcher :refer [dispatch!]])) + [braid.ui.views.pills :refer [user-pill-view]] + [chat.client.dispatcher :refer [dispatch!]] + [chat.client.reagent-adapter :refer [subscribe]])) (defn caller-tag-view [callee-id] (fn [] - [:div - [:h3 "Call"] + [:div.call ;TODO: pass user to render pill + [:div + [:h3 "Call"] + [user-pill-view callee-id]] + [:br] [:a.button {:on-click (fn [_] @@ -19,8 +24,24 @@ (fn [_] (dispatch! :start-call (assoc {} :callee-id callee-id :call-type "video")))} - "Video"]])) + "Video"] + [calling-panel-view]])) + +(defn calling-panel-view + [] + (let [calls (subscribe [:calls])] + (fn [] + (when (seq @calls) + [:div + [:h3 "Calling..."] + [:div.calls + (doall + (for [call @calls] + ^{:key (call :id)} + [:div + [:p (str (call :id))] + [:a.button "Accept"] + [:a.button "Decline"]]))]])))) -(defn calling-panel-view []) (defn call-interface-view []) diff --git a/src/braid/ui/views/pages/user.cljs b/src/braid/ui/views/pages/user.cljs index 109a73fb0..6e5ceee60 100644 --- a/src/braid/ui/views/pages/user.cljs +++ b/src/braid/ui/views/pages/user.cljs @@ -41,7 +41,6 @@ [:p "Currently only showing your open threads that mention this user."] [:p "Soon, you will see all recent threads this user has participated in."] (when (not= @current-user-id (@user :id)) - [:div.call - [caller-tag-view (@user :id)]])]] + [caller-tag-view (@user :id)])]] [threads-view {:new-thread-args {:mentioned-ids [(@user :id)]} :threads @sorted-threads}]]))) diff --git a/src/braid/ui/views/pills.cljs b/src/braid/ui/views/pills.cljs index 191c2f1b2..f99301299 100644 --- a/src/braid/ui/views/pills.cljs +++ b/src/braid/ui/views/pills.cljs @@ -1,7 +1,6 @@ (ns braid.ui.views.pills (:require [reagent.core :as r] [reagent.impl.util :refer [extract-props]] - [braid.ui.views.call :refer [caller-tag-view]] [chat.client.routes :as routes] [chat.client.dispatcher :refer [dispatch!]] [chat.client.views.helpers :refer [id->color]] diff --git a/src/chat/client/dispatcher.cljs b/src/chat/client/dispatcher.cljs index 25825f4ee..e81d5ef2f 100644 --- a/src/chat/client/dispatcher.cljs +++ b/src/chat/client/dispatcher.cljs @@ -272,6 +272,9 @@ (defmethod dispatch! :start-call [_ call-data] (sync/chsk-send! [:chat/make-call call-data])) +(defmethod dispatch! :accept-call [_ call-id] + (store/update-call-status! call-id "accepted")) + (defn check-client-version [server-checksum] (when (not= (aget js/window "checksum") server-checksum) (store/display-error! :client-out-of-date "Client out of date - please refresh"))) @@ -362,5 +365,5 @@ (let [call (schema/make-call {:type call-type :source-id caller-id :target-id callee-id - :status "created"})] + :status "incoming"})] (store/add-call! call))) diff --git a/src/chat/client/store.cljs b/src/chat/client/store.cljs index c71675b5c..888aa6e6a 100644 --- a/src/chat/client/store.cljs +++ b/src/chat/client/store.cljs @@ -403,6 +403,7 @@ ; calls (defn add-call! [call] + (println "call to add" call) (transact! [:calls] #(assoc % (:id call) call))) (defn update-call-status! [call-id status] From e443c8b0f3d5aafad1fc1b3f5a87347bec5b16aa Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 13 May 2016 20:16:30 -0400 Subject: [PATCH 12/94] Move each call to its own component, changed names of call components --- src/braid/ui/views/call.cljs | 24 +++++++++++++++++------- src/braid/ui/views/pages/user.cljs | 4 ++-- src/chat/client/dispatcher.cljs | 3 +++ 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/braid/ui/views/call.cljs b/src/braid/ui/views/call.cljs index 698aa12ac..127c35bb7 100644 --- a/src/braid/ui/views/call.cljs +++ b/src/braid/ui/views/call.cljs @@ -5,7 +5,7 @@ [chat.client.dispatcher :refer [dispatch!]] [chat.client.reagent-adapter :refer [subscribe]])) -(defn caller-tag-view +(defn call-start-view [callee-id] (fn [] [:div.call ;TODO: pass user to render pill @@ -25,9 +25,9 @@ (dispatch! :start-call (assoc {} :callee-id callee-id :call-type "video")))} "Video"] - [calling-panel-view]])) + [call-list-view]])) -(defn calling-panel-view +(defn call-list-view [] (let [calls (subscribe [:calls])] (fn [] @@ -38,10 +38,20 @@ (doall (for [call @calls] ^{:key (call :id)} - [:div - [:p (str (call :id))] - [:a.button "Accept"] - [:a.button "Decline"]]))]])))) + [new-call-view call]))]])))) +(defn new-call-view + [call] + (fn [] + [:div + [:p (str (call :id))] + [:a.button + {:on-click + (fn [_] (dispatch! :accept-call (call :id)))} + "Accept"] + [:a.button + {:on-click + (fn [_] (dispatch! :decline-call (call :id)))} + "Decline"]])) (defn call-interface-view []) diff --git a/src/braid/ui/views/pages/user.cljs b/src/braid/ui/views/pages/user.cljs index 6e5ceee60..1002843a5 100644 --- a/src/braid/ui/views/pages/user.cljs +++ b/src/braid/ui/views/pages/user.cljs @@ -1,6 +1,6 @@ (ns braid.ui.views.pages.user (:require [reagent.ratom :include-macros true :refer-macros [reaction]] - [braid.ui.views.call :refer [caller-tag-view]] + [braid.ui.views.call :refer [call-start-view]] [braid.ui.views.pills :refer [user-pill-view]] [braid.ui.views.new-thread :refer [new-thread-view]] [braid.ui.views.threads :refer [threads-view]] @@ -41,6 +41,6 @@ [:p "Currently only showing your open threads that mention this user."] [:p "Soon, you will see all recent threads this user has participated in."] (when (not= @current-user-id (@user :id)) - [caller-tag-view (@user :id)])]] + [call-start-view (@user :id)])]] [threads-view {:new-thread-args {:mentioned-ids [(@user :id)]} :threads @sorted-threads}]]))) diff --git a/src/chat/client/dispatcher.cljs b/src/chat/client/dispatcher.cljs index e81d5ef2f..3ff126aef 100644 --- a/src/chat/client/dispatcher.cljs +++ b/src/chat/client/dispatcher.cljs @@ -275,6 +275,9 @@ (defmethod dispatch! :accept-call [_ call-id] (store/update-call-status! call-id "accepted")) +(defmethod dispatch! :decline-call [_ call-id] + (store/update-call-status! call-id "declined")) + (defn check-client-version [server-checksum] (when (not= (aget js/window "checksum") server-checksum) (store/display-error! :client-out-of-date "Client out of date - please refresh"))) From ee38e487014e53e739ee2abb6bca2733ef601485 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 13 May 2016 21:26:18 -0400 Subject: [PATCH 13/94] Add control flow to call views to render based on status --- src/braid/ui/views/call.cljs | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/braid/ui/views/call.cljs b/src/braid/ui/views/call.cljs index 127c35bb7..70cbc1af6 100644 --- a/src/braid/ui/views/call.cljs +++ b/src/braid/ui/views/call.cljs @@ -42,16 +42,29 @@ (defn new-call-view [call] - (fn [] - [:div - [:p (str (call :id))] - [:a.button - {:on-click - (fn [_] (dispatch! :accept-call (call :id)))} - "Accept"] - [:a.button - {:on-click - (fn [_] (dispatch! :decline-call (call :id)))} - "Decline"]])) + (let [call-id (call :id) + call-status (subscribe [:call-status?] [call-id])] + (fn [] + #_(case @call-status + "incoming" + [:div + [:p (str (call :id))] + [:a.button + {:on-click + (fn [_] + (dispatch! :accept-call (call :id)))} + "Accept"] + [:a.button + {:on-click + (fn [_] + (dispatch! :decline-call (call :id)))} + "Decline"]] + "accepted" + [:p "Call Accepted"] + "declined" + {:p "Call Declined"} + "default" + [:p "Dunno"]) + [:div [:p "Hello"]]))) (defn call-interface-view []) From 40841c9cf2b778078c8934aa0f44bad806a7528b Mon Sep 17 00:00:00 2001 From: 10plusY Date: Mon, 16 May 2016 16:23:33 -0400 Subject: [PATCH 14/94] Fix subscription to render state-dependent call panel --- src/braid/common/state.cljs | 7 +-- src/braid/ui/views/call.cljs | 84 +++++++++++++++------------- src/chat/client/reagent_adapter.cljs | 4 +- src/chat/client/store.cljs | 1 - 4 files changed, 49 insertions(+), 47 deletions(-) diff --git a/src/braid/common/state.cljs b/src/braid/common/state.cljs index 9377ba6d4..e6e70dc16 100644 --- a/src/braid/common/state.cljs +++ b/src/braid/common/state.cljs @@ -297,7 +297,6 @@ [state _] (reaction (vals (@state :calls)))) -(defn get-call-status? - ([state [_ call-id]] get-call-status? state nil call-id) - ([state _ call-id] - (reaction (get-in @state [:calls call-id :status])))) +(defn get-call-status + [state [_ call-id]] + (reaction (get-in @state [:calls call-id :status]))) diff --git a/src/braid/ui/views/call.cljs b/src/braid/ui/views/call.cljs index 70cbc1af6..eccac071d 100644 --- a/src/braid/ui/views/call.cljs +++ b/src/braid/ui/views/call.cljs @@ -5,6 +5,50 @@ [chat.client.dispatcher :refer [dispatch!]] [chat.client.reagent-adapter :refer [subscribe]])) + + +(defn call-interface-view + [call] + (let [caller-id (call :source-id) + callee-id (call :target-id)] + (fn [] + [:div (str "call between" caller-id "and" callee-id)]))) + + +(defn new-call-view + [call] + [:div + (case (call :status) + "incoming" + [:div + [:p (str (call :id))] + [:a.button + {:on-click + (fn [_] + (dispatch! :accept-call (call :id)))} + "Accept"] + [:a.button + {:on-click + (fn [_] + (dispatch! :decline-call (call :id)))} + "Decline"]] + "accepted" [:p "accepted"] + "declined" [:p "declined"] + "default" [:p "dunno"]) + [call-interface-view call]]) + +(defn call-list-view + [] + (let [calls (subscribe [:calls])] + (fn [] + (when (seq @calls) + [:div + [:div.calls + (doall + (for [call @calls] + ^{:key (call :id)} + [new-call-view call]))]])))) + (defn call-start-view [callee-id] (fn [] @@ -27,44 +71,4 @@ "Video"] [call-list-view]])) -(defn call-list-view - [] - (let [calls (subscribe [:calls])] - (fn [] - (when (seq @calls) - [:div - [:h3 "Calling..."] - [:div.calls - (doall - (for [call @calls] - ^{:key (call :id)} - [new-call-view call]))]])))) - -(defn new-call-view - [call] - (let [call-id (call :id) - call-status (subscribe [:call-status?] [call-id])] - (fn [] - #_(case @call-status - "incoming" - [:div - [:p (str (call :id))] - [:a.button - {:on-click - (fn [_] - (dispatch! :accept-call (call :id)))} - "Accept"] - [:a.button - {:on-click - (fn [_] - (dispatch! :decline-call (call :id)))} - "Decline"]] - "accepted" - [:p "Call Accepted"] - "declined" - {:p "Call Declined"} - "default" - [:p "Dunno"]) - [:div [:p "Hello"]]))) -(defn call-interface-view []) diff --git a/src/chat/client/reagent_adapter.cljs b/src/chat/client/reagent_adapter.cljs index e1dd3c55f..94fcc4ea2 100644 --- a/src/chat/client/reagent_adapter.cljs +++ b/src/chat/client/reagent_adapter.cljs @@ -175,8 +175,8 @@ state/get-calls) (register-sub - :call-status? - state/get-call-status?) + :call-status + state/get-call-status) (defn subscribe ([v] diff --git a/src/chat/client/store.cljs b/src/chat/client/store.cljs index 888aa6e6a..c71675b5c 100644 --- a/src/chat/client/store.cljs +++ b/src/chat/client/store.cljs @@ -403,7 +403,6 @@ ; calls (defn add-call! [call] - (println "call to add" call) (transact! [:calls] #(assoc % (:id call) call))) (defn update-call-status! [call-id status] From af4ec81f60c851a60544de9511c31d45428aec96 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Mon, 16 May 2016 17:23:25 -0400 Subject: [PATCH 15/94] Add "ended" status and flow for call schema to end call --- src/braid/ui/views/call.cljs | 55 ++++++++++++++++++++------------- src/chat/client/dispatcher.cljs | 3 ++ 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/braid/ui/views/call.cljs b/src/braid/ui/views/call.cljs index eccac071d..2afccd0d3 100644 --- a/src/braid/ui/views/call.cljs +++ b/src/braid/ui/views/call.cljs @@ -10,32 +10,45 @@ (defn call-interface-view [call] (let [caller-id (call :source-id) - callee-id (call :target-id)] + user (subscribe [:user caller-id])] (fn [] - [:div (str "call between" caller-id "and" callee-id)]))) + [:div + [:h3 "Call with " (@user :nickname) "..."] + [:div "seconds"] + [:br] + [:a.button "A"] + [:a.button "M"] + [:a.button "V"] + [:a.button + {:on-click + (fn [_] + (dispatch! :end-call (call :id)))} "End Call"] + [:video]]))) (defn new-call-view [call] [:div - (case (call :status) - "incoming" - [:div - [:p (str (call :id))] - [:a.button - {:on-click - (fn [_] - (dispatch! :accept-call (call :id)))} - "Accept"] - [:a.button - {:on-click - (fn [_] - (dispatch! :decline-call (call :id)))} - "Decline"]] - "accepted" [:p "accepted"] - "declined" [:p "declined"] - "default" [:p "dunno"]) - [call-interface-view call]]) + (case (call :status) + "incoming" + [:div + [:p (str (call :id))] + [:a.button + {:on-click + (fn [_] + (dispatch! :accept-call (call :id)))} + "Accept"] + [:a.button + {:on-click + (fn [_] + (dispatch! :decline-call (call :id)))} + "Decline"]] + "accepted" + [call-interface-view call] + "declined" + [:p "declined"] + "ended" + [:p "ended"])]) (defn call-list-view [] @@ -70,5 +83,3 @@ :call-type "video")))} "Video"] [call-list-view]])) - - diff --git a/src/chat/client/dispatcher.cljs b/src/chat/client/dispatcher.cljs index 3ff126aef..1328b19b0 100644 --- a/src/chat/client/dispatcher.cljs +++ b/src/chat/client/dispatcher.cljs @@ -278,6 +278,9 @@ (defmethod dispatch! :decline-call [_ call-id] (store/update-call-status! call-id "declined")) +(defmethod dispatch! :end-call [_ call-id] + (store/update-call-status! call-id "ended")) + (defn check-client-version [server-checksum] (when (not= (aget js/window "checksum") server-checksum) (store/display-error! :client-out-of-date "Client out of date - please refresh"))) From b7f2884519185a25019bdede7630bbac0a68452c Mon Sep 17 00:00:00 2001 From: 10plusY Date: Mon, 16 May 2016 17:32:49 -0400 Subject: [PATCH 16/94] Make video player render only when call is video --- src/braid/ui/views/call.cljs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/braid/ui/views/call.cljs b/src/braid/ui/views/call.cljs index 2afccd0d3..540ff59f8 100644 --- a/src/braid/ui/views/call.cljs +++ b/src/braid/ui/views/call.cljs @@ -13,17 +13,18 @@ user (subscribe [:user caller-id])] (fn [] [:div - [:h3 "Call with " (@user :nickname) "..."] + [:h4 "Call with " (@user :nickname) "..."] [:div "seconds"] [:br] [:a.button "A"] [:a.button "M"] [:a.button "V"] + (when (= (call :type) "video") + [:video]) [:a.button {:on-click (fn [_] - (dispatch! :end-call (call :id)))} "End Call"] - [:video]]))) + (dispatch! :end-call (call :id)))} "End Call"]]))) (defn new-call-view From 0b4d54f8503cd6e02b9b0bcc8b87a4bc132f33d8 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Mon, 16 May 2016 17:37:44 -0400 Subject: [PATCH 17/94] Add timer div for length of call --- src/braid/ui/views/call.cljs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/braid/ui/views/call.cljs b/src/braid/ui/views/call.cljs index 540ff59f8..70a7f818c 100644 --- a/src/braid/ui/views/call.cljs +++ b/src/braid/ui/views/call.cljs @@ -9,12 +9,14 @@ (defn call-interface-view [call] - (let [caller-id (call :source-id) + (let [call-time (r/atom 0) + caller-id (call :source-id) user (subscribe [:user caller-id])] (fn [] + (js/setTimeout #(swap! call-time inc) 1000) [:div [:h4 "Call with " (@user :nickname) "..."] - [:div "seconds"] + [:div (str @call-time)] [:br] [:a.button "A"] [:a.button "M"] From c84ba43ce696b3edaeb5ca6cd8a0f2f13731d5e9 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Mon, 16 May 2016 19:47:43 -0400 Subject: [PATCH 18/94] Change call flow so that call added to state on both sides, add logic to render source/target views appropriately --- src/braid/ui/views/call.cljs | 81 +++++++++++++++++------------- src/braid/ui/views/pages/user.cljs | 2 +- src/chat/client/dispatcher.cljs | 15 +++--- src/chat/server/sync.clj | 6 +-- 4 files changed, 57 insertions(+), 47 deletions(-) diff --git a/src/braid/ui/views/call.cljs b/src/braid/ui/views/call.cljs index 70a7f818c..ace98919d 100644 --- a/src/braid/ui/views/call.cljs +++ b/src/braid/ui/views/call.cljs @@ -5,8 +5,6 @@ [chat.client.dispatcher :refer [dispatch!]] [chat.client.reagent-adapter :refer [subscribe]])) - - (defn call-interface-view [call] (let [call-time (r/atom 0) @@ -28,45 +26,54 @@ (fn [_] (dispatch! :end-call (call :id)))} "End Call"]]))) - (defn new-call-view [call] - [:div - (case (call :status) - "incoming" - [:div - [:p (str (call :id))] - [:a.button - {:on-click - (fn [_] - (dispatch! :accept-call (call :id)))} - "Accept"] - [:a.button - {:on-click - (fn [_] - (dispatch! :decline-call (call :id)))} - "Decline"]] - "accepted" - [call-interface-view call] - "declined" - [:p "declined"] - "ended" - [:p "ended"])]) + (let [user-id (subscribe [:user-id])] + (fn [call] + [:div + (case (call :status) + "incoming" + (if (= @user-id (call :target-id)) + [:div + [:p (str "Call from" (call :source-id))] + [:a.button + {:on-click + (fn [_] + (dispatch! :accept-call (call :id)))} + "Accept"] + [:a.button + {:on-click + (fn [_] + (dispatch! :decline-call (call :id)))} + "Decline"]] + [:div + [:p (str "Calling " (call :source-id) "...")] + [:a.button + {:on-click + (fn [_] + (dispatch! :end-call (call :id)))} + "Drop"]]) + "accepted" + [call-interface-view call] + "declined" + [:p "declined"] + "ended" + [:p "ended"])]))) (defn call-list-view [] (let [calls (subscribe [:calls])] (fn [] - (when (seq @calls) - [:div - [:div.calls - (doall - (for [call @calls] - ^{:key (call :id)} - [new-call-view call]))]])))) + [:div + (when (seq @calls) + [:div.calls + (doall + (for [call @calls] + ^{:key (call :id)} + [new-call-view call]))])]))) (defn call-start-view - [callee-id] + [caller-id callee-id] (fn [] [:div.call ;TODO: pass user to render pill [:div @@ -76,13 +83,15 @@ [:a.button {:on-click (fn [_] - (dispatch! :start-call (assoc {} :callee-id callee-id - :call-type "audio")))} + (dispatch! :start-call (assoc {} :type "audio" + :source-id caller-id + :target-id callee-id)))} "Audio"] [:a.button {:on-click (fn [_] - (dispatch! :start-call (assoc {} :callee-id callee-id - :call-type "video")))} + (dispatch! :start-call (assoc {} :type "video" + :source-id caller-id + :target-id callee-id)))} "Video"] [call-list-view]])) diff --git a/src/braid/ui/views/pages/user.cljs b/src/braid/ui/views/pages/user.cljs index 1002843a5..8bf46a66d 100644 --- a/src/braid/ui/views/pages/user.cljs +++ b/src/braid/ui/views/pages/user.cljs @@ -41,6 +41,6 @@ [:p "Currently only showing your open threads that mention this user."] [:p "Soon, you will see all recent threads this user has participated in."] (when (not= @current-user-id (@user :id)) - [call-start-view (@user :id)])]] + [call-start-view @current-user-id (@user :id)])]] [threads-view {:new-thread-args {:mentioned-ids [(@user :id)]} :threads @sorted-threads}]]))) diff --git a/src/chat/client/dispatcher.cljs b/src/chat/client/dispatcher.cljs index 1328b19b0..32dc6243e 100644 --- a/src/chat/client/dispatcher.cljs +++ b/src/chat/client/dispatcher.cljs @@ -270,7 +270,12 @@ (dispatch! :hide-thread {:thread-id id})))) (defmethod dispatch! :start-call [_ call-data] - (sync/chsk-send! [:chat/make-call call-data])) + (let [call (schema/make-call {:type (call-data :type) + :source-id (call-data :source-id) + :target-id (call-data :target-id) + :status "incoming"})] + (store/add-call! call) + (sync/chsk-send! [:chat/make-call call]))) (defmethod dispatch! :accept-call [_ call-id] (store/update-call-status! call-id "accepted")) @@ -367,9 +372,5 @@ (notify/notify {:msg (:content message)})) (defmethod sync/event-handler :chat/receive-call - [[_ [caller-id callee-id call-type]]] - (let [call (schema/make-call {:type call-type - :source-id caller-id - :target-id callee-id - :status "incoming"})] - (store/add-call! call))) + [[_ call]] + (store/add-call! call)) diff --git a/src/chat/server/sync.clj b/src/chat/server/sync.clj index ce0819bea..c3429577a 100644 --- a/src/chat/server/sync.clj +++ b/src/chat/server/sync.clj @@ -373,9 +373,9 @@ (defmethod event-msg-handler :chat/make-call [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] - (let [{:keys [callee-id call-type]} ?data - caller-id (get-in ring-req [:session :user-id])] - (chsk-send! callee-id [:chat/receive-call [caller-id callee-id call-type]]))) + (let [target-id (?data :target-id) + call ?data] + (chsk-send! target-id [:chat/receive-call call]))) (defmethod event-msg-handler :session/start [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] From baafe333d8668eedbf63d97a024dff2446baa910 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Tue, 17 May 2016 13:38:30 -0400 Subject: [PATCH 19/94] Add dropped status for calls --- src/braid/ui/views/call.cljs | 6 ++++-- src/chat/client/dispatcher.cljs | 3 +++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/braid/ui/views/call.cljs b/src/braid/ui/views/call.cljs index ace98919d..6994164a9 100644 --- a/src/braid/ui/views/call.cljs +++ b/src/braid/ui/views/call.cljs @@ -51,14 +51,16 @@ [:a.button {:on-click (fn [_] - (dispatch! :end-call (call :id)))} + (dispatch! :drop-call (call :id)))} "Drop"]]) "accepted" [call-interface-view call] "declined" [:p "declined"] "ended" - [:p "ended"])]))) + [:p "ended"] + "dropped" + [:p "dropped"])]))) (defn call-list-view [] diff --git a/src/chat/client/dispatcher.cljs b/src/chat/client/dispatcher.cljs index 32dc6243e..d3a3b87d9 100644 --- a/src/chat/client/dispatcher.cljs +++ b/src/chat/client/dispatcher.cljs @@ -286,6 +286,9 @@ (defmethod dispatch! :end-call [_ call-id] (store/update-call-status! call-id "ended")) +(defmethod dispatch! :drop-call [_ call-id] + (store/update-call-status! call-id "dropped")) + (defn check-client-version [server-checksum] (when (not= (aget js/window "checksum") server-checksum) (store/display-error! :client-out-of-date "Client out of date - please refresh"))) From 404c87bc1be7c5bc60310ee51b1425c95f3cc3cd Mon Sep 17 00:00:00 2001 From: 10plusY Date: Tue, 17 May 2016 16:04:26 -0400 Subject: [PATCH 20/94] Set up routes to signal other user about changes in call status --- src/braid/ui/views/call.cljs | 9 +++++---- src/chat/client/dispatcher.cljs | 34 ++++++++++++++++++++++----------- src/chat/server/sync.clj | 14 ++++++++++++++ 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/braid/ui/views/call.cljs b/src/braid/ui/views/call.cljs index 6994164a9..0202c3dbb 100644 --- a/src/braid/ui/views/call.cljs +++ b/src/braid/ui/views/call.cljs @@ -24,7 +24,8 @@ [:a.button {:on-click (fn [_] - (dispatch! :end-call (call :id)))} "End Call"]]))) + (dispatch! :end-call call))} + "End Call"]]))) (defn new-call-view [call] @@ -39,19 +40,19 @@ [:a.button {:on-click (fn [_] - (dispatch! :accept-call (call :id)))} + (dispatch! :accept-call call))} "Accept"] [:a.button {:on-click (fn [_] - (dispatch! :decline-call (call :id)))} + (dispatch! :decline-call call))} "Decline"]] [:div [:p (str "Calling " (call :source-id) "...")] [:a.button {:on-click (fn [_] - (dispatch! :drop-call (call :id)))} + (dispatch! :drop-call call))} "Drop"]]) "accepted" [call-interface-view call] diff --git a/src/chat/client/dispatcher.cljs b/src/chat/client/dispatcher.cljs index d3a3b87d9..118e080a8 100644 --- a/src/chat/client/dispatcher.cljs +++ b/src/chat/client/dispatcher.cljs @@ -277,17 +277,25 @@ (store/add-call! call) (sync/chsk-send! [:chat/make-call call]))) -(defmethod dispatch! :accept-call [_ call-id] - (store/update-call-status! call-id "accepted")) - -(defmethod dispatch! :decline-call [_ call-id] - (store/update-call-status! call-id "declined")) - -(defmethod dispatch! :end-call [_ call-id] - (store/update-call-status! call-id "ended")) - -(defmethod dispatch! :drop-call [_ call-id] - (store/update-call-status! call-id "dropped")) +(defmethod dispatch! :accept-call [_ call] + (store/update-call-status! (call :id) "accepted") + (sync/chsk-send! [:chat/change-call-status {:call call + :status "accepted"}])) + +(defmethod dispatch! :decline-call [_ call] + (store/update-call-status! (call :id) "declined") + (sync/chsk-send! [:chat/change-call-status {:call call + :status "declined"}])) + +(defmethod dispatch! :end-call [_ call] + (store/update-call-status! (call :id) "ended") + (sync/chsk-send! [:chat/change-call-status {:call call + :status "ended"}])) + +(defmethod dispatch! :drop-call [_ call] + (store/update-call-status! (call :id) "dropped") + (sync/chsk-send! [:chat/change-call-status {:call call + :status "dropped"}])) (defn check-client-version [server-checksum] (when (not= (aget js/window "checksum") server-checksum) @@ -377,3 +385,7 @@ (defmethod sync/event-handler :chat/receive-call [[_ call]] (store/add-call! call)) + +(defmethod sync/event-handler :chat/new-call-status + [[_ [call-id status]]] + (store/update-call-status! call-id status)) diff --git a/src/chat/server/sync.clj b/src/chat/server/sync.clj index c3429577a..5b5bb8ca2 100644 --- a/src/chat/server/sync.clj +++ b/src/chat/server/sync.clj @@ -93,6 +93,10 @@ (doseq [uid ids-to-send-to] (chsk-send! uid info)))) +(defn signal-call-status-change + [user-id info] + (chsk-send! user-id info)) + (defmethod event-msg-handler :chsk/uidport-open [{:as ev-msg :keys [event id ring-req]}] (when-let [user-id (get-in ring-req [:session :user-id])] @@ -377,6 +381,16 @@ call ?data] (chsk-send! target-id [:chat/receive-call call]))) +(defmethod event-msg-handler :chat/change-call-status + [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] + (let [call (?data :call) + call-id (call :id) + status (?data :status) + signal-id (get-in ring-req [:session :user-id])] + (if (= signal-id (call :target-id)) + (signal-call-status-change (call :source-id) [:chat/new-call-status [call-id status]]) + (signal-call-status-change (call :target-id) [:chat/new-call-status [call-id status]])))) + (defmethod event-msg-handler :session/start [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] (when-let [user-id (get-in ring-req [:session :user-id])] From 0c21696785c570f60e72309add972854f4cac3c0 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Tue, 17 May 2016 17:30:55 -0400 Subject: [PATCH 21/94] Make client and server webrtc namespaces, add dependencies, add lein profile with twilio keys --- project.clj | 1 + src/chat/client/webrtc.cljs | 1 + src/chat/server/webrtc.clj | 4 ++++ 3 files changed, 6 insertions(+) create mode 100644 src/chat/client/webrtc.cljs create mode 100644 src/chat/server/webrtc.clj diff --git a/project.clj b/project.clj index 18a2b226d..7be2376cc 100644 --- a/project.clj +++ b/project.clj @@ -2,6 +2,7 @@ :source-paths ["src"] :dependencies [;server + [org.clojure/data.json "0.2.6"] [org.clojure/clojure "1.7.0"] [javax.servlet/servlet-api "2.5"] [commons-codec "1.10"] diff --git a/src/chat/client/webrtc.cljs b/src/chat/client/webrtc.cljs new file mode 100644 index 000000000..fca4b7d4d --- /dev/null +++ b/src/chat/client/webrtc.cljs @@ -0,0 +1 @@ +(ns chat.client.webrtc) diff --git a/src/chat/server/webrtc.clj b/src/chat/server/webrtc.clj new file mode 100644 index 000000000..505d45398 --- /dev/null +++ b/src/chat/server/webrtc.clj @@ -0,0 +1,4 @@ +(ns chat.server.webrtc + {:require [org.httpkit.client :as http] + [clojure.data.json :as json] + [environ.core :refer [env]]}) From f156e169d9a2862f6d67c7c51ab2c2f7c584d0f9 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Tue, 17 May 2016 21:28:23 -0400 Subject: [PATCH 22/94] Create socket routes to request ice servers for webrtc --- src/braid/ui/views/call.cljs | 44 +++++++++++++++++---------------- src/chat/client/dispatcher.cljs | 9 ++++++- src/chat/client/webrtc.cljs | 2 ++ src/chat/server/sync.clj | 5 ++++ src/chat/server/webrtc.clj | 16 ++++++++++-- 5 files changed, 52 insertions(+), 24 deletions(-) diff --git a/src/braid/ui/views/call.cljs b/src/braid/ui/views/call.cljs index 0202c3dbb..095996d46 100644 --- a/src/braid/ui/views/call.cljs +++ b/src/braid/ui/views/call.cljs @@ -2,6 +2,7 @@ (:require [reagent.core :as r] [reagent.impl.util :refer [extract-props]] [braid.ui.views.pills :refer [user-pill-view]] + [chat.client.webrtc :as rtc] [chat.client.dispatcher :refer [dispatch!]] [chat.client.reagent-adapter :refer [subscribe]])) @@ -34,26 +35,26 @@ [:div (case (call :status) "incoming" - (if (= @user-id (call :target-id)) - [:div - [:p (str "Call from" (call :source-id))] - [:a.button - {:on-click - (fn [_] - (dispatch! :accept-call call))} - "Accept"] - [:a.button - {:on-click - (fn [_] - (dispatch! :decline-call call))} - "Decline"]] - [:div - [:p (str "Calling " (call :source-id) "...")] - [:a.button - {:on-click - (fn [_] - (dispatch! :drop-call call))} - "Drop"]]) + (if (= @user-id (call :target-id)) + [:div + [:p (str "Call from" (call :source-id))] + [:a.button + {:on-click + (fn [_] + (dispatch! :accept-call call))} + "Accept"] + [:a.button + {:on-click + (fn [_] + (dispatch! :decline-call call))} + "Decline"]] + [:div + [:p (str "Calling " (call :source-id) "...")] + [:a.button + {:on-click + (fn [_] + (dispatch! :drop-call call))} + "Drop"]]) "accepted" [call-interface-view call] "declined" @@ -95,6 +96,7 @@ (fn [_] (dispatch! :start-call (assoc {} :type "video" :source-id caller-id - :target-id callee-id)))} + :target-id callee-id)) + (dispatch! :request-ice-servers))} "Video"] [call-list-view]])) diff --git a/src/chat/client/dispatcher.cljs b/src/chat/client/dispatcher.cljs index 118e080a8..d7f68907e 100644 --- a/src/chat/client/dispatcher.cljs +++ b/src/chat/client/dispatcher.cljs @@ -4,6 +4,7 @@ [chat.client.store :as store] [chat.client.sync :as sync] [chat.client.schema :as schema] + [chat.client.webrtc :as rtc] [chat.shared.util :as util] [chat.client.router :as router] [chat.client.xhr :refer [edn-xhr]] @@ -297,6 +298,11 @@ (sync/chsk-send! [:chat/change-call-status {:call call :status "dropped"}])) +(defmethod dispatch! :request-ice-servers [_ _] + (sync/chsk-send! [:rtc/get-ice-servers] 500 + (fn [servers] + (println "SERVERS:" servers)))) + (defn check-client-version [server-checksum] (when (not= (aget js/window "checksum") server-checksum) (store/display-error! :client-out-of-date "Client out of date - please refresh"))) @@ -384,7 +390,8 @@ (defmethod sync/event-handler :chat/receive-call [[_ call]] - (store/add-call! call)) + (store/add-call! call) + (rtc/initialize-rtc-environment)) (defmethod sync/event-handler :chat/new-call-status [[_ [call-id status]]] diff --git a/src/chat/client/webrtc.cljs b/src/chat/client/webrtc.cljs index fca4b7d4d..205f5528a 100644 --- a/src/chat/client/webrtc.cljs +++ b/src/chat/client/webrtc.cljs @@ -1 +1,3 @@ (ns chat.client.webrtc) + +(defn initialize-rtc-environment []) diff --git a/src/chat/server/sync.clj b/src/chat/server/sync.clj index 5b5bb8ca2..ed875c400 100644 --- a/src/chat/server/sync.clj +++ b/src/chat/server/sync.clj @@ -9,6 +9,7 @@ [chat.server.search :as search] [chat.server.invite :as invites] [chat.server.digest :as digest] + [chat.server.webrtc :as rtc] [clojure.set :refer [difference intersection]] [chat.shared.util :refer [valid-nickname? valid-tag-name?]] [chat.server.extensions :refer [handle-thread-change]] @@ -410,6 +411,10 @@ :invitations (db/fetch-invitations-for-user user-id) :tags (db/fetch-tags-for-user user-id)})])))) +(defmethod event-msg-handler :rtc/get-ice-servers + [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] + (?reply-fn (rtc/request-ice-servers))) + (defonce router_ (atom nil)) (defn stop-router! [] (when-let [stop-f @router_] (stop-f))) diff --git a/src/chat/server/webrtc.clj b/src/chat/server/webrtc.clj index 505d45398..a278c60f2 100644 --- a/src/chat/server/webrtc.clj +++ b/src/chat/server/webrtc.clj @@ -1,4 +1,16 @@ (ns chat.server.webrtc - {:require [org.httpkit.client :as http] + (:require [org.httpkit.client :as http] [clojure.data.json :as json] - [environ.core :refer [env]]}) + [environ.core :refer [env]])) + +(defn request-ice-servers [] + (let [response @(http/request + {:url "https://api.twilio.com/2010-04-01/Accounts/AC01773bdc00cc61649a19c84303b85c82/Tokens.json" + :method :post + :basic-auth [(env :twilio-key) + (env :twilio-secret)]}) + ice-servers (-> response + (get :body) + (json/read-str :key-fn keyword) + (get :ice_servers))] + ice-servers)) From 42aa8d75e9ff17454be37b18bb413e5758abbb1c Mon Sep 17 00:00:00 2001 From: 10plusY Date: Wed, 18 May 2016 11:49:31 -0400 Subject: [PATCH 23/94] Add methods to initialize local peer connection --- src/chat/client/dispatcher.cljs | 4 ++-- src/chat/client/webrtc.cljs | 14 +++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/chat/client/dispatcher.cljs b/src/chat/client/dispatcher.cljs index d7f68907e..bb87eee2c 100644 --- a/src/chat/client/dispatcher.cljs +++ b/src/chat/client/dispatcher.cljs @@ -301,7 +301,7 @@ (defmethod dispatch! :request-ice-servers [_ _] (sync/chsk-send! [:rtc/get-ice-servers] 500 (fn [servers] - (println "SERVERS:" servers)))) + (rtc/initialize-rtc-environment servers)))) (defn check-client-version [server-checksum] (when (not= (aget js/window "checksum") server-checksum) @@ -391,7 +391,7 @@ (defmethod sync/event-handler :chat/receive-call [[_ call]] (store/add-call! call) - (rtc/initialize-rtc-environment)) + (dispatch! :request-ice-servers)) (defmethod sync/event-handler :chat/new-call-status [[_ [call-id status]]] diff --git a/src/chat/client/webrtc.cljs b/src/chat/client/webrtc.cljs index 205f5528a..817f897ae 100644 --- a/src/chat/client/webrtc.cljs +++ b/src/chat/client/webrtc.cljs @@ -1,3 +1,15 @@ (ns chat.client.webrtc) -(defn initialize-rtc-environment []) +(def local-peer-connnection (atom nil)) + +(defn create-connection [servers] + (let [connection (js/webkitRTCPeerConnection. (clj->js {:iceServers servers}))] + connection)) + +(defn create-local-connection [servers] + (reset! local-peer-connnection (create-connection servers)) + (js/console.log "Local Connection" @local-peer-connnection)) + +(defn initialize-rtc-environment [servers] + (when servers + (create-local-connection servers))) From 3226eea60e0d8c264a626552b2d4286b851f8c3f Mon Sep 17 00:00:00 2001 From: 10plusY Date: Wed, 18 May 2016 13:16:40 -0400 Subject: [PATCH 24/94] Refactor ice-server request to not not rely on dispatch --- src/braid/ui/views/call.cljs | 5 ++--- src/chat/client/dispatcher.cljs | 11 +++++++++-- src/chat/client/webrtc.cljs | 13 ++++++++++++- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/braid/ui/views/call.cljs b/src/braid/ui/views/call.cljs index 095996d46..65a16a95d 100644 --- a/src/braid/ui/views/call.cljs +++ b/src/braid/ui/views/call.cljs @@ -21,7 +21,7 @@ [:a.button "M"] [:a.button "V"] (when (= (call :type) "video") - [:video]) + [:video {:id "vid"}]) [:a.button {:on-click (fn [_] @@ -96,7 +96,6 @@ (fn [_] (dispatch! :start-call (assoc {} :type "video" :source-id caller-id - :target-id callee-id)) - (dispatch! :request-ice-servers))} + :target-id callee-id)))} "Video"] [call-list-view]])) diff --git a/src/chat/client/dispatcher.cljs b/src/chat/client/dispatcher.cljs index bb87eee2c..77bd18953 100644 --- a/src/chat/client/dispatcher.cljs +++ b/src/chat/client/dispatcher.cljs @@ -28,6 +28,11 @@ (filter store/user-in-open-group?) (remove nil?)))) +(defn- get-ice-servers [] + (sync/chsk-send! [:rtc/get-ice-servers] 500 + (fn [servers] + (rtc/initialize-rtc-environment servers)))) + (defn identify-mentions [content] (-> content @@ -276,7 +281,8 @@ :target-id (call-data :target-id) :status "incoming"})] (store/add-call! call) - (sync/chsk-send! [:chat/make-call call]))) + (sync/chsk-send! [:chat/make-call call]) + (get-ice-servers))) (defmethod dispatch! :accept-call [_ call] (store/update-call-status! (call :id) "accepted") @@ -301,6 +307,7 @@ (defmethod dispatch! :request-ice-servers [_ _] (sync/chsk-send! [:rtc/get-ice-servers] 500 (fn [servers] + (println "SERVERS:" servers) (rtc/initialize-rtc-environment servers)))) (defn check-client-version [server-checksum] @@ -391,7 +398,7 @@ (defmethod sync/event-handler :chat/receive-call [[_ call]] (store/add-call! call) - (dispatch! :request-ice-servers)) + (get-ice-servers)) (defmethod sync/event-handler :chat/new-call-status [[_ [call-id status]]] diff --git a/src/chat/client/webrtc.cljs b/src/chat/client/webrtc.cljs index 817f897ae..1cdcbb8c1 100644 --- a/src/chat/client/webrtc.cljs +++ b/src/chat/client/webrtc.cljs @@ -1,13 +1,24 @@ -(ns chat.client.webrtc) +(ns chat.client.webrtc + (:require [chat.client.sync :as sync])) (def local-peer-connnection (atom nil)) +(defn handle-stream [evt] + ;TODO: how to link up stream in ui + (let [stream (.-stream evt)])) + +(defn handle-ice-candidate [evt] + (let [candidate (.-candidate evt)] + )) + (defn create-connection [servers] (let [connection (js/webkitRTCPeerConnection. (clj->js {:iceServers servers}))] connection)) (defn create-local-connection [servers] (reset! local-peer-connnection (create-connection servers)) + (set! (.-onicecandidate @local-peer-connnection) handle-ice-candidate) + (set! (.-onaddstream @local-peer-connnection) handle-stream) (js/console.log "Local Connection" @local-peer-connnection)) (defn initialize-rtc-environment [servers] From c0227d279e8ce8019b136ece6123a329cf886207 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Wed, 18 May 2016 13:43:14 -0400 Subject: [PATCH 25/94] Add socket route to handle RTC protocol exchange --- src/chat/client/dispatcher.cljs | 4 ++++ src/chat/client/webrtc.cljs | 3 +++ src/chat/server/sync.clj | 7 +++++++ 3 files changed, 14 insertions(+) diff --git a/src/chat/client/dispatcher.cljs b/src/chat/client/dispatcher.cljs index 77bd18953..5f5356c06 100644 --- a/src/chat/client/dispatcher.cljs +++ b/src/chat/client/dispatcher.cljs @@ -403,3 +403,7 @@ (defmethod sync/event-handler :chat/new-call-status [[_ [call-id status]]] (store/update-call-status! call-id status)) + +(defmethod sync/event-handler :rtc/receive-protocol-info + [[_ protocol-data]] + (rtc/handle-protocol-signal protocol-data)) diff --git a/src/chat/client/webrtc.cljs b/src/chat/client/webrtc.cljs index 1cdcbb8c1..3293aa8b1 100644 --- a/src/chat/client/webrtc.cljs +++ b/src/chat/client/webrtc.cljs @@ -3,6 +3,9 @@ (def local-peer-connnection (atom nil)) +(defn handle-protocol-signal [signal] + (println "Signal" signal)) + (defn handle-stream [evt] ;TODO: how to link up stream in ui (let [stream (.-stream evt)])) diff --git a/src/chat/server/sync.clj b/src/chat/server/sync.clj index ed875c400..36eba8f2b 100644 --- a/src/chat/server/sync.clj +++ b/src/chat/server/sync.clj @@ -415,6 +415,13 @@ [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] (?reply-fn (rtc/request-ice-servers))) +(defmethod event-msg-handler :rtc/send-protocol-info + [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] + (let [signal-id (get-in ring-req [:session :user-id])] + signal-data ?data + (doseq [user-id (:any @connected-uids) :when (not= signal-id user-id)] + (chsk-send! user-id [:rtc/receive-protocol-info signal-data])))) + (defonce router_ (atom nil)) (defn stop-router! [] (when-let [stop-f @router_] (stop-f))) From e4cb91b8a1b6cc44b2d5765b41ec6d19693f6f64 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Wed, 18 May 2016 17:16:42 -0400 Subject: [PATCH 26/94] Add sdp handling and method to open user media stream --- src/chat/client/dispatcher.cljs | 3 +- src/chat/client/webrtc.cljs | 51 ++++++++++++++++++++++++++++++--- src/chat/server/sync.clj | 4 +-- 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/src/chat/client/dispatcher.cljs b/src/chat/client/dispatcher.cljs index 5f5356c06..401a913f0 100644 --- a/src/chat/client/dispatcher.cljs +++ b/src/chat/client/dispatcher.cljs @@ -287,7 +287,8 @@ (defmethod dispatch! :accept-call [_ call] (store/update-call-status! (call :id) "accepted") (sync/chsk-send! [:chat/change-call-status {:call call - :status "accepted"}])) + :status "accepted"}]) + (rtc/open-local-stream (call :type))) (defmethod dispatch! :decline-call [_ call] (store/update-call-status! (call :id) "declined") diff --git a/src/chat/client/webrtc.cljs b/src/chat/client/webrtc.cljs index 3293aa8b1..d441c44fa 100644 --- a/src/chat/client/webrtc.cljs +++ b/src/chat/client/webrtc.cljs @@ -1,18 +1,61 @@ (ns chat.client.webrtc (:require [chat.client.sync :as sync])) +(defonce svga-dimensions + (clj->js {:mandatory + {:maxWidth 320 + :maxHeight 180}})) + (def local-peer-connnection (atom nil)) +; Offers and Answers + +(defn signal-session-description [description] + (js/console.log "DESCRIPTION: " description)) + +(defn create-answer [connection] + (.createAnswer @connection + signal-session-description + (fn [error] + (println "Error creating offer description:" (.-message error))))) + +(defn create-offer [connection] + (.createOffer @connection + signal-session-description + (fn [error] + (println "Error creating offer description:" (.-message error))))) + +; Media + +(defn open-local-stream [stream-type] + (let [constraints (atom nil) + stream-success (fn [stream] + (.addStream @local-peer-connnection stream) + (create-offer local-peer-connnection)) + stream-failure (fn [error] + (println "Error opening stream:" (.-message error)))] + (if (= stream-type "audio") + (reset! constraints (clj->js {:audio true :video false})) + (reset! constraints (clj->js {:audio true :video svga-dimensions}))) + (. js/navigator (webkitGetUserMedia @constraints stream-success stream-failure)))) + +; Protocol Exchange + (defn handle-protocol-signal [signal] (println "Signal" signal)) -(defn handle-stream [evt] - ;TODO: how to link up stream in ui - (let [stream (.-stream evt)])) +; RTC Handlers (defn handle-ice-candidate [evt] (let [candidate (.-candidate evt)] - )) + (js/console.log "CANDIDATE: " candidate))) + +(defn handle-stream [evt] + ;TODO: how to link up stream in ui + (let [stream (.-stream evt) + stream-url (.. js/window -URL (createObjectURL stream))])) + +; Connection (defn create-connection [servers] (let [connection (js/webkitRTCPeerConnection. (clj->js {:iceServers servers}))] diff --git a/src/chat/server/sync.clj b/src/chat/server/sync.clj index 36eba8f2b..ce9b41523 100644 --- a/src/chat/server/sync.clj +++ b/src/chat/server/sync.clj @@ -417,8 +417,8 @@ (defmethod event-msg-handler :rtc/send-protocol-info [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] - (let [signal-id (get-in ring-req [:session :user-id])] - signal-data ?data + (let [signal-id (get-in ring-req [:session :user-id]) + signal-data ?data] (doseq [user-id (:any @connected-uids) :when (not= signal-id user-id)] (chsk-send! user-id [:rtc/receive-protocol-info signal-data])))) From 5aa3d438f9796811dbee666255591facfc964c7e Mon Sep 17 00:00:00 2001 From: 10plusY Date: Wed, 18 May 2016 18:26:54 -0400 Subject: [PATCH 27/94] Add reverse signalling for protocol info --- src/chat/client/webrtc.cljs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/chat/client/webrtc.cljs b/src/chat/client/webrtc.cljs index d441c44fa..6d29d69ea 100644 --- a/src/chat/client/webrtc.cljs +++ b/src/chat/client/webrtc.cljs @@ -11,7 +11,10 @@ ; Offers and Answers (defn signal-session-description [description] - (js/console.log "DESCRIPTION: " description)) + (js/console.log "LOCAL DESC: " description) + (.setLocalDescription @local-peer-connnection description) + (sync/chsk-send! [:rtc/send-protocol-info {:sdp (.-sdp description) + :type (.-type description)}])) (defn create-answer [connection] (.createAnswer @connection @@ -42,13 +45,20 @@ ; Protocol Exchange (defn handle-protocol-signal [signal] - (println "Signal" signal)) + (if (signal :sdp) + (println "REMOTE DESC:" signal) + (println "REMOTE CAND:" signal))) ; RTC Handlers (defn handle-ice-candidate [evt] (let [candidate (.-candidate evt)] - (js/console.log "CANDIDATE: " candidate))) + #_(js/console.log "CANDIDATE: " candidate) + (println "LOCAL CAND:") + (when candidate + (sync/chsk-send! [:rtc/send-protocol-info {:candidate (.-candidate candidate) + :sdpMid (.-sdpMid candidate) + :sdpMLineIndex (.-sdpMLineIndex candidate)}])))) (defn handle-stream [evt] ;TODO: how to link up stream in ui From 20ae23febf610e8a9d74fed141aecb9337011e25 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Wed, 18 May 2016 18:54:45 -0400 Subject: [PATCH 28/94] Make different callback for creating answer to not re-trigger signal cycle --- src/chat/client/webrtc.cljs | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/chat/client/webrtc.cljs b/src/chat/client/webrtc.cljs index 6d29d69ea..8f8977183 100644 --- a/src/chat/client/webrtc.cljs +++ b/src/chat/client/webrtc.cljs @@ -10,7 +10,10 @@ ; Offers and Answers -(defn signal-session-description [description] +(defn signal-sdp-answer [description] + (js/console.log "LOCAL ANSWER:" description)) + +(defn signal-sdp-offer [description] (js/console.log "LOCAL DESC: " description) (.setLocalDescription @local-peer-connnection description) (sync/chsk-send! [:rtc/send-protocol-info {:sdp (.-sdp description) @@ -18,13 +21,13 @@ (defn create-answer [connection] (.createAnswer @connection - signal-session-description + signal-sdp-answer (fn [error] (println "Error creating offer description:" (.-message error))))) (defn create-offer [connection] (.createOffer @connection - signal-session-description + signal-sdp-offer (fn [error] (println "Error creating offer description:" (.-message error))))) @@ -46,15 +49,23 @@ (defn handle-protocol-signal [signal] (if (signal :sdp) - (println "REMOTE DESC:" signal) - (println "REMOTE CAND:" signal))) + (let [received-desc (js/RTCSessionDescription. (clj->js {:sdp (signal :sdp) + :type (signal :type)}))] + (js/console.log "REMOTE DESC:" received-desc) + (.setRemoteDescription @local-peer-connnection received-desc)) + (let [received-cand (js/RTCIceCandidate. (clj->js {:candidate (signal :candidate) + :sdpMid (signal :sdpMid) + :sdpMLineIndex (signal :sdpMLineIndex)}))] + (js/console.log "REMOTE CAND:" received-cand) + (.addIceCandidate @local-peer-connnection received-cand) + (create-answer local-peer-connnection)))) ; RTC Handlers (defn handle-ice-candidate [evt] (let [candidate (.-candidate evt)] #_(js/console.log "CANDIDATE: " candidate) - (println "LOCAL CAND:") + (println "LOCAL CAND:" candidate) (when candidate (sync/chsk-send! [:rtc/send-protocol-info {:candidate (.-candidate candidate) :sdpMid (.-sdpMid candidate) @@ -63,7 +74,8 @@ (defn handle-stream [evt] ;TODO: how to link up stream in ui (let [stream (.-stream evt) - stream-url (.. js/window -URL (createObjectURL stream))])) + stream-url (.. js/window -URL (createObjectURL stream))] + (println "onaddstream" stream))) ; Connection From a0637f351e959eb4c969affa7e538cee4ec94e86 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Wed, 18 May 2016 18:57:39 -0400 Subject: [PATCH 29/94] Fire answer in correct situation to create one answer description --- src/chat/client/webrtc.cljs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/chat/client/webrtc.cljs b/src/chat/client/webrtc.cljs index 8f8977183..8c19dc093 100644 --- a/src/chat/client/webrtc.cljs +++ b/src/chat/client/webrtc.cljs @@ -52,13 +52,13 @@ (let [received-desc (js/RTCSessionDescription. (clj->js {:sdp (signal :sdp) :type (signal :type)}))] (js/console.log "REMOTE DESC:" received-desc) - (.setRemoteDescription @local-peer-connnection received-desc)) + (.setRemoteDescription @local-peer-connnection received-desc) + (create-answer local-peer-connnection)) (let [received-cand (js/RTCIceCandidate. (clj->js {:candidate (signal :candidate) :sdpMid (signal :sdpMid) :sdpMLineIndex (signal :sdpMLineIndex)}))] (js/console.log "REMOTE CAND:" received-cand) - (.addIceCandidate @local-peer-connnection received-cand) - (create-answer local-peer-connnection)))) + (.addIceCandidate @local-peer-connnection received-cand)))) ; RTC Handlers From 779da2528b750e3bc311a6f6e2dca4f2acd5af18 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Thu, 19 May 2016 18:27:30 -0400 Subject: [PATCH 30/94] Fixed socket routes/client methods to handle ICE candidates, SDP offers and SDP answers differently --- src/chat/client/dispatcher.cljs | 16 +++++++++++--- src/chat/client/webrtc.cljs | 39 +++++++++++++++++++-------------- src/chat/server/sync.clj | 8 +++++-- 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/src/chat/client/dispatcher.cljs b/src/chat/client/dispatcher.cljs index 401a913f0..d737d0ff2 100644 --- a/src/chat/client/dispatcher.cljs +++ b/src/chat/client/dispatcher.cljs @@ -405,6 +405,16 @@ [[_ [call-id status]]] (store/update-call-status! call-id status)) -(defmethod sync/event-handler :rtc/receive-protocol-info - [[_ protocol-data]] - (rtc/handle-protocol-signal protocol-data)) +(defmethod sync/event-handler :rtc/receive-sdp-offer + [[_ offer]] + (rtc/handle-sdp-offer offer)) + +(defmethod sync/event-handler :rtc/receive-sdp-answer + [[_ answer]] + (rtc/handle-sdp-answer answer)) + +(defmethod sync/event-handler :rtc/receive-ice-candidate + [[_ candidate]] + (rtc/handle-ice-candidate candidate)) + + diff --git a/src/chat/client/webrtc.cljs b/src/chat/client/webrtc.cljs index 8c19dc093..0cadb5685 100644 --- a/src/chat/client/webrtc.cljs +++ b/src/chat/client/webrtc.cljs @@ -11,13 +11,12 @@ ; Offers and Answers (defn signal-sdp-answer [description] - (js/console.log "LOCAL ANSWER:" description)) + (.setLocalDescription @local-peer-connnection description)) (defn signal-sdp-offer [description] - (js/console.log "LOCAL DESC: " description) (.setLocalDescription @local-peer-connnection description) - (sync/chsk-send! [:rtc/send-protocol-info {:sdp (.-sdp description) - :type (.-type description)}])) + (sync/chsk-send! [:rtc/send-protocol-signal {:sdp (.-sdp description) + :type (.-type description)}])) (defn create-answer [connection] (.createAnswer @connection @@ -47,35 +46,43 @@ ; Protocol Exchange +(defn handle-ice-candidate [candidate] + (println "RECEIVED CANDIDATE:" candidate)) + +(defn handle-sdp-answer [answer] + (println "RECEIVED ANSWER:" answer)) + +(defn handle-sdp-offer [offer] + (println "RECEIVED OFFER:" offer)) + (defn handle-protocol-signal [signal] (if (signal :sdp) (let [received-desc (js/RTCSessionDescription. (clj->js {:sdp (signal :sdp) :type (signal :type)}))] - (js/console.log "REMOTE DESC:" received-desc) (.setRemoteDescription @local-peer-connnection received-desc) (create-answer local-peer-connnection)) (let [received-cand (js/RTCIceCandidate. (clj->js {:candidate (signal :candidate) :sdpMid (signal :sdpMid) :sdpMLineIndex (signal :sdpMLineIndex)}))] - (js/console.log "REMOTE CAND:" received-cand) - (.addIceCandidate @local-peer-connnection received-cand)))) + #_(.addIceCandidate @local-peer-connnection received-cand)))) ; RTC Handlers -(defn handle-ice-candidate [evt] +(defn handle-local-ice [evt] (let [candidate (.-candidate evt)] - #_(js/console.log "CANDIDATE: " candidate) - (println "LOCAL CAND:" candidate) (when candidate - (sync/chsk-send! [:rtc/send-protocol-info {:candidate (.-candidate candidate) - :sdpMid (.-sdpMid candidate) - :sdpMLineIndex (.-sdpMLineIndex candidate)}])))) + (sync/chsk-send! [:rtc/send-protocol-signal {:candidate (.-candidate candidate) + :sdpMid (.-sdpMid candidate) + :sdpMLineIndex (.-sdpMLineIndex candidate)}])))) (defn handle-stream [evt] ;TODO: how to link up stream in ui (let [stream (.-stream evt) - stream-url (.. js/window -URL (createObjectURL stream))] - (println "onaddstream" stream))) + stream-url (.. js/window -URL (createObjectURL stream)) + video-player (. js/document (getElementById "vid"))] + (println "video object" video-player) + (set! (.-src video-player) stream-url) + (set! (.-onloadedmetadata video-player) (fn [_] (.play video-player))))) ; Connection @@ -85,7 +92,7 @@ (defn create-local-connection [servers] (reset! local-peer-connnection (create-connection servers)) - (set! (.-onicecandidate @local-peer-connnection) handle-ice-candidate) + (set! (.-onicecandidate @local-peer-connnection) handle-local-ice) (set! (.-onaddstream @local-peer-connnection) handle-stream) (js/console.log "Local Connection" @local-peer-connnection)) diff --git a/src/chat/server/sync.clj b/src/chat/server/sync.clj index ce9b41523..331696b22 100644 --- a/src/chat/server/sync.clj +++ b/src/chat/server/sync.clj @@ -415,12 +415,16 @@ [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] (?reply-fn (rtc/request-ice-servers))) -(defmethod event-msg-handler :rtc/send-protocol-info +(defmethod event-msg-handler :rtc/send-protocol-signal [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] (let [signal-id (get-in ring-req [:session :user-id]) signal-data ?data] (doseq [user-id (:any @connected-uids) :when (not= signal-id user-id)] - (chsk-send! user-id [:rtc/receive-protocol-info signal-data])))) + (if (signal-data :sdp) + (if (= "offer" (signal-data :type)) + (chsk-send! user-id [:rtc/receive-sdp-offer signal-data]) + (chsk-send! user-id [:rtc/receive-sdp-answer signal-data])) + (chsk-send! user-id [:rtc/receive-ice-candidate signal-data]))))) (defonce router_ (atom nil)) From 1979b4189ef012d9171cb71147f6de770b12ef96 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Thu, 19 May 2016 18:52:18 -0400 Subject: [PATCH 31/94] Merge sdp signal methods into one --- src/chat/client/webrtc.cljs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/chat/client/webrtc.cljs b/src/chat/client/webrtc.cljs index 0cadb5685..81573b234 100644 --- a/src/chat/client/webrtc.cljs +++ b/src/chat/client/webrtc.cljs @@ -10,23 +10,20 @@ ; Offers and Answers -(defn signal-sdp-answer [description] - (.setLocalDescription @local-peer-connnection description)) - -(defn signal-sdp-offer [description] +(defn signal-sdp-description [description] (.setLocalDescription @local-peer-connnection description) (sync/chsk-send! [:rtc/send-protocol-signal {:sdp (.-sdp description) :type (.-type description)}])) (defn create-answer [connection] (.createAnswer @connection - signal-sdp-answer + signal-sdp-description (fn [error] (println "Error creating offer description:" (.-message error))))) (defn create-offer [connection] (.createOffer @connection - signal-sdp-offer + signal-sdp-description (fn [error] (println "Error creating offer description:" (.-message error))))) @@ -47,13 +44,17 @@ ; Protocol Exchange (defn handle-ice-candidate [candidate] - (println "RECEIVED CANDIDATE:" candidate)) + #_(println "RECEIVED CANDIDATE:" candidate)) (defn handle-sdp-answer [answer] (println "RECEIVED ANSWER:" answer)) (defn handle-sdp-offer [offer] - (println "RECEIVED OFFER:" offer)) + (println "RECEIVED OFFER:" offer) + (let [remote-offer (js/RTCSessionDescription. (clj->js {:sdp (offer :sdp) + :type (offer :type)}))] + (.setRemoteDescription @local-peer-connnection remote-offer) + (create-answer local-peer-connnection))) (defn handle-protocol-signal [signal] (if (signal :sdp) From 7387ddc452ef7334b6eabdcb5d11c61d89c5b9d5 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Thu, 19 May 2016 19:58:15 -0400 Subject: [PATCH 32/94] Complete signal exchange --- src/chat/client/webrtc.cljs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/chat/client/webrtc.cljs b/src/chat/client/webrtc.cljs index 81573b234..926221fa8 100644 --- a/src/chat/client/webrtc.cljs +++ b/src/chat/client/webrtc.cljs @@ -44,13 +44,20 @@ ; Protocol Exchange (defn handle-ice-candidate [candidate] - #_(println "RECEIVED CANDIDATE:" candidate)) + #_(println "RECEIVED CANDIDATE:" candidate) + (let [candidate (js/RTCIceCandidate. (clj->js {:candidate (candidate :candidate) + :sdpMid (candidate :sdpMid) + :sdpMLineIndex (candidate :sdpMLineIndex)}))] + (.addIceCandidate @local-peer-connnection candidate))) (defn handle-sdp-answer [answer] - (println "RECEIVED ANSWER:" answer)) + #_(println "RECEIVED ANSWER:" answer) + (let [remote-answer (js/RTCSessionDescription. (clj->js {:sdp (answer :sdp) + :type (answer :type)}))] + (.setRemoteDescription @local-peer-connnection remote-answer))) (defn handle-sdp-offer [offer] - (println "RECEIVED OFFER:" offer) + #_(println "RECEIVED OFFER:" offer) (let [remote-offer (js/RTCSessionDescription. (clj->js {:sdp (offer :sdp) :type (offer :type)}))] (.setRemoteDescription @local-peer-connnection remote-offer) From abad354f2f2a8663157049bfd86e1ffb26362e26 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Thu, 19 May 2016 21:37:42 -0400 Subject: [PATCH 33/94] Return to single method to handle protocol exchange --- src/chat/client/dispatcher.cljs | 16 +++--------- src/chat/client/webrtc.cljs | 46 +++++++++------------------------ src/chat/server/sync.clj | 6 +---- 3 files changed, 16 insertions(+), 52 deletions(-) diff --git a/src/chat/client/dispatcher.cljs b/src/chat/client/dispatcher.cljs index d737d0ff2..c936ef42a 100644 --- a/src/chat/client/dispatcher.cljs +++ b/src/chat/client/dispatcher.cljs @@ -405,16 +405,6 @@ [[_ [call-id status]]] (store/update-call-status! call-id status)) -(defmethod sync/event-handler :rtc/receive-sdp-offer - [[_ offer]] - (rtc/handle-sdp-offer offer)) - -(defmethod sync/event-handler :rtc/receive-sdp-answer - [[_ answer]] - (rtc/handle-sdp-answer answer)) - -(defmethod sync/event-handler :rtc/receive-ice-candidate - [[_ candidate]] - (rtc/handle-ice-candidate candidate)) - - +(defmethod sync/event-handler :rtc/receive-protocol-signal + [[_ signal]] + (rtc/handle-protocol-signal signal)) diff --git a/src/chat/client/webrtc.cljs b/src/chat/client/webrtc.cljs index 926221fa8..2ea55133c 100644 --- a/src/chat/client/webrtc.cljs +++ b/src/chat/client/webrtc.cljs @@ -43,40 +43,21 @@ ; Protocol Exchange -(defn handle-ice-candidate [candidate] - #_(println "RECEIVED CANDIDATE:" candidate) - (let [candidate (js/RTCIceCandidate. (clj->js {:candidate (candidate :candidate) - :sdpMid (candidate :sdpMid) - :sdpMLineIndex (candidate :sdpMLineIndex)}))] - (.addIceCandidate @local-peer-connnection candidate))) - -(defn handle-sdp-answer [answer] - #_(println "RECEIVED ANSWER:" answer) - (let [remote-answer (js/RTCSessionDescription. (clj->js {:sdp (answer :sdp) - :type (answer :type)}))] - (.setRemoteDescription @local-peer-connnection remote-answer))) - -(defn handle-sdp-offer [offer] - #_(println "RECEIVED OFFER:" offer) - (let [remote-offer (js/RTCSessionDescription. (clj->js {:sdp (offer :sdp) - :type (offer :type)}))] - (.setRemoteDescription @local-peer-connnection remote-offer) - (create-answer local-peer-connnection))) - (defn handle-protocol-signal [signal] (if (signal :sdp) - (let [received-desc (js/RTCSessionDescription. (clj->js {:sdp (signal :sdp) - :type (signal :type)}))] - (.setRemoteDescription @local-peer-connnection received-desc) - (create-answer local-peer-connnection)) - (let [received-cand (js/RTCIceCandidate. (clj->js {:candidate (signal :candidate) - :sdpMid (signal :sdpMid) - :sdpMLineIndex (signal :sdpMLineIndex)}))] - #_(.addIceCandidate @local-peer-connnection received-cand)))) + (let [remote-description (js/RTCSessionDescription. (clj->js {:sdp (signal :sdp) + :type (signal :type)}))] + (.setRemoteDescription @local-peer-connnection remote-description) + (when (= "offer" (signal :type)) + (create-answer local-peer-connnection))) + (let [remote-candidate (js/RTCIceCandidate. (clj->js {:candidate (signal :candidate) + :sdpMid (signal :sdpMid) + :sdpMLineIndex (signal :sdpMLineIndex)}))] + (.addIceCandidate @local-peer-connnection remote-candidate)))) ; RTC Handlers -(defn handle-local-ice [evt] +(defn handle-ice-candidate [evt] (let [candidate (.-candidate evt)] (when candidate (sync/chsk-send! [:rtc/send-protocol-signal {:candidate (.-candidate candidate) @@ -84,11 +65,9 @@ :sdpMLineIndex (.-sdpMLineIndex candidate)}])))) (defn handle-stream [evt] - ;TODO: how to link up stream in ui (let [stream (.-stream evt) stream-url (.. js/window -URL (createObjectURL stream)) video-player (. js/document (getElementById "vid"))] - (println "video object" video-player) (set! (.-src video-player) stream-url) (set! (.-onloadedmetadata video-player) (fn [_] (.play video-player))))) @@ -100,9 +79,8 @@ (defn create-local-connection [servers] (reset! local-peer-connnection (create-connection servers)) - (set! (.-onicecandidate @local-peer-connnection) handle-local-ice) - (set! (.-onaddstream @local-peer-connnection) handle-stream) - (js/console.log "Local Connection" @local-peer-connnection)) + (set! (.-onicecandidate @local-peer-connnection) handle-ice-candidate) + (set! (.-onaddstream @local-peer-connnection) handle-stream)) (defn initialize-rtc-environment [servers] (when servers diff --git a/src/chat/server/sync.clj b/src/chat/server/sync.clj index 331696b22..5f8346e33 100644 --- a/src/chat/server/sync.clj +++ b/src/chat/server/sync.clj @@ -420,11 +420,7 @@ (let [signal-id (get-in ring-req [:session :user-id]) signal-data ?data] (doseq [user-id (:any @connected-uids) :when (not= signal-id user-id)] - (if (signal-data :sdp) - (if (= "offer" (signal-data :type)) - (chsk-send! user-id [:rtc/receive-sdp-offer signal-data]) - (chsk-send! user-id [:rtc/receive-sdp-answer signal-data])) - (chsk-send! user-id [:rtc/receive-ice-candidate signal-data]))))) + (chsk-send! user-id [:rtc/receive-protocol-signal signal-data])))) (defonce router_ (atom nil)) From 90050a458c90d2ab9a30f3ddb378aa9402832ede Mon Sep 17 00:00:00 2001 From: 10plusY Date: Tue, 2 Aug 2016 15:42:36 -0400 Subject: [PATCH 34/94] Rename client sync namespace --- src/chat/client/webrtc.cljs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/chat/client/webrtc.cljs b/src/chat/client/webrtc.cljs index 2ea55133c..1743ea9b8 100644 --- a/src/chat/client/webrtc.cljs +++ b/src/chat/client/webrtc.cljs @@ -1,5 +1,5 @@ (ns chat.client.webrtc - (:require [chat.client.sync :as sync])) + (:require [braid.client.sync :as sync])) (defonce svga-dimensions (clj->js {:mandatory From 0fc20e7ca45acccf22dee690f262fd13c606af72 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Tue, 2 Aug 2016 17:11:13 -0400 Subject: [PATCH 35/94] Move webrtc namespaces to braid directory --- src/braid/client/webrtc.cljs | 87 ++++++++++++++++++++++++++++++++++++ src/braid/server/webrtc.clj | 16 +++++++ 2 files changed, 103 insertions(+) create mode 100644 src/braid/client/webrtc.cljs create mode 100644 src/braid/server/webrtc.clj diff --git a/src/braid/client/webrtc.cljs b/src/braid/client/webrtc.cljs new file mode 100644 index 000000000..0ac652b32 --- /dev/null +++ b/src/braid/client/webrtc.cljs @@ -0,0 +1,87 @@ +(ns braid.client.webrtc + (:require [braid.client.sync :as sync])) + +(defonce svga-dimensions + (clj->js {:mandatory + {:maxWidth 320 + :maxHeight 180}})) + +(def local-peer-connnection (atom nil)) + +; Offers and Answers + +(defn signal-sdp-description [description] + (.setLocalDescription @local-peer-connnection description) + (sync/chsk-send! [:rtc/send-protocol-signal {:sdp (.-sdp description) + :type (.-type description)}])) + +(defn create-answer [connection] + (.createAnswer @connection + signal-sdp-description + (fn [error] + (println "Error creating offer description:" (.-message error))))) + +(defn create-offer [connection] + (.createOffer @connection + signal-sdp-description + (fn [error] + (println "Error creating offer description:" (.-message error))))) + +; Media + +(defn open-local-stream [stream-type] + (let [constraints (atom nil) + stream-success (fn [stream] + (.addStream @local-peer-connnection stream) + (create-offer local-peer-connnection)) + stream-failure (fn [error] + (println "Error opening stream:" (.-message error)))] + (if (= stream-type "audio") + (reset! constraints (clj->js {:audio true :video false})) + (reset! constraints (clj->js {:audio true :video svga-dimensions}))) + (. js/navigator (webkitGetUserMedia @constraints stream-success stream-failure)))) + +; Protocol Exchange + +(defn handle-protocol-signal [signal] + (if (signal :sdp) + (let [remote-description (js/RTCSessionDescription. (clj->js {:sdp (signal :sdp) + :type (signal :type)}))] + (.setRemoteDescription @local-peer-connnection remote-description) + (when (= "offer" (signal :type)) + (create-answer local-peer-connnection))) + (let [remote-candidate (js/RTCIceCandidate. (clj->js {:candidate (signal :candidate) + :sdpMid (signal :sdpMid) + :sdpMLineIndex (signal :sdpMLineIndex)}))] + (.addIceCandidate @local-peer-connnection remote-candidate)))) + +; RTC Handlers + +(defn handle-ice-candidate [evt] + (let [candidate (.-candidate evt)] + (when candidate + (sync/chsk-send! [:rtc/send-protocol-signal {:candidate (.-candidate candidate) + :sdpMid (.-sdpMid candidate) + :sdpMLineIndex (.-sdpMLineIndex candidate)}])))) + +(defn handle-stream [evt] + (let [stream (.-stream evt) + stream-url (.. js/window -URL (createObjectURL stream)) + video-player (. js/document (getElementById "vid"))] + (set! (.-src video-player) stream-url) + (set! (.-onloadedmetadata video-player) (fn [_] (.play video-player))))) + +; Connection + +(defn create-connection [servers] + (let [connection (js/webkitRTCPeerConnection. (clj->js {:iceServers servers}))] + connection)) + +(defn create-local-connection [servers] + (reset! local-peer-connnection (create-connection servers)) + (set! (.-onicecandidate @local-peer-connnection) handle-ice-candidate) + (set! (.-onaddstream @local-peer-connnection) handle-stream)) + +(defn initialize-rtc-environment [servers] + (when servers + (create-local-connection servers))) diff --git a/src/braid/server/webrtc.clj b/src/braid/server/webrtc.clj new file mode 100644 index 000000000..e062fd042 --- /dev/null +++ b/src/braid/server/webrtc.clj @@ -0,0 +1,16 @@ +(ns braid.server.webrtc + (:require [org.httpkit.client :as http] + [clojure.data.json :as json] + [environ.core :refer [env]])) + +(defn request-ice-servers [] + (let [response @(http/request + {:url "https://api.twilio.com/2010-04-01/Accounts/AC01773bdc00cc61649a19c84303b85c82/Tokens.json" + :method :post + :basic-auth [(env :twilio-key) + (env :twilio-secret)]}) + ice-servers (-> response + (get :body) + (json/read-str :key-fn keyword) + (get :ice_servers))] + ice-servers)) From b9cd8a956f75fb7f5dd22338c50504034d699310 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Tue, 2 Aug 2016 17:12:14 -0400 Subject: [PATCH 36/94] Remove original chat directory --- src/chat/client/webrtc.cljs | 87 ------------------------------------- src/chat/server/webrtc.clj | 16 ------- 2 files changed, 103 deletions(-) delete mode 100644 src/chat/client/webrtc.cljs delete mode 100644 src/chat/server/webrtc.clj diff --git a/src/chat/client/webrtc.cljs b/src/chat/client/webrtc.cljs deleted file mode 100644 index 1743ea9b8..000000000 --- a/src/chat/client/webrtc.cljs +++ /dev/null @@ -1,87 +0,0 @@ -(ns chat.client.webrtc - (:require [braid.client.sync :as sync])) - -(defonce svga-dimensions - (clj->js {:mandatory - {:maxWidth 320 - :maxHeight 180}})) - -(def local-peer-connnection (atom nil)) - -; Offers and Answers - -(defn signal-sdp-description [description] - (.setLocalDescription @local-peer-connnection description) - (sync/chsk-send! [:rtc/send-protocol-signal {:sdp (.-sdp description) - :type (.-type description)}])) - -(defn create-answer [connection] - (.createAnswer @connection - signal-sdp-description - (fn [error] - (println "Error creating offer description:" (.-message error))))) - -(defn create-offer [connection] - (.createOffer @connection - signal-sdp-description - (fn [error] - (println "Error creating offer description:" (.-message error))))) - -; Media - -(defn open-local-stream [stream-type] - (let [constraints (atom nil) - stream-success (fn [stream] - (.addStream @local-peer-connnection stream) - (create-offer local-peer-connnection)) - stream-failure (fn [error] - (println "Error opening stream:" (.-message error)))] - (if (= stream-type "audio") - (reset! constraints (clj->js {:audio true :video false})) - (reset! constraints (clj->js {:audio true :video svga-dimensions}))) - (. js/navigator (webkitGetUserMedia @constraints stream-success stream-failure)))) - -; Protocol Exchange - -(defn handle-protocol-signal [signal] - (if (signal :sdp) - (let [remote-description (js/RTCSessionDescription. (clj->js {:sdp (signal :sdp) - :type (signal :type)}))] - (.setRemoteDescription @local-peer-connnection remote-description) - (when (= "offer" (signal :type)) - (create-answer local-peer-connnection))) - (let [remote-candidate (js/RTCIceCandidate. (clj->js {:candidate (signal :candidate) - :sdpMid (signal :sdpMid) - :sdpMLineIndex (signal :sdpMLineIndex)}))] - (.addIceCandidate @local-peer-connnection remote-candidate)))) - -; RTC Handlers - -(defn handle-ice-candidate [evt] - (let [candidate (.-candidate evt)] - (when candidate - (sync/chsk-send! [:rtc/send-protocol-signal {:candidate (.-candidate candidate) - :sdpMid (.-sdpMid candidate) - :sdpMLineIndex (.-sdpMLineIndex candidate)}])))) - -(defn handle-stream [evt] - (let [stream (.-stream evt) - stream-url (.. js/window -URL (createObjectURL stream)) - video-player (. js/document (getElementById "vid"))] - (set! (.-src video-player) stream-url) - (set! (.-onloadedmetadata video-player) (fn [_] (.play video-player))))) - -; Connection - -(defn create-connection [servers] - (let [connection (js/webkitRTCPeerConnection. (clj->js {:iceServers servers}))] - connection)) - -(defn create-local-connection [servers] - (reset! local-peer-connnection (create-connection servers)) - (set! (.-onicecandidate @local-peer-connnection) handle-ice-candidate) - (set! (.-onaddstream @local-peer-connnection) handle-stream)) - -(defn initialize-rtc-environment [servers] - (when servers - (create-local-connection servers))) diff --git a/src/chat/server/webrtc.clj b/src/chat/server/webrtc.clj deleted file mode 100644 index a278c60f2..000000000 --- a/src/chat/server/webrtc.clj +++ /dev/null @@ -1,16 +0,0 @@ -(ns chat.server.webrtc - (:require [org.httpkit.client :as http] - [clojure.data.json :as json] - [environ.core :refer [env]])) - -(defn request-ice-servers [] - (let [response @(http/request - {:url "https://api.twilio.com/2010-04-01/Accounts/AC01773bdc00cc61649a19c84303b85c82/Tokens.json" - :method :post - :basic-auth [(env :twilio-key) - (env :twilio-secret)]}) - ice-servers (-> response - (get :body) - (json/read-str :key-fn keyword) - (get :ice_servers))] - ice-servers)) From 39e81d267aeb21eaaa1135e18a566ca76fcaa4da Mon Sep 17 00:00:00 2001 From: 10plusY Date: Wed, 3 Aug 2016 18:28:11 -0400 Subject: [PATCH 37/94] Change set call helper to new form --- src/braid/client/state/helpers.cljs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/braid/client/state/helpers.cljs b/src/braid/client/state/helpers.cljs index 9ecad1775..d1d2e0bfe 100644 --- a/src/braid/client/state/helpers.cljs +++ b/src/braid/client/state/helpers.cljs @@ -333,4 +333,4 @@ (update-in state [:calls] #(assoc % (:id call) call))) (defn set-call-status [state call-id status] - (update-in state [:calls call-id :status] (constantly status))) + (assoc-in state [:calls call-id :status] status)) From feee911779f9edd12a3756f90f748e98d354771c Mon Sep 17 00:00:00 2001 From: 10plusY Date: Wed, 3 Aug 2016 18:28:53 -0400 Subject: [PATCH 38/94] Add handlers for adding a call and setting a call status --- src/braid/client/state/handler/impl.cljs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/braid/client/state/handler/impl.cljs b/src/braid/client/state/handler/impl.cljs index ed074c10c..9687a541a 100644 --- a/src/braid/client/state/handler/impl.cljs +++ b/src/braid/client/state/handler/impl.cljs @@ -517,3 +517,9 @@ (defmethod handler :add-group-bot [state [_ [group-id bot]]] (helpers/add-group-bot state group-id bot)) + +(defmethod handler :add-call [state [_ call]] + (helpers/add-call state call)) + +(defmethod handler :set-call-status [state [_ [call-id status]]] + (helpers/set-call-status state call-id status)) From eb5b103048e3819ade15e90aa15dc7b5cf65aa0e Mon Sep 17 00:00:00 2001 From: 10plusY Date: Sun, 7 Aug 2016 20:07:14 -0400 Subject: [PATCH 39/94] Rename call UI namespace --- src/braid/{ => client}/ui/views/call.cljs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) rename src/braid/{ => client}/ui/views/call.cljs (88%) diff --git a/src/braid/ui/views/call.cljs b/src/braid/client/ui/views/call.cljs similarity index 88% rename from src/braid/ui/views/call.cljs rename to src/braid/client/ui/views/call.cljs index 65a16a95d..e7225da0a 100644 --- a/src/braid/ui/views/call.cljs +++ b/src/braid/client/ui/views/call.cljs @@ -1,10 +1,9 @@ -(ns braid.ui.views.call +(ns braid.client.ui.views.call (:require [reagent.core :as r] - [reagent.impl.util :refer [extract-props]] - [braid.ui.views.pills :refer [user-pill-view]] - [chat.client.webrtc :as rtc] - [chat.client.dispatcher :refer [dispatch!]] - [chat.client.reagent-adapter :refer [subscribe]])) + [braid.client.ui.views.pills :refer [user-pill-view]] + [braid.client.webrtc :as rtc] + [braid.client.dispatcher :refer [dispatch!]] + [braid.client.state :refer [subscribe]])) (defn call-interface-view [call] @@ -79,7 +78,7 @@ (defn call-start-view [caller-id callee-id] (fn [] - [:div.call ;TODO: pass user to render pill + [:div.call [:div [:h3 "Call"] [user-pill-view callee-id]] From b00a74c6588e04a01649a4ea4943b873a56f9ce2 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Mon, 8 Aug 2016 13:20:58 -0400 Subject: [PATCH 40/94] Change call schema types to use keywords where possible --- src/braid/client/schema.cljs | 2 +- src/braid/common/schema.cljc | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/braid/client/schema.cljs b/src/braid/client/schema.cljs index b6eabfcf6..86a867f89 100644 --- a/src/braid/client/schema.cljs +++ b/src/braid/client/schema.cljs @@ -35,7 +35,7 @@ :group-id (data :group-id)}) (defn make-call [data] - {:id (or (data :id) (uuid/make-random-squuid)) + {:id (uuid/make-random-squuid) :type (data :type) :source-id (data :source-id) :target-id (data :target-id) diff --git a/src/braid/common/schema.cljc b/src/braid/common/schema.cljc index e3280c9cc..f3754623f 100644 --- a/src/braid/common/schema.cljc +++ b/src/braid/common/schema.cljc @@ -113,10 +113,10 @@ (def Call {:id s/Uuid - :type s/Str + :type (s/enum :audio :video) :source-id s/Uuid :target-id s/Uuid - :status s/Str}) + :status s/Keyword}) (def Upload {:id s/Uuid From 13a4b4879bffe50b8d4f265a2d00d25e6a3f7133 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Mon, 8 Aug 2016 13:36:45 -0400 Subject: [PATCH 41/94] Set initial call schema to have incoming status --- src/braid/client/schema.cljs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/braid/client/schema.cljs b/src/braid/client/schema.cljs index 86a867f89..91739b9d4 100644 --- a/src/braid/client/schema.cljs +++ b/src/braid/client/schema.cljs @@ -39,7 +39,7 @@ :type (data :type) :source-id (data :source-id) :target-id (data :target-id) - :status (data :status)}) + :status :incoming}) (defn make-bot [data] (merge {:id (uuid/make-random-squuid)} From 9970c36412ef8af7b12ac8b4d43180f98325caa0 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Mon, 8 Aug 2016 13:50:17 -0400 Subject: [PATCH 42/94] Move call list view out of call start view into user page --- src/braid/client/ui/views/pages/user.cljs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/braid/client/ui/views/pages/user.cljs b/src/braid/client/ui/views/pages/user.cljs index 88957e808..983f0a409 100644 --- a/src/braid/client/ui/views/pages/user.cljs +++ b/src/braid/client/ui/views/pages/user.cljs @@ -1,6 +1,6 @@ (ns braid.client.ui.views.pages.user (:require [reagent.ratom :include-macros true :refer-macros [reaction]] - [braid.ui.views.call :refer [call-start-view]] + [braid.client.ui.views.call :refer [call-start-view call-list-view]] [braid.client.ui.views.pills :refer [user-pill-view]] [braid.client.ui.views.threads :refer [threads-view]] [braid.client.state :refer [subscribe]])) @@ -43,6 +43,8 @@ [:p "Currently only showing your open threads that mention this user."] [:p "Soon, you will see all recent threads this user has participated in."] (when (not= @current-user-id (@user :id)) - [call-start-view @current-user-id (@user :id)])]] + [:div.calls + [call-start-view @current-user-id (@user :id)] + [call-list-view]])]] [threads-view {:new-thread-args {:mentioned-ids [(@user :id)]} :threads @sorted-threads}]]))) From 66737c9a66691ed29d422745b0bd41e40906c656 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Mon, 8 Aug 2016 13:53:59 -0400 Subject: [PATCH 43/94] Refactor call components --- src/braid/client/ui/views/call.cljs | 112 +++++++++++++++------------- 1 file changed, 59 insertions(+), 53 deletions(-) diff --git a/src/braid/client/ui/views/call.cljs b/src/braid/client/ui/views/call.cljs index e7225da0a..b667409e3 100644 --- a/src/braid/client/ui/views/call.cljs +++ b/src/braid/client/ui/views/call.cljs @@ -5,12 +5,21 @@ [braid.client.dispatcher :refer [dispatch!]] [braid.client.state :refer [subscribe]])) -(defn call-interface-view +(defn dropped-call-view [] + [:p "dropped"]) + +(defn ended-call-view [] + [:p "ended"]) + +(defn declined-call-view [] + [:p "declined"]) + +(defn accepted-call-view [call] (let [call-time (r/atom 0) caller-id (call :source-id) user (subscribe [:user caller-id])] - (fn [] + (fn [call] (js/setTimeout #(swap! call-time inc) 1000) [:div [:h4 "Call with " (@user :nickname) "..."] @@ -27,55 +36,43 @@ (dispatch! :end-call call))} "End Call"]]))) -(defn new-call-view +(defn incoming-call-view [call] + (let [user-id (subscribe [:user-id])] + (fn [call] + (if (= @user-id (call :target-id)) + [:div + [:p (str "Call from" (call :source-id))] + [:a.button + {:on-click + (fn [_] + (dispatch! :accept-call call))} + "Accept"] + [:a.button + {:on-click + (fn [_] + (dispatch! :decline-call call))} + "Decline"]] + [:div + [:p (str "Calling " (call :source-id) "...")] + [:a.button + {:on-click + (fn [_] + (dispatch! :drop-call call))} + "Drop"]])))) + +(defn call-view [call] (let [user-id (subscribe [:user-id])] (fn [call] [:div (case (call :status) - "incoming" - (if (= @user-id (call :target-id)) - [:div - [:p (str "Call from" (call :source-id))] - [:a.button - {:on-click - (fn [_] - (dispatch! :accept-call call))} - "Accept"] - [:a.button - {:on-click - (fn [_] - (dispatch! :decline-call call))} - "Decline"]] - [:div - [:p (str "Calling " (call :source-id) "...")] - [:a.button - {:on-click - (fn [_] - (dispatch! :drop-call call))} - "Drop"]]) - "accepted" - [call-interface-view call] - "declined" - [:p "declined"] - "ended" - [:p "ended"] - "dropped" - [:p "dropped"])]))) + :incoming [incoming-call-view] + :accepted [accepted-call-view call] + :declined [declined-call-view] + :ended [ended-call-view] + :dropped [dropped-call-view])]))) -(defn call-list-view - [] - (let [calls (subscribe [:calls])] - (fn [] - [:div - (when (seq @calls) - [:div.calls - (doall - (for [call @calls] - ^{:key (call :id)} - [new-call-view call]))])]))) - -(defn call-start-view +(defn pre-call-view [caller-id callee-id] (fn [] [:div.call @@ -86,15 +83,24 @@ [:a.button {:on-click (fn [_] - (dispatch! :start-call (assoc {} :type "audio" - :source-id caller-id - :target-id callee-id)))} + (dispatch! :start-call {:type :audio + :source-id caller-id + :target-id callee-id}))} "Audio"] [:a.button {:on-click (fn [_] - (dispatch! :start-call (assoc {} :type "video" - :source-id caller-id - :target-id callee-id)))} - "Video"] - [call-list-view]])) + (dispatch! :start-call {:type :video + :source-id caller-id + :target-id callee-id}))} + "Video"]])) + +(defn call-list-view + [] + (let [calls (subscribe [:calls])] + (fn [] + [:div.calls + (doall + (for [call @calls] + ^{:key (call :id)} + [call-view call]))]))) From ec8347b526d85fc9fbc7a3d960f5721d720da065 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Tue, 9 Aug 2016 15:06:33 -0400 Subject: [PATCH 44/94] Add time stamp to call to order call map --- src/braid/client/schema.cljs | 1 + src/braid/common/schema.cljc | 1 + 2 files changed, 2 insertions(+) diff --git a/src/braid/client/schema.cljs b/src/braid/client/schema.cljs index 91739b9d4..d28dc11dd 100644 --- a/src/braid/client/schema.cljs +++ b/src/braid/client/schema.cljs @@ -36,6 +36,7 @@ (defn make-call [data] {:id (uuid/make-random-squuid) + :created-at (or (data :created-at) (js/Date.)) :type (data :type) :source-id (data :source-id) :target-id (data :target-id) diff --git a/src/braid/common/schema.cljc b/src/braid/common/schema.cljc index f3754623f..d29b7f8fb 100644 --- a/src/braid/common/schema.cljc +++ b/src/braid/common/schema.cljc @@ -113,6 +113,7 @@ (def Call {:id s/Uuid + :created-at s/Inst :type (s/enum :audio :video) :source-id s/Uuid :target-id s/Uuid From 2279eed42a49a92d29cde98ef8ec9c375446bdad Mon Sep 17 00:00:00 2001 From: 10plusY Date: Wed, 10 Aug 2016 00:43:40 -0400 Subject: [PATCH 45/94] Refactor call components --- src/braid/client/ui/views/call.cljs | 156 +++++++++++++++------------- 1 file changed, 83 insertions(+), 73 deletions(-) diff --git a/src/braid/client/ui/views/call.cljs b/src/braid/client/ui/views/call.cljs index b667409e3..8009ea092 100644 --- a/src/braid/client/ui/views/call.cljs +++ b/src/braid/client/ui/views/call.cljs @@ -1,106 +1,116 @@ (ns braid.client.ui.views.call (:require [reagent.core :as r] + [reagent.ratom :include-macros true :refer-macros [reaction]] [braid.client.ui.views.pills :refer [user-pill-view]] [braid.client.webrtc :as rtc] [braid.client.dispatcher :refer [dispatch!]] [braid.client.state :refer [subscribe]])) -(defn dropped-call-view [] - [:p "dropped"]) +;TODO: make sub +(defn is-caller? [user-id call] + (= user-id (call :source-id))) -(defn ended-call-view [] - [:p "ended"]) +(defn ended-call-view + [nickname] + [:p (str "Call with " nickname " ended")]) -(defn declined-call-view [] - [:p "declined"]) +(defn dropped-call-view + [nickname] + [:p (str "Call with " nickname " dropped")]) + +(defn declined-call-view + [call] + (let [current-user-id (subscribe [:user-id]) + caller-nickname (subscribe [:nickname (call :source-id)]) + callee-nickname (subscribe [:nickname (call :target-id)])] + (fn [call] + (if (is-caller? @current-user-id call) + [:p (str @callee-nickname " declined your call")] + [:p (str "Call with " @caller-nickname "declined")])))) (defn accepted-call-view [call] (let [call-time (r/atom 0) - caller-id (call :source-id) - user (subscribe [:user caller-id])] + current-user-id (subscribe [:user-id]) + caller-nickname (subscribe [:nickname (call :source-id)]) + callee-nickname (subscribe [:nickname (call :target-id)])] (fn [call] (js/setTimeout #(swap! call-time inc) 1000) [:div - [:h4 "Call with " (@user :nickname) "..."] + [:h4 (str "Call with " (if (is-caller? @current-user-id call) @callee-nickname @caller-nickname) "...")] [:div (str @call-time)] [:br] [:a.button "A"] [:a.button "M"] [:a.button "V"] - (when (= (call :type) "video") - [:video {:id "vid"}]) - [:a.button - {:on-click - (fn [_] - (dispatch! :end-call call))} - "End Call"]]))) + [:video {:class (if (= (call :type) :video) "video" "audio")}] + [:a.button {:on-click + (fn [_] + (dispatch! :end-call (call :id)))} "End"]]))) -(defn incoming-call-view [call] - (let [user-id (subscribe [:user-id])] +(defn incoming-call-view + [call] + (let [current-user-id (subscribe [:user-id])] (fn [call] - (if (= @user-id (call :target-id)) + (if (= @current-user-id (call :target-id)) + [:div - [:p (str "Call from" (call :source-id))] - [:a.button - {:on-click - (fn [_] - (dispatch! :accept-call call))} - "Accept"] - [:a.button - {:on-click - (fn [_] - (dispatch! :decline-call call))} - "Decline"]] + [:p (str "Call from " (call :source-id))] + [:a.button {:on-click + (fn [_] + (dispatch! :accept-call (call :id)))} "Accept"] + [:a.button {:on-click + (fn [_] + (dispatch! :decline-call (call :id)))} "Decline"]] + [:div - [:p (str "Calling " (call :source-id) "...")] - [:a.button - {:on-click - (fn [_] - (dispatch! :drop-call call))} - "Drop"]])))) + [:p (str "Calling " (call :target-id) "...")] + [:a.button {:on-click + (fn [_] + (dispatch! :drop-call (call :id)))} "Drop"]])))) -(defn call-view +(defn during-call-view [call] - (let [user-id (subscribe [:user-id])] + (let [call-status (subscribe [:call-status (call :id)]) + current-user-id (subscribe [:user-id]) + caller-nickname (subscribe [:nickname (call :source-id)]) + callee-nickname (subscribe [:nickname (call :target-id)]) + nickname (if (is-caller? @current-user-id call) @callee-nickname @caller-nickname)] (fn [call] [:div - (case (call :status) - :incoming [incoming-call-view] + (case @call-status + :incoming [incoming-call-view call] :accepted [accepted-call-view call] - :declined [declined-call-view] - :ended [ended-call-view] - :dropped [dropped-call-view])]))) + :declined [declined-call-view call] + :dropped [dropped-call-view nickname] + :ended [ended-call-view nickname])]))) -(defn pre-call-view - [caller-id callee-id] - (fn [] - [:div.call - [:div - [:h3 "Call"] - [user-pill-view callee-id]] - [:br] - [:a.button - {:on-click - (fn [_] - (dispatch! :start-call {:type :audio - :source-id caller-id - :target-id callee-id}))} - "Audio"] - [:a.button - {:on-click - (fn [_] - (dispatch! :start-call {:type :video - :source-id caller-id - :target-id callee-id}))} - "Video"]])) +(defn before-call-view + [callee-id] + (let [caller-id (subscribe [:user-id])] + (fn [callee-id] + [:div.call + [:div + [:h3 "Call"] + [user-pill-view callee-id]] + [:br] + [:a.button {:on-click + (fn [_] + (dispatch! :start-call {:type :audio + :source-id caller-id + :target-id callee-id}))} + "Audio"] + [:a.button {:on-click + (fn [_] + (dispatch! :start-call {:type :video + :source-id caller-id + :target-id callee-id}))} + "Video"]]))) -(defn call-list-view - [] - (let [calls (subscribe [:calls])] +(defn call-view [] + (let [callee-id (subscribe [:page-id]) + new-call (subscribe [:new-call])] (fn [] - [:div.calls - (doall - (for [call @calls] - ^{:key (call :id)} - [call-view call]))]))) + (if-not @new-call + [before-call-view @callee-id] + [during-call-view @new-call])))) From 624b317ef557281afeab71c7b7ee402e35e64b3e Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 12 Aug 2016 15:01:31 -0400 Subject: [PATCH 46/94] Remove old dispatcher dependencies --- src/braid/client/dispatcher.cljs | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/braid/client/dispatcher.cljs b/src/braid/client/dispatcher.cljs index 2a3a6c19d..775efa78d 100644 --- a/src/braid/client/dispatcher.cljs +++ b/src/braid/client/dispatcher.cljs @@ -1,16 +1,6 @@ (ns braid.client.dispatcher - (:require [clojure.string :as string] - [cljs-uuid-utils.core :as uuid] - [braid.client.webrtc :as rtc] - [braid.client.store :as store] - [braid.client.sync :as sync] - [braid.client.state.handler.core :refer [handler]] - [braid.client.schema :as schema] - [braid.common.util :as util] - [braid.client.router :as router] - [braid.client.routes :as routes] - [braid.client.xhr :refer [edn-xhr]] - [braid.client.desktop.notify :as notify])) + (:require [braid.client.store :as store] + [braid.client.state.handler.core :refer [handler]])) (defn dispatch! ([event args] From 51e1188eb7df0e3645e413dc0daa12fef8cb2be7 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 12 Aug 2016 15:02:11 -0400 Subject: [PATCH 47/94] rename schema party ids from source/target to caller/callee --- src/braid/client/schema.cljs | 4 ++-- src/braid/common/schema.cljc | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/braid/client/schema.cljs b/src/braid/client/schema.cljs index d28dc11dd..ea9de6ee7 100644 --- a/src/braid/client/schema.cljs +++ b/src/braid/client/schema.cljs @@ -38,8 +38,8 @@ {:id (uuid/make-random-squuid) :created-at (or (data :created-at) (js/Date.)) :type (data :type) - :source-id (data :source-id) - :target-id (data :target-id) + :caller-id (data :caller-id) + :callee-id (data :callee-id) :status :incoming}) (defn make-bot [data] diff --git a/src/braid/common/schema.cljc b/src/braid/common/schema.cljc index d29b7f8fb..6dd6d938a 100644 --- a/src/braid/common/schema.cljc +++ b/src/braid/common/schema.cljc @@ -115,8 +115,8 @@ {:id s/Uuid :created-at s/Inst :type (s/enum :audio :video) - :source-id s/Uuid - :target-id s/Uuid + :caller-id s/Uuid + :callee-id s/Uuid :status s/Keyword}) (def Upload From c91ab146a840bb29120371694f0aa470ab9ba0f2 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 12 Aug 2016 15:04:06 -0400 Subject: [PATCH 48/94] Add sync logic to only send new call when request is from caller --- src/braid/server/sync.clj | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/braid/server/sync.clj b/src/braid/server/sync.clj index 54c578db8..9c8ad5c6a 100644 --- a/src/braid/server/sync.clj +++ b/src/braid/server/sync.clj @@ -541,11 +541,12 @@ (doseq [user-id (:any @connected-uids) :when (not= signal-id user-id)] (chsk-send! user-id [:rtc/receive-protocol-signal signal-data])))) -(defmethod event-msg-handler :chat/make-call +(defmethod event-msg-handler :braid.server/make-call [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] - (let [target-id (?data :target-id) - call ?data] - (chsk-send! target-id [:chat/receive-call call]))) + (let [call ?data + source-id (get-in ring-req [:session :user-id])] + (when (not= source-id (call :callee-id)) + (chsk-send! (call :callee-id) [:braid.client/receive-call call])))) (defmethod event-msg-handler :chat/change-call-status [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] From 2c85d1e1c1192bb4ad1ea40e698d0c4cb79cebff Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 12 Aug 2016 15:04:47 -0400 Subject: [PATCH 49/94] Consolidate call components on user page into one call-view component --- src/braid/client/ui/views/pages/user.cljs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/braid/client/ui/views/pages/user.cljs b/src/braid/client/ui/views/pages/user.cljs index 983f0a409..ab6dd08b9 100644 --- a/src/braid/client/ui/views/pages/user.cljs +++ b/src/braid/client/ui/views/pages/user.cljs @@ -1,6 +1,6 @@ (ns braid.client.ui.views.pages.user (:require [reagent.ratom :include-macros true :refer-macros [reaction]] - [braid.client.ui.views.call :refer [call-start-view call-list-view]] + [braid.client.ui.views.call :refer [call-view]] [braid.client.ui.views.pills :refer [user-pill-view]] [braid.client.ui.views.threads :refer [threads-view]] [braid.client.state :refer [subscribe]])) @@ -43,8 +43,6 @@ [:p "Currently only showing your open threads that mention this user."] [:p "Soon, you will see all recent threads this user has participated in."] (when (not= @current-user-id (@user :id)) - [:div.calls - [call-start-view @current-user-id (@user :id)] - [call-list-view]])]] + [call-view])]] [threads-view {:new-thread-args {:mentioned-ids [(@user :id)]} :threads @sorted-threads}]]))) From 38be8b469704d255eb5dff55f9907ae7ca21259c Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 12 Aug 2016 15:05:21 -0400 Subject: [PATCH 50/94] Remove is-caller? helper in call ns in favour of subscription --- src/braid/client/ui/views/call.cljs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/braid/client/ui/views/call.cljs b/src/braid/client/ui/views/call.cljs index 8009ea092..1ebd01587 100644 --- a/src/braid/client/ui/views/call.cljs +++ b/src/braid/client/ui/views/call.cljs @@ -6,10 +6,6 @@ [braid.client.dispatcher :refer [dispatch!]] [braid.client.state :refer [subscribe]])) -;TODO: make sub -(defn is-caller? [user-id call] - (= user-id (call :source-id))) - (defn ended-call-view [nickname] [:p (str "Call with " nickname " ended")]) From f7f464b4f9384223112cd3b2e4070793ca678e01 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 12 Aug 2016 15:05:49 -0400 Subject: [PATCH 51/94] Add subscription to tell if current user initiated call --- src/braid/client/state.cljs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/braid/client/state.cljs b/src/braid/client/state.cljs index 7c2f33684..d7c9bcf11 100644 --- a/src/braid/client/state.cljs +++ b/src/braid/client/state.cljs @@ -304,3 +304,6 @@ (defmethod subscription :call-status [state [_ call-id]] (reaction (get-in @state [:calls call-id :status]))) +(defmethod subscription :current-user-is-caller? + [state [_ caller-id]] + (reaction (= @(subscription state [:user-id]) caller-id))) From 0f1bb801ac19a7502adaeed1710798961329e6e1 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 12 Aug 2016 15:08:53 -0400 Subject: [PATCH 52/94] Change before/during call render logic to use if instead of if-not statement for consistency --- src/braid/client/ui/views/call.cljs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/braid/client/ui/views/call.cljs b/src/braid/client/ui/views/call.cljs index 1ebd01587..9ffdec5c6 100644 --- a/src/braid/client/ui/views/call.cljs +++ b/src/braid/client/ui/views/call.cljs @@ -107,6 +107,6 @@ (let [callee-id (subscribe [:page-id]) new-call (subscribe [:new-call])] (fn [] - (if-not @new-call - [before-call-view @callee-id] - [during-call-view @new-call])))) + (if @new-call + [during-call-view @new-call] + [before-call-view @callee-id])))) From 745115e97e6611d7054f1de17a7cf8d9409b5a8e Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 12 Aug 2016 15:09:47 -0400 Subject: [PATCH 53/94] Rename change-call-status backend socket event --- src/braid/server/sync.clj | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/braid/server/sync.clj b/src/braid/server/sync.clj index 9c8ad5c6a..177671434 100644 --- a/src/braid/server/sync.clj +++ b/src/braid/server/sync.clj @@ -548,15 +548,15 @@ (when (not= source-id (call :callee-id)) (chsk-send! (call :callee-id) [:braid.client/receive-call call])))) -(defmethod event-msg-handler :chat/change-call-status +(defmethod event-msg-handler :braid.server/change-call-status [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] (let [call (?data :call) call-id (call :id) status (?data :status) - signal-id (get-in ring-req [:session :user-id])] - (if (= signal-id (call :target-id)) - (chsk-send! (call :source-id) [:chat/new-call-status [call-id status]]) - (chsk-send! (call :target-id) [:chat/new-call-status [call-id status]])))) + source-id (get-in ring-req [:session :user-id])] + (if (= source-id (call :caller-id)) + (chsk-send! (call :callee-id) [:braid.client/receive-new-call-status [call-id status]]) + (chsk-send! (call :caller-id) [:braid.client/receive-new-call-status [call-id status]])))) (defmethod event-msg-handler :braid.server/start [{:as ev-msg :keys [user-id]}] From 972b9e05360341ad97364aa3886d4ce338c7ceaf Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 12 Aug 2016 15:10:45 -0400 Subject: [PATCH 54/94] Rename webrtc namespaces/socket events to new conventions --- src/braid/server/sync.clj | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/braid/server/sync.clj b/src/braid/server/sync.clj index 177671434..797b729d6 100644 --- a/src/braid/server/sync.clj +++ b/src/braid/server/sync.clj @@ -12,7 +12,7 @@ [braid.server.search :as search] [braid.server.invite :as invites] [braid.server.digest :as digest] - [chat.server.webrtc :as rtc] + [braid.server.webrtc :as rtc] [clojure.set :refer [difference intersection]] [braid.common.util :as util :refer [valid-nickname? valid-tag-name?]] [braid.server.email-digest :as email] @@ -530,11 +530,11 @@ (?reply-fn {:braid/ok (db/uploads-in-group ?data)}) (?reply-fn {:braid/error "Not allowed"})))) -(defmethod event-msg-handler :rtc/get-ice-servers +(defmethod event-msg-handler :braid.server/get-ice-servers [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] (?reply-fn (rtc/request-ice-servers))) -(defmethod event-msg-handler :rtc/send-protocol-signal +(defmethod event-msg-handler :braid.server/send-rtc-protocol-signal [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] (let [signal-id (get-in ring-req [:session :user-id]) signal-data ?data] From 567880c4ebd76951734f3f4ab19a3722a65a6147 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 12 Aug 2016 15:12:05 -0400 Subject: [PATCH 55/94] Refactor call dispatch events to use new state helpers --- src/braid/client/state/handler/impl.cljs | 31 +++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/braid/client/state/handler/impl.cljs b/src/braid/client/state/handler/impl.cljs index 9687a541a..a0809e77d 100644 --- a/src/braid/client/state/handler/impl.cljs +++ b/src/braid/client/state/handler/impl.cljs @@ -518,8 +518,37 @@ (defmethod handler :add-group-bot [state [_ [group-id bot]]] (helpers/add-group-bot state group-id bot)) +(defmethod handler :start-call [state [_ data]] + (let [call (schema/make-call data)] + (sync/chsk-send! [:braid.server/make-call call]) + (helpers/add-call state call) + + #_(get-ice-servers))) + (defmethod handler :add-call [state [_ call]] (helpers/add-call state call)) -(defmethod handler :set-call-status [state [_ [call-id status]]] +(defmethod handler :accept-call [state [_ call]] + (sync/chsk-send! [:braid.server/change-call-status {:call call :status :accepted}]) + (helpers/set-call-status state (call :id) :accepted) + + #_(rtc/open-local-stream (call :type))) + +(defmethod handler :decline-call [state [_ call]] + (sync/chsk-send! [:braid.server/change-call-status {:call call :status :declined}]) + (helpers/set-call-status state (call :id) :declined)) + +(defmethod handler :end-call [state [_ call]] + (sync/chsk-send! [:braid.server/change-call-status {:call call :status :ended}]) + (helpers/set-call-status state (call :id) :ended)) + +(defmethod handler :drop-call [state [_ call]] + (sync/chsk-send! [:braid.server/change-call-status {:call call :status :dropped}]) + (helpers/set-call-status state (call :id) :dropped)) + +(defmethod handler :set-requester-call-status [state [_ [call-id status]]] + (sync/chsk-send! [:braid.server/change-call-status {:call call-id :status status}]) + (helpers/set-call-status state call-id status)) + +(defmethod handler :set-receiver-call-status [state [_ [call-id status]]] (helpers/set-call-status state call-id status)) From ce8fe689bec34977294ed6a72563cd57e850b9d7 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 12 Aug 2016 15:13:33 -0400 Subject: [PATCH 56/94] Rename ice server socket event in ice-server method --- src/braid/client/state/handler/impl.cljs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/braid/client/state/handler/impl.cljs b/src/braid/client/state/handler/impl.cljs index a0809e77d..2649179d8 100644 --- a/src/braid/client/state/handler/impl.cljs +++ b/src/braid/client/state/handler/impl.cljs @@ -50,6 +50,11 @@ "#" (or (store/name->open-tag-id tag-name) tag-name)))))) +(defn- get-ice-servers [] + (sync/chsk-send! [:braid.server/get-ice-servers] 500 + (fn [servers] + (rtc/initialize-rtc-environment servers)))) + (defmethod handler :clear-session [state _] (helpers/clear-session state)) From 4cfd4178f591668a06d5eff90ea0658ee6cfa667 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 12 Aug 2016 15:14:21 -0400 Subject: [PATCH 57/94] Add subscription for new call so call interface can react to most recent call --- src/braid/client/state.cljs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/braid/client/state.cljs b/src/braid/client/state.cljs index d7c9bcf11..40cec6b47 100644 --- a/src/braid/client/state.cljs +++ b/src/braid/client/state.cljs @@ -304,6 +304,15 @@ (defmethod subscription :call-status [state [_ call-id]] (reaction (get-in @state [:calls call-id :status]))) + +(defmethod subscription :new-call + [state _] + (reaction (->> (@state :calls) + vals + (filter (fn [c] (not= :ended (c :status)))) + (sort-by :created-at) + first))) + (defmethod subscription :current-user-is-caller? [state [_ caller-id]] (reaction (= @(subscription state [:user-id]) caller-id))) From 613bcf130d4d8655dc198d48cb0b0087d878aba6 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 12 Aug 2016 15:15:17 -0400 Subject: [PATCH 58/94] Rename rtc protocol exchange event in client webrtc namespace --- src/braid/client/webrtc.cljs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/braid/client/webrtc.cljs b/src/braid/client/webrtc.cljs index 0ac652b32..eaabd5c98 100644 --- a/src/braid/client/webrtc.cljs +++ b/src/braid/client/webrtc.cljs @@ -60,9 +60,9 @@ (defn handle-ice-candidate [evt] (let [candidate (.-candidate evt)] (when candidate - (sync/chsk-send! [:rtc/send-protocol-signal {:candidate (.-candidate candidate) - :sdpMid (.-sdpMid candidate) - :sdpMLineIndex (.-sdpMLineIndex candidate)}])))) + (sync/chsk-send! [:braid.server/send-rtc-protocol-signal {:candidate (.-candidate candidate) + :sdpMid (.-sdpMid candidate) + :sdpMLineIndex (.-sdpMLineIndex candidate)}])))) (defn handle-stream [evt] (let [stream (.-stream evt) From 6dd7655f9b2cb4ff299e59418a635d247c3b2861 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 12 Aug 2016 15:16:31 -0400 Subject: [PATCH 59/94] Indent answer offer methods in client webrtc namespace for readability --- src/braid/client/webrtc.cljs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/braid/client/webrtc.cljs b/src/braid/client/webrtc.cljs index eaabd5c98..518e46872 100644 --- a/src/braid/client/webrtc.cljs +++ b/src/braid/client/webrtc.cljs @@ -16,16 +16,18 @@ :type (.-type description)}])) (defn create-answer [connection] - (.createAnswer @connection - signal-sdp-description - (fn [error] - (println "Error creating offer description:" (.-message error))))) + (.createAnswer + @connection + signal-sdp-description + (fn [error] + (println "Error creating offer description:" (.-message error))))) (defn create-offer [connection] - (.createOffer @connection - signal-sdp-description - (fn [error] - (println "Error creating offer description:" (.-message error))))) + (.createOffer + @connection + signal-sdp-description + (fn [error] + (println "Error creating offer description:" (.-message error))))) ; Media From 4da74befa608410d51103a1b02a0b05439a0c097 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 12 Aug 2016 15:17:14 -0400 Subject: [PATCH 60/94] Rename webrtc protocol exchange ws event in webrtc namespace --- src/braid/client/webrtc.cljs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/braid/client/webrtc.cljs b/src/braid/client/webrtc.cljs index 518e46872..92a1393a5 100644 --- a/src/braid/client/webrtc.cljs +++ b/src/braid/client/webrtc.cljs @@ -12,8 +12,8 @@ (defn signal-sdp-description [description] (.setLocalDescription @local-peer-connnection description) - (sync/chsk-send! [:rtc/send-protocol-signal {:sdp (.-sdp description) - :type (.-type description)}])) + (sync/chsk-send! [:braid.server/send-rtc-protocol-signal {:sdp (.-sdp description) + :type (.-type description)}])) (defn create-answer [connection] (.createAnswer From 622bc9547a9238d655b20fba1b42751a42b998f7 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 12 Aug 2016 15:17:45 -0400 Subject: [PATCH 61/94] Refactor/rename remote webrtc socket events --- src/braid/client/state/remote_handlers.cljs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/braid/client/state/remote_handlers.cljs b/src/braid/client/state/remote_handlers.cljs index 24b22b848..091e03ee0 100644 --- a/src/braid/client/state/remote_handlers.cljs +++ b/src/braid/client/state/remote_handlers.cljs @@ -3,6 +3,7 @@ [braid.client.sync :as sync] [braid.client.router :as router] [braid.client.desktop.notify :as notify] + [braid.client.webrtc :as rtc] [braid.client.dispatcher :refer [dispatch!]])) (defmethod sync/event-handler :braid.client/thread @@ -116,4 +117,14 @@ [[_ thread]] (dispatch! :add-open-thread thread)) +(defmethod sync/event-handler :braid.client/receive-call + [[_ call]] + (dispatch! :add-call call)) +(defmethod sync/event-handler :braid.client/receive-new-call-status + [[_ [call status]]] + (dispatch! :set-receiver-call-status [call status])) + +(defmethod sync/event-handler :braid.client/receive-protocol-signal + [[_ signal]] + (rtc/handle-protocol-signal signal)) From 613bfefb284644e3dc31c678b0f0c769cc19a4fe Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 12 Aug 2016 15:18:23 -0400 Subject: [PATCH 62/94] Move webrtc dependency to impl namespace --- src/braid/client/state/handler/impl.cljs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/braid/client/state/handler/impl.cljs b/src/braid/client/state/handler/impl.cljs index 2649179d8..f3091c3dc 100644 --- a/src/braid/client/state/handler/impl.cljs +++ b/src/braid/client/state/handler/impl.cljs @@ -8,7 +8,8 @@ [braid.client.xhr :refer [edn-xhr]] [braid.client.state.helpers :as helpers] [braid.client.dispatcher :refer [dispatch!]] - [braid.client.state.handler.core :refer [handler]])) + [braid.client.state.handler.core :refer [handler]] + [braid.client.webrtc :as rtc])) (defn extract-tag-ids [text] From 4de12e4b93c08be99f96ad5129f1d7829a212aae Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 12 Aug 2016 15:21:12 -0400 Subject: [PATCH 63/94] Add subscription to tell correct nickname based on who initialized call --- src/braid/client/state.cljs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/braid/client/state.cljs b/src/braid/client/state.cljs index 40cec6b47..81f45cc53 100644 --- a/src/braid/client/state.cljs +++ b/src/braid/client/state.cljs @@ -316,3 +316,10 @@ (defmethod subscription :current-user-is-caller? [state [_ caller-id]] (reaction (= @(subscription state [:user-id]) caller-id))) + +(defmethod subscription :correct-nickname + [state [_ call]] + (let [is-caller? (reaction @(subscription state [:current-user-is-caller? (call :caller-id)])) + caller-nickname (reaction @(subscription state [:nickname (call :caller-id)])) + callee-nickname (reaction @(subscription state [:nickname (call :callee-id)]))] + (reaction (if @is-caller? @callee-nickname @caller-nickname)))) From 48958b60d85bbb425838bcc95123ff7cd397d0d6 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 12 Aug 2016 15:23:04 -0400 Subject: [PATCH 64/94] Refactor call ui namespace to use more subscriptions --- src/braid/client/ui/views/call.cljs | 81 +++++++++++++---------------- 1 file changed, 36 insertions(+), 45 deletions(-) diff --git a/src/braid/client/ui/views/call.cljs b/src/braid/client/ui/views/call.cljs index 9ffdec5c6..d2a2d9292 100644 --- a/src/braid/client/ui/views/call.cljs +++ b/src/braid/client/ui/views/call.cljs @@ -15,25 +15,23 @@ [:p (str "Call with " nickname " dropped")]) (defn declined-call-view - [call] - (let [current-user-id (subscribe [:user-id]) - caller-nickname (subscribe [:nickname (call :source-id)]) - callee-nickname (subscribe [:nickname (call :target-id)])] - (fn [call] - (if (is-caller? @current-user-id call) + [caller-id callee-id] + (let [user-is-caller? (subscribe [:current-user-is-caller? caller-id]) + caller-nickname (subscribe [:nickname caller-id]) + callee-nickname (subscribe [:nickname callee-id])] + (fn [caller-id callee-id] + (if @user-is-caller? [:p (str @callee-nickname " declined your call")] [:p (str "Call with " @caller-nickname "declined")])))) (defn accepted-call-view [call] (let [call-time (r/atom 0) - current-user-id (subscribe [:user-id]) - caller-nickname (subscribe [:nickname (call :source-id)]) - callee-nickname (subscribe [:nickname (call :target-id)])] + correct-nickname (subscribe [:correct-nickname call])] (fn [call] (js/setTimeout #(swap! call-time inc) 1000) [:div - [:h4 (str "Call with " (if (is-caller? @current-user-id call) @callee-nickname @caller-nickname) "...")] + [:h4 (str "Call with " @correct-nickname "...")] [:div (str @call-time)] [:br] [:a.button "A"] @@ -42,66 +40,59 @@ [:video {:class (if (= (call :type) :video) "video" "audio")}] [:a.button {:on-click (fn [_] - (dispatch! :end-call (call :id)))} "End"]]))) + (dispatch! :end-call call))} "End"]]))) (defn incoming-call-view [call] - (let [current-user-id (subscribe [:user-id])] + (let [user-is-caller? (subscribe [:current-user-is-caller? (call :caller-id)]) + caller-nickname (subscribe [:nickname (call :caller-id)]) + callee-nickname (subscribe [:nickname (call :callee-id)])] (fn [call] - (if (= @current-user-id (call :target-id)) - + (if @user-is-caller? [:div - [:p (str "Call from " (call :source-id))] + [:p (str "Calling " @callee-nickname "...")] [:a.button {:on-click (fn [_] - (dispatch! :accept-call (call :id)))} "Accept"] + (dispatch! :drop-call call))} "Drop"]] + [:div + [:p (str "Call from " @caller-nickname)] [:a.button {:on-click (fn [_] - (dispatch! :decline-call (call :id)))} "Decline"]] - - [:div - [:p (str "Calling " (call :target-id) "...")] + (dispatch! :accept-call call))} "Accept"] [:a.button {:on-click (fn [_] - (dispatch! :drop-call (call :id)))} "Drop"]])))) + (dispatch! :decline-call call))} "Decline"]])))) (defn during-call-view [call] (let [call-status (subscribe [:call-status (call :id)]) - current-user-id (subscribe [:user-id]) - caller-nickname (subscribe [:nickname (call :source-id)]) - callee-nickname (subscribe [:nickname (call :target-id)]) - nickname (if (is-caller? @current-user-id call) @callee-nickname @caller-nickname)] + correct-nickname (subscribe [:correct-nickname call])] (fn [call] [:div (case @call-status :incoming [incoming-call-view call] :accepted [accepted-call-view call] - :declined [declined-call-view call] - :dropped [dropped-call-view nickname] - :ended [ended-call-view nickname])]))) + :declined [declined-call-view (call :caller-id) (call :caller-id)] + :dropped [dropped-call-view @correct-nickname] + :ended [ended-call-view @correct-nickname])]))) (defn before-call-view [callee-id] - (let [caller-id (subscribe [:user-id])] + (let [caller-id (subscribe [:user-id]) + callee-nickname (subscribe [:nickname callee-id])] (fn [callee-id] [:div.call - [:div - [:h3 "Call"] - [user-pill-view callee-id]] - [:br] - [:a.button {:on-click - (fn [_] - (dispatch! :start-call {:type :audio - :source-id caller-id - :target-id callee-id}))} - "Audio"] - [:a.button {:on-click - (fn [_] - (dispatch! :start-call {:type :video - :source-id caller-id - :target-id callee-id}))} - "Video"]]))) + [:h3 (str "Call " @callee-nickname)] + [:a.button {:on-click + (fn [_] + (dispatch! :start-call {:type :audio + :caller-id @caller-id + :callee-id callee-id}))} "Audio"] + [:a.button {:on-click + (fn [_] + (dispatch! :start-call {:type :video + :caller-id @caller-id + :callee-id callee-id}))} "Video"]]))) (defn call-view [] (let [callee-id (subscribe [:page-id]) From 2c812e23ffde87425725330b747e6ae65a4100b7 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 12 Aug 2016 19:08:34 -0400 Subject: [PATCH 65/94] Add archived status to call so ended view can render --- src/braid/client/state/handler/impl.cljs | 4 +++ src/braid/client/ui/views/call.cljs | 34 +++++++++++++++--------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/braid/client/state/handler/impl.cljs b/src/braid/client/state/handler/impl.cljs index f3091c3dc..72f282246 100644 --- a/src/braid/client/state/handler/impl.cljs +++ b/src/braid/client/state/handler/impl.cljs @@ -552,6 +552,10 @@ (sync/chsk-send! [:braid.server/change-call-status {:call call :status :dropped}]) (helpers/set-call-status state (call :id) :dropped)) +(defmethod handler :archive-call [state [_ call]] + (sync/chsk-send! [:braid.server/change-call-status {:call call :status :archived}]) + (helpers/set-call-status state (call :id) :archived)) + (defmethod handler :set-requester-call-status [state [_ [call-id status]]] (sync/chsk-send! [:braid.server/change-call-status {:call call-id :status status}]) (helpers/set-call-status state call-id status)) diff --git a/src/braid/client/ui/views/call.cljs b/src/braid/client/ui/views/call.cljs index d2a2d9292..e314def8e 100644 --- a/src/braid/client/ui/views/call.cljs +++ b/src/braid/client/ui/views/call.cljs @@ -7,22 +7,32 @@ [braid.client.state :refer [subscribe]])) (defn ended-call-view - [nickname] - [:p (str "Call with " nickname " ended")]) + [call] + (let [correct-nickname (subscribe [:correct-nickname call])] + (fn [call] + [:div + [:a.button {:on-click (fn [_] (dispatch! :archive-call call))} "X"] + [:p (str "Call with " @correct-nickname " ended")]]))) (defn dropped-call-view - [nickname] - [:p (str "Call with " nickname " dropped")]) + [call] + (let [correct-nickname (subscribe [:correct-nickname call])] + (fn [call] + [:div + [:a.button {:on-click (fn [_] (dispatch! :archive-call call))} "X"] + [:p (str "Call with " @correct-nickname " dropped")]]))) (defn declined-call-view - [caller-id callee-id] - (let [user-is-caller? (subscribe [:current-user-is-caller? caller-id]) - caller-nickname (subscribe [:nickname caller-id]) - callee-nickname (subscribe [:nickname callee-id])] - (fn [caller-id callee-id] - (if @user-is-caller? - [:p (str @callee-nickname " declined your call")] - [:p (str "Call with " @caller-nickname "declined")])))) + [call] + (let [user-is-caller? (subscribe [:current-user-is-caller? (call :caller-id)]) + caller-nickname (subscribe [:nickname (call :caller-id)]) + callee-nickname (subscribe [:nickname (call :callee-id)])] + (fn [call] + [:div + [:a.button {:on-click (fn [_] (dispatch! :archive-call call))} "X"] + (if @user-is-caller? + [:p (str @callee-nickname " declined your call")] + [:p (str "Call with " @caller-nickname "declined")])]))) (defn accepted-call-view [call] From 79c5dfb11a9f053f7a33b05b2fdc9570a6c838ba Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 12 Aug 2016 19:09:06 -0400 Subject: [PATCH 66/94] Change new-call filter to archived --- src/braid/client/state.cljs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/braid/client/state.cljs b/src/braid/client/state.cljs index 81f45cc53..ec520050d 100644 --- a/src/braid/client/state.cljs +++ b/src/braid/client/state.cljs @@ -309,7 +309,7 @@ [state _] (reaction (->> (@state :calls) vals - (filter (fn [c] (not= :ended (c :status)))) + (filter (fn [c] (not= :archived (c :status)))) (sort-by :created-at) first))) From 30f2d4175df2bf6d999adbdcbe177f044bddbf50 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 12 Aug 2016 19:11:08 -0400 Subject: [PATCH 67/94] Switch call status subscription to take in call --- src/braid/client/state.cljs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/braid/client/state.cljs b/src/braid/client/state.cljs index ec520050d..9c3191d2f 100644 --- a/src/braid/client/state.cljs +++ b/src/braid/client/state.cljs @@ -302,8 +302,8 @@ (reaction (vals (@state :calls)))) (defmethod subscription :call-status - [state [_ call-id]] - (reaction (get-in @state [:calls call-id :status]))) + [state [_ call]] + (reaction (get-in @state [:calls (call :id) :status]))) (defmethod subscription :new-call [state _] From a598c96d3dff84589808a29f846f27f0d8160d34 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 12 Aug 2016 19:11:56 -0400 Subject: [PATCH 68/94] Refactor before and during call views to be form 3 so props don't close over --- src/braid/client/ui/views/call.cljs | 64 ++++++++++++++++++----------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/src/braid/client/ui/views/call.cljs b/src/braid/client/ui/views/call.cljs index e314def8e..4002b3a2d 100644 --- a/src/braid/client/ui/views/call.cljs +++ b/src/braid/client/ui/views/call.cljs @@ -75,34 +75,48 @@ (defn during-call-view [call] - (let [call-status (subscribe [:call-status (call :id)]) - correct-nickname (subscribe [:correct-nickname call])] - (fn [call] - [:div - (case @call-status - :incoming [incoming-call-view call] - :accepted [accepted-call-view call] - :declined [declined-call-view (call :caller-id) (call :caller-id)] - :dropped [dropped-call-view @correct-nickname] - :ended [ended-call-view @correct-nickname])]))) + (let [call-atom (r/atom call) + call-status (subscribe [:call-status] [call-atom]) + correct-nickname (subscribe [:correct-nickname] [call-atom])] + (r/create-class + {:display-name "during-call-view" + :component-will-receive-props + (fn [_ [_ new-call]] + (reset! call-atom new-call)) + :reagent-render + (fn [call] + [:div + (case @call-status + :incoming [incoming-call-view call] + :accepted [accepted-call-view call] + :declined [declined-call-view call] + :dropped [dropped-call-view call] + :ended [ended-call-view call])])}))) (defn before-call-view [callee-id] - (let [caller-id (subscribe [:user-id]) - callee-nickname (subscribe [:nickname callee-id])] - (fn [callee-id] - [:div.call - [:h3 (str "Call " @callee-nickname)] - [:a.button {:on-click - (fn [_] - (dispatch! :start-call {:type :audio - :caller-id @caller-id - :callee-id callee-id}))} "Audio"] - [:a.button {:on-click - (fn [_] - (dispatch! :start-call {:type :video - :caller-id @caller-id - :callee-id callee-id}))} "Video"]]))) + (let [callee-id-atom (r/atom callee-id) + caller-id (subscribe [:user-id]) + callee-nickname (subscribe [:nickname] [callee-id-atom])] + (r/create-class + {:display-name "before-call-view" + :component-will-receive-props + (fn [_ [_ new-callee-id]] + (reset! callee-id-atom new-callee-id)) + :reagent-render + (fn [callee-id] + [:div.call + [:h3 (str "Call " @callee-nickname)] + [:a.button {:on-click + (fn [_] + (dispatch! :start-call {:type :audio + :caller-id @caller-id + :callee-id callee-id}))} "Audio"] + [:a.button {:on-click + (fn [_] + (dispatch! :start-call {:type :video + :caller-id @caller-id + :callee-id callee-id}))} "Video"]])}))) (defn call-view [] (let [callee-id (subscribe [:page-id]) From 95080e08d339cc56a3f0843460820663e775b2b0 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 12 Aug 2016 20:21:42 -0400 Subject: [PATCH 69/94] Rename various call methods for clarity --- src/braid/client/state/handler/impl.cljs | 9 ++++----- src/braid/client/state/remote_handlers.cljs | 2 +- src/braid/client/ui/views/call.cljs | 4 ++-- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/braid/client/state/handler/impl.cljs b/src/braid/client/state/handler/impl.cljs index 72f282246..64ced55b4 100644 --- a/src/braid/client/state/handler/impl.cljs +++ b/src/braid/client/state/handler/impl.cljs @@ -524,14 +524,13 @@ (defmethod handler :add-group-bot [state [_ [group-id bot]]] (helpers/add-group-bot state group-id bot)) -(defmethod handler :start-call [state [_ data]] +(defmethod handler :start-new-call [state [_ data]] (let [call (schema/make-call data)] (sync/chsk-send! [:braid.server/make-call call]) - (helpers/add-call state call) + (get-ice-servers) + (helpers/add-call state call))) - #_(get-ice-servers))) - -(defmethod handler :add-call [state [_ call]] +(defmethod handler :add-new-call [state [_ call]] (helpers/add-call state call)) (defmethod handler :accept-call [state [_ call]] diff --git a/src/braid/client/state/remote_handlers.cljs b/src/braid/client/state/remote_handlers.cljs index 091e03ee0..501b6b051 100644 --- a/src/braid/client/state/remote_handlers.cljs +++ b/src/braid/client/state/remote_handlers.cljs @@ -119,7 +119,7 @@ (defmethod sync/event-handler :braid.client/receive-call [[_ call]] - (dispatch! :add-call call)) + (dispatch! :add-new-call call)) (defmethod sync/event-handler :braid.client/receive-new-call-status [[_ [call status]]] diff --git a/src/braid/client/ui/views/call.cljs b/src/braid/client/ui/views/call.cljs index 4002b3a2d..5695e0ea8 100644 --- a/src/braid/client/ui/views/call.cljs +++ b/src/braid/client/ui/views/call.cljs @@ -109,12 +109,12 @@ [:h3 (str "Call " @callee-nickname)] [:a.button {:on-click (fn [_] - (dispatch! :start-call {:type :audio + (dispatch! :start-new-call {:type :audio :caller-id @caller-id :callee-id callee-id}))} "Audio"] [:a.button {:on-click (fn [_] - (dispatch! :start-call {:type :video + (dispatch! :start-new-call {:type :video :caller-id @caller-id :callee-id callee-id}))} "Video"]])}))) From 81266be682a4bca7638a74614a5ec0330152b878 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Tue, 16 Aug 2016 20:01:32 -0400 Subject: [PATCH 70/94] Rename certain call sync events --- src/braid/server/sync.clj | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/braid/server/sync.clj b/src/braid/server/sync.clj index 797b729d6..99ba8e236 100644 --- a/src/braid/server/sync.clj +++ b/src/braid/server/sync.clj @@ -532,7 +532,7 @@ (defmethod event-msg-handler :braid.server/get-ice-servers [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] - (?reply-fn (rtc/request-ice-servers))) + (?reply-fn (rtc/get-ice-servers))) (defmethod event-msg-handler :braid.server/send-rtc-protocol-signal [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] @@ -545,8 +545,8 @@ [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] (let [call ?data source-id (get-in ring-req [:session :user-id])] - (when (not= source-id (call :callee-id)) - (chsk-send! (call :callee-id) [:braid.client/receive-call call])))) + (when (= source-id (call :caller-id)) + (chsk-send! (call :callee-id) [:braid.client/receive-new-call call])))) (defmethod event-msg-handler :braid.server/change-call-status [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] From 87e5509cd85e2047f75f5c9f0c7e6f775ff5a5d1 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Tue, 16 Aug 2016 20:02:39 -0400 Subject: [PATCH 71/94] Add local-connection key to call schema to hold RTCPeerConnection object --- src/braid/client/schema.cljs | 3 ++- src/braid/common/schema.cljc | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/braid/client/schema.cljs b/src/braid/client/schema.cljs index ea9de6ee7..9593e47e8 100644 --- a/src/braid/client/schema.cljs +++ b/src/braid/client/schema.cljs @@ -40,7 +40,8 @@ :type (data :type) :caller-id (data :caller-id) :callee-id (data :callee-id) - :status :incoming}) + :status :incoming + :local-connection (data :local-connection)}) (defn make-bot [data] (merge {:id (uuid/make-random-squuid)} diff --git a/src/braid/common/schema.cljc b/src/braid/common/schema.cljc index 6dd6d938a..07be5e75f 100644 --- a/src/braid/common/schema.cljc +++ b/src/braid/common/schema.cljc @@ -117,7 +117,8 @@ :type (s/enum :audio :video) :caller-id s/Uuid :callee-id s/Uuid - :status s/Keyword}) + :status s/Keyword + :local-connection #?(:cljs js/webkitRTCPeerConnection :clj nil)}) (def Upload {:id s/Uuid From 5ca900028e754ff9f939243d0d77421445142b97 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Tue, 16 Aug 2016 20:03:55 -0400 Subject: [PATCH 72/94] Rename receiving new call dispatch methods --- src/braid/client/state/handler/impl.cljs | 33 ++++++++++++++++----- src/braid/client/state/remote_handlers.cljs | 4 +-- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/braid/client/state/handler/impl.cljs b/src/braid/client/state/handler/impl.cljs index 64ced55b4..95f7a3097 100644 --- a/src/braid/client/state/handler/impl.cljs +++ b/src/braid/client/state/handler/impl.cljs @@ -51,10 +51,7 @@ "#" (or (store/name->open-tag-id tag-name) tag-name)))))) -(defn- get-ice-servers [] - (sync/chsk-send! [:braid.server/get-ice-servers] 500 - (fn [servers] - (rtc/initialize-rtc-environment servers)))) + (defmethod handler :clear-session [state _] (helpers/clear-session state)) @@ -524,15 +521,35 @@ (defmethod handler :add-group-bot [state [_ [group-id bot]]] (helpers/add-group-bot state group-id bot)) +(defn- get-ice-servers [cb] + (sync/chsk-send! [:braid.server/get-ice-servers] 2500 + (fn [servers] + (if (= servers :chsk/timeout) + (println "TIMEOUT") ; TODO TRY AGAIN + (cb servers))))) + (defmethod handler :start-new-call [state [_ data]] - (let [call (schema/make-call data)] - (sync/chsk-send! [:braid.server/make-call call]) - (get-ice-servers) - (helpers/add-call state call))) + (get-ice-servers + (fn [servers] + (let [call (-> data + (assoc :local-connection (rtc/create-local-connection servers)) + (schema/make-call))] + (sync/chsk-send! [:braid.server/make-call (dissoc call :local-connection)]) + (dispatch! :add-new-call call)))) + state) + +(defmethod handler :receive-new-call [state [_ call]] + (get-ice-servers + (fn [servers] + (let [call (assoc call :local-connection (rtc/create-local-connection servers))] + (dispatch! :add-new-call call)))) + state) +;TODO: RENAME (defmethod handler :add-new-call [state [_ call]] (helpers/add-call state call)) +;TODO: CONDENSE TO ONE METHOD (defmethod handler :accept-call [state [_ call]] (sync/chsk-send! [:braid.server/change-call-status {:call call :status :accepted}]) (helpers/set-call-status state (call :id) :accepted) diff --git a/src/braid/client/state/remote_handlers.cljs b/src/braid/client/state/remote_handlers.cljs index 501b6b051..3f1926b48 100644 --- a/src/braid/client/state/remote_handlers.cljs +++ b/src/braid/client/state/remote_handlers.cljs @@ -117,9 +117,9 @@ [[_ thread]] (dispatch! :add-open-thread thread)) -(defmethod sync/event-handler :braid.client/receive-call +(defmethod sync/event-handler :braid.client/receive-new-call [[_ call]] - (dispatch! :add-new-call call)) + (dispatch! :receive-new-call call)) (defmethod sync/event-handler :braid.client/receive-new-call-status [[_ [call status]]] From 47147d10809fe57896e36cfff5f3e58abdad9edd Mon Sep 17 00:00:00 2001 From: 10plusY Date: Tue, 16 Aug 2016 20:04:34 -0400 Subject: [PATCH 73/94] Rename server side ice servers method --- src/braid/server/webrtc.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/braid/server/webrtc.clj b/src/braid/server/webrtc.clj index e062fd042..bc9831cc7 100644 --- a/src/braid/server/webrtc.clj +++ b/src/braid/server/webrtc.clj @@ -3,7 +3,7 @@ [clojure.data.json :as json] [environ.core :refer [env]])) -(defn request-ice-servers [] +(defn get-ice-servers [] (let [response @(http/request {:url "https://api.twilio.com/2010-04-01/Accounts/AC01773bdc00cc61649a19c84303b85c82/Tokens.json" :method :post From 242c8fc452876cb1f35755cc95e55fe1a035b113 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Tue, 16 Aug 2016 21:16:38 -0400 Subject: [PATCH 74/94] Refactor rtc connection and handlers to use aset instead of set! --- src/braid/client/webrtc.cljs | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/src/braid/client/webrtc.cljs b/src/braid/client/webrtc.cljs index 92a1393a5..54fb26a3a 100644 --- a/src/braid/client/webrtc.cljs +++ b/src/braid/client/webrtc.cljs @@ -60,30 +60,24 @@ ; RTC Handlers (defn handle-ice-candidate [evt] - (let [candidate (.-candidate evt)] - (when candidate - (sync/chsk-send! [:braid.server/send-rtc-protocol-signal {:candidate (.-candidate candidate) - :sdpMid (.-sdpMid candidate) - :sdpMLineIndex (.-sdpMLineIndex candidate)}])))) + (let [candidate (aget evt "candidate")] + (sync/chsk-send! + [:braid.server/send-rtc-protocol-signal + {:candidate (aget candidate "candidate") + :sdpMid (aget candidate "sdpMid") + :sdpMLineIndex (aget candidate "sdpMLineIndex")}]))) (defn handle-stream [evt] - (let [stream (.-stream evt) - stream-url (.. js/window -URL (createObjectURL stream)) + (let [stream (aget evt "stream") + stream-url (aset js/window "URL" (.createObjectURL stream)) video-player (. js/document (getElementById "vid"))] - (set! (.-src video-player) stream-url) - (set! (.-onloadedmetadata video-player) (fn [_] (.play video-player))))) + (aset video-player "src" stream-url) + (aset video-player "onloadedmetadata" (fn [_] (.play video-player))))) ; Connection -(defn create-connection [servers] +(defn create-local-connection [servers] (let [connection (js/webkitRTCPeerConnection. (clj->js {:iceServers servers}))] + (aset connection "onicecandidate" handle-ice-candidate) + (aset connection "onaddstream" handle-stream) connection)) - -(defn create-local-connection [servers] - (reset! local-peer-connnection (create-connection servers)) - (set! (.-onicecandidate @local-peer-connnection) handle-ice-candidate) - (set! (.-onaddstream @local-peer-connnection) handle-stream)) - -(defn initialize-rtc-environment [servers] - (when servers - (create-local-connection servers))) From 1d1923d3d5d94050f0160301291c768e6248c4b0 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Wed, 17 Aug 2016 12:36:48 -0400 Subject: [PATCH 75/94] Switch status change dispatch methods to one method --- src/braid/client/state/handler/impl.cljs | 36 +++++------------------- src/braid/client/ui/views/call.cljs | 25 ++++++++-------- 2 files changed, 20 insertions(+), 41 deletions(-) diff --git a/src/braid/client/state/handler/impl.cljs b/src/braid/client/state/handler/impl.cljs index 95f7a3097..a8636e8c3 100644 --- a/src/braid/client/state/handler/impl.cljs +++ b/src/braid/client/state/handler/impl.cljs @@ -545,36 +545,14 @@ (dispatch! :add-new-call call)))) state) -;TODO: RENAME (defmethod handler :add-new-call [state [_ call]] (helpers/add-call state call)) -;TODO: CONDENSE TO ONE METHOD -(defmethod handler :accept-call [state [_ call]] - (sync/chsk-send! [:braid.server/change-call-status {:call call :status :accepted}]) - (helpers/set-call-status state (call :id) :accepted) +(defmethod handler :set-requester-call-status [state [_ [call status]]] + (sync/chsk-send! [:braid.server/change-call-status {:call (dissoc call :local-connection) :status status}]) +;; (when (= status :accepted) +;; (rtc/open-local-stream (call :type))) + (helpers/set-call-status state (call :id) status)) - #_(rtc/open-local-stream (call :type))) - -(defmethod handler :decline-call [state [_ call]] - (sync/chsk-send! [:braid.server/change-call-status {:call call :status :declined}]) - (helpers/set-call-status state (call :id) :declined)) - -(defmethod handler :end-call [state [_ call]] - (sync/chsk-send! [:braid.server/change-call-status {:call call :status :ended}]) - (helpers/set-call-status state (call :id) :ended)) - -(defmethod handler :drop-call [state [_ call]] - (sync/chsk-send! [:braid.server/change-call-status {:call call :status :dropped}]) - (helpers/set-call-status state (call :id) :dropped)) - -(defmethod handler :archive-call [state [_ call]] - (sync/chsk-send! [:braid.server/change-call-status {:call call :status :archived}]) - (helpers/set-call-status state (call :id) :archived)) - -(defmethod handler :set-requester-call-status [state [_ [call-id status]]] - (sync/chsk-send! [:braid.server/change-call-status {:call call-id :status status}]) - (helpers/set-call-status state call-id status)) - -(defmethod handler :set-receiver-call-status [state [_ [call-id status]]] - (helpers/set-call-status state call-id status)) +(defmethod handler :set-receiver-call-status [state [_ [call status]]] + (helpers/set-call-status state (call :id) status)) diff --git a/src/braid/client/ui/views/call.cljs b/src/braid/client/ui/views/call.cljs index 5695e0ea8..201c0ecd7 100644 --- a/src/braid/client/ui/views/call.cljs +++ b/src/braid/client/ui/views/call.cljs @@ -11,7 +11,7 @@ (let [correct-nickname (subscribe [:correct-nickname call])] (fn [call] [:div - [:a.button {:on-click (fn [_] (dispatch! :archive-call call))} "X"] + [:a.button {:on-click (fn [_] (dispatch! :set-requester-call-status [call :archived]))} "X"] [:p (str "Call with " @correct-nickname " ended")]]))) (defn dropped-call-view @@ -19,7 +19,7 @@ (let [correct-nickname (subscribe [:correct-nickname call])] (fn [call] [:div - [:a.button {:on-click (fn [_] (dispatch! :archive-call call))} "X"] + [:a.button {:on-click (fn [_] (dispatch! :set-requester-call-status [call :archived]))} "X"] [:p (str "Call with " @correct-nickname " dropped")]]))) (defn declined-call-view @@ -29,7 +29,7 @@ callee-nickname (subscribe [:nickname (call :callee-id)])] (fn [call] [:div - [:a.button {:on-click (fn [_] (dispatch! :archive-call call))} "X"] + [:a.button {:on-click (fn [_] (dispatch! :set-requester-call-status [call :archived]))} "X"] (if @user-is-caller? [:p (str @callee-nickname " declined your call")] [:p (str "Call with " @caller-nickname "declined")])]))) @@ -47,10 +47,11 @@ [:a.button "A"] [:a.button "M"] [:a.button "V"] - [:video {:class (if (= (call :type) :video) "video" "audio")}] + [:video {:id "video" + :class (if (= (call :type) :video) "video" "audio")}] [:a.button {:on-click (fn [_] - (dispatch! :end-call call))} "End"]]))) + (dispatch! :set-requester-call-status [call :ended]))} "End"]]))) (defn incoming-call-view [call] @@ -63,15 +64,15 @@ [:p (str "Calling " @callee-nickname "...")] [:a.button {:on-click (fn [_] - (dispatch! :drop-call call))} "Drop"]] + (dispatch! :set-requester-call-status [call :dropped]))} "Drop"]] [:div [:p (str "Call from " @caller-nickname)] [:a.button {:on-click (fn [_] - (dispatch! :accept-call call))} "Accept"] + (dispatch! :set-requester-call-status [call :accepted]))} "Accept"] [:a.button {:on-click (fn [_] - (dispatch! :decline-call call))} "Decline"]])))) + (dispatch! :set-requester-call-status [call :declined]))} "Decline"]])))) (defn during-call-view [call] @@ -110,13 +111,13 @@ [:a.button {:on-click (fn [_] (dispatch! :start-new-call {:type :audio - :caller-id @caller-id - :callee-id callee-id}))} "Audio"] + :caller-id @caller-id + :callee-id callee-id}))} "Audio"] [:a.button {:on-click (fn [_] (dispatch! :start-new-call {:type :video - :caller-id @caller-id - :callee-id callee-id}))} "Video"]])}))) + :caller-id @caller-id + :callee-id callee-id}))} "Video"]])}))) (defn call-view [] (let [callee-id (subscribe [:page-id]) From afa7d4eed5c04b7d7511b3a0894148017b3ad8cb Mon Sep 17 00:00:00 2001 From: 10plusY Date: Wed, 17 Aug 2016 12:37:56 -0400 Subject: [PATCH 76/94] Refactor socket rtc events for consistency --- src/braid/server/sync.clj | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/braid/server/sync.clj b/src/braid/server/sync.clj index 99ba8e236..c3af0d4b1 100644 --- a/src/braid/server/sync.clj +++ b/src/braid/server/sync.clj @@ -541,12 +541,10 @@ (doseq [user-id (:any @connected-uids) :when (not= signal-id user-id)] (chsk-send! user-id [:rtc/receive-protocol-signal signal-data])))) -(defmethod event-msg-handler :braid.server/make-call +(defmethod event-msg-handler :braid.server/make-new-call [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] - (let [call ?data - source-id (get-in ring-req [:session :user-id])] - (when (= source-id (call :caller-id)) - (chsk-send! (call :callee-id) [:braid.client/receive-new-call call])))) + (when (= (get-in ring-req [:session :user-id]) (?data :caller-id)) + (chsk-send! (?data :callee-id) [:braid.client/receive-new-call ?data]))) (defmethod event-msg-handler :braid.server/change-call-status [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] @@ -555,8 +553,8 @@ status (?data :status) source-id (get-in ring-req [:session :user-id])] (if (= source-id (call :caller-id)) - (chsk-send! (call :callee-id) [:braid.client/receive-new-call-status [call-id status]]) - (chsk-send! (call :caller-id) [:braid.client/receive-new-call-status [call-id status]])))) + (chsk-send! (call :callee-id) [:braid.client/receive-new-call-status [call status]]) + (chsk-send! (call :caller-id) [:braid.client/receive-new-call-status [call status]])))) (defmethod event-msg-handler :braid.server/start [{:as ev-msg :keys [user-id]}] From 128863144e79277bbceccb7469ea06842d72dcf9 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Wed, 17 Aug 2016 12:46:09 -0400 Subject: [PATCH 77/94] Move get-ice-servers method to client webrtc namespace --- src/braid/client/state/handler/impl.cljs | 13 +++---------- src/braid/client/webrtc.cljs | 7 +++++++ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/braid/client/state/handler/impl.cljs b/src/braid/client/state/handler/impl.cljs index a8636e8c3..2acc6a2ae 100644 --- a/src/braid/client/state/handler/impl.cljs +++ b/src/braid/client/state/handler/impl.cljs @@ -521,25 +521,18 @@ (defmethod handler :add-group-bot [state [_ [group-id bot]]] (helpers/add-group-bot state group-id bot)) -(defn- get-ice-servers [cb] - (sync/chsk-send! [:braid.server/get-ice-servers] 2500 - (fn [servers] - (if (= servers :chsk/timeout) - (println "TIMEOUT") ; TODO TRY AGAIN - (cb servers))))) - (defmethod handler :start-new-call [state [_ data]] - (get-ice-servers + (rtc/get-ice-servers (fn [servers] (let [call (-> data (assoc :local-connection (rtc/create-local-connection servers)) (schema/make-call))] - (sync/chsk-send! [:braid.server/make-call (dissoc call :local-connection)]) + (sync/chsk-send! [:braid.server/make-new-call (dissoc call :local-connection)]) (dispatch! :add-new-call call)))) state) (defmethod handler :receive-new-call [state [_ call]] - (get-ice-servers + (rtc/get-ice-servers (fn [servers] (let [call (assoc call :local-connection (rtc/create-local-connection servers))] (dispatch! :add-new-call call)))) diff --git a/src/braid/client/webrtc.cljs b/src/braid/client/webrtc.cljs index 54fb26a3a..909cad688 100644 --- a/src/braid/client/webrtc.cljs +++ b/src/braid/client/webrtc.cljs @@ -81,3 +81,10 @@ (aset connection "onicecandidate" handle-ice-candidate) (aset connection "onaddstream" handle-stream) connection)) + +(defn get-ice-servers [handler] + (sync/chsk-send! [:braid.server/get-ice-servers] 2500 + (fn [servers] + (if (= servers :chsk/timeout) + (println "TIMEOUT") ; TODO TRY AGAIN + (handler servers))))) From d9222eb7ea3c061d08fbc38befb0a31091bc9ad9 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Wed, 17 Aug 2016 12:46:29 -0400 Subject: [PATCH 78/94] Refactor webrtc methods --- src/braid/client/webrtc.cljs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/braid/client/webrtc.cljs b/src/braid/client/webrtc.cljs index 909cad688..ac8ca4ffe 100644 --- a/src/braid/client/webrtc.cljs +++ b/src/braid/client/webrtc.cljs @@ -47,14 +47,11 @@ (defn handle-protocol-signal [signal] (if (signal :sdp) - (let [remote-description (js/RTCSessionDescription. (clj->js {:sdp (signal :sdp) - :type (signal :type)}))] + (let [remote-description (js/RTCSessionDescription. (clj->js signal))] (.setRemoteDescription @local-peer-connnection remote-description) (when (= "offer" (signal :type)) (create-answer local-peer-connnection))) - (let [remote-candidate (js/RTCIceCandidate. (clj->js {:candidate (signal :candidate) - :sdpMid (signal :sdpMid) - :sdpMLineIndex (signal :sdpMLineIndex)}))] + (let [remote-candidate (js/RTCIceCandidate. (clj->js signal))] (.addIceCandidate @local-peer-connnection remote-candidate)))) ; RTC Handlers @@ -70,7 +67,7 @@ (defn handle-stream [evt] (let [stream (aget evt "stream") stream-url (aset js/window "URL" (.createObjectURL stream)) - video-player (. js/document (getElementById "vid"))] + video-player (. js/document (getElementById "video"))] ;TODO: avoid document selectors (aset video-player "src" stream-url) (aset video-player "onloadedmetadata" (fn [_] (.play video-player))))) From 3ad4642cdd284097a0e3d46b894bbae925847c56 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Wed, 17 Aug 2016 12:46:49 -0400 Subject: [PATCH 79/94] Create new namespaces for webrtc handlers and subscriptions --- src/braid/client/calls/handlers.cljs | 1 + src/braid/client/calls/helpers.cljs | 1 + src/braid/client/calls/subscriptions.cljs | 1 + 3 files changed, 3 insertions(+) create mode 100644 src/braid/client/calls/handlers.cljs create mode 100644 src/braid/client/calls/helpers.cljs create mode 100644 src/braid/client/calls/subscriptions.cljs diff --git a/src/braid/client/calls/handlers.cljs b/src/braid/client/calls/handlers.cljs new file mode 100644 index 000000000..e34e4be0f --- /dev/null +++ b/src/braid/client/calls/handlers.cljs @@ -0,0 +1 @@ +(ns braid.client.calls.handlers) diff --git a/src/braid/client/calls/helpers.cljs b/src/braid/client/calls/helpers.cljs new file mode 100644 index 000000000..956e2f703 --- /dev/null +++ b/src/braid/client/calls/helpers.cljs @@ -0,0 +1 @@ +(ns braid.client.calls.helpers) diff --git a/src/braid/client/calls/subscriptions.cljs b/src/braid/client/calls/subscriptions.cljs new file mode 100644 index 000000000..03fdcefd8 --- /dev/null +++ b/src/braid/client/calls/subscriptions.cljs @@ -0,0 +1 @@ +(ns braid.client.calls.subscriptions) From cfd2a8673a283e86a2d8d24b3a42bcd5132f748a Mon Sep 17 00:00:00 2001 From: 10plusY Date: Thu, 18 Aug 2016 11:27:48 -0400 Subject: [PATCH 80/94] Switch back to holding peer-connection objects in local atoms --- src/braid/client/schema.cljs | 3 +- src/braid/client/state/handler/impl.cljs | 13 ++--- src/braid/client/webrtc.cljs | 73 ++++++++++++------------ src/braid/common/schema.cljc | 3 +- 4 files changed, 42 insertions(+), 50 deletions(-) diff --git a/src/braid/client/schema.cljs b/src/braid/client/schema.cljs index 9593e47e8..ea9de6ee7 100644 --- a/src/braid/client/schema.cljs +++ b/src/braid/client/schema.cljs @@ -40,8 +40,7 @@ :type (data :type) :caller-id (data :caller-id) :callee-id (data :callee-id) - :status :incoming - :local-connection (data :local-connection)}) + :status :incoming}) (defn make-bot [data] (merge {:id (uuid/make-random-squuid)} diff --git a/src/braid/client/state/handler/impl.cljs b/src/braid/client/state/handler/impl.cljs index 2acc6a2ae..8b47747f5 100644 --- a/src/braid/client/state/handler/impl.cljs +++ b/src/braid/client/state/handler/impl.cljs @@ -524,18 +524,17 @@ (defmethod handler :start-new-call [state [_ data]] (rtc/get-ice-servers (fn [servers] - (let [call (-> data - (assoc :local-connection (rtc/create-local-connection servers)) - (schema/make-call))] - (sync/chsk-send! [:braid.server/make-new-call (dissoc call :local-connection)]) + (rtc/create-local-connection servers) + (let [call (schema/make-call data)] + (sync/chsk-send! [:braid.server/make-new-call call]) (dispatch! :add-new-call call)))) state) (defmethod handler :receive-new-call [state [_ call]] (rtc/get-ice-servers (fn [servers] - (let [call (assoc call :local-connection (rtc/create-local-connection servers))] - (dispatch! :add-new-call call)))) + (rtc/create-local-connection servers) + (dispatch! :add-new-call call))) state) (defmethod handler :add-new-call [state [_ call]] @@ -543,8 +542,6 @@ (defmethod handler :set-requester-call-status [state [_ [call status]]] (sync/chsk-send! [:braid.server/change-call-status {:call (dissoc call :local-connection) :status status}]) -;; (when (= status :accepted) -;; (rtc/open-local-stream (call :type))) (helpers/set-call-status state (call :id) status)) (defmethod handler :set-receiver-call-status [state [_ [call status]]] diff --git a/src/braid/client/webrtc.cljs b/src/braid/client/webrtc.cljs index ac8ca4ffe..ec80baa79 100644 --- a/src/braid/client/webrtc.cljs +++ b/src/braid/client/webrtc.cljs @@ -1,5 +1,6 @@ (ns braid.client.webrtc - (:require [braid.client.sync :as sync])) + (:require [braid.client.sync :as sync] + [braid.client.dispatcher :refer [dispatch!]])) (defonce svga-dimensions (clj->js {:mandatory @@ -7,54 +8,51 @@ :maxHeight 180}})) (def local-peer-connnection (atom nil)) +(def remote-peer-connection (atom nil)) -; Offers and Answers +; Offer/Answer + +;TODO: handle somewhere else - can't get connection (defn signal-sdp-description [description] (.setLocalDescription @local-peer-connnection description) - (sync/chsk-send! [:braid.server/send-rtc-protocol-signal {:sdp (.-sdp description) - :type (.-type description)}])) + (sync/chsk-send! + [:braid.server/send-rtc-protocol-signal + {:sdp (aget description "sdp") + :type (aget description "type")}])) (defn create-answer [connection] - (.createAnswer - @connection - signal-sdp-description + (.createAnswer connection signal-sdp-description (fn [error] - (println "Error creating offer description:" (.-message error))))) + (println "Error creating offer description: " (aget error "message"))))) (defn create-offer [connection] - (.createOffer - @connection - signal-sdp-description + (.createOffer connection signal-sdp-description (fn [error] - (println "Error creating offer description:" (.-message error))))) + (println "Error creating offer description: " (aget error "message"))))) + +(defn handle-protocol-signal [signal] + (if (signal :candidate) + (.addIceCandidate @local-peer-connnection (js/RTCIceCandidate. (clj->js signal))) + (do + (.setRemoteDescription @local-peer-connnection (js/RTCSessionDescription. (clj->js signal))) + (when (= "offer" (signal :type)) + (create-answer local-peer-connnection))))) ; Media -(defn open-local-stream [stream-type] - (let [constraints (atom nil) +(defn open-local-stream [call] + (let [local-connection (call :local-connection) stream-success (fn [stream] - (.addStream @local-peer-connnection stream) - (create-offer local-peer-connnection)) + (.addStream local-connection stream) + (dispatch! :create-sdp-offer local-connection)) stream-failure (fn [error] - (println "Error opening stream:" (.-message error)))] - (if (= stream-type "audio") - (reset! constraints (clj->js {:audio true :video false})) - (reset! constraints (clj->js {:audio true :video svga-dimensions}))) - (. js/navigator (webkitGetUserMedia @constraints stream-success stream-failure)))) + (println "Error opening stream: " (aget error "message")))] + (. js/navigator + (webkitGetUserMedia + (clj->js {:audio true :video svga-dimensions}) stream-success stream-failure)))) -; Protocol Exchange - -(defn handle-protocol-signal [signal] - (if (signal :sdp) - (let [remote-description (js/RTCSessionDescription. (clj->js signal))] - (.setRemoteDescription @local-peer-connnection remote-description) - (when (= "offer" (signal :type)) - (create-answer local-peer-connnection))) - (let [remote-candidate (js/RTCIceCandidate. (clj->js signal))] - (.addIceCandidate @local-peer-connnection remote-candidate)))) - -; RTC Handlers +; Setup (defn handle-ice-candidate [evt] (let [candidate (aget evt "candidate")] @@ -67,21 +65,20 @@ (defn handle-stream [evt] (let [stream (aget evt "stream") stream-url (aset js/window "URL" (.createObjectURL stream)) - video-player (. js/document (getElementById "video"))] ;TODO: avoid document selectors + video-player (. js/document (getElementById "video"))] (aset video-player "src" stream-url) (aset video-player "onloadedmetadata" (fn [_] (.play video-player))))) -; Connection - (defn create-local-connection [servers] (let [connection (js/webkitRTCPeerConnection. (clj->js {:iceServers servers}))] (aset connection "onicecandidate" handle-ice-candidate) (aset connection "onaddstream" handle-stream) - connection)) + (reset! local-peer-connnection connection) + (js/console.log @local-peer-connnection))) (defn get-ice-servers [handler] (sync/chsk-send! [:braid.server/get-ice-servers] 2500 (fn [servers] (if (= servers :chsk/timeout) - (println "TIMEOUT") ; TODO TRY AGAIN + (get-ice-servers handler) ; TODO TRY AGAIN (handler servers))))) diff --git a/src/braid/common/schema.cljc b/src/braid/common/schema.cljc index 07be5e75f..6dd6d938a 100644 --- a/src/braid/common/schema.cljc +++ b/src/braid/common/schema.cljc @@ -117,8 +117,7 @@ :type (s/enum :audio :video) :caller-id s/Uuid :callee-id s/Uuid - :status s/Keyword - :local-connection #?(:cljs js/webkitRTCPeerConnection :clj nil)}) + :status s/Keyword}) (def Upload {:id s/Uuid From d83b879444ff36f167f23fb3c395bb13dbc27872 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Thu, 18 Aug 2016 14:35:31 -0400 Subject: [PATCH 81/94] Implement one-way webrtc flow --- src/braid/client/state/handler/impl.cljs | 4 +- src/braid/client/state/remote_handlers.cljs | 2 +- src/braid/client/ui/views/call.cljs | 2 +- src/braid/client/webrtc.cljs | 49 +++++++++------------ src/braid/server/sync.clj | 4 +- 5 files changed, 29 insertions(+), 32 deletions(-) diff --git a/src/braid/client/state/handler/impl.cljs b/src/braid/client/state/handler/impl.cljs index 8b47747f5..e97cd2e6b 100644 --- a/src/braid/client/state/handler/impl.cljs +++ b/src/braid/client/state/handler/impl.cljs @@ -541,7 +541,9 @@ (helpers/add-call state call)) (defmethod handler :set-requester-call-status [state [_ [call status]]] - (sync/chsk-send! [:braid.server/change-call-status {:call (dissoc call :local-connection) :status status}]) + (when (= status :accepted) + (rtc/open-local-stream)) + (sync/chsk-send! [:braid.server/change-call-status {:call call :status status}]) (helpers/set-call-status state (call :id) status)) (defmethod handler :set-receiver-call-status [state [_ [call status]]] diff --git a/src/braid/client/state/remote_handlers.cljs b/src/braid/client/state/remote_handlers.cljs index 3f1926b48..5e2862c1e 100644 --- a/src/braid/client/state/remote_handlers.cljs +++ b/src/braid/client/state/remote_handlers.cljs @@ -127,4 +127,4 @@ (defmethod sync/event-handler :braid.client/receive-protocol-signal [[_ signal]] - (rtc/handle-protocol-signal signal)) + (rtc/receive-protocol-signal signal)) diff --git a/src/braid/client/ui/views/call.cljs b/src/braid/client/ui/views/call.cljs index 201c0ecd7..3fce4bc1d 100644 --- a/src/braid/client/ui/views/call.cljs +++ b/src/braid/client/ui/views/call.cljs @@ -47,7 +47,7 @@ [:a.button "A"] [:a.button "M"] [:a.button "V"] - [:video {:id "video" + [:video {:id "vid" :class (if (= (call :type) :video) "video" "audio")}] [:a.button {:on-click (fn [_] diff --git a/src/braid/client/webrtc.cljs b/src/braid/client/webrtc.cljs index ec80baa79..ee1d5672e 100644 --- a/src/braid/client/webrtc.cljs +++ b/src/braid/client/webrtc.cljs @@ -3,35 +3,31 @@ [braid.client.dispatcher :refer [dispatch!]])) (defonce svga-dimensions - (clj->js {:mandatory - {:maxWidth 320 - :maxHeight 180}})) + (clj->js {:mandatory {:maxWidth 320 :maxHeight 180}})) (def local-peer-connnection (atom nil)) (def remote-peer-connection (atom nil)) -; Offer/Answer - -;TODO: handle somewhere else - can't get connection +; Protocols (defn signal-sdp-description [description] (.setLocalDescription @local-peer-connnection description) (sync/chsk-send! - [:braid.server/send-rtc-protocol-signal + [:braid.server/send-protocol-signal {:sdp (aget description "sdp") :type (aget description "type")}])) (defn create-answer [connection] - (.createAnswer connection signal-sdp-description + (.createAnswer @connection signal-sdp-description (fn [error] (println "Error creating offer description: " (aget error "message"))))) (defn create-offer [connection] - (.createOffer connection signal-sdp-description + (.createOffer @connection signal-sdp-description (fn [error] (println "Error creating offer description: " (aget error "message"))))) -(defn handle-protocol-signal [signal] +(defn receive-protocol-signal [signal] (if (signal :candidate) (.addIceCandidate @local-peer-connnection (js/RTCIceCandidate. (clj->js signal))) (do @@ -41,13 +37,12 @@ ; Media -(defn open-local-stream [call] - (let [local-connection (call :local-connection) - stream-success (fn [stream] - (.addStream local-connection stream) - (dispatch! :create-sdp-offer local-connection)) - stream-failure (fn [error] - (println "Error opening stream: " (aget error "message")))] +(defn open-local-stream [] + (letfn [(stream-success [stream] + (.addStream @local-peer-connnection stream) + (create-offer local-peer-connnection)) + (stream-failure [error] + (println "Error opening stream: " (aget error "message")))] (. js/navigator (webkitGetUserMedia (clj->js {:audio true :video svga-dimensions}) stream-success stream-failure)))) @@ -56,16 +51,17 @@ (defn handle-ice-candidate [evt] (let [candidate (aget evt "candidate")] - (sync/chsk-send! - [:braid.server/send-rtc-protocol-signal - {:candidate (aget candidate "candidate") - :sdpMid (aget candidate "sdpMid") - :sdpMLineIndex (aget candidate "sdpMLineIndex")}]))) + (when candidate + (sync/chsk-send! + [:braid.server/send-protocol-signal + {:candidate (aget candidate "candidate") + :sdpMid (aget candidate "sdpMid") + :sdpMLineIndex (aget candidate "sdpMLineIndex")}])))) (defn handle-stream [evt] (let [stream (aget evt "stream") - stream-url (aset js/window "URL" (.createObjectURL stream)) - video-player (. js/document (getElementById "video"))] + stream-url (.. js/window -URL (createObjectURL stream)) + video-player (. js/document (getElementById "vid"))] (aset video-player "src" stream-url) (aset video-player "onloadedmetadata" (fn [_] (.play video-player))))) @@ -73,12 +69,11 @@ (let [connection (js/webkitRTCPeerConnection. (clj->js {:iceServers servers}))] (aset connection "onicecandidate" handle-ice-candidate) (aset connection "onaddstream" handle-stream) - (reset! local-peer-connnection connection) - (js/console.log @local-peer-connnection))) + (reset! local-peer-connnection connection))) (defn get-ice-servers [handler] (sync/chsk-send! [:braid.server/get-ice-servers] 2500 (fn [servers] (if (= servers :chsk/timeout) - (get-ice-servers handler) ; TODO TRY AGAIN + (get-ice-servers handler) (handler servers))))) diff --git a/src/braid/server/sync.clj b/src/braid/server/sync.clj index c3af0d4b1..422d843d4 100644 --- a/src/braid/server/sync.clj +++ b/src/braid/server/sync.clj @@ -534,12 +534,12 @@ [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] (?reply-fn (rtc/get-ice-servers))) -(defmethod event-msg-handler :braid.server/send-rtc-protocol-signal +(defmethod event-msg-handler :braid.server/send-protocol-signal [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] (let [signal-id (get-in ring-req [:session :user-id]) signal-data ?data] (doseq [user-id (:any @connected-uids) :when (not= signal-id user-id)] - (chsk-send! user-id [:rtc/receive-protocol-signal signal-data])))) + (chsk-send! user-id [:braid.client/receive-protocol-signal signal-data])))) (defmethod event-msg-handler :braid.server/make-new-call [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] From eb18c0ab9204a0a9a011976e67f977e64eda5009 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Thu, 18 Aug 2016 14:36:29 -0400 Subject: [PATCH 82/94] Remove old dispatch events --- src/braid/client/dispatcher.cljs | 530 ------------------------------- 1 file changed, 530 deletions(-) diff --git a/src/braid/client/dispatcher.cljs b/src/braid/client/dispatcher.cljs index 775efa78d..ea02041c7 100644 --- a/src/braid/client/dispatcher.cljs +++ b/src/braid/client/dispatcher.cljs @@ -8,533 +8,3 @@ (store/transact! (handler @store/app-state [event args]))) ([event] (dispatch! event nil))) - -(defn extract-tag-ids - [text] - (let [mentioned-names (->> (re-seq util/sigiled-tag-name-re text) - (map second))] - (->> mentioned-names - (map store/name->open-tag-id) - (remove nil?)))) - -(defn extract-user-ids - [text] - (let [mentioned-names (->> (re-seq util/sigiled-nickname-re text) - (map second)) - nick->id (reduce (fn [m {:keys [id nickname]}] (assoc m nickname id)) - {} - (store/all-users))] - (->> mentioned-names - (map nick->id) - (filter store/user-in-open-group?) - (remove nil?)))) - -(defn- get-ice-servers [] - (sync/chsk-send! [:rtc/get-ice-servers] 500 - (fn [servers] - (rtc/initialize-rtc-environment servers)))) - -(defn identify-mentions - [content] - (-> content - (string/replace util/sigiled-nickname-re - (fn [[m nick]] - ; sometimes need leading whitespace, because javascript regex doesn't have lookbehind - (str (second (re-matches #"^(\s).*" m)) - "@" - (if-let [user-id (:id (store/nickname->user nick))] - (if (store/user-in-open-group? user-id) - user-id - nick) - nick)))) - (string/replace util/sigiled-tag-name-re - (fn [[m tag-name]] - ; sometimes need leading whitespace, because javascript regex doesn't have lookbehind - (str (second (re-matches #"^(\s).*" m)) - "#" (or (store/name->open-tag-id tag-name) - tag-name)))))) - -(defmulti dispatch! (fn [event data] event)) - -(defmethod dispatch! :new-message-text [_ {:keys [thread-id content]}] - (store/set-new-message! thread-id content)) - -(defmethod dispatch! :new-message [_ data] - (when-not (string/blank? (data :content)) - (let [message (schema/make-message - {:user-id (store/current-user-id) - :content (identify-mentions (data :content)) - :thread-id (data :thread-id) - :group-id (data :group-id) - - :mentioned-tag-ids (concat (data :mentioned-tag-ids) - (extract-tag-ids (data :content))) - :mentioned-user-ids (concat (data :mentioned-user-ids) - (extract-user-ids (data :content)))})] - (store/add-message! message) - (sync/chsk-send! - [:braid.server/new-message message] - 2000 - (fn [reply] - (when (not= :braid/ok reply) - (store/display-error! (str :failed-to-send (message :id)) "Message failed to send!") - (store/set-message-failed! message))))))) - -(defmethod dispatch! :resend-message [_ message] - (store/clear-error! (str :failed-to-send (message :id))) - (store/clear-message-failed! message) - (sync/chsk-send! - [:braid.server/new-message message] - 2000 - (fn [reply] - (when (not= :braid/ok reply) - (store/display-error! (str :failed-to-send (message :id)) "Message failed to send!") - (store/set-message-failed! message))))) - -(defmethod dispatch! :hide-thread [_ thread-id] - (sync/chsk-send! [:braid.server/hide-thread thread-id]) - (store/hide-thread! thread-id)) - -(defmethod dispatch! :reopen-thread [_ thread-id] - (store/show-thread! thread-id) - (sync/chsk-send! [:braid.server/show-thread thread-id])) - -(defmethod dispatch! :unsub-thread [_ data] - (sync/chsk-send! [:braid.server/unsub-thread (data :thread-id)]) - (store/hide-thread! (data :thread-id))) - -(defmethod dispatch! :create-tag [_ [tag-name group-id id]] - (let [tag (schema/make-tag {:name tag-name - :group-id group-id - :group-name (:name (store/id->group group-id)) - :id id})] - (store/add-tag! tag) - (sync/chsk-send! - [:braid.server/create-tag tag] - 1000 - (fn [reply] - (if-let [msg (:error reply)] - (do - (store/remove-tag! (tag :id)) - (store/display-error! (str :bad-tag (tag :id)) msg)) - (dispatch! :subscribe-to-tag (tag :id))))))) - -(defmethod dispatch! :unsubscribe-from-tag [_ tag-id] - (sync/chsk-send! [:braid.server/unsubscribe-from-tag tag-id]) - (store/unsubscribe-from-tag! tag-id)) - -(defmethod dispatch! :subscribe-to-tag [_ tag-id] - (sync/chsk-send! [:braid.server/subscribe-to-tag tag-id]) - (store/subscribe-to-tag! tag-id)) - -(defmethod dispatch! :set-tag-description [_ [tag-id desc]] - (store/update-tag-description! tag-id desc) - (sync/chsk-send! - [:braid.server/set-tag-description {:tag-id tag-id :description desc}])) - -(defmethod dispatch! :retract-tag [_ tag-id] - (store/remove-tag! tag-id) - (sync/chsk-send! [:braid.server/retract-tag tag-id])) - -(defmethod dispatch! :create-group [_ group] - (let [group (schema/make-group group)] - (sync/chsk-send! - [:braid.server/create-group group] - 1000 - (fn [reply] - (when-let [msg (reply :error)] - (.error js/console msg) - (store/display-error! (str :bad-group (group :id)) msg) - (store/remove-group! group)))) - (store/add-group! group) - (store/become-group-admin! (:id group)))) - -(defmethod dispatch! :set-nickname [_ [nickname on-error]] - (sync/chsk-send! - [:braid.server/set-nickname {:nickname nickname}] - 1000 - (fn [reply] - (if-let [msg (reply :error)] - (on-error msg) - (store/update-user-nick! (store/current-user-id) nickname))))) - -(defmethod dispatch! :set-user-avatar [_ avatar-url] - (store/update-user-avatar! (store/current-user-id) avatar-url) - (sync/chsk-send! [:braid.server/set-user-avatar avatar-url])) - -(defmethod dispatch! :set-password [_ [password on-success on-error]] - (sync/chsk-send! - [:braid.server/set-password {:password password}] - 3000 - (fn [reply] - (cond - (reply :error) (on-error reply) - (= reply :chsk/timeout) (on-error - {:error "Couldn't connect to server, please try again"}) - true (on-success))))) - -(defmethod dispatch! :set-preference [_ [k v]] - (store/add-preferences! {k v}) - (sync/chsk-send! [:braid.server/set-preferences {k v}])) - -(defmethod dispatch! :add-notification-rule [_ rule] - (let [current-rules (get (store/user-preferences) :notification-rules [])] - (dispatch! :set-preference [:notification-rules (conj current-rules rule)]))) - -(defmethod dispatch! :remove-notification-rule [_ rule] - (let [new-rules (->> (get (store/user-preferences) :notification-rules []) - (into [] (remove (partial = rule))))] - (dispatch! :set-preference [:notification-rules new-rules]))) - -(defmethod dispatch! :search-history [_ [query group-id]] - (when query - (store/clear-search-error!) - (sync/chsk-send! - [:braid.server/search [query group-id]] - 15000 - (fn [reply] - (if (:thread-ids reply) - (store/set-search-results! query reply) - (store/set-search-error!)))))) - -(defmethod dispatch! :load-recent-threads - [_ {:keys [group-id on-error on-complete]}] - (sync/chsk-send! - [:braid.server/load-recent-threads group-id] - 5000 - (fn [reply] - (if-let [threads (:braid/ok reply)] - (store/add-threads! threads) - (cond - (= reply :chsk/timeout) (on-error "Timed out") - (:braid/error reply) (on-error (:braid/error reply)) - :else (on-error "Something went wrong"))) - (on-complete (some? (:braid/ok reply)))))) - -(defmethod dispatch! :load-threads [_ {:keys [thread-ids on-complete]}] - (sync/chsk-send! - [:braid.server/load-threads thread-ids] - 5000 - (fn [reply] - (when-let [threads (:threads reply)] - (store/add-threads! threads)) - (when on-complete - (on-complete))))) - -(defmethod dispatch! :threads-for-tag [_ {:keys [tag-id offset limit on-complete] - :or {offset 0 limit 25}}] - (sync/chsk-send! - [:braid.server/threads-for-tag {:tag-id tag-id :offset offset :limit limit}] - 2500 - (fn [reply] - (when-let [results (:threads reply)] - (if (zero? offset) - ; initial load of threads - (store/set-channel-results! results) - ; paging more results in - (store/add-channel-results! results)) - (store/set-pagination-remaining! (:remaining reply)) - (when on-complete (on-complete)))))) - -(defmethod dispatch! :mark-thread-read [_ thread-id] - (store/update-thread-last-open-at thread-id) - (sync/chsk-send! [:braid.server/mark-thread-read thread-id])) - -(defmethod dispatch! :clear-inbox [_ _] - (let [open-thread-ids (map :id (store/open-threads))] - (doseq [id open-thread-ids] - (dispatch! :hide-thread id)))) - -(defmethod dispatch! :invite [_ data] - (let [invite (schema/make-invitation data)] - (sync/chsk-send! [:braid.server/invite-to-group invite]))) - -(defmethod dispatch! :generate-link [_ {:keys [group-id expires complete]}] - (println "dispatching generate") - (sync/chsk-send! - [:braid.server/generate-invite-link {:group-id group-id :expires expires}] - 5000 - (fn [reply] - ; indicate error if it fails? - (when-let [link (:link reply)] - (complete link))))) - -(defmethod dispatch! :accept-invite [_ invite] - (sync/chsk-send! [:braid.server/invitation-accept invite]) - (store/remove-invite! invite)) - -(defmethod dispatch! :decline-invite [_ invite] - (sync/chsk-send! [:braid.server/invitation-decline invite]) - (store/remove-invite! invite)) - -(defmethod dispatch! :make-admin [_ {:keys [group-id user-id] :as args}] - (sync/chsk-send! [:braid.server/make-user-admin args]) - (store/add-group-admin! group-id user-id)) - -(defmethod dispatch! :remove-from-group [ _ {:keys [group-id user-id] :as args}] - (sync/chsk-send! [:braid.server/remove-from-group args])) - -(defmethod dispatch! :set-intro [_ {:keys [group-id intro] :as args}] - (sync/chsk-send! [:braid.server/set-group-intro args]) - (store/set-group-intro! group-id intro)) - -(defmethod dispatch! :set-group-avatar [_ {:keys [group-id avatar] :as args}] - (sync/chsk-send! [:braid.server/set-group-avatar args]) - (store/set-group-avatar! group-id avatar)) - -(defmethod dispatch! :make-group-public! [_ group-id] - (sync/chsk-send! [:braid.server/set-group-publicity [group-id true]])) - -(defmethod dispatch! :make-group-private! [_ group-id] - (sync/chsk-send! [:braid.server/set-group-publicity [group-id false]])) - -(defmethod dispatch! :new-bot [_ {:keys [bot on-complete]}] - (let [bot (schema/make-bot bot)] - (sync/chsk-send! - [:braid.server/create-bot bot] - 5000 - (fn [reply] - (when (nil? (:braid/ok reply)) - (store/display-error! - (str "bot-" (bot :id) (rand)) - (get reply :braid/error "Something when wrong creating bot"))) - (on-complete (:braid/ok reply)))))) - -(defmethod dispatch! :get-bot-info [_ {:keys [bot-id on-complete]}] - (sync/chsk-send! - [:braid.server/get-bot-info bot-id] - 2000 - (fn [reply] - (when-let [bot (:braid/ok reply)] - (on-complete bot))))) - -(defmethod dispatch! :create-upload [_ {:keys [url thread-id group-id]}] - (sync/chsk-send! [:braid.server/create-upload - (schema/make-upload {:url url :thread-id thread-id})]) - (dispatch! :new-message {:content url :thread-id thread-id :group-id group-id})) - -(defmethod dispatch! :get-group-uploads [_ {:keys [group-id on-success on-error]}] - (sync/chsk-send! - [:braid.server/uploads-in-group group-id] - 5000 - (fn [reply] - (if-let [uploads (:braid/ok reply)] - (on-success uploads) - (on-error (get reply :braid/error "Couldn't get uploads in group")))))) - -(defmethod dispatch! :check-auth! [_ _] - (edn-xhr {:uri "/check" - :method :get - :on-complete (fn [_] - (dispatch! :start-socket!)) - :on-error (fn [_] - (dispatch! :set-login-state! :login-form))})) - -(defmethod dispatch! :set-login-state! [_ state] - (store/set-login-state! state)) - -(defmethod dispatch! :start-socket! [_ _] - (dispatch! :set-login-state! :ws-connect) - (sync/make-socket!) - (sync/start-router!)) - -(defmethod dispatch! :auth [_ data] - (edn-xhr {:uri "/auth" - :method :post - :params {:email (data :email) - :password (data :password)} - :on-complete (fn [_] - (when-let [cb (data :on-complete)] - (cb)) - (dispatch! :start-socket!)) - :on-error (fn [_] - (when-let [cb (data :on-error)] - (cb)))})) - -(defmethod dispatch! :request-reset [_ email] - (edn-xhr {:uri "/request-reset" - :method :post - :params {:email email}})) - -(defmethod dispatch! :logout [_ _] - (edn-xhr {:uri "/logout" - :method :post - :params {:csrf-token (:csrf-token @sync/chsk-state)} - :on-complete (fn [data] - (dispatch! :set-login-state! :login-form) - (store/clear-session!))})) - -(defmethod dispatch! :start-call [_ call-data] - (let [call (schema/make-call {:type (call-data :type) - :source-id (call-data :source-id) - :target-id (call-data :target-id) - :status "incoming"})] - (store/add-call! call) - (sync/chsk-send! [:chat/make-call call]) - (get-ice-servers))) - -(defmethod dispatch! :accept-call [_ call] - (store/update-call-status! (call :id) "accepted") - (sync/chsk-send! [:chat/change-call-status {:call call - :status "accepted"}]) - (rtc/open-local-stream (call :type))) - -(defmethod dispatch! :decline-call [_ call] - (store/update-call-status! (call :id) "declined") - (sync/chsk-send! [:chat/change-call-status {:call call - :status "declined"}])) - -(defmethod dispatch! :end-call [_ call] - (store/update-call-status! (call :id) "ended") - (sync/chsk-send! [:chat/change-call-status {:call call - :status "ended"}])) - -(defmethod dispatch! :drop-call [_ call] - (store/update-call-status! (call :id) "dropped") - (sync/chsk-send! [:chat/change-call-status {:call call - :status "dropped"}])) - -(defmethod dispatch! :request-ice-servers [_ _] - (sync/chsk-send! [:rtc/get-ice-servers] 500 - (fn [servers] - (println "SERVERS:" servers) - (rtc/initialize-rtc-environment servers)))) - -(defn check-client-version [server-checksum] - (when (not= (aget js/window "checksum") server-checksum) - (store/display-error! :client-out-of-date "Client out of date - please refresh" :info))) - -; Websocket Events - -(defmethod sync/event-handler :braid.client/thread - [[_ data]] - (store/add-open-thread! data)) - -(defmethod sync/event-handler :braid.client/init-data - [[_ data]] - (dispatch! :set-login-state! :app) - (check-client-version (data :version-checksum)) - (store/set-session! {:user-id (data :user-id)}) - (store/add-users! (data :users)) - (store/add-tags! (data :tags)) - (store/set-user-subscribed-tag-ids! (data :user-subscribed-tag-ids)) - (store/add-preferences! (data :user-preferences)) - (store/set-user-joined-groups! (data :user-groups)) - (store/set-invitations! (data :invitations)) - (store/set-open-threads! (data :user-threads)) - (router/dispatch-current-path!)) - -(defmethod sync/event-handler :socket/connected - [[_ _]] - (sync/chsk-send! [:braid.server/start nil])) - -(defmethod sync/event-handler :braid.client/create-tag - [[_ data]] - (store/add-tag! data) - (store/subscribe-to-tag! (data :id))) - -(defmethod sync/event-handler :braid.client/joined-group - [[_ data]] - (store/add-group! (data :group)) - (store/add-tags! (data :tags)) - (doseq [t (data :tags)] - (store/subscribe-to-tag! (t :id)))) - -(defmethod sync/event-handler :braid.client/update-users - [[_ data]] - (store/add-users! data)) - -(defmethod sync/event-handler :braid.client/invitation-received - [[_ invite]] - (store/add-invite! invite)) - -(defmethod sync/event-handler :braid.client/name-change - [[_ {:keys [user-id nickname]}]] - (store/update-user-nick! user-id nickname)) - -(defmethod sync/event-handler :braid.client/user-new-avatar - [[_ {:keys [user-id avatar]}]] - (store/update-user-avatar! user-id avatar)) - -(defmethod sync/event-handler :braid.client/left-group - [[_ [group-id group-name]]] - (store/remove-group! {:id group-id}) - (store/display-error! - (str "left-" group-id) - (str "You have been removed from " group-name) - :info) - (when-let [sidebar-order (:groups-order (store/user-preferences))] - (store/add-preferences! - {:groups-order (into [] (remove (partial = group-id)) - sidebar-order)})) - (when (= group-id (store/open-group-id)) - (routes/go-to! (routes/index-path)))) - -(defmethod sync/event-handler :braid.client/user-connected - [[_ user-id]] - (store/update-user-status! user-id :online)) - -(defmethod sync/event-handler :braid.client/user-disconnected - [[_ user-id]] - (store/update-user-status! user-id :offline)) - -(defmethod sync/event-handler :braid.client/new-user - [[_ user]] - (store/add-user! (assoc user :status :online))) - -(defmethod sync/event-handler :braid.client/user-left - [[_ [group-id user-id]]] - (store/remove-user-group! user-id group-id)) - -(defmethod sync/event-handler :braid.client/new-admin - [[_ [group-id new-admin-id]]] - (store/add-group-admin! group-id new-admin-id)) - -(defmethod sync/event-handler :braid.client/tag-descrption-change - [[_ [tag-id new-description]]] - (store/update-tag-description! tag-id new-description)) - -(defmethod sync/event-handler :braid.client/retract-tag - [[ _ tag-id]] - (store/remove-tag! tag-id)) - -(defmethod sync/event-handler :braid.client/new-intro - [[_ [group-id intro]]] - (store/set-group-intro! group-id intro)) - -(defmethod sync/event-handler :braid.client/group-new-avatar - [[_ [group-id avatar]]] - (store/set-group-avatar! group-id avatar)) - -(defmethod sync/event-handler :braid.client/publicity-changed - [[_ [group-id publicity]]] - (store/set-group-publicity! group-id publicity)) - -(defmethod sync/event-handler :braid.client/new-bot - [[_ [group-id bot]]] - (store/add-group-bot! group-id bot)) - -(defmethod sync/event-handler :braid.client/notify-message - [[_ message]] - (notify/notify {:msg (:content message)})) - -(defmethod sync/event-handler :chat/receive-call - [[_ call]] - (store/add-call! call) - (get-ice-servers)) - -(defmethod sync/event-handler :chat/new-call-status - [[_ [call-id status]]] - (store/update-call-status! call-id status)) - -(defmethod sync/event-handler :rtc/receive-protocol-signal - [[_ signal]] - (rtc/handle-protocol-signal signal)) - -(defmethod sync/event-handler :braid.client/hide-thread - [[_ thread-id]] - (store/hide-thread! thread-id)) - -(defmethod sync/event-handler :braid.client/show-thread - [[_ thread]] - (store/add-open-thread! thread)) From 14804abfa734e3b3ce276d4bbc9d280038cfa215 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 19 Aug 2016 17:16:28 -0400 Subject: [PATCH 83/94] Refactor various rtc client methods --- src/braid/client/state/handler/impl.cljs | 6 +-- src/braid/client/webrtc.cljs | 61 +++++++++++++++++------- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/src/braid/client/state/handler/impl.cljs b/src/braid/client/state/handler/impl.cljs index e97cd2e6b..7fbae7651 100644 --- a/src/braid/client/state/handler/impl.cljs +++ b/src/braid/client/state/handler/impl.cljs @@ -524,7 +524,7 @@ (defmethod handler :start-new-call [state [_ data]] (rtc/get-ice-servers (fn [servers] - (rtc/create-local-connection servers) + (rtc/create-connections servers) (let [call (schema/make-call data)] (sync/chsk-send! [:braid.server/make-new-call call]) (dispatch! :add-new-call call)))) @@ -533,7 +533,7 @@ (defmethod handler :receive-new-call [state [_ call]] (rtc/get-ice-servers (fn [servers] - (rtc/create-local-connection servers) + (rtc/create-connections servers) (dispatch! :add-new-call call))) state) @@ -542,7 +542,7 @@ (defmethod handler :set-requester-call-status [state [_ [call status]]] (when (= status :accepted) - (rtc/open-local-stream)) + (rtc/set-caller-stream)) (sync/chsk-send! [:braid.server/change-call-status {:call call :status status}]) (helpers/set-call-status state (call :id) status)) diff --git a/src/braid/client/webrtc.cljs b/src/braid/client/webrtc.cljs index ee1d5672e..53330534a 100644 --- a/src/braid/client/webrtc.cljs +++ b/src/braid/client/webrtc.cljs @@ -5,48 +5,63 @@ (defonce svga-dimensions (clj->js {:mandatory {:maxWidth 320 :maxHeight 180}})) -(def local-peer-connnection (atom nil)) -(def remote-peer-connection (atom nil)) +(def sending-peer-connection (atom nil)) + +(def receiving-peer-connection (atom nil)) + +; REMOTE STUFF ; Protocols -(defn signal-sdp-description [description] - (.setLocalDescription @local-peer-connnection description) +(defn signal-sdp-description [connection description] + (.setLocalDescription @connection description) (sync/chsk-send! [:braid.server/send-protocol-signal {:sdp (aget description "sdp") :type (aget description "type")}])) (defn create-answer [connection] - (.createAnswer @connection signal-sdp-description + (.createAnswer + @connection + (fn [description] + (signal-sdp-description connection description)) (fn [error] (println "Error creating offer description: " (aget error "message"))))) (defn create-offer [connection] - (.createOffer @connection signal-sdp-description + (.createOffer + @connection + (fn [description] + (signal-sdp-description connection description)) (fn [error] (println "Error creating offer description: " (aget error "message"))))) (defn receive-protocol-signal [signal] (if (signal :candidate) - (.addIceCandidate @local-peer-connnection (js/RTCIceCandidate. (clj->js signal))) + (.addIceCandidate @sending-peer-connection (js/RTCIceCandidate. (clj->js signal))) (do - (.setRemoteDescription @local-peer-connnection (js/RTCSessionDescription. (clj->js signal))) + (.setRemoteDescription @sending-peer-connection (js/RTCSessionDescription. (clj->js signal))) (when (= "offer" (signal :type)) - (create-answer local-peer-connnection))))) + (create-answer sending-peer-connection))))) ; Media -(defn open-local-stream [] +(defn set-stream [peer-connection] (letfn [(stream-success [stream] - (.addStream @local-peer-connnection stream) - (create-offer local-peer-connnection)) + (.addStream @peer-connection stream) + (create-offer peer-connection)) (stream-failure [error] (println "Error opening stream: " (aget error "message")))] (. js/navigator (webkitGetUserMedia (clj->js {:audio true :video svga-dimensions}) stream-success stream-failure)))) +(defn set-callee-stream [] + (set-stream receiving-peer-connection)) + +(defn set-caller-stream [] + (set-stream sending-peer-connection)) + ; Setup (defn handle-ice-candidate [evt] @@ -65,11 +80,23 @@ (aset video-player "src" stream-url) (aset video-player "onloadedmetadata" (fn [_] (.play video-player))))) -(defn create-local-connection [servers] - (let [connection (js/webkitRTCPeerConnection. (clj->js {:iceServers servers}))] - (aset connection "onicecandidate" handle-ice-candidate) - (aset connection "onaddstream" handle-stream) - (reset! local-peer-connnection connection))) +(defn set-connection-atom [conn conn-atom on-ice on-stream] + (aset conn "onicecandidate" on-ice) + (aset conn "onaddstream" on-stream) + (reset! conn-atom conn) + (js/console.log @conn-atom)) + +(defn create-connections [servers] + (set-connection-atom + (js/webkitRTCPeerConnection. (clj->js {:iceServers servers})) + sending-peer-connection + handle-ice-candidate + handle-stream) + (set-connection-atom + (js/webkitRTCPeerConnection. (clj->js {:iceServers servers})) + receiving-peer-connection + handle-ice-candidate + handle-stream)) (defn get-ice-servers [handler] (sync/chsk-send! [:braid.server/get-ice-servers] 2500 From bd26c0053fcd1d2ec2002b1e83888b698a743281 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 19 Aug 2016 17:28:01 -0400 Subject: [PATCH 84/94] Attempt bidirectional streaming --- src/braid/client/state/handler/impl.cljs | 2 ++ src/braid/client/webrtc.cljs | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/braid/client/state/handler/impl.cljs b/src/braid/client/state/handler/impl.cljs index 7fbae7651..9ccefc11a 100644 --- a/src/braid/client/state/handler/impl.cljs +++ b/src/braid/client/state/handler/impl.cljs @@ -547,4 +547,6 @@ (helpers/set-call-status state (call :id) status)) (defmethod handler :set-receiver-call-status [state [_ [call status]]] + (when (= status :accepted) + (rtc/set-caller-stream)) (helpers/set-call-status state (call :id) status)) diff --git a/src/braid/client/webrtc.cljs b/src/braid/client/webrtc.cljs index 53330534a..46ea6af1d 100644 --- a/src/braid/client/webrtc.cljs +++ b/src/braid/client/webrtc.cljs @@ -92,7 +92,7 @@ sending-peer-connection handle-ice-candidate handle-stream) - (set-connection-atom + #_(set-connection-atom (js/webkitRTCPeerConnection. (clj->js {:iceServers servers})) receiving-peer-connection handle-ice-candidate From 1efbefebc2ab5c3663bab2de0f4f9d2d6dce8101 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Fri, 19 Aug 2016 18:54:04 -0400 Subject: [PATCH 85/94] Remove second peer connection and connection specific wrappers --- src/braid/client/state/handler/impl.cljs | 4 ++-- src/braid/client/webrtc.cljs | 28 ++++++------------------ 2 files changed, 9 insertions(+), 23 deletions(-) diff --git a/src/braid/client/state/handler/impl.cljs b/src/braid/client/state/handler/impl.cljs index 9ccefc11a..5ed25bd75 100644 --- a/src/braid/client/state/handler/impl.cljs +++ b/src/braid/client/state/handler/impl.cljs @@ -542,11 +542,11 @@ (defmethod handler :set-requester-call-status [state [_ [call status]]] (when (= status :accepted) - (rtc/set-caller-stream)) + (rtc/set-stream)) (sync/chsk-send! [:braid.server/change-call-status {:call call :status status}]) (helpers/set-call-status state (call :id) status)) (defmethod handler :set-receiver-call-status [state [_ [call status]]] (when (= status :accepted) - (rtc/set-caller-stream)) + (rtc/set-stream)) (helpers/set-call-status state (call :id) status)) diff --git a/src/braid/client/webrtc.cljs b/src/braid/client/webrtc.cljs index 46ea6af1d..0376df742 100644 --- a/src/braid/client/webrtc.cljs +++ b/src/braid/client/webrtc.cljs @@ -5,9 +5,7 @@ (defonce svga-dimensions (clj->js {:mandatory {:maxWidth 320 :maxHeight 180}})) -(def sending-peer-connection (atom nil)) - -(def receiving-peer-connection (atom nil)) +(def peer-connection (atom nil)) ; REMOTE STUFF @@ -38,15 +36,15 @@ (defn receive-protocol-signal [signal] (if (signal :candidate) - (.addIceCandidate @sending-peer-connection (js/RTCIceCandidate. (clj->js signal))) + (.addIceCandidate @peer-connection (js/RTCIceCandidate. (clj->js signal))) (do - (.setRemoteDescription @sending-peer-connection (js/RTCSessionDescription. (clj->js signal))) + (.setRemoteDescription @peer-connection (js/RTCSessionDescription. (clj->js signal))) (when (= "offer" (signal :type)) - (create-answer sending-peer-connection))))) + (create-answer peer-connection))))) ; Media -(defn set-stream [peer-connection] +(defn set-stream [] (letfn [(stream-success [stream] (.addStream @peer-connection stream) (create-offer peer-connection)) @@ -56,12 +54,6 @@ (webkitGetUserMedia (clj->js {:audio true :video svga-dimensions}) stream-success stream-failure)))) -(defn set-callee-stream [] - (set-stream receiving-peer-connection)) - -(defn set-caller-stream [] - (set-stream sending-peer-connection)) - ; Setup (defn handle-ice-candidate [evt] @@ -83,18 +75,12 @@ (defn set-connection-atom [conn conn-atom on-ice on-stream] (aset conn "onicecandidate" on-ice) (aset conn "onaddstream" on-stream) - (reset! conn-atom conn) - (js/console.log @conn-atom)) + (reset! conn-atom conn)) (defn create-connections [servers] (set-connection-atom (js/webkitRTCPeerConnection. (clj->js {:iceServers servers})) - sending-peer-connection - handle-ice-candidate - handle-stream) - #_(set-connection-atom - (js/webkitRTCPeerConnection. (clj->js {:iceServers servers})) - receiving-peer-connection + peer-connection handle-ice-candidate handle-stream)) From 7ef3efba31e29633d331cc32b77d5329818f112f Mon Sep 17 00:00:00 2001 From: 10plusY Date: Sat, 20 Aug 2016 16:26:11 -0400 Subject: [PATCH 86/94] Move calls handlers to their own folder --- src/braid/client/calls/handlers.cljs | 38 +++++++++++++++++++++++- src/braid/client/state/handler/impl.cljs | 31 +------------------ src/braid/client/ui/views/call.cljs | 26 ++++++++-------- 3 files changed, 51 insertions(+), 44 deletions(-) diff --git a/src/braid/client/calls/handlers.cljs b/src/braid/client/calls/handlers.cljs index e34e4be0f..9f92a12d3 100644 --- a/src/braid/client/calls/handlers.cljs +++ b/src/braid/client/calls/handlers.cljs @@ -1 +1,37 @@ -(ns braid.client.calls.handlers) +(ns braid.client.calls.handlers + (:require [braid.client.webrtc :as rtc] + [braid.client.dispatcher :refer [dispatch!]] + [braid.client.sync :as sync] + [braid.client.schema :as schema] + [braid.client.state.helpers :as helpers] + [braid.client.state.handler.core :refer [handler]])) + +(defmethod handler :calls/start-new-call [state [_ data]] + (rtc/get-ice-servers + (fn [servers] + (rtc/create-connections servers) + (let [call (schema/make-call data)] + (sync/chsk-send! [:braid.server/make-new-call call]) + (dispatch! :calls/add-new-call call)))) + state) + +(defmethod handler :calls/receive-new-call [state [_ call]] + (rtc/get-ice-servers + (fn [servers] + (rtc/create-connections servers) + (dispatch! :calls/add-new-call call))) + state) + +(defmethod handler :calls/add-new-call [state [_ call]] + (helpers/add-call state call)) + +(defmethod handler :calls/set-requester-call-status [state [_ [call status]]] + (when (= status :accepted) + (rtc/set-stream)) + (sync/chsk-send! [:braid.server/change-call-status {:call call :status status}]) + (helpers/set-call-status state (call :id) status)) + +(defmethod handler :calls/set-receiver-call-status [state [_ [call status]]] + (when (= status :accepted) + (rtc/set-stream)) + (helpers/set-call-status state (call :id) status)) diff --git a/src/braid/client/state/handler/impl.cljs b/src/braid/client/state/handler/impl.cljs index 5ed25bd75..0151d589f 100644 --- a/src/braid/client/state/handler/impl.cljs +++ b/src/braid/client/state/handler/impl.cljs @@ -9,7 +9,7 @@ [braid.client.state.helpers :as helpers] [braid.client.dispatcher :refer [dispatch!]] [braid.client.state.handler.core :refer [handler]] - [braid.client.webrtc :as rtc])) + [braid.client.calls.handlers])) (defn extract-tag-ids [text] @@ -521,32 +521,3 @@ (defmethod handler :add-group-bot [state [_ [group-id bot]]] (helpers/add-group-bot state group-id bot)) -(defmethod handler :start-new-call [state [_ data]] - (rtc/get-ice-servers - (fn [servers] - (rtc/create-connections servers) - (let [call (schema/make-call data)] - (sync/chsk-send! [:braid.server/make-new-call call]) - (dispatch! :add-new-call call)))) - state) - -(defmethod handler :receive-new-call [state [_ call]] - (rtc/get-ice-servers - (fn [servers] - (rtc/create-connections servers) - (dispatch! :add-new-call call))) - state) - -(defmethod handler :add-new-call [state [_ call]] - (helpers/add-call state call)) - -(defmethod handler :set-requester-call-status [state [_ [call status]]] - (when (= status :accepted) - (rtc/set-stream)) - (sync/chsk-send! [:braid.server/change-call-status {:call call :status status}]) - (helpers/set-call-status state (call :id) status)) - -(defmethod handler :set-receiver-call-status [state [_ [call status]]] - (when (= status :accepted) - (rtc/set-stream)) - (helpers/set-call-status state (call :id) status)) diff --git a/src/braid/client/ui/views/call.cljs b/src/braid/client/ui/views/call.cljs index 3fce4bc1d..e98d85f7a 100644 --- a/src/braid/client/ui/views/call.cljs +++ b/src/braid/client/ui/views/call.cljs @@ -11,7 +11,7 @@ (let [correct-nickname (subscribe [:correct-nickname call])] (fn [call] [:div - [:a.button {:on-click (fn [_] (dispatch! :set-requester-call-status [call :archived]))} "X"] + [:a.button {:on-click (fn [_] (dispatch! :calls/set-requester-call-status [call :archived]))} "X"] [:p (str "Call with " @correct-nickname " ended")]]))) (defn dropped-call-view @@ -19,7 +19,7 @@ (let [correct-nickname (subscribe [:correct-nickname call])] (fn [call] [:div - [:a.button {:on-click (fn [_] (dispatch! :set-requester-call-status [call :archived]))} "X"] + [:a.button {:on-click (fn [_] (dispatch! :calls/set-requester-call-status [call :archived]))} "X"] [:p (str "Call with " @correct-nickname " dropped")]]))) (defn declined-call-view @@ -29,7 +29,7 @@ callee-nickname (subscribe [:nickname (call :callee-id)])] (fn [call] [:div - [:a.button {:on-click (fn [_] (dispatch! :set-requester-call-status [call :archived]))} "X"] + [:a.button {:on-click (fn [_] (dispatch! :calls/set-requester-call-status [call :archived]))} "X"] (if @user-is-caller? [:p (str @callee-nickname " declined your call")] [:p (str "Call with " @caller-nickname "declined")])]))) @@ -51,7 +51,7 @@ :class (if (= (call :type) :video) "video" "audio")}] [:a.button {:on-click (fn [_] - (dispatch! :set-requester-call-status [call :ended]))} "End"]]))) + (dispatch! :calls/set-requester-call-status [call :ended]))} "End"]]))) (defn incoming-call-view [call] @@ -64,15 +64,15 @@ [:p (str "Calling " @callee-nickname "...")] [:a.button {:on-click (fn [_] - (dispatch! :set-requester-call-status [call :dropped]))} "Drop"]] + (dispatch! :calls/set-requester-call-status [call :dropped]))} "Drop"]] [:div [:p (str "Call from " @caller-nickname)] [:a.button {:on-click (fn [_] - (dispatch! :set-requester-call-status [call :accepted]))} "Accept"] + (dispatch! :calls/set-requester-call-status [call :accepted]))} "Accept"] [:a.button {:on-click (fn [_] - (dispatch! :set-requester-call-status [call :declined]))} "Decline"]])))) + (dispatch! :calls/set-requester-call-status [call :declined]))} "Decline"]])))) (defn during-call-view [call] @@ -110,14 +110,14 @@ [:h3 (str "Call " @callee-nickname)] [:a.button {:on-click (fn [_] - (dispatch! :start-new-call {:type :audio - :caller-id @caller-id - :callee-id callee-id}))} "Audio"] + (dispatch! :calls/start-new-call {:type :audio + :caller-id @caller-id + :callee-id callee-id}))} "Audio"] [:a.button {:on-click (fn [_] - (dispatch! :start-new-call {:type :video - :caller-id @caller-id - :callee-id callee-id}))} "Video"]])}))) + (dispatch! :calls/start-new-call {:type :video + :caller-id @caller-id + :callee-id callee-id}))} "Video"]])}))) (defn call-view [] (let [callee-id (subscribe [:page-id]) From 84cf7f6029d939ab462c8047825d99c5bf9ba67c Mon Sep 17 00:00:00 2001 From: 10plusY Date: Sat, 20 Aug 2016 16:28:38 -0400 Subject: [PATCH 87/94] Move call views to calls folder --- src/braid/client/{ui/views/call.cljs => calls/views.cljs} | 2 +- src/braid/client/ui/views/pages/user.cljs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/braid/client/{ui/views/call.cljs => calls/views.cljs} (99%) diff --git a/src/braid/client/ui/views/call.cljs b/src/braid/client/calls/views.cljs similarity index 99% rename from src/braid/client/ui/views/call.cljs rename to src/braid/client/calls/views.cljs index e98d85f7a..a77792aab 100644 --- a/src/braid/client/ui/views/call.cljs +++ b/src/braid/client/calls/views.cljs @@ -1,4 +1,4 @@ -(ns braid.client.ui.views.call +(ns braid.client.calls.views (:require [reagent.core :as r] [reagent.ratom :include-macros true :refer-macros [reaction]] [braid.client.ui.views.pills :refer [user-pill-view]] diff --git a/src/braid/client/ui/views/pages/user.cljs b/src/braid/client/ui/views/pages/user.cljs index ab6dd08b9..a49424d42 100644 --- a/src/braid/client/ui/views/pages/user.cljs +++ b/src/braid/client/ui/views/pages/user.cljs @@ -1,6 +1,6 @@ (ns braid.client.ui.views.pages.user (:require [reagent.ratom :include-macros true :refer-macros [reaction]] - [braid.client.ui.views.call :refer [call-view]] + [braid.client.calls.views :refer [call-view]] [braid.client.ui.views.pills :refer [user-pill-view]] [braid.client.ui.views.threads :refer [threads-view]] [braid.client.state :refer [subscribe]])) From f75334a22b1f015525efaea2b2ae2b69547c9e9e Mon Sep 17 00:00:00 2001 From: 10plusY Date: Sat, 20 Aug 2016 16:31:10 -0400 Subject: [PATCH 88/94] Move call helpers to calls folder --- src/braid/client/calls/handlers.cljs | 2 +- src/braid/client/calls/helpers.cljs | 6 ++++++ src/braid/client/state/helpers.cljs | 8 -------- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/braid/client/calls/handlers.cljs b/src/braid/client/calls/handlers.cljs index 9f92a12d3..9fc6bc489 100644 --- a/src/braid/client/calls/handlers.cljs +++ b/src/braid/client/calls/handlers.cljs @@ -3,7 +3,7 @@ [braid.client.dispatcher :refer [dispatch!]] [braid.client.sync :as sync] [braid.client.schema :as schema] - [braid.client.state.helpers :as helpers] + [braid.client.calls.helpers :as helpers] [braid.client.state.handler.core :refer [handler]])) (defmethod handler :calls/start-new-call [state [_ data]] diff --git a/src/braid/client/calls/helpers.cljs b/src/braid/client/calls/helpers.cljs index 956e2f703..d677dfb57 100644 --- a/src/braid/client/calls/helpers.cljs +++ b/src/braid/client/calls/helpers.cljs @@ -1 +1,7 @@ (ns braid.client.calls.helpers) + +(defn add-call [state call] + (update-in state [:calls] #(assoc % (:id call) call))) + +(defn set-call-status [state call-id status] + (assoc-in state [:calls call-id :status] status)) diff --git a/src/braid/client/state/helpers.cljs b/src/braid/client/state/helpers.cljs index d1d2e0bfe..c5a298b6b 100644 --- a/src/braid/client/state/helpers.cljs +++ b/src/braid/client/state/helpers.cljs @@ -326,11 +326,3 @@ (defn add-group-bot [state group-id bot] (update-in state [:groups group-id :bots] #(conj % bot))) - -; calls - -(defn add-call [state call] - (update-in state [:calls] #(assoc % (:id call) call))) - -(defn set-call-status [state call-id status] - (assoc-in state [:calls call-id :status] status)) From 3ee5af1b77bd3800a5a334febe1fd61dbbf7625e Mon Sep 17 00:00:00 2001 From: 10plusY Date: Sat, 20 Aug 2016 16:36:58 -0400 Subject: [PATCH 89/94] Move call subscriptions to own folder --- src/braid/client/calls/subscriptions.cljs | 31 ++++++++++++++++++- src/braid/client/state.cljs | 37 ++--------------------- src/braid/client/state/subscription.cljs | 7 +++++ 3 files changed, 40 insertions(+), 35 deletions(-) create mode 100644 src/braid/client/state/subscription.cljs diff --git a/src/braid/client/calls/subscriptions.cljs b/src/braid/client/calls/subscriptions.cljs index 03fdcefd8..258f629d0 100644 --- a/src/braid/client/calls/subscriptions.cljs +++ b/src/braid/client/calls/subscriptions.cljs @@ -1 +1,30 @@ -(ns braid.client.calls.subscriptions) +(ns braid.client.calls.subscriptions + (:require [reagent.ratom :include-macros true :refer-macros [reaction]] + [braid.client.state.subscription :refer [subscription]])) + +(defmethod subscription :calls + [state _] + (reaction (vals (@state :calls)))) + +(defmethod subscription :call-status + [state [_ call]] + (reaction (get-in @state [:calls (call :id) :status]))) + +(defmethod subscription :new-call + [state _] + (reaction (->> (@state :calls) + vals + (filter (fn [c] (not= :archived (c :status)))) + (sort-by :created-at) + first))) + +(defmethod subscription :current-user-is-caller? + [state [_ caller-id]] + (reaction (= @(subscription state [:user-id]) caller-id))) + +(defmethod subscription :correct-nickname + [state [_ call]] + (let [is-caller? (reaction @(subscription state [:current-user-is-caller? (call :caller-id)])) + caller-nickname (reaction @(subscription state [:nickname (call :caller-id)])) + callee-nickname (reaction @(subscription state [:nickname (call :callee-id)]))] + (reaction (if @is-caller? @callee-nickname @caller-nickname)))) diff --git a/src/braid/client/state.cljs b/src/braid/client/state.cljs index 9c3191d2f..b1e9aa2f7 100644 --- a/src/braid/client/state.cljs +++ b/src/braid/client/state.cljs @@ -1,15 +1,11 @@ (ns braid.client.state (:require [reagent.ratom :include-macros true :refer-macros [reaction]] [braid.client.store :as store] - [clojure.set :refer [union intersection subset?]]) + [clojure.set :refer [union intersection subset?]] + [braid.client.state.subscription :refer [subscription]] + [braid.client.calls.subscriptions]) (:import goog.Uri)) -(defmulti subscription - "Create a reaction for the particular type of information. - Do not call directly, should be invoked by `subscribe`" - {:arglists '([state [sub-name args]])} - (fn [_ [sub-name _]] sub-name)) - (defn subscribe "Get a reaction for the given data. In one-argument form, this looks like `(subscribe [:key arg1 arg2])` @@ -296,30 +292,3 @@ (defmethod subscription :user-preference [state [_ pref]] (reaction (get-in @state [:preferences pref]))) - -(defmethod subscription :calls - [state _] - (reaction (vals (@state :calls)))) - -(defmethod subscription :call-status - [state [_ call]] - (reaction (get-in @state [:calls (call :id) :status]))) - -(defmethod subscription :new-call - [state _] - (reaction (->> (@state :calls) - vals - (filter (fn [c] (not= :archived (c :status)))) - (sort-by :created-at) - first))) - -(defmethod subscription :current-user-is-caller? - [state [_ caller-id]] - (reaction (= @(subscription state [:user-id]) caller-id))) - -(defmethod subscription :correct-nickname - [state [_ call]] - (let [is-caller? (reaction @(subscription state [:current-user-is-caller? (call :caller-id)])) - caller-nickname (reaction @(subscription state [:nickname (call :caller-id)])) - callee-nickname (reaction @(subscription state [:nickname (call :callee-id)]))] - (reaction (if @is-caller? @callee-nickname @caller-nickname)))) diff --git a/src/braid/client/state/subscription.cljs b/src/braid/client/state/subscription.cljs new file mode 100644 index 000000000..7039a900e --- /dev/null +++ b/src/braid/client/state/subscription.cljs @@ -0,0 +1,7 @@ +(ns braid.client.state.subscription) + +(defmulti subscription + "Create a reaction for the particular type of information. + Do not call directly, should be invoked by `subscribe`" + {:arglists '([state [sub-name args]])} + (fn [_ [sub-name _]] sub-name)) From 1dc0594f652afb351c254608ea6abe5f89525021 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Sat, 20 Aug 2016 16:42:17 -0400 Subject: [PATCH 90/94] Move call remote-handlers into own folder --- src/braid/client/calls/remote_handlers.cljs | 17 +++++++++++++++++ src/braid/client/state/remote_handlers.cljs | 16 ++-------------- 2 files changed, 19 insertions(+), 14 deletions(-) create mode 100644 src/braid/client/calls/remote_handlers.cljs diff --git a/src/braid/client/calls/remote_handlers.cljs b/src/braid/client/calls/remote_handlers.cljs new file mode 100644 index 000000000..bec8b7275 --- /dev/null +++ b/src/braid/client/calls/remote_handlers.cljs @@ -0,0 +1,17 @@ +(ns braid.client.calls.remote-handlers + (:require + [braid.client.sync :as sync] + [braid.client.webrtc :as rtc] + [braid.client.dispatcher :refer [dispatch!]])) + +(defmethod sync/event-handler :braid.client/receive-new-call + [[_ call]] + (dispatch! :receive-new-call call)) + +(defmethod sync/event-handler :braid.client/receive-new-call-status + [[_ [call status]]] + (dispatch! :set-receiver-call-status [call status])) + +(defmethod sync/event-handler :braid.client/receive-protocol-signal + [[_ signal]] + (rtc/receive-protocol-signal signal)) diff --git a/src/braid/client/state/remote_handlers.cljs b/src/braid/client/state/remote_handlers.cljs index 5e2862c1e..16afdf83c 100644 --- a/src/braid/client/state/remote_handlers.cljs +++ b/src/braid/client/state/remote_handlers.cljs @@ -3,8 +3,8 @@ [braid.client.sync :as sync] [braid.client.router :as router] [braid.client.desktop.notify :as notify] - [braid.client.webrtc :as rtc] - [braid.client.dispatcher :refer [dispatch!]])) + [braid.client.dispatcher :refer [dispatch!]] + [braid.client.calls.remote-handlers])) (defmethod sync/event-handler :braid.client/thread [[_ data]] @@ -116,15 +116,3 @@ (defmethod sync/event-handler :braid.client/show-thread [[_ thread]] (dispatch! :add-open-thread thread)) - -(defmethod sync/event-handler :braid.client/receive-new-call - [[_ call]] - (dispatch! :receive-new-call call)) - -(defmethod sync/event-handler :braid.client/receive-new-call-status - [[_ [call status]]] - (dispatch! :set-receiver-call-status [call status])) - -(defmethod sync/event-handler :braid.client/receive-protocol-signal - [[_ signal]] - (rtc/receive-protocol-signal signal)) From 5489cbd1c8416d3bde9182a060cf7260ef9ac2b5 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Sat, 20 Aug 2016 16:55:38 -0400 Subject: [PATCH 91/94] Move call state to its own folder --- src/braid/client/calls/state.cljs | 9 +++++++++ src/braid/client/store.cljs | 5 +++-- 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 src/braid/client/calls/state.cljs diff --git a/src/braid/client/calls/state.cljs b/src/braid/client/calls/state.cljs new file mode 100644 index 000000000..e2f107a6c --- /dev/null +++ b/src/braid/client/calls/state.cljs @@ -0,0 +1,9 @@ +(ns braid.client.calls.state + (:require [schema.core :as s :include-macros true] + [braid.common.schema :as app-schema])) + +(def init-state + {:calls {}}) + +(def CallsState + {:calls {s/Uuid app-schema/Call}}) diff --git a/src/braid/client/store.cljs b/src/braid/client/store.cljs index bdbd75647..1347e855b 100644 --- a/src/braid/client/store.cljs +++ b/src/braid/client/store.cljs @@ -7,6 +7,7 @@ [braid.common.schema :as app-schema] [reagent.core :as r] [braid.client.state.helpers :as helpers] + [braid.client.calls.state :as calls] [braid.client.quests.state :as quests])) (defonce app-state @@ -31,8 +32,8 @@ :user {:open-thread-ids #{} :subscribed-tag-ids #{}} :new-thread-id (uuid/make-random-squuid) - :calls {} :focused-thread-id nil} + calls/init-state quests/init-state))) (def AppState @@ -62,8 +63,8 @@ :user {:open-thread-ids #{s/Uuid} :subscribed-tag-ids #{s/Uuid}} :new-thread-id s/Uuid - :calls {s/Uuid app-schema/Call} :focused-thread-id (s/maybe s/Uuid)} + calls/CallsState quests/QuestsState)) (def check-app-state! (s/validator AppState)) From 51da74fa02fd64b0bc245d6f473458da534ca0fe Mon Sep 17 00:00:00 2001 From: 10plusY Date: Sat, 20 Aug 2016 17:15:22 -0400 Subject: [PATCH 92/94] Add call button to user hover card --- src/braid/client/ui/views/pills.cljs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/braid/client/ui/views/pills.cljs b/src/braid/client/ui/views/pills.cljs index c604f42c0..5d239bf80 100644 --- a/src/braid/client/ui/views/pills.cljs +++ b/src/braid/client/ui/views/pills.cljs @@ -68,6 +68,20 @@ [tag-pill tag-id] [tag-car-view tag-id]]) +(defn call-button-view + [callee-id] + (let [caller-id (subscribe [:user-id]) + callee-status (subscribe [:user-status callee-id])] + (fn [callee-id] + (when (and (= @callee-status :online) + (not= @caller-id callee-id)) + [:a.button {:on-click + (fn [_] + (dispatch! :calls/start-new-call {:type :audio + :caller-id @caller-id + :callee-id callee-id}))} + "Call"])))) + (defn user-pill [user-id] (let [user (subscribe [:user user-id]) @@ -86,7 +100,8 @@ (let [user (subscribe [:user user-id]) open-group-id (subscribe [:open-group-id]) admin? (subscribe [:user-is-group-admin? user-id open-group-id]) - user-status (subscribe [:user-status user-id])] + user-status (subscribe [:user-status user-id]) + current-user-id (subscribe [:user-id])] (fn [user-id] [:div.card [:div.header {:style {:background-color (id->color user-id)}} @@ -108,7 +123,8 @@ [:div.actions ; [:a.pm "PM"] ; [:a.mute "Mute"] - [search-button-view (str "@" (@user :nickname))]]]))) + [search-button-view (str "@" (@user :nickname))] + [call-button-view user-id]]]))) (defn user-pill-view [user-id] From d1f6f9920ada4afb4391a62670cdab1a6ea6cf03 Mon Sep 17 00:00:00 2001 From: 10plusY Date: Sat, 20 Aug 2016 17:29:03 -0400 Subject: [PATCH 93/94] Fix remote-handlers dispatch names --- src/braid/client/calls/remote_handlers.cljs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/braid/client/calls/remote_handlers.cljs b/src/braid/client/calls/remote_handlers.cljs index bec8b7275..448ae7e8f 100644 --- a/src/braid/client/calls/remote_handlers.cljs +++ b/src/braid/client/calls/remote_handlers.cljs @@ -6,11 +6,11 @@ (defmethod sync/event-handler :braid.client/receive-new-call [[_ call]] - (dispatch! :receive-new-call call)) + (dispatch! :calls/receive-new-call call)) (defmethod sync/event-handler :braid.client/receive-new-call-status [[_ [call status]]] - (dispatch! :set-receiver-call-status [call status])) + (dispatch! :calls/set-receiver-call-status [call status])) (defmethod sync/event-handler :braid.client/receive-protocol-signal [[_ signal]] From 8b0e21a23117eab721aa4dfeb52d06f60f4161db Mon Sep 17 00:00:00 2001 From: 10plusY Date: Sat, 20 Aug 2016 17:29:43 -0400 Subject: [PATCH 94/94] Show call view when receiving call --- src/braid/client/calls/styles.cljs | 15 ++++++++++++ src/braid/client/calls/views.cljs | 35 +++------------------------ src/braid/client/ui/views/main.cljs | 2 ++ src/braid/client/ui/views/styles.cljs | 2 ++ 4 files changed, 23 insertions(+), 31 deletions(-) create mode 100644 src/braid/client/calls/styles.cljs diff --git a/src/braid/client/calls/styles.cljs b/src/braid/client/calls/styles.cljs new file mode 100644 index 000000000..8d8bdf4ea --- /dev/null +++ b/src/braid/client/calls/styles.cljs @@ -0,0 +1,15 @@ +(ns braid.client.calls.styles + (:require [garden.units :refer [em px rem]] + [garden.arithmetic :as m] + [braid.client.ui.styles.vars :as vars] + [braid.client.ui.styles.mixins :as mixins])) + +(defn call-view [] + [:.call + {:position "absolute" + :top "20px" + :left "20px" + :width "200px" + :height "200px" + :z-index 1005 + :background "white"}]) diff --git a/src/braid/client/calls/views.cljs b/src/braid/client/calls/views.cljs index a77792aab..86aaeb756 100644 --- a/src/braid/client/calls/views.cljs +++ b/src/braid/client/calls/views.cljs @@ -86,7 +86,7 @@ (reset! call-atom new-call)) :reagent-render (fn [call] - [:div + [:div.call (case @call-status :incoming [incoming-call-view call] :accepted [accepted-call-view call] @@ -94,35 +94,8 @@ :dropped [dropped-call-view call] :ended [ended-call-view call])])}))) -(defn before-call-view - [callee-id] - (let [callee-id-atom (r/atom callee-id) - caller-id (subscribe [:user-id]) - callee-nickname (subscribe [:nickname] [callee-id-atom])] - (r/create-class - {:display-name "before-call-view" - :component-will-receive-props - (fn [_ [_ new-callee-id]] - (reset! callee-id-atom new-callee-id)) - :reagent-render - (fn [callee-id] - [:div.call - [:h3 (str "Call " @callee-nickname)] - [:a.button {:on-click - (fn [_] - (dispatch! :calls/start-new-call {:type :audio - :caller-id @caller-id - :callee-id callee-id}))} "Audio"] - [:a.button {:on-click - (fn [_] - (dispatch! :calls/start-new-call {:type :video - :caller-id @caller-id - :callee-id callee-id}))} "Video"]])}))) - (defn call-view [] - (let [callee-id (subscribe [:page-id]) - new-call (subscribe [:new-call])] + (let [new-call (subscribe [:new-call])] (fn [] - (if @new-call - [during-call-view @new-call] - [before-call-view @callee-id])))) + (when @new-call + [during-call-view @new-call])))) diff --git a/src/braid/client/ui/views/main.cljs b/src/braid/client/ui/views/main.cljs index add312029..13b0c300a 100644 --- a/src/braid/client/ui/views/main.cljs +++ b/src/braid/client/ui/views/main.cljs @@ -4,6 +4,7 @@ [braid.client.ui.views.error-banner :refer [error-banner-view]] [braid.client.ui.views.sidebar :refer [sidebar-view]] [braid.client.ui.views.header :refer [header-view]] + [braid.client.calls.views :refer [call-view]] [braid.client.ui.views.pages.inbox :refer [inbox-page-view]] [braid.client.ui.views.pages.recent :refer [recent-page-view]] [braid.client.ui.views.pages.users :refer [users-page-view]] @@ -48,4 +49,5 @@ [sidebar-view] (when @group-id [header-view]) + [call-view] [page-view]]))) diff --git a/src/braid/client/ui/views/styles.cljs b/src/braid/client/ui/views/styles.cljs index 9891e3d53..48e84824b 100644 --- a/src/braid/client/ui/views/styles.cljs +++ b/src/braid/client/ui/views/styles.cljs @@ -17,6 +17,7 @@ [braid.client.ui.styles.pages.me] [braid.client.ui.styles.pages.bots] [braid.client.ui.styles.pages.uploads] + [braid.client.calls.styles] [braid.client.ui.styles.vars :as vars])) (defn styles-view [] @@ -50,6 +51,7 @@ braid.client.ui.styles.pills/tag braid.client.ui.styles.pills/user braid.client.ui.styles.misc/status + (braid.client.calls.styles/call-view) (braid.client.ui.styles.misc/threads vars/pad) (braid.client.ui.styles.thread/thread vars/pad) (braid.client.ui.styles.thread/head vars/pad)