1 // Copyright 2014 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/test/loopback_transport.h"
7 #include "base/single_thread_task_runner.h"
8 #include "base/time/tick_clock.h"
9 #include "media/cast/test/utility/udp_proxy.h"
15 // Shim that turns forwards packets from a test::PacketPipe to a
16 // PacketReceiverCallback.
17 class LoopBackPacketPipe
: public test::PacketPipe
{
20 const PacketReceiverCallback
& packet_receiver
)
21 : packet_receiver_(packet_receiver
) {}
23 ~LoopBackPacketPipe() override
{}
25 // PacketPipe implementations.
26 void Send(scoped_ptr
<Packet
> packet
) override
{
27 packet_receiver_
.Run(packet
.Pass());
31 PacketReceiverCallback packet_receiver_
;
33 DISALLOW_COPY_AND_ASSIGN(LoopBackPacketPipe
);
38 LoopBackTransport::LoopBackTransport(
39 scoped_refptr
<CastEnvironment
> cast_environment
)
40 : cast_environment_(cast_environment
),
44 LoopBackTransport::~LoopBackTransport() {
47 bool LoopBackTransport::SendPacket(PacketRef packet
,
48 const base::Closure
& cb
) {
49 DCHECK(cast_environment_
->CurrentlyOn(CastEnvironment::MAIN
));
50 scoped_ptr
<Packet
> packet_copy(new Packet(packet
->data
));
51 packet_pipe_
->Send(packet_copy
.Pass());
52 bytes_sent_
+= packet
->data
.size();
56 int64
LoopBackTransport::GetBytesSent() {
60 void LoopBackTransport::Initialize(
61 scoped_ptr
<test::PacketPipe
> pipe
,
62 const PacketReceiverCallback
& packet_receiver
,
63 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
64 base::TickClock
* clock
) {
65 scoped_ptr
<test::PacketPipe
> loopback_pipe(
66 new LoopBackPacketPipe(packet_receiver
));
67 // Append the loopback pipe to the end.
68 pipe
->AppendToPipe(loopback_pipe
.Pass());
69 packet_pipe_
= pipe
.Pass();
70 packet_pipe_
->InitOnIOThread(task_runner
, clock
);