Merge "Add BarringInfo to Cuttlefish RIL for Radio 1.5"
diff --git a/guest/hals/ril/libril/ril_service.cpp b/guest/hals/ril/libril/ril_service.cpp
index 86d4599..7349845 100755
--- a/guest/hals/ril/libril/ril_service.cpp
+++ b/guest/hals/ril/libril/ril_service.cpp
@@ -9994,7 +9994,7 @@
         RLOGD("registerService: starting android::hardware::radio::V1_5::IRadio %s for slot %d",
                 serviceNames[i], i);
         android::status_t status = radioService[i]->registerAsService(serviceNames[i]);
-        assert(status == android::OK);
+        LOG_ALWAYS_FATAL_IF(status != android::OK, "status %d", status);
 
         RLOGD("registerService: OemHook is enabled = %s", kOemHookEnabled ? "true" : "false");
         if (kOemHookEnabled) {
diff --git a/host/commands/fetcher/Android.bp b/host/commands/fetcher/Android.bp
index 0677d76..1aaf04f 100644
--- a/host/commands/fetcher/Android.bp
+++ b/host/commands/fetcher/Android.bp
@@ -34,6 +34,7 @@
         "libcurl",
         "libcrypto",
         "libgflags",
+        "liblog",
         "libssl",
         "libz",
         "libjsoncpp",
diff --git a/host/commands/run_cvd/launch.cc b/host/commands/run_cvd/launch.cc
index 52562fb..42e59b8 100644
--- a/host/commands/run_cvd/launch.cc
+++ b/host/commands/run_cvd/launch.cc
@@ -287,6 +287,7 @@
       webrtc.AddParameter("--certs_dir=", config.webrtc_certs_dir());
   }
 
+  webrtc.AddParameter("--http_server_port=", vsoc::GetPerInstanceDefault(8443));
   webrtc.AddParameter("--public_ip=", config.webrtc_public_ip());
   webrtc.AddParameter("--assets_dir=", config.webrtc_assets_dir());
 
