OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 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 "webrtc/video/video_capture_input.h" | 11 #include "webrtc/video/video_capture_input.h" |
12 | 12 |
13 #include "webrtc/base/checks.h" | 13 #include "webrtc/base/checks.h" |
14 #include "webrtc/base/logging.h" | 14 #include "webrtc/base/logging.h" |
15 #include "webrtc/base/trace_event.h" | 15 #include "webrtc/base/trace_event.h" |
16 #include "webrtc/modules/include/module_common_types.h" | 16 #include "webrtc/modules/include/module_common_types.h" |
17 #include "webrtc/modules/video_capture/video_capture_factory.h" | 17 #include "webrtc/modules/video_capture/video_capture_factory.h" |
18 #include "webrtc/modules/video_processing/include/video_processing.h" | 18 #include "webrtc/modules/video_processing/include/video_processing.h" |
19 #include "webrtc/modules/video_render/video_render_defines.h" | 19 #include "webrtc/modules/video_render/video_render_defines.h" |
20 #include "webrtc/video/overuse_frame_detector.h" | 20 #include "webrtc/video/overuse_frame_detector.h" |
21 #include "webrtc/video/send_statistics_proxy.h" | 21 #include "webrtc/video/send_statistics_proxy.h" |
22 #include "webrtc/video/vie_encoder.h" | 22 #include "webrtc/video/vie_encoder.h" |
23 | 23 |
24 namespace webrtc { | 24 namespace webrtc { |
25 | 25 |
26 namespace internal { | 26 namespace internal { |
27 VideoCaptureInput::VideoCaptureInput(VideoCaptureCallback* frame_callback, | 27 VideoCaptureInput::VideoCaptureInput(rtc::Event* capture_event, |
28 VideoRenderer* local_renderer, | 28 VideoRenderer* local_renderer, |
29 SendStatisticsProxy* stats_proxy, | 29 SendStatisticsProxy* stats_proxy, |
30 OveruseFrameDetector* overuse_detector) | 30 OveruseFrameDetector* overuse_detector) |
31 : frame_callback_(frame_callback), | 31 : local_renderer_(local_renderer), |
32 local_renderer_(local_renderer), | |
33 stats_proxy_(stats_proxy), | 32 stats_proxy_(stats_proxy), |
34 encoder_thread_(EncoderThreadFunction, this, "EncoderThread"), | 33 capture_event_(capture_event), |
35 capture_event_(false, false), | |
36 stop_(0), | |
37 // TODO(danilchap): Pass clock from outside to ensure it is same clock | 34 // TODO(danilchap): Pass clock from outside to ensure it is same clock |
38 // rtcp module use to calculate offset since last frame captured | 35 // rtcp module use to calculate offset since last frame captured |
39 // to estimate rtp timestamp for SenderReport. | 36 // to estimate rtp timestamp for SenderReport. |
40 clock_(Clock::GetRealTimeClock()), | 37 clock_(Clock::GetRealTimeClock()), |
41 last_captured_timestamp_(0), | 38 last_captured_timestamp_(0), |
42 delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - | 39 delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - |
43 clock_->TimeInMilliseconds()), | 40 clock_->TimeInMilliseconds()), |
44 overuse_detector_(overuse_detector) { | 41 overuse_detector_(overuse_detector) {} |
45 encoder_thread_.Start(); | |
46 encoder_thread_.SetPriority(rtc::kHighPriority); | |
47 } | |
48 | 42 |
49 VideoCaptureInput::~VideoCaptureInput() { | 43 VideoCaptureInput::~VideoCaptureInput() { |
50 // Stop the thread. | |
51 rtc::AtomicOps::ReleaseStore(&stop_, 1); | |
52 capture_event_.Set(); | |
53 encoder_thread_.Stop(); | |
54 } | 44 } |
55 | 45 |
56 void VideoCaptureInput::IncomingCapturedFrame(const VideoFrame& video_frame) { | 46 void VideoCaptureInput::IncomingCapturedFrame(const VideoFrame& video_frame) { |
57 // TODO(pbos): Remove local rendering, it should be handled by the client code | 47 // TODO(pbos): Remove local rendering, it should be handled by the client code |
58 // if required. | 48 // if required. |
59 if (local_renderer_) | 49 if (local_renderer_) |
60 local_renderer_->RenderFrame(video_frame, 0); | 50 local_renderer_->RenderFrame(video_frame, 0); |
61 | 51 |
62 stats_proxy_->OnIncomingFrame(video_frame.width(), video_frame.height()); | 52 stats_proxy_->OnIncomingFrame(video_frame.width(), video_frame.height()); |
63 | 53 |
(...skipping 30 matching lines...) Expand all Loading... |
94 } | 84 } |
95 | 85 |
96 captured_frame_.ShallowCopy(incoming_frame); | 86 captured_frame_.ShallowCopy(incoming_frame); |
97 last_captured_timestamp_ = incoming_frame.ntp_time_ms(); | 87 last_captured_timestamp_ = incoming_frame.ntp_time_ms(); |
98 | 88 |
99 overuse_detector_->FrameCaptured(captured_frame_); | 89 overuse_detector_->FrameCaptured(captured_frame_); |
100 | 90 |
101 TRACE_EVENT_ASYNC_BEGIN1("webrtc", "Video", video_frame.render_time_ms(), | 91 TRACE_EVENT_ASYNC_BEGIN1("webrtc", "Video", video_frame.render_time_ms(), |
102 "render_time", video_frame.render_time_ms()); | 92 "render_time", video_frame.render_time_ms()); |
103 | 93 |
104 capture_event_.Set(); | 94 capture_event_->Set(); |
105 } | 95 } |
106 | 96 |
107 bool VideoCaptureInput::EncoderThreadFunction(void* obj) { | 97 bool VideoCaptureInput::GetVideoFrame(VideoFrame* video_frame) { |
108 return static_cast<VideoCaptureInput*>(obj)->EncoderProcess(); | 98 rtc::CritScope lock(&crit_); |
109 } | 99 if (captured_frame_.IsZeroSize()) |
| 100 return false; |
110 | 101 |
111 bool VideoCaptureInput::EncoderProcess() { | 102 *video_frame = captured_frame_; |
112 static const int kThreadWaitTimeMs = 100; | 103 captured_frame_.Reset(); |
113 if (capture_event_.Wait(kThreadWaitTimeMs)) { | |
114 if (rtc::AtomicOps::AcquireLoad(&stop_)) | |
115 return false; | |
116 | |
117 VideoFrame deliver_frame; | |
118 { | |
119 rtc::CritScope lock(&crit_); | |
120 if (!captured_frame_.IsZeroSize()) { | |
121 deliver_frame = captured_frame_; | |
122 captured_frame_.Reset(); | |
123 } | |
124 } | |
125 if (!deliver_frame.IsZeroSize()) { | |
126 frame_callback_->DeliverFrame(deliver_frame); | |
127 } | |
128 } | |
129 return true; | 104 return true; |
130 } | 105 } |
131 | 106 |
132 } // namespace internal | 107 } // namespace internal |
133 } // namespace webrtc | 108 } // namespace webrtc |
OLD | NEW |