Я принимаю медиа-поток MPEG2-TS по UDP, вот код#include <iostream>
#include <stdio.h>
#include <fcntl.h>
#if defined(__linux__)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#elif defined(_WIN32)
#include <winsock2.h>
#else
#error "Cannot determine OS type"
#endif
#define TIMEOUT 5000
//mg - multicast group
#define port_mg 3425
#define ip_mg "237.7.7.7"
using namespace std;
//Receiving Transport Stream from Multicast Group
void receive_from(int ic_sock)
{
size_t bytes_read;
char buf[1024];
int i = 1;
cout << "Source: udp://" << ip_mg << ":" << port_mg << endl;
while(true)
{
bytes_read = recvfrom(ic_sock, buf, 1024, 0, NULL, NULL);
buf[bytes_read] = '\0';
if (0 <= bytes_read)
{
cout << i << ". Read bytes - " << bytes_read << endl;
}
else
{
break;
}
i++;
}
}
int main()
{
//Create socket
int incoming_sock, rc;
incoming_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (incoming_sock < 0)
{
perror("socket");
return -1;
}
cout << "socket: Success" << endl;
//Bind socket with local interface
struct sockaddr_in saddr;
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = INADDR_ANY;
saddr.sin_port = htons(port_mg);
rc = bind(incoming_sock, (struct sockaddr *)&saddr, sizeof(struct sockaddr_in));
if (rc != 0)
{
perror("bind");
return -2;
}
cout << "bind: Success" << endl;
//Join to multicast group
struct ip_mreq mreq;
mreq.imr_interface.s_addr = INADDR_ANY;
inet_aton(ip_mg, &mreq.imr_multiaddr);
//Adjuct socket for receiving
rc = setsockopt(incoming_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*) &mreq, sizeof(mreq));
if (rc != 0)
{
perror("setsockopt");
return -3;
}
cout << "setsockopt: Success" << endl;
receive_from(incoming_sock);
close(incoming_sock);
return 0;
}
Как мне теперь сохранить медиа-поток в файл?