0

Playing video from RTSP stream on Android surface using solution from this repo https://github.com/alexandruc/android-ffmpeg-player/blob/master/android-ffmpeg-player/jni/ffmpeg-player.c

Video is playng but have a lot of glitches especially when something moving. Have not enought expirience using libav. Will be happy if someone can help or give links on some tutorials or community.

Here is the function to display video on surface.

void* decodeAndRender(void *voidArgs) {
auto *args = (decode_args*)voidArgs;
CamCon* cc = getCamCon(args->name);

ANativeWindow_Buffer    windowBuffer;
AVPacket                packet;
int                     i=0;
int                     frameFinished;
int                     lineCnt;

int counter = 0;
while(av_read_frame(cc->formatCtx, &packet)>=0 && cc->isConnect) {
    counter = 1;
    // Is this a packet from the video stream?
    if(packet.stream_index==cc->videoStreamIdx) {
        // Decode video frame
        avcodec_decode_video2(cc->codecCtx, cc->decodedFrame, &frameFinished, &packet);
        // Did we get a video frame?
        if(frameFinished) {
            // RECORD video
            if(cc->isRecord)
                recordMP4(packet, cc);

            // DISPLAY video
            // Convert the image from its native format to RGBA
            sws_scale (
                    cc->sws_ctx,
                    (uint8_t const * const *)cc->decodedFrame->data,
                    cc->decodedFrame->linesize,
                    0,
                    cc->codecCtx->height,
                    cc->frameRGBA->data,
                    cc->frameRGBA->linesize
            );
            // lock the window buffer
            if (ANativeWindow_lock(cc->window, &windowBuffer, NULL) < 0) {
                LOGE("cannot lock window");
            } else {
                // draw the frame on buffer
                LOGI("copy buffer %d:%d:%d", cc->displayWidth, cc->displayHeight, cc->displayWidth * cc->displayHeight*4);
                LOGI("window buffer: %d:%d:%d", windowBuffer.width,
                     windowBuffer.height, windowBuffer.stride);
                memcpy(windowBuffer.bits, cc->buffer,  cc->displayWidth * cc->displayHeight * 4);
                // unlock the window buffer and post it to display
                ANativeWindow_unlockAndPost(cc->window);
                // count number of frames
                ++i;
            }
        }
    }
    // Free the packet that was allocated by av_read_frame
    av_free_packet(&packet);
}
LOGI("total No. of frames decoded and rendered %d", i);
finish(args->env, args->name);

}

iLobanov
  • 381
  • 4
  • 7
  • You sohould probably read the answers to *[Play RTSP streaming in an Android application](https://stackoverflow.com/questions/11274906/play-rtsp-streaming-in-an-android-application)* – Alex Cohn Nov 16 '18 at 15:55
  • @AlexCohn, thx for your response. I seen this answer already. But I have no problems play RTSP stream on Android. I have several different implementations and my problem in playing with ffmpeg(libav libraries). I'm need full control of the video stream (record/share/play video at the same time with minimum resourse usage). Or I have problem with codec formats in my code? If so can you explain what actually should I change. – iLobanov Nov 20 '18 at 04:51

0 Answers0