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/modules/video_render/linux/video_render_linux_impl.h"
12 
13 #include "webrtc/modules/video_render/linux/video_x11_render.h"
14 #include "webrtc/system_wrappers/include/critical_section_wrapper.h"
15 #include "webrtc/system_wrappers/include/trace.h"
16 
17 #include <X11/Xlib.h>
18 
19 namespace webrtc {
20 
VideoRenderLinuxImpl(const int32_t id,const VideoRenderType videoRenderType,void * window,const bool fullscreen)21 VideoRenderLinuxImpl::VideoRenderLinuxImpl(
22                                            const int32_t id,
23                                            const VideoRenderType videoRenderType,
24                                            void* window, const bool fullscreen) :
25             _id(id),
26             _renderLinuxCritsect(
27                                  *CriticalSectionWrapper::CreateCriticalSection()),
28             _ptrWindow(window), _ptrX11Render(NULL)
29 {
30 }
31 
~VideoRenderLinuxImpl()32 VideoRenderLinuxImpl::~VideoRenderLinuxImpl()
33 {
34     if (_ptrX11Render)
35         delete _ptrX11Render;
36 
37     delete &_renderLinuxCritsect;
38 }
39 
Init()40 int32_t VideoRenderLinuxImpl::Init()
41 {
42     WEBRTC_TRACE(kTraceInfo, kTraceVideoRenderer, _id, "%s",
43                  __FUNCTION__);
44 
45     CriticalSectionScoped cs(&_renderLinuxCritsect);
46     _ptrX11Render = new VideoX11Render((Window) _ptrWindow);
47     if (!_ptrX11Render)
48     {
49         WEBRTC_TRACE(kTraceError, kTraceVideoRenderer, _id,
50                      "%s",
51                      "Failed to create instance of VideoX11Render object");
52         return -1;
53     }
54     int retVal = _ptrX11Render->Init();
55     if (retVal == -1)
56     {
57         return -1;
58     }
59 
60     return 0;
61 
62 }
63 
ChangeWindow(void * window)64 int32_t VideoRenderLinuxImpl::ChangeWindow(void* window)
65 {
66     WEBRTC_TRACE(kTraceInfo, kTraceVideoRenderer, _id, "%s",
67                  __FUNCTION__);
68 
69     CriticalSectionScoped cs(&_renderLinuxCritsect);
70     _ptrWindow = window;
71 
72     if (_ptrX11Render)
73     {
74         return _ptrX11Render->ChangeWindow((Window) window);
75     }
76 
77     return -1;
78 }
79 
AddIncomingRenderStream(const uint32_t streamId,const uint32_t zOrder,const float left,const float top,const float right,const float bottom)80 VideoRenderCallback* VideoRenderLinuxImpl::AddIncomingRenderStream(
81                                                                        const uint32_t streamId,
82                                                                        const uint32_t zOrder,
83                                                                        const float left,
84                                                                        const float top,
85                                                                        const float right,
86                                                                        const float bottom)
87 {
88     WEBRTC_TRACE(kTraceInfo, kTraceVideoRenderer, _id, "%s",
89                  __FUNCTION__);
90     CriticalSectionScoped cs(&_renderLinuxCritsect);
91 
92     VideoRenderCallback* renderCallback = NULL;
93     if (_ptrX11Render)
94     {
95         VideoX11Channel* renderChannel =
96                 _ptrX11Render->CreateX11RenderChannel(streamId, zOrder, left,
97                                                       top, right, bottom);
98         if (!renderChannel)
99         {
100             WEBRTC_TRACE(kTraceError, kTraceVideoRenderer, _id,
101                          "Render channel creation failed for stream id: %d",
102                          streamId);
103             return NULL;
104         }
105         renderCallback = (VideoRenderCallback *) renderChannel;
106     }
107     else
108     {
109         WEBRTC_TRACE(kTraceError, kTraceVideoRenderer, _id,
110                      "_ptrX11Render is NULL");
111         return NULL;
112     }
113     return renderCallback;
114 }
115 
DeleteIncomingRenderStream(const uint32_t streamId)116 int32_t VideoRenderLinuxImpl::DeleteIncomingRenderStream(
117                                                                const uint32_t streamId)
118 {
119     WEBRTC_TRACE(kTraceInfo, kTraceVideoRenderer, _id, "%s",
120                  __FUNCTION__);
121     CriticalSectionScoped cs(&_renderLinuxCritsect);
122 
123     if (_ptrX11Render)
124     {
125         return _ptrX11Render->DeleteX11RenderChannel(streamId);
126     }
127     return -1;
128 }
129 
GetIncomingRenderStreamProperties(const uint32_t streamId,uint32_t & zOrder,float & left,float & top,float & right,float & bottom) const130 int32_t VideoRenderLinuxImpl::GetIncomingRenderStreamProperties(
131                                                                       const uint32_t streamId,
132                                                                       uint32_t& zOrder,
133                                                                       float& left,
134                                                                       float& top,
135                                                                       float& right,
136                                                                       float& bottom) const
137 {
138     WEBRTC_TRACE(kTraceInfo, kTraceVideoRenderer, _id, "%s",
139                  __FUNCTION__);
140     CriticalSectionScoped cs(&_renderLinuxCritsect);
141 
142     if (_ptrX11Render)
143     {
144         return _ptrX11Render->GetIncomingStreamProperties(streamId, zOrder,
145                                                           left, top, right,
146                                                           bottom);
147     }
148     return -1;
149 }
150 
StartRender()151 int32_t VideoRenderLinuxImpl::StartRender()
152 {
153     WEBRTC_TRACE(kTraceInfo, kTraceVideoRenderer, _id, "%s",
154                  __FUNCTION__);
155     return 0;
156 }
157 
StopRender()158 int32_t VideoRenderLinuxImpl::StopRender()
159 {
160     WEBRTC_TRACE(kTraceInfo, kTraceVideoRenderer, _id, "%s",
161                  __FUNCTION__);
162     return 0;
163 }
164 
RenderType()165 VideoRenderType VideoRenderLinuxImpl::RenderType()
166 {
167     return kRenderX11;
168 }
169 
PerferedVideoType()170 RawVideoType VideoRenderLinuxImpl::PerferedVideoType()
171 {
172     return kVideoI420;
173 }
174 
FullScreen()175 bool VideoRenderLinuxImpl::FullScreen()
176 {
177     return false;
178 }
179 
GetGraphicsMemory(uint64_t &,uint64_t &) const180 int32_t VideoRenderLinuxImpl::GetGraphicsMemory(
181                                                       uint64_t& /*totalGraphicsMemory*/,
182                                                       uint64_t& /*availableGraphicsMemory*/) const
183 {
184     WEBRTC_TRACE(kTraceError, kTraceVideoRenderer, _id,
185                  "%s - not supported on Linux", __FUNCTION__);
186     return -1;
187 }
188 
GetScreenResolution(uint32_t &,uint32_t &) const189 int32_t VideoRenderLinuxImpl::GetScreenResolution(
190                                                         uint32_t& /*screenWidth*/,
191                                                         uint32_t& /*screenHeight*/) const
192 {
193     return -1;
194 }
195 
RenderFrameRate(const uint32_t)196 uint32_t VideoRenderLinuxImpl::RenderFrameRate(const uint32_t /*streamId*/)
197 {
198     return -1;
199 }
200 
SetStreamCropping(const uint32_t,const float,const float,const float,const float)201 int32_t VideoRenderLinuxImpl::SetStreamCropping(
202                                                       const uint32_t /*streamId*/,
203                                                       const float /*left*/,
204                                                       const float /*top*/,
205                                                       const float /*right*/,
206                                                       const float /*bottom*/)
207 {
208     WEBRTC_TRACE(kTraceError, kTraceVideoRenderer, _id,
209                  "%s - not supported on Linux", __FUNCTION__);
210     return -1;
211 }
212 
SetTransparentBackground(const bool)213 int32_t VideoRenderLinuxImpl::SetTransparentBackground(const bool /*enable*/)
214 {
215     WEBRTC_TRACE(kTraceError, kTraceVideoRenderer, _id,
216                  "%s - not supported on Linux", __FUNCTION__);
217     return -1;
218 }
219 
ConfigureRenderer(const uint32_t streamId,const unsigned int zOrder,const float left,const float top,const float right,const float bottom)220 int32_t VideoRenderLinuxImpl::ConfigureRenderer(
221                                                       const uint32_t streamId,
222                                                       const unsigned int zOrder,
223                                                       const float left,
224                                                       const float top,
225                                                       const float right,
226                                                       const float bottom)
227 {
228     WEBRTC_TRACE(kTraceError, kTraceVideoRenderer, _id,
229                  "%s - not supported on Linux", __FUNCTION__);
230     return -1;
231 }
232 
SetText(const uint8_t textId,const uint8_t * text,const int32_t textLength,const uint32_t textColorRef,const uint32_t backgroundColorRef,const float left,const float top,const float rigth,const float bottom)233 int32_t VideoRenderLinuxImpl::SetText(
234                                             const uint8_t textId,
235                                             const uint8_t* text,
236                                             const int32_t textLength,
237                                             const uint32_t textColorRef,
238                                             const uint32_t backgroundColorRef,
239                                             const float left, const float top,
240                                             const float rigth,
241                                             const float bottom)
242 {
243     WEBRTC_TRACE(kTraceError, kTraceVideoRenderer, _id,
244                  "%s - not supported on Linux", __FUNCTION__);
245     return -1;
246 }
247 
SetBitmap(const void * bitMap,const uint8_t pictureId,const void * colorKey,const float left,const float top,const float right,const float bottom)248 int32_t VideoRenderLinuxImpl::SetBitmap(const void* bitMap,
249                                         const uint8_t pictureId,
250                                         const void* colorKey,
251                                         const float left,
252                                         const float top,
253                                         const float right,
254                                         const float bottom)
255 {
256     WEBRTC_TRACE(kTraceError, kTraceVideoRenderer, _id,
257                  "%s - not supported on Linux", __FUNCTION__);
258     return -1;
259 }
260 
261 }  // namespace webrtc
262