qt_player.pl 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use File::Basename;
  5. use IPC::System::Simple;
  6. # Local modules
  7. use Cwd qw(abs_path);
  8. use File::Basename qw(dirname);
  9. use lib dirname(abs_path($0));
  10. use DMX;
  11. use Audio;
  12. # User config
  13. my $MEDIA_PATH = `~/bin/video/mediaPath`;
  14. my %FILES = ();
  15. # Prototypes
  16. sub init();
  17. sub load($$);
  18. sub unload($);
  19. sub stop();
  20. sub printFiles();
  21. sub cmdApp($$);
  22. # App config
  23. my $DATA_DIR = DMX::dataDir();
  24. my $STATE_SOCK = 'QT_PLAYER';
  25. my $OUTPUT_FILE = $DATA_DIR . $STATE_SOCK;
  26. my $DELAY = 5;
  27. my $MAX_CMD_LEN = 16384;
  28. # Debug
  29. my $DEBUG = 0;
  30. if ($ENV{'DEBUG'}) {
  31. $DEBUG = 1;
  32. }
  33. # Clear our output file
  34. if (-e $OUTPUT_FILE) {
  35. unlink($OUTPUT_FILE);
  36. }
  37. # Reset QT Player
  38. init();
  39. # Sockets
  40. my $SELECT = DMX::selectSock($STATE_SOCK);
  41. # Loop forever
  42. while (1) {
  43. # Wait for commands
  44. {
  45. my @ready_clients = $SELECT->can_read($DELAY);
  46. foreach my $fh (@ready_clients) {
  47. # Ensure we won't block on recv()
  48. $fh->blocking(0);
  49. # Read
  50. while (defined($fh->recv(my $text, $MAX_CMD_LEN))) {
  51. # Parse
  52. my @argv = split(/\|/, $text);
  53. # Ignore empty commands
  54. if (scalar(@argv) < 1) {
  55. next;
  56. }
  57. # Dispatch commands
  58. my $cmd = shift(@argv);
  59. if ($cmd eq 'LOAD' && scalar(@argv) == 2) {
  60. load($argv[0], $argv[1]);
  61. } elsif ($cmd eq 'UNLOAD' && scalar(@argv) == 1) {
  62. unload($argv[0]);
  63. } elsif ($cmd eq 'STOP') {
  64. stop();
  65. } elsif ($cmd eq 'RESET') {
  66. init();
  67. } else {
  68. warn('Ignored invalid command: ' . $text . "\n");
  69. }
  70. }
  71. }
  72. }
  73. # Keep our list of files in-sync with reality
  74. foreach my $name (keys(%FILES)) {
  75. eval { Audio::runApplescript('tell application "QuickTime Player" to get document ' . $FILES{$name}); };
  76. if ($@) {
  77. warn('File went missing: ' . $name . "\n");
  78. delete($FILES{$name});
  79. printFiles();
  80. }
  81. }
  82. # Ensure the output file always exists
  83. if (!-r $OUTPUT_FILE) {
  84. printFiles();
  85. }
  86. # Ensure "silence" is always available -- reset if it goes away
  87. if (!exists($FILES{'SILENCE'})) {
  88. die("We need only the SILENCE\n");
  89. }
  90. }
  91. # Get things into a reasonable state
  92. sub init() {
  93. if ($DEBUG) {
  94. print STDERR "init()\n";
  95. }
  96. # Reset our local state
  97. %FILES = ();
  98. # Close all QT documents and relaunch QT Player
  99. cmdApp('QuickTime Player', 'close every document');
  100. cmdApp('QuickTime Player', 'quit');
  101. sleep(1);
  102. IPC::System::Simple::system('open', '-a', 'QuickTime Player');
  103. # Always load and play the SILENCE file (for sanity and general init)
  104. load('SILENCE', 'DMX/Silence.wav');
  105. # Bring Plex back to the front
  106. cmdApp('Plex', 'activate');
  107. }
  108. # Open the named file and record the handle
  109. sub load($$) {
  110. my ($name, $path) = @_;
  111. if ($DEBUG) {
  112. print STDERR 'load(): ' . $name . ', ' . $path . "\n";
  113. }
  114. # Build a reasonable path
  115. if (!($path =~ /^\//)) {
  116. $path = $MEDIA_PATH . '/' . $path;
  117. }
  118. # Ensure the file exists
  119. if (!-r $path) {
  120. warn('Invalid audio file path: ' . $path . "\n");
  121. return;
  122. }
  123. # Ignore duplicate loads
  124. if (exists($FILES{$name})) {
  125. warn('Ignoring load request for existing file: ' . $name . "\n");
  126. return;
  127. }
  128. # Find out how many files are already open
  129. my $count = Audio::runApplescript('tell application "QuickTime Player" to count items of every document');
  130. # Open the file
  131. # It would be nice to use AppleScript here, so we can get a more definitive name, but the open() call is borked
  132. IPC::System::Simple::system('open', '-a', 'QuickTime Player', $path);
  133. # Wait for QT to load the new file
  134. # To avoid indefinate hangs this will give up after 5 seconds
  135. my @cmd = ('tell application "QuickTime Player"');
  136. push(@cmd, 'set endDate to current date + (5/60 * minutes)');
  137. push(@cmd, 'repeat while (count items of every document) <= ' . $count . ' and current date < endDate');
  138. push(@cmd, 'delay 0.1');
  139. push(@cmd, 'end repeat');
  140. push(@cmd, 'if (count items of every document) > ' . $count . ' then');
  141. push(@cmd, 'get document 1');
  142. push(@cmd, 'end if');
  143. push(@cmd, 'end tell');
  144. my $doc = Audio::runApplescript(join("\n", @cmd));
  145. # Reset if we cannot open -- something is out-of-sync
  146. if (!$doc) {
  147. init();
  148. return;
  149. }
  150. # Clean up the document name
  151. $doc =~ s/^\s*document //;
  152. $doc =~ s/\s+$//;
  153. $doc =~ s/\"/\\\"/g;
  154. $doc = '"' . $doc . '"';
  155. # Save the document handle
  156. $FILES{$name} = $doc;
  157. printFiles();
  158. # Bring Plex back to the front
  159. cmdApp('Plex', 'activate');
  160. }
  161. # Close the named file
  162. sub unload($) {
  163. my ($name) = @_;
  164. if ($DEBUG) {
  165. print STDERR 'unload(): ' . $name . "\n";
  166. }
  167. # Ignore files that don't exist
  168. if (!exists($FILES{$name})) {
  169. warn('No such file loaded: ' . $name . "\n");
  170. return;
  171. }
  172. # Close to document
  173. Audio::runApplescript('tell application "QuickTime Player" to close document ' . $FILES{$name});
  174. # Delete our handle
  175. delete($FILES{$name});
  176. printFiles();
  177. }
  178. # Stop and rewind all QT documents
  179. sub stop() {
  180. Audio::runApplescript('tell application "QuickTime Player" to stop every document');
  181. Audio::runApplescript('tell application "QuickTime Player" to set current time of every document to 0');
  182. }
  183. # Update the output file list
  184. sub printFiles() {
  185. my ($fh, $tmp) = File::Temp::tempfile($OUTPUT_FILE . '.XXXXXXXX', 'UNLINK' => 0);
  186. foreach my $file (keys(%FILES)) {
  187. print $fh $file . '|' . $FILES{$file} . "\n";
  188. }
  189. close($fh);
  190. rename($tmp, $OUTPUT_FILE);
  191. }
  192. # Request the specified action from the provided app, if it's already running
  193. sub cmdApp($$) {
  194. my ($app, $cmd) = @_;
  195. my @cmd = ('tell application "System Events"');
  196. push(@cmd, 'if exists process "' . $app . '" then');
  197. push(@cmd, 'tell application "' . $app . '" to ' . $cmd);
  198. push(@cmd, 'end if');
  199. push(@cmd, 'end tell');
  200. Audio::runApplescript(join("\n", @cmd));
  201. }