Chromium Code Reviews| 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 "content/public/renderer/media_stream_source_api.h" | |
| 9 #include "content/public/renderer/render_thread.h" | |
| 10 #include "media/base/audio_capturer_source.h" | |
| 11 #include "media/video/capture/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 class CastStreamingAudioCaptureSource : public media::AudioCapturerSource { | |
|
Alpha Left Google
2015/02/04 03:07:52
Should be CastSreamingAudioCapturerSource.
hubbe
2015/02/05 20:22:59
Done.
| |
| 17 public: | |
| 18 CastStreamingAudioCaptureSource( | |
| 19 scoped_refptr<CastReceiverSession> cast_receiver_session); | |
| 20 void Initialize(const media::AudioParameters& params, | |
| 21 CaptureCallback* callback, | |
| 22 int session_id) override; | |
| 23 void Start() override; | |
| 24 void Stop() override; | |
| 25 void SetVolume(double volume) override; | |
| 26 void SetAutomaticGainControl(bool enable) override; | |
| 27 private: | |
| 28 ~CastStreamingAudioCaptureSource() override; | |
| 29 scoped_refptr<CastReceiverSession> cast_receiver_session_; | |
| 30 CaptureCallback* capture_callback_; // Ownership?? | |
| 31 }; | |
| 32 | |
| 33 class CastVideoCapturerSource : public media::VideoCapturerSource { | |
|
Alpha Left Google
2015/02/04 03:07:52
Should be CastStreamingvideoCapturerSource to be c
hubbe
2015/02/05 20:22:59
Done.
| |
| 34 public: | |
| 35 explicit CastVideoCapturerSource( | |
| 36 scoped_refptr<CastReceiverSession> cast_receiver_session); | |
| 37 protected: | |
| 38 void GetCurrentSupportedFormats( | |
| 39 int max_requested_width, | |
| 40 int max_requested_height, | |
| 41 double max_requested_frame_rate, | |
| 42 const VideoCaptureDeviceFormatsCB& callback) override; | |
| 43 void StartCapture( | |
| 44 const media::VideoCaptureParams& params, | |
| 45 const content::VideoCaptureDeliverFrameCB& frame_callback, | |
| 46 const RunningCallback& running_callback) override; | |
| 47 void StopCapture() override; | |
| 48 private: | |
| 49 ~CastVideoCapturerSource() override; | |
| 50 scoped_refptr<CastReceiverSession> cast_receiver_session_; | |
| 51 }; | |
| 52 | |
| 53 CastReceiverSession::CastReceiverSession() | |
| 54 : delegate_(new CastReceiverSessionDelegate()), | |
| 55 io_message_loop_proxy_( | |
| 56 content::RenderThread::Get()->GetIOMessageLoopProxy()) {} | |
| 57 | |
| 58 CastReceiverSession::~CastReceiverSession() { | |
| 59 // We should always be able to delete the object on the IO thread. | |
| 60 CHECK(io_message_loop_proxy_->DeleteSoon(FROM_HERE, delegate_.release())); | |
| 61 } | |
| 62 | |
| 63 void CastReceiverSession::Start( | |
| 64 const media::cast::FrameReceiverConfig& audio_config, | |
| 65 const media::cast::FrameReceiverConfig& video_config, | |
| 66 const net::IPEndPoint& local_endpoint, | |
| 67 const net::IPEndPoint& remote_endpoint, | |
| 68 scoped_ptr<base::DictionaryValue> options, | |
| 69 const media::VideoCaptureFormat& capture_format, | |
| 70 const StartCB& start_callback) { | |
| 71 format_ = capture_format; | |
| 72 io_message_loop_proxy_->PostTask( | |
| 73 FROM_HERE, | |
| 74 base::Bind(&CastReceiverSessionDelegate::Start, | |
| 75 base::Unretained(delegate_.get()), | |
| 76 audio_config, | |
| 77 video_config, | |
| 78 local_endpoint, | |
| 79 remote_endpoint, | |
| 80 base::Passed(&options), | |
| 81 format_)); | |
| 82 blink::WebMediaStreamSource video_source; | |
| 83 content::InitializeVideoWebMediaSource(new CastVideoCapturerSource(this), | |
| 84 &video_source); | |
| 85 blink::WebVector<blink::WebMediaStreamTrack> video_tracks( | |
| 86 static_cast<size_t>(1)); | |
| 87 video_tracks[0].initialize(video_source); | |
| 88 | |
| 89 blink::WebMediaStreamSource audio_source; | |
| 90 content::InitializeAudioWebMediaSource( | |
| 91 new CastStreamingAudioCaptureSource(this), | |
| 92 &audio_source); | |
| 93 blink::WebVector<blink::WebMediaStreamTrack> audio_tracks( | |
| 94 static_cast<size_t>(1)); | |
| 95 audio_tracks[0].initialize(audio_source); | |
| 96 | |
| 97 blink::WebMediaStream stream; | |
| 98 stream.initialize(audio_tracks, video_tracks); | |
| 99 | |
| 100 base::MessageLoop::current()->PostTask( | |
| 101 FROM_HERE, | |
| 102 base::Bind(start_callback, stream)); | |
| 103 } | |
| 104 | |
| 105 void CastReceiverSession::StartAudio( | |
| 106 CastReceiverSessionDelegate::AudioCB audio_callback) { | |
| 107 io_message_loop_proxy_->PostTask( | |
| 108 FROM_HERE, | |
| 109 base::Bind(&CastReceiverSessionDelegate::StartAudio, | |
| 110 base::Unretained(delegate_.get()), | |
| 111 audio_callback)); | |
| 112 } | |
| 113 | |
| 114 void CastReceiverSession::StopAudio(base::WaitableEvent* event) { | |
| 115 io_message_loop_proxy_->PostTask( | |
| 116 FROM_HERE, | |
| 117 base::Bind(&CastReceiverSessionDelegate::StopAudio, | |
| 118 base::Unretained(delegate_.get()), | |
| 119 event)); | |
| 120 } | |
| 121 | |
| 122 void CastReceiverSession::StartVideo( | |
| 123 content::VideoCaptureDeliverFrameCB frame_callback) { | |
| 124 io_message_loop_proxy_->PostTask( | |
| 125 FROM_HERE, | |
| 126 base::Bind(&CastReceiverSessionDelegate::StartVideo, | |
| 127 base::Unretained(delegate_.get()), | |
| 128 frame_callback)); | |
| 129 } | |
| 130 | |
| 131 void CastReceiverSession::StopVideo(base::WaitableEvent* event) { | |
| 132 io_message_loop_proxy_->PostTask( | |
| 133 FROM_HERE, | |
| 134 base::Bind(&CastReceiverSessionDelegate::StopVideo, | |
| 135 base::Unretained(delegate_.get()), | |
| 136 event)); | |
| 137 } | |
| 138 | |
| 139 | |
| 140 CastVideoCapturerSource::CastVideoCapturerSource( | |
| 141 scoped_refptr<CastReceiverSession> cast_receiver_session) | |
| 142 : cast_receiver_session_(cast_receiver_session) { | |
| 143 } | |
| 144 | |
| 145 CastVideoCapturerSource::~CastVideoCapturerSource() {} | |
| 146 | |
| 147 void CastVideoCapturerSource::GetCurrentSupportedFormats( | |
| 148 int max_requested_width, | |
| 149 int max_requested_height, | |
| 150 double max_requested_frame_rate, | |
| 151 const VideoCaptureDeviceFormatsCB& callback) { | |
| 152 std::vector<media::VideoCaptureFormat> formats; | |
| 153 if (cast_receiver_session_->format().IsValid()) { | |
| 154 formats.push_back(cast_receiver_session_->format()); | |
| 155 } | |
| 156 callback.Run(formats); | |
| 157 } | |
| 158 | |
| 159 void CastVideoCapturerSource::StartCapture( | |
| 160 const media::VideoCaptureParams& params, | |
| 161 const content::VideoCaptureDeliverFrameCB& frame_callback, | |
| 162 const RunningCallback& running_callback) { | |
| 163 cast_receiver_session_->StartVideo(frame_callback); | |
| 164 } | |
| 165 | |
| 166 void CastVideoCapturerSource::StopCapture() { | |
| 167 base::WaitableEvent event(true, false); | |
|
Alpha Left Google
2015/02/04 03:07:52
There is no need to stop and wait. |frame_callback
hubbe
2015/02/05 20:22:59
Done.
| |
| 168 cast_receiver_session_->StopVideo(&event); | |
| 169 event.Wait(); | |
| 170 } | |
| 171 | |
| 172 CastStreamingAudioCaptureSource::CastStreamingAudioCaptureSource( | |
| 173 scoped_refptr<CastReceiverSession> cast_receiver_session) | |
| 174 : cast_receiver_session_(cast_receiver_session) { | |
| 175 } | |
| 176 | |
| 177 CastStreamingAudioCaptureSource::~CastStreamingAudioCaptureSource() {} | |
| 178 | |
| 179 void CastStreamingAudioCaptureSource::Initialize( | |
| 180 const media::AudioParameters& params, | |
| 181 CaptureCallback* callback, | |
| 182 int session_id) { | |
| 183 if (params.sample_rate() != 48000 || | |
|
Alpha Left Google
2015/02/04 03:07:52
Need a comment or TODO to fix this limitation.
hubbe
2015/02/05 20:22:59
Improved to not hardcode values and added TODO to
| |
| 184 params.bits_per_sample() != 16 || | |
| 185 params.channels() != 2) { | |
| 186 callback->OnCaptureError(); | |
| 187 return; | |
| 188 } | |
| 189 capture_callback_ = callback; | |
| 190 } | |
| 191 | |
| 192 void CastStreamingAudioCaptureSource::Start() { | |
| 193 cast_receiver_session_->StartAudio( | |
| 194 base::Bind(&media::AudioCapturerSource::CaptureCallback::Capture, | |
| 195 base::Unretained(capture_callback_))); | |
| 196 } | |
| 197 | |
| 198 void CastStreamingAudioCaptureSource::Stop() { | |
| 199 base::WaitableEvent event(true, false); | |
|
Alpha Left Google
2015/02/04 03:07:52
Instead of blocking here what about having a third
hubbe
2015/02/05 20:22:59
Done.
| |
| 200 cast_receiver_session_->StopAudio(&event); | |
| 201 event.Wait(); | |
| 202 } | |
| 203 | |
| 204 void CastStreamingAudioCaptureSource::SetVolume(double volume) { | |
| 205 // not supported | |
| 206 } | |
| 207 | |
| 208 void CastStreamingAudioCaptureSource::SetAutomaticGainControl(bool enable) { | |
| 209 // not supported | |
| 210 } | |
| OLD | NEW |