]> git.mdlowis.com Git - proto/atv.git/commitdiff
add assets and almost completed reorg. weird bug when transitioning to new video...
authorMichael D. Lowis <mike@mdlowis.com>
Mon, 8 Jan 2024 03:50:33 +0000 (22:50 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Mon, 8 Jan 2024 03:50:33 +0000 (22:50 -0500)
atv/assets/control.html [new file with mode: 0644]
atv/assets/index.html [new file with mode: 0644]
atv/bin/atv
atv/lib/atv/channel.rb
atv/lib/atv/player.rb
atv/lib/atv/server.rb

diff --git a/atv/assets/control.html b/atv/assets/control.html
new file mode 100644 (file)
index 0000000..9747101
--- /dev/null
@@ -0,0 +1,35 @@
+<!DOCTYPE html>
+<html dir="ltr" lang="en-US">
+<head>
+  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+  <meta name="viewport" content="width=device-width,initial-scale=1">
+  <style>
+    html, body {
+      margin: 0;
+      padding: 0;
+      width: 100%;
+      height: 100%;
+      max-height: 100%;
+      background-color: #000;
+      overflow: hidden;
+    }
+  </style>
+</head>
+<body>
+
+
+<input type="button" value="Skip" onclick="javscript:Client.skip()"/>
+<input type="button" value="Play" onclick="javscript:Client.play()"/>
+<input type="button" value="Pause" onclick="javscript:Client.pause()"/>
+<input type="button" value="Chan +" onclick="javscript:Client.chan_next()"/>
+<input type="button" value="Chan -" onclick="javscript:Client.chan_prev()"/>
+
+
+<script type="text/javascript" src="client.js"></script>
+<script>
+(()=>{
+    Client.connect(()=>{});
+})();
+</script>
+</body>
+</html>
diff --git a/atv/assets/index.html b/atv/assets/index.html
new file mode 100644 (file)
index 0000000..f6bc175
--- /dev/null
@@ -0,0 +1,61 @@
+<!DOCTYPE html>
+<html dir="ltr" lang="en-US">
+<head>
+  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+  <meta name="viewport" content="width=device-width,initial-scale=1">
+  <style>
+    html, body {
+      margin: 0;
+      padding: 0;
+      width: 100%;
+      height: 100%;
+      max-height: 100%;
+      background-color: #000;
+      overflow: hidden;
+    }
+  </style>
+</head>
+<body>
+<video id="Video" width="100%" height="100%" src="" autoplay muted playsinline>
+</video>
+
+<script>
+const updatePlayer = (data)=>{
+    console.log(data["curr"]);
+    console.log(data["curr"]["duration"])
+    const duration = data["curr"]["duration"];
+    const elapsed = data["time"]
+    if (elapsed > 0 && elapsed < duration)
+    {
+        Video.src = data["curr"]["path"] + "#t=" + Math.floor(elapsed);
+    }
+};
+
+const Cmd = {
+    play: (data)=>{
+        console.log(data);
+        updatePlayer(data);
+    }
+};
+
+const connect = ()=>{
+    let ws = new WebSocket(
+        "ws://" + window.location.host + ":3000");
+
+    ws.onmessage = (event)=>{
+        const msg = JSON.parse(event.data);
+        Cmd[msg.cmd](msg);
+    };
+
+    ws.onclose = (event)=>{
+        connect();
+    };
+};
+
+Video.addEventListener("ended", updatePlayer);
+document.addEventListener("visibilitychange", updatePlayer);
+
+(()=>{ connect(); })();
+</script>
+
+</body></html>
index f7a63f2646864f93553ac0f65023031eafb5f7d1..3a04355921e189271645d839c06a2674a91f5df5 100755 (executable)
@@ -11,5 +11,6 @@ channels = [
     ["Movies", "Shorts", "Shows"])
 ]
 player = ATV::Player.new(db, channels)
