Skip to content

Commit

Permalink
Add 'PayloadType' check to make it compatible with SDP media without …
Browse files Browse the repository at this point in the history
…'time rate'
  • Loading branch information
Ansersion committed Feb 22, 2016
1 parent 016f3b0 commit 3c45fea
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ CoreDumpTest.sh
example/recv_video_and_audio_example
example/test_audio_recv.mp3
example/test_video_recv.h264
test.txt
54 changes: 53 additions & 1 deletion gtest/test_suit_rtspClient_parseSDP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ TEST(rtspClient, RtspClient_ParseSDP_InvalidInput)
EXPECT_EQ(MediaSessionMap.empty(), true);
}

TEST(rtspClient, RtspClient_ParseSDP_RegularInput)
TEST(rtspClient, RtspClient_ParseSDP_1_RegularInput)
{
string SDP("\
RTSP/1.0 200 OK\r\n\
Expand Down Expand Up @@ -104,11 +104,63 @@ TEST(rtspClient, RtspClient_ParseSDP_RegularInput)
EXPECT_EQ(true, MediaSessionMap["audio"].TimeRate == 90000);
EXPECT_EQ(true, MediaSessionMap["audio"].ControlURI == "rtsp://127.0.0.1:554/ansersion/trackID=0");

EXPECT_EQ(1, MediaSessionMap["audio"].PayloadType.size());
for(vector<int>::iterator it = MediaSessionMap["audio"].PayloadType.begin(); it != MediaSessionMap["audio"].PayloadType.end(); it++) {
EXPECT_EQ(14, *it);
}

EXPECT_EQ(true, MediaSessionMap["video"].MediaType == "video");
EXPECT_EQ(true, MediaSessionMap["video"].EncodeType == "H264");
EXPECT_EQ(true, MediaSessionMap["video"].Protocol == "RTP/AVP");
EXPECT_EQ(true, MediaSessionMap["video"].TimeRate == 90000);
EXPECT_EQ(true, MediaSessionMap["video"].ControlURI == "rtsp://127.0.0.1:554/ansersion/trackID=1");
EXPECT_EQ(true, MediaSessionMap["video"].Packetization == 1);

EXPECT_EQ(1, MediaSessionMap["audio"].PayloadType.size());
for(vector<int>::iterator it = MediaSessionMap["video"].PayloadType.begin(); it != MediaSessionMap["video"].PayloadType.end(); it++) {
EXPECT_EQ(96, *it);
}
}

TEST(rtspClient, RtspClient_ParseSDP_2_RegularInput)
{
string SDP("\
v=0\r\n\
o=- 1456130741857568 1 IN IP4 192.168.1.162\r\n\
s=Session streamed by testOnDemandRTSPServer\r\n\
i=mp3AudioTest\r\n\
t=0 0\r\n\
a=tool:LIVE555 Streaming Media v2015.12.22\r\n\
a=type:broadcast\r\n\
a=control:*\r\n\
a=range:npt=0-163.524\r\n\
a=x-qt-text-nam:Session streamed by testOnDemandRTSPServer\r\n\
a=x-qt-text-inf:mp3AudioTest\r\n\
m=audio 0 RTP/AVP 14\r\n\
c=IN IP4 0.0.0.0\r\n\
b=AS:128\r\n\
a=control:track1\r\n");

RtspClient Client;
Client.ParseSDP(SDP);
// multimap<string, string> SDPInfo = Client.GetSDPInfo();
// typedef multimap<string, string>::iterator It_type;
// pair<It_type, It_type> p = SDPInfo.equal_range("s");
// It_type It = p.first;
map<string, MediaSession> MediaSessionMap = Client.GetMediaSessions();

// EXPECT_EQ(true, It->second == "Unnamed");
// EXPECT_EQ(SDPInfo.count("a"), 10);
// EXPECT_EQ(SDPInfo.count("m"), 2);
// EXPECT_EQ(SDPInfo.count("v"), 1);
// EXPECT_EQ(SDPInfo.count("x"), 0);

EXPECT_EQ(true, MediaSessionMap["audio"].MediaType == "audio");
EXPECT_EQ(true, MediaSessionMap["audio"].Protocol == "RTP/AVP");
EXPECT_EQ(true, MediaSessionMap["audio"].TimeRate == 90000);

EXPECT_EQ(1, MediaSessionMap["audio"].PayloadType.size());
for(vector<int>::iterator it = MediaSessionMap["audio"].PayloadType.begin(); it != MediaSessionMap["audio"].PayloadType.end(); it++) {
EXPECT_EQ(14, *it);
}
}
34 changes: 14 additions & 20 deletions gtest/test_suit_tmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,26 @@
#include "myRegex.h"
#include "rtspClient.h"
#include "MediaSession.h"
// #include "myRtpSession.h"

// using namespace std;
// using namespace jrtplib;
using namespace std;
// #include "myRtpSession.h"

// TEST(rtspClient, TMP_TEST)
// TEST(myRegex, Regex_InvalidInput)
// {
// MyRegex Regex;
//
// MediaSession Ms;
// Ms.RTPPort = 20020;
// Ms.TimeRate = 8000;
// // char str[] = "audio 0 RTP/AVP 14 16 18\r\n";
// char str[] = "audio 0 RTP/AVP 14\r\n";
// char pattern[] = "([a-zA-Z]+) +([0-9/]+) +([A-Za-z/]+) +\\b([0-9]+)\\b";
// list<string> group;
// bool IgnoreCase = true;
//
// MyRTPSession Mrs;
// if(!Mrs.MyRTP_SetUp(&Ms)) {
// printf("Msr Setup Error");
// return;
// }
//
// uint8_t buf[4192];
// size_t size = 0;
// int num = 100;
// EXPECT_EQ(Regex.Regex(str, pattern, &group, IgnoreCase), MyRegex::REGEX_SUCCESS);
//
// for (int i = 1 ; i <= num ; i++)
// {
// Mrs.GetMyRTPData(buf, &size);
// printf("size: %lu\n", size);
// int i = 0;
// for(list<string>::iterator it = group.begin(); it != group.end(); it++) {
// i++;
// cout << i << ": " << *it << endl;
// }
// EXPECT_EQ(true, true);
// }
54 changes: 54 additions & 0 deletions myRtspClient/MediaSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,45 @@
#include "MediaSession.h"
#include "myRtpSession.h"
#include "nalu_types.h"
#include <vector>

using std::vector;

#define MEDIA_SESSION_OK 1
#define MEDIA_SESSION_ERROR 0

/*
Refer to RTP PayloadType list
"0" means that the item is reserved
*/
int PT2TimeRateMap[] =
{
8000,
0,
0,
8000,
8000,
8000,
16000,
8000,
8000,
8000,
44100,
44100,
8000,
8000,
90000,
8000,
11025,
22050,
8000,
0,
0,
0,
0,
0
};

MediaSession::MediaSession():
MediaType(""),
Protocol(""),
Expand Down Expand Up @@ -88,3 +123,22 @@ uint8_t * MediaSession::GetMediaPacket(uint8_t * buf, size_t * size, unsigned lo
if(!RTPInterface) return NULL;
return RTPInterface->GetMyRTPPacket(buf, size, timeout);
}

int MediaSession::MediaInfoCheck()
{
// Check "PayloadType"
if(PayloadType.size() == 0) {
printf("WARNING: invalid PayloadType\n");
return -1;
}
for(vector<int>::iterator it = PayloadType.begin(); it != PayloadType.end(); it++) {
if(*it < 0) {
printf("WARNING: invalid PayloadType\n");
return -1;
}
}
if(TimeRate <= 0) {
TimeRate = PT2TimeRateMap[*(PayloadType.begin())]; // FIXME: only use the first PayloadType
printf("MediaInfoCheck: %d\n", TimeRate);
}
}
6 changes: 4 additions & 2 deletions myRtspClient/include/MediaSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class StreamParameters // ###2015-01-11### //
{
public:
/* For general media session */
unsigned int PayloadNum;
unsigned int PayloadType;
std::string EncodeType;
unsigned int TimeRate;

Expand Down Expand Up @@ -68,12 +68,14 @@ class MediaSession
* */
uint8_t * GetMediaPacket(uint8_t * buf, size_t * size, unsigned long timeout_ms = TIMEOUT_MICROSECONDS);

int MediaInfoCheck();

public:
std::string MediaType;
std::vector<uint16_t> Ports; // RTP and RTCP ports, -1 indicate none. ###2015-01-11### //
std::string Protocol;

std::vector<int> PayloadNum; // ###2015-01-11### //
std::vector<int> PayloadType; // ###2015-01-11### //
std::string EncodeType;
unsigned int TimeRate;
// std::map<unsigned int, StreamParameters> StreamParams;
Expand Down
20 changes: 18 additions & 2 deletions myRtspClient/rtspClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,18 +470,30 @@ int RtspClient::ParseSDP(string SDP)
if(Key == "s") CollectMediaInfo = false;
if(!CollectMediaInfo) continue;

if(Key == "m") {
string PatternTmp("([a-zA-Z]+) +.+ +(.+) +.*");
if(Key == "m") {
/* Pattern: (MediaType) +(Ports) +(Protocol) +(PayloadType)"
Example: "(audio) (0) (RTP/AVP) (14)"
*/
// string PatternTmp("([a-zA-Z]+) +.+ +(.+) +.*");
string PatternTmp("([a-zA-Z]+) +([0-9/]+) +([A-Za-z/]+) +\\b([0-9]+)\\b");
if(!Regex.Regex(Value.c_str(), PatternTmp.c_str(), &Group)) {
continue;
}
Group.pop_front();
CurrentMediaSession.assign(Group.front());
Group.pop_front();
Group.pop_front(); // FIXME: Ports are ignored
string Protocol(Group.front());
Group.pop_front();
int PayloadTypeTmp = -1;
stringstream ssPayloadType;
ssPayloadType << Group.front();
ssPayloadType >> PayloadTypeTmp;

MediaSession NewMediaSession;
NewMediaSession.MediaType.assign(CurrentMediaSession);
NewMediaSession.Protocol.assign(Protocol);
NewMediaSession.PayloadType.push_back(PayloadTypeTmp);
(*MediaSessionMap)[CurrentMediaSession] = NewMediaSession;

}
Expand Down Expand Up @@ -536,6 +548,10 @@ int RtspClient::ParseSDP(string SDP)
}
}
}

for(map<string, MediaSession>::iterator it = MediaSessionMap->begin(); it != MediaSessionMap->end(); it++) {
it->second.MediaInfoCheck();
}

return Result;
}
Expand Down
3 changes: 2 additions & 1 deletion third_party/jrtplib-3.9.1/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ include(CheckCXXSourceCompiles)
include(TestBigEndian)
include(${PROJECT_SOURCE_DIR}/cmake/Macros.cmake)

find_package(JThread)
# Uncomment the following to enable JThread probing
# find_package(JThread)

set(JRTPLIB_LINK_LIBS "")
set(JRTPLIB_INTERNAL_INCLUDES "")
Expand Down

0 comments on commit 3c45fea

Please sign in to comment.