Home | History | Annotate | Download | only in webrtc
      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/message_loop/message_loop.h"
      8 #include "base/run_loop.h"
      9 #include "base/strings/utf_string_conversions.h"
     10 #include "content/child/child_process.h"
     11 #include "content/renderer/media/media_stream.h"
     12 #include "content/renderer/media/media_stream_video_track.h"
     13 #include "content/renderer/media/mock_media_stream_registry.h"
     14 #include "content/renderer/media/mock_media_stream_video_sink.h"
     15 #include "content/renderer/media/webrtc/video_destination_handler.h"
     16 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
     17 #include "content/renderer/pepper/ppb_image_data_impl.h"
     18 #include "content/test/ppapi_unittest.h"
     19 #include "testing/gmock/include/gmock/gmock.h"
     20 #include "testing/gtest/include/gtest/gtest.h"
     21 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
     22 #include "third_party/WebKit/public/platform/WebString.h"
     23 
     24 using ::testing::_;
     25 
     26 namespace content {
     27 
     28 ACTION_P(RunClosure, closure) {
     29   closure.Run();
     30 }
     31 
     32 static const std::string kTestStreamUrl = "stream_url";
     33 static const std::string kUnknownStreamUrl = "unknown_stream_url";
     34 
     35 class VideoDestinationHandlerTest : public PpapiUnittest {
     36  public:
     37   VideoDestinationHandlerTest()
     38      : child_process_(new ChildProcess()),
     39        registry_(new MockMediaStreamRegistry()) {
     40     registry_->Init(kTestStreamUrl);
     41   }
     42 
     43   virtual void TearDown() {
     44     registry_.reset();
     45     PpapiUnittest::TearDown();
     46   }
     47 
     48   base::MessageLoop* io_message_loop() const {
     49     return child_process_->io_message_loop();
     50   }
     51 
     52  protected:
     53   scoped_ptr<ChildProcess> child_process_;
     54   scoped_ptr<MockMediaStreamRegistry> registry_;
     55 };
     56 
     57 TEST_F(VideoDestinationHandlerTest, Open) {
     58   FrameWriterInterface* frame_writer = NULL;
     59   // Unknow url will return false.
     60   EXPECT_FALSE(VideoDestinationHandler::Open(registry_.get(),
     61                                              kUnknownStreamUrl, &frame_writer));
     62   EXPECT_TRUE(VideoDestinationHandler::Open(registry_.get(),
     63                                             kTestStreamUrl, &frame_writer));
     64   // The |frame_writer| is a proxy and is owned by whoever call Open.
     65   delete frame_writer;
     66 }
     67 
     68 TEST_F(VideoDestinationHandlerTest, PutFrame) {
     69   FrameWriterInterface* frame_writer = NULL;
     70   EXPECT_TRUE(VideoDestinationHandler::Open(registry_.get(),
     71                                             kTestStreamUrl, &frame_writer));
     72   ASSERT_TRUE(frame_writer);
     73 
     74   // Verify the video track has been added.
     75   const blink::WebMediaStream test_stream = registry_->test_stream();
     76   blink::WebVector<blink::WebMediaStreamTrack> video_tracks;
     77   test_stream.videoTracks(video_tracks);
     78   ASSERT_EQ(1u, video_tracks.size());
     79 
     80   // Verify the native video track has been added.
     81   MediaStreamVideoTrack* native_track =
     82       MediaStreamVideoTrack::GetVideoTrack(video_tracks[0]);
     83   ASSERT_TRUE(native_track != NULL);
     84 
     85   MockMediaStreamVideoSink sink;
     86   native_track->AddSink(&sink, sink.GetDeliverFrameCB());
     87    scoped_refptr<PPB_ImageData_Impl> image(
     88       new PPB_ImageData_Impl(instance()->pp_instance(),
     89                              PPB_ImageData_Impl::ForTest()));
     90   image->Init(PP_IMAGEDATAFORMAT_BGRA_PREMUL, 640, 360, true);
     91   {
     92     base::RunLoop run_loop;
     93     base::Closure quit_closure = run_loop.QuitClosure();
     94 
     95     EXPECT_CALL(sink, OnVideoFrame()).WillOnce(
     96         RunClosure(quit_closure));
     97     frame_writer->PutFrame(image, 10);
     98     run_loop.Run();
     99   }
    100   // TODO(perkj): Verify that the track output I420 when
    101   // https://codereview.chromium.org/213423006/ is landed.
    102   EXPECT_EQ(1, sink.number_of_frames());
    103   native_track->RemoveSink(&sink);
    104 
    105   // The |frame_writer| is a proxy and is owned by whoever call Open.
    106   delete frame_writer;
    107 }
    108 
    109 }  // namespace content
    110