使用的url
rtsp://admin:dowse123@192.168.1.31:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
rtsp://192.168.1.31:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
rtsp://192.168.1.31/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
特点:
1 @...../
2 //......:
3 //......./
代码记录如下
#ifndef DPRINTK
#define DPRINTK(fmt, args...) printf("(%s,%d)%s: " fmt,__FILE__,__LINE__, __FUNCTION__ , ## args)
#endif
static int FindSTR( char * byhtml, char *cfind, int nStartPos)
{
int i;
long length;
char * tmphtml;
char * temfind ;
if( byhtml == NULL || cfind == NULL)
return -1;
tmphtml =(char *) byhtml + nStartPos;
temfind = cfind;
if(nStartPos < 0)
return -1;
length = strlen(tmphtml);
for( i = 0 ; i < length; i++)
{
if( *tmphtml != *temfind )
{
tmphtml++;
continue;
}
while( *tmphtml == *temfind )
{
//printf(" %c.%c|",*tmphtml,*temfind);
tmphtml++;
temfind++;
if( *temfind == (char)NULL ) // 找到了。
{
return nStartPos + i;
}
}
//printf("\n");
if( *temfind == (char)NULL ) // 找到了。
{
return nStartPos + i;
}
else
{ // 从与temfind首字节相同的那一位的后一位重新开始找,
temfind = cfind;
tmphtml = (char *)byhtml + nStartPos + i + 1;
}
}
return -1;
}
// start 是指 '<' 所在的位置,end 指 '>'的位置,此函数取两个位置之间的那部分字符串。
static int GetTmpLink(char * byhtml, char * temp, int start, int end,int max_num)
{
int i;
if( byhtml == NULL || temp == NULL)
return -1;
if( end - start > max_num )
{
temp[0] = (char)NULL;
return -1;
}
for(i = start + 1; i < end ; i++)
{
*temp = byhtml[i];
temp++;
}
*temp = (char)NULL; // 结束符。
return 1;
}
static int getUrlIp(char * ip,const char * url)
{
char tmp_ip[100] ={0};
char tmp_user[100] = {0};
char tmp_pass[100] = {0};
char tmp_url[100] = {0};
char tmp_url2[100] = {0};
int len = 0;
int i = 0;
int j = 0;
int rel;
strcpy(tmp_url,url);
DPRINTK("============ FindSTR url:%s\n",url);
j = FindSTR(tmp_url,"@",0);
DPRINTK("=========== FindSTR \n");
if( j >= 0 )
{
//url中有用户信息,要提取出来。
rel = GetTmpLink(tmp_url,tmp_user,strlen("rtsp://")-1,j,30);
if( rel < 0)
{
DPRINTK("Can't get user info: %s\n",url);
return -1;
}else
{
DPRINTK("get tmp %s\n",tmp_user);
for( i = 0; i < strlen(tmp_user); i++)
{
if( tmp_user[i] == ':')
{
strcpy(tmp_pass, &tmp_user[i+1]);
tmp_user[i] = 0;
DPRINTK("get user %s %s\n",tmp_user,tmp_pass);
break;
}
}
}
sprintf(tmp_url2,"rtsp://%s",&tmp_url[j+1]);
DPRINTK("get url2 %s\n",tmp_url2);
strcpy(tmp_url,tmp_url2);
}
sscanf(tmp_url,"rtsp://%s",tmp_ip);
DPRINTK("==============tmp_ip:%s\n",tmp_ip);
len = strlen(tmp_ip);
for( i = 0; i < len ; i++)
{
if( tmp_ip[i] == ':' )
tmp_ip[i] = 0;
if( tmp_ip[i] == '/' )
tmp_ip[i] = 0;
}
if( tmp_ip[0] != 0 )
strcpy(ip,tmp_ip);
else
return -1;
return 1;
}
static bool checkUrl(const QString &url, int checkTime)
{
QRegExp reg("((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))");
reg.indexIn(url);
QString ip = url.mid(url.indexOf(reg), reg.matchedLength());
int port = 554;
QStringList list = url.split(":");
if (list.count() > 2) {
int index = 2;
if (url.contains("@")) {
index = 3;
}
list = list.at(index).split("/");
QString str = list.first();
if (!str.isEmpty()) {
port = str.toInt();
}
}
QEventLoop eventLoop;
QTimer timer;
QObject::connect(&timer, SIGNAL(timeout()), &eventLoop, SLOT(quit()));
timer.setSingleShot(true);
timer.start(checkTime);
QTcpSocket tcpSocket;
QObject::connect(&tcpSocket, SIGNAL(connected()), &eventLoop, SLOT(quit()));
tcpSocket.connectToHost(ip, port);
eventLoop.exec();
if (tcpSocket.state() != QAbstractSocket::ConnectedState) {
qDebug() << TIMEMS << url << "connect error";
return false;
}
return true;
}