Android Open Source - screenshare-playground Sender Async Task






From Project

Back to project page screenshare-playground.

License

The source code is released under:

GNU General Public License

If you think the Android project screenshare-playground listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (c) 2015 Renard Wellnitz.//from ww w  . j  a  v  a 2 s .  co  m
 *
 *  This file is part of ScreenShare.
 *
 *     Foobar is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Foobar is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.screenshare.renard314.screenshare;

import android.media.MediaCodec;
import android.os.AsyncTask;
import android.util.Log;

import com.screenshare.renard314.screensharelib.VideoChunk;

import java.io.DataOutputStream;
import java.io.IOException;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * Created by renard on 22/12/14.
 */
public class SenderAsyncTask extends AsyncTask<Void,Void,Void> {

  private static final String LOG_TAG = SenderAsyncTask.class.getSimpleName();
  private final String mIp;
  LinkedBlockingDeque<VideoChunk> mVideoChunks = new LinkedBlockingDeque<VideoChunk>();

  public void addChunk(VideoChunk chunk) {
        synchronized (mVideoChunks) {
//            boolean isKeyFrame = (chunk.getFlags() & MediaCodec.BUFFER_FLAG_KEY_FRAME) == MediaCodec.BUFFER_FLAG_KEY_FRAME;
//            if (isKeyFrame) {
//
//
//                Log.i(LOG_TAG, "adding keyframe");
//                VideoChunk configChunk = null;
//                for(VideoChunk c: mVideoChunks){
//                    boolean containsConfig = (chunk.getFlags() & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) == MediaCodec.BUFFER_FLAG_CODEC_CONFIG;
//                    if(containsConfig){
//                        configChunk = c;
//                        break;
//                    }
//                }
//                mVideoChunks.clear();
//                if(configChunk!=null){
//                    mVideoChunks.add(configChunk);
//                }
//            }
            mVideoChunks.addFirst(chunk);
            if(mVideoChunks.size()>2) {
                Log.i(LOG_TAG, "Chunks: " + mVideoChunks.size());
            }
        }
  }

  SenderAsyncTask(String ip){
    mIp = ip;
  }

  @Override
  protected Void doInBackground(Void... params) {
        Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
        int port = 1234;
    java.net.Socket socket=null;
    try {
      socket = new java.net.Socket(mIp,port); // connect to server
    } catch (IOException e) {
      e.printStackTrace();
    }
    DataOutputStream dataOut = null;
    try {
      dataOut = new DataOutputStream(socket.getOutputStream());
    } catch (IOException e) {
      e.printStackTrace();
    }

    while(!isCancelled()){
      VideoChunk chunk = null;
      try {
        //Log.d(LOG_TAG, "waiting for data to send");
        chunk = mVideoChunks.takeLast();
        //Log.d(LOG_TAG,"got data. writing to socket");
        int length = chunk.getData().length;

        dataOut.writeInt(length);
        dataOut.writeInt(chunk.getFlags());
        dataOut.writeLong(chunk.getTimeUs());
        dataOut.write(chunk.getData());
        dataOut.flush();
      } catch (InterruptedException e) {
        e.printStackTrace();
        continue;
      } catch (IOException e) {
        e.printStackTrace();
      }

    }
    return null;
  }
}




Java Source Code List

com.screenshare.renard314.decoder.ApplicationTest.java
com.screenshare.renard314.decoder.MainActivity.java
com.screenshare.renard314.decoder.MyApplication.java
com.screenshare.renard314.decoder.ReceiverAsyncTask.java
com.screenshare.renard314.screenshare.ApplicationTest.java
com.screenshare.renard314.screenshare.EncoderAsyncTask.java
com.screenshare.renard314.screenshare.MainActivity.java
com.screenshare.renard314.screenshare.SenderAsyncTask.java
com.screenshare.renard314.screensharelib.ApplicationTest.java
com.screenshare.renard314.screensharelib.DecoderAsyncTask.java
com.screenshare.renard314.screensharelib.MediaCodecFactory.java
com.screenshare.renard314.screensharelib.OnVideoSizeChangedListener.java
com.screenshare.renard314.screensharelib.VideoChunk.java