Update To 11.40.268.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(VideoOptions(), 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   VideoEncoderConfig video_encoder_config;
227   EXPECT_TRUE(engine_->SetDefaultVideoEncoderConfig(video_encoder_config));
228   EXPECT_TRUE(engine_->SetSoundDevices(NULL, NULL));
229   EXPECT_TRUE(engine_->SetVideoCaptureDevice(NULL));
230   EXPECT_TRUE(engine_->SetOutputVolume(0));
231   EXPECT_EQ(0, engine_->GetInputLevel());
232   EXPECT_TRUE(engine_->SetLocalMonitor(true));
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.
245 TEST_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(VideoOptions(), NULL) == NULL);
251 }
252
253 TEST_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.
277 TEST_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.
309 TEST_F(FileMediaEngineTest, TestVoiceChannelSetSend) {
310   EXPECT_TRUE(CreateEngineAndChannels(voice_input_filename_,
311                                       voice_output_filename_, "", "", 1));
312   EXPECT_TRUE(NULL != voice_channel_.get());
313   rtc::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   rtc::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   rtc::Thread::Current()->ProcessMessages(kWaitTimeMs);
329   size_t old_number = net_interface.num_sent_packets();
330   rtc::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   rtc::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.
347 TEST_F(FileMediaEngineTest, TestVoiceChannelSenderThread) {
348   EXPECT_TRUE(CreateEngineAndChannels(voice_input_filename_,
349                                       voice_output_filename_, "", "", 1));
350   EXPECT_TRUE(NULL != voice_channel_.get());
351   rtc::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   rtc::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   rtc::scoped_ptr<rtc::StreamInterface> voice_output_;
373   voice_output_.reset(rtc::Filesystem::OpenFile(
374       rtc::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.
382 TEST_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   rtc::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   rtc::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   rtc::scoped_ptr<rtc::StreamInterface> voice_output_;
410   voice_output_.reset(rtc::Filesystem::OpenFile(
411       rtc::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.
419 TEST_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   rtc::scoped_ptr<rtc::StreamInterface> input_stream(
426       rtc::Filesystem::OpenFile(
427           rtc::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   rtc::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   rtc::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.
459 TEST_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