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_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"
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 factory_
.EnsurePeerConnectionFactory();
64 registry_
.Init(kTestStreamUrl
);
68 MockMediaStreamDependencyFactory factory_
;
69 MockMediaStreamRegistry registry_
;
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
)));
78 EXPECT_EQ(cricket::CS_STOPPED
, last_capture_state_
);
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]);
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
);
95 desired
.fourcc
= best_format
.fourcc
;
96 EXPECT_EQ(desired
, best_format
);
99 TEST_F(VideoDestinationHandlerTest
, Open
) {
100 FrameWriterInterface
* frame_writer
= NULL
;
101 // Unknow url will return false.
102 EXPECT_FALSE(VideoDestinationHandler::Open(&factory_
, ®istry_
,
103 kUnknownStreamUrl
, &frame_writer
));
104 EXPECT_TRUE(VideoDestinationHandler::Open(&factory_
, ®istry_
,
105 kTestStreamUrl
, &frame_writer
));
106 EXPECT_TRUE(frame_writer
);
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());
114 // Verify the native video track has been added.
115 MediaStreamExtraData
* extra_data
=
116 static_cast<MediaStreamExtraData
*>(test_stream
.extraData());
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());
127 } // namespace content