• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef TEST_PC_E2E_ANALYZER_VIDEO_SINGLE_PROCESS_ENCODED_IMAGE_DATA_INJECTOR_H_
12 #define TEST_PC_E2E_ANALYZER_VIDEO_SINGLE_PROCESS_ENCODED_IMAGE_DATA_INJECTOR_H_
13 
14 #include <cstdint>
15 #include <map>
16 #include <memory>
17 #include <utility>
18 #include <vector>
19 
20 #include "api/video/encoded_image.h"
21 #include "rtc_base/synchronization/mutex.h"
22 #include "test/pc/e2e/analyzer/video/encoded_image_data_injector.h"
23 
24 namespace webrtc {
25 namespace webrtc_pc_e2e {
26 
27 // Based on assumption that all call participants are in the same OS process
28 // and uses same QualityAnalyzingVideoContext to obtain
29 // EncodedImageDataInjector.
30 //
31 // To inject frame id and discard flag into EncodedImage injector uses last 3rd
32 // and 2nd bytes of EncodedImage payload. Then it uses last byte for frame
33 // sub id, that is required to distinguish different spatial layers. The origin
34 // data from these 3 bytes will be stored inside injector's internal storage and
35 // then will be restored during extraction phase.
36 //
37 // This injector won't add any extra overhead into EncodedImage payload and
38 // support frames with any size of payload. Also assumes that every EncodedImage
39 // payload size is greater or equals to 3 bytes
40 //
41 // This injector doesn't support video frames/encoded images without frame ID.
42 class SingleProcessEncodedImageDataInjector
43     : public EncodedImageDataPropagator {
44  public:
45   SingleProcessEncodedImageDataInjector();
46   ~SingleProcessEncodedImageDataInjector() override;
47 
48   // Id and discard flag will be injected into EncodedImage buffer directly.
49   // This buffer won't be fully copied, so `source` image buffer will be also
50   // changed.
51   EncodedImage InjectData(uint16_t id,
52                           bool discard,
53                           const EncodedImage& source) override;
54 
Start(int expected_receivers_count)55   void Start(int expected_receivers_count) override {
56     MutexLock crit(&lock_);
57     expected_receivers_count_ = expected_receivers_count;
58   }
59   void AddParticipantInCall() override;
60   void RemoveParticipantInCall() override;
61   EncodedImageExtractionResult ExtractData(const EncodedImage& source) override;
62 
63  private:
64   // Contains data required to extract frame id from EncodedImage and restore
65   // original buffer.
66   struct ExtractionInfo {
67     // Number of bytes from the beginning of the EncodedImage buffer that will
68     // be used to store frame id and sub id.
69     const static size_t kUsedBufferSize = 3;
70     // Frame sub id to distinguish encoded images for different spatial layers.
71     uint8_t sub_id;
72     // Flag to show is this encoded images should be discarded by analyzing
73     // decoder because of not required spatial layer/simulcast stream.
74     bool discard;
75     // Data from first 3 bytes of origin encoded image's payload.
76     uint8_t origin_data[ExtractionInfo::kUsedBufferSize];
77     // Count of how many times this frame was received.
78     int received_count = 0;
79   };
80 
81   struct ExtractionInfoVector {
82     ExtractionInfoVector();
83     ~ExtractionInfoVector();
84 
85     // Next sub id, that have to be used for this frame id.
86     uint8_t next_sub_id = 0;
87     std::map<uint8_t, ExtractionInfo> infos;
88   };
89 
90   Mutex lock_;
91   int expected_receivers_count_ RTC_GUARDED_BY(lock_);
92   // Stores a mapping from frame id to extraction info for spatial layers
93   // for this frame id. There can be a lot of them, because if frame was
94   // dropped we can't clean it up, because we won't receive a signal on
95   // decoder side about that frame. In such case it will be replaced
96   // when sub id will overlap.
97   std::map<uint16_t, ExtractionInfoVector> extraction_cache_
98       RTC_GUARDED_BY(lock_);
99 };
100 
101 }  // namespace webrtc_pc_e2e
102 }  // namespace webrtc
103 
104 #endif  // TEST_PC_E2E_ANALYZER_VIDEO_SINGLE_PROCESS_ENCODED_IMAGE_DATA_INJECTOR_H_
105