Merge "Capture audio in the webrtc client and send it to the device" am: e36ce69c3d am: 6e37c4e713

Original change: https://android-review.googlesource.com/c/device/google/cuttlefish/+/1623499

Change-Id: I21585a4f23822ddcddb69010ec7ac05597dcd44b
diff --git a/host/frontend/webrtc_operator/assets/index.html b/host/frontend/webrtc_operator/assets/index.html
index 66eec25..cdbf549 100644
--- a/host/frontend/webrtc_operator/assets/index.html
+++ b/host/frontend/webrtc_operator/assets/index.html
@@ -34,6 +34,7 @@
           <div id='app_controls'>
             <h2 class="section-title">App Controls</h2>
             <div id="keyboardCaptureCtrl" title="Capture Keyboard"></div>
+            <div id="micCaptureCtrl" title="Capture Microphone"></div>
           </div>
           <hr>
           <div id='control_panel'>
diff --git a/host/frontend/webrtc_operator/assets/js/app.js b/host/frontend/webrtc_operator/assets/js/app.js
index 2ae6da7..097da13 100644
--- a/host/frontend/webrtc_operator/assets/js/app.js
+++ b/host/frontend/webrtc_operator/assets/js/app.js
@@ -20,6 +20,8 @@
   console.log('ConnectToDevice ', device_id);
   const keyboardCaptureCtrl = document.getElementById('keyboardCaptureCtrl');
   createToggleControl(keyboardCaptureCtrl, "keyboard", onKeyboardCaptureToggle);
+  const micCaptureCtrl = document.getElementById('micCaptureCtrl');
+  createToggleControl(micCaptureCtrl, "mic", onMicCaptureToggle);
 
   const deviceScreen = document.getElementById('deviceScreen');
   const deviceAudio = document.getElementById('deviceAudio');
@@ -320,6 +322,10 @@
     }
   }
 
+  function onMicCaptureToggle(enabled) {
+    deviceConnection.useMic(enabled);
+  }
+
   function onControlPanelButton(e) {
     if (e.type == 'mouseout' && e.which == 0) {
       // Ignore mouseout events if no mouse button is pressed.
diff --git a/host/frontend/webrtc_operator/assets/js/cf_webrtc.js b/host/frontend/webrtc_operator/assets/js/cf_webrtc.js
index ab78845..87e8715 100644
--- a/host/frontend/webrtc_operator/assets/js/cf_webrtc.js
+++ b/host/frontend/webrtc_operator/assets/js/cf_webrtc.js
@@ -82,9 +82,12 @@
 }
 
 class DeviceConnection {
-  constructor(pc, control) {
+  constructor(pc, control, audio_stream) {
     this._pc = pc;
     this._control = control;
+    this._audio_stream = audio_stream;
+    // Disable the microphone by default
+    this.useMic(false);
     this._inputChannel = createDataChannel(pc, 'input-channel');
     this._adbChannel = createDataChannel(pc, 'adb-channel', (msg) => {
       if (this._onAdbMessage) {
@@ -189,6 +192,10 @@
     this._controlChannel.send(msg);
   }
 
+  useMic(in_use) {
+    this._audio_stream.getTracks().forEach(track => track.enabled = in_use);
+  }
+
   // Provide a callback to receive control-related comms from the device
   onControlMessage(cb) {
     this._onControlMessage = cb;
@@ -379,7 +386,17 @@
     }
   }
   let pc = createPeerConnection(infraConfig, control);
-  let deviceConnection = new DeviceConnection(pc, control);
+
+  const audioStream =
+      await navigator.mediaDevices.getUserMedia({video: false, audio: true});
+  const audioTracks = audioStream.getAudioTracks();
+  if (audioTracks.length > 0) {
+    console.log(`Using Audio device: ${audioTracks[0].label}, with ${
+        audioTracks.length} tracks`);
+    audioTracks.forEach(track => pc.addTrack(track, audioStream));
+  }
+
+  let deviceConnection = new DeviceConnection(pc, control, audioStream);
   deviceConnection.description = deviceInfo;
   async function acceptOfferAndReplyAnswer(offer) {
     try {