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.
4
5 #include "media/cast/test/loopback_transport.h"
6
7 #include "base/single_thread_task_runner.h"
8 #include "base/time/tick_clock.h"
9 #include "media/cast/test/utility/udp_proxy.h"
10
11 namespace media {
12 namespace cast {
13 namespace {
14
15 // Shim that turns forwards packets from a test::PacketPipe to a
16 // PacketReceiverCallback.
17 class LoopBackPacketPipe : public test::PacketPipe {
18 public:
LoopBackPacketPipe(const PacketReceiverCallback & packet_receiver)19 LoopBackPacketPipe(
20 const PacketReceiverCallback& packet_receiver)
21 : packet_receiver_(packet_receiver) {}
22
~LoopBackPacketPipe()23 virtual ~LoopBackPacketPipe() {}
24
25 // PacketPipe implementations.
Send(scoped_ptr<Packet> packet)26 virtual void Send(scoped_ptr<Packet> packet) OVERRIDE {
27 packet_receiver_.Run(packet.Pass());
28 }
29
30 private:
31 PacketReceiverCallback packet_receiver_;
32
33 DISALLOW_COPY_AND_ASSIGN(LoopBackPacketPipe);
34 };
35
36 } // namespace
37
LoopBackTransport(scoped_refptr<CastEnvironment> cast_environment)38 LoopBackTransport::LoopBackTransport(
39 scoped_refptr<CastEnvironment> cast_environment)
40 : cast_environment_(cast_environment),
41 bytes_sent_(0) {
42 }
43
~LoopBackTransport()44 LoopBackTransport::~LoopBackTransport() {
45 }
46
SendPacket(PacketRef packet,const base::Closure & cb)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();
53 return true;
54 }
55
GetBytesSent()56 int64 LoopBackTransport::GetBytesSent() {
57 return bytes_sent_;
58 }
59
Initialize(scoped_ptr<test::PacketPipe> pipe,const PacketReceiverCallback & packet_receiver,const scoped_refptr<base::SingleThreadTaskRunner> & task_runner,base::TickClock * clock)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);
71 }
72
73 } // namespace cast
74 } // namespace media
75