10e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// libjingle
20e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Copyright 2010 Google Inc.
30e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//
40e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Redistribution and use in source and binary forms, with or without
50e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// modification, are permitted provided that the following conditions are met:
60e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//
70e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//  1. Redistributions of source code must retain the above copyright notice,
80e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//     this list of conditions and the following disclaimer.
90e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//  2. Redistributions in binary form must reproduce the above copyright notice,
100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//     this list of conditions and the following disclaimer in the documentation
110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//     and/or other materials provided with the distribution.
120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//  3. The name of the author may not be used to endorse or promote products
130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//     derived from this software without specific prior written permission.
140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//
150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include <string>
270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/media/base/fakemediaengine.h"
290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/media/base/rtpdump.h"
300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/media/base/testutils.h"
310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/p2p/base/fakesession.h"
320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/session/media/channel.h"
330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/session/media/mediarecorder.h"
34cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "webrtc/base/bytebuffer.h"
35cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "webrtc/base/fileutils.h"
36cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "webrtc/base/gunit.h"
37cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "webrtc/base/pathutils.h"
38cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "webrtc/base/thread.h"
390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgnamespace cricket {
410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
422a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.orgrtc::StreamInterface* Open(const std::string& path) {
432a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  return rtc::Filesystem::OpenFile(
442a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org      rtc::Pathname(path), "wb");
450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/////////////////////////////////////////////////////////////////////////
480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Test RtpDumpSink
490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/////////////////////////////////////////////////////////////////////////
500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass RtpDumpSinkTest : public testing::Test {
510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void SetUp() {
532a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org    EXPECT_TRUE(rtc::Filesystem::GetTemporaryFolder(path_, true, NULL));
540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    path_.SetFilename("sink-test.rtpdump");
550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    sink_.reset(new RtpDumpSink(Open(path_.pathname())));
560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    for (int i = 0; i < ARRAY_SIZE(rtp_buf_); ++i) {
580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      RtpTestUtility::kTestRawRtpPackets[i].WriteToByteBuffer(
590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org          RtpTestUtility::kDefaultSsrc, &rtp_buf_[i]);
600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    }
610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void TearDown() {
640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    stream_.reset();
652a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org    EXPECT_TRUE(rtc::Filesystem::DeleteFile(path_));
660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org protected:
690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnRtpPacket(const RawRtpPacket& raw) {
702a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org    rtc::ByteBuffer buf;
710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    raw.WriteToByteBuffer(RtpTestUtility::kDefaultSsrc, &buf);
720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    sink_->OnPacket(buf.Data(), buf.Length(), false);
730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
752a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::StreamResult ReadPacket(RtpDumpPacket* packet) {
760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    if (!stream_.get()) {
770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      sink_.reset();  // This will close the file. So we can read it.
782a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org      stream_.reset(rtc::Filesystem::OpenFile(path_, "rb"));
790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      reader_.reset(new RtpDumpReader(stream_.get()));
800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    }
810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return reader_->ReadPacket(packet);
820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
842a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::Pathname path_;
852a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_ptr<RtpDumpSink> sink_;
862a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::ByteBuffer rtp_buf_[3];
872a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_ptr<rtc::StreamInterface> stream_;
882a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_ptr<RtpDumpReader> reader_;
890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST_F(RtpDumpSinkTest, TestRtpDumpSink) {
920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // By default, the sink is disabled. The 1st packet is not written.
930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(sink_->IsEnabled());
940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  sink_->set_packet_filter(PF_ALL);
950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  OnRtpPacket(RtpTestUtility::kTestRawRtpPackets[0]);
960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Enable the sink. The 2nd packet is written.
980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(sink_->Enable(true));
990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(sink_->IsEnabled());
1002a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  EXPECT_TRUE(rtc::Filesystem::IsFile(path_.pathname()));
1010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  OnRtpPacket(RtpTestUtility::kTestRawRtpPackets[1]);
1020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Disable the sink. The 3rd packet is not written.
1040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(sink_->Enable(false));
1050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(sink_->IsEnabled());
1060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  OnRtpPacket(RtpTestUtility::kTestRawRtpPackets[2]);
1070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Read the recorded file and verify it contains only the 2nd packet.
1090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  RtpDumpPacket packet;
1102a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  EXPECT_EQ(rtc::SR_SUCCESS, ReadPacket(&packet));
1110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(RtpTestUtility::VerifyPacket(
1120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      &packet, &RtpTestUtility::kTestRawRtpPackets[1], false));
1132a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  EXPECT_EQ(rtc::SR_EOS, ReadPacket(&packet));
1140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST_F(RtpDumpSinkTest, TestRtpDumpSinkMaxSize) {
1170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(sink_->Enable(true));
1180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  sink_->set_packet_filter(PF_ALL);
1190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  sink_->SetMaxSize(strlen(RtpDumpFileHeader::kFirstLine) +
1200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                    RtpDumpFileHeader::kHeaderLength +
1210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                    RtpDumpPacket::kHeaderLength +
1220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                    RtpTestUtility::kTestRawRtpPackets[0].size());
1230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  OnRtpPacket(RtpTestUtility::kTestRawRtpPackets[0]);
1240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Exceed the limit size: the 2nd and 3rd packets are not written.
1260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  OnRtpPacket(RtpTestUtility::kTestRawRtpPackets[1]);
1270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  OnRtpPacket(RtpTestUtility::kTestRawRtpPackets[2]);
1280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Read the recorded file and verify that it contains only the first packet.
1300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  RtpDumpPacket packet;
1312a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  EXPECT_EQ(rtc::SR_SUCCESS, ReadPacket(&packet));
1320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(RtpTestUtility::VerifyPacket(
1330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      &packet, &RtpTestUtility::kTestRawRtpPackets[0], false));
1342a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  EXPECT_EQ(rtc::SR_EOS, ReadPacket(&packet));
1350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST_F(RtpDumpSinkTest, TestRtpDumpSinkFilter) {
1380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // The default filter is PF_NONE.
1390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(PF_NONE, sink_->packet_filter());
1400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Set to PF_RTPHEADER before enable.
1420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  sink_->set_packet_filter(PF_RTPHEADER);
1430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(PF_RTPHEADER, sink_->packet_filter());
1440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(sink_->Enable(true));
1450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // We dump only the header of the first packet.
1460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  OnRtpPacket(RtpTestUtility::kTestRawRtpPackets[0]);
1470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Set the filter to PF_RTPPACKET. We dump all the second packet.
1490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  sink_->set_packet_filter(PF_RTPPACKET);
1500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(PF_RTPPACKET, sink_->packet_filter());
1510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  OnRtpPacket(RtpTestUtility::kTestRawRtpPackets[1]);
1520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Set the filter to PF_NONE. We do not dump the third packet.
1540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  sink_->set_packet_filter(PF_NONE);
1550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(PF_NONE, sink_->packet_filter());
1560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  OnRtpPacket(RtpTestUtility::kTestRawRtpPackets[2]);
1570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Read the recorded file and verify the header of the first packet and
1590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // the whole packet for the second packet.
1600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  RtpDumpPacket packet;
1612a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  EXPECT_EQ(rtc::SR_SUCCESS, ReadPacket(&packet));
1620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(RtpTestUtility::VerifyPacket(
1630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      &packet, &RtpTestUtility::kTestRawRtpPackets[0], true));
1642a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  EXPECT_EQ(rtc::SR_SUCCESS, ReadPacket(&packet));
1650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(RtpTestUtility::VerifyPacket(
1660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      &packet, &RtpTestUtility::kTestRawRtpPackets[1], false));
1672a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  EXPECT_EQ(rtc::SR_EOS, ReadPacket(&packet));
1680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/////////////////////////////////////////////////////////////////////////
1710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Test MediaRecorder
1720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/////////////////////////////////////////////////////////////////////////
1730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid TestMediaRecorder(BaseChannel* channel,
1740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                       FakeVideoMediaChannel* video_media_channel,
1750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                       int filter) {
1760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Create media recorder.
1772a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_ptr<MediaRecorder> recorder(new MediaRecorder);
1780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Fail to EnableChannel before AddChannel.
1790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(recorder->EnableChannel(channel, true, true, SINK_PRE_CRYPTO));
1800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(channel->HasSendSinks(SINK_PRE_CRYPTO));
1810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(channel->HasRecvSinks(SINK_PRE_CRYPTO));
1820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(channel->HasSendSinks(SINK_POST_CRYPTO));
1830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(channel->HasRecvSinks(SINK_POST_CRYPTO));
1840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Add the channel to the recorder.
1862a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::Pathname path;
1872a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  EXPECT_TRUE(rtc::Filesystem::GetTemporaryFolder(path, true, NULL));
1880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  path.SetFilename("send.rtpdump");
1890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::string send_file = path.pathname();
1900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  path.SetFilename("recv.rtpdump");
1910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::string recv_file = path.pathname();
1920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  if (video_media_channel) {
1930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    EXPECT_TRUE(recorder->AddChannel(static_cast<VideoChannel*>(channel),
1940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                     Open(send_file), Open(recv_file), filter));
1950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  } else {
1960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    EXPECT_TRUE(recorder->AddChannel(static_cast<VoiceChannel*>(channel),
1970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                     Open(send_file), Open(recv_file), filter));
1980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
1990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Enable recording only the sent media.
2010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(recorder->EnableChannel(channel, true, false, SINK_PRE_CRYPTO));
2020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(channel->HasSendSinks(SINK_PRE_CRYPTO));
2030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(channel->HasRecvSinks(SINK_POST_CRYPTO));
2040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(channel->HasSendSinks(SINK_POST_CRYPTO));
2050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(channel->HasRecvSinks(SINK_POST_CRYPTO));
2060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  if (video_media_channel) {
2070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    EXPECT_TRUE_WAIT(video_media_channel->sent_intra_frame(), 100);
2080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
2090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Enable recording only the received meida.
2110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(recorder->EnableChannel(channel, false, true, SINK_PRE_CRYPTO));
2120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(channel->HasSendSinks(SINK_PRE_CRYPTO));
2130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(channel->HasRecvSinks(SINK_PRE_CRYPTO));
2140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  if (video_media_channel) {
2150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    EXPECT_TRUE(video_media_channel->requested_intra_frame());
2160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
2170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Enable recording both the sent and the received video.
2190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(recorder->EnableChannel(channel, true, true, SINK_PRE_CRYPTO));
2200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(channel->HasSendSinks(SINK_PRE_CRYPTO));
2210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(channel->HasRecvSinks(SINK_PRE_CRYPTO));
2220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Enable recording only headers.
2240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  if (video_media_channel) {
2250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    video_media_channel->set_sent_intra_frame(false);
2260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    video_media_channel->set_requested_intra_frame(false);
2270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
2280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(recorder->EnableChannel(channel, true, true, SINK_PRE_CRYPTO));
2290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(channel->HasSendSinks(SINK_PRE_CRYPTO));
2300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(channel->HasRecvSinks(SINK_PRE_CRYPTO));
2310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  if (video_media_channel) {
2320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    if ((filter & PF_RTPPACKET) == PF_RTPPACKET) {
2330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      // If record the whole RTP packet, trigers FIR.
2340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      EXPECT_TRUE(video_media_channel->requested_intra_frame());
2350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      EXPECT_TRUE(video_media_channel->sent_intra_frame());
2360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    } else {
2370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      // If record only the RTP header, does not triger FIR.
2380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      EXPECT_FALSE(video_media_channel->requested_intra_frame());
2390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      EXPECT_FALSE(video_media_channel->sent_intra_frame());
2400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    }
2410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
2420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Remove the voice channel from the recorder.
2440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  recorder->RemoveChannel(channel, SINK_PRE_CRYPTO);
2450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(channel->HasSendSinks(SINK_PRE_CRYPTO));
2460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(channel->HasRecvSinks(SINK_PRE_CRYPTO));
2470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Delete all files.
2490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  recorder.reset();
2502a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  EXPECT_TRUE(rtc::Filesystem::DeleteFile(send_file));
2512a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  EXPECT_TRUE(rtc::Filesystem::DeleteFile(recv_file));
2520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
2530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Fisrt start recording header and then start recording media. Verify that
2550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// differnt files are created for header and media.
2560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid TestRecordHeaderAndMedia(BaseChannel* channel,
2570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                              FakeVideoMediaChannel* video_media_channel) {
2580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Create RTP header recorder.
2592a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_ptr<MediaRecorder> header_recorder(new MediaRecorder);
2600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2612a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::Pathname path;
2622a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  EXPECT_TRUE(rtc::Filesystem::GetTemporaryFolder(path, true, NULL));
2630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  path.SetFilename("send-header.rtpdump");
2640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::string send_header_file = path.pathname();
2650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  path.SetFilename("recv-header.rtpdump");
2660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::string recv_header_file = path.pathname();
2670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  if (video_media_channel) {
2680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    EXPECT_TRUE(header_recorder->AddChannel(
2690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        static_cast<VideoChannel*>(channel),
2700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        Open(send_header_file), Open(recv_header_file), PF_RTPHEADER));
2710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  } else {
2720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    EXPECT_TRUE(header_recorder->AddChannel(
2730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        static_cast<VoiceChannel*>(channel),
2740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        Open(send_header_file), Open(recv_header_file), PF_RTPHEADER));
2750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
2760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Enable recording both sent and received.
2780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(
2790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      header_recorder->EnableChannel(channel, true, true, SINK_POST_CRYPTO));
2800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(channel->HasSendSinks(SINK_POST_CRYPTO));
2810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(channel->HasRecvSinks(SINK_POST_CRYPTO));
2820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(channel->HasSendSinks(SINK_PRE_CRYPTO));
2830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(channel->HasRecvSinks(SINK_PRE_CRYPTO));
2840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  if (video_media_channel) {
2850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    EXPECT_FALSE(video_media_channel->sent_intra_frame());
2860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    EXPECT_FALSE(video_media_channel->requested_intra_frame());
2870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
2880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Verify that header files are created.
2902a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  EXPECT_TRUE(rtc::Filesystem::IsFile(send_header_file));
2912a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  EXPECT_TRUE(rtc::Filesystem::IsFile(recv_header_file));
2920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Create RTP header recorder.
2942a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  rtc::scoped_ptr<MediaRecorder> recorder(new MediaRecorder);
2950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  path.SetFilename("send.rtpdump");
2960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::string send_file = path.pathname();
2970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  path.SetFilename("recv.rtpdump");
2980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  std::string recv_file = path.pathname();
2990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  if (video_media_channel) {
3000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    EXPECT_TRUE(recorder->AddChannel(
3010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        static_cast<VideoChannel*>(channel),
3020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        Open(send_file), Open(recv_file), PF_RTPPACKET));
3030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  } else {
3040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    EXPECT_TRUE(recorder->AddChannel(
3050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        static_cast<VoiceChannel*>(channel),
3060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        Open(send_file), Open(recv_file), PF_RTPPACKET));
3070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
3080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Enable recording both sent and received.
3100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(recorder->EnableChannel(channel, true, true, SINK_PRE_CRYPTO));
3110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(channel->HasSendSinks(SINK_POST_CRYPTO));
3120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(channel->HasRecvSinks(SINK_POST_CRYPTO));
3130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(channel->HasSendSinks(SINK_PRE_CRYPTO));
3140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(channel->HasRecvSinks(SINK_PRE_CRYPTO));
3150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  if (video_media_channel) {
3160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    EXPECT_TRUE_WAIT(video_media_channel->sent_intra_frame(), 100);
3170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    EXPECT_TRUE(video_media_channel->requested_intra_frame());
3180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
3190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Verify that media files are created.
3212a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  EXPECT_TRUE(rtc::Filesystem::IsFile(send_file));
3222a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  EXPECT_TRUE(rtc::Filesystem::IsFile(recv_file));
3230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Delete all files.
3250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  header_recorder.reset();
3260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  recorder.reset();
3272a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  EXPECT_TRUE(rtc::Filesystem::DeleteFile(send_header_file));
3282a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  EXPECT_TRUE(rtc::Filesystem::DeleteFile(recv_header_file));
3292a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  EXPECT_TRUE(rtc::Filesystem::DeleteFile(send_file));
3302a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  EXPECT_TRUE(rtc::Filesystem::DeleteFile(recv_file));
3310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
3320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST(MediaRecorderTest, TestMediaRecorderVoiceChannel) {
3340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Create the voice channel.
3350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  FakeSession session(true);
3360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  FakeMediaEngine media_engine;
3372a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  VoiceChannel channel(rtc::Thread::Current(), &media_engine,
3380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                       new FakeVoiceMediaChannel(NULL), &session, "", false);
3390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(channel.Init());
3400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TestMediaRecorder(&channel, NULL, PF_RTPPACKET);
3410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TestMediaRecorder(&channel, NULL, PF_RTPHEADER);
3420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TestRecordHeaderAndMedia(&channel, NULL);
3430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
3440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST(MediaRecorderTest, TestMediaRecorderVideoChannel) {
3460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Create the video channel.
3470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  FakeSession session(true);
3480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  FakeMediaEngine media_engine;
3490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  FakeVideoMediaChannel* media_channel = new FakeVideoMediaChannel(NULL);
3502a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org  VideoChannel channel(rtc::Thread::Current(), &media_engine,
3510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                       media_channel, &session, "", false, NULL);
3520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(channel.Init());
3530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TestMediaRecorder(&channel, media_channel, PF_RTPPACKET);
3540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TestMediaRecorder(&channel, media_channel, PF_RTPHEADER);
3550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  TestRecordHeaderAndMedia(&channel, media_channel);
3560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
3570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}  // namespace cricket
359