1 /*
2  *  Copyright (c) 2017 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 #include "media/engine/scopedvideodecoder.h"
12 
13 #include <vector>
14 
15 #include "api/video_codecs/video_decoder.h"
16 
17 namespace cricket {
18 
19 namespace {
20 
21 class ScopedVideoDecoder : public webrtc::VideoDecoder {
22  public:
23   ScopedVideoDecoder(WebRtcVideoDecoderFactory* factory,
24                      webrtc::VideoDecoder* decoder);
25 
26   int32_t InitDecode(const webrtc::VideoCodec* codec_settings,
27                      int32_t number_of_cores) override;
28   int32_t RegisterDecodeCompleteCallback(
29       webrtc::DecodedImageCallback* callback) override;
30   int32_t Release() override;
31   int32_t Decode(const webrtc::EncodedImage& input_image,
32                  bool missing_frames,
33                  const webrtc::RTPFragmentationHeader* fragmentation,
34                  const webrtc::CodecSpecificInfo* codec_specific_info,
35                  int64_t render_time_ms) override;
36   bool PrefersLateDecoding() const override;
37   const char* ImplementationName() const override;
38 
39   ~ScopedVideoDecoder() override;
40 
41  private:
42   WebRtcVideoDecoderFactory* factory_;
43   webrtc::VideoDecoder* decoder_;
44 };
45 
ScopedVideoDecoder(WebRtcVideoDecoderFactory * factory,webrtc::VideoDecoder * decoder)46 ScopedVideoDecoder::ScopedVideoDecoder(WebRtcVideoDecoderFactory* factory,
47                                        webrtc::VideoDecoder* decoder)
48     : factory_(factory), decoder_(decoder) {}
49 
InitDecode(const webrtc::VideoCodec * codec_settings,int32_t number_of_cores)50 int32_t ScopedVideoDecoder::InitDecode(const webrtc::VideoCodec* codec_settings,
51                                        int32_t number_of_cores) {
52   return decoder_->InitDecode(codec_settings, number_of_cores);
53 }
54 
RegisterDecodeCompleteCallback(webrtc::DecodedImageCallback * callback)55 int32_t ScopedVideoDecoder::RegisterDecodeCompleteCallback(
56     webrtc::DecodedImageCallback* callback) {
57   return decoder_->RegisterDecodeCompleteCallback(callback);
58 }
59 
Release()60 int32_t ScopedVideoDecoder::Release() {
61   return decoder_->Release();
62 }
63 
Decode(const webrtc::EncodedImage & input_image,bool missing_frames,const webrtc::RTPFragmentationHeader * fragmentation,const webrtc::CodecSpecificInfo * codec_specific_info,int64_t render_time_ms)64 int32_t ScopedVideoDecoder::Decode(
65     const webrtc::EncodedImage& input_image,
66     bool missing_frames,
67     const webrtc::RTPFragmentationHeader* fragmentation,
68     const webrtc::CodecSpecificInfo* codec_specific_info,
69     int64_t render_time_ms) {
70   return decoder_->Decode(input_image, missing_frames, fragmentation,
71                           codec_specific_info, render_time_ms);
72 }
73 
PrefersLateDecoding() const74 bool ScopedVideoDecoder::PrefersLateDecoding() const {
75   return decoder_->PrefersLateDecoding();
76 }
77 
ImplementationName() const78 const char* ScopedVideoDecoder::ImplementationName() const {
79   return decoder_->ImplementationName();
80 }
81 
~ScopedVideoDecoder()82 ScopedVideoDecoder::~ScopedVideoDecoder() {
83   factory_->DestroyVideoDecoder(decoder_);
84 }
85 
86 }  // namespace
87 
CreateScopedVideoDecoder(WebRtcVideoDecoderFactory * factory,const VideoCodec & codec,VideoDecoderParams params)88 std::unique_ptr<webrtc::VideoDecoder> CreateScopedVideoDecoder(
89     WebRtcVideoDecoderFactory* factory,
90     const VideoCodec& codec,
91     VideoDecoderParams params) {
92   webrtc::VideoDecoder* decoder =
93       factory->CreateVideoDecoderWithParams(codec, params);
94   if (!decoder)
95     return nullptr;
96   return std::unique_ptr<webrtc::VideoDecoder>(
97       new ScopedVideoDecoder(factory, decoder));
98 }
99 
100 }  // namespace cricket
101