Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
depacketizer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Roc Streaming authors
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 //! @file roc_audio/depacketizer.h
10 //! @brief Depacketizer.
11 
12 #ifndef ROC_AUDIO_DEPACKETIZER_H_
13 #define ROC_AUDIO_DEPACKETIZER_H_
14 
17 #include "roc_audio/sample.h"
18 #include "roc_audio/sample_spec.h"
19 #include "roc_core/noncopyable.h"
20 #include "roc_core/rate_limiter.h"
21 #include "roc_packet/ireader.h"
22 
23 namespace roc {
24 namespace audio {
25 
26 //! Depacketizer.
27 //! @remarks
28 //! Reads packets from a packet reader, decodes samples from packets using a
29 //! decoder, and produces an audio stream.
30 class Depacketizer : public IFrameReader, public core::NonCopyable<> {
31 public:
32  //! Initialization.
33  //!
34  //! @b Parameters
35  //! - @p reader is used to read packets
36  //! - @p payload_decoder is used to extract samples from packets
37  //! - @p sample_spec defines a set of channels in the output frames
38  //! - @p beep enables weird beeps instead of silence on packet loss
40  IFrameDecoder& payload_decoder,
41  const audio::SampleSpec& sample_spec,
42  bool beep);
43 
44  //! Was depacketizer constructed without errors?
45  bool is_valid() const;
46 
47  //! Did depacketizer catch first packet?
48  bool is_started() const;
49 
50  //! Read audio frame.
51  virtual bool read(Frame& frame);
52 
53  //! Get next timestamp to be rendered.
54  //! @pre
55  //! is_started() should return true
57 
58 private:
59  struct FrameInfo {
60  // Number of samples decoded from packets into the frame.
61  size_t n_decoded_samples;
62 
63  // Number of samples filled out in the frame.
64  size_t n_filled_samples;
65 
66  // Number of packets dropped during frame construction.
67  size_t n_dropped_packets;
68 
69  // This frame first sample timestamp.
70  core::nanoseconds_t capture_ts;
71 
72  FrameInfo()
73  : n_decoded_samples(0)
74  , n_filled_samples(0)
75  , n_dropped_packets(0)
76  , capture_ts(0) {
77  }
78  };
79 
80  void read_frame_(Frame& frame);
81 
82  sample_t* read_samples_(sample_t* buff_ptr, sample_t* buff_end, FrameInfo& info);
83 
84  sample_t* read_packet_samples_(sample_t* buff_ptr, sample_t* buff_end);
85  sample_t* read_missing_samples_(sample_t* buff_ptr, sample_t* buff_end);
86 
87  void update_packet_(FrameInfo& info);
88  packet::PacketPtr read_packet_();
89 
90  void set_frame_props_(Frame& frame, const FrameInfo& info);
91 
92  void report_stats_();
93 
94  packet::IReader& reader_;
95  IFrameDecoder& payload_decoder_;
96 
97  const audio::SampleSpec sample_spec_;
98 
99  packet::PacketPtr packet_;
100 
101  packet::stream_timestamp_t stream_ts_;
102  core::nanoseconds_t next_capture_ts_;
103  bool valid_capture_ts_;
104 
105  packet::stream_timestamp_t zero_samples_;
106  packet::stream_timestamp_t missing_samples_;
107  packet::stream_timestamp_t packet_samples_;
108 
109  core::RateLimiter rate_limiter_;
110 
111  const bool beep_;
112 
113  bool first_packet_;
114  bool valid_;
115 };
116 
117 } // namespace audio
118 } // namespace roc
119 
120 #endif // ROC_AUDIO_DEPACKETIZER_H_
bool is_started() const
Did depacketizer catch first packet?
Depacketizer(packet::IReader &reader, IFrameDecoder &payload_decoder, const audio::SampleSpec &sample_spec, bool beep)
Initialization.
virtual bool read(Frame &frame)
Read audio frame.
bool is_valid() const
Was depacketizer constructed without errors?
packet::stream_timestamp_t next_timestamp() const
Get next timestamp to be rendered.
Audio frame.
Definition: frame.h:25
Audio frame decoder interface.
Frame reader interface.
Definition: iframe_reader.h:22
Sample specification. Describes sample rate and channels.
Definition: sample_spec.h:26
Base class for non-copyable objects.
Definition: noncopyable.h:23
Packet reader interface.
Definition: ireader.h:23
Audio frame decoder interface.
Frame reader interface.
Packet reader interface.
float sample_t
Audio sample.
Definition: sample.h:22
int64_t nanoseconds_t
Nanoseconds.
Definition: time.h:58
uint32_t stream_timestamp_t
Packet stream timestamp.
Definition: units.h:36
Root namespace.
Non-copyable object.
Rate limiter.
Audio sample.
Sample specifications.