qt_player.pl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 filesMatch($$);
  21. sub dieUnloaded($);
  22. # App config
  23. my $DATA_DIR = DMX::dataDir();
  24. my $OUTPUT_FILE = $DATA_DIR . 'QT_PLAYER';
  25. my $STATE_SOCK = $OUTPUT_FILE . '.socket';
  26. my $DELAY = 10;
  27. my $MAX_CMD_LEN = 16384;
  28. # Debug
  29. my $DEBUG = 0;
  30. if ($ENV{'DEBUG'}) {
  31. $DEBUG = 1;
  32. }
  33. # Reset QT Player
  34. init();
  35. # Clear our output file
  36. if (-e $OUTPUT_FILE) {
  37. unlink($OUTPUT_FILE);
  38. }
  39. # Sockets
  40. my $SELECT = DMX::selectSock($STATE_SOCK);
  41. # Loop forever
  42. while (1) {
  43. # Save the last file list
  44. my %lastFiles = %FILES;
  45. # Wait for commands
  46. {
  47. my @ready_clients = $SELECT->can_read($DELAY);
  48. foreach my $fh (@ready_clients) {
  49. # Ensure we won't block on recv()
  50. $fh->blocking(0);
  51. # Read
  52. while (defined($fh->recv(my $text, $MAX_CMD_LEN))) {
  53. # Parse
  54. my @argv = split(/\|/, $text);
  55. # Ignore empty commands
  56. if (scalar(@argv) < 1) {
  57. next;
  58. }
  59. # Dispatch commands
  60. my $cmd = shift(@argv);
  61. if ($cmd eq 'LOAD' && scalar(@argv) == 2) {
  62. load($argv[0], $argv[1]);
  63. } elsif ($cmd eq 'UNLOAD' && scalar(@argv) == 1) {
  64. unload($argv[0]);
  65. } elsif ($cmd eq 'STOP') {
  66. stop();
  67. } elsif ($cmd eq 'RESET') {
  68. init();
  69. } else {
  70. warn('Ignored invalid command: ' . $text . "\n");
  71. }
  72. }
  73. }
  74. }
  75. # Ensure "silence" is always available
  76. if (!exists($FILES{'SILENCE'})) {
  77. init();
  78. }
  79. # Publish the file list whenever it changes, or if the output file does not exist
  80. if (filesMatch(\%lastFiles, \%FILES) || !-r $OUTPUT_FILE) {
  81. my ($fh, $tmp) = File::Temp::tempfile($OUTPUT_FILE . '.XXXXXXXX', 'UNLINK' => 0);
  82. foreach my $file (keys(%FILES)) {
  83. print $fh $file . '|' . $FILES{$file} . "\n";
  84. }
  85. close($fh);
  86. rename($tmp, $OUTPUT_FILE);
  87. }
  88. }
  89. # Get things into a reasonable state
  90. sub init() {
  91. if ($DEBUG) {
  92. print STDERR "init()\n";
  93. }
  94. # Reset our local state
  95. %FILES = ();
  96. # Close all QT documents and relaunch QT Player
  97. Audio::runApplescript('tell application "QuickTime Player" to close every document');
  98. Audio::runApplescript('tell application "QuickTime Player" to quit');
  99. sleep(1);
  100. IPC::System::Simple::system('open', '-a', 'QuickTime Player');
  101. # Always load and play the SILENCE file (for sanity and general init)
  102. load('SILENCE', 'DMX/Silence.wav');
  103. # Bring Plex back to the front
  104. Audio::runApplescript('tell application "Plex" to activate');
  105. }
  106. # Open the named file and record the handle
  107. sub load($$) {
  108. my ($name, $path) = @_;
  109. if ($DEBUG) {
  110. print STDERR 'load(): ' . $name . ', ' . $path . "\n";
  111. }
  112. # Build a reasonable path
  113. if (!($path =~ /^\//)) {
  114. $path = $MEDIA_PATH . '/' . $path;
  115. }
  116. # Ensure the file exists
  117. if (!-r $path) {
  118. warn('Invalid audio file path: ' . $path . "\n");
  119. return;
  120. }
  121. # Reload as needed
  122. if (exists($FILES{$name})) {
  123. unload($name);
  124. }
  125. # Find out how many files are already open
  126. my $count = Audio::runApplescript('tell application "QuickTime Player" to count items of every document');
  127. # Open the file
  128. # It would be nice to use AppleScript here, so we can get a more definitive name, but the open() call is borked
  129. IPC::System::Simple::system('open', '-a', 'QuickTime Player', $path);
  130. # Wait for QT to load the new file
  131. # To avoid indefinate hangs this will give up after 5 seconds. When it does the results are undefined.
  132. my @cmd = ('tell application "QuickTime Player"');
  133. push(@cmd, 'set endDate to current date + (5/60 * minutes)');
  134. push(@cmd, 'repeat while (count items of every document) <= ' . $count . ' and current date < endDate');
  135. push(@cmd, 'delay 0.1');
  136. push(@cmd, 'end repeat');
  137. push(@cmd, 'get document 1');
  138. push(@cmd, 'end tell');
  139. my $doc = Audio::runApplescript(join("\n", @cmd));
  140. # Clean up the document name
  141. $doc =~ s/^\s*document //;
  142. $doc =~ s/\s+$//;
  143. $doc =~ s/\"/\\\"/g;
  144. $doc = '"' . $doc . '"';
  145. # Save the document handle
  146. $FILES{$name} = $doc;
  147. # Bring Plex back to the front
  148. Audio::runApplescript('tell application "Plex" to activate');
  149. }
  150. # Close the named file
  151. sub unload($) {
  152. my ($name) = @_;
  153. if ($DEBUG) {
  154. print STDERR 'unload(): ' . $name . "\n";
  155. }
  156. # Ignore files that don't exist
  157. if (!exists($FILES{$name})) {
  158. warn('No such file loaded: ' . $name . "\n");
  159. return;
  160. }
  161. # Close to document
  162. Audio::runApplescript('tell application "QuickTime Player" to close document ' . $FILES{$name});
  163. # Delete our handle
  164. delete($FILES{$name});
  165. }
  166. # Stop and rewind all QT documents
  167. sub stop() {
  168. Audio::runApplescript('tell application "QuickTime Player" to stop every document');
  169. Audio::runApplescript('tell application "QuickTime Player" to set current time of every document to 0');
  170. }
  171. # Determine if anything has changed
  172. sub filesMatch($$) {
  173. my ($old, $new) = @_;
  174. if (scalar(keys(%{$old})) != scalar(keys(%{$new}))) {
  175. return 0;
  176. }
  177. foreach my $name (keys(%{$old})) {
  178. if (!exists($new->{$name})) {
  179. return 0;
  180. }
  181. if ($old->{$name} ne $new->{$name}) {
  182. return 0;
  183. }
  184. }
  185. return 1;
  186. }