summaryrefslogtreecommitdiff
path: root/p2pvr/p2comp/recordingStream.cpp
blob: fc1687a2479204762eae5cd22345d9cb5c99fe34 (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
#include <p2pvr.h>
#include <options.h>
#include <temporaryIceAdapterObject.h>
#include "streamSinkWrapper.h"
#include "streamBase.h"
#include <limits>
#include <cgiRequestContext.h>

class RecordingStream : public StreamBase {
	public:
		RecordingStream(ScriptNodePtr p) :
			StreamBase(p),
			recording(p, "recording")
		{
		}

		void runStream(const Sink & sink, ExecContext * ec) const override
		{
			auto storage = ice->GetProxy<P2PVR::StoragePrx>("Storage", ec);
			assert(storage);

			std::promise<int> prom;
			{
				TemporaryIceAdapterObject<P2PVR::RawDataClient> output(adapter, new StreamSinkWrapper(sink, prom));

				try {
					auto range = getRange(ec);
					if (!range) {
						range = { std::string(), 0, (unsigned long long)(storage->FileSize(recording(ec)) - 1) };
					}
					storage->Send(recording(ec), output, range->Start, range->End - range->Start + 1);
				}
				catch (const std::ios_base::failure &) {
				}
			}
		}

		boost::optional<unsigned long long> getSizeInBytes(ExecContext * ec) const override
		{
			auto storage = ice->GetProxy<P2PVR::StoragePrx>("Storage", ec);
			assert(storage);
			return storage->FileSize(recording(ec));
		}

		bool isSeekable() const override
		{
			return true;
		}

		boost::optional<RangeResponse> getRange(ExecContext * ec) const override
		{
			auto crc = dynamic_cast<CgiRequestContext *>(ec);
			if (crc) {
				auto range = crc->getRequestRange();
				if (range && range->Unit == "bytes") {
					auto storage = ice->GetProxy<P2PVR::StoragePrx>("Storage", ec);
					assert(storage);
					auto size = storage->FileSize(recording(ec));
					if (!range->Start) {
						range->Start = 0;
					}
					if (!range->End) {
						range->End = size - 1;
					}
					else {
						range->End = std::min<RangePos>(*range->End, size - 1);
					}
					return (RangeResponse){ std::string("bytes"), *range->Start, *range->End };
				}
			}
			return boost::optional<RangeResponse>();
		}

	private:
		Variable recording;
};

NAMEDFACTORY("p2pvrrecordingstream", RecordingStream, StreamFactory);