Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(379)

Side by Side Diff: talk/media/base/filemediaengine_unittest.cc

Issue 1169833004: Remove FileMediaEngine. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « talk/media/base/filemediaengine.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 !=
70 dump_writer_->WriteRtpPacket(packet->data(), packet->size())) {
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 ASSERT_TRUE(GetTempFilename(&voice_input_filename_));
100 ASSERT_TRUE(GetTempFilename(&voice_output_filename_));
101 ASSERT_TRUE(GetTempFilename(&video_input_filename_));
102 ASSERT_TRUE(GetTempFilename(&video_output_filename_));
103 }
104 virtual void TearDown() {
105 // Force to close the dump files, if opened.
106 voice_channel_.reset();
107 video_channel_.reset();
108
109 DeleteTempFile(voice_input_filename_);
110 DeleteTempFile(voice_output_filename_);
111 DeleteTempFile(video_input_filename_);
112 DeleteTempFile(video_output_filename_);
113 }
114
115 protected:
116 void CreateEngineAndChannels(const std::string& voice_in,
117 const std::string& voice_out,
118 const std::string& video_in,
119 const std::string& video_out,
120 size_t ssrc_count) {
121 // Force to close the dump files, if opened.
122 voice_channel_.reset();
123 video_channel_.reset();
124
125 if (!voice_in.empty()) {
126 EXPECT_TRUE(WriteTestPacketsToFile(voice_in, ssrc_count));
127 }
128 if (!video_in.empty()) {
129 EXPECT_TRUE(WriteTestPacketsToFile(video_in, ssrc_count));
130 }
131
132 engine_.reset(new FileMediaEngine);
133 engine_->set_voice_input_filename(voice_in);
134 engine_->set_voice_output_filename(voice_out);
135 engine_->set_video_input_filename(video_in);
136 engine_->set_video_output_filename(video_out);
137 engine_->set_rtp_sender_thread(rtc::Thread::Current());
138
139 voice_channel_.reset(engine_->CreateChannel(AudioOptions()));
140 video_channel_.reset(engine_->CreateVideoChannel(VideoOptions(), nullptr));
141 }
142
143 bool GetTempFilename(std::string* filename) {
144 rtc::Pathname temp_path;
145 if (!rtc::Filesystem::GetTemporaryFolder(temp_path, true, NULL)) {
146 return false;
147 }
148 temp_path.SetPathname(
149 rtc::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 rtc::scoped_ptr<rtc::StreamInterface> stream(
159 rtc::Filesystem::OpenFile(rtc::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,
166 static_cast<uint32>(RtpTestUtility::kDefaultSsrc + i),
167 &writer);
168 }
169 return ret;
170 }
171
172 void DeleteTempFile(std::string filename) {
173 rtc::Pathname pathname(filename);
174 if (rtc::Filesystem::IsFile(rtc::Pathname(pathname))) {
175 rtc::Filesystem::DeleteFile(pathname);
176 }
177 }
178
179 bool GetSsrcAndPacketCounts(rtc::StreamInterface* stream,
180 size_t* ssrc_count, size_t* packet_count) {
181 rtc::scoped_ptr<RtpDumpReader> reader(new RtpDumpReader(stream));
182 size_t count = 0;
183 RtpDumpPacket packet;
184 std::set<uint32> ssrcs;
185 while (rtc::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 std::string voice_input_filename_;
204 std::string voice_output_filename_;
205 std::string video_input_filename_;
206 std::string video_output_filename_;
207 rtc::scoped_ptr<FileMediaEngine> engine_;
208 rtc::scoped_ptr<VoiceMediaChannel> voice_channel_;
209 rtc::scoped_ptr<VideoMediaChannel> video_channel_;
210 };
211
212 TEST_F(FileMediaEngineTest, TestDefaultImplementation) {
213 CreateEngineAndChannels("", "", "", "", 1);
214 EXPECT_TRUE(engine_->Init(rtc::Thread::Current()));
215 EXPECT_EQ(0, engine_->GetCapabilities());
216 EXPECT_TRUE(NULL == voice_channel_.get());
217 EXPECT_TRUE(NULL == video_channel_.get());
218 cricket::AudioOptions audio_options;
219 EXPECT_TRUE(engine_->SetAudioOptions(audio_options));
220 VideoEncoderConfig video_encoder_config;
221 EXPECT_TRUE(engine_->SetDefaultVideoEncoderConfig(video_encoder_config));
222 EXPECT_TRUE(engine_->SetSoundDevices(NULL, NULL));
223 EXPECT_TRUE(engine_->SetVideoCaptureDevice(NULL));
224 EXPECT_TRUE(engine_->SetOutputVolume(0));
225 EXPECT_EQ(0, engine_->GetInputLevel());
226 EXPECT_TRUE(engine_->SetLocalMonitor(true));
227 EXPECT_TRUE(engine_->SetVideoCapture(true));
228 EXPECT_EQ(0U, engine_->audio_codecs().size());
229 EXPECT_EQ(0U, engine_->video_codecs().size());
230 AudioCodec voice_codec;
231 EXPECT_TRUE(engine_->FindAudioCodec(voice_codec));
232 VideoCodec video_codec;
233 EXPECT_TRUE(engine_->FindVideoCodec(video_codec));
234 engine_->Terminate();
235 }
236
237 // Test that when file path is not pointing to a valid stream file, the channel
238 // creation function should fail and return NULL.
239 TEST_F(FileMediaEngineTest, TestBadFilePath) {
240 engine_.reset(new FileMediaEngine);
241 engine_->set_voice_input_filename(kFakeFileName);
242 engine_->set_video_input_filename(kFakeFileName);
243 EXPECT_TRUE(engine_->CreateChannel(AudioOptions()) == nullptr);
244 EXPECT_TRUE(engine_->CreateVideoChannel(VideoOptions(), nullptr) == nullptr);
245 }
246
247 TEST_F(FileMediaEngineTest, TestCodecs) {
248 CreateEngineAndChannels("", "", "", "", 1);
249 std::vector<AudioCodec> voice_codecs = engine_->audio_codecs();
250 std::vector<VideoCodec> video_codecs = engine_->video_codecs();
251 EXPECT_EQ(0U, voice_codecs.size());
252 EXPECT_EQ(0U, video_codecs.size());
253
254 AudioCodec voice_codec(103, "ISAC", 16000, 0, 1, 0);
255 voice_codecs.push_back(voice_codec);
256 engine_->set_voice_codecs(voice_codecs);
257 voice_codecs = engine_->audio_codecs();
258 ASSERT_EQ(1U, voice_codecs.size());
259 EXPECT_EQ(voice_codec, voice_codecs[0]);
260
261 VideoCodec video_codec(96, "H264-SVC", 320, 240, 30, 0);
262 video_codecs.push_back(video_codec);
263 engine_->set_video_codecs(video_codecs);
264 video_codecs = engine_->video_codecs();
265 ASSERT_EQ(1U, video_codecs.size());
266 EXPECT_EQ(video_codec, video_codecs[0]);
267 }
268
269 // Test that the capabilities and channel creation of the Filemedia engine
270 // depend on the stream parameters passed to its constructor.
271 TEST_F(FileMediaEngineTest, TestGetCapabilities) {
272 CreateEngineAndChannels(voice_input_filename_, "", "", "", 1);
273 EXPECT_EQ(AUDIO_SEND, engine_->GetCapabilities());
274 EXPECT_TRUE(NULL != voice_channel_.get());
275 EXPECT_TRUE(NULL == video_channel_.get());
276
277 CreateEngineAndChannels(voice_input_filename_, voice_output_filename_, "", "",
278 1);
279 EXPECT_EQ(AUDIO_SEND | AUDIO_RECV, engine_->GetCapabilities());
280 EXPECT_TRUE(NULL != voice_channel_.get());
281 EXPECT_TRUE(NULL == video_channel_.get());
282
283 CreateEngineAndChannels("", "", video_input_filename_, "", 1);
284 EXPECT_EQ(VIDEO_SEND, engine_->GetCapabilities());
285 EXPECT_TRUE(NULL == voice_channel_.get());
286 EXPECT_TRUE(NULL != video_channel_.get());
287
288 CreateEngineAndChannels(voice_input_filename_, voice_output_filename_,
289 video_input_filename_, video_output_filename_, 1);
290 EXPECT_EQ(AUDIO_SEND | AUDIO_RECV | VIDEO_SEND | VIDEO_RECV,
291 engine_->GetCapabilities());
292 EXPECT_TRUE(NULL != voice_channel_.get());
293 EXPECT_TRUE(NULL != video_channel_.get());
294 }
295
296 // FileVideoChannel is the same as FileVoiceChannel in terms of receiving and
297 // sending the RTP packets. We therefore test only FileVoiceChannel.
298
299 // Test that SetSend() controls whether a voice channel sends RTP packets.
300 TEST_F(FileMediaEngineTest, TestVoiceChannelSetSend) {
301 CreateEngineAndChannels(voice_input_filename_, voice_output_filename_, "", "",
302 1);
303 EXPECT_TRUE(NULL != voice_channel_.get());
304 rtc::MemoryStream net_dump;
305 FileNetworkInterface net_interface(&net_dump, voice_channel_.get());
306 voice_channel_->SetInterface(&net_interface);
307
308 // The channel is not sending yet.
309 rtc::Thread::Current()->ProcessMessages(kWaitTimeMs);
310 EXPECT_EQ(0U, net_interface.num_sent_packets());
311
312 // The channel starts sending.
313 voice_channel_->SetSend(SEND_MICROPHONE);
314 EXPECT_TRUE_WAIT(net_interface.num_sent_packets() >= 1U, kWaitTimeout);
315
316 // The channel stops sending.
317 voice_channel_->SetSend(SEND_NOTHING);
318 // Wait until packets are all delivered.
319 rtc::Thread::Current()->ProcessMessages(kWaitTimeMs);
320 size_t old_number = net_interface.num_sent_packets();
321 rtc::Thread::Current()->ProcessMessages(kWaitTimeMs);
322 EXPECT_EQ(old_number, net_interface.num_sent_packets());
323
324 // The channel starts sending again.
325 voice_channel_->SetSend(SEND_MICROPHONE);
326 EXPECT_TRUE_WAIT(net_interface.num_sent_packets() > old_number, kWaitTimeout);
327
328 // When the function exits, the net_interface object is released. The sender
329 // thread may call net_interface to send packets, which results in a segment
330 // fault. We hence stop sending and wait until all packets are delivered
331 // before we exit this function.
332 voice_channel_->SetSend(SEND_NOTHING);
333 rtc::Thread::Current()->ProcessMessages(kWaitTimeMs);
334 }
335
336 // Test the sender thread of the channel. The sender sends RTP packets
337 // continuously with proper sequence number, timestamp, and payload.
338 TEST_F(FileMediaEngineTest, TestVoiceChannelSenderThread) {
339 CreateEngineAndChannels(voice_input_filename_, voice_output_filename_, "", "",
340 1);
341 EXPECT_TRUE(NULL != voice_channel_.get());
342 rtc::MemoryStream net_dump;
343 FileNetworkInterface net_interface(&net_dump, voice_channel_.get());
344 voice_channel_->SetInterface(&net_interface);
345
346 voice_channel_->SetSend(SEND_MICROPHONE);
347 // Wait until the number of sent packets is no less than 2 * kPacketNumber.
348 EXPECT_TRUE_WAIT(
349 net_interface.num_sent_packets() >=
350 2 * RtpTestUtility::GetTestPacketCount(),
351 kWaitTimeout);
352 voice_channel_->SetSend(SEND_NOTHING);
353 // Wait until packets are all delivered.
354 rtc::Thread::Current()->ProcessMessages(kWaitTimeMs);
355 EXPECT_TRUE(RtpTestUtility::VerifyTestPacketsFromStream(
356 2 * RtpTestUtility::GetTestPacketCount(), &net_dump,
357 RtpTestUtility::kDefaultSsrc));
358
359 // Each sent packet is dumped to net_dump and is also feed to the channel
360 // via OnPacketReceived, which in turn writes the packets into voice_output_.
361 // We next verify the packets in voice_output_.
362 voice_channel_.reset(); // Force to close the files.
363 rtc::scoped_ptr<rtc::StreamInterface> voice_output_;
364 voice_output_.reset(rtc::Filesystem::OpenFile(
365 rtc::Pathname(voice_output_filename_), "rb"));
366 EXPECT_TRUE(voice_output_.get() != NULL);
367 EXPECT_TRUE(RtpTestUtility::VerifyTestPacketsFromStream(
368 2 * RtpTestUtility::GetTestPacketCount(), voice_output_.get(),
369 RtpTestUtility::kDefaultSsrc));
370 }
371
372 // Test that we can specify the ssrc for outgoing RTP packets.
373 TEST_F(FileMediaEngineTest, TestVoiceChannelSendSsrc) {
374 CreateEngineAndChannels(voice_input_filename_, voice_output_filename_, "", "",
375 1);
376 EXPECT_TRUE(NULL != voice_channel_.get());
377 const uint32 send_ssrc = RtpTestUtility::kDefaultSsrc + 1;
378 voice_channel_->AddSendStream(StreamParams::CreateLegacy(send_ssrc));
379
380 rtc::MemoryStream net_dump;
381 FileNetworkInterface net_interface(&net_dump, voice_channel_.get());
382 voice_channel_->SetInterface(&net_interface);
383
384 voice_channel_->SetSend(SEND_MICROPHONE);
385 // Wait until the number of sent packets is no less than 2 * kPacketNumber.
386 EXPECT_TRUE_WAIT(
387 net_interface.num_sent_packets() >=
388 2 * RtpTestUtility::GetTestPacketCount(),
389 kWaitTimeout);
390 voice_channel_->SetSend(SEND_NOTHING);
391 // Wait until packets are all delivered.
392 rtc::Thread::Current()->ProcessMessages(kWaitTimeMs);
393 EXPECT_TRUE(RtpTestUtility::VerifyTestPacketsFromStream(
394 2 * RtpTestUtility::GetTestPacketCount(), &net_dump, send_ssrc));
395
396 // Each sent packet is dumped to net_dump and is also feed to the channel
397 // via OnPacketReceived, which in turn writes the packets into voice_output_.
398 // We next verify the packets in voice_output_.
399 voice_channel_.reset(); // Force to close the files.
400 rtc::scoped_ptr<rtc::StreamInterface> voice_output_;
401 voice_output_.reset(rtc::Filesystem::OpenFile(
402 rtc::Pathname(voice_output_filename_), "rb"));
403 EXPECT_TRUE(voice_output_.get() != NULL);
404 EXPECT_TRUE(RtpTestUtility::VerifyTestPacketsFromStream(
405 2 * RtpTestUtility::GetTestPacketCount(), voice_output_.get(),
406 send_ssrc));
407 }
408
409 // Test the sender thread of the channel, where the input rtpdump has two SSRCs.
410 TEST_F(FileMediaEngineTest, TestVoiceChannelSenderThreadTwoSsrcs) {
411 CreateEngineAndChannels(voice_input_filename_, voice_output_filename_, "", "",
412 2);
413 // Verify that voice_input_filename_ contains 2 *
414 // RtpTestUtility::GetTestPacketCount() packets
415 // with different SSRCs.
416 rtc::scoped_ptr<rtc::StreamInterface> input_stream(
417 rtc::Filesystem::OpenFile(
418 rtc::Pathname(voice_input_filename_), "rb"));
419 ASSERT_TRUE(NULL != input_stream.get());
420 size_t ssrc_count;
421 size_t packet_count;
422 EXPECT_TRUE(GetSsrcAndPacketCounts(input_stream.get(), &ssrc_count,
423 &packet_count));
424 EXPECT_EQ(2U, ssrc_count);
425 EXPECT_EQ(2 * RtpTestUtility::GetTestPacketCount(), packet_count);
426 input_stream.reset();
427
428 // Send 2 * RtpTestUtility::GetTestPacketCount() packets and verify that all
429 // these packets have the same SSRCs (that is, the packets with different
430 // SSRCs are skipped by the filemediaengine).
431 EXPECT_TRUE(NULL != voice_channel_.get());
432 rtc::MemoryStream net_dump;
433 FileNetworkInterface net_interface(&net_dump, voice_channel_.get());
434 voice_channel_->SetInterface(&net_interface);
435 voice_channel_->SetSend(SEND_MICROPHONE);
436 EXPECT_TRUE_WAIT(
437 net_interface.num_sent_packets() >=
438 2 * RtpTestUtility::GetTestPacketCount(),
439 kWaitTimeout);
440 voice_channel_->SetSend(SEND_NOTHING);
441 // Wait until packets are all delivered.
442 rtc::Thread::Current()->ProcessMessages(kWaitTimeMs);
443 net_dump.Rewind();
444 EXPECT_TRUE(GetSsrcAndPacketCounts(&net_dump, &ssrc_count, &packet_count));
445 EXPECT_EQ(1U, ssrc_count);
446 EXPECT_GE(packet_count, 2 * RtpTestUtility::GetTestPacketCount());
447 }
448
449 // Test SendIntraFrame() and RequestIntraFrame() of video channel.
450 TEST_F(FileMediaEngineTest, TestVideoChannelIntraFrame) {
451 CreateEngineAndChannels("", "", video_input_filename_, video_output_filename_,
452 1);
453 EXPECT_TRUE(NULL != video_channel_.get());
454 EXPECT_FALSE(video_channel_->SendIntraFrame());
455 EXPECT_FALSE(video_channel_->RequestIntraFrame());
456 }
457
458 } // namespace cricket
OLDNEW
« no previous file with comments | « talk/media/base/filemediaengine.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698