OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/renderer/pepper/pepper_media_stream_video_track_host.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "ppapi/c/pp_errors.h" |
| 9 #include "ppapi/c/ppb_video_frame.h" |
| 10 #include "ppapi/shared_impl/media_stream_frame.h" |
| 11 |
| 12 using media::VideoFrame; |
| 13 |
| 14 namespace { |
| 15 |
| 16 // TODO(penghuang): make it configurable. |
| 17 const int32_t kNumberOfFrames = 4; |
| 18 |
| 19 PP_VideoFrame_Format ToPpapiFormat(VideoFrame::Format format) { |
| 20 switch (format) { |
| 21 case VideoFrame::YV12: |
| 22 return PP_VIDEOFRAME_FORMAT_YV12; |
| 23 case VideoFrame::YV16: |
| 24 return PP_VIDEOFRAME_FORMAT_YV16; |
| 25 case VideoFrame::I420: |
| 26 return PP_VIDEOFRAME_FORMAT_I420; |
| 27 case VideoFrame::YV12A: |
| 28 return PP_VIDEOFRAME_FORMAT_YV12A; |
| 29 case VideoFrame::YV12J: |
| 30 return PP_VIDEOFRAME_FORMAT_YV12J; |
| 31 default: |
| 32 DVLOG(1) << "Unsupported pixel format " << format; |
| 33 return PP_VIDEOFRAME_FORMAT_UNKNOWN; |
| 34 } |
| 35 } |
| 36 |
| 37 } // namespace |
| 38 |
| 39 namespace content { |
| 40 |
| 41 PepperMediaStreamVideoTrackHost::PepperMediaStreamVideoTrackHost( |
| 42 RendererPpapiHost* host, |
| 43 PP_Instance instance, |
| 44 PP_Resource resource, |
| 45 const blink::WebMediaStreamTrack& track) |
| 46 : PepperMediaStreamTrackHostBase(host, instance, resource), |
| 47 track_(track), |
| 48 connected_(false), |
| 49 frame_format_(VideoFrame::UNKNOWN), |
| 50 frame_data_size_(0) { |
| 51 DCHECK(!track_.isNull()); |
| 52 } |
| 53 |
| 54 PepperMediaStreamVideoTrackHost::~PepperMediaStreamVideoTrackHost() { |
| 55 OnClose(); |
| 56 } |
| 57 |
| 58 void PepperMediaStreamVideoTrackHost::OnClose() { |
| 59 if (connected_) { |
| 60 MediaStreamVideoSink::RemoveFromVideoTrack(this, track_); |
| 61 connected_ = false; |
| 62 } |
| 63 } |
| 64 |
| 65 void PepperMediaStreamVideoTrackHost::OnVideoFrame( |
| 66 const scoped_refptr<VideoFrame>& frame) { |
| 67 DCHECK(frame); |
| 68 // TODO(penghuang): Check |frame->end_of_stream()| and close the track. |
| 69 PP_VideoFrame_Format ppformat = ToPpapiFormat(frame->format()); |
| 70 if (ppformat == PP_VIDEOFRAME_FORMAT_UNKNOWN) |
| 71 return; |
| 72 |
| 73 if (frame_size_ != frame->coded_size() || frame_format_ != frame->format()) { |
| 74 frame_size_ = frame->coded_size(); |
| 75 frame_format_ = frame->format(); |
| 76 // TODO(penghuang): Support changing |frame_size_| & |frame_format_| more |
| 77 // than once. |
| 78 DCHECK(!frame_data_size_); |
| 79 frame_data_size_ = VideoFrame::AllocationSize(frame_format_, frame_size_); |
| 80 int32_t size = sizeof(ppapi::MediaStreamFrame::Video) + frame_data_size_; |
| 81 InitFrames(kNumberOfFrames, size); |
| 82 } |
| 83 |
| 84 int32_t index = frame_buffer()->DequeueFrame(); |
| 85 // Drop frames if the underlying buffer is full. |
| 86 if (index < 0) |
| 87 return; |
| 88 |
| 89 // TODO(penghuang): support format conversion and size scaling. |
| 90 ppapi::MediaStreamFrame::Video* ppframe = |
| 91 &(frame_buffer()->GetFramePointer(index)->video); |
| 92 ppframe->header.size = frame_buffer()->frame_size(); |
| 93 ppframe->header.type = ppapi::MediaStreamFrame::TYPE_VIDEO; |
| 94 ppframe->timestamp = frame->GetTimestamp().InSecondsF(); |
| 95 ppframe->format = ppformat; |
| 96 ppframe->size.width = frame->coded_size().width(); |
| 97 ppframe->size.height = frame->coded_size().height(); |
| 98 ppframe->data_size = frame_data_size_; |
| 99 |
| 100 COMPILE_ASSERT(VideoFrame::kYPlane == 0, y_plane_should_be_0); |
| 101 COMPILE_ASSERT(VideoFrame::kUPlane == 1, u_plane_should_be_1); |
| 102 COMPILE_ASSERT(VideoFrame::kVPlane == 2, v_plane_should_be_2); |
| 103 |
| 104 uint8_t* dst = ppframe->data; |
| 105 size_t num_planes = VideoFrame::NumPlanes(frame->format()); |
| 106 for (size_t i = 0; i < num_planes; ++i) { |
| 107 const uint8_t* src = frame->data(i); |
| 108 const size_t row_bytes = frame->row_bytes(i); |
| 109 const size_t src_stride = frame->stride(i); |
| 110 int rows = frame->rows(i); |
| 111 for (int j = 0; j < rows; ++j) { |
| 112 memcpy(dst, src, row_bytes); |
| 113 dst += row_bytes; |
| 114 src += src_stride; |
| 115 } |
| 116 } |
| 117 |
| 118 SendEnqueueFrameMessageToPlugin(index); |
| 119 } |
| 120 |
| 121 void PepperMediaStreamVideoTrackHost::DidConnectPendingHostToResource() { |
| 122 if (!connected_) { |
| 123 MediaStreamVideoSink::AddToVideoTrack(this, track_); |
| 124 connected_ = true; |
| 125 } |
| 126 } |
| 127 |
| 128 } // namespace content |
OLD | NEW |