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