| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/renderer/media/webrtc/webrtc_video_capturer_adapter.h" | 5 #include "content/renderer/media/webrtc/webrtc_video_capturer_adapter.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
| 9 #include "base/memory/aligned_memory.h" | 9 #include "base/memory/aligned_memory.h" |
| 10 #include "media/base/video_frame.h" | 10 #include "media/base/video_frame.h" |
| 11 #include "third_party/libyuv/include/libyuv/convert.h" | 11 #include "third_party/libyuv/include/libyuv/scale.h" |
| 12 | 12 |
| 13 namespace content { | 13 namespace content { |
| 14 | 14 |
| 15 WebRtcVideoCapturerAdapter::WebRtcVideoCapturerAdapter(bool is_screencast) | 15 WebRtcVideoCapturerAdapter::WebRtcVideoCapturerAdapter(bool is_screencast) |
| 16 : is_screencast_(is_screencast), | 16 : is_screencast_(is_screencast), |
| 17 running_(false), | 17 running_(false), |
| 18 buffer_(NULL), | 18 buffer_(NULL), |
| 19 buffer_size_(0) { | 19 buffer_size_(0) { |
| 20 } | 20 } |
| 21 | 21 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 // Use the desired format as the best format. | 70 // Use the desired format as the best format. |
| 71 best_format->width = desired.width; | 71 best_format->width = desired.width; |
| 72 best_format->height = desired.height; | 72 best_format->height = desired.height; |
| 73 best_format->fourcc = cricket::FOURCC_I420; | 73 best_format->fourcc = cricket::FOURCC_I420; |
| 74 best_format->interval = desired.interval; | 74 best_format->interval = desired.interval; |
| 75 return true; | 75 return true; |
| 76 } | 76 } |
| 77 | 77 |
| 78 void WebRtcVideoCapturerAdapter::OnFrameCaptured( | 78 void WebRtcVideoCapturerAdapter::OnFrameCaptured( |
| 79 const scoped_refptr<media::VideoFrame>& frame) { | 79 const scoped_refptr<media::VideoFrame>& frame) { |
| 80 DCHECK(media::VideoFrame::I420 == frame->format() || | 80 TRACE_EVENT0("video", "WebRtcVideoCapturerAdapter::OnFrameCaptured"); |
| 81 media::VideoFrame::YV12 == frame->format()); | 81 if (!(media::VideoFrame::I420 == frame->format() || |
| 82 media::VideoFrame::YV12 == frame->format())) { |
| 83 // Some types of sources support textures as output. Since connecting |
| 84 // sources and sinks do not check the format, we need to just ignore |
| 85 // formats that we can not handle. |
| 86 NOTREACHED(); |
| 87 return; |
| 88 } |
| 89 |
| 82 if (first_frame_timestamp_ == media::kNoTimestamp()) | 90 if (first_frame_timestamp_ == media::kNoTimestamp()) |
| 83 first_frame_timestamp_ = frame->timestamp(); | 91 first_frame_timestamp_ = frame->timestamp(); |
| 84 | 92 |
| 85 cricket::CapturedFrame captured_frame; | 93 cricket::CapturedFrame captured_frame; |
| 86 captured_frame.width = frame->visible_rect().width(); | 94 captured_frame.width = frame->natural_size().width(); |
| 87 captured_frame.height = frame->visible_rect().height(); | 95 captured_frame.height = frame->natural_size().height(); |
| 88 // cricket::CapturedFrame time is in nanoseconds. | 96 // cricket::CapturedFrame time is in nanoseconds. |
| 89 captured_frame.elapsed_time = | 97 captured_frame.elapsed_time = |
| 90 (frame->timestamp() - first_frame_timestamp_).InMicroseconds() * | 98 (frame->timestamp() - first_frame_timestamp_).InMicroseconds() * |
| 91 base::Time::kNanosecondsPerMicrosecond; | 99 base::Time::kNanosecondsPerMicrosecond; |
| 92 captured_frame.time_stamp = frame->timestamp().InMicroseconds() * | 100 captured_frame.time_stamp = frame->timestamp().InMicroseconds() * |
| 93 base::Time::kNanosecondsPerMicrosecond; | 101 base::Time::kNanosecondsPerMicrosecond; |
| 94 captured_frame.pixel_height = 1; | 102 captured_frame.pixel_height = 1; |
| 95 captured_frame.pixel_width = 1; | 103 captured_frame.pixel_width = 1; |
| 96 | 104 |
| 97 // TODO(perkj): | 105 // TODO(perkj): |
| 98 // Libjingle expects contiguous layout of image planes as input. | 106 // Libjingle expects contiguous layout of image planes as input. |
| 99 // The only format where that is true in Chrome is I420 where the | 107 // The only format where that is true in Chrome is I420 where the |
| 100 // coded_size == visible_rect().size(). | 108 // coded_size == natural_size(). |
| 101 if (frame->format() != media::VideoFrame::I420 || | 109 if (frame->format() != media::VideoFrame::I420 || |
| 102 frame->coded_size() != frame->visible_rect().size()) { | 110 frame->coded_size() != frame->natural_size()) { |
| 103 // Cropping and or switching UV planes is needed. | 111 // Cropping / Scaling and or switching UV planes is needed. |
| 104 UpdateI420Buffer(frame); | 112 UpdateI420Buffer(frame); |
| 105 captured_frame.data = buffer_; | 113 captured_frame.data = buffer_; |
| 106 captured_frame.data_size = buffer_size_; | 114 captured_frame.data_size = buffer_size_; |
| 107 captured_frame.fourcc = cricket::FOURCC_I420; | 115 captured_frame.fourcc = cricket::FOURCC_I420; |
| 108 } else { | 116 } else { |
| 109 captured_frame.fourcc = media::VideoFrame::I420 == frame->format() ? | 117 captured_frame.fourcc = media::VideoFrame::I420 == frame->format() ? |
| 110 cricket::FOURCC_I420 : cricket::FOURCC_YV12; | 118 cricket::FOURCC_I420 : cricket::FOURCC_YV12; |
| 111 captured_frame.data = frame->data(0); | 119 captured_frame.data = frame->data(0); |
| 112 captured_frame.data_size = | 120 captured_frame.data_size = |
| 113 media::VideoFrame::AllocationSize(frame->format(), frame->coded_size()); | 121 media::VideoFrame::AllocationSize(frame->format(), frame->coded_size()); |
| 114 } | 122 } |
| 115 | 123 |
| 116 // This signals to libJingle that a new VideoFrame is available. | 124 // This signals to libJingle that a new VideoFrame is available. |
| 117 // libJingle have no assumptions on what thread this signal come from. | 125 // libJingle have no assumptions on what thread this signal come from. |
| 118 SignalFrameCaptured(this, &captured_frame); | 126 SignalFrameCaptured(this, &captured_frame); |
| 119 } | 127 } |
| 120 | 128 |
| 121 void WebRtcVideoCapturerAdapter::UpdateI420Buffer( | 129 void WebRtcVideoCapturerAdapter::UpdateI420Buffer( |
| 122 const scoped_refptr<media::VideoFrame>& src) { | 130 const scoped_refptr<media::VideoFrame>& src) { |
| 123 const int src_width = src->coded_size().width(); | 131 const int src_width = src->coded_size().width(); |
| 124 const int src_height = src->coded_size().height(); | 132 const int src_height = src->coded_size().height(); |
| 125 const int dst_width = src->visible_rect().width(); | 133 const int dst_width = src->natural_size().width(); |
| 126 const int dst_height = src->visible_rect().height(); | 134 const int dst_height = src->natural_size().height(); |
| 127 DCHECK(src_width >= dst_width && src_height >= dst_height); | 135 DCHECK(src_width >= dst_width && src_height >= dst_height); |
| 128 | 136 |
| 129 const int horiz_crop = src->visible_rect().x(); | 137 const gfx::Rect& visible_rect = src->visible_rect(); |
| 130 const int vert_crop = src->visible_rect().y(); | |
| 131 | 138 |
| 132 const uint8* src_y = src->data(media::VideoFrame::kYPlane) + | 139 const uint8* src_y = src->data(media::VideoFrame::kYPlane) + |
| 133 (src_width * vert_crop + horiz_crop); | 140 visible_rect.y() * src->stride(media::VideoFrame::kYPlane) + |
| 134 const int center = (src_width + 1) / 2; | 141 visible_rect.x(); |
| 135 const uint8* src_u = src->data(media::VideoFrame::kUPlane) + | 142 const uint8* src_u = src->data(media::VideoFrame::kUPlane) + |
| 136 (center * vert_crop + horiz_crop) / 2; | 143 visible_rect.y() * src->stride(media::VideoFrame::kUPlane) + |
| 144 visible_rect.x() / 2; |
| 137 const uint8* src_v = src->data(media::VideoFrame::kVPlane) + | 145 const uint8* src_v = src->data(media::VideoFrame::kVPlane) + |
| 138 (center * vert_crop + horiz_crop) / 2; | 146 visible_rect.y() * src->stride(media::VideoFrame::kVPlane) + |
| 147 visible_rect.x() / 2; |
| 139 | 148 |
| 140 const size_t dst_size = | 149 const size_t dst_size = |
| 141 media::VideoFrame::AllocationSize(src->format(), | 150 media::VideoFrame::AllocationSize(src->format(), src->natural_size()); |
| 142 src->visible_rect().size()); | |
| 143 | 151 |
| 144 if (dst_size != buffer_size_) { | 152 if (dst_size != buffer_size_) { |
| 145 base::AlignedFree(buffer_); | 153 base::AlignedFree(buffer_); |
| 146 buffer_ = reinterpret_cast<uint8*>( | 154 buffer_ = reinterpret_cast<uint8*>( |
| 147 base::AlignedAlloc(dst_size + media::VideoFrame::kFrameSizePadding, | 155 base::AlignedAlloc(dst_size + media::VideoFrame::kFrameSizePadding, |
| 148 media::VideoFrame::kFrameAddressAlignment)); | 156 media::VideoFrame::kFrameAddressAlignment)); |
| 149 buffer_size_ = dst_size; | 157 buffer_size_ = dst_size; |
| 150 } | 158 } |
| 151 | 159 |
| 152 uint8* dst_y = buffer_; | 160 uint8* dst_y = buffer_; |
| 153 const int dst_stride_y = dst_width; | 161 const int dst_stride_y = dst_width; |
| 154 uint8* dst_u = dst_y + dst_width * dst_height; | 162 uint8* dst_u = dst_y + dst_width * dst_height; |
| 155 const int dst_halfwidth = (dst_width + 1) / 2; | 163 const int dst_halfwidth = (dst_width + 1) / 2; |
| 156 const int dst_halfheight = (dst_height + 1) / 2; | 164 const int dst_halfheight = (dst_height + 1) / 2; |
| 157 uint8* dst_v = dst_u + dst_halfwidth * dst_halfheight; | 165 uint8* dst_v = dst_u + dst_halfwidth * dst_halfheight; |
| 158 | 166 |
| 159 libyuv::I420Copy(src_y, | 167 libyuv::I420Scale(src_y, |
| 160 src->stride(media::VideoFrame::kYPlane), | 168 src->stride(media::VideoFrame::kYPlane), |
| 161 src_u, | 169 src_u, |
| 162 src->stride(media::VideoFrame::kUPlane), | 170 src->stride(media::VideoFrame::kUPlane), |
| 163 src_v, | 171 src_v, |
| 164 src->stride(media::VideoFrame::kVPlane), | 172 src->stride(media::VideoFrame::kVPlane), |
| 173 visible_rect.width(), |
| 174 visible_rect.height(), |
| 165 dst_y, | 175 dst_y, |
| 166 dst_stride_y, | 176 dst_stride_y, |
| 167 dst_u, | 177 dst_u, |
| 168 dst_halfwidth, | 178 dst_halfwidth, |
| 169 dst_v, | 179 dst_v, |
| 170 dst_halfwidth, | 180 dst_halfwidth, |
| 171 dst_width, | 181 dst_width, |
| 172 dst_height); | 182 dst_height, |
| 183 libyuv::kFilterBilinear); |
| 173 } | 184 } |
| 174 | 185 |
| 175 } // namespace content | 186 } // namespace content |
| OLD | NEW |