設置 | 登錄 | 註冊

作者共發了2篇帖子。

rtsp转发代码

1樓 巨大八爪鱼 2025-3-20 11:51
#include <stdio.h>
#include <WinSock2.h>

#pragma comment(lib, "ws2_32.lib")

struct line_status
{
    char line[1024];
    char prev_c;
    int data_i;
    int line_i;
};

char *parse_line(const char *data, int len, struct line_status *line_status)
{
    char prev_c, c;

    while (line_status->data_i < len)
    {
        prev_c = line_status->prev_c;
        c = data[line_status->data_i];
        line_status->data_i++;
        line_status->prev_c = c;
        if (line_status->line_i < sizeof(line_status->line) - 1)
        {
            line_status->line[line_status->line_i] = c;
            line_status->line_i++;
        }
        if (prev_c == '\r' && c == '\n')
        {
            line_status->line[line_status->line_i] = '\0';
            line_status->line_i = 0;
            return line_status->line;
        }
    }
    line_status->data_i = 0;
    return NULL;
}

int str_starts_with(const char *haystack, const char *needle)
{
    int a, b;

    a = strlen(haystack);
    b = strlen(needle);
    return a >= b && memcmp(haystack, needle, b) == 0;
}

void process_client(SOCKET sock_client)
{
    char data[4096];
    char *line;
    int len, ret;
    struct line_status line_status = {0};
    FD_SET readset;
    SOCKADDR_IN addr = {0};
    SOCKET sock_target, sock_max;
    TIMEVAL timeout;
    
    sock_target = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    addr.sin_family = AF_INET;
    addr.sin_addr.S_un.S_addr = inet_addr("172.32.0.93");
    addr.sin_port = htons(554);
    ret = connect(sock_target, (LPSOCKADDR)&addr, sizeof(addr));
    if (ret == -1)
    {
        printf("connect() failed\n");
        goto end;
    }

    sock_max = 0;
    if (sock_max < sock_client)
        sock_max = sock_client;
    if (sock_max < sock_target)
        sock_max = sock_target;
    
    FD_ZERO(&readset);
    timeout.tv_sec = 5;
    timeout.tv_usec = 0;
    while (1)
    {
        FD_SET(sock_client, &readset);
        FD_SET(sock_target, &readset);
        ret = select(sock_max + 1, &readset, NULL, NULL, &timeout);
        if (ret == -1)
            break;

        if (FD_ISSET(sock_client, &readset))
        {
            printf("sock_client\n");
            ret = recv(sock_client, data, sizeof(data), 0);
            if (ret <= 0)
            {
                printf("Connection has been closed by the media player.\n");
                break;
            }

            while (line = parse_line(data, ret, &line_status))
            {
                len = strlen(line);
                if (!str_starts_with(line, "ClientChallenge:") && !str_starts_with(line, "PlayerStarttime:") && !str_starts_with(line, "CompanyID:") && !str_starts_with(line, "GUID:") && !str_starts_with(line, "ClientChallenge:") && !str_starts_with(line, "ClientID:") && !str_starts_with(line, "RegionData:"))
                {
                    printf("[Send] len=%d\n%s", len, line);
                    send(sock_target, line, len, 0);
                }
            }
        }
        if (FD_ISSET(sock_target, &readset))
        {
            printf("sock_target\n");
            ret = recv(sock_target, data, sizeof(data), 0);
            if (ret <= 0)
            {
                printf("Connection has been closed by the device.\n");
                break;
            }
            printf("[Recv] len=%d\n%.*s", ret, ret, data);
            send(sock_client, data, ret, 0);
        }
    }

end:
    closesocket(sock_target);
    closesocket(sock_client);
}

void main()
{
    int ret;
    SOCKADDR_IN addr = {0};
    SOCKET sock_client, sock_server;
    WSADATA wsadata;

    WSAStartup(MAKEWORD(2, 2), &wsadata);
    
    sock_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    addr.sin_family = AF_INET;
    addr.sin_port = htons(554);
    ret = bind(sock_server, (LPSOCKADDR)&addr, sizeof(addr));
    if (ret == -1)
    {
        printf("bind() failed. ret=%d\n", ret);
        goto err;
    }

    listen(sock_server, 5);
    ret = sizeof(addr);
    sock_client = accept(sock_server, (LPSOCKADDR)&addr, &ret);
    process_client(sock_client);
    
err:
    closesocket(sock_server);
    WSACleanup();
}
2樓 巨大八爪鱼 2025-3-20 14:33
sock_client
[Send] len=42
OPTIONS rtsp://localhost/live/0 RTSP/1.0
[Send] len=9
CSeq: 2
[Send] len=64
User-Agent: LibVLC/2.1.3 (LIVE555 Streaming Media v2014.01.21)
[Send] len=2