-server = ATV::Server.new(player)
-server.start
+ATV::Server.start(player)
+#server = ATV::Server.new(player)
+#server.start
index 3c63716fd17edf29ecc1ce345acad8cc02bce977..2cbb4c6f6ea9c3efb4a05c49107c11b1318742ca 100644 (file)
@@ -8,13 +8,14 @@ module ATV
       @items = selectors.map do |sel|
         files.select {|f| f["path"].start_with? sel }
       end.flatten.shuffle
+      @time = @items[@index]["duration"] - 10
     end
 
     def update(playing)
       @time += 1 if playing
       item = @items[@index]
       if @time >= item["duration"] then
-        next()
+        next_vid()
         pp @items[@index]
         true
       else
@@ -22,12 +23,22 @@ module ATV
       end
     end
 
-    def next()
-      @time = 0
+    def next_vid()
       @index += 1
       if @index >= @items.length
         @index = 0
       end
+      @time = 0
+      @time = @items[@index]["duration"] - 10
+    end
+
+    def state()
+       next_index = ((@index+1) >= @items.length ? 0 : (@index+1))
+      {
+        "time" => @time,
+        "curr" => @items[@index],
+        "next" => @items[next_index]
+      }
     end
   end
 end
index 1709e1af33eb0936bc100b1b5d01e441b925b872..d1de7f00ca55103989a3b735d7b7d23d88b93f43 100644 (file)
@@ -12,6 +12,8 @@ module ATV
       @channels.each do |c|
         updated ||= c.update(@playing)
       end
+#      puts updated
+      updated
     end
 
     def play()
@@ -39,5 +41,9 @@ module ATV
         @channel = @channels.length - 1
       end
     end
+
+    def state()
+      @channels[@channel].state
+    end
   end
 end
index e81b8a8465f720da54bab447dee2a03b12dcb559..8c5036922601e767ce7bd8831953071b8d6e0288 100644 (file)
@@ -1,24 +1,86 @@
 require 'iodine'
 
 module ATV
-  class Server
+  module Server
     APP = Proc.new do |env|
       if env['rack.upgrade?'.freeze] == :websocket
-        env['rack.upgrade'.freeze] = ATV
+        env['rack.upgrade'.freeze] = ATV::Server
         [0,{}, []] # It's possible to set cookies for the response.
       end
     end
 
-    def initialize(player)
-      @player = player
-    end
-
-    def start()
+    def start(player)
+      @@player = player
       Iodine.listen(service: :http, handler: APP)
       Iodine.run_every(1000) do
-        @player.update
+        if player.update
+          publish(@@player.state.merge({"cmd" => "play"}))
+        end
       end
+      Iodine.threads = 1
       Iodine.start
     end
+
+    def publish(obj)
+      puts JSON.dump(obj)
+      Iodine.publish(:atv, JSON.dump(obj))
+    end
+
+    def on_open(client)
+      puts "connect: #{client}"
+      client.subscribe :atv
+      publish(@@player.state.merge({"cmd" => "play"}))
+    end
+
+    def on_close(client)
+      puts "disconnect: #{client}"
+    end
+
+    def on_message(client, cmd)
+      cmd = JSON.parse(cmd)
+      Cmds[cmd["cmd"]].call(cmd["data"])
+    end
+
+    def send(client, data)
+      client.write JSON.dump(data)
+    end
+
+#    Cmds = {
+#      "skip" => lambda do |data|
+#        cfg = $channels[$channel]
+#        cfg[:play][:start_time] = secs_since_midnight()
+#        cfg[:play][:curr] = cfg[:play][:next]
+#        cfg[:play][:next] = next_show($channel)
+#        update_program()
+#      end,
+#
+#      "play" => lambda do |data|
+#        pp data
+#      end,
+#
+#      "pause" => lambda do |data|
+#        pp data
+#      end,
+#
+#      "chan_prev" => lambda do |data|
+#        $channel -= 1
+#        if $channel < 0
+#          $channel = $channels.length - 1
+#        end
+#        puts $channel
+#        update_program()
+#      end,
+#
+#      "chan_next" => lambda do |data|
+#        $channel += 1
+#        if $channel >= $channels.length
+#          $channel = 0
+#        end
+#        puts $channel
+#        update_program()
+#      end,
+#    }
+
+    extend self
   end
 end