• 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 class SingleProcessEncodedImageDataInjector : public EncodedImageDataInjector,
41                                               public EncodedImageDataExtractor {
42  public:
43   SingleProcessEncodedImageDataInjector();
44   ~SingleProcessEncodedImageDataInjector() override;
45 
46   // Id and discard flag will be injected into EncodedImage buffer directly.
47   // This buffer won't be fully copied, so |source| image buffer will be also
48   // changed.
49   EncodedImage InjectData(uint16_t id,
50                           bool discard,
51                           const EncodedImage& source,
52                           int coding_entity_id) override;
53   EncodedImageExtractionResult ExtractData(const EncodedImage& source,
54                                            int coding_entity_id) override;
55 
56  private:
57   // Contains data required to extract frame id from EncodedImage and restore
58   // original buffer.
59   struct ExtractionInfo {
60     // Number of bytes from the beginning of the EncodedImage buffer that will
61     // be used to store frame id and sub id.
62     const static size_t kUsedBufferSize = 3;
63     // Frame sub id to distinguish encoded images for different spatial layers.
64     uint8_t sub_id;
65     // Flag to show is this encoded images should be discarded by analyzing
66     // decoder because of not required spatial layer/simulcast stream.
67     bool discard;
68     // Data from first 3 bytes of origin encoded image's payload.
69     uint8_t origin_data[ExtractionInfo::kUsedBufferSize];
70   };
71 
72   struct ExtractionInfoVector {
73     ExtractionInfoVector();
74     ~ExtractionInfoVector();
75 
76     // Next sub id, that have to be used for this frame id.
77     uint8_t next_sub_id = 0;
78     std::map<uint8_t, ExtractionInfo> infos;
79   };
80 
81   Mutex lock_;
82   // Stores a mapping from frame id to extraction info for spatial layers
83   // for this frame id. There can be a lot of them, because if frame was
84   // dropped we can't clean it up, because we won't receive a signal on
85   // decoder side about that frame. In such case it will be replaced
86   // when sub id will overlap.
87   std::map<uint16_t, ExtractionInfoVector> extraction_cache_
88       RTC_GUARDED_BY(lock_);
89 };
90 
91 }  // namespace webrtc_pc_e2e
92 }  // namespace webrtc
93 
94 #endif  // TEST_PC_E2E_ANALYZER_VIDEO_SINGLE_PROCESS_ENCODED_IMAGE_DATA_INJECTOR_H_
95