Wednesday, June 22, 2011

How to publish H264 frame

One more task is to publish H264 frame. In my case it is byte[] encoded array. I found one Java library that can deal with this ShoutcastH264.jar. I do not copy the code but some technical info saved my time:


public class VideoFramer
{
 
protected static Logger log = LoggerFactory.getLogger(VideoFramer.class);
 
private IICYEventSink output;
 
private int _codecSetupLength;
 
private boolean codecSent;
 
private int _SPSLength;
 
private int _PPSLength;
 
private int[] _pCodecSetup;
 
private int[] _pSPS;
 
private int[] _pPPS;
 
private int[] _pSEI;
 
public boolean keepNALU;
 
private IRTMPEvent lastKey;
 
private List<IRTMPEvent> slices = new ArrayList<IRTMPEvent>();
 
 
private final int CodedSlice = 1;

 
private final int IDR = 5;
 
private final int SEI = 6;
 
private final int SPS = 7;
 
private final int PPS = 8;

...


  public void pushAVCFrame(int[] frame, int timecode)
  {
   
if (frame.length == 0) {
     
return;
   
}
   
if (this.keepNALU)
    {
     
IoBuffer buffV = IoBuffer.allocate(frame.length + 6);
      buffV.setAutoExpand
(true);
      buffV.put
((byte)23);
      buffV.put
((byte)1);

      buffV.put
((byte)0);
      buffV.put
((byte)0);
      buffV.put
((byte)0);

      buffV.put
((byte)0);
      buffV.put
((byte)0);

     
for (int r = 0; r < frame.length; ++r)
      {
       
buffV.put((byte)frame[r]);
     
}

     
buffV.put((byte)0);
      buffV.flip
();
      buffV.position
(0);

      IRTMPEvent videoNAL =
new VideoData(buffV);
      videoNAL.setHeader
(new Header());

//      this.output.dispatchEvent(videoNAL);

     
return;
   
}

   
for (int i = 0; i < frame.length; ++i) {
     
if ((frame[i] != 0) ||
       
(frame[(i + 1)] != 0) ||
       
(frame[(i + 2)] != 0) ||
       
(frame[(i + 3)] != 1))
       
continue;
      i +=
4;

     
int size = findFrameEnd(frame, i);

     
if (size == -1) {
       
size = frame.length - i;
     
}
     
else {
       
size -= i;
     
}
     
processNal(frame, i, size);

      i += size -
1;
   
}
  }

No comments:

Post a Comment