void KinesisVideoClientWrapper::putKinesisVideoFrame()

in src/JNI/com/amazonaws/kinesis/video/producer/jni/KinesisVideoClientWrapper.cpp [344:407]


void KinesisVideoClientWrapper::putKinesisVideoFrame(jlong streamHandle, jobject kinesisVideoFrame)
{
    STATUS retStatus = STATUS_SUCCESS;
    JNIEnv *env;
    mJvm->GetEnv((PVOID*) &env, JNI_VERSION_1_6);

    if (!IS_VALID_CLIENT_HANDLE(mClientHandle))
    {
        DLOGE("Invalid client object");
        throwNativeException(env, EXCEPTION_NAME, "Invalid call after the client is freed.", STATUS_INVALID_OPERATION);
        return;
    }

    if (!IS_VALID_STREAM_HANDLE(streamHandle))
    {
        DLOGE("Invalid stream handle 0x%016" PRIx64, (UINT64) streamHandle);
        throwNativeException(env, EXCEPTION_NAME, "Invalid stream handle.", STATUS_INVALID_OPERATION);
        return;
    }

    if (kinesisVideoFrame == NULL)
    {
        DLOGE("Invalid kinesis video frame.");
        throwNativeException(env, EXCEPTION_NAME, "Kinesis video frame is null.", STATUS_INVALID_OPERATION);
        return;
    }

    // Convert the KinesisVideoFrame object
    Frame frame;
    if (!setFrame(env, kinesisVideoFrame, &frame))
    {
        DLOGE("Failed converting frame object.");
        throwNativeException(env, EXCEPTION_NAME, "Failed converting frame object.", STATUS_INVALID_OPERATION);
        return;
    }

    PStreamInfo pStreamInfo;
    UINT32 zeroCount = 0;
    ::kinesisVideoStreamGetStreamInfo(streamHandle, &pStreamInfo);

    if ((pStreamInfo->streamCaps.nalAdaptationFlags & NAL_ADAPTATION_ANNEXB_NALS) != NAL_ADAPTATION_FLAG_NONE) {
        // In some devices encoder would generate annexb frames with more than 3 trailing zeros
        // which is not allowed by AnnexB specification
        while (frame.size > zeroCount) {
            if (frame.frameData[frame.size - 1 - zeroCount] != 0) {
                break;
            } else {
                zeroCount++;
            }
        }

        // Only remove zeros when zero count is more than 2
        if (zeroCount > 2) {
            frame.size -= zeroCount;
        }
    }

    if (STATUS_FAILED(retStatus = ::putKinesisVideoFrame(streamHandle, &frame)))
    {
        DLOGE("Failed to put a frame with status code 0x%08x", retStatus);
        throwNativeException(env, EXCEPTION_NAME, "Failed to put a frame into the stream.", retStatus);
        return;
    }
}