DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
cue.cc
1 #include "packager/media/formats/webvtt/cue.h"
2 
3 #include "packager/base/strings/string_util.h"
4 
5 namespace shaka {
6 namespace media {
7 
8 Cue::Cue() : start_time(0), duration(0) {}
9 Cue::~Cue() {}
10 
11 // Mapping:
12 // comment --> side data (and side data only sample)
13 // settings --> side data
14 // start_time --> pts
15 std::shared_ptr<MediaSample> CueToMediaSample(const Cue& cue) {
16  const bool kKeyFrame = true;
17  if (!cue.comment.empty()) {
18  const std::string comment = base::JoinString(cue.comment, "\n");
20  reinterpret_cast<const uint8_t*>(comment.data()), comment.size());
21  }
22 
23  const std::string payload = base::JoinString(cue.payload, "\n");
24  std::shared_ptr<MediaSample> media_sample = MediaSample::CopyFrom(
25  reinterpret_cast<const uint8_t*>(payload.data()), payload.size(),
26  reinterpret_cast<const uint8_t*>(cue.settings.data()),
27  cue.settings.size(), !kKeyFrame);
28 
29  media_sample->set_config_id(cue.identifier);
30  media_sample->set_pts(cue.start_time);
31  media_sample->set_duration(cue.duration);
32  return media_sample;
33 }
34 
35 // TODO(rkuroiwa): Cue gets converted to MediaSample in WebVttMediaParser and
36 // then back to Cue in the muxer. Consider making MediaSample a protobuf or make
37 // Cue a protobuf and (ab)use MediaSample::data() to store serialized Cue.
38 Cue MediaSampleToCue(const MediaSample& sample) {
39  Cue cue;
40  if (sample.data_size() == 0) {
41  std::string comment(sample.side_data(),
42  sample.side_data() + sample.side_data_size());
43  cue.comment.push_back(comment);
44  return cue;
45  }
46 
47  std::string payload(sample.data(), sample.data() + sample.data_size());
48  cue.payload.push_back(payload);
49  cue.identifier.assign(sample.config_id());
50  cue.start_time = sample.pts();
51  cue.duration = sample.duration();
52  if (sample.side_data_size() != 0) {
53  cue.settings.assign(sample.side_data(),
54  sample.side_data() + sample.side_data_size());
55  }
56  return cue;
57 }
58 
59 } // namespace media
60 } // namespace shaka
static std::shared_ptr< MediaSample > FromMetadata(const uint8_t *metadata, size_t metadata_size)
Definition: media_sample.cc:67
static std::shared_ptr< MediaSample > CopyFrom(const uint8_t *data, size_t size, bool is_key_frame)
Definition: media_sample.cc:45