Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
manglemix committed Sep 23, 2023
2 parents 25a3df2 + 6e2db32 commit e290c00
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 11 deletions.
22 changes: 22 additions & 0 deletions lunabase/addons/kenney_interface_sounds/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@


Interface Sounds (1.0)

Created/distributed by Kenney (www.kenney.nl)
Creation date: 11-02-2020

------------------------------

License: (Creative Commons Zero, CC0)
http://creativecommons.org/publicdomain/zero/1.0/

This content is free to use in personal, educational and commercial projects.
Support us by crediting Kenney or www.kenney.nl (this is not mandatory)

------------------------------

Donate: http://support.kenney.nl
Patreon: http://patreon.com/kenney/

Follow on Twitter for updates:
http://twitter.com/KenneyNL
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[remap]

importer="wav"
type="AudioStreamWAV"
uid="uid://dpp0287wnfjsc"
path="res://.godot/imported/confirmation_002.wav-73fe748f66ffd31b01fbcf2befa1842b.sample"

[deps]

source_file="res://addons/kenney_interface_sounds/confirmation_002.wav"
dest_files=["res://.godot/imported/confirmation_002.wav-73fe748f66ffd31b01fbcf2befa1842b.sample"]

[params]

force/8_bit=false
force/mono=false
force/max_rate=false
force/max_rate_hz=44100
edit/trim=false
edit/normalize=false
edit/loop_mode=0
edit/loop_begin=0
edit/loop_end=-1
compress/mode=0
Binary file not shown.
24 changes: 24 additions & 0 deletions lunabase/addons/kenney_interface_sounds/error_002.wav.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[remap]

importer="wav"
type="AudioStreamWAV"
uid="uid://cephremnedhtp"
path="res://.godot/imported/error_002.wav-0ace3396c9a9a4a935dcc15d3408b216.sample"

[deps]

source_file="res://addons/kenney_interface_sounds/error_002.wav"
dest_files=["res://.godot/imported/error_002.wav-0ace3396c9a9a4a935dcc15d3408b216.sample"]

[params]

force/8_bit=false
force/mono=false
force/max_rate=false
force/max_rate_hz=44100
edit/trim=false
edit/normalize=false
edit/loop_mode=0
edit/loop_begin=0
edit/loop_end=-1
compress/mode=0
115 changes: 115 additions & 0 deletions lunabase/ffmpeg.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
extends Node


signal image_received(Image)


const IMAGE_WIDTH := 1280
const IMAGE_HEIGHT := 720
const IMAGE_SIZE := IMAGE_HEIGHT * IMAGE_WIDTH * 3

#var thr := Thread.new()
#var udp_sender := PacketPeerUDP.new()
var tcp_server := TCPServer.new()
var ffmpeg_tcp: StreamPeerTCP
var buffer := PackedByteArray()

var send_from: int
#var send_to: int


func subprocess():
var output := []
var err := OS.execute(
"ffmpeg",
# [
# "-f", "mpegts",
# "-c:v", "libvpx-vp9",
# "-i", "udp://127.0.0.1:%s" % send_to,
# "-f", "rawvideo",
# "-pix_fmt", "rgb24",
# "udp://127.0.0.1:%s" % send_from
# ],
[
"-f", "mpegts",
"-c:v", "libvpx-vp9",
"-b:v", "1500k",
"-i", "tcp://127.0.0.1:%s" % send_from,
"-c", "copy",
"-map", "0",
"-f", "segment",
"-segment_time", "5",
"-segment_format", "matroska",
"capture-%03d.mkv",

],
# output,
# true
[],
false,
true
)

if err != OK:
push_error(output[0])


func _ready() -> void:
send_from = randi_range(10000, 65000)
# send_to = randi_range(10000, 65000)
if tcp_server.listen(send_from, "127.0.0.1") != OK:
push_error("Failed to start FFMPEG TCP Server")
# udp_sender.bind(send_from, "127.0.0.1")
# udp_sender.set_dest_address("127.0.0.1", send_to)
# thr.start(subprocess)
OS.create_process(
"ffmpeg",
# [
# "-f", "mpegts",
# "-c:v", "libvpx-vp9",
# "-i", "udp://127.0.0.1:%s" % send_to,
# "-f", "rawvideo",
# "-pix_fmt", "rgb24",
# "udp://127.0.0.1:%s" % send_from
# ],
[
"-v", "debug",
# "-f", "mpegts",
# "-c:v", "libvpx-vp9",
"-i", "tcp://127.0.0.1:%s" % send_from,
"-f", "matroska",
"out.mkv"
],
true
)


func _process(_delta: float) -> void:
if ffmpeg_tcp == null and tcp_server.is_connection_available():
ffmpeg_tcp = tcp_server.take_connection()
# if udp_sender.get_available_packet_count() == 0:
# return
#
# print_debug("receivedee")
# for _i in range(udp_sender.get_available_packet_count()):
# buffer.append_array(udp_sender.get_packet())
# if buffer.size() >= IMAGE_SIZE:
# var img := Image.create_from_data(IMAGE_WIDTH, IMAGE_HEIGHT, false,Image.FORMAT_RGB8, buffer.slice(0, IMAGE_SIZE))
# buffer = buffer.slice(IMAGE_SIZE)
# image_received.emit(img)
# print_debug("received")


func process_data(data: PackedByteArray):
if ffmpeg_tcp == null:
push_error("Failed to send to FFMPEG")
return
ffmpeg_tcp.put_data(data)
# ffmpeg_tcp.disconnect_from_host()
# ffmpeg_tcp = null


