This source file includes following definitions.
- video_sender_
- InsertRawVideoFrame
- audio_sender_
- InsertAudio
- weak_factory_
- InitializeAudio
- InitializeVideo
- ReceivedPacket
- audio_frame_input
- video_frame_input
- packet_receiver
#include "media/cast/cast_sender_impl.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "media/base/video_frame.h"
namespace media {
namespace cast {
class LocalVideoFrameInput : public VideoFrameInput {
public:
LocalVideoFrameInput(scoped_refptr<CastEnvironment> cast_environment,
base::WeakPtr<VideoSender> video_sender)
: cast_environment_(cast_environment), video_sender_(video_sender) {}
virtual void InsertRawVideoFrame(
const scoped_refptr<media::VideoFrame>& video_frame,
const base::TimeTicks& capture_time) OVERRIDE {
cast_environment_->PostTask(CastEnvironment::MAIN,
FROM_HERE,
base::Bind(&VideoSender::InsertRawVideoFrame,
video_sender_,
video_frame,
capture_time));
}
protected:
virtual ~LocalVideoFrameInput() {}
private:
friend class base::RefCountedThreadSafe<LocalVideoFrameInput>;
scoped_refptr<CastEnvironment> cast_environment_;
base::WeakPtr<VideoSender> video_sender_;
DISALLOW_COPY_AND_ASSIGN(LocalVideoFrameInput);
};
class LocalAudioFrameInput : public AudioFrameInput {
public:
LocalAudioFrameInput(scoped_refptr<CastEnvironment> cast_environment,
base::WeakPtr<AudioSender> audio_sender)
: cast_environment_(cast_environment), audio_sender_(audio_sender) {}
virtual void InsertAudio(scoped_ptr<AudioBus> audio_bus,
const base::TimeTicks& recorded_time) OVERRIDE {
cast_environment_->PostTask(CastEnvironment::MAIN,
FROM_HERE,
base::Bind(&AudioSender::InsertAudio,
audio_sender_,
base::Passed(&audio_bus),
recorded_time));
}
protected:
virtual ~LocalAudioFrameInput() {}
private:
friend class base::RefCountedThreadSafe<LocalAudioFrameInput>;
scoped_refptr<CastEnvironment> cast_environment_;
base::WeakPtr<AudioSender> audio_sender_;
DISALLOW_COPY_AND_ASSIGN(LocalAudioFrameInput);
};
scoped_ptr<CastSender> CastSender::Create(
scoped_refptr<CastEnvironment> cast_environment,
transport::CastTransportSender* const transport_sender) {
CHECK(cast_environment);
return scoped_ptr<CastSender>(
new CastSenderImpl(cast_environment, transport_sender));
}
CastSenderImpl::CastSenderImpl(
scoped_refptr<CastEnvironment> cast_environment,
transport::CastTransportSender* const transport_sender)
: cast_environment_(cast_environment),
transport_sender_(transport_sender),
weak_factory_(this) {
CHECK(cast_environment);
}
void CastSenderImpl::InitializeAudio(
const AudioSenderConfig& audio_config,
const CastInitializationCallback& cast_initialization_cb) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
CHECK(audio_config.use_external_encoder ||
cast_environment_->HasAudioThread());
audio_sender_.reset(
new AudioSender(cast_environment_, audio_config, transport_sender_));
CastInitializationStatus status = audio_sender_->InitializationResult();
if (status == STATUS_AUDIO_INITIALIZED) {
ssrc_of_audio_sender_ = audio_config.incoming_feedback_ssrc;
audio_frame_input_ =
new LocalAudioFrameInput(cast_environment_, audio_sender_->AsWeakPtr());
}
cast_initialization_cb.Run(status);
}
void CastSenderImpl::InitializeVideo(
const VideoSenderConfig& video_config,
const CastInitializationCallback& cast_initialization_cb,
const CreateVideoEncodeAcceleratorCallback& create_vea_cb,
const CreateVideoEncodeMemoryCallback& create_video_encode_mem_cb) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
CHECK(video_config.use_external_encoder ||
cast_environment_->HasVideoThread());
video_sender_.reset(new VideoSender(cast_environment_,
video_config,
create_vea_cb,
create_video_encode_mem_cb,
cast_initialization_cb,
transport_sender_));
ssrc_of_video_sender_ = video_config.incoming_feedback_ssrc;
video_frame_input_ =
new LocalVideoFrameInput(cast_environment_, video_sender_->AsWeakPtr());
}
CastSenderImpl::~CastSenderImpl() {}
void CastSenderImpl::ReceivedPacket(scoped_ptr<Packet> packet) {
DCHECK(cast_environment_);
size_t length = packet->size();
const uint8_t* data = &packet->front();
if (!Rtcp::IsRtcpPacket(data, length)) {
VLOG(1) << "Unexpectedly received a RTP packet in the cast sender";
return;
}
uint32 ssrc_of_sender = Rtcp::GetSsrcOfSender(data, length);
if (ssrc_of_sender == ssrc_of_audio_sender_) {
if (!audio_sender_) {
NOTREACHED();
return;
}
cast_environment_->PostTask(CastEnvironment::MAIN,
FROM_HERE,
base::Bind(&AudioSender::IncomingRtcpPacket,
audio_sender_->AsWeakPtr(),
base::Passed(&packet)));
} else if (ssrc_of_sender == ssrc_of_video_sender_) {
if (!video_sender_) {
NOTREACHED();
return;
}
cast_environment_->PostTask(CastEnvironment::MAIN,
FROM_HERE,
base::Bind(&VideoSender::IncomingRtcpPacket,
video_sender_->AsWeakPtr(),
base::Passed(&packet)));
} else {
VLOG(1) << "Received a RTCP packet with a non matching sender SSRC "
<< ssrc_of_sender;
}
}
scoped_refptr<AudioFrameInput> CastSenderImpl::audio_frame_input() {
return audio_frame_input_;
}
scoped_refptr<VideoFrameInput> CastSenderImpl::video_frame_input() {
return video_frame_input_;
}
transport::PacketReceiverCallback CastSenderImpl::packet_receiver() {
return base::Bind(&CastSenderImpl::ReceivedPacket,
weak_factory_.GetWeakPtr());
}
}
}