webrtc-sys 0.3.33

Unsafe bindings to libwebrtc
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "video_source.h"

#include <stdio.h>

#include "fileutils.h"

#define ASSERT_TRUE(condition) \
  do { \
    if (!(condition)) { \
      fprintf(stderr, "Assertion failed: %s\n", #condition); \
      abort(); \
    } \
  } while (0)

VideoSource::VideoSource()
:
_fileName(webrtc::test::ProjectRootPath() + "resources/foreman_cif.yuv"),
_width(352),
_height(288),
_type(webrtc::VideoType::kI420),
_frameRate(30)
{
}

VideoSource::VideoSource(std::string fileName, VideoSize size,
    int frameRate /*= 30*/, webrtc::VideoType type /*=  webrtc::kI420*/)
:
_fileName(fileName),
_type(type),
_frameRate(frameRate)
{
    assert(size != kUndefined && size != kNumberOfVideoSizes);
    assert(type != webrtc::VideoType::kUnknown);
    assert(frameRate > 0);
    if (GetWidthHeight(size, _width, _height) != 0) {
        assert(false);
    }
}

VideoSource::VideoSource(std::string fileName, int width, int height,
    int frameRate /*= 30*/,  webrtc::VideoType type /*=  webrtc::kI420*/)
:
_fileName(fileName),
_width(width),
_height(height),
_type(type),
_frameRate(frameRate)
{
    assert(width > 0);
    assert(height > 0);
    assert(type != webrtc::VideoType::kUnknown);
    assert(frameRate > 0);
}

VideoSize
VideoSource::GetSize() const
{
    return GetSize(_width, _height);
}

VideoSize
VideoSource::GetSize(uint16_t width, uint16_t height)
{
    if(width == 128 && height == 96)
    {
        return kSQCIF;
    }else if(width == 160 && height == 120)
    {
        return kQQVGA;
    }else if(width == 176 && height == 144)
    {
        return kQCIF;
    }else if(width == 320 && height == 240)
    {
        return kQVGA;
    }else if(width == 352 && height == 288)
    {
        return kCIF;
    }else if(width == 640 && height == 480)
    {
        return kVGA;
    }else if(width == 720 && height == 480)
    {
        return kNTSC;
    }else if(width == 704 && height == 576)
    {
        return k4CIF;
    }else if(width == 800 && height == 600)
    {
        return kSVGA;
    }else if(width == 960 && height == 720)
    {
        return kHD;
    }else if(width == 1024 && height == 768)
    {
        return kXGA;
    }else if(width == 1440 && height == 1080)
    {
        return kFullHD;
    }else if(width == 400 && height == 240)
    {
        return kWQVGA;
    }else if(width == 800 && height == 480)
    {
        return kWVGA;
    }else if(width == 1280 && height == 720)
    {
        return kWHD;
    }else if(width == 1920 && height == 1080)
    {
        return kWFullHD;
    }
    return kUndefined;
}

unsigned int
VideoSource::GetFrameLength() const
{
    return webrtc::CalcBufferSize(_type, _width, _height);
}

const char*
VideoSource::GetMySizeString() const
{
    return VideoSource::GetSizeString(GetSize());
}

const char*
VideoSource::GetSizeString(VideoSize size)
{
    switch (size)
    {
        case kSQCIF:
            return "SQCIF";
        case kQQVGA:
            return "QQVGA";
        case kQCIF:
            return "QCIF";
        case kQVGA:
            return "QVGA";
        case kCIF:
            return "CIF";
        case kVGA:
            return "VGA";
        case kNTSC:
            return "NTSC";
        case k4CIF:
            return "4CIF";
        case kSVGA:
            return "SVGA";
        case kHD:
            return "HD";
        case kXGA:
            return "XGA";
        case kFullHD:
            return "Full_HD";
        case kWQVGA:
            return "WQVGA";
        case kWHD:
            return "WHD";
        case kWFullHD:
            return "WFull_HD";
        default:
            return "Undefined";
    }
}

std::string
VideoSource::GetFilePath() const
{
    size_t slashPos = _fileName.find_last_of("/\\");
    if (slashPos == std::string::npos)
    {
        return ".";
    }

    return _fileName.substr(0, slashPos);
}

std::string
VideoSource::GetName() const
{
    // Remove path.
    size_t slashPos = _fileName.find_last_of("/\\");
    if (slashPos == std::string::npos)
    {
        slashPos = 0;
    }
    else
    {
        slashPos++;
    }

    // Remove extension and underscored suffix if it exists.
    return _fileName.substr(slashPos, std::min(_fileName.find_last_of("_"),
        _fileName.find_last_of(".")) - slashPos);
}

void
VideoSource::Convert(const VideoSource &target, bool force /* = false */) const
{
    // Ensure target rate is less than or equal to source
    // (i.e. we are only temporally downsampling).
    ASSERT_TRUE(target.GetFrameRate() <= _frameRate);
    // Only supports YUV420 currently.
    ASSERT_TRUE(_type == webrtc::VideoType::kI420 && target.GetType() == webrtc::VideoType::kI420);
    if (!force && (FileExists(target.GetFileName().c_str()) ||
        (target.GetWidth() == _width && target.GetHeight() == _height && target.GetFrameRate() == _frameRate)))
    {
        // Assume that the filename uniquely defines the content.
        // If the file already exists, it is the correct file.
        return;
    }
    FILE *inFile = NULL;
    FILE *outFile = NULL;

    inFile = fopen(_fileName.c_str(), "rb");
    ASSERT_TRUE(inFile != NULL);

    outFile = fopen(target.GetFileName().c_str(), "wb");
    ASSERT_TRUE(outFile != NULL);

    FrameDropper fd;
    fd.SetFrameRate(target.GetFrameRate(), _frameRate);

    const size_t lengthOutFrame = webrtc::CalcBufferSize(target.GetType(),
        target.GetWidth(), target.GetHeight());
    ASSERT_TRUE(lengthOutFrame > 0);
    unsigned char *outFrame = new unsigned char[lengthOutFrame];

    const size_t lengthInFrame = webrtc::CalcBufferSize(_type, _width, _height);
    ASSERT_TRUE(lengthInFrame > 0);
    unsigned char *inFrame = new unsigned char[lengthInFrame];

    while (fread(inFrame, 1, lengthInFrame, inFile) == lengthInFrame)
    {
        if (!fd.DropFrame())
        {
            ASSERT_TRUE(target.GetWidth() == _width &&
                   target.GetHeight() == _height);
            // Add video interpolator here!
            if (fwrite(outFrame, 1, lengthOutFrame,
                       outFile) !=  lengthOutFrame) {
              return;
            }
        }
    }

    delete inFrame;
    delete outFrame;
    fclose(inFile);
    fclose(outFile);
}

bool VideoSource::FileExists(const char* fileName)
{
    FILE* fp = NULL;
    fp = fopen(fileName, "rb");
    if(fp != NULL)
    {
        fclose(fp);
        return true;
    }
    return false;
}


int
VideoSource::GetWidthHeight( VideoSize size, int & width, int& height)
{
    switch(size)
    {
    case kSQCIF:
        width = 128;
        height = 96;
        return 0;
    case kQQVGA:
        width = 160;
        height = 120;
        return 0;
    case kQCIF:
        width = 176;
        height = 144;
        return 0;
    case kCGA:
        width = 320;
        height = 200;
        return 0;
    case kQVGA:
        width = 320;
        height = 240;
        return 0;
    case kSIF:
        width = 352;
        height = 240;
        return 0;
    case kWQVGA:
        width = 400;
        height = 240;
        return 0;
    case kCIF:
        width = 352;
        height = 288;
        return 0;
    case kW288p:
        width = 512;
        height = 288;
        return 0;
    case k448p:
        width = 576;
        height = 448;
        return 0;
    case kVGA:
        width = 640;
        height = 480;
        return 0;
    case k432p:
        width = 720;
        height = 432;
        return 0;
    case kW432p:
        width = 768;
        height = 432;
        return 0;
    case k4SIF:
        width = 704;
        height = 480;
        return 0;
    case kW448p:
        width = 768;
        height = 448;
        return 0;
    case kNTSC:
        width = 720;
        height = 480;
        return 0;
    case kFW448p:
        width = 800;
        height = 448;
        return 0;
    case kWVGA:
        width = 800;
        height = 480;
        return 0;
    case k4CIF:
        width = 704;
        height = 576;
        return 0;
    case kSVGA:
        width = 800;
        height = 600;
        return 0;
    case kW544p:
        width = 960;
        height = 544;
        return 0;
    case kW576p:
        width = 1024;
        height = 576;
        return 0;
    case kHD:
        width = 960;
        height = 720;
        return 0;
    case kXGA:
        width = 1024;
        height = 768;
        return 0;
    case kFullHD:
        width = 1440;
        height = 1080;
        return 0;
    case kWHD:
        width = 1280;
        height = 720;
        return 0;
    case kWFullHD:
        width = 1920;
        height = 1080;
        return 0;
    default:
        return -1;
    }
}

FrameDropper::FrameDropper()
:
_dropsBetweenRenders(0),
_frameCounter(0)
{
}

bool
FrameDropper::DropFrame()
{
    _frameCounter++;
    if (_frameCounter > _dropsBetweenRenders)
    {
        _frameCounter = 0;
        return false;
    }
    return true;
}

unsigned int
FrameDropper::DropsBetweenRenders()
{
    return _dropsBetweenRenders;
}

void
FrameDropper::SetFrameRate(double frameRate, double maxFrameRate)
{
    if (frameRate >= 1.0)
    {
        _dropsBetweenRenders = static_cast<unsigned int>(maxFrameRate / frameRate + 0.5) - 1;
    }
    else
    {
        _dropsBetweenRenders = 0;
    }
}