Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

player.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. var spawn = require("child_process").spawn;
  2. var fs = require("fs");
  3. var net = require("net");
  4. var Queue = require("../queue");
  5. var child = null;
  6. var ipcServer = process.cwd()+"/mpv-ipc-socket";
  7. function cmd(params, cb) {
  8. if (child == null || child.sock == null)
  9. return;
  10. child.sock.write(JSON.stringify({
  11. command: params
  12. })+"\n");
  13. child.msgqueue.dequeue(obj => {
  14. if (cb)
  15. cb(obj);
  16. });
  17. }
  18. function getState(cb) {
  19. if (child == null) {
  20. cb({
  21. playing: false,
  22. paused: false,
  23. muted: false,
  24. duration: 0,
  25. time_pos: 0,
  26. volume: 0,
  27. volume_max: 0
  28. });
  29. return;
  30. }
  31. var state = {
  32. playing: true
  33. };
  34. var cbs = 6;
  35. function next() {
  36. cbs -= 1;
  37. if (cbs === 0)
  38. cb(state);
  39. }
  40. cmd(["get_property", "pause"], res => {
  41. state.paused = res.data;
  42. next();
  43. });
  44. cmd(["get_property", "mute"], res => {
  45. state.muted = res.data;
  46. next();
  47. });
  48. cmd(["get_property", "duration"], res => {
  49. state.duration = res.data;
  50. next();
  51. });
  52. cmd(["get_property", "time-pos"], res => {
  53. state.time_pos = res.data;
  54. next();
  55. });
  56. cmd(["get_property", "volume"], res => {
  57. state.volume = res.data;
  58. next();
  59. });
  60. cmd(["get_property", "volume-max"], res => {
  61. state.volume_max = res.data;
  62. next();
  63. });
  64. }
  65. exports.isPlaying = function() {
  66. return child != null;
  67. }
  68. exports.play = function(path, cb) {
  69. exports.stop();
  70. var lchild = spawn("mpv", [
  71. path,
  72. "--input-ipc-server", ipcServer
  73. ], { stdio: "inherit" });
  74. child = lchild;
  75. lchild.running = true;
  76. lchild.once("close", () => {
  77. if (lchild.running) exports.stop();
  78. });
  79. lchild.on("error", err => console.error(err.toString()));
  80. lchild.state = {};
  81. lchild.msgqueue = Queue();
  82. console.log("lchild is "+lchild);
  83. lchild.initTimeout = setTimeout(() => {
  84. console.log("lchild still is "+lchild);
  85. // Create socket
  86. lchild.sock = net.connect(ipcServer, () => {
  87. // Add output from mpv to the queue
  88. lchild.sock.on("data", d => {
  89. d.toString().split("\n").forEach(str => {
  90. if (str == "") return;
  91. var obj = JSON.parse(str);
  92. if (obj.event) return;
  93. lchild.msgqueue.push(obj);
  94. });
  95. });
  96. lchild.sock.on("error", err => console.trace(err));
  97. // Set fullscreen
  98. cmd(["set_property", "fullscreen", "yes"]);
  99. cb();
  100. });
  101. }, 1000);
  102. }
  103. exports.stop = function() {
  104. if (child) {
  105. child.running = false;
  106. child.kill("SIGKILL");
  107. clearTimeout(child.initTimeout);
  108. child = null;
  109. }
  110. try {
  111. fs.unlinkSync(ipcServer);
  112. } catch (err) {}
  113. }
  114. exports.init = function(app) {
  115. function evt(p, cb) {
  116. app.post("/playback/"+p, (req, res) => cb(req, res));
  117. }
  118. evt("exit", (req, res) => { res.end(); exports.stop() });
  119. evt("state", (req, res) => {
  120. getState((state) => {
  121. res.json(state);
  122. });
  123. });
  124. evt("set/:key/:val", (req, res) => {
  125. cmd(["set_property", req.params.key, req.params.val]);
  126. res.end();
  127. });
  128. }