當前位置: 首頁>>代碼示例>>Java>>正文


Java MediaCodec.BUFFER_FLAG_SYNC_FRAME屬性代碼示例

本文整理匯總了Java中android.media.MediaCodec.BUFFER_FLAG_SYNC_FRAME屬性的典型用法代碼示例。如果您正苦於以下問題:Java MediaCodec.BUFFER_FLAG_SYNC_FRAME屬性的具體用法?Java MediaCodec.BUFFER_FLAG_SYNC_FRAME怎麽用?Java MediaCodec.BUFFER_FLAG_SYNC_FRAME使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在android.media.MediaCodec的用法示例。


在下文中一共展示了MediaCodec.BUFFER_FLAG_SYNC_FRAME屬性的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: addSample

public void addSample(long offset, MediaCodec.BufferInfo bufferInfo) {
    long delta = bufferInfo.presentationTimeUs - lastPresentationTimeUs;
    if (delta < 0) {
        return;
    }
    boolean isSyncFrame = !isAudio && (bufferInfo.flags & MediaCodec.BUFFER_FLAG_SYNC_FRAME) != 0;
    samples.add(new Sample(offset, bufferInfo.size));
    if (syncSamples != null && isSyncFrame) {
        syncSamples.add(samples.size());
    }

    delta = (delta * timeScale + 500000L) / 1000000L;
    lastPresentationTimeUs = bufferInfo.presentationTimeUs;
    if (!first) {
        sampleDurations.add(sampleDurations.size() - 1, delta);
        duration += delta;
    }
    first = false;
}
 
開發者ID:MLNO,項目名稱:airgram,代碼行數:19,代碼來源:Track.java

示例2: getFirstIndex

/**
 * Returns the index of the oldest sync frame.  Valid until the next add().
 * <p>
 * When sending output to a MediaMuxer, start here.
 */
public int getFirstIndex() {
    final int metaLen = mPacketStart.length;

    int index = mMetaTail;
    while (index != mMetaHead) {
        if ((mPacketFlags[index] & MediaCodec.BUFFER_FLAG_SYNC_FRAME) != 0) {
            break;
        }
        index = (index + 1) % metaLen;
    }

    if (index == mMetaHead) {
        Log.w(TAG, "HEY: could not find sync frame in buffer");
        index = -1;
    }
    return index;
}
 
開發者ID:AndyZhu1991,項目名稱:grafika,代碼行數:22,代碼來源:CircularEncoderBuffer.java

示例3: addSample

public void addSample(long offset, MediaCodec.BufferInfo bufferInfo) {
    boolean isSyncFrame = !isAudio && (bufferInfo.flags & MediaCodec.BUFFER_FLAG_SYNC_FRAME) != 0;
    samples.add(new Sample(offset, bufferInfo.size));
    if (syncSamples != null && isSyncFrame) {
        syncSamples.add(samples.size());
    }

    long delta = bufferInfo.presentationTimeUs - lastPresentationTimeUs;
    lastPresentationTimeUs = bufferInfo.presentationTimeUs;
    delta = (delta * timeScale + 500000L) / 1000000L;
    if (!first) {
        sampleDurations.add(sampleDurations.size() - 1, delta);
        duration += delta;
    }
    first = false;
}
 
開發者ID:fishwjy,項目名稱:VideoCompressor,代碼行數:16,代碼來源:Track.java

示例4: dequeueOutputBuffer

