| 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 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include <sys/time.h> | 21 #include <sys/time.h> |
| 22 #endif | 22 #endif |
| 23 | 23 |
| 24 #include "webrtc/base/platform_thread.h" | 24 #include "webrtc/base/platform_thread.h" |
| 25 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" | 25 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
| 26 #include "webrtc/common_video/video_render_frames.h" | 26 #include "webrtc/common_video/video_render_frames.h" |
| 27 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" | 27 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
| 28 #include "webrtc/system_wrappers/include/event_wrapper.h" | 28 #include "webrtc/system_wrappers/include/event_wrapper.h" |
| 29 #include "webrtc/system_wrappers/include/tick_util.h" | 29 #include "webrtc/system_wrappers/include/tick_util.h" |
| 30 #include "webrtc/system_wrappers/include/trace.h" | 30 #include "webrtc/system_wrappers/include/trace.h" |
| 31 #include "webrtc/video_renderer.h" |
| 31 | 32 |
| 32 namespace webrtc { | 33 namespace webrtc { |
| 33 | 34 |
| 34 IncomingVideoStream::IncomingVideoStream(uint32_t stream_id) | 35 IncomingVideoStream::IncomingVideoStream(uint32_t stream_id, |
| 36 bool disable_prerenderer_smoothing) |
| 35 : stream_id_(stream_id), | 37 : stream_id_(stream_id), |
| 38 disable_prerenderer_smoothing_(disable_prerenderer_smoothing), |
| 36 stream_critsect_(CriticalSectionWrapper::CreateCriticalSection()), | 39 stream_critsect_(CriticalSectionWrapper::CreateCriticalSection()), |
| 37 thread_critsect_(CriticalSectionWrapper::CreateCriticalSection()), | 40 thread_critsect_(CriticalSectionWrapper::CreateCriticalSection()), |
| 38 buffer_critsect_(CriticalSectionWrapper::CreateCriticalSection()), | 41 buffer_critsect_(CriticalSectionWrapper::CreateCriticalSection()), |
| 39 incoming_render_thread_(), | 42 incoming_render_thread_(), |
| 40 deliver_buffer_event_(EventTimerWrapper::Create()), | 43 deliver_buffer_event_(EventTimerWrapper::Create()), |
| 41 running_(false), | 44 running_(false), |
| 42 external_callback_(nullptr), | 45 external_callback_(nullptr), |
| 43 render_callback_(nullptr), | 46 render_callback_(nullptr), |
| 44 render_buffers_(new VideoRenderFrames()), | 47 render_buffers_(new VideoRenderFrames()), |
| 45 incoming_rate_(0), | 48 incoming_rate_(0), |
| 46 last_rate_calculation_time_ms_(0), | 49 last_rate_calculation_time_ms_(0), |
| 47 num_frames_since_last_calculation_(0), | 50 num_frames_since_last_calculation_(0), |
| 48 last_render_time_ms_(0), | 51 last_render_time_ms_(0), |
| 49 temp_frame_(), | 52 temp_frame_(), |
| 50 start_image_(), | 53 start_image_(), |
| 51 timeout_image_(), | 54 timeout_image_(), |
| 52 timeout_time_() { | 55 timeout_time_() {} |
| 53 } | |
| 54 | 56 |
| 55 IncomingVideoStream::~IncomingVideoStream() { | 57 IncomingVideoStream::~IncomingVideoStream() { |
| 56 Stop(); | 58 Stop(); |
| 57 } | 59 } |
| 58 | 60 |
| 59 VideoRenderCallback* IncomingVideoStream::ModuleCallback() { | 61 VideoRenderCallback* IncomingVideoStream::ModuleCallback() { |
| 60 CriticalSectionScoped cs(stream_critsect_.get()); | 62 CriticalSectionScoped cs(stream_critsect_.get()); |
| 61 return this; | 63 return this; |
| 62 } | 64 } |
| 63 | 65 |
| 64 int32_t IncomingVideoStream::RenderFrame(const uint32_t stream_id, | 66 int32_t IncomingVideoStream::RenderFrame(const uint32_t stream_id, |
| 65 const VideoFrame& video_frame) { | 67 const VideoFrame& video_frame) { |
| 66 CriticalSectionScoped csS(stream_critsect_.get()); | 68 CriticalSectionScoped csS(stream_critsect_.get()); |
| 67 | 69 |
| 68 if (!running_) { | 70 if (!running_) { |
| 69 return -1; | 71 return -1; |
| 70 } | 72 } |
| 71 | 73 |
| 72 // Rate statistics. | 74 // Rate statistics. |
| 73 num_frames_since_last_calculation_++; | 75 num_frames_since_last_calculation_++; |
| 74 int64_t now_ms = TickTime::MillisecondTimestamp(); | 76 int64_t now_ms = TickTime::MillisecondTimestamp(); |
| 75 if (now_ms >= last_rate_calculation_time_ms_ + kFrameRatePeriodMs) { | 77 if (now_ms >= last_rate_calculation_time_ms_ + kFrameRatePeriodMs) { |
| 76 incoming_rate_ = | 78 incoming_rate_ = |
| 77 static_cast<uint32_t>(1000 * num_frames_since_last_calculation_ / | 79 static_cast<uint32_t>(1000 * num_frames_since_last_calculation_ / |
| 78 (now_ms - last_rate_calculation_time_ms_)); | 80 (now_ms - last_rate_calculation_time_ms_)); |
| 79 num_frames_since_last_calculation_ = 0; | 81 num_frames_since_last_calculation_ = 0; |
| 80 last_rate_calculation_time_ms_ = now_ms; | 82 last_rate_calculation_time_ms_ = now_ms; |
| 81 } | 83 } |
| 82 | 84 |
| 83 // Insert frame. | 85 // Hand over or insert frame. |
| 84 CriticalSectionScoped csB(buffer_critsect_.get()); | 86 if (disable_prerenderer_smoothing_) { |
| 85 if (render_buffers_->AddFrame(video_frame) == 1) | 87 DeliverFrame(video_frame); |
| 86 deliver_buffer_event_->Set(); | 88 } else { |
| 87 | 89 CriticalSectionScoped csB(buffer_critsect_.get()); |
| 90 if (render_buffers_->AddFrame(video_frame) == 1) { |
| 91 deliver_buffer_event_->Set(); |
| 92 } |
| 93 } |
| 88 return 0; | 94 return 0; |
| 89 } | 95 } |
| 90 | 96 |
| 91 int32_t IncomingVideoStream::SetStartImage(const VideoFrame& video_frame) { | 97 int32_t IncomingVideoStream::SetStartImage(const VideoFrame& video_frame) { |
| 92 CriticalSectionScoped csS(thread_critsect_.get()); | 98 CriticalSectionScoped csS(thread_critsect_.get()); |
| 93 return start_image_.CopyFrame(video_frame); | 99 return start_image_.CopyFrame(video_frame); |
| 94 } | 100 } |
| 95 | 101 |
| 96 int32_t IncomingVideoStream::SetTimeoutImage(const VideoFrame& video_frame, | 102 int32_t IncomingVideoStream::SetTimeoutImage(const VideoFrame& video_frame, |
| 97 const uint32_t timeout) { | 103 const uint32_t timeout) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 121 CriticalSectionScoped cs(thread_critsect_.get()); | 127 CriticalSectionScoped cs(thread_critsect_.get()); |
| 122 external_callback_ = external_callback; | 128 external_callback_ = external_callback; |
| 123 } | 129 } |
| 124 | 130 |
| 125 int32_t IncomingVideoStream::Start() { | 131 int32_t IncomingVideoStream::Start() { |
| 126 CriticalSectionScoped csS(stream_critsect_.get()); | 132 CriticalSectionScoped csS(stream_critsect_.get()); |
| 127 if (running_) { | 133 if (running_) { |
| 128 return 0; | 134 return 0; |
| 129 } | 135 } |
| 130 | 136 |
| 131 CriticalSectionScoped csT(thread_critsect_.get()); | 137 if (!disable_prerenderer_smoothing_) { |
| 132 assert(incoming_render_thread_ == NULL); | 138 CriticalSectionScoped csT(thread_critsect_.get()); |
| 139 assert(incoming_render_thread_ == NULL); |
| 133 | 140 |
| 134 incoming_render_thread_ = PlatformThread::CreateThread( | 141 incoming_render_thread_ = PlatformThread::CreateThread( |
| 135 IncomingVideoStreamThreadFun, this, "IncomingVideoStreamThread"); | 142 IncomingVideoStreamThreadFun, this, "IncomingVideoStreamThread"); |
| 136 if (!incoming_render_thread_) { | 143 if (!incoming_render_thread_) { |
| 137 return -1; | 144 return -1; |
| 145 } |
| 146 |
| 147 if (incoming_render_thread_->Start()) { |
| 148 } else { |
| 149 return -1; |
| 150 } |
| 151 incoming_render_thread_->SetPriority(kRealtimePriority); |
| 152 deliver_buffer_event_->StartTimer(false, kEventStartupTimeMs); |
| 138 } | 153 } |
| 139 | |
| 140 if (incoming_render_thread_->Start()) { | |
| 141 } else { | |
| 142 return -1; | |
| 143 } | |
| 144 incoming_render_thread_->SetPriority(kRealtimePriority); | |
| 145 deliver_buffer_event_->StartTimer(false, kEventStartupTimeMs); | |
| 146 | |
| 147 running_ = true; | 154 running_ = true; |
| 148 return 0; | 155 return 0; |
| 149 } | 156 } |
| 150 | 157 |
| 151 int32_t IncomingVideoStream::Stop() { | 158 int32_t IncomingVideoStream::Stop() { |
| 152 CriticalSectionScoped cs_stream(stream_critsect_.get()); | 159 CriticalSectionScoped cs_stream(stream_critsect_.get()); |
| 153 | 160 |
| 154 if (!running_) { | 161 if (!running_) { |
| 155 return 0; | 162 return 0; |
| 156 } | 163 } |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 return static_cast<IncomingVideoStream*>(obj)->IncomingVideoStreamProcess(); | 205 return static_cast<IncomingVideoStream*>(obj)->IncomingVideoStreamProcess(); |
| 199 } | 206 } |
| 200 | 207 |
| 201 bool IncomingVideoStream::IncomingVideoStreamProcess() { | 208 bool IncomingVideoStream::IncomingVideoStreamProcess() { |
| 202 if (kEventError != deliver_buffer_event_->Wait(kEventMaxWaitTimeMs)) { | 209 if (kEventError != deliver_buffer_event_->Wait(kEventMaxWaitTimeMs)) { |
| 203 CriticalSectionScoped cs(thread_critsect_.get()); | 210 CriticalSectionScoped cs(thread_critsect_.get()); |
| 204 if (incoming_render_thread_ == NULL) { | 211 if (incoming_render_thread_ == NULL) { |
| 205 // Terminating | 212 // Terminating |
| 206 return false; | 213 return false; |
| 207 } | 214 } |
| 215 |
| 208 // Get a new frame to render and the time for the frame after this one. | 216 // Get a new frame to render and the time for the frame after this one. |
| 209 VideoFrame frame_to_render; | 217 VideoFrame frame_to_render; |
| 210 uint32_t wait_time; | 218 uint32_t wait_time; |
| 211 { | 219 { |
| 212 CriticalSectionScoped cs(buffer_critsect_.get()); | 220 CriticalSectionScoped cs(buffer_critsect_.get()); |
| 213 frame_to_render = render_buffers_->FrameToRender(); | 221 frame_to_render = render_buffers_->FrameToRender(); |
| 214 wait_time = render_buffers_->TimeToNextFrameRelease(); | 222 wait_time = render_buffers_->TimeToNextFrameRelease(); |
| 215 } | 223 } |
| 216 | 224 |
| 217 // Set timer for next frame to render. | 225 // Set timer for next frame to render. |
| 218 if (wait_time > kEventMaxWaitTimeMs) { | 226 if (wait_time > kEventMaxWaitTimeMs) { |
| 219 wait_time = kEventMaxWaitTimeMs; | 227 wait_time = kEventMaxWaitTimeMs; |
| 220 } | 228 } |
| 221 deliver_buffer_event_->StartTimer(false, wait_time); | 229 deliver_buffer_event_->StartTimer(false, wait_time); |
| 222 | 230 |
| 223 if (frame_to_render.IsZeroSize()) { | 231 DeliverFrame(frame_to_render); |
| 224 if (render_callback_) { | |
| 225 if (last_render_time_ms_ == 0 && !start_image_.IsZeroSize()) { | |
| 226 // We have not rendered anything and have a start image. | |
| 227 temp_frame_.CopyFrame(start_image_); | |
| 228 render_callback_->RenderFrame(stream_id_, temp_frame_); | |
| 229 } else if (!timeout_image_.IsZeroSize() && | |
| 230 last_render_time_ms_ + timeout_time_ < | |
| 231 TickTime::MillisecondTimestamp()) { | |
| 232 // Render a timeout image. | |
| 233 temp_frame_.CopyFrame(timeout_image_); | |
| 234 render_callback_->RenderFrame(stream_id_, temp_frame_); | |
| 235 } | |
| 236 } | |
| 237 | |
| 238 // No frame. | |
| 239 return true; | |
| 240 } | |
| 241 | |
| 242 // Send frame for rendering. | |
| 243 if (external_callback_) { | |
| 244 external_callback_->RenderFrame(stream_id_, frame_to_render); | |
| 245 } else if (render_callback_) { | |
| 246 render_callback_->RenderFrame(stream_id_, frame_to_render); | |
| 247 } | |
| 248 | |
| 249 // We're done with this frame. | |
| 250 if (!frame_to_render.IsZeroSize()) | |
| 251 last_render_time_ms_ = frame_to_render.render_time_ms(); | |
| 252 } | 232 } |
| 253 return true; | 233 return true; |
| 254 } | 234 } |
| 255 | 235 |
| 236 void IncomingVideoStream::DeliverFrame(const VideoFrame& video_frame) { |
| 237 CriticalSectionScoped cs(thread_critsect_.get()); |
| 238 if (video_frame.IsZeroSize()) { |
| 239 if (render_callback_) { |
| 240 if (last_render_time_ms_ == 0 && !start_image_.IsZeroSize()) { |
| 241 // We have not rendered anything and have a start image. |
| 242 temp_frame_.CopyFrame(start_image_); |
| 243 render_callback_->RenderFrame(stream_id_, temp_frame_); |
| 244 } else if (!timeout_image_.IsZeroSize() && |
| 245 last_render_time_ms_ + timeout_time_ < |
| 246 TickTime::MillisecondTimestamp()) { |
| 247 // Render a timeout image. |
| 248 temp_frame_.CopyFrame(timeout_image_); |
| 249 render_callback_->RenderFrame(stream_id_, temp_frame_); |
| 250 } |
| 251 } |
| 252 |
| 253 // No frame. |
| 254 return; |
| 255 } |
| 256 |
| 257 // Send frame for rendering. |
| 258 if (external_callback_) { |
| 259 external_callback_->RenderFrame(stream_id_, video_frame); |
| 260 } else if (render_callback_) { |
| 261 render_callback_->RenderFrame(stream_id_, video_frame); |
| 262 } |
| 263 |
| 264 // We're done with this frame. |
| 265 last_render_time_ms_ = video_frame.render_time_ms(); |
| 266 } |
| 267 |
| 256 } // namespace webrtc | 268 } // namespace webrtc |
| OLD | NEW |