OLD | NEW |
| (Empty) |
1 /* | |
2 * libjingle | |
3 * Copyright 2004 Google Inc. | |
4 * | |
5 * Redistribution and use in source and binary forms, with or without | |
6 * modification, are permitted provided that the following conditions are met: | |
7 * | |
8 * 1. Redistributions of source code must retain the above copyright notice, | |
9 * this list of conditions and the following disclaimer. | |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | |
11 * this list of conditions and the following disclaimer in the documentation | |
12 * and/or other materials provided with the distribution. | |
13 * 3. The name of the author may not be used to endorse or promote products | |
14 * derived from this software without specific prior written permission. | |
15 * | |
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | |
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
26 */ | |
27 | |
28 #include "talk/media/webrtc/webrtcpassthroughrender.h" | |
29 | |
30 #include "webrtc/base/common.h" | |
31 #include "webrtc/base/logging.h" | |
32 | |
33 namespace cricket { | |
34 | |
35 #define LOG_FIND_STREAM_ERROR(func, id) LOG(LS_ERROR) \ | |
36 << "" << func << " - Failed to find stream: " << id | |
37 | |
38 class PassthroughStream: public webrtc::VideoRenderCallback { | |
39 public: | |
40 PassthroughStream() : running_(false) {} | |
41 virtual ~PassthroughStream() { | |
42 } | |
43 virtual int32_t RenderFrame(const uint32_t stream_id, | |
44 const webrtc::VideoFrame& videoFrame) { | |
45 rtc::CritScope cs(&stream_critical_); | |
46 // Send frame for rendering directly | |
47 if (running_ && renderer_) { | |
48 renderer_->RenderFrame(stream_id, videoFrame); | |
49 } | |
50 return 0; | |
51 } | |
52 int32_t SetRenderer(VideoRenderCallback* renderer) { | |
53 rtc::CritScope cs(&stream_critical_); | |
54 renderer_ = renderer; | |
55 return 0; | |
56 } | |
57 | |
58 int32_t StartRender() { | |
59 rtc::CritScope cs(&stream_critical_); | |
60 running_ = true; | |
61 return 0; | |
62 } | |
63 | |
64 int32_t StopRender() { | |
65 rtc::CritScope cs(&stream_critical_); | |
66 running_ = false; | |
67 return 0; | |
68 } | |
69 | |
70 private: | |
71 VideoRenderCallback* renderer_; | |
72 rtc::CriticalSection stream_critical_; | |
73 bool running_; | |
74 }; | |
75 | |
76 WebRtcPassthroughRender::WebRtcPassthroughRender() | |
77 : window_(NULL) { | |
78 } | |
79 | |
80 WebRtcPassthroughRender::~WebRtcPassthroughRender() { | |
81 while (!stream_render_map_.empty()) { | |
82 PassthroughStream* stream = stream_render_map_.begin()->second; | |
83 stream_render_map_.erase(stream_render_map_.begin()); | |
84 delete stream; | |
85 } | |
86 } | |
87 | |
88 webrtc::VideoRenderCallback* WebRtcPassthroughRender::AddIncomingRenderStream( | |
89 const uint32_t stream_id, | |
90 const uint32_t zOrder, | |
91 const float left, const float top, | |
92 const float right, const float bottom) { | |
93 rtc::CritScope cs(&render_critical_); | |
94 // Stream already exist. | |
95 if (FindStream(stream_id) != NULL) { | |
96 LOG(LS_ERROR) << "AddIncomingRenderStream - Stream already exists: " | |
97 << stream_id; | |
98 return NULL; | |
99 } | |
100 | |
101 PassthroughStream* stream = new PassthroughStream(); | |
102 // Store the stream | |
103 stream_render_map_[stream_id] = stream; | |
104 return stream; | |
105 } | |
106 | |
107 int32_t WebRtcPassthroughRender::DeleteIncomingRenderStream( | |
108 const uint32_t stream_id) { | |
109 rtc::CritScope cs(&render_critical_); | |
110 PassthroughStream* stream = FindStream(stream_id); | |
111 if (stream == NULL) { | |
112 LOG_FIND_STREAM_ERROR("DeleteIncomingRenderStream", stream_id); | |
113 return -1; | |
114 } | |
115 delete stream; | |
116 stream_render_map_.erase(stream_id); | |
117 return 0; | |
118 } | |
119 | |
120 int32_t WebRtcPassthroughRender::AddExternalRenderCallback( | |
121 const uint32_t stream_id, | |
122 webrtc::VideoRenderCallback* render_object) { | |
123 rtc::CritScope cs(&render_critical_); | |
124 PassthroughStream* stream = FindStream(stream_id); | |
125 if (stream == NULL) { | |
126 LOG_FIND_STREAM_ERROR("AddExternalRenderCallback", stream_id); | |
127 return -1; | |
128 } | |
129 return stream->SetRenderer(render_object); | |
130 } | |
131 | |
132 bool WebRtcPassthroughRender::HasIncomingRenderStream( | |
133 const uint32_t stream_id) const { | |
134 return (FindStream(stream_id) != NULL); | |
135 } | |
136 | |
137 webrtc::RawVideoType WebRtcPassthroughRender::PreferredVideoType() const { | |
138 return webrtc::kVideoI420; | |
139 } | |
140 | |
141 int32_t WebRtcPassthroughRender::StartRender(const uint32_t stream_id) { | |
142 rtc::CritScope cs(&render_critical_); | |
143 PassthroughStream* stream = FindStream(stream_id); | |
144 if (stream == NULL) { | |
145 LOG_FIND_STREAM_ERROR("StartRender", stream_id); | |
146 return -1; | |
147 } | |
148 return stream->StartRender(); | |
149 } | |
150 | |
151 int32_t WebRtcPassthroughRender::StopRender(const uint32_t stream_id) { | |
152 rtc::CritScope cs(&render_critical_); | |
153 PassthroughStream* stream = FindStream(stream_id); | |
154 if (stream == NULL) { | |
155 LOG_FIND_STREAM_ERROR("StopRender", stream_id); | |
156 return -1; | |
157 } | |
158 return stream->StopRender(); | |
159 } | |
160 | |
161 // TODO(ronghuawu): Is it ok to return non-const pointer to PassthroughStream | |
162 // from this const function FindStream. | |
163 PassthroughStream* WebRtcPassthroughRender::FindStream( | |
164 const uint32_t stream_id) const { | |
165 StreamMap::const_iterator it = stream_render_map_.find(stream_id); | |
166 if (it == stream_render_map_.end()) { | |
167 return NULL; | |
168 } | |
169 return it->second; | |
170 } | |
171 | |
172 } // namespace cricket | |
OLD | NEW |