blob: a72e0dc6a784d919e10e73cc8ab1613cc126ae0d [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
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.
15 *
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.
26 */
27
28#include <set>
29
30#include "talk/base/buffer.h"
31#include "talk/base/gunit.h"
32#include "talk/base/helpers.h"
33#include "talk/base/pathutils.h"
34#include "talk/base/stream.h"
35#include "talk/media/base/filemediaengine.h"
36#include "talk/media/base/rtpdump.h"
37#include "talk/media/base/streamparams.h"
38#include "talk/media/base/testutils.h"
39
40namespace cricket {
41
42static const int kWaitTimeMs = 100;
43static const std::string kFakeFileName = "foobar";
44
45//////////////////////////////////////////////////////////////////////////////
46// Media channel sends RTP packets via NetworkInterface. Rather than sending
47// packets to the network, FileNetworkInterface writes packets to a stream and
48// feeds packets back to the channel via OnPacketReceived.
49//////////////////////////////////////////////////////////////////////////////
50class FileNetworkInterface : public MediaChannel::NetworkInterface {
51 public:
52 FileNetworkInterface(talk_base::StreamInterface* output, MediaChannel* ch)
53 : media_channel_(ch),
54 num_sent_packets_(0) {
55 if (output) {
56 dump_writer_.reset(new RtpDumpWriter(output));
57 }
58 }
59
60 // Implement pure virtual methods of NetworkInterface.
mallinath@webrtc.org1112c302013-09-23 20:34:45 +000061 virtual bool SendPacket(talk_base::Buffer* packet,
62 talk_base::DiffServCodePoint dscp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063 if (!packet) return false;
64
65 if (media_channel_) {
66 media_channel_->OnPacketReceived(packet);
67 }
68 if (dump_writer_.get() &&
69 talk_base::SR_SUCCESS != dump_writer_->WriteRtpPacket(
70 packet->data(), packet->length())) {
71 return false;
72 }
73
74 ++num_sent_packets_;
75 return true;
76 }
77
mallinath@webrtc.org1112c302013-09-23 20:34:45 +000078 virtual bool SendRtcp(talk_base::Buffer* packet,
79 talk_base::DiffServCodePoint dscp) { return false; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080 virtual int SetOption(MediaChannel::NetworkInterface::SocketType type,
81 talk_base::Socket::Option opt, int option) {
82 return 0;
83 }
mallinath@webrtc.org1112c302013-09-23 20:34:45 +000084 virtual void SetDefaultDSCPCode(talk_base::DiffServCodePoint dscp) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085
86 size_t num_sent_packets() const { return num_sent_packets_; }
87
88 private:
89 MediaChannel* media_channel_;
90 talk_base::scoped_ptr<RtpDumpWriter> dump_writer_;
91 size_t num_sent_packets_;
92
93 DISALLOW_COPY_AND_ASSIGN(FileNetworkInterface);
94};
95
96class FileMediaEngineTest : public testing::Test {
97 public:
98 virtual void SetUp() {
99 setup_ok_ = true;
100 setup_ok_ &= GetTempFilename(&voice_input_filename_);
101 setup_ok_ &= GetTempFilename(&voice_output_filename_);
102 setup_ok_ &= GetTempFilename(&video_input_filename_);
103 setup_ok_ &= GetTempFilename(&video_output_filename_);
104 }
105 virtual void TearDown() {
106 // Force to close the dump files, if opened.
107 voice_channel_.reset();
108 video_channel_.reset();
109
110 DeleteTempFile(voice_input_filename_);
111 DeleteTempFile(voice_output_filename_);
112 DeleteTempFile(video_input_filename_);
113 DeleteTempFile(video_output_filename_);
114 }
115
116 protected:
117 bool CreateEngineAndChannels(const std::string& voice_in,
118 const std::string& voice_out,
119 const std::string& video_in,
120 const std::string& video_out,
121 size_t ssrc_count) {
122 // Force to close the dump files, if opened.
123 voice_channel_.reset();
124 video_channel_.reset();
125
126 bool ret = setup_ok_;
127 if (!voice_in.empty()) {
128 ret &= WriteTestPacketsToFile(voice_in, ssrc_count);
129 }
130 if (!video_in.empty()) {
131 ret &= WriteTestPacketsToFile(video_in, ssrc_count);
132 }
133
134 engine_.reset(new FileMediaEngine);
135 engine_->set_voice_input_filename(voice_in);
136 engine_->set_voice_output_filename(voice_out);
137 engine_->set_video_input_filename(video_in);
138 engine_->set_video_output_filename(video_out);
139
140 voice_channel_.reset(engine_->CreateChannel());
141 video_channel_.reset(engine_->CreateVideoChannel(NULL));
142
143 return ret;
144 }
145
146 bool GetTempFilename(std::string* filename) {
147 talk_base::Pathname temp_path;
148 if (!talk_base::Filesystem::GetTemporaryFolder(temp_path, true, NULL)) {
149 return false;
150 }
151 temp_path.SetPathname(
152 talk_base::Filesystem::TempFilename(temp_path, "fme-test-"));
153
154 if (filename) {
155 *filename = temp_path.pathname();
156 }
157 return true;
158 }
159
160 bool WriteTestPacketsToFile(const std::string& filename, size_t ssrc_count) {
161 talk_base::scoped_ptr<talk_base::StreamInterface> stream(
162 talk_base::Filesystem::OpenFile(talk_base::Pathname(filename), "wb"));
163 bool ret = (NULL != stream.get());
164 RtpDumpWriter writer(stream.get());
165
166 for (size_t i = 0; i < ssrc_count; ++i) {
167 ret &= RtpTestUtility::WriteTestPackets(
168 RtpTestUtility::GetTestPacketCount(), false,
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000169 static_cast<uint32>(RtpTestUtility::kDefaultSsrc + i),
170 &writer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171 }
172 return ret;
173 }
174
175 void DeleteTempFile(std::string filename) {
176 talk_base::Pathname pathname(filename);
177 if (talk_base::Filesystem::IsFile(talk_base::Pathname(pathname))) {
178 talk_base::Filesystem::DeleteFile(pathname);
179 }
180 }
181
182 bool GetSsrcAndPacketCounts(talk_base::StreamInterface* stream,
183 size_t* ssrc_count, size_t* packet_count) {
184 talk_base::scoped_ptr<RtpDumpReader> reader(new RtpDumpReader(stream));
185 size_t count = 0;
186 RtpDumpPacket packet;
187 std::set<uint32> ssrcs;
188 while (talk_base::SR_SUCCESS == reader->ReadPacket(&packet)) {
189 count++;
190 uint32 ssrc;
191 if (!packet.GetRtpSsrc(&ssrc)) {
192 return false;
193 }
194 ssrcs.insert(ssrc);
195 }
196 if (ssrc_count) {
197 *ssrc_count = ssrcs.size();
198 }
199 if (packet_count) {
200 *packet_count = count;
201 }
202 return true;
203 }
204
205 static const uint32 kWaitTimeout = 3000;
206 bool setup_ok_;
207 std::string voice_input_filename_;
208 std::string voice_output_filename_;
209 std::string video_input_filename_;
210 std::string video_output_filename_;
211 talk_base::scoped_ptr<FileMediaEngine> engine_;
212 talk_base::scoped_ptr<VoiceMediaChannel> voice_channel_;
213 talk_base::scoped_ptr<VideoMediaChannel> video_channel_;
214};
215
216TEST_F(FileMediaEngineTest, TestDefaultImplementation) {
217 EXPECT_TRUE(CreateEngineAndChannels("", "", "", "", 1));
218 EXPECT_TRUE(engine_->Init(talk_base::Thread::Current()));
219 EXPECT_EQ(0, engine_->GetCapabilities());
220 EXPECT_TRUE(NULL == voice_channel_.get());
221 EXPECT_TRUE(NULL == video_channel_.get());
222 EXPECT_TRUE(NULL == engine_->CreateSoundclip());
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000223 cricket::AudioOptions audio_options;
224 EXPECT_TRUE(engine_->SetAudioOptions(audio_options));
225 cricket::VideoOptions video_options;
226 EXPECT_TRUE(engine_->SetVideoOptions(video_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000227 VideoEncoderConfig video_encoder_config;
228 EXPECT_TRUE(engine_->SetDefaultVideoEncoderConfig(video_encoder_config));
229 EXPECT_TRUE(engine_->SetSoundDevices(NULL, NULL));
230 EXPECT_TRUE(engine_->SetVideoCaptureDevice(NULL));
231 EXPECT_TRUE(engine_->SetOutputVolume(0));
232 EXPECT_EQ(0, engine_->GetInputLevel());
233 EXPECT_TRUE(engine_->SetLocalMonitor(true));
234 EXPECT_TRUE(engine_->SetLocalRenderer(NULL));
235 EXPECT_TRUE(engine_->SetVideoCapture(true));
236 EXPECT_EQ(0U, engine_->audio_codecs().size());
237 EXPECT_EQ(0U, engine_->video_codecs().size());
238 AudioCodec voice_codec;
239 EXPECT_TRUE(engine_->FindAudioCodec(voice_codec));
240 VideoCodec video_codec;
241 EXPECT_TRUE(engine_->FindVideoCodec(video_codec));
242 engine_->Terminate();
243}
244
245// Test that when file path is not pointing to a valid stream file, the channel
246// creation function should fail and return NULL.
247TEST_F(FileMediaEngineTest, TestBadFilePath) {
248 engine_.reset(new FileMediaEngine);
249 engine_->set_voice_input_filename(kFakeFileName);
250 engine_->set_video_input_filename(kFakeFileName);
251 EXPECT_TRUE(engine_->CreateChannel() == NULL);
252 EXPECT_TRUE(engine_->CreateVideoChannel(NULL) == NULL);
253}
254
255TEST_F(FileMediaEngineTest, TestCodecs) {
256 EXPECT_TRUE(CreateEngineAndChannels("", "", "", "", 1));
257 std::vector<AudioCodec> voice_codecs = engine_->audio_codecs();
258 std::vector<VideoCodec> video_codecs = engine_->video_codecs();
259 EXPECT_EQ(0U, voice_codecs.size());
260 EXPECT_EQ(0U, video_codecs.size());
261
262 AudioCodec voice_codec(103, "ISAC", 16000, 0, 1, 0);
263 voice_codecs.push_back(voice_codec);
264 engine_->set_voice_codecs(voice_codecs);
265 voice_codecs = engine_->audio_codecs();
266 ASSERT_EQ(1U, voice_codecs.size());
267 EXPECT_EQ(voice_codec, voice_codecs[0]);
268
269 VideoCodec video_codec(96, "H264-SVC", 320, 240, 30, 0);
270 video_codecs.push_back(video_codec);
271 engine_->set_video_codecs(video_codecs);
272 video_codecs = engine_->video_codecs();
273 ASSERT_EQ(1U, video_codecs.size());
274 EXPECT_EQ(video_codec, video_codecs[0]);
275}
276
277// Test that the capabilities and channel creation of the Filemedia engine
278// depend on the stream parameters passed to its constructor.
279TEST_F(FileMediaEngineTest, TestGetCapabilities) {
280 EXPECT_TRUE(CreateEngineAndChannels(voice_input_filename_, "", "", "", 1));
281 EXPECT_EQ(AUDIO_SEND, engine_->GetCapabilities());
282 EXPECT_TRUE(NULL != voice_channel_.get());
283 EXPECT_TRUE(NULL == video_channel_.get());
284
285 EXPECT_TRUE(CreateEngineAndChannels(voice_input_filename_,
286 voice_output_filename_, "", "", 1));
287 EXPECT_EQ(AUDIO_SEND | AUDIO_RECV, engine_->GetCapabilities());
288 EXPECT_TRUE(NULL != voice_channel_.get());
289 EXPECT_TRUE(NULL == video_channel_.get());
290
291 EXPECT_TRUE(CreateEngineAndChannels("", "", video_input_filename_, "", 1));
292 EXPECT_EQ(VIDEO_SEND, engine_->GetCapabilities());
293 EXPECT_TRUE(NULL == voice_channel_.get());
294 EXPECT_TRUE(NULL != video_channel_.get());
295
296 EXPECT_TRUE(CreateEngineAndChannels(voice_input_filename_,
297 voice_output_filename_,
298 video_input_filename_,
299 video_output_filename_,
300 1));
301 EXPECT_EQ(AUDIO_SEND | AUDIO_RECV | VIDEO_SEND | VIDEO_RECV,
302 engine_->GetCapabilities());
303 EXPECT_TRUE(NULL != voice_channel_.get());
304 EXPECT_TRUE(NULL != video_channel_.get());
305}
306
307// FileVideoChannel is the same as FileVoiceChannel in terms of receiving and
308// sending the RTP packets. We therefore test only FileVoiceChannel.
309
310// Test that SetSend() controls whether a voice channel sends RTP packets.
311TEST_F(FileMediaEngineTest, TestVoiceChannelSetSend) {
312 EXPECT_TRUE(CreateEngineAndChannels(voice_input_filename_,
313 voice_output_filename_, "", "", 1));
314 EXPECT_TRUE(NULL != voice_channel_.get());
315 talk_base::MemoryStream net_dump;
316 FileNetworkInterface net_interface(&net_dump, voice_channel_.get());
317 voice_channel_->SetInterface(&net_interface);
318
319 // The channel is not sending yet.
320 talk_base::Thread::Current()->ProcessMessages(kWaitTimeMs);
321 EXPECT_EQ(0U, net_interface.num_sent_packets());
322
323 // The channel starts sending.
324 voice_channel_->SetSend(SEND_MICROPHONE);
325 EXPECT_TRUE_WAIT(net_interface.num_sent_packets() >= 1U, kWaitTimeout);
326
327 // The channel stops sending.
328 voice_channel_->SetSend(SEND_NOTHING);
329 // Wait until packets are all delivered.
330 talk_base::Thread::Current()->ProcessMessages(kWaitTimeMs);
331 size_t old_number = net_interface.num_sent_packets();
332 talk_base::Thread::Current()->ProcessMessages(kWaitTimeMs);
333 EXPECT_EQ(old_number, net_interface.num_sent_packets());
334
335 // The channel starts sending again.
336 voice_channel_->SetSend(SEND_MICROPHONE);
337 EXPECT_TRUE_WAIT(net_interface.num_sent_packets() > old_number, kWaitTimeout);
338
339 // When the function exits, the net_interface object is released. The sender
340 // thread may call net_interface to send packets, which results in a segment
341 // fault. We hence stop sending and wait until all packets are delivered
342 // before we exit this function.
343 voice_channel_->SetSend(SEND_NOTHING);
344 talk_base::Thread::Current()->ProcessMessages(kWaitTimeMs);
345}
346
347// Test the sender thread of the channel. The sender sends RTP packets
348// continuously with proper sequence number, timestamp, and payload.
349TEST_F(FileMediaEngineTest, TestVoiceChannelSenderThread) {
350 EXPECT_TRUE(CreateEngineAndChannels(voice_input_filename_,
351 voice_output_filename_, "", "", 1));
352 EXPECT_TRUE(NULL != voice_channel_.get());
353 talk_base::MemoryStream net_dump;
354 FileNetworkInterface net_interface(&net_dump, voice_channel_.get());
355 voice_channel_->SetInterface(&net_interface);
356
357 voice_channel_->SetSend(SEND_MICROPHONE);
358 // Wait until the number of sent packets is no less than 2 * kPacketNumber.
359 EXPECT_TRUE_WAIT(
360 net_interface.num_sent_packets() >=
361 2 * RtpTestUtility::GetTestPacketCount(),
362 kWaitTimeout);
363 voice_channel_->SetSend(SEND_NOTHING);
364 // Wait until packets are all delivered.
365 talk_base::Thread::Current()->ProcessMessages(kWaitTimeMs);
366 EXPECT_TRUE(RtpTestUtility::VerifyTestPacketsFromStream(
367 2 * RtpTestUtility::GetTestPacketCount(), &net_dump,
368 RtpTestUtility::kDefaultSsrc));
369
370 // Each sent packet is dumped to net_dump and is also feed to the channel
371 // via OnPacketReceived, which in turn writes the packets into voice_output_.
372 // We next verify the packets in voice_output_.
373 voice_channel_.reset(); // Force to close the files.
374 talk_base::scoped_ptr<talk_base::StreamInterface> voice_output_;
375 voice_output_.reset(talk_base::Filesystem::OpenFile(
376 talk_base::Pathname(voice_output_filename_), "rb"));
377 EXPECT_TRUE(voice_output_.get() != NULL);
378 EXPECT_TRUE(RtpTestUtility::VerifyTestPacketsFromStream(
379 2 * RtpTestUtility::GetTestPacketCount(), voice_output_.get(),
380 RtpTestUtility::kDefaultSsrc));
381}
382
383// Test that we can specify the ssrc for outgoing RTP packets.
384TEST_F(FileMediaEngineTest, TestVoiceChannelSendSsrc) {
385 EXPECT_TRUE(CreateEngineAndChannels(voice_input_filename_,
386 voice_output_filename_, "", "", 1));
387 EXPECT_TRUE(NULL != voice_channel_.get());
388 const uint32 send_ssrc = RtpTestUtility::kDefaultSsrc + 1;
389 voice_channel_->AddSendStream(StreamParams::CreateLegacy(send_ssrc));
390
391 talk_base::MemoryStream net_dump;
392 FileNetworkInterface net_interface(&net_dump, voice_channel_.get());
393 voice_channel_->SetInterface(&net_interface);
394
395 voice_channel_->SetSend(SEND_MICROPHONE);
396 // Wait until the number of sent packets is no less than 2 * kPacketNumber.
397 EXPECT_TRUE_WAIT(
398 net_interface.num_sent_packets() >=
399 2 * RtpTestUtility::GetTestPacketCount(),
400 kWaitTimeout);
401 voice_channel_->SetSend(SEND_NOTHING);
402 // Wait until packets are all delivered.
403 talk_base::Thread::Current()->ProcessMessages(kWaitTimeMs);
404 EXPECT_TRUE(RtpTestUtility::VerifyTestPacketsFromStream(
405 2 * RtpTestUtility::GetTestPacketCount(), &net_dump, send_ssrc));
406
407 // Each sent packet is dumped to net_dump and is also feed to the channel
408 // via OnPacketReceived, which in turn writes the packets into voice_output_.
409 // We next verify the packets in voice_output_.
410 voice_channel_.reset(); // Force to close the files.
411 talk_base::scoped_ptr<talk_base::StreamInterface> voice_output_;
412 voice_output_.reset(talk_base::Filesystem::OpenFile(
413 talk_base::Pathname(voice_output_filename_), "rb"));
414 EXPECT_TRUE(voice_output_.get() != NULL);
415 EXPECT_TRUE(RtpTestUtility::VerifyTestPacketsFromStream(
416 2 * RtpTestUtility::GetTestPacketCount(), voice_output_.get(),
417 send_ssrc));
418}
419
420// Test the sender thread of the channel, where the input rtpdump has two SSRCs.
421TEST_F(FileMediaEngineTest, TestVoiceChannelSenderThreadTwoSsrcs) {
422 EXPECT_TRUE(CreateEngineAndChannels(voice_input_filename_,
423 voice_output_filename_, "", "", 2));
424 // Verify that voice_input_filename_ contains 2 *
425 // RtpTestUtility::GetTestPacketCount() packets
426 // with different SSRCs.
427 talk_base::scoped_ptr<talk_base::StreamInterface> input_stream(
428 talk_base::Filesystem::OpenFile(
429 talk_base::Pathname(voice_input_filename_), "rb"));
430 ASSERT_TRUE(NULL != input_stream.get());
431 size_t ssrc_count;
432 size_t packet_count;
433 EXPECT_TRUE(GetSsrcAndPacketCounts(input_stream.get(), &ssrc_count,
434 &packet_count));
435 EXPECT_EQ(2U, ssrc_count);
436 EXPECT_EQ(2 * RtpTestUtility::GetTestPacketCount(), packet_count);
437 input_stream.reset();
438
439 // Send 2 * RtpTestUtility::GetTestPacketCount() packets and verify that all
440 // these packets have the same SSRCs (that is, the packets with different
441 // SSRCs are skipped by the filemediaengine).
442 EXPECT_TRUE(NULL != voice_channel_.get());
443 talk_base::MemoryStream net_dump;
444 FileNetworkInterface net_interface(&net_dump, voice_channel_.get());
445 voice_channel_->SetInterface(&net_interface);
446 voice_channel_->SetSend(SEND_MICROPHONE);
447 EXPECT_TRUE_WAIT(
448 net_interface.num_sent_packets() >=
449 2 * RtpTestUtility::GetTestPacketCount(),
450 kWaitTimeout);
451 voice_channel_->SetSend(SEND_NOTHING);
452 // Wait until packets are all delivered.
453 talk_base::Thread::Current()->ProcessMessages(kWaitTimeMs);
454 net_dump.Rewind();
455 EXPECT_TRUE(GetSsrcAndPacketCounts(&net_dump, &ssrc_count, &packet_count));
456 EXPECT_EQ(1U, ssrc_count);
457 EXPECT_GE(packet_count, 2 * RtpTestUtility::GetTestPacketCount());
458}
459
460// Test SendIntraFrame() and RequestIntraFrame() of video channel.
461TEST_F(FileMediaEngineTest, TestVideoChannelIntraFrame) {
462 EXPECT_TRUE(CreateEngineAndChannels("", "", video_input_filename_,
463 video_output_filename_, 1));
464 EXPECT_TRUE(NULL != video_channel_.get());
465 EXPECT_FALSE(video_channel_->SendIntraFrame());
466 EXPECT_FALSE(video_channel_->RequestIntraFrame());
467}
468
469} // namespace cricket