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
3 // found in the LICENSE file.
5 #include "media/cast/cast_receiver_impl.h"
8 #include "base/callback.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h"
15 // The video and audio receivers should only be called from the main thread.
16 // LocalFrameReciever posts tasks to the main thread, making the cast interface
18 class LocalFrameReceiver
: public FrameReceiver
{
20 LocalFrameReceiver(scoped_refptr
<CastEnvironment
> cast_environment
,
21 AudioReceiver
* audio_receiver
,
22 VideoReceiver
* video_receiver
)
23 : cast_environment_(cast_environment
),
24 audio_receiver_(audio_receiver
),
25 video_receiver_(video_receiver
) {}
27 virtual void GetRawVideoFrame(const VideoFrameDecodedCallback
& callback
)
29 cast_environment_
->PostTask(CastEnvironment::MAIN
,
31 base::Bind(&VideoReceiver::GetRawVideoFrame
,
32 video_receiver_
->AsWeakPtr(),
36 virtual void GetEncodedVideoFrame(const VideoFrameEncodedCallback
& callback
)
38 cast_environment_
->PostTask(CastEnvironment::MAIN
,
40 base::Bind(&VideoReceiver::GetEncodedVideoFrame
,
41 video_receiver_
->AsWeakPtr(),
45 virtual void GetRawAudioFrame(const AudioFrameDecodedCallback
& callback
)
47 cast_environment_
->PostTask(CastEnvironment::MAIN
,
49 base::Bind(&AudioReceiver::GetRawAudioFrame
,
50 audio_receiver_
->AsWeakPtr(),
54 virtual void GetCodedAudioFrame(const AudioFrameEncodedCallback
& callback
)
56 cast_environment_
->PostTask(CastEnvironment::MAIN
,
58 base::Bind(&AudioReceiver::GetEncodedAudioFrame
,
59 audio_receiver_
->AsWeakPtr(),
64 virtual ~LocalFrameReceiver() {}
67 friend class base::RefCountedThreadSafe
<LocalFrameReceiver
>;
69 scoped_refptr
<CastEnvironment
> cast_environment_
;
70 AudioReceiver
* audio_receiver_
;
71 VideoReceiver
* video_receiver_
;
74 scoped_ptr
<CastReceiver
> CastReceiver::Create(
75 scoped_refptr
<CastEnvironment
> cast_environment
,
76 const AudioReceiverConfig
& audio_config
,
77 const VideoReceiverConfig
& video_config
,
78 transport::PacketSender
* const packet_sender
) {
79 return scoped_ptr
<CastReceiver
>(new CastReceiverImpl(
80 cast_environment
, audio_config
, video_config
, packet_sender
));
83 CastReceiverImpl::CastReceiverImpl(
84 scoped_refptr
<CastEnvironment
> cast_environment
,
85 const AudioReceiverConfig
& audio_config
,
86 const VideoReceiverConfig
& video_config
,
87 transport::PacketSender
* const packet_sender
)
88 : pacer_(cast_environment
->Clock(),
89 cast_environment
->Logging(),
91 cast_environment
->GetTaskRunner(CastEnvironment::MAIN
)),
92 audio_receiver_(cast_environment
, audio_config
, &pacer_
),
93 video_receiver_(cast_environment
,
96 frame_receiver_(new LocalFrameReceiver(cast_environment
,
99 cast_environment_(cast_environment
),
100 ssrc_of_audio_sender_(audio_config
.incoming_ssrc
),
101 ssrc_of_video_sender_(video_config
.incoming_ssrc
) {}
103 CastReceiverImpl::~CastReceiverImpl() {}
105 // The video and audio receivers should only be called from the main thread.
106 void CastReceiverImpl::ReceivedPacket(scoped_ptr
<Packet
> packet
) {
107 const uint8_t* data
= &packet
->front();
108 size_t length
= packet
->size();
109 if (length
< kMinLengthOfRtcp
) {
110 VLOG(1) << "Received a packet which is too short " << length
;
113 uint32 ssrc_of_sender
;
114 if (!Rtcp::IsRtcpPacket(data
, length
)) {
115 if (length
< kMinLengthOfRtp
) {
116 VLOG(1) << "Received a RTP packet which is too short " << length
;
119 ssrc_of_sender
= RtpReceiver::GetSsrcOfSender(data
, length
);
121 ssrc_of_sender
= Rtcp::GetSsrcOfSender(data
, length
);
123 if (ssrc_of_sender
== ssrc_of_audio_sender_
) {
124 cast_environment_
->PostTask(CastEnvironment::MAIN
,
126 base::Bind(&AudioReceiver::IncomingPacket
,
127 audio_receiver_
.AsWeakPtr(),
128 base::Passed(&packet
)));
129 } else if (ssrc_of_sender
== ssrc_of_video_sender_
) {
130 cast_environment_
->PostTask(CastEnvironment::MAIN
,
132 base::Bind(&VideoReceiver::IncomingPacket
,
133 video_receiver_
.AsWeakPtr(),
134 base::Passed(&packet
)));
136 VLOG(1) << "Received a packet with a non matching sender SSRC "
141 transport::PacketReceiverCallback
CastReceiverImpl::packet_receiver() {
142 return base::Bind(&CastReceiverImpl::ReceivedPacket
, base::Unretained(this));
145 scoped_refptr
<FrameReceiver
> CastReceiverImpl::frame_receiver() {
146 return frame_receiver_
;