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