"H.264 (Media Acodec) + UDP + VLC does not perform UDP send, encoding Camera Preview Data data, then decoding the display (bound SurfaceView's Surface) If you enable the UDP push thread, encode the CAMERA Preview Data data, encoded data Put UDP Send a list, let the UDP thread go to obtain the data, and send it to the VLC side.
Codec, type VIDEO / AVC (H.264)
VLC: Network play address (open network stream, address bar input): udp: @ @: 5000 // remote_host = "192.168.10.113"; // The Terminal IP Address of VLC Play, change // // REMOTE_HOST_PORT = 5000; // VLC listening port, can be changed
Camera Camera = NULL;
Private udpsendtask netsendtask;
Private Bufferedputstream OutputStream;
Private Camera getcamera (int Cameratype) {
Camera Camera = NULL;
Try {
Camera = Camera.open (Camera.GetNumberofcameras () - 1);
} catch (exception e) {
E.PrintStackTrace ();
}
Return Camera; // Returns Null IF Camera IS Unavailable
}
Private void Opencamera (Surfaceholder Holder) {
Try {
Camera = getcamera (Camera.camerainfo.camera_facing_back);
} catch (exception e) {
Camera = NULL;
E.PrintStackTrace ();
}
IF (Camera! = NULL) {
Camera.SetDisplayorientation (90);
Camera.Parameters Parameters = Camera.getParameters ();
Parameters.SetPreviewSize (Width, Height);
Parameters.setFlashMode ("OFF");
Parameters.SetWhiteBalance (Camera.Parameters.white_balance_auto;
Parameters.SetPreviewFormat (imageformat.yv12); // 3 £ óãññ½ oNV21 / YV12
Parameters.setSceneMode (Camera.Parameters.Scene_Mode_AUTO);
Camera.SetParameters (Parameters);
BUF = New byte [width * height * 3/2];
Camera.AddcallbackBuffer (buf);
Camera.SetPreviewCallbackwithBuffer (this);
List
fpsrange = parameters.getsupportedPreviewFpsRange ();
For (int [] TEMP3: FPSRANGE) {
System.out.println (Arrays.toString (TEMP3));
}
Parameters.SetPreviewFPsRange (15000, 15000);
Camera.startPreview ();
}
}
@Override
Public void onpreviewframe (byte [] data, camera badra) {
// Todo auto-generated method stub
OnFrame (data, data.length); // calls MediaCodec for H.264 encoding, decoding playback, stack (to push storage cache data for UDP)
Camera.AddcallbackBuffer (buf);
}
Private static int selectcolorformat (MediacodecInfo Codecinfo, String Mimetype) {
Mediacodecinfo.codeccapabilities Capabilities = Codecinfo.getcapabilitiesfortype (MIMETYPE);
For (int i = 0; i mlist; // nalu cache list
Static Boolean Running;
Public void init ()
{
Try {
Socket = new datagramsocket ();
Address = inetaddress.getbyName (transote_host); // remote_host => VLC IP address
mList = new arraylist ();
Try {
Thread.sleep (1000);
} catch (interruptedexception e) {
E.PrintStackTrace ();
}
Running = true;
} catch (socketexception e) {
E.PrintStackTrace ();
} catch (unknownhostexception e) {
E.PrintStackTrace ();
}
}
Public void end ()
{
Running = false;
Runflag = false;
}
Public void pushbuf (byte [] buf, int LEN)
{
Bytebuffer buffer = bytebuffer.allocate (len);
Buffer.put (BUF, 0, LEN);
mList.Add (buffer);
}
@Override
Public void run () {
While (Running) {
IF (mlist.size () <= 0) {
Try {
Thread.sleep (70);
} catch (interruptedexception e) {
E.PrintStackTrace ();
}
}
While (mlist.size ()> 0) {
Bytebuffer sendbuf = mlist.get (0);
Try {
DataGrampacket Packet = New DataGrampacket (Sendbuf.Array (), sendbuf.capacity (), address, remote_host_port;
Socket.send (packet);
} catch (throwable t) {
T.PrintStackTrace ();
}
mList.remove (0);
IF (! Running) {
Break;
}
}
}
mlist.clear ();
}
}
// Decoding H.264 encoded data
Public void onframe0 (byte [] buf, int lay {
BYTEBUFFER [] InputBuffers = Mediacodecd.getinputBuffers ();
INT INPUTBUFFERINDEX = MediaCodeCD.DequeueInputBuffer (-1);
IF (InputBufferIndex> = 0) {
Bytebuffer InputBuffer = INPUTBUFFERS [InputBufferIndex];
InputBuffer.clear ();
InputBuffer.Put (buf, 0, length);
Mediacodecd.queueInputBuffer (InputBufferIndex, 0, Length,
McOUNT * 1000000 / frame_rate, 0); // Add PTS to correctly render Surface, displayFferIndex> = 0) {
Mediacodecd.ReleaseOutputBuffer (OutputBufferIndex, true);
OutputBufferIndex = mediacodecd.dequeueoutputbuffer (bufferinfo, 0);
}
}
// YV12 = "YUV420P: YVU -> YUV
Private void swapyv12toi420 (byte [] yv12bytes, byte [] i420bytes, int width, int hotht)
{
System.ArrayCopy (YV12BYTES, 0, I420Bytes, 0, Width * Height);
System.ArrayCopy (YV12BYTES, WIDTH * Height + Width * Height / 4, i420bytes, width * height, width * height / 4);
System.ArrayCopy (YV12BYTES, WIDTH * Height, I420bytes, Width * Height + Width * Height / 4, Width * Height / 4);
}
// Encode Camera Preview Data
Public void onframe (byte [] buf, int layth) {
SWAPYV12TOI420 (BUF, H264, Width, Height); // H.264 Encoder only supports YUV video format input
Bytebuffer [] InputBuffers = Mediacodec0.GetinputBuffers ();
Bytebuffer [] OutputBuffers = Mediacodec0.getOutputBuffrs ();
INT INPUTBUFFERINDEX = MediaCodec0.dequeueInputBuffer (-1);
IF (InputBufferIndex> = 0) {
Bytebuffer InputBuffer = INPUTBUFFERS [InputBufferIndex];
InputBuffer.clear ();
InputBuffer.Put (H264, 0, Length);
Mediacodec0.QueueInputBuffer (InputBufferIndex, 0, Length, 0, 0);
}
Mediacodec.bufferinfo bufferinfo = new mediacodec.bufferinfo ();
INT OUTPUTBUFFERINDEX = mediacodec0.dequeueoutputbuffer (bufferInfo, 0);
While (OutputBufferIndex> = 0) {
BYTEBUFFER OUTPUTBUFFER = OUTPUTBUFFERS [OUTPUTBUFFERINDEX];
Byte [] Outdata = new byte [bufferinfo.size];
OutputBuffer.get (Outdata);
/ *
Try {
OutputStream.write (Outdata, 0, Outdata.Length); // Write Into H264 File
} catch (ioexception e) {
// Todo Auto-Generated Catch Block
E.PrintStackTrace ();
}
* /
IF (! runflag) {
Onframe0 (Outdata, 0, Outdata.length, Flag); // Call Decoding, Locally Display
IF (firstflag) {
Log.i ("" Encoder "," Onframe PPS SPS ");
INT LEN = Outdata.length;
For (int = 0; ix = 0) {
Mediacodecd.ReleaseOutputBuffer (OutputBufferIndex, true);
OutputBufferIndex = mediacodecd.dequeueoutputbuffer (bufferinfo, 0);
}
}
// YV12 = "YUV420P: YVU -> YUV
Private void swapyv12toi420 (byte [] yv12bytes, byte [] i420bytes, int width, int hotht)
{
System.ArrayCopy (YV12BYTES, 0, I420Bytes, 0, Width * Height);
System.ArrayCopy (YV12BYTES, WIDTH * Height + Width * Height / 4, i420bytes, width * height, width * height / 4);
System.ArrayCopy (YV12BYTES, WIDTH * Height, I420bytes, Width * Height + Width * Height / 4, Width * Height / 4);
}
// Encode Camera Preview Data
Public void onframe (byte [] buf, int layth) {
SWAPYV12TOI420 (BUF, H264, Width, Height); // H.264 Encoder only supports YUV video format input
Bytebuffer [] InputBuffers = Mediacodec0.GetinputBuffers ();
Bytebuffer [] OutputBuffers = Mediacodec0.getOutputBuffrs ();
INT INPUTBUFFERINDEX = MediaCodec0.dequeueInputBuffer (-1);
IF (InputBufferIndex> = 0) {
Bytebuffer InputBuffer = INPUTBUFFERS [InputBufferIndex];
InputBuffer.clear ();
InputBuffer.Put (H264, 0, Length);
Mediacodec0.QueueInputBuffer (InputBufferIndex, 0, Length, 0, 0);
}
Mediacodec.bufferinfo bufferinfo = new mediacodec.bufferinfo ();
INT OUTPUTBUFFERINDEX = mediacodec0.dequeueoutputbuffer (bufferInfo, 0);
While (OutputBufferIndex> = 0) {
BYTEBUFFER OUTPUTBUFFER = OUTPUTBUFFERS [OUTPUTBUFFERINDEX];
Byte [] Outdata = new byte [bufferinfo.size];
OutputBuffer.get (Outdata);
/ *
Try {
OutputStream.write (Outdata, 0, Outdata.Length); // Write Into H264 File
} catch (ioexception e) {
// Todo Auto-Generated Catch Block
E.PrintStackTrace ();
}
* /
IF (! runflag) {
Onframe0 (Outdata, 0, Outdata.length, Flag); // Call Decoding, Locally Display
IF (firstflag) {
Log.i ("" Encoder "," Onframe PPS SPS ");
INT LEN = Outdata.length;
For (int = 0; ix mlist; // nalu cache list
Static Boolean Running;
Public void init ()
{
Try {
Socket = new datagramsocket ();
Address = inetaddress.getbyName (transote_host); // remote_host => VLC IP address
mList = new arraylist ();
Try {
Thread.sleep (1000);
} catch (interruptedexception e) {
E.PrintStackTrace ();
}
Running = true;
} catch (socketexception e) {
E.PrintStackTrace ();
} catch (unknownhostexception e) {
E.PrintStackTrace ();
}
}
Public void end ()
{
Running = false;
Runflag = false;
}
Public void pushbuf (byte [] buf, int LEN)
{
Bytebuffer buffer = bytebuffer.allocate (len);
Buffer.put (BUF, 0, LEN);
mList.Add (buffer);
}
@Override
Public void run () {
While (Running) {
IF (mlist.size () <= 0) {
Try {
Thread.sleep (70);
} catch (interruptedexception e) {
E.PrintStackTrace ();
}
}
While (mlist.size ()> 0) {
Bytebuffer sendbuf = mlist.get (0);
Try {
DataGrampacket Packet = New DataGrampacket (Sendbuf.Array (), sendbuf.capacity (), address, remote_host_port;
Socket.send (packet);
} catch (throwable t) {
T.PrintStackTrace ();
}
mList.remove (0);
IF (! Running) {
Break;
}
}
}
mlist.clear ();
}
}
// Decoding H.264 encoded data
Public void onframe0 (byte [] buf, int lay {
BYTEBUFFER [] InputBuffers = Mediacodecd.getinputBuffers ();
INT INPUTBUFFERINDEX = MediaCodeCD.DequeueInputBuffer (-1);
IF (InputBufferIndex> = 0) {
Bytebuffer InputBuffer = INPUTBUFFERS [InputBufferIndex];
InputBuffer.clear ();
InputBuffer.Put (buf, 0, length);
Mediacodecd.queueInputBuffer (InputBufferIndex, 0, Length,
McOUNT * 1000000 / frame_rate, 0); // Add PTS to correctly render Surface, display
McOUNT ++;
}
Mediacodec.bufferinfo bufferinfo = new mediacodec.bufferinfo ();
Int outputbufferindex = mediacodecd.dequeueoutputbuffer (bufferinfo, 0);
While (OutputBu (f.delete ()) {
Try {
F.createNewFile ();
} catch (ioexception e) {
// Todo Auto-Generated Catch Block
E.PrintStackTrace ();
}
}
}
Try {
// Store to H.264 encoding file, test use
OutputStream = New BufferedOutputStream (New FileoutputStream (f));
Log.i ("Encoder", "" OutputStream Initialized ");
} catch (exception e) {
E.PrintStackTrace ();
}
/ / Select color format according to Type type
INT colorformat = selectcolorformat (SelectCodec ("" "" "" "" "Video / AVC"); "" "" VIDEO / AVC ");
Mediacodec0 = mediacodec.createEncoderbytype (Type);
Mediacodec = MediaCodec.createEncoderByType (Type);
Mediaformat MediaFormat = MediaFormat.createvideoFormat (Type, Width, Height);
Mediaformat.setinteger (MediaFormat.key_bit_Rate, 125000); // 125kbps
Mediaformat.setinteger (MediaFormat.key_Frame_Rate, 15); // frame rate
//MediaFormat.setInteger (MediaFormat.Key_Color_Format,
// mediacodecinfo.codeccapabilities.color_formatyuv420semiplanar); // color_formatyuv420planar
MediaFormat.setinteger (MediaFormat.key_color_format, colorformat);
MediaFormat.setinteger (MediaFormat.key_i_frame_interval, 5); // i frame interval
Mediacodec.configure (MediaFormat, Null, Null, MediaCodec.configure_flag_encode);
Mediacodec.start ();
Mediacodec0.configure (MediaFormat, Null, Null, MediaCodec.configure_flag_encode);
Mediacodec0.start ();
}
// decoder configuration
Public void mediacodecodeinit () {
String Type = "" VIDEO / AVC ""
Mediacodecd = mediacodec.createdecoderbytype (Type);
Mediaformat MediaFormat = MediaFormat.createvideoFormat (Type, Width, Height);
Mediacodecd.configure (MediaFormat, Surface, NULL, 0);
Mediacodecd.start ();
}
Private static string remote_host = "192.168.10.113"; // the Terminal of VLC Play
Private static final short remote_host_port = 5000; // VLC listening port
Private static inetdress address;
Private static datagramsocket socket;
Static Class Udpsendtask Extends Thread {
Private arraylist mlist; // nalu cache list
Static Boolean Running;
Public void init ()
{
Try {
Socket = new datagramsocket ();
Address = inetaddress.getbyName (transote_host); // remote_host => VLC IP address
mList = new arraylist ();
Try {
Thread.sleep (1000);
} catch (interruptedexception e) {
E.PrintStackTrace ();
}
Running = true;
} catch (socketexception e) {
E.PrintStackTrace ();
} catch (unknownhostexception e) {
E.PrintStackTrace ();
}
}
Public void end ()
{
Running = false;
Runflag = false;
}
Public void pushbuf (byte [] buf, int LEN)
{
Bytebuffer buffer = bytebuffer.allocate (len);
Buffer.put (BUF, 0, LEN);
mList.Add (buffer);
}
@Override
Public void run () {
While (Running) {
IF (mlist.size () <= 0) {
Try {
Thread.sleep (70);
} catch (interruptedexception e) {
E.PrintStackTrace ();
}
}
While (mlist.size ()> 0) {
Bytebuffer sendbuf = mlist.get (0);
Try {
DataGrampacket Packet = New DataGrampacket (Sendbuf.Array (), sendbuf.capacity (), address, remote_host_port;
Socket.send (packet);
} catch (throwable t) {
T.PrintStackTrace ();
}
mList.remove (0);
IF (! Running) {
Break;
}
}
}
mlist.clear ();
}
}
// Decoding H.264 encoded data
Public void onframe0 (byte [] buf, int lay {
BYTEBUFFER [] InputBuffers = Mediacodecd.getinputBuffers ();
INT INPUTBUFFERINDEX = MediaCodeCD.DequeueInputBuffer (-1);
IF (InputBufferIndex> = 0) {
Bytebuffer InputBuffer = INPUTBUFFERS [InputBufferIndex];
InputBuffer.clear ();
InputBuffer.Put (buf, 0, length);
Mediacodecd.queueInputBuffer (InputBufferIndex, 0, Length,
McOUNT * 1000000 / frame_rate, 0); // Add PTS to correctly render Surface, display
McOUNT ++;
}
Mediacodec.bufferinfo bufferinfo = new mediacodec.bufferinfo ();
Int outputbufferindex = mediacodecd.dequeueoutputbuffer (bufferinfo, 0);
While (OutputBu McOUNT ++;
}
Mediacodec.bufferinfo bufferinfo = new mediacodec.bufferinfo ();
Int outputbufferindex = mediacodecd.dequeueoutputbuffer (bufferinfo, 0);
While (OutputBu
Our other product: