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.
5 #include "chrome/renderer/media/cast_receiver_session_delegate.h"
7 #include "base/synchronization/waitable_event.h"
8 #include "base/values.h"
10 CastReceiverSessionDelegate::CastReceiverSessionDelegate()
11 : weak_factory_(this) {
13 CastReceiverSessionDelegate::~CastReceiverSessionDelegate() {}
15 void CastReceiverSessionDelegate::LogRawEvents(
16 const std::vector
<media::cast::PacketEvent
>& packet_events
,
17 const std::vector
<media::cast::FrameEvent
>& frame_events
) {
21 void CastReceiverSessionDelegate::Start(
22 const media::cast::FrameReceiverConfig
& audio_config
,
23 const media::cast::FrameReceiverConfig
& video_config
,
24 const net::IPEndPoint
& local_endpoint
,
25 const net::IPEndPoint
& remote_endpoint
,
26 scoped_ptr
<base::DictionaryValue
> options
,
27 const media::VideoCaptureFormat
& format
) {
29 DCHECK(io_message_loop_proxy_
->BelongsToCurrentThread());
30 CastSessionDelegateBase::StartUDP(local_endpoint
,
33 cast_receiver_
= media::cast::CastReceiver::Create(cast_environment_
,
36 cast_transport_
.get());
37 on_audio_decoded_cb_
= base::Bind(
38 &CastReceiverSessionDelegate::OnDecodedAudioFrame
,
39 weak_factory_
.GetWeakPtr());
40 on_video_decoded_cb_
= base::Bind(
41 &CastReceiverSessionDelegate::OnDecodedVideoFrame
,
42 weak_factory_
.GetWeakPtr());
45 void CastReceiverSessionDelegate::ReceivePacket(
46 scoped_ptr
<media::cast::Packet
> packet
) {
47 cast_receiver_
->ReceivePacket(packet
.Pass());
50 void CastReceiverSessionDelegate::StartAudio(
51 scoped_refptr
<CastReceiverAudioValve
> audio_valve
) {
52 DCHECK(io_message_loop_proxy_
->BelongsToCurrentThread());
53 audio_valve_
= audio_valve
;
54 cast_receiver_
->RequestDecodedAudioFrame(on_audio_decoded_cb_
);
57 void CastReceiverSessionDelegate::OnDecodedAudioFrame(
58 scoped_ptr
<media::AudioBus
> audio_bus
,
59 const base::TimeTicks
& playout_time
,
61 DCHECK(io_message_loop_proxy_
->BelongsToCurrentThread());
65 // We're on the IO thread, which doesn't allow blocking
66 // operations. Since we don't know what the Capture callback
67 // will do exactly, we need to jump to a different thread.
68 // Let's re-use the audio decoder thread.
69 base::TimeTicks now
= cast_environment_
->Clock()->NowTicks();
70 cast_environment_
->PostTask(
71 media::cast::CastEnvironment::AUDIO
,
73 base::Bind(&CastReceiverAudioValve::Capture
,
75 base::Owned(audio_bus
.release()),
76 (playout_time
- now
).InMilliseconds(),
79 cast_receiver_
->RequestDecodedAudioFrame(on_audio_decoded_cb_
);
82 void CastReceiverSessionDelegate::StartVideo(
83 content::VideoCaptureDeliverFrameCB video_callback
) {
84 DCHECK(io_message_loop_proxy_
->BelongsToCurrentThread());
85 frame_callback_
= video_callback
;
86 cast_receiver_
->RequestDecodedVideoFrame(on_video_decoded_cb_
);
89 void CastReceiverSessionDelegate::StopVideo() {
90 frame_callback_
= content::VideoCaptureDeliverFrameCB();
93 void CastReceiverSessionDelegate::OnDecodedVideoFrame(
94 const scoped_refptr
<media::VideoFrame
>& video_frame
,
95 const base::TimeTicks
& playout_time
,
97 if (frame_callback_
.is_null())
99 frame_callback_
.Run(video_frame
, format_
, playout_time
);
100 cast_receiver_
->RequestDecodedVideoFrame(on_video_decoded_cb_
);