OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "chrome/renderer/media/cast_receiver_session.h" |
| 6 |
| 7 #include "base/synchronization/waitable_event.h" |
| 8 #include "chrome/renderer/media/cast_receiver_audio_valve.h" |
| 9 #include "content/public/renderer/render_thread.h" |
| 10 #include "media/base/audio_capturer_source.h" |
| 11 #include "media/base/video_capturer_source.h" |
| 12 #include "third_party/WebKit/public/platform/WebMediaStream.h" |
| 13 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" |
| 14 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" |
| 15 |
| 16 // This is a render thread object. |
| 17 class CastReceiverSession::AudioCapturerSource : |
| 18 public media::AudioCapturerSource { |
| 19 public: |
| 20 AudioCapturerSource( |
| 21 const scoped_refptr<CastReceiverSession> cast_receiver_session); |
| 22 void Initialize(const media::AudioParameters& params, |
| 23 CaptureCallback* callback, |
| 24 int session_id) override; |
| 25 void Start() override; |
| 26 void Stop() override; |
| 27 void SetVolume(double volume) override; |
| 28 void SetAutomaticGainControl(bool enable) override; |
| 29 private: |
| 30 ~AudioCapturerSource() override; |
| 31 const scoped_refptr<CastReceiverSession> cast_receiver_session_; |
| 32 scoped_refptr<CastReceiverAudioValve> audio_valve_; |
| 33 }; |
| 34 |
| 35 // This is a render thread object. |
| 36 class CastReceiverSession::VideoCapturerSource : |
| 37 public media::VideoCapturerSource { |
| 38 public: |
| 39 explicit VideoCapturerSource( |
| 40 const scoped_refptr<CastReceiverSession> cast_receiver_session); |
| 41 protected: |
| 42 void GetCurrentSupportedFormats( |
| 43 int max_requested_width, |
| 44 int max_requested_height, |
| 45 double max_requested_frame_rate, |
| 46 const VideoCaptureDeviceFormatsCB& callback) override; |
| 47 void StartCapture( |
| 48 const media::VideoCaptureParams& params, |
| 49 const content::VideoCaptureDeliverFrameCB& frame_callback, |
| 50 scoped_refptr<base::SingleThreadTaskRunner> frame_callback_task_runner, |
| 51 const RunningCallback& running_callback) override; |
| 52 void StopCapture() override; |
| 53 private: |
| 54 const scoped_refptr<CastReceiverSession> cast_receiver_session_; |
| 55 }; |
| 56 |
| 57 CastReceiverSession::CastReceiverSession() |
| 58 : delegate_(new CastReceiverSessionDelegate()), |
| 59 io_message_loop_proxy_( |
| 60 content::RenderThread::Get()->GetIOMessageLoopProxy()) {} |
| 61 |
| 62 CastReceiverSession::~CastReceiverSession() { |
| 63 // We should always be able to delete the object on the IO thread. |
| 64 CHECK(io_message_loop_proxy_->DeleteSoon(FROM_HERE, delegate_.release())); |
| 65 } |
| 66 |
| 67 void CastReceiverSession::Start( |
| 68 const media::cast::FrameReceiverConfig& audio_config, |
| 69 const media::cast::FrameReceiverConfig& video_config, |
| 70 const net::IPEndPoint& local_endpoint, |
| 71 const net::IPEndPoint& remote_endpoint, |
| 72 scoped_ptr<base::DictionaryValue> options, |
| 73 const media::VideoCaptureFormat& capture_format, |
| 74 const StartCB& start_callback) { |
| 75 audio_config_ = audio_config; |
| 76 video_config_ = video_config; |
| 77 format_ = capture_format; |
| 78 io_message_loop_proxy_->PostTask( |
| 79 FROM_HERE, |
| 80 base::Bind(&CastReceiverSessionDelegate::Start, |
| 81 base::Unretained(delegate_.get()), |
| 82 audio_config, |
| 83 video_config, |
| 84 local_endpoint, |
| 85 remote_endpoint, |
| 86 base::Passed(&options), |
| 87 format_)); |
| 88 scoped_refptr<media::AudioCapturerSource> audio( |
| 89 new CastReceiverSession::AudioCapturerSource(this)); |
| 90 scoped_ptr<media::VideoCapturerSource> video( |
| 91 new CastReceiverSession::VideoCapturerSource(this)); |
| 92 base::MessageLoop::current()->PostTask( |
| 93 FROM_HERE, |
| 94 base::Bind(start_callback, audio, base::Passed(&video))); |
| 95 } |
| 96 |
| 97 void CastReceiverSession::StartAudio( |
| 98 scoped_refptr<CastReceiverAudioValve> audio_valve) { |
| 99 io_message_loop_proxy_->PostTask( |
| 100 FROM_HERE, |
| 101 base::Bind(&CastReceiverSessionDelegate::StartAudio, |
| 102 base::Unretained(delegate_.get()), |
| 103 audio_valve)); |
| 104 } |
| 105 |
| 106 void CastReceiverSession::StartVideo( |
| 107 content::VideoCaptureDeliverFrameCB frame_callback) { |
| 108 io_message_loop_proxy_->PostTask( |
| 109 FROM_HERE, |
| 110 base::Bind(&CastReceiverSessionDelegate::StartVideo, |
| 111 base::Unretained(delegate_.get()), |
| 112 frame_callback)); |
| 113 } |
| 114 |
| 115 void CastReceiverSession::StopVideo() { |
| 116 io_message_loop_proxy_->PostTask( |
| 117 FROM_HERE, |
| 118 base::Bind(&CastReceiverSessionDelegate::StopVideo, |
| 119 base::Unretained(delegate_.get()))); |
| 120 } |
| 121 |
| 122 CastReceiverSession::VideoCapturerSource::VideoCapturerSource( |
| 123 const scoped_refptr<CastReceiverSession> cast_receiver_session) |
| 124 : cast_receiver_session_(cast_receiver_session) { |
| 125 } |
| 126 |
| 127 void CastReceiverSession::VideoCapturerSource::GetCurrentSupportedFormats( |
| 128 int max_requested_width, |
| 129 int max_requested_height, |
| 130 double max_requested_frame_rate, |
| 131 const VideoCaptureDeviceFormatsCB& callback) { |
| 132 std::vector<media::VideoCaptureFormat> formats; |
| 133 if (cast_receiver_session_->format_.IsValid()) { |
| 134 formats.push_back(cast_receiver_session_->format_); |
| 135 } |
| 136 callback.Run(formats); |
| 137 } |
| 138 |
| 139 void CastReceiverSession::VideoCapturerSource::StartCapture( |
| 140 const media::VideoCaptureParams& params, |
| 141 const content::VideoCaptureDeliverFrameCB& frame_callback, |
| 142 scoped_refptr<base::SingleThreadTaskRunner> frame_callback_task_runner, |
| 143 const RunningCallback& running_callback) { |
| 144 if (frame_callback_task_runner != |
| 145 content::RenderThread::Get()->GetIOMessageLoopProxy()) { |
| 146 DCHECK(false) << "Only IO thread supported right now."; |
| 147 running_callback.Run(false); |
| 148 return; |
| 149 } |
| 150 cast_receiver_session_->StartVideo(frame_callback); |
| 151 running_callback.Run(true); |
| 152 } |
| 153 |
| 154 void CastReceiverSession::VideoCapturerSource::StopCapture() { |
| 155 cast_receiver_session_->StopVideo(); |
| 156 } |
| 157 |
| 158 CastReceiverSession::AudioCapturerSource::AudioCapturerSource( |
| 159 const scoped_refptr<CastReceiverSession> cast_receiver_session) |
| 160 : cast_receiver_session_(cast_receiver_session) { |
| 161 } |
| 162 |
| 163 CastReceiverSession::AudioCapturerSource::~AudioCapturerSource() { |
| 164 DCHECK(!audio_valve_); |
| 165 } |
| 166 |
| 167 void CastReceiverSession::AudioCapturerSource::Initialize( |
| 168 const media::AudioParameters& params, |
| 169 CaptureCallback* callback, |
| 170 int session_id) { |
| 171 // TODO(hubbe): Consider converting the audio to whatever the caller wants. |
| 172 if (params.sample_rate() != |
| 173 cast_receiver_session_->audio_config_.rtp_timebase || |
| 174 params.channels() != cast_receiver_session_->audio_config_.channels) { |
| 175 callback->OnCaptureError(); |
| 176 return; |
| 177 } |
| 178 audio_valve_ = new CastReceiverAudioValve(callback); |
| 179 } |
| 180 |
| 181 void CastReceiverSession::AudioCapturerSource::Start() { |
| 182 DCHECK(audio_valve_); |
| 183 cast_receiver_session_->StartAudio(audio_valve_); |
| 184 } |
| 185 |
| 186 void CastReceiverSession::AudioCapturerSource::Stop() { |
| 187 audio_valve_->Stop(); |
| 188 audio_valve_ = nullptr; |
| 189 } |
| 190 |
| 191 void CastReceiverSession::AudioCapturerSource::SetVolume(double volume) { |
| 192 // not supported |
| 193 } |
| 194 |
| 195 void CastReceiverSession::AudioCapturerSource::SetAutomaticGainControl( |
| 196 bool enable) { |
| 197 // not supported |
| 198 } |
OLD | NEW |