一 大家有用过librtmp可曾遇到一个比较头疼的问题
向某家视频云推送视频的时候,需要指定域名,但是又想指定接入服务器ip。librtmp里面自己给解析
了域名,看看源码实现。
RTMP_Connect(RTMP *r, RTMPPacket *cp, unsigned int *outip, unsigned int *selfPort)
{
//fix the problem of when rtmp reconnect, the state should be reinitialize.
r->m_switchToNonBlock = 0;
r->m_sb.tcpBufferOffset = 0;
struct sockaddr_in service;
if (!r->Link.hostname.av_len)
{
return FALSE;
}
unsigned int nowtime = time(0);
memset(&service, 0, sizeof(struct sockaddr_in));
service.sin_family = AF_INET;
if (r->Link.socksport)
{
/* Connect via SOCKS */
if (!add_addr_info(&service, &r->Link.sockshost, r->Link.socksport))//DNS解析
二 实现代码
typedef struct RTMP
{
int m_inChunkSize;
int m_outChunkSize;
int m_nBWCheckCounter;
int m_nBytesIn;
int m_nBytesInSent;
int m_nBufferMS;
int m_stream_id; /* returned in _result from createStream */
int m_mediaChannel;
uint32_t m_mediaStamp;
uint32_t m_pauseStamp;
int m_pausing;
int m_nServerBW;
int m_nClientBW;
uint8_t m_nClientBW2;
uint8_t m_bPlaying;
uint8_t m_bSendEncoding;
uint8_t m_bSendCounter;
int m_numInvokes;
int m_numCalls;
RTMP_METHOD *m_methodCalls; /* remote method calls queue */
int m_channelsAllocatedIn;
int m_channelsAllocatedOut;
RTMPPacket **m_vecChannelsIn;
RTMPPacket **m_vecChannelsOut;
int *m_channelTimestamp; /* abs timestamp of last packet */
double m_fAudioCodecs; /* audioCodecs for the connect packet */
double m_fVideoCodecs; /* videoCodecs for the connect packet */
double m_fEncoding; /* AMF0 or AMF3 */
double m_fDuration; /* duration of stream in seconds */
int m_msgCounter; /* RTMPT stuff */
int m_polling;
int m_resplen;
int m_unackd;
AVal m_clientID;
RTMP_READ m_read;
RTMPPacket m_write;
RTMPSockBuf m_sb;
RTMP_LNK Link;
int m_switchToNonBlock;
char server_address[24];//新增服务器地址,从外面传进来
int server_port;//新增服务器端口
} RTMP;
RTMP_Connect-->
if(strlen(r->server_address) != 0)
{
service.sin_addr.s_addr = inet_addr(r->server_address);
service.sin_port = htons(r->server_port);
}
从外面传进去。
三 使用
重新编译 librtmp,调用的时候,将ip地址和端口写到RTMP对象中即可。
调用代码:
if(ip)
{
memset(rtmp_object_->server_address,0x0,24);//设置地址和端口
strcpy(rtmp_object_->server_address,ip);
rtmp_object_->server_port = 1935;
if(port != 0)
{
rtmp_object_->server_port = port;
}
}
if(!RTMP_Connect(rtmp_object_,NULL,(unsigned int *)&outip,(unsigned int *)&self_port))
{
return -1;
}
使用usage:
向某家视频云推送视频的时候,需要指定域名,但是又想指定接入服务器ip。librtmp里面自己给解析
了域名,看看源码实现。
RTMP_Connect(RTMP *r, RTMPPacket *cp, unsigned int *outip, unsigned int *selfPort)
{
//fix the problem of when rtmp reconnect, the state should be reinitialize.
r->m_switchToNonBlock = 0;
r->m_sb.tcpBufferOffset = 0;
struct sockaddr_in service;
if (!r->Link.hostname.av_len)
{
return FALSE;
}
unsigned int nowtime = time(0);
memset(&service, 0, sizeof(struct sockaddr_in));
service.sin_family = AF_INET;
if (r->Link.socksport)
{
/* Connect via SOCKS */
if (!add_addr_info(&service, &r->Link.sockshost, r->Link.socksport))//DNS解析
二 实现代码
typedef struct RTMP
{
int m_inChunkSize;
int m_outChunkSize;
int m_nBWCheckCounter;
int m_nBytesIn;
int m_nBytesInSent;
int m_nBufferMS;
int m_stream_id; /* returned in _result from createStream */
int m_mediaChannel;
uint32_t m_mediaStamp;
uint32_t m_pauseStamp;
int m_pausing;
int m_nServerBW;
int m_nClientBW;
uint8_t m_nClientBW2;
uint8_t m_bPlaying;
uint8_t m_bSendEncoding;
uint8_t m_bSendCounter;
int m_numInvokes;
int m_numCalls;
RTMP_METHOD *m_methodCalls; /* remote method calls queue */
int m_channelsAllocatedIn;
int m_channelsAllocatedOut;
RTMPPacket **m_vecChannelsIn;
RTMPPacket **m_vecChannelsOut;
int *m_channelTimestamp; /* abs timestamp of last packet */
double m_fAudioCodecs; /* audioCodecs for the connect packet */
double m_fVideoCodecs; /* videoCodecs for the connect packet */
double m_fEncoding; /* AMF0 or AMF3 */
double m_fDuration; /* duration of stream in seconds */
int m_msgCounter; /* RTMPT stuff */
int m_polling;
int m_resplen;
int m_unackd;
AVal m_clientID;
RTMP_READ m_read;
RTMPPacket m_write;
RTMPSockBuf m_sb;
RTMP_LNK Link;
int m_switchToNonBlock;
char server_address[24];//新增服务器地址,从外面传进来
int server_port;//新增服务器端口
} RTMP;
RTMP_Connect-->
if(strlen(r->server_address) != 0)
{
service.sin_addr.s_addr = inet_addr(r->server_address);
service.sin_port = htons(r->server_port);
}
从外面传进去。
三 使用
重新编译 librtmp,调用的时候,将ip地址和端口写到RTMP对象中即可。
调用代码:
if(ip)
{
memset(rtmp_object_->server_address,0x0,24);//设置地址和端口
strcpy(rtmp_object_->server_address,ip);
rtmp_object_->server_port = 1935;
if(port != 0)
{
rtmp_object_->server_port = port;
}
}
if(!RTMP_Connect(rtmp_object_,NULL,(unsigned int *)&outip,(unsigned int *)&self_port))
{
return -1;
}
使用usage:
./bin xxx.flv rtmp://xxx.com/live/xxx 127.0.0.1 1935