OutputBufferInfo dequeueOutputBuffer() {
  checkOnMediaCodecThread();
  try {
    MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
    int result = mediaCodec.dequeueOutputBuffer(info, DEQUEUE_TIMEOUT);
    // Check if this is config frame and save configuration data.
    if (result >= 0) {
      boolean isConfigFrame = (info.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0;
      if (isConfigFrame) {
        Logging.d(TAG, "Config frame generated. Offset: " + info.offset + ". Size: " + info.size);
        configData = ByteBuffer.allocateDirect(info.size);
        outputBuffers[result].position(info.offset);
        outputBuffers[result].limit(info.offset + info.size);
        configData.put(outputBuffers[result]);
        // Log few SPS header bytes to check profile and level.
        String spsData = "";
        for (int i = 0; i < (info.size < 8 ? info.size : 8); i++) {
          spsData += Integer.toHexString(configData.get(i) & 0xff) + " ";
        }
        Logging.d(TAG, spsData);
        // Release buffer back.
        mediaCodec.releaseOutputBuffer(result, false);
        // Query next output.
        result = mediaCodec.dequeueOutputBuffer(info, DEQUEUE_TIMEOUT);
      }
    }
    if (result >= 0) {
      // MediaCodec doesn't care about Buffer position/remaining/etc so we can
      // mess with them to get a slice and avoid having to pass extra
      // (BufferInfo-related) parameters back to C++.
      ByteBuffer outputBuffer = outputBuffers[result].duplicate();
      outputBuffer.position(info.offset);
      outputBuffer.limit(info.offset + info.size);
      reportEncodedFrame(info.size);

      // Check key frame flag.
      boolean isKeyFrame = (info.flags & MediaCodec.BUFFER_FLAG_SYNC_FRAME) != 0;
      if (isKeyFrame) {
        Logging.d(TAG, "Sync frame generated");
      }
      if (isKeyFrame && type == VideoCodecType.VIDEO_CODEC_H264) {
        Logging.d(TAG, "Appending config frame of size " + configData.capacity()
                + " to output buffer with offset " + info.offset + ", size " + info.size);
        // For H.264 key frame append SPS and PPS NALs at the start
        ByteBuffer keyFrameBuffer = ByteBuffer.allocateDirect(configData.capacity() + info.size);
        configData.rewind();
        keyFrameBuffer.put(configData);
        keyFrameBuffer.put(outputBuffer);
        keyFrameBuffer.position(0);
        return new OutputBufferInfo(result, keyFrameBuffer, isKeyFrame, info.presentationTimeUs);
      } else {
        return new OutputBufferInfo(
            result, outputBuffer.slice(), isKeyFrame, info.presentationTimeUs);
      }
    } else if (result == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
      outputBuffers = mediaCodec.getOutputBuffers();
      return dequeueOutputBuffer();
    } else if (result == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
      return dequeueOutputBuffer();
    } else if (result == MediaCodec.INFO_TRY_AGAIN_LATER) {
      return null;
    }
    throw new RuntimeException("dequeueOutputBuffer: " + result);
  } catch (IllegalStateException e) {
    Logging.e(TAG, "dequeueOutputBuffer failed", e);
    return new OutputBufferInfo(-1, null, false, -1);
  }
}
 
開發者ID:Piasy,項目名稱:AppRTC-Android,代碼行數:68,代碼來源:MediaCodecVideoEncoder.java

示例5: deliverEncodedImage

private void deliverEncodedImage() {
  outputThreadChecker.checkIsOnValidThread();
  try {
    MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
    int index = codec.dequeueOutputBuffer(info, DEQUEUE_OUTPUT_BUFFER_TIMEOUT_US);
    if (index < 0) {
      return;
    }

    ByteBuffer codecOutputBuffer = codec.getOutputBuffers()[index];
    codecOutputBuffer.position(info.offset);
    codecOutputBuffer.limit(info.offset + info.size);

    if ((info.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
      Logging.d(TAG, "Config frame generated. Offset: " + info.offset + ". Size: " + info.size);
      configBuffer = ByteBuffer.allocateDirect(info.size);
      configBuffer.put(codecOutputBuffer);
    } else {
      bitrateAdjuster.reportEncodedFrame(info.size);
      if (adjustedBitrate != bitrateAdjuster.getAdjustedBitrateBps()) {
        updateBitrate();
      }

      final boolean isKeyFrame = (info.flags & MediaCodec.BUFFER_FLAG_SYNC_FRAME) != 0;
      if (isKeyFrame) {
        Logging.d(TAG, "Sync frame generated");
      }

      final ByteBuffer frameBuffer;
      if (isKeyFrame && codecType == VideoCodecType.H264) {
        Logging.d(TAG,
            "Prepending config frame of size " + configBuffer.capacity()
                + " to output buffer with offset " + info.offset + ", size " + info.size);
        // For H.264 key frame prepend SPS and PPS NALs at the start.
        frameBuffer = ByteBuffer.allocateDirect(info.size + configBuffer.capacity());
        configBuffer.rewind();
        frameBuffer.put(configBuffer);
      } else {
        frameBuffer = ByteBuffer.allocateDirect(info.size);
      }
      frameBuffer.put(codecOutputBuffer);
      frameBuffer.rewind();

      final EncodedImage.FrameType frameType = isKeyFrame
          ? EncodedImage.FrameType.VideoFrameKey
          : EncodedImage.FrameType.VideoFrameDelta;

      EncodedImage.Builder builder = outputBuilders.poll();
      builder.setBuffer(frameBuffer).setFrameType(frameType);
      // TODO(mellem):  Set codec-specific info.
      callback.onEncodedFrame(builder.createEncodedImage(), new CodecSpecificInfo());
    }
    codec.releaseOutputBuffer(index, false);
  } catch (IllegalStateException e) {
    Logging.e(TAG, "deliverOutput failed", e);
  }
}
 
開發者ID:Piasy,項目名稱:AppRTC-Android,代碼行數:57,代碼來源:HardwareVideoEncoder.java

示例6: dequeueOutputBuffer

OutputBufferInfo dequeueOutputBuffer() {
  checkOnMediaCodecThread();
  try {
    MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
    int result = mediaCodec.dequeueOutputBuffer(info, DEQUEUE_TIMEOUT);
    // Check if this is config frame and save configuration data.
    if (result >= 0) {
      boolean isConfigFrame = (info.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0;
      if (isConfigFrame) {
        Logging.d(TAG, "Config frame generated. Offset: " + info.offset + ". Size: " + info.size);
        configData = ByteBuffer.allocateDirect(info.size);
        outputBuffers[result].position(info.offset);
        outputBuffers[result].limit(info.offset + info.size);
        configData.put(outputBuffers[result]);
        // Release buffer back.
        mediaCodec.releaseOutputBuffer(result, false);
        // Query next output.
        result = mediaCodec.dequeueOutputBuffer(info, DEQUEUE_TIMEOUT);
      }
    }
    if (result >= 0) {
      // MediaCodec doesn't care about Buffer position/remaining/etc so we can
      // mess with them to get a slice and avoid having to pass extra
      // (BufferInfo-related) parameters back to C++.
      ByteBuffer outputBuffer = outputBuffers[result].duplicate();
      outputBuffer.position(info.offset);
      outputBuffer.limit(info.offset + info.size);
      reportEncodedFrame(info.size);

      // Check key frame flag.
      boolean isKeyFrame = (info.flags & MediaCodec.BUFFER_FLAG_SYNC_FRAME) != 0;
      if (isKeyFrame) {
        Logging.d(TAG, "Sync frame generated");
      }
      if (isKeyFrame && type == VideoCodecType.VIDEO_CODEC_H264) {
        Logging.d(TAG, "Appending config frame of size " + configData.capacity()
                + " to output buffer with offset " + info.offset + ", size " + info.size);
        // For H.264 key frame append SPS and PPS NALs at the start
        ByteBuffer keyFrameBuffer = ByteBuffer.allocateDirect(configData.capacity() + info.size);
        configData.rewind();
        keyFrameBuffer.put(configData);
        keyFrameBuffer.put(outputBuffer);
        keyFrameBuffer.position(0);
        return new OutputBufferInfo(result, keyFrameBuffer, isKeyFrame, info.presentationTimeUs);
      } else {
        return new OutputBufferInfo(
            result, outputBuffer.slice(), isKeyFrame, info.presentationTimeUs);
      }
    } else if (result == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
      outputBuffers = mediaCodec.getOutputBuffers();
      return dequeueOutputBuffer();
    } else if (result == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
      return dequeueOutputBuffer();
    } else if (result == MediaCodec.INFO_TRY_AGAIN_LATER) {
      return null;
    }
    throw new RuntimeException("dequeueOutputBuffer: " + result);
  } catch (IllegalStateException e) {
    Logging.e(TAG, "dequeueOutputBuffer failed", e);
    return new OutputBufferInfo(-1, null, false, -1);
  }
}
 
開發者ID:lgyjg,項目名稱:AndroidRTC,代碼行數:62,代碼來源:MediaCodecVideoEncoder.java

示例7: deliverEncodedImage

private void deliverEncodedImage() {
  try {
    int index = mediaCodec.dequeueOutputBuffer(outputBufferInfo,
            OUTPUT_THREAD_DEQUEUE_TIMEOUT_US);
    if (index < 0) {
      if (index == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
        callback.onOutputFormatChanged(mediaCodec, mediaCodec.getOutputFormat());
      }
      return;
    }

    ByteBuffer codecOutputBuffer = mediaCodec.getOutputBuffers()[index];
    codecOutputBuffer.position(outputBufferInfo.offset);
    codecOutputBuffer.limit(outputBufferInfo.offset + outputBufferInfo.size);

    if ((outputBufferInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
      Logging.d(TAG, "Config frame generated. Offset: " + outputBufferInfo.offset
                     + ". Size: " + outputBufferInfo.size);
      configData = ByteBuffer.allocateDirect(outputBufferInfo.size);
      configData.put(codecOutputBuffer);
      // Log few SPS header bytes to check profile and level.
      String spsData = "";
      for (int i = 0; i < (outputBufferInfo.size < 8 ? outputBufferInfo.size : 8); i++) {
        spsData += Integer.toHexString(configData.get(i) & 0xff) + " ";
      }
      Logging.d(TAG, spsData);
    } else {
      reportEncodedFrame(outputBufferInfo.size);

      // Check key frame flag.
      boolean isKeyFrame = (outputBufferInfo.flags & MediaCodec.BUFFER_FLAG_SYNC_FRAME) != 0;
      if (isKeyFrame && type == VideoCodecType.VIDEO_CODEC_H264) {
        // For H.264 key frame append SPS and PPS NALs at the start
        if (keyFrameData.capacity() < configData.capacity() + outputBufferInfo.size) {
          // allocate double size
          int newSize = Math.max(keyFrameData.capacity() * 2,
                  configData.capacity() + outputBufferInfo.size);
          keyFrameData = ByteBuffer.allocateDirect(newSize);
        }
        keyFrameData.position(0);
        configData.rewind();
        keyFrameData.put(configData);
        keyFrameData.put(codecOutputBuffer);
        keyFrameData.position(0);
        outputFrame.fill(index, keyFrameData, configData.capacity() + outputBufferInfo.size,
                isKeyFrame, outputBufferInfo.presentationTimeUs);
        callback.onEncodedFrame(outputFrame, outputBufferInfo);
        releaseOutputBuffer(index);
      } else {
        outputFrame.fill(index, codecOutputBuffer, outputBufferInfo.size, isKeyFrame,
                outputBufferInfo.presentationTimeUs);
        callback.onEncodedFrame(outputFrame, outputBufferInfo);
        releaseOutputBuffer(index);
      }
    }
  } catch (IllegalStateException e) {
    Logging.e(TAG, "deliverOutput failed", e);
  }
}
 
開發者ID:Piasy,項目名稱:VideoCRE,代碼行數:59,代碼來源:MediaCodecVideoEncoder.java


注:本文中的android.media.MediaCodec.BUFFER_FLAG_SYNC_FRAME屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。