1/*
2 *  Copyright (c) 2012 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 WEBRTC_MODULES_VIDEO_CODING_CODECS_TEST_FRAMEWORK_UNIT_TEST_H_
12#define WEBRTC_MODULES_VIDEO_CODING_CODECS_TEST_FRAMEWORK_UNIT_TEST_H_
13
14#include "webrtc/modules/video_coding/codecs/test_framework/test.h"
15#include "webrtc/system_wrappers/interface/event_wrapper.h"
16
17// Disable "conditional expression is constant" warnings on the perfectly
18// acceptable
19// do { ... } while (0) constructions below.
20// Refer to http://stackoverflow.com/questions/1946445/
21//   is-there-better-way-to-write-do-while0-construct-to-avoid-compiler-warnings
22// for some discussion of the issue.
23#ifdef _WIN32
24#pragma warning(disable : 4127)
25#endif
26
27class VideoSource;
28class UnitTestEncodeCompleteCallback;
29class UnitTestDecodeCompleteCallback;
30
31class UnitTest : public CodecTest
32{
33public:
34    UnitTest();
35    virtual ~UnitTest();
36    virtual void Setup();
37    virtual void Perform();
38    virtual void Print();
39
40protected:
41    UnitTest(std::string name, std::string description);
42    virtual uint32_t CodecSpecific_SetBitrate(
43        uint32_t bitRate,
44        uint32_t /* frameRate */);
45    virtual void Teardown();
46    virtual void RateControlTests();
47    virtual int Decode();
48    virtual int DecodeWithoutAssert();
49    virtual int SetCodecSpecificParameters() {return 0;};
50
51    virtual bool CheckIfBitExact(const void *ptrA, unsigned int aLengthBytes,
52                                 const void *ptrB, unsigned int bLengthBytes);
53
54    uint32_t WaitForEncodedFrame() const;
55    uint32_t WaitForDecodedFrame() const;
56
57    int _tests;
58    int _errors;
59
60    VideoSource* _source;
61    unsigned char* _refFrame;
62    unsigned char* _refEncFrame;
63    unsigned char* _refDecFrame;
64    unsigned int _refEncFrameLength;
65    FILE* _sourceFile;
66    bool is_key_frame_;
67
68    UnitTestEncodeCompleteCallback* _encodeCompleteCallback;
69    UnitTestDecodeCompleteCallback* _decodeCompleteCallback;
70    enum { kMaxWaitEncTimeMs = 100 };
71    enum { kMaxWaitDecTimeMs = 25 };
72};
73
74class UnitTestEncodeCompleteCallback : public webrtc::EncodedImageCallback
75{
76public:
77    UnitTestEncodeCompleteCallback(webrtc::VideoFrame* buffer,
78                                   uint32_t decoderSpecificSize = 0,
79                                   void* decoderSpecificInfo = NULL) :
80      _encodedVideoBuffer(buffer),
81      _encodeComplete(false) {}
82    int32_t Encoded(webrtc::EncodedImage& encodedImage,
83                    const webrtc::CodecSpecificInfo* codecSpecificInfo,
84                    const webrtc::RTPFragmentationHeader* fragmentation = NULL);
85    bool EncodeComplete();
86    // Note that this only makes sense if an encode has been completed
87    webrtc::VideoFrameType EncodedFrameType() const;
88private:
89    webrtc::VideoFrame* _encodedVideoBuffer;
90    bool _encodeComplete;
91    webrtc::VideoFrameType _encodedFrameType;
92};
93
94class UnitTestDecodeCompleteCallback : public webrtc::DecodedImageCallback
95{
96public:
97    UnitTestDecodeCompleteCallback(webrtc::I420VideoFrame* buffer) :
98        _decodedVideoBuffer(buffer), _decodeComplete(false) {}
99    int32_t Decoded(webrtc::I420VideoFrame& image);
100    bool DecodeComplete();
101private:
102    webrtc::I420VideoFrame* _decodedVideoBuffer;
103    bool _decodeComplete;
104};
105
106#endif // WEBRTC_MODULES_VIDEO_CODING_CODECS_TEST_FRAMEWORK_UNIT_TEST_H_
107