Home | History | Annotate | Download | only in webrtc
      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/webrtc/video_destination_handler.h"
      6 
      7 #include <string>
      8 
      9 #include "base/base64.h"
     10 #include "base/logging.h"
     11 #include "base/rand_util.h"
     12 #include "base/strings/utf_string_conversions.h"
     13 #include "content/renderer/media/media_stream.h"
     14 #include "content/renderer/media/media_stream_registry_interface.h"
     15 #include "content/renderer/media/media_stream_video_track.h"
     16 #include "content/renderer/pepper/ppb_image_data_impl.h"
     17 #include "content/renderer/render_thread_impl.h"
     18 #include "media/video/capture/video_capture_types.h"
     19 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
     20 #include "third_party/WebKit/public/platform/WebURL.h"
     21 #include "third_party/WebKit/public/web/WebMediaStreamRegistry.h"
     22 #include "third_party/libyuv/include/libyuv/convert.h"
     23 #include "url/gurl.h"
     24 
     25 namespace content {
     26 
     27 class PpFrameWriter::FrameWriterDelegate
     28     : public base::RefCountedThreadSafe<FrameWriterDelegate> {
     29  public:
     30   FrameWriterDelegate(
     31       const scoped_refptr<base::MessageLoopProxy>& io_message_loop_proxy,
     32       const VideoCaptureDeliverFrameCB& new_frame_callback);
     33 
     34   void DeliverFrame(const scoped_refptr<media::VideoFrame>& frame,
     35                     const media::VideoCaptureFormat& format);
     36  private:
     37   friend class base::RefCountedThreadSafe<FrameWriterDelegate>;
     38   virtual ~FrameWriterDelegate();
     39 
     40   void DeliverFrameOnIO(const scoped_refptr<media::VideoFrame>& frame,
     41                         const media::VideoCaptureFormat& format);
     42 
     43   scoped_refptr<base::MessageLoopProxy> io_message_loop_;
     44   VideoCaptureDeliverFrameCB new_frame_callback_;
     45 };
     46 
     47 PpFrameWriter::FrameWriterDelegate::FrameWriterDelegate(
     48     const scoped_refptr<base::MessageLoopProxy>& io_message_loop_proxy,
     49     const VideoCaptureDeliverFrameCB& new_frame_callback)
     50     : io_message_loop_(io_message_loop_proxy),
     51       new_frame_callback_(new_frame_callback) {
     52 }
     53 
     54 PpFrameWriter::FrameWriterDelegate::~FrameWriterDelegate() {
     55 }
     56 
     57 void PpFrameWriter::FrameWriterDelegate::DeliverFrame(
     58     const scoped_refptr<media::VideoFrame>& frame,
     59     const media::VideoCaptureFormat& format) {
     60   io_message_loop_->PostTask(
     61       FROM_HERE,
     62       base::Bind(&FrameWriterDelegate::DeliverFrameOnIO,
     63                  this, frame, format));
     64 }
     65 
     66 void PpFrameWriter::FrameWriterDelegate::DeliverFrameOnIO(
     67      const scoped_refptr<media::VideoFrame>& frame,
     68      const media::VideoCaptureFormat& format) {
     69   DCHECK(io_message_loop_->BelongsToCurrentThread());
     70   // The local time when this frame is generated is unknown so give a null
     71   // value to |estimated_capture_time|.
     72   new_frame_callback_.Run(frame, format, base::TimeTicks());
     73 }
     74 
     75 PpFrameWriter::PpFrameWriter() {
     76   DVLOG(3) << "PpFrameWriter ctor";
     77 }
     78 
     79 PpFrameWriter::~PpFrameWriter() {
     80   DVLOG(3) << "PpFrameWriter dtor";
     81 }
     82 
     83 void PpFrameWriter::GetCurrentSupportedFormats(
     84     int max_requested_width,
     85     int max_requested_height,
     86     const VideoCaptureDeviceFormatsCB& callback) {
     87   DCHECK(CalledOnValidThread());
     88   DVLOG(3) << "PpFrameWriter::GetCurrentSupportedFormats()";
     89   // Since the input is free to change the resolution at any point in time
     90   // the supported formats are unknown.
     91   media::VideoCaptureFormats formats;
     92   callback.Run(formats);
     93 }
     94 
     95 void PpFrameWriter::StartSourceImpl(
     96     const media::VideoCaptureParams& params,
     97     const VideoCaptureDeliverFrameCB& frame_callback) {
     98   DCHECK(CalledOnValidThread());
     99   DCHECK(!delegate_);
    100   DVLOG(3) << "PpFrameWriter::StartSourceImpl()";
    101   delegate_ = new FrameWriterDelegate(io_message_loop(), frame_callback);
    102   OnStartDone(true);
    103 }
    104 
    105 void PpFrameWriter::StopSourceImpl() {
    106   DCHECK(CalledOnValidThread());
    107 }
    108 
    109 void PpFrameWriter::PutFrame(PPB_ImageData_Impl* image_data,
    110                              int64 time_stamp_ns) {
    111   DCHECK(CalledOnValidThread());
    112   DVLOG(3) << "PpFrameWriter::PutFrame()";
    113 
    114   if (!image_data) {
    115     LOG(ERROR) << "PpFrameWriter::PutFrame - Called with NULL image_data.";
    116     return;
    117   }
    118   ImageDataAutoMapper mapper(image_data);
    119   if (!mapper.is_valid()) {
    120     LOG(ERROR) << "PpFrameWriter::PutFrame - "
    121                << "The image could not be mapped and is unusable.";
    122     return;
    123   }
    124   const SkBitmap* bitmap = image_data->GetMappedBitmap();
    125   if (!bitmap) {
    126     LOG(ERROR) << "PpFrameWriter::PutFrame - "
    127                << "The image_data's mapped bitmap is NULL.";
    128     return;
    129   }
    130 
    131   const gfx::Size frame_size(bitmap->width(), bitmap->height());
    132 
    133   if (state() != MediaStreamVideoSource::STARTED)
    134     return;
    135 
    136   const base::TimeDelta timestamp = base::TimeDelta::FromMicroseconds(
    137       time_stamp_ns / base::Time::kNanosecondsPerMicrosecond);
    138 
    139   // TODO(perkj): It would be more efficient to use I420 here. Using YV12 will
    140   // force a copy into a tightly packed I420 frame in
    141   // WebRtcVideoCapturerAdapter before the frame is delivered to libJingle.
    142   // crbug/359587.
    143   scoped_refptr<media::VideoFrame> new_frame =
    144       frame_pool_.CreateFrame(media::VideoFrame::YV12, frame_size,
    145                               gfx::Rect(frame_size), frame_size, timestamp);
    146   media::VideoCaptureFormat format(
    147       frame_size,
    148       MediaStreamVideoSource::kDefaultFrameRate,
    149       media::PIXEL_FORMAT_YV12);
    150 
    151   libyuv::BGRAToI420(reinterpret_cast<uint8*>(bitmap->getPixels()),
    152                      bitmap->rowBytes(),
    153                      new_frame->data(media::VideoFrame::kYPlane),
    154                      new_frame->stride(media::VideoFrame::kYPlane),
    155                      new_frame->data(media::VideoFrame::kUPlane),
    156                      new_frame->stride(media::VideoFrame::kUPlane),
    157                      new_frame->data(media::VideoFrame::kVPlane),
    158                      new_frame->stride(media::VideoFrame::kVPlane),
    159                      frame_size.width(), frame_size.height());
    160 
    161   delegate_->DeliverFrame(new_frame, format);
    162 }
    163 
    164 // PpFrameWriterProxy is a helper class to make sure the user won't use
    165 // PpFrameWriter after it is released (IOW its owner - WebMediaStreamSource -
    166 // is released).
    167 class PpFrameWriterProxy : public FrameWriterInterface {
    168  public:
    169   explicit PpFrameWriterProxy(const base::WeakPtr<PpFrameWriter>& writer)
    170       : writer_(writer) {
    171     DCHECK(writer_ != NULL);
    172   }
    173 
    174   virtual ~PpFrameWriterProxy() {}
    175 
    176   virtual void PutFrame(PPB_ImageData_Impl* image_data,
    177                         int64 time_stamp_ns) OVERRIDE {
    178     writer_->PutFrame(image_data, time_stamp_ns);
    179   }
    180 
    181  private:
    182   base::WeakPtr<PpFrameWriter> writer_;
    183 
    184   DISALLOW_COPY_AND_ASSIGN(PpFrameWriterProxy);
    185 };
    186 
    187 bool VideoDestinationHandler::Open(
    188     MediaStreamRegistryInterface* registry,
    189     const std::string& url,
    190     FrameWriterInterface** frame_writer) {
    191   DVLOG(3) << "VideoDestinationHandler::Open";
    192   blink::WebMediaStream stream;
    193   if (registry) {
    194     stream = registry->GetMediaStream(url);
    195   } else {
    196     stream =
    197         blink::WebMediaStreamRegistry::lookupMediaStreamDescriptor(GURL(url));
    198   }
    199   if (stream.isNull()) {
    200     LOG(ERROR) << "VideoDestinationHandler::Open - invalid url: " << url;
    201     return false;
    202   }
    203 
    204   // Create a new native video track and add it to |stream|.
    205   std::string track_id;
    206   // According to spec, a media stream source's id should be unique per
    207   // application. There's no easy way to strictly achieve that. The id
    208   // generated with this method should be unique for most of the cases but
    209   // theoretically it's possible we can get an id that's duplicated with the
    210   // existing sources.
    211   base::Base64Encode(base::RandBytesAsString(64), &track_id);
    212 
    213   PpFrameWriter* writer = new PpFrameWriter();
    214 
    215   // Create a new webkit video track.
    216   blink::WebMediaStreamSource webkit_source;
    217   blink::WebMediaStreamSource::Type type =
    218       blink::WebMediaStreamSource::TypeVideo;
    219   blink::WebString webkit_track_id = base::UTF8ToUTF16(track_id);
    220   webkit_source.initialize(webkit_track_id, type, webkit_track_id);
    221   webkit_source.setExtraData(writer);
    222 
    223   blink::WebMediaConstraints constraints;
    224   constraints.initialize();
    225   bool track_enabled = true;
    226 
    227   stream.addTrack(MediaStreamVideoTrack::CreateVideoTrack(
    228       writer, constraints, MediaStreamVideoSource::ConstraintsCallback(),
    229       track_enabled));
    230 
    231   *frame_writer = new PpFrameWriterProxy(writer->AsWeakPtr());
    232   return true;
    233 }
    234 
    235 }  // namespace content
    236