Home | History | Annotate | Download | only in media
      1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include <string>
      6 
      7 #include "base/strings/utf_string_conversions.h"
      8 #include "content/renderer/media/media_stream_extra_data.h"
      9 #include "content/renderer/media/mock_media_stream_dependency_factory.h"
     10 #include "content/renderer/media/mock_media_stream_registry.h"
     11 #include "content/renderer/media/video_destination_handler.h"
     12 #include "testing/gmock/include/gmock/gmock.h"
     13 #include "testing/gtest/include/gtest/gtest.h"
     14 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
     15 #include "third_party/WebKit/public/platform/WebString.h"
     16 
     17 using cricket::CapturedFrame;
     18 using cricket::CaptureState;
     19 using cricket::VideoCapturer;
     20 using cricket::VideoFormat;
     21 using cricket::VideoFormatPod;
     22 
     23 namespace content {
     24 
     25 static const std::string kTestStreamUrl = "stream_url";
     26 static const std::string kUnknownStreamUrl = "unknown_stream_url";
     27 static const VideoFormatPod kTestFormat = {
     28   640, 360, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY
     29 };
     30 
     31 class PpFrameWriterTest
     32     : public ::testing::Test,
     33       public sigslot::has_slots<> {
     34  public:
     35   PpFrameWriterTest()
     36       : last_capture_state_(cricket::CS_FAILED),
     37         captured_frame_count_(0),
     38         captured_frame_(NULL) {
     39     writer_.SignalStateChange.connect(this, &PpFrameWriterTest::OnStateChange);
     40     writer_.SignalFrameCaptured.connect(
     41         this, &PpFrameWriterTest::OnFrameCaptured);
     42   }
     43 
     44   void OnStateChange(VideoCapturer* capturer, CaptureState state) {
     45     last_capture_state_ = state;
     46   }
     47 
     48   void OnFrameCaptured(VideoCapturer* capturer, const CapturedFrame* frame) {
     49     ++captured_frame_count_;
     50     captured_frame_ = const_cast<CapturedFrame*>(frame);
     51   }
     52 
     53  protected:
     54   PpFrameWriter writer_;
     55   CaptureState last_capture_state_;
     56   int captured_frame_count_;
     57   CapturedFrame* captured_frame_;
     58 };
     59 
     60 class VideoDestinationHandlerTest : public ::testing::Test {
     61  public:
     62   VideoDestinationHandlerTest() : registry_(&factory_) {
     63     factory_.EnsurePeerConnectionFactory();
     64     registry_.Init(kTestStreamUrl);
     65   }
     66 
     67  protected:
     68   MockMediaStreamDependencyFactory factory_;
     69   MockMediaStreamRegistry registry_;
     70 };
     71 
     72 TEST_F(PpFrameWriterTest, StartStop) {
     73   EXPECT_FALSE(writer_.IsRunning());
     74   EXPECT_EQ(cricket::CS_STARTING, writer_.Start(VideoFormat(kTestFormat)));
     75   EXPECT_TRUE(writer_.IsRunning());
     76   EXPECT_EQ(cricket::CS_FAILED, writer_.Start(VideoFormat(kTestFormat)));
     77   writer_.Stop();
     78   EXPECT_EQ(cricket::CS_STOPPED, last_capture_state_);
     79 }
     80 
     81 TEST_F(PpFrameWriterTest, GetPreferredFourccs) {
     82   std::vector<uint32> fourccs;
     83   EXPECT_TRUE(writer_.GetPreferredFourccs(&fourccs));
     84   EXPECT_EQ(1u, fourccs.size());
     85   EXPECT_EQ(cricket::FOURCC_BGRA, fourccs[0]);
     86 }
     87 
     88 TEST_F(PpFrameWriterTest, GetBestCaptureFormat) {
     89   VideoFormat desired(kTestFormat);
     90   VideoFormat best_format;
     91   EXPECT_FALSE(writer_.GetBestCaptureFormat(desired, NULL));
     92   EXPECT_TRUE(writer_.GetBestCaptureFormat(desired, &best_format));
     93   EXPECT_EQ(cricket::FOURCC_BGRA, best_format.fourcc);
     94 
     95   desired.fourcc = best_format.fourcc;
     96   EXPECT_EQ(desired, best_format);
     97 }
     98 
     99 TEST_F(VideoDestinationHandlerTest, Open) {
    100   FrameWriterInterface* frame_writer = NULL;
    101   // Unknow url will return false.
    102   EXPECT_FALSE(VideoDestinationHandler::Open(&factory_, &registry_,
    103                                              kUnknownStreamUrl, &frame_writer));
    104   EXPECT_TRUE(VideoDestinationHandler::Open(&factory_, &registry_,
    105                                             kTestStreamUrl, &frame_writer));
    106   EXPECT_TRUE(frame_writer);
    107 
    108   // Verify the video track has been added.
    109   const blink::WebMediaStream test_stream = registry_.test_stream();
    110   blink::WebVector<blink::WebMediaStreamTrack> video_tracks;
    111   test_stream.videoTracks(video_tracks);
    112   EXPECT_EQ(1u, video_tracks.size());
    113 
    114   // Verify the native video track has been added.
    115   MediaStreamExtraData* extra_data =
    116       static_cast<MediaStreamExtraData*>(test_stream.extraData());
    117   DCHECK(extra_data);
    118   webrtc::MediaStreamInterface* native_stream = extra_data->stream().get();
    119   DCHECK(native_stream);
    120   webrtc::VideoTrackVector native_video_tracks =
    121       native_stream->GetVideoTracks();
    122   EXPECT_EQ(1u, native_video_tracks.size());
    123 
    124   delete frame_writer;
    125 }
    126 
    127 }  // namespace content
    128