3
* Copyright 2012, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* 1. Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
* 3. The name of the author may not be used to endorse or promote products
14
* derived from this software without specific prior written permission.
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
#include "talk/app/webrtc/jsepicecandidate.h"
31
#include "talk/app/webrtc/jsepsessiondescription.h"
32
#include "talk/base/gunit.h"
33
#include "talk/base/scoped_ptr.h"
34
#include "talk/p2p/base/candidate.h"
35
#include "talk/p2p/base/constants.h"
36
#include "talk/p2p/base/sessiondescription.h"
37
#include "talk/session/phone/mediasession.h"
39
using webrtc::IceCandidateColletion;
40
using webrtc::IceCandidateInterface;
41
using webrtc::JsepIceCandidate;
42
using webrtc::JsepSessionDescription;
43
using webrtc::SessionDescriptionInterface;
44
using talk_base::scoped_ptr;
46
// This creates a session description with both audio and video media contents.
47
// In SDP this is described by two m lines, one audio and one video.
48
static cricket::SessionDescription* CreateCricketSessionDescription() {
49
cricket::SessionDescription* desc(new cricket::SessionDescription());
50
// AudioContentDescription
51
scoped_ptr<cricket::AudioContentDescription> audio(
52
new cricket::AudioContentDescription());
54
// VideoContentDescription
55
scoped_ptr<cricket::VideoContentDescription> video(
56
new cricket::VideoContentDescription());
58
audio->AddCodec(cricket::AudioCodec(103, "ISAC", 16000, 0, 0, 0));
59
desc->AddContent(cricket::CN_AUDIO, cricket::NS_JINGLE_RTP,
62
video->AddCodec(cricket::VideoCodec(120, "VP8", 640, 480, 30, 0));
63
desc->AddContent(cricket::CN_VIDEO, cricket::NS_JINGLE_RTP,
68
class JsepSessionDescriptionTest : public testing::Test {
70
virtual void SetUp() {
72
talk_base::SocketAddress address("127.0.0.1", port++);
73
cricket::Candidate candidate("rtp", "udp", address, 1, "user_rtp",
74
"password_rtp", "local", "eth0", 0);
75
candidate_ = candidate;
77
jsep_desc_.reset(new JsepSessionDescription());
78
jsep_desc_->SetDescription(CreateCricketSessionDescription());
81
std::string Serialize(const SessionDescriptionInterface* desc) {
83
EXPECT_TRUE(desc->ToString(&sdp));
84
EXPECT_FALSE(sdp.empty());
88
SessionDescriptionInterface* DeSerialize(const std::string& sdp) {
89
JsepSessionDescription* desc(new JsepSessionDescription());
90
EXPECT_TRUE(desc->Initialize(sdp));
94
cricket::Candidate candidate_;
95
talk_base::scoped_ptr<JsepSessionDescription> jsep_desc_;
98
// Test that number_of_mediasections() returns the number of media contents in
99
// a session description.
100
TEST_F(JsepSessionDescriptionTest, CheckSessionDescription) {
101
EXPECT_EQ(2u, jsep_desc_->number_of_mediasections());
104
// Test that we can add a candidate to a session description.
105
TEST_F(JsepSessionDescriptionTest, AddCandidate) {
106
JsepIceCandidate jsep_candidate("0", candidate_);
107
EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
108
const IceCandidateColletion* ice_candidates = jsep_desc_->candidates(0);
109
ASSERT_TRUE(ice_candidates != NULL);
110
EXPECT_EQ(1u, ice_candidates->count());
111
const IceCandidateInterface* ice_candidate = ice_candidates->at(0);
112
ASSERT_TRUE(ice_candidate != NULL);
113
EXPECT_TRUE(ice_candidate->candidate().IsEquivalent(candidate_));
115
EXPECT_EQ(0u, jsep_desc_->candidates(1)->count());
118
// Test that we can not add a candidate if there is no corresponding media
119
// content in the session description.
120
TEST_F(JsepSessionDescriptionTest, AddBadCandidate) {
121
JsepIceCandidate bad_candidate1("55", candidate_);
122
EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate1));
124
JsepIceCandidate bad_candidate2("some weird label", candidate_);
125
EXPECT_FALSE(jsep_desc_->AddCandidate(&bad_candidate2));
128
// Test that we can serialize a JsepSessionDescription and deserialize it again.
129
TEST_F(JsepSessionDescriptionTest, SerializeDeserialize) {
130
std::string sdp = Serialize(jsep_desc_.get());
132
scoped_ptr<SessionDescriptionInterface> parsed_jsep_desc(DeSerialize(sdp));
133
EXPECT_EQ(2u, parsed_jsep_desc->number_of_mediasections());
135
std::string parsed_sdp = Serialize(parsed_jsep_desc.get());
136
EXPECT_EQ(sdp, parsed_sdp);
139
// Tests that we can serialize and deserialize a JsepSesssionDescription
141
TEST_F(JsepSessionDescriptionTest, SerializeDeserializeWithCandidates) {
142
std::string sdp = Serialize(jsep_desc_.get());
144
// Add a candidate and check that the serialized result is different.
145
JsepIceCandidate jsep_candidate("0", candidate_);
146
EXPECT_TRUE(jsep_desc_->AddCandidate(&jsep_candidate));
147
std::string sdp_with_candidate = Serialize(jsep_desc_.get());
148
EXPECT_NE(sdp, sdp_with_candidate);
150
scoped_ptr<SessionDescriptionInterface> parsed_jsep_desc(
151
DeSerialize(sdp_with_candidate));
152
std::string parsed_sdp_with_candidate = Serialize(parsed_jsep_desc.get());
154
EXPECT_EQ(sdp_with_candidate, parsed_sdp_with_candidate);