summaryrefslogtreecommitdiff
path: root/p2pvr/dvb/unittests/createBroadcast.cpp
blob: c65c9ad56a385d295ac3267a7d4419e6152ef51c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#define BOOST_TEST_MODULE CreateSamples
#include <testOptionsSource.h>
#include <boost/test/unit_test.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/tuple/tuple.hpp>
#include <Ice/ObjectAdapter.h>
#include <Ice/Service.h>
#include <scopeObject.h>
#include <linux/dvb/frontend.h>
#include "serviceStreamerCore.h"
#include "bindDataHandler.h"

#define XSTR(s) STR(s)
#define STR(s) #s
const boost::filesystem::path root(XSTR(ROOT));

Ice::StringSeq params {
				"--Ice.ThreadPool.Client.Size=5",
				"--Ice.ThreadPool.Client.SizeMax=10",
				"--Ice.ThreadPool.Server.Size=5",
				"--Ice.ThreadPool.Server.SizeMax=10"
			};

class Core {
	public:
		Core() :
			ic(Ice::initialize(params)),
			adapter(ic->createObjectAdapterWithEndpoints("Adp", "tcp -p 12003"))
		{
			TestOptionsSource::LoadTestOptions({ });
			adapter->activate();

			devs = P2PVR::DevicesPrx::checkedCast(ic->stringToProxy("Devices:tcp -h defiant -p 10000"));
			BOOST_REQUIRE(devs);
			devs->ice_ping();
		}

		~Core()
		{
			ic->destroy();
		}

		P2PVR::DevicesPrx devs;
		Ice::CommunicatorPtr ic;
		Ice::ObjectAdapterPtr adapter;

};

class Sampler {
	public:
		Sampler(int sid, P2PVR::DevicesPrx d, Ice::ObjectAdapterPtr a) :
				patp(a, new BindDataHandler(boost::bind(&Sampler::Handle, this, _1, boost::ref(pat)))),
				pmtp(a, new BindDataHandler(boost::bind(&Sampler::Handle, this, _1, boost::ref(pmt)))),
				vidp(a, new BindDataHandler(boost::bind(&Sampler::Handle, this, _1, boost::ref(vid)))),
				ss(sid, patp, pmtp, vidp, d, a)
		{
		}

		TemporaryIceAdapterObject<P2PVR::RawDataClient> patp;
		TemporaryIceAdapterObject<P2PVR::RawDataClient> pmtp;
		TemporaryIceAdapterObject<P2PVR::RawDataClient> vidp;
		ServiceStreamerCore ss;

		typedef std::list<P2PVR::Data> Sampled;
		Sampled pat, pmt, vid;

		bool Handle(const P2PVR::Data & data, Sampled & rec)
		{
			rec.push_back(data);
			return false;
		}

		void Start(DVBSI::DeliveryPtr transport) { ss.Start(transport); }
		void Stop() { ss.Stop(); }

		void Save(Ice::CommunicatorPtr ic)
		{
			Save(ic, pat, "pat.dat");
			Save(ic, pmt, "pmt.dat");
			Save(ic, vid, "vid.dat");
		}

	private:
		void Save(Ice::CommunicatorPtr ic, const Sampled & s, const boost::filesystem::path & fileName)
		{
			auto out = Ice::createOutputStream(ic);
			out->write(s);
			Ice::ByteSeq buf;
			out->finished(buf);
			BOOST_MESSAGE(root);
			auto outFile = fopen((root / fileName).string().c_str(), "w");
			BOOST_REQUIRE(outFile);
			BOOST_REQUIRE_EQUAL(1, fwrite(&buf.front(), buf.size(), 1, outFile));
			BOOST_REQUIRE_EQUAL(0, fclose(outFile));
		}

};

BOOST_FIXTURE_TEST_SUITE( X, Core )

BOOST_AUTO_TEST_CASE( broadcast_sample )
{
	DVBSI::TerrestrialDeliveryPtr transport = new DVBSI::TerrestrialDelivery {
		4170, 682000000, 0, true, true, true, 3, 0, 2, 1, 0, 1, true
	};
	BOOST_REQUIRE_EQUAL(transport->Frequency, 682000000);
	BOOST_REQUIRE_EQUAL(transport->TransmissionMode, 1);

	BOOST_CHECKPOINT("Acquire device");
	P2PVR::TunerPrx tuner = devs->GetTunerSpecific(transport);
	BOOST_REQUIRE(tuner);
	tuner->ice_ping();

	BOOST_CHECKPOINT("Create sampler");
	Sampler sampler(4170, devs, adapter);

	BOOST_CHECKPOINT("Start stream");
	sampler.Start(transport);

	sleep(10);

	BOOST_CHECKPOINT("Stop stream");
	sampler.Stop();

	BOOST_MESSAGE("PAT packets: " << sampler.pat.size());
	BOOST_MESSAGE("PMT packets: " << sampler.pmt.size());
	BOOST_MESSAGE("Vid packets: " << sampler.vid.size());

	BOOST_CHECKPOINT("Save samples");
	sampler.Save(ic);
}

BOOST_AUTO_TEST_SUITE_END();