5

I have an app that is using bindProcessToNetwork() to force all connections out over the wifi connection. This is done because the wifi connection is connected to a camera with no Internet connection so naturally android tries to push all traffic out over the cellular data connection which has full Internet connectivity. The code for this is :

final ConnectivityManager connection_manager =
            (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkRequest.Builder request = new NetworkRequest.Builder();
    request.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);

    connection_manager.requestNetwork(request.build(), new ConnectivityManager.NetworkCallback()
    {
        @Override
        public void onAvailable(Network network)
        {
            try {
                connection_manager.bindProcessToNetwork(network);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

This works fine when connecting to the camera over HTTP to issue commands etc. However, when initiating an RTSP stream and trying to view it through an android MediaPlayer, it won't connect. The code for the media player:

mMediaPlayer = new MediaPlayer();
mMediaPlayer.setSurface(new Surface(mTextureView.getSurfaceTexture()));
mMediaPlayer.setWakeMode(getActivity().getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
mMediaPlayer.setDataSource(loc);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.prepareAsync();

If I disable the cellular data on the phone, the code works perfectly and the RTSP stream plays fine, as the default route is out over the wifi network. However when both networks are connected it appears that the RTSP request isn't getting through to the camera over the wifi connection. It's almost as though only HTTP requests are getting forced over the wifi by bindProcessToNetwork(). This can't be the case though as elsewhere in the app the camera is being pinged using [InetAddress.isReachable()][1]

Is there any way to force the MediaPlayer RTSP connection to go over the wifi connection as well?

Kebabman
  • 776
  • 1
  • 7
  • 18

1 Answers1

2

From what I have read about the media player API, the media playback is handled by the MediaServer which runs on a separate process (lookie here). When you call bindProcessToNetwork you are forcing your app (which runs on its own process) to use only WiFi network and this will have no effect on MediaServer's behaviour (because it runs on a separate process from your app).

So looks like the option you have is to pull the media file over to the android device and play it as a local file instead of streaming it.

k1slay
  • 1,068
  • 1
  • 10
  • 18
  • 1
    What we ended up doing was to use ffmpeg. You can compile the library in to the app and it runs as part of the same process so bindProcessToNetwork then performs as expected. – Kebabman Feb 02 '18 at 08:51
  • You are right. That way you can still stream without having to download the file to your device first. – k1slay Feb 02 '18 at 10:16
  • @Kebabman Can you give us an example on how you run it under the same process? usually, it runs using `Runtime.getRuntime().exec("ffmpeg")` which separates it to a different process. – Danpe Oct 02 '18 at 11:19
  • @Danpe - Are you using an ffmpeg executable, I presume? – Kebabman Oct 02 '18 at 11:31