Home | History | Annotate | Download | only in media
      1 // Copyright 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/media/mock_media_stream_video_source.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/callback_helpers.h"
      9 #include "base/location.h"
     10 
     11 namespace content {
     12 
     13 MockMediaStreamVideoSource::MockMediaStreamVideoSource(
     14     bool manual_get_supported_formats)
     15     : manual_get_supported_formats_(manual_get_supported_formats),
     16       max_requested_height_(0),
     17       max_requested_width_(0),
     18       max_requested_frame_rate_(0.0),
     19       attempted_to_start_(false) {
     20   supported_formats_.push_back(
     21       media::VideoCaptureFormat(
     22           gfx::Size(MediaStreamVideoSource::kDefaultWidth,
     23                     MediaStreamVideoSource::kDefaultHeight),
     24           MediaStreamVideoSource::kDefaultFrameRate,
     25           media::PIXEL_FORMAT_I420));
     26 }
     27 
     28 MockMediaStreamVideoSource::~MockMediaStreamVideoSource() {}
     29 
     30 void MockMediaStreamVideoSource::StartMockedSource() {
     31   DCHECK(attempted_to_start_);
     32   attempted_to_start_ = false;
     33   OnStartDone(MEDIA_DEVICE_OK);
     34 }
     35 
     36 void MockMediaStreamVideoSource::FailToStartMockedSource() {
     37   DCHECK(attempted_to_start_);
     38   attempted_to_start_ = false;
     39   OnStartDone(MEDIA_DEVICE_TRACK_START_FAILURE);
     40 }
     41 
     42 void MockMediaStreamVideoSource::CompleteGetSupportedFormats() {
     43   DCHECK(!formats_callback_.is_null());
     44   base::ResetAndReturn(&formats_callback_).Run(supported_formats_);
     45 }
     46 
     47 void MockMediaStreamVideoSource::GetCurrentSupportedFormats(
     48     int max_requested_height,
     49     int max_requested_width,
     50     double max_requested_frame_rate,
     51     const VideoCaptureDeviceFormatsCB& callback) {
     52   DCHECK(formats_callback_.is_null());
     53   max_requested_height_ = max_requested_height;
     54   max_requested_width_ = max_requested_width;
     55   max_requested_frame_rate_ = max_requested_frame_rate;
     56 
     57   if (manual_get_supported_formats_) {
     58     formats_callback_ = callback;
     59     return;
     60   }
     61   callback.Run(supported_formats_);
     62 }
     63 
     64 void MockMediaStreamVideoSource::StartSourceImpl(
     65     const media::VideoCaptureFormat& format,
     66     const VideoCaptureDeliverFrameCB& frame_callback) {
     67   DCHECK(frame_callback_.is_null());
     68   format_ = format;
     69   attempted_to_start_ = true;
     70   frame_callback_ = frame_callback;
     71 }
     72 
     73 void MockMediaStreamVideoSource::StopSourceImpl() {
     74 }
     75 
     76 void MockMediaStreamVideoSource::DeliverVideoFrame(
     77     const scoped_refptr<media::VideoFrame>& frame) {
     78   DCHECK(!frame_callback_.is_null());
     79   io_message_loop()->PostTask(
     80       FROM_HERE,
     81       base::Bind(&MockMediaStreamVideoSource::DeliverVideoFrameOnIO,
     82                  base::Unretained(this), frame, format_,
     83                  base::TimeTicks(), frame_callback_));
     84 }
     85 
     86 void MockMediaStreamVideoSource::DeliverVideoFrameOnIO(
     87     const scoped_refptr<media::VideoFrame>& frame,
     88     media::VideoCaptureFormat format,
     89     const base::TimeTicks& estimated_capture_time,
     90     const VideoCaptureDeliverFrameCB& frame_callback) {
     91   frame_callback.Run(frame, format, estimated_capture_time);
     92 }
     93 
     94 }  // namespace content
     95