Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(476)

Side by Side Diff: content/renderer/media/video_destination_handler.cc

Issue 14312015: Effects Pepper Plugin and MediaStream Glue. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2013 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/media/video_destination_handler.h"
6
7 #include <string>
8
9 #include "base/logging.h"
10 #include "content/renderer/media/media_stream_dependency_factory.h"
11 #include "content/renderer/media/media_stream_registry_interface.h"
12 #include "content/renderer/render_thread_impl.h"
13 #include "third_party/WebKit/Source/Platform/chromium/public/WebMediaStreamTrack .h"
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaStreamRegistr y.h"
15 #include "webkit/plugins/ppapi/ppb_image_data_impl.h"
16
17 using cricket::CaptureState;
18 using cricket::VideoFormat;
19 using webkit::ppapi::PPB_ImageData_Impl;
20 using webrtc::VideoTrackInterface;
21 using webrtc::VideoTrackVector;
22
23 static const cricket::FourCC kEffectColorFormat = cricket::FOURCC_BGRA;
24
25 namespace content {
26
27 PpFrameWriter::PpFrameWriter()
28 : started_(false) {}
29
30 PpFrameWriter::~PpFrameWriter() {}
31
32 CaptureState PpFrameWriter::Start(const VideoFormat& capture_format) {
33 base::AutoLock auto_lock(lock_);
34 if (started_) {
35 LOG(ERROR) << "PpFrameWriter::Start - "
36 << "Got a StartCapture when already started!";
37 return cricket::CS_FAILED;
38 }
39 started_ = true;
40 return cricket::CS_STARTING;
41 }
42
43 void PpFrameWriter::Stop() {
44 base::AutoLock auto_lock(lock_);
45 started_ = false;
46 SignalStateChange(this, cricket::CS_STOPPED);
47 }
48
49 bool PpFrameWriter::IsRunning() {
50 return started_;
51 }
52
53 bool PpFrameWriter::GetPreferredFourccs(std::vector<uint32>* fourccs) {
54 if (!fourccs) {
55 LOG(ERROR) << "PpFrameWriter::GetPreferredFourccs - "
56 << "fourccs is NULL.";
57 return false;
58 }
59 // The effects plugin output BGRA.
60 fourccs->push_back(kEffectColorFormat);
61 return true;
62 }
63
64 bool PpFrameWriter::GetBestCaptureFormat(const VideoFormat& desired,
65 VideoFormat* best_format) {
66 if (!best_format) {
67 LOG(ERROR) << "PpFrameWriter::GetBestCaptureFormat - "
68 << "best_format is NULL.";
69 return false;
70 }
71
72 // Use the desired format as the best format.
73 best_format->width = desired.width;
74 best_format->height = desired.height;
75 best_format->fourcc = kEffectColorFormat;
76 best_format->interval = desired.interval;
77 return true;
78 }
79
80 bool PpFrameWriter::IsScreencast() const {
81 return false;
82 }
83
84 void PpFrameWriter::PutFrame(PPB_ImageData_Impl* image_data,
85 int64 time_stamp_ns) {
86 base::AutoLock auto_lock(lock_);
87 // This assumes the handler of the SignalFrameCaptured won't call Start/Stop.
88 // TODO(ronghuawu): Avoid the using of lock. One way is to post this call to
89 // libjingle worker thread, which will require an extra copy of |image_data|.
90 // However if pepper host can hand over the ownership of |image_data|
91 // then we can avoid this extra copy.
92 if (!started_) {
93 LOG(ERROR) << "PpFrameWriter::PutFrame - "
94 << "Called when capturer is not started.";
95 return;
96 }
97 ASSERT(image_data != NULL);
98 webkit::ppapi::ImageDataAutoMapper mapper(image_data);
99 if (!mapper.is_valid()) {
100 LOG(ERROR) << "PpFrameWriter::PutFrame - "
101 << "The image could not be mapped and is unusable.";
102 return;
103 }
104 const SkBitmap* bitmap = image_data->GetMappedBitmap();
105 ASSERT(bitmap != NULL);
106
107 cricket::CapturedFrame frame;
108 frame.elapsed_time = 0;
109 frame.time_stamp = time_stamp_ns;
110 frame.pixel_height = 1;
111 frame.pixel_width = 1;
112 frame.width = bitmap->width();
113 frame.height = bitmap->height();
114 if (image_data->format() == PP_IMAGEDATAFORMAT_BGRA_PREMUL) {
115 frame.fourcc = cricket::FOURCC_BGRA;
116 } else {
117 LOG(ERROR) << "PpFrameWriter::PutFrame - Got RGBA which is not supported.";
118 ASSERT(false);
119 return;
120 }
121 frame.data_size = bitmap->getSize();
122 frame.data = bitmap->getPixels();
123
124 // This signals to libJingle that a new VideoFrame is available.
125 // libJingle have no assumptions on what thread this signal come from.
126 SignalFrameCaptured(this, &frame);
127 }
128
129 // PpFrameWriterProxy is a helper class to make sure the user won't use
130 // PpFrameWriter after it is released (IOW its owner - WebMediaStreamTrack -
131 // is released).
132 class PpFrameWriterProxy : public FrameWriterInterface {
133 public:
134 PpFrameWriterProxy(VideoTrackInterface* track,
135 PpFrameWriter* writer)
136 : track_(track),
137 writer_(writer) {
138 ASSERT(writer_ != NULL);
139 }
140
141 virtual ~PpFrameWriterProxy() {}
142
143 virtual void PutFrame(webkit::ppapi::PPB_ImageData_Impl* image_data,
144 int64 time_stamp_ns) OVERRIDE {
145 writer_->PutFrame(image_data, time_stamp_ns);
146 }
147
148 private:
149 scoped_refptr<VideoTrackInterface> track_;
150 PpFrameWriter* writer_;
151
152 DISALLOW_COPY_AND_ASSIGN(PpFrameWriterProxy);
153 };
154
155 bool VideoDestinationHandler::Open(
156 MediaStreamDependencyFactory* factory,
157 MediaStreamRegistryInterface* registry,
158 const std::string& url, FrameWriterInterface** frame_writer) {
wjia(left Chromium) 2013/05/03 21:42:55 nit: one argument per line.
Ronghua Wu (Left Chromium) 2013/05/03 22:40:02 Done.
159 if (!factory) {
160 LOG(ERROR) << "VideoDestinationHandler::Open - factory is NULL.";
161 }
162 WebKit::WebMediaStream stream;
163 if (registry) {
164 stream = registry->GetMediaStream(url);
165 } else {
166 stream =
167 WebKit::WebMediaStreamRegistry::lookupMediaStreamDescriptor(GURL(url));
168 }
169 if (stream.isNull() || !stream.extraData()) {
170 LOG(ERROR) << "VideoDestinationHandler::Open - invalid url: " << url;
171 return false;
172 }
173
174 // Create a new native video track and add it to |stream|.
175 std::string track_id = talk_base::ToString(talk_base::CreateRandomId64());
176 PpFrameWriter* writer = new PpFrameWriter();
177 if (!factory->AddNativeVideoMediaTrack(track_id, &stream, writer)) {
178 delete writer;
179 return false;
180 }
181
182 // Gets a handler to the native video track, which owns the |writer|.
183 MediaStreamExtraData* extra_data =
184 static_cast<MediaStreamExtraData*>(stream.extraData());
185 DCHECK(extra_data);
186 webrtc::MediaStreamInterface* native_stream = extra_data->stream();
187 DCHECK(native_stream);
188 VideoTrackVector video_tracks = native_stream->GetVideoTracks();
189 // Currently one supports one video track per media stream.
190 ASSERT(video_tracks.size() == 1);
191
192 *frame_writer = new PpFrameWriterProxy(video_tracks[0].get(), writer);
193 return true;
194 }
195
196 } // namespace content
197
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698