This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

RTSP receiver C-code help



Hi All,

HW: DM814x
SW: ipnc v3.2

          I am streaming H264 encoded video over RTSP. VLC player able to receive this stream and play it properly.
But, I want to write a custom socket app, which can look into my port & ip of the board , receive data & dump to a File.

I wrote below code to achieve that. It successfully creates sockets, connects & then waits in receive from function without receiving any packets. I don have any clue WHY IT WAITS in recvfrom()?  It will be great if some one  can help me here.

/********************************************************** My udp scoket(receiver) code**********************************************************************/
 
/* Creates a datagram server. The port number is passed as an argument. This server runs forever */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h> /* close() */
#include <string.h> /* memset() */
#include <stdlib.h>
#include<time.h>
#define SIZE_IN_BYTES 1500

/* This IP & port represents Ip & port of Board */
#define IPADDR "192.168.1.109"
#define PORT 8557

void error(char *msg)
{
perror(msg);
exit(0);
}

int main(int argc, char *argv[])
{
int sock, length, fromlen, n;
struct sockaddr_in server;
struct sockaddr_in from;
char buf[SIZE_IN_BYTES];//1500
static int count=0,frame=0;
static int inTime=0, outTime=0;
unsigned short disp;
int ret;

if (argc < 2)
{
    fprintf(stderr, "ERROR, no port provided\n");
    exit(0);
}
sock=socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
    error("Opening socket");
}

int val=1;
int ret1=setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(const int *)(&val), sizeof val);
    if(ret1<0)
    {    
        printf("ERR in set opt\n\n");
        exit(1);
    }


length = sizeof(server);
bzero(&server,length);
server.sin_family=AF_INET;
server.sin_port=htons(atoi(argv[1]));
if (inet_aton(IPADDR, &server.sin_addr)==0)
{
          fprintf(stderr, "inet_aton() failed\n");
          exit(1);
}

   if (connect(sock,(struct sockaddr *)&server,length)<0)
    {
        error("pathpartner connect: ");
    }

    fromlen = sizeof(struct sockaddr_in);

    while (1)
    {
        n = recvfrom(sock,buf,SIZE_IN_BYTES,0,(struct sockaddr *)&from,&fromlen);
        if (n < 0)
        {
            error("recvfrom");
            exit(0);
        }
        else
        {
            frame++;
            printf("\nreceived data count=%d,frames=%d\n",count,frame);
        }

    }    
}

  • ravikiran hv

    I don't think it's wise idea to write a socket program to connect RTSP server from the beginning, because you will not get streaming after

    some come and go interaction(RTCP negotiation ) with RTSP server, if you use wireshark to capture the packets when you used VLC player to connect Camera,

    you'll know what's happening there,

    Why not referring to some open source application, Like ffmpeg4 which already supported 'Connect to the RTSP server and dump file',

    you can modify as you needed based on it.

     

    Best regards,

    Christian.

  • Ravikiran,

    As I Christian suggested you should look at live555 or other opensource stacks which will enable you to receive H.264 frame directly but just to answer your question I would like to mention following points.

    1. I see you are creating a socket of type SOCK_STREAM, are you sure your source is using RTP over TCP underneath RTSP? Usually video data is streamed on RTP over UDP.  Please confirm this and then create socket appropriately SOCK_STREAM or SOCK_DGRAM

    2. I dont see you using bind(), you need to bind your socket to some address (see $>man 2 bind), If it is a TCP connection you have additional system calls accept() and listen() before connect(), if its is UDP connection you should be doing connect() after bind(). Lot of doumentation is available on these system calls

    3. You might need to explicitly make socket non-blocking using O_NONBLOCK with fcntl(), I you have blocking socket and you are using select(), it will sometimes block even after it is marked as ready to be read, this typically can happen if checksum fails.