OLD | NEW |
(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, |
| 159 FrameWriterInterface** frame_writer) { |
| 160 if (!factory) { |
| 161 factory = RenderThreadImpl::current()->GetMediaStreamDependencyFactory(); |
| 162 ASSERT(factory != NULL); |
| 163 } |
| 164 WebKit::WebMediaStream stream; |
| 165 if (registry) { |
| 166 stream = registry->GetMediaStream(url); |
| 167 } else { |
| 168 stream = |
| 169 WebKit::WebMediaStreamRegistry::lookupMediaStreamDescriptor(GURL(url)); |
| 170 } |
| 171 if (stream.isNull() || !stream.extraData()) { |
| 172 LOG(ERROR) << "VideoDestinationHandler::Open - invalid url: " << url; |
| 173 return false; |
| 174 } |
| 175 |
| 176 // Create a new native video track and add it to |stream|. |
| 177 std::string track_id = talk_base::ToString(talk_base::CreateRandomId64()); |
| 178 PpFrameWriter* writer = new PpFrameWriter(); |
| 179 if (!factory->AddNativeVideoMediaTrack(track_id, &stream, writer)) { |
| 180 delete writer; |
| 181 return false; |
| 182 } |
| 183 |
| 184 // Gets a handler to the native video track, which owns the |writer|. |
| 185 MediaStreamExtraData* extra_data = |
| 186 static_cast<MediaStreamExtraData*>(stream.extraData()); |
| 187 DCHECK(extra_data); |
| 188 webrtc::MediaStreamInterface* native_stream = extra_data->stream(); |
| 189 DCHECK(native_stream); |
| 190 VideoTrackVector video_tracks = native_stream->GetVideoTracks(); |
| 191 // Currently one supports one video track per media stream. |
| 192 ASSERT(video_tracks.size() == 1); |
| 193 |
| 194 *frame_writer = new PpFrameWriterProxy(video_tracks[0].get(), writer); |
| 195 return true; |
| 196 } |
| 197 |
| 198 } // namespace content |
| 199 |
OLD | NEW |