Friday, October 17, 2008

Reinventing a Wheel: HAL and CD/DVD Playback

My wife called me up at work:
- "I'm on a tight schedule, I need to get some work done and the kid is making me crazy; can she watch a DVD on your computer?"
- "Yeah sure, just insert the disc into the external drive, and I'll start the movie for you..."

So we're not beyond brain washing our offspring, but that's not the point. You're probably wondering why my wife would make such a call - can't she do that herself? isn't it just a matter of inserting the disc and let the operating system do its thing? and why use a computer instead of a TV/DVD set in the first place?

I'll start with the second question: our only TV is in another room, and our daughter preferred staying around her mom. As for the first question: normally you'd be right, if my computer would only be running Window$ or Linux with a Desktop Manager like GNOME or KDE, but it does not. I'm using awesome as my Window Manager, with no Desktop Manager, so I have to reinvent a few wheels sometimes.

So I went ahead, connected to my home computer via ssh, with a clear concept of the future: I'll just launch a media player from the command line to play the DVD, and ask my wife to take over and select the right DVD menu option. It took me several long minutes, during which both wife and daughter became impatient, before I was forced to admit failure. I did promise them to sort it out later. My wife was less than happy, and was rather verbal about it.

I ended up writing a script that uses HAL utilities to detect the type of media in the optical drive in order to launch the relevant playback application. The optical disc may be a video DVD, audio CD, or a data disc containing multimedia files in its root directory. The script also disables the GNOME screensaver during playback, and re-enables it when done.

It's not automatic, i.e. one has to manually run the script in order to start playback. I did, however, add a key binding to awesome so that my wife can launch the script herself, if she so wishes, by pressing Mod4+Shift+d (Mod4 refers, by default, to the Winodws-logo key on normal PC keyboards). I'm using version 2.3 of awesome, so this translates to the following lines in ~/.awesomerc:

key {
modkey = {"Mod4", "Shift"}
key = "d"
command = "spawn"
arg = "exec ~/bin/"

And here's the script ~/bin/ itself:

#! /bin/bash

gconftool-2 --set -t boolean /apps/gnome-screensaver/idle_activation_enabled false

udi=$(hal-find-by-property --key block.device --string $device | \
while read u ; do \
[[ "$(hal-get-property --udi $u --key block.is_volume)" == "true" ]] && \
[[ "$(hal-get-property --udi $u --key volume.is_disc)" == "true" ]] && \
[[ "$(hal-get-property --udi $u --key volume.disc.is_blank)" == "false" ]] && \
echo $u ; \

if [[ "$udi" != "" ]]; then
if [[ "$(hal-get-property --udi $udi --key volume.disc.has_audio)" == "true" ]]; then
DISPLAY=:0 sound-juicer --device $device --play
elif [[ "$(hal-get-property --udi $udi --key volume.disc.is_videodvd)" == "true" ]]; then
DISPLAY=:0 xine -f dvd:///$device
elif [[ "$(hal-get-property --udi $udi --key volume.disc.has_data)" == "true" ]]; then
if [[ "$(hal-get-property --udi $udi --key volume.is_mounted)" == "false" ]]; then
pmount $device
DISPLAY=:0 xine -f "$(hal-get-property --udi $udi --key volume.mount_point)"
pumount $device

gconftool-2 --set -t boolean /apps/gnome-screensaver/idle_activation_enabled true

  1. The script accepts, as a command line argument, an optional device path to use instead of the default /dev/scd0 (the path to my external LG DVD re-writer).

  2. The prefix DISPLAY=:0 is used to ensure that playback starts on the default display on my home machine.

  3. xine is used for video playback, but that's a matter of personal taste.

  4. pmount is used to mount removable storage devices, but mount is also fine, as long as /etc/fstab has a correct entry for the drive in question, e.g.:

    /dev/scd0 /media/cdrom1 udf,iso9660 user,noauto 0 0

  5. It's necessary to unmount a data disc after playback, so that it can be manually ejected from the drive.

All together now: "Daisy, Daisy..."

[30 Oct 2008] UPDATE: fixed script to ignore blank media in drive.

No comments:

Post a Comment