summaryrefslogtreecommitdiff
path: root/httptnt/resourceStreamer.ecpp
blob: 55d112bd71e0bf6b69a9ef27fa3aca66157e09be (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
<%args>
objectID;
int resourceID = 0;
</%args>
<%pre>
#include <string>
#include <stdint.h>
#include <limits>
#include <boost/shared_ptr.hpp>
#include "../include/tools.h"
#include "../include/media/mediaManager.h"
#include "../include/server.h"
#include "../include/media/requestCounter.h"

using namespace upnp;
</%pre>
<%request>
  boost::shared_ptr<cResourceStreamer> streamer;
  request_counter_t counter;
</%request>
<%cpp>
  _unused(counter);

  if(objectID.empty()){
    reply.out() << "Object ID missing";
    return HTTP_BAD_REQUEST;
  }

  cMediaServer* server = cMediaServer::GetInstance();
  cMediaManager& manager = server->GetManager();
  streamer = boost::shared_ptr<cResourceStreamer>(manager.GetResourceStreamer(objectID, resourceID));

  if(!streamer.get()){
    reply.out() << "Object ID not found or invalid";
    return HTTP_NOT_FOUND;
  }

  int code = HTTP_OK;
  string codeText = "OK";

  // DLNA requires KeepAlive timeout of 60s.
  reply.setKeepAliveTimeout(60000);
  reply.setContentType(streamer->GetContentType());
  reply.setHeader("friendlyName.dlna.org  ", server->GetServerDescription().friendlyName);
  reply.setHeader("contentFeatures.dlna.org  ", streamer->GetContentFeatures());
  reply.setHeader("transferMode.dlna.org  ", streamer->GetTransferMode(reply.getHeader("transferMode.dlna.org")));

  size_t from = 0, to = 0, contentLength = streamer->GetContentLength(), length = contentLength;
  bool hasRange = false;

  if(contentLength > 0){
    reply.setContentLengthHeader(contentLength);
    if(streamer->Seekable()){
      reply.setHeader("Accept-Ranges  ", "bytes");
      if(request.hasHeader("getAvailableSeekRange.dlna.org:")){
        std::stringstream availableRangeHeader;
        availableRangeHeader << "1 bytes=0-" << contentLength;
        reply.setHeader("availableSeekRange.dlna.org  ", availableRangeHeader.str());
      }
    }

    if(request.hasHeader("Range:")){
      std::string rangeRequest = request.getHeader("Range:");
      if(rangeRequest.find("bytes=",0) == 0){
        unsigned int minus = rangeRequest.find_first_of('-',6);
        if(minus != std::string::npos){
          from  = atol(rangeRequest.substr(6, minus - 6).c_str());
          to = atol(rangeRequest.substr(minus + 1).c_str());
          if(from){
            // If range is off the road, reset it to correct values.
            if(from <= 0 || from > contentLength) from = 0;
            if(to <= 0 || to > contentLength) to = contentLength;
            length = to - from;
            hasRange = true;

            std::stringstream contentRangeHeader;
            contentRangeHeader << "bytes " << from << "-" << to << "/" << contentLength;
            reply.setHeader("Content-Range  ", contentRangeHeader.str());
            reply.setContentLengthHeader(length);

          }
        }
      }
    }
  }

  if(!request.isMethodHEAD()){
    if(!streamer->Open()){
      return HTTP_INTERNAL_SERVER_ERROR;
    }

    if(hasRange && streamer->Seekable() && contentLength > 0){
      if(!streamer->Seek(from, SEEK_SET)){
        return HTTP_INTERNAL_SERVER_ERROR;
      }
      code = HTTP_PARTIAL_CONTENT;
      codeText = "Partial Content";
    }

    reply.setDirectMode(code, codeText.c_str());

    size_t bytesRead = 0;
    char buffer[KB(16)];
    while ((bytesRead = streamer->Read(buffer, KB(16))) > 0 && length) {
      if(!(reply.out().write(buffer, bytesRead))) break;
      length -= bytesRead;
      request.touch();
    }
  }

  reply.out() << std::flush;
  return code;
</%cpp>