|
| 1 | +#include <rho/img/tVpxImageEncoder.h> |
| 2 | +#include <rho/img/tVpxImageAsyncReadable.h> |
| 3 | +#include <rho/img/tImageCapFactory.h> |
| 4 | +#include <rho/sync/tStreamPCQ.h> |
| 5 | +#include <rho/sync/tThread.h> |
| 6 | +#include <rho/tCrashReporter.h> |
| 7 | +#include <rho/tTest.h> |
| 8 | + |
| 9 | + |
| 10 | +using namespace rho; |
| 11 | + |
| 12 | + |
| 13 | +class tReaderThread : public sync::iRunnable, |
| 14 | + public img::iAsyncReadableImageObserver, |
| 15 | + public bNonCopyable |
| 16 | +{ |
| 17 | + public: |
| 18 | + |
| 19 | + tReaderThread(const tTest& t, refc<iReadable> readable) |
| 20 | + : t(t), |
| 21 | + m_readable(readable), |
| 22 | + m_imageReadable(this), |
| 23 | + m_stop(false), |
| 24 | + m_imageCounter(0) |
| 25 | + { |
| 26 | + } |
| 27 | + |
| 28 | + void run() |
| 29 | + { |
| 30 | + try |
| 31 | + { |
| 32 | + while (!m_stop) |
| 33 | + { |
| 34 | + u8 buf[1024]; |
| 35 | + i32 r = m_readable->read(buf, 1024); |
| 36 | + if (r <= 0) |
| 37 | + break; |
| 38 | + m_imageReadable.takeInput(buf, r); |
| 39 | + } |
| 40 | + } |
| 41 | + catch (std::exception& e) |
| 42 | + { |
| 43 | + std::cerr << "Caught exception: " << e.what() << std::endl; |
| 44 | + t.fail(); |
| 45 | + } |
| 46 | + m_imageReadable.endStream(); |
| 47 | + } |
| 48 | + |
| 49 | + void gotImage(const img::tImage& image) |
| 50 | + { |
| 51 | + std::cout << "GOT image " << m_imageCounter++ << std::endl; |
| 52 | + } |
| 53 | + |
| 54 | + void endStream() |
| 55 | + { |
| 56 | + m_stop = true; |
| 57 | + } |
| 58 | + |
| 59 | + private: |
| 60 | + |
| 61 | + const tTest& t; |
| 62 | + refc<iReadable> m_readable; |
| 63 | + img::tVpxImageAsyncReadable m_imageReadable; |
| 64 | + bool m_stop; |
| 65 | + int m_imageCounter; |
| 66 | +}; |
| 67 | + |
| 68 | + |
| 69 | +void testDelay(const tTest& t) |
| 70 | +{ |
| 71 | + refc<img::iImageCapParamsEnumerator> enumerator; |
| 72 | + |
| 73 | + try |
| 74 | + { |
| 75 | + enumerator = |
| 76 | + img::tImageCapFactory::getImageCapParamsEnumerator(); |
| 77 | + } |
| 78 | + catch (eNotImplemented& e) |
| 79 | + { |
| 80 | + return; // it's ok if the enumerator is not implemented on this platform |
| 81 | + } |
| 82 | + |
| 83 | + if (enumerator->size() == 0) |
| 84 | + return; |
| 85 | + |
| 86 | + img::tImageCapParams params = (*enumerator)[0]; |
| 87 | + params.displayFormat = img::kRGB24; |
| 88 | + |
| 89 | + refc<img::iImageCap> cap; |
| 90 | + |
| 91 | + try |
| 92 | + { |
| 93 | + cap = |
| 94 | + img::tImageCapFactory::getImageCap(params, true); |
| 95 | + } |
| 96 | + catch (eNotImplemented& e) |
| 97 | + { |
| 98 | + return; // it's ok if the capturer is not implemented on this platform |
| 99 | + } |
| 100 | + |
| 101 | + img::tImage image; |
| 102 | + |
| 103 | + refc< sync::tPCQ<std::pair<u8*, u32> > > pcq(new sync::tPCQ<std::pair<u8*, u32> >(8, sync::kGrow)); |
| 104 | + refc<iReadable> readable(new sync::tStreamPCQ(pcq, sync::tStreamPCQ::kReadableEnd)); |
| 105 | + refc<iWritable> writable(new sync::tStreamPCQ(pcq, sync::tStreamPCQ::kWritableEnd)); |
| 106 | + |
| 107 | + sync::tThread thread(refc<sync::iRunnable>(new tReaderThread(t, readable))); |
| 108 | + |
| 109 | + img::tVpxImageEncoder encoder(writable, 10000, |
| 110 | + params.frameIntervalDenominator / params.frameIntervalNumerator, |
| 111 | + params.imageWidth, params.imageHeight); |
| 112 | + |
| 113 | + for (int i = 0; i < 10; i++) |
| 114 | + { |
| 115 | + cap->getFrame(&image); |
| 116 | + std::cout << "SENT image " << i << std::endl; |
| 117 | + encoder.encodeImage(image); |
| 118 | + } |
| 119 | + encoder.signalEndOfStream(); |
| 120 | + |
| 121 | + thread.join(); |
| 122 | +} |
| 123 | + |
| 124 | + |
| 125 | +int main() |
| 126 | +{ |
| 127 | + tCrashReporter::init(); |
| 128 | + |
| 129 | + tTest("VPX Delay Test", testDelay); |
| 130 | + |
| 131 | + return 0; |
| 132 | +} |
0 commit comments