diff --git a/host/frontend/gcastv2/https/Android.bp b/host/frontend/gcastv2/https/Android.bp
index e410f77..e4d135a 100644
--- a/host/frontend/gcastv2/https/Android.bp
+++ b/host/frontend/gcastv2/https/Android.bp
@@ -1,3 +1,18 @@
+//
+// Copyright (C) 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
 cc_library_static {
     name: "libhttps",
     host_supported: true,
diff --git a/host/frontend/gcastv2/https/BaseConnection.cpp b/host/frontend/gcastv2/https/BaseConnection.cpp
index 610fb5f..4041b97 100644
--- a/host/frontend/gcastv2/https/BaseConnection.cpp
+++ b/host/frontend/gcastv2/https/BaseConnection.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <https/BaseConnection.h>
 
 #include <https/SafeCallbackable.h>
diff --git a/host/frontend/gcastv2/https/BufferedSocket.cpp b/host/frontend/gcastv2/https/BufferedSocket.cpp
index 117fc35..0156b1c 100644
--- a/host/frontend/gcastv2/https/BufferedSocket.cpp
+++ b/host/frontend/gcastv2/https/BufferedSocket.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <https/BufferedSocket.h>
 
 #include <cassert>
diff --git a/host/frontend/gcastv2/https/ClientSocket.cpp b/host/frontend/gcastv2/https/ClientSocket.cpp
index 3b6623d..8529461 100644
--- a/host/frontend/gcastv2/https/ClientSocket.cpp
+++ b/host/frontend/gcastv2/https/ClientSocket.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <https/ClientSocket.h>
 
 #include <https/HTTPServer.h>
diff --git a/host/frontend/gcastv2/https/HTTPClientConnection.cpp b/host/frontend/gcastv2/https/HTTPClientConnection.cpp
index d589734..cc29c6e 100644
--- a/host/frontend/gcastv2/https/HTTPClientConnection.cpp
+++ b/host/frontend/gcastv2/https/HTTPClientConnection.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <https/HTTPClientConnection.h>
 
 #include <https/HTTPRequestResponse.h>
diff --git a/host/frontend/gcastv2/https/HTTPRequestResponse.cpp b/host/frontend/gcastv2/https/HTTPRequestResponse.cpp
index b61d436..3f05065 100644
--- a/host/frontend/gcastv2/https/HTTPRequestResponse.cpp
+++ b/host/frontend/gcastv2/https/HTTPRequestResponse.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <https/HTTPRequestResponse.h>
 
 #include <cerrno>
diff --git a/host/frontend/gcastv2/https/HTTPServer.cpp b/host/frontend/gcastv2/https/HTTPServer.cpp
index 6c59d18..e472142 100644
--- a/host/frontend/gcastv2/https/HTTPServer.cpp
+++ b/host/frontend/gcastv2/https/HTTPServer.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <https/HTTPServer.h>
 
 #include <https/ClientSocket.h>
diff --git a/host/frontend/gcastv2/https/PlainSocket.cpp b/host/frontend/gcastv2/https/PlainSocket.cpp
index 583567e..26ee574 100644
--- a/host/frontend/gcastv2/https/PlainSocket.cpp
+++ b/host/frontend/gcastv2/https/PlainSocket.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <https/PlainSocket.h>
 
 #include <sys/socket.h>
diff --git a/host/frontend/gcastv2/https/RunLoop.cpp b/host/frontend/gcastv2/https/RunLoop.cpp
index aec8275..9027343 100644
--- a/host/frontend/gcastv2/https/RunLoop.cpp
+++ b/host/frontend/gcastv2/https/RunLoop.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <https/RunLoop.h>
 
 #include <https/Support.h>
@@ -9,6 +25,9 @@
 #include <iostream>
 #include <unistd.h>
 
+#include <mutex>
+#include <condition_variable>
+
 bool RunLoop::QueueElem::operator<=(const QueueElem &other) const {
     if (mWhen) {
         if (other.mWhen) {
@@ -72,6 +91,35 @@
     return token;
 }
 
+bool RunLoop::postAndAwait(AsyncFunction fn) {
+    if (isCurrentThread()) {
+        // To wait from the runloop's thread would cause deadlock
+        post(fn);
+        return false;
+    }
+
+    std::mutex mtx;
+    bool ran = false;
+    std::condition_variable cond_var;
+
+    post([&cond_var, &mtx, &ran, fn](){
+        fn();
+        {
+            std::unique_lock<std::mutex> lock(mtx);
+            ran = true;
+            // Notify while holding the mutex, otherwise the condition variable
+            // could be destroyed before the call to notify_all.
+            cond_var.notify_all();
+        }
+    });
+
+    {
+        std::unique_lock<std::mutex> lock(mtx);
+        cond_var.wait(lock, [&ran](){ return ran;});
+    }
+    return ran;
+}
+
 RunLoop::Token RunLoop::postWithDelay(
         std::chrono::steady_clock::duration delay, AsyncFunction fn) {
     CHECK(fn != nullptr);
diff --git a/host/frontend/gcastv2/https/SSLSocket.cpp b/host/frontend/gcastv2/https/SSLSocket.cpp
index d575730..38ad1c4 100644
--- a/host/frontend/gcastv2/https/SSLSocket.cpp
+++ b/host/frontend/gcastv2/https/SSLSocket.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <https/SSLSocket.h>
 
 #include <https/SafeCallbackable.h>
diff --git a/host/frontend/gcastv2/https/ServerSocket.cpp b/host/frontend/gcastv2/https/ServerSocket.cpp
index 08774b0..8928475 100644
--- a/host/frontend/gcastv2/https/ServerSocket.cpp
+++ b/host/frontend/gcastv2/https/ServerSocket.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <https/ServerSocket.h>
 
 #include <https/ClientSocket.h>
diff --git a/host/frontend/gcastv2/https/Support.cpp b/host/frontend/gcastv2/https/Support.cpp
index b0aa615..0138365 100644
--- a/host/frontend/gcastv2/https/Support.cpp
+++ b/host/frontend/gcastv2/https/Support.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <https/Support.h>
 
 #include <cassert>
diff --git a/host/frontend/gcastv2/https/WebSocketHandler.cpp b/host/frontend/gcastv2/https/WebSocketHandler.cpp
index a8388c2..fe8c74e 100644
--- a/host/frontend/gcastv2/https/WebSocketHandler.cpp
+++ b/host/frontend/gcastv2/https/WebSocketHandler.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <https/WebSocketHandler.h>
 
 #include <https/ClientSocket.h>
diff --git a/host/frontend/gcastv2/https/include/https/BaseConnection.h b/host/frontend/gcastv2/https/include/https/BaseConnection.h
index eb96155..7dd7dc6 100644
--- a/host/frontend/gcastv2/https/include/https/BaseConnection.h
+++ b/host/frontend/gcastv2/https/include/https/BaseConnection.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include <https/BufferedSocket.h>
diff --git a/host/frontend/gcastv2/https/include/https/BufferedSocket.h b/host/frontend/gcastv2/https/include/https/BufferedSocket.h
index 1bce0c4..f6095ff 100644
--- a/host/frontend/gcastv2/https/include/https/BufferedSocket.h
+++ b/host/frontend/gcastv2/https/include/https/BufferedSocket.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include <https/RunLoop.h>
diff --git a/host/frontend/gcastv2/https/include/https/ClientSocket.h b/host/frontend/gcastv2/https/include/https/ClientSocket.h
index b7847b2..fd29daf 100644
--- a/host/frontend/gcastv2/https/include/https/ClientSocket.h
+++ b/host/frontend/gcastv2/https/include/https/ClientSocket.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <https/BufferedSocket.h>
 
 #include <https/WebSocketHandler.h>
diff --git a/host/frontend/gcastv2/https/include/https/HTTPClientConnection.h b/host/frontend/gcastv2/https/include/https/HTTPClientConnection.h
index 16c73bc..357136f 100644
--- a/host/frontend/gcastv2/https/include/https/HTTPClientConnection.h
+++ b/host/frontend/gcastv2/https/include/https/HTTPClientConnection.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include <arpa/inet.h>
diff --git a/host/frontend/gcastv2/https/include/https/HTTPRequestResponse.h b/host/frontend/gcastv2/https/include/https/HTTPRequestResponse.h
index db89ba2..e353cbc 100644
--- a/host/frontend/gcastv2/https/include/https/HTTPRequestResponse.h
+++ b/host/frontend/gcastv2/https/include/https/HTTPRequestResponse.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include <cstdint>
diff --git a/host/frontend/gcastv2/https/include/https/HTTPServer.h b/host/frontend/gcastv2/https/include/https/HTTPServer.h
index 07996ae..d003e87 100644
--- a/host/frontend/gcastv2/https/include/https/HTTPServer.h
+++ b/host/frontend/gcastv2/https/include/https/HTTPServer.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include <https/ServerSocket.h>
diff --git a/host/frontend/gcastv2/https/include/https/PlainSocket.h b/host/frontend/gcastv2/https/include/https/PlainSocket.h
index f58649c..634c5a7 100644
--- a/host/frontend/gcastv2/https/include/https/PlainSocket.h
+++ b/host/frontend/gcastv2/https/include/https/PlainSocket.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include <https/BufferedSocket.h>
diff --git a/host/frontend/gcastv2/https/include/https/RunLoop.h b/host/frontend/gcastv2/https/include/https/RunLoop.h
index c189a20..7987cde 100644
--- a/host/frontend/gcastv2/https/include/https/RunLoop.h
+++ b/host/frontend/gcastv2/https/include/https/RunLoop.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include <atomic>
@@ -35,6 +51,12 @@
     typedef int32_t Token;
 
     Token post(AsyncFunction fn);
+    // Post a callback to the run loop and wait for it to be executed. Returns
+    // whether it actually waited for the execution to happen (if posted from
+    // the same run loop's thread it won't wait to avoid a deadlock).
+    // WARNING: This function can cause the calling thread to wait forever if
+    // the run loop is stopped.
+    bool postAndAwait(AsyncFunction fn);
 
     Token postWithDelay(
             std::chrono::steady_clock::duration delay, AsyncFunction fn);
diff --git a/host/frontend/gcastv2/https/include/https/SSLSocket.h b/host/frontend/gcastv2/https/include/https/SSLSocket.h
index 9e2ce57..7b204d8 100644
--- a/host/frontend/gcastv2/https/include/https/SSLSocket.h
+++ b/host/frontend/gcastv2/https/include/https/SSLSocket.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <https/BufferedSocket.h>
 
 #include <openssl/bio.h>
diff --git a/host/frontend/gcastv2/https/include/https/SafeCallbackable.h b/host/frontend/gcastv2/https/include/https/SafeCallbackable.h
index 26fdff8..9e88813 100644
--- a/host/frontend/gcastv2/https/include/https/SafeCallbackable.h
+++ b/host/frontend/gcastv2/https/include/https/SafeCallbackable.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include <memory>
diff --git a/host/frontend/gcastv2/https/include/https/ServerSocket.h b/host/frontend/gcastv2/https/include/https/ServerSocket.h
index 4a6e41e..d422a82 100644
--- a/host/frontend/gcastv2/https/include/https/ServerSocket.h
+++ b/host/frontend/gcastv2/https/include/https/ServerSocket.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include <cstdint>
diff --git a/host/frontend/gcastv2/https/include/https/Support.h b/host/frontend/gcastv2/https/include/https/Support.h
index 13c8bd6..6ab5520 100644
--- a/host/frontend/gcastv2/https/include/https/Support.h
+++ b/host/frontend/gcastv2/https/include/https/Support.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include <sys/types.h>
diff --git a/host/frontend/gcastv2/https/include/https/WebSocketHandler.h b/host/frontend/gcastv2/https/include/https/WebSocketHandler.h
index 82a5f0c..7670876 100644
--- a/host/frontend/gcastv2/https/include/https/WebSocketHandler.h
+++ b/host/frontend/gcastv2/https/include/https/WebSocketHandler.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include <arpa/inet.h>
diff --git a/host/frontend/gcastv2/libsource/AudioSource.cpp b/host/frontend/gcastv2/libsource/AudioSource.cpp
index 35e24cc..c3f40dd 100644
--- a/host/frontend/gcastv2/libsource/AudioSource.cpp
+++ b/host/frontend/gcastv2/libsource/AudioSource.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <source/AudioSource.h>
 
 #include <libyuv/convert.h>
diff --git a/host/frontend/gcastv2/libsource/FrameBufferSource.cpp b/host/frontend/gcastv2/libsource/FrameBufferSource.cpp
index ef16516..f473c19 100644
--- a/host/frontend/gcastv2/libsource/FrameBufferSource.cpp
+++ b/host/frontend/gcastv2/libsource/FrameBufferSource.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <source/FrameBufferSource.h>
 
 #include <algorithm>
diff --git a/host/frontend/gcastv2/libsource/HostToGuestComms.cpp b/host/frontend/gcastv2/libsource/HostToGuestComms.cpp
index c9b2e7a..44b9b5b 100644
--- a/host/frontend/gcastv2/libsource/HostToGuestComms.cpp
+++ b/host/frontend/gcastv2/libsource/HostToGuestComms.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <source/HostToGuestComms.h>
 
 #include <https/SafeCallbackable.h>
diff --git a/host/frontend/gcastv2/libsource/StreamingSource.cpp b/host/frontend/gcastv2/libsource/StreamingSource.cpp
index 3c29bba..d96bf2b 100644
--- a/host/frontend/gcastv2/libsource/StreamingSource.cpp
+++ b/host/frontend/gcastv2/libsource/StreamingSource.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <source/StreamingSource.h>
 
 namespace android {
diff --git a/host/frontend/gcastv2/libsource/TouchSink.cpp b/host/frontend/gcastv2/libsource/TouchSink.cpp
index 66efd95..dc0c309 100644
--- a/host/frontend/gcastv2/libsource/TouchSink.cpp
+++ b/host/frontend/gcastv2/libsource/TouchSink.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <source/TouchSink.h>
 
 #include <https/SafeCallbackable.h>
diff --git a/host/frontend/gcastv2/libsource/include/source/HostToGuestComms.h b/host/frontend/gcastv2/libsource/include/source/HostToGuestComms.h
index 79426a8..e3c0cef 100644
--- a/host/frontend/gcastv2/libsource/include/source/HostToGuestComms.h
+++ b/host/frontend/gcastv2/libsource/include/source/HostToGuestComms.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include <https/RunLoop.h>
@@ -10,9 +26,6 @@
 struct HostToGuestComms : std::enable_shared_from_this<HostToGuestComms> {
     using ReceiveCb = std::function<void(const void *data, size_t size)>;
 
-    // Used to communicate with the guest userspace "RemoterService".
-    static constexpr uint16_t kPortMain = 8555;
-
     // Used to carry updated framebuffers from guest to host.
     static constexpr uint16_t kPortVideo = 5580;
 
diff --git a/host/frontend/gcastv2/libsource/include/source/StreamingSink.h b/host/frontend/gcastv2/libsource/include/source/StreamingSink.h
index fbfb877..2d308be 100644
--- a/host/frontend/gcastv2/libsource/include/source/StreamingSink.h
+++ b/host/frontend/gcastv2/libsource/include/source/StreamingSink.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include <cinttypes>
diff --git a/host/frontend/gcastv2/libsource/include/source/TouchSink.h b/host/frontend/gcastv2/libsource/include/source/TouchSink.h
index b3d6cc7..048b523 100644
--- a/host/frontend/gcastv2/libsource/include/source/TouchSink.h
+++ b/host/frontend/gcastv2/libsource/include/source/TouchSink.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include <https/RunLoop.h>
diff --git a/host/frontend/gcastv2/webrtc/AdbWebSocketHandler.cpp b/host/frontend/gcastv2/webrtc/AdbWebSocketHandler.cpp
index ddc5aae..ae0f8af 100644
--- a/host/frontend/gcastv2/webrtc/AdbWebSocketHandler.cpp
+++ b/host/frontend/gcastv2/webrtc/AdbWebSocketHandler.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <webrtc/AdbWebSocketHandler.h>
 
 #include "Utils.h"
@@ -7,8 +23,6 @@
 
 #include <android-base/logging.h>
 
-#include <media/stagefright/foundation/hexdump.h>
-
 #include <unistd.h>
 
 using namespace android;
diff --git a/host/frontend/gcastv2/webrtc/Android.bp b/host/frontend/gcastv2/webrtc/Android.bp
index c804660..c048ff4 100644
--- a/host/frontend/gcastv2/webrtc/Android.bp
+++ b/host/frontend/gcastv2/webrtc/Android.bp
@@ -1,3 +1,18 @@
+//
+// Copyright (C) 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
 cc_library_static {
     name: "libwebrtc",
     srcs: [
@@ -21,13 +36,20 @@
         "libhttps",
         "libsrtp2",
         "libcuttlefish_host_config",
+        "libcuttlefish_screen_connector",
+        "libcuttlefish_wayland_server",
         "libgflags",
         "libjsoncpp",
         "libsource",
+        "libdrm",
+        "libffi",
+        "libwayland_server",
+        "libwayland_extension_server_protocols",
     ],
     shared_libs: [
         "libssl",
         "libbase",
+        "libcuttlefish_fs",
     ],
     header_libs: [
         "cuttlefish_common_headers",
@@ -49,6 +71,7 @@
     shared_libs: [
         "libbase",
         "libcrypto",
+        "libcuttlefish_fs",
         "libcuttlefish_utils",
         "libopus",
         "libssl",
@@ -57,12 +80,18 @@
     ],
     static_libs: [
         "libcuttlefish_host_config",
+        "libcuttlefish_screen_connector",
+        "libcuttlefish_wayland_server",
         "libgflags",
         "libhttps",
         "libjsoncpp",
         "libsource",
         "libsrtp2",
         "libwebrtc",
+        "libdrm",
+        "libffi",
+        "libwayland_server",
+        "libwayland_extension_server_protocols",
     ],
     cpp_std: "experimental",
     defaults: ["cuttlefish_host_only"],
diff --git a/host/frontend/gcastv2/webrtc/DTLS.cpp b/host/frontend/gcastv2/webrtc/DTLS.cpp
index 720529a..ea84842 100644
--- a/host/frontend/gcastv2/webrtc/DTLS.cpp
+++ b/host/frontend/gcastv2/webrtc/DTLS.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <webrtc/DTLS.h>
 
 #include <webrtc/RTPSocketHandler.h>
diff --git a/host/frontend/gcastv2/webrtc/G711Packetizer.cpp b/host/frontend/gcastv2/webrtc/G711Packetizer.cpp
index 8e9b27e..afbd7da 100644
--- a/host/frontend/gcastv2/webrtc/G711Packetizer.cpp
+++ b/host/frontend/gcastv2/webrtc/G711Packetizer.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <webrtc/G711Packetizer.h>
 
 #include "Utils.h"
diff --git a/host/frontend/gcastv2/webrtc/MyWebSocketHandler.cpp b/host/frontend/gcastv2/webrtc/MyWebSocketHandler.cpp
index 164fd0e..c167197 100644
--- a/host/frontend/gcastv2/webrtc/MyWebSocketHandler.cpp
+++ b/host/frontend/gcastv2/webrtc/MyWebSocketHandler.cpp
@@ -1,9 +1,23 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <webrtc/MyWebSocketHandler.h>
 
 #include "Utils.h"
 
-#include <media/stagefright/foundation/hexdump.h>
-
 #include <json/json.h>
 
 #include <netdb.h>
@@ -23,10 +37,6 @@
 }
 
 MyWebSocketHandler::~MyWebSocketHandler() {
-    for (auto rtp : mRTPs) {
-        mServerState->releasePort(rtp->getLocalPort());
-    }
-
     mServerState->releaseHandlerId(mId);
 }
 
@@ -329,12 +339,6 @@
     if (!(mOptions & OptionBits::bundleTracks) || mRTPs.empty()) {
         // Only allocate a local port once if we bundle tracks.
 
-        auto localPort = mServerState->acquirePort();
-
-        if (!localPort) {
-            return false;
-        }
-
         size_t sessionIndex = mlineIndex;
 
         uint32_t trackMask = 0;
@@ -364,7 +368,6 @@
                 mRunLoop,
                 mServerState,
                 PF_INET,
-                localPort,
                 trackMask,
                 session);
 
diff --git a/host/frontend/gcastv2/webrtc/OpusPacketizer.cpp b/host/frontend/gcastv2/webrtc/OpusPacketizer.cpp
index 44720fc..30086e8 100644
--- a/host/frontend/gcastv2/webrtc/OpusPacketizer.cpp
+++ b/host/frontend/gcastv2/webrtc/OpusPacketizer.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <webrtc/OpusPacketizer.h>
 
 #include "Utils.h"
diff --git a/host/frontend/gcastv2/webrtc/Packetizer.cpp b/host/frontend/gcastv2/webrtc/Packetizer.cpp
index 19d5ee4..450abc3 100644
--- a/host/frontend/gcastv2/webrtc/Packetizer.cpp
+++ b/host/frontend/gcastv2/webrtc/Packetizer.cpp
@@ -1,9 +1,25 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <webrtc/Packetizer.h>
 
 #include <webrtc/RTPSender.h>
 
 void Packetizer::queueRTPDatagram(std::vector<uint8_t> *packet) {
-    auto it = mSenders.begin(); 
+    auto it = mSenders.begin();
     while (it != mSenders.end()) {
         auto sender = it->lock();
         if (!sender) {
diff --git a/host/frontend/gcastv2/webrtc/RTPSender.cpp b/host/frontend/gcastv2/webrtc/RTPSender.cpp
index 87e6674..de05556 100644
--- a/host/frontend/gcastv2/webrtc/RTPSender.cpp
+++ b/host/frontend/gcastv2/webrtc/RTPSender.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <webrtc/RTPSender.h>
 
 #include "Utils.h"
@@ -6,7 +22,6 @@
 
 #include <https/SafeCallbackable.h>
 #include <https/Support.h>
-#include <media/stagefright/foundation/hexdump.h>
 
 #include <random>
 #include <unordered_set>
diff --git a/host/frontend/gcastv2/webrtc/RTPSession.cpp b/host/frontend/gcastv2/webrtc/RTPSession.cpp
index d67b557..8eeef1f 100644
--- a/host/frontend/gcastv2/webrtc/RTPSession.cpp
+++ b/host/frontend/gcastv2/webrtc/RTPSession.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <webrtc/RTPSession.h>
 
 #include <android-base/logging.h>
diff --git a/host/frontend/gcastv2/webrtc/RTPSocketHandler.cpp b/host/frontend/gcastv2/webrtc/RTPSocketHandler.cpp
index 6943ae0..51f052f 100644
--- a/host/frontend/gcastv2/webrtc/RTPSocketHandler.cpp
+++ b/host/frontend/gcastv2/webrtc/RTPSocketHandler.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <webrtc/RTPSocketHandler.h>
 
 #include <webrtc/MyWebSocketHandler.h>
@@ -20,6 +36,10 @@
 
 DECLARE_string(public_ip);
 
+// These are the ports we currently open in the firewall (15550..15557)
+static constexpr int kPortRangeBegin = 15550;
+static constexpr int kPortRangeEnd = 15558;
+
 static socklen_t getSockAddrLen(const sockaddr_storage &addr) {
     switch (addr.ss_family) {
         case AF_INET:
@@ -32,16 +52,53 @@
     }
 }
 
+static int acquirePort(int sockfd, int domain) {
+    sockaddr_storage addr;
+    uint16_t* port_ptr;
+
+    if (domain == PF_INET) {
+        sockaddr_in addrV4;
+        memset(addrV4.sin_zero, 0, sizeof(addrV4.sin_zero));
+        addrV4.sin_family = AF_INET;
+        addrV4.sin_addr.s_addr = INADDR_ANY;
+        memcpy(&addr, &addrV4, sizeof(addrV4));
+        port_ptr = &(reinterpret_cast<sockaddr_in*>(&addr)->sin_port);
+    } else {
+        CHECK_EQ(domain, PF_INET6);
+        sockaddr_in6 addrV6;
+        addrV6.sin6_family = AF_INET6;
+        addrV6.sin6_addr = in6addr_any;
+        addrV6.sin6_scope_id = 0;
+        memcpy(&addr, &addrV6, sizeof(addrV6));
+        port_ptr = &(reinterpret_cast<sockaddr_in6*>(&addr)->sin6_port);
+    }
+
+    int port = kPortRangeBegin;
+    for (;port < kPortRangeEnd; ++port) {
+        *port_ptr = htons(port);
+        errno = 0;
+        int res = bind(sockfd, reinterpret_cast<const sockaddr *>(&addr),
+                       getSockAddrLen(addr));
+        if (res == 0) {
+            return port;
+        }
+        if (errno != EADDRINUSE) {
+            return -1;
+        }
+        // else try the next port
+    }
+
+    return -1;
+}
+
 RTPSocketHandler::RTPSocketHandler(
         std::shared_ptr<RunLoop> runLoop,
         std::shared_ptr<ServerState> serverState,
         int domain,
-        uint16_t port,
         uint32_t trackMask,
         std::shared_ptr<RTPSession> session)
     : mRunLoop(runLoop),
       mServerState(serverState),
-      mLocalPort(port),
       mTrackMask(trackMask),
       mSession(session),
       mSendPending(false),
@@ -51,32 +108,9 @@
     makeFdNonblocking(sock);
     mSocket = std::make_shared<PlainSocket>(mRunLoop, sock);
 
-    sockaddr_storage addr;
+    mLocalPort = acquirePort(sock, domain);
 
-    if (domain == PF_INET) {
-        sockaddr_in addrV4;
-        memset(addrV4.sin_zero, 0, sizeof(addrV4.sin_zero));
-        addrV4.sin_family = AF_INET;
-        addrV4.sin_port = htons(port);
-        addrV4.sin_addr.s_addr = INADDR_ANY;
-        memcpy(&addr, &addrV4, sizeof(addrV4));
-    } else {
-        CHECK_EQ(domain, PF_INET6);
-
-        sockaddr_in6 addrV6;
-        addrV6.sin6_family = AF_INET6;
-        addrV6.sin6_port = htons(port);
-        addrV6.sin6_addr = in6addr_any;
-        addrV6.sin6_scope_id = 0;
-        memcpy(&addr, &addrV6, sizeof(addrV6));
-    }
-
-    int res = bind(
-            sock,
-            reinterpret_cast<const sockaddr *>(&addr),
-            getSockAddrLen(addr));
-
-    CHECK(!res);
+    CHECK(mLocalPort > 0);
 
     auto videoPacketizer =
         (trackMask & TRACK_VIDEO)
diff --git a/host/frontend/gcastv2/webrtc/SCTPHandler.cpp b/host/frontend/gcastv2/webrtc/SCTPHandler.cpp
index 58d1325..c89f925 100644
--- a/host/frontend/gcastv2/webrtc/SCTPHandler.cpp
+++ b/host/frontend/gcastv2/webrtc/SCTPHandler.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <webrtc/SCTPHandler.h>
 
 #include "Utils.h"
diff --git a/host/frontend/gcastv2/webrtc/SDP.cpp b/host/frontend/gcastv2/webrtc/SDP.cpp
index 6af4825..daedc79 100644
--- a/host/frontend/gcastv2/webrtc/SDP.cpp
+++ b/host/frontend/gcastv2/webrtc/SDP.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <webrtc/SDP.h>
 
 #include "Utils.h"
diff --git a/host/frontend/gcastv2/webrtc/STUNMessage.cpp b/host/frontend/gcastv2/webrtc/STUNMessage.cpp
index ed7b23b..92c3b3e 100644
--- a/host/frontend/gcastv2/webrtc/STUNMessage.cpp
+++ b/host/frontend/gcastv2/webrtc/STUNMessage.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <webrtc/STUNMessage.h>
 
 #include "Utils.h"
diff --git a/host/frontend/gcastv2/webrtc/ServerState.cpp b/host/frontend/gcastv2/webrtc/ServerState.cpp
index ec08d24..8f5fbbb 100644
--- a/host/frontend/gcastv2/webrtc/ServerState.cpp
+++ b/host/frontend/gcastv2/webrtc/ServerState.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <webrtc/ServerState.h>
 
 #include <webrtc/OpusPacketizer.h>
@@ -21,26 +37,8 @@
       mRunLoop(runLoop),
       mVideoFormat(videoFormat) {
 
-    // This is the list of ports we currently instruct the firewall to open.
-    mAvailablePorts.insert(
-            { 15550, 15551, 15552, 15553, 15554, 15555, 15556, 15557 });
-
     auto config = vsoc::CuttlefishConfig::Get();
 
-    mHostToGuestComms = std::make_shared<HostToGuestComms>(
-            mRunLoop,
-            false /* isServer */,
-            vsoc::GetDefaultPerInstanceVsockCid(),
-            HostToGuestComms::kPortMain,
-            [](const void *data, size_t size) {
-                (void)data;
-                LOG(VERBOSE) << "Received " << size << " bytes from guest.";
-            });
-
-    mHostToGuestComms->start();
-
-    changeResolution(1440 /* width */, 2880 /* height */, 524 /* dpi */);
-
     android::FrameBufferSource::Format fbSourceFormat;
     switch (videoFormat) {
         case VideoFormat::VP8:
@@ -53,39 +51,6 @@
     mFrameBufferSource =
         std::make_shared<android::FrameBufferSource>(fbSourceFormat);
 
-    if (FLAGS_frame_server_fd < 0) {
-        mFrameBufferComms = std::make_shared<HostToGuestComms>(
-            mRunLoop,
-            true /* isServer */,
-            VMADDR_CID_HOST,
-            HostToGuestComms::kPortVideo,
-            [this](const void *data, size_t size) {
-                LOG(VERBOSE)
-                    << "Received packet of "
-                    << size
-                    << " bytes of data from hwcomposer HAL.";
-
-                static_cast<android::FrameBufferSource *>(
-                        mFrameBufferSource.get())->injectFrame(data, size);
-            });
-    } else {
-        mFrameBufferComms = std::make_shared<HostToGuestComms>(
-            mRunLoop,
-            true /* isServer */,
-            FLAGS_frame_server_fd,
-            [this](const void *data, size_t size) {
-                LOG(VERBOSE)
-                    << "Received packet of "
-                    << size
-                    << " bytes of data from hwcomposer HAL.";
-
-                static_cast<android::FrameBufferSource *>(
-                        mFrameBufferSource.get())->injectFrame(data, size);
-            });
-    }
-
-    mFrameBufferComms->start();
-
     int32_t screenParams[4];
     screenParams[0] = config->x_res();
     screenParams[1] = config->y_res();
@@ -95,6 +60,11 @@
     static_cast<android::FrameBufferSource *>(
             mFrameBufferSource.get())->setScreenParams(screenParams);
 
+    mScreenConnector = std::shared_ptr<cvd::ScreenConnector>(
+        cvd::ScreenConnector::Get(FLAGS_frame_server_fd));
+    mScreenConnectorMonitor.reset(
+        new std::thread([this]() { MonitorScreenConnector(); }));
+
     mAudioSource = std::make_shared<android::AudioSource>(
             android::AudioSource::Format::OPUS);
 
@@ -125,6 +95,21 @@
     mTouchSink = touchSink;
 }
 
+void ServerState::MonitorScreenConnector() {
+    std::uint32_t last_frame = 0;
+    while (true) {
+      mScreenConnector->OnFrameAfter(last_frame, [this, &last_frame](
+                                                     std::uint32_t frame_num,
+                                                     std::uint8_t *data) {
+        mRunLoop->postAndAwait([this, data]() {
+          static_cast<android::FrameBufferSource *>(mFrameBufferSource.get())
+              ->injectFrame(data, cvd::ScreenConnector::ScreenSizeInBytes());
+        });
+        last_frame = frame_num;
+      });
+    }
+}
+
 std::shared_ptr<Packetizer> ServerState::getVideoPacketizer() {
     auto packetizer = mVideoPacketizer.lock();
     if (!packetizer) {
@@ -173,95 +158,6 @@
     CHECK_EQ(mAllocatedHandlerIds.erase(id), 1);
 }
 
-uint16_t ServerState::acquirePort() {
-    std::lock_guard autoLock(mPortLock);
-
-    if (mAvailablePorts.empty()) {
-        return 0;
-    }
-
-    uint16_t port = *mAvailablePorts.begin();
-    mAvailablePorts.erase(mAvailablePorts.begin());
-
-    return port;
-}
-
-void ServerState::releasePort(uint16_t port) {
-    CHECK(port);
-
-    std::lock_guard autoLock(mPortLock);
-    mAvailablePorts.insert(port);
-}
-
 std::shared_ptr<android::StreamingSink> ServerState::getTouchSink() {
     return mTouchSink;
 }
-
-void ServerState::changeResolution(
-        int32_t width, int32_t height, int32_t densityDpi) {
-    LOG(INFO)
-        << "Requested dimensions: "
-        << width
-        << "x"
-        << height
-        << " @"
-        << densityDpi
-        << " dpi";
-
-    /* Arguments "width", "height" and "densityDpi" need to be matched to the
-     * native screen dimensions specified as "launch_cvd" arguments "x_res"
-     * and "y_res".
-     */
-
-    auto config = vsoc::CuttlefishConfig::Get();
-    const int nativeWidth = config->x_res();
-    const int nativeHeight = config->y_res();
-
-    const float ratio = (float)width / (float)height;
-    int32_t outWidth = nativeWidth;
-    int32_t outHeight = (int32_t)((float)outWidth / ratio);
-
-    if (outHeight > nativeHeight) {
-        outHeight = nativeHeight;
-        outWidth = (int32_t)((float)outHeight * ratio);
-    }
-
-    const int32_t outDensity =
-        (int32_t)((float)densityDpi * (float)outWidth / (float)width);
-
-    LOG(INFO)
-        << "Scaled dimensions: "
-        << outWidth
-        << "x"
-        << outHeight
-        << " @"
-        << outDensity
-        << " dpi";
-
-    enum class PacketType : uint8_t {
-        CHANGE_RESOLUTION = 6,
-    };
-
-    static constexpr size_t totalSize =
-        sizeof(PacketType) + 3 * sizeof(int32_t);
-
-    std::unique_ptr<uint8_t[]> packet(new uint8_t[totalSize]);
-    size_t offset = 0;
-
-    auto append = [ptr = packet.get(), &offset](
-            const void *data, size_t len) {
-        CHECK_LE(offset + len, totalSize);
-        memcpy(ptr + offset, data, len);
-        offset += len;
-    };
-
-    static constexpr PacketType type = PacketType::CHANGE_RESOLUTION;
-    append(&type, sizeof(type));
-    append(&outWidth, sizeof(outWidth));
-    append(&outHeight, sizeof(outHeight));
-    append(&outDensity, sizeof(outDensity));
-
-    CHECK_EQ(offset, totalSize);
-
-    mHostToGuestComms->send(packet.get(), totalSize);
-}
diff --git a/host/frontend/gcastv2/webrtc/Utils.cpp b/host/frontend/gcastv2/webrtc/Utils.cpp
index 8e10038..56c6158 100644
--- a/host/frontend/gcastv2/webrtc/Utils.cpp
+++ b/host/frontend/gcastv2/webrtc/Utils.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include "Utils.h"
 
 std::vector<std::string> SplitString(const std::string &s, char c) {
diff --git a/host/frontend/gcastv2/webrtc/Utils.h b/host/frontend/gcastv2/webrtc/Utils.h
index 031665f..7dcc1db 100644
--- a/host/frontend/gcastv2/webrtc/Utils.h
+++ b/host/frontend/gcastv2/webrtc/Utils.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include <cstdint>
diff --git a/host/frontend/gcastv2/webrtc/VP8Packetizer.cpp b/host/frontend/gcastv2/webrtc/VP8Packetizer.cpp
index 92bcc1a..9f542af 100644
--- a/host/frontend/gcastv2/webrtc/VP8Packetizer.cpp
+++ b/host/frontend/gcastv2/webrtc/VP8Packetizer.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <webrtc/VP8Packetizer.h>
 
 #include "Utils.h"
diff --git a/host/frontend/gcastv2/webrtc/assets/index.html b/host/frontend/gcastv2/webrtc/assets/index.html
index fe4cb3c..69995fc 100644
--- a/host/frontend/gcastv2/webrtc/assets/index.html
+++ b/host/frontend/gcastv2/webrtc/assets/index.html
@@ -1,6 +1,6 @@
 <html>
     <head>
-        <title>My WebRTC Playground</title>
+        <title>My Virtual Device Playground</title>
 
         <link rel="stylesheet" type="text/css" href="style.css" >
     </head>
@@ -10,7 +10,7 @@
         <hr>
         <section class="noscroll">
             <div class="one" >
-                <video id="video" autoplay controls width="360" height="720" style="touch-action:none" ></video>
+                <video id="video" autoplay width="540" height="1080" style="touch-action:none" ></video>
             </div>
 
             <div class="two" >
diff --git a/host/frontend/gcastv2/webrtc/assets/style.css b/host/frontend/gcastv2/webrtc/assets/style.css
index d98614d..96b27e6 100644
--- a/host/frontend/gcastv2/webrtc/assets/style.css
+++ b/host/frontend/gcastv2/webrtc/assets/style.css
@@ -1,4 +1,6 @@
-body,
+body {
+    background-color:black
+},
 html {
 #    position: fixed;
 }
diff --git a/host/frontend/gcastv2/webrtc/include/webrtc/AdbWebSocketHandler.h b/host/frontend/gcastv2/webrtc/include/webrtc/AdbWebSocketHandler.h
index 4983835..0a622b2 100644
--- a/host/frontend/gcastv2/webrtc/include/webrtc/AdbWebSocketHandler.h
+++ b/host/frontend/gcastv2/webrtc/include/webrtc/AdbWebSocketHandler.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include <https/WebSocketHandler.h>
diff --git a/host/frontend/gcastv2/webrtc/include/webrtc/DTLS.h b/host/frontend/gcastv2/webrtc/include/webrtc/DTLS.h
index ea8ffeb..fc8738f 100644
--- a/host/frontend/gcastv2/webrtc/include/webrtc/DTLS.h
+++ b/host/frontend/gcastv2/webrtc/include/webrtc/DTLS.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include <https/RunLoop.h>
diff --git a/host/frontend/gcastv2/webrtc/include/webrtc/G711Packetizer.h b/host/frontend/gcastv2/webrtc/include/webrtc/G711Packetizer.h
index f1ec199..50cedae 100644
--- a/host/frontend/gcastv2/webrtc/include/webrtc/G711Packetizer.h
+++ b/host/frontend/gcastv2/webrtc/include/webrtc/G711Packetizer.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include "Packetizer.h"
diff --git a/host/frontend/gcastv2/webrtc/include/webrtc/MyWebSocketHandler.h b/host/frontend/gcastv2/webrtc/include/webrtc/MyWebSocketHandler.h
index 03b6764..a89b257 100644
--- a/host/frontend/gcastv2/webrtc/include/webrtc/MyWebSocketHandler.h
+++ b/host/frontend/gcastv2/webrtc/include/webrtc/MyWebSocketHandler.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include <webrtc/RTPSession.h>
diff --git a/host/frontend/gcastv2/webrtc/include/webrtc/OpusPacketizer.h b/host/frontend/gcastv2/webrtc/include/webrtc/OpusPacketizer.h
index b272620..60752a0 100644
--- a/host/frontend/gcastv2/webrtc/include/webrtc/OpusPacketizer.h
+++ b/host/frontend/gcastv2/webrtc/include/webrtc/OpusPacketizer.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include "Packetizer.h"
diff --git a/host/frontend/gcastv2/webrtc/include/webrtc/Packetizer.h b/host/frontend/gcastv2/webrtc/include/webrtc/Packetizer.h
index 14a4b8c..eb63bcd 100644
--- a/host/frontend/gcastv2/webrtc/include/webrtc/Packetizer.h
+++ b/host/frontend/gcastv2/webrtc/include/webrtc/Packetizer.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include <stdint.h>
diff --git a/host/frontend/gcastv2/webrtc/include/webrtc/RTPSender.h b/host/frontend/gcastv2/webrtc/include/webrtc/RTPSender.h
index fefa57b..3473fe4 100644
--- a/host/frontend/gcastv2/webrtc/include/webrtc/RTPSender.h
+++ b/host/frontend/gcastv2/webrtc/include/webrtc/RTPSender.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include "Packetizer.h"
diff --git a/host/frontend/gcastv2/webrtc/include/webrtc/RTPSession.h b/host/frontend/gcastv2/webrtc/include/webrtc/RTPSession.h
index f37321b..4faeb42 100644
--- a/host/frontend/gcastv2/webrtc/include/webrtc/RTPSession.h
+++ b/host/frontend/gcastv2/webrtc/include/webrtc/RTPSession.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include <https/RunLoop.h>
diff --git a/host/frontend/gcastv2/webrtc/include/webrtc/RTPSocketHandler.h b/host/frontend/gcastv2/webrtc/include/webrtc/RTPSocketHandler.h
index 1933ab5..b0f8ff8 100644
--- a/host/frontend/gcastv2/webrtc/include/webrtc/RTPSocketHandler.h
+++ b/host/frontend/gcastv2/webrtc/include/webrtc/RTPSocketHandler.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include <https/BufferedSocket.h>
@@ -28,7 +44,6 @@
             std::shared_ptr<RunLoop> runLoop,
             std::shared_ptr<ServerState> serverState,
             int domain,
-            uint16_t port,
             uint32_t trackMask,
             std::shared_ptr<RTPSession> session);
 
diff --git a/host/frontend/gcastv2/webrtc/include/webrtc/SCTPHandler.h b/host/frontend/gcastv2/webrtc/include/webrtc/SCTPHandler.h
index eabad73..af1b11c 100644
--- a/host/frontend/gcastv2/webrtc/include/webrtc/SCTPHandler.h
+++ b/host/frontend/gcastv2/webrtc/include/webrtc/SCTPHandler.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include <webrtc/DTLS.h>
diff --git a/host/frontend/gcastv2/webrtc/include/webrtc/SDP.h b/host/frontend/gcastv2/webrtc/include/webrtc/SDP.h
index c13bf61..b531d6d 100644
--- a/host/frontend/gcastv2/webrtc/include/webrtc/SDP.h
+++ b/host/frontend/gcastv2/webrtc/include/webrtc/SDP.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include <string>
diff --git a/host/frontend/gcastv2/webrtc/include/webrtc/STUNMessage.h b/host/frontend/gcastv2/webrtc/include/webrtc/STUNMessage.h
index 08a0731..1498f59 100644
--- a/host/frontend/gcastv2/webrtc/include/webrtc/STUNMessage.h
+++ b/host/frontend/gcastv2/webrtc/include/webrtc/STUNMessage.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include <cstdint>
diff --git a/host/frontend/gcastv2/webrtc/include/webrtc/ServerState.h b/host/frontend/gcastv2/webrtc/include/webrtc/ServerState.h
index 8432690..b56a7f9 100644
--- a/host/frontend/gcastv2/webrtc/include/webrtc/ServerState.h
+++ b/host/frontend/gcastv2/webrtc/include/webrtc/ServerState.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include "Packetizer.h"
@@ -13,6 +29,8 @@
 #include <mutex>
 #include <set>
 
+#include <host/libs/screen_connector/screen_connector.h>
+
 struct ServerState {
     using StreamingSink = android::StreamingSink;
 
@@ -32,9 +50,6 @@
     size_t acquireHandlerId();
     void releaseHandlerId(size_t id);
 
-    uint16_t acquirePort();
-    void releasePort(uint16_t port);
-
 private:
     using StreamingSource = android::StreamingSource;
 
@@ -49,16 +64,13 @@
 
     std::shared_ptr<StreamingSource> mAudioSource;
 
-    std::shared_ptr<HostToGuestComms> mHostToGuestComms;
-    std::shared_ptr<HostToGuestComms> mFrameBufferComms;
     std::shared_ptr<HostToGuestComms> mAudioComms;
+    std::shared_ptr<cvd::ScreenConnector> mScreenConnector;
+    std::shared_ptr<std::thread> mScreenConnectorMonitor;
 
     std::shared_ptr<StreamingSink> mTouchSink;
 
     std::set<size_t> mAllocatedHandlerIds;
 
-    std::mutex mPortLock;
-    std::set<uint16_t> mAvailablePorts;
-
-    void changeResolution(int32_t width, int32_t height, int32_t densityDpi);
+    void MonitorScreenConnector();
 };
diff --git a/host/frontend/gcastv2/webrtc/include/webrtc/VP8Packetizer.h b/host/frontend/gcastv2/webrtc/include/webrtc/VP8Packetizer.h
index 4783cb8..faa1e94 100644
--- a/host/frontend/gcastv2/webrtc/include/webrtc/VP8Packetizer.h
+++ b/host/frontend/gcastv2/webrtc/include/webrtc/VP8Packetizer.h
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #pragma once
 
 #include "Packetizer.h"
diff --git a/host/frontend/gcastv2/webrtc/webRTC.cpp b/host/frontend/gcastv2/webrtc/webRTC.cpp
index bd16f4c..8aaa30e 100644
--- a/host/frontend/gcastv2/webrtc/webRTC.cpp
+++ b/host/frontend/gcastv2/webrtc/webRTC.cpp
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #include <webrtc/AdbWebSocketHandler.h>
 #include <webrtc/DTLS.h>
 #include <webrtc/MyWebSocketHandler.h>
@@ -11,13 +27,14 @@
 #include <https/SafeCallbackable.h>
 #include <https/SSLSocket.h>
 #include <https/Support.h>
-#include <media/stagefright/foundation/hexdump.h>
 
 #include <iostream>
 #include <unordered_map>
 
 #include <gflags/gflags.h>
 
+DEFINE_int32(http_server_port, 8443, "The port for the http server.");
+DEFINE_bool(use_secure_http, true, "Whether to use HTTPS or HTTP.");
 DEFINE_string(
         public_ip,
         "0.0.0.0",
@@ -49,15 +66,15 @@
     auto state = std::make_shared<ServerState>(
             runLoop, ServerState::VideoFormat::VP8);
 
-    auto port = 8443;  // Change to 8080 to use plain http instead of https.
+    auto port = FLAGS_http_server_port;
 
     auto httpd = std::make_shared<HTTPServer>(
             runLoop,
             "0.0.0.0",
             port,
-            port == 8080
-                ? ServerSocket::TransportType::TCP
-                : ServerSocket::TransportType::TLS,
+            FLAGS_use_secure_http
+                ? ServerSocket::TransportType::TLS
+                : ServerSocket::TransportType::TCP,
             FLAGS_certs_dir + "/server.crt",
             FLAGS_certs_dir + "/server.key");
 
diff --git a/host/libs/screen_connector/screen_connector.h b/host/libs/screen_connector/screen_connector.h
index c369ab0..3a0e0e2 100644
--- a/host/libs/screen_connector/screen_connector.h
+++ b/host/libs/screen_connector/screen_connector.h
@@ -55,7 +55,7 @@
   }
 
   static inline int ScreenSizeInBytes() {
-      return ScreenStride() * ScreenHeight() * BytesPerPixel();
+      return ScreenStride() * ScreenHeight();
   }
 
  protected:
diff --git a/shared/sepolicy/vendor/file_contexts b/shared/sepolicy/vendor/file_contexts
index d58dac3..5000e6f 100644
--- a/shared/sepolicy/vendor/file_contexts
+++ b/shared/sepolicy/vendor/file_contexts
@@ -80,7 +80,7 @@
 /vendor/lib(64)?/libglapi.so  u:object_r:same_process_hal_file:s0
 /vendor/lib(64)?/dri/.* u:object_r:same_process_hal_file:s0
 /vendor/lib(64)?/hw/android\.hardware\.graphics\.mapper@2\.0-impl-2\.1\.so u:object_r:same_process_hal_file:s0
-/vendor/lib(64)?/android\.hardware\.health@2\.1-impl-cuttlefish  u:object_r:same_process_hal_file:s0
+/vendor/lib(64)?/hw/android\.hardware\.health@2\.0-impl-2\.1-cuttlefish\.so  u:object_r:same_process_hal_file:s0
 /vendor/lib(64)?/hw/gralloc.cutf_ashmem.so  u:object_r:same_process_hal_file:s0
 /vendor/lib(64)?/hw/gralloc.minigbm.so  u:object_r:same_process_hal_file:s0
 /vendor/lib(64)?/hw/vulkan.pastel.so  u:object_r:same_process_hal_file:s0
diff --git a/tools/create_base_image_arm.sh b/tools/create_base_image_arm.sh
index 64133e9..262d427 100755
--- a/tools/create_base_image_arm.sh
+++ b/tools/create_base_image_arm.sh
@@ -14,6 +14,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
+
 source "${ANDROID_BUILD_TOP}/external/shflags/src/shflags"
 
 DEFINE_boolean p1 \
@@ -142,7 +144,6 @@
 find_script=if test -e mmc ${devnum}:${distro_bootpart} /boot/boot.scr; then echo Found U-Boot script /boot/boot.scr; run run_scr; fi
 run_scr=load mmc ${devnum}:${distro_bootpart} ${scriptaddr} /boot/boot.scr; source ${scriptaddr}
 EOF
-	script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
 	echo "Sha=`${script_dir}/gen_sha.sh --kernel ${KERNEL_DIR}`" >> ${tmpfile}
 	${ANDROID_HOST_OUT}/bin/mkenvimage -s 32768 -o ${bootenv} - < ${tmpfile}
 fi