5G-MAG Reference Tools - MBMS Middleware
Service.cpp
Go to the documentation of this file.
1 // 5G-MAG Reference Tools
2 // MBMS Middleware Process
3 //
4 // Copyright (C) 2021 Klaus Kühnhammer (Österreichische Rundfunksender GmbH & Co KG)
5 //
6 // Licensed under the License terms and conditions for use, reproduction, and
7 // distribution of 5G-MAG software (the “License”). You may not use this file
8 // except in compliance with the License. You may obtain a copy of the License at
9 // https://www.5g-mag.com/reference-tools. Unless required by applicable law or
10 // agreed to in writing, software distributed under the License is distributed on
11 // an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 // or implied.
13 //
14 // See the License for the specific language governing permissions and limitations
15 // under the License.
16 //
17 
18 #include <regex>
19 #include <boost/algorithm/string/split.hpp>
20 #include <boost/algorithm/string/classification.hpp>
21 #include "Service.h"
22 #include "Receiver.h"
23 #include "HlsPrimaryPlaylist.h"
24 #include "DashManifest.h"
25 #include "Constants.h"
26 
27 #include "spdlog/spdlog.h"
28 #include "gmime/gmime.h"
29 #include "tinyxml2.h"
30 
31 auto MBMS_RT::Service::add_name(const std::string &name, const std::string &lang) -> void {
32  spdlog::debug("Service name added: {} ({})", name, lang);
33  _names[lang] = name;
34 }
35 
36 auto MBMS_RT::Service::read_master_manifest(const std::string &manifest, const std::string &base_path) -> void {
37  spdlog::debug("service: master manifest contents:\n{}, base path {}", manifest, base_path);
38  if (_delivery_protocol == DeliveryProtocol::HLS) {
39  _hls_primary_playlist = HlsPrimaryPlaylist(manifest, base_path);
40  } else if(_delivery_protocol == DeliveryProtocol::DASH) {
41  _dash_manifest = DashManifest(manifest, base_path);
42  }
43 
44  _manifest_path =
45  _delivery_protocol == DeliveryProtocol::HLS ? base_path + "manifest.m3u8" : base_path + "manifest.mpd",
46  _cache.add_item(std::make_shared<CachedPlaylist>(
47  _manifest_path,
48  0,
49  [&]() -> const std::string & {
50  spdlog::debug("Service: master manifest requested");
51  return _manifest;
52  }
53  ));
54 }
55 
56 auto MBMS_RT::Service::add_and_start_content_stream(std::shared_ptr<ContentStream> s) -> void // NOLINT
57 {
58  spdlog::debug("adding stream with playlist path {}", s->playlist_path());
59  if (_delivery_protocol == DeliveryProtocol::HLS) {
60  for (const auto &stream: _hls_primary_playlist.streams()) {
61  if (stream.uri == s->playlist_path()) {
62  spdlog::debug("matched hls entry with uri {}", stream.uri);
63  s->set_resolution(stream.resolution);
64  s->set_codecs(stream.codecs);
65  s->set_bandwidth(stream.bandwidth);
66  s->set_frame_rate(stream.frame_rate);
67  }
68  }
69  }
70  _content_streams[s->playlist_path()] = s;
71  s->start();
72 
73  if (_delivery_protocol == DeliveryProtocol::HLS) {
74  // recreate the manifest
76  for (const auto &stream: _content_streams) {
78  "/" + stream.second->playlist_path(),
79  stream.second->resolution(),
80  stream.second->codecs(),
81  stream.second->bandwidth(),
82  stream.second->frame_rate()
83  };
84  pl.add_stream(s);
85  }
86  _manifest = pl.to_string();
87  } else if(_delivery_protocol == DeliveryProtocol::DASH) {
88  _manifest = _dash_manifest.content;
89  }
90 }
91 
92 auto MBMS_RT::Service::set_delivery_protocol_from_mime_type(const std::string &mime_type) -> void {
93  // Need to remove potential profile from mimeType, example:application/dash+xml;profiles=urn:3GPP:PSS:profile:DASH10
94  std::vector<std::string> strs;
95  boost::split(strs, mime_type, boost::is_any_of(";"));
96  std::string adjusted_mime_type = strs.front();
97  _delivery_protocol =
98  (adjusted_mime_type == ContentTypeConstants::HLS ? DeliveryProtocol::HLS :
99  (adjusted_mime_type == ContentTypeConstants::DASH ? DeliveryProtocol::DASH : DeliveryProtocol::RTP));
100  spdlog::info("Setting delivery type {} from MIME type {}", _delivery_protocol == DeliveryProtocol::HLS ? "HLS" : (
101  _delivery_protocol == DeliveryProtocol::DASH ? "DASH" : "RTP"), mime_type);
102 };
103 
void add_stream(Stream stream)
void add_name(const std::string &name, const std::string &lang)
Definition: Service.cpp:31
void set_delivery_protocol_from_mime_type(const std::string &mime_type)
Definition: Service.cpp:92
void read_master_manifest(const std::string &manifest, const std::string &base_path)
Definition: Service.cpp:36
void add_and_start_content_stream(std::shared_ptr< ContentStream > s)
Definition: Service.cpp:56
const std::string DASH
Definition: Constants.h:25
const std::string HLS
Definition: Constants.h:24