OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/filters/rtc_video_decoder.h" | 5 #include "content/renderer/media/rtc_video_decoder.h" |
6 | 6 |
7 #include <deque> | 7 #include <deque> |
8 | 8 |
9 #include "base/task.h" | 9 #include "base/task.h" |
10 #include "googleurl/src/gurl.h" | |
11 #include "media/base/callback.h" | 10 #include "media/base/callback.h" |
12 #include "media/base/filter_host.h" | 11 #include "media/base/filter_host.h" |
13 #include "media/base/filters.h" | 12 #include "media/base/filters.h" |
14 #include "media/base/limits.h" | 13 #include "media/base/limits.h" |
15 #include "media/base/media_format.h" | 14 #include "media/base/media_format.h" |
16 #include "media/base/video_frame.h" | 15 #include "media/base/video_frame.h" |
17 | 16 |
18 namespace media { | 17 using media::DemuxerStream; |
19 | 18 using media::FilterCallback; |
20 static const char kMediaScheme[] = "media"; | 19 using media::FilterStatusCB; |
| 20 using media::kNoTimestamp; |
| 21 using media::Limits; |
| 22 using media::MediaFormat; |
| 23 using media::PIPELINE_OK; |
| 24 using media::StatisticsCallback; |
| 25 using media::VideoDecoder; |
| 26 using media::VideoFrame; |
21 | 27 |
22 RTCVideoDecoder::RTCVideoDecoder(MessageLoop* message_loop, | 28 RTCVideoDecoder::RTCVideoDecoder(MessageLoop* message_loop, |
23 const std::string& url) | 29 const std::string& url) |
24 : message_loop_(message_loop), | 30 : message_loop_(message_loop), |
25 width_(176), | 31 width_(176), |
26 height_(144), | 32 height_(144), |
27 url_(url), | 33 url_(url), |
28 state_(kUnInitialized) { | 34 state_(kUnInitialized) { |
29 } | 35 } |
30 | 36 |
(...skipping 16 matching lines...) Expand all Loading... |
47 DCHECK_EQ(MessageLoop::current(), message_loop_); | 53 DCHECK_EQ(MessageLoop::current(), message_loop_); |
48 | 54 |
49 lock_.Acquire(); | 55 lock_.Acquire(); |
50 frame_queue_available_.clear(); | 56 frame_queue_available_.clear(); |
51 lock_.Release(); | 57 lock_.Release(); |
52 media_format_.SetAsInteger(MediaFormat::kWidth, width_); | 58 media_format_.SetAsInteger(MediaFormat::kWidth, width_); |
53 media_format_.SetAsInteger(MediaFormat::kHeight, height_); | 59 media_format_.SetAsInteger(MediaFormat::kHeight, height_); |
54 media_format_.SetAsInteger(MediaFormat::kSurfaceType, | 60 media_format_.SetAsInteger(MediaFormat::kSurfaceType, |
55 static_cast<int>(VideoFrame::YV12)); | 61 static_cast<int>(VideoFrame::YV12)); |
56 media_format_.SetAsInteger(MediaFormat::kSurfaceFormat, | 62 media_format_.SetAsInteger(MediaFormat::kSurfaceFormat, |
57 static_cast<int>(VideoFrame::TYPE_SYSTEM_MEMORY)); | 63 static_cast<int>(VideoFrame::TYPE_SYSTEM_MEMORY)); |
58 | 64 |
59 state_ = kNormal; | 65 state_ = kNormal; |
60 | 66 |
61 filter_callback->Run(); | 67 filter_callback->Run(); |
62 delete filter_callback; | 68 delete filter_callback; |
63 | 69 |
64 // TODO(acolwell): Implement stats. | 70 // TODO(acolwell): Implement stats. |
65 delete stat_callback; | 71 delete stat_callback; |
66 } | 72 } |
67 | 73 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 | 112 |
107 DCHECK_EQ(MessageLoop::current(), message_loop_); | 113 DCHECK_EQ(MessageLoop::current(), message_loop_); |
108 | 114 |
109 state_ = kStopped; | 115 state_ = kStopped; |
110 | 116 |
111 VideoDecoder::Stop(callback); | 117 VideoDecoder::Stop(callback); |
112 | 118 |
113 // TODO(ronghuawu): Stop rtc | 119 // TODO(ronghuawu): Stop rtc |
114 } | 120 } |
115 | 121 |
116 void RTCVideoDecoder::Seek(base::TimeDelta time, const FilterStatusCB& cb) { | 122 void RTCVideoDecoder::Seek(base::TimeDelta time, |
| 123 const FilterStatusCB& cb) { |
117 if (MessageLoop::current() != message_loop_) { | 124 if (MessageLoop::current() != message_loop_) { |
118 message_loop_->PostTask(FROM_HERE, | 125 message_loop_->PostTask(FROM_HERE, |
119 NewRunnableMethod(this, &RTCVideoDecoder::Seek, | 126 NewRunnableMethod(this, &RTCVideoDecoder::Seek, |
120 time, cb)); | 127 time, cb)); |
121 return; | 128 return; |
122 } | 129 } |
123 | 130 |
124 DCHECK_EQ(MessageLoop::current(), message_loop_); | 131 DCHECK_EQ(MessageLoop::current(), message_loop_); |
125 | 132 |
126 state_ = kSeeking; | 133 state_ = kSeeking; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 DCHECK_EQ(MessageLoop::current(), message_loop_); | 190 DCHECK_EQ(MessageLoop::current(), message_loop_); |
184 lock_.Acquire(); | 191 lock_.Acquire(); |
185 frame_queue_available_.push_back(video_frame); | 192 frame_queue_available_.push_back(video_frame); |
186 lock_.Release(); | 193 lock_.Release(); |
187 } | 194 } |
188 | 195 |
189 bool RTCVideoDecoder::ProvidesBuffer() { | 196 bool RTCVideoDecoder::ProvidesBuffer() { |
190 return true; | 197 return true; |
191 } | 198 } |
192 | 199 |
193 int RTCVideoDecoder::FrameSizeChange(unsigned int width, | 200 bool RTCVideoDecoder::SetSize(int width, int height, int reserved) { |
194 unsigned int height, | |
195 unsigned int number_of_streams) { | |
196 width_ = width; | 201 width_ = width; |
197 height_ = height; | 202 height_ = height; |
198 | 203 |
199 media_format_.SetAsInteger(MediaFormat::kWidth, width_); | 204 media_format_.SetAsInteger(MediaFormat::kWidth, width_); |
200 media_format_.SetAsInteger(MediaFormat::kHeight, height_); | 205 media_format_.SetAsInteger(MediaFormat::kHeight, height_); |
201 host()->SetVideoSize(width_, height_); | 206 host()->SetVideoSize(width_, height_); |
202 return 0; | 207 return true; |
203 } | 208 } |
204 | 209 |
205 int RTCVideoDecoder::DeliverFrame(unsigned char* buffer, | 210 bool RTCVideoDecoder::RenderFrame(const cricket::VideoFrame* frame) { |
206 int buffer_size) { | 211 DCHECK(frame); |
207 DCHECK(buffer); | |
208 | |
209 if (frame_queue_available_.size() == 0) | |
210 return 0; | |
211 | 212 |
212 if (state_ != kNormal) | 213 if (state_ != kNormal) |
213 return 0; | 214 return true; |
214 | 215 |
215 // This is called from another thread | 216 // This is called from another thread |
216 lock_.Acquire(); | 217 lock_.Acquire(); |
| 218 if (frame_queue_available_.size() == 0) { |
| 219 lock_.Release(); |
| 220 return true; |
| 221 } |
217 scoped_refptr<VideoFrame> video_frame = frame_queue_available_.front(); | 222 scoped_refptr<VideoFrame> video_frame = frame_queue_available_.front(); |
218 frame_queue_available_.pop_front(); | 223 frame_queue_available_.pop_front(); |
219 lock_.Release(); | 224 lock_.Release(); |
220 | 225 |
221 // Check if there's a size change | 226 // Check if there's a size change |
222 if (video_frame->width() != width_ || video_frame->height() != height_) { | 227 if (video_frame->width() != width_ || video_frame->height() != height_) { |
223 video_frame.release(); | 228 video_frame.release(); |
224 // Allocate new buffer based on the new size | 229 // Allocate new buffer based on the new size |
225 VideoFrame::CreateFrame(VideoFrame::YV12, | 230 VideoFrame::CreateFrame(VideoFrame::YV12, |
226 width_, | 231 width_, |
227 height_, | 232 height_, |
228 kNoTimestamp, | 233 kNoTimestamp, |
229 kNoTimestamp, | 234 kNoTimestamp, |
230 &video_frame); | 235 &video_frame); |
231 if (!video_frame.get()) { | 236 if (!video_frame.get()) |
232 return -1; | 237 return false; |
233 } | |
234 } | 238 } |
235 | 239 |
236 video_frame->SetTimestamp(host()->GetTime()); | 240 video_frame->SetTimestamp(host()->GetTime()); |
237 video_frame->SetDuration(base::TimeDelta::FromMilliseconds(30)); | 241 video_frame->SetDuration(base::TimeDelta::FromMilliseconds(30)); |
238 | 242 |
239 uint8* y_plane = video_frame->data(VideoFrame::kYPlane); | 243 uint8* y_plane = video_frame->data(VideoFrame::kYPlane); |
| 244 const uint8* y_plane_src = frame->GetYPlane(); |
240 for (size_t row = 0; row < video_frame->height(); ++row) { | 245 for (size_t row = 0; row < video_frame->height(); ++row) { |
241 memcpy(y_plane, buffer, width_); | 246 memcpy(y_plane, y_plane_src, frame->GetYPitch()); |
242 y_plane += video_frame->stride(VideoFrame::kYPlane); | 247 y_plane += video_frame->stride(VideoFrame::kYPlane); |
243 buffer += width_; | 248 y_plane_src += frame->GetYPitch(); |
244 } | 249 } |
245 size_t uv_width = width_/2; | |
246 uint8* u_plane = video_frame->data(VideoFrame::kUPlane); | 250 uint8* u_plane = video_frame->data(VideoFrame::kUPlane); |
| 251 const uint8* u_plane_src = frame->GetUPlane(); |
247 for (size_t row = 0; row < video_frame->height(); row += 2) { | 252 for (size_t row = 0; row < video_frame->height(); row += 2) { |
248 memcpy(u_plane, buffer, uv_width); | 253 memcpy(u_plane, u_plane_src, frame->GetUPitch()); |
249 u_plane += video_frame->stride(VideoFrame::kUPlane); | 254 u_plane += video_frame->stride(VideoFrame::kUPlane); |
250 buffer += uv_width; | 255 u_plane_src += frame->GetUPitch(); |
251 } | 256 } |
252 uint8* v_plane = video_frame->data(VideoFrame::kVPlane); | 257 uint8* v_plane = video_frame->data(VideoFrame::kVPlane); |
| 258 const uint8* v_plane_src = frame->GetVPlane(); |
253 for (size_t row = 0; row < video_frame->height(); row += 2) { | 259 for (size_t row = 0; row < video_frame->height(); row += 2) { |
254 memcpy(v_plane, buffer, uv_width); | 260 memcpy(v_plane, v_plane_src, frame->GetVPitch()); |
255 v_plane += video_frame->stride(VideoFrame::kVPlane); | 261 v_plane += video_frame->stride(VideoFrame::kVPlane); |
256 buffer += uv_width; | 262 v_plane_src += frame->GetVPitch(); |
257 } | 263 } |
258 | 264 |
259 if (MessageLoop::current() != message_loop_) { | 265 if (MessageLoop::current() != message_loop_) { |
260 message_loop_->PostTask( | 266 message_loop_->PostTask( |
261 FROM_HERE, | 267 FROM_HERE, |
262 NewRunnableMethod(this, | 268 NewRunnableMethod(this, |
263 &RTCVideoDecoder::VideoFrameReady, | 269 &RTCVideoDecoder::VideoFrameReady, |
264 video_frame)); | 270 video_frame)); |
265 } else { | 271 } else { |
266 VideoFrameReady(video_frame); | 272 VideoFrameReady(video_frame); |
267 } | 273 } |
268 | 274 |
269 return 0; | 275 return true; |
270 } | 276 } |
271 | |
272 bool RTCVideoDecoder::IsUrlSupported(const std::string& url) { | |
273 GURL gurl(url); | |
274 return gurl.SchemeIs(kMediaScheme); | |
275 } | |
276 | |
277 } // namespace media | |
OLD | NEW |