sock_target
[Recv] len=136
RTSP/1.0 200 OK
CSeq: 2
Date: Thu Jan  1 00:01:51 1970
Public: OPTIONS, DESCRIBE, SETUP, PLAY, PAUSE, TEARDOWN
Server: rtsp_demo

sock_client
[Send] len=43
DESCRIBE rtsp://localhost/live/0 RTSP/1.0
[Send] len=9
CSeq: 3
[Send] len=64
User-Agent: LibVLC/2.1.3 (LIVE555 Streaming Media v2014.01.21)
[Send] len=25
Accept: application/sdp
[Send] len=2

sock_target
[Recv] len=592
RTSP/1.0 200 OK
CSeq: 3
Date: Thu Jan  1 00:01:51 1970
Server: rtsp_demo
Content-Type: application/sdp
Content-Length: 461

v=0
o=- 0 0 IN IP4 0.0.0.0
s=rtsp_demo
t=0 0
a=control:rtsp://localhost/live/0
a=range:npt=0-
m=video 0 RTP/AVP 96
c=IN IP4 0.0.0.0
a=rtpmap:96 H265/90000
a=fmtp:96 sprop-vps=QAEMAf//AWAAAAMAgAAAAwAAAwCWvAk=;sprop-sps=QgEBAWAAAAMAgAAAA
wAAAwCWoAEgIAURY2+7ymbgICAggAAAAwCAAAAMhA==;sprop-pps=RAHAcoaNQTZA
a=control:rtsp://localhost/live/0/track1
m=audio 0 RTP/AVP 97
c=IN IP4 0.0.0.0
a=rtpmap:97 PCMA/8000/1
a=control:rtsp://localhost/live/0/track2
sock_client
[Send] len=47
SETUP rtsp://localhost/live/0/track1 RTSP/1.0
[Send] len=9
CSeq: 4
[Send] len=64
User-Agent: LibVLC/2.1.3 (LIVE555 Streaming Media v2014.01.21)
[Send] len=50
Transport: RTP/AVP;unicast;client_port=3808-3809
[Send] len=2

sock_target
[Recv] len=186
RTSP/1.0 200 OK
CSeq: 4
Date: Thu Jan  1 00:01:52 1970
Session: 12345679
Transport: RTP/AVP;ssrc=2234567A;unicast;client_port=3808-3809;server_port=49152
-49153
Server: rtsp_demo

sock_client
[Send] len=47
SETUP rtsp://localhost/live/0/track2 RTSP/1.0
[Send] len=9
CSeq: 5
[Send] len=64
User-Agent: LibVLC/2.1.3 (LIVE555 Streaming Media v2014.01.21)
[Send] len=50
Transport: RTP/AVP;unicast;client_port=3810-3811
[Send] len=19
Session: 12345679
[Send] len=2

sock_target
[Recv] len=186
RTSP/1.0 200 OK
CSeq: 5
Date: Thu Jan  1 00:01:52 1970
Session: 12345679
Transport: RTP/AVP;ssrc=2234567B;unicast;client_port=3810-3811;server_port=49154
-49155
Server: rtsp_demo

sock_client
[Send] len=39
PLAY rtsp://localhost/live/0 RTSP/1.0
[Send] len=9
CSeq: 6
[Send] len=64
User-Agent: LibVLC/2.1.3 (LIVE555 Streaming Media v2014.01.21)
[Send] len=19
Session: 12345679
[Send] len=19
Range: npt=0.000-
[Send] len=2

sock_target
[Recv] len=98
RTSP/1.0 200 OK
CSeq: 6
Date: Thu Jan  1 00:01:52 1970
Session: 12345679
Server: rtsp_demo

sock_client
[Send] len=50
TEARDOWN rtsp://localhost/live/0/track1 RTSP/1.0
[Send] len=9
CSeq: 7
[Send] len=64
User-Agent: LibVLC/2.1.3 (LIVE555 Streaming Media v2014.01.21)
[Send] len=19
Session: 12345679
[Send] len=2

sock_target
[Recv] len=98
RTSP/1.0 200 OK
CSeq: 7
Date: Thu Jan  1 00:01:52 1970
Session: 12345679
Server: rtsp_demo

sock_client
[Send] len=43
TEARDOWN rtsp://localhost/live/0 RTSP/1.0
[Send] len=9
CSeq: 8
[Send] len=64
User-Agent: LibVLC/2.1.3 (LIVE555 Streaming Media v2014.01.21)
[Send] len=19
Session: 12345679
[Send] len=2

sock_client
Connection has been closed by the media player.
请按任意键继续. . .

內容轉換:

回覆帖子
內容:
用戶名: 您目前是匿名發表。
驗證碼:
看不清?換一張
©2010-2025 Purasbar Ver3.0 [手機版] [桌面版]
除非另有聲明,本站採用知識共享署名-相同方式共享 3.0 Unported許可協議進行許可。