qt_player.pl 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 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 = 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 "silence" is always available -- reset if it goes away
  83. if (!exists($FILES{'SILENCE'})) {
  84. die("We need only the SILENCE\n");
  85. }
  86. }
  87. # Get things into a reasonable state
  88. sub init() {
  89. if ($DEBUG) {
  90. print STDERR "init()\n";
  91. }
  92. # Reset our local state
  93. %FILES = ();
  94. # Close all QT documents and relaunch QT Player
  95. Audio::runApplescript('tell application "QuickTime Player" to close every document');
  96. Audio::runApplescript('tell application "QuickTime Player" to quit');
  97. sleep(1);
  98. IPC::System::Simple::system('open', '-a', 'QuickTime Player');
  99. # Always load and play the SILENCE file (for sanity and general init)
  100. load('SILENCE', 'DMX/Silence.wav');
  101. # Bring Plex back to the front
  102. activatePlex();
  103. }
  104. # Open the named file and record the handle
  105. sub load($$) {
  106. my ($name, $path) = @_;
  107. if ($DEBUG) {
  108. print STDERR 'load(): ' . $name . ', ' . $path . "\n";
  109. }
  110. # Build a reasonable path
  111. if (!($path =~ /^\//)) {
  112. $path = $MEDIA_PATH . '/' . $path;
  113. }
  114. # Ensure the file exists
  115. if (!-r $path) {
  116. warn('Invalid audio file path: ' . $path . "\n");
  117. return;
  118. }
  119. # Ignore duplicate loads
  120. if (exists($FILES{$name})) {
  121. warn('Ignoring load request for existing file: ' . $name . "\n");
  122. return;
  123. }
  124. # Find out how many files are already open
  125. my $count = Audio::runApplescript('tell application "QuickTime Player" to count items of every document');
  126. # Open the file
  127. # It would be nice to use AppleScript here, so we can get a more definitive name, but the open() call is borked
  128. IPC::System::Simple::system('open', '-a', 'QuickTime Player', $path);
  129. # Wait for QT to load the new file
  130. # To avoid indefinate hangs this will give up after 5 seconds. When it does the results are undefined.
  131. my @cmd = ('tell application "QuickTime Player"');
  132. push(@cmd, 'set endDate to current date + (5/60 * minutes)');
  133. push(@cmd, 'repeat while (count items of every document) <= ' . $count . ' and current date < endDate');
  134. push(@cmd, 'delay 0.1');
  135. push(@cmd, 'end repeat');
  136. push(@cmd, 'get document 1');
  137. push(@cmd, 'end tell');
  138. my $doc = Audio::runApplescript(join("\n", @cmd));
  139. # Clean up the document name
  140. $doc =~ s/^\s*document //;
  141. $doc =~ s/\s+$//;
  142. $doc =~ s/\"/\\\"/g;
  143. $doc = '"' . $doc . '"';
  144. # Save the document handle
  145. $FILES{$name} = $doc;
  146. printFiles();
  147. # Bring Plex back to the front
  148. activatePlex();
  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. printFiles();
  166. }
  167. # Stop and rewind all QT documents
  168. sub stop() {
  169. Audio::runApplescript('tell application "QuickTime Player" to stop every document');
  170. Audio::runApplescript('tell application "QuickTime Player" to set current time of every document to 0');
  171. }
  172. # Update the output file list
  173. sub printFiles() {
  174. my ($fh, $tmp) = File::Temp::tempfile($OUTPUT_FILE . '.XXXXXXXX', 'UNLINK' => 0);
  175. foreach my $file (keys(%FILES)) {
  176. print $fh $file . '|' . $FILES{$file} . "\n";
  177. }
  178. close($fh);
  179. rename($tmp, $OUTPUT_FILE);
  180. }
  181. # Bring Plex to the front, if it's already running
  182. sub activatePlex() {
  183. my @cmd = ('tell application "System Events"');
  184. push(@cmd, 'if exists process "Plex" then');
  185. push(@cmd, 'tell application "Plex" to activate');
  186. push(@cmd, 'end if');
  187. push(@cmd, 'end tell');
  188. Audio::runApplescript(join("\n", @cmd));
  189. }