func _exit_tree() -> void:
if ffmpeg_tcp != null:
ffmpeg_tcp.disconnect_from_host()
# thr.wait_to_finish()
41 changes: 31 additions & 10 deletions lunabase/lunabot.gd
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
extends Node


const USE_ARCHIMEDES := true
const RECV_FROM := 43762
const USE_ARCHIMEDES := false

enum Channels { IMPORTANT, CAMERA, ODOMETRY, CONTROLS, MAX }
enum ImportantMessage { ENABLE_CAMERA, DISABLE_CAMERA }
enum ImportantMessage { ENABLE_CAMERA, DISABLE_CAMERA, PING }

signal connected
signal disconnected
Expand All @@ -29,7 +30,7 @@ func _ready() -> void:
controls_data = PackedByteArray([0, 0, 0])

server = ENetConnection.new()
var err := server.create_host_bound("*", 43721, 1, 0, 0, 0)
var err := server.create_host_bound("*", RECV_FROM, 1, 0, 0, 0)
if err != OK:
push_error("Failed to start ENet Server: " + str(err))
return
Expand All @@ -46,11 +47,21 @@ func is_lunabot_connected() -> bool:


func _run_thr():
var last_receive_time := Time.get_ticks_msec()
var pinged := false
while running:
var result: Array = server.service(200)

lunabot_mutex.lock()
if lunabot != null:
if not pinged and Time.get_ticks_msec() - last_receive_time >= 3000:
pinged = true
lunabot.send(
Channels.IMPORTANT,
PackedByteArray([ImportantMessage.PING]),
ENetPacketPeer.FLAG_RELIABLE
)

call_deferred(
"emit_signal",
"network_statistics",
Expand Down Expand Up @@ -89,15 +100,31 @@ func _run_thr():
continue
lunabot = null
lunabot_mutex.unlock()
push_error("Lost connection to lunabot!")
call_deferred("emit_signal", "disconnected")

ENetConnection.EventType.EVENT_RECEIVE:
last_receive_time = Time.get_ticks_msec()
pinged = false
_on_receive(channel)

lunabot_mutex.lock()
if echo_controls:
raw_send_unreliable(Channels.CONTROLS, controls_data)
lunabot_mutex.unlock()

if lunabot != null:
lunabot_mutex.lock()
lunabot.peer_disconnect()
while true:
var result: Array = server.service(200)
var event_type: ENetConnection.EventType = result[0]

if event_type == ENetConnection.EventType.EVENT_DISCONNECT:
break
lunabot_mutex.unlock()

server.destroy()


func _on_receive(channel: int) -> void:
Expand All @@ -107,16 +134,10 @@ func _on_receive(channel: int) -> void:

match channel:
Channels.IMPORTANT:
# TODO
pass

Channels.CAMERA:
var frame := Image.new()
var err := frame.load_webp_from_buffer(data)
if err != OK:
push_error("Bad frame: " + str(err))
return
call_deferred("emit_signal", "camera_frame_received", frame)
Ffmpeg.process_data(data)

Channels.ODOMETRY:
var x := data.decode_float(0)
Expand Down
13 changes: 12 additions & 1 deletion lunabase/main.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[gd_scene load_steps=11 format=3 uid="uid://b8qgudi6vkflb"]
[gd_scene load_steps=13 format=3 uid="uid://b8qgudi6vkflb"]

[ext_resource type="AudioStream" uid="uid://dpp0287wnfjsc" path="res://addons/kenney_interface_sounds/confirmation_002.wav" id="1_hl7kb"]
[ext_resource type="AudioStream" uid="uid://cephremnedhtp" path="res://addons/kenney_interface_sounds/error_002.wav" id="2_kejy0"]

[sub_resource type="GDScript" id="GDScript_45865"]
script/source = "extends MarginContainer
Expand All @@ -8,6 +11,8 @@ func _ready() -> void:
set_process_input(false)
Lunabot.connected.connect(func(): set_process_input(true))
Lunabot.disconnected.connect(func(): set_process_input(false))
Lunabot.connected.connect($Connected.play)
Lunabot.disconnected.connect($Disconnected.play)


func _input(event: InputEvent) -> void:
Expand Down Expand Up @@ -250,5 +255,11 @@ grow_vertical = 2
layout_mode = 2
text = "Click to Drive"
[node name="Connected" type="AudioStreamPlayer" parent="."]
stream = ExtResource("1_hl7kb")
[node name="Disconnected" type="AudioStreamPlayer" parent="."]
stream = ExtResource("2_kejy0")
[connection signal="pressed" from="Control/VBoxContainer/HBoxContainer/EnableCamera" to="Control/VBoxContainer/HBoxContainer/EnableCamera" method="_on_pressed"]
[connection signal="mouse_exited" from="Control/VBoxContainer/VBoxContainer/AspectRatioContainer/TextureRect" to="Control/VBoxContainer/VBoxContainer/AspectRatioContainer/TextureRect" method="_on_mouse_exited"]
2 changes: 2 additions & 0 deletions lunabase/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ config/icon="res://icon.png"
[autoload]

Lunabot="*res://lunabot.gd"
Ffmpeg="*res://ffmpeg.gd"

[display]

Expand Down Expand Up @@ -70,4 +71,5 @@ SpinDrumCounterClockwise={

[rendering]

renderer/rendering_method="mobile"
environment/defaults/default_clear_color=Color(0.0588235, 0.0588235, 0.0588235, 1)

0 comments on commit e290c00

Please sign in to comment.