Enable running in headless mode

b/124321327

Change-Id: I9c3e3820f860ac532e1815f9abb0586e7450b48d
diff --git a/snapshot b/snapshot
index 455d41f..548f139 100755
--- a/snapshot
+++ b/snapshot
@@ -23,6 +23,7 @@
 FILENAME_PREFIX = 'img'
 FILENAME_SUFFIX = '.png'
 AF_SYSFS_NODE = '/sys/module/ov5645_camera_mipi_v2/parameters/ov5645_af'
+HDMI_SYSFS_NODE = '/sys/class/drm/card0/card0-HDMI-A-1/status'
 
 # No of initial frames to throw away before camera has stabilized
 SCRAP_FRAMES = 1
@@ -49,6 +50,10 @@
     t. ! {leaky_q} ! videoconvert ! {sink_caps} ! {sink_element}
     '''
 
+def monitor_connected():
+  with open(HDMI_SYSFS_NODE, 'r') as hdmi_status:
+    status = hdmi_status.read()
+    return (status.rstrip() == 'connected')
 
 def on_bus_message(bus, message, loop):
   t = message.type
@@ -91,7 +96,7 @@
   src_caps = SRC_CAPS.format(width=SRC_WIDTH, height=SRC_HEIGHT, rate=SRC_RATE)
   sink_caps = SINK_CAPS.format(width=SINK_WIDTH, height=SINK_HEIGHT)
 
-  if snapinfo.oneshot:
+  if snapinfo.oneshot or not monitor_connected():
     screen_sink = FAKE_SINK
   else:
     screen_sink = SCREEN_SINK