| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/renderer/media/cast_rtp_stream.h" | 5 #include "chrome/renderer/media/cast_rtp_stream.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
| 10 #include "chrome/renderer/media/cast_session.h" | 10 #include "chrome/renderer/media/cast_session.h" |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 DCHECK(render_thread_task_runner_->BelongsToCurrentThread()); | 137 DCHECK(render_thread_task_runner_->BelongsToCurrentThread()); |
| 138 DCHECK(frame_input_); | 138 DCHECK(frame_input_); |
| 139 // TODO(hclam): Pass in the accurate capture time to have good | 139 // TODO(hclam): Pass in the accurate capture time to have good |
| 140 // audio/video sync. | 140 // audio/video sync. |
| 141 frame_input_->InsertRawVideoFrame(frame, base::TimeTicks::Now()); | 141 frame_input_->InsertRawVideoFrame(frame, base::TimeTicks::Now()); |
| 142 } | 142 } |
| 143 | 143 |
| 144 // Attach this sink to MediaStreamTrack. This method call must | 144 // Attach this sink to MediaStreamTrack. This method call must |
| 145 // be made on the render thread. Incoming data can then be | 145 // be made on the render thread. Incoming data can then be |
| 146 // passed to media::cast::FrameInput on any thread. | 146 // passed to media::cast::FrameInput on any thread. |
| 147 void AddToTrack(const scoped_refptr<media::cast::FrameInput>& frame_input) { | 147 void AddToTrack( |
| 148 const scoped_refptr<media::cast::VideoFrameInput>& frame_input) { |
| 148 DCHECK(render_thread_task_runner_->BelongsToCurrentThread()); | 149 DCHECK(render_thread_task_runner_->BelongsToCurrentThread()); |
| 149 | 150 |
| 150 frame_input_ = frame_input; | 151 frame_input_ = frame_input; |
| 151 if (!sink_added_) { | 152 if (!sink_added_) { |
| 152 AddToVideoTrack(this, track_); | 153 AddToVideoTrack(this, track_); |
| 153 sink_added_ = true; | 154 sink_added_ = true; |
| 154 } | 155 } |
| 155 } | 156 } |
| 156 | 157 |
| 157 private: | 158 private: |
| 158 blink::WebMediaStreamTrack track_; | 159 blink::WebMediaStreamTrack track_; |
| 159 scoped_refptr<media::cast::FrameInput> frame_input_; | 160 scoped_refptr<media::cast::VideoFrameInput> frame_input_; |
| 160 bool sink_added_; | 161 bool sink_added_; |
| 161 CastRtpStream::ErrorCallback error_callback_; | 162 CastRtpStream::ErrorCallback error_callback_; |
| 162 scoped_refptr<base::SingleThreadTaskRunner> render_thread_task_runner_; | 163 scoped_refptr<base::SingleThreadTaskRunner> render_thread_task_runner_; |
| 163 | 164 |
| 164 DISALLOW_COPY_AND_ASSIGN(CastVideoSink); | 165 DISALLOW_COPY_AND_ASSIGN(CastVideoSink); |
| 165 }; | 166 }; |
| 166 | 167 |
| 167 // Receives audio data from a MediaStreamTrack. Data is submitted to | 168 // Receives audio data from a MediaStreamTrack. Data is submitted to |
| 168 // media::cast::FrameInput. | 169 // media::cast::FrameInput. |
| 169 // | 170 // |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 DCHECK(frame_input_); | 222 DCHECK(frame_input_); |
| 222 frame_input_->InsertAudio(audio_bus_ptr, recorded_time, done_callback); | 223 frame_input_->InsertAudio(audio_bus_ptr, recorded_time, done_callback); |
| 223 } | 224 } |
| 224 | 225 |
| 225 // Called on real-time audio thread. | 226 // Called on real-time audio thread. |
| 226 virtual void OnSetFormat(const media::AudioParameters& params) OVERRIDE { | 227 virtual void OnSetFormat(const media::AudioParameters& params) OVERRIDE { |
| 227 NOTIMPLEMENTED(); | 228 NOTIMPLEMENTED(); |
| 228 } | 229 } |
| 229 | 230 |
| 230 // See CastVideoSink for details. | 231 // See CastVideoSink for details. |
| 231 void AddToTrack(const scoped_refptr<media::cast::FrameInput>& frame_input) { | 232 void AddToTrack( |
| 233 const scoped_refptr<media::cast::AudioFrameInput>& frame_input) { |
| 232 DCHECK(render_thread_task_runner_->BelongsToCurrentThread()); | 234 DCHECK(render_thread_task_runner_->BelongsToCurrentThread()); |
| 233 frame_input_ = frame_input; | 235 frame_input_ = frame_input; |
| 234 if (!sink_added_) { | 236 if (!sink_added_) { |
| 235 AddToAudioTrack(this, track_); | 237 AddToAudioTrack(this, track_); |
| 236 sink_added_ = true; | 238 sink_added_ = true; |
| 237 } | 239 } |
| 238 } | 240 } |
| 239 | 241 |
| 240 private: | 242 private: |
| 241 blink::WebMediaStreamTrack track_; | 243 blink::WebMediaStreamTrack track_; |
| 242 scoped_refptr<media::cast::FrameInput> frame_input_; | 244 scoped_refptr<media::cast::AudioFrameInput> frame_input_; |
| 243 bool sink_added_; | 245 bool sink_added_; |
| 244 CastRtpStream::ErrorCallback error_callback_; | 246 CastRtpStream::ErrorCallback error_callback_; |
| 245 base::WeakPtrFactory<CastAudioSink> weak_factory_; | 247 base::WeakPtrFactory<CastAudioSink> weak_factory_; |
| 246 scoped_refptr<base::SingleThreadTaskRunner> render_thread_task_runner_; | 248 scoped_refptr<base::SingleThreadTaskRunner> render_thread_task_runner_; |
| 247 | 249 |
| 248 DISALLOW_COPY_AND_ASSIGN(CastAudioSink); | 250 DISALLOW_COPY_AND_ASSIGN(CastAudioSink); |
| 249 }; | 251 }; |
| 250 | 252 |
| 251 CastRtpParams::CastRtpParams(const CastRtpPayloadParams& payload_params) | 253 CastRtpParams::CastRtpParams(const CastRtpPayloadParams& payload_params) |
| 252 : payload(payload_params) {} | 254 : payload(payload_params) {} |
| 253 | 255 |
| 254 CastCodecSpecificParams::CastCodecSpecificParams() {} | 256 CastCodecSpecificParams::CastCodecSpecificParams() {} |
| 255 | 257 |
| 256 CastCodecSpecificParams::~CastCodecSpecificParams() {} | 258 CastCodecSpecificParams::~CastCodecSpecificParams() {} |
| 257 | 259 |
| 258 CastRtpPayloadParams::CastRtpPayloadParams() | 260 CastRtpPayloadParams::CastRtpPayloadParams() |
| 259 : payload_type(0), | 261 : payload_type(0), |
| 260 ssrc(0), | 262 ssrc(0), |
| 261 feedback_ssrc(0), | 263 feedback_ssrc(0), |
| 262 clock_rate(0), | 264 clock_rate(0), |
| 263 max_bitrate(0), | 265 max_bitrate(0), |
| 264 min_bitrate(0), | 266 min_bitrate(0), |
| 265 channels(0), | 267 channels(0), |
| 266 width(0), | 268 width(0), |
| 267 height(0) { | 269 height(0) {} |
| 268 } | |
| 269 | 270 |
| 270 CastRtpPayloadParams::~CastRtpPayloadParams() { | 271 CastRtpPayloadParams::~CastRtpPayloadParams() {} |
| 271 } | |
| 272 | 272 |
| 273 CastRtpParams::CastRtpParams() { | 273 CastRtpParams::CastRtpParams() {} |
| 274 } | |
| 275 | 274 |
| 276 CastRtpParams::~CastRtpParams() { | 275 CastRtpParams::~CastRtpParams() {} |
| 277 } | |
| 278 | 276 |
| 279 CastRtpStream::CastRtpStream(const blink::WebMediaStreamTrack& track, | 277 CastRtpStream::CastRtpStream(const blink::WebMediaStreamTrack& track, |
| 280 const scoped_refptr<CastSession>& session) | 278 const scoped_refptr<CastSession>& session) |
| 281 : track_(track), | 279 : track_(track), cast_session_(session), weak_factory_(this) {} |
| 282 cast_session_(session), | |
| 283 weak_factory_(this) {} | |
| 284 | 280 |
| 285 CastRtpStream::~CastRtpStream() { | 281 CastRtpStream::~CastRtpStream() {} |
| 286 } | |
| 287 | 282 |
| 288 std::vector<CastRtpParams> CastRtpStream::GetSupportedParams() { | 283 std::vector<CastRtpParams> CastRtpStream::GetSupportedParams() { |
| 289 if (IsAudio()) | 284 if (IsAudio()) |
| 290 return SupportedAudioParams(); | 285 return SupportedAudioParams(); |
| 291 else | 286 else |
| 292 return SupportedVideoParams(); | 287 return SupportedVideoParams(); |
| 293 } | 288 } |
| 294 | 289 |
| 295 CastRtpParams CastRtpStream::GetParams() { | 290 CastRtpParams CastRtpStream::GetParams() { return params_; } |
| 296 return params_; | |
| 297 } | |
| 298 | 291 |
| 299 void CastRtpStream::Start(const CastRtpParams& params, | 292 void CastRtpStream::Start(const CastRtpParams& params, |
| 300 const base::Closure& start_callback, | 293 const base::Closure& start_callback, |
| 301 const base::Closure& stop_callback, | 294 const base::Closure& stop_callback, |
| 302 const ErrorCallback& error_callback) { | 295 const ErrorCallback& error_callback) { |
| 303 stop_callback_ = stop_callback; | 296 stop_callback_ = stop_callback; |
| 304 error_callback_ = error_callback; | 297 error_callback_ = error_callback; |
| 305 | 298 |
| 306 if (IsAudio()) { | 299 if (IsAudio()) { |
| 307 AudioSenderConfig config; | 300 AudioSenderConfig config; |
| 308 if (!ToAudioSenderConfig(params, &config)) { | 301 if (!ToAudioSenderConfig(params, &config)) { |
| 309 DidEncounterError("Invalid parameters for audio."); | 302 DidEncounterError("Invalid parameters for audio."); |
| 310 return; | 303 return; |
| 311 } | 304 } |
| 312 // In case of error we have to go through DidEncounterError() to stop | 305 // In case of error we have to go through DidEncounterError() to stop |
| 313 // the streaming after reporting the error. | 306 // the streaming after reporting the error. |
| 314 audio_sink_.reset(new CastAudioSink( | 307 audio_sink_.reset(new CastAudioSink( |
| 315 track_, | 308 track_, |
| 316 media::BindToCurrentLoop(base::Bind(&CastRtpStream::DidEncounterError, | 309 media::BindToCurrentLoop(base::Bind(&CastRtpStream::DidEncounterError, |
| 317 weak_factory_.GetWeakPtr())))); | 310 weak_factory_.GetWeakPtr())))); |
| 318 cast_session_->StartAudio( | 311 cast_session_->StartAudio( |
| 319 config, | 312 config, |
| 320 base::Bind(&CastAudioSink::AddToTrack, | 313 base::Bind(&CastAudioSink::AddToTrack, audio_sink_->AsWeakPtr())); |
| 321 audio_sink_->AsWeakPtr())); | |
| 322 start_callback.Run(); | 314 start_callback.Run(); |
| 323 } else { | 315 } else { |
| 324 VideoSenderConfig config; | 316 VideoSenderConfig config; |
| 325 if (!ToVideoSenderConfig(params, &config)) { | 317 if (!ToVideoSenderConfig(params, &config)) { |
| 326 DidEncounterError("Invalid parameters for video."); | 318 DidEncounterError("Invalid parameters for video."); |
| 327 return; | 319 return; |
| 328 } | 320 } |
| 329 // See the code for audio above for explanation of callbacks. | 321 // See the code for audio above for explanation of callbacks. |
| 330 video_sink_.reset(new CastVideoSink( | 322 video_sink_.reset(new CastVideoSink( |
| 331 track_, | 323 track_, |
| 332 media::BindToCurrentLoop(base::Bind(&CastRtpStream::DidEncounterError, | 324 media::BindToCurrentLoop(base::Bind(&CastRtpStream::DidEncounterError, |
| 333 weak_factory_.GetWeakPtr())))); | 325 weak_factory_.GetWeakPtr())))); |
| 334 cast_session_->StartVideo( | 326 cast_session_->StartVideo( |
| 335 config, | 327 config, |
| 336 base::Bind(&CastVideoSink::AddToTrack, | 328 base::Bind(&CastVideoSink::AddToTrack, video_sink_->AsWeakPtr())); |
| 337 video_sink_->AsWeakPtr())); | |
| 338 start_callback.Run(); | 329 start_callback.Run(); |
| 339 } | 330 } |
| 340 } | 331 } |
| 341 | 332 |
| 342 void CastRtpStream::Stop() { | 333 void CastRtpStream::Stop() { |
| 343 audio_sink_.reset(); | 334 audio_sink_.reset(); |
| 344 video_sink_.reset(); | 335 video_sink_.reset(); |
| 345 stop_callback_.Run(); | 336 stop_callback_.Run(); |
| 346 } | 337 } |
| 347 | 338 |
| 348 void CastRtpStream::ToggleLogging(bool enable) { | 339 void CastRtpStream::ToggleLogging(bool enable) { |
| 349 cast_session_->ToggleLogging(IsAudio(), enable); | 340 cast_session_->ToggleLogging(IsAudio(), enable); |
| 350 } | 341 } |
| 351 | 342 |
| 352 void CastRtpStream::GetRawEvents( | 343 void CastRtpStream::GetRawEvents( |
| 353 const base::Callback<void(scoped_ptr<std::string>)>& callback) { | 344 const base::Callback<void(scoped_ptr<std::string>)>& callback) { |
| 354 cast_session_->GetEventLogsAndReset(IsAudio(), callback); | 345 cast_session_->GetEventLogsAndReset(IsAudio(), callback); |
| 355 } | 346 } |
| 356 | 347 |
| 357 bool CastRtpStream::IsAudio() const { | 348 bool CastRtpStream::IsAudio() const { |
| 358 return track_.source().type() == blink::WebMediaStreamSource::TypeAudio; | 349 return track_.source().type() == blink::WebMediaStreamSource::TypeAudio; |
| 359 } | 350 } |
| 360 | 351 |
| 361 void CastRtpStream::DidEncounterError(const std::string& message) { | 352 void CastRtpStream::DidEncounterError(const std::string& message) { |
| 362 error_callback_.Run(message); | 353 error_callback_.Run(message); |
| 363 Stop(); | 354 Stop(); |
| 364 } | 355 } |
| OLD | NEW |