Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android bindProcessToNetwork and RTSP stream through MediaPlayer

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?

like image 731
Kebabman Avatar asked Oct 29 '22 12:10

Kebabman


1 Answers

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.

like image 97
k1slay Avatar answered Nov 11 '22 10:11

k1slay