| 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/media_stream_source_api.h" |
| 10 #include "content/public/renderer/render_thread.h" |
| 11 #include "media/base/audio_capturer_source.h" |
| 12 #include "media/base/video_capturer_source.h" |
| 13 #include "third_party/WebKit/public/platform/WebMediaStream.h" |
| 14 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" |
| 15 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" |
| 16 |
| 17 // This is a render thread object. |
| 18 class CastStreamingAudioCapturerSource : public media::AudioCapturerSource { |
| 19 public: |
| 20 CastStreamingAudioCapturerSource( |
| 21 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 ~CastStreamingAudioCapturerSource() override; |
| 31 scoped_refptr<CastReceiverSession> cast_receiver_session_; |
| 32 scoped_refptr<CastReceiverAudioValve> audio_valve_; |
| 33 }; |
| 34 |
| 35 // This is a render thread object. |
| 36 class CastStreamingVideoCapturerSource : public media::VideoCapturerSource { |
| 37 public: |
| 38 explicit CastStreamingVideoCapturerSource( |
| 39 scoped_refptr<CastReceiverSession> cast_receiver_session); |
| 40 protected: |
| 41 void GetCurrentSupportedFormats( |
| 42 int max_requested_width, |
| 43 int max_requested_height, |
| 44 double max_requested_frame_rate, |
| 45 const VideoCaptureDeviceFormatsCB& callback) override; |
| 46 void StartCapture( |
| 47 const media::VideoCaptureParams& params, |
| 48 const content::VideoCaptureDeliverFrameCB& frame_callback, |
| 49 scoped_refptr<base::SingleThreadTaskRunner> frame_callback_task_runner, |
| 50 const RunningCallback& running_callback) override; |
| 51 void StopCapture() override; |
| 52 private: |
| 53 ~CastStreamingVideoCapturerSource() override; |
| 54 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 format_ = capture_format; |
| 76 io_message_loop_proxy_->PostTask( |
| 77 FROM_HERE, |
| 78 base::Bind(&CastReceiverSessionDelegate::Start, |
| 79 base::Unretained(delegate_.get()), |
| 80 audio_config, |
| 81 video_config, |
| 82 local_endpoint, |
| 83 remote_endpoint, |
| 84 base::Passed(&options), |
| 85 format_)); |
| 86 blink::WebMediaStreamSource video_source; |
| 87 content::InitializeVideoWebMediaStreamSource( |
| 88 new CastStreamingVideoCapturerSource(this), |
| 89 &video_source); |
| 90 blink::WebVector<blink::WebMediaStreamTrack> video_tracks( |
| 91 static_cast<size_t>(1)); |
| 92 video_tracks[0].initialize(video_source); |
| 93 |
| 94 blink::WebMediaStreamSource audio_source; |
| 95 content::InitializeAudioWebMediaStreamSource( |
| 96 new CastStreamingAudioCapturerSource(this), |
| 97 &audio_source); |
| 98 blink::WebVector<blink::WebMediaStreamTrack> audio_tracks( |
| 99 static_cast<size_t>(1)); |
| 100 audio_tracks[0].initialize(audio_source); |
| 101 |
| 102 blink::WebMediaStream stream; |
| 103 stream.initialize(audio_tracks, video_tracks); |
| 104 |
| 105 base::MessageLoop::current()->PostTask( |
| 106 FROM_HERE, |
| 107 base::Bind(start_callback, stream)); |
| 108 } |
| 109 |
| 110 void CastReceiverSession::StartAudio( |
| 111 scoped_refptr<CastReceiverAudioValve> audio_valve) { |
| 112 io_message_loop_proxy_->PostTask( |
| 113 FROM_HERE, |
| 114 base::Bind(&CastReceiverSessionDelegate::StartAudio, |
| 115 base::Unretained(delegate_.get()), |
| 116 audio_valve)); |
| 117 } |
| 118 |
| 119 void CastReceiverSession::StartVideo( |
| 120 content::VideoCaptureDeliverFrameCB frame_callback) { |
| 121 io_message_loop_proxy_->PostTask( |
| 122 FROM_HERE, |
| 123 base::Bind(&CastReceiverSessionDelegate::StartVideo, |
| 124 base::Unretained(delegate_.get()), |
| 125 frame_callback)); |
| 126 } |
| 127 |
| 128 void CastReceiverSession::StopVideo() { |
| 129 io_message_loop_proxy_->PostTask( |
| 130 FROM_HERE, |
| 131 base::Bind(&CastReceiverSessionDelegate::StopVideo, |
| 132 base::Unretained(delegate_.get()))); |
| 133 } |
| 134 |
| 135 CastStreamingVideoCapturerSource::CastStreamingVideoCapturerSource( |
| 136 scoped_refptr<CastReceiverSession> cast_receiver_session) |
| 137 : cast_receiver_session_(cast_receiver_session) { |
| 138 } |
| 139 |
| 140 CastStreamingVideoCapturerSource::~CastStreamingVideoCapturerSource() {} |
| 141 |
| 142 void CastStreamingVideoCapturerSource::GetCurrentSupportedFormats( |
| 143 int max_requested_width, |
| 144 int max_requested_height, |
| 145 double max_requested_frame_rate, |
| 146 const VideoCaptureDeviceFormatsCB& callback) { |
| 147 std::vector<media::VideoCaptureFormat> formats; |
| 148 if (cast_receiver_session_->format().IsValid()) { |
| 149 formats.push_back(cast_receiver_session_->format()); |
| 150 } |
| 151 callback.Run(formats); |
| 152 } |
| 153 |
| 154 void CastStreamingVideoCapturerSource::StartCapture( |
| 155 const media::VideoCaptureParams& params, |
| 156 const content::VideoCaptureDeliverFrameCB& frame_callback, |
| 157 scoped_refptr<base::SingleThreadTaskRunner> frame_callback_task_runner, |
| 158 const RunningCallback& running_callback) { |
| 159 if (frame_callback_task_runner != |
| 160 content::RenderThread::Get()->GetIOMessageLoopProxy()) { |
| 161 DCHECK(false) << "Only IO thread supported right now."; |
| 162 running_callback.Run(false); |
| 163 return; |
| 164 } |
| 165 cast_receiver_session_->StartVideo(frame_callback); |
| 166 running_callback.Run(true); |
| 167 } |
| 168 |
| 169 void CastStreamingVideoCapturerSource::StopCapture() { |
| 170 cast_receiver_session_->StopVideo(); |
| 171 } |
| 172 |
| 173 CastStreamingAudioCapturerSource::CastStreamingAudioCapturerSource( |
| 174 scoped_refptr<CastReceiverSession> cast_receiver_session) |
| 175 : cast_receiver_session_(cast_receiver_session) { |
| 176 } |
| 177 |
| 178 CastStreamingAudioCapturerSource::~CastStreamingAudioCapturerSource() {} |
| 179 |
| 180 void CastStreamingAudioCapturerSource::Initialize( |
| 181 const media::AudioParameters& params, |
| 182 CaptureCallback* callback, |
| 183 int session_id) { |
| 184 // TODO(hubbe): Consider converting the audio to whatever the caller wants. |
| 185 if (params.sample_rate() != |
| 186 cast_receiver_session_->audio_config_.rtp_timebase || |
| 187 params.channels() != cast_receiver_session_->audio_config_.channels) { |
| 188 callback->OnCaptureError(); |
| 189 return; |
| 190 } |
| 191 audio_valve_ = new CastReceiverAudioValve(callback); |
| 192 } |
| 193 |
| 194 void CastStreamingAudioCapturerSource::Start() { |
| 195 cast_receiver_session_->StartAudio(audio_valve_); |
| 196 } |
| 197 |
| 198 void CastStreamingAudioCapturerSource::Stop() { |
| 199 audio_valve_->Stop(); |
| 200 } |
| 201 |
| 202 void CastStreamingAudioCapturerSource::SetVolume(double volume) { |
| 203 // not supported |
| 204 } |
| 205 |
| 206 void CastStreamingAudioCapturerSource::SetAutomaticGainControl(bool enable) { |
| 207 // not supported |
| 208 } |
| OLD | NEW |