1 /* 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 3 * 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 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include "webrtc/video_engine/vie_render_impl.h" 12 13 #include "webrtc/engine_configurations.h" 14 #include "webrtc/modules/video_render/include/video_render.h" 15 #include "webrtc/modules/video_render/include/video_render_defines.h" 16 #include "webrtc/system_wrappers/interface/logging.h" 17 #include "webrtc/video_engine/include/vie_errors.h" 18 #include "webrtc/video_engine/vie_capturer.h" 19 #include "webrtc/video_engine/vie_channel.h" 20 #include "webrtc/video_engine/vie_channel_manager.h" 21 #include "webrtc/video_engine/vie_defines.h" 22 #include "webrtc/video_engine/vie_frame_provider_base.h" 23 #include "webrtc/video_engine/vie_impl.h" 24 #include "webrtc/video_engine/vie_input_manager.h" 25 #include "webrtc/video_engine/vie_render_manager.h" 26 #include "webrtc/video_engine/vie_renderer.h" 27 #include "webrtc/video_engine/vie_shared_data.h" 28 29 namespace webrtc { 30 31 ViERender* ViERender::GetInterface(VideoEngine* video_engine) { 32 #ifdef WEBRTC_VIDEO_ENGINE_RENDER_API 33 if (!video_engine) { 34 return NULL; 35 } 36 VideoEngineImpl* vie_impl = static_cast<VideoEngineImpl*>(video_engine); 37 ViERenderImpl* vie_render_impl = vie_impl; 38 // Increase ref count. 39 (*vie_render_impl)++; 40 return vie_render_impl; 41 #else 42 return NULL; 43 #endif 44 } 45 46 int ViERenderImpl::Release() { 47 // Decrease ref count 48 (*this)--; 49 int32_t ref_count = GetCount(); 50 if (ref_count < 0) { 51 LOG(LS_ERROR) << "ViERender release too many times"; 52 return -1; 53 } 54 return ref_count; 55 } 56 57 ViERenderImpl::ViERenderImpl(ViESharedData* shared_data) 58 : shared_data_(shared_data) {} 59 60 ViERenderImpl::~ViERenderImpl() {} 61 62 int ViERenderImpl::RegisterVideoRenderModule( 63 VideoRender& render_module) { 64 LOG_F(LS_INFO); 65 if (shared_data_->render_manager()->RegisterVideoRenderModule( 66 &render_module) != 0) { 67 shared_data_->SetLastError(kViERenderUnknownError); 68 return -1; 69 } 70 return 0; 71 } 72 73 int ViERenderImpl::DeRegisterVideoRenderModule( 74 VideoRender& render_module) { 75 LOG_F(LS_INFO); 76 if (shared_data_->render_manager()->DeRegisterVideoRenderModule( 77 &render_module) != 0) { 78 // Error logging is done in ViERenderManager::DeRegisterVideoRenderModule. 79 shared_data_->SetLastError(kViERenderUnknownError); 80 return -1; 81 } 82 return 0; 83 } 84 85 int ViERenderImpl::AddRenderer(const int render_id, void* window, 86 const unsigned int z_order, const float left, 87 const float top, const float right, 88 const float bottom) { 89 LOG_F(LS_INFO) << "render_id: " << render_id << " z_order: " << z_order 90 << " left: " << left << " top: " << top << " right: " << right 91 << " bottom: " << bottom; 92 { 93 ViERenderManagerScoped rs(*(shared_data_->render_manager())); 94 if (rs.Renderer(render_id)) { 95 LOG(LS_ERROR) << "Renderer for render_id: " << render_id 96 << " already exists."; 97 shared_data_->SetLastError(kViERenderAlreadyExists); 98 return -1; 99 } 100 } 101 if (render_id >= kViEChannelIdBase && render_id <= kViEChannelIdMax) { 102 // This is a channel. 103 ViEChannelManagerScoped cm(*(shared_data_->channel_manager())); 104 ViEFrameProviderBase* frame_provider = cm.Channel(render_id); 105 if (!frame_provider) { 106 shared_data_->SetLastError(kViERenderInvalidRenderId); 107 return -1; 108 } 109 ViERenderer* renderer = shared_data_->render_manager()->AddRenderStream( 110 render_id, window, z_order, left, top, right, bottom); 111 if (!renderer) { 112 shared_data_->SetLastError(kViERenderUnknownError); 113 return -1; 114 } 115 return frame_provider->RegisterFrameCallback(render_id, renderer); 116 } else { 117 // Camera or file. 118 ViEInputManagerScoped is(*(shared_data_->input_manager())); 119 ViEFrameProviderBase* frame_provider = is.FrameProvider(render_id); 120 if (!frame_provider) { 121 shared_data_->SetLastError(kViERenderInvalidRenderId); 122 return -1; 123 } 124 ViERenderer* renderer = shared_data_->render_manager()->AddRenderStream( 125 render_id, window, z_order, left, top, right, bottom); 126 if (!renderer) { 127 shared_data_->SetLastError(kViERenderUnknownError); 128 return -1; 129 } 130 return frame_provider->RegisterFrameCallback(render_id, renderer); 131 } 132 } 133 134 int ViERenderImpl::RemoveRenderer(const int render_id) { 135 LOG_F(LS_INFO) << "render_id: " << render_id; 136 ViERenderer* renderer = NULL; 137 { 138 ViERenderManagerScoped rs(*(shared_data_->render_manager())); 139 renderer = rs.Renderer(render_id); 140 if (!renderer) { 141 shared_data_->SetLastError(kViERenderInvalidRenderId); 142 return -1; 143 } 144 // Leave the scope lock since we don't want to lock two managers 145 // simultanousely. 146 } 147 if (render_id >= kViEChannelIdBase && render_id <= kViEChannelIdMax) { 148 // This is a channel. 149 ViEChannelManagerScoped cm(*(shared_data_->channel_manager())); 150 ViEChannel* channel = cm.Channel(render_id); 151 if (!channel) { 152 shared_data_->SetLastError(kViERenderInvalidRenderId); 153 return -1; 154 } 155 channel->DeregisterFrameCallback(renderer); 156 } else { 157 // Provider owned by inputmanager, i.e. file or capture device. 158 ViEInputManagerScoped is(*(shared_data_->input_manager())); 159 ViEFrameProviderBase* provider = is.FrameProvider(render_id); 160 if (!provider) { 161 shared_data_->SetLastError(kViERenderInvalidRenderId); 162 return -1; 163 } 164 provider->DeregisterFrameCallback(renderer); 165 } 166 if (shared_data_->render_manager()->RemoveRenderStream(render_id) != 0) { 167 shared_data_->SetLastError(kViERenderUnknownError); 168 return -1; 169 } 170 return 0; 171 } 172 173 int ViERenderImpl::StartRender(const int render_id) { 174 LOG_F(LS_INFO) << "render_id: " << render_id; 175 ViERenderManagerScoped rs(*(shared_data_->render_manager())); 176 ViERenderer* renderer = rs.Renderer(render_id); 177 if (!renderer) { 178 shared_data_->SetLastError(kViERenderInvalidRenderId); 179 return -1; 180 } 181 if (renderer->StartRender() != 0) { 182 shared_data_->SetLastError(kViERenderUnknownError); 183 return -1; 184 } 185 return 0; 186 } 187 188 int ViERenderImpl::StopRender(const int render_id) { 189 LOG_F(LS_INFO) << "render_id: " << render_id; 190 ViERenderManagerScoped rs(*(shared_data_->render_manager())); 191 ViERenderer* renderer = rs.Renderer(render_id); 192 if (!renderer) { 193 shared_data_->SetLastError(kViERenderInvalidRenderId); 194 return -1; 195 } 196 if (renderer->StopRender() != 0) { 197 shared_data_->SetLastError(kViERenderUnknownError); 198 return -1; 199 } 200 return 0; 201 } 202 203 int ViERenderImpl::SetExpectedRenderDelay(int render_id, int render_delay) { 204 LOG_F(LS_INFO) << "render_id: " << render_id 205 << " render_delay: " << render_delay; 206 ViERenderManagerScoped rs(*(shared_data_->render_manager())); 207 ViERenderer* renderer = rs.Renderer(render_id); 208 if (!renderer) { 209 shared_data_->SetLastError(kViERenderInvalidRenderId); 210 return -1; 211 } 212 if (renderer->SetExpectedRenderDelay(render_delay) != 0) { 213 shared_data_->SetLastError(kViERenderUnknownError); 214 return -1; 215 } 216 return 0; 217 } 218 219 int ViERenderImpl::ConfigureRender(int render_id, const unsigned int z_order, 220 const float left, const float top, 221 const float right, const float bottom) { 222 LOG_F(LS_INFO) << "render_id: " << render_id << " z_order: " << z_order 223 << " left: " << left << " top: " << top << " right: " << right 224 << " bottom: " << bottom; 225 ViERenderManagerScoped rs(*(shared_data_->render_manager())); 226 ViERenderer* renderer = rs.Renderer(render_id); 227 if (!renderer) { 228 shared_data_->SetLastError(kViERenderInvalidRenderId); 229 return -1; 230 } 231 232 if (renderer->ConfigureRenderer(z_order, left, top, right, bottom) != 0) { 233 shared_data_->SetLastError(kViERenderUnknownError); 234 return -1; 235 } 236 return 0; 237 } 238 239 int ViERenderImpl::MirrorRenderStream(const int render_id, const bool enable, 240 const bool mirror_xaxis, 241 const bool mirror_yaxis) { 242 ViERenderManagerScoped rs(*(shared_data_->render_manager())); 243 ViERenderer* renderer = rs.Renderer(render_id); 244 if (!renderer) { 245 shared_data_->SetLastError(kViERenderInvalidRenderId); 246 return -1; 247 } 248 if (renderer->EnableMirroring(render_id, enable, mirror_xaxis, mirror_yaxis) 249 != 0) { 250 shared_data_->SetLastError(kViERenderUnknownError); 251 return -1; 252 } 253 return 0; 254 } 255 256 int ViERenderImpl::AddRenderer(const int render_id, 257 RawVideoType video_input_format, 258 ExternalRenderer* external_renderer) { 259 // Check if the client requested a format that we can convert the frames to. 260 if (video_input_format != kVideoI420 && 261 video_input_format != kVideoYV12 && 262 video_input_format != kVideoYUY2 && 263 video_input_format != kVideoUYVY && 264 video_input_format != kVideoARGB && 265 video_input_format != kVideoRGB24 && 266 video_input_format != kVideoRGB565 && 267 video_input_format != kVideoARGB4444 && 268 video_input_format != kVideoARGB1555) { 269 LOG(LS_ERROR) << "Unsupported video frame format requested."; 270 shared_data_->SetLastError(kViERenderInvalidFrameFormat); 271 return -1; 272 } 273 { 274 // Verify the renderer doesn't exist. 275 ViERenderManagerScoped rs(*(shared_data_->render_manager())); 276 if (rs.Renderer(render_id)) { 277 LOG_F(LS_ERROR) << "Renderer already exists for render_id: " << render_id; 278 shared_data_->SetLastError(kViERenderAlreadyExists); 279 return -1; 280 } 281 } 282 if (render_id >= kViEChannelIdBase && render_id <= kViEChannelIdMax) { 283 // This is a channel. 284 ViEChannelManagerScoped cm(*(shared_data_->channel_manager())); 285 ViEFrameProviderBase* frame_provider = cm.Channel(render_id); 286 if (!frame_provider) { 287 shared_data_->SetLastError(kViERenderInvalidRenderId); 288 return -1; 289 } 290 ViERenderer* renderer = shared_data_->render_manager()->AddRenderStream( 291 render_id, NULL, 0, 0.0f, 0.0f, 1.0f, 1.0f); 292 if (!renderer) { 293 shared_data_->SetLastError(kViERenderUnknownError); 294 return -1; 295 } 296 if (renderer->SetExternalRenderer(render_id, video_input_format, 297 external_renderer) == -1) { 298 shared_data_->SetLastError(kViERenderUnknownError); 299 return -1; 300 } 301 302 return frame_provider->RegisterFrameCallback(render_id, renderer); 303 } else { 304 // Camera or file. 305 ViEInputManagerScoped is(*(shared_data_->input_manager())); 306 ViEFrameProviderBase* frame_provider = is.FrameProvider(render_id); 307 if (!frame_provider) { 308 shared_data_->SetLastError(kViERenderInvalidRenderId); 309 return -1; 310 } 311 ViERenderer* renderer = shared_data_->render_manager()->AddRenderStream( 312 render_id, NULL, 0, 0.0f, 0.0f, 1.0f, 1.0f); 313 if (!renderer) { 314 shared_data_->SetLastError(kViERenderUnknownError); 315 return -1; 316 } 317 if (renderer->SetExternalRenderer(render_id, video_input_format, 318 external_renderer) == -1) { 319 shared_data_->SetLastError(kViERenderUnknownError); 320 return -1; 321 } 322 return frame_provider->RegisterFrameCallback(render_id, renderer); 323 } 324 } 325 326 int ViERenderImpl::AddRenderCallback(int render_id, 327 VideoRenderCallback* callback) { 328 if (render_id < kViEChannelIdBase || render_id > kViEChannelIdMax) 329 return -1; 330 // This is a channel. 331 ViEChannelManagerScoped cm(*(shared_data_->channel_manager())); 332 ViEFrameProviderBase* frame_provider = cm.Channel(render_id); 333 if (!frame_provider) { 334 shared_data_->SetLastError(kViERenderInvalidRenderId); 335 return -1; 336 } 337 ViERenderer* renderer = shared_data_->render_manager()->AddRenderStream( 338 render_id, NULL, 0, 0.0f, 0.0f, 1.0f, 1.0f); 339 if (!renderer) { 340 shared_data_->SetLastError(kViERenderUnknownError); 341 return -1; 342 } 343 if (renderer->SetVideoRenderCallback(render_id, callback) != 0) { 344 shared_data_->SetLastError(kViERenderUnknownError); 345 return -1; 346 } 347 348 return frame_provider->RegisterFrameCallback(render_id, renderer); 349 } 350 351 } // namespace webrtc 352