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/message_loop/message_loop.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "content/child/child_process.h"
10 #include "content/common/media/video_capture.h"
11 #include "content/public/renderer/media_stream_video_sink.h"
12 #include "content/renderer/media/media_stream.h"
13 #include "content/renderer/media/media_stream_registry_interface.h"
14 #include "content/renderer/media/mock_media_stream_registry.h"
15 #include "content/renderer/media/video_source_handler.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
19 #include "third_party/WebKit/public/platform/WebString.h"
20 #include "third_party/WebKit/public/web/WebHeap.h"
24 static const std::string kTestStreamUrl
= "stream_url";
25 static const std::string kTestVideoTrackId
= "video_track_id";
26 static const std::string kUnknownStreamUrl
= "unknown_stream_url";
28 class FakeFrameReader
: public FrameReaderInterface
{
30 void GotFrame(const scoped_refptr
<media::VideoFrame
>& frame
) override
{
34 const media::VideoFrame
* last_frame() {
35 return last_frame_
.get();
39 scoped_refptr
<media::VideoFrame
> last_frame_
;
42 class VideoSourceHandlerTest
: public ::testing::Test
{
44 VideoSourceHandlerTest()
45 : child_process_(new ChildProcess()),
46 registry_(new MockMediaStreamRegistry()) {
47 handler_
.reset(new VideoSourceHandler(registry_
.get()));
48 registry_
->Init(kTestStreamUrl
);
49 registry_
->AddVideoTrack(kTestVideoTrackId
);
52 void TearDown() override
{
55 blink::WebHeap::collectAllGarbageForTesting();
59 base::MessageLoop message_loop_
;
60 scoped_ptr
<ChildProcess
> child_process_
;
61 scoped_ptr
<VideoSourceHandler
> handler_
;
62 scoped_ptr
<MockMediaStreamRegistry
> registry_
;
65 TEST_F(VideoSourceHandlerTest
, OpenClose
) {
66 FakeFrameReader reader
;
67 // Unknow url will return false.
68 EXPECT_FALSE(handler_
->Open(kUnknownStreamUrl
, &reader
));
69 EXPECT_TRUE(handler_
->Open(kTestStreamUrl
, &reader
));
73 base::TimeDelta ts
= base::TimeDelta::FromInternalValue(789012);
75 // A new frame is captured.
76 scoped_refptr
<media::VideoFrame
> captured_frame
=
77 media::VideoFrame::CreateBlackFrame(gfx::Size(width
, height
));
78 captured_frame
->set_timestamp(ts
);
80 // The frame is delivered to VideoSourceHandler.
81 handler_
->DeliverFrameForTesting(&reader
, captured_frame
);
83 // Compare |frame| to |captured_frame|.
84 const media::VideoFrame
* frame
= reader
.last_frame();
85 ASSERT_TRUE(frame
!= NULL
);
86 EXPECT_EQ(width
, frame
->coded_size().width());
87 EXPECT_EQ(height
, frame
->coded_size().height());
88 EXPECT_EQ(ts
, frame
->timestamp());
89 EXPECT_EQ(captured_frame
->data(media::VideoFrame::kYPlane
),
90 frame
->data(media::VideoFrame::kYPlane
));
92 EXPECT_FALSE(handler_
->Close(NULL
));
93 EXPECT_TRUE(handler_
->Close(&reader
));
96 TEST_F(VideoSourceHandlerTest
, OpenWithoutClose
) {
97 FakeFrameReader reader
;
98 EXPECT_TRUE(handler_
->Open(kTestStreamUrl
, &reader
));
101 } // namespace content