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