dsh-apk-vm 0.12.0 → 0.12.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/instances.js CHANGED
@@ -111,6 +111,10 @@ export class ExtraVmInstance {
111
111
  // AndroidVm would attach to whichever adb device happens to be online
112
112
  // (normally the first/default VM).
113
113
  serial: `emulator-${port}`,
114
+ // Media (virtual camera/mic) flags resolved by the plugin at create time;
115
+ // they are stored by AndroidVm and used on every emulator (re)spawn.
116
+ extraEmulatorArgs: options.extraEmulatorArgs,
117
+ enableAudio: options.enableAudio === true,
114
118
  log: (line) => this._log(line),
115
119
  });
116
120
  this.stream = options.scrcpyJarPath
@@ -391,6 +395,10 @@ export class InstanceManager {
391
395
  this.dnsServers = Array.isArray(options.dnsServers) ? options.dnsServers.map(String) : [];
392
396
  this.networkMode = options.networkMode === 'shared' ? 'shared' : 'isolated';
393
397
  this.tcpdumpPath = options.tcpdumpPath ? String(options.tcpdumpPath) : null;
398
+ // Optional callback returning the latest media launch options
399
+ // ({extraEmulatorArgs, enableAudio}); resolved for every new instance.
400
+ this.mediaOptionsProvider = typeof options.mediaOptionsProvider === 'function' ? options.mediaOptionsProvider : null;
401
+ this.mediaOptions = null;
394
402
  this.logSink = typeof options.log === 'function' ? options.log : null;
395
403
  this.instances = new Map();
396
404
  this.agentInstances = new Map();
@@ -414,7 +422,7 @@ export class InstanceManager {
414
422
  id: instance.id,
415
423
  avdName: instance.avdName,
416
424
  port: instance.port,
417
- kind: 'extra',
425
+ kind: instance.kind || 'extra',
418
426
  state: instance.vm._state || 'stopped',
419
427
  serial: instance.vm._serialActive || instance.vm.serial || null,
420
428
  root: instance.vm._root === true,
@@ -652,6 +660,18 @@ export class InstanceManager {
652
660
  avdPresent = true;
653
661
  }
654
662
  await patchAvdConfig(avdName);
663
+ // Resolve the media (virtual camera/mic) flags at create time so the new
664
+ // AndroidVm can append them on every emulator spawn. A broken provider
665
+ // must never block VM creation.
666
+ let mediaOptions = null;
667
+ if (this.mediaOptionsProvider) {
668
+ try {
669
+ const resolved = this.mediaOptionsProvider();
670
+ if (resolved && typeof resolved === 'object') mediaOptions = resolved;
671
+ } catch (error) {
672
+ this._log(`[instances] mediaOptionsProvider failed: ${error && error.message ? error.message : error}`);
673
+ }
674
+ }
655
675
  const instance = new ExtraVmInstance({
656
676
  id,
657
677
  avdName,
@@ -673,6 +693,10 @@ export class InstanceManager {
673
693
  scrcpyMaxSize: this.scrcpyMaxSize,
674
694
  scrcpyBitrate: this.scrcpyBitrate,
675
695
  scrcpyMaxFps: this.scrcpyMaxFps,
696
+ extraEmulatorArgs: mediaOptions && Array.isArray(mediaOptions.extraEmulatorArgs)
697
+ ? mediaOptions.extraEmulatorArgs
698
+ : undefined,
699
+ enableAudio: mediaOptions ? mediaOptions.enableAudio === true : undefined,
676
700
  },
677
701
  log: (line) => this._log(line),
678
702
  });
@@ -713,6 +737,75 @@ export class InstanceManager {
713
737
  return { ok: true, id };
714
738
  }
715
739
 
740
+ /**
741
+ * Register an externally managed VM (a remote SSH-tunnel session) so the
742
+ * existing per-instance HTTP routes can serve it like an extra VM.
743
+ *
744
+ * The caller (RemoteSessionManager) owns the lifecycle. Remote instances do
745
+ * not count against `maxInstances` (the session manager enforces `maxSessions`)
746
+ * and are skipped by reapIdle(): stopping a remote VM would tear down its SSH
747
+ * tunnel, which the session manager must decide explicitly.
748
+ */
749
+ addRemoteInstance(instance) {
750
+ if (!instance || typeof instance.id !== 'string' || !instance.id) {
751
+ throw new Error('addRemoteInstance requires an instance with an id');
752
+ }
753
+ this.instances.set(instance.id, instance);
754
+ this._log(`[instances] registered remote instance ${instance.id}`);
755
+ if (typeof this.onCreated === 'function') {
756
+ try {
757
+ this.onCreated(instance.id);
758
+ } catch {
759
+ /* route registration is best effort */
760
+ }
761
+ }
762
+ return instance;
763
+ }
764
+
765
+ /** Remove a VM from the routable set without disposing it (remote sessions). */
766
+ removeInstance(id) {
767
+ const instance = this.instances.get(id);
768
+ if (!instance) return null;
769
+ this.instances.delete(id);
770
+ this.leaseBook.release(id, null, { force: true });
771
+ for (const [agent, mapped] of this.agentInstances) {
772
+ if (mapped === id) this.agentInstances.delete(agent);
773
+ }
774
+ this._log(`[instances] unregistered instance ${id}`);
775
+ return instance;
776
+ }
777
+
778
+ /** Registered remote sessions (kind === 'remote'). */
779
+ remoteInstances() {
780
+ return [...this.instances.values()].filter((instance) => instance.kind === 'remote');
781
+ }
782
+
783
+ /**
784
+ * Remember the latest media launch options and apply them to every existing
785
+ * instance VM (local and remote). Nothing is restarted here: the caller
786
+ * decides when to stop/start a VM so the new flags take effect.
787
+ */
788
+ applyMediaOptions(media) {
789
+ this.mediaOptions = media && typeof media === 'object' ? media : null;
790
+ let applied = 0;
791
+ for (const instance of this.instances.values()) {
792
+ const vm = instance && instance.vm;
793
+ if (!vm) continue;
794
+ try {
795
+ if (this.mediaOptions && Array.isArray(this.mediaOptions.extraEmulatorArgs)) {
796
+ vm.extraEmulatorArgs = this.mediaOptions.extraEmulatorArgs.map(String).filter((arg) => arg.trim().length > 0);
797
+ } else if (!this.mediaOptions) {
798
+ vm.extraEmulatorArgs = [];
799
+ }
800
+ vm.enableAudio = this.mediaOptions ? this.mediaOptions.enableAudio === true : false;
801
+ applied += 1;
802
+ } catch {
803
+ /* best effort: a frozen/foreign VM is skipped */
804
+ }
805
+ }
806
+ return { ok: true, applied, media: this.mediaOptions ? { ...this.mediaOptions } : null };
807
+ }
808
+
716
809
  touch(id) {
717
810
  const instance = this.instances.get(id);
718
811
  if (instance) instance.touch();
@@ -722,6 +815,9 @@ export class InstanceManager {
722
815
  async reapIdle(now = Date.now()) {
723
816
  const stopped = [];
724
817
  for (const instance of this.instances.values()) {
818
+ // Remote sessions are owned by the RemoteSessionManager (killRemote and
819
+ // SSH tunnel teardown are explicit actions); never reap them here.
820
+ if (instance.kind === 'remote') continue;
725
821
  const idleMs = now - instance.lastUsedAt;
726
822
  const running = instance.vm._state === 'ready' || instance.vm._state === 'booting';
727
823
  const clients = instance.stream ? instance.stream.clients.size : 0;