multicast UDP issues

steeley

Well-known member
hello
running T4.1 with IDE Arduino-1.8.16 Teensyduino-1.55 and NativeEthernet.

I'm trying to read from a UDP multicast address (grabbing a text SAP message sent by ffmpeg encoding/streaming tool)
The file is being sent, as I can read it using a python script( shown below) However my Teensy code receives the data but
when I print to the serial monitor it is blank.

As a simple test I can use netcat to send a text file on disk and my Teensy code can display correctly.
(Only thing is the UDP IP and port reported seem wrong - I am using 224.2.127.254. port 9875)

Command:
Code:
nc -u 224.2.127.254 9875 < a.txt

Teensy result:
my ip 192.168.2.155
Received packet of size 55
From 192.168.2.54, port 63758
Contents:
the quick brown fox!
Jumps over the lazy dog
12345678

My code running on the Teensy is this:

Code:
#include <NativeEthernet.h>
#include <NativeEthernetUdp.h>

byte mac[] = { 0x0A, 0x1B, 0x3C, 0x4D, 0x5E, 0x6E};
byte ip[] = { 192, 168, 2, 155};

EthernetUDP Udp;

byte mcastip[] = { 224, 2, 127, 254};
int mport = 9875;

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  delay(1000);
  Ethernet.begin(mac, ip);
  Udp.beginMulticast(mcastip, mport);
  IPAddress ipaddr = Ethernet.localIP();
  Serial.print("my ip "); Serial.println(ipaddr);
}

void loop()
{
  int packetSize = Udp.parsePacket();
  if (packetSize) {
    char packetBuffer[packetSize];
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i = 0; i < 4; i++) {
      Serial.print(remote[i], DEC);
      if (i < 3) {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    Udp.read(packetBuffer, packetSize);
    Serial.println("Contents:");
    Serial.println(packetBuffer);
    Serial.println("--------");
  }
}

However with the ffmpeg command, which sends the SDP text every few seconds I get no printed text, although the
result shows the packet has a size which is correct. (Again IP and port seem wrong)

Received packet of size 309
From 192.168.2.54, port 62806
Contents:

This python script does show the SDP text data ( and anything else I send perfectly):

Code:
import socket
import struct

multicast_addr = '224.2.127.254'
bind_addr = '0.0.0.0'
port = 9875

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
membership = socket.inet_aton(multicast_addr) + socket.inet_aton(bind_addr)

sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, membership)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

sock.bind((bind_addr, port))

while True:
    message, address = sock.recvfrom(255)
    print(message)

So any ideas why this isn't working on the Teensy? Maybe weird control characters?

My text file shows in the python script output like this:
b'the quick brown fox!\nJumps over the lazy dog\n12345678\n\n'

it shows the SDP text like this which is what I expect:
b'$\x00\xc6\xb2\xc0\xa8\x026application/sdp\x00v=0\r\no=- 0 0 IN IP4 127.0.0.1\r\ns=No Name\r\nc=IN IP4 239.255.0.10/255\r\nt=0 0\r\na=tool:libavformat 58.76.100\r\nm=audio 5004 RTP/AVP 96\r\nb=AS:128\r\na=rtpmap:96 MPEG4-GENERIC/44100/2\r\na=fmtp:96 profile-level-id=1;mode=AAC-hbr;sizelength=13'

TIA
 
Since you’re using NUL-terminated string-based printing to print to the console, it will stop printing at the first NUL character. I’d expect it to print “$”, though, since a NUL is after that.
 
Since you’re using NUL-terminated string-based printing to print to the console, it will stop printing at the first NUL character. I’d expect it to print “$”, though, since a NUL is after that.

Ahh, ok that makes sense. I guess I need a way to 'decode' the raw hex and get rid of the odd characters.
thanks.
 
Back
Top