blob: e4d72bbe610ce00726571b942a54e79cb92aa542 [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());
223 EXPECT_TRUE(engine_->SetAudioOptions(0));
224 EXPECT_TRUE(engine_->SetVideoOptions(0));
225 VideoEncoderConfig video_encoder_config;
226 EXPECT_TRUE(engine_->SetDefaultVideoEncoderConfig(video_encoder_config));
227 EXPECT_TRUE(engine_->SetSoundDevices(NULL, NULL));
228 EXPECT_TRUE(engine_->SetVideoCaptureDevice(NULL));
229 EXPECT_TRUE(engine_->SetOutputVolume(0));
230 EXPECT_EQ(0, engine_->GetInputLevel());
231 EXPECT_TRUE(engine_->SetLocalMonitor(true));
232 EXPECT_TRUE(engine_->SetLocalRenderer(NULL));
233 EXPECT_TRUE(engine_->SetVideoCapture(true));
234 EXPECT_EQ(0U, engine_->audio_codecs().size());
235 EXPECT_EQ(0U, engine_->video_codecs().size());
236 AudioCodec voice_codec;
237 EXPECT_TRUE(engine_->FindAudioCodec(voice_codec));
238 VideoCodec video_codec;
239 EXPECT_TRUE(engine_->FindVideoCodec(video_codec));
240 engine_->Terminate();
241}
242
243// Test that when file path is not pointing to a valid stream file, the channel
244// creation function should fail and return NULL.
245TEST_F(FileMediaEngineTest, TestBadFilePath) {
246 engine_.reset(new FileMediaEngine);
247 engine_->set_voice_input_filename(kFakeFileName);
248 engine_->set_video_input_filename(kFakeFileName);
249 EXPECT_TRUE(engine_->CreateChannel() == NULL);
250 EXPECT_TRUE(engine_->CreateVideoChannel(NULL) == NULL);
251}
252
253TEST_F(FileMediaEngineTest, TestCodecs) {
254 EXPECT_TRUE(CreateEngineAndChannels("", "", "", "", 1));
255 std::vector<AudioCodec> voice_codecs = engine_->audio_codecs();
256 std::vector<VideoCodec> video_codecs = engine_->video_codecs();
257 EXPECT_EQ(0U, voice_codecs.size());
258 EXPECT_EQ(0U, video_codecs.size());
259
260 AudioCodec voice_codec(103, "ISAC", 16000, 0, 1, 0);
261 voice_codecs.push_back(voice_codec);
262 engine_->set_voice_codecs(voice_codecs);
263 voice_codecs = engine_->audio_codecs();
264 ASSERT_EQ(1U, voice_codecs.size());
265 EXPECT_EQ(voice_codec, voice_codecs[0]);
266
267 VideoCodec video_codec(96, "H264-SVC", 320, 240, 30, 0);
268 video_codecs.push_back(video_codec);
269 engine_->set_video_codecs(video_codecs);
270 video_codecs = engine_->video_codecs();
271 ASSERT_EQ(1U, video_codecs.size());
272 EXPECT_EQ(video_codec, video_codecs[0]);
273}
274
275// Test that the capabilities and channel creation of the Filemedia engine
276// depend on the stream parameters passed to its constructor.
277TEST_F(FileMediaEngineTest, TestGetCapabilities) {
278 EXPECT_TRUE(CreateEngineAndChannels(voice_input_filename_, "", "", "", 1));
279 EXPECT_EQ(AUDIO_SEND, engine_->GetCapabilities());
280 EXPECT_TRUE(NULL != voice_channel_.get());
281 EXPECT_TRUE(NULL == video_channel_.get());
282
283 EXPECT_TRUE(CreateEngineAndChannels(voice_input_filename_,
284 voice_output_filename_, "", "", 1));
285 EXPECT_EQ(AUDIO_SEND | AUDIO_RECV, engine_->GetCapabilities());
286 EXPECT_TRUE(NULL != voice_channel_.get());
287 EXPECT_TRUE(NULL == video_channel_.get());
288
289 EXPECT_TRUE(CreateEngineAndChannels("", "", video_input_filename_, "", 1));
290 EXPECT_EQ(VIDEO_SEND, engine_->GetCapabilities());
291 EXPECT_TRUE(NULL == voice_channel_.get());
292 EXPECT_TRUE(NULL != video_channel_.get());
293
294 EXPECT_TRUE(CreateEngineAndChannels(voice_input_filename_,
295 voice_output_filename_,
296 video_input_filename_,
297 video_output_filename_,
298 1));
299 EXPECT_EQ(AUDIO_SEND | AUDIO_RECV | VIDEO_SEND | VIDEO_RECV,
300 engine_->GetCapabilities());
301 EXPECT_TRUE(NULL != voice_channel_.get());
302 EXPECT_TRUE(NULL != video_channel_.get());
303}
304
305// FileVideoChannel is the same as FileVoiceChannel in terms of receiving and
306// sending the RTP packets. We therefore test only FileVoiceChannel.
307
308// Test that SetSend() controls whether a voice channel sends RTP packets.
309TEST_F(FileMediaEngineTest, TestVoiceChannelSetSend) {
310 EXPECT_TRUE(CreateEngineAndChannels(voice_input_filename_,
311 voice_output_filename_, "", "", 1));
312 EXPECT_TRUE(NULL != voice_channel_.get());
313 talk_base::MemoryStream net_dump;
314 FileNetworkInterface net_interface(&net_dump, voice_channel_.get());
315 voice_channel_->SetInterface(&net_interface);
316
317 // The channel is not sending yet.
318 talk_base::Thread::Current()->ProcessMessages(kWaitTimeMs);
319 EXPECT_EQ(0U, net_interface.num_sent_packets());
320
321 // The channel starts sending.
322 voice_channel_->SetSend(SEND_MICROPHONE);
323 EXPECT_TRUE_WAIT(net_interface.num_sent_packets() >= 1U, kWaitTimeout);
324
325 // The channel stops sending.
326 voice_channel_->SetSend(SEND_NOTHING);
327 // Wait until packets are all delivered.
328 talk_base::Thread::Current()->ProcessMessages(kWaitTimeMs);
329 size_t old_number = net_interface.num_sent_packets();
330 talk_base::Thread::Current()->ProcessMessages(kWaitTimeMs);
331 EXPECT_EQ(old_number, net_interface.num_sent_packets());
332
333 // The channel starts sending again.
334 voice_channel_->SetSend(SEND_MICROPHONE);
335 EXPECT_TRUE_WAIT(net_interface.num_sent_packets() > old_number, kWaitTimeout);
336
337 // When the function exits, the net_interface object is released. The sender
338 // thread may call net_interface to send packets, which results in a segment
339 // fault. We hence stop sending and wait until all packets are delivered
340 // before we exit this function.
341 voice_channel_->SetSend(SEND_NOTHING);
342 talk_base::Thread::Current()->ProcessMessages(kWaitTimeMs);
343}
344
345// Test the sender thread of the channel. The sender sends RTP packets
346// continuously with proper sequence number, timestamp, and payload.
347TEST_F(FileMediaEngineTest, TestVoiceChannelSenderThread) {
348 EXPECT_TRUE(CreateEngineAndChannels(voice_input_filename_,
349 voice_output_filename_, "", "", 1));
350 EXPECT_TRUE(NULL != voice_channel_.get());
351 talk_base::MemoryStream net_dump;
352 FileNetworkInterface net_interface(&net_dump, voice_channel_.get());
353 voice_channel_->SetInterface(&net_interface);
354
355 voice_channel_->SetSend(SEND_MICROPHONE);
356 // Wait until the number of sent packets is no less than 2 * kPacketNumber.
357 EXPECT_TRUE_WAIT(
358 net_interface.num_sent_packets() >=
359 2 * RtpTestUtility::GetTestPacketCount(),
360 kWaitTimeout);
361 voice_channel_->SetSend(SEND_NOTHING);
362 // Wait until packets are all delivered.
363 talk_base::Thread::Current()->ProcessMessages(kWaitTimeMs);
364 EXPECT_TRUE(RtpTestUtility::VerifyTestPacketsFromStream(
365 2 * RtpTestUtility::GetTestPacketCount(), &net_dump,
366 RtpTestUtility::kDefaultSsrc));
367
368 // Each sent packet is dumped to net_dump and is also feed to the channel
369 // via OnPacketReceived, which in turn writes the packets into voice_output_.
370 // We next verify the packets in voice_output_.
371 voice_channel_.reset(); // Force to close the files.
372 talk_base::scoped_ptr<talk_base::StreamInterface> voice_output_;
373 voice_output_.reset(talk_base::Filesystem::OpenFile(
374 talk_base::Pathname(voice_output_filename_), "rb"));
375 EXPECT_TRUE(voice_output_.get() != NULL);
376 EXPECT_TRUE(RtpTestUtility::VerifyTestPacketsFromStream(
377 2 * RtpTestUtility::GetTestPacketCount(), voice_output_.get(),
378 RtpTestUtility::kDefaultSsrc));
379}
380
381// Test that we can specify the ssrc for outgoing RTP packets.
382TEST_F(FileMediaEngineTest, TestVoiceChannelSendSsrc) {
383 EXPECT_TRUE(CreateEngineAndChannels(voice_input_filename_,
384 voice_output_filename_, "", "", 1));
385 EXPECT_TRUE(NULL != voice_channel_.get());
386 const uint32 send_ssrc = RtpTestUtility::kDefaultSsrc + 1;
387 voice_channel_->AddSendStream(StreamParams::CreateLegacy(send_ssrc));
388
389 talk_base::MemoryStream net_dump;
390 FileNetworkInterface net_interface(&net_dump, voice_channel_.get());
391 voice_channel_->SetInterface(&net_interface);
392
393 voice_channel_->SetSend(SEND_MICROPHONE);
394 // Wait until the number of sent packets is no less than 2 * kPacketNumber.
395 EXPECT_TRUE_WAIT(
396 net_interface.num_sent_packets() >=
397 2 * RtpTestUtility::GetTestPacketCount(),
398 kWaitTimeout);
399 voice_channel_->SetSend(SEND_NOTHING);
400 // Wait until packets are all delivered.
401 talk_base::Thread::Current()->ProcessMessages(kWaitTimeMs);
402 EXPECT_TRUE(RtpTestUtility::VerifyTestPacketsFromStream(
403 2 * RtpTestUtility::GetTestPacketCount(), &net_dump, send_ssrc));
404
405 // Each sent packet is dumped to net_dump and is also feed to the channel
406 // via OnPacketReceived, which in turn writes the packets into voice_output_.
407 // We next verify the packets in voice_output_.
408 voice_channel_.reset(); // Force to close the files.
409 talk_base::scoped_ptr<talk_base::StreamInterface> voice_output_;
410 voice_output_.reset(talk_base::Filesystem::OpenFile(
411 talk_base::Pathname(voice_output_filename_), "rb"));
412 EXPECT_TRUE(voice_output_.get() != NULL);
413 EXPECT_TRUE(RtpTestUtility::VerifyTestPacketsFromStream(
414 2 * RtpTestUtility::GetTestPacketCount(), voice_output_.get(),
415 send_ssrc));
416}
417
418// Test the sender thread of the channel, where the input rtpdump has two SSRCs.
419TEST_F(FileMediaEngineTest, TestVoiceChannelSenderThreadTwoSsrcs) {
420 EXPECT_TRUE(CreateEngineAndChannels(voice_input_filename_,
421 voice_output_filename_, "", "", 2));
422 // Verify that voice_input_filename_ contains 2 *
423 // RtpTestUtility::GetTestPacketCount() packets
424 // with different SSRCs.
425 talk_base::scoped_ptr<talk_base::StreamInterface> input_stream(
426 talk_base::Filesystem::OpenFile(
427 talk_base::Pathname(voice_input_filename_), "rb"));
428 ASSERT_TRUE(NULL != input_stream.get());
429 size_t ssrc_count;
430 size_t packet_count;
431 EXPECT_TRUE(GetSsrcAndPacketCounts(input_stream.get(), &ssrc_count,
432 &packet_count));
433 EXPECT_EQ(2U, ssrc_count);
434 EXPECT_EQ(2 * RtpTestUtility::GetTestPacketCount(), packet_count);
435 input_stream.reset();
436
437 // Send 2 * RtpTestUtility::GetTestPacketCount() packets and verify that all
438 // these packets have the same SSRCs (that is, the packets with different
439 // SSRCs are skipped by the filemediaengine).
440 EXPECT_TRUE(NULL != voice_channel_.get());
441 talk_base::MemoryStream net_dump;
442 FileNetworkInterface net_interface(&net_dump, voice_channel_.get());
443 voice_channel_->SetInterface(&net_interface);
444 voice_channel_->SetSend(SEND_MICROPHONE);
445 EXPECT_TRUE_WAIT(
446 net_interface.num_sent_packets() >=
447 2 * RtpTestUtility::GetTestPacketCount(),
448 kWaitTimeout);
449 voice_channel_->SetSend(SEND_NOTHING);
450 // Wait until packets are all delivered.
451 talk_base::Thread::Current()->ProcessMessages(kWaitTimeMs);
452 net_dump.Rewind();
453 EXPECT_TRUE(GetSsrcAndPacketCounts(&net_dump, &ssrc_count, &packet_count));
454 EXPECT_EQ(1U, ssrc_count);
455 EXPECT_GE(packet_count, 2 * RtpTestUtility::GetTestPacketCount());
456}
457
458// Test SendIntraFrame() and RequestIntraFrame() of video channel.
459TEST_F(FileMediaEngineTest, TestVideoChannelIntraFrame) {
460 EXPECT_TRUE(CreateEngineAndChannels("", "", video_input_filename_,
461 video_output_filename_, 1));
462 EXPECT_TRUE(NULL != video_channel_.get());
463 EXPECT_FALSE(video_channel_->SendIntraFrame());
464 EXPECT_FALSE(video_channel_->RequestIntraFrame());
465}
466
467} // namespace cricket