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"
23 static const std::string kTestStreamUrl
= "stream_url";
24 static const std::string kTestVideoTrackId
= "video_track_id";
25 static const std::string kUnknownStreamUrl
= "unknown_stream_url";
27 class FakeFrameReader
: public FrameReaderInterface
{
29 virtual bool GotFrame(
30 const scoped_refptr
<media::VideoFrame
>& frame
) OVERRIDE
{
35 const media::VideoFrame
* last_frame() {
36 return last_frame_
.get();
40 scoped_refptr
<media::VideoFrame
> last_frame_
;
43 class VideoSourceHandlerTest
: public ::testing::Test
{
45 VideoSourceHandlerTest()
46 : child_process_(new ChildProcess()),
48 handler_
.reset(new VideoSourceHandler(®istry_
));
49 registry_
.Init(kTestStreamUrl
);
50 registry_
.AddVideoTrack(kTestVideoTrackId
);
54 base::MessageLoop message_loop_
;
55 scoped_ptr
<ChildProcess
> child_process_
;
56 scoped_ptr
<VideoSourceHandler
> handler_
;
57 MockMediaStreamRegistry registry_
;
60 TEST_F(VideoSourceHandlerTest
, OpenClose
) {
61 FakeFrameReader reader
;
62 // Unknow url will return false.
63 EXPECT_FALSE(handler_
->Open(kUnknownStreamUrl
, &reader
));
64 EXPECT_TRUE(handler_
->Open(kTestStreamUrl
, &reader
));
68 base::TimeDelta ts
= base::TimeDelta::FromInternalValue(789012);
70 // A new frame is captured.
71 scoped_refptr
<media::VideoFrame
> captured_frame
=
72 media::VideoFrame::CreateBlackFrame(gfx::Size(width
, height
));
73 captured_frame
->set_timestamp(ts
);
75 // The frame is delivered to VideoSourceHandler.
76 handler_
->DeliverFrameForTesting(&reader
, captured_frame
);
78 // Compare |frame| to |captured_frame|.
79 const media::VideoFrame
* frame
= reader
.last_frame();
80 ASSERT_TRUE(frame
!= NULL
);
81 EXPECT_EQ(width
, frame
->coded_size().width());
82 EXPECT_EQ(height
, frame
->coded_size().height());
83 EXPECT_EQ(ts
, frame
->timestamp());
84 EXPECT_EQ(captured_frame
->data(media::VideoFrame::kYPlane
),
85 frame
->data(media::VideoFrame::kYPlane
));
87 EXPECT_FALSE(handler_
->Close(NULL
));
88 EXPECT_TRUE(handler_
->Close(&reader
));
91 TEST_F(VideoSourceHandlerTest
, OpenWithoutClose
) {
92 FakeFrameReader reader
;
93 EXPECT_TRUE(handler_
->Open(kTestStreamUrl
, &reader
));
96 } // namespace content