OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2008 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2008 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include <stdio.h> | 11 #include <stdio.h> |
12 | 12 |
13 #include <memory> | 13 #include <memory> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 #include "webrtc/base/gunit.h" | 16 #include "webrtc/base/gunit.h" |
17 #include "webrtc/base/logging.h" | 17 #include "webrtc/base/logging.h" |
| 18 #include "webrtc/base/random.h" |
18 #include "webrtc/base/thread.h" | 19 #include "webrtc/base/thread.h" |
19 #include "webrtc/media/base/fakevideocapturer.h" | 20 #include "webrtc/media/base/fakevideocapturer.h" |
20 #include "webrtc/media/base/fakevideorenderer.h" | 21 #include "webrtc/media/base/fakevideorenderer.h" |
21 #include "webrtc/media/base/testutils.h" | 22 #include "webrtc/media/base/testutils.h" |
22 #include "webrtc/media/base/videocapturer.h" | 23 #include "webrtc/media/base/videocapturer.h" |
23 | 24 |
24 using cricket::FakeVideoCapturer; | 25 using cricket::FakeVideoCapturer; |
25 | 26 |
26 namespace { | 27 namespace { |
27 | 28 |
(...skipping 746 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
774 capturer_->set_enable_camera_list(true); | 775 capturer_->set_enable_camera_list(true); |
775 capturer_->ConstrainSupportedFormats(vga_format); | 776 capturer_->ConstrainSupportedFormats(vga_format); |
776 EXPECT_EQ(2u, capturer_->GetSupportedFormats()->size()); | 777 EXPECT_EQ(2u, capturer_->GetSupportedFormats()->size()); |
777 // To make sure it's not just the camera list being broken, add in VGA and | 778 // To make sure it's not just the camera list being broken, add in VGA and |
778 // try again. This time, only the VGA format should be there. | 779 // try again. This time, only the VGA format should be there. |
779 supported_formats.push_back(vga_format); | 780 supported_formats.push_back(vga_format); |
780 capturer_->ResetSupportedFormats(supported_formats); | 781 capturer_->ResetSupportedFormats(supported_formats); |
781 ASSERT_EQ(1u, capturer_->GetSupportedFormats()->size()); | 782 ASSERT_EQ(1u, capturer_->GetSupportedFormats()->size()); |
782 EXPECT_EQ(vga_format.height, capturer_->GetSupportedFormats()->at(0).height); | 783 EXPECT_EQ(vga_format.height, capturer_->GetSupportedFormats()->at(0).height); |
783 } | 784 } |
| 785 |
| 786 TEST_F(VideoCapturerTest, AttenuateTimestampJitter) { |
| 787 const int kWidth = 800; |
| 788 const int kHeight = 400; |
| 789 |
| 790 const float rel_freq_error = 0.001; |
| 791 const int64_t epoch = 10000; |
| 792 const int64_t jitter_us = 5000; |
| 793 const int64_t interval_us = 33333; // 30 FPS |
| 794 const int64_t interval_error_us = interval_us * rel_freq_error; |
| 795 const int nframes = 200; |
| 796 |
| 797 const int64_t system_start_us = rtc::TimeMicros(); |
| 798 webrtc::Random random(17); |
| 799 |
| 800 for (int i = 0; i < nframes; i++) { |
| 801 // Camera time subject to drift. |
| 802 int64_t camera_time_us = epoch + i * (interval_us + interval_error_us); |
| 803 int64_t system_time_us = system_start_us + i * interval_us; |
| 804 // And system time readings are subject to jitter. |
| 805 int64_t system_measured_us = system_time_us + random.Rand(jitter_us); |
| 806 |
| 807 int out_width; |
| 808 int out_height; |
| 809 int crop_width; |
| 810 int crop_height; |
| 811 int crop_x; |
| 812 int crop_y; |
| 813 int64_t translated_time_us; |
| 814 |
| 815 EXPECT_TRUE(capturer_->AdaptFrame(kWidth, kHeight, |
| 816 camera_time_us, system_measured_us, |
| 817 &out_width, &out_height, |
| 818 &crop_width, &crop_height, |
| 819 &crop_x, &crop_y, &translated_time_us)); |
| 820 |
| 821 // The relative frequency error contributes to the expected error |
| 822 // by a factor which is the difference between the current time |
| 823 // and the average of earlier sample times. This expression is |
| 824 // accurate as long as we do plain averaging (i.e, for the first |
| 825 // 100 frames), after which it converges exponentially to the |
| 826 // limit of interval_us * (window_size - 1). |
| 827 int64_t expected_error_us = jitter_us / 2 + |
| 828 rel_freq_error * i * interval_us / 2; |
| 829 |
| 830 if (i == 0) { |
| 831 EXPECT_EQ(translated_time_us, system_measured_us); |
| 832 } else { |
| 833 EXPECT_NEAR(translated_time_us, system_time_us + expected_error_us, |
| 834 2.0 * jitter_us / sqrt(std::max(i, 100))); |
| 835 } |
| 836 } |
| 837 } |
| 838 |
| 839 TEST_F(VideoCapturerTest, TimestampTranslationBypass) { |
| 840 const int kWidth = 800; |
| 841 const int kHeight = 400; |
| 842 |
| 843 const int64_t jitter_us = 10000; |
| 844 const int64_t interval_us = 33333; // 30 FPS |
| 845 const int nframes = 50; |
| 846 |
| 847 const int64_t system_start_us = rtc::TimeMicros(); |
| 848 webrtc::Random random(17); |
| 849 |
| 850 for (int i = 0; i < nframes; i++) { |
| 851 // Camera time and system time are the same, but we read the |
| 852 // system time a few ms later. |
| 853 int64_t camera_time_us = system_start_us + i * interval_us; |
| 854 int64_t system_measured_us = camera_time_us + random.Rand(jitter_us); |
| 855 |
| 856 int out_width; |
| 857 int out_height; |
| 858 int crop_width; |
| 859 int crop_height; |
| 860 int crop_x; |
| 861 int crop_y; |
| 862 int64_t translated_time_us; |
| 863 |
| 864 EXPECT_TRUE(capturer_->AdaptFrame(kWidth, kHeight, |
| 865 camera_time_us, system_measured_us, |
| 866 &out_width, &out_height, |
| 867 &crop_width, &crop_height, |
| 868 &crop_x, &crop_y, &translated_time_us)); |
| 869 EXPECT_EQ(camera_time_us, translated_time_us); |
| 870 } |
| 871 } |
OLD | NEW |