Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / cast / test / loopback_transport.cc
blob7c71d581b4c83f723b20ae0aba7fe633217bf455
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"
11 namespace media {
12 namespace cast {
13 namespace {
15 // Shim that turns forwards packets from a test::PacketPipe to a
16 // PacketReceiverCallback.
17 class LoopBackPacketPipe : public test::PacketPipe {
18 public:
19 LoopBackPacketPipe(
20 const PacketReceiverCallback& packet_receiver)
21 : packet_receiver_(packet_receiver) {}
23 ~LoopBackPacketPipe() final {}
25 // PacketPipe implementations.
26 void Send(scoped_ptr<Packet> packet) final {
27 packet_receiver_.Run(packet.Pass());
30 private:
31 PacketReceiverCallback packet_receiver_;
33 DISALLOW_COPY_AND_ASSIGN(LoopBackPacketPipe);
36 } // namespace
38 LoopBackTransport::LoopBackTransport(
39 scoped_refptr<CastEnvironment> cast_environment)
40 : cast_environment_(cast_environment),
41 bytes_sent_(0) {
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();
53 return true;
56 int64 LoopBackTransport::GetBytesSent() {
57 return bytes_sent_;
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 if (pipe) {
68 // Append the loopback pipe to the end.
69 pipe->AppendToPipe(loopback_pipe.Pass());
70 packet_pipe_ = pipe.Pass();
71 } else {
72 packet_pipe_ = loopback_pipe.Pass();
74 packet_pipe_->InitOnIOThread(task_runner, clock);
77 } // namespace cast
78 } // namespace media