General (Read-only)

UDP Receive issue on Nexus 5X
InAdvantage
We have two Nexus 5X devices. One of them drops packets in a UDP receive() call, the other doesn't.

I believe this is likely the same issue that was reported on Google Code:
https://code.google.com/p/android/issues/detail?id=204823

I have created a small test application to test this in our setup. On one Nexus 5X it didn't miss a packet for several minutes. In another, it missed bursts of often 100's of packets in a row.

I see on the method trace that the UDP receive method is indeed blocking for hundreds of milliseconds, whereas I'm sending packets at least every ~30ms.

This program passes on a Kindle Fire, two different Samsung tablets, and one Asus tablet. All under identical conditions. It fails on one Nexus 5X, but seems to pass on another.

I'm wondering if this is a hardware issue, or if there is something I'm missing to improve UDP Rx reliability on specific phones. The issue happened on Android 7.0, and an upgrade to 7.1.1 still caused the issue to happen.

package com.udp_receiver_test;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.nio.ByteBuffer;

public class MainActivity extends AppCompatActivity {

private WifiManager.WifiLock wifiLock;
Thread t;

DatagramSocket socket;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "Player");
wifiLock.setReferenceCounted(false);
wifiLock.acquire();

// Use sustained performance mode on N+ to ensure consistent
// CPU availability
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
getWindow().setSustainedPerformanceMode(true);
}


startListenThread();
}

private void startListenThread()
{
t = new Thread() {
@Override
public void run()
{
int nextSequenceNumber = 0;

ByteBuffer byteBuffer;

int BufferSize = 1292;
int NumRingElements = 784;
long correctPacketCounter = 0;

byte ring[][] = new byte[NumRingElements][1292];

try {
socket = new DatagramSocket(1239);
socket.setReceiveBufferSize(256 * 1024);
} catch (Exception e) {
e.printStackTrace();
return;
}

byte[] buffer;
int ringCounter = 0;
DatagramPacket packet = new DatagramPacket(new byte[1], 1);
//short rtpSequenceNumber;
int sequenceNumber;

while (!isInterrupted()) {
try {
buffer = ring[ringCounter];

packet.setData(buffer, 0, buffer.length);
socket.receive(packet);

if (packet.getLength() > 20) {

// Read the RTP sequence number field (big endian)
byteBuffer = ByteBuffer.wrap(buffer);
byteBuffer.position(16);
sequenceNumber = (byteBuffer.getInt() >> 8) & 0xFFFFFF;

if (sequenceNumber != nextSequenceNumber) {
Log.d("TAG", "Not next sequence number - Got:" + sequenceNumber + " Expected:" + nextSequenceNumber);
} else {

if (((++correctPacketCounter) % 100) == 0) {
Log.d("TAG", "Correct packets = " + correctPacketCounter);
}

}

nextSequenceNumber = (sequenceNumber + 256) % 65535;
} else {
Log.d("TAG", "Message too short");
}

ringCounter = (ringCounter + 1) % NumRingElements;
} catch (SocketException e) {
Log.d("TAG", "Closing socket thread");
return;
} catch (Exception e) {
e.printStackTrace();
return;
}
}
}
};
t.setName("Receiver");
t.setPriority(Thread.MAX_PRIORITY - 1);
t.start();
}

@Override
public void onStop()
{
super.onStop();
wifiLock.release();
if (t.isAlive()) {
t.interrupt();
}

if (socket != null) {
socket.close();
}
}
}
Posted on 11-Jan-2017
changbae

Re:UDP Receive issue on Nexus 5X

I wonder about your device information.

Let me know about your devices information.
I want to know both of them(works well device & doesn't work device)
(Setting > About Phone > Hardware info & Software info)

And I want to know more.. about your location(country).


Posted on 16-Jan-2017
InAdvantage

Re:UDP Receive issue on Nexus 5X

Both devices are:
Baseband M8994F-2.6.36.2.20
Kernel 3.10.73-gd28c62a
Build N4F26I

We're in the US


Posted on 19-Jan-2017
Navigation