10e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/*
20e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * libjingle
30e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Copyright 2012, Google Inc.
40e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
50e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Redistribution and use in source and binary forms, with or without
60e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * modification, are permitted provided that the following conditions are met:
70e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
80e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  1. Redistributions of source code must retain the above copyright notice,
90e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     this list of conditions and the following disclaimer.
100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  2. Redistributions in binary form must reproduce the above copyright notice,
110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     this list of conditions and the following disclaimer in the documentation
120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     and/or other materials provided with the distribution.
130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  3. The name of the author may not be used to endorse or promote products
140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     derived from this software without specific prior written permission.
150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org */
270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/media/base/capturemanager.h"
290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/media/base/fakemediaprocessor.h"
310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/media/base/fakevideocapturer.h"
320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/media/base/fakevideorenderer.h"
33cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "webrtc/base/gunit.h"
34cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "webrtc/base/sigslot.h"
350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgconst int kMsCallbackWait = 50;
370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgconst int kFps = 30;
390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgconst cricket::VideoFormatPod kCameraFormats[] = {
400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  {640, 480, cricket::VideoFormat::FpsToInterval(kFps), cricket::FOURCC_I420},
410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  {320, 240, cricket::VideoFormat::FpsToInterval(kFps), cricket::FOURCC_I420}
420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass CaptureManagerTest : public ::testing::Test, public sigslot::has_slots<> {
450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  CaptureManagerTest()
470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      : capture_manager_(),
480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        callback_count_(0),
490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        format_vga_(kCameraFormats[0]),
500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        format_qvga_(kCameraFormats[1]) {
510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual void SetUp() {
530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    PopulateSupportedFormats();
540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    capture_state_ = cricket::CS_STOPPED;
550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    capture_manager_.SignalCapturerStateChange.connect(
560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        this,
570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        &CaptureManagerTest::OnCapturerStateChange);
580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void PopulateSupportedFormats() {
600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    std::vector<cricket::VideoFormat> formats;
610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    for (int i = 0; i < ARRAY_SIZE(kCameraFormats); ++i) {
620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      formats.push_back(cricket::VideoFormat(kCameraFormats[i]));
630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    }
640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    video_capturer_.ResetSupportedFormats(formats);
650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  int NumFramesProcessed() { return media_processor_.video_frame_count(); }
670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  int NumFramesRendered() { return video_renderer_.num_rendered_frames(); }
680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool WasRenderedResolution(cricket::VideoFormat format) {
690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return format.width == video_renderer_.width() &&
700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        format.height == video_renderer_.height();
710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  cricket::CaptureState capture_state() { return capture_state_; }
730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  int callback_count() { return callback_count_; }
740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  void OnCapturerStateChange(cricket::VideoCapturer* capturer,
750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                             cricket::CaptureState capture_state) {
760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    capture_state_ = capture_state;
770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    ++callback_count_;
780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org protected:
810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  cricket::FakeMediaProcessor media_processor_;
820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  cricket::FakeVideoCapturer video_capturer_;
830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  cricket::FakeVideoRenderer video_renderer_;
840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  cricket::CaptureManager capture_manager_;
860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  cricket::CaptureState capture_state_;
880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  int callback_count_;
890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  cricket::VideoFormat format_vga_;
900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  cricket::VideoFormat format_qvga_;
910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Incorrect use cases.
940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST_F(CaptureManagerTest, InvalidCallOrder) {
950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Capturer must be registered before any of these calls.
960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(capture_manager_.AddVideoRenderer(&video_capturer_,
970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                 &video_renderer_));
980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(capture_manager_.AddVideoProcessor(&video_capturer_,
990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                  &media_processor_));
1000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST_F(CaptureManagerTest, InvalidAddingRemoving) {
1030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(capture_manager_.StopVideoCapture(&video_capturer_,
1040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                 cricket::VideoFormat()));
1050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(capture_manager_.StartVideoCapture(&video_capturer_,
1060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                 format_vga_));
1070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ_WAIT(cricket::CS_RUNNING, capture_state(), kMsCallbackWait);
1080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(1, callback_count());
1090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(capture_manager_.AddVideoRenderer(&video_capturer_, NULL));
1100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(capture_manager_.RemoveVideoRenderer(&video_capturer_,
1110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                    &video_renderer_));
1120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(capture_manager_.AddVideoProcessor(&video_capturer_,
1130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                  NULL));
1140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(capture_manager_.RemoveVideoProcessor(&video_capturer_,
1150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                     &media_processor_));
1160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(capture_manager_.StopVideoCapture(&video_capturer_, format_vga_));
1170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Valid use cases
1200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST_F(CaptureManagerTest, ProcessorTest) {
1210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(capture_manager_.StartVideoCapture(&video_capturer_,
1220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                 format_vga_));
1230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ_WAIT(cricket::CS_RUNNING, capture_state(), kMsCallbackWait);
1240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(1, callback_count());
1250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(capture_manager_.AddVideoRenderer(&video_capturer_,
1260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                &video_renderer_));
1270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(capture_manager_.AddVideoProcessor(&video_capturer_,
1280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                 &media_processor_));
1290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(video_capturer_.CaptureFrame());
1300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(1, NumFramesProcessed());
1310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(1, NumFramesRendered());
1320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(capture_manager_.RemoveVideoProcessor(&video_capturer_,
1330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                    &media_processor_));
1340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Processor has been removed so no more frames should be processed.
1350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(video_capturer_.CaptureFrame());
1360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(1, NumFramesProcessed());
1370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(2, NumFramesRendered());
1380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(capture_manager_.StopVideoCapture(&video_capturer_, format_vga_));
1390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(2, callback_count());
1400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST_F(CaptureManagerTest, KeepFirstResolutionHigh) {
1430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(capture_manager_.StartVideoCapture(&video_capturer_,
1440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                 format_vga_));
1450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ_WAIT(cricket::CS_RUNNING, capture_state(), kMsCallbackWait);
1460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(1, callback_count());
1470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(capture_manager_.AddVideoRenderer(&video_capturer_,
1480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                &video_renderer_));
1490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(video_capturer_.CaptureFrame());
1500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(1, NumFramesRendered());
1510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Renderer should be fed frames with the resolution of format_vga_.
1520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(WasRenderedResolution(format_vga_));
1530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Start again with one more format.
1550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(capture_manager_.StartVideoCapture(&video_capturer_,
1560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                 format_qvga_));
1570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Existing renderers should be fed frames with the resolution of format_vga_.
1580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(video_capturer_.CaptureFrame());
1590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(WasRenderedResolution(format_vga_));
1600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(capture_manager_.StopVideoCapture(&video_capturer_, format_vga_));
1610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(capture_manager_.StopVideoCapture(&video_capturer_,
1620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                format_qvga_));
1630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(capture_manager_.StopVideoCapture(&video_capturer_,
1640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                 format_vga_));
1650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(capture_manager_.StopVideoCapture(&video_capturer_,
1660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                 format_qvga_));
1670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Should pick the lowest resolution as the highest resolution is not chosen
1700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// until after capturing has started. This ensures that no particular resolution
1710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// is favored over others.
1720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST_F(CaptureManagerTest, KeepFirstResolutionLow) {
1730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(capture_manager_.StartVideoCapture(&video_capturer_,
1740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                 format_qvga_));
1750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(capture_manager_.StartVideoCapture(&video_capturer_,
1760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                 format_vga_));
1770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(capture_manager_.AddVideoRenderer(&video_capturer_,
1780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                &video_renderer_));
1790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ_WAIT(1, callback_count(), kMsCallbackWait);
1800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(video_capturer_.CaptureFrame());
1810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(1, NumFramesRendered());
1820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(WasRenderedResolution(format_qvga_));
183d156f12e9ee96258b7b79d0e3d8ddab6b4b87fbbwu@webrtc.org  EXPECT_TRUE(capture_manager_.StopVideoCapture(&video_capturer_,
184d156f12e9ee96258b7b79d0e3d8ddab6b4b87fbbwu@webrtc.org                                                format_qvga_));
185d156f12e9ee96258b7b79d0e3d8ddab6b4b87fbbwu@webrtc.org  EXPECT_TRUE(capture_manager_.StopVideoCapture(&video_capturer_,
186d156f12e9ee96258b7b79d0e3d8ddab6b4b87fbbwu@webrtc.org                                                format_vga_));
1870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Ensure that the reference counting is working when multiple start and
1900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// multiple stop calls are made.
1910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST_F(CaptureManagerTest, MultipleStartStops) {
1920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(capture_manager_.StartVideoCapture(&video_capturer_,
1930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                 format_vga_));
1940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Add video capturer but with different format.
1950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(capture_manager_.StartVideoCapture(&video_capturer_,
1960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                 format_qvga_));
1970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ_WAIT(cricket::CS_RUNNING, capture_state(), kMsCallbackWait);
1980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(1, callback_count());
1990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(capture_manager_.AddVideoRenderer(&video_capturer_,
2000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                &video_renderer_));
2010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Ensure that a frame can be captured when two start calls have been made.
2020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(video_capturer_.CaptureFrame());
2030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(1, NumFramesRendered());
2040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(capture_manager_.StopVideoCapture(&video_capturer_, format_vga_));
2060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Video should still render since there has been two start calls but only
2070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // one stop call.
2080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(video_capturer_.CaptureFrame());
2090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(2, NumFramesRendered());
2100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(capture_manager_.StopVideoCapture(&video_capturer_,
2120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                format_qvga_));
2130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ_WAIT(cricket::CS_STOPPED, capture_state(), kMsCallbackWait);
2140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(2, callback_count());
2150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Last stop call should fail as it is one more than the number of start
2160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // calls.
2170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_FALSE(capture_manager_.StopVideoCapture(&video_capturer_,
2180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                 format_vga_));
2190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
2200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST_F(CaptureManagerTest, TestForceRestart) {
2220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(capture_manager_.StartVideoCapture(&video_capturer_,
2230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                 format_qvga_));
2240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(capture_manager_.AddVideoRenderer(&video_capturer_,
2250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                &video_renderer_));
2260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ_WAIT(1, callback_count(), kMsCallbackWait);
2270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(video_capturer_.CaptureFrame());
2280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(1, NumFramesRendered());
2290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(WasRenderedResolution(format_qvga_));
2300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Now restart with vga.
2310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(capture_manager_.RestartVideoCapture(
2320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      &video_capturer_, format_qvga_, format_vga_,
2330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      cricket::CaptureManager::kForceRestart));
2340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(video_capturer_.CaptureFrame());
2350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(2, NumFramesRendered());
2360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(WasRenderedResolution(format_vga_));
237d156f12e9ee96258b7b79d0e3d8ddab6b4b87fbbwu@webrtc.org  EXPECT_TRUE(capture_manager_.StopVideoCapture(&video_capturer_,
238d156f12e9ee96258b7b79d0e3d8ddab6b4b87fbbwu@webrtc.org                                                format_vga_));
2390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
2400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST_F(CaptureManagerTest, TestRequestRestart) {
2420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(capture_manager_.StartVideoCapture(&video_capturer_,
2430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                 format_vga_));
2440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(capture_manager_.AddVideoRenderer(&video_capturer_,
2450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                                &video_renderer_));
2460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ_WAIT(1, callback_count(), kMsCallbackWait);
2470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(video_capturer_.CaptureFrame());
2480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(1, NumFramesRendered());
2490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(WasRenderedResolution(format_vga_));
2500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Now request restart with qvga.
2510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(capture_manager_.RestartVideoCapture(
2520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      &video_capturer_, format_vga_, format_qvga_,
2530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      cricket::CaptureManager::kRequestRestart));
2540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(video_capturer_.CaptureFrame());
2550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_EQ(2, NumFramesRendered());
2560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  EXPECT_TRUE(WasRenderedResolution(format_vga_));
257d156f12e9ee96258b7b79d0e3d8ddab6b4b87fbbwu@webrtc.org  EXPECT_TRUE(capture_manager_.StopVideoCapture(&video_capturer_,
258d156f12e9ee96258b7b79d0e3d8ddab6b4b87fbbwu@webrtc.org                                                format_qvga_));
2590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
260