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.
7 #include "base/strings/utf_string_conversions.h"
8 #include "content/renderer/media/media_stream.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"
17 using cricket::CapturedFrame;
18 using cricket::CaptureState;
19 using cricket::VideoCapturer;
20 using cricket::VideoFormat;
21 using cricket::VideoFormatPod;
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
31 class PpFrameWriterTest
32 : public ::testing::Test,
33 public sigslot::has_slots<> {
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);
44 void OnStateChange(VideoCapturer* capturer, CaptureState state) {
45 last_capture_state_ = state;
48 void OnFrameCaptured(VideoCapturer* capturer, const CapturedFrame* frame) {
49 ++captured_frame_count_;
50 captured_frame_ = const_cast<CapturedFrame*>(frame);
54 PpFrameWriter writer_;
55 CaptureState last_capture_state_;
56 int captured_frame_count_;
57 CapturedFrame* captured_frame_;
60 class VideoDestinationHandlerTest : public ::testing::Test {
62 VideoDestinationHandlerTest() : registry_(&factory_) {
63 registry_.Init(kTestStreamUrl);
67 MockMediaStreamDependencyFactory factory_;
68 MockMediaStreamRegistry registry_;
71 TEST_F(PpFrameWriterTest, StartStop) {
72 EXPECT_FALSE(writer_.IsRunning());
73 EXPECT_EQ(cricket::CS_STARTING, writer_.Start(VideoFormat(kTestFormat)));
74 EXPECT_TRUE(writer_.IsRunning());
75 EXPECT_EQ(cricket::CS_FAILED, writer_.Start(VideoFormat(kTestFormat)));
77 EXPECT_EQ(cricket::CS_STOPPED, last_capture_state_);
80 TEST_F(PpFrameWriterTest, GetPreferredFourccs) {
81 std::vector<uint32> fourccs;
82 EXPECT_TRUE(writer_.GetPreferredFourccs(&fourccs));
83 EXPECT_EQ(1u, fourccs.size());
84 EXPECT_EQ(cricket::FOURCC_BGRA, fourccs[0]);
87 TEST_F(PpFrameWriterTest, GetBestCaptureFormat) {
88 VideoFormat desired(kTestFormat);
89 VideoFormat best_format;
90 EXPECT_FALSE(writer_.GetBestCaptureFormat(desired, NULL));
91 EXPECT_TRUE(writer_.GetBestCaptureFormat(desired, &best_format));
92 EXPECT_EQ(cricket::FOURCC_BGRA, best_format.fourcc);
94 desired.fourcc = best_format.fourcc;
95 EXPECT_EQ(desired, best_format);
98 TEST_F(VideoDestinationHandlerTest, Open) {
99 FrameWriterInterface* frame_writer = NULL;
100 // Unknow url will return false.
101 EXPECT_FALSE(VideoDestinationHandler::Open(&factory_, ®istry_,
102 kUnknownStreamUrl, &frame_writer));
103 EXPECT_TRUE(VideoDestinationHandler::Open(&factory_, ®istry_,
104 kTestStreamUrl, &frame_writer));
105 EXPECT_TRUE(frame_writer);
107 // Verify the video track has been added.
108 const blink::WebMediaStream test_stream = registry_.test_stream();
109 blink::WebVector<blink::WebMediaStreamTrack> video_tracks;
110 test_stream.videoTracks(video_tracks);
111 EXPECT_EQ(1u, video_tracks.size());
113 // Verify the native video track has been added.
114 MediaStream* native_stream = MediaStream::GetMediaStream(test_stream);
115 DCHECK(native_stream);
116 webrtc::MediaStreamInterface* webrtc_stream =
117 MediaStream::GetAdapter(test_stream);
118 DCHECK(webrtc_stream);
119 webrtc::VideoTrackVector webrtc_video_tracks =
120 webrtc_stream->GetVideoTracks();
121 EXPECT_EQ(1u, webrtc_video_tracks.size());
126 } // namespace content