commit 833d7dc068a606c9c35cdf657008c16a27047224 Author: zcy <290198252@qq.com> Date: Mon Nov 13 00:13:24 2023 +0800 no message diff --git a/components/toast.cpp b/components/toast.cpp new file mode 100644 index 0000000..e2b6987 --- /dev/null +++ b/components/toast.cpp @@ -0,0 +1,82 @@ +#include "toast.h" +#include +#include +#include +#include +#include +#include + +ToastWidget::ToastWidget(QWidget *parent) + : QWidget(parent) +{ + ui.setupUi(this); + + setWindowFlags(windowFlags() | Qt::FramelessWindowHint | Qt::Tool);// 无边框 无任务栏 + setAttribute(Qt::WA_TranslucentBackground, true); // 背景透明 +} + +ToastWidget::~ToastWidget() +{ + +} + + +void ToastWidget::setText(const QString& text) +{ + ui.label->setText(text); +} + +void ToastWidget::showAnimation(int timeout /*= 2000*/) +{ + // 开始动画 + QPropertyAnimation *animation = new QPropertyAnimation(this, "windowOpacity"); + animation->setDuration(1000); + animation->setStartValue(0); + animation->setEndValue(1); + animation->start(); + show(); + + QTimer::singleShot(timeout, [&] + { + // 结束动画 + QPropertyAnimation *animation = new QPropertyAnimation(this, "windowOpacity"); + animation->setDuration(1000); + animation->setStartValue(1); + animation->setEndValue(0); + animation->start(); + connect(animation, &QPropertyAnimation::finished, [&] + { + close(); + deleteLater();// 关闭后析构 + }); + }); +} + +void ToastWidget::showTip(const QString& text, QWidget* parent /*= nullptr*/) +{ + ToastWidget* toast = new ToastWidget(parent); + toast->setWindowFlags(toast->windowFlags() | Qt::WindowStaysOnTopHint); // 置顶 + toast->setText(text); + toast->setStyleSheet("font:bold;font-size:24px;color:rgb(255,255,255);"); + toast->adjustSize(); //设置完文本后调整下大小 + // 测试显示位于主屏的70%高度位置 + qDebug()<geometry(); + toast->move((parent->geometry().x() + (parent->size().width() - toast->width()) / 2), + parent->geometry().y() + (parent->size().height() * 5 / 10)); + toast->showAnimation(100); +} + +void ToastWidget::paintEvent(QPaintEvent *event) +{ + QPainter paint(this); + paint.begin(this); + auto kBackgroundColor = QColor(255, 255, 255); + kBackgroundColor.setAlpha(0.1);// 透明度为0 + paint.setRenderHint(QPainter::Antialiasing, true); + paint.setPen(Qt::NoPen); + paint.setBrush(QBrush(kBackgroundColor, Qt::SolidPattern));//设置画刷形式 + paint.drawRect(0, 0, width(), height()); + paint.end(); +} + + diff --git a/components/toast.h b/components/toast.h new file mode 100644 index 0000000..41c8209 --- /dev/null +++ b/components/toast.h @@ -0,0 +1,31 @@ + +#ifndef __TOAST__ +#define __TOAST__ + +#include +#include "ui_toast.h" + +class ToastWidget : public QWidget +{ + Q_OBJECT + +public: + ToastWidget(QWidget *parent = Q_NULLPTR); + ~ToastWidget(); + + void setText(const QString& text); + + void showAnimation(int timeout = 2000);// 动画方式show出,默认2秒后消失 + +public: + // 静态调用 + static void showTip(const QString& text, QWidget* parent = nullptr); + +protected: + virtual void paintEvent(QPaintEvent *event); + +private: + Ui::Form ui; +}; + +#endif diff --git a/components/toast.ui b/components/toast.ui new file mode 100644 index 0000000..0cbc9bc --- /dev/null +++ b/components/toast.ui @@ -0,0 +1,32 @@ + + + Form + + + + 0 + 0 + 932 + 59 + + + + Form + + + + + 170 + 10 + 231 + 31 + + + + TextLabel + + + + + + diff --git a/conanfile.txt b/conanfile.txt new file mode 100644 index 0000000..3134468 --- /dev/null +++ b/conanfile.txt @@ -0,0 +1,4 @@ +[requires] +ffmpeg/4.2.1 +[imports] +.,* -> ./third/msvc32 @ folder=True, ignore_case=True, excludes=*.html *.jpeg \ No newline at end of file diff --git a/cplaywidget.cpp b/cplaywidget.cpp new file mode 100644 index 0000000..f4e8d87 --- /dev/null +++ b/cplaywidget.cpp @@ -0,0 +1,422 @@ +#include "CPlayWidget.h" + +#include +#include +#include +#include "CPlayWidget.h" + + +// ɫԴ + +const char *vsrcyuv = "attribute vec4 vertexIn; \ +attribute vec2 textureIn; \ +varying vec2 textureOut; \ +void main(void) \ +{ \ + gl_Position = vertexIn; \ + textureOut = textureIn; \ +}"; + + +// ƬɫԴ + +const char *fsrcyuv = "varying vec2 textureOut; \ +uniform sampler2D tex_y; \ +uniform sampler2D tex_u; \ +uniform sampler2D tex_v; \ +void main(void) \ +{ \ + vec3 yuv; \ + vec3 rgb; \ + yuv.x = texture2D(tex_y, textureOut).r; \ + yuv.y = texture2D(tex_u, textureOut).r - 0.5; \ + yuv.z = texture2D(tex_v, textureOut).r - 0.5; \ + rgb = mat3( 1, 1, 1, \ + 0, -0.39465, 2.03211, \ + 1.13983, -0.58060, 0) * yuv; \ + gl_FragColor = vec4(rgb, 1); \ +}"; + +// rgbƬɫԴ +// עMEDIASUBTYPE_RGB32 bgrģҪٽһת + + +const char *fsrcrgb = "varying vec2 textureOut; \ + uniform sampler2D rgbdata; \ + void main() \ + { \ + gl_FragColor = texture(rgbdata, textureOut); \ + }"; + +void CPlayWidget::OnUpdateFrame() { + this->PlayOneFrame(); +} + +void CPlayWidget::OnPaintData(const uint8_t *data, uint32_t len) +{ + if(nullptr == m_pBufYuv420p) + { + m_pBufYuv420p = new unsigned char[len]; + qDebug("CPlayWidget::PlayOneFrame new data memory. Len=%d width=%d height=%d\n", + len, m_nVideoW, m_nVideoW); + memcpy(m_pBufYuv420p, data,len); + //ˢ½,paintGLӿ + update(); + } +} + +CPlayWidget::CPlayWidget(QWidget *parent):QOpenGLWidget(parent) { + textureUniformY = 0; + textureUniformU = 0; + textureUniformV = 0; + id_y = 0; + id_u = 0; + id_v = 0; + m_pTextureRGB = nullptr; + m_pBufYuv420p = nullptr; + m_pVSHader = NULL; + m_pFSHader = NULL; + m_pShaderProgram = NULL; + m_pTextureY = NULL; + m_pTextureU = NULL; + m_pTextureV = NULL; + m_pYuvFile = NULL; + m_nVideoH = 0; + m_nVideoW = 0; + mType = TYPE_YUV420P; + connect(&this->tm,SIGNAL(timeout()),this,SLOT(OnUpdateFrame())); + //tm.start(1000); +} + +CPlayWidget::~CPlayWidget() { +} + +void CPlayWidget::PlayOneFrame() {//ܶȡһyuvͼݽʾ,ÿһΣʾһͼƬ + if(NULL == m_pYuvFile) + { + //yuvƵļ ע޸ļ· + // m_pYuvFile = fopen("F://OpenglYuvDemo//1920_1080.yuv", "rb"); + m_pYuvFile = fopen("F://md_sample_sp420_1080p.yuv", "rb"); + //yuvƵݵķֱÿ,demo1080pطҪעʵݷֱʶӦ +// m_nVideoW = 1920; +// m_nVideoH = 1080; + } + //ڴһ֡yuvͼ,СΪֱʵ1.5 + + + int nLen = m_nVideoW*m_nVideoH*3/2; + if(nullptr == m_pBufYuv420p) + { + m_pBufYuv420p = new unsigned char[nLen]; + qDebug("CPlayWidget::PlayOneFrame new data memory. Len=%d width=%d height=%d\n", + nLen, m_nVideoW, m_nVideoW); + } + //һ֡yuvͼڴ + + if(NULL == m_pYuvFile) + { + qFatal("read yuv file err.may be path is wrong!\n"); + return; + } + fread(m_pBufYuv420p, 1, nLen, m_pYuvFile); + //ˢ½,paintGLӿ + update(); + return; +} + +int CPlayWidget::SetDataType(CPlayWidget::IMG_TYPE type){ + this->mType = type; + return 0; +} + +int CPlayWidget::OnCameraData(uint8_t *dat, uint32_t size) +{ + memcpy(this->m_pBufRgb32,dat,size); + update(); + return 0; + +} + +int CPlayWidget::SetImgSize(uint32_t width, uint32_t height) +{ + m_nVideoH = height; + m_nVideoW = width; + if(mType == TYPE_RGB32){ + m_pBufRgb32 = new uint8_t[width * height *4]; + } + if(mType == TYPE_YUV420P){ + m_pBufYuv420p = new uint8_t[width * height *3/2]; + } + return 0; +} + + +/* + + * Y = 0.299 R + 0.587 G + 0.114 B + +U = - 0.1687 R - 0.3313 G + 0.5 B + 128 + +V = 0.5 R - 0.4187 G - 0.0813 B + 128 + +RGB ҲֱӴYUV (256) : + +R = Y + 1.402 (Cr-128) + +G = Y - 0.34414 (Cb-128) - 0.71414 (Cr-128) + +B = Y + 1.772 (Cb-128) +*/ +void CPlayWidget::initializeGL() +{ + initializeOpenGLFunctions(); + glEnable(GL_DEPTH_TEST); + + //ִopenglȾɫ + //ɫʹopenGLɫ(OpenGL Shading Language, GLSL)дһС, + // GLSLǹOpenGLɫ,GLSLԵ﷨Ҫ߲ + //ʼɫ + + m_pVSHader = new QOpenGLShader(QOpenGLShader::Vertex, this); + + //붥ɫ + bool bCompile = m_pVSHader->compileSourceCode(vsrcyuv); + if(!bCompile) + { + // todo ô״̬ + } + //ʼƬɫ gpuyuvתrgb + m_pFSHader = new QOpenGLShader(QOpenGLShader::Fragment, this); + if(mType == TYPE_RGB32){ + bCompile = m_pFSHader->compileSourceCode(fsrcrgb); + } + if(mType == TYPE_YUV420P){ + bCompile = m_pFSHader->compileSourceCode(fsrcyuv); + } + if(!bCompile) + { + // todo ô״̬ + } +#define PROGRAM_VERTEX_ATTRIBUTE 0 +#define PROGRAM_TEXCOORD_ATTRIBUTE 1 + //ɫ + + m_pShaderProgram = new QOpenGLShaderProgram; + //Ƭɫӵ + + m_pShaderProgram->addShader(m_pFSHader); + //ɫӵ + + m_pShaderProgram->addShader(m_pVSHader); + //vertexInָλATTRIB_VERTEX,ڶɫԴ + + m_pShaderProgram->bindAttributeLocation("vertexIn", ATTRIB_VERTEX); + //textureInָλATTRIB_TEXTURE,ڶɫԴ + + m_pShaderProgram->bindAttributeLocation("textureIn", ATTRIB_TEXTURE); + //뵽ɫ + + m_pShaderProgram->link(); + + // + + m_pShaderProgram->bind(); + + if(this->mType == TYPE_YUV420P){ + initShaderYuv(); + } + if(this->mType == TYPE_RGB32){ + initShaderRgb(); + } + glClearColor(0.0,0.0,0.0,0.0);//ñɫ +} + + +void CPlayWidget::resizeGL(int w, int h) +{ + if(h == 0)// ֹ + { + h = 1;// Ϊ1 + } + //ӿ + glViewport(0,0, w,h); +} + +void CPlayWidget::paintGL() +{ + if(mType == TYPE_YUV420P) + loadYuvTexture(); + if(mType == TYPE_RGB32){ + loadRgbTexture(); + } + //ʹö鷽ʽͼ + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + return; +} + +void CPlayWidget::initShaderYuv() +{ + //ȡɫеݱtex_y, tex_u, tex_vλ,Щ + //ƬɫԴпԿ + textureUniformY = m_pShaderProgram->uniformLocation("tex_y"); + textureUniformU = m_pShaderProgram->uniformLocation("tex_u"); + textureUniformV = m_pShaderProgram->uniformLocation("tex_v"); + // + static const GLfloat vertexVertices[] = { + -1.0f, -1.0f, + 1.0f, -1.0f, + -1.0f, 1.0f, + 1.0f, 1.0f, + }; + // + static const GLfloat textureVertices[] = { + 0.0f, 1.0f, + 1.0f, 1.0f, + 0.0f, 0.0f, + 1.0f, 0.0f, + }; + //ATTRIB_VERTEXĶֵԼʽ + glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, vertexVertices); + //ATTRIB_TEXTUREֵԼʽ + glVertexAttribPointer(ATTRIB_TEXTURE, 2, GL_FLOAT, 0, 0, textureVertices); + //ATTRIB_VERTEXԵ,Ĭǹرյ + glEnableVertexAttribArray(ATTRIB_VERTEX); + //ATTRIB_TEXTUREԵ,Ĭǹرյ + glEnableVertexAttribArray(ATTRIB_TEXTURE); + //ֱ𴴽y,u,v + m_pTextureY = new QOpenGLTexture(QOpenGLTexture::Target2D); + m_pTextureU = new QOpenGLTexture(QOpenGLTexture::Target2D); + m_pTextureV = new QOpenGLTexture(QOpenGLTexture::Target2D); + m_pTextureY->create(); + m_pTextureU->create(); + m_pTextureV->create(); + //ȡyֵ + id_y = m_pTextureY->textureId(); + //ȡuֵ + id_u = m_pTextureU->textureId(); + //ȡvֵ + id_v = m_pTextureV->textureId(); +} + +void CPlayWidget::initShaderRgb() +{ + //ȡɫеݱtex_y, tex_u, tex_vλ,Щ + //ƬɫԴпԿ + textureUniformRGB = m_pShaderProgram->uniformLocation("rgbdata"); + // + static const GLfloat vertexVertices[] = { + -1.0f, -1.0f, + 1.0f, -1.0f, + -1.0f, 1.0f, + 1.0f, 1.0f, + }; + + // + + static const GLfloat textureVertices[] = { + 0.0f, 0.0f, + 1.0f, 0.0f, + 0.0f, 1.0f, + 1.0f, 1.0f, + }; + //ATTRIB_VERTEXĶֵԼʽ + glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, vertexVertices); + //ATTRIB_TEXTUREֵԼʽ + glVertexAttribPointer(ATTRIB_TEXTURE, 2, GL_FLOAT, 0, 0, textureVertices); + //ATTRIB_VERTEXԵ,Ĭǹرյ + glEnableVertexAttribArray(ATTRIB_VERTEX); + //ATTRIB_TEXTUREԵ,Ĭǹرյ + glEnableVertexAttribArray(ATTRIB_TEXTURE); + //ֱ𴴽y,u,v + m_pTextureRGB = new QOpenGLTexture(QOpenGLTexture::Target2D); + m_pTextureRGB->create(); + //ȡyֵ + id_rgb = m_pTextureRGB->textureId(); +} + +int CPlayWidget::loadYuvTexture() +{ + //y + //ԪGL_TEXTURE0 + glActiveTexture(GL_TEXTURE0); + //ʹy + glBindTexture(GL_TEXTURE_2D, id_y); + //ʹڴm_pBufYuv420pݴy + glTexImage2D(GL_TEXTURE_2D, + 0, + GL_RED, + m_nVideoW, + m_nVideoH, + 0, + GL_RED, + GL_UNSIGNED_BYTE, + m_pBufYuv420p); + + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + //u + glActiveTexture(GL_TEXTURE1);//ԪGL_TEXTURE1 + glBindTexture(GL_TEXTURE_2D, id_u); + glTexImage2D(GL_TEXTURE_2D, + 0, GL_RED, + m_nVideoW/2, + m_nVideoH/2, + 0, + GL_RED, + GL_UNSIGNED_BYTE, + (char*)m_pBufYuv420p+m_nVideoW*m_nVideoH); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + //v + glActiveTexture(GL_TEXTURE2);//ԪGL_TEXTURE2 + glBindTexture(GL_TEXTURE_2D, id_v); + glTexImage2D(GL_TEXTURE_2D, + 0, GL_RED, + m_nVideoW/2, + m_nVideoH/2, + 0, GL_RED, + GL_UNSIGNED_BYTE, + (char*)m_pBufYuv420p+m_nVideoW*m_nVideoH*5/4); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + //ָyҪʹֵ ֻ0,1,2ȱʾԪopenglԻĵط + //0ӦԪGL_TEXTURE0 1ӦԪGL_TEXTURE1 2ӦĵԪ + glUniform1i(textureUniformY, 0); + //ָuҪʹֵ + glUniform1i(textureUniformU, 1); + //ָvҪʹֵ + glUniform1i(textureUniformV, 2); + return 0; +} + +int CPlayWidget::loadRgbTexture() +{ + //rgb + //ԪGL_TEXTURE0 + glActiveTexture(GL_TEXTURE0); + //ʹy + glBindTexture(GL_TEXTURE_2D, id_rgb); + //ʹڴm_pBufYuv420pݴy + glTexImage2D(GL_TEXTURE_2D, + 0, + GL_RGBA, + m_nVideoW, + m_nVideoH, + 0, + GL_BGRA, + GL_UNSIGNED_BYTE, + m_pBufRgb32); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glUniform1i(textureUniformRGB, 0); + return 0; +} + diff --git a/cplaywidget.h b/cplaywidget.h new file mode 100644 index 0000000..ec88d6e --- /dev/null +++ b/cplaywidget.h @@ -0,0 +1,75 @@ +#ifndef GLPLAYWIDGET_H +#define GLPLAYWIDGET_H +#include +#include +#include +#include +#include +#include "media/CameraCapture.h" + +#include + +#define ATTRIB_VERTEX 3 +#define ATTRIB_TEXTURE 4 + + + +class CPlayWidget:public QOpenGLWidget,protected QOpenGLFunctions,public Camera::CameraObserver +{ + Q_OBJECT +public slots: + void OnUpdateFrame(); + void OnPaintData(const uint8_t *data,uint32_t len); +public: + typedef enum{ + TYPE_YUV420P, + TYPE_RGB32, + }IMG_TYPE; + CPlayWidget(QWidget* parent); + ~CPlayWidget(); + void PlayOneFrame(); + int SetDataType(IMG_TYPE); + int OnCameraData(uint8_t *dat, uint32_t size) override; + int SetImgSize(uint32_t width,uint32_t ); +protected: + QTimer tm; + void initializeGL() override; + void resizeGL(int w, int h) override; + void paintGL() override; +private: + IMG_TYPE mType; // Ŀǰֵֻ֧RGB32,YUV420P + GLuint textureUniformY; //yλ + GLuint textureUniformU; //uλ + GLuint textureUniformV; //vλ + GLuint textureUniformRGB; //RGBλ + + + GLuint textureUnifromRGB; //rgb32 λ + + GLuint id_rgb; + GLuint id_y; + GLuint id_u; + GLuint id_v; //vID + + QOpenGLTexture* m_pTextureRGB; //RGB һ + + QOpenGLTexture* m_pTextureY; //y + QOpenGLTexture* m_pTextureU; //u + QOpenGLTexture* m_pTextureV; //v + QOpenGLShader *m_pVSHader; //ɫ + QOpenGLShader *m_pFSHader; //Ƭɫ + QOpenGLShaderProgram *m_pShaderProgram; //ɫ + int m_nVideoW; //Ƶֱʿ + int m_nVideoH; //Ƶֱʸ + unsigned char *m_pBufYuv420p; + unsigned char* m_pBufRgb32; + + FILE* m_pYuvFile; + + void initShaderYuv(); + void initShaderRgb(); + + int loadYuvTexture(); + int loadRgbTexture(); +}; +#endif diff --git a/depency/conanfile.txt b/depency/conanfile.txt new file mode 100644 index 0000000..3134468 --- /dev/null +++ b/depency/conanfile.txt @@ -0,0 +1,4 @@ +[requires] +ffmpeg/4.2.1 +[imports] +.,* -> ./third/msvc32 @ folder=True, ignore_case=True, excludes=*.html *.jpeg \ No newline at end of file diff --git a/inc/Base64.h b/inc/Base64.h new file mode 100644 index 0000000..9c1ead1 --- /dev/null +++ b/inc/Base64.h @@ -0,0 +1,25 @@ + +const unsigned char Base64IdxTab[128] = +{ + 255,255,255,255, 255,255,255,255, 255,255,255,255, 255,255,255,255, + 255,255,255,255, 255,255,255,255, 255,255,255,255, 255,255,255,255, + 255,255,255,255, 255,255,255,255, 255,255,255,62, 255,255,255,63, + 52,53,54,55, 56,57,58,59, 60,61,255,255, 255,255,255,255, + 255,0,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,255, 255,255,255,255, + 255,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,255, 255,255,255,255 +}; + +#define BVal(x) Base64IdxTab[x] + +int DecodeBase64(char * pInput, char * pOutput); + +const char Base64ValTab[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +#define AVal(x) Base64ValTab[x] + +int EncodeBase64(unsigned char * pInput, int iInputLen, unsigned char * pOutput); + +#define DCD_ONCE_LEN 400*1024 +#define CDC_ONCE_LEN 300*1024 diff --git a/inc/BitmapEx.h b/inc/BitmapEx.h new file mode 100644 index 0000000..f6682dd --- /dev/null +++ b/inc/BitmapEx.h @@ -0,0 +1,181 @@ +// BitmapEx.h: interface for the CBitmapEx class. +// +////////////////////////////////////////////////////////////////////// + +#if !defined(AFX_BITMAPEX_H__80F20A52_B43F_42C5_B182_AC8D27BF5C0E__INCLUDED_) +#define AFX_BITMAPEX_H__80F20A52_B43F_42C5_B182_AC8D27BF5C0E__INCLUDED_ + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + + +#define _PI 3.1415926f // Value of PI +#define _BITS_PER_PIXEL_32 32 // 32-bit color depth +#define _BITS_PER_PIXEL_24 24 // 24-bit color depth +#define _PIXEL DWORD // Pixel +#define _RGB(r,g,b) (((r) << 16) | ((g) << 8) | (b)) // Convert to RGB +#define _GetRValue(c) ((BYTE)(((c) & 0x00FF0000) >> 16)) // Red color component +#define _GetGValue(c) ((BYTE)(((c) & 0x0000FF00) >> 8)) // Green color component +#define _GetBValue(c) ((BYTE)((c) & 0x000000FF)) // Blue color component + + +typedef long fixed; // Our new fixed point type +#define itofx(x) ((x) << 8) // Integer to fixed point +#define ftofx(x) (long)((x) * 256) // Float to fixed point +#define dtofx(x) (long)((x) * 256) // Double to fixed point +#define fxtoi(x) ((x) >> 8) // Fixed point to integer +#define fxtof(x) ((float) (x) / 256) // Fixed point to float +#define fxtod(x) ((double)(x) / 256) // Fixed point to double +#define Mulfx(x,y) (((x) * (y)) >> 8) // Multiply a fixed by a fixed +#define Divfx(x,y) (((x) << 8) / (y)) // Divide a fixed by a fixed + + +typedef struct __POINT +{ + long x; + long y; + +} _POINT, *_LPPOINT; + +typedef struct __QUAD +{ + _POINT p1; + _POINT p2; + _POINT p3; + _POINT p4; + +} _QUAD, *_LPQUAD; + +typedef enum __RESAMPLE_MODE +{ + RM_NEARESTNEIGHBOUR = 0, + RM_BILINEAR, + RM_BICUBIC, + +} _RESAMPLE_MODE; + +typedef enum __GRADIENT_MODE +{ + GM_NONE = 0x00, + GM_HORIZONTAL = 0x01, + GM_VERTICAL = 0x02, + GM_RADIAL = 0x04 + +} _GRADIENT_MODE; + + +class CBitmapEx +{ +public: + // Public methods + CBitmapEx(); + virtual ~CBitmapEx(); + void Create(long width, long height); + void Create(CBitmapEx& bitmapEx); + void Load(LPTSTR lpszBitmapFile); + void Load(LPBYTE lpBitmapData); + void Save(LPTSTR lpszBitmapFile); + void Save(LPBYTE lpBitmapData); + void Scale(long horizontalPercent=100, long verticalPercent=100); + void Rotate(long degrees=0, _PIXEL bgColor=_RGB(0,0,0)); + void FlipHorizontal(); + void FlipVertical(); + void MirrorLeft(); + void MirrorRight(); + void MirrorTop(); + void MirrorBottom(); + void Clear(_PIXEL clearColor=_RGB(0,0,0)); + void Negative(); + void Grayscale(); + void Sepia(long depth=34); + void Emboss(); + void Engrave(); + void Pixelize(long size=4); + void Draw(HDC hDC); + void Draw(HDC hDC, long dstX, long dstY); + void Draw(long dstX, long dstY, long width, long height, CBitmapEx& bitmapEx, long srcX, long srcY); + void Draw(long dstX, long dstY, long width, long height, CBitmapEx& bitmapEx, long srcX, long srcY, long alpha); + void Draw(_QUAD dstQuad, CBitmapEx& bitmapEx); + void Draw(_QUAD dstQuad, CBitmapEx& bitmapEx, long alpha); + void Draw(_QUAD dstQuad, CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight); + void Draw(_QUAD dstQuad, CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, long alpha); + void Draw(long dstX, long dstY, long dstWidth, long dstHeight, CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight); + void Draw(long dstX, long dstY, long dstWidth, long dstHeight, CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, long alpha); + void DrawTransparent(long dstX, long dstY, long width, long height, CBitmapEx& bitmapEx, long srcX, long srcY, _PIXEL transparentColor=_RGB(0,0,0)); + void DrawTransparent(long dstX, long dstY, long width, long height, CBitmapEx& bitmapEx, long srcX, long srcY, long alpha, _PIXEL transparentColor=_RGB(0,0,0)); + void DrawTransparent(long dstX, long dstY, long dstWidth, long dstHeight, CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, _PIXEL transparentColor=_RGB(0,0,0)); + void DrawTransparent(long dstX, long dstY, long dstWidth, long dstHeight, CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, long alpha, _PIXEL transparentColor=_RGB(0,0,0)); + void DrawTransparent(_QUAD dstQuad, CBitmapEx& bitmapEx, _PIXEL transparentColor=_RGB(0,0,0)); + void DrawTransparent(_QUAD dstQuad, CBitmapEx& bitmapEx, long alpha, _PIXEL transparentColor=_RGB(0,0,0)); + void DrawTransparent(_QUAD dstQuad, CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, _PIXEL transparentColor=_RGB(0,0,0)); + void DrawTransparent(_QUAD dstQuad, CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, long alpha, _PIXEL transparentColor=_RGB(0,0,0)); + void DrawBlended(long dstX, long dstY, long width, long height, CBitmapEx& bitmapEx, long srcX, long srcY, long startAlpha, long endAlpha, DWORD mode=GM_NONE); + void DrawBlended(long dstX, long dstY, long dstWidth, long dstHeight, CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, long startAlpha, long endAlpha, DWORD mode=GM_NONE); + LPBITMAPFILEHEADER GetFileInfo() {return &m_bfh;} + LPBITMAPINFOHEADER GetInfo() {return &m_bih;} + long GetWidth() {return m_bih.biWidth;} + long GetHeight() {return m_bih.biHeight;} + long GetPitch() {return m_iPitch;} + long GetBpp() {return m_iBpp;} + long GetPaletteEntries() {return m_iPaletteEntries;} + LPRGBQUAD GetPalette() {return m_lpPalette;} + DWORD GetSize() {return m_dwSize;} + LPBYTE GetData() {return m_lpData;} + void SetResampleMode(_RESAMPLE_MODE mode=RM_NEARESTNEIGHBOUR) {m_ResampleMode = mode;} + _RESAMPLE_MODE GetResampleMode() {return m_ResampleMode;} + BOOL IsValid() {return (m_dwSize > 0);} + _PIXEL GetPixel(long x, long y); + void SetPixel(long x, long y, _PIXEL pixel); + + //wangjun + void LoadImageFile(LPTSTR lpszImageFile); + void SaveJPGFile(LPTSTR lpszImageFile); + +private: + // Private methods + void _ConvertTo32Bpp(); + void _ConvertTo24Bpp(); + void _ScaleNearestNeighbour(long horizontalPercent, long verticalPercent); + void _ScaleBilinear(long horizontalPercent, long verticalPercent); + void _ScaleBicubic(long horizontalPercent, long verticalPercent); + void _RotateNearestNeighbour(long degrees, _PIXEL bgColor); + void _RotateBilinear(long degrees, _PIXEL bgColor); + void _RotateBicubic(long degrees, _PIXEL bgColor); + void _DrawNearestNeighbour(long dstX, long dstY, long dstWidth, long dstHeight, CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight); + void _DrawBilinear(long dstX, long dstY, long dstWidth, long dstHeight, CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight); + void _DrawBicubic(long dstX, long dstY, long dstWidth, long dstHeight, CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight); + void _DrawNearestNeighbour(long dstX, long dstY, long dstWidth, long dstHeight, CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, long alpha); + void _DrawBilinear(long dstX, long dstY, long dstWidth, long dstHeight, CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, long alpha); + void _DrawBicubic(long dstX, long dstY, long dstWidth, long dstHeight, CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, long alpha); + void _DrawTransparentNearestNeighbour(long dstX, long dstY, long dstWidth, long dstHeight, CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, _PIXEL transparentColor=_RGB(0,0,0)); + void _DrawTransparentBilinear(long dstX, long dstY, long dstWidth, long dstHeight, CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, _PIXEL transparentColor=_RGB(0,0,0)); + void _DrawTransparentBicubic(long dstX, long dstY, long dstWidth, long dstHeight, CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, _PIXEL transparentColor=_RGB(0,0,0)); + void _DrawTransparentNearestNeighbour(long dstX, long dstY, long dstWidth, long dstHeight, CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, long alpha, _PIXEL transparentColor=_RGB(0,0,0)); + void _DrawTransparentBilinear(long dstX, long dstY, long dstWidth, long dstHeight, CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, long alpha, _PIXEL transparentColor=_RGB(0,0,0)); + void _DrawTransparentBicubic(long dstX, long dstY, long dstWidth, long dstHeight, CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, long alpha, _PIXEL transparentColor=_RGB(0,0,0)); + void _DrawBlendedNearestNeighbour(long dstX, long dstY, long dstWidth, long dstHeight, CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, long startAlpha, long endAlpha, DWORD mode=GM_NONE); + void _DrawBlendedBilinear(long dstX, long dstY, long dstWidth, long dstHeight, CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, long startAlpha, long endAlpha, DWORD mode=GM_NONE); + void _DrawBlendedBicubic(long dstX, long dstY, long dstWidth, long dstHeight, CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, long startAlpha, long endAlpha, DWORD mode=GM_NONE); + + // wanhjun + HANDLE _dibFromBitmap(HBITMAP hBitmap); //DDB->DIB + int _DIBNumColors (LPBITMAPINFOHEADER lpbi); + HBITMAP _extractBitmap(IPicture* pPicture); + int _GetCodecClsid(const WCHAR* format, CLSID* pClsid); + +private: + // Private members + BITMAPFILEHEADER m_bfh; + BITMAPINFOHEADER m_bih; + long m_iPaletteEntries; + RGBQUAD m_lpPalette[256]; + long m_iPitch; + long m_iBpp; + DWORD m_dwSize; + LPBYTE m_lpData; + _RESAMPLE_MODE m_ResampleMode; + +}; + +#endif // !defined(AFX_BITMAPEX_H__80F20A52_B43F_42C5_B182_AC8D27BF5C0E__INCLUDED_) diff --git a/inc/qedit.h b/inc/qedit.h new file mode 100644 index 0000000..a520288 --- /dev/null +++ b/inc/qedit.h @@ -0,0 +1,66 @@ + +#include +#include + +#pragma comment(lib, "strmiids.lib") + +#ifndef __qedit_h__ +#define __qedit_h__ + +/////////////////////////////////////////////////////////////////////////////////// + +#pragma once + +/////////////////////////////////////////////////////////////////////////////////// + +struct __declspec(uuid("0579154a-2b53-4994-b0d0-e773148eff85")) + ISampleGrabberCB : IUnknown +{ + // + // Raw methods provided by interface + // + + virtual HRESULT __stdcall SampleCB( + double SampleTime, + struct IMediaSample * pSample) = 0; + virtual HRESULT __stdcall BufferCB( + double SampleTime, + unsigned char * pBuffer, + long BufferLen) = 0; +}; + + + + +struct __declspec(uuid("6b652fff-11fe-4fce-92ad-0266b5d7c78f")) + ISampleGrabber : IUnknown +{ + // + // Raw methods provided by interface + // + + virtual HRESULT __stdcall SetOneShot( + long OneShot) = 0; + virtual HRESULT __stdcall SetMediaType( + struct _AMMediaType * pType) = 0; + virtual HRESULT __stdcall GetConnectedMediaType( + struct _AMMediaType * pType) = 0; + virtual HRESULT __stdcall SetBufferSamples( + long BufferThem) = 0; + virtual HRESULT __stdcall GetCurrentBuffer( + /*[in,out]*/ long * pBufferSize, + /*[out]*/ long * pBuffer) = 0; + virtual HRESULT __stdcall GetCurrentSample( + /*[out,retval]*/ struct IMediaSample * * ppSample) = 0; + virtual HRESULT __stdcall SetCallback( + struct ISampleGrabberCB * pCallback, + long WhichMethodToCallback) = 0; +}; + + +static const IID IID_ISampleGrabber = { 0x6B652FFF, 0x11FE, 0x4fce,{ 0x92, 0xAD, 0x02, 0x66, 0xB5, 0xD7, 0xC7, 0x8F } }; +static const IID IID_ISampleGrabberCB = { 0x0579154A, 0x2B53, 0x4994,{ 0xB0, 0xD0, 0xE7, 0x73, 0x14, 0x8E, 0xFF, 0x85 } }; +static const CLSID CLSID_SampleGrabber = { 0xC1F400A0, 0x3F08, 0x11d3,{ 0x9F, 0x0B, 0x00, 0x60, 0x08, 0x03, 0x9E, 0x37 } }; +static const CLSID CLSID_NullRenderer = { 0xC1F400A4, 0x3F08, 0x11d3,{ 0x9F, 0x0B, 0x00, 0x60, 0x08, 0x03, 0x9E, 0x37 } }; + +#endif \ No newline at end of file diff --git a/inc/utils.h b/inc/utils.h new file mode 100644 index 0000000..d6b42ba --- /dev/null +++ b/inc/utils.h @@ -0,0 +1,21 @@ +#pragma once +#include +#include +#include "guiddef.h" +#include +#include +#include "qedit.h" +extern "C" +{ +#include "libavcodec/avcodec.h" +#include "libavformat/avformat.h" +#include "libavutil/avutil.h" +#include "libswscale/swscale.h" +#include "libavutil/opt.h" +#include "libavutil/imgutils.h" +}; + +using namespace std; + + +AVPixelFormat GUIDToAvFormat(GUID mediatype); diff --git a/inc/winuuids.h b/inc/winuuids.h new file mode 100644 index 0000000..2582b98 --- /dev/null +++ b/inc/winuuids.h @@ -0,0 +1,1843 @@ +//------------------------------------------------------------------------------ +// File: uuids.h +// +// Desc: Contains the GUIDs for the MediaType type, subtype fields and format +// types for standard media types, and also class ids for well-known +// components. +// +// Copyright (c) 1992 - 2002, Microsoft Corporation. All rights reserved. +//------------------------------------------------------------------------------ + + +// +// We want to use this list for generating strings for debugging too +// so we redefine OUR_GUID_ENTRY depending on what we want to do +// +// It is imperative that all entries in this file are declared using +// OUR_GUID_ENTRY as that macro might have been defined in advance of +// including this file. See wxdebug.cpp in sdk\classes\base. +// + +#include + +#pragma region Desktop Family +#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) + +#define DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) EXTERN_C const GUID FAR name + +#ifndef OUR_GUID_ENTRY + #define OUR_GUID_ENTRY(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8); +#endif + + +// -- to allow consistent labeling of Media types and subtypes -- + +#define MEDIATYPE_NULL GUID_NULL +#define MEDIASUBTYPE_NULL GUID_NULL + +// -- Use this subtype if you don't have a use for a subtype for your type +// e436eb8e-524f-11ce-9f53-0020af0ba770 MEDIASUBTYPE_None +OUR_GUID_ENTRY(MEDIASUBTYPE_None, +0xe436eb8e, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + + +// -- major types --- + + +// 73646976-0000-0010-8000-00AA00389B71 'vids' == MEDIATYPE_Video +OUR_GUID_ENTRY(MEDIATYPE_Video, +0x73646976, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 73647561-0000-0010-8000-00AA00389B71 'auds' == MEDIATYPE_Audio +OUR_GUID_ENTRY(MEDIATYPE_Audio, +0x73647561, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 73747874-0000-0010-8000-00AA00389B71 'txts' == MEDIATYPE_Text +OUR_GUID_ENTRY(MEDIATYPE_Text, +0x73747874, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 7364696D-0000-0010-8000-00AA00389B71 'mids' == MEDIATYPE_Midi +OUR_GUID_ENTRY(MEDIATYPE_Midi, +0x7364696D, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// e436eb83-524f-11ce-9f53-0020af0ba770 MEDIATYPE_Stream +OUR_GUID_ENTRY(MEDIATYPE_Stream, +0xe436eb83, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// 73(s)76(v)61(a)69(i)-0000-0010-8000-00AA00389B71 'iavs' == MEDIATYPE_Interleaved +OUR_GUID_ENTRY(MEDIATYPE_Interleaved, +0x73766169, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 656c6966-0000-0010-8000-00AA00389B71 'file' == MEDIATYPE_File +OUR_GUID_ENTRY(MEDIATYPE_File, +0x656c6966, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 73636d64-0000-0010-8000-00AA00389B71 'scmd' == MEDIATYPE_ScriptCommand +OUR_GUID_ENTRY(MEDIATYPE_ScriptCommand, +0x73636d64, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 670AEA80-3A82-11d0-B79B-00AA003767A7 MEDIATYPE_AUXLine21Data +OUR_GUID_ENTRY(MEDIATYPE_AUXLine21Data, +0x670aea80, 0x3a82, 0x11d0, 0xb7, 0x9b, 0x0, 0xaa, 0x0, 0x37, 0x67, 0xa7) + +// {11264ACB-37DE-4eba-8C35-7F04A1A68332} +OUR_GUID_ENTRY(MEDIATYPE_AUXTeletextPage, +0x11264acb, 0x37de, 0x4eba, 0x8c, 0x35, 0x7f, 0x4, 0xa1, 0xa6, 0x83, 0x32) + +// AEB312E9-3357-43ca-B701-97EC198E2B62 MEDIATYPE_CC_CONTAINER +OUR_GUID_ENTRY(MEDIATYPE_CC_CONTAINER, +0xaeb312e9, 0x3357, 0x43ca, 0xb7, 0x1, 0x97, 0xec, 0x19, 0x8e, 0x2b, 0x62) + +// FB77E152-53B2-499c-B46B-509FC33EDFD7 MEDIATYPE_DTVCCData +OUR_GUID_ENTRY(MEDIATYPE_DTVCCData, +0xfb77e152, 0x53b2, 0x499c, 0xb4, 0x6b, 0x50, 0x9f, 0xc3, 0x3e, 0xdf, 0xd7) + +// B88B8A89-B049-4C80-ADCF-5898985E22C1 MEDIATYPE_MSTVCaption +OUR_GUID_ENTRY(MEDIATYPE_MSTVCaption, +0xB88B8A89, 0xB049, 0x4C80, 0xAD, 0xCF, 0x58, 0x98, 0x98, 0x5E, 0x22, 0xC1) + +// F72A76E1-EB0A-11D0-ACE4-0000C0CC16BA MEDIATYPE_VBI +OUR_GUID_ENTRY(MEDIATYPE_VBI, +0xf72a76e1, 0xeb0a, 0x11d0, 0xac, 0xe4, 0x00, 0x00, 0xc0, 0xcc, 0x16, 0xba) + +// 34FFCBC3-D5B3-4171-9002-D4C60301697F DVB_SUBTITLES +OUR_GUID_ENTRY(MEDIASUBTYPE_DVB_SUBTITLES, +0x34FFCBC3, 0xD5B3, 0x4171, 0x90, 0x02, 0xD4, 0xC6, 0x03, 0x01, 0x69, 0x7F) + +// 059DD67D-2E55-4d41-8D1B-01F5E4F50607 ISDB_CAPTIONS +OUR_GUID_ENTRY(MEDIASUBTYPE_ISDB_CAPTIONS, +0x059dd67d, 0x2e55, 0x4d41, 0x8d, 0x1b, 0x01, 0xf5, 0xe4, 0xf5, 0x06, 0x07) + +// 36dc6d28-f1a6-4216-9048-9cfcefeb5eba ISDB_SUPERIMPOSE +OUR_GUID_ENTRY(MEDIASUBTYPE_ISDB_SUPERIMPOSE, +0x36dc6d28, 0xf1a6, 0x4216, 0x90, 0x48, 0x9c, 0xfc, 0xef, 0xeb, 0x5e, 0xba) + +// 0482DEE3-7817-11cf-8a03-00aa006ecb65 MEDIATYPE_Timecode +OUR_GUID_ENTRY(MEDIATYPE_Timecode, +0x482dee3, 0x7817, 0x11cf, 0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65) + +// 74726c6d-0000-0010-8000-00AA00389B71 'lmrt' == MEDIATYPE_LMRT +OUR_GUID_ENTRY(MEDIATYPE_LMRT, +0x74726c6d, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 74726c6d-0000-0010-8000-00AA00389B71 'urls' == MEDIATYPE_URL_STREAM +OUR_GUID_ENTRY(MEDIATYPE_URL_STREAM, +0x736c7275, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// -- sub types --- + +// 4C504C43-0000-0010-8000-00AA00389B71 'CLPL' == MEDIASUBTYPE_CLPL +OUR_GUID_ENTRY(MEDIASUBTYPE_CLPL, +0x4C504C43, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 56595559-0000-0010-8000-00AA00389B71 'YUYV' == MEDIASUBTYPE_YUYV +OUR_GUID_ENTRY(MEDIASUBTYPE_YUYV, +0x56595559, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 56555949-0000-0010-8000-00AA00389B71 'IYUV' == MEDIASUBTYPE_IYUV +OUR_GUID_ENTRY(MEDIASUBTYPE_IYUV, +0x56555949, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 39555659-0000-0010-8000-00AA00389B71 'YVU9' == MEDIASUBTYPE_YVU9 +OUR_GUID_ENTRY(MEDIASUBTYPE_YVU9, +0x39555659, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 31313459-0000-0010-8000-00AA00389B71 'Y411' == MEDIASUBTYPE_Y411 +OUR_GUID_ENTRY(MEDIASUBTYPE_Y411, +0x31313459, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 50313459-0000-0010-8000-00AA00389B71 'Y41P' == MEDIASUBTYPE_Y41P +OUR_GUID_ENTRY(MEDIASUBTYPE_Y41P, +0x50313459, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 32595559-0000-0010-8000-00AA00389B71 'YUY2' == MEDIASUBTYPE_YUY2 +OUR_GUID_ENTRY(MEDIASUBTYPE_YUY2, +0x32595559, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 55595659-0000-0010-8000-00AA00389B71 'YVYU' == MEDIASUBTYPE_YVYU +OUR_GUID_ENTRY(MEDIASUBTYPE_YVYU, +0x55595659, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 59565955-0000-0010-8000-00AA00389B71 'UYVY' == MEDIASUBTYPE_UYVY +OUR_GUID_ENTRY(MEDIASUBTYPE_UYVY, +0x59565955, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 31313259-0000-0010-8000-00AA00389B71 'Y211' == MEDIASUBTYPE_Y211 +OUR_GUID_ENTRY(MEDIASUBTYPE_Y211, +0x31313259, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 524a4c43-0000-0010-8000-00AA00389B71 'CLJR' == MEDIASUBTYPE_CLJR +OUR_GUID_ENTRY(MEDIASUBTYPE_CLJR, +0x524a4c43, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 39304649-0000-0010-8000-00AA00389B71 'IF09' == MEDIASUBTYPE_IF09 +OUR_GUID_ENTRY(MEDIASUBTYPE_IF09, +0x39304649, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 414c5043-0000-0010-8000-00AA00389B71 'CPLA' == MEDIASUBTYPE_CPLA +OUR_GUID_ENTRY(MEDIASUBTYPE_CPLA, +0x414c5043, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 47504A4D-0000-0010-8000-00AA00389B71 MEDIASUBTYPE_MJPG +OUR_GUID_ENTRY(MEDIASUBTYPE_MJPG, +0x47504A4D, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 4A4D5654-0000-0010-8000-00AA00389B71 MEDIASUBTYPE_TVMJ +OUR_GUID_ENTRY(MEDIASUBTYPE_TVMJ, +0x4A4D5654, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 454B4157-0000-0010-8000-00AA00389B71 MEDIASUBTYPE_WAKE +OUR_GUID_ENTRY(MEDIASUBTYPE_WAKE, +0x454B4157, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 43434643-0000-0010-8000-00AA00389B71 MEDIASUBTYPE_CFCC +OUR_GUID_ENTRY(MEDIASUBTYPE_CFCC, +0x43434643, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 47504A49-0000-0010-8000-00AA00389B71 MEDIASUBTYPE_IJPG +OUR_GUID_ENTRY(MEDIASUBTYPE_IJPG, +0x47504A49, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 6D756C50-0000-0010-8000-00AA00389B71 MEDIASUBTYPE_Plum +OUR_GUID_ENTRY(MEDIASUBTYPE_Plum, +0x6D756C50, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// FAST DV-Master +// 53435644-0000-0010-8000-00AA00389B71 MEDIASUBTYPE_DVCS +OUR_GUID_ENTRY(MEDIASUBTYPE_DVCS, +0x53435644, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// H.264 compressed video stream +// 34363248-0000-0010-8000-00AA00389B71 'H264' == MEDIASUBTYPE_H264 +OUR_GUID_ENTRY(MEDIASUBTYPE_H264, +0x34363248, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// FAST DV-Master +// 44535644-0000-0010-8000-00AA00389B71 MEDIASUBTYPE_DVSD +OUR_GUID_ENTRY(MEDIASUBTYPE_DVSD, +0x44535644, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// MIROVideo DV +// 4656444D-0000-0010-8000-00AA00389B71 MEDIASUBTYPE_MDVF +OUR_GUID_ENTRY(MEDIASUBTYPE_MDVF, +0x4656444D, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// e436eb78-524f-11ce-9f53-0020af0ba770 MEDIASUBTYPE_RGB1 +// e436eb78-524f-11ce-9f53-0020af0ba770 MEDIASUBTYPE_RGB1 +OUR_GUID_ENTRY(MEDIASUBTYPE_RGB1, +0xe436eb78, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// e436eb79-524f-11ce-9f53-0020af0ba770 MEDIASUBTYPE_RGB4 +OUR_GUID_ENTRY(MEDIASUBTYPE_RGB4, +0xe436eb79, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// e436eb7a-524f-11ce-9f53-0020af0ba770 MEDIASUBTYPE_RGB8 +OUR_GUID_ENTRY(MEDIASUBTYPE_RGB8, +0xe436eb7a, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// e436eb7b-524f-11ce-9f53-0020af0ba770 MEDIASUBTYPE_RGB565 +OUR_GUID_ENTRY(MEDIASUBTYPE_RGB565, +0xe436eb7b, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// e436eb7c-524f-11ce-9f53-0020af0ba770 MEDIASUBTYPE_RGB555 +OUR_GUID_ENTRY(MEDIASUBTYPE_RGB555, +0xe436eb7c, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// e436eb7d-524f-11ce-9f53-0020af0ba770 MEDIASUBTYPE_RGB24 +OUR_GUID_ENTRY(MEDIASUBTYPE_RGB24, +0xe436eb7d, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// e436eb7e-524f-11ce-9f53-0020af0ba770 MEDIASUBTYPE_RGB32 +OUR_GUID_ENTRY(MEDIASUBTYPE_RGB32, +0xe436eb7e, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + + +// +// RGB surfaces that contain per pixel alpha values. +// + +// 297C55AF-E209-4cb3-B757-C76D6B9C88A8 MEDIASUBTYPE_ARGB1555 +OUR_GUID_ENTRY(MEDIASUBTYPE_ARGB1555, +0x297c55af, 0xe209, 0x4cb3, 0xb7, 0x57, 0xc7, 0x6d, 0x6b, 0x9c, 0x88, 0xa8) + +// 6E6415E6-5C24-425f-93CD-80102B3D1CCA MEDIASUBTYPE_ARGB4444 +OUR_GUID_ENTRY(MEDIASUBTYPE_ARGB4444, +0x6e6415e6, 0x5c24, 0x425f, 0x93, 0xcd, 0x80, 0x10, 0x2b, 0x3d, 0x1c, 0xca) + +// 773c9ac0-3274-11d0-B724-00aa006c1A01 MEDIASUBTYPE_ARGB32 +OUR_GUID_ENTRY(MEDIASUBTYPE_ARGB32, +0x773c9ac0, 0x3274, 0x11d0, 0xb7, 0x24, 0x0, 0xaa, 0x0, 0x6c, 0x1a, 0x1 ) + + +// 2f8bb76d-b644-4550-acf3-d30caa65d5c5 MEDIASUBTYPE_A2R10G10B10 +OUR_GUID_ENTRY(MEDIASUBTYPE_A2R10G10B10, +0x2f8bb76d, 0xb644, 0x4550, 0xac, 0xf3, 0xd3, 0x0c, 0xaa, 0x65, 0xd5, 0xc5) + +// 576f7893-bdf6-48c4-875f-ae7b81834567 MEDIASUBTYPE_A2B10G10R10 +OUR_GUID_ENTRY(MEDIASUBTYPE_A2B10G10R10, +0x576f7893, 0xbdf6, 0x48c4, 0x87, 0x5f, 0xae, 0x7b, 0x81, 0x83, 0x45, 0x67) + + +// 56555941-0000-0010-8000-00AA00389B71 'AYUV' == MEDIASUBTYPE_AYUV +// +// See the DX-VA header and documentation for a description of this format. +// +OUR_GUID_ENTRY(MEDIASUBTYPE_AYUV, +0x56555941, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 34344941-0000-0010-8000-00AA00389B71 'AI44' == MEDIASUBTYPE_AI44 +// +// See the DX-VA header and documentation for a description of this format. +// +OUR_GUID_ENTRY(MEDIASUBTYPE_AI44, +0x34344941, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 34344149-0000-0010-8000-00AA00389B71 'IA44' == MEDIASUBTYPE_IA44 +// +// See the DX-VA header and documentation for a description of this format. +// +OUR_GUID_ENTRY(MEDIASUBTYPE_IA44, +0x34344149, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + + +// +// DirectX7 D3D Render Target media subtypes. +// + +// 32335237-0000-0010-8000-00AA00389B71 '7R32' == MEDIASUBTYPE_RGB32_D3D_DX7_RT +OUR_GUID_ENTRY(MEDIASUBTYPE_RGB32_D3D_DX7_RT, +0x32335237, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 36315237-0000-0010-8000-00AA00389B71 '7R16' == MEDIASUBTYPE_RGB16_D3D_DX7_RT +OUR_GUID_ENTRY(MEDIASUBTYPE_RGB16_D3D_DX7_RT, +0x36315237, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 38384137-0000-0010-8000-00AA00389B71 '7A88' == MEDIASUBTYPE_ARGB32_D3D_DX7_RT +OUR_GUID_ENTRY(MEDIASUBTYPE_ARGB32_D3D_DX7_RT, +0x38384137, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 34344137-0000-0010-8000-00AA00389B71 '7A44' == MEDIASUBTYPE_ARGB4444_D3D_DX7_RT +OUR_GUID_ENTRY(MEDIASUBTYPE_ARGB4444_D3D_DX7_RT, +0x34344137, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 35314137-0000-0010-8000-00AA00389B71 '7A15' == MEDIASUBTYPE_ARGB1555_D3D_DX7_RT +OUR_GUID_ENTRY(MEDIASUBTYPE_ARGB1555_D3D_DX7_RT, +0x35314137, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + + +// +// DirectX9 D3D Render Target media subtypes. +// + +// 32335239-0000-0010-8000-00AA00389B71 '9R32' == MEDIASUBTYPE_RGB32_D3D_DX9_RT +OUR_GUID_ENTRY(MEDIASUBTYPE_RGB32_D3D_DX9_RT, +0x32335239, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 36315239-0000-0010-8000-00AA00389B71 '9R16' == MEDIASUBTYPE_RGB16_D3D_DX9_RT +OUR_GUID_ENTRY(MEDIASUBTYPE_RGB16_D3D_DX9_RT, +0x36315239, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 38384139-0000-0010-8000-00AA00389B71 '9A88' == MEDIASUBTYPE_ARGB32_D3D_DX9_RT +OUR_GUID_ENTRY(MEDIASUBTYPE_ARGB32_D3D_DX9_RT, +0x38384139, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 34344139-0000-0010-8000-00AA00389B71 '9A44' == MEDIASUBTYPE_ARGB4444_D3D_DX9_RT +OUR_GUID_ENTRY(MEDIASUBTYPE_ARGB4444_D3D_DX9_RT, +0x34344139, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 35314139-0000-0010-8000-00AA00389B71 '9A15' == MEDIASUBTYPE_ARGB1555_D3D_DX9_RT +OUR_GUID_ENTRY(MEDIASUBTYPE_ARGB1555_D3D_DX9_RT, +0x35314139, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + + +#define MEDIASUBTYPE_HASALPHA(mt) ( ((mt).subtype == MEDIASUBTYPE_ARGB4444) || \ + ((mt).subtype == MEDIASUBTYPE_ARGB32) || \ + ((mt).subtype == MEDIASUBTYPE_AYUV) || \ + ((mt).subtype == MEDIASUBTYPE_AI44) || \ + ((mt).subtype == MEDIASUBTYPE_IA44) || \ + ((mt).subtype == MEDIASUBTYPE_ARGB1555) || \ + ((mt).subtype == MEDIASUBTYPE_ARGB32_D3D_DX7_RT) || \ + ((mt).subtype == MEDIASUBTYPE_ARGB4444_D3D_DX7_RT) || \ + ((mt).subtype == MEDIASUBTYPE_ARGB1555_D3D_DX7_RT) || \ + ((mt).subtype == MEDIASUBTYPE_ARGB32_D3D_DX9_RT) || \ + ((mt).subtype == MEDIASUBTYPE_ARGB4444_D3D_DX9_RT) || \ + ((mt).subtype == MEDIASUBTYPE_ARGB1555_D3D_DX9_RT) ) + +#define MEDIASUBTYPE_HASALPHA7(mt) (((mt).subtype == MEDIASUBTYPE_ARGB32_D3D_DX7_RT) || \ + ((mt).subtype == MEDIASUBTYPE_ARGB4444_D3D_DX7_RT) || \ + ((mt).subtype == MEDIASUBTYPE_ARGB1555_D3D_DX7_RT) ) + +#define MEDIASUBTYPE_D3D_DX7_RT(mt) (((mt).subtype == MEDIASUBTYPE_ARGB32_D3D_DX7_RT) || \ + ((mt).subtype == MEDIASUBTYPE_ARGB4444_D3D_DX7_RT) || \ + ((mt).subtype == MEDIASUBTYPE_ARGB1555_D3D_DX7_RT) || \ + ((mt).subtype == MEDIASUBTYPE_RGB32_D3D_DX7_RT) || \ + ((mt).subtype == MEDIASUBTYPE_RGB16_D3D_DX7_RT)) + +#define MEDIASUBTYPE_HASALPHA9(mt) (((mt).subtype == MEDIASUBTYPE_ARGB32_D3D_DX9_RT) || \ + ((mt).subtype == MEDIASUBTYPE_ARGB4444_D3D_DX9_RT) || \ + ((mt).subtype == MEDIASUBTYPE_ARGB1555_D3D_DX9_RT) ) + + +#define MEDIASUBTYPE_D3D_DX9_RT(mt) (((mt).subtype == MEDIASUBTYPE_ARGB32_D3D_DX9_RT) || \ + ((mt).subtype == MEDIASUBTYPE_ARGB4444_D3D_DX9_RT) || \ + ((mt).subtype == MEDIASUBTYPE_ARGB1555_D3D_DX9_RT) || \ + ((mt).subtype == MEDIASUBTYPE_RGB32_D3D_DX9_RT) || \ + ((mt).subtype == MEDIASUBTYPE_RGB16_D3D_DX9_RT)) + + +// +// DX-VA uncompressed surface formats +// + +// 32315659-0000-0010-8000-00AA00389B71 'YV12' == MEDIASUBTYPE_YV12 +OUR_GUID_ENTRY(MEDIASUBTYPE_YV12, +0x32315659, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 3231564E-0000-0010-8000-00AA00389B71 'NV12' == MEDIASUBTYPE_NV12 +OUR_GUID_ENTRY(MEDIASUBTYPE_NV12, +0x3231564E, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 3131564E-0000-0010-8000-00AA00389B71 'NV11' == MEDIASUBTYPE_NV11 +#ifndef MEDIASUBTYPE_NV11_DEFINED +#define MEDIASUBTYPE_NV11_DEFINED +OUR_GUID_ENTRY(MEDIASUBTYPE_NV11, +0x3131564E, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) +#endif + +// 38303250-0000-0010-8000-00AA00389B71 'P208' == MEDIASUBTYPE_P208 +OUR_GUID_ENTRY(MEDIASUBTYPE_P208, +'802P', 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 38303250-0000-0010-8000-00AA00389B71 'P210' == MEDIASUBTYPE_P210 +OUR_GUID_ENTRY(MEDIASUBTYPE_P210, +'012P', 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 38303250-0000-0010-8000-00AA00389B71 'P216' == MEDIASUBTYPE_P216 +OUR_GUID_ENTRY(MEDIASUBTYPE_P216, +'612P', 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 38303250-0000-0010-8000-00AA00389B71 'P010' == MEDIASUBTYPE_P010 +OUR_GUID_ENTRY(MEDIASUBTYPE_P010, +'010P', 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 38303250-0000-0010-8000-00AA00389B71 'P016' == MEDIASUBTYPE_P016 +OUR_GUID_ENTRY(MEDIASUBTYPE_P016, +'610P', 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 38303250-0000-0010-8000-00AA00389B71 'Y210' == MEDIASUBTYPE_Y210 +OUR_GUID_ENTRY(MEDIASUBTYPE_Y210, +'012Y', 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 38303250-0000-0010-8000-00AA00389B71 'Y216' == MEDIASUBTYPE_Y216 +OUR_GUID_ENTRY(MEDIASUBTYPE_Y216, +'612Y', 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 38303450-0000-0010-8000-00AA00389B71 'P408' == MEDIASUBTYPE_P408 +OUR_GUID_ENTRY(MEDIASUBTYPE_P408, +'804P', 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 3432564E-0000-0010-8000-00AA00389B71 'NV24' == MEDIASUBTYPE_NV24 +OUR_GUID_ENTRY(MEDIASUBTYPE_NV24, +0x3432564E, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 4F303234-0000-0010-8000-00AA00389B71 '420O' == MEDIASUBTYPE_420O +OUR_GUID_ENTRY(MEDIASUBTYPE_420O, +0x4F303234, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 31434D49-0000-0010-8000-00AA00389B71 'IMC1' == MEDIASUBTYPE_IMC1 +OUR_GUID_ENTRY(MEDIASUBTYPE_IMC1, +0x31434D49, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 32434d49-0000-0010-8000-00AA00389B71 'IMC2' == MEDIASUBTYPE_IMC2 +OUR_GUID_ENTRY(MEDIASUBTYPE_IMC2, +0x32434D49, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 33434d49-0000-0010-8000-00AA00389B71 'IMC3' == MEDIASUBTYPE_IMC3 +OUR_GUID_ENTRY(MEDIASUBTYPE_IMC3, +0x33434D49, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 34434d49-0000-0010-8000-00AA00389B71 'IMC4' == MEDIASUBTYPE_IMC4 +OUR_GUID_ENTRY(MEDIASUBTYPE_IMC4, +0x34434D49, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 30343353-0000-0010-8000-00AA00389B71 'S340' == MEDIASUBTYPE_S340 +OUR_GUID_ENTRY(MEDIASUBTYPE_S340, +0x30343353, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 32343353-0000-0010-8000-00AA00389B71 'S342' == MEDIASUBTYPE_S342 +OUR_GUID_ENTRY(MEDIASUBTYPE_S342, +0x32343353, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + + +// e436eb7f-524f-11ce-9f53-0020af0ba770 MEDIASUBTYPE_Overlay +OUR_GUID_ENTRY(MEDIASUBTYPE_Overlay, +0xe436eb7f, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// e436eb80-524f-11ce-9f53-0020af0ba770 MEDIASUBTYPE_MPEGPacket +OUR_GUID_ENTRY(MEDIASUBTYPE_MPEG1Packet, +0xe436eb80, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// e436eb81-524f-11ce-9f53-0020af0ba770 MEDIASUBTYPE_MPEG1Payload +OUR_GUID_ENTRY(MEDIASUBTYPE_MPEG1Payload, +0xe436eb81, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// 00000050-0000-0010-8000-00AA00389B71 MEDIASUBTYPE_MPEG1AudioPayload +OUR_GUID_ENTRY(MEDIASUBTYPE_MPEG1AudioPayload, +0x00000050, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71) + +// e436eb82-524f-11ce-9f53-0020af0ba770 MEDIASUBTYPE_MPEG1SystemStream +OUR_GUID_ENTRY(MEDIATYPE_MPEG1SystemStream, +0xe436eb82, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// the next consecutive number is assigned to MEDIATYPE_Stream and appears higher up +// e436eb84-524f-11ce-9f53-0020af0ba770 MEDIASUBTYPE_MPEG1System +OUR_GUID_ENTRY(MEDIASUBTYPE_MPEG1System, +0xe436eb84, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// e436eb85-524f-11ce-9f53-0020af0ba770 MEDIASUBTYPE_MPEG1VideoCD +OUR_GUID_ENTRY(MEDIASUBTYPE_MPEG1VideoCD, +0xe436eb85, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// e436eb86-524f-11ce-9f53-0020af0ba770 MEDIASUBTYPE_MPEG1Video +OUR_GUID_ENTRY(MEDIASUBTYPE_MPEG1Video, +0xe436eb86, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// e436eb87-524f-11ce-9f53-0020af0ba770 MEDIASUBTYPE_MPEG1Audio +OUR_GUID_ENTRY(MEDIASUBTYPE_MPEG1Audio, +0xe436eb87, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// e436eb88-524f-11ce-9f53-0020af0ba770 MEDIASUBTYPE_Avi +OUR_GUID_ENTRY(MEDIASUBTYPE_Avi, +0xe436eb88, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// {3DB80F90-9412-11d1-ADED-0000F8754B99} MEDIASUBTYPE_Asf +OUR_GUID_ENTRY(MEDIASUBTYPE_Asf, +0x3db80f90, 0x9412, 0x11d1, 0xad, 0xed, 0x0, 0x0, 0xf8, 0x75, 0x4b, 0x99) + +// e436eb89-524f-11ce-9f53-0020af0ba770 MEDIASUBTYPE_QTMovie +OUR_GUID_ENTRY(MEDIASUBTYPE_QTMovie, +0xe436eb89, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// 617a7072-0000-0010-8000-00AA00389B71 MEDIASUBTYPE_Rpza +OUR_GUID_ENTRY(MEDIASUBTYPE_QTRpza, +0x617a7072, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 20636d73-0000-0010-8000-00AA00389B71 MEDIASUBTYPE_Smc +OUR_GUID_ENTRY(MEDIASUBTYPE_QTSmc, +0x20636d73, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 20656c72-0000-0010-8000-00AA00389B71 MEDIASUBTYPE_Rle +OUR_GUID_ENTRY(MEDIASUBTYPE_QTRle, +0x20656c72, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 6765706a-0000-0010-8000-00AA00389B71 MEDIASUBTYPE_Jpeg +OUR_GUID_ENTRY(MEDIASUBTYPE_QTJpeg, +0x6765706a, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// e436eb8a-524f-11ce-9f53-0020af0ba770 MEDIASUBTYPE_PCMAudio_Obsolete +OUR_GUID_ENTRY(MEDIASUBTYPE_PCMAudio_Obsolete, +0xe436eb8a, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// 00000001-0000-0010-8000-00AA00389B71 MEDIASUBTYPE_PCM +OUR_GUID_ENTRY(MEDIASUBTYPE_PCM, +0x00000001, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71) + +// e436eb8b-524f-11ce-9f53-0020af0ba770 MEDIASUBTYPE_WAVE +OUR_GUID_ENTRY(MEDIASUBTYPE_WAVE, +0xe436eb8b, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// e436eb8c-524f-11ce-9f53-0020af0ba770 MEDIASUBTYPE_AU +OUR_GUID_ENTRY(MEDIASUBTYPE_AU, +0xe436eb8c, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// e436eb8d-524f-11ce-9f53-0020af0ba770 MEDIASUBTYPE_AIFF +OUR_GUID_ENTRY(MEDIASUBTYPE_AIFF, +0xe436eb8d, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// 64(d)73(s)76(v)64(d)-0000-0010-8000-00AA00389B71 'dvsd' == MEDIASUBTYPE_dvsd +OUR_GUID_ENTRY(MEDIASUBTYPE_dvsd, +0x64737664, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 64(d)68(h)76(v)64(d)-0000-0010-8000-00AA00389B71 'dvhd' == MEDIASUBTYPE_dvhd +OUR_GUID_ENTRY(MEDIASUBTYPE_dvhd, +0x64687664, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 6c(l)73(s)76(v)64(d)-0000-0010-8000-00AA00389B71 'dvsl' == MEDIASUBTYPE_dvsl +OUR_GUID_ENTRY(MEDIASUBTYPE_dvsl, +0x6c737664, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 35(5)32(2)76(v)64(d)-0000-0010-8000-00AA00389B71 'dv25' == MEDIASUBTYPE_dv25 +OUR_GUID_ENTRY(MEDIASUBTYPE_dv25, +0x35327664, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 30(0)35(5)76(v)64(d)-0000-0010-8000-00AA00389B71 'dv50' == MEDIASUBTYPE_dv50 +OUR_GUID_ENTRY(MEDIASUBTYPE_dv50, +0x30357664, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 31(1)68(h)76(v)64(d)-0000-0010-8000-00AA00389B71 'dvh1' == MEDIASUBTYPE_dvh1 +OUR_GUID_ENTRY(MEDIASUBTYPE_dvh1, +0x31687664, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// 6E8D4A22-310C-11d0-B79A-00AA003767A7 MEDIASUBTYPE_Line21_BytePair +OUR_GUID_ENTRY(MEDIASUBTYPE_Line21_BytePair, +0x6e8d4a22, 0x310c, 0x11d0, 0xb7, 0x9a, 0x0, 0xaa, 0x0, 0x37, 0x67, 0xa7) + +// 6E8D4A23-310C-11d0-B79A-00AA003767A7 MEDIASUBTYPE_Line21_GOPPacket +OUR_GUID_ENTRY(MEDIASUBTYPE_Line21_GOPPacket, +0x6e8d4a23, 0x310c, 0x11d0, 0xb7, 0x9a, 0x0, 0xaa, 0x0, 0x37, 0x67, 0xa7) + +// 6E8D4A24-310C-11d0-B79A-00AA003767A7 MEDIASUBTYPE_Line21_VBIRawData +OUR_GUID_ENTRY(MEDIASUBTYPE_Line21_VBIRawData, +0x6e8d4a24, 0x310c, 0x11d0, 0xb7, 0x9a, 0x0, 0xaa, 0x0, 0x37, 0x67, 0xa7) + +//0AF414BC-4ED2-445e-9839-8F095568AB3C MEDIASUBTYPE_708_608Data +OUR_GUID_ENTRY(MEDIASUBTYPE_708_608Data, +0xaf414bc, 0x4ed2, 0x445e, 0x98, 0x39, 0x8f, 0x9, 0x55, 0x68, 0xab, 0x3c) + +// F52ADDAA-36F0-43F5-95EA-6D866484262A MEDIASUBTYPE_DtvCcData +OUR_GUID_ENTRY(MEDIASUBTYPE_DtvCcData, +0xF52ADDAA, 0x36F0, 0x43F5, 0x95, 0xEA, 0x6D, 0x86, 0x64, 0x84, 0x26, 0x2A) + +// 7EA626DB-54DA-437b-BE9F-F73073ADFA3C MEDIASUBTYPE_CC_CONTAINER +OUR_GUID_ENTRY(MEDIASUBTYPE_CC_CONTAINER, +0x7ea626db, 0x54da, 0x437b, 0xbe, 0x9f, 0xf7, 0x30, 0x73, 0xad, 0xfa, 0x3c) + +// F72A76E3-EB0A-11D0-ACE4-0000C0CC16BA MEDIASUBTYPE_TELETEXT +OUR_GUID_ENTRY(MEDIASUBTYPE_TELETEXT, +0xf72a76e3, 0xeb0a, 0x11d0, 0xac, 0xe4, 0x00, 0x00, 0xc0, 0xcc, 0x16, 0xba) + +// 663DA43C-03E8-4e9a-9CD5-BF11ED0DEF76 MEDIASUBTYPE_VBI +OUR_GUID_ENTRY(MEDIASUBTYPE_VBI, +0x663da43c, 0x3e8, 0x4e9a, 0x9c, 0xd5, 0xbf, 0x11, 0xed, 0xd, 0xef, 0x76) + +// 2791D576-8E7A-466F-9E90-5D3F3083738B MEDIASUBTYPE_WSS +OUR_GUID_ENTRY(MEDIASUBTYPE_WSS, +0x2791D576, 0x8E7A, 0x466F, 0x9E, 0x90, 0x5D, 0x3F, 0x30, 0x83, 0x73, 0x8B) + +// 01CA73E3-DCE6-4575-AFE1-2BF1C902CAF3 MEDIASUBTYPE_XDS +OUR_GUID_ENTRY(MEDIASUBTYPE_XDS, +0x1ca73e3, 0xdce6, 0x4575, 0xaf, 0xe1, 0x2b, 0xf1, 0xc9, 0x2, 0xca, 0xf3) + +// A1B3F620-9792-4d8d-81A4-86AF25772090 MEDIASUBTYPE_VPS +OUR_GUID_ENTRY(MEDIASUBTYPE_VPS, +0xa1b3f620, 0x9792, 0x4d8d, 0x81, 0xa4, 0x86, 0xaf, 0x25, 0x77, 0x20, 0x90) + +// derived from WAVE_FORMAT_DRM +// 00000009-0000-0010-8000-00aa00389b71 +OUR_GUID_ENTRY(MEDIASUBTYPE_DRM_Audio, +0x00000009, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// derived from WAVE_FORMAT_IEEE_FLOAT +// 00000003-0000-0010-8000-00aa00389b71 +OUR_GUID_ENTRY(MEDIASUBTYPE_IEEE_FLOAT, +0x00000003, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// derived from WAVE_FORMAT_DOLBY_AC3_SPDIF +// 00000092-0000-0010-8000-00aa00389b71 +OUR_GUID_ENTRY(MEDIASUBTYPE_DOLBY_AC3_SPDIF, +0x00000092, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// derived from WAVE_FORMAT_RAW_SPORT +// 00000240-0000-0010-8000-00aa00389b71 +OUR_GUID_ENTRY(MEDIASUBTYPE_RAW_SPORT, +0x00000240, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + +// derived from wave format tag 0x241, call it SPDIF_TAG_241h for now +// 00000241-0000-0010-8000-00aa00389b71 +OUR_GUID_ENTRY(MEDIASUBTYPE_SPDIF_TAG_241h, +0x00000241, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71) + + + +// DirectShow DSS definitions + +// A0AF4F81-E163-11d0-BAD9-00609744111A +OUR_GUID_ENTRY(MEDIASUBTYPE_DssVideo, +0xa0af4f81, 0xe163, 0x11d0, 0xba, 0xd9, 0x0, 0x60, 0x97, 0x44, 0x11, 0x1a) + +// A0AF4F82-E163-11d0-BAD9-00609744111A +OUR_GUID_ENTRY(MEDIASUBTYPE_DssAudio, +0xa0af4f82, 0xe163, 0x11d0, 0xba, 0xd9, 0x0, 0x60, 0x97, 0x44, 0x11, 0x1a) + +// 5A9B6A40-1A22-11D1-BAD9-00609744111A +OUR_GUID_ENTRY(MEDIASUBTYPE_VPVideo, +0x5a9b6a40, 0x1a22, 0x11d1, 0xba, 0xd9, 0x0, 0x60, 0x97, 0x44, 0x11, 0x1a) + +// 5A9B6A41-1A22-11D1-BAD9-00609744111A +OUR_GUID_ENTRY(MEDIASUBTYPE_VPVBI, +0x5a9b6a41, 0x1a22, 0x11d1, 0xba, 0xd9, 0x0, 0x60, 0x97, 0x44, 0x11, 0x1a) + +// BF87B6E0-8C27-11d0-B3F0-00AA003761C5 Capture graph building +OUR_GUID_ENTRY(CLSID_CaptureGraphBuilder, +0xBF87B6E0, 0x8C27, 0x11d0, 0xB3, 0xF0, 0x0, 0xAA, 0x00, 0x37, 0x61, 0xC5) + +// BF87B6E1-8C27-11d0-B3F0-00AA003761C5 New Capture graph building +OUR_GUID_ENTRY(CLSID_CaptureGraphBuilder2, +0xBF87B6E1, 0x8C27, 0x11d0, 0xB3, 0xF0, 0x0, 0xAA, 0x00, 0x37, 0x61, 0xC5) + +// e436ebb0-524f-11ce-9f53-0020af0ba770 Prototype filtergraph +OUR_GUID_ENTRY(CLSID_ProtoFilterGraph, +0xe436ebb0, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// e436ebb1-524f-11ce-9f53-0020af0ba770 Reference clock +OUR_GUID_ENTRY(CLSID_SystemClock, +0xe436ebb1, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// e436ebb2-524f-11ce-9f53-0020af0ba770 Filter Mapper +OUR_GUID_ENTRY(CLSID_FilterMapper, +0xe436ebb2, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// e436ebb3-524f-11ce-9f53-0020af0ba770 Filter Graph +OUR_GUID_ENTRY(CLSID_FilterGraph, +0xe436ebb3, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// e436ebb8-524f-11ce-9f53-0020af0ba770 Filter Graph no thread +OUR_GUID_ENTRY(CLSID_FilterGraphNoThread, +0xe436ebb8, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// a3ecbc41-581a-4476-b693-a63340462d8b +OUR_GUID_ENTRY(CLSID_FilterGraphPrivateThread, +0xa3ecbc41, 0x581a, 0x4476, 0xb6, 0x93, 0xa6, 0x33, 0x40, 0x46, 0x2d, 0x8b) + +// e4bbd160-4269-11ce-838d-00aa0055595a MPEG System stream +OUR_GUID_ENTRY(CLSID_MPEG1Doc, +0xe4bbd160, 0x4269, 0x11ce, 0x83, 0x8d, 0x0, 0xaa, 0x0, 0x55, 0x59, 0x5a) + +// 701722e0-8ae3-11ce-a85c-00aa002feab5 MPEG file reader +OUR_GUID_ENTRY(CLSID_FileSource, +0x701722e0, 0x8ae3, 0x11ce, 0xa8, 0x5c, 0x00, 0xaa, 0x00, 0x2f, 0xea, 0xb5) + +// 26C25940-4CA9-11ce-A828-00AA002FEAB5 Takes MPEG1 packets as input +OUR_GUID_ENTRY(CLSID_MPEG1PacketPlayer, +0x26c25940, 0x4ca9, 0x11ce, 0xa8, 0x28, 0x0, 0xaa, 0x0, 0x2f, 0xea, 0xb5) + +// 336475d0-942a-11ce-a870-00aa002feab5 MPEG splitter +OUR_GUID_ENTRY(CLSID_MPEG1Splitter, +0x336475d0, 0x942a, 0x11ce, 0xa8, 0x70, 0x00, 0xaa, 0x00, 0x2f, 0xea, 0xb5) + +// feb50740-7bef-11ce-9bd9-0000e202599c MPEG video decoder +OUR_GUID_ENTRY(CLSID_CMpegVideoCodec, +0xfeb50740, 0x7bef, 0x11ce, 0x9b, 0xd9, 0x0, 0x0, 0xe2, 0x2, 0x59, 0x9c) + +// 4a2286e0-7bef-11ce-9bd9-0000e202599c MPEG audio decoder +OUR_GUID_ENTRY(CLSID_CMpegAudioCodec, +0x4a2286e0, 0x7bef, 0x11ce, 0x9b, 0xd9, 0x0, 0x0, 0xe2, 0x2, 0x59, 0x9c) + +// e30629d3-27e5-11ce-875d-00608cb78066 Text renderer +OUR_GUID_ENTRY(CLSID_TextRender, +0xe30629d3, 0x27e5, 0x11ce, 0x87, 0x5d, 0x0, 0x60, 0x8c, 0xb7, 0x80, 0x66) + + + +// {F8388A40-D5BB-11d0-BE5A-0080C706568E} +OUR_GUID_ENTRY(CLSID_InfTee, +0xf8388a40, 0xd5bb, 0x11d0, 0xbe, 0x5a, 0x0, 0x80, 0xc7, 0x6, 0x56, 0x8e) + +// 1b544c20-fd0b-11ce-8c63-00aa0044b51e Avi Stream Splitter +OUR_GUID_ENTRY(CLSID_AviSplitter, +0x1b544c20, 0xfd0b, 0x11ce, 0x8c, 0x63, 0x0, 0xaa, 0x00, 0x44, 0xb5, 0x1e) + +// 1b544c21-fd0b-11ce-8c63-00aa0044b51e Avi File Reader +OUR_GUID_ENTRY(CLSID_AviReader, +0x1b544c21, 0xfd0b, 0x11ce, 0x8c, 0x63, 0x0, 0xaa, 0x00, 0x44, 0xb5, 0x1e) + +// 1b544c22-fd0b-11ce-8c63-00aa0044b51e Vfw 2.0 Capture Driver +OUR_GUID_ENTRY(CLSID_VfwCapture, +0x1b544c22, 0xfd0b, 0x11ce, 0x8c, 0x63, 0x0, 0xaa, 0x00, 0x44, 0xb5, 0x1e) + +OUR_GUID_ENTRY(CLSID_CaptureProperties, +0x1B544c22, 0xFD0B, 0x11ce, 0x8C, 0x63, 0x00, 0xAA, 0x00, 0x44, 0xB5, 0x1F) + +//e436ebb4-524f-11ce-9f53-0020af0ba770 Control Distributor +OUR_GUID_ENTRY(CLSID_FGControl, +0xe436ebb4, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// 44584800-F8EE-11ce-B2D4-00DD01101B85 .MOV reader (old) +OUR_GUID_ENTRY(CLSID_MOVReader, +0x44584800, 0xf8ee, 0x11ce, 0xb2, 0xd4, 0x00, 0xdd, 0x1, 0x10, 0x1b, 0x85) + +// D51BD5A0-7548-11cf-A520-0080C77EF58A QT Splitter +OUR_GUID_ENTRY(CLSID_QuickTimeParser, +0xd51bd5a0, 0x7548, 0x11cf, 0xa5, 0x20, 0x0, 0x80, 0xc7, 0x7e, 0xf5, 0x8a) + +// FDFE9681-74A3-11d0-AFA7-00AA00B67A42 QT Decoder +OUR_GUID_ENTRY(CLSID_QTDec, +0xfdfe9681, 0x74a3, 0x11d0, 0xaf, 0xa7, 0x0, 0xaa, 0x0, 0xb6, 0x7a, 0x42) + +// D3588AB0-0781-11ce-B03A-0020AF0BA770 AVIFile-based reader +OUR_GUID_ENTRY(CLSID_AVIDoc, +0xd3588ab0, 0x0781, 0x11ce, 0xb0, 0x3a, 0x00, 0x20, 0xaf, 0xb, 0xa7, 0x70) + +// 70e102b0-5556-11ce-97c0-00aa0055595a Video renderer +OUR_GUID_ENTRY(CLSID_VideoRenderer, +0x70e102b0, 0x5556, 0x11ce, 0x97, 0xc0, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a) + +// 1643e180-90f5-11ce-97d5-00aa0055595a Colour space convertor +OUR_GUID_ENTRY(CLSID_Colour, +0x1643e180, 0x90f5, 0x11ce, 0x97, 0xd5, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a) + +// 1da08500-9edc-11cf-bc10-00aa00ac74f6 VGA 16 color ditherer +OUR_GUID_ENTRY(CLSID_Dither, +0x1da08500, 0x9edc, 0x11cf, 0xbc, 0x10, 0x00, 0xaa, 0x00, 0xac, 0x74, 0xf6) + +// 07167665-5011-11cf-BF33-00AA0055595A Modex video renderer +OUR_GUID_ENTRY(CLSID_ModexRenderer, +0x7167665, 0x5011, 0x11cf, 0xbf, 0x33, 0x0, 0xaa, 0x0, 0x55, 0x59, 0x5a) + +// e30629d1-27e5-11ce-875d-00608cb78066 Waveout audio renderer +OUR_GUID_ENTRY(CLSID_AudioRender, +0xe30629d1, 0x27e5, 0x11ce, 0x87, 0x5d, 0x0, 0x60, 0x8c, 0xb7, 0x80, 0x66) + +// 05589faf-c356-11ce-bf01-00aa0055595a Audio Renderer Property Page +OUR_GUID_ENTRY(CLSID_AudioProperties, +0x05589faf, 0xc356, 0x11ce, 0xbf, 0x01, 0x0, 0xaa, 0x0, 0x55, 0x59, 0x5a) + +// 79376820-07D0-11cf-A24D-0020AFD79767 DSound audio renderer +OUR_GUID_ENTRY(CLSID_DSoundRender, +0x79376820, 0x07D0, 0x11CF, 0xA2, 0x4D, 0x0, 0x20, 0xAF, 0xD7, 0x97, 0x67) + +// e30629d2-27e5-11ce-875d-00608cb78066 Wavein audio recorder +OUR_GUID_ENTRY(CLSID_AudioRecord, +0xe30629d2, 0x27e5, 0x11ce, 0x87, 0x5d, 0x0, 0x60, 0x8c, 0xb7, 0x80, 0x66) + +// {2CA8CA52-3C3F-11d2-B73D-00C04FB6BD3D} IAMAudioInputMixer property page +OUR_GUID_ENTRY(CLSID_AudioInputMixerProperties, +0x2ca8ca52, 0x3c3f, 0x11d2, 0xb7, 0x3d, 0x0, 0xc0, 0x4f, 0xb6, 0xbd, 0x3d) + +// {CF49D4E0-1115-11ce-B03A-0020AF0BA770} AVI Decoder +OUR_GUID_ENTRY(CLSID_AVIDec, +0xcf49d4e0, 0x1115, 0x11ce, 0xb0, 0x3a, 0x0, 0x20, 0xaf, 0xb, 0xa7, 0x70) + +// {A888DF60-1E90-11cf-AC98-00AA004C0FA9} AVI ICDraw* wrapper +OUR_GUID_ENTRY(CLSID_AVIDraw, +0xa888df60, 0x1e90, 0x11cf, 0xac, 0x98, 0x0, 0xaa, 0x0, 0x4c, 0xf, 0xa9) + +// 6a08cf80-0e18-11cf-a24d-0020afd79767 ACM Wrapper +OUR_GUID_ENTRY(CLSID_ACMWrapper, +0x6a08cf80, 0x0e18, 0x11cf, 0xa2, 0x4d, 0x0, 0x20, 0xaf, 0xd7, 0x97, 0x67) + +// {e436ebb5-524f-11ce-9f53-0020af0ba770} Async File Reader +OUR_GUID_ENTRY(CLSID_AsyncReader, +0xe436ebb5, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// {e436ebb6-524f-11ce-9f53-0020af0ba770} Async URL Reader +OUR_GUID_ENTRY(CLSID_URLReader, +0xe436ebb6, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// {e436ebb7-524f-11ce-9f53-0020af0ba770} IPersistMoniker PID +OUR_GUID_ENTRY(CLSID_PersistMonikerPID, +0xe436ebb7, 0x524f, 0x11ce, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70) + +// {D76E2820-1563-11cf-AC98-00AA004C0FA9} +OUR_GUID_ENTRY(CLSID_AVICo, +0xd76e2820, 0x1563, 0x11cf, 0xac, 0x98, 0x0, 0xaa, 0x0, 0x4c, 0xf, 0xa9) + +// {8596E5F0-0DA5-11d0-BD21-00A0C911CE86} +OUR_GUID_ENTRY(CLSID_FileWriter, +0x8596e5f0, 0xda5, 0x11d0, 0xbd, 0x21, 0x0, 0xa0, 0xc9, 0x11, 0xce, 0x86) + +// {E2510970-F137-11CE-8B67-00AA00A3F1A6} AVI mux filter +OUR_GUID_ENTRY(CLSID_AviDest, +0xe2510970, 0xf137, 0x11ce, 0x8b, 0x67, 0x0, 0xaa, 0x0, 0xa3, 0xf1, 0xa6) + +// {C647B5C0-157C-11d0-BD23-00A0C911CE86} +OUR_GUID_ENTRY(CLSID_AviMuxProptyPage, +0xc647b5c0, 0x157c, 0x11d0, 0xbd, 0x23, 0x0, 0xa0, 0xc9, 0x11, 0xce, 0x86) + +// {0A9AE910-85C0-11d0-BD42-00A0C911CE86} +OUR_GUID_ENTRY(CLSID_AviMuxProptyPage1, +0xa9ae910, 0x85c0, 0x11d0, 0xbd, 0x42, 0x0, 0xa0, 0xc9, 0x11, 0xce, 0x86) + +// {07b65360-c445-11ce-afde-00aa006c14f4} +OUR_GUID_ENTRY(CLSID_AVIMIDIRender, +0x07b65360, 0xc445, 0x11ce, 0xaf, 0xde, 0x00, 0xaa, 0x00, 0x6c, 0x14, 0xf4) + +// {187463A0-5BB7-11d3-ACBE-0080C75E246E} WMSDK-based ASF reader +OUR_GUID_ENTRY(CLSID_WMAsfReader, +0x187463a0, 0x5bb7, 0x11d3, 0xac, 0xbe, 0x0, 0x80, 0xc7, 0x5e, 0x24, 0x6e) + +// {7c23220e-55bb-11d3-8b16-00c04fb6bd3d} WMSDK-based ASF writer +OUR_GUID_ENTRY(CLSID_WMAsfWriter, +0x7c23220e, 0x55bb, 0x11d3, 0x8b, 0x16, 0x0, 0xc0, 0x4f, 0xb6, 0xbd, 0x3d) + +// {afb6c280-2c41-11d3-8a60-0000f81e0e4a} +OUR_GUID_ENTRY(CLSID_MPEG2Demultiplexer, +0xafb6c280, 0x2c41, 0x11d3, 0x8a, 0x60, 0x00, 0x00, 0xf8, 0x1e, 0x0e, 0x4a) + +// {687D3367-3644-467a-ADFE-6CD7A85C4A2C} +OUR_GUID_ENTRY(CLSID_MPEG2Demultiplexer_NoClock, +0x687d3367, 0x3644, 0x467a, 0xad, 0xfe, 0x6c, 0xd7, 0xa8, 0x5c, 0x4a, 0x2c) + +// {3ae86b20-7be8-11d1-abe6-00a0c905f375} +OUR_GUID_ENTRY(CLSID_MMSPLITTER, +0x3ae86b20, 0x7be8, 0x11d1, 0xab, 0xe6, 0x00, 0xa0, 0xc9, 0x05, 0xf3, 0x75) + +// {2DB47AE5-CF39-43c2-B4D6-0CD8D90946F4} +OUR_GUID_ENTRY(CLSID_StreamBufferSink, +0x2db47ae5, 0xcf39, 0x43c2, 0xb4, 0xd6, 0xc, 0xd8, 0xd9, 0x9, 0x46, 0xf4) + +// {E2448508-95DA-4205-9A27-7EC81E723B1A} +OUR_GUID_ENTRY(CLSID_SBE2Sink, +0xe2448508, 0x95da, 0x4205, 0x9a, 0x27, 0x7e, 0xc8, 0x1e, 0x72, 0x3b, 0x1a) + +// {C9F5FE02-F851-4eb5-99EE-AD602AF1E619} +OUR_GUID_ENTRY(CLSID_StreamBufferSource, +0xc9f5fe02, 0xf851, 0x4eb5, 0x99, 0xee, 0xad, 0x60, 0x2a, 0xf1, 0xe6, 0x19) + +// {FA8A68B2-C864-4ba2-AD53-D3876A87494B} +OUR_GUID_ENTRY(CLSID_StreamBufferConfig, +0xfa8a68b2, 0xc864, 0x4ba2, 0xad, 0x53, 0xd3, 0x87, 0x6a, 0x87, 0x49, 0x4b) + +// {E37A73F8-FB01-43dc-914E-AAEE76095AB9} +OUR_GUID_ENTRY(CLSID_StreamBufferPropertyHandler, +0xe37a73f8, 0xfb01, 0x43dc, 0x91, 0x4e, 0xaa, 0xee, 0x76, 0x9, 0x5a, 0xb9) + +// {713790EE-5EE1-45ba-8070-A1337D2762FA} +OUR_GUID_ENTRY(CLSID_StreamBufferThumbnailHandler, +0x713790ee, 0x5ee1, 0x45ba, 0x80, 0x70, 0xa1, 0x33, 0x7d, 0x27, 0x62, 0xfa) + +// {6CFAD761-735D-4aa5-8AFC-AF91A7D61EBA} +OUR_GUID_ENTRY(CLSID_Mpeg2VideoStreamAnalyzer, +0x6cfad761, 0x735d, 0x4aa5, 0x8a, 0xfc, 0xaf, 0x91, 0xa7, 0xd6, 0x1e, 0xba) + +// {CCAA63AC-1057-4778-AE92-1206AB9ACEE6} +OUR_GUID_ENTRY(CLSID_StreamBufferRecordingAttributes, +0xccaa63ac, 0x1057, 0x4778, 0xae, 0x92, 0x12, 0x6, 0xab, 0x9a, 0xce, 0xe6) + +// {D682C4BA-A90A-42fe-B9E1-03109849C423} +OUR_GUID_ENTRY(CLSID_StreamBufferComposeRecording, +0xd682c4ba, 0xa90a, 0x42fe, 0xb9, 0xe1, 0x3, 0x10, 0x98, 0x49, 0xc4, 0x23) + +// {93A094D7-51E8-485b-904A-8D6B97DC6B39} +OUR_GUID_ENTRY(CLSID_SBE2File, +0x93a094d7, 0x51e8, 0x485b, 0x90, 0x4a, 0x8d, 0x6b, 0x97, 0xdc, 0x6b, 0x39) + +// {B1B77C00-C3E4-11cf-AF79-00AA00B67A42} DV video decoder +OUR_GUID_ENTRY(CLSID_DVVideoCodec, +0xb1b77c00, 0xc3e4, 0x11cf, 0xaf, 0x79, 0x0, 0xaa, 0x0, 0xb6, 0x7a, 0x42) + +// {13AA3650-BB6F-11d0-AFB9-00AA00B67A42} DV video encoder +OUR_GUID_ENTRY(CLSID_DVVideoEnc, +0x13aa3650, 0xbb6f, 0x11d0, 0xaf, 0xb9, 0x0, 0xaa, 0x0, 0xb6, 0x7a, 0x42) + +// {4EB31670-9FC6-11cf-AF6E-00AA00B67A42} DV splitter +OUR_GUID_ENTRY(CLSID_DVSplitter, +0x4eb31670, 0x9fc6, 0x11cf, 0xaf, 0x6e, 0x0, 0xaa, 0x0, 0xb6, 0x7a, 0x42) + +// {129D7E40-C10D-11d0-AFB9-00AA00B67A42} DV muxer +OUR_GUID_ENTRY(CLSID_DVMux, +0x129d7e40, 0xc10d, 0x11d0, 0xaf, 0xb9, 0x0, 0xaa, 0x0, 0xb6, 0x7a, 0x42) + +// {060AF76C-68DD-11d0-8FC1-00C04FD9189D} +OUR_GUID_ENTRY(CLSID_SeekingPassThru, +0x60af76c, 0x68dd, 0x11d0, 0x8f, 0xc1, 0x0, 0xc0, 0x4f, 0xd9, 0x18, 0x9d) + +// 6E8D4A20-310C-11d0-B79A-00AA003767A7 Line21 (CC) Decoder +OUR_GUID_ENTRY(CLSID_Line21Decoder, +0x6e8d4a20, 0x310c, 0x11d0, 0xb7, 0x9a, 0x0, 0xaa, 0x0, 0x37, 0x67, 0xa7) + +// E4206432-01A1-4BEE-B3E1-3702C8EDC574 Line21 (CC) Decoder v2 +OUR_GUID_ENTRY(CLSID_Line21Decoder2, +0xe4206432, 0x01a1, 0x4bee, 0xb3, 0xe1, 0x37, 0x02, 0xc8, 0xed, 0xc5, 0x74) + +OUR_GUID_ENTRY(CLSID_CCAFilter, +0x3d07a539, 0x35ca, 0x447c, 0x9b, 0x5, 0x8d, 0x85, 0xce, 0x92, 0x4f, 0x9e) + +// {CD8743A1-3736-11d0-9E69-00C04FD7C15B} +OUR_GUID_ENTRY(CLSID_OverlayMixer, +0xcd8743a1, 0x3736, 0x11d0, 0x9e, 0x69, 0x0, 0xc0, 0x4f, 0xd7, 0xc1, 0x5b) + +// {814B9800-1C88-11d1-BAD9-00609744111A} +OUR_GUID_ENTRY(CLSID_VBISurfaces, +0x814b9800, 0x1c88, 0x11d1, 0xba, 0xd9, 0x0, 0x60, 0x97, 0x44, 0x11, 0x1a) + +// {70BC06E0-5666-11d3-A184-00105AEF9F33} WST Teletext Decoder +OUR_GUID_ENTRY(CLSID_WSTDecoder, +0x70bc06e0, 0x5666, 0x11d3, 0xa1, 0x84, 0x0, 0x10, 0x5a, 0xef, 0x9f, 0x33) + +// {301056D0-6DFF-11d2-9EEB-006008039E37} +OUR_GUID_ENTRY(CLSID_MjpegDec, +0x301056d0, 0x6dff, 0x11d2, 0x9e, 0xeb, 0x0, 0x60, 0x8, 0x3, 0x9e, 0x37) + +// {B80AB0A0-7416-11d2-9EEB-006008039E37} +OUR_GUID_ENTRY(CLSID_MJPGEnc, +0xb80ab0a0, 0x7416, 0x11d2, 0x9e, 0xeb, 0x0, 0x60, 0x8, 0x3, 0x9e, 0x37) + + + +// pnp objects and categories +// 62BE5D10-60EB-11d0-BD3B-00A0C911CE86 ICreateDevEnum +OUR_GUID_ENTRY(CLSID_SystemDeviceEnum, +0x62BE5D10,0x60EB,0x11d0,0xBD,0x3B,0x00,0xA0,0xC9,0x11,0xCE,0x86) + +// 4315D437-5B8C-11d0-BD3B-00A0C911CE86 +OUR_GUID_ENTRY(CLSID_CDeviceMoniker, +0x4315D437,0x5B8C,0x11d0,0xBD,0x3B,0x00,0xA0,0xC9,0x11,0xCE,0x86) + +// 860BB310-5D01-11d0-BD3B-00A0C911CE86 Video capture category +OUR_GUID_ENTRY(CLSID_VideoInputDeviceCategory, +0x860BB310,0x5D01,0x11d0,0xBD,0x3B,0x00,0xA0,0xC9,0x11,0xCE,0x86) +OUR_GUID_ENTRY(CLSID_CVidCapClassManager, +0x860BB310,0x5D01,0x11d0,0xBD,0x3B,0x00,0xA0,0xC9,0x11,0xCE,0x86) + +// 083863F1-70DE-11d0-BD40-00A0C911CE86 Filter category +OUR_GUID_ENTRY(CLSID_LegacyAmFilterCategory, +0x083863F1,0x70DE,0x11d0,0xBD,0x40,0x00,0xA0,0xC9,0x11,0xCE,0x86) +OUR_GUID_ENTRY(CLSID_CQzFilterClassManager, +0x083863F1,0x70DE,0x11d0,0xBD,0x40,0x00,0xA0,0xC9,0x11,0xCE,0x86) + +// 33D9A760-90C8-11d0-BD43-00A0C911CE86 +OUR_GUID_ENTRY(CLSID_VideoCompressorCategory, +0x33d9a760, 0x90c8, 0x11d0, 0xbd, 0x43, 0x0, 0xa0, 0xc9, 0x11, 0xce, 0x86) +OUR_GUID_ENTRY(CLSID_CIcmCoClassManager, +0x33d9a760, 0x90c8, 0x11d0, 0xbd, 0x43, 0x0, 0xa0, 0xc9, 0x11, 0xce, 0x86) + +// 33D9A761-90C8-11d0-BD43-00A0C911CE86 +OUR_GUID_ENTRY(CLSID_AudioCompressorCategory, +0x33d9a761, 0x90c8, 0x11d0, 0xbd, 0x43, 0x0, 0xa0, 0xc9, 0x11, 0xce, 0x86) +OUR_GUID_ENTRY(CLSID_CAcmCoClassManager, +0x33d9a761, 0x90c8, 0x11d0, 0xbd, 0x43, 0x0, 0xa0, 0xc9, 0x11, 0xce, 0x86) + +// 33D9A762-90C8-11d0-BD43-00A0C911CE86 Audio source cateogry +OUR_GUID_ENTRY(CLSID_AudioInputDeviceCategory, +0x33d9a762, 0x90c8, 0x11d0, 0xbd, 0x43, 0x0, 0xa0, 0xc9, 0x11, 0xce, 0x86) +OUR_GUID_ENTRY(CLSID_CWaveinClassManager, +0x33d9a762, 0x90c8, 0x11d0, 0xbd, 0x43, 0x0, 0xa0, 0xc9, 0x11, 0xce, 0x86) + +// E0F158E1-CB04-11d0-BD4E-00A0C911CE86 Audio renderer category +OUR_GUID_ENTRY(CLSID_AudioRendererCategory, +0xe0f158e1, 0xcb04, 0x11d0, 0xbd, 0x4e, 0x0, 0xa0, 0xc9, 0x11, 0xce, 0x86) +OUR_GUID_ENTRY(CLSID_CWaveOutClassManager, +0xe0f158e1, 0xcb04, 0x11d0, 0xbd, 0x4e, 0x0, 0xa0, 0xc9, 0x11, 0xce, 0x86) + +// 4EFE2452-168A-11d1-BC76-00C04FB9453B Midi renderer category +OUR_GUID_ENTRY(CLSID_MidiRendererCategory, +0x4EfE2452, 0x168A, 0x11d1, 0xBC, 0x76, 0x0, 0xc0, 0x4F, 0xB9, 0x45, 0x3B) +OUR_GUID_ENTRY(CLSID_CMidiOutClassManager, +0x4EfE2452, 0x168A, 0x11d1, 0xBC, 0x76, 0x0, 0xc0, 0x4F, 0xB9, 0x45, 0x3B) + +// CC7BFB41-F175-11d1-A392-00E0291F3959 External Renderers Category +OUR_GUID_ENTRY(CLSID_TransmitCategory, +0xcc7bfb41, 0xf175, 0x11d1, 0xa3, 0x92, 0x0, 0xe0, 0x29, 0x1f, 0x39, 0x59) + +// CC7BFB46-F175-11d1-A392-00E0291F3959 Device Control Filters +OUR_GUID_ENTRY(CLSID_DeviceControlCategory, +0xcc7bfb46, 0xf175, 0x11d1, 0xa3, 0x92, 0x0, 0xe0, 0x29, 0x1f, 0x39, 0x59) + +// DA4E3DA0-D07D-11d0-BD50-00A0C911CE86 +OUR_GUID_ENTRY(CLSID_ActiveMovieCategories, +0xda4e3da0, 0xd07d, 0x11d0, 0xbd, 0x50, 0x0, 0xa0, 0xc9, 0x11, 0xce, 0x86) + +// 2721AE20-7E70-11D0-A5D6-28DB04C10000 +OUR_GUID_ENTRY(CLSID_DVDHWDecodersCategory, +0x2721AE20, 0x7E70, 0x11D0, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00) + +// 7D22E920-5CA9-4787-8C2B-A6779BD11781 Encoder API encoder category +OUR_GUID_ENTRY(CLSID_MediaEncoderCategory, +0x7D22E920, 0x5CA9, 0x4787, 0x8C, 0x2B, 0xA6, 0x77, 0x9B, 0xD1, 0x17, 0x81) + +// 236C9559-ADCE-4736-BF72-BAB34E392196 Encoder API multiplexer category +OUR_GUID_ENTRY(CLSID_MediaMultiplexerCategory, +0x236C9559, 0xADCE, 0x4736, 0xBF, 0x72, 0xBA, 0xB3, 0x4E, 0x39, 0x21, 0x96) + +// CDA42200-BD88-11d0-BD4E-00A0C911CE86 +OUR_GUID_ENTRY(CLSID_FilterMapper2, +0xcda42200, 0xbd88, 0x11d0, 0xbd, 0x4e, 0x0, 0xa0, 0xc9, 0x11, 0xce, 0x86) + + +// 1e651cc0-b199-11d0-8212-00c04fc32c45 +OUR_GUID_ENTRY(CLSID_MemoryAllocator, +0x1e651cc0, 0xb199, 0x11d0, 0x82, 0x12, 0x00, 0xc0, 0x4f, 0xc3, 0x2c, 0x45) + +// CDBD8D00-C193-11d0-BD4E-00A0C911CE86 +OUR_GUID_ENTRY(CLSID_MediaPropertyBag, +0xcdbd8d00, 0xc193, 0x11d0, 0xbd, 0x4e, 0x0, 0xa0, 0xc9, 0x11, 0xce, 0x86) + +// FCC152B7-F372-11d0-8E00-00C04FD7C08B +OUR_GUID_ENTRY(CLSID_DvdGraphBuilder, +0xFCC152B7, 0xF372, 0x11d0, 0x8E, 0x00, 0x00, 0xC0, 0x4F, 0xD7, 0xC0, 0x8B) + +// 9B8C4620-2C1A-11d0-8493-00A02438AD48 +OUR_GUID_ENTRY(CLSID_DVDNavigator, +0x9b8c4620, 0x2c1a, 0x11d0, 0x84, 0x93, 0x0, 0xa0, 0x24, 0x38, 0xad, 0x48) + +// f963c5cf-a659-4a93-9638-caf3cd277d13 +OUR_GUID_ENTRY(CLSID_DVDState, +0xf963c5cf, 0xa659, 0x4a93, 0x96, 0x38, 0xca, 0xf3, 0xcd, 0x27, 0x7d, 0x13) + +// CC58E280-8AA1-11d1-B3F1-00AA003761C5 +OUR_GUID_ENTRY(CLSID_SmartTee, +0xcc58e280, 0x8aa1, 0x11d1, 0xb3, 0xf1, 0x0, 0xaa, 0x0, 0x37, 0x61, 0xc5) + +// FB056BA0-2502-45B9-8E86-2B40DE84AD29 +OUR_GUID_ENTRY(CLSID_DtvCcFilter, +0xfb056ba0, 0x2502, 0x45b9, 0x8e, 0x86, 0x2b, 0x40, 0xde, 0x84, 0xad, 0x29) + +// 2F7EE4B6-6FF5-4EB4-B24A-2BFC41117171 +OUR_GUID_ENTRY(CLSID_CaptionsFilter, +0x2F7EE4B6, 0x6FF5, 0x4EB4, 0xB2, 0x4A, 0x2B, 0xFC, 0x41, 0x11, 0x71, 0x71) + +// {9F22CFEA-CE07-41ab-8BA0-C7364AF90AF9} +OUR_GUID_ENTRY(CLSID_SubtitlesFilter, +0x9f22cfea, 0xce07, 0x41ab, 0x8b, 0xa0, 0xc7, 0x36, 0x4a, 0xf9, 0x0a, 0xf9) + +// {8670C736-F614-427b-8ADA-BBADC587194B} +OUR_GUID_ENTRY(CLSID_DirectShowPluginControl, +0x8670c736, 0xf614, 0x427b, 0x8a, 0xda, 0xbb, 0xad, 0xc5, 0x87, 0x19, 0x4b) + + +// -- format types --- + +// 0F6417D6-C318-11D0-A43F-00A0C9223196 FORMAT_None +OUR_GUID_ENTRY(FORMAT_None, +0x0F6417D6, 0xc318, 0x11d0, 0xa4, 0x3f, 0x00, 0xa0, 0xc9, 0x22, 0x31, 0x96) + +// 05589f80-c356-11ce-bf01-00aa0055595a FORMAT_VideoInfo +OUR_GUID_ENTRY(FORMAT_VideoInfo, +0x05589f80, 0xc356, 0x11ce, 0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a) + +// F72A76A0-EB0A-11d0-ACE4-0000C0CC16BA FORMAT_VideoInfo2 +OUR_GUID_ENTRY(FORMAT_VideoInfo2, +0xf72a76A0, 0xeb0a, 0x11d0, 0xac, 0xe4, 0x00, 0x00, 0xc0, 0xcc, 0x16, 0xba) + +// 05589f81-c356-11ce-bf01-00aa0055595a FORMAT_WaveFormatEx +OUR_GUID_ENTRY(FORMAT_WaveFormatEx, +0x05589f81, 0xc356, 0x11ce, 0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a) + +// 05589f82-c356-11ce-bf01-00aa0055595a FORMAT_MPEGVideo +OUR_GUID_ENTRY(FORMAT_MPEGVideo, +0x05589f82, 0xc356, 0x11ce, 0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a) + +// 05589f83-c356-11ce-bf01-00aa0055595a FORMAT_MPEGStreams +OUR_GUID_ENTRY(FORMAT_MPEGStreams, +0x05589f83, 0xc356, 0x11ce, 0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a) + +// 05589f84-c356-11ce-bf01-00aa0055595a FORMAT_DvInfo, DVINFO +OUR_GUID_ENTRY(FORMAT_DvInfo, +0x05589f84, 0xc356, 0x11ce, 0xbf, 0x01, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a) + +// C7ECF04D-4582-4869-9ABB-BFB523B62EDF FORMAT_525WSS +OUR_GUID_ENTRY(FORMAT_525WSS, +0xc7ecf04d, 0x4582, 0x4869, 0x9a, 0xbb, 0xbf, 0xb5, 0x23, 0xb6, 0x2e, 0xdf) + +// -- Video related GUIDs --- + +// 944d4c00-dd52-11ce-bf0e-00aa0055595a +OUR_GUID_ENTRY(CLSID_DirectDrawProperties, +0x944d4c00, 0xdd52, 0x11ce, 0xbf, 0x0e, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a) + +// 59ce6880-acf8-11cf-b56e-0080c7c4b68a +OUR_GUID_ENTRY(CLSID_PerformanceProperties, +0x59ce6880, 0xacf8, 0x11cf, 0xb5, 0x6e, 0x00, 0x80, 0xc7, 0xc4, 0xb6, 0x8a) + +// 418afb70-f8b8-11ce-aac6-0020af0b99a3 +OUR_GUID_ENTRY(CLSID_QualityProperties, +0x418afb70, 0xf8b8, 0x11ce, 0xaa, 0xc6, 0x00, 0x20, 0xaf, 0x0b, 0x99, 0xa3) + +// 61ded640-e912-11ce-a099-00aa00479a58 +OUR_GUID_ENTRY(IID_IBaseVideoMixer, +0x61ded640, 0xe912, 0x11ce, 0xa0, 0x99, 0x00, 0xaa, 0x00, 0x47, 0x9a, 0x58) + +// 36d39eb0-dd75-11ce-bf0e-00aa0055595a +OUR_GUID_ENTRY(IID_IDirectDrawVideo, +0x36d39eb0, 0xdd75, 0x11ce, 0xbf, 0x0e, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a) + +// bd0ecb0-f8e2-11ce-aac6-0020af0b99a3 +OUR_GUID_ENTRY(IID_IQualProp, +0x1bd0ecb0, 0xf8e2, 0x11ce, 0xaa, 0xc6, 0x00, 0x20, 0xaf, 0x0b, 0x99, 0xa3) + +// {CE292861-FC88-11d0-9E69-00C04FD7C15B} +OUR_GUID_ENTRY(CLSID_VPObject, +0xce292861, 0xfc88, 0x11d0, 0x9e, 0x69, 0x0, 0xc0, 0x4f, 0xd7, 0xc1, 0x5b) + +// {CE292862-FC88-11d0-9E69-00C04FD7C15B} +OUR_GUID_ENTRY(IID_IVPObject, +0xce292862, 0xfc88, 0x11d0, 0x9e, 0x69, 0x0, 0xc0, 0x4f, 0xd7, 0xc1, 0x5b) + +// {25DF12C1-3DE0-11d1-9E69-00C04FD7C15B} +OUR_GUID_ENTRY(IID_IVPControl, +0x25df12c1, 0x3de0, 0x11d1, 0x9e, 0x69, 0x0, 0xc0, 0x4f, 0xd7, 0xc1, 0x5b) + +// {814B9801-1C88-11d1-BAD9-00609744111A} +OUR_GUID_ENTRY(CLSID_VPVBIObject, +0x814b9801, 0x1c88, 0x11d1, 0xba, 0xd9, 0x0, 0x60, 0x97, 0x44, 0x11, 0x1a) + +// {814B9802-1C88-11d1-BAD9-00609744111A} +OUR_GUID_ENTRY(IID_IVPVBIObject, +0x814b9802, 0x1c88, 0x11d1, 0xba, 0xd9, 0x0, 0x60, 0x97, 0x44, 0x11, 0x1a) + +// {BC29A660-30E3-11d0-9E69-00C04FD7C15B} +OUR_GUID_ENTRY(IID_IVPConfig, +0xbc29a660, 0x30e3, 0x11d0, 0x9e, 0x69, 0x0, 0xc0, 0x4f, 0xd7, 0xc1, 0x5b) + +// {C76794A1-D6C5-11d0-9E69-00C04FD7C15B} +OUR_GUID_ENTRY(IID_IVPNotify, +0xc76794a1, 0xd6c5, 0x11d0, 0x9e, 0x69, 0x0, 0xc0, 0x4f, 0xd7, 0xc1, 0x5b) + +// {EBF47183-8764-11d1-9E69-00C04FD7C15B} +OUR_GUID_ENTRY(IID_IVPNotify2, +0xebf47183, 0x8764, 0x11d1, 0x9e, 0x69, 0x0, 0xc0, 0x4f, 0xd7, 0xc1, 0x5b) + + +// {EC529B00-1A1F-11D1-BAD9-00609744111A} +OUR_GUID_ENTRY(IID_IVPVBIConfig, +0xec529b00, 0x1a1f, 0x11d1, 0xba, 0xd9, 0x0, 0x60, 0x97, 0x44, 0x11, 0x1a) + +// {EC529B01-1A1F-11D1-BAD9-00609744111A} +OUR_GUID_ENTRY(IID_IVPVBINotify, +0xec529b01, 0x1a1f, 0x11d1, 0xba, 0xd9, 0x0, 0x60, 0x97, 0x44, 0x11, 0x1a) + +// {593CDDE1-0759-11d1-9E69-00C04FD7C15B} +OUR_GUID_ENTRY(IID_IMixerPinConfig, +0x593cdde1, 0x759, 0x11d1, 0x9e, 0x69, 0x0, 0xc0, 0x4f, 0xd7, 0xc1, 0x5b) + +// {EBF47182-8764-11d1-9E69-00C04FD7C15B} +OUR_GUID_ENTRY(IID_IMixerPinConfig2, +0xebf47182, 0x8764, 0x11d1, 0x9e, 0x69, 0x0, 0xc0, 0x4f, 0xd7, 0xc1, 0x5b) + + +// This is a real pain in the neck. The OLE GUIDs are separated out into a +// different file from the main header files. The header files can then be +// included multiple times and are protected with the following statements, +// +// #ifndef __SOMETHING_DEFINED__ +// #define __SOMETHING_DEFINED__ +// all the header contents +// #endif // __SOMETHING_DEFINED__ +// +// When the actual GUIDs are to be defined (using initguid) the GUID header +// file can then be included to really define them just once. Unfortunately +// DirectDraw has the GUIDs defined in the main header file. So if the base +// classes bring in ddraw.h to get at the DirectDraw structures and so on +// nobody would then be able to really include ddraw.h to allocate the GUID +// memory structures because of the aforementioned header file protection +// Therefore the DirectDraw GUIDs are defined and allocated for real here + +#ifndef __DDRAW_INCLUDED__ +OUR_GUID_ENTRY(CLSID_DirectDraw, 0xD7B70EE0,0x4340,0x11CF,0xB0,0x63,0x00,0x20,0xAF,0xC2,0xCD,0x35) +OUR_GUID_ENTRY(CLSID_DirectDrawClipper, 0x593817A0,0x7DB3,0x11CF,0xA2,0xDE,0x00,0xAA,0x00,0xb9,0x33,0x56) +OUR_GUID_ENTRY(IID_IDirectDraw, 0x6C14DB80,0xA733,0x11CE,0xA5,0x21,0x00,0x20,0xAF,0x0B,0xE5,0x60) +OUR_GUID_ENTRY(IID_IDirectDraw2, 0xB3A6F3E0,0x2B43,0x11CF,0xA2,0xDE,0x00,0xAA,0x00,0xB9,0x33,0x56) +OUR_GUID_ENTRY(IID_IDirectDrawSurface, 0x6C14DB81,0xA733,0x11CE,0xA5,0x21,0x00,0x20,0xAF,0x0B,0xE5,0x60) +OUR_GUID_ENTRY(IID_IDirectDrawSurface2, 0x57805885,0x6eec,0x11cf,0x94,0x41,0xa8,0x23,0x03,0xc1,0x0e,0x27) +OUR_GUID_ENTRY(IID_IDirectDrawSurface3, 0xDA044E00,0x69B2,0x11D0,0xA1,0xD5,0x00,0xAA,0x00,0xB8,0xDF,0xBB) +OUR_GUID_ENTRY(IID_IDirectDrawSurface4, 0x0B2B8630,0xAD35,0x11D0,0x8E,0xA6,0x00,0x60,0x97,0x97,0xEA,0x5B) +OUR_GUID_ENTRY(IID_IDirectDrawSurface7, 0x06675a80,0x3b9b,0x11d2,0xb9,0x2f,0x00,0x60,0x97,0x97,0xea,0x5b) +OUR_GUID_ENTRY(IID_IDirectDrawPalette, 0x6C14DB84,0xA733,0x11CE,0xA5,0x21,0x00,0x20,0xAF,0x0B,0xE5,0x60) +OUR_GUID_ENTRY(IID_IDirectDrawClipper, 0x6C14DB85,0xA733,0x11CE,0xA5,0x21,0x00,0x20,0xAF,0x0B,0xE5,0x60) +OUR_GUID_ENTRY(IID_IDirectDrawColorControl, 0x4B9F0EE0,0x0D7E,0x11D0,0x9B,0x06,0x00,0xA0,0xC9,0x03,0xA3,0xB8) +#endif + +#ifndef __DVP_INCLUDED__ +OUR_GUID_ENTRY(IID_IDDVideoPortContainer, 0x6C142760,0xA733,0x11CE,0xA5,0x21,0x00,0x20,0xAF,0x0B,0xE5,0x60) +#endif + +#ifndef __DDKM_INCLUDED__ +OUR_GUID_ENTRY(IID_IDirectDrawKernel, 0x8D56C120,0x6A08,0x11D0,0x9B,0x06,0x00,0xA0,0xC9,0x03,0xA3,0xB8) +OUR_GUID_ENTRY(IID_IDirectDrawSurfaceKernel, 0x60755DA0,0x6A40,0x11D0,0x9B,0x06,0x00,0xA0,0xC9,0x03,0xA3,0xB8) +#endif + +// 0618aa30-6bc4-11cf-bf36-00aa0055595a +OUR_GUID_ENTRY(CLSID_ModexProperties, +0x0618aa30, 0x6bc4, 0x11cf, 0xbf, 0x36, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a) + +// dd1d7110-7836-11cf-bf47-00aa0055595a +OUR_GUID_ENTRY(IID_IFullScreenVideo, +0xdd1d7110, 0x7836, 0x11cf, 0xbf, 0x47, 0x00, 0xaa, 0x00, 0x55, 0x59, 0x5a) + +// 53479470-f1dd-11cf-bc42-00aa00ac74f6 +OUR_GUID_ENTRY(IID_IFullScreenVideoEx, +0x53479470, 0xf1dd, 0x11cf, 0xbc, 0x42, 0x00, 0xaa, 0x00, 0xac, 0x74, 0xf6) + +// {101193C0-0BFE-11d0-AF91-00AA00B67A42} DV decoder property +OUR_GUID_ENTRY(CLSID_DVDecPropertiesPage, +0x101193c0, 0xbfe, 0x11d0, 0xaf, 0x91, 0x0, 0xaa, 0x0, 0xb6, 0x7a, 0x42) + +// {4150F050-BB6F-11d0-AFB9-00AA00B67A42} DV encoder property +OUR_GUID_ENTRY(CLSID_DVEncPropertiesPage, +0x4150f050, 0xbb6f, 0x11d0, 0xaf, 0xb9, 0x0, 0xaa, 0x0, 0xb6, 0x7a, 0x42) + +// {4DB880E0-C10D-11d0-AFB9-00AA00B67A42} DV Muxer property +OUR_GUID_ENTRY(CLSID_DVMuxPropertyPage, +0x4db880e0, 0xc10d, 0x11d0, 0xaf, 0xb9, 0x0, 0xaa, 0x0, 0xb6, 0x7a, 0x42) + + +// -- Direct Sound Audio related GUID --- + +// 546F4260-D53E-11cf-B3F0-00AA003761C5 +OUR_GUID_ENTRY(IID_IAMDirectSound, +0x546f4260, 0xd53e, 0x11cf, 0xb3, 0xf0, 0x0, 0xaa, 0x0, 0x37, 0x61, 0xc5) + +// -- MPEG audio decoder properties + +// {b45dd570-3c77-11d1-abe1-00a0c905f375} +OUR_GUID_ENTRY(IID_IMpegAudioDecoder, +0xb45dd570, 0x3c77, 0x11d1, 0xab, 0xe1, 0x00, 0xa0, 0xc9, 0x05, 0xf3, 0x75) + +// --- Line21 Decoder interface GUID --- + +// 6E8D4A21-310C-11d0-B79A-00AA003767A7 IID_IAMLine21Decoder +OUR_GUID_ENTRY(IID_IAMLine21Decoder, +0x6e8d4a21, 0x310c, 0x11d0, 0xb7, 0x9a, 0x0, 0xaa, 0x0, 0x37, 0x67, 0xa7) + +// --- WST Decoder interface GUID --- + +// C056DE21-75C2-11d3-A184-00105AEF9F33 IID_IAMWstDecoder +OUR_GUID_ENTRY(IID_IAMWstDecoder, +0xc056de21, 0x75c2, 0x11d3, 0xa1, 0x84, 0x0, 0x10, 0x5a, 0xef, 0x9f, 0x33) + +// --- WST Decoder Property Page --- + +// 04E27F80-91E4-11d3-A184-00105AEF9F33 WST Decoder Property Page +OUR_GUID_ENTRY(CLSID_WstDecoderPropertyPage, +0x4e27f80, 0x91e4, 0x11d3, 0xa1, 0x84, 0x0, 0x10, 0x5a, 0xef, 0x9f, 0x33) + + +// -- Analog video related GUIDs --- + + +// -- format types --- +// 0482DDE0-7817-11cf-8A03-00AA006ECB65 +OUR_GUID_ENTRY(FORMAT_AnalogVideo, +0x482dde0, 0x7817, 0x11cf, 0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65) + + +// -- major type, Analog Video + +// 0482DDE1-7817-11cf-8A03-00AA006ECB65 +OUR_GUID_ENTRY(MEDIATYPE_AnalogVideo, +0x482dde1, 0x7817, 0x11cf, 0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65) + + +// -- Analog Video subtypes, NTSC + +// 0482DDE2-7817-11cf-8A03-00AA006ECB65 +OUR_GUID_ENTRY(MEDIASUBTYPE_AnalogVideo_NTSC_M, +0x482dde2, 0x7817, 0x11cf, 0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65) + +// -- Analog Video subtypes, PAL + +// 0482DDE5-7817-11cf-8A03-00AA006ECB65 +OUR_GUID_ENTRY(MEDIASUBTYPE_AnalogVideo_PAL_B, +0x482dde5, 0x7817, 0x11cf, 0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65) + +// 0482DDE6-7817-11cf-8A03-00AA006ECB65 +OUR_GUID_ENTRY(MEDIASUBTYPE_AnalogVideo_PAL_D, +0x482dde6, 0x7817, 0x11cf, 0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65) + +// 0482DDE7-7817-11cf-8A03-00AA006ECB65 +OUR_GUID_ENTRY(MEDIASUBTYPE_AnalogVideo_PAL_G, +0x482dde7, 0x7817, 0x11cf, 0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65) + +// 0482DDE8-7817-11cf-8A03-00AA006ECB65 +OUR_GUID_ENTRY(MEDIASUBTYPE_AnalogVideo_PAL_H, +0x482dde8, 0x7817, 0x11cf, 0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65) + +// 0482DDE9-7817-11cf-8A03-00AA006ECB65 +OUR_GUID_ENTRY(MEDIASUBTYPE_AnalogVideo_PAL_I, +0x482dde9, 0x7817, 0x11cf, 0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65) + +// 0482DDEA-7817-11cf-8A03-00AA006ECB65 +OUR_GUID_ENTRY(MEDIASUBTYPE_AnalogVideo_PAL_M, +0x482ddea, 0x7817, 0x11cf, 0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65) + +// 0482DDEB-7817-11cf-8A03-00AA006ECB65 +OUR_GUID_ENTRY(MEDIASUBTYPE_AnalogVideo_PAL_N, +0x482ddeb, 0x7817, 0x11cf, 0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65) + +// 0482DDEC-7817-11cf-8A03-00AA006ECB65 +OUR_GUID_ENTRY(MEDIASUBTYPE_AnalogVideo_PAL_N_COMBO, +0x482ddec, 0x7817, 0x11cf, 0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65) + +// -- Analog Video subtypes, SECAM + +// 0482DDF0-7817-11cf-8A03-00AA006ECB65 +OUR_GUID_ENTRY(MEDIASUBTYPE_AnalogVideo_SECAM_B, +0x482ddf0, 0x7817, 0x11cf, 0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65) + +// 0482DDF1-7817-11cf-8A03-00AA006ECB65 +OUR_GUID_ENTRY(MEDIASUBTYPE_AnalogVideo_SECAM_D, +0x482ddf1, 0x7817, 0x11cf, 0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65) + +// 0482DDF2-7817-11cf-8A03-00AA006ECB65 +OUR_GUID_ENTRY(MEDIASUBTYPE_AnalogVideo_SECAM_G, +0x482ddf2, 0x7817, 0x11cf, 0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65) + +// 0482DDF3-7817-11cf-8A03-00AA006ECB65 +OUR_GUID_ENTRY(MEDIASUBTYPE_AnalogVideo_SECAM_H, +0x482ddf3, 0x7817, 0x11cf, 0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65) + +// 0482DDF4-7817-11cf-8A03-00AA006ECB65 +OUR_GUID_ENTRY(MEDIASUBTYPE_AnalogVideo_SECAM_K, +0x482ddf4, 0x7817, 0x11cf, 0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65) + +// 0482DDF5-7817-11cf-8A03-00AA006ECB65 +OUR_GUID_ENTRY(MEDIASUBTYPE_AnalogVideo_SECAM_K1, +0x482ddf5, 0x7817, 0x11cf, 0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65) + +// 0482DDF6-7817-11cf-8A03-00AA006ECB65 +OUR_GUID_ENTRY(MEDIASUBTYPE_AnalogVideo_SECAM_L, +0x482ddf6, 0x7817, 0x11cf, 0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65) + + +// -- External audio related GUIDs --- + +// -- major types, Analog Audio + +// 0482DEE1-7817-11cf-8a03-00aa006ecb65 +OUR_GUID_ENTRY(MEDIATYPE_AnalogAudio, +0x482dee1, 0x7817, 0x11cf, 0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65) + +// -- Video analysis related GUIDs --- + +// -- format types used by VA -- H.264, captioning + +// {A4EFC024-873E-4da3-898B-474DDBD79FD0} +OUR_GUID_ENTRY(FORMAT_CAPTIONED_H264VIDEO, +0xa4efc024, 0x873e, 0x4da3, 0x89, 0x8b, 0x47, 0x4d, 0xdb, 0xd7, 0x9f, 0xd0) + +// -- media, media subtype, and format types, CC container + +// {50997A4A-E508-4054-A2B2-10FF0AC1A69A} +OUR_GUID_ENTRY(FORMAT_CC_CONTAINER, +0x50997a4a, 0xe508, 0x4054, 0xa2, 0xb2, 0x10, 0xff, 0xa, 0xc1, 0xa6, 0x9a) + +// {3ED9CB31-FD10-4ade-BCCC-FB9105D2F3EF} +OUR_GUID_ENTRY(CAPTION_FORMAT_ATSC, +0x3ed9cb31, 0xfd10, 0x4ade, 0xbc, 0xcc, 0xfb, 0x91, 0x5, 0xd2, 0xf3, 0xef) + +// {12230DB4-FF2A-447e-BB88-6841C416D068} +OUR_GUID_ENTRY(CAPTION_FORMAT_DVB, +0x12230db4, 0xff2a, 0x447e, 0xbb, 0x88, 0x68, 0x41, 0xc4, 0x16, 0xd0, 0x68) + +// {E9CA1CE7-915E-47be-9BB9-BF1D8A13A5EC} +OUR_GUID_ENTRY(CAPTION_FORMAT_DIRECTV, +0xe9ca1ce7, 0x915e, 0x47be, 0x9b, 0xb9, 0xbf, 0x1d, 0x8a, 0x13, 0xa5, 0xec) + +// {EBB1A262-1158-4b99-AE80-92AC776952C4} +OUR_GUID_ENTRY(CAPTION_FORMAT_ECHOSTAR, +0xebb1a262, 0x1158, 0x4b99, 0xae, 0x80, 0x92, 0xac, 0x77, 0x69, 0x52, 0xc4) + +// -- format types, MPEG-2 + +// {7AB2ADA2-81B6-4f14-B3C8-D0C486393B67} +OUR_GUID_ENTRY(FORMAT_CAPTIONED_MPEG2VIDEO, +0x7ab2ada2, 0x81b6, 0x4f14, 0xb3, 0xc8, 0xd0, 0xc4, 0x86, 0x39, 0x3b, 0x67) + +// +// DirectShow's include file based on ksmedia.h from WDM DDK +// +#include "ksuuids.h" + + +// -- Well known time format GUIDs --- + + +// 00000000-0000-0000-0000-000000000000 +OUR_GUID_ENTRY(TIME_FORMAT_NONE, +0L, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) + +// 7b785570-8c82-11cf-bc0c-00aa00ac74f6 +OUR_GUID_ENTRY(TIME_FORMAT_FRAME, +0x7b785570, 0x8c82, 0x11cf, 0xbc, 0xc, 0x0, 0xaa, 0x0, 0xac, 0x74, 0xf6) + +// 7b785571-8c82-11cf-bc0c-00aa00ac74f6 +OUR_GUID_ENTRY(TIME_FORMAT_BYTE, +0x7b785571, 0x8c82, 0x11cf, 0xbc, 0xc, 0x0, 0xaa, 0x0, 0xac, 0x74, 0xf6) + +// 7b785572-8c82-11cf-bc0c-00aa00ac74f6 +OUR_GUID_ENTRY(TIME_FORMAT_SAMPLE, +0x7b785572, 0x8c82, 0x11cf, 0xbc, 0xc, 0x0, 0xaa, 0x0, 0xac, 0x74, 0xf6) + +// 7b785573-8c82-11cf-bc0c-00aa00ac74f6 +OUR_GUID_ENTRY(TIME_FORMAT_FIELD, +0x7b785573, 0x8c82, 0x11cf, 0xbc, 0xc, 0x0, 0xaa, 0x0, 0xac, 0x74, 0xf6) + + +// 7b785574-8c82-11cf-bc0c-00aa00ac74f6 +OUR_GUID_ENTRY(TIME_FORMAT_MEDIA_TIME, +0x7b785574, 0x8c82, 0x11cf, 0xbc, 0xc, 0x0, 0xaa, 0x0, 0xac, 0x74, 0xf6) + + +// for IKsPropertySet + +// 9B00F101-1567-11d1-B3F1-00AA003761C5 +OUR_GUID_ENTRY(AMPROPSETID_Pin, +0x9b00f101, 0x1567, 0x11d1, 0xb3, 0xf1, 0x0, 0xaa, 0x0, 0x37, 0x61, 0xc5) + +// fb6c4281-0353-11d1-905f-0000c0cc16ba +OUR_GUID_ENTRY(PIN_CATEGORY_CAPTURE, +0xfb6c4281, 0x0353, 0x11d1, 0x90, 0x5f, 0x00, 0x00, 0xc0, 0xcc, 0x16, 0xba) + +// fb6c4282-0353-11d1-905f-0000c0cc16ba +OUR_GUID_ENTRY(PIN_CATEGORY_PREVIEW, +0xfb6c4282, 0x0353, 0x11d1, 0x90, 0x5f, 0x00, 0x00, 0xc0, 0xcc, 0x16, 0xba) + +// fb6c4283-0353-11d1-905f-0000c0cc16ba +OUR_GUID_ENTRY(PIN_CATEGORY_ANALOGVIDEOIN, +0xfb6c4283, 0x0353, 0x11d1, 0x90, 0x5f, 0x00, 0x00, 0xc0, 0xcc, 0x16, 0xba) + +// fb6c4284-0353-11d1-905f-0000c0cc16ba +OUR_GUID_ENTRY(PIN_CATEGORY_VBI, +0xfb6c4284, 0x0353, 0x11d1, 0x90, 0x5f, 0x00, 0x00, 0xc0, 0xcc, 0x16, 0xba) + +// fb6c4285-0353-11d1-905f-0000c0cc16ba +OUR_GUID_ENTRY(PIN_CATEGORY_VIDEOPORT, +0xfb6c4285, 0x0353, 0x11d1, 0x90, 0x5f, 0x00, 0x00, 0xc0, 0xcc, 0x16, 0xba) + +// fb6c4286-0353-11d1-905f-0000c0cc16ba +OUR_GUID_ENTRY(PIN_CATEGORY_NABTS, +0xfb6c4286, 0x0353, 0x11d1, 0x90, 0x5f, 0x00, 0x00, 0xc0, 0xcc, 0x16, 0xba) + +// fb6c4287-0353-11d1-905f-0000c0cc16ba +OUR_GUID_ENTRY(PIN_CATEGORY_EDS, +0xfb6c4287, 0x0353, 0x11d1, 0x90, 0x5f, 0x00, 0x00, 0xc0, 0xcc, 0x16, 0xba) + +// fb6c4288-0353-11d1-905f-0000c0cc16ba +OUR_GUID_ENTRY(PIN_CATEGORY_TELETEXT, +0xfb6c4288, 0x0353, 0x11d1, 0x90, 0x5f, 0x00, 0x00, 0xc0, 0xcc, 0x16, 0xba) + +// fb6c4289-0353-11d1-905f-0000c0cc16ba +OUR_GUID_ENTRY(PIN_CATEGORY_CC, +0xfb6c4289, 0x0353, 0x11d1, 0x90, 0x5f, 0x00, 0x00, 0xc0, 0xcc, 0x16, 0xba) + +// fb6c428a-0353-11d1-905f-0000c0cc16ba +OUR_GUID_ENTRY(PIN_CATEGORY_STILL, +0xfb6c428a, 0x0353, 0x11d1, 0x90, 0x5f, 0x00, 0x00, 0xc0, 0xcc, 0x16, 0xba) + +// fb6c428b-0353-11d1-905f-0000c0cc16ba +OUR_GUID_ENTRY(PIN_CATEGORY_TIMECODE, +0xfb6c428b, 0x0353, 0x11d1, 0x90, 0x5f, 0x00, 0x00, 0xc0, 0xcc, 0x16, 0xba) + +// fb6c428c-0353-11d1-905f-0000c0cc16ba +OUR_GUID_ENTRY(PIN_CATEGORY_VIDEOPORT_VBI, +0xfb6c428c, 0x0353, 0x11d1, 0x90, 0x5f, 0x00, 0x00, 0xc0, 0xcc, 0x16, 0xba) + + +// the following special GUIDS are used by ICaptureGraphBuilder::FindInterface + +// {AC798BE0-98E3-11d1-B3F1-00AA003761C5} +OUR_GUID_ENTRY(LOOK_UPSTREAM_ONLY, +0xac798be0, 0x98e3, 0x11d1, 0xb3, 0xf1, 0x0, 0xaa, 0x0, 0x37, 0x61, 0xc5) + +// {AC798BE1-98E3-11d1-B3F1-00AA003761C5} +OUR_GUID_ENTRY(LOOK_DOWNSTREAM_ONLY, +0xac798be1, 0x98e3, 0x11d1, 0xb3, 0xf1, 0x0, 0xaa, 0x0, 0x37, 0x61, 0xc5) + +// ------------------------------------------------------------------------- +// KSProxy GUIDS +// ------------------------------------------------------------------------- + +// {266EEE41-6C63-11cf-8A03-00AA006ECB65} +OUR_GUID_ENTRY(CLSID_TVTunerFilterPropertyPage, +0x266eee41, 0x6c63, 0x11cf, 0x8a, 0x3, 0x0, 0xaa, 0x0, 0x6e, 0xcb, 0x65) + +// {71F96461-78F3-11d0-A18C-00A0C9118956} +OUR_GUID_ENTRY(CLSID_CrossbarFilterPropertyPage, +0x71f96461, 0x78f3, 0x11d0, 0xa1, 0x8c, 0x0, 0xa0, 0xc9, 0x11, 0x89, 0x56) + +// {71F96463-78F3-11d0-A18C-00A0C9118956} +OUR_GUID_ENTRY(CLSID_TVAudioFilterPropertyPage, +0x71f96463, 0x78f3, 0x11d0, 0xa1, 0x8c, 0x0, 0xa0, 0xc9, 0x11, 0x89, 0x56) + +// {71F96464-78F3-11d0-A18C-00A0C9118956} +OUR_GUID_ENTRY(CLSID_VideoProcAmpPropertyPage, +0x71f96464, 0x78f3, 0x11d0, 0xa1, 0x8c, 0x0, 0xa0, 0xc9, 0x11, 0x89, 0x56) + +// {71F96465-78F3-11d0-A18C-00A0C9118956} +OUR_GUID_ENTRY(CLSID_CameraControlPropertyPage, +0x71f96465, 0x78f3, 0x11d0, 0xa1, 0x8c, 0x0, 0xa0, 0xc9, 0x11, 0x89, 0x56) + +// {71F96466-78F3-11d0-A18C-00A0C9118956} +OUR_GUID_ENTRY(CLSID_AnalogVideoDecoderPropertyPage, +0x71f96466, 0x78f3, 0x11d0, 0xa1, 0x8c, 0x0, 0xa0, 0xc9, 0x11, 0x89, 0x56) + +// {71F96467-78F3-11d0-A18C-00A0C9118956} +OUR_GUID_ENTRY(CLSID_VideoStreamConfigPropertyPage, +0x71f96467, 0x78f3, 0x11d0, 0xa1, 0x8c, 0x0, 0xa0, 0xc9, 0x11, 0x89, 0x56) + +// {37E92A92-D9AA-11d2-BF84-8EF2B1555AED} Audio Renderer Advanced Property Page +OUR_GUID_ENTRY(CLSID_AudioRendererAdvancedProperties, +0x37e92a92, 0xd9aa, 0x11d2, 0xbf, 0x84, 0x8e, 0xf2, 0xb1, 0x55, 0x5a, 0xed) + + +// ------------------------------------------------------------------------- +// VMR GUIDS +// ------------------------------------------------------------------------- + +// {B87BEB7B-8D29-423f-AE4D-6582C10175AC} +OUR_GUID_ENTRY(CLSID_VideoMixingRenderer, +0xB87BEB7B, 0x8D29, 0x423f, 0xAE, 0x4D, 0x65, 0x82, 0xC1, 0x01, 0x75, 0xAC) + +// {6BC1CFFA-8FC1-4261-AC22-CFB4CC38DB50} +OUR_GUID_ENTRY(CLSID_VideoRendererDefault, +0x6BC1CFFA, 0x8FC1, 0x4261, 0xAC, 0x22, 0xCF, 0xB4, 0xCC, 0x38, 0xDB, 0x50) + +// {99d54f63-1a69-41ae-aa4d-c976eb3f0713} +OUR_GUID_ENTRY(CLSID_AllocPresenter, +0x99d54f63, 0x1a69, 0x41ae, 0xaa, 0x4d, 0xc9, 0x76, 0xeb, 0x3f, 0x07, 0x13) + +// {4444ac9e-242e-471b-a3c7-45dcd46352bc} +OUR_GUID_ENTRY(CLSID_AllocPresenterDDXclMode, +0x4444ac9e, 0x242e, 0x471b, 0xa3, 0xc7, 0x45, 0xdc, 0xd4, 0x63, 0x52, 0xbc) + +// {6f26a6cd-967b-47fd-874a-7aed2c9d25a2} +OUR_GUID_ENTRY(CLSID_VideoPortManager, +0x6f26a6cd, 0x967b, 0x47fd, 0x87, 0x4a, 0x7a, 0xed, 0x2c, 0x9d, 0x25, 0xa2) + + +// ------------------------------------------------------------------------- +// VMR GUIDS for DX9 +// ------------------------------------------------------------------------- + +// {51b4abf3-748f-4e3b-a276-c828330e926a} +OUR_GUID_ENTRY(CLSID_VideoMixingRenderer9, +0x51b4abf3, 0x748f, 0x4e3b, 0xa2, 0x76, 0xc8, 0x28, 0x33, 0x0e, 0x92, 0x6a) + + +// ------------------------------------------------------------------------- +// EVR GUIDS +// ------------------------------------------------------------------------- + +// {FA10746C-9B63-4b6c-BC49-FC300EA5F256} +OUR_GUID_ENTRY(CLSID_EnhancedVideoRenderer, +0xfa10746c, 0x9b63, 0x4b6c, 0xbc, 0x49, 0xfc, 0x30, 0xe, 0xa5, 0xf2, 0x56) + +#ifndef __EVR_GUIDS__ +#define __EVR_GUIDS__ + +// {E474E05A-AB65-4f6a-827C-218B1BAAF31F} +OUR_GUID_ENTRY(CLSID_MFVideoMixer9, +0xE474E05A, 0xAB65, 0x4f6a, 0x82, 0x7C, 0x21, 0x8B, 0x1B, 0xAA, 0xF3, 0x1F) + +// {98455561-5136-4d28-AB08-4CEE40EA2781} +OUR_GUID_ENTRY(CLSID_MFVideoPresenter9, +0x98455561, 0x5136, 0x4d28, 0xab, 0x8, 0x4c, 0xee, 0x40, 0xea, 0x27, 0x81) + +#endif // __EVR_GUIDS__ + +// {a0a7a57b-59b2-4919-a694-add0a526c373} +OUR_GUID_ENTRY(CLSID_EVRTearlessWindowPresenter9, +0xa0a7a57b, 0x59b2, 0x4919, 0xa6, 0x94, 0xad, 0xd0, 0xa5, 0x26, 0xc3, 0x73) + +// {62079164-233b-41f8-a80f-f01705f514a8} +OUR_GUID_ENTRY(CLSID_EVRPlaybackPipelineOptimizer, +0x62079164, 0x233b, 0x41f8, 0xa8, 0x0f, 0xf0, 0x17, 0x05, 0xf5, 0x14, 0xa8) + +// {e447df01-10ca-4d17-b17e-6a840f8a3a4c} +// {e447df02-10ca-4d17-b17e-6a840f8a3a4c} +// {e447df03-10ca-4d17-b17e-6a840f8a3a4c} +// {e447df04-10ca-4d17-b17e-6a840f8a3a4c} +// {e447df05-10ca-4d17-b17e-6a840f8a3a4c} +// {e447df06-10ca-4d17-b17e-6a840f8a3a4c} +// {e447df07-10ca-4d17-b17e-6a840f8a3a4c} +// {e447df08-10ca-4d17-b17e-6a840f8a3a4c} +// {e447df09-10ca-4d17-b17e-6a840f8a3a4c} +// {e447df0a-10ca-4d17-b17e-6a840f8a3a4c} +OUR_GUID_ENTRY( EVRConfig_ForceBob, 0xe447df01,0x10ca,0x4d17,0xb1, 0x7e, 0x6a, 0x84, 0x0f, 0x8a, 0x3a, 0x4c) +OUR_GUID_ENTRY( EVRConfig_AllowDropToBob, 0xe447df02,0x10ca,0x4d17,0xb1, 0x7e, 0x6a, 0x84, 0x0f, 0x8a, 0x3a, 0x4c) +OUR_GUID_ENTRY( EVRConfig_ForceThrottle, 0xe447df03,0x10ca,0x4d17,0xb1, 0x7e, 0x6a, 0x84, 0x0f, 0x8a, 0x3a, 0x4c) +OUR_GUID_ENTRY( EVRConfig_AllowDropToThrottle, 0xe447df04,0x10ca,0x4d17,0xb1, 0x7e, 0x6a, 0x84, 0x0f, 0x8a, 0x3a, 0x4c) +OUR_GUID_ENTRY( EVRConfig_ForceHalfInterlace, 0xe447df05,0x10ca,0x4d17,0xb1, 0x7e, 0x6a, 0x84, 0x0f, 0x8a, 0x3a, 0x4c) +OUR_GUID_ENTRY( EVRConfig_AllowDropToHalfInterlace,0xe447df06,0x10ca,0x4d17,0xb1, 0x7e, 0x6a, 0x84, 0x0f, 0x8a, 0x3a, 0x4c) +OUR_GUID_ENTRY( EVRConfig_ForceScaling, 0xe447df07,0x10ca,0x4d17,0xb1, 0x7e, 0x6a, 0x84, 0x0f, 0x8a, 0x3a, 0x4c) +OUR_GUID_ENTRY( EVRConfig_AllowScaling, 0xe447df08,0x10ca,0x4d17,0xb1, 0x7e, 0x6a, 0x84, 0x0f, 0x8a, 0x3a, 0x4c) +OUR_GUID_ENTRY( EVRConfig_ForceBatching, 0xe447df09,0x10ca,0x4d17,0xb1, 0x7e, 0x6a, 0x84, 0x0f, 0x8a, 0x3a, 0x4c) +OUR_GUID_ENTRY( EVRConfig_AllowBatching, 0xe447df0a,0x10ca,0x4d17,0xb1, 0x7e, 0x6a, 0x84, 0x0f, 0x8a, 0x3a, 0x4c) + + +// ------------------------------------------------------------------------- +// BDA Network Provider GUIDS +// ------------------------------------------------------------------------- + +// This is the GUID for the generic NP which would replace ATSC, DVBT, DVBS +// and DVBC NP. All the other GUIDs are still kept for backward compatibility +// {B2F3A67C-29DA-4c78-8831-091ED509A475} +OUR_GUID_ENTRY(CLSID_NetworkProvider, +0xb2f3a67c, 0x29da, 0x4c78, 0x88, 0x31, 0x9, 0x1e, 0xd5, 0x9, 0xa4, 0x75) + +// {0DAD2FDD-5FD7-11D3-8F50-00C04F7971E2} +OUR_GUID_ENTRY(CLSID_ATSCNetworkProvider, +0x0dad2fdd, 0x5fd7, 0x11d3, 0x8f, 0x50, 0x00, 0xc0, 0x4f, 0x79, 0x71, 0xe2) + +// {E3444D16-5AC4-4386-88DF-13FD230E1DDA} +OUR_GUID_ENTRY(CLSID_ATSCNetworkPropertyPage, +0xe3444d16, 0x5ac4, 0x4386, 0x88, 0xdf, 0x13, 0xfd, 0x23, 0x0e, 0x1d, 0xda) + +// {FA4B375A-45B4-4d45-8440-263957B11623} +OUR_GUID_ENTRY(CLSID_DVBSNetworkProvider, +0xfa4b375a, 0x45b4, 0x4d45, 0x84, 0x40, 0x26, 0x39, 0x57, 0xb1, 0x16, 0x23) + +// {216C62DF-6D7F-4e9a-8571-05F14EDB766A} +OUR_GUID_ENTRY(CLSID_DVBTNetworkProvider, +0x216c62df, 0x6d7f, 0x4e9a, 0x85, 0x71, 0x5, 0xf1, 0x4e, 0xdb, 0x76, 0x6a) + +// {DC0C0FE7-0485-4266-B93F-68FBF80ED834} +OUR_GUID_ENTRY(CLSID_DVBCNetworkProvider, +0xdc0c0fe7, 0x485, 0x4266, 0xb9, 0x3f, 0x68, 0xfb, 0xf8, 0xe, 0xd8, 0x34) + +// ------------------------------------------------------------------------- +// attribute GUIDs +// ------------------------------------------------------------------------- + +// {EB7836CA-14FF-4919-BCE7-3AF12319E50C} +OUR_GUID_ENTRY(DSATTRIB_UDCRTag, +0xEB7836CA, 0x14FF, 0x4919, 0xbc, 0xe7, 0x3a, 0xf1, 0x23, 0x19, 0xe5, 0x0c) + +// {2F5BAE02-7B8F-4f60-82D6-E4EA2F1F4C99} +OUR_GUID_ENTRY(DSATTRIB_PicSampleSeq, +0x2f5bae02, 0x7b8f, 0x4f60, 0x82, 0xd6, 0xe4, 0xea, 0x2f, 0x1f, 0x4c, 0x99) + +// {5A5F08CA-55C2-4033-92AB-55DB8F781226} +OUR_GUID_ENTRY(DSATTRIB_OptionalVideoAttributes, +0x5A5F08CA, 0x55C2, 0x4033, 0x92, 0xAB, 0x55, 0xDB, 0x8F, 0x78, 0x12, 0x26) + +// {e7e050fb-dd5d-40dd-9915-35dcb81bdc8a} +OUR_GUID_ENTRY(DSATTRIB_CC_CONTAINER_INFO, +0xe7e050fb, 0xdd5d, 0x40dd, 0x99, 0x15, 0x35, 0xDC, 0xB8, 0x1B, 0xDC, 0x8a) + +// {B622F612-47AD-4671-AD6C-05A98E65DE3A} +OUR_GUID_ENTRY(DSATTRIB_TRANSPORT_PROPERTIES, +0xb622f612, 0x47ad, 0x4671, 0xad, 0x6c, 0x5, 0xa9, 0x8e, 0x65, 0xde, 0x3a) + +// {e0b56679-12b9-43cc-b7df-578caa5a7b63} +OUR_GUID_ENTRY(DSATTRIB_PBDATAG_ATTRIBUTE, +0xe0b56679, 0x12b9, 0x43cc, 0xb7, 0xdf, 0x57, 0x8c, 0xaa, 0x5a, 0x7b, 0x63) + +// {0c1a5614-30cd-4f40-bcbf-d03e52306207} +OUR_GUID_ENTRY( DSATTRIB_CAPTURE_STREAMTIME, +0x0c1a5614, 0x30cd, 0x4f40, 0xbc, 0xbf, 0xd0, 0x3e, 0x52, 0x30, 0x62, 0x07) + +// {5FB5673B-0A2A-4565-827B-6853FD75E611} DSATTRIB_DSHOW_STREAM_DESC +OUR_GUID_ENTRY(DSATTRIB_DSHOW_STREAM_DESC, +0x5fb5673b, 0xa2a, 0x4565, 0x82, 0x7b, 0x68, 0x53, 0xfd, 0x75, 0xe6, 0x11) + +// {892CD111-72F3-411d-8B91-A9E9123AC29A} +OUR_GUID_ENTRY(DSATTRIB_SAMPLE_LIVE_STREAM_TIME, +0x892cd111, 0x72f3, 0x411d, 0x8b, 0x91, 0xa9, 0xe9, 0x12, 0x3a, 0xc2, 0x9a) + +// UUID for supported UDRI TAG tables +OUR_GUID_ENTRY( UUID_UdriTagTables, +0xe1b98d74, 0x9778, 0x4878, 0xb6, 0x64, 0xeb, 0x20, 0x20, 0x36, 0x4d, 0x88) + +// UUID for supported WMDRM TAG tables +OUR_GUID_ENTRY( UUID_WMDRMTagTables, +0x5DCD1101, 0x9263, 0x45bb, 0xa4, 0xd5, 0xc4, 0x15, 0xab, 0x8c, 0x58, 0x9c) + +// ------------------------------------------------------------------------- +// TVE Receiver filter guids +// ------------------------------------------------------------------------- + +// The CLSID used by the TVE Receiver filter +// {05500280-FAA5-4DF9-8246-BFC23AC5CEA8} +OUR_GUID_ENTRY(CLSID_DShowTVEFilter, +0x05500280, 0xFAA5, 0x4DF9, 0x82, 0x46, 0xBF, 0xC2, 0x3A, 0xC5, 0xCE, 0xA8) + +// {05500281-FAA5-4DF9-8246-BFC23AC5CEA8} +OUR_GUID_ENTRY(CLSID_TVEFilterTuneProperties, +0x05500281, 0xFAA5, 0x4DF9, 0x82, 0x46, 0xBF, 0xC2, 0x3A, 0xC5, 0xCE, 0xA8) + + +// {05500282-FAA5-4DF9-8246-BFC23AC5CEA8} +OUR_GUID_ENTRY(CLSID_TVEFilterCCProperties, +0x05500282, 0xFAA5, 0x4DF9, 0x82, 0x46, 0xBF, 0xC2, 0x3A, 0xC5, 0xCE, 0xA8) + +// {05500283-FAA5-4DF9-8246-BFC23AC5CEA8} +OUR_GUID_ENTRY(CLSID_TVEFilterStatsProperties, +0x05500283, 0xFAA5, 0x4DF9, 0x82, 0x46, 0xBF, 0xC2, 0x3A, 0xC5, 0xCE, 0xA8) + +// ------------------------------------------------------------------------- +// Defined ENCAPI parameter GUIDs +// ------------------------------------------------------------------------- + +// The CLSID for the original IVideoEncoder proxy plug-in +// {B43C4EEC-8C32-4791-9102-508ADA5EE8E7} +OUR_GUID_ENTRY(CLSID_IVideoEncoderProxy, +0xb43c4eec, 0x8c32, 0x4791, 0x91, 0x2, 0x50, 0x8a, 0xda, 0x5e, 0xe8, 0xe7) + +// The CLSID for the ICodecAPI proxy plug-in +// {7ff0997a-1999-4286-a73c-622b8814e7eb} +OUR_GUID_ENTRY(CLSID_ICodecAPIProxy, +0x7ff0997a, 0x1999, 0x4286, 0xa7, 0x3c, 0x62, 0x2b, 0x88, 0x14, 0xe7, 0xeb ) + +// The CLSID for the combination ICodecAPI/IVideoEncoder proxy plug-in +// {b05dabd9-56e5-4fdc-afa4-8a47e91f1c9c} +OUR_GUID_ENTRY(CLSID_IVideoEncoderCodecAPIProxy, +0xb05dabd9, 0x56e5, 0x4fdc, 0xaf, 0xa4, 0x8a, 0x47, 0xe9, 0x1f, 0x1c, 0x9c ) + +#ifndef __ENCODER_API_GUIDS__ +#define __ENCODER_API_GUIDS__ + +// {49CC4C43-CA83-4ad4-A9AF-F3696AF666DF} +OUR_GUID_ENTRY(ENCAPIPARAM_BITRATE, +0x49cc4c43, 0xca83, 0x4ad4, 0xa9, 0xaf, 0xf3, 0x69, 0x6a, 0xf6, 0x66, 0xdf) + +// {703F16A9-3D48-44a1-B077-018DFF915D19} +OUR_GUID_ENTRY(ENCAPIPARAM_PEAK_BITRATE, +0x703f16a9, 0x3d48, 0x44a1, 0xb0, 0x77, 0x1, 0x8d, 0xff, 0x91, 0x5d, 0x19) + +// {EE5FB25C-C713-40d1-9D58-C0D7241E250F} +OUR_GUID_ENTRY(ENCAPIPARAM_BITRATE_MODE, +0xee5fb25c, 0xc713, 0x40d1, 0x9d, 0x58, 0xc0, 0xd7, 0x24, 0x1e, 0x25, 0xf) + +// {0C0171DB-FEFC-4af7-9991-A5657C191CD1} +OUR_GUID_ENTRY(ENCAPIPARAM_SAP_MODE, +0xc0171db, 0xfefc, 0x4af7, 0x99, 0x91, 0xa5, 0x65, 0x7c, 0x19, 0x1c, 0xd1) + +// for kernel control + +// {62b12acf-f6b0-47d9-9456-96f22c4e0b9d} +OUR_GUID_ENTRY(CODECAPI_CHANGELISTS, +0x62b12acf, 0xf6b0, 0x47d9, 0x94, 0x56, 0x96, 0xf2, 0x2c, 0x4e, 0x0b, 0x9d) + +// {7112e8e1-3d03-47ef-8e60-03f1cf537301 } +OUR_GUID_ENTRY(CODECAPI_VIDEO_ENCODER, +0x7112e8e1, 0x3d03, 0x47ef, 0x8e, 0x60, 0x03, 0xf1, 0xcf, 0x53, 0x73, 0x01) + +// {b9d19a3e-f897-429c-bc46-8138b7272b2d } +OUR_GUID_ENTRY(CODECAPI_AUDIO_ENCODER, +0xb9d19a3e, 0xf897, 0x429c, 0xbc, 0x46, 0x81, 0x38, 0xb7, 0x27, 0x2b, 0x2d) + +// {6c5e6a7c-acf8-4f55-a999-1a628109051b } +OUR_GUID_ENTRY(CODECAPI_SETALLDEFAULTS, +0x6c5e6a7c, 0xacf8, 0x4f55, 0xa9, 0x99, 0x1a, 0x62, 0x81, 0x09, 0x05, 0x1b) + +// {6a577e92-83e1-4113-adc2-4fcec32f83a1 } +OUR_GUID_ENTRY(CODECAPI_ALLSETTINGS, +0x6a577e92, 0x83e1, 0x4113, 0xad, 0xc2, 0x4f, 0xce, 0xc3, 0x2f, 0x83, 0xa1) + +// {0581af97-7693-4dbd-9dca-3f9ebd6585a1 } +OUR_GUID_ENTRY(CODECAPI_SUPPORTSEVENTS, +0x0581af97, 0x7693, 0x4dbd, 0x9d, 0xca, 0x3f, 0x9e, 0xbd, 0x65, 0x85, 0xa1 ) + +// {1cb14e83-7d72-4657-83fd-47a2c5b9d13d } +OUR_GUID_ENTRY(CODECAPI_CURRENTCHANGELIST, +0x1cb14e83, 0x7d72, 0x4657, 0x83, 0xfd, 0x47, 0xa2, 0xc5, 0xb9, 0xd1, 0x3d ) + +// {1f26a602-2b5c-4b63-b8e8-9ea5c1a7dc2e} +OUR_GUID_ENTRY(CLSID_SBE2MediaTypeProfile, +0x1f26a602, 0x2b5c, 0x4b63, 0xb8, 0xe8, 0x9e, 0xa5, 0xc1, 0xa7, 0xdc, 0x2e ) + +// {3E458037-0CA6-41aa-A594-2AA6C02D709B} +OUR_GUID_ENTRY(CLSID_SBE2FileScan, +0x3e458037, 0xca6, 0x41aa, 0xa5, 0x94, 0x2a, 0xa6, 0xc0, 0x2d, 0x70, 0x9b) ; + +// When generating strmiids.lib, include codecapi definitions +#ifdef INITGUID +#define UUID_GEN +#include +#endif + +#endif // __ENCODER_API_GUIDS__ + +// ----------------------------------------------- +// Used for decoders that exposing ICodecAPI +// ----------------------------------------------- +OUR_GUID_ENTRY(CODECAPI_AVDecMmcssClass, +0xe0ad4828, 0xdf66, 0x4893, 0x9f, 0x33, 0x78, 0x8a, 0xa4, 0xec, 0x40, 0x82) + +#undef OUR_GUID_ENTRY + +#endif /* WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) */ +#pragma endregion + diff --git a/inc/zlib.h b/inc/zlib.h new file mode 100644 index 0000000..32c2ce0 --- /dev/null +++ b/inc/zlib.h @@ -0,0 +1,1916 @@ +/* zlib.h -- interface of the 'zlib' general purpose compression library + version 1.2.11, January 15th, 2017 + + Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. + + Jean-loup Gailly Mark Adler + jloup@gzip.org madler@alumni.caltech.edu + + + The data format used by the zlib library is described by RFCs (Request for + Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950 + (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format). +*/ + +#ifndef ZLIB_H +#define ZLIB_H + +#include "zconf.h" + +#include +#undef ZEXTERN +#define ZEXTERN Q_CORE_EXPORT + +#ifdef __cplusplus +extern "C" { +#endif + +#define ZLIB_VERSION "1.2.11 (Qt)" +#define ZLIB_VERNUM 0x12b0f +#define ZLIB_VER_MAJOR 1 +#define ZLIB_VER_MINOR 2 +#define ZLIB_VER_REVISION 11 +#define ZLIB_VER_SUBREVISION 0 + +/* + The 'zlib' compression library provides in-memory compression and + decompression functions, including integrity checks of the uncompressed data. + This version of the library supports only one compression method (deflation) + but other algorithms will be added later and will have the same stream + interface. + + Compression can be done in a single step if the buffers are large enough, + or can be done by repeated calls of the compression function. In the latter + case, the application must provide more input and/or consume the output + (providing more output space) before each call. + + The compressed data format used by default by the in-memory functions is + the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped + around a deflate stream, which is itself documented in RFC 1951. + + The library also supports reading and writing files in gzip (.gz) format + with an interface similar to that of stdio using the functions that start + with "gz". The gzip format is different from the zlib format. gzip is a + gzip wrapper, documented in RFC 1952, wrapped around a deflate stream. + + This library can optionally read and write gzip and raw deflate streams in + memory as well. + + The zlib format was designed to be compact and fast for use in memory + and on communications channels. The gzip format was designed for single- + file compression on file systems, has a larger header than zlib to maintain + directory information, and uses a different, slower check method than zlib. + + The library does not install any signal handler. The decoder checks + the consistency of the compressed data, so the library should never crash + even in the case of corrupted input. +*/ + +typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size)); +typedef void (*free_func) OF((voidpf opaque, voidpf address)); + +struct internal_state; + +typedef struct z_stream_s { + z_const Bytef *next_in; /* next input byte */ + uInt avail_in; /* number of bytes available at next_in */ + uLong total_in; /* total number of input bytes read so far */ + + Bytef *next_out; /* next output byte will go here */ + uInt avail_out; /* remaining free space at next_out */ + uLong total_out; /* total number of bytes output so far */ + + z_const char *msg; /* last error message, NULL if no error */ + struct internal_state FAR *state; /* not visible by applications */ + + alloc_func zalloc; /* used to allocate the internal state */ + free_func zfree; /* used to free the internal state */ + voidpf opaque; /* private data object passed to zalloc and zfree */ + + int data_type; /* best guess about the data type: binary or text + for deflate, or the decoding state for inflate */ + uLong adler; /* Adler-32 or CRC-32 value of the uncompressed data */ + uLong reserved; /* reserved for future use */ +} z_stream; + +typedef z_stream FAR *z_streamp; + +/* + gzip header information passed to and from zlib routines. See RFC 1952 + for more details on the meanings of these fields. +*/ +typedef struct gz_header_s { + int text; /* true if compressed data believed to be text */ + uLong time; /* modification time */ + int xflags; /* extra flags (not used when writing a gzip file) */ + int os; /* operating system */ + Bytef *extra; /* pointer to extra field or Z_NULL if none */ + uInt extra_len; /* extra field length (valid if extra != Z_NULL) */ + uInt extra_max; /* space at extra (only when reading header) */ + Bytef *name; /* pointer to zero-terminated file name or Z_NULL */ + uInt name_max; /* space at name (only when reading header) */ + Bytef *comment; /* pointer to zero-terminated comment or Z_NULL */ + uInt comm_max; /* space at comment (only when reading header) */ + int hcrc; /* true if there was or will be a header crc */ + int done; /* true when done reading gzip header (not used + when writing a gzip file) */ +} gz_header; + +typedef gz_header FAR *gz_headerp; + +/* + The application must update next_in and avail_in when avail_in has dropped + to zero. It must update next_out and avail_out when avail_out has dropped + to zero. The application must initialize zalloc, zfree and opaque before + calling the init function. All other fields are set by the compression + library and must not be updated by the application. + + The opaque value provided by the application will be passed as the first + parameter for calls of zalloc and zfree. This can be useful for custom + memory management. The compression library attaches no meaning to the + opaque value. + + zalloc must return Z_NULL if there is not enough memory for the object. + If zlib is used in a multi-threaded application, zalloc and zfree must be + thread safe. In that case, zlib is thread-safe. When zalloc and zfree are + Z_NULL on entry to the initialization function, they are set to internal + routines that use the standard library functions malloc() and free(). + + On 16-bit systems, the functions zalloc and zfree must be able to allocate + exactly 65536 bytes, but will not be required to allocate more than this if + the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS, pointers + returned by zalloc for objects of exactly 65536 bytes *must* have their + offset normalized to zero. The default allocation function provided by this + library ensures this (see zutil.c). To reduce memory requirements and avoid + any allocation of 64K objects, at the expense of compression ratio, compile + the library with -DMAX_WBITS=14 (see zconf.h). + + The fields total_in and total_out can be used for statistics or progress + reports. After compression, total_in holds the total size of the + uncompressed data and may be saved for use by the decompressor (particularly + if the decompressor wants to decompress everything in a single step). +*/ + + /* constants */ + +#define Z_NO_FLUSH 0 +#define Z_PARTIAL_FLUSH 1 +#define Z_SYNC_FLUSH 2 +#define Z_FULL_FLUSH 3 +#define Z_FINISH 4 +#define Z_BLOCK 5 +#define Z_TREES 6 +/* Allowed flush values; see deflate() and inflate() below for details */ + +#define Z_OK 0 +#define Z_STREAM_END 1 +#define Z_NEED_DICT 2 +#define Z_ERRNO (-1) +#define Z_STREAM_ERROR (-2) +#define Z_DATA_ERROR (-3) +#define Z_MEM_ERROR (-4) +#define Z_BUF_ERROR (-5) +#define Z_VERSION_ERROR (-6) +/* Return codes for the compression/decompression functions. Negative values + * are errors, positive values are used for special but normal events. + */ + +#define Z_NO_COMPRESSION 0 +#define Z_BEST_SPEED 1 +#define Z_BEST_COMPRESSION 9 +#define Z_DEFAULT_COMPRESSION (-1) +/* compression levels */ + +#define Z_FILTERED 1 +#define Z_HUFFMAN_ONLY 2 +#define Z_RLE 3 +#define Z_FIXED 4 +#define Z_DEFAULT_STRATEGY 0 +/* compression strategy; see deflateInit2() below for details */ + +#define Z_BINARY 0 +#define Z_TEXT 1 +#define Z_ASCII Z_TEXT /* for compatibility with 1.2.2 and earlier */ +#define Z_UNKNOWN 2 +/* Possible values of the data_type field for deflate() */ + +#define Z_DEFLATED 8 +/* The deflate compression method (the only one supported in this version) */ + +#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */ + +#define zlib_version zlibVersion() +/* for compatibility with versions < 1.0.2 */ + + + /* basic functions */ + +ZEXTERN const char * ZEXPORT zlibVersion OF((void)); +/* The application can compare zlibVersion and ZLIB_VERSION for consistency. + If the first character differs, the library code actually used is not + compatible with the zlib.h header file used by the application. This check + is automatically made by deflateInit and inflateInit. + */ + +/* +ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level)); + + Initializes the internal stream state for compression. The fields + zalloc, zfree and opaque must be initialized before by the caller. If + zalloc and zfree are set to Z_NULL, deflateInit updates them to use default + allocation functions. + + The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9: + 1 gives best speed, 9 gives best compression, 0 gives no compression at all + (the input data is simply copied a block at a time). Z_DEFAULT_COMPRESSION + requests a default compromise between speed and compression (currently + equivalent to level 6). + + deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_STREAM_ERROR if level is not a valid compression level, or + Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible + with the version assumed by the caller (ZLIB_VERSION). msg is set to null + if there is no error message. deflateInit does not perform any compression: + this will be done by deflate(). +*/ + + +ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush)); +/* + deflate compresses as much data as possible, and stops when the input + buffer becomes empty or the output buffer becomes full. It may introduce + some output latency (reading input without producing any output) except when + forced to flush. + + The detailed semantics are as follows. deflate performs one or both of the + following actions: + + - Compress more input starting at next_in and update next_in and avail_in + accordingly. If not all input can be processed (because there is not + enough room in the output buffer), next_in and avail_in are updated and + processing will resume at this point for the next call of deflate(). + + - Generate more output starting at next_out and update next_out and avail_out + accordingly. This action is forced if the parameter flush is non zero. + Forcing flush frequently degrades the compression ratio, so this parameter + should be set only when necessary. Some output may be provided even if + flush is zero. + + Before the call of deflate(), the application should ensure that at least + one of the actions is possible, by providing more input and/or consuming more + output, and updating avail_in or avail_out accordingly; avail_out should + never be zero before the call. The application can consume the compressed + output when it wants, for example when the output buffer is full (avail_out + == 0), or after each call of deflate(). If deflate returns Z_OK and with + zero avail_out, it must be called again after making room in the output + buffer because there might be more output pending. See deflatePending(), + which can be used if desired to determine whether or not there is more ouput + in that case. + + Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to + decide how much data to accumulate before producing output, in order to + maximize compression. + + If the parameter flush is set to Z_SYNC_FLUSH, all pending output is + flushed to the output buffer and the output is aligned on a byte boundary, so + that the decompressor can get all input data available so far. (In + particular avail_in is zero after the call if enough output space has been + provided before the call.) Flushing may degrade compression for some + compression algorithms and so it should be used only when necessary. This + completes the current deflate block and follows it with an empty stored block + that is three bits plus filler bits to the next byte, followed by four bytes + (00 00 ff ff). + + If flush is set to Z_PARTIAL_FLUSH, all pending output is flushed to the + output buffer, but the output is not aligned to a byte boundary. All of the + input data so far will be available to the decompressor, as for Z_SYNC_FLUSH. + This completes the current deflate block and follows it with an empty fixed + codes block that is 10 bits long. This assures that enough bytes are output + in order for the decompressor to finish the block before the empty fixed + codes block. + + If flush is set to Z_BLOCK, a deflate block is completed and emitted, as + for Z_SYNC_FLUSH, but the output is not aligned on a byte boundary, and up to + seven bits of the current block are held to be written as the next byte after + the next deflate block is completed. In this case, the decompressor may not + be provided enough bits at this point in order to complete decompression of + the data provided so far to the compressor. It may need to wait for the next + block to be emitted. This is for advanced applications that need to control + the emission of deflate blocks. + + If flush is set to Z_FULL_FLUSH, all output is flushed as with + Z_SYNC_FLUSH, and the compression state is reset so that decompression can + restart from this point if previous compressed data has been damaged or if + random access is desired. Using Z_FULL_FLUSH too often can seriously degrade + compression. + + If deflate returns with avail_out == 0, this function must be called again + with the same value of the flush parameter and more output space (updated + avail_out), until the flush is complete (deflate returns with non-zero + avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that + avail_out is greater than six to avoid repeated flush markers due to + avail_out == 0 on return. + + If the parameter flush is set to Z_FINISH, pending input is processed, + pending output is flushed and deflate returns with Z_STREAM_END if there was + enough output space. If deflate returns with Z_OK or Z_BUF_ERROR, this + function must be called again with Z_FINISH and more output space (updated + avail_out) but no more input data, until it returns with Z_STREAM_END or an + error. After deflate has returned Z_STREAM_END, the only possible operations + on the stream are deflateReset or deflateEnd. + + Z_FINISH can be used in the first deflate call after deflateInit if all the + compression is to be done in a single step. In order to complete in one + call, avail_out must be at least the value returned by deflateBound (see + below). Then deflate is guaranteed to return Z_STREAM_END. If not enough + output space is provided, deflate will not return Z_STREAM_END, and it must + be called again as described above. + + deflate() sets strm->adler to the Adler-32 checksum of all input read + so far (that is, total_in bytes). If a gzip stream is being generated, then + strm->adler will be the CRC-32 checksum of the input read so far. (See + deflateInit2 below.) + + deflate() may update strm->data_type if it can make a good guess about + the input data type (Z_BINARY or Z_TEXT). If in doubt, the data is + considered binary. This field is only for information purposes and does not + affect the compression algorithm in any manner. + + deflate() returns Z_OK if some progress has been made (more input + processed or more output produced), Z_STREAM_END if all input has been + consumed and all output has been produced (only when flush is set to + Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example + if next_in or next_out was Z_NULL or the state was inadvertently written over + by the application), or Z_BUF_ERROR if no progress is possible (for example + avail_in or avail_out was zero). Note that Z_BUF_ERROR is not fatal, and + deflate() can be called again with more input and more output space to + continue compressing. +*/ + + +ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm)); +/* + All dynamically allocated data structures for this stream are freed. + This function discards any unprocessed input and does not flush any pending + output. + + deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the + stream state was inconsistent, Z_DATA_ERROR if the stream was freed + prematurely (some input or output was discarded). In the error case, msg + may be set but then points to a static string (which must not be + deallocated). +*/ + + +/* +ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm)); + + Initializes the internal stream state for decompression. The fields + next_in, avail_in, zalloc, zfree and opaque must be initialized before by + the caller. In the current version of inflate, the provided input is not + read or consumed. The allocation of a sliding window will be deferred to + the first call of inflate (if the decompression does not complete on the + first call). If zalloc and zfree are set to Z_NULL, inflateInit updates + them to use default allocation functions. + + inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_VERSION_ERROR if the zlib library version is incompatible with the + version assumed by the caller, or Z_STREAM_ERROR if the parameters are + invalid, such as a null pointer to the structure. msg is set to null if + there is no error message. inflateInit does not perform any decompression. + Actual decompression will be done by inflate(). So next_in, and avail_in, + next_out, and avail_out are unused and unchanged. The current + implementation of inflateInit() does not process any header information -- + that is deferred until inflate() is called. +*/ + + +ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush)); +/* + inflate decompresses as much data as possible, and stops when the input + buffer becomes empty or the output buffer becomes full. It may introduce + some output latency (reading input without producing any output) except when + forced to flush. + + The detailed semantics are as follows. inflate performs one or both of the + following actions: + + - Decompress more input starting at next_in and update next_in and avail_in + accordingly. If not all input can be processed (because there is not + enough room in the output buffer), then next_in and avail_in are updated + accordingly, and processing will resume at this point for the next call of + inflate(). + + - Generate more output starting at next_out and update next_out and avail_out + accordingly. inflate() provides as much output as possible, until there is + no more input data or no more space in the output buffer (see below about + the flush parameter). + + Before the call of inflate(), the application should ensure that at least + one of the actions is possible, by providing more input and/or consuming more + output, and updating the next_* and avail_* values accordingly. If the + caller of inflate() does not provide both available input and available + output space, it is possible that there will be no progress made. The + application can consume the uncompressed output when it wants, for example + when the output buffer is full (avail_out == 0), or after each call of + inflate(). If inflate returns Z_OK and with zero avail_out, it must be + called again after making room in the output buffer because there might be + more output pending. + + The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH, Z_FINISH, + Z_BLOCK, or Z_TREES. Z_SYNC_FLUSH requests that inflate() flush as much + output as possible to the output buffer. Z_BLOCK requests that inflate() + stop if and when it gets to the next deflate block boundary. When decoding + the zlib or gzip format, this will cause inflate() to return immediately + after the header and before the first block. When doing a raw inflate, + inflate() will go ahead and process the first block, and will return when it + gets to the end of that block, or when it runs out of data. + + The Z_BLOCK option assists in appending to or combining deflate streams. + To assist in this, on return inflate() always sets strm->data_type to the + number of unused bits in the last byte taken from strm->next_in, plus 64 if + inflate() is currently decoding the last block in the deflate stream, plus + 128 if inflate() returned immediately after decoding an end-of-block code or + decoding the complete header up to just before the first byte of the deflate + stream. The end-of-block will not be indicated until all of the uncompressed + data from that block has been written to strm->next_out. The number of + unused bits may in general be greater than seven, except when bit 7 of + data_type is set, in which case the number of unused bits will be less than + eight. data_type is set as noted here every time inflate() returns for all + flush options, and so can be used to determine the amount of currently + consumed input in bits. + + The Z_TREES option behaves as Z_BLOCK does, but it also returns when the + end of each deflate block header is reached, before any actual data in that + block is decoded. This allows the caller to determine the length of the + deflate block header for later use in random access within a deflate block. + 256 is added to the value of strm->data_type when inflate() returns + immediately after reaching the end of the deflate block header. + + inflate() should normally be called until it returns Z_STREAM_END or an + error. However if all decompression is to be performed in a single step (a + single call of inflate), the parameter flush should be set to Z_FINISH. In + this case all pending input is processed and all pending output is flushed; + avail_out must be large enough to hold all of the uncompressed data for the + operation to complete. (The size of the uncompressed data may have been + saved by the compressor for this purpose.) The use of Z_FINISH is not + required to perform an inflation in one step. However it may be used to + inform inflate that a faster approach can be used for the single inflate() + call. Z_FINISH also informs inflate to not maintain a sliding window if the + stream completes, which reduces inflate's memory footprint. If the stream + does not complete, either because not all of the stream is provided or not + enough output space is provided, then a sliding window will be allocated and + inflate() can be called again to continue the operation as if Z_NO_FLUSH had + been used. + + In this implementation, inflate() always flushes as much output as + possible to the output buffer, and always uses the faster approach on the + first call. So the effects of the flush parameter in this implementation are + on the return value of inflate() as noted below, when inflate() returns early + when Z_BLOCK or Z_TREES is used, and when inflate() avoids the allocation of + memory for a sliding window when Z_FINISH is used. + + If a preset dictionary is needed after this call (see inflateSetDictionary + below), inflate sets strm->adler to the Adler-32 checksum of the dictionary + chosen by the compressor and returns Z_NEED_DICT; otherwise it sets + strm->adler to the Adler-32 checksum of all output produced so far (that is, + total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described + below. At the end of the stream, inflate() checks that its computed Adler-32 + checksum is equal to that saved by the compressor and returns Z_STREAM_END + only if the checksum is correct. + + inflate() can decompress and check either zlib-wrapped or gzip-wrapped + deflate data. The header type is detected automatically, if requested when + initializing with inflateInit2(). Any information contained in the gzip + header is not retained unless inflateGetHeader() is used. When processing + gzip-wrapped deflate data, strm->adler32 is set to the CRC-32 of the output + produced so far. The CRC-32 is checked against the gzip trailer, as is the + uncompressed length, modulo 2^32. + + inflate() returns Z_OK if some progress has been made (more input processed + or more output produced), Z_STREAM_END if the end of the compressed data has + been reached and all uncompressed output has been produced, Z_NEED_DICT if a + preset dictionary is needed at this point, Z_DATA_ERROR if the input data was + corrupted (input stream not conforming to the zlib format or incorrect check + value, in which case strm->msg points to a string with a more specific + error), Z_STREAM_ERROR if the stream structure was inconsistent (for example + next_in or next_out was Z_NULL, or the state was inadvertently written over + by the application), Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR + if no progress was possible or if there was not enough room in the output + buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and + inflate() can be called again with more input and more output space to + continue decompressing. If Z_DATA_ERROR is returned, the application may + then call inflateSync() to look for a good compression block if a partial + recovery of the data is to be attempted. +*/ + + +ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm)); +/* + All dynamically allocated data structures for this stream are freed. + This function discards any unprocessed input and does not flush any pending + output. + + inflateEnd returns Z_OK if success, or Z_STREAM_ERROR if the stream state + was inconsistent. +*/ + + + /* Advanced functions */ + +/* + The following functions are needed only in some special applications. +*/ + +/* +ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm, + int level, + int method, + int windowBits, + int memLevel, + int strategy)); + + This is another version of deflateInit with more compression options. The + fields next_in, zalloc, zfree and opaque must be initialized before by the + caller. + + The method parameter is the compression method. It must be Z_DEFLATED in + this version of the library. + + The windowBits parameter is the base two logarithm of the window size + (the size of the history buffer). It should be in the range 8..15 for this + version of the library. Larger values of this parameter result in better + compression at the expense of memory usage. The default value is 15 if + deflateInit is used instead. + + For the current implementation of deflate(), a windowBits value of 8 (a + window size of 256 bytes) is not supported. As a result, a request for 8 + will result in 9 (a 512-byte window). In that case, providing 8 to + inflateInit2() will result in an error when the zlib header with 9 is + checked against the initialization of inflate(). The remedy is to not use 8 + with deflateInit2() with this initialization, or at least in that case use 9 + with inflateInit2(). + + windowBits can also be -8..-15 for raw deflate. In this case, -windowBits + determines the window size. deflate() will then generate raw deflate data + with no zlib header or trailer, and will not compute a check value. + + windowBits can also be greater than 15 for optional gzip encoding. Add + 16 to windowBits to write a simple gzip header and trailer around the + compressed data instead of a zlib wrapper. The gzip header will have no + file name, no extra data, no comment, no modification time (set to zero), no + header crc, and the operating system will be set to the appropriate value, + if the operating system was determined at compile time. If a gzip stream is + being written, strm->adler is a CRC-32 instead of an Adler-32. + + For raw deflate or gzip encoding, a request for a 256-byte window is + rejected as invalid, since only the zlib header provides a means of + transmitting the window size to the decompressor. + + The memLevel parameter specifies how much memory should be allocated + for the internal compression state. memLevel=1 uses minimum memory but is + slow and reduces compression ratio; memLevel=9 uses maximum memory for + optimal speed. The default value is 8. See zconf.h for total memory usage + as a function of windowBits and memLevel. + + The strategy parameter is used to tune the compression algorithm. Use the + value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a + filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no + string match), or Z_RLE to limit match distances to one (run-length + encoding). Filtered data consists mostly of small values with a somewhat + random distribution. In this case, the compression algorithm is tuned to + compress them better. The effect of Z_FILTERED is to force more Huffman + coding and less string matching; it is somewhat intermediate between + Z_DEFAULT_STRATEGY and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as + fast as Z_HUFFMAN_ONLY, but give better compression for PNG image data. The + strategy parameter only affects the compression ratio but not the + correctness of the compressed output even if it is not set appropriately. + Z_FIXED prevents the use of dynamic Huffman codes, allowing for a simpler + decoder for special applications. + + deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_STREAM_ERROR if any parameter is invalid (such as an invalid + method), or Z_VERSION_ERROR if the zlib library version (zlib_version) is + incompatible with the version assumed by the caller (ZLIB_VERSION). msg is + set to null if there is no error message. deflateInit2 does not perform any + compression: this will be done by deflate(). +*/ + +ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm, + const Bytef *dictionary, + uInt dictLength)); +/* + Initializes the compression dictionary from the given byte sequence + without producing any compressed output. When using the zlib format, this + function must be called immediately after deflateInit, deflateInit2 or + deflateReset, and before any call of deflate. When doing raw deflate, this + function must be called either before any call of deflate, or immediately + after the completion of a deflate block, i.e. after all input has been + consumed and all output has been delivered when using any of the flush + options Z_BLOCK, Z_PARTIAL_FLUSH, Z_SYNC_FLUSH, or Z_FULL_FLUSH. The + compressor and decompressor must use exactly the same dictionary (see + inflateSetDictionary). + + The dictionary should consist of strings (byte sequences) that are likely + to be encountered later in the data to be compressed, with the most commonly + used strings preferably put towards the end of the dictionary. Using a + dictionary is most useful when the data to be compressed is short and can be + predicted with good accuracy; the data can then be compressed better than + with the default empty dictionary. + + Depending on the size of the compression data structures selected by + deflateInit or deflateInit2, a part of the dictionary may in effect be + discarded, for example if the dictionary is larger than the window size + provided in deflateInit or deflateInit2. Thus the strings most likely to be + useful should be put at the end of the dictionary, not at the front. In + addition, the current implementation of deflate will use at most the window + size minus 262 bytes of the provided dictionary. + + Upon return of this function, strm->adler is set to the Adler-32 value + of the dictionary; the decompressor may later use this value to determine + which dictionary has been used by the compressor. (The Adler-32 value + applies to the whole dictionary even if only a subset of the dictionary is + actually used by the compressor.) If a raw deflate was requested, then the + Adler-32 value is not computed and strm->adler is not set. + + deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a + parameter is invalid (e.g. dictionary being Z_NULL) or the stream state is + inconsistent (for example if deflate has already been called for this stream + or if not at a block boundary for raw deflate). deflateSetDictionary does + not perform any compression: this will be done by deflate(). +*/ + +ZEXTERN int ZEXPORT deflateGetDictionary OF((z_streamp strm, + Bytef *dictionary, + uInt *dictLength)); +/* + Returns the sliding dictionary being maintained by deflate. dictLength is + set to the number of bytes in the dictionary, and that many bytes are copied + to dictionary. dictionary must have enough space, where 32768 bytes is + always enough. If deflateGetDictionary() is called with dictionary equal to + Z_NULL, then only the dictionary length is returned, and nothing is copied. + Similary, if dictLength is Z_NULL, then it is not set. + + deflateGetDictionary() may return a length less than the window size, even + when more than the window size in input has been provided. It may return up + to 258 bytes less in that case, due to how zlib's implementation of deflate + manages the sliding window and lookahead for matches, where matches can be + up to 258 bytes long. If the application needs the last window-size bytes of + input, then that would need to be saved by the application outside of zlib. + + deflateGetDictionary returns Z_OK on success, or Z_STREAM_ERROR if the + stream state is inconsistent. +*/ + +ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest, + z_streamp source)); +/* + Sets the destination stream as a complete copy of the source stream. + + This function can be useful when several compression strategies will be + tried, for example when there are several ways of pre-processing the input + data with a filter. The streams that will be discarded should then be freed + by calling deflateEnd. Note that deflateCopy duplicates the internal + compression state which can be quite large, so this strategy is slow and can + consume lots of memory. + + deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_STREAM_ERROR if the source stream state was inconsistent + (such as zalloc being Z_NULL). msg is left unchanged in both source and + destination. +*/ + +ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm)); +/* + This function is equivalent to deflateEnd followed by deflateInit, but + does not free and reallocate the internal compression state. The stream + will leave the compression level and any other attributes that may have been + set unchanged. + + deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent (such as zalloc or state being Z_NULL). +*/ + +ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm, + int level, + int strategy)); +/* + Dynamically update the compression level and compression strategy. The + interpretation of level and strategy is as in deflateInit2(). This can be + used to switch between compression and straight copy of the input data, or + to switch to a different kind of input data requiring a different strategy. + If the compression approach (which is a function of the level) or the + strategy is changed, and if any input has been consumed in a previous + deflate() call, then the input available so far is compressed with the old + level and strategy using deflate(strm, Z_BLOCK). There are three approaches + for the compression levels 0, 1..3, and 4..9 respectively. The new level + and strategy will take effect at the next call of deflate(). + + If a deflate(strm, Z_BLOCK) is performed by deflateParams(), and it does + not have enough output space to complete, then the parameter change will not + take effect. In this case, deflateParams() can be called again with the + same parameters and more output space to try again. + + In order to assure a change in the parameters on the first try, the + deflate stream should be flushed using deflate() with Z_BLOCK or other flush + request until strm.avail_out is not zero, before calling deflateParams(). + Then no more input data should be provided before the deflateParams() call. + If this is done, the old level and strategy will be applied to the data + compressed before deflateParams(), and the new level and strategy will be + applied to the the data compressed after deflateParams(). + + deflateParams returns Z_OK on success, Z_STREAM_ERROR if the source stream + state was inconsistent or if a parameter was invalid, or Z_BUF_ERROR if + there was not enough output space to complete the compression of the + available input data before a change in the strategy or approach. Note that + in the case of a Z_BUF_ERROR, the parameters are not changed. A return + value of Z_BUF_ERROR is not fatal, in which case deflateParams() can be + retried with more output space. +*/ + +ZEXTERN int ZEXPORT deflateTune OF((z_streamp strm, + int good_length, + int max_lazy, + int nice_length, + int max_chain)); +/* + Fine tune deflate's internal compression parameters. This should only be + used by someone who understands the algorithm used by zlib's deflate for + searching for the best matching string, and even then only by the most + fanatic optimizer trying to squeeze out the last compressed bit for their + specific input data. Read the deflate.c source code for the meaning of the + max_lazy, good_length, nice_length, and max_chain parameters. + + deflateTune() can be called after deflateInit() or deflateInit2(), and + returns Z_OK on success, or Z_STREAM_ERROR for an invalid deflate stream. + */ + +ZEXTERN uLong ZEXPORT deflateBound OF((z_streamp strm, + uLong sourceLen)); +/* + deflateBound() returns an upper bound on the compressed size after + deflation of sourceLen bytes. It must be called after deflateInit() or + deflateInit2(), and after deflateSetHeader(), if used. This would be used + to allocate an output buffer for deflation in a single pass, and so would be + called before deflate(). If that first deflate() call is provided the + sourceLen input bytes, an output buffer allocated to the size returned by + deflateBound(), and the flush value Z_FINISH, then deflate() is guaranteed + to return Z_STREAM_END. Note that it is possible for the compressed size to + be larger than the value returned by deflateBound() if flush options other + than Z_FINISH or Z_NO_FLUSH are used. +*/ + +ZEXTERN int ZEXPORT deflatePending OF((z_streamp strm, + unsigned *pending, + int *bits)); +/* + deflatePending() returns the number of bytes and bits of output that have + been generated, but not yet provided in the available output. The bytes not + provided would be due to the available output space having being consumed. + The number of bits of output not provided are between 0 and 7, where they + await more bits to join them in order to fill out a full byte. If pending + or bits are Z_NULL, then those values are not set. + + deflatePending returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. + */ + +ZEXTERN int ZEXPORT deflatePrime OF((z_streamp strm, + int bits, + int value)); +/* + deflatePrime() inserts bits in the deflate output stream. The intent + is that this function is used to start off the deflate output with the bits + leftover from a previous deflate stream when appending to it. As such, this + function can only be used for raw deflate, and must be used before the first + deflate() call after a deflateInit2() or deflateReset(). bits must be less + than or equal to 16, and that many of the least significant bits of value + will be inserted in the output. + + deflatePrime returns Z_OK if success, Z_BUF_ERROR if there was not enough + room in the internal buffer to insert the bits, or Z_STREAM_ERROR if the + source stream state was inconsistent. +*/ + +ZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm, + gz_headerp head)); +/* + deflateSetHeader() provides gzip header information for when a gzip + stream is requested by deflateInit2(). deflateSetHeader() may be called + after deflateInit2() or deflateReset() and before the first call of + deflate(). The text, time, os, extra field, name, and comment information + in the provided gz_header structure are written to the gzip header (xflag is + ignored -- the extra flags are set according to the compression level). The + caller must assure that, if not Z_NULL, name and comment are terminated with + a zero byte, and that if extra is not Z_NULL, that extra_len bytes are + available there. If hcrc is true, a gzip header crc is included. Note that + the current versions of the command-line version of gzip (up through version + 1.3.x) do not support header crc's, and will report that it is a "multi-part + gzip file" and give up. + + If deflateSetHeader is not used, the default gzip header has text false, + the time set to zero, and os set to 255, with no extra, name, or comment + fields. The gzip header is returned to the default state by deflateReset(). + + deflateSetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. +*/ + +/* +ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm, + int windowBits)); + + This is another version of inflateInit with an extra parameter. The + fields next_in, avail_in, zalloc, zfree and opaque must be initialized + before by the caller. + + The windowBits parameter is the base two logarithm of the maximum window + size (the size of the history buffer). It should be in the range 8..15 for + this version of the library. The default value is 15 if inflateInit is used + instead. windowBits must be greater than or equal to the windowBits value + provided to deflateInit2() while compressing, or it must be equal to 15 if + deflateInit2() was not used. If a compressed stream with a larger window + size is given as input, inflate() will return with the error code + Z_DATA_ERROR instead of trying to allocate a larger window. + + windowBits can also be zero to request that inflate use the window size in + the zlib header of the compressed stream. + + windowBits can also be -8..-15 for raw inflate. In this case, -windowBits + determines the window size. inflate() will then process raw deflate data, + not looking for a zlib or gzip header, not generating a check value, and not + looking for any check values for comparison at the end of the stream. This + is for use with other formats that use the deflate compressed data format + such as zip. Those formats provide their own check values. If a custom + format is developed using the raw deflate format for compressed data, it is + recommended that a check value such as an Adler-32 or a CRC-32 be applied to + the uncompressed data as is done in the zlib, gzip, and zip formats. For + most applications, the zlib format should be used as is. Note that comments + above on the use in deflateInit2() applies to the magnitude of windowBits. + + windowBits can also be greater than 15 for optional gzip decoding. Add + 32 to windowBits to enable zlib and gzip decoding with automatic header + detection, or add 16 to decode only the gzip format (the zlib format will + return a Z_DATA_ERROR). If a gzip stream is being decoded, strm->adler is a + CRC-32 instead of an Adler-32. Unlike the gunzip utility and gzread() (see + below), inflate() will not automatically decode concatenated gzip streams. + inflate() will return Z_STREAM_END at the end of the gzip stream. The state + would need to be reset to continue decoding a subsequent gzip stream. + + inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_VERSION_ERROR if the zlib library version is incompatible with the + version assumed by the caller, or Z_STREAM_ERROR if the parameters are + invalid, such as a null pointer to the structure. msg is set to null if + there is no error message. inflateInit2 does not perform any decompression + apart from possibly reading the zlib header if present: actual decompression + will be done by inflate(). (So next_in and avail_in may be modified, but + next_out and avail_out are unused and unchanged.) The current implementation + of inflateInit2() does not process any header information -- that is + deferred until inflate() is called. +*/ + +ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm, + const Bytef *dictionary, + uInt dictLength)); +/* + Initializes the decompression dictionary from the given uncompressed byte + sequence. This function must be called immediately after a call of inflate, + if that call returned Z_NEED_DICT. The dictionary chosen by the compressor + can be determined from the Adler-32 value returned by that call of inflate. + The compressor and decompressor must use exactly the same dictionary (see + deflateSetDictionary). For raw inflate, this function can be called at any + time to set the dictionary. If the provided dictionary is smaller than the + window and there is already data in the window, then the provided dictionary + will amend what's there. The application must insure that the dictionary + that was used for compression is provided. + + inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a + parameter is invalid (e.g. dictionary being Z_NULL) or the stream state is + inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the + expected one (incorrect Adler-32 value). inflateSetDictionary does not + perform any decompression: this will be done by subsequent calls of + inflate(). +*/ + +ZEXTERN int ZEXPORT inflateGetDictionary OF((z_streamp strm, + Bytef *dictionary, + uInt *dictLength)); +/* + Returns the sliding dictionary being maintained by inflate. dictLength is + set to the number of bytes in the dictionary, and that many bytes are copied + to dictionary. dictionary must have enough space, where 32768 bytes is + always enough. If inflateGetDictionary() is called with dictionary equal to + Z_NULL, then only the dictionary length is returned, and nothing is copied. + Similary, if dictLength is Z_NULL, then it is not set. + + inflateGetDictionary returns Z_OK on success, or Z_STREAM_ERROR if the + stream state is inconsistent. +*/ + +ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm)); +/* + Skips invalid compressed data until a possible full flush point (see above + for the description of deflate with Z_FULL_FLUSH) can be found, or until all + available input is skipped. No output is provided. + + inflateSync searches for a 00 00 FF FF pattern in the compressed data. + All full flush points have this pattern, but not all occurrences of this + pattern are full flush points. + + inflateSync returns Z_OK if a possible full flush point has been found, + Z_BUF_ERROR if no more input was provided, Z_DATA_ERROR if no flush point + has been found, or Z_STREAM_ERROR if the stream structure was inconsistent. + In the success case, the application may save the current current value of + total_in which indicates where valid compressed data was found. In the + error case, the application may repeatedly call inflateSync, providing more + input each time, until success or end of the input data. +*/ + +ZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest, + z_streamp source)); +/* + Sets the destination stream as a complete copy of the source stream. + + This function can be useful when randomly accessing a large stream. The + first pass through the stream can periodically record the inflate state, + allowing restarting inflate at those points when randomly accessing the + stream. + + inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_STREAM_ERROR if the source stream state was inconsistent + (such as zalloc being Z_NULL). msg is left unchanged in both source and + destination. +*/ + +ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm)); +/* + This function is equivalent to inflateEnd followed by inflateInit, + but does not free and reallocate the internal decompression state. The + stream will keep attributes that may have been set by inflateInit2. + + inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent (such as zalloc or state being Z_NULL). +*/ + +ZEXTERN int ZEXPORT inflateReset2 OF((z_streamp strm, + int windowBits)); +/* + This function is the same as inflateReset, but it also permits changing + the wrap and window size requests. The windowBits parameter is interpreted + the same as it is for inflateInit2. If the window size is changed, then the + memory allocated for the window is freed, and the window will be reallocated + by inflate() if needed. + + inflateReset2 returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent (such as zalloc or state being Z_NULL), or if + the windowBits parameter is invalid. +*/ + +ZEXTERN int ZEXPORT inflatePrime OF((z_streamp strm, + int bits, + int value)); +/* + This function inserts bits in the inflate input stream. The intent is + that this function is used to start inflating at a bit position in the + middle of a byte. The provided bits will be used before any bytes are used + from next_in. This function should only be used with raw inflate, and + should be used before the first inflate() call after inflateInit2() or + inflateReset(). bits must be less than or equal to 16, and that many of the + least significant bits of value will be inserted in the input. + + If bits is negative, then the input stream bit buffer is emptied. Then + inflatePrime() can be called again to put bits in the buffer. This is used + to clear out bits leftover after feeding inflate a block description prior + to feeding inflate codes. + + inflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. +*/ + +ZEXTERN long ZEXPORT inflateMark OF((z_streamp strm)); +/* + This function returns two values, one in the lower 16 bits of the return + value, and the other in the remaining upper bits, obtained by shifting the + return value down 16 bits. If the upper value is -1 and the lower value is + zero, then inflate() is currently decoding information outside of a block. + If the upper value is -1 and the lower value is non-zero, then inflate is in + the middle of a stored block, with the lower value equaling the number of + bytes from the input remaining to copy. If the upper value is not -1, then + it is the number of bits back from the current bit position in the input of + the code (literal or length/distance pair) currently being processed. In + that case the lower value is the number of bytes already emitted for that + code. + + A code is being processed if inflate is waiting for more input to complete + decoding of the code, or if it has completed decoding but is waiting for + more output space to write the literal or match data. + + inflateMark() is used to mark locations in the input data for random + access, which may be at bit positions, and to note those cases where the + output of a code may span boundaries of random access blocks. The current + location in the input stream can be determined from avail_in and data_type + as noted in the description for the Z_BLOCK flush parameter for inflate. + + inflateMark returns the value noted above, or -65536 if the provided + source stream state was inconsistent. +*/ + +ZEXTERN int ZEXPORT inflateGetHeader OF((z_streamp strm, + gz_headerp head)); +/* + inflateGetHeader() requests that gzip header information be stored in the + provided gz_header structure. inflateGetHeader() may be called after + inflateInit2() or inflateReset(), and before the first call of inflate(). + As inflate() processes the gzip stream, head->done is zero until the header + is completed, at which time head->done is set to one. If a zlib stream is + being decoded, then head->done is set to -1 to indicate that there will be + no gzip header information forthcoming. Note that Z_BLOCK or Z_TREES can be + used to force inflate() to return immediately after header processing is + complete and before any actual data is decompressed. + + The text, time, xflags, and os fields are filled in with the gzip header + contents. hcrc is set to true if there is a header CRC. (The header CRC + was valid if done is set to one.) If extra is not Z_NULL, then extra_max + contains the maximum number of bytes to write to extra. Once done is true, + extra_len contains the actual extra field length, and extra contains the + extra field, or that field truncated if extra_max is less than extra_len. + If name is not Z_NULL, then up to name_max characters are written there, + terminated with a zero unless the length is greater than name_max. If + comment is not Z_NULL, then up to comm_max characters are written there, + terminated with a zero unless the length is greater than comm_max. When any + of extra, name, or comment are not Z_NULL and the respective field is not + present in the header, then that field is set to Z_NULL to signal its + absence. This allows the use of deflateSetHeader() with the returned + structure to duplicate the header. However if those fields are set to + allocated memory, then the application will need to save those pointers + elsewhere so that they can be eventually freed. + + If inflateGetHeader is not used, then the header information is simply + discarded. The header is always checked for validity, including the header + CRC if present. inflateReset() will reset the process to discard the header + information. The application would need to call inflateGetHeader() again to + retrieve the header from the next gzip stream. + + inflateGetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. +*/ + +/* +ZEXTERN int ZEXPORT inflateBackInit OF((z_streamp strm, int windowBits, + unsigned char FAR *window)); + + Initialize the internal stream state for decompression using inflateBack() + calls. The fields zalloc, zfree and opaque in strm must be initialized + before the call. If zalloc and zfree are Z_NULL, then the default library- + derived memory allocation routines are used. windowBits is the base two + logarithm of the window size, in the range 8..15. window is a caller + supplied buffer of that size. Except for special applications where it is + assured that deflate was used with small window sizes, windowBits must be 15 + and a 32K byte window must be supplied to be able to decompress general + deflate streams. + + See inflateBack() for the usage of these routines. + + inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of + the parameters are invalid, Z_MEM_ERROR if the internal state could not be + allocated, or Z_VERSION_ERROR if the version of the library does not match + the version of the header file. +*/ + +typedef unsigned (*in_func) OF((void FAR *, + z_const unsigned char FAR * FAR *)); +typedef int (*out_func) OF((void FAR *, unsigned char FAR *, unsigned)); + +ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm, + in_func in, void FAR *in_desc, + out_func out, void FAR *out_desc)); +/* + inflateBack() does a raw inflate with a single call using a call-back + interface for input and output. This is potentially more efficient than + inflate() for file i/o applications, in that it avoids copying between the + output and the sliding window by simply making the window itself the output + buffer. inflate() can be faster on modern CPUs when used with large + buffers. inflateBack() trusts the application to not change the output + buffer passed by the output function, at least until inflateBack() returns. + + inflateBackInit() must be called first to allocate the internal state + and to initialize the state with the user-provided window buffer. + inflateBack() may then be used multiple times to inflate a complete, raw + deflate stream with each call. inflateBackEnd() is then called to free the + allocated state. + + A raw deflate stream is one with no zlib or gzip header or trailer. + This routine would normally be used in a utility that reads zip or gzip + files and writes out uncompressed files. The utility would decode the + header and process the trailer on its own, hence this routine expects only + the raw deflate stream to decompress. This is different from the default + behavior of inflate(), which expects a zlib header and trailer around the + deflate stream. + + inflateBack() uses two subroutines supplied by the caller that are then + called by inflateBack() for input and output. inflateBack() calls those + routines until it reads a complete deflate stream and writes out all of the + uncompressed data, or until it encounters an error. The function's + parameters and return types are defined above in the in_func and out_func + typedefs. inflateBack() will call in(in_desc, &buf) which should return the + number of bytes of provided input, and a pointer to that input in buf. If + there is no input available, in() must return zero -- buf is ignored in that + case -- and inflateBack() will return a buffer error. inflateBack() will + call out(out_desc, buf, len) to write the uncompressed data buf[0..len-1]. + out() should return zero on success, or non-zero on failure. If out() + returns non-zero, inflateBack() will return with an error. Neither in() nor + out() are permitted to change the contents of the window provided to + inflateBackInit(), which is also the buffer that out() uses to write from. + The length written by out() will be at most the window size. Any non-zero + amount of input may be provided by in(). + + For convenience, inflateBack() can be provided input on the first call by + setting strm->next_in and strm->avail_in. If that input is exhausted, then + in() will be called. Therefore strm->next_in must be initialized before + calling inflateBack(). If strm->next_in is Z_NULL, then in() will be called + immediately for input. If strm->next_in is not Z_NULL, then strm->avail_in + must also be initialized, and then if strm->avail_in is not zero, input will + initially be taken from strm->next_in[0 .. strm->avail_in - 1]. + + The in_desc and out_desc parameters of inflateBack() is passed as the + first parameter of in() and out() respectively when they are called. These + descriptors can be optionally used to pass any information that the caller- + supplied in() and out() functions need to do their job. + + On return, inflateBack() will set strm->next_in and strm->avail_in to + pass back any unused input that was provided by the last in() call. The + return values of inflateBack() can be Z_STREAM_END on success, Z_BUF_ERROR + if in() or out() returned an error, Z_DATA_ERROR if there was a format error + in the deflate stream (in which case strm->msg is set to indicate the nature + of the error), or Z_STREAM_ERROR if the stream was not properly initialized. + In the case of Z_BUF_ERROR, an input or output error can be distinguished + using strm->next_in which will be Z_NULL only if in() returned an error. If + strm->next_in is not Z_NULL, then the Z_BUF_ERROR was due to out() returning + non-zero. (in() will always be called before out(), so strm->next_in is + assured to be defined if out() returns non-zero.) Note that inflateBack() + cannot return Z_OK. +*/ + +ZEXTERN int ZEXPORT inflateBackEnd OF((z_streamp strm)); +/* + All memory allocated by inflateBackInit() is freed. + + inflateBackEnd() returns Z_OK on success, or Z_STREAM_ERROR if the stream + state was inconsistent. +*/ + +ZEXTERN uLong ZEXPORT zlibCompileFlags OF((void)); +/* Return flags indicating compile-time options. + + Type sizes, two bits each, 00 = 16 bits, 01 = 32, 10 = 64, 11 = other: + 1.0: size of uInt + 3.2: size of uLong + 5.4: size of voidpf (pointer) + 7.6: size of z_off_t + + Compiler, assembler, and debug options: + 8: ZLIB_DEBUG + 9: ASMV or ASMINF -- use ASM code + 10: ZLIB_WINAPI -- exported functions use the WINAPI calling convention + 11: 0 (reserved) + + One-time table building (smaller code, but not thread-safe if true): + 12: BUILDFIXED -- build static block decoding tables when needed + 13: DYNAMIC_CRC_TABLE -- build CRC calculation tables when needed + 14,15: 0 (reserved) + + Library content (indicates missing functionality): + 16: NO_GZCOMPRESS -- gz* functions cannot compress (to avoid linking + deflate code when not needed) + 17: NO_GZIP -- deflate can't write gzip streams, and inflate can't detect + and decode gzip streams (to avoid linking crc code) + 18-19: 0 (reserved) + + Operation variations (changes in library functionality): + 20: PKZIP_BUG_WORKAROUND -- slightly more permissive inflate + 21: FASTEST -- deflate algorithm with only one, lowest compression level + 22,23: 0 (reserved) + + The sprintf variant used by gzprintf (zero is best): + 24: 0 = vs*, 1 = s* -- 1 means limited to 20 arguments after the format + 25: 0 = *nprintf, 1 = *printf -- 1 means gzprintf() not secure! + 26: 0 = returns value, 1 = void -- 1 means inferred string length returned + + Remainder: + 27-31: 0 (reserved) + */ + +#ifndef Z_SOLO + + /* utility functions */ + +/* + The following utility functions are implemented on top of the basic + stream-oriented functions. To simplify the interface, some default options + are assumed (compression level and memory usage, standard memory allocation + functions). The source code of these utility functions can be modified if + you need special options. +*/ + +ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen, + const Bytef *source, uLong sourceLen)); +/* + Compresses the source buffer into the destination buffer. sourceLen is + the byte length of the source buffer. Upon entry, destLen is the total size + of the destination buffer, which must be at least the value returned by + compressBound(sourceLen). Upon exit, destLen is the actual size of the + compressed data. compress() is equivalent to compress2() with a level + parameter of Z_DEFAULT_COMPRESSION. + + compress returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_BUF_ERROR if there was not enough room in the output + buffer. +*/ + +ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen, + const Bytef *source, uLong sourceLen, + int level)); +/* + Compresses the source buffer into the destination buffer. The level + parameter has the same meaning as in deflateInit. sourceLen is the byte + length of the source buffer. Upon entry, destLen is the total size of the + destination buffer, which must be at least the value returned by + compressBound(sourceLen). Upon exit, destLen is the actual size of the + compressed data. + + compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_BUF_ERROR if there was not enough room in the output buffer, + Z_STREAM_ERROR if the level parameter is invalid. +*/ + +ZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen)); +/* + compressBound() returns an upper bound on the compressed size after + compress() or compress2() on sourceLen bytes. It would be used before a + compress() or compress2() call to allocate the destination buffer. +*/ + +ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen, + const Bytef *source, uLong sourceLen)); +/* + Decompresses the source buffer into the destination buffer. sourceLen is + the byte length of the source buffer. Upon entry, destLen is the total size + of the destination buffer, which must be large enough to hold the entire + uncompressed data. (The size of the uncompressed data must have been saved + previously by the compressor and transmitted to the decompressor by some + mechanism outside the scope of this compression library.) Upon exit, destLen + is the actual size of the uncompressed data. + + uncompress returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_BUF_ERROR if there was not enough room in the output + buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete. In + the case where there is not enough room, uncompress() will fill the output + buffer with the uncompressed data up to that point. +*/ + +ZEXTERN int ZEXPORT uncompress2 OF((Bytef *dest, uLongf *destLen, + const Bytef *source, uLong *sourceLen)); +/* + Same as uncompress, except that sourceLen is a pointer, where the + length of the source is *sourceLen. On return, *sourceLen is the number of + source bytes consumed. +*/ + + /* gzip file access functions */ + +/* + This library supports reading and writing files in gzip (.gz) format with + an interface similar to that of stdio, using the functions that start with + "gz". The gzip format is different from the zlib format. gzip is a gzip + wrapper, documented in RFC 1952, wrapped around a deflate stream. +*/ + +typedef struct gzFile_s *gzFile; /* semi-opaque gzip file descriptor */ + +/* +ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode)); + + Opens a gzip (.gz) file for reading or writing. The mode parameter is as + in fopen ("rb" or "wb") but can also include a compression level ("wb9") or + a strategy: 'f' for filtered data as in "wb6f", 'h' for Huffman-only + compression as in "wb1h", 'R' for run-length encoding as in "wb1R", or 'F' + for fixed code compression as in "wb9F". (See the description of + deflateInit2 for more information about the strategy parameter.) 'T' will + request transparent writing or appending with no compression and not using + the gzip format. + + "a" can be used instead of "w" to request that the gzip stream that will + be written be appended to the file. "+" will result in an error, since + reading and writing to the same gzip file is not supported. The addition of + "x" when writing will create the file exclusively, which fails if the file + already exists. On systems that support it, the addition of "e" when + reading or writing will set the flag to close the file on an execve() call. + + These functions, as well as gzip, will read and decode a sequence of gzip + streams in a file. The append function of gzopen() can be used to create + such a file. (Also see gzflush() for another way to do this.) When + appending, gzopen does not test whether the file begins with a gzip stream, + nor does it look for the end of the gzip streams to begin appending. gzopen + will simply append a gzip stream to the existing file. + + gzopen can be used to read a file which is not in gzip format; in this + case gzread will directly read from the file without decompression. When + reading, this will be detected automatically by looking for the magic two- + byte gzip header. + + gzopen returns NULL if the file could not be opened, if there was + insufficient memory to allocate the gzFile state, or if an invalid mode was + specified (an 'r', 'w', or 'a' was not provided, or '+' was provided). + errno can be checked to determine if the reason gzopen failed was that the + file could not be opened. +*/ + +ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode)); +/* + gzdopen associates a gzFile with the file descriptor fd. File descriptors + are obtained from calls like open, dup, creat, pipe or fileno (if the file + has been previously opened with fopen). The mode parameter is as in gzopen. + + The next call of gzclose on the returned gzFile will also close the file + descriptor fd, just like fclose(fdopen(fd, mode)) closes the file descriptor + fd. If you want to keep fd open, use fd = dup(fd_keep); gz = gzdopen(fd, + mode);. The duplicated descriptor should be saved to avoid a leak, since + gzdopen does not close fd if it fails. If you are using fileno() to get the + file descriptor from a FILE *, then you will have to use dup() to avoid + double-close()ing the file descriptor. Both gzclose() and fclose() will + close the associated file descriptor, so they need to have different file + descriptors. + + gzdopen returns NULL if there was insufficient memory to allocate the + gzFile state, if an invalid mode was specified (an 'r', 'w', or 'a' was not + provided, or '+' was provided), or if fd is -1. The file descriptor is not + used until the next gz* read, write, seek, or close operation, so gzdopen + will not detect if fd is invalid (unless fd is -1). +*/ + +ZEXTERN int ZEXPORT gzbuffer OF((gzFile file, unsigned size)); +/* + Set the internal buffer size used by this library's functions. The + default buffer size is 8192 bytes. This function must be called after + gzopen() or gzdopen(), and before any other calls that read or write the + file. The buffer memory allocation is always deferred to the first read or + write. Three times that size in buffer space is allocated. A larger buffer + size of, for example, 64K or 128K bytes will noticeably increase the speed + of decompression (reading). + + The new buffer size also affects the maximum length for gzprintf(). + + gzbuffer() returns 0 on success, or -1 on failure, such as being called + too late. +*/ + +ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy)); +/* + Dynamically update the compression level or strategy. See the description + of deflateInit2 for the meaning of these parameters. Previously provided + data is flushed before the parameter change. + + gzsetparams returns Z_OK if success, Z_STREAM_ERROR if the file was not + opened for writing, Z_ERRNO if there is an error writing the flushed data, + or Z_MEM_ERROR if there is a memory allocation error. +*/ + +ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len)); +/* + Reads the given number of uncompressed bytes from the compressed file. If + the input file is not in gzip format, gzread copies the given number of + bytes into the buffer directly from the file. + + After reaching the end of a gzip stream in the input, gzread will continue + to read, looking for another gzip stream. Any number of gzip streams may be + concatenated in the input file, and will all be decompressed by gzread(). + If something other than a gzip stream is encountered after a gzip stream, + that remaining trailing garbage is ignored (and no error is returned). + + gzread can be used to read a gzip file that is being concurrently written. + Upon reaching the end of the input, gzread will return with the available + data. If the error code returned by gzerror is Z_OK or Z_BUF_ERROR, then + gzclearerr can be used to clear the end of file indicator in order to permit + gzread to be tried again. Z_OK indicates that a gzip stream was completed + on the last gzread. Z_BUF_ERROR indicates that the input file ended in the + middle of a gzip stream. Note that gzread does not return -1 in the event + of an incomplete gzip stream. This error is deferred until gzclose(), which + will return Z_BUF_ERROR if the last gzread ended in the middle of a gzip + stream. Alternatively, gzerror can be used before gzclose to detect this + case. + + gzread returns the number of uncompressed bytes actually read, less than + len for end of file, or -1 for error. If len is too large to fit in an int, + then nothing is read, -1 is returned, and the error state is set to + Z_STREAM_ERROR. +*/ + +ZEXTERN z_size_t ZEXPORT gzfread OF((voidp buf, z_size_t size, z_size_t nitems, + gzFile file)); +/* + Read up to nitems items of size size from file to buf, otherwise operating + as gzread() does. This duplicates the interface of stdio's fread(), with + size_t request and return types. If the library defines size_t, then + z_size_t is identical to size_t. If not, then z_size_t is an unsigned + integer type that can contain a pointer. + + gzfread() returns the number of full items read of size size, or zero if + the end of the file was reached and a full item could not be read, or if + there was an error. gzerror() must be consulted if zero is returned in + order to determine if there was an error. If the multiplication of size and + nitems overflows, i.e. the product does not fit in a z_size_t, then nothing + is read, zero is returned, and the error state is set to Z_STREAM_ERROR. + + In the event that the end of file is reached and only a partial item is + available at the end, i.e. the remaining uncompressed data length is not a + multiple of size, then the final partial item is nevetheless read into buf + and the end-of-file flag is set. The length of the partial item read is not + provided, but could be inferred from the result of gztell(). This behavior + is the same as the behavior of fread() implementations in common libraries, + but it prevents the direct use of gzfread() to read a concurrently written + file, reseting and retrying on end-of-file, when size is not 1. +*/ + +ZEXTERN int ZEXPORT gzwrite OF((gzFile file, + voidpc buf, unsigned len)); +/* + Writes the given number of uncompressed bytes into the compressed file. + gzwrite returns the number of uncompressed bytes written or 0 in case of + error. +*/ + +ZEXTERN z_size_t ZEXPORT gzfwrite OF((voidpc buf, z_size_t size, + z_size_t nitems, gzFile file)); +/* + gzfwrite() writes nitems items of size size from buf to file, duplicating + the interface of stdio's fwrite(), with size_t request and return types. If + the library defines size_t, then z_size_t is identical to size_t. If not, + then z_size_t is an unsigned integer type that can contain a pointer. + + gzfwrite() returns the number of full items written of size size, or zero + if there was an error. If the multiplication of size and nitems overflows, + i.e. the product does not fit in a z_size_t, then nothing is written, zero + is returned, and the error state is set to Z_STREAM_ERROR. +*/ + +ZEXTERN int ZEXPORTVA gzprintf Z_ARG((gzFile file, const char *format, ...)); +/* + Converts, formats, and writes the arguments to the compressed file under + control of the format string, as in fprintf. gzprintf returns the number of + uncompressed bytes actually written, or a negative zlib error code in case + of error. The number of uncompressed bytes written is limited to 8191, or + one less than the buffer size given to gzbuffer(). The caller should assure + that this limit is not exceeded. If it is exceeded, then gzprintf() will + return an error (0) with nothing written. In this case, there may also be a + buffer overflow with unpredictable consequences, which is possible only if + zlib was compiled with the insecure functions sprintf() or vsprintf() + because the secure snprintf() or vsnprintf() functions were not available. + This can be determined using zlibCompileFlags(). +*/ + +ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s)); +/* + Writes the given null-terminated string to the compressed file, excluding + the terminating null character. + + gzputs returns the number of characters written, or -1 in case of error. +*/ + +ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len)); +/* + Reads bytes from the compressed file until len-1 characters are read, or a + newline character is read and transferred to buf, or an end-of-file + condition is encountered. If any characters are read or if len == 1, the + string is terminated with a null character. If no characters are read due + to an end-of-file or len < 1, then the buffer is left untouched. + + gzgets returns buf which is a null-terminated string, or it returns NULL + for end-of-file or in case of error. If there was an error, the contents at + buf are indeterminate. +*/ + +ZEXTERN int ZEXPORT gzputc OF((gzFile file, int c)); +/* + Writes c, converted to an unsigned char, into the compressed file. gzputc + returns the value that was written, or -1 in case of error. +*/ + +ZEXTERN int ZEXPORT gzgetc OF((gzFile file)); +/* + Reads one byte from the compressed file. gzgetc returns this byte or -1 + in case of end of file or error. This is implemented as a macro for speed. + As such, it does not do all of the checking the other functions do. I.e. + it does not check to see if file is NULL, nor whether the structure file + points to has been clobbered or not. +*/ + +ZEXTERN int ZEXPORT gzungetc OF((int c, gzFile file)); +/* + Push one character back onto the stream to be read as the first character + on the next read. At least one character of push-back is allowed. + gzungetc() returns the character pushed, or -1 on failure. gzungetc() will + fail if c is -1, and may fail if a character has been pushed but not read + yet. If gzungetc is used immediately after gzopen or gzdopen, at least the + output buffer size of pushed characters is allowed. (See gzbuffer above.) + The pushed character will be discarded if the stream is repositioned with + gzseek() or gzrewind(). +*/ + +ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush)); +/* + Flushes all pending output into the compressed file. The parameter flush + is as in the deflate() function. The return value is the zlib error number + (see function gzerror below). gzflush is only permitted when writing. + + If the flush parameter is Z_FINISH, the remaining data is written and the + gzip stream is completed in the output. If gzwrite() is called again, a new + gzip stream will be started in the output. gzread() is able to read such + concatenated gzip streams. + + gzflush should be called only when strictly necessary because it will + degrade compression if called too often. +*/ + +/* +ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file, + z_off_t offset, int whence)); + + Sets the starting position for the next gzread or gzwrite on the given + compressed file. The offset represents a number of bytes in the + uncompressed data stream. The whence parameter is defined as in lseek(2); + the value SEEK_END is not supported. + + If the file is opened for reading, this function is emulated but can be + extremely slow. If the file is opened for writing, only forward seeks are + supported; gzseek then compresses a sequence of zeroes up to the new + starting position. + + gzseek returns the resulting offset location as measured in bytes from + the beginning of the uncompressed stream, or -1 in case of error, in + particular if the file is opened for writing and the new starting position + would be before the current position. +*/ + +ZEXTERN int ZEXPORT gzrewind OF((gzFile file)); +/* + Rewinds the given file. This function is supported only for reading. + + gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET) +*/ + +/* +ZEXTERN z_off_t ZEXPORT gztell OF((gzFile file)); + + Returns the starting position for the next gzread or gzwrite on the given + compressed file. This position represents a number of bytes in the + uncompressed data stream, and is zero when starting, even if appending or + reading a gzip stream from the middle of a file using gzdopen(). + + gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR) +*/ + +/* +ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile file)); + + Returns the current offset in the file being read or written. This offset + includes the count of bytes that precede the gzip stream, for example when + appending or when using gzdopen() for reading. When reading, the offset + does not include as yet unused buffered input. This information can be used + for a progress indicator. On error, gzoffset() returns -1. +*/ + +ZEXTERN int ZEXPORT gzeof OF((gzFile file)); +/* + Returns true (1) if the end-of-file indicator has been set while reading, + false (0) otherwise. Note that the end-of-file indicator is set only if the + read tried to go past the end of the input, but came up short. Therefore, + just like feof(), gzeof() may return false even if there is no more data to + read, in the event that the last read request was for the exact number of + bytes remaining in the input file. This will happen if the input file size + is an exact multiple of the buffer size. + + If gzeof() returns true, then the read functions will return no more data, + unless the end-of-file indicator is reset by gzclearerr() and the input file + has grown since the previous end of file was detected. +*/ + +ZEXTERN int ZEXPORT gzdirect OF((gzFile file)); +/* + Returns true (1) if file is being copied directly while reading, or false + (0) if file is a gzip stream being decompressed. + + If the input file is empty, gzdirect() will return true, since the input + does not contain a gzip stream. + + If gzdirect() is used immediately after gzopen() or gzdopen() it will + cause buffers to be allocated to allow reading the file to determine if it + is a gzip file. Therefore if gzbuffer() is used, it should be called before + gzdirect(). + + When writing, gzdirect() returns true (1) if transparent writing was + requested ("wT" for the gzopen() mode), or false (0) otherwise. (Note: + gzdirect() is not needed when writing. Transparent writing must be + explicitly requested, so the application already knows the answer. When + linking statically, using gzdirect() will include all of the zlib code for + gzip file reading and decompression, which may not be desired.) +*/ + +ZEXTERN int ZEXPORT gzclose OF((gzFile file)); +/* + Flushes all pending output if necessary, closes the compressed file and + deallocates the (de)compression state. Note that once file is closed, you + cannot call gzerror with file, since its structures have been deallocated. + gzclose must not be called more than once on the same file, just as free + must not be called more than once on the same allocation. + + gzclose will return Z_STREAM_ERROR if file is not valid, Z_ERRNO on a + file operation error, Z_MEM_ERROR if out of memory, Z_BUF_ERROR if the + last read ended in the middle of a gzip stream, or Z_OK on success. +*/ + +ZEXTERN int ZEXPORT gzclose_r OF((gzFile file)); +ZEXTERN int ZEXPORT gzclose_w OF((gzFile file)); +/* + Same as gzclose(), but gzclose_r() is only for use when reading, and + gzclose_w() is only for use when writing or appending. The advantage to + using these instead of gzclose() is that they avoid linking in zlib + compression or decompression code that is not used when only reading or only + writing respectively. If gzclose() is used, then both compression and + decompression code will be included the application when linking to a static + zlib library. +*/ + +ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum)); +/* + Returns the error message for the last error which occurred on the given + compressed file. errnum is set to zlib error number. If an error occurred + in the file system and not in the compression library, errnum is set to + Z_ERRNO and the application may consult errno to get the exact error code. + + The application must not modify the returned string. Future calls to + this function may invalidate the previously returned string. If file is + closed, then the string previously returned by gzerror will no longer be + available. + + gzerror() should be used to distinguish errors from end-of-file for those + functions above that do not distinguish those cases in their return values. +*/ + +ZEXTERN void ZEXPORT gzclearerr OF((gzFile file)); +/* + Clears the error and end-of-file flags for file. This is analogous to the + clearerr() function in stdio. This is useful for continuing to read a gzip + file that is being written concurrently. +*/ + +#endif /* !Z_SOLO */ + + /* checksum functions */ + +/* + These functions are not related to compression but are exported + anyway because they might be useful in applications using the compression + library. +*/ + +ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len)); +/* + Update a running Adler-32 checksum with the bytes buf[0..len-1] and + return the updated checksum. If buf is Z_NULL, this function returns the + required initial value for the checksum. + + An Adler-32 checksum is almost as reliable as a CRC-32 but can be computed + much faster. + + Usage example: + + uLong adler = adler32(0L, Z_NULL, 0); + + while (read_buffer(buffer, length) != EOF) { + adler = adler32(adler, buffer, length); + } + if (adler != original_adler) error(); +*/ + +ZEXTERN uLong ZEXPORT adler32_z OF((uLong adler, const Bytef *buf, + z_size_t len)); +/* + Same as adler32(), but with a size_t length. +*/ + +/* +ZEXTERN uLong ZEXPORT adler32_combine OF((uLong adler1, uLong adler2, + z_off_t len2)); + + Combine two Adler-32 checksums into one. For two sequences of bytes, seq1 + and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for + each, adler1 and adler2. adler32_combine() returns the Adler-32 checksum of + seq1 and seq2 concatenated, requiring only adler1, adler2, and len2. Note + that the z_off_t type (like off_t) is a signed integer. If len2 is + negative, the result has no meaning or utility. +*/ + +ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len)); +/* + Update a running CRC-32 with the bytes buf[0..len-1] and return the + updated CRC-32. If buf is Z_NULL, this function returns the required + initial value for the crc. Pre- and post-conditioning (one's complement) is + performed within this function so it shouldn't be done by the application. + + Usage example: + + uLong crc = crc32(0L, Z_NULL, 0); + + while (read_buffer(buffer, length) != EOF) { + crc = crc32(crc, buffer, length); + } + if (crc != original_crc) error(); +*/ + +ZEXTERN uLong ZEXPORT crc32_z OF((uLong adler, const Bytef *buf, + z_size_t len)); +/* + Same as crc32(), but with a size_t length. +*/ + +/* +ZEXTERN uLong ZEXPORT crc32_combine OF((uLong crc1, uLong crc2, z_off_t len2)); + + Combine two CRC-32 check values into one. For two sequences of bytes, + seq1 and seq2 with lengths len1 and len2, CRC-32 check values were + calculated for each, crc1 and crc2. crc32_combine() returns the CRC-32 + check value of seq1 and seq2 concatenated, requiring only crc1, crc2, and + len2. +*/ + + + /* various hacks, don't look :) */ + +/* deflateInit and inflateInit are macros to allow checking the zlib version + * and the compiler's view of z_stream: + */ +ZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level, + const char *version, int stream_size)); +ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm, + const char *version, int stream_size)); +ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int level, int method, + int windowBits, int memLevel, + int strategy, const char *version, + int stream_size)); +ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int windowBits, + const char *version, int stream_size)); +ZEXTERN int ZEXPORT inflateBackInit_ OF((z_streamp strm, int windowBits, + unsigned char FAR *window, + const char *version, + int stream_size)); +#ifdef Z_PREFIX_SET +# define z_deflateInit(strm, level) \ + deflateInit_((strm), (level), ZLIB_VERSION, (int)sizeof(z_stream)) +# define z_inflateInit(strm) \ + inflateInit_((strm), ZLIB_VERSION, (int)sizeof(z_stream)) +# define z_deflateInit2(strm, level, method, windowBits, memLevel, strategy) \ + deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\ + (strategy), ZLIB_VERSION, (int)sizeof(z_stream)) +# define z_inflateInit2(strm, windowBits) \ + inflateInit2_((strm), (windowBits), ZLIB_VERSION, \ + (int)sizeof(z_stream)) +# define z_inflateBackInit(strm, windowBits, window) \ + inflateBackInit_((strm), (windowBits), (window), \ + ZLIB_VERSION, (int)sizeof(z_stream)) +#else +# define deflateInit(strm, level) \ + deflateInit_((strm), (level), ZLIB_VERSION, (int)sizeof(z_stream)) +# define inflateInit(strm) \ + inflateInit_((strm), ZLIB_VERSION, (int)sizeof(z_stream)) +# define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \ + deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\ + (strategy), ZLIB_VERSION, (int)sizeof(z_stream)) +# define inflateInit2(strm, windowBits) \ + inflateInit2_((strm), (windowBits), ZLIB_VERSION, \ + (int)sizeof(z_stream)) +# define inflateBackInit(strm, windowBits, window) \ + inflateBackInit_((strm), (windowBits), (window), \ + ZLIB_VERSION, (int)sizeof(z_stream)) +#endif + +#ifndef Z_SOLO + +/* gzgetc() macro and its supporting function and exposed data structure. Note + * that the real internal state is much larger than the exposed structure. + * This abbreviated structure exposes just enough for the gzgetc() macro. The + * user should not mess with these exposed elements, since their names or + * behavior could change in the future, perhaps even capriciously. They can + * only be used by the gzgetc() macro. You have been warned. + */ +struct gzFile_s { + unsigned have; + unsigned char *next; + z_off64_t pos; +}; +ZEXTERN int ZEXPORT gzgetc_ OF((gzFile file)); /* backward compatibility */ +#ifdef Z_PREFIX_SET +# undef z_gzgetc +# define z_gzgetc(g) \ + ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : (gzgetc)(g)) +#else +# define gzgetc(g) \ + ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : (gzgetc)(g)) +#endif + +/* provide 64-bit offset functions if _LARGEFILE64_SOURCE defined, and/or + * change the regular functions to 64 bits if _FILE_OFFSET_BITS is 64 (if + * both are true, the application gets the *64 functions, and the regular + * functions are changed to 64 bits) -- in case these are set on systems + * without large file support, _LFS64_LARGEFILE must also be true + */ +#ifdef Z_LARGE64 + ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *)); + ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int)); + ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile)); + ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile)); + ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off64_t)); + ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off64_t)); +#endif + +#if !defined(ZLIB_INTERNAL) && defined(Z_WANT64) +# ifdef Z_PREFIX_SET +# define z_gzopen z_gzopen64 +# define z_gzseek z_gzseek64 +# define z_gztell z_gztell64 +# define z_gzoffset z_gzoffset64 +# define z_adler32_combine z_adler32_combine64 +# define z_crc32_combine z_crc32_combine64 +# else +# define gzopen gzopen64 +# define gzseek gzseek64 +# define gztell gztell64 +# define gzoffset gzoffset64 +# define adler32_combine adler32_combine64 +# define crc32_combine crc32_combine64 +# endif +# ifndef Z_LARGE64 + ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *)); + ZEXTERN z_off_t ZEXPORT gzseek64 OF((gzFile, z_off_t, int)); + ZEXTERN z_off_t ZEXPORT gztell64 OF((gzFile)); + ZEXTERN z_off_t ZEXPORT gzoffset64 OF((gzFile)); + ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t)); + ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t)); +# endif +#else + ZEXTERN gzFile ZEXPORT gzopen OF((const char *, const char *)); + ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile, z_off_t, int)); + ZEXTERN z_off_t ZEXPORT gztell OF((gzFile)); + ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile)); + ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t)); + ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t)); +#endif + +#else /* Z_SOLO */ + + ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t)); + ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t)); + +#endif /* !Z_SOLO */ + +/* undocumented functions */ +ZEXTERN const char * ZEXPORT zError OF((int)); +ZEXTERN int ZEXPORT inflateSyncPoint OF((z_streamp)); +ZEXTERN const z_crc_t FAR * ZEXPORT get_crc_table OF((void)); +ZEXTERN int ZEXPORT inflateUndermine OF((z_streamp, int)); +ZEXTERN int ZEXPORT inflateValidate OF((z_streamp, int)); +ZEXTERN unsigned long ZEXPORT inflateCodesUsed OF ((z_streamp)); +ZEXTERN int ZEXPORT inflateResetKeep OF((z_streamp)); +ZEXTERN int ZEXPORT deflateResetKeep OF((z_streamp)); +#if (defined(_WIN32) || defined(__CYGWIN__)) && !defined(Z_SOLO) +ZEXTERN gzFile ZEXPORT gzopen_w OF((const wchar_t *path, + const char *mode)); +#endif +#if defined(STDC) || defined(Z_HAVE_STDARG_H) +# ifndef Z_SOLO +ZEXTERN int ZEXPORTVA gzvprintf Z_ARG((gzFile file, + const char *format, + va_list va)); +# endif +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* ZLIB_H */ diff --git a/librtmp/COPYING b/librtmp/COPYING new file mode 100644 index 0000000..00b4fed --- /dev/null +++ b/librtmp/COPYING @@ -0,0 +1,504 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! + + diff --git a/librtmp/Makefile b/librtmp/Makefile new file mode 100644 index 0000000..2c1c790 --- /dev/null +++ b/librtmp/Makefile @@ -0,0 +1,121 @@ +VERSION=v2.4 + +prefix=/usr/local + +incdir=$(prefix)/include/librtmp +bindir=$(prefix)/bin +libdir=$(prefix)/lib +mandir=$(prefix)/man +BINDIR=$(DESTDIR)$(bindir) +INCDIR=$(DESTDIR)$(incdir) +LIBDIR=$(DESTDIR)$(libdir) +MANDIR=$(DESTDIR)$(mandir) + +CC=$(CROSS_COMPILE)gcc +LD=$(CROSS_COMPILE)ld +AR=$(CROSS_COMPILE)ar + +SYS=posix +CRYPTO=OPENSSL +#CRYPTO=GNUTLS +DEF_POLARSSL=-DUSE_POLARSSL +DEF_OPENSSL=-DUSE_OPENSSL +DEF_GNUTLS=-DUSE_GNUTLS +DEF_=-DNO_CRYPTO +REQ_GNUTLS=gnutls,hogweed,nettle +REQ_OPENSSL=libssl,libcrypto +PUB_GNUTLS=-lgmp +LIBZ=-lz +LIBS_posix= +LIBS_darwin= +LIBS_mingw=-lws2_32 -lwinmm -lgdi32 +LIB_GNUTLS=-lgnutls -lhogweed -lnettle -lgmp $(LIBZ) +LIB_OPENSSL=-lssl -lcrypto $(LIBZ) +LIB_POLARSSL=-lpolarssl $(LIBZ) +PRIVATE_LIBS=$(LIBS_$(SYS)) +CRYPTO_LIB=$(LIB_$(CRYPTO)) $(PRIVATE_LIBS) +CRYPTO_REQ=$(REQ_$(CRYPTO)) +CRYPTO_DEF=$(DEF_$(CRYPTO)) +PUBLIC_LIBS=$(PUB_$(CRYPTO)) + +SO_VERSION=1 +SOX_posix=so +SOX_darwin=dylib +SOX_mingw=dll +SOX=$(SOX_$(SYS)) +SO_posix=.$(SOX).$(SO_VERSION) +SO_darwin=.$(SO_VERSION).$(SOX) +SO_mingw=-$(SO_VERSION).$(SOX) +SO_EXT=$(SO_$(SYS)) + +SODIR_posix=$(LIBDIR) +SODIR_darwin=$(LIBDIR) +SODIR_mingw=$(BINDIR) +SODIR=$(SODIR_$(SYS)) + +SO_LDFLAGS_posix=-shared -Wl,-soname,$@ +SO_LDFLAGS_darwin=-dynamiclib -twolevel_namespace -undefined dynamic_lookup \ + -fno-common -headerpad_max_install_names -install_name $(libdir)/$@ +SO_LDFLAGS_mingw=-shared -Wl,--out-implib,librtmp.dll.a +SO_LDFLAGS=$(SO_LDFLAGS_$(SYS)) + +INSTALL_IMPLIB_posix= +INSTALL_IMPLIB_darwin= +INSTALL_IMPLIB_mingw=cp librtmp.dll.a $(LIBDIR) +INSTALL_IMPLIB=$(INSTALL_IMPLIB_$(SYS)) + +SHARED=yes +SODEF_yes=-fPIC +SOLIB_yes=librtmp$(SO_EXT) +SOINST_yes=install_so +SO_DEF=$(SODEF_$(SHARED)) +SO_LIB=$(SOLIB_$(SHARED)) +SO_INST=$(SOINST_$(SHARED)) + +DEF=-DRTMPDUMP_VERSION=\"$(VERSION)\" $(CRYPTO_DEF) $(XDEF) +OPT=-O2 +CFLAGS=-Wall $(XCFLAGS) $(INC) $(DEF) $(OPT) $(SO_DEF) +LDFLAGS=$(XLDFLAGS) + + +OBJS=rtmp.o log.o amf.o hashswf.o parseurl.o + +all: librtmp.a $(SO_LIB) + +clean: + rm -f *.o *.a *.$(SOX) *$(SO_EXT) librtmp.pc + +librtmp.a: $(OBJS) + $(AR) rs $@ $? + +librtmp$(SO_EXT): $(OBJS) + $(CC) $(SO_LDFLAGS) $(LDFLAGS) -o $@ $^ $> $(CRYPTO_LIB) + ln -sf $@ librtmp.$(SOX) + +log.o: log.c log.h Makefile +rtmp.o: rtmp.c rtmp.h rtmp_sys.h handshake.h dh.h log.h amf.h Makefile +amf.o: amf.c amf.h bytes.h log.h Makefile +hashswf.o: hashswf.c http.h rtmp.h rtmp_sys.h Makefile +parseurl.o: parseurl.c rtmp.h rtmp_sys.h log.h Makefile + +librtmp.pc: librtmp.pc.in Makefile + sed -e "s;@prefix@;$(prefix);" -e "s;@libdir@;$(libdir);" \ + -e "s;@VERSION@;$(VERSION);" \ + -e "s;@CRYPTO_REQ@;$(CRYPTO_REQ);" \ + -e "s;@PUBLIC_LIBS@;$(PUBLIC_LIBS);" \ + -e "s;@PRIVATE_LIBS@;$(PRIVATE_LIBS);" librtmp.pc.in > $@ + +install: install_base $(SO_INST) + +install_base: librtmp.a librtmp.pc + -mkdir -p $(INCDIR) $(LIBDIR)/pkgconfig $(MANDIR)/man3 $(SODIR) + cp amf.h http.h log.h rtmp.h $(INCDIR) + cp librtmp.a $(LIBDIR) + cp librtmp.pc $(LIBDIR)/pkgconfig + cp librtmp.3 $(MANDIR)/man3 + +install_so: librtmp$(SO_EXT) + cp librtmp$(SO_EXT) $(SODIR) + $(INSTALL_IMPLIB) + cd $(SODIR); ln -sf librtmp$(SO_EXT) librtmp.$(SOX) + diff --git a/librtmp/amf.c b/librtmp/amf.c new file mode 100644 index 0000000..7954144 --- /dev/null +++ b/librtmp/amf.c @@ -0,0 +1,1311 @@ +/* + * Copyright (C) 2005-2008 Team XBMC + * http://www.xbmc.org + * Copyright (C) 2008-2009 Andrej Stepanchuk + * Copyright (C) 2009-2010 Howard Chu + * + * This file is part of librtmp. + * + * librtmp is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1, + * or (at your option) any later version. + * + * librtmp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with librtmp see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/lgpl.html + */ + +#include +#include +#include + +#include "rtmp_sys.h" +#include "amf.h" +#include "log.h" +#include "bytes.h" + +static const AMFObjectProperty AMFProp_Invalid = { {0, 0}, AMF_INVALID }; +static const AMFObject AMFObj_Invalid = { 0, 0 }; +static const AVal AV_empty = { 0, 0 }; + +/* Data is Big-Endian */ +unsigned short +AMF_DecodeInt16(const char *data) +{ + unsigned char *c = (unsigned char *) data; + unsigned short val; + val = (c[0] << 8) | c[1]; + return val; +} + +unsigned int +AMF_DecodeInt24(const char *data) +{ + unsigned char *c = (unsigned char *) data; + unsigned int val; + val = (c[0] << 16) | (c[1] << 8) | c[2]; + return val; +} + +unsigned int +AMF_DecodeInt32(const char *data) +{ + unsigned char *c = (unsigned char *)data; + unsigned int val; + val = (c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3]; + return val; +} + +void +AMF_DecodeString(const char *data, AVal *bv) +{ + bv->av_len = AMF_DecodeInt16(data); + bv->av_val = (bv->av_len > 0) ? (char *)data + 2 : NULL; +} + +void +AMF_DecodeLongString(const char *data, AVal *bv) +{ + bv->av_len = AMF_DecodeInt32(data); + bv->av_val = (bv->av_len > 0) ? (char *)data + 4 : NULL; +} + +double +AMF_DecodeNumber(const char *data) +{ + double dVal; +#if __FLOAT_WORD_ORDER == __BYTE_ORDER +#if __BYTE_ORDER == __BIG_ENDIAN + memcpy(&dVal, data, 8); +#elif __BYTE_ORDER == __LITTLE_ENDIAN + unsigned char *ci, *co; + ci = (unsigned char *)data; + co = (unsigned char *)&dVal; + co[0] = ci[7]; + co[1] = ci[6]; + co[2] = ci[5]; + co[3] = ci[4]; + co[4] = ci[3]; + co[5] = ci[2]; + co[6] = ci[1]; + co[7] = ci[0]; +#endif +#else +#if __BYTE_ORDER == __LITTLE_ENDIAN /* __FLOAT_WORD_ORER == __BIG_ENDIAN */ + unsigned char *ci, *co; + ci = (unsigned char *)data; + co = (unsigned char *)&dVal; + co[0] = ci[3]; + co[1] = ci[2]; + co[2] = ci[1]; + co[3] = ci[0]; + co[4] = ci[7]; + co[5] = ci[6]; + co[6] = ci[5]; + co[7] = ci[4]; +#else /* __BYTE_ORDER == __BIG_ENDIAN && __FLOAT_WORD_ORER == __LITTLE_ENDIAN */ + unsigned char *ci, *co; + ci = (unsigned char *)data; + co = (unsigned char *)&dVal; + co[0] = ci[4]; + co[1] = ci[5]; + co[2] = ci[6]; + co[3] = ci[7]; + co[4] = ci[0]; + co[5] = ci[1]; + co[6] = ci[2]; + co[7] = ci[3]; +#endif +#endif + return dVal; +} + +int +AMF_DecodeBoolean(const char *data) +{ + return *data != 0; +} + +char * +AMF_EncodeInt16(char *output, char *outend, short nVal) +{ + if (output+2 > outend) + return NULL; + + output[1] = nVal & 0xff; + output[0] = nVal >> 8; + return output+2; +} + +char * +AMF_EncodeInt24(char *output, char *outend, int nVal) +{ + if (output+3 > outend) + return NULL; + + output[2] = nVal & 0xff; + output[1] = nVal >> 8; + output[0] = nVal >> 16; + return output+3; +} + +char * +AMF_EncodeInt32(char *output, char *outend, int nVal) +{ + if (output+4 > outend) + return NULL; + + output[3] = nVal & 0xff; + output[2] = nVal >> 8; + output[1] = nVal >> 16; + output[0] = nVal >> 24; + return output+4; +} + +char * +AMF_EncodeString(char *output, char *outend, const AVal *bv) +{ + if ((bv->av_len < 65536 && output + 1 + 2 + bv->av_len > outend) || + output + 1 + 4 + bv->av_len > outend) + return NULL; + + if (bv->av_len < 65536) + { + *output++ = AMF_STRING; + + output = AMF_EncodeInt16(output, outend, bv->av_len); + } + else + { + *output++ = AMF_LONG_STRING; + + output = AMF_EncodeInt32(output, outend, bv->av_len); + } + memcpy(output, bv->av_val, bv->av_len); + output += bv->av_len; + + return output; +} + +char * +AMF_EncodeNumber(char *output, char *outend, double dVal) +{ + if (output+1+8 > outend) + return NULL; + + *output++ = AMF_NUMBER; /* type: Number */ + +#if __FLOAT_WORD_ORDER == __BYTE_ORDER +#if __BYTE_ORDER == __BIG_ENDIAN + memcpy(output, &dVal, 8); +#elif __BYTE_ORDER == __LITTLE_ENDIAN + { + unsigned char *ci, *co; + ci = (unsigned char *)&dVal; + co = (unsigned char *)output; + co[0] = ci[7]; + co[1] = ci[6]; + co[2] = ci[5]; + co[3] = ci[4]; + co[4] = ci[3]; + co[5] = ci[2]; + co[6] = ci[1]; + co[7] = ci[0]; + } +#endif +#else +#if __BYTE_ORDER == __LITTLE_ENDIAN /* __FLOAT_WORD_ORER == __BIG_ENDIAN */ + { + unsigned char *ci, *co; + ci = (unsigned char *)&dVal; + co = (unsigned char *)output; + co[0] = ci[3]; + co[1] = ci[2]; + co[2] = ci[1]; + co[3] = ci[0]; + co[4] = ci[7]; + co[5] = ci[6]; + co[6] = ci[5]; + co[7] = ci[4]; + } +#else /* __BYTE_ORDER == __BIG_ENDIAN && __FLOAT_WORD_ORER == __LITTLE_ENDIAN */ + { + unsigned char *ci, *co; + ci = (unsigned char *)&dVal; + co = (unsigned char *)output; + co[0] = ci[4]; + co[1] = ci[5]; + co[2] = ci[6]; + co[3] = ci[7]; + co[4] = ci[0]; + co[5] = ci[1]; + co[6] = ci[2]; + co[7] = ci[3]; + } +#endif +#endif + + return output+8; +} + +char * +AMF_EncodeBoolean(char *output, char *outend, int bVal) +{ + if (output+2 > outend) + return NULL; + + *output++ = AMF_BOOLEAN; + + *output++ = bVal ? 0x01 : 0x00; + + return output; +} + +char * +AMF_EncodeNamedString(char *output, char *outend, const AVal *strName, const AVal *strValue) +{ + if (output+2+strName->av_len > outend) + return NULL; + output = AMF_EncodeInt16(output, outend, strName->av_len); + + memcpy(output, strName->av_val, strName->av_len); + output += strName->av_len; + + return AMF_EncodeString(output, outend, strValue); +} + +char * +AMF_EncodeNamedNumber(char *output, char *outend, const AVal *strName, double dVal) +{ + if (output+2+strName->av_len > outend) + return NULL; + output = AMF_EncodeInt16(output, outend, strName->av_len); + + memcpy(output, strName->av_val, strName->av_len); + output += strName->av_len; + + return AMF_EncodeNumber(output, outend, dVal); +} + +char * +AMF_EncodeNamedBoolean(char *output, char *outend, const AVal *strName, int bVal) +{ + if (output+2+strName->av_len > outend) + return NULL; + output = AMF_EncodeInt16(output, outend, strName->av_len); + + memcpy(output, strName->av_val, strName->av_len); + output += strName->av_len; + + return AMF_EncodeBoolean(output, outend, bVal); +} + +void +AMFProp_GetName(AMFObjectProperty *prop, AVal *name) +{ + *name = prop->p_name; +} + +void +AMFProp_SetName(AMFObjectProperty *prop, AVal *name) +{ + prop->p_name = *name; +} + +AMFDataType +AMFProp_GetType(AMFObjectProperty *prop) +{ + return prop->p_type; +} + +double +AMFProp_GetNumber(AMFObjectProperty *prop) +{ + return prop->p_vu.p_number; +} + +int +AMFProp_GetBoolean(AMFObjectProperty *prop) +{ + return prop->p_vu.p_number != 0; +} + +void +AMFProp_GetString(AMFObjectProperty *prop, AVal *str) +{ + if (prop->p_type == AMF_STRING) + *str = prop->p_vu.p_aval; + else + *str = AV_empty; +} + +void +AMFProp_GetObject(AMFObjectProperty *prop, AMFObject *obj) +{ + if (prop->p_type == AMF_OBJECT) + *obj = prop->p_vu.p_object; + else + *obj = AMFObj_Invalid; +} + +int +AMFProp_IsValid(AMFObjectProperty *prop) +{ + return prop->p_type != AMF_INVALID; +} + +char * +AMFProp_Encode(AMFObjectProperty *prop, char *pBuffer, char *pBufEnd) +{ + if (prop->p_type == AMF_INVALID) + return NULL; + + if (prop->p_type != AMF_NULL && pBuffer + prop->p_name.av_len + 2 + 1 >= pBufEnd) + return NULL; + + if (prop->p_type != AMF_NULL && prop->p_name.av_len) + { + *pBuffer++ = prop->p_name.av_len >> 8; + *pBuffer++ = prop->p_name.av_len & 0xff; + memcpy(pBuffer, prop->p_name.av_val, prop->p_name.av_len); + pBuffer += prop->p_name.av_len; + } + + switch (prop->p_type) + { + case AMF_NUMBER: + pBuffer = AMF_EncodeNumber(pBuffer, pBufEnd, prop->p_vu.p_number); + break; + + case AMF_BOOLEAN: + pBuffer = AMF_EncodeBoolean(pBuffer, pBufEnd, prop->p_vu.p_number != 0); + break; + + case AMF_STRING: + pBuffer = AMF_EncodeString(pBuffer, pBufEnd, &prop->p_vu.p_aval); + break; + + case AMF_NULL: + if (pBuffer+1 >= pBufEnd) + return NULL; + *pBuffer++ = AMF_NULL; + break; + + case AMF_OBJECT: + pBuffer = AMF_Encode(&prop->p_vu.p_object, pBuffer, pBufEnd); + break; + + case AMF_ECMA_ARRAY: + pBuffer = AMF_EncodeEcmaArray(&prop->p_vu.p_object, pBuffer, pBufEnd); + break; + + case AMF_STRICT_ARRAY: + pBuffer = AMF_EncodeArray(&prop->p_vu.p_object, pBuffer, pBufEnd); + break; + + default: + RTMP_Log(RTMP_LOGERROR, "%s, invalid type. %d", __FUNCTION__, prop->p_type); + pBuffer = NULL; + }; + + return pBuffer; +} + +#define AMF3_INTEGER_MAX 268435455 +#define AMF3_INTEGER_MIN -268435456 + +int +AMF3ReadInteger(const char *data, int32_t *valp) +{ + int i = 0; + int32_t val = 0; + + while (i <= 2) + { /* handle first 3 bytes */ + if (data[i] & 0x80) + { /* byte used */ + val <<= 7; /* shift up */ + val |= (data[i] & 0x7f); /* add bits */ + i++; + } + else + { + break; + } + } + + if (i > 2) + { /* use 4th byte, all 8bits */ + val <<= 8; + val |= data[3]; + + /* range check */ + if (val > AMF3_INTEGER_MAX) + val -= (1 << 29); + } + else + { /* use 7bits of last unparsed byte (0xxxxxxx) */ + val <<= 7; + val |= data[i]; + } + + *valp = val; + + return i > 2 ? 4 : i + 1; +} + +int +AMF3ReadString(const char *data, AVal *str) +{ + int32_t ref = 0; + int len; + assert(str != 0); + + len = AMF3ReadInteger(data, &ref); + data += len; + + if ((ref & 0x1) == 0) + { /* reference: 0xxx */ + uint32_t refIndex = (ref >> 1); + RTMP_Log(RTMP_LOGDEBUG, + "%s, string reference, index: %d, not supported, ignoring!", + __FUNCTION__, refIndex); + str->av_val = NULL; + str->av_len = 0; + return len; + } + else + { + uint32_t nSize = (ref >> 1); + + str->av_val = (char *)data; + str->av_len = nSize; + + return len + nSize; + } + return len; +} + +int +AMF3Prop_Decode(AMFObjectProperty *prop, const char *pBuffer, int nSize, + int bDecodeName) +{ + int nOriginalSize = nSize; + AMF3DataType type; + + prop->p_name.av_len = 0; + prop->p_name.av_val = NULL; + + if (nSize == 0 || !pBuffer) + { + RTMP_Log(RTMP_LOGDEBUG, "empty buffer/no buffer pointer!"); + return -1; + } + + /* decode name */ + if (bDecodeName) + { + AVal name; + int nRes = AMF3ReadString(pBuffer, &name); + + if (name.av_len <= 0) + return nRes; + + nSize -= nRes; + if (nSize <= 0) + return -1; + prop->p_name = name; + pBuffer += nRes; + } + + /* decode */ + type = *pBuffer++; + nSize--; + + switch (type) + { + case AMF3_UNDEFINED: + case AMF3_NULL: + prop->p_type = AMF_NULL; + break; + case AMF3_FALSE: + prop->p_type = AMF_BOOLEAN; + prop->p_vu.p_number = 0.0; + break; + case AMF3_TRUE: + prop->p_type = AMF_BOOLEAN; + prop->p_vu.p_number = 1.0; + break; + case AMF3_INTEGER: + { + int32_t res = 0; + int len = AMF3ReadInteger(pBuffer, &res); + prop->p_vu.p_number = (double)res; + prop->p_type = AMF_NUMBER; + nSize -= len; + break; + } + case AMF3_DOUBLE: + if (nSize < 8) + return -1; + prop->p_vu.p_number = AMF_DecodeNumber(pBuffer); + prop->p_type = AMF_NUMBER; + nSize -= 8; + break; + case AMF3_STRING: + case AMF3_XML_DOC: + case AMF3_XML: + { + int len = AMF3ReadString(pBuffer, &prop->p_vu.p_aval); + prop->p_type = AMF_STRING; + nSize -= len; + break; + } + case AMF3_DATE: + { + int32_t res = 0; + int len = AMF3ReadInteger(pBuffer, &res); + + nSize -= len; + pBuffer += len; + + if ((res & 0x1) == 0) + { /* reference */ + uint32_t nIndex = (res >> 1); + RTMP_Log(RTMP_LOGDEBUG, "AMF3_DATE reference: %d, not supported!", nIndex); + } + else + { + if (nSize < 8) + return -1; + + prop->p_vu.p_number = AMF_DecodeNumber(pBuffer); + nSize -= 8; + prop->p_type = AMF_NUMBER; + } + break; + } + case AMF3_OBJECT: + { + int nRes = AMF3_Decode(&prop->p_vu.p_object, pBuffer, nSize, TRUE); + if (nRes == -1) + return -1; + nSize -= nRes; + prop->p_type = AMF_OBJECT; + break; + } + case AMF3_ARRAY: + case AMF3_BYTE_ARRAY: + default: + RTMP_Log(RTMP_LOGDEBUG, "%s - AMF3 unknown/unsupported datatype 0x%02x, @%p", + __FUNCTION__, (unsigned char)(*pBuffer), pBuffer); + return -1; + } + if (nSize < 0) + return -1; + + return nOriginalSize - nSize; +} + +int +AMFProp_Decode(AMFObjectProperty *prop, const char *pBuffer, int nSize, + int bDecodeName) +{ + int nOriginalSize = nSize; + int nRes; + + prop->p_name.av_len = 0; + prop->p_name.av_val = NULL; + + if (nSize == 0 || !pBuffer) + { + RTMP_Log(RTMP_LOGDEBUG, "%s: Empty buffer/no buffer pointer!", __FUNCTION__); + return -1; + } + + if (bDecodeName && nSize < 4) + { /* at least name (length + at least 1 byte) and 1 byte of data */ + RTMP_Log(RTMP_LOGDEBUG, + "%s: Not enough data for decoding with name, less than 4 bytes!", + __FUNCTION__); + return -1; + } + + if (bDecodeName) + { + unsigned short nNameSize = AMF_DecodeInt16(pBuffer); + if (nNameSize > nSize - 2) + { + RTMP_Log(RTMP_LOGDEBUG, + "%s: Name size out of range: namesize (%d) > len (%d) - 2", + __FUNCTION__, nNameSize, nSize); + return -1; + } + + AMF_DecodeString(pBuffer, &prop->p_name); + nSize -= 2 + nNameSize; + pBuffer += 2 + nNameSize; + } + + if (nSize == 0) + { + return -1; + } + + nSize--; + + prop->p_type = *pBuffer++; + switch (prop->p_type) + { + case AMF_NUMBER: + if (nSize < 8) + return -1; + prop->p_vu.p_number = AMF_DecodeNumber(pBuffer); + nSize -= 8; + break; + case AMF_BOOLEAN: + if (nSize < 1) + return -1; + prop->p_vu.p_number = (double)AMF_DecodeBoolean(pBuffer); + nSize--; + break; + case AMF_STRING: + { + unsigned short nStringSize = AMF_DecodeInt16(pBuffer); + + if (nSize < (long)nStringSize + 2) + return -1; + AMF_DecodeString(pBuffer, &prop->p_vu.p_aval); + nSize -= (2 + nStringSize); + break; + } + case AMF_OBJECT: + { + int nRes = AMF_Decode(&prop->p_vu.p_object, pBuffer, nSize, TRUE); + if (nRes == -1) + return -1; + nSize -= nRes; + break; + } + case AMF_MOVIECLIP: + { + RTMP_Log(RTMP_LOGERROR, "AMF_MOVIECLIP reserved!"); + return -1; + break; + } + case AMF_NULL: + case AMF_UNDEFINED: + case AMF_UNSUPPORTED: + prop->p_type = AMF_NULL; + break; + case AMF_REFERENCE: + { + RTMP_Log(RTMP_LOGERROR, "AMF_REFERENCE not supported!"); + return -1; + break; + } + case AMF_ECMA_ARRAY: + { + nSize -= 4; + + /* next comes the rest, mixed array has a final 0x000009 mark and names, so its an object */ + nRes = AMF_Decode(&prop->p_vu.p_object, pBuffer + 4, nSize, TRUE); + if (nRes == -1) + return -1; + nSize -= nRes; + break; + } + case AMF_OBJECT_END: + { + return -1; + break; + } + case AMF_STRICT_ARRAY: + { + unsigned int nArrayLen = AMF_DecodeInt32(pBuffer); + nSize -= 4; + + nRes = AMF_DecodeArray(&prop->p_vu.p_object, pBuffer + 4, nSize, + nArrayLen, FALSE); + if (nRes == -1) + return -1; + nSize -= nRes; + break; + } + case AMF_DATE: + { + RTMP_Log(RTMP_LOGDEBUG, "AMF_DATE"); + + if (nSize < 10) + return -1; + + prop->p_vu.p_number = AMF_DecodeNumber(pBuffer); + prop->p_UTCoffset = AMF_DecodeInt16(pBuffer + 8); + + nSize -= 10; + break; + } + case AMF_LONG_STRING: + case AMF_XML_DOC: + { + unsigned int nStringSize = AMF_DecodeInt32(pBuffer); + if (nSize < (long)nStringSize + 4) + return -1; + AMF_DecodeLongString(pBuffer, &prop->p_vu.p_aval); + nSize -= (4 + nStringSize); + if (prop->p_type == AMF_LONG_STRING) + prop->p_type = AMF_STRING; + break; + } + case AMF_RECORDSET: + { + RTMP_Log(RTMP_LOGERROR, "AMF_RECORDSET reserved!"); + return -1; + break; + } + case AMF_TYPED_OBJECT: + { + RTMP_Log(RTMP_LOGERROR, "AMF_TYPED_OBJECT not supported!"); + return -1; + break; + } + case AMF_AVMPLUS: + { + int nRes = AMF3_Decode(&prop->p_vu.p_object, pBuffer, nSize, TRUE); + if (nRes == -1) + return -1; + nSize -= nRes; + prop->p_type = AMF_OBJECT; + break; + } + default: + RTMP_Log(RTMP_LOGDEBUG, "%s - unknown datatype 0x%02x, @%p", __FUNCTION__, + prop->p_type, pBuffer - 1); + return -1; + } + + return nOriginalSize - nSize; +} + +void +AMFProp_Dump(AMFObjectProperty *prop) +{ + char strRes[256]; + char str[256]; + AVal name; + + if (prop->p_type == AMF_INVALID) + { + RTMP_Log(RTMP_LOGDEBUG, "Property: INVALID"); + return; + } + + if (prop->p_type == AMF_NULL) + { + RTMP_Log(RTMP_LOGDEBUG, "Property: NULL"); + return; + } + + if (prop->p_name.av_len) + { + name = prop->p_name; + } + else + { + name.av_val = "no-name."; + name.av_len = sizeof("no-name.") - 1; + } + if (name.av_len > 18) + name.av_len = 18; + + snprintf(strRes, 255, "Name: %18.*s, ", name.av_len, name.av_val); + + if (prop->p_type == AMF_OBJECT) + { + RTMP_Log(RTMP_LOGDEBUG, "Property: <%sOBJECT>", strRes); + AMF_Dump(&prop->p_vu.p_object); + return; + } + else if (prop->p_type == AMF_ECMA_ARRAY) + { + RTMP_Log(RTMP_LOGDEBUG, "Property: <%sECMA_ARRAY>", strRes); + AMF_Dump(&prop->p_vu.p_object); + return; + } + else if (prop->p_type == AMF_STRICT_ARRAY) + { + RTMP_Log(RTMP_LOGDEBUG, "Property: <%sSTRICT_ARRAY>", strRes); + AMF_Dump(&prop->p_vu.p_object); + return; + } + + switch (prop->p_type) + { + case AMF_NUMBER: + snprintf(str, 255, "NUMBER:\t%.2f", prop->p_vu.p_number); + break; + case AMF_BOOLEAN: + snprintf(str, 255, "BOOLEAN:\t%s", + prop->p_vu.p_number != 0.0 ? "TRUE" : "FALSE"); + break; + case AMF_STRING: + snprintf(str, 255, "STRING:\t%.*s", prop->p_vu.p_aval.av_len, + prop->p_vu.p_aval.av_val); + break; + case AMF_DATE: + snprintf(str, 255, "DATE:\ttimestamp: %.2f, UTC offset: %d", + prop->p_vu.p_number, prop->p_UTCoffset); + break; + default: + snprintf(str, 255, "INVALID TYPE 0x%02x", (unsigned char)prop->p_type); + } + + RTMP_Log(RTMP_LOGDEBUG, "Property: <%s%s>", strRes, str); +} + +void +AMFProp_Reset(AMFObjectProperty *prop) +{ + if (prop->p_type == AMF_OBJECT || prop->p_type == AMF_ECMA_ARRAY || + prop->p_type == AMF_STRICT_ARRAY) + AMF_Reset(&prop->p_vu.p_object); + else + { + prop->p_vu.p_aval.av_len = 0; + prop->p_vu.p_aval.av_val = NULL; + } + prop->p_type = AMF_INVALID; +} + +/* AMFObject */ + +char * +AMF_Encode(AMFObject *obj, char *pBuffer, char *pBufEnd) +{ + int i; + + if (pBuffer+4 >= pBufEnd) + return NULL; + + *pBuffer++ = AMF_OBJECT; + + for (i = 0; i < obj->o_num; i++) + { + char *res = AMFProp_Encode(&obj->o_props[i], pBuffer, pBufEnd); + if (res == NULL) + { + RTMP_Log(RTMP_LOGERROR, "AMF_Encode - failed to encode property in index %d", + i); + break; + } + else + { + pBuffer = res; + } + } + + if (pBuffer + 3 >= pBufEnd) + return NULL; /* no room for the end marker */ + + pBuffer = AMF_EncodeInt24(pBuffer, pBufEnd, AMF_OBJECT_END); + + return pBuffer; +} + +char * +AMF_EncodeEcmaArray(AMFObject *obj, char *pBuffer, char *pBufEnd) +{ + int i; + + if (pBuffer+4 >= pBufEnd) + return NULL; + + *pBuffer++ = AMF_ECMA_ARRAY; + + pBuffer = AMF_EncodeInt32(pBuffer, pBufEnd, obj->o_num); + + for (i = 0; i < obj->o_num; i++) + { + char *res = AMFProp_Encode(&obj->o_props[i], pBuffer, pBufEnd); + if (res == NULL) + { + RTMP_Log(RTMP_LOGERROR, "AMF_Encode - failed to encode property in index %d", + i); + break; + } + else + { + pBuffer = res; + } + } + + if (pBuffer + 3 >= pBufEnd) + return NULL; /* no room for the end marker */ + + pBuffer = AMF_EncodeInt24(pBuffer, pBufEnd, AMF_OBJECT_END); + + return pBuffer; +} + +char * +AMF_EncodeArray(AMFObject *obj, char *pBuffer, char *pBufEnd) +{ + int i; + + if (pBuffer+4 >= pBufEnd) + return NULL; + + *pBuffer++ = AMF_STRICT_ARRAY; + + pBuffer = AMF_EncodeInt32(pBuffer, pBufEnd, obj->o_num); + + for (i = 0; i < obj->o_num; i++) + { + char *res = AMFProp_Encode(&obj->o_props[i], pBuffer, pBufEnd); + if (res == NULL) + { + RTMP_Log(RTMP_LOGERROR, "AMF_Encode - failed to encode property in index %d", + i); + break; + } + else + { + pBuffer = res; + } + } + + //if (pBuffer + 3 >= pBufEnd) + // return NULL; /* no room for the end marker */ + + //pBuffer = AMF_EncodeInt24(pBuffer, pBufEnd, AMF_OBJECT_END); + + return pBuffer; +} + +int +AMF_DecodeArray(AMFObject *obj, const char *pBuffer, int nSize, + int nArrayLen, int bDecodeName) +{ + int nOriginalSize = nSize; + int bError = FALSE; + + obj->o_num = 0; + obj->o_props = NULL; + while (nArrayLen > 0) + { + AMFObjectProperty prop; + int nRes; + nArrayLen--; + + if (nSize <= 0) + { + bError = TRUE; + break; + } + nRes = AMFProp_Decode(&prop, pBuffer, nSize, bDecodeName); + if (nRes == -1) + { + bError = TRUE; + break; + } + else + { + nSize -= nRes; + pBuffer += nRes; + AMF_AddProp(obj, &prop); + } + } + if (bError) + return -1; + + return nOriginalSize - nSize; +} + +int +AMF3_Decode(AMFObject *obj, const char *pBuffer, int nSize, int bAMFData) +{ + int nOriginalSize = nSize; + int32_t ref; + int len; + + obj->o_num = 0; + obj->o_props = NULL; + if (bAMFData) + { + if (*pBuffer != AMF3_OBJECT) + RTMP_Log(RTMP_LOGERROR, + "AMF3 Object encapsulated in AMF stream does not start with AMF3_OBJECT!"); + pBuffer++; + nSize--; + } + + ref = 0; + len = AMF3ReadInteger(pBuffer, &ref); + pBuffer += len; + nSize -= len; + + if ((ref & 1) == 0) + { /* object reference, 0xxx */ + uint32_t objectIndex = (ref >> 1); + + RTMP_Log(RTMP_LOGDEBUG, "Object reference, index: %d", objectIndex); + } + else /* object instance */ + { + int32_t classRef = (ref >> 1); + + AMF3ClassDef cd = { {0, 0} + }; + AMFObjectProperty prop; + + if ((classRef & 0x1) == 0) + { /* class reference */ + uint32_t classIndex = (classRef >> 1); + RTMP_Log(RTMP_LOGDEBUG, "Class reference: %d", classIndex); + } + else + { + int32_t classExtRef = (classRef >> 1); + int i, cdnum; + + cd.cd_externalizable = (classExtRef & 0x1) == 1; + cd.cd_dynamic = ((classExtRef >> 1) & 0x1) == 1; + + cdnum = classExtRef >> 2; + + /* class name */ + + len = AMF3ReadString(pBuffer, &cd.cd_name); + nSize -= len; + pBuffer += len; + + /*std::string str = className; */ + + RTMP_Log(RTMP_LOGDEBUG, + "Class name: %s, externalizable: %d, dynamic: %d, classMembers: %d", + cd.cd_name.av_val, cd.cd_externalizable, cd.cd_dynamic, + cd.cd_num); + + for (i = 0; i < cdnum; i++) + { + AVal memberName; + if (nSize <=0) + { +invalid: + RTMP_Log(RTMP_LOGDEBUG, "%s, invalid class encoding!", + __FUNCTION__); + return nOriginalSize; + } + len = AMF3ReadString(pBuffer, &memberName); + RTMP_Log(RTMP_LOGDEBUG, "Member: %s", memberName.av_val); + AMF3CD_AddProp(&cd, &memberName); + nSize -= len; + pBuffer += len; + } + } + + /* add as referencable object */ + + if (cd.cd_externalizable) + { + int nRes; + AVal name = AVC("DEFAULT_ATTRIBUTE"); + + RTMP_Log(RTMP_LOGDEBUG, "Externalizable, TODO check"); + + nRes = AMF3Prop_Decode(&prop, pBuffer, nSize, FALSE); + if (nRes == -1) + RTMP_Log(RTMP_LOGDEBUG, "%s, failed to decode AMF3 property!", + __FUNCTION__); + else + { + nSize -= nRes; + pBuffer += nRes; + } + + AMFProp_SetName(&prop, &name); + AMF_AddProp(obj, &prop); + } + else + { + int nRes, i; + for (i = 0; i < cd.cd_num; i++) /* non-dynamic */ + { + if (nSize <=0) + goto invalid; + nRes = AMF3Prop_Decode(&prop, pBuffer, nSize, FALSE); + if (nRes == -1) + RTMP_Log(RTMP_LOGDEBUG, "%s, failed to decode AMF3 property!", + __FUNCTION__); + + AMFProp_SetName(&prop, AMF3CD_GetProp(&cd, i)); + AMF_AddProp(obj, &prop); + + pBuffer += nRes; + nSize -= nRes; + } + if (cd.cd_dynamic) + { + int len = 0; + + do + { + if (nSize <=0) + goto invalid; + nRes = AMF3Prop_Decode(&prop, pBuffer, nSize, TRUE); + AMF_AddProp(obj, &prop); + + pBuffer += nRes; + nSize -= nRes; + + len = prop.p_name.av_len; + } + while (len > 0); + } + } + RTMP_Log(RTMP_LOGDEBUG, "class object!"); + } + return nOriginalSize - nSize; +} + +int +AMF_Decode(AMFObject *obj, const char *pBuffer, int nSize, int bDecodeName) +{ + int nOriginalSize = nSize; + int bError = FALSE; /* if there is an error while decoding - try to at least find the end mark AMF_OBJECT_END */ + + obj->o_num = 0; + obj->o_props = NULL; + while (nSize > 0) + { + AMFObjectProperty prop; + int nRes; + + if (nSize >=3 && AMF_DecodeInt24(pBuffer) == AMF_OBJECT_END) + { + nSize -= 3; + bError = FALSE; + break; + } + + if (bError) + { + RTMP_Log(RTMP_LOGERROR, + "DECODING ERROR, IGNORING BYTES UNTIL NEXT KNOWN PATTERN!"); + nSize--; + pBuffer++; + continue; + } + + nRes = AMFProp_Decode(&prop, pBuffer, nSize, bDecodeName); + if (nRes == -1) + { + bError = TRUE; + break; + } + else + { + nSize -= nRes; + if (nSize < 0) + { + bError = TRUE; + break; + } + pBuffer += nRes; + AMF_AddProp(obj, &prop); + } + } + + if (bError) + return -1; + + return nOriginalSize - nSize; +} + +void +AMF_AddProp(AMFObject *obj, const AMFObjectProperty *prop) +{ + if (!(obj->o_num & 0x0f)) + obj->o_props = + realloc(obj->o_props, (obj->o_num + 16) * sizeof(AMFObjectProperty)); + memcpy(&obj->o_props[obj->o_num++], prop, sizeof(AMFObjectProperty)); +} + +int +AMF_CountProp(AMFObject *obj) +{ + return obj->o_num; +} + +AMFObjectProperty * +AMF_GetProp(AMFObject *obj, const AVal *name, int nIndex) +{ + if (nIndex >= 0) + { + if (nIndex < obj->o_num) + return &obj->o_props[nIndex]; + } + else + { + int n; + for (n = 0; n < obj->o_num; n++) + { + if (AVMATCH(&obj->o_props[n].p_name, name)) + return &obj->o_props[n]; + } + } + + return (AMFObjectProperty *)&AMFProp_Invalid; +} + +void +AMF_Dump(AMFObject *obj) +{ + int n; + RTMP_Log(RTMP_LOGDEBUG, "(object begin)"); + for (n = 0; n < obj->o_num; n++) + { + AMFProp_Dump(&obj->o_props[n]); + } + RTMP_Log(RTMP_LOGDEBUG, "(object end)"); +} + +void +AMF_Reset(AMFObject *obj) +{ + int n; + for (n = 0; n < obj->o_num; n++) + { + AMFProp_Reset(&obj->o_props[n]); + } + free(obj->o_props); + obj->o_props = NULL; + obj->o_num = 0; +} + + +/* AMF3ClassDefinition */ + +void +AMF3CD_AddProp(AMF3ClassDef *cd, AVal *prop) +{ + if (!(cd->cd_num & 0x0f)) + cd->cd_props = realloc(cd->cd_props, (cd->cd_num + 16) * sizeof(AVal)); + cd->cd_props[cd->cd_num++] = *prop; +} + +AVal * +AMF3CD_GetProp(AMF3ClassDef *cd, int nIndex) +{ + if (nIndex >= cd->cd_num) + return (AVal *)&AV_empty; + return &cd->cd_props[nIndex]; +} diff --git a/librtmp/amf.h b/librtmp/amf.h new file mode 100644 index 0000000..5de414b --- /dev/null +++ b/librtmp/amf.h @@ -0,0 +1,164 @@ +#ifndef __AMF_H__ +#define __AMF_H__ +/* + * Copyright (C) 2005-2008 Team XBMC + * http://www.xbmc.org + * Copyright (C) 2008-2009 Andrej Stepanchuk + * Copyright (C) 2009-2010 Howard Chu + * + * This file is part of librtmp. + * + * librtmp is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1, + * or (at your option) any later version. + * + * librtmp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with librtmp see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/lgpl.html + */ + +#include + +#ifndef TRUE +#define TRUE 1 +#define FALSE 0 +#endif + +#ifdef __cplusplus +extern "C" +{ +#endif + + typedef enum + { AMF_NUMBER = 0, AMF_BOOLEAN, AMF_STRING, AMF_OBJECT, + AMF_MOVIECLIP, /* reserved, not used */ + AMF_NULL, AMF_UNDEFINED, AMF_REFERENCE, AMF_ECMA_ARRAY, AMF_OBJECT_END, + AMF_STRICT_ARRAY, AMF_DATE, AMF_LONG_STRING, AMF_UNSUPPORTED, + AMF_RECORDSET, /* reserved, not used */ + AMF_XML_DOC, AMF_TYPED_OBJECT, + AMF_AVMPLUS, /* switch to AMF3 */ + AMF_INVALID = 0xff + } AMFDataType; + + typedef enum + { AMF3_UNDEFINED = 0, AMF3_NULL, AMF3_FALSE, AMF3_TRUE, + AMF3_INTEGER, AMF3_DOUBLE, AMF3_STRING, AMF3_XML_DOC, AMF3_DATE, + AMF3_ARRAY, AMF3_OBJECT, AMF3_XML, AMF3_BYTE_ARRAY + } AMF3DataType; + + typedef struct AVal + { + char *av_val; + int av_len; + } AVal; +#define AVC(str) {str,sizeof(str)-1} +#define AVMATCH(a1,a2) ((a1)->av_len == (a2)->av_len && !memcmp((a1)->av_val,(a2)->av_val,(a1)->av_len)) + + struct AMFObjectProperty; + + typedef struct AMFObject + { + int o_num; + struct AMFObjectProperty *o_props; + } AMFObject; + + typedef struct AMFObjectProperty + { + AVal p_name; + AMFDataType p_type; + union + { + double p_number; + AVal p_aval; + AMFObject p_object; + } p_vu; + int16_t p_UTCoffset; + } AMFObjectProperty; + + char *AMF_EncodeString(char *output, char *outend, const AVal * str); + char *AMF_EncodeNumber(char *output, char *outend, double dVal); + char *AMF_EncodeInt16(char *output, char *outend, short nVal); + char *AMF_EncodeInt24(char *output, char *outend, int nVal); + char *AMF_EncodeInt32(char *output, char *outend, int nVal); + char *AMF_EncodeBoolean(char *output, char *outend, int bVal); + + /* Shortcuts for AMFProp_Encode */ + char *AMF_EncodeNamedString(char *output, char *outend, const AVal * name, const AVal * value); + char *AMF_EncodeNamedNumber(char *output, char *outend, const AVal * name, double dVal); + char *AMF_EncodeNamedBoolean(char *output, char *outend, const AVal * name, int bVal); + + unsigned short AMF_DecodeInt16(const char *data); + unsigned int AMF_DecodeInt24(const char *data); + unsigned int AMF_DecodeInt32(const char *data); + void AMF_DecodeString(const char *data, AVal * str); + void AMF_DecodeLongString(const char *data, AVal * str); + int AMF_DecodeBoolean(const char *data); + double AMF_DecodeNumber(const char *data); + + char *AMF_Encode(AMFObject * obj, char *pBuffer, char *pBufEnd); + char *AMF_EncodeEcmaArray(AMFObject *obj, char *pBuffer, char *pBufEnd); + char *AMF_EncodeArray(AMFObject *obj, char *pBuffer, char *pBufEnd); + + int AMF_Decode(AMFObject * obj, const char *pBuffer, int nSize, + int bDecodeName); + int AMF_DecodeArray(AMFObject * obj, const char *pBuffer, int nSize, + int nArrayLen, int bDecodeName); + int AMF3_Decode(AMFObject * obj, const char *pBuffer, int nSize, + int bDecodeName); + void AMF_Dump(AMFObject * obj); + void AMF_Reset(AMFObject * obj); + + void AMF_AddProp(AMFObject * obj, const AMFObjectProperty * prop); + int AMF_CountProp(AMFObject * obj); + AMFObjectProperty *AMF_GetProp(AMFObject * obj, const AVal * name, + int nIndex); + + AMFDataType AMFProp_GetType(AMFObjectProperty * prop); + void AMFProp_SetNumber(AMFObjectProperty * prop, double dval); + void AMFProp_SetBoolean(AMFObjectProperty * prop, int bflag); + void AMFProp_SetString(AMFObjectProperty * prop, AVal * str); + void AMFProp_SetObject(AMFObjectProperty * prop, AMFObject * obj); + + void AMFProp_GetName(AMFObjectProperty * prop, AVal * name); + void AMFProp_SetName(AMFObjectProperty * prop, AVal * name); + double AMFProp_GetNumber(AMFObjectProperty * prop); + int AMFProp_GetBoolean(AMFObjectProperty * prop); + void AMFProp_GetString(AMFObjectProperty * prop, AVal * str); + void AMFProp_GetObject(AMFObjectProperty * prop, AMFObject * obj); + + int AMFProp_IsValid(AMFObjectProperty * prop); + + char *AMFProp_Encode(AMFObjectProperty * prop, char *pBuffer, char *pBufEnd); + int AMF3Prop_Decode(AMFObjectProperty * prop, const char *pBuffer, + int nSize, int bDecodeName); + int AMFProp_Decode(AMFObjectProperty * prop, const char *pBuffer, + int nSize, int bDecodeName); + + void AMFProp_Dump(AMFObjectProperty * prop); + void AMFProp_Reset(AMFObjectProperty * prop); + + typedef struct AMF3ClassDef + { + AVal cd_name; + char cd_externalizable; + char cd_dynamic; + int cd_num; + AVal *cd_props; + } AMF3ClassDef; + + void AMF3CD_AddProp(AMF3ClassDef * cd, AVal * prop); + AVal *AMF3CD_GetProp(AMF3ClassDef * cd, int idx); + +#ifdef __cplusplus +} +#endif + +#endif /* __AMF_H__ */ diff --git a/librtmp/bytes.h b/librtmp/bytes.h new file mode 100644 index 0000000..8c6e80d --- /dev/null +++ b/librtmp/bytes.h @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2005-2008 Team XBMC + * http://www.xbmc.org + * Copyright (C) 2008-2009 Andrej Stepanchuk + * Copyright (C) 2009-2010 Howard Chu + * + * This file is part of librtmp. + * + * librtmp is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1, + * or (at your option) any later version. + * + * librtmp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with librtmp see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/lgpl.html + */ + +#ifndef __BYTES_H__ +#define __BYTES_H__ + +#include + +#ifdef _WIN32 +/* Windows is little endian only */ +#define __LITTLE_ENDIAN 1234 +#define __BIG_ENDIAN 4321 +#define __BYTE_ORDER __LITTLE_ENDIAN +#define __FLOAT_WORD_ORDER __BYTE_ORDER + +typedef unsigned char uint8_t; + +#else /* !_WIN32 */ + +#include + +#if defined(BYTE_ORDER) && !defined(__BYTE_ORDER) +#define __BYTE_ORDER BYTE_ORDER +#endif + +#if defined(BIG_ENDIAN) && !defined(__BIG_ENDIAN) +#define __BIG_ENDIAN BIG_ENDIAN +#endif + +#if defined(LITTLE_ENDIAN) && !defined(__LITTLE_ENDIAN) +#define __LITTLE_ENDIAN LITTLE_ENDIAN +#endif + +#endif /* !_WIN32 */ + +/* define default endianness */ +#ifndef __LITTLE_ENDIAN +#define __LITTLE_ENDIAN 1234 +#endif + +#ifndef __BIG_ENDIAN +#define __BIG_ENDIAN 4321 +#endif + +#ifndef __BYTE_ORDER +#warning "Byte order not defined on your system, assuming little endian!" +#define __BYTE_ORDER __LITTLE_ENDIAN +#endif + +/* ok, we assume to have the same float word order and byte order if float word order is not defined */ +#ifndef __FLOAT_WORD_ORDER +#warning "Float word order not defined, assuming the same as byte order!" +#define __FLOAT_WORD_ORDER __BYTE_ORDER +#endif + +#if !defined(__BYTE_ORDER) || !defined(__FLOAT_WORD_ORDER) +#error "Undefined byte or float word order!" +#endif + +#if __FLOAT_WORD_ORDER != __BIG_ENDIAN && __FLOAT_WORD_ORDER != __LITTLE_ENDIAN +#error "Unknown/unsupported float word order!" +#endif + +#if __BYTE_ORDER != __BIG_ENDIAN && __BYTE_ORDER != __LITTLE_ENDIAN +#error "Unknown/unsupported byte order!" +#endif + +#endif + diff --git a/librtmp/dh.h b/librtmp/dh.h new file mode 100644 index 0000000..5fc3f32 --- /dev/null +++ b/librtmp/dh.h @@ -0,0 +1,376 @@ +/* RTMPDump - Diffie-Hellmann Key Exchange + * Copyright (C) 2009 Andrej Stepanchuk + * Copyright (C) 2009-2010 Howard Chu + * + * This file is part of librtmp. + * + * librtmp is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1, + * or (at your option) any later version. + * + * librtmp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with librtmp see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/lgpl.html + */ + +#include +#include +#include +#include +#include + +#ifdef USE_POLARSSL +#include +typedef mpi * MP_t; +#define MP_new(m) m = malloc(sizeof(mpi)); mpi_init(m) +#define MP_set_w(mpi, w) mpi_lset(mpi, w) +#define MP_cmp(u, v) mpi_cmp_mpi(u, v) +#define MP_set(u, v) mpi_copy(u, v) +#define MP_sub_w(mpi, w) mpi_sub_int(mpi, mpi, w) +#define MP_cmp_1(mpi) mpi_cmp_int(mpi, 1) +#define MP_modexp(r, y, q, p) mpi_exp_mod(r, y, q, p, NULL) +#define MP_free(mpi) mpi_free(mpi); free(mpi) +#define MP_gethex(u, hex, res) MP_new(u); res = mpi_read_string(u, 16, hex) == 0 +#define MP_bytes(u) mpi_size(u) +#define MP_setbin(u,buf,len) mpi_write_binary(u,buf,len) +#define MP_getbin(u,buf,len) MP_new(u); mpi_read_binary(u,buf,len) + +typedef struct MDH { + MP_t p; + MP_t g; + MP_t pub_key; + MP_t priv_key; + long length; + dhm_context ctx; +} MDH; + +#define MDH_new() calloc(1,sizeof(MDH)) +#define MDH_free(vp) {MDH *_dh = vp; dhm_free(&_dh->ctx); MP_free(_dh->p); MP_free(_dh->g); MP_free(_dh->pub_key); MP_free(_dh->priv_key); free(_dh);} + +static int MDH_generate_key(MDH *dh) +{ + unsigned char out[2]; + MP_set(&dh->ctx.P, dh->p); + MP_set(&dh->ctx.G, dh->g); + dh->ctx.len = 128; + dhm_make_public(&dh->ctx, 1024, out, 1, havege_random, &RTMP_TLS_ctx->hs); + MP_new(dh->pub_key); + MP_new(dh->priv_key); + MP_set(dh->pub_key, &dh->ctx.GX); + MP_set(dh->priv_key, &dh->ctx.X); + return 1; +} + +static int MDH_compute_key(uint8_t *secret, size_t len, MP_t pub, MDH *dh) +{ + MP_set(&dh->ctx.GY, pub); + dhm_calc_secret(&dh->ctx, secret, &len); + return 0; +} + +#elif defined(USE_GNUTLS) +#include +#include +#include +typedef mpz_ptr MP_t; +#define MP_new(m) m = malloc(sizeof(*m)); mpz_init2(m, 1) +#define MP_set_w(mpi, w) mpz_set_ui(mpi, w) +#define MP_cmp(u, v) mpz_cmp(u, v) +#define MP_set(u, v) mpz_set(u, v) +#define MP_sub_w(mpi, w) mpz_sub_ui(mpi, mpi, w) +#define MP_cmp_1(mpi) mpz_cmp_ui(mpi, 1) +#define MP_modexp(r, y, q, p) mpz_powm(r, y, q, p) +#define MP_free(mpi) mpz_clear(mpi); free(mpi) +#define MP_gethex(u, hex, res) u = malloc(sizeof(*u)); mpz_init2(u, 1); res = (mpz_set_str(u, hex, 16) == 0) +#define MP_bytes(u) (mpz_sizeinbase(u, 2) + 7) / 8 +#define MP_setbin(u,buf,len) nettle_mpz_get_str_256(len,buf,u) +#define MP_getbin(u,buf,len) u = malloc(sizeof(*u)); mpz_init2(u, 1); nettle_mpz_set_str_256_u(u,len,buf) + +typedef struct MDH { + MP_t p; + MP_t g; + MP_t pub_key; + MP_t priv_key; + long length; +} MDH; + +#define MDH_new() calloc(1,sizeof(MDH)) +#define MDH_free(dh) do {MP_free(((MDH*)(dh))->p); MP_free(((MDH*)(dh))->g); MP_free(((MDH*)(dh))->pub_key); MP_free(((MDH*)(dh))->priv_key); free(dh);} while(0) + +static int MDH_generate_key(MDH *dh) +{ + int num_bytes; + uint32_t seed; + gmp_randstate_t rs; + + num_bytes = (mpz_sizeinbase(dh->p, 2) + 7) / 8 - 1; + if (num_bytes <= 0 || num_bytes > 18000) + return 0; + + dh->priv_key = calloc(1, sizeof(*dh->priv_key)); + if (!dh->priv_key) + return 0; + mpz_init2(dh->priv_key, 1); + gnutls_rnd(GNUTLS_RND_RANDOM, &seed, sizeof(seed)); + gmp_randinit_mt(rs); + gmp_randseed_ui(rs, seed); + mpz_urandomb(dh->priv_key, rs, num_bytes); + gmp_randclear(rs); + + dh->pub_key = calloc(1, sizeof(*dh->pub_key)); + if (!dh->pub_key) + return 0; + mpz_init2(dh->pub_key, 1); + if (!dh->pub_key) { + mpz_clear(dh->priv_key); + free(dh->priv_key); + return 0; + } + + mpz_powm(dh->pub_key, dh->g, dh->priv_key, dh->p); + + return 1; +} + +static int MDH_compute_key(uint8_t *secret, size_t len, MP_t pub, MDH *dh) +{ + mpz_ptr k; + int num_bytes; + + num_bytes = (mpz_sizeinbase(dh->p, 2) + 7) / 8; + if (num_bytes <= 0 || num_bytes > 18000) + return -1; + + k = calloc(1, sizeof(*k)); + if (!k) + return -1; + mpz_init2(k, 1); + + mpz_powm(k, pub, dh->priv_key, dh->p); + nettle_mpz_get_str_256(len, secret, k); + mpz_clear(k); + free(k); + + /* return the length of the shared secret key like DH_compute_key */ + return len; +} + +#else /* USE_OPENSSL */ +#include +#include + +typedef BIGNUM * MP_t; +#define MP_new(m) m = BN_new() +#define MP_set_w(mpi, w) BN_set_word(mpi, w) +#define MP_cmp(u, v) BN_cmp(u, v) +#define MP_set(u, v) BN_copy(u, v) +#define MP_sub_w(mpi, w) BN_sub_word(mpi, w) +#define MP_cmp_1(mpi) BN_cmp(mpi, BN_value_one()) +#define MP_modexp(r, y, q, p) do {BN_CTX *ctx = BN_CTX_new(); BN_mod_exp(r, y, q, p, ctx); BN_CTX_free(ctx);} while(0) +#define MP_free(mpi) BN_free(mpi) +#define MP_gethex(u, hex, res) res = BN_hex2bn(&u, hex) +#define MP_bytes(u) BN_num_bytes(u) +#define MP_setbin(u,buf,len) BN_bn2bin(u,buf) +#define MP_getbin(u,buf,len) u = BN_bin2bn(buf,len,0) + +#define MDH DH +#define MDH_new() DH_new() +#define MDH_free(dh) DH_free(dh) +#define MDH_generate_key(dh) DH_generate_key(dh) +#define MDH_compute_key(secret, seclen, pub, dh) DH_compute_key(secret, pub, dh) + +#endif + +#include "log.h" +#include "dhgroups.h" + +/* RFC 2631, Section 2.1.5, http://www.ietf.org/rfc/rfc2631.txt */ +static int +isValidPublicKey(MP_t y, MP_t p, MP_t q) +{ + int ret = TRUE; + MP_t bn; + assert(y); + + MP_new(bn); + assert(bn); + + /* y must lie in [2,p-1] */ + MP_set_w(bn, 1); + if (MP_cmp(y, bn) < 0) + { + RTMP_Log(RTMP_LOGERROR, "DH public key must be at least 2"); + ret = FALSE; + goto failed; + } + + /* bn = p-2 */ + MP_set(bn, p); + MP_sub_w(bn, 1); + if (MP_cmp(y, bn) > 0) + { + RTMP_Log(RTMP_LOGERROR, "DH public key must be at most p-2"); + ret = FALSE; + goto failed; + } + + /* Verify with Sophie-Germain prime + * + * This is a nice test to make sure the public key position is calculated + * correctly. This test will fail in about 50% of the cases if applied to + * random data. + */ + if (q) + { + /* y must fulfill y^q mod p = 1 */ + MP_modexp(bn, y, q, p); + + if (MP_cmp_1(bn) != 0) + { + RTMP_Log(RTMP_LOGWARNING, "DH public key does not fulfill y^q mod p = 1"); + } + } + +failed: + MP_free(bn); + return ret; +} + +static MDH * +DHInit(int nKeyBits) +{ + size_t res; + MDH *dh = MDH_new(); + + if (!dh) + goto failed; + + MP_new(dh->g); + + if (!dh->g) + goto failed; + + MP_gethex(dh->p, P1024, res); /* prime P1024, see dhgroups.h */ + if (!res) + { + goto failed; + } + + MP_set_w(dh->g, 2); /* base 2 */ + + dh->length = nKeyBits; + return dh; + +failed: + if (dh) + MDH_free(dh); + + return 0; +} + +static int +DHGenerateKey(MDH *dh) +{ + size_t res = 0; + if (!dh) + return 0; + + while (!res) + { + MP_t q1 = NULL; + + if (!MDH_generate_key(dh)) + return 0; + + MP_gethex(q1, Q1024, res); + assert(res); + + res = isValidPublicKey(dh->pub_key, dh->p, q1); + if (!res) + { + MP_free(dh->pub_key); + MP_free(dh->priv_key); + dh->pub_key = dh->priv_key = 0; + } + + MP_free(q1); + } + return 1; +} + +/* fill pubkey with the public key in BIG ENDIAN order + * 00 00 00 00 00 x1 x2 x3 ..... + */ + +static int +DHGetPublicKey(MDH *dh, uint8_t *pubkey, size_t nPubkeyLen) +{ + int len; + if (!dh || !dh->pub_key) + return 0; + + len = MP_bytes(dh->pub_key); + if (len <= 0 || len > (int) nPubkeyLen) + return 0; + + memset(pubkey, 0, nPubkeyLen); + MP_setbin(dh->pub_key, pubkey + (nPubkeyLen - len), len); + return 1; +} + +#if 0 /* unused */ +static int +DHGetPrivateKey(MDH *dh, uint8_t *privkey, size_t nPrivkeyLen) +{ + if (!dh || !dh->priv_key) + return 0; + + int len = MP_bytes(dh->priv_key); + if (len <= 0 || len > (int) nPrivkeyLen) + return 0; + + memset(privkey, 0, nPrivkeyLen); + MP_setbin(dh->priv_key, privkey + (nPrivkeyLen - len), len); + return 1; +} +#endif + +/* computes the shared secret key from the private MDH value and the + * other party's public key (pubkey) + */ +static int +DHComputeSharedSecretKey(MDH *dh, uint8_t *pubkey, size_t nPubkeyLen, + uint8_t *secret) +{ + MP_t q1 = NULL, pubkeyBn = NULL; + size_t len; + int res; + + if (!dh || !secret || nPubkeyLen >= INT_MAX) + return -1; + + MP_getbin(pubkeyBn, pubkey, nPubkeyLen); + if (!pubkeyBn) + return -1; + + MP_gethex(q1, Q1024, len); + assert(len); + + if (isValidPublicKey(pubkeyBn, dh->p, q1)) + res = MDH_compute_key(secret, nPubkeyLen, pubkeyBn, dh); + else + res = -1; + + MP_free(q1); + MP_free(pubkeyBn); + + return res; +} diff --git a/librtmp/dhgroups.h b/librtmp/dhgroups.h new file mode 100644 index 0000000..2db3989 --- /dev/null +++ b/librtmp/dhgroups.h @@ -0,0 +1,199 @@ +/* librtmp - Diffie-Hellmann Key Exchange + * Copyright (C) 2009 Andrej Stepanchuk + * + * This file is part of librtmp. + * + * librtmp is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1, + * or (at your option) any later version. + * + * librtmp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with librtmp see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/lgpl.html + */ + +/* from RFC 3526, see http://www.ietf.org/rfc/rfc3526.txt */ + +/* 2^768 - 2 ^704 - 1 + 2^64 * { [2^638 pi] + 149686 } */ +#define P768 \ + "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \ + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \ + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \ + "E485B576625E7EC6F44C42E9A63A3620FFFFFFFFFFFFFFFF" + +/* 2^1024 - 2^960 - 1 + 2^64 * { [2^894 pi] + 129093 } */ +#define P1024 \ + "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \ + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \ + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \ + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \ + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381" \ + "FFFFFFFFFFFFFFFF" + +/* Group morder largest prime factor: */ +#define Q1024 \ + "7FFFFFFFFFFFFFFFE487ED5110B4611A62633145C06E0E68" \ + "948127044533E63A0105DF531D89CD9128A5043CC71A026E" \ + "F7CA8CD9E69D218D98158536F92F8A1BA7F09AB6B6A8E122" \ + "F242DABB312F3F637A262174D31BF6B585FFAE5B7A035BF6" \ + "F71C35FDAD44CFD2D74F9208BE258FF324943328F67329C0" \ + "FFFFFFFFFFFFFFFF" + +/* 2^1536 - 2^1472 - 1 + 2^64 * { [2^1406 pi] + 741804 } */ +#define P1536 \ + "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \ + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \ + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \ + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \ + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \ + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \ + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \ + "670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF" + +/* 2^2048 - 2^1984 - 1 + 2^64 * { [2^1918 pi] + 124476 } */ +#define P2048 \ + "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \ + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \ + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \ + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \ + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \ + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \ + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \ + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" \ + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" \ + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" \ + "15728E5A8AACAA68FFFFFFFFFFFFFFFF" + +/* 2^3072 - 2^3008 - 1 + 2^64 * { [2^2942 pi] + 1690314 } */ +#define P3072 \ + "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \ + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \ + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \ + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \ + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \ + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \ + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \ + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" \ + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" \ + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" \ + "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" \ + "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" \ + "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" \ + "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" \ + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" \ + "43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF" + +/* 2^4096 - 2^4032 - 1 + 2^64 * { [2^3966 pi] + 240904 } */ +#define P4096 \ + "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \ + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \ + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \ + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \ + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \ + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \ + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \ + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" \ + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" \ + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" \ + "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" \ + "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" \ + "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" \ + "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" \ + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" \ + "43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7" \ + "88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA" \ + "2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6" \ + "287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED" \ + "1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9" \ + "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199" \ + "FFFFFFFFFFFFFFFF" + +/* 2^6144 - 2^6080 - 1 + 2^64 * { [2^6014 pi] + 929484 } */ +#define P6144 \ + "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \ + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \ + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \ + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \ + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \ + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \ + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \ + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" \ + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" \ + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" \ + "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" \ + "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" \ + "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" \ + "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" \ + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" \ + "43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7" \ + "88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA" \ + "2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6" \ + "287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED" \ + "1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9" \ + "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492" \ + "36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD" \ + "F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831" \ + "179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B" \ + "DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF" \ + "5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6" \ + "D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3" \ + "23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA" \ + "CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328" \ + "06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C" \ + "DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE" \ + "12BF2D5B0B7474D6E694F91E6DCC4024FFFFFFFFFFFFFFFF" + +/* 2^8192 - 2^8128 - 1 + 2^64 * { [2^8062 pi] + 4743158 } */ +#define P8192 \ + "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \ + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \ + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \ + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \ + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \ + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \ + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \ + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" \ + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" \ + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" \ + "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" \ + "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" \ + "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" \ + "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" \ + "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" \ + "43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7" \ + "88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA" \ + "2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6" \ + "287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED" \ + "1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9" \ + "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492" \ + "36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD" \ + "F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831" \ + "179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B" \ + "DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF" \ + "5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6" \ + "D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3" \ + "23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA" \ + "CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328" \ + "06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C" \ + "DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE" \ + "12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E4" \ + "38777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300" \ + "741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F568" \ + "3423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9" \ + "22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B" \ + "4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A" \ + "062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A36" \ + "4597E899A0255DC164F31CC50846851DF9AB48195DED7EA1" \ + "B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F92" \ + "4009438B481C6CD7889A002ED5EE382BC9190DA6FC026E47" \ + "9558E4475677E9AA9E3050E2765694DFC81F56E880B96E71" \ + "60C980DD98EDD3DFFFFFFFFFFFFFFFFF" + diff --git a/librtmp/handshake.h b/librtmp/handshake.h new file mode 100644 index 0000000..0438486 --- /dev/null +++ b/librtmp/handshake.h @@ -0,0 +1,1419 @@ +/* + * Copyright (C) 2008-2009 Andrej Stepanchuk + * Copyright (C) 2009-2010 Howard Chu + * Copyright (C) 2010 2a665470ced7adb7156fcef47f8199a6371c117b8a79e399a2771e0b36384090 + * + * This file is part of librtmp. + * + * librtmp is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1, + * or (at your option) any later version. + * + * librtmp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with librtmp see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/lgpl.html + */ + +/* This file is #included in rtmp.c, it is not meant to be compiled alone */ + +#ifdef USE_POLARSSL +#include +#include +#ifndef SHA256_DIGEST_LENGTH +#define SHA256_DIGEST_LENGTH 32 +#endif +#define HMAC_CTX sha2_context +#define HMAC_setup(ctx, key, len) sha2_hmac_starts(&ctx, (unsigned char *)key, len, 0) +#define HMAC_crunch(ctx, buf, len) sha2_hmac_update(&ctx, buf, len) +#define HMAC_finish(ctx, dig, dlen) dlen = SHA256_DIGEST_LENGTH; sha2_hmac_finish(&ctx, dig) + +typedef arc4_context * RC4_handle; +#define RC4_alloc(h) *h = malloc(sizeof(arc4_context)) +#define RC4_setkey(h,l,k) arc4_setup(h,k,l) +#define RC4_encrypt(h,l,d) arc4_crypt(h,l,(unsigned char *)d,(unsigned char *)d) +#define RC4_encrypt2(h,l,s,d) arc4_crypt(h,l,(unsigned char *)s,(unsigned char *)d) +#define RC4_free(h) free(h) + +#elif defined(USE_GNUTLS) +#include +#include +#ifndef SHA256_DIGEST_LENGTH +#define SHA256_DIGEST_LENGTH 32 +#endif +#undef HMAC_CTX +#define HMAC_CTX struct hmac_sha256_ctx +#define HMAC_setup(ctx, key, len) hmac_sha256_set_key(&ctx, len, key) +#define HMAC_crunch(ctx, buf, len) hmac_sha256_update(&ctx, len, buf) +#define HMAC_finish(ctx, dig, dlen) dlen = SHA256_DIGEST_LENGTH; hmac_sha256_digest(&ctx, SHA256_DIGEST_LENGTH, dig) +#define HMAC_close(ctx) + +typedef struct arcfour_ctx* RC4_handle; +#define RC4_alloc(h) *h = malloc(sizeof(struct arcfour_ctx)) +#define RC4_setkey(h,l,k) arcfour_set_key(h, l, k) +#define RC4_encrypt(h,l,d) arcfour_crypt(h,l,(uint8_t *)d,(uint8_t *)d) +#define RC4_encrypt2(h,l,s,d) arcfour_crypt(h,l,(uint8_t *)d,(uint8_t *)s) +#define RC4_free(h) free(h) + +#else /* USE_OPENSSL */ +#include +#include +#include +#if OPENSSL_VERSION_NUMBER < 0x0090800 || !defined(SHA256_DIGEST_LENGTH) +#error Your OpenSSL is too old, need 0.9.8 or newer with SHA256 +#endif +#define HMAC_setup(ctx, key, len) HMAC_CTX_init(&ctx); HMAC_Init_ex(&ctx, key, len, EVP_sha256(), 0) +#define HMAC_crunch(ctx, buf, len) HMAC_Update(&ctx, buf, len) +#define HMAC_finish(ctx, dig, dlen) HMAC_Final(&ctx, dig, &dlen); HMAC_CTX_cleanup(&ctx) + +typedef RC4_KEY * RC4_handle; +#define RC4_alloc(h) *h = malloc(sizeof(RC4_KEY)) +#define RC4_setkey(h,l,k) RC4_set_key(h,l,k) +#define RC4_encrypt(h,l,d) RC4(h,l,(uint8_t *)d,(uint8_t *)d) +#define RC4_encrypt2(h,l,s,d) RC4(h,l,(uint8_t *)s,(uint8_t *)d) +#define RC4_free(h) free(h) +#endif + +#define FP10 + +#include "dh.h" + +static const uint8_t GenuineFMSKey[] = { + 0x47, 0x65, 0x6e, 0x75, 0x69, 0x6e, 0x65, 0x20, 0x41, 0x64, 0x6f, 0x62, + 0x65, 0x20, 0x46, 0x6c, + 0x61, 0x73, 0x68, 0x20, 0x4d, 0x65, 0x64, 0x69, 0x61, 0x20, 0x53, 0x65, + 0x72, 0x76, 0x65, 0x72, + 0x20, 0x30, 0x30, 0x31, /* Genuine Adobe Flash Media Server 001 */ + + 0xf0, 0xee, 0xc2, 0x4a, 0x80, 0x68, 0xbe, 0xe8, 0x2e, 0x00, 0xd0, 0xd1, + 0x02, 0x9e, 0x7e, 0x57, 0x6e, 0xec, 0x5d, 0x2d, 0x29, 0x80, 0x6f, 0xab, + 0x93, 0xb8, 0xe6, 0x36, + 0xcf, 0xeb, 0x31, 0xae +}; /* 68 */ + +static const uint8_t GenuineFPKey[] = { + 0x47, 0x65, 0x6E, 0x75, 0x69, 0x6E, 0x65, 0x20, 0x41, 0x64, 0x6F, 0x62, + 0x65, 0x20, 0x46, 0x6C, + 0x61, 0x73, 0x68, 0x20, 0x50, 0x6C, 0x61, 0x79, 0x65, 0x72, 0x20, 0x30, + 0x30, 0x31, /* Genuine Adobe Flash Player 001 */ + 0xF0, 0xEE, + 0xC2, 0x4A, 0x80, 0x68, 0xBE, 0xE8, 0x2E, 0x00, 0xD0, 0xD1, 0x02, 0x9E, + 0x7E, 0x57, 0x6E, 0xEC, + 0x5D, 0x2D, 0x29, 0x80, 0x6F, 0xAB, 0x93, 0xB8, 0xE6, 0x36, 0xCF, 0xEB, + 0x31, 0xAE +}; /* 62 */ + +static void InitRC4Encryption + (uint8_t * secretKey, + uint8_t * pubKeyIn, + uint8_t * pubKeyOut, RC4_handle *rc4keyIn, RC4_handle *rc4keyOut) +{ + uint8_t digest[SHA256_DIGEST_LENGTH]; + unsigned int digestLen = 0; + HMAC_CTX ctx; + + RC4_alloc(rc4keyIn); + RC4_alloc(rc4keyOut); + + HMAC_setup(ctx, secretKey, 128); + HMAC_crunch(ctx, pubKeyIn, 128); + HMAC_finish(ctx, digest, digestLen); + + RTMP_Log(RTMP_LOGDEBUG, "RC4 Out Key: "); + RTMP_LogHex(RTMP_LOGDEBUG, digest, 16); + + RC4_setkey(*rc4keyOut, 16, digest); + + HMAC_setup(ctx, secretKey, 128); + HMAC_crunch(ctx, pubKeyOut, 128); + HMAC_finish(ctx, digest, digestLen); + + RTMP_Log(RTMP_LOGDEBUG, "RC4 In Key: "); + RTMP_LogHex(RTMP_LOGDEBUG, digest, 16); + + RC4_setkey(*rc4keyIn, 16, digest); +} + +typedef unsigned int (getoff)(uint8_t *buf, unsigned int len); + +static unsigned int +GetDHOffset2(uint8_t *handshake, unsigned int len) +{ + unsigned int offset = 0; + uint8_t *ptr = handshake + 768; + unsigned int res; + + assert(RTMP_SIG_SIZE <= len); + + offset += (*ptr); + ptr++; + offset += (*ptr); + ptr++; + offset += (*ptr); + ptr++; + offset += (*ptr); + + res = (offset % 632) + 8; + + if (res + 128 > 767) + { + RTMP_Log(RTMP_LOGERROR, + "%s: Couldn't calculate correct DH offset (got %d), exiting!", + __FUNCTION__, res); + exit(1); + } + return res; +} + +static unsigned int +GetDigestOffset2(uint8_t *handshake, unsigned int len) +{ + unsigned int offset = 0; + uint8_t *ptr = handshake + 772; + unsigned int res; + + offset += (*ptr); + ptr++; + offset += (*ptr); + ptr++; + offset += (*ptr); + ptr++; + offset += (*ptr); + + res = (offset % 728) + 776; + + if (res + 32 > 1535) + { + RTMP_Log(RTMP_LOGERROR, + "%s: Couldn't calculate correct digest offset (got %d), exiting", + __FUNCTION__, res); + exit(1); + } + return res; +} + +static unsigned int +GetDHOffset1(uint8_t *handshake, unsigned int len) +{ + unsigned int offset = 0; + uint8_t *ptr = handshake + 1532; + unsigned int res; + + assert(RTMP_SIG_SIZE <= len); + + offset += (*ptr); + ptr++; + offset += (*ptr); + ptr++; + offset += (*ptr); + ptr++; + offset += (*ptr); + + res = (offset % 632) + 772; + + if (res + 128 > 1531) + { + RTMP_Log(RTMP_LOGERROR, "%s: Couldn't calculate DH offset (got %d), exiting!", + __FUNCTION__, res); + exit(1); + } + + return res; +} + +static unsigned int +GetDigestOffset1(uint8_t *handshake, unsigned int len) +{ + unsigned int offset = 0; + uint8_t *ptr = handshake + 8; + unsigned int res; + + assert(12 <= len); + + offset += (*ptr); + ptr++; + offset += (*ptr); + ptr++; + offset += (*ptr); + ptr++; + offset += (*ptr); + + res = (offset % 728) + 12; + + if (res + 32 > 771) + { + RTMP_Log(RTMP_LOGERROR, + "%s: Couldn't calculate digest offset (got %d), exiting!", + __FUNCTION__, res); + exit(1); + } + + return res; +} + +static getoff *digoff[] = {GetDigestOffset1, GetDigestOffset2}; +static getoff *dhoff[] = {GetDHOffset1, GetDHOffset2}; + +static void +HMACsha256(const uint8_t *message, size_t messageLen, const uint8_t *key, + size_t keylen, uint8_t *digest) +{ + unsigned int digestLen; + HMAC_CTX ctx; + + HMAC_setup(ctx, key, keylen); + HMAC_crunch(ctx, message, messageLen); + HMAC_finish(ctx, digest, digestLen); + + assert(digestLen == 32); +} + +static void +CalculateDigest(unsigned int digestPos, uint8_t *handshakeMessage, + const uint8_t *key, size_t keyLen, uint8_t *digest) +{ + const int messageLen = RTMP_SIG_SIZE - SHA256_DIGEST_LENGTH; + uint8_t message[RTMP_SIG_SIZE - SHA256_DIGEST_LENGTH]; + + memcpy(message, handshakeMessage, digestPos); + memcpy(message + digestPos, + &handshakeMessage[digestPos + SHA256_DIGEST_LENGTH], + messageLen - digestPos); + + HMACsha256(message, messageLen, key, keyLen, digest); +} + +static int +VerifyDigest(unsigned int digestPos, uint8_t *handshakeMessage, const uint8_t *key, + size_t keyLen) +{ + uint8_t calcDigest[SHA256_DIGEST_LENGTH]; + + CalculateDigest(digestPos, handshakeMessage, key, keyLen, calcDigest); + + return memcmp(&handshakeMessage[digestPos], calcDigest, + SHA256_DIGEST_LENGTH) == 0; +} + +/* handshake + * + * Type = [1 bytes] plain: 0x03, encrypted: 0x06, 0x08, 0x09 + * -------------------------------------------------------------------- [1536 bytes] + * Uptime = [4 bytes] big endian unsigned number, uptime + * Version = [4 bytes] each byte represents a version number, e.g. 9.0.124.0 + * ... + * + */ + +static const uint32_t rtmpe8_keys[16][4] = { + {0xbff034b2, 0x11d9081f, 0xccdfb795, 0x748de732}, + {0x086a5eb6, 0x1743090e, 0x6ef05ab8, 0xfe5a39e2}, + {0x7b10956f, 0x76ce0521, 0x2388a73a, 0x440149a1}, + {0xa943f317, 0xebf11bb2, 0xa691a5ee, 0x17f36339}, + {0x7a30e00a, 0xb529e22c, 0xa087aea5, 0xc0cb79ac}, + {0xbdce0c23, 0x2febdeff, 0x1cfaae16, 0x1123239d}, + {0x55dd3f7b, 0x77e7e62e, 0x9bb8c499, 0xc9481ee4}, + {0x407bb6b4, 0x71e89136, 0xa7aebf55, 0xca33b839}, + {0xfcf6bdc3, 0xb63c3697, 0x7ce4f825, 0x04d959b2}, + {0x28e091fd, 0x41954c4c, 0x7fb7db00, 0xe3a066f8}, + {0x57845b76, 0x4f251b03, 0x46d45bcd, 0xa2c30d29}, + {0x0acceef8, 0xda55b546, 0x03473452, 0x5863713b}, + {0xb82075dc, 0xa75f1fee, 0xd84268e8, 0xa72a44cc}, + {0x07cf6e9e, 0xa16d7b25, 0x9fa7ae6c, 0xd92f5629}, + {0xfeb1eae4, 0x8c8c3ce1, 0x4e0064a7, 0x6a387c2a}, + {0x893a9427, 0xcc3013a2, 0xf106385b, 0xa829f927} +}; + +/* RTMPE type 8 uses XTEA on the regular signature + * http://en.wikipedia.org/wiki/XTEA + */ +static void rtmpe8_sig(uint8_t *in, uint8_t *out, int keyid) +{ + unsigned int i, num_rounds = 32; + uint32_t v0, v1, sum=0, delta=0x9E3779B9; + uint32_t const *k; + + v0 = in[0] | (in[1] << 8) | (in[2] << 16) | (in[3] << 24); + v1 = in[4] | (in[5] << 8) | (in[6] << 16) | (in[7] << 24); + k = rtmpe8_keys[keyid]; + + for (i=0; i < num_rounds; i++) { + v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]); + sum += delta; + v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]); + } + + out[0] = v0; v0 >>= 8; + out[1] = v0; v0 >>= 8; + out[2] = v0; v0 >>= 8; + out[3] = v0; + + out[4] = v1; v1 >>= 8; + out[5] = v1; v1 >>= 8; + out[6] = v1; v1 >>= 8; + out[7] = v1; +} + +/* RTMPE type 9 uses Blowfish on the regular signature + * http://en.wikipedia.org/wiki/Blowfish_(cipher) + */ +#define BF_ROUNDS 16 +typedef struct bf_key { + uint32_t s[4][256]; + uint32_t p[BF_ROUNDS+2]; +} bf_key; + +static const uint32_t bf_sinit[][256] = { + + /* S-Box 0 */ + { 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, 0xb8e1afed, 0x6a267e96, + 0xba7c9045, 0xf12c7f99, 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16, + 0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e, 0x0d95748f, 0x728eb658, + 0x718bcd58, 0x82154aee, 0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013, + 0xc5d1b023, 0x286085f0, 0xca417918, 0xb8db38ef, 0x8e79dcb0, 0x603a180e, + 0x6c9e0e8b, 0xb01e8a3e, 0xd71577c1, 0xbd314b27, 0x78af2fda, 0x55605c60, + 0xe65525f3, 0xaa55ab94, 0x57489862, 0x63e81440, 0x55ca396a, 0x2aab10b6, + 0xb4cc5c34, 0x1141e8ce, 0xa15486af, 0x7c72e993, 0xb3ee1411, 0x636fbc2a, + 0x2ba9c55d, 0x741831f6, 0xce5c3e16, 0x9b87931e, 0xafd6ba33, 0x6c24cf5c, + 0x7a325381, 0x28958677, 0x3b8f4898, 0x6b4bb9af, 0xc4bfe81b, 0x66282193, + 0x61d809cc, 0xfb21a991, 0x487cac60, 0x5dec8032, 0xef845d5d, 0xe98575b1, + 0xdc262302, 0xeb651b88, 0x23893e81, 0xd396acc5, 0x0f6d6ff3, 0x83f44239, + 0x2e0b4482, 0xa4842004, 0x69c8f04a, 0x9e1f9b5e, 0x21c66842, 0xf6e96c9a, + 0x670c9c61, 0xabd388f0, 0x6a51a0d2, 0xd8542f68, 0x960fa728, 0xab5133a3, + 0x6eef0b6c, 0x137a3be4, 0xba3bf050, 0x7efb2a98, 0xa1f1651d, 0x39af0176, + 0x66ca593e, 0x82430e88, 0x8cee8619, 0x456f9fb4, 0x7d84a5c3, 0x3b8b5ebe, + 0xe06f75d8, 0x85c12073, 0x401a449f, 0x56c16aa6, 0x4ed3aa62, 0x363f7706, + 0x1bfedf72, 0x429b023d, 0x37d0d724, 0xd00a1248, 0xdb0fead3, 0x49f1c09b, + 0x075372c9, 0x80991b7b, 0x25d479d8, 0xf6e8def7, 0xe3fe501a, 0xb6794c3b, + 0x976ce0bd, 0x04c006ba, 0xc1a94fb6, 0x409f60c4, 0x5e5c9ec2, 0x196a2463, + 0x68fb6faf, 0x3e6c53b5, 0x1339b2eb, 0x3b52ec6f, 0x6dfc511f, 0x9b30952c, + 0xcc814544, 0xaf5ebd09, 0xbee3d004, 0xde334afd, 0x660f2807, 0x192e4bb3, + 0xc0cba857, 0x45c8740f, 0xd20b5f39, 0xb9d3fbdb, 0x5579c0bd, 0x1a60320a, + 0xd6a100c6, 0x402c7279, 0x679f25fe, 0xfb1fa3cc, 0x8ea5e9f8, 0xdb3222f8, + 0x3c7516df, 0xfd616b15, 0x2f501ec8, 0xad0552ab, 0x323db5fa, 0xfd238760, + 0x53317b48, 0x3e00df82, 0x9e5c57bb, 0xca6f8ca0, 0x1a87562e, 0xdf1769db, + 0xd542a8f6, 0x287effc3, 0xac6732c6, 0x8c4f5573, 0x695b27b0, 0xbbca58c8, + 0xe1ffa35d, 0xb8f011a0, 0x10fa3d98, 0xfd2183b8, 0x4afcb56c, 0x2dd1d35b, + 0x9a53e479, 0xb6f84565, 0xd28e49bc, 0x4bfb9790, 0xe1ddf2da, 0xa4cb7e33, + 0x62fb1341, 0xcee4c6e8, 0xef20cada, 0x36774c01, 0xd07e9efe, 0x2bf11fb4, + 0x95dbda4d, 0xae909198, 0xeaad8e71, 0x6b93d5a0, 0xd08ed1d0, 0xafc725e0, + 0x8e3c5b2f, 0x8e7594b7, 0x8ff6e2fb, 0xf2122b64, 0x8888b812, 0x900df01c, + 0x4fad5ea0, 0x688fc31c, 0xd1cff191, 0xb3a8c1ad, 0x2f2f2218, 0xbe0e1777, + 0xea752dfe, 0x8b021fa1, 0xe5a0cc0f, 0xb56f74e8, 0x18acf3d6, 0xce89e299, + 0xb4a84fe0, 0xfd13e0b7, 0x7cc43b81, 0xd2ada8d9, 0x165fa266, 0x80957705, + 0x93cc7314, 0x211a1477, 0xe6ad2065, 0x77b5fa86, 0xc75442f5, 0xfb9d35cf, + 0xebcdaf0c, 0x7b3e89a0, 0xd6411bd3, 0xae1e7e49, 0x00250e2d, 0x2071b35e, + 0x226800bb, 0x57b8e0af, 0x2464369b, 0xf009b91e, 0x5563911d, 0x59dfa6aa, + 0x78c14389, 0xd95a537f, 0x207d5ba2, 0x02e5b9c5, 0x83260376, 0x6295cfa9, + 0x11c81968, 0x4e734a41, 0xb3472dca, 0x7b14a94a, 0x1b510052, 0x9a532915, + 0xd60f573f, 0xbc9bc6e4, 0x2b60a476, 0x81e67400, 0x08ba6fb5, 0x571be91f, + 0xf296ec6b, 0x2a0dd915, 0xb6636521, 0xe7b9f9b6, 0xff34052e, 0xc5855664, + 0x53b02d5d, 0xa99f8fa1, 0x08ba4799, 0x6e85076a, }, + + /* S-Box 1 */ + { 0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623, 0xad6ea6b0, 0x49a7df7d, + 0x9cee60b8, 0x8fedb266, 0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1, + 0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e, 0x3f54989a, 0x5b429d65, + 0x6b8fe4d6, 0x99f73fd6, 0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1, + 0x4cdd2086, 0x8470eb26, 0x6382e9c6, 0x021ecc5e, 0x09686b3f, 0x3ebaefc9, + 0x3c971814, 0x6b6a70a1, 0x687f3584, 0x52a0e286, 0xb79c5305, 0xaa500737, + 0x3e07841c, 0x7fdeae5c, 0x8e7d44ec, 0x5716f2b8, 0xb03ada37, 0xf0500c0d, + 0xf01c1f04, 0x0200b3ff, 0xae0cf51a, 0x3cb574b2, 0x25837a58, 0xdc0921bd, + 0xd19113f9, 0x7ca92ff6, 0x94324773, 0x22f54701, 0x3ae5e581, 0x37c2dadc, + 0xc8b57634, 0x9af3dda7, 0xa9446146, 0x0fd0030e, 0xecc8c73e, 0xa4751e41, + 0xe238cd99, 0x3bea0e2f, 0x3280bba1, 0x183eb331, 0x4e548b38, 0x4f6db908, + 0x6f420d03, 0xf60a04bf, 0x2cb81290, 0x24977c79, 0x5679b072, 0xbcaf89af, + 0xde9a771f, 0xd9930810, 0xb38bae12, 0xdccf3f2e, 0x5512721f, 0x2e6b7124, + 0x501adde6, 0x9f84cd87, 0x7a584718, 0x7408da17, 0xbc9f9abc, 0xe94b7d8c, + 0xec7aec3a, 0xdb851dfa, 0x63094366, 0xc464c3d2, 0xef1c1847, 0x3215d908, + 0xdd433b37, 0x24c2ba16, 0x12a14d43, 0x2a65c451, 0x50940002, 0x133ae4dd, + 0x71dff89e, 0x10314e55, 0x81ac77d6, 0x5f11199b, 0x043556f1, 0xd7a3c76b, + 0x3c11183b, 0x5924a509, 0xf28fe6ed, 0x97f1fbfa, 0x9ebabf2c, 0x1e153c6e, + 0x86e34570, 0xeae96fb1, 0x860e5e0a, 0x5a3e2ab3, 0x771fe71c, 0x4e3d06fa, + 0x2965dcb9, 0x99e71d0f, 0x803e89d6, 0x5266c825, 0x2e4cc978, 0x9c10b36a, + 0xc6150eba, 0x94e2ea78, 0xa5fc3c53, 0x1e0a2df4, 0xf2f74ea7, 0x361d2b3d, + 0x1939260f, 0x19c27960, 0x5223a708, 0xf71312b6, 0xebadfe6e, 0xeac31f66, + 0xe3bc4595, 0xa67bc883, 0xb17f37d1, 0x018cff28, 0xc332ddef, 0xbe6c5aa5, + 0x65582185, 0x68ab9802, 0xeecea50f, 0xdb2f953b, 0x2aef7dad, 0x5b6e2f84, + 0x1521b628, 0x29076170, 0xecdd4775, 0x619f1510, 0x13cca830, 0xeb61bd96, + 0x0334fe1e, 0xaa0363cf, 0xb5735c90, 0x4c70a239, 0xd59e9e0b, 0xcbaade14, + 0xeecc86bc, 0x60622ca7, 0x9cab5cab, 0xb2f3846e, 0x648b1eaf, 0x19bdf0ca, + 0xa02369b9, 0x655abb50, 0x40685a32, 0x3c2ab4b3, 0x319ee9d5, 0xc021b8f7, + 0x9b540b19, 0x875fa099, 0x95f7997e, 0x623d7da8, 0xf837889a, 0x97e32d77, + 0x11ed935f, 0x16681281, 0x0e358829, 0xc7e61fd6, 0x96dedfa1, 0x7858ba99, + 0x57f584a5, 0x1b227263, 0x9b83c3ff, 0x1ac24696, 0xcdb30aeb, 0x532e3054, + 0x8fd948e4, 0x6dbc3128, 0x58ebf2ef, 0x34c6ffea, 0xfe28ed61, 0xee7c3c73, + 0x5d4a14d9, 0xe864b7e3, 0x42105d14, 0x203e13e0, 0x45eee2b6, 0xa3aaabea, + 0xdb6c4f15, 0xfacb4fd0, 0xc742f442, 0xef6abbb5, 0x654f3b1d, 0x41cd2105, + 0xd81e799e, 0x86854dc7, 0xe44b476a, 0x3d816250, 0xcf62a1f2, 0x5b8d2646, + 0xfc8883a0, 0xc1c7b6a3, 0x7f1524c3, 0x69cb7492, 0x47848a0b, 0x5692b285, + 0x095bbf00, 0xad19489d, 0x1462b174, 0x23820e00, 0x58428d2a, 0x0c55f5ea, + 0x1dadf43e, 0x233f7061, 0x3372f092, 0x8d937e41, 0xd65fecf1, 0x6c223bdb, + 0x7cde3759, 0xcbee7460, 0x4085f2a7, 0xce77326e, 0xa6078084, 0x19f8509e, + 0xe8efd855, 0x61d99735, 0xa969a7aa, 0xc50c06c2, 0x5a04abfc, 0x800bcadc, + 0x9e447a2e, 0xc3453484, 0xfdd56705, 0x0e1e9ec9, 0xdb73dbd3, 0x105588cd, + 0x675fda79, 0xe3674340, 0xc5c43465, 0x713e38d8, 0x3d28f89e, 0xf16dff20, + 0x153e21e7, 0x8fb03d4a, 0xe6e39f2b, 0xdb83adf7, }, + + /* S-Box 2 */ + { 0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934, 0x411520f7, 0x7602d4f7, + 0xbcf46b2e, 0xd4a20068, 0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af, + 0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840, 0x4d95fc1d, 0x96b591af, + 0x70f4ddd3, 0x66a02f45, 0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504, + 0x96eb27b3, 0x55fd3941, 0xda2547e6, 0xabca0a9a, 0x28507825, 0x530429f4, + 0x0a2c86da, 0xe9b66dfb, 0x68dc1462, 0xd7486900, 0x680ec0a4, 0x27a18dee, + 0x4f3ffea2, 0xe887ad8c, 0xb58ce006, 0x7af4d6b6, 0xaace1e7c, 0xd3375fec, + 0xce78a399, 0x406b2a42, 0x20fe9e35, 0xd9f385b9, 0xee39d7ab, 0x3b124e8b, + 0x1dc9faf7, 0x4b6d1856, 0x26a36631, 0xeae397b2, 0x3a6efa74, 0xdd5b4332, + 0x6841e7f7, 0xca7820fb, 0xfb0af54e, 0xd8feb397, 0x454056ac, 0xba489527, + 0x55533a3a, 0x20838d87, 0xfe6ba9b7, 0xd096954b, 0x55a867bc, 0xa1159a58, + 0xcca92963, 0x99e1db33, 0xa62a4a56, 0x3f3125f9, 0x5ef47e1c, 0x9029317c, + 0xfdf8e802, 0x04272f70, 0x80bb155c, 0x05282ce3, 0x95c11548, 0xe4c66d22, + 0x48c1133f, 0xc70f86dc, 0x07f9c9ee, 0x41041f0f, 0x404779a4, 0x5d886e17, + 0x325f51eb, 0xd59bc0d1, 0xf2bcc18f, 0x41113564, 0x257b7834, 0x602a9c60, + 0xdff8e8a3, 0x1f636c1b, 0x0e12b4c2, 0x02e1329e, 0xaf664fd1, 0xcad18115, + 0x6b2395e0, 0x333e92e1, 0x3b240b62, 0xeebeb922, 0x85b2a20e, 0xe6ba0d99, + 0xde720c8c, 0x2da2f728, 0xd0127845, 0x95b794fd, 0x647d0862, 0xe7ccf5f0, + 0x5449a36f, 0x877d48fa, 0xc39dfd27, 0xf33e8d1e, 0x0a476341, 0x992eff74, + 0x3a6f6eab, 0xf4f8fd37, 0xa812dc60, 0xa1ebddf8, 0x991be14c, 0xdb6e6b0d, + 0xc67b5510, 0x6d672c37, 0x2765d43b, 0xdcd0e804, 0xf1290dc7, 0xcc00ffa3, + 0xb5390f92, 0x690fed0b, 0x667b9ffb, 0xcedb7d9c, 0xa091cf0b, 0xd9155ea3, + 0xbb132f88, 0x515bad24, 0x7b9479bf, 0x763bd6eb, 0x37392eb3, 0xcc115979, + 0x8026e297, 0xf42e312d, 0x6842ada7, 0xc66a2b3b, 0x12754ccc, 0x782ef11c, + 0x6a124237, 0xb79251e7, 0x06a1bbe6, 0x4bfb6350, 0x1a6b1018, 0x11caedfa, + 0x3d25bdd8, 0xe2e1c3c9, 0x44421659, 0x0a121386, 0xd90cec6e, 0xd5abea2a, + 0x64af674e, 0xda86a85f, 0xbebfe988, 0x64e4c3fe, 0x9dbc8057, 0xf0f7c086, + 0x60787bf8, 0x6003604d, 0xd1fd8346, 0xf6381fb0, 0x7745ae04, 0xd736fccc, + 0x83426b33, 0xf01eab71, 0xb0804187, 0x3c005e5f, 0x77a057be, 0xbde8ae24, + 0x55464299, 0xbf582e61, 0x4e58f48f, 0xf2ddfda2, 0xf474ef38, 0x8789bdc2, + 0x5366f9c3, 0xc8b38e74, 0xb475f255, 0x46fcd9b9, 0x7aeb2661, 0x8b1ddf84, + 0x846a0e79, 0x915f95e2, 0x466e598e, 0x20b45770, 0x8cd55591, 0xc902de4c, + 0xb90bace1, 0xbb8205d0, 0x11a86248, 0x7574a99e, 0xb77f19b6, 0xe0a9dc09, + 0x662d09a1, 0xc4324633, 0xe85a1f02, 0x09f0be8c, 0x4a99a025, 0x1d6efe10, + 0x1ab93d1d, 0x0ba5a4df, 0xa186f20f, 0x2868f169, 0xdcb7da83, 0x573906fe, + 0xa1e2ce9b, 0x4fcd7f52, 0x50115e01, 0xa70683fa, 0xa002b5c4, 0x0de6d027, + 0x9af88c27, 0x773f8641, 0xc3604c06, 0x61a806b5, 0xf0177a28, 0xc0f586e0, + 0x006058aa, 0x30dc7d62, 0x11e69ed7, 0x2338ea63, 0x53c2dd94, 0xc2c21634, + 0xbbcbee56, 0x90bcb6de, 0xebfc7da1, 0xce591d76, 0x6f05e409, 0x4b7c0188, + 0x39720a3d, 0x7c927c24, 0x86e3725f, 0x724d9db9, 0x1ac15bb4, 0xd39eb8fc, + 0xed545578, 0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4, 0x1e50ef5e, 0xb161e6f8, + 0xa28514d9, 0x6c51133c, 0x6fd5c7e7, 0x56e14ec4, 0x362abfce, 0xddc6c837, + 0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0, }, + + /* S-Box 3 */ + { 0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b, 0x5cb0679e, 0x4fa33742, + 0xd3822740, 0x99bc9bbe, 0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b, + 0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4, 0x5748ab2f, 0xbc946e79, + 0xc6a376d2, 0x6549c2c8, 0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6, + 0x2939bbdb, 0xa9ba4650, 0xac9526e8, 0xbe5ee304, 0xa1fad5f0, 0x6a2d519a, + 0x63ef8ce2, 0x9a86ee22, 0xc089c2b8, 0x43242ef6, 0xa51e03aa, 0x9cf2d0a4, + 0x83c061ba, 0x9be96a4d, 0x8fe51550, 0xba645bd6, 0x2826a2f9, 0xa73a3ae1, + 0x4ba99586, 0xef5562e9, 0xc72fefd3, 0xf752f7da, 0x3f046f69, 0x77fa0a59, + 0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593, 0xe990fd5a, 0x9e34d797, + 0x2cf0b7d9, 0x022b8b51, 0x96d5ac3a, 0x017da67d, 0xd1cf3ed6, 0x7c7d2d28, + 0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c, 0xe029ac71, 0xe019a5e6, + 0x47b0acfd, 0xed93fa9b, 0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28, + 0x785f0191, 0xed756055, 0xf7960e44, 0xe3d35e8c, 0x15056dd4, 0x88f46dba, + 0x03a16125, 0x0564f0bd, 0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a, + 0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319, 0x7533d928, 0xb155fdf5, + 0x03563482, 0x8aba3cbb, 0x28517711, 0xc20ad9f8, 0xabcc5167, 0xccad925f, + 0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991, 0xea7a90c2, 0xfb3e7bce, + 0x5121ce64, 0x774fbe32, 0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680, + 0xa2ae0810, 0xdd6db224, 0x69852dfd, 0x09072166, 0xb39a460a, 0x6445c0dd, + 0x586cdecf, 0x1c20c8ae, 0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb, + 0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5, 0x72eacea8, 0xfa6484bb, + 0x8d6612ae, 0xbf3c6f47, 0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370, + 0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d, 0x4040cb08, 0x4eb4e2cc, + 0x34d2466a, 0x0115af84, 0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048, + 0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8, 0x611560b1, 0xe7933fdc, + 0xbb3a792b, 0x344525bd, 0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9, + 0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7, 0x1a908749, 0xd44fbd9a, + 0xd0dadecb, 0xd50ada38, 0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f, + 0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c, 0xbf97222c, 0x15e6fc2a, + 0x0f91fc71, 0x9b941525, 0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1, + 0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442, 0xe0ec6e0e, 0x1698db3b, + 0x4c98a0be, 0x3278e964, 0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e, + 0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8, 0xdf359f8d, 0x9b992f2e, + 0xe60b6f47, 0x0fe3f11d, 0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f, + 0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299, 0xf523f357, 0xa6327623, + 0x93a83531, 0x56cccd02, 0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc, + 0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614, 0xe6c6c7bd, 0x327a140a, + 0x45e1d006, 0xc3f27b9a, 0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6, + 0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b, 0x53113ec0, 0x1640e3d3, + 0x38abbd60, 0x2547adf0, 0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060, + 0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e, 0x1948c25c, 0x02fb8a8c, + 0x01c36ae4, 0xd6ebe1f9, 0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f, + 0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6, }, +}; + +static const uint32_t bf_pinit[] = { + /* P-Box */ + 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, 0xa4093822, 0x299f31d0, + 0x082efa98, 0xec4e6c89, 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c, + 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917, 0x9216d5d9, 0x8979fb1b, +}; + +#define KEYBYTES 24 + +static const unsigned char rtmpe9_keys[16][KEYBYTES] = { + { 0x79, 0x34, 0x77, 0x4c, 0x67, 0xd1, 0x38, 0x3a, 0xdf, 0xb3, 0x56, 0xbe, + 0x8b, 0x7b, 0xd0, 0x24, 0x38, 0xe0, 0x73, 0x58, 0x41, 0x5d, 0x69, 0x67, }, + { 0x46, 0xf6, 0xb4, 0xcc, 0x01, 0x93, 0xe3, 0xa1, 0x9e, 0x7d, 0x3c, 0x65, + 0x55, 0x86, 0xfd, 0x09, 0x8f, 0xf7, 0xb3, 0xc4, 0x6f, 0x41, 0xca, 0x5c, }, + { 0x1a, 0xe7, 0xe2, 0xf3, 0xf9, 0x14, 0x79, 0x94, 0xc0, 0xd3, 0x97, 0x43, + 0x08, 0x7b, 0xb3, 0x84, 0x43, 0x2f, 0x9d, 0x84, 0x3f, 0x21, 0x01, 0x9b, }, + { 0xd3, 0xe3, 0x54, 0xb0, 0xf7, 0x1d, 0xf6, 0x2b, 0x5a, 0x43, 0x4d, 0x04, + 0x83, 0x64, 0x3e, 0x0d, 0x59, 0x2f, 0x61, 0xcb, 0xb1, 0x6a, 0x59, 0x0d, }, + { 0xc8, 0xc1, 0xe9, 0xb8, 0x16, 0x56, 0x99, 0x21, 0x7b, 0x5b, 0x36, 0xb7, + 0xb5, 0x9b, 0xdf, 0x06, 0x49, 0x2c, 0x97, 0xf5, 0x95, 0x48, 0x85, 0x7e, }, + { 0xeb, 0xe5, 0xe6, 0x2e, 0xa4, 0xba, 0xd4, 0x2c, 0xf2, 0x16, 0xe0, 0x8f, + 0x66, 0x23, 0xa9, 0x43, 0x41, 0xce, 0x38, 0x14, 0x84, 0x95, 0x00, 0x53, }, + { 0x66, 0xdb, 0x90, 0xf0, 0x3b, 0x4f, 0xf5, 0x6f, 0xe4, 0x9c, 0x20, 0x89, + 0x35, 0x5e, 0xd2, 0xb2, 0xc3, 0x9e, 0x9f, 0x7f, 0x63, 0xb2, 0x28, 0x81, }, + { 0xbb, 0x20, 0xac, 0xed, 0x2a, 0x04, 0x6a, 0x19, 0x94, 0x98, 0x9b, 0xc8, + 0xff, 0xcd, 0x93, 0xef, 0xc6, 0x0d, 0x56, 0xa7, 0xeb, 0x13, 0xd9, 0x30, }, + { 0xbc, 0xf2, 0x43, 0x82, 0x09, 0x40, 0x8a, 0x87, 0x25, 0x43, 0x6d, 0xe6, + 0xbb, 0xa4, 0xb9, 0x44, 0x58, 0x3f, 0x21, 0x7c, 0x99, 0xbb, 0x3f, 0x24, }, + { 0xec, 0x1a, 0xaa, 0xcd, 0xce, 0xbd, 0x53, 0x11, 0xd2, 0xfb, 0x83, 0xb6, + 0xc3, 0xba, 0xab, 0x4f, 0x62, 0x79, 0xe8, 0x65, 0xa9, 0x92, 0x28, 0x76, }, + { 0xc6, 0x0c, 0x30, 0x03, 0x91, 0x18, 0x2d, 0x7b, 0x79, 0xda, 0xe1, 0xd5, + 0x64, 0x77, 0x9a, 0x12, 0xc5, 0xb1, 0xd7, 0x91, 0x4f, 0x96, 0x4c, 0xa3, }, + { 0xd7, 0x7c, 0x2a, 0xbf, 0xa6, 0xe7, 0x85, 0x7c, 0x45, 0xad, 0xff, 0x12, + 0x94, 0xd8, 0xde, 0xa4, 0x5c, 0x3d, 0x79, 0xa4, 0x44, 0x02, 0x5d, 0x22, }, + { 0x16, 0x19, 0x0d, 0x81, 0x6a, 0x4c, 0xc7, 0xf8, 0xb8, 0xf9, 0x4e, 0xcd, + 0x2c, 0x9e, 0x90, 0x84, 0xb2, 0x08, 0x25, 0x60, 0xe1, 0x1e, 0xae, 0x18, }, + { 0xe9, 0x7c, 0x58, 0x26, 0x1b, 0x51, 0x9e, 0x49, 0x82, 0x60, 0x61, 0xfc, + 0xa0, 0xa0, 0x1b, 0xcd, 0xf5, 0x05, 0xd6, 0xa6, 0x6d, 0x07, 0x88, 0xa3, }, + { 0x2b, 0x97, 0x11, 0x8b, 0xd9, 0x4e, 0xd9, 0xdf, 0x20, 0xe3, 0x9c, 0x10, + 0xe6, 0xa1, 0x35, 0x21, 0x11, 0xf9, 0x13, 0x0d, 0x0b, 0x24, 0x65, 0xb2, }, + { 0x53, 0x6a, 0x4c, 0x54, 0xac, 0x8b, 0x9b, 0xb8, 0x97, 0x29, 0xfc, 0x60, + 0x2c, 0x5b, 0x3a, 0x85, 0x68, 0xb5, 0xaa, 0x6a, 0x44, 0xcd, 0x3f, 0xa7, }, +}; + +#define BF_ENC(X,S) \ + (((S[0][X>>24] + S[1][X>>16 & 0xff]) ^ S[2][(X>>8) & 0xff]) + S[3][X & 0xff]) + +static void bf_enc(uint32_t *x, bf_key *key) +{ + uint32_t Xl; + uint32_t Xr; + uint32_t temp; + int i; + + Xl = x[0]; + Xr = x[1]; + + for (i = 0; i < BF_ROUNDS; ++i) { + Xl ^= key->p[i]; + Xr ^= BF_ENC(Xl,key->s); + + temp = Xl; + Xl = Xr; + Xr = temp; + } + + Xl ^= key->p[BF_ROUNDS]; + Xr ^= key->p[BF_ROUNDS + 1]; + + x[0] = Xr; + x[1] = Xl; +} + +static void bf_setkey(const unsigned char *kp, int keybytes, bf_key *key) +{ + int i; + int j; + int k; + uint32_t data; + uint32_t d[2]; + + memcpy(key->p, bf_pinit, sizeof(key->p)); + memcpy(key->s, bf_sinit, sizeof(key->s)); + + j = 0; + for (i = 0; i < BF_ROUNDS + 2; ++i) { + data = 0x00000000; + for (k = 0; k < 4; ++k) { + data = (data << 8) | kp[j]; + j = j + 1; + if (j >= keybytes) { + j = 0; + } + } + key->p[i] ^= data; + } + + d[0] = 0x00000000; + d[1] = 0x00000000; + + for (i = 0; i < BF_ROUNDS + 2; i += 2) { + bf_enc(d, key); + + key->p[i] = d[0]; + key->p[i + 1] = d[1]; + } + + for (i = 0; i < 4; ++i) { + for (j = 0; j < 256; j += 2) { + + bf_enc(d, key); + + key->s[i][j] = d[0]; + key->s[i][j + 1] = d[1]; + } + } +} + +static void rtmpe9_sig(uint8_t *in, uint8_t *out, int keyid) +{ + uint32_t d[2]; + bf_key key; + + bf_setkey(rtmpe9_keys[keyid], KEYBYTES, &key); + + /* input is little-endian */ + d[0] = in[0] | (in[1] << 8) | (in[2] << 16) | (in[3] << 24); + d[1] = in[4] | (in[5] << 8) | (in[6] << 16) | (in[7] << 24); + bf_enc(d, &key); + out[0] = d[0] & 0xff; + out[1] = (d[0] >> 8) & 0xff; + out[2] = (d[0] >> 16) & 0xff; + out[3] = (d[0] >> 24) & 0xff; + out[4] = d[1] & 0xff; + out[5] = (d[1] >> 8) & 0xff; + out[6] = (d[1] >> 16) & 0xff; + out[7] = (d[1] >> 24) & 0xff; +} + +static int +HandShake(RTMP * r, int FP9HandShake) +{ + int i, offalg = 0; + int dhposClient = 0; + int digestPosClient = 0; + int encrypted = r->Link.protocol & RTMP_FEATURE_ENC; + + RC4_handle keyIn = 0; + RC4_handle keyOut = 0; + + int32_t *ip; + uint32_t uptime; + + uint8_t clientbuf[RTMP_SIG_SIZE + 4], *clientsig=clientbuf+4; + uint8_t serversig[RTMP_SIG_SIZE], client2[RTMP_SIG_SIZE], *reply; + uint8_t type; + getoff *getdh = NULL, *getdig = NULL; + + if (encrypted || r->Link.SWFSize) + FP9HandShake = TRUE; + else + FP9HandShake = FALSE; + + r->Link.rc4keyIn = r->Link.rc4keyOut = 0; + + if (encrypted) + { + clientsig[-1] = 0x06; /* 0x08 is RTMPE as well */ + offalg = 1; + } + else + clientsig[-1] = 0x03; + + uptime = htonl(RTMP_GetTime()); + memcpy(clientsig, &uptime, 4); + + if (FP9HandShake) + { + /* set version to at least 9.0.115.0 */ + if (encrypted) + { + clientsig[4] = 128; + clientsig[6] = 3; + } + else + { + clientsig[4] = 10; + clientsig[6] = 45; + } + clientsig[5] = 0; + clientsig[7] = 2; + + RTMP_Log(RTMP_LOGDEBUG, "%s: Client type: %02X", __FUNCTION__, clientsig[-1]); + getdig = digoff[offalg]; + getdh = dhoff[offalg]; + } + else + { + memset(&clientsig[4], 0, 4); + } + + /* generate random data */ +#ifdef _DEBUG + memset(clientsig+8, 0, RTMP_SIG_SIZE-8); +#else + ip = (int32_t *)(clientsig+8); + for (i = 2; i < RTMP_SIG_SIZE/4; i++) + *ip++ = rand(); +#endif + + /* set handshake digest */ + if (FP9HandShake) + { + if (encrypted) + { + /* generate Diffie-Hellmann parameters */ + r->Link.dh = DHInit(1024); + if (!r->Link.dh) + { + RTMP_Log(RTMP_LOGERROR, "%s: Couldn't initialize Diffie-Hellmann!", + __FUNCTION__); + return FALSE; + } + + dhposClient = getdh(clientsig, RTMP_SIG_SIZE); + RTMP_Log(RTMP_LOGDEBUG, "%s: DH pubkey position: %d", __FUNCTION__, dhposClient); + + if (!DHGenerateKey(r->Link.dh)) + { + RTMP_Log(RTMP_LOGERROR, "%s: Couldn't generate Diffie-Hellmann public key!", + __FUNCTION__); + return FALSE; + } + + if (!DHGetPublicKey(r->Link.dh, &clientsig[dhposClient], 128)) + { + RTMP_Log(RTMP_LOGERROR, "%s: Couldn't write public key!", __FUNCTION__); + return FALSE; + } + } + + digestPosClient = getdig(clientsig, RTMP_SIG_SIZE); /* reuse this value in verification */ + RTMP_Log(RTMP_LOGDEBUG, "%s: Client digest offset: %d", __FUNCTION__, + digestPosClient); + + CalculateDigest(digestPosClient, clientsig, GenuineFPKey, 30, + &clientsig[digestPosClient]); + + RTMP_Log(RTMP_LOGDEBUG, "%s: Initial client digest: ", __FUNCTION__); + RTMP_LogHex(RTMP_LOGDEBUG, clientsig + digestPosClient, + SHA256_DIGEST_LENGTH); + } + +#ifdef _DEBUG + RTMP_Log(RTMP_LOGDEBUG, "Clientsig: "); + RTMP_LogHex(RTMP_LOGDEBUG, clientsig, RTMP_SIG_SIZE); +#endif + + if (!WriteN(r, (char *)clientsig-1, RTMP_SIG_SIZE + 1)) + return FALSE; + + if (ReadN(r, (char *)&type, 1) != 1) /* 0x03 or 0x06 */ + return FALSE; + + RTMP_Log(RTMP_LOGDEBUG, "%s: Type Answer : %02X", __FUNCTION__, type); + + if (type != clientsig[-1]) + RTMP_Log(RTMP_LOGWARNING, "%s: Type mismatch: client sent %d, server answered %d", + __FUNCTION__, clientsig[-1], type); + + if (ReadN(r, (char *)serversig, RTMP_SIG_SIZE) != RTMP_SIG_SIZE) + return FALSE; + + /* decode server response */ + memcpy(&uptime, serversig, 4); + uptime = ntohl(uptime); + + RTMP_Log(RTMP_LOGDEBUG, "%s: Server Uptime : %d", __FUNCTION__, uptime); + RTMP_Log(RTMP_LOGDEBUG, "%s: FMS Version : %d.%d.%d.%d", __FUNCTION__, serversig[4], + serversig[5], serversig[6], serversig[7]); + + if (FP9HandShake && type == 3 && !serversig[4]) + FP9HandShake = FALSE; + +#ifdef _DEBUG + RTMP_Log(RTMP_LOGDEBUG, "Server signature:"); + RTMP_LogHex(RTMP_LOGDEBUG, serversig, RTMP_SIG_SIZE); +#endif + + if (FP9HandShake) + { + uint8_t digestResp[SHA256_DIGEST_LENGTH]; + uint8_t *signatureResp = NULL; + + /* we have to use this signature now to find the correct algorithms for getting the digest and DH positions */ + int digestPosServer = getdig(serversig, RTMP_SIG_SIZE); + + if (!VerifyDigest(digestPosServer, serversig, GenuineFMSKey, 36)) + { + RTMP_Log(RTMP_LOGWARNING, "Trying different position for server digest!"); + offalg ^= 1; + getdig = digoff[offalg]; + getdh = dhoff[offalg]; + digestPosServer = getdig(serversig, RTMP_SIG_SIZE); + + if (!VerifyDigest(digestPosServer, serversig, GenuineFMSKey, 36)) + { + RTMP_Log(RTMP_LOGERROR, "Couldn't verify the server digest"); /* continuing anyway will probably fail */ + return FALSE; + } + } + + /* generate SWFVerification token (SHA256 HMAC hash of decompressed SWF, key are the last 32 bytes of the server handshake) */ + if (r->Link.SWFSize) + { + const char swfVerify[] = { 0x01, 0x01 }; + char *vend = r->Link.SWFVerificationResponse+sizeof(r->Link.SWFVerificationResponse); + + memcpy(r->Link.SWFVerificationResponse, swfVerify, 2); + AMF_EncodeInt32(&r->Link.SWFVerificationResponse[2], vend, r->Link.SWFSize); + AMF_EncodeInt32(&r->Link.SWFVerificationResponse[6], vend, r->Link.SWFSize); + HMACsha256(r->Link.SWFHash, SHA256_DIGEST_LENGTH, + &serversig[RTMP_SIG_SIZE - SHA256_DIGEST_LENGTH], + SHA256_DIGEST_LENGTH, + (uint8_t *)&r->Link.SWFVerificationResponse[10]); + } + + /* do Diffie-Hellmann Key exchange for encrypted RTMP */ + if (encrypted) + { + /* compute secret key */ + uint8_t secretKey[128] = { 0 }; + int len, dhposServer; + + dhposServer = getdh(serversig, RTMP_SIG_SIZE); + RTMP_Log(RTMP_LOGDEBUG, "%s: Server DH public key offset: %d", __FUNCTION__, + dhposServer); + len = DHComputeSharedSecretKey(r->Link.dh, &serversig[dhposServer], + 128, secretKey); + if (len < 0) + { + RTMP_Log(RTMP_LOGDEBUG, "%s: Wrong secret key position!", __FUNCTION__); + return FALSE; + } + + RTMP_Log(RTMP_LOGDEBUG, "%s: Secret key: ", __FUNCTION__); + RTMP_LogHex(RTMP_LOGDEBUG, secretKey, 128); + + InitRC4Encryption(secretKey, + (uint8_t *) & serversig[dhposServer], + (uint8_t *) & clientsig[dhposClient], + &keyIn, &keyOut); + } + + + reply = client2; +#ifdef _DEBUG + memset(reply, 0xff, RTMP_SIG_SIZE); +#else + ip = (int32_t *)reply; + for (i = 0; i < RTMP_SIG_SIZE/4; i++) + *ip++ = rand(); +#endif + /* calculate response now */ + signatureResp = reply+RTMP_SIG_SIZE-SHA256_DIGEST_LENGTH; + + HMACsha256(&serversig[digestPosServer], SHA256_DIGEST_LENGTH, + GenuineFPKey, sizeof(GenuineFPKey), digestResp); + HMACsha256(reply, RTMP_SIG_SIZE - SHA256_DIGEST_LENGTH, digestResp, + SHA256_DIGEST_LENGTH, signatureResp); + + /* some info output */ + RTMP_Log(RTMP_LOGDEBUG, + "%s: Calculated digest key from secure key and server digest: ", + __FUNCTION__); + RTMP_LogHex(RTMP_LOGDEBUG, digestResp, SHA256_DIGEST_LENGTH); + +#ifdef FP10 + if (type == 8 ) + { + uint8_t *dptr = digestResp; + uint8_t *sig = signatureResp; + /* encrypt signatureResp */ + for (i=0; iLink.rc4keyIn = keyIn; + r->Link.rc4keyOut = keyOut; + + + /* update the keystreams */ + if (r->Link.rc4keyIn) + { + RC4_encrypt(r->Link.rc4keyIn, RTMP_SIG_SIZE, (uint8_t *) buff); + } + + if (r->Link.rc4keyOut) + { + RC4_encrypt(r->Link.rc4keyOut, RTMP_SIG_SIZE, (uint8_t *) buff); + } + } + } + else + { + if (memcmp(serversig, clientsig, RTMP_SIG_SIZE) != 0) + { + RTMP_Log(RTMP_LOGWARNING, "%s: client signature does not match!", + __FUNCTION__); + } + } + + RTMP_Log(RTMP_LOGDEBUG, "%s: Handshaking finished....", __FUNCTION__); + return TRUE; +} + +static int +SHandShake(RTMP * r) +{ + int i, offalg = 0; + int dhposServer = 0; + int digestPosServer = 0; + RC4_handle keyIn = 0; + RC4_handle keyOut = 0; + int FP9HandShake = FALSE; + int encrypted; + int32_t *ip; + + uint8_t clientsig[RTMP_SIG_SIZE]; + uint8_t serverbuf[RTMP_SIG_SIZE + 4], *serversig = serverbuf+4; + uint8_t type; + uint32_t uptime; + getoff *getdh = NULL, *getdig = NULL; + + if (ReadN(r, (char *)&type, 1) != 1) /* 0x03 or 0x06 */ + return FALSE; + + if (ReadN(r, (char *)clientsig, RTMP_SIG_SIZE) != RTMP_SIG_SIZE) + return FALSE; + + RTMP_Log(RTMP_LOGDEBUG, "%s: Type Requested : %02X", __FUNCTION__, type); + RTMP_LogHex(RTMP_LOGDEBUG2, clientsig, RTMP_SIG_SIZE); + + if (type == 3) + { + encrypted = FALSE; + } + else if (type == 6 || type == 8) + { + offalg = 1; + encrypted = TRUE; + FP9HandShake = TRUE; + r->Link.protocol |= RTMP_FEATURE_ENC; + /* use FP10 if client is capable */ + if (clientsig[4] == 128) + type = 8; + } + else + { + RTMP_Log(RTMP_LOGERROR, "%s: Unknown version %02x", + __FUNCTION__, type); + return FALSE; + } + + if (!FP9HandShake && clientsig[4]) + FP9HandShake = TRUE; + + serversig[-1] = type; + + r->Link.rc4keyIn = r->Link.rc4keyOut = 0; + + uptime = htonl(RTMP_GetTime()); + memcpy(serversig, &uptime, 4); + + if (FP9HandShake) + { + /* Server version */ + serversig[4] = 3; + serversig[5] = 5; + serversig[6] = 1; + serversig[7] = 1; + + getdig = digoff[offalg]; + getdh = dhoff[offalg]; + } + else + { + memset(&serversig[4], 0, 4); + } + + /* generate random data */ +#ifdef _DEBUG + memset(serversig+8, 0, RTMP_SIG_SIZE-8); +#else + ip = (int32_t *)(serversig+8); + for (i = 2; i < RTMP_SIG_SIZE/4; i++) + *ip++ = rand(); +#endif + + /* set handshake digest */ + if (FP9HandShake) + { + if (encrypted) + { + /* generate Diffie-Hellmann parameters */ + r->Link.dh = DHInit(1024); + if (!r->Link.dh) + { + RTMP_Log(RTMP_LOGERROR, "%s: Couldn't initialize Diffie-Hellmann!", + __FUNCTION__); + return FALSE; + } + + dhposServer = getdh(serversig, RTMP_SIG_SIZE); + RTMP_Log(RTMP_LOGDEBUG, "%s: DH pubkey position: %d", __FUNCTION__, dhposServer); + + if (!DHGenerateKey(r->Link.dh)) + { + RTMP_Log(RTMP_LOGERROR, "%s: Couldn't generate Diffie-Hellmann public key!", + __FUNCTION__); + return FALSE; + } + + if (!DHGetPublicKey + (r->Link.dh, (uint8_t *) &serversig[dhposServer], 128)) + { + RTMP_Log(RTMP_LOGERROR, "%s: Couldn't write public key!", __FUNCTION__); + return FALSE; + } + } + + digestPosServer = getdig(serversig, RTMP_SIG_SIZE); /* reuse this value in verification */ + RTMP_Log(RTMP_LOGDEBUG, "%s: Server digest offset: %d", __FUNCTION__, + digestPosServer); + + CalculateDigest(digestPosServer, serversig, GenuineFMSKey, 36, + &serversig[digestPosServer]); + + RTMP_Log(RTMP_LOGDEBUG, "%s: Initial server digest: ", __FUNCTION__); + RTMP_LogHex(RTMP_LOGDEBUG, serversig + digestPosServer, + SHA256_DIGEST_LENGTH); + } + + RTMP_Log(RTMP_LOGDEBUG2, "Serversig: "); + RTMP_LogHex(RTMP_LOGDEBUG2, serversig, RTMP_SIG_SIZE); + + if (!WriteN(r, (char *)serversig-1, RTMP_SIG_SIZE + 1)) + return FALSE; + + /* decode client response */ + memcpy(&uptime, clientsig, 4); + uptime = ntohl(uptime); + + RTMP_Log(RTMP_LOGDEBUG, "%s: Client Uptime : %d", __FUNCTION__, uptime); + RTMP_Log(RTMP_LOGDEBUG, "%s: Player Version: %d.%d.%d.%d", __FUNCTION__, clientsig[4], + clientsig[5], clientsig[6], clientsig[7]); + + if (FP9HandShake) + { + uint8_t digestResp[SHA256_DIGEST_LENGTH]; + uint8_t *signatureResp = NULL; + + /* we have to use this signature now to find the correct algorithms for getting the digest and DH positions */ + int digestPosClient = getdig(clientsig, RTMP_SIG_SIZE); + + if (!VerifyDigest(digestPosClient, clientsig, GenuineFPKey, 30)) + { + RTMP_Log(RTMP_LOGWARNING, "Trying different position for client digest!"); + offalg ^= 1; + getdig = digoff[offalg]; + getdh = dhoff[offalg]; + + digestPosClient = getdig(clientsig, RTMP_SIG_SIZE); + + if (!VerifyDigest(digestPosClient, clientsig, GenuineFPKey, 30)) + { + RTMP_Log(RTMP_LOGERROR, "Couldn't verify the client digest"); /* continuing anyway will probably fail */ + return FALSE; + } + } + + /* generate SWFVerification token (SHA256 HMAC hash of decompressed SWF, key are the last 32 bytes of the server handshake) */ + if (r->Link.SWFSize) + { + const char swfVerify[] = { 0x01, 0x01 }; + char *vend = r->Link.SWFVerificationResponse+sizeof(r->Link.SWFVerificationResponse); + + memcpy(r->Link.SWFVerificationResponse, swfVerify, 2); + AMF_EncodeInt32(&r->Link.SWFVerificationResponse[2], vend, r->Link.SWFSize); + AMF_EncodeInt32(&r->Link.SWFVerificationResponse[6], vend, r->Link.SWFSize); + HMACsha256(r->Link.SWFHash, SHA256_DIGEST_LENGTH, + &serversig[RTMP_SIG_SIZE - SHA256_DIGEST_LENGTH], + SHA256_DIGEST_LENGTH, + (uint8_t *)&r->Link.SWFVerificationResponse[10]); + } + + /* do Diffie-Hellmann Key exchange for encrypted RTMP */ + if (encrypted) + { + int dhposClient, len; + /* compute secret key */ + uint8_t secretKey[128] = { 0 }; + + dhposClient = getdh(clientsig, RTMP_SIG_SIZE); + RTMP_Log(RTMP_LOGDEBUG, "%s: Client DH public key offset: %d", __FUNCTION__, + dhposClient); + len = + DHComputeSharedSecretKey(r->Link.dh, + (uint8_t *) &clientsig[dhposClient], 128, + secretKey); + if (len < 0) + { + RTMP_Log(RTMP_LOGDEBUG, "%s: Wrong secret key position!", __FUNCTION__); + return FALSE; + } + + RTMP_Log(RTMP_LOGDEBUG, "%s: Secret key: ", __FUNCTION__); + RTMP_LogHex(RTMP_LOGDEBUG, secretKey, 128); + + InitRC4Encryption(secretKey, + (uint8_t *) &clientsig[dhposClient], + (uint8_t *) &serversig[dhposServer], + &keyIn, &keyOut); + } + + + /* calculate response now */ + signatureResp = clientsig+RTMP_SIG_SIZE-SHA256_DIGEST_LENGTH; + + HMACsha256(&clientsig[digestPosClient], SHA256_DIGEST_LENGTH, + GenuineFMSKey, sizeof(GenuineFMSKey), digestResp); + HMACsha256(clientsig, RTMP_SIG_SIZE - SHA256_DIGEST_LENGTH, digestResp, + SHA256_DIGEST_LENGTH, signatureResp); +#ifdef FP10 + if (type == 8 ) + { + uint8_t *dptr = digestResp; + uint8_t *sig = signatureResp; + /* encrypt signatureResp */ + for (i=0; iLink.rc4keyIn = keyIn; + r->Link.rc4keyOut = keyOut; + + /* update the keystreams */ + if (r->Link.rc4keyIn) + { + RC4_encrypt(r->Link.rc4keyIn, RTMP_SIG_SIZE, (uint8_t *) buff); + } + + if (r->Link.rc4keyOut) + { + RC4_encrypt(r->Link.rc4keyOut, RTMP_SIG_SIZE, (uint8_t *) buff); + } + } + } + else + { + if (memcmp(serversig, clientsig, RTMP_SIG_SIZE) != 0) + { + RTMP_Log(RTMP_LOGWARNING, "%s: client signature does not match!", + __FUNCTION__); + } + } + + RTMP_Log(RTMP_LOGDEBUG, "%s: Handshaking finished....", __FUNCTION__); + return TRUE; +} diff --git a/librtmp/hashswf.c b/librtmp/hashswf.c new file mode 100644 index 0000000..b0bec65 --- /dev/null +++ b/librtmp/hashswf.c @@ -0,0 +1,665 @@ +/* + * Copyright (C) 2009-2010 Howard Chu + * + * This file is part of librtmp. + * + * librtmp is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1, + * or (at your option) any later version. + * + * librtmp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with librtmp see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/lgpl.html + */ + +#include +#include +#include +#include + +#include "rtmp_sys.h" +#include "log.h" +#include "http.h" + +#ifdef CRYPTO +#ifdef USE_POLARSSL +#include +#ifndef SHA256_DIGEST_LENGTH +#define SHA256_DIGEST_LENGTH 32 +#endif +#define HMAC_CTX sha2_context +#define HMAC_setup(ctx, key, len) sha2_hmac_starts(&ctx, (unsigned char *)key, len, 0) +#define HMAC_crunch(ctx, buf, len) sha2_hmac_update(&ctx, buf, len) +#define HMAC_finish(ctx, dig, dlen) dlen = SHA256_DIGEST_LENGTH; sha2_hmac_finish(&ctx, dig) +#define HMAC_close(ctx) +#elif defined(USE_GNUTLS) +#include +#ifndef SHA256_DIGEST_LENGTH +#define SHA256_DIGEST_LENGTH 32 +#endif +#undef HMAC_CTX +#define HMAC_CTX struct hmac_sha256_ctx +#define HMAC_setup(ctx, key, len) hmac_sha256_set_key(&ctx, len, key) +#define HMAC_crunch(ctx, buf, len) hmac_sha256_update(&ctx, len, buf) +#define HMAC_finish(ctx, dig, dlen) dlen = SHA256_DIGEST_LENGTH; hmac_sha256_digest(&ctx, SHA256_DIGEST_LENGTH, dig) +#define HMAC_close(ctx) +#else /* USE_OPENSSL */ +#include +#include +#include +#include +#define HMAC_setup(ctx, key, len) HMAC_CTX_init(&ctx); HMAC_Init_ex(&ctx, (unsigned char *)key, len, EVP_sha256(), 0) +#define HMAC_crunch(ctx, buf, len) HMAC_Update(&ctx, (unsigned char *)buf, len) +#define HMAC_finish(ctx, dig, dlen) HMAC_Final(&ctx, (unsigned char *)dig, &dlen); +#define HMAC_close(ctx) HMAC_CTX_cleanup(&ctx) +#endif + +extern void RTMP_TLS_Init(); +extern TLS_CTX RTMP_TLS_ctx; + +#include +#include "strncasecmp.h" + +#endif /* CRYPTO */ + +#define AGENT "Mozilla/5.0" + +HTTPResult +HTTP_get(struct HTTP_ctx *http, const char *url, HTTP_read_callback *cb) +{ + char *host, *path; + char *p1, *p2; + char hbuf[256]; + int port = 80; +#ifdef CRYPTO + int ssl = 0; +#endif + int hlen, flen = 0; + int rc, i; + int len_known; + HTTPResult ret = HTTPRES_OK; + struct sockaddr_in sa; + RTMPSockBuf sb = {0}; + + http->status = -1; + + memset(&sa, 0, sizeof(struct sockaddr_in)); + sa.sin_family = AF_INET; + + /* we only handle http here */ + if (strncasecmp(url, "http", 4)) + return HTTPRES_BAD_REQUEST; + + if (url[4] == 's') + { +#ifdef CRYPTO + ssl = 1; + port = 443; + if (!RTMP_TLS_ctx) + RTMP_TLS_Init(); +#else + return HTTPRES_BAD_REQUEST; +#endif + } + + p1 = strchr(url + 4, ':'); + if (!p1 || strncmp(p1, "://", 3)) + return HTTPRES_BAD_REQUEST; + + host = p1 + 3; + path = strchr(host, '/'); + hlen = path - host; + strncpy(hbuf, host, hlen); + hbuf[hlen] = '\0'; + host = hbuf; + p1 = strrchr(host, ':'); + if (p1) + { + *p1++ = '\0'; + port = atoi(p1); + } + + sa.sin_addr.s_addr = inet_addr(host); + if (sa.sin_addr.s_addr == INADDR_NONE) + { + struct hostent *hp = gethostbyname(host); + if (!hp || !hp->h_addr) + return HTTPRES_LOST_CONNECTION; + sa.sin_addr = *(struct in_addr *)hp->h_addr; + } + sa.sin_port = htons(port); + sb.sb_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (sb.sb_socket == -1) + return HTTPRES_LOST_CONNECTION; + i = + sprintf(sb.sb_buf, + "GET %s HTTP/1.0\r\nUser-Agent: %s\r\nHost: %s\r\nReferer: %.*s\r\n", + path, AGENT, host, (int)(path - url + 1), url); + if (http->date[0]) + i += sprintf(sb.sb_buf + i, "If-Modified-Since: %s\r\n", http->date); + i += sprintf(sb.sb_buf + i, "\r\n"); + + if (connect + (sb.sb_socket, (struct sockaddr *)&sa, sizeof(struct sockaddr)) < 0) + { + ret = HTTPRES_LOST_CONNECTION; + goto leave; + } +#ifdef CRYPTO + if (ssl) + { +#ifdef NO_SSL + RTMP_Log(RTMP_LOGERROR, "%s, No SSL/TLS support", __FUNCTION__); + ret = HTTPRES_BAD_REQUEST; + goto leave; +#else + TLS_client(RTMP_TLS_ctx, sb.sb_ssl); + TLS_setfd(sb.sb_ssl, sb.sb_socket); + if (TLS_connect(sb.sb_ssl) < 0) + { + RTMP_Log(RTMP_LOGERROR, "%s, TLS_Connect failed", __FUNCTION__); + ret = HTTPRES_LOST_CONNECTION; + goto leave; + } +#endif + } +#endif + RTMPSockBuf_Send(&sb, sb.sb_buf, i); + + /* set timeout */ +#define HTTP_TIMEOUT 5 + { + SET_RCVTIMEO(tv, HTTP_TIMEOUT); + if (setsockopt + (sb.sb_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv))) + { + RTMP_Log(RTMP_LOGERROR, "%s, Setting socket timeout to %ds failed!", + __FUNCTION__, HTTP_TIMEOUT); + } + } + + sb.sb_size = 0; + sb.sb_timedout = FALSE; + if (RTMPSockBuf_Fill(&sb) < 1) + { + ret = HTTPRES_LOST_CONNECTION; + goto leave; + } + if (strncmp(sb.sb_buf, "HTTP/1", 6)) + { + ret = HTTPRES_BAD_REQUEST; + goto leave; + } + + p1 = strchr(sb.sb_buf, ' '); + rc = atoi(p1 + 1); + http->status = rc; + + if (rc >= 300) + { + if (rc == 304) + { + ret = HTTPRES_OK_NOT_MODIFIED; + goto leave; + } + else if (rc == 404) + ret = HTTPRES_NOT_FOUND; + else if (rc >= 500) + ret = HTTPRES_SERVER_ERROR; + else if (rc >= 400) + ret = HTTPRES_BAD_REQUEST; + else + ret = HTTPRES_REDIRECTED; + } + + p1 = memchr(sb.sb_buf, '\n', sb.sb_size); + if (!p1) + { + ret = HTTPRES_BAD_REQUEST; + goto leave; + } + sb.sb_start = p1 + 1; + sb.sb_size -= sb.sb_start - sb.sb_buf; + + while ((p2 = memchr(sb.sb_start, '\r', sb.sb_size))) + { + if (*sb.sb_start == '\r') + { + sb.sb_start += 2; + sb.sb_size -= 2; + break; + } + else + if (!strncasecmp + (sb.sb_start, "Content-Length: ", sizeof("Content-Length: ") - 1)) + { + flen = atoi(sb.sb_start + sizeof("Content-Length: ") - 1); + } + else + if (!strncasecmp + (sb.sb_start, "Last-Modified: ", sizeof("Last-Modified: ") - 1)) + { + *p2 = '\0'; + strcpy(http->date, sb.sb_start + sizeof("Last-Modified: ") - 1); + } + p2 += 2; + sb.sb_size -= p2 - sb.sb_start; + sb.sb_start = p2; + if (sb.sb_size < 1) + { + if (RTMPSockBuf_Fill(&sb) < 1) + { + ret = HTTPRES_LOST_CONNECTION; + goto leave; + } + } + } + + len_known = flen > 0; + while ((!len_known || flen > 0) && + (sb.sb_size > 0 || RTMPSockBuf_Fill(&sb) > 0)) + { + cb(sb.sb_start, 1, sb.sb_size, http->data); + if (len_known) + flen -= sb.sb_size; + http->size += sb.sb_size; + sb.sb_size = 0; + } + + if (flen > 0) + ret = HTTPRES_LOST_CONNECTION; + +leave: + RTMPSockBuf_Close(&sb); + return ret; +} + +#ifdef CRYPTO + +#define CHUNK 16384 + +struct info +{ + z_stream *zs; + HMAC_CTX ctx; + int first; + int zlib; + int size; +}; + +static size_t +swfcrunch(void *ptr, size_t size, size_t nmemb, void *stream) +{ + struct info *i = stream; + char *p = ptr; + size_t len = size * nmemb; + + if (i->first) + { + i->first = 0; + /* compressed? */ + if (!strncmp(p, "CWS", 3)) + { + *p = 'F'; + i->zlib = 1; + } + HMAC_crunch(i->ctx, (unsigned char *)p, 8); + p += 8; + len -= 8; + i->size = 8; + } + + if (i->zlib) + { + unsigned char out[CHUNK]; + i->zs->next_in = (unsigned char *)p; + i->zs->avail_in = len; + do + { + i->zs->avail_out = CHUNK; + i->zs->next_out = out; + inflate(i->zs, Z_NO_FLUSH); + len = CHUNK - i->zs->avail_out; + i->size += len; + HMAC_crunch(i->ctx, out, len); + } + while (i->zs->avail_out == 0); + } + else + { + i->size += len; + HMAC_crunch(i->ctx, (unsigned char *)p, len); + } + return size * nmemb; +} + +static int tzoff; +static int tzchecked; + +#define JAN02_1980 318340800 + +static const char *monthtab[12] = { "Jan", "Feb", "Mar", + "Apr", "May", "Jun", + "Jul", "Aug", "Sep", + "Oct", "Nov", "Dec" +}; +static const char *days[] = + { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; + +/* Parse an HTTP datestamp into Unix time */ +static time_t +make_unix_time(char *s) +{ + struct tm time; + int i, ysub = 1900, fmt = 0; + char *month; + char *n; + time_t res; + + if (s[3] != ' ') + { + fmt = 1; + if (s[3] != ',') + ysub = 0; + } + for (n = s; *n; ++n) + if (*n == '-' || *n == ':') + *n = ' '; + + time.tm_mon = 0; + n = strchr(s, ' '); + if (fmt) + { + /* Day, DD-MMM-YYYY HH:MM:SS GMT */ + time.tm_mday = strtol(n + 1, &n, 0); + month = n + 1; + n = strchr(month, ' '); + time.tm_year = strtol(n + 1, &n, 0); + time.tm_hour = strtol(n + 1, &n, 0); + time.tm_min = strtol(n + 1, &n, 0); + time.tm_sec = strtol(n + 1, NULL, 0); + } + else + { + /* Unix ctime() format. Does not conform to HTTP spec. */ + /* Day MMM DD HH:MM:SS YYYY */ + month = n + 1; + n = strchr(month, ' '); + while (isspace(*n)) + n++; + time.tm_mday = strtol(n, &n, 0); + time.tm_hour = strtol(n + 1, &n, 0); + time.tm_min = strtol(n + 1, &n, 0); + time.tm_sec = strtol(n + 1, &n, 0); + time.tm_year = strtol(n + 1, NULL, 0); + } + if (time.tm_year > 100) + time.tm_year -= ysub; + + for (i = 0; i < 12; i++) + if (!strncasecmp(month, monthtab[i], 3)) + { + time.tm_mon = i; + break; + } + time.tm_isdst = 0; /* daylight saving is never in effect in GMT */ + + /* this is normally the value of extern int timezone, but some + * braindead C libraries don't provide it. + */ + if (!tzchecked) + { + struct tm *tc; + time_t then = JAN02_1980; + tc = localtime(&then); + tzoff = (12 - tc->tm_hour) * 3600 + tc->tm_min * 60 + tc->tm_sec; + tzchecked = 1; + } + res = mktime(&time); + /* Unfortunately, mktime() assumes the input is in local time, + * not GMT, so we have to correct it here. + */ + if (res != -1) + res += tzoff; + return res; +} + +/* Convert a Unix time to a network time string + * Weekday, DD-MMM-YYYY HH:MM:SS GMT + */ +static void +strtime(time_t * t, char *s) +{ + struct tm *tm; + + tm = gmtime((time_t *) t); + sprintf(s, "%s, %02d %s %d %02d:%02d:%02d GMT", + days[tm->tm_wday], tm->tm_mday, monthtab[tm->tm_mon], + tm->tm_year + 1900, tm->tm_hour, tm->tm_min, tm->tm_sec); +} + +#define HEX2BIN(a) (((a)&0x40)?((a)&0xf)+9:((a)&0xf)) + +int +RTMP_HashSWF(const char *url, unsigned int *size, unsigned char *hash, + int age) +{ + FILE *f = NULL; + char *path, date[64], cctim[64]; + long pos = 0; + time_t ctim = -1, cnow; + int i, got = 0, ret = 0; + unsigned int hlen; + struct info in = { 0 }; + struct HTTP_ctx http = { 0 }; + HTTPResult httpres; + z_stream zs = { 0 }; + AVal home, hpre; + + date[0] = '\0'; +#ifdef _WIN32 +#ifdef XBMC4XBOX + hpre.av_val = "Q:"; + hpre.av_len = 2; + home.av_val = "\\UserData"; +#else + hpre.av_val = getenv("HOMEDRIVE"); + hpre.av_len = strlen(hpre.av_val); + home.av_val = getenv("HOMEPATH"); +#endif +#define DIRSEP "\\" + +#else /* !_WIN32 */ + hpre.av_val = ""; + hpre.av_len = 0; + home.av_val = getenv("HOME"); +#define DIRSEP "/" +#endif + if (!home.av_val) + home.av_val = "."; + home.av_len = strlen(home.av_val); + + /* SWF hash info is cached in a fixed-format file. + * url: + * ctim: HTTP datestamp of when we last checked it. + * date: HTTP datestamp of the SWF's last modification. + * size: SWF size in hex + * hash: SWF hash in hex + * + * These fields must be present in this order. All fields + * besides URL are fixed size. + */ + path = malloc(hpre.av_len + home.av_len + sizeof(DIRSEP ".swfinfo")); + sprintf(path, "%s%s" DIRSEP ".swfinfo", hpre.av_val, home.av_val); + + f = fopen(path, "r+"); + while (f) + { + char buf[4096], *file, *p; + + file = strchr(url, '/'); + if (!file) + break; + file += 2; + file = strchr(file, '/'); + if (!file) + break; + file++; + hlen = file - url; + p = strrchr(file, '/'); + if (p) + file = p; + else + file--; + + while (fgets(buf, sizeof(buf), f)) + { + char *r1; + + got = 0; + + if (strncmp(buf, "url: ", 5)) + continue; + if (strncmp(buf + 5, url, hlen)) + continue; + r1 = strrchr(buf, '/'); + i = strlen(r1); + r1[--i] = '\0'; + if (strncmp(r1, file, i)) + continue; + pos = ftell(f); + while (got < 4 && fgets(buf, sizeof(buf), f)) + { + if (!strncmp(buf, "size: ", 6)) + { + *size = strtol(buf + 6, NULL, 16); + got++; + } + else if (!strncmp(buf, "hash: ", 6)) + { + unsigned char *ptr = hash, *in = (unsigned char *)buf + 6; + int l = strlen((char *)in) - 1; + for (i = 0; i < l; i += 2) + *ptr++ = (HEX2BIN(in[i]) << 4) | HEX2BIN(in[i + 1]); + got++; + } + else if (!strncmp(buf, "date: ", 6)) + { + buf[strlen(buf) - 1] = '\0'; + strncpy(date, buf + 6, sizeof(date)); + got++; + } + else if (!strncmp(buf, "ctim: ", 6)) + { + buf[strlen(buf) - 1] = '\0'; + ctim = make_unix_time(buf + 6); + got++; + } + else if (!strncmp(buf, "url: ", 5)) + break; + } + break; + } + break; + } + + cnow = time(NULL); + /* If we got a cache time, see if it's young enough to use directly */ + if (age && ctim > 0) + { + ctim = cnow - ctim; + ctim /= 3600 * 24; /* seconds to days */ + if (ctim < age) /* ok, it's new enough */ + goto out; + } + + in.first = 1; + HMAC_setup(in.ctx, "Genuine Adobe Flash Player 001", 30); + inflateInit(&zs); + in.zs = &zs; + + http.date = date; + http.data = ∈ + + httpres = HTTP_get(&http, url, swfcrunch); + + inflateEnd(&zs); + + if (httpres != HTTPRES_OK && httpres != HTTPRES_OK_NOT_MODIFIED) + { + ret = -1; + if (httpres == HTTPRES_LOST_CONNECTION) + RTMP_Log(RTMP_LOGERROR, "%s: connection lost while downloading swfurl %s", + __FUNCTION__, url); + else if (httpres == HTTPRES_NOT_FOUND) + RTMP_Log(RTMP_LOGERROR, "%s: swfurl %s not found", __FUNCTION__, url); + else + RTMP_Log(RTMP_LOGERROR, "%s: couldn't contact swfurl %s (HTTP error %d)", + __FUNCTION__, url, http.status); + } + else + { + if (got && pos) + fseek(f, pos, SEEK_SET); + else + { + char *q; + if (!f) + f = fopen(path, "w"); + if (!f) + { + int err = errno; + RTMP_Log(RTMP_LOGERROR, + "%s: couldn't open %s for writing, errno %d (%s)", + __FUNCTION__, path, err, strerror(err)); + ret = -1; + goto out; + } + fseek(f, 0, SEEK_END); + q = strchr(url, '?'); + if (q) + i = q - url; + else + i = strlen(url); + + fprintf(f, "url: %.*s\n", i, url); + } + strtime(&cnow, cctim); + fprintf(f, "ctim: %s\n", cctim); + + if (!in.first) + { + HMAC_finish(in.ctx, hash, hlen); + *size = in.size; + + fprintf(f, "date: %s\n", date); + fprintf(f, "size: %08x\n", in.size); + fprintf(f, "hash: "); + for (i = 0; i < SHA256_DIGEST_LENGTH; i++) + fprintf(f, "%02x", hash[i]); + fprintf(f, "\n"); + } + } + HMAC_close(in.ctx); +out: + free(path); + if (f) + fclose(f); + return ret; +} +#else +int +RTMP_HashSWF(const char *url, unsigned int *size, unsigned char *hash, + int age) +{ + return -1; +} +#endif diff --git a/librtmp/http.h b/librtmp/http.h new file mode 100644 index 0000000..cf3d903 --- /dev/null +++ b/librtmp/http.h @@ -0,0 +1,47 @@ +#ifndef __RTMP_HTTP_H__ +#define __RTMP_HTTP_H__ +/* + * Copyright (C) 2010 Howard Chu + * Copyright (C) 2010 Antti Ajanki + * + * This file is part of librtmp. + * + * librtmp is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1, + * or (at your option) any later version. + * + * librtmp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with librtmp see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/lgpl.html + */ + +typedef enum { + HTTPRES_OK, /* result OK */ + HTTPRES_OK_NOT_MODIFIED, /* not modified since last request */ + HTTPRES_NOT_FOUND, /* not found */ + HTTPRES_BAD_REQUEST, /* client error */ + HTTPRES_SERVER_ERROR, /* server reported an error */ + HTTPRES_REDIRECTED, /* resource has been moved */ + HTTPRES_LOST_CONNECTION /* connection lost while waiting for data */ +} HTTPResult; + +struct HTTP_ctx { + char *date; + int size; + int status; + void *data; +}; + +typedef size_t (HTTP_read_callback)(void *ptr, size_t size, size_t nmemb, void *stream); + +HTTPResult HTTP_get(struct HTTP_ctx *http, const char *url, HTTP_read_callback *cb); + +#endif diff --git a/librtmp/librtmp.3 b/librtmp/librtmp.3 new file mode 100644 index 0000000..7c424aa --- /dev/null +++ b/librtmp/librtmp.3 @@ -0,0 +1,210 @@ +.TH LIBRTMP 3 "2011-07-20" "RTMPDump v2.4" +.\" Copyright 2011 Howard Chu. +.\" Copying permitted according to the GNU General Public License V2. +.SH NAME +librtmp \- RTMPDump Real-Time Messaging Protocol API +.SH LIBRARY +RTMPDump RTMP (librtmp, -lrtmp) +.SH SYNOPSIS +.B #include +.SH DESCRIPTION +The Real-Time Messaging Protocol (RTMP) is used for streaming +multimedia content across a TCP/IP network. This API provides most client +functions and a few server functions needed to support RTMP, RTMP tunneled +in HTTP (RTMPT), encrypted RTMP (RTMPE), RTMP over SSL/TLS (RTMPS) and +tunneled variants of these encrypted types (RTMPTE, RTMPTS). The basic +RTMP specification has been published by Adobe but this API was +reverse-engineered without use of the Adobe specification. As such, it may +deviate from any published specifications but it usually duplicates the +actual behavior of the original Adobe clients. + +The RTMPDump software package includes a basic client utility program +in +.BR rtmpdump (1), +some sample servers, and a library used to provide programmatic access +to the RTMP protocol. This man page gives an overview of the RTMP +library routines. These routines are found in the -lrtmp library. Many +other routines are also available, but they are not documented yet. + +The basic interaction is as follows. A session handle is created using +.BR RTMP_Alloc () +and initialized using +.BR RTMP_Init (). +All session parameters are provided using +.BR RTMP_SetupURL (). +The network connection is established using +.BR RTMP_Connect (), +and then the RTMP session is established using +.BR RTMP_ConnectStream (). +The stream is read using +.BR RTMP_Read (). +A client can publish a stream by calling +.BR RTMP_EnableWrite () +before the +.BR RTMP_Connect () +call, and then using +.BR RTMP_Write () +after the session is established. +While a stream is playing it may be paused and unpaused using +.BR RTMP_Pause (). +The stream playback position can be moved using +.BR RTMP_Seek (). +When +.BR RTMP_Read () +returns 0 bytes, the stream is complete and may be closed using +.BR RTMP_Close (). +The session handle is freed using +.BR RTMP_Free (). + +All data is transferred using FLV format. The basic session requires +an RTMP URL. The RTMP URL format is of the form +.nf + rtmp[t][e|s]://hostname[:port][/app[/playpath]] +.fi + +Plain rtmp, as well as tunneled and encrypted sessions are supported. + +Additional options may be specified by appending space-separated +key=value pairs to the URL. Special characters in values may need +to be escaped to prevent misinterpretation by the option parser. +The escape encoding uses a backslash followed by two hexadecimal digits +representing the ASCII value of the character. E.g., spaces must +be escaped as \fB\\20\fP and backslashes must be escaped as \fB\\5c\fP. +.SH OPTIONS +.SS "Network Parameters" +These options define how to connect to the media server. +.TP +.BI socks= host:port +Use the specified SOCKS4 proxy. +.SS "Connection Parameters" +These options define the content of the RTMP Connect request packet. +If correct values are not provided, the media server will reject the +connection attempt. +.TP +.BI app= name +Name of application to connect to on the RTMP server. Overrides +the app in the RTMP URL. Sometimes the librtmp URL parser cannot +determine the app name automatically, so it must be given explicitly +using this option. +.TP +.BI tcUrl= url +URL of the target stream. Defaults to rtmp[t][e|s]://host[:port]/app. +.TP +.BI pageUrl= url +URL of the web page in which the media was embedded. By default no +value will be sent. +.TP +.BI swfUrl= url +URL of the SWF player for the media. By default no value will be sent. +.TP +.BI flashVer= version +Version of the Flash plugin used to run the SWF player. The +default is "LNX 10,0,32,18". +.TP +.BI conn= type:data +Append arbitrary AMF data to the Connect message. The type +must be B for Boolean, N for number, S for string, O for object, or Z +for null. For Booleans the data must be either 0 or 1 for FALSE or TRUE, +respectively. Likewise for Objects the data must be 0 or 1 to end or +begin an object, respectively. Data items in subobjects may be named, by +prefixing the type with 'N' and specifying the name before the value, e.g. +NB:myFlag:1. This option may be used multiple times to construct arbitrary +AMF sequences. E.g. +.nf + conn=B:1 conn=S:authMe conn=O:1 conn=NN:code:1.23 conn=NS:flag:ok conn=O:0 +.fi +.SS "Session Parameters" +These options take effect after the Connect request has succeeded. +.TP +.BI playpath= path +Overrides the playpath parsed from the RTMP URL. Sometimes the +rtmpdump URL parser cannot determine the correct playpath +automatically, so it must be given explicitly using this option. +.TP +.BI playlist= 0|1 +If the value is 1 or TRUE, issue a set_playlist command before sending the +play command. The playlist will just contain the current playpath. If the +value is 0 or FALSE, the set_playlist command will not be sent. The +default is FALSE. +.TP +.BI live= 0|1 +Specify that the media is a live stream. No resuming or seeking in +live streams is possible. +.TP +.BI subscribe= path +Name of live stream to subscribe to. Defaults to +.IR playpath . +.TP +.BI start= num +Start at +.I num +seconds into the stream. Not valid for live streams. +.TP +.BI stop= num +Stop at +.I num +seconds into the stream. +.TP +.BI buffer= num +Set buffer time to +.I num +milliseconds. The default is 30000. +.TP +.BI timeout= num +Timeout the session after +.I num +seconds without receiving any data from the server. The default is 120. +.SS "Security Parameters" +These options handle additional authentication requests from the server. +.TP +.BI token= key +Key for SecureToken response, used if the server requires SecureToken +authentication. +.TP +.BI jtv= JSON +JSON token used by legacy Justin.tv servers. Invokes NetStream.Authenticate.UsherToken +.TP +.BI swfVfy= 0|1 +If the value is 1 or TRUE, the SWF player is retrieved from the +specified +.I swfUrl +for performing SWF Verification. The SWF hash and size (used in the +verification step) are computed automatically. Also the SWF information is +cached in a +.I .swfinfo +file in the user's home directory, so that it doesn't need to be retrieved +and recalculated every time. The .swfinfo file records +the SWF URL, the time it was fetched, the modification timestamp of the SWF +file, its size, and its hash. By default, the cached info will be used +for 30 days before re-checking. +.TP +.BI swfAge= days +Specify how many days to use the cached SWF info before re-checking. Use +0 to always check the SWF URL. Note that if the check shows that the +SWF file has the same modification timestamp as before, it will not be +retrieved again. +.SH EXAMPLES +An example character string suitable for use with +.BR RTMP_SetupURL (): +.nf + "rtmp://flashserver:1935/ondemand/thefile swfUrl=http://flashserver/player.swf swfVfy=1" +.fi +.SH ENVIRONMENT +.TP +.B HOME +The value of +.RB $ HOME +is used as the location for the +.I .swfinfo +file. +.SH FILES +.TP +.I $HOME/.swfinfo +Cache of SWF Verification information +.SH "SEE ALSO" +.BR rtmpdump (1), +.BR rtmpgw (8) +.SH AUTHORS +Andrej Stepanchuk, Howard Chu, The Flvstreamer Team +.br + diff --git a/librtmp/librtmp.3.html b/librtmp/librtmp.3.html new file mode 100644 index 0000000..6f59851 --- /dev/null +++ b/librtmp/librtmp.3.html @@ -0,0 +1,312 @@ + + +LIBRTMP(3): + + + + + +
LIBRTMP(3)LIBRTMP(3) +
RTMPDump v2.42011-07-20LIBRTMP(3) +


    + +
+ +

NAME

    +librtmp − RTMPDump Real-Time Messaging Protocol API +
+ +

LIBRARY

    +RTMPDump RTMP (librtmp, -lrtmp) +
+ +

SYNOPSIS

    +#include <librtmp/rtmp.h> +
+ +

DESCRIPTION

    +The Real-Time Messaging Protocol (RTMP) is used for streaming +multimedia content across a TCP/IP network. This API provides most client +functions and a few server functions needed to support RTMP, RTMP tunneled +in HTTP (RTMPT), encrypted RTMP (RTMPE), RTMP over SSL/TLS (RTMPS) and +tunneled variants of these encrypted types (RTMPTE, RTMPTS). The basic +RTMP specification has been published by Adobe but this API was +reverse-engineered without use of the Adobe specification. As such, it may +deviate from any published specifications but it usually duplicates the +actual behavior of the original Adobe clients. +

    +The RTMPDump software package includes a basic client utility program +in +rtmpdump(1), +some sample servers, and a library used to provide programmatic access +to the RTMP protocol. This man page gives an overview of the RTMP +library routines. These routines are found in the -lrtmp library. Many +other routines are also available, but they are not documented yet. +

    +The basic interaction is as follows. A session handle is created using +RTMP_Alloc() +and initialized using +RTMP_Init(). +All session parameters are provided using +RTMP_SetupURL(). +The network connection is established using +RTMP_Connect(), +and then the RTMP session is established using +RTMP_ConnectStream(). +The stream is read using +RTMP_Read(). +A client can publish a stream by calling +RTMP_EnableWrite() +before the +RTMP_Connect() +call, and then using +RTMP_Write() +after the session is established. +While a stream is playing it may be paused and unpaused using +RTMP_Pause(). +The stream playback position can be moved using +RTMP_Seek(). +When +RTMP_Read() +returns 0 bytes, the stream is complete and may be closed using +RTMP_Close(). +The session handle is freed using +RTMP_Free(). +

    +All data is transferred using FLV format. The basic session requires +an RTMP URL. The RTMP URL format is of the form +

    +  rtmp[t][e|s]://hostname[:port][/app[/playpath]]
    +
    +

    +Plain rtmp, as well as tunneled and encrypted sessions are supported. +

    +Additional options may be specified by appending space-separated +key=value pairs to the URL. Special characters in values may need +to be escaped to prevent misinterpretation by the option parser. +The escape encoding uses a backslash followed by two hexadecimal digits +representing the ASCII value of the character. E.g., spaces must +be escaped as \20 and backslashes must be escaped as \5c. +

+ +

OPTIONS

    +
+ +

Network Parameters

    +These options define how to connect to the media server. +

    +

    +socks=host:port +
    +Use the specified SOCKS4 proxy. +
    +
+ +

Connection Parameters

    +These options define the content of the RTMP Connect request packet. +If correct values are not provided, the media server will reject the +connection attempt. +

    +

    +app=name +
    +Name of application to connect to on the RTMP server. Overrides +the app in the RTMP URL. Sometimes the librtmp URL parser cannot +determine the app name automatically, so it must be given explicitly +using this option. +
    +

    +

    +tcUrl=url +
    +URL of the target stream. Defaults to rtmp[t][e|s]://host[:port]/app. +
    +

    +

    +pageUrl=url +
    +URL of the web page in which the media was embedded. By default no +value will be sent. +
    +

    +

    +swfUrl=url +
    +URL of the SWF player for the media. By default no value will be sent. +
    +

    +

    +flashVer=version +
    +Version of the Flash plugin used to run the SWF player. The +default is "LNX 10,0,32,18". +
    +

    +

    +conn=type:data +
    +Append arbitrary AMF data to the Connect message. The type +must be B for Boolean, N for number, S for string, O for object, or Z +for null. For Booleans the data must be either 0 or 1 for FALSE or TRUE, +respectively. Likewise for Objects the data must be 0 or 1 to end or +begin an object, respectively. Data items in subobjects may be named, by +prefixing the type with 'N' and specifying the name before the value, e.g. +NB:myFlag:1. This option may be used multiple times to construct arbitrary +AMF sequences. E.g. +
    +  conn=B:1 conn=S:authMe conn=O:1 conn=NN:code:1.23 conn=NS:flag:ok conn=O:0
    +
    +
    +
+ +

Session Parameters

    +These options take effect after the Connect request has succeeded. +

    +

    +playpath=path +
    +Overrides the playpath parsed from the RTMP URL. Sometimes the +rtmpdump URL parser cannot determine the correct playpath +automatically, so it must be given explicitly using this option. +
    +

    +

    +playlist=0|1 +
    +If the value is 1 or TRUE, issue a set_playlist command before sending the +play command. The playlist will just contain the current playpath. If the +value is 0 or FALSE, the set_playlist command will not be sent. The +default is FALSE. +
    +

    +

    +live=0|1 +
    +Specify that the media is a live stream. No resuming or seeking in +live streams is possible. +
    +

    +

    +subscribe=path +
    +Name of live stream to subscribe to. Defaults to +playpath. +
    +

    +

    +start=num +
    +Start at +num +seconds into the stream. Not valid for live streams. +
    +

    +

    +stop=num +
    +Stop at +num +seconds into the stream. +
    +

    +

    +buffer=num +
    +Set buffer time to +num +milliseconds. The default is 30000. +
    +

    +

    +timeout=num +
    +Timeout the session after +num +seconds without receiving any data from the server. The default is 120. +
    +
+ +

Security Parameters

    +These options handle additional authentication requests from the server. +

    +

    +token=key +
    +Key for SecureToken response, used if the server requires SecureToken +authentication. +
    +

    +

    +jtv=JSON +
    +JSON token used by legacy Justin.tv servers. Invokes NetStream.Authenticate.UsherToken +
    +

    +

    +swfVfy=0|1 +
    +If the value is 1 or TRUE, the SWF player is retrieved from the +specified +swfUrl +for performing SWF Verification. The SWF hash and size (used in the +verification step) are computed automatically. Also the SWF information is +cached in a +.swfinfo +file in the user's home directory, so that it doesn't need to be retrieved +and recalculated every time. The .swfinfo file records +the SWF URL, the time it was fetched, the modification timestamp of the SWF +file, its size, and its hash. By default, the cached info will be used +for 30 days before re-checking. +
    +

    +

    +swfAge=days +
    +Specify how many days to use the cached SWF info before re-checking. Use +0 to always check the SWF URL. Note that if the check shows that the +SWF file has the same modification timestamp as before, it will not be +retrieved again. +
    +
+ +

EXAMPLES

    +An example character string suitable for use with +RTMP_SetupURL(): +
    +  "rtmp://flashserver:1935/ondemand/thefile swfUrl=http://flashserver/player.swf swfVfy=1"
    +
    +
+ +

ENVIRONMENT

    +

    +

    +HOME +
    +The value of +$HOME +is used as the location for the +.swfinfo +file. +
    +
+ +

FILES

    +

    +

    +$HOME/.swfinfo +
    +Cache of SWF Verification information +
    +
+ +

SEE ALSO

+ +

AUTHORS

diff --git a/librtmp/librtmp.pc.in b/librtmp/librtmp.pc.in new file mode 100644 index 0000000..551b4d9 --- /dev/null +++ b/librtmp/librtmp.pc.in @@ -0,0 +1,13 @@ +prefix=@prefix@ +exec_prefix=${prefix} +libdir=@libdir@ +incdir=${prefix}/include + +Name: librtmp +Description: RTMP implementation +Version: @VERSION@ +Requires: @CRYPTO_REQ@ +URL: http://rtmpdump.mplayerhq.hu +Libs: -L${libdir} -lrtmp -lz @PUBLIC_LIBS@ +Libs.private: @PRIVATE_LIBS@ +Cflags: -I${incdir} diff --git a/librtmp/log.c b/librtmp/log.c new file mode 100644 index 0000000..52e849f --- /dev/null +++ b/librtmp/log.c @@ -0,0 +1,223 @@ +/* + * Copyright (C) 2008-2009 Andrej Stepanchuk + * Copyright (C) 2009-2010 Howard Chu + * + * This file is part of librtmp. + * + * librtmp is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1, + * or (at your option) any later version. + * + * librtmp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with librtmp see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/lgpl.html + */ + +#include +#include +#include +#include + +#include "rtmp_sys.h" +#include "log.h" + +#define MAX_PRINT_LEN 2048 + +RTMP_LogLevel RTMP_debuglevel = RTMP_LOGERROR; + +static int neednl; + +static FILE *fmsg; + +static RTMP_LogCallback rtmp_log_default, *cb = rtmp_log_default; + +static const char *levels[] = { + "CRIT", "ERROR", "WARNING", "INFO", + "DEBUG", "DEBUG2" +}; + +static void rtmp_log_default(int level, const char *format, va_list vl) +{ + char str[MAX_PRINT_LEN]=""; + + vsnprintf(str, MAX_PRINT_LEN-1, format, vl); + + /* Filter out 'no-name' */ + if ( RTMP_debuglevel RTMP_debuglevel ) + return; + + va_start(args, format); + cb(level, format, args); + va_end(args); +} + +static const char hexdig[] = "0123456789abcdef"; + +void RTMP_LogHex(int level, const uint8_t *data, unsigned long len) +{ + unsigned long i; + char line[50], *ptr; + + if ( level > RTMP_debuglevel ) + return; + + ptr = line; + + for(i=0; i> 4)]; + *ptr++ = hexdig[0x0f & data[i]]; + if ((i & 0x0f) == 0x0f) { + *ptr = '\0'; + ptr = line; + RTMP_Log(level, "%s", line); + } else { + *ptr++ = ' '; + } + } + if (i & 0x0f) { + *ptr = '\0'; + RTMP_Log(level, "%s", line); + } +} + +void RTMP_LogHexString(int level, const uint8_t *data, unsigned long len) +{ +#define BP_OFFSET 9 +#define BP_GRAPH 60 +#define BP_LEN 80 + char line[BP_LEN]; + unsigned long i; + + if ( !data || level > RTMP_debuglevel ) + return; + + /* in case len is zero */ + line[0] = '\0'; + + for ( i = 0 ; i < len ; i++ ) { + int n = i % 16; + unsigned off; + + if( !n ) { + if( i ) RTMP_Log( level, "%s", line ); + memset( line, ' ', sizeof(line)-2 ); + line[sizeof(line)-2] = '\0'; + + off = i % 0x0ffffU; + + line[2] = hexdig[0x0f & (off >> 12)]; + line[3] = hexdig[0x0f & (off >> 8)]; + line[4] = hexdig[0x0f & (off >> 4)]; + line[5] = hexdig[0x0f & off]; + line[6] = ':'; + } + + off = BP_OFFSET + n*3 + ((n >= 8)?1:0); + line[off] = hexdig[0x0f & ( data[i] >> 4 )]; + line[off+1] = hexdig[0x0f & data[i]]; + + off = BP_GRAPH + n + ((n >= 8)?1:0); + + if ( isprint( data[i] )) { + line[BP_GRAPH + n] = data[i]; + } else { + line[BP_GRAPH + n] = '.'; + } + } + + RTMP_Log( level, "%s", line ); +} + +/* These should only be used by apps, never by the library itself */ +void RTMP_LogPrintf(const char *format, ...) +{ + char str[MAX_PRINT_LEN]=""; + int len; + va_list args; + va_start(args, format); + len = vsnprintf(str, MAX_PRINT_LEN-1, format, args); + va_end(args); + + if ( RTMP_debuglevel==RTMP_LOGCRIT ) + return; + + if ( !fmsg ) fmsg = stderr; + + if (neednl) { + putc('\n', fmsg); + neednl = 0; + } + + if (len > MAX_PRINT_LEN-1) + len = MAX_PRINT_LEN-1; + fprintf(fmsg, "%s", str); + if (str[len-1] == '\n') + fflush(fmsg); +} + +void RTMP_LogStatus(const char *format, ...) +{ + char str[MAX_PRINT_LEN]=""; + va_list args; + va_start(args, format); + vsnprintf(str, MAX_PRINT_LEN-1, format, args); + va_end(args); + + if ( RTMP_debuglevel==RTMP_LOGCRIT ) + return; + + if ( !fmsg ) fmsg = stderr; + + fprintf(fmsg, "%s", str); + fflush(fmsg); + neednl = 1; +} diff --git a/librtmp/log.h b/librtmp/log.h new file mode 100644 index 0000000..2adb111 --- /dev/null +++ b/librtmp/log.h @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2008-2009 Andrej Stepanchuk + * Copyright (C) 2009-2010 Howard Chu + * + * This file is part of librtmp. + * + * librtmp is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1, + * or (at your option) any later version. + * + * librtmp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with librtmp see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/lgpl.html + */ + +#ifndef __RTMP_LOG_H__ +#define __RTMP_LOG_H__ + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif +/* Enable this to get full debugging output */ +/* #define _DEBUG */ + +#ifdef _DEBUG +#undef NODEBUG +#endif + +typedef enum +{ RTMP_LOGCRIT=0, RTMP_LOGERROR, RTMP_LOGWARNING, RTMP_LOGINFO, + RTMP_LOGDEBUG, RTMP_LOGDEBUG2, RTMP_LOGALL +} RTMP_LogLevel; + +extern RTMP_LogLevel RTMP_debuglevel; + +typedef void (RTMP_LogCallback)(int level, const char *fmt, va_list); +void RTMP_LogSetCallback(RTMP_LogCallback *cb); +void RTMP_LogSetOutput(FILE *file); +#ifdef __GNUC__ +void RTMP_LogPrintf(const char *format, ...) __attribute__ ((__format__ (__printf__, 1, 2))); +void RTMP_LogStatus(const char *format, ...) __attribute__ ((__format__ (__printf__, 1, 2))); +void RTMP_Log(int level, const char *format, ...) __attribute__ ((__format__ (__printf__, 2, 3))); +#else +void RTMP_LogPrintf(const char *format, ...); +void RTMP_LogStatus(const char *format, ...); +void RTMP_Log(int level, const char *format, ...); +#endif +void RTMP_LogHex(int level, const uint8_t *data, unsigned long len); +void RTMP_LogHexString(int level, const uint8_t *data, unsigned long len); +void RTMP_LogSetLevel(RTMP_LogLevel lvl); +RTMP_LogLevel RTMP_LogGetLevel(void); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/librtmp/parseurl.c b/librtmp/parseurl.c new file mode 100644 index 0000000..5b915b9 --- /dev/null +++ b/librtmp/parseurl.c @@ -0,0 +1,290 @@ +/* + * Copyright (C) 2009 Andrej Stepanchuk + * Copyright (C) 2009-2010 Howard Chu + * + * This file is part of librtmp. + * + * librtmp is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1, + * or (at your option) any later version. + * + * librtmp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with librtmp see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/lgpl.html + */ + +#include +#include + +#include +#include + +#include "rtmp_sys.h" +#include "log.h" +#include "strncasecmp.h" + +int RTMP_ParseURL(const char *url, int *protocol, AVal *host, unsigned int *port, + AVal *playpath, AVal *app) +{ + char *p, *end, *col, *ques, *slash; + + RTMP_Log(RTMP_LOGDEBUG, "Parsing..."); + + *protocol = RTMP_PROTOCOL_RTMP; + *port = 0; + playpath->av_len = 0; + playpath->av_val = NULL; + app->av_len = 0; + app->av_val = NULL; + + /* Old School Parsing */ + + /* look for usual :// pattern */ + p = strstr(url, "://"); + if(!p) { + RTMP_Log(RTMP_LOGERROR, "RTMP URL: No :// in url!"); + return FALSE; + } + { + int len = (int)(p-url); + + if(len == 4 && strncasecmp(url, "rtmp", 4)==0) + *protocol = RTMP_PROTOCOL_RTMP; + else if(len == 5 && strncasecmp(url, "rtmpt", 5)==0) + *protocol = RTMP_PROTOCOL_RTMPT; + else if(len == 5 && strncasecmp(url, "rtmps", 5)==0) + *protocol = RTMP_PROTOCOL_RTMPS; + else if(len == 5 && strncasecmp(url, "rtmpe", 5)==0) + *protocol = RTMP_PROTOCOL_RTMPE; + else if(len == 5 && strncasecmp(url, "rtmfp", 5)==0) + *protocol = RTMP_PROTOCOL_RTMFP; + else if(len == 6 && strncasecmp(url, "rtmpte", 6)==0) + *protocol = RTMP_PROTOCOL_RTMPTE; + else if(len == 6 && strncasecmp(url, "rtmpts", 6)==0) + *protocol = RTMP_PROTOCOL_RTMPTS; + else { + RTMP_Log(RTMP_LOGWARNING, "Unknown protocol!\n"); + goto parsehost; + } + } + + RTMP_Log(RTMP_LOGDEBUG, "Parsed protocol: %d", *protocol); + +parsehost: + /* let's get the hostname */ + p+=3; + + /* check for sudden death */ + if(*p==0) { + RTMP_Log(RTMP_LOGWARNING, "No hostname in URL!"); + return FALSE; + } + + end = p + strlen(p); + col = strchr(p, ':'); + ques = strchr(p, '?'); + slash = strchr(p, '/'); + + { + int hostlen; + if(slash) + hostlen = slash - p; + else + hostlen = end - p; + if(col && col -p < hostlen) + hostlen = col - p; + + if(hostlen < 256) { + host->av_val = p; + host->av_len = hostlen; + RTMP_Log(RTMP_LOGDEBUG, "Parsed host : %.*s", hostlen, host->av_val); + } else { + RTMP_Log(RTMP_LOGWARNING, "Hostname exceeds 255 characters!"); + } + + p+=hostlen; + } + + /* get the port number if available */ + if(*p == ':') { + unsigned int p2; + p++; + p2 = atoi(p); + if(p2 > 65535) { + RTMP_Log(RTMP_LOGWARNING, "Invalid port number!"); + } else { + *port = p2; + } + } + + if(!slash) { + RTMP_Log(RTMP_LOGWARNING, "No application or playpath in URL!"); + return TRUE; + } + p = slash+1; + + { + /* parse application + * + * rtmp://host[:port]/app[/appinstance][/...] + * application = app[/appinstance] + */ + + char *slash2, *slash3 = NULL, *slash4 = NULL; + int applen, appnamelen; + + slash2 = strchr(p, '/'); + if(slash2) + slash3 = strchr(slash2+1, '/'); + if(slash3) + slash4 = strchr(slash3+1, '/'); + + applen = end-p; /* ondemand, pass all parameters as app */ + appnamelen = applen; /* ondemand length */ + + if(ques && strstr(p, "slist=")) { /* whatever it is, the '?' and slist= means we need to use everything as app and parse plapath from slist= */ + appnamelen = ques-p; + } + else if(strncmp(p, "ondemand/", 9)==0) { + /* app = ondemand/foobar, only pass app=ondemand */ + applen = 8; + appnamelen = 8; + } + else { /* app!=ondemand, so app is app[/appinstance] */ + if(slash4) + appnamelen = slash4-p; + else if(slash3) + appnamelen = slash3-p; + else if(slash2) + appnamelen = slash2-p; + + applen = appnamelen; + } + + app->av_val = p; + app->av_len = applen; + RTMP_Log(RTMP_LOGDEBUG, "Parsed app : %.*s", applen, p); + + p += appnamelen; + } + + if (*p == '/') + p++; + + if (end-p) { + AVal av = {p, end-p}; + RTMP_ParsePlaypath(&av, playpath); + } + + return TRUE; +} + +/* + * Extracts playpath from RTMP URL. playpath is the file part of the + * URL, i.e. the part that comes after rtmp://host:port/app/ + * + * Returns the stream name in a format understood by FMS. The name is + * the playpath part of the URL with formatting depending on the stream + * type: + * + * mp4 streams: prepend "mp4:", remove extension + * mp3 streams: prepend "mp3:", remove extension + * flv streams: remove extension + */ +void RTMP_ParsePlaypath(AVal *in, AVal *out) { + int addMP4 = 0; + int addMP3 = 0; + int subExt = 0; + const char *playpath = in->av_val; + const char *temp, *q, *ext = NULL; + const char *ppstart = playpath; + char *streamname, *destptr, *p; + + int pplen = in->av_len; + + out->av_val = NULL; + out->av_len = 0; + + if ((*ppstart == '?') && + (temp=strstr(ppstart, "slist=")) != 0) { + ppstart = temp+6; + pplen = strlen(ppstart); + + temp = strchr(ppstart, '&'); + if (temp) { + pplen = temp-ppstart; + } + } + + q = strchr(ppstart, '?'); + if (pplen >= 4) { + if (q) + ext = q-4; + else + ext = &ppstart[pplen-4]; + if ((strncmp(ext, ".f4v", 4) == 0) || + (strncmp(ext, ".mp4", 4) == 0)) { + addMP4 = 1; + subExt = 1; + /* Only remove .flv from rtmp URL, not slist params */ + } else if ((ppstart == playpath) && + (strncmp(ext, ".flv", 4) == 0)) { + subExt = 1; + } else if (strncmp(ext, ".mp3", 4) == 0) { + addMP3 = 1; + subExt = 1; + } + } + + streamname = (char *)malloc((pplen+4+1)*sizeof(char)); + if (!streamname) + return; + + destptr = streamname; + if (addMP4) { + if (strncmp(ppstart, "mp4:", 4)) { + strcpy(destptr, "mp4:"); + destptr += 4; + } else { + subExt = 0; + } + } else if (addMP3) { + if (strncmp(ppstart, "mp3:", 4)) { + strcpy(destptr, "mp3:"); + destptr += 4; + } else { + subExt = 0; + } + } + + for (p=(char *)ppstart; pplen >0;) { + /* skip extension */ + if (subExt && p == ext) { + p += 4; + pplen -= 4; + continue; + } + if (*p == '%') { + unsigned int c; + sscanf(p+1, "%02x", &c); + *destptr++ = c; + pplen -= 3; + p += 3; + } else { + *destptr++ = *p++; + pplen--; + } + } + *destptr = '\0'; + + out->av_val = streamname; + out->av_len = destptr - streamname; +} diff --git a/librtmp/rtmp.c b/librtmp/rtmp.c new file mode 100644 index 0000000..261583e --- /dev/null +++ b/librtmp/rtmp.c @@ -0,0 +1,5350 @@ +/* + * Copyright (C) 2005-2008 Team XBMC + * http://www.xbmc.org + * Copyright (C) 2008-2009 Andrej Stepanchuk + * Copyright (C) 2009-2010 Howard Chu + * + * This file is part of librtmp. + * + * librtmp is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1, + * or (at your option) any later version. + * + * librtmp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with librtmp see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/lgpl.html + */ + +#include +#include +#include +#include +#include + +#include "rtmp_sys.h" +#include "log.h" +#include "strncasecmp.h" + +#ifdef CRYPTO +#ifdef USE_POLARSSL +#include +#include +#include +#define MD5_DIGEST_LENGTH 16 + +static const char *my_dhm_P = + "E4004C1F94182000103D883A448B3F80" \ + "2CE4B44A83301270002C20D0321CFD00" \ + "11CCEF784C26A400F43DFB901BCA7538" \ + "F2C6B176001CF5A0FD16D2C48B1D0C1C" \ + "F6AC8E1DA6BCC3B4E1F96B0564965300" \ + "FFA1D0B601EB2800F489AA512C4B248C" \ + "01F76949A60BB7F00A40B1EAB64BDD48" \ + "E8A700D60B7F1200FA8E77B0A979DABF"; + +static const char *my_dhm_G = "4"; + +#elif defined(USE_GNUTLS) +#include +#define MD5_DIGEST_LENGTH 16 +#include +#include +#else /* USE_OPENSSL */ +#include +#include +#include +#include +#include +#endif +TLS_CTX RTMP_TLS_ctx; +#endif + +#define RTMP_SIG_SIZE 1536 +#define RTMP_LARGE_HEADER_SIZE 12 + +static const int packetSize[] = { 12, 8, 4, 1 }; + +int RTMP_ctrlC; + +const char RTMPProtocolStrings[][7] = { + "RTMP", + "RTMPT", + "RTMPE", + "RTMPTE", + "RTMPS", + "RTMPTS", + "", + "", + "RTMFP" +}; + +const char RTMPProtocolStringsLower[][7] = { + "rtmp", + "rtmpt", + "rtmpe", + "rtmpte", + "rtmps", + "rtmpts", + "", + "", + "rtmfp" +}; + +static const char *RTMPT_cmds[] = { + "open", + "send", + "idle", + "close" +}; + +typedef enum { + RTMPT_OPEN=0, RTMPT_SEND, RTMPT_IDLE, RTMPT_CLOSE +} RTMPTCmd; + +static int DumpMetaData(AMFObject *obj); +static int HandShake(RTMP *r, int FP9HandShake); +static int SocksNegotiate(RTMP *r); + +static int SendConnectPacket(RTMP *r, RTMPPacket *cp); +static int SendCheckBW(RTMP *r); +static int SendCheckBWResult(RTMP *r, double txn); +static int SendDeleteStream(RTMP *r, double dStreamId); +static int SendFCSubscribe(RTMP *r, AVal *subscribepath); +static int SendPlay(RTMP *r); +static int SendBytesReceived(RTMP *r); +static int SendUsherToken(RTMP *r, AVal *usherToken); + +#if 0 /* unused */ +static int SendBGHasStream(RTMP *r, double dId, AVal *playpath); +#endif + +static int HandleInvoke(RTMP *r, const char *body, unsigned int nBodySize); +static int HandleMetadata(RTMP *r, char *body, unsigned int len); +static void HandleChangeChunkSize(RTMP *r, const RTMPPacket *packet); +static void HandleAudio(RTMP *r, const RTMPPacket *packet); +static void HandleVideo(RTMP *r, const RTMPPacket *packet); +static void HandleCtrl(RTMP *r, const RTMPPacket *packet); +static void HandleServerBW(RTMP *r, const RTMPPacket *packet); +static void HandleClientBW(RTMP *r, const RTMPPacket *packet); + +static int ReadN(RTMP *r, char *buffer, int n); +static int WriteN(RTMP *r, const char *buffer, int n); + +static void DecodeTEA(AVal *key, AVal *text); + +static int HTTP_Post(RTMP *r, RTMPTCmd cmd, const char *buf, int len); +static int HTTP_read(RTMP *r, int fill); + +static void CloseInternal(RTMP *r, int reconnect); + +#ifndef _WIN32 +static int clk_tck; +#endif + +#ifdef CRYPTO +#include "handshake.h" +#endif + +uint32_t +RTMP_GetTime() +{ +#ifdef _DEBUG + return 0; +#elif defined(_WIN32) + return timeGetTime(); +#else + struct tms t; + if (!clk_tck) clk_tck = sysconf(_SC_CLK_TCK); + return times(&t) * 1000 / clk_tck; +#endif +} + +void +RTMP_UserInterrupt() +{ + RTMP_ctrlC = TRUE; +} + +void +RTMPPacket_Reset(RTMPPacket *p) +{ + p->m_headerType = 0; + p->m_packetType = 0; + p->m_nChannel = 0; + p->m_nTimeStamp = 0; + p->m_nInfoField2 = 0; + p->m_hasAbsTimestamp = FALSE; + p->m_nBodySize = 0; + p->m_nBytesRead = 0; +} + +int +RTMPPacket_Alloc(RTMPPacket *p, uint32_t nSize) +{ + char *ptr; + if (nSize > SIZE_MAX - RTMP_MAX_HEADER_SIZE) + return FALSE; + ptr = calloc(1, nSize + RTMP_MAX_HEADER_SIZE); + if (!ptr) + return FALSE; + p->m_body = ptr + RTMP_MAX_HEADER_SIZE; + p->m_nBytesRead = 0; + return TRUE; +} + +void +RTMPPacket_Free(RTMPPacket *p) +{ + if (p->m_body) + { + free(p->m_body - RTMP_MAX_HEADER_SIZE); + p->m_body = NULL; + } +} + +void +RTMPPacket_Dump(RTMPPacket *p) +{ + RTMP_Log(RTMP_LOGDEBUG, + "RTMP PACKET: packet type: 0x%02x. channel: 0x%02x. info 1: %d info 2: %d. Body size: %u. body: 0x%02x", + p->m_packetType, p->m_nChannel, p->m_nTimeStamp, p->m_nInfoField2, + p->m_nBodySize, p->m_body ? (unsigned char)p->m_body[0] : 0); +} + +int +RTMP_LibVersion() +{ + return RTMP_LIB_VERSION; +} + +void +RTMP_TLS_Init() +{ +#ifdef CRYPTO +#ifdef USE_POLARSSL + /* Do this regardless of NO_SSL, we use havege for rtmpe too */ + RTMP_TLS_ctx = calloc(1,sizeof(struct tls_ctx)); + havege_init(&RTMP_TLS_ctx->hs); +#elif defined(USE_GNUTLS) && !defined(NO_SSL) + /* Technically we need to initialize libgcrypt ourselves if + * we're not going to call gnutls_global_init(). Ignoring this + * for now. + */ + gnutls_global_init(); + RTMP_TLS_ctx = malloc(sizeof(struct tls_ctx)); + gnutls_certificate_allocate_credentials(&RTMP_TLS_ctx->cred); + gnutls_priority_init(&RTMP_TLS_ctx->prios, "NORMAL", NULL); + gnutls_certificate_set_x509_trust_file(RTMP_TLS_ctx->cred, + "ca.pem", GNUTLS_X509_FMT_PEM); +#elif !defined(NO_SSL) /* USE_OPENSSL */ + /* libcrypto doesn't need anything special */ + SSL_load_error_strings(); + SSL_library_init(); + OpenSSL_add_all_digests(); + RTMP_TLS_ctx = SSL_CTX_new(SSLv23_method()); + SSL_CTX_set_options(RTMP_TLS_ctx, SSL_OP_ALL); + SSL_CTX_set_default_verify_paths(RTMP_TLS_ctx); +#endif +#endif +} + +void * +RTMP_TLS_AllocServerContext(const char* cert, const char* key) +{ + void *ctx = NULL; +#ifdef CRYPTO + if (!RTMP_TLS_ctx) + RTMP_TLS_Init(); +#ifdef USE_POLARSSL + tls_server_ctx *tc = ctx = calloc(1, sizeof(struct tls_server_ctx)); + tc->dhm_P = my_dhm_P; + tc->dhm_G = my_dhm_G; + tc->hs = &RTMP_TLS_ctx->hs; + if (x509parse_crtfile(&tc->cert, cert)) { + free(tc); + return NULL; + } + if (x509parse_keyfile(&tc->key, key, NULL)) { + x509_free(&tc->cert); + free(tc); + return NULL; + } +#elif defined(USE_GNUTLS) && !defined(NO_SSL) + gnutls_certificate_allocate_credentials((gnutls_certificate_credentials*) &ctx); + if (gnutls_certificate_set_x509_key_file(ctx, cert, key, GNUTLS_X509_FMT_PEM) != 0) { + gnutls_certificate_free_credentials(ctx); + return NULL; + } +#elif !defined(NO_SSL) /* USE_OPENSSL */ + ctx = SSL_CTX_new(SSLv23_server_method()); + if (!SSL_CTX_use_certificate_chain_file(ctx, cert)) { + SSL_CTX_free(ctx); + return NULL; + } + if (!SSL_CTX_use_PrivateKey_file(ctx, key, SSL_FILETYPE_PEM)) { + SSL_CTX_free(ctx); + return NULL; + } +#endif +#endif + return ctx; +} + +void +RTMP_TLS_FreeServerContext(void *ctx) +{ +#ifdef CRYPTO +#ifdef USE_POLARSSL + x509_free(&((tls_server_ctx*)ctx)->cert); + rsa_free(&((tls_server_ctx*)ctx)->key); + free(ctx); +#elif defined(USE_GNUTLS) && !defined(NO_SSL) + gnutls_certificate_free_credentials(ctx); +#elif !defined(NO_SSL) /* USE_OPENSSL */ + SSL_CTX_free(ctx); +#endif +#endif +} + +RTMP * +RTMP_Alloc() +{ + return calloc(1, sizeof(RTMP)); +} + +void +RTMP_Free(RTMP *r) +{ + free(r); +} + +// @remark debug info by http://github.com/ossrs/srs +int _srs_state = 0; + +void +RTMP_Init(RTMP *r) +{ +#ifdef CRYPTO + if (!RTMP_TLS_ctx) + RTMP_TLS_Init(); +#endif + + memset(r, 0, sizeof(RTMP)); + r->m_sb.sb_socket = -1; + r->m_inChunkSize = RTMP_DEFAULT_CHUNKSIZE; + r->m_outChunkSize = RTMP_DEFAULT_CHUNKSIZE; + r->m_nBufferMS = 30000; + r->m_nClientBW = 2500000; + r->m_nClientBW2 = 2; + r->m_nServerBW = 2500000; + r->m_fAudioCodecs = 3191.0; + r->m_fVideoCodecs = 252.0; + r->Link.timeout = 30; + r->Link.swfAge = 30; + + // @remark debug info by http://github.com/ossrs/srs + _srs_state = 1; +} + +void +RTMP_EnableWrite(RTMP *r) +{ + r->Link.protocol |= RTMP_FEATURE_WRITE; +} + +double +RTMP_GetDuration(RTMP *r) +{ + return r->m_fDuration; +} + +int +RTMP_IsConnected(RTMP *r) +{ + return r->m_sb.sb_socket != -1; +} + +int +RTMP_Socket(RTMP *r) +{ + return r->m_sb.sb_socket; +} + +int +RTMP_IsTimedout(RTMP *r) +{ + return r->m_sb.sb_timedout; +} + +void +RTMP_SetBufferMS(RTMP *r, int size) +{ + r->m_nBufferMS = size; +} + +void +RTMP_UpdateBufferMS(RTMP *r) +{ + RTMP_SendCtrl(r, 3, r->m_stream_id, r->m_nBufferMS); +} + +#undef OSS +#ifdef _WIN32 +#define OSS "WIN" +#elif defined(__sun__) +#define OSS "SOL" +#elif defined(__APPLE__) +#define OSS "MAC" +#elif defined(__linux__) +#define OSS "LNX" +#else +#define OSS "GNU" +#endif +#define DEF_VERSTR OSS " 10,0,32,18" +static const char DEFAULT_FLASH_VER[] = DEF_VERSTR; +const AVal RTMP_DefaultFlashVer = + { (char *)DEFAULT_FLASH_VER, sizeof(DEFAULT_FLASH_VER) - 1 }; + +static void +SocksSetup(RTMP *r, AVal *sockshost) +{ + if (sockshost->av_len) + { + const char *socksport = strchr(sockshost->av_val, ':'); + char *hostname = strdup(sockshost->av_val); + + if (socksport) + hostname[socksport - sockshost->av_val] = '\0'; + r->Link.sockshost.av_val = hostname; + r->Link.sockshost.av_len = strlen(hostname); + + r->Link.socksport = socksport ? atoi(socksport + 1) : 1080; + RTMP_Log(RTMP_LOGDEBUG, "Connecting via SOCKS proxy: %s:%d", r->Link.sockshost.av_val, + r->Link.socksport); + } + else + { + r->Link.sockshost.av_val = NULL; + r->Link.sockshost.av_len = 0; + r->Link.socksport = 0; + } +} + +void +RTMP_SetupStream(RTMP *r, + int protocol, + AVal *host, + unsigned int port, + AVal *sockshost, + AVal *playpath, + AVal *tcUrl, + AVal *swfUrl, + AVal *pageUrl, + AVal *app, + AVal *auth, + AVal *swfSHA256Hash, + uint32_t swfSize, + AVal *flashVer, + AVal *subscribepath, + AVal *usherToken, + int dStart, + int dStop, int bLiveStream, long int timeout) +{ + RTMP_Log(RTMP_LOGDEBUG, "Protocol : %s", RTMPProtocolStrings[protocol&7]); + RTMP_Log(RTMP_LOGDEBUG, "Hostname : %.*s", host->av_len, host->av_val); + RTMP_Log(RTMP_LOGDEBUG, "Port : %d", port); + RTMP_Log(RTMP_LOGDEBUG, "Playpath : %s", playpath->av_val); + + if (tcUrl && tcUrl->av_val) + RTMP_Log(RTMP_LOGDEBUG, "tcUrl : %s", tcUrl->av_val); + if (swfUrl && swfUrl->av_val) + RTMP_Log(RTMP_LOGDEBUG, "swfUrl : %s", swfUrl->av_val); + if (pageUrl && pageUrl->av_val) + RTMP_Log(RTMP_LOGDEBUG, "pageUrl : %s", pageUrl->av_val); + if (app && app->av_val) + RTMP_Log(RTMP_LOGDEBUG, "app : %.*s", app->av_len, app->av_val); + if (auth && auth->av_val) + RTMP_Log(RTMP_LOGDEBUG, "auth : %s", auth->av_val); + if (subscribepath && subscribepath->av_val) + RTMP_Log(RTMP_LOGDEBUG, "subscribepath : %s", subscribepath->av_val); + if (usherToken && usherToken->av_val) + RTMP_Log(RTMP_LOGDEBUG, "NetStream.Authenticate.UsherToken : %s", usherToken->av_val); + if (flashVer && flashVer->av_val) + RTMP_Log(RTMP_LOGDEBUG, "flashVer : %s", flashVer->av_val); + if (dStart > 0) + RTMP_Log(RTMP_LOGDEBUG, "StartTime : %d msec", dStart); + if (dStop > 0) + RTMP_Log(RTMP_LOGDEBUG, "StopTime : %d msec", dStop); + + RTMP_Log(RTMP_LOGDEBUG, "live : %s", bLiveStream ? "yes" : "no"); + RTMP_Log(RTMP_LOGDEBUG, "timeout : %ld sec", timeout); + +#ifdef CRYPTO + if (swfSHA256Hash != NULL && swfSize > 0) + { + memcpy(r->Link.SWFHash, swfSHA256Hash->av_val, sizeof(r->Link.SWFHash)); + r->Link.SWFSize = swfSize; + RTMP_Log(RTMP_LOGDEBUG, "SWFSHA256:"); + RTMP_LogHex(RTMP_LOGDEBUG, r->Link.SWFHash, sizeof(r->Link.SWFHash)); + RTMP_Log(RTMP_LOGDEBUG, "SWFSize : %u", r->Link.SWFSize); + } + else + { + r->Link.SWFSize = 0; + } +#endif + + SocksSetup(r, sockshost); + + if (tcUrl && tcUrl->av_len) + r->Link.tcUrl = *tcUrl; + if (swfUrl && swfUrl->av_len) + r->Link.swfUrl = *swfUrl; + if (pageUrl && pageUrl->av_len) + r->Link.pageUrl = *pageUrl; + if (app && app->av_len) + r->Link.app = *app; + if (auth && auth->av_len) + { + r->Link.auth = *auth; + r->Link.lFlags |= RTMP_LF_AUTH; + } + if (flashVer && flashVer->av_len) + r->Link.flashVer = *flashVer; + else + r->Link.flashVer = RTMP_DefaultFlashVer; + if (subscribepath && subscribepath->av_len) + r->Link.subscribepath = *subscribepath; + if (usherToken && usherToken->av_len) + r->Link.usherToken = *usherToken; + r->Link.seekTime = dStart; + r->Link.stopTime = dStop; + if (bLiveStream) + r->Link.lFlags |= RTMP_LF_LIVE; + r->Link.timeout = timeout; + + r->Link.protocol = protocol; + r->Link.hostname = *host; + r->Link.port = port; + r->Link.playpath = *playpath; + + if (r->Link.port == 0) + { + if (protocol & RTMP_FEATURE_SSL) + r->Link.port = 443; + else if (protocol & RTMP_FEATURE_HTTP) + r->Link.port = 80; + else + r->Link.port = 1935; + } +} + +enum { OPT_STR=0, OPT_INT, OPT_BOOL, OPT_CONN }; +static const char *optinfo[] = { + "string", "integer", "boolean", "AMF" }; + +#define OFF(x) offsetof(struct RTMP,x) + +static struct urlopt { + AVal name; + off_t off; + int otype; + int omisc; + char *use; +} options[] = { + { AVC("socks"), OFF(Link.sockshost), OPT_STR, 0, + "Use the specified SOCKS proxy" }, + { AVC("app"), OFF(Link.app), OPT_STR, 0, + "Name of target app on server" }, + { AVC("tcUrl"), OFF(Link.tcUrl), OPT_STR, 0, + "URL to played stream" }, + { AVC("pageUrl"), OFF(Link.pageUrl), OPT_STR, 0, + "URL of played media's web page" }, + { AVC("swfUrl"), OFF(Link.swfUrl), OPT_STR, 0, + "URL to player SWF file" }, + { AVC("flashver"), OFF(Link.flashVer), OPT_STR, 0, + "Flash version string (default " DEF_VERSTR ")" }, + { AVC("conn"), OFF(Link.extras), OPT_CONN, 0, + "Append arbitrary AMF data to Connect message" }, + { AVC("playpath"), OFF(Link.playpath), OPT_STR, 0, + "Path to target media on server" }, + { AVC("playlist"), OFF(Link.lFlags), OPT_BOOL, RTMP_LF_PLST, + "Set playlist before play command" }, + { AVC("live"), OFF(Link.lFlags), OPT_BOOL, RTMP_LF_LIVE, + "Stream is live, no seeking possible" }, + { AVC("subscribe"), OFF(Link.subscribepath), OPT_STR, 0, + "Stream to subscribe to" }, + { AVC("jtv"), OFF(Link.usherToken), OPT_STR, 0, + "Justin.tv authentication token" }, + { AVC("token"), OFF(Link.token), OPT_STR, 0, + "Key for SecureToken response" }, + { AVC("swfVfy"), OFF(Link.lFlags), OPT_BOOL, RTMP_LF_SWFV, + "Perform SWF Verification" }, + { AVC("swfAge"), OFF(Link.swfAge), OPT_INT, 0, + "Number of days to use cached SWF hash" }, + { AVC("start"), OFF(Link.seekTime), OPT_INT, 0, + "Stream start position in milliseconds" }, + { AVC("stop"), OFF(Link.stopTime), OPT_INT, 0, + "Stream stop position in milliseconds" }, + { AVC("buffer"), OFF(m_nBufferMS), OPT_INT, 0, + "Buffer time in milliseconds" }, + { AVC("timeout"), OFF(Link.timeout), OPT_INT, 0, + "Session timeout in seconds" }, + { AVC("pubUser"), OFF(Link.pubUser), OPT_STR, 0, + "Publisher username" }, + { AVC("pubPasswd"), OFF(Link.pubPasswd), OPT_STR, 0, + "Publisher password" }, + { {NULL,0}, 0, 0} +}; + +static const AVal truth[] = { + AVC("1"), + AVC("on"), + AVC("yes"), + AVC("true"), + {0,0} +}; + +static void RTMP_OptUsage() +{ + int i; + + RTMP_Log(RTMP_LOGERROR, "Valid RTMP options are:\n"); + for (i=0; options[i].name.av_len; i++) { + RTMP_Log(RTMP_LOGERROR, "%10s %-7s %s\n", options[i].name.av_val, + optinfo[options[i].otype], options[i].use); + } +} + +static int +parseAMF(AMFObject *obj, AVal *av, int *depth) +{ + AMFObjectProperty prop = {{0,0}}; + int i; + char *p, *arg = av->av_val; + + if (arg[1] == ':') + { + p = (char *)arg+2; + switch(arg[0]) + { + case 'B': + prop.p_type = AMF_BOOLEAN; + prop.p_vu.p_number = atoi(p); + break; + case 'S': + prop.p_type = AMF_STRING; + prop.p_vu.p_aval.av_val = p; + prop.p_vu.p_aval.av_len = av->av_len - (p-arg); + break; + case 'N': + prop.p_type = AMF_NUMBER; + prop.p_vu.p_number = strtod(p, NULL); + break; + case 'Z': + prop.p_type = AMF_NULL; + break; + case 'O': + i = atoi(p); + if (i) + { + prop.p_type = AMF_OBJECT; + } + else + { + (*depth)--; + return 0; + } + break; + default: + return -1; + } + } + else if (arg[2] == ':' && arg[0] == 'N') + { + p = strchr(arg+3, ':'); + if (!p || !*depth) + return -1; + prop.p_name.av_val = (char *)arg+3; + prop.p_name.av_len = p - (arg+3); + + p++; + switch(arg[1]) + { + case 'B': + prop.p_type = AMF_BOOLEAN; + prop.p_vu.p_number = atoi(p); + break; + case 'S': + prop.p_type = AMF_STRING; + prop.p_vu.p_aval.av_val = p; + prop.p_vu.p_aval.av_len = av->av_len - (p-arg); + break; + case 'N': + prop.p_type = AMF_NUMBER; + prop.p_vu.p_number = strtod(p, NULL); + break; + case 'O': + prop.p_type = AMF_OBJECT; + break; + default: + return -1; + } + } + else + return -1; + + if (*depth) + { + AMFObject *o2; + for (i=0; i<*depth; i++) + { + o2 = &obj->o_props[obj->o_num-1].p_vu.p_object; + obj = o2; + } + } + AMF_AddProp(obj, &prop); + if (prop.p_type == AMF_OBJECT) + (*depth)++; + return 0; +} + +int RTMP_SetOpt(RTMP *r, const AVal *opt, AVal *arg) +{ + int i; + void *v; + + for (i=0; options[i].name.av_len; i++) { + if (opt->av_len != options[i].name.av_len) continue; + if (strcasecmp(opt->av_val, options[i].name.av_val)) continue; + v = (char *)r + options[i].off; + switch(options[i].otype) { + case OPT_STR: { + AVal *aptr = v; + *aptr = *arg; } + break; + case OPT_INT: { + long l = strtol(arg->av_val, NULL, 0); + *(int *)v = l; } + break; + case OPT_BOOL: { + int j, fl; + fl = *(int *)v; + for (j=0; truth[j].av_len; j++) { + if (arg->av_len != truth[j].av_len) continue; + if (strcasecmp(arg->av_val, truth[j].av_val)) continue; + fl |= options[i].omisc; break; } + *(int *)v = fl; + } + break; + case OPT_CONN: + if (parseAMF(&r->Link.extras, arg, &r->Link.edepth)) + return FALSE; + break; + } + break; + } + if (!options[i].name.av_len) { + RTMP_Log(RTMP_LOGERROR, "Unknown option %s", opt->av_val); + RTMP_OptUsage(); + return FALSE; + } + + return TRUE; +} + +int RTMP_SetupURL(RTMP *r, char *url) +{ + AVal opt, arg; + char *p1, *p2, *ptr = strchr(url, ' '); + int ret, len; + unsigned int port = 0; + + if (ptr) + *ptr = '\0'; + + len = strlen(url); + ret = RTMP_ParseURL(url, &r->Link.protocol, &r->Link.hostname, + &port, &r->Link.playpath0, &r->Link.app); + if (!ret) + return ret; + r->Link.port = port; + r->Link.playpath = r->Link.playpath0; + + while (ptr) { + *ptr++ = '\0'; + p1 = ptr; + p2 = strchr(p1, '='); + if (!p2) + break; + opt.av_val = p1; + opt.av_len = p2 - p1; + *p2++ = '\0'; + arg.av_val = p2; + ptr = strchr(p2, ' '); + if (ptr) { + *ptr = '\0'; + arg.av_len = ptr - p2; + /* skip repeated spaces */ + while(ptr[1] == ' ') + *ptr++ = '\0'; + } else { + arg.av_len = strlen(p2); + } + + /* unescape */ + port = arg.av_len; + for (p1=p2; port >0;) { + if (*p1 == '\\') { + unsigned int c; + if (port < 3) + return FALSE; + sscanf(p1+1, "%02x", &c); + *p2++ = c; + port -= 3; + p1 += 3; + } else { + *p2++ = *p1++; + port--; + } + } + arg.av_len = p2 - arg.av_val; + + ret = RTMP_SetOpt(r, &opt, &arg); + if (!ret) + return ret; + } + + if (!r->Link.tcUrl.av_len) + { + r->Link.tcUrl.av_val = url; + if (r->Link.app.av_len) + { + if (r->Link.app.av_val < url + len) + { + /* if app is part of original url, just use it */ + r->Link.tcUrl.av_len = r->Link.app.av_len + (r->Link.app.av_val - url); + } + else + { + len = r->Link.hostname.av_len + r->Link.app.av_len + + sizeof("rtmpte://:65535/"); + r->Link.tcUrl.av_val = malloc(len); + r->Link.tcUrl.av_len = snprintf(r->Link.tcUrl.av_val, len, + "%s://%.*s:%d/%.*s", + RTMPProtocolStringsLower[r->Link.protocol], + r->Link.hostname.av_len, r->Link.hostname.av_val, + r->Link.port, + r->Link.app.av_len, r->Link.app.av_val); + r->Link.lFlags |= RTMP_LF_FTCU; + } + } + else + { + r->Link.tcUrl.av_len = strlen(url); + } + } + +#ifdef CRYPTO + if ((r->Link.lFlags & RTMP_LF_SWFV) && r->Link.swfUrl.av_len) + RTMP_HashSWF(r->Link.swfUrl.av_val, &r->Link.SWFSize, + (unsigned char *)r->Link.SWFHash, r->Link.swfAge); +#endif + + SocksSetup(r, &r->Link.sockshost); + + if (r->Link.port == 0) + { + if (r->Link.protocol & RTMP_FEATURE_SSL) + r->Link.port = 443; + else if (r->Link.protocol & RTMP_FEATURE_HTTP) + r->Link.port = 80; + else + r->Link.port = 1935; + } + return TRUE; +} + +static int +add_addr_info(struct sockaddr_in *service, AVal *host, int port) +{ + char *hostname; + int ret = TRUE; + if (host->av_val[host->av_len]) + { + hostname = malloc(host->av_len+1); + memcpy(hostname, host->av_val, host->av_len); + hostname[host->av_len] = '\0'; + } + else + { + hostname = host->av_val; + } + + service->sin_addr.s_addr = inet_addr(hostname); + if (service->sin_addr.s_addr == INADDR_NONE) + { + struct hostent *host = gethostbyname(hostname); + if (host == NULL || host->h_addr == NULL) + { + RTMP_Log(RTMP_LOGERROR, "Problem accessing the DNS. (addr: %s)", hostname); + ret = FALSE; + goto finish; + } + service->sin_addr = *(struct in_addr *)host->h_addr; + } + + service->sin_port = htons(port); +finish: + if (hostname != host->av_val) + free(hostname); + return ret; +} + +int +RTMP_Connect0(RTMP *r, struct sockaddr * service) +{ + int on = 1; + r->m_sb.sb_timedout = FALSE; + r->m_pausing = 0; + r->m_fDuration = 0.0; + + r->m_sb.sb_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (r->m_sb.sb_socket != -1) + { + if (connect(r->m_sb.sb_socket, service, sizeof(struct sockaddr)) < 0) + { + int err = GetSockError(); + RTMP_Log(RTMP_LOGERROR, "%s, failed to connect socket. %d (%s)", + __FUNCTION__, err, strerror(err)); + RTMP_Close(r); + return FALSE; + } + + if (r->Link.socksport) + { + RTMP_Log(RTMP_LOGDEBUG, "%s ... SOCKS negotiation", __FUNCTION__); + if (!SocksNegotiate(r)) + { + RTMP_Log(RTMP_LOGERROR, "%s, SOCKS negotiation failed.", __FUNCTION__); + RTMP_Close(r); + return FALSE; + } + } + } + else + { + RTMP_Log(RTMP_LOGERROR, "%s, failed to create socket. Error: %d", __FUNCTION__, + GetSockError()); + return FALSE; + } + + /* set timeout */ + { + SET_RCVTIMEO(tv, r->Link.timeout); + if (setsockopt + (r->m_sb.sb_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv))) + { + RTMP_Log(RTMP_LOGERROR, "%s, Setting socket timeout to %ds failed!", + __FUNCTION__, r->Link.timeout); + } + } + + setsockopt(r->m_sb.sb_socket, IPPROTO_TCP, TCP_NODELAY, (char *) &on, sizeof(on)); + + // @remark debug info by http://github.com/ossrs/srs + _srs_state = 2; + + return TRUE; +} + +int +RTMP_TLS_Accept(RTMP *r, void *ctx) +{ +#if defined(CRYPTO) && !defined(NO_SSL) + TLS_server(ctx, r->m_sb.sb_ssl); + TLS_setfd(r->m_sb.sb_ssl, r->m_sb.sb_socket); + if (TLS_accept(r->m_sb.sb_ssl) < 0) + { + RTMP_Log(RTMP_LOGERROR, "%s, TLS_Connect failed", __FUNCTION__); + return FALSE; + } + return TRUE; +#else + return FALSE; +#endif +} + +int +RTMP_Connect1(RTMP *r, RTMPPacket *cp) +{ + if (r->Link.protocol & RTMP_FEATURE_SSL) + { +#if defined(CRYPTO) && !defined(NO_SSL) + TLS_client(RTMP_TLS_ctx, r->m_sb.sb_ssl); + TLS_setfd(r->m_sb.sb_ssl, r->m_sb.sb_socket); + if (TLS_connect(r->m_sb.sb_ssl) < 0) + { + RTMP_Log(RTMP_LOGERROR, "%s, TLS_Connect failed", __FUNCTION__); + RTMP_Close(r); + return FALSE; + } +#else + RTMP_Log(RTMP_LOGERROR, "%s, no SSL/TLS support", __FUNCTION__); + RTMP_Close(r); + return FALSE; + +#endif + } + if (r->Link.protocol & RTMP_FEATURE_HTTP) + { + r->m_msgCounter = 1; + r->m_clientID.av_val = NULL; + r->m_clientID.av_len = 0; + HTTP_Post(r, RTMPT_OPEN, "", 1); + if (HTTP_read(r, 1) != 0) + { + r->m_msgCounter = 0; + RTMP_Log(RTMP_LOGDEBUG, "%s, Could not connect for handshake", __FUNCTION__); + RTMP_Close(r); + return 0; + } + r->m_msgCounter = 0; + } + RTMP_Log(RTMP_LOGDEBUG, "%s, ... connected, handshaking", __FUNCTION__); + if (!HandShake(r, TRUE)) + { + RTMP_Log(RTMP_LOGERROR, "%s, handshake failed.", __FUNCTION__); + RTMP_Close(r); + return FALSE; + } + RTMP_Log(RTMP_LOGDEBUG, "%s, handshaked", __FUNCTION__); + + if (!SendConnectPacket(r, cp)) + { + RTMP_Log(RTMP_LOGERROR, "%s, RTMP connect failed.", __FUNCTION__); + RTMP_Close(r); + return FALSE; + } + return TRUE; +} + +int +RTMP_Connect(RTMP *r, RTMPPacket *cp) +{ + struct sockaddr_in service; + if (!r->Link.hostname.av_len) + return FALSE; + + memset(&service, 0, sizeof(struct sockaddr_in)); + service.sin_family = AF_INET; + + if (r->Link.socksport) + { + /* Connect via SOCKS */ + if (!add_addr_info(&service, &r->Link.sockshost, r->Link.socksport)) + return FALSE; + } + else + { + /* Connect directly */ + if (!add_addr_info(&service, &r->Link.hostname, r->Link.port)) + return FALSE; + } + + if (!RTMP_Connect0(r, (struct sockaddr *)&service)) + return FALSE; + + r->m_bSendCounter = TRUE; + + return RTMP_Connect1(r, cp); +} + +static int +SocksNegotiate(RTMP *r) +{ + unsigned long addr; + struct sockaddr_in service; + memset(&service, 0, sizeof(struct sockaddr_in)); + + add_addr_info(&service, &r->Link.hostname, r->Link.port); + addr = htonl(service.sin_addr.s_addr); + + { + char packet[] = { + 4, 1, /* SOCKS 4, connect */ + (r->Link.port >> 8) & 0xFF, + (r->Link.port) & 0xFF, + (char)(addr >> 24) & 0xFF, (char)(addr >> 16) & 0xFF, + (char)(addr >> 8) & 0xFF, (char)addr & 0xFF, + 0 + }; /* NULL terminate */ + + WriteN(r, packet, sizeof packet); + + if (ReadN(r, packet, 8) != 8) + return FALSE; + + if (packet[0] == 0 && packet[1] == 90) + { + return TRUE; + } + else + { + RTMP_Log(RTMP_LOGERROR, "%s, SOCKS returned error code %d", __FUNCTION__, packet[1]); + return FALSE; + } + } +} + +int +RTMP_ConnectStream(RTMP *r, int seekTime) +{ + RTMPPacket packet = { 0 }; + + /* seekTime was already set by SetupStream / SetupURL. + * This is only needed by ReconnectStream. + */ + if (seekTime > 0) + r->Link.seekTime = seekTime; + + r->m_mediaChannel = 0; + + while (!r->m_bPlaying && RTMP_IsConnected(r) && RTMP_ReadPacket(r, &packet)) + { + if (RTMPPacket_IsReady(&packet)) + { + if (!packet.m_nBodySize) + continue; + if ((packet.m_packetType == RTMP_PACKET_TYPE_AUDIO) || + (packet.m_packetType == RTMP_PACKET_TYPE_VIDEO) || + (packet.m_packetType == RTMP_PACKET_TYPE_INFO)) + { + RTMP_Log(RTMP_LOGWARNING, "Received FLV packet before play()! Ignoring."); + RTMPPacket_Free(&packet); + continue; + } + + RTMP_ClientPacket(r, &packet); + RTMPPacket_Free(&packet); + } + } + + return r->m_bPlaying; +} + +int +RTMP_ReconnectStream(RTMP *r, int seekTime) +{ + RTMP_DeleteStream(r); + + RTMP_SendCreateStream(r); + + return RTMP_ConnectStream(r, seekTime); +} + +int +RTMP_ToggleStream(RTMP *r) +{ + int res; + + if (!r->m_pausing) + { + if (RTMP_IsTimedout(r) && r->m_read.status == RTMP_READ_EOF) + r->m_read.status = 0; + + res = RTMP_SendPause(r, TRUE, r->m_pauseStamp); + if (!res) + return res; + + r->m_pausing = 1; + msleep(1000); + } + res = RTMP_SendPause(r, FALSE, r->m_pauseStamp); + r->m_pausing = 3; + return res; +} + +void +RTMP_DeleteStream(RTMP *r) +{ + if (r->m_stream_id < 0) + return; + + r->m_bPlaying = FALSE; + + SendDeleteStream(r, r->m_stream_id); + r->m_stream_id = -1; +} + +int +RTMP_GetNextMediaPacket(RTMP *r, RTMPPacket *packet) +{ + int bHasMediaPacket = 0; + + while (!bHasMediaPacket && RTMP_IsConnected(r) + && RTMP_ReadPacket(r, packet)) + { + if (!RTMPPacket_IsReady(packet) || !packet->m_nBodySize) + { + continue; + } + + bHasMediaPacket = RTMP_ClientPacket(r, packet); + + if (!bHasMediaPacket) + { + RTMPPacket_Free(packet); + } + else if (r->m_pausing == 3) + { + if (packet->m_nTimeStamp <= r->m_mediaStamp) + { + bHasMediaPacket = 0; +#ifdef _DEBUG + RTMP_Log(RTMP_LOGDEBUG, + "Skipped type: %02X, size: %d, TS: %d ms, abs TS: %d, pause: %d ms", + packet->m_packetType, packet->m_nBodySize, + packet->m_nTimeStamp, packet->m_hasAbsTimestamp, + r->m_mediaStamp); +#endif + RTMPPacket_Free(packet); + continue; + } + r->m_pausing = 0; + } + } + + if (bHasMediaPacket) + r->m_bPlaying = TRUE; + else if (r->m_sb.sb_timedout && !r->m_pausing) + r->m_pauseStamp = r->m_mediaChannel < r->m_channelsAllocatedIn ? + r->m_channelTimestamp[r->m_mediaChannel] : 0; + + return bHasMediaPacket; +} + +// @remark debug info by http://github.com/ossrs/srs +char* _srs_ip = NULL; +int _srs_pid = 0; +int _srs_cid = 0; +// Internal variables. +static const AVal _const_srs_server_ip = AVC("srs_server_ip"); +static const AVal _const_srs_pid = AVC("srs_pid"); +static const AVal _const_srs_cid = AVC("srs_id"); + +int +RTMP_ClientPacket(RTMP *r, RTMPPacket *packet) +{ + int bHasMediaPacket = 0; + switch (packet->m_packetType) + { + case RTMP_PACKET_TYPE_CHUNK_SIZE: + /* chunk size */ + HandleChangeChunkSize(r, packet); + break; + + case RTMP_PACKET_TYPE_BYTES_READ_REPORT: + /* bytes read report */ + RTMP_Log(RTMP_LOGDEBUG, "%s, received: bytes read report", __FUNCTION__); + break; + + case RTMP_PACKET_TYPE_CONTROL: + /* ctrl */ + HandleCtrl(r, packet); + break; + + case RTMP_PACKET_TYPE_SERVER_BW: + /* server bw */ + HandleServerBW(r, packet); + break; + + case RTMP_PACKET_TYPE_CLIENT_BW: + /* client bw */ + HandleClientBW(r, packet); + break; + + case RTMP_PACKET_TYPE_AUDIO: + /* audio data */ + /*RTMP_Log(RTMP_LOGDEBUG, "%s, received: audio %lu bytes", __FUNCTION__, packet.m_nBodySize); */ + HandleAudio(r, packet); + bHasMediaPacket = 1; + if (!r->m_mediaChannel) + r->m_mediaChannel = packet->m_nChannel; + if (!r->m_pausing) + r->m_mediaStamp = packet->m_nTimeStamp; + break; + + case RTMP_PACKET_TYPE_VIDEO: + /* video data */ + /*RTMP_Log(RTMP_LOGDEBUG, "%s, received: video %lu bytes", __FUNCTION__, packet.m_nBodySize); */ + HandleVideo(r, packet); + bHasMediaPacket = 1; + if (!r->m_mediaChannel) + r->m_mediaChannel = packet->m_nChannel; + if (!r->m_pausing) + r->m_mediaStamp = packet->m_nTimeStamp; + break; + + case RTMP_PACKET_TYPE_FLEX_STREAM_SEND: + /* flex stream send */ + RTMP_Log(RTMP_LOGDEBUG, + "%s, flex stream send, size %u bytes, not supported, ignoring", + __FUNCTION__, packet->m_nBodySize); + break; + + case RTMP_PACKET_TYPE_FLEX_SHARED_OBJECT: + /* flex shared object */ + RTMP_Log(RTMP_LOGDEBUG, + "%s, flex shared object, size %u bytes, not supported, ignoring", + __FUNCTION__, packet->m_nBodySize); + break; + + case RTMP_PACKET_TYPE_FLEX_MESSAGE: + /* flex message */ + { + RTMP_Log(RTMP_LOGDEBUG, + "%s, flex message, size %u bytes, not fully supported", + __FUNCTION__, packet->m_nBodySize); + /*RTMP_LogHex(packet.m_body, packet.m_nBodySize); */ + + /* some DEBUG code */ +#if 0 + RTMP_LIB_AMFObject obj; + int nRes = obj.Decode(packet.m_body+1, packet.m_nBodySize-1); + if(nRes < 0) { + RTMP_Log(RTMP_LOGERROR, "%s, error decoding AMF3 packet", __FUNCTION__); + /*return; */ + } + + obj.Dump(); +#endif + + if (HandleInvoke(r, packet->m_body + 1, packet->m_nBodySize - 1) == 1) + bHasMediaPacket = 2; + break; + } + case RTMP_PACKET_TYPE_INFO: + /* metadata (notify) */ + RTMP_Log(RTMP_LOGDEBUG, "%s, received: notify %u bytes", __FUNCTION__, + packet->m_nBodySize); + if (HandleMetadata(r, packet->m_body, packet->m_nBodySize)) + bHasMediaPacket = 1; + break; + + case RTMP_PACKET_TYPE_SHARED_OBJECT: + RTMP_Log(RTMP_LOGDEBUG, "%s, shared object, not supported, ignoring", + __FUNCTION__); + break; + + case RTMP_PACKET_TYPE_INVOKE: + /* invoke */ + RTMP_Log(RTMP_LOGDEBUG, "%s, received: invoke %u bytes", __FUNCTION__, + packet->m_nBodySize); + /*RTMP_LogHex(packet.m_body, packet.m_nBodySize); */ + + // @remark debug info by http://github.com/ossrs/srs + while (1) { + // String(_result) + char* p = packet->m_body; + int nb = packet->m_nBodySize; + // Marker. + if (nb < 1) { + RTMP_Log(RTMP_LOGERROR, "ignore string marker for nb=%d", nb); + break; + } + AMFDataType t = (AMFDataType)p[0]; + if (t != AMF_STRING) { + RTMP_Log(RTMP_LOGERROR, "ignore string marker for type=%d", t); + break; + } + nb--; p++; + // String content. + if (nb < 2) { + RTMP_Log(RTMP_LOGERROR, "ignore string data for nb=%d", nb); + break; + } + AVal _result; + AMF_DecodeString(p, &_result); + nb -= (int)_result.av_len + 2; p += (int)_result.av_len + 2; + + // Number(0.0) + // Marker + if (nb < 1) { + RTMP_Log(RTMP_LOGERROR, "ignore number marker for nb=%d", nb); + break; + } + t = (AMFDataType)p[0]; + if (t != AMF_NUMBER) { + RTMP_Log(RTMP_LOGERROR, "ignore number marker for type=%d", t); + break; + } + nb--; p++; + // Number content. + if (nb < 8) { + RTMP_Log(RTMP_LOGERROR, "ignore number data for nb=%d", nb); + break; + } + double tid = AMF_DecodeNumber(p); (void)tid; + nb -= 8; p += 8; + + // Object + // Marker + if (nb < 1) { + RTMP_Log(RTMP_LOGERROR, "ignore object marker for nb=%d", nb); + break; + } + t = (AMFDataType)p[0]; + if (t != AMF_OBJECT) { + RTMP_Log(RTMP_LOGERROR, "ignore object marker for type=%d", t); + break; + } + nb--; p++; + // Object info content + AMFObject obj; + if (nb < 3) { + RTMP_Log(RTMP_LOGERROR, "ignore object eof for nb=%d", nb); + break; + } + int nRes = -1; + if ((nRes = AMF_Decode(&obj, p, nb, TRUE)) < 0) { + RTMP_Log(RTMP_LOGERROR, "decode object failed, ret=%d", nRes); + break; + } + nb -= nRes; p += nRes; + + // Object + // Marker + if (nb < 1) { + RTMP_Log(RTMP_LOGERROR, "ignore object marker for nb=%d", nb); + break; + } + t = (AMFDataType)p[0]; + if (t != AMF_OBJECT) { + RTMP_Log(RTMP_LOGERROR, "ignore object marker for type=%d", t); + break; + } + nb--; p++; + // Object data content + if (nb < 3) { + RTMP_Log(RTMP_LOGERROR, "ignore object eof for nb=%d", nb); + break; + } + if ((nRes = AMF_Decode(&obj, p, nb, TRUE)) < 0) { + RTMP_Log(RTMP_LOGERROR, "decode object failed, ret=%d", nRes); + break; + } + nb -= nRes; p += nRes; + // Parse data object. + int i,j; + for (i = 0; i < obj.o_num; i++) { + AMFObjectProperty* prop = &obj.o_props[i]; + if (prop->p_type == AMF_OBJECT || prop->p_type == AMF_ECMA_ARRAY) { + obj = prop->p_vu.p_object; + for (j = 0; j < obj.o_num; j++) { + prop = &obj.o_props[j]; + if (AVMATCH(&prop->p_name, &_const_srs_server_ip)) { + if (_srs_ip) { + free(_srs_ip); + } + _srs_ip = (char*)malloc(prop->p_vu.p_aval.av_len + 1); + memcpy(_srs_ip, prop->p_vu.p_aval.av_val, prop->p_vu.p_aval.av_len); + _srs_ip[prop->p_vu.p_aval.av_len] = 0; + } else if (AVMATCH(&prop->p_name, &_const_srs_pid)) { + _srs_pid = (int)prop->p_vu.p_number; + } else if (AVMATCH(&prop->p_name, &_const_srs_cid)) { + _srs_cid = (int)prop->p_vu.p_number; + } + } + break; + } + } + + // Print info. + if (_srs_pid > 0) { + RTMP_Log(RTMP_LOGINFO, "SRS ip=%s, pid=%d, cid=%d", _srs_ip, _srs_pid, _srs_cid); + } + + break; + } + + if (HandleInvoke(r, packet->m_body, packet->m_nBodySize) == 1) + bHasMediaPacket = 2; + break; + + case RTMP_PACKET_TYPE_FLASH_VIDEO: + { + /* go through FLV packets and handle metadata packets */ + unsigned int pos = 0; + uint32_t nTimeStamp = packet->m_nTimeStamp; + + while (pos + 11 < packet->m_nBodySize) + { + uint32_t dataSize = AMF_DecodeInt24(packet->m_body + pos + 1); /* size without header (11) and prevTagSize (4) */ + + if (pos + 11 + dataSize + 4 > packet->m_nBodySize) + { + RTMP_Log(RTMP_LOGWARNING, "Stream corrupt?!"); + break; + } + if (packet->m_body[pos] == 0x12) + { + HandleMetadata(r, packet->m_body + pos + 11, dataSize); + } + else if (packet->m_body[pos] == 8 || packet->m_body[pos] == 9) + { + nTimeStamp = AMF_DecodeInt24(packet->m_body + pos + 4); + nTimeStamp |= (packet->m_body[pos + 7] << 24); + } + pos += (11 + dataSize + 4); + } + if (!r->m_pausing) + r->m_mediaStamp = nTimeStamp; + + /* FLV tag(s) */ + /*RTMP_Log(RTMP_LOGDEBUG, "%s, received: FLV tag(s) %lu bytes", __FUNCTION__, packet.m_nBodySize); */ + bHasMediaPacket = 1; + break; + } + default: + RTMP_Log(RTMP_LOGDEBUG, "%s, unknown packet type received: 0x%02x", __FUNCTION__, + packet->m_packetType); +#ifdef _DEBUG + RTMP_LogHex(RTMP_LOGDEBUG, packet->m_body, packet->m_nBodySize); +#endif + } + + return bHasMediaPacket; +} + +//#ifdef _DEBUG +//extern FILE *netstackdump; +//extern FILE *netstackdump_read; +//#endif + +// @remark debug info by http://github.com/ossrs/srs +unsigned long _srs_rbytes = 0; +unsigned long _srs_sbytes = 0; + +static int +ReadN(RTMP *r, char *buffer, int n) +{ + int nOriginalSize = n; + int avail; + char *ptr; + + r->m_sb.sb_timedout = FALSE; + +#ifdef _DEBUG + memset(buffer, 0, n); +#endif + + // @remark debug info by http://github.com/ossrs/srs + _srs_rbytes += n; + + ptr = buffer; + while (n > 0) + { + int nBytes = 0, nRead; + if (r->Link.protocol & RTMP_FEATURE_HTTP) + { + int refill = 0; + while (!r->m_resplen) + { + int ret; + if (r->m_sb.sb_size < 13 || refill) + { + if (!r->m_unackd) + HTTP_Post(r, RTMPT_IDLE, "", 1); + if (RTMPSockBuf_Fill(&r->m_sb) < 1) + { + if (!r->m_sb.sb_timedout) + RTMP_Close(r); + return 0; + } + } + if ((ret = HTTP_read(r, 0)) == -1) + { + RTMP_Log(RTMP_LOGDEBUG, "%s, No valid HTTP response found", __FUNCTION__); + RTMP_Close(r); + return 0; + } + else if (ret == -2) + { + refill = 1; + } + else + { + refill = 0; + } + } + if (r->m_resplen && !r->m_sb.sb_size) + RTMPSockBuf_Fill(&r->m_sb); + avail = r->m_sb.sb_size; + if (avail > r->m_resplen) + avail = r->m_resplen; + } + else + { + avail = r->m_sb.sb_size; + if (avail == 0) + { + if (RTMPSockBuf_Fill(&r->m_sb) < 1) + { + if (!r->m_sb.sb_timedout) + RTMP_Close(r); + return 0; + } + avail = r->m_sb.sb_size; + } + } + nRead = ((n < avail) ? n : avail); + if (nRead > 0) + { + memcpy(ptr, r->m_sb.sb_start, nRead); + r->m_sb.sb_start += nRead; + r->m_sb.sb_size -= nRead; + nBytes = nRead; + r->m_nBytesIn += nRead; + if (r->m_bSendCounter + && r->m_nBytesIn > ( r->m_nBytesInSent + r->m_nClientBW / 10)) + if (!SendBytesReceived(r)) + return FALSE; + } + /*RTMP_Log(RTMP_LOGDEBUG, "%s: %d bytes\n", __FUNCTION__, nBytes); */ +#ifdef _DEBUG +// fwrite(ptr, 1, nBytes, netstackdump_read); +#endif + + if (nBytes == 0) + { + RTMP_Log(RTMP_LOGDEBUG, "%s, RTMP socket closed by peer", __FUNCTION__); + /*goto again; */ + RTMP_Close(r); + break; + } + + if (r->Link.protocol & RTMP_FEATURE_HTTP) + r->m_resplen -= nBytes; + +#ifdef CRYPTO + if (r->Link.rc4keyIn) + { + RC4_encrypt(r->Link.rc4keyIn, nBytes, ptr); + } +#endif + + n -= nBytes; + ptr += nBytes; + } + + return nOriginalSize - n; +} + +static int +WriteN(RTMP *r, const char *buffer, int n) +{ + const char *ptr = buffer; +#ifdef CRYPTO + char *encrypted = 0; + char buf[RTMP_BUFFER_CACHE_SIZE]; + + // @remark debug info by http://github.com/ossrs/srs + _srs_sbytes += n; + + if (r->Link.rc4keyOut) + { + if (n > sizeof(buf)) + encrypted = (char *)malloc(n); + else + encrypted = (char *)buf; + ptr = encrypted; + RC4_encrypt2(r->Link.rc4keyOut, n, buffer, ptr); + } +#endif + + // @remark debug info by http://github.com/ossrs/srs + _srs_sbytes += n; + + while (n > 0) + { + int nBytes; + + if (r->Link.protocol & RTMP_FEATURE_HTTP) + nBytes = HTTP_Post(r, RTMPT_SEND, ptr, n); + else + nBytes = RTMPSockBuf_Send(&r->m_sb, ptr, n); + /*RTMP_Log(RTMP_LOGDEBUG, "%s: %d\n", __FUNCTION__, nBytes); */ + + if (nBytes < 0) + { + int sockerr = GetSockError(); + RTMP_Log(RTMP_LOGERROR, "%s, RTMP send error %d (%d bytes)", __FUNCTION__, + sockerr, n); + + if (sockerr == EINTR && !RTMP_ctrlC) + continue; + + RTMP_Close(r); + n = 1; + break; + } + + if (nBytes == 0) + break; + + n -= nBytes; + ptr += nBytes; + } + +#ifdef CRYPTO + if (encrypted && encrypted != buf) + free(encrypted); +#endif + + return n == 0; +} + +#define SAVC(x) static const AVal av_##x = AVC(#x) + +SAVC(app); +SAVC(connect); +SAVC(flashVer); +SAVC(swfUrl); +SAVC(pageUrl); +SAVC(tcUrl); +SAVC(fpad); +SAVC(capabilities); +SAVC(audioCodecs); +SAVC(videoCodecs); +SAVC(videoFunction); +SAVC(objectEncoding); +SAVC(secureToken); +SAVC(secureTokenResponse); +SAVC(type); +SAVC(nonprivate); + +static int +SendConnectPacket(RTMP *r, RTMPPacket *cp) +{ + RTMPPacket packet; + char pbuf[4096], *pend = pbuf + sizeof(pbuf); + char *enc; + + if (cp) + return RTMP_SendPacket(r, cp, TRUE); + + packet.m_nChannel = 0x03; /* control channel (invoke) */ + packet.m_headerType = RTMP_PACKET_SIZE_LARGE; + packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; + packet.m_nTimeStamp = 0; + packet.m_nInfoField2 = 0; + packet.m_hasAbsTimestamp = 0; + packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; + + enc = packet.m_body; + enc = AMF_EncodeString(enc, pend, &av_connect); + enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); + *enc++ = AMF_OBJECT; + + enc = AMF_EncodeNamedString(enc, pend, &av_app, &r->Link.app); + if (!enc) + return FALSE; + if (r->Link.protocol & RTMP_FEATURE_WRITE) + { + enc = AMF_EncodeNamedString(enc, pend, &av_type, &av_nonprivate); + if (!enc) + return FALSE; + } + if (r->Link.flashVer.av_len) + { + enc = AMF_EncodeNamedString(enc, pend, &av_flashVer, &r->Link.flashVer); + if (!enc) + return FALSE; + } + if (r->Link.swfUrl.av_len) + { + enc = AMF_EncodeNamedString(enc, pend, &av_swfUrl, &r->Link.swfUrl); + if (!enc) + return FALSE; + } + if (r->Link.tcUrl.av_len) + { + enc = AMF_EncodeNamedString(enc, pend, &av_tcUrl, &r->Link.tcUrl); + if (!enc) + return FALSE; + } + if (!(r->Link.protocol & RTMP_FEATURE_WRITE)) + { + enc = AMF_EncodeNamedBoolean(enc, pend, &av_fpad, FALSE); + if (!enc) + return FALSE; + enc = AMF_EncodeNamedNumber(enc, pend, &av_capabilities, 15.0); + if (!enc) + return FALSE; + enc = AMF_EncodeNamedNumber(enc, pend, &av_audioCodecs, r->m_fAudioCodecs); + if (!enc) + return FALSE; + enc = AMF_EncodeNamedNumber(enc, pend, &av_videoCodecs, r->m_fVideoCodecs); + if (!enc) + return FALSE; + enc = AMF_EncodeNamedNumber(enc, pend, &av_videoFunction, 1.0); + if (!enc) + return FALSE; + if (r->Link.pageUrl.av_len) + { + enc = AMF_EncodeNamedString(enc, pend, &av_pageUrl, &r->Link.pageUrl); + if (!enc) + return FALSE; + } + } + if (r->m_fEncoding != 0.0 || r->m_bSendEncoding) + { /* AMF0, AMF3 not fully supported yet */ + enc = AMF_EncodeNamedNumber(enc, pend, &av_objectEncoding, r->m_fEncoding); + if (!enc) + return FALSE; + } + if (enc + 3 >= pend) + return FALSE; + *enc++ = 0; + *enc++ = 0; /* end of object - 0x00 0x00 0x09 */ + *enc++ = AMF_OBJECT_END; + + /* add auth string */ + if (r->Link.auth.av_len) + { + enc = AMF_EncodeBoolean(enc, pend, r->Link.lFlags & RTMP_LF_AUTH); + if (!enc) + return FALSE; + enc = AMF_EncodeString(enc, pend, &r->Link.auth); + if (!enc) + return FALSE; + } + if (r->Link.extras.o_num) + { + int i; + for (i = 0; i < r->Link.extras.o_num; i++) + { + enc = AMFProp_Encode(&r->Link.extras.o_props[i], enc, pend); + if (!enc) + return FALSE; + } + } + packet.m_nBodySize = enc - packet.m_body; + + return RTMP_SendPacket(r, &packet, TRUE); +} + +#if 0 /* unused */ +SAVC(bgHasStream); + +static int +SendBGHasStream(RTMP *r, double dId, AVal *playpath) +{ + RTMPPacket packet; + char pbuf[1024], *pend = pbuf + sizeof(pbuf); + char *enc; + + packet.m_nChannel = 0x03; /* control channel (invoke) */ + packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; + packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; + packet.m_nTimeStamp = 0; + packet.m_nInfoField2 = 0; + packet.m_hasAbsTimestamp = 0; + packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; + + enc = packet.m_body; + enc = AMF_EncodeString(enc, pend, &av_bgHasStream); + enc = AMF_EncodeNumber(enc, pend, dId); + *enc++ = AMF_NULL; + + enc = AMF_EncodeString(enc, pend, playpath); + if (enc == NULL) + return FALSE; + + packet.m_nBodySize = enc - packet.m_body; + + return RTMP_SendPacket(r, &packet, TRUE); +} +#endif + +SAVC(createStream); + +int +RTMP_SendCreateStream(RTMP *r) +{ + RTMPPacket packet; + char pbuf[256], *pend = pbuf + sizeof(pbuf); + char *enc; + + packet.m_nChannel = 0x03; /* control channel (invoke) */ + packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; + packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; + packet.m_nTimeStamp = 0; + packet.m_nInfoField2 = 0; + packet.m_hasAbsTimestamp = 0; + packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; + + enc = packet.m_body; + enc = AMF_EncodeString(enc, pend, &av_createStream); + enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); + *enc++ = AMF_NULL; /* NULL */ + + packet.m_nBodySize = enc - packet.m_body; + + return RTMP_SendPacket(r, &packet, TRUE); +} + +SAVC(FCSubscribe); + +static int +SendFCSubscribe(RTMP *r, AVal *subscribepath) +{ + RTMPPacket packet; + char pbuf[512], *pend = pbuf + sizeof(pbuf); + char *enc; + packet.m_nChannel = 0x03; /* control channel (invoke) */ + packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; + packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; + packet.m_nTimeStamp = 0; + packet.m_nInfoField2 = 0; + packet.m_hasAbsTimestamp = 0; + packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; + + RTMP_Log(RTMP_LOGDEBUG, "FCSubscribe: %s", subscribepath->av_val); + enc = packet.m_body; + enc = AMF_EncodeString(enc, pend, &av_FCSubscribe); + enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); + *enc++ = AMF_NULL; + enc = AMF_EncodeString(enc, pend, subscribepath); + + if (!enc) + return FALSE; + + packet.m_nBodySize = enc - packet.m_body; + + return RTMP_SendPacket(r, &packet, TRUE); +} + +/* Justin.tv specific authentication */ +static const AVal av_NetStream_Authenticate_UsherToken = AVC("NetStream.Authenticate.UsherToken"); + +static int +SendUsherToken(RTMP *r, AVal *usherToken) +{ + RTMPPacket packet; + char pbuf[1024], *pend = pbuf + sizeof(pbuf); + char *enc; + packet.m_nChannel = 0x03; /* control channel (invoke) */ + packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; + packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; + packet.m_nTimeStamp = 0; + packet.m_nInfoField2 = 0; + packet.m_hasAbsTimestamp = 0; + packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; + + RTMP_Log(RTMP_LOGDEBUG, "UsherToken: %s", usherToken->av_val); + enc = packet.m_body; + enc = AMF_EncodeString(enc, pend, &av_NetStream_Authenticate_UsherToken); + enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); + *enc++ = AMF_NULL; + enc = AMF_EncodeString(enc, pend, usherToken); + + if (!enc) + return FALSE; + + packet.m_nBodySize = enc - packet.m_body; + + return RTMP_SendPacket(r, &packet, FALSE); +} +/******************************************/ + +SAVC(releaseStream); + +static int +SendReleaseStream(RTMP *r) +{ + RTMPPacket packet; + char pbuf[1024], *pend = pbuf + sizeof(pbuf); + char *enc; + + packet.m_nChannel = 0x03; /* control channel (invoke) */ + packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; + packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; + packet.m_nTimeStamp = 0; + packet.m_nInfoField2 = 0; + packet.m_hasAbsTimestamp = 0; + packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; + + enc = packet.m_body; + enc = AMF_EncodeString(enc, pend, &av_releaseStream); + enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); + *enc++ = AMF_NULL; + enc = AMF_EncodeString(enc, pend, &r->Link.playpath); + if (!enc) + return FALSE; + + packet.m_nBodySize = enc - packet.m_body; + + return RTMP_SendPacket(r, &packet, FALSE); +} + +SAVC(FCPublish); + +static int +SendFCPublish(RTMP *r) +{ + RTMPPacket packet; + char pbuf[1024], *pend = pbuf + sizeof(pbuf); + char *enc; + + packet.m_nChannel = 0x03; /* control channel (invoke) */ + packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; + packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; + packet.m_nTimeStamp = 0; + packet.m_nInfoField2 = 0; + packet.m_hasAbsTimestamp = 0; + packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; + + enc = packet.m_body; + enc = AMF_EncodeString(enc, pend, &av_FCPublish); + enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); + *enc++ = AMF_NULL; + enc = AMF_EncodeString(enc, pend, &r->Link.playpath); + if (!enc) + return FALSE; + + packet.m_nBodySize = enc - packet.m_body; + + return RTMP_SendPacket(r, &packet, FALSE); +} + +SAVC(FCUnpublish); + +static int +SendFCUnpublish(RTMP *r) +{ + RTMPPacket packet; + char pbuf[1024], *pend = pbuf + sizeof(pbuf); + char *enc; + + packet.m_nChannel = 0x03; /* control channel (invoke) */ + packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; + packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; + packet.m_nTimeStamp = 0; + packet.m_nInfoField2 = 0; + packet.m_hasAbsTimestamp = 0; + packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; + + enc = packet.m_body; + enc = AMF_EncodeString(enc, pend, &av_FCUnpublish); + enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); + *enc++ = AMF_NULL; + enc = AMF_EncodeString(enc, pend, &r->Link.playpath); + if (!enc) + return FALSE; + + packet.m_nBodySize = enc - packet.m_body; + + return RTMP_SendPacket(r, &packet, FALSE); +} + +SAVC(publish); +SAVC(live); +SAVC(record); + +static int +SendPublish(RTMP *r) +{ + RTMPPacket packet; + char pbuf[1024], *pend = pbuf + sizeof(pbuf); + char *enc; + + packet.m_nChannel = 0x04; /* source channel (invoke) */ + packet.m_headerType = RTMP_PACKET_SIZE_LARGE; + packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; + packet.m_nTimeStamp = 0; + packet.m_nInfoField2 = r->m_stream_id; + packet.m_hasAbsTimestamp = 0; + packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; + + enc = packet.m_body; + enc = AMF_EncodeString(enc, pend, &av_publish); + enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); + *enc++ = AMF_NULL; + enc = AMF_EncodeString(enc, pend, &r->Link.playpath); + if (!enc) + return FALSE; + + /* FIXME: should we choose live based on Link.lFlags & RTMP_LF_LIVE? */ + enc = AMF_EncodeString(enc, pend, &av_live); + if (!enc) + return FALSE; + + packet.m_nBodySize = enc - packet.m_body; + + return RTMP_SendPacket(r, &packet, TRUE); +} + +SAVC(deleteStream); + +static int +SendDeleteStream(RTMP *r, double dStreamId) +{ + RTMPPacket packet; + char pbuf[256], *pend = pbuf + sizeof(pbuf); + char *enc; + + packet.m_nChannel = 0x03; /* control channel (invoke) */ + packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; + packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; + packet.m_nTimeStamp = 0; + packet.m_nInfoField2 = 0; + packet.m_hasAbsTimestamp = 0; + packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; + + enc = packet.m_body; + enc = AMF_EncodeString(enc, pend, &av_deleteStream); + enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); + *enc++ = AMF_NULL; + enc = AMF_EncodeNumber(enc, pend, dStreamId); + + packet.m_nBodySize = enc - packet.m_body; + + /* no response expected */ + return RTMP_SendPacket(r, &packet, FALSE); +} + +SAVC(pause); + +int +RTMP_SendPause(RTMP *r, int DoPause, int iTime) +{ + RTMPPacket packet; + char pbuf[256], *pend = pbuf + sizeof(pbuf); + char *enc; + + packet.m_nChannel = 0x08; /* video channel */ + packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; + packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; + packet.m_nTimeStamp = 0; + packet.m_nInfoField2 = 0; + packet.m_hasAbsTimestamp = 0; + packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; + + enc = packet.m_body; + enc = AMF_EncodeString(enc, pend, &av_pause); + enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); + *enc++ = AMF_NULL; + enc = AMF_EncodeBoolean(enc, pend, DoPause); + enc = AMF_EncodeNumber(enc, pend, (double)iTime); + + packet.m_nBodySize = enc - packet.m_body; + + RTMP_Log(RTMP_LOGDEBUG, "%s, %d, pauseTime=%d", __FUNCTION__, DoPause, iTime); + return RTMP_SendPacket(r, &packet, TRUE); +} + +int RTMP_Pause(RTMP *r, int DoPause) +{ + if (DoPause) + r->m_pauseStamp = r->m_mediaChannel < r->m_channelsAllocatedIn ? + r->m_channelTimestamp[r->m_mediaChannel] : 0; + return RTMP_SendPause(r, DoPause, r->m_pauseStamp); +} + +SAVC(seek); + +int +RTMP_SendSeek(RTMP *r, int iTime) +{ + RTMPPacket packet; + char pbuf[256], *pend = pbuf + sizeof(pbuf); + char *enc; + + packet.m_nChannel = 0x08; /* video channel */ + packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; + packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; + packet.m_nTimeStamp = 0; + packet.m_nInfoField2 = 0; + packet.m_hasAbsTimestamp = 0; + packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; + + enc = packet.m_body; + enc = AMF_EncodeString(enc, pend, &av_seek); + enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); + *enc++ = AMF_NULL; + enc = AMF_EncodeNumber(enc, pend, (double)iTime); + + packet.m_nBodySize = enc - packet.m_body; + + r->m_read.flags |= RTMP_READ_SEEKING; + r->m_read.nResumeTS = 0; + + return RTMP_SendPacket(r, &packet, TRUE); +} + +int +RTMP_SendServerBW(RTMP *r) +{ + RTMPPacket packet; + char pbuf[256], *pend = pbuf + sizeof(pbuf); + + packet.m_nChannel = 0x02; /* control channel (invoke) */ + packet.m_headerType = RTMP_PACKET_SIZE_LARGE; + packet.m_packetType = RTMP_PACKET_TYPE_SERVER_BW; + packet.m_nTimeStamp = 0; + packet.m_nInfoField2 = 0; + packet.m_hasAbsTimestamp = 0; + packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; + + packet.m_nBodySize = 4; + + AMF_EncodeInt32(packet.m_body, pend, r->m_nServerBW); + return RTMP_SendPacket(r, &packet, FALSE); +} + +int +RTMP_SendClientBW(RTMP *r) +{ + RTMPPacket packet; + char pbuf[256], *pend = pbuf + sizeof(pbuf); + + packet.m_nChannel = 0x02; /* control channel (invoke) */ + packet.m_headerType = RTMP_PACKET_SIZE_LARGE; + packet.m_packetType = RTMP_PACKET_TYPE_CLIENT_BW; + packet.m_nTimeStamp = 0; + packet.m_nInfoField2 = 0; + packet.m_hasAbsTimestamp = 0; + packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; + + packet.m_nBodySize = 5; + + AMF_EncodeInt32(packet.m_body, pend, r->m_nClientBW); + packet.m_body[4] = r->m_nClientBW2; + return RTMP_SendPacket(r, &packet, FALSE); +} + +static int +SendBytesReceived(RTMP *r) +{ + RTMPPacket packet; + char pbuf[256], *pend = pbuf + sizeof(pbuf); + + packet.m_nChannel = 0x02; /* control channel (invoke) */ + packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; + packet.m_packetType = RTMP_PACKET_TYPE_BYTES_READ_REPORT; + packet.m_nTimeStamp = 0; + packet.m_nInfoField2 = 0; + packet.m_hasAbsTimestamp = 0; + packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; + + packet.m_nBodySize = 4; + + AMF_EncodeInt32(packet.m_body, pend, r->m_nBytesIn); /* hard coded for now */ + r->m_nBytesInSent = r->m_nBytesIn; + + /*RTMP_Log(RTMP_LOGDEBUG, "Send bytes report. 0x%x (%d bytes)", (unsigned int)m_nBytesIn, m_nBytesIn); */ + return RTMP_SendPacket(r, &packet, FALSE); +} + +SAVC(_checkbw); + +static int +SendCheckBW(RTMP *r) +{ + RTMPPacket packet; + char pbuf[256], *pend = pbuf + sizeof(pbuf); + char *enc; + + packet.m_nChannel = 0x03; /* control channel (invoke) */ + packet.m_headerType = RTMP_PACKET_SIZE_LARGE; + packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; + packet.m_nTimeStamp = 0; /* RTMP_GetTime(); */ + packet.m_nInfoField2 = 0; + packet.m_hasAbsTimestamp = 0; + packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; + + enc = packet.m_body; + enc = AMF_EncodeString(enc, pend, &av__checkbw); + enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); + *enc++ = AMF_NULL; + + packet.m_nBodySize = enc - packet.m_body; + + /* triggers _onbwcheck and eventually results in _onbwdone */ + return RTMP_SendPacket(r, &packet, FALSE); +} + +SAVC(_result); + +static int +SendCheckBWResult(RTMP *r, double txn) +{ + RTMPPacket packet; + char pbuf[256], *pend = pbuf + sizeof(pbuf); + char *enc; + + packet.m_nChannel = 0x03; /* control channel (invoke) */ + packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; + packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; + packet.m_nTimeStamp = 0x16 * r->m_nBWCheckCounter; /* temp inc value. till we figure it out. */ + packet.m_nInfoField2 = 0; + packet.m_hasAbsTimestamp = 0; + packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; + + enc = packet.m_body; + enc = AMF_EncodeString(enc, pend, &av__result); + enc = AMF_EncodeNumber(enc, pend, txn); + *enc++ = AMF_NULL; + enc = AMF_EncodeNumber(enc, pend, (double)r->m_nBWCheckCounter++); + + packet.m_nBodySize = enc - packet.m_body; + + return RTMP_SendPacket(r, &packet, FALSE); +} + +SAVC(ping); +SAVC(pong); + +static int +SendPong(RTMP *r, double txn) +{ + RTMPPacket packet; + char pbuf[256], *pend = pbuf + sizeof(pbuf); + char *enc; + + packet.m_nChannel = 0x03; /* control channel (invoke) */ + packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; + packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; + packet.m_nTimeStamp = 0x16 * r->m_nBWCheckCounter; /* temp inc value. till we figure it out. */ + packet.m_nInfoField2 = 0; + packet.m_hasAbsTimestamp = 0; + packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; + + enc = packet.m_body; + enc = AMF_EncodeString(enc, pend, &av_pong); + enc = AMF_EncodeNumber(enc, pend, txn); + *enc++ = AMF_NULL; + + packet.m_nBodySize = enc - packet.m_body; + + return RTMP_SendPacket(r, &packet, FALSE); +} + +SAVC(play); + +static int +SendPlay(RTMP *r) +{ + RTMPPacket packet; + char pbuf[1024], *pend = pbuf + sizeof(pbuf); + char *enc; + + packet.m_nChannel = 0x08; /* we make 8 our stream channel */ + packet.m_headerType = RTMP_PACKET_SIZE_LARGE; + packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; + packet.m_nTimeStamp = 0; + packet.m_nInfoField2 = r->m_stream_id; /*0x01000000; */ + packet.m_hasAbsTimestamp = 0; + packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; + + enc = packet.m_body; + enc = AMF_EncodeString(enc, pend, &av_play); + enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); + *enc++ = AMF_NULL; + + RTMP_Log(RTMP_LOGDEBUG, "%s, seekTime=%d, stopTime=%d, sending play: %s", + __FUNCTION__, r->Link.seekTime, r->Link.stopTime, + r->Link.playpath.av_val); + enc = AMF_EncodeString(enc, pend, &r->Link.playpath); + if (!enc) + return FALSE; + + /* Optional parameters start and len. + * + * start: -2, -1, 0, positive number + * -2: looks for a live stream, then a recorded stream, + * if not found any open a live stream + * -1: plays a live stream + * >=0: plays a recorded streams from 'start' milliseconds + */ + if (r->Link.lFlags & RTMP_LF_LIVE) + enc = AMF_EncodeNumber(enc, pend, -1000.0); + else + { + if (r->Link.seekTime > 0.0) + enc = AMF_EncodeNumber(enc, pend, r->Link.seekTime); /* resume from here */ + else + enc = AMF_EncodeNumber(enc, pend, 0.0); /*-2000.0);*/ /* recorded as default, -2000.0 is not reliable since that freezes the player if the stream is not found */ + } + if (!enc) + return FALSE; + + /* len: -1, 0, positive number + * -1: plays live or recorded stream to the end (default) + * 0: plays a frame 'start' ms away from the beginning + * >0: plays a live or recoded stream for 'len' milliseconds + */ + /*enc += EncodeNumber(enc, -1.0); */ /* len */ + if (r->Link.stopTime) + { + enc = AMF_EncodeNumber(enc, pend, r->Link.stopTime - r->Link.seekTime); + if (!enc) + return FALSE; + } + + packet.m_nBodySize = enc - packet.m_body; + + return RTMP_SendPacket(r, &packet, TRUE); +} + +SAVC(set_playlist); +SAVC(0); + +static int +SendPlaylist(RTMP *r) +{ + RTMPPacket packet; + char pbuf[1024], *pend = pbuf + sizeof(pbuf); + char *enc; + + packet.m_nChannel = 0x08; /* we make 8 our stream channel */ + packet.m_headerType = RTMP_PACKET_SIZE_LARGE; + packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; + packet.m_nTimeStamp = 0; + packet.m_nInfoField2 = r->m_stream_id; /*0x01000000; */ + packet.m_hasAbsTimestamp = 0; + packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; + + enc = packet.m_body; + enc = AMF_EncodeString(enc, pend, &av_set_playlist); + enc = AMF_EncodeNumber(enc, pend, 0); + *enc++ = AMF_NULL; + *enc++ = AMF_ECMA_ARRAY; + *enc++ = 0; + *enc++ = 0; + *enc++ = 0; + *enc++ = AMF_OBJECT; + enc = AMF_EncodeNamedString(enc, pend, &av_0, &r->Link.playpath); + if (!enc) + return FALSE; + if (enc + 3 >= pend) + return FALSE; + *enc++ = 0; + *enc++ = 0; + *enc++ = AMF_OBJECT_END; + + packet.m_nBodySize = enc - packet.m_body; + + return RTMP_SendPacket(r, &packet, TRUE); +} + +static int +SendSecureTokenResponse(RTMP *r, AVal *resp) +{ + RTMPPacket packet; + char pbuf[1024], *pend = pbuf + sizeof(pbuf); + char *enc; + + packet.m_nChannel = 0x03; /* control channel (invoke) */ + packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; + packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; + packet.m_nTimeStamp = 0; + packet.m_nInfoField2 = 0; + packet.m_hasAbsTimestamp = 0; + packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; + + enc = packet.m_body; + enc = AMF_EncodeString(enc, pend, &av_secureTokenResponse); + enc = AMF_EncodeNumber(enc, pend, 0.0); + *enc++ = AMF_NULL; + enc = AMF_EncodeString(enc, pend, resp); + if (!enc) + return FALSE; + + packet.m_nBodySize = enc - packet.m_body; + + return RTMP_SendPacket(r, &packet, FALSE); +} + +/* +from http://jira.red5.org/confluence/display/docs/Ping: + +Ping is the most mysterious message in RTMP and till now we haven't fully interpreted it yet. In summary, Ping message is used as a special command that are exchanged between client and server. This page aims to document all known Ping messages. Expect the list to grow. + +The type of Ping packet is 0x4 and contains two mandatory parameters and two optional parameters. The first parameter is the type of Ping and in short integer. The second parameter is the target of the ping. As Ping is always sent in Channel 2 (control channel) and the target object in RTMP header is always 0 which means the Connection object, it's necessary to put an extra parameter to indicate the exact target object the Ping is sent to. The second parameter takes this responsibility. The value has the same meaning as the target object field in RTMP header. (The second value could also be used as other purposes, like RTT Ping/Pong. It is used as the timestamp.) The third and fourth parameters are optional and could be looked upon as the parameter of the Ping packet. Below is an unexhausted list of Ping messages. + + * type 0: Clear the stream. No third and fourth parameters. The second parameter could be 0. After the connection is established, a Ping 0,0 will be sent from server to client. The message will also be sent to client on the start of Play and in response of a Seek or Pause/Resume request. This Ping tells client to re-calibrate the clock with the timestamp of the next packet server sends. + * type 1: Tell the stream to clear the playing buffer. + * type 3: Buffer time of the client. The third parameter is the buffer time in millisecond. + * type 4: Reset a stream. Used together with type 0 in the case of VOD. Often sent before type 0. + * type 6: Ping the client from server. The second parameter is the current time. + * type 7: Pong reply from client. The second parameter is the time the server sent with his ping request. + * type 26: SWFVerification request + * type 27: SWFVerification response +*/ +int +RTMP_SendCtrl(RTMP *r, short nType, unsigned int nObject, unsigned int nTime) +{ + RTMPPacket packet; + char pbuf[256], *pend = pbuf + sizeof(pbuf); + int nSize; + char *buf; + + RTMP_Log(RTMP_LOGDEBUG, "sending ctrl. type: 0x%04x", (unsigned short)nType); + + packet.m_nChannel = 0x02; /* control channel (ping) */ + packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; + packet.m_packetType = RTMP_PACKET_TYPE_CONTROL; + packet.m_nTimeStamp = 0; /* RTMP_GetTime(); */ + packet.m_nInfoField2 = 0; + packet.m_hasAbsTimestamp = 0; + packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; + + switch(nType) { + case 0x03: nSize = 10; break; /* buffer time */ + case 0x1A: nSize = 3; break; /* SWF verify request */ + case 0x1B: nSize = 44; break; /* SWF verify response */ + default: nSize = 6; break; + } + + packet.m_nBodySize = nSize; + + buf = packet.m_body; + buf = AMF_EncodeInt16(buf, pend, nType); + + if (nType == 0x1B) + { +#ifdef CRYPTO + memcpy(buf, r->Link.SWFVerificationResponse, 42); + RTMP_Log(RTMP_LOGDEBUG, "Sending SWFVerification response: "); + RTMP_LogHex(RTMP_LOGDEBUG, (uint8_t *)packet.m_body, packet.m_nBodySize); +#endif + } + else if (nType == 0x1A) + { + *buf = nObject & 0xff; + } + else + { + if (nSize > 2) + buf = AMF_EncodeInt32(buf, pend, nObject); + + if (nSize > 6) + buf = AMF_EncodeInt32(buf, pend, nTime); + } + + return RTMP_SendPacket(r, &packet, FALSE); +} + +static void +AV_erase(RTMP_METHOD *vals, int *num, int i, int freeit) +{ + if (freeit) + free(vals[i].name.av_val); + (*num)--; + for (; i < *num; i++) + { + vals[i] = vals[i + 1]; + } + vals[i].name.av_val = NULL; + vals[i].name.av_len = 0; + vals[i].num = 0; +} + +void +RTMP_DropRequest(RTMP *r, int i, int freeit) +{ + AV_erase(r->m_methodCalls, &r->m_numCalls, i, freeit); +} + +static void +AV_queue(RTMP_METHOD **vals, int *num, AVal *av, int txn) +{ + char *tmp; + if (!(*num & 0x0f)) + *vals = realloc(*vals, (*num + 16) * sizeof(RTMP_METHOD)); + tmp = malloc(av->av_len + 1); + memcpy(tmp, av->av_val, av->av_len); + tmp[av->av_len] = '\0'; + (*vals)[*num].num = txn; + (*vals)[*num].name.av_len = av->av_len; + (*vals)[(*num)++].name.av_val = tmp; +} + +static void +AV_clear(RTMP_METHOD *vals, int num) +{ + int i; + for (i = 0; i < num; i++) + free(vals[i].name.av_val); + free(vals); +} + + +#ifdef CRYPTO +static int +b64enc(const unsigned char *input, int length, char *output, int maxsize) +{ +#ifdef USE_POLARSSL + size_t buf_size = maxsize; + if(base64_encode((unsigned char *) output, &buf_size, input, length) == 0) + { + output[buf_size] = '\0'; + return 1; + } + else + { + RTMP_Log(RTMP_LOGDEBUG, "%s, error", __FUNCTION__); + return 0; + } +#elif defined(USE_GNUTLS) + if (BASE64_ENCODE_RAW_LENGTH(length) <= maxsize) + base64_encode_raw((uint8_t*) output, length, input); + else + { + RTMP_Log(RTMP_LOGDEBUG, "%s, error", __FUNCTION__); + return 0; + } +#else /* USE_OPENSSL */ + BIO *bmem, *b64; + BUF_MEM *bptr; + + b64 = BIO_new(BIO_f_base64()); + bmem = BIO_new(BIO_s_mem()); + b64 = BIO_push(b64, bmem); + BIO_write(b64, input, length); + if (BIO_flush(b64) == 1) + { + BIO_get_mem_ptr(b64, &bptr); + memcpy(output, bptr->data, bptr->length-1); + output[bptr->length-1] = '\0'; + } + else + { + RTMP_Log(RTMP_LOGDEBUG, "%s, error", __FUNCTION__); + return 0; + } + BIO_free_all(b64); +#endif + return 1; +} + +#ifdef USE_POLARSSL +#define MD5_CTX md5_context +#define MD5_Init(ctx) md5_starts(ctx) +#define MD5_Update(ctx,data,len) md5_update(ctx,(unsigned char *)data,len) +#define MD5_Final(dig,ctx) md5_finish(ctx,dig) +#elif defined(USE_GNUTLS) +typedef struct md5_ctx MD5_CTX; +#define MD5_Init(ctx) md5_init(ctx) +#define MD5_Update(ctx,data,len) md5_update(ctx,len,data) +#define MD5_Final(dig,ctx) md5_digest(ctx,MD5_DIGEST_LENGTH,dig) +#else +#endif + +static const AVal av_authmod_adobe = AVC("authmod=adobe"); +static const AVal av_authmod_llnw = AVC("authmod=llnw"); + +static void hexenc(unsigned char *inbuf, int len, char *dst) +{ + char *ptr = dst; + while(len--) { + sprintf(ptr, "%02x", *inbuf++); + ptr += 2; + } + *ptr = '\0'; +} + +static char * +AValChr(AVal *av, char c) +{ + int i; + for (i = 0; i < av->av_len; i++) + if (av->av_val[i] == c) + return &av->av_val[i]; + return NULL; +} + +static int +PublisherAuth(RTMP *r, AVal *description) +{ + char *token_in = NULL; + char *ptr; + unsigned char md5sum_val[MD5_DIGEST_LENGTH+1]; + MD5_CTX md5ctx; + int challenge2_data; +#define RESPONSE_LEN 32 +#define CHALLENGE2_LEN 16 +#define SALTED2_LEN (32+8+8+8) +#define B64DIGEST_LEN 24 /* 16 byte digest => 22 b64 chars + 2 chars padding */ +#define B64INT_LEN 8 /* 4 byte int => 6 b64 chars + 2 chars padding */ +#define HEXHASH_LEN (2*MD5_DIGEST_LENGTH) + char response[RESPONSE_LEN]; + char challenge2[CHALLENGE2_LEN]; + char salted2[SALTED2_LEN]; + AVal pubToken; + + if (strstr(description->av_val, av_authmod_adobe.av_val) != NULL) + { + if(strstr(description->av_val, "code=403 need auth") != NULL) + { + if (strstr(r->Link.app.av_val, av_authmod_adobe.av_val) != NULL) { + RTMP_Log(RTMP_LOGERROR, "%s, wrong pubUser & pubPasswd for publisher auth", __FUNCTION__); + return 0; + } else if(r->Link.pubUser.av_len && r->Link.pubPasswd.av_len) { + pubToken.av_val = malloc(r->Link.pubUser.av_len + av_authmod_adobe.av_len + 8); + pubToken.av_len = sprintf(pubToken.av_val, "?%s&user=%s", + av_authmod_adobe.av_val, + r->Link.pubUser.av_val); + RTMP_Log(RTMP_LOGDEBUG, "%s, pubToken1: %s", __FUNCTION__, pubToken.av_val); + } else { + RTMP_Log(RTMP_LOGERROR, "%s, need to set pubUser & pubPasswd for publisher auth", __FUNCTION__); + return 0; + } + } + else if((token_in = strstr(description->av_val, "?reason=needauth")) != NULL) + { + char *par, *val = NULL, *orig_ptr; + AVal user, salt, opaque, challenge, *aptr = NULL; + opaque.av_len = 0; + challenge.av_len = 0; + + ptr = orig_ptr = strdup(token_in); + while (ptr) + { + par = ptr; + ptr = strchr(par, '&'); + if(ptr) + *ptr++ = '\0'; + + val = strchr(par, '='); + if(val) + *val++ = '\0'; + + if (aptr) { + aptr->av_len = par - aptr->av_val - 1; + aptr = NULL; + } + if (strcmp(par, "user") == 0){ + user.av_val = val; + aptr = &user; + } else if (strcmp(par, "salt") == 0){ + salt.av_val = val; + aptr = &salt; + } else if (strcmp(par, "opaque") == 0){ + opaque.av_val = val; + aptr = &opaque; + } else if (strcmp(par, "challenge") == 0){ + challenge.av_val = val; + aptr = &challenge; + } + + RTMP_Log(RTMP_LOGDEBUG, "%s, par:\"%s\" = val:\"%s\"", __FUNCTION__, par, val); + } + if (aptr) + aptr->av_len = strlen(aptr->av_val); + + /* hash1 = base64enc(md5(user + _aodbeAuthSalt + password)) */ + MD5_Init(&md5ctx); + MD5_Update(&md5ctx, user.av_val, user.av_len); + MD5_Update(&md5ctx, salt.av_val, salt.av_len); + MD5_Update(&md5ctx, r->Link.pubPasswd.av_val, r->Link.pubPasswd.av_len); + MD5_Final(md5sum_val, &md5ctx); + RTMP_Log(RTMP_LOGDEBUG, "%s, md5(%s%s%s) =>", __FUNCTION__, + user.av_val, salt.av_val, r->Link.pubPasswd.av_val); + RTMP_LogHexString(RTMP_LOGDEBUG, md5sum_val, MD5_DIGEST_LENGTH); + + b64enc(md5sum_val, MD5_DIGEST_LENGTH, salted2, SALTED2_LEN); + RTMP_Log(RTMP_LOGDEBUG, "%s, b64(md5_1) = %s", __FUNCTION__, salted2); + + challenge2_data = rand(); + + b64enc((unsigned char *) &challenge2_data, sizeof(int), challenge2, CHALLENGE2_LEN); + RTMP_Log(RTMP_LOGDEBUG, "%s, b64(%d) = %s", __FUNCTION__, challenge2_data, challenge2); + + MD5_Init(&md5ctx); + MD5_Update(&md5ctx, salted2, B64DIGEST_LEN); + /* response = base64enc(md5(hash1 + opaque + challenge2)) */ + if (opaque.av_len) + MD5_Update(&md5ctx, opaque.av_val, opaque.av_len); + else if (challenge.av_len) + MD5_Update(&md5ctx, challenge.av_val, challenge.av_len); + MD5_Update(&md5ctx, challenge2, B64INT_LEN); + MD5_Final(md5sum_val, &md5ctx); + + RTMP_Log(RTMP_LOGDEBUG, "%s, md5(%s%s%s) =>", __FUNCTION__, + salted2, opaque.av_len ? opaque.av_val : "", challenge2); + RTMP_LogHexString(RTMP_LOGDEBUG, md5sum_val, MD5_DIGEST_LENGTH); + + b64enc(md5sum_val, MD5_DIGEST_LENGTH, response, RESPONSE_LEN); + RTMP_Log(RTMP_LOGDEBUG, "%s, b64(md5_2) = %s", __FUNCTION__, response); + + /* have all hashes, create auth token for the end of app */ + pubToken.av_val = malloc(32 + B64INT_LEN + B64DIGEST_LEN + opaque.av_len); + pubToken.av_len = sprintf(pubToken.av_val, + "&challenge=%s&response=%s&opaque=%s", + challenge2, + response, + opaque.av_len ? opaque.av_val : ""); + RTMP_Log(RTMP_LOGDEBUG, "%s, pubToken2: %s", __FUNCTION__, pubToken.av_val); + free(orig_ptr); + } + else if(strstr(description->av_val, "?reason=authfailed") != NULL) + { + RTMP_Log(RTMP_LOGERROR, "%s, Authentication failed: wrong password", __FUNCTION__); + return 0; + } + else if(strstr(description->av_val, "?reason=nosuchuser") != NULL) + { + RTMP_Log(RTMP_LOGERROR, "%s, Authentication failed: no such user", __FUNCTION__); + return 0; + } + else + { + RTMP_Log(RTMP_LOGERROR, "%s, Authentication failed: unknown auth mode: %s", + __FUNCTION__, description->av_val); + return 0; + } + + ptr = malloc(r->Link.app.av_len + pubToken.av_len); + strncpy(ptr, r->Link.app.av_val, r->Link.app.av_len); + strncpy(ptr + r->Link.app.av_len, pubToken.av_val, pubToken.av_len); + r->Link.app.av_len += pubToken.av_len; + if(r->Link.lFlags & RTMP_LF_FAPU) + free(r->Link.app.av_val); + r->Link.app.av_val = ptr; + + ptr = malloc(r->Link.tcUrl.av_len + pubToken.av_len); + strncpy(ptr, r->Link.tcUrl.av_val, r->Link.tcUrl.av_len); + strncpy(ptr + r->Link.tcUrl.av_len, pubToken.av_val, pubToken.av_len); + r->Link.tcUrl.av_len += pubToken.av_len; + if(r->Link.lFlags & RTMP_LF_FTCU) + free(r->Link.tcUrl.av_val); + r->Link.tcUrl.av_val = ptr; + + free(pubToken.av_val); + r->Link.lFlags |= RTMP_LF_FTCU | RTMP_LF_FAPU; + + RTMP_Log(RTMP_LOGDEBUG, "%s, new app: %.*s tcUrl: %.*s playpath: %s", __FUNCTION__, + r->Link.app.av_len, r->Link.app.av_val, + r->Link.tcUrl.av_len, r->Link.tcUrl.av_val, + r->Link.playpath.av_val); + } + else if (strstr(description->av_val, av_authmod_llnw.av_val) != NULL) + { + if(strstr(description->av_val, "code=403 need auth") != NULL) + { + /* This part seems to be the same for llnw and adobe */ + + if (strstr(r->Link.app.av_val, av_authmod_llnw.av_val) != NULL) { + RTMP_Log(RTMP_LOGERROR, "%s, wrong pubUser & pubPasswd for publisher auth", __FUNCTION__); + return 0; + } else if(r->Link.pubUser.av_len && r->Link.pubPasswd.av_len) { + pubToken.av_val = malloc(r->Link.pubUser.av_len + av_authmod_llnw.av_len + 8); + pubToken.av_len = sprintf(pubToken.av_val, "?%s&user=%s", + av_authmod_llnw.av_val, + r->Link.pubUser.av_val); + RTMP_Log(RTMP_LOGDEBUG, "%s, pubToken1: %s", __FUNCTION__, pubToken.av_val); + } else { + RTMP_Log(RTMP_LOGERROR, "%s, need to set pubUser & pubPasswd for publisher auth", __FUNCTION__); + return 0; + } + } + else if((token_in = strstr(description->av_val, "?reason=needauth")) != NULL) + { + char *orig_ptr; + char *par, *val = NULL; + char hash1[HEXHASH_LEN+1], hash2[HEXHASH_LEN+1], hash3[HEXHASH_LEN+1]; + AVal user, nonce, *aptr = NULL; + AVal apptmp; + + /* llnw auth method + * Seems to be closely based on HTTP Digest Auth: + * http://tools.ietf.org/html/rfc2617 + * http://en.wikipedia.org/wiki/Digest_access_authentication + */ + + const char authmod[] = "llnw"; + const char realm[] = "live"; + const char method[] = "publish"; + const char qop[] = "auth"; + /* nc = 1..connection count (or rather, number of times cnonce has been reused) */ + int nc = 1; + /* nchex = hexenc(nc) (8 hex digits according to RFC 2617) */ + char nchex[9]; + /* cnonce = hexenc(4 random bytes) (initialized on first connection) */ + char cnonce[9]; + + ptr = orig_ptr = strdup(token_in); + /* Extract parameters (we need user and nonce) */ + while (ptr) + { + par = ptr; + ptr = strchr(par, '&'); + if(ptr) + *ptr++ = '\0'; + + val = strchr(par, '='); + if(val) + *val++ = '\0'; + + if (aptr) { + aptr->av_len = par - aptr->av_val - 1; + aptr = NULL; + } + if (strcmp(par, "user") == 0){ + user.av_val = val; + aptr = &user; + } else if (strcmp(par, "nonce") == 0){ + nonce.av_val = val; + aptr = &nonce; + } + + RTMP_Log(RTMP_LOGDEBUG, "%s, par:\"%s\" = val:\"%s\"", __FUNCTION__, par, val); + } + if (aptr) + aptr->av_len = strlen(aptr->av_val); + + /* FIXME: handle case where user==NULL or nonce==NULL */ + + sprintf(nchex, "%08x", nc); + sprintf(cnonce, "%08x", rand()); + + /* hash1 = hexenc(md5(user + ":" + realm + ":" + password)) */ + MD5_Init(&md5ctx); + MD5_Update(&md5ctx, user.av_val, user.av_len); + MD5_Update(&md5ctx, ":", 1); + MD5_Update(&md5ctx, realm, sizeof(realm)-1); + MD5_Update(&md5ctx, ":", 1); + MD5_Update(&md5ctx, r->Link.pubPasswd.av_val, r->Link.pubPasswd.av_len); + MD5_Final(md5sum_val, &md5ctx); + RTMP_Log(RTMP_LOGDEBUG, "%s, md5(%s:%s:%s) =>", __FUNCTION__, + user.av_val, realm, r->Link.pubPasswd.av_val); + RTMP_LogHexString(RTMP_LOGDEBUG, md5sum_val, MD5_DIGEST_LENGTH); + hexenc(md5sum_val, MD5_DIGEST_LENGTH, hash1); + + /* hash2 = hexenc(md5(method + ":/" + app + "/" + appInstance)) */ + /* Extract appname + appinstance without query parameters */ + apptmp = r->Link.app; + ptr = AValChr(&apptmp, '?'); + if (ptr) + apptmp.av_len = ptr - apptmp.av_val; + + MD5_Init(&md5ctx); + MD5_Update(&md5ctx, method, sizeof(method)-1); + MD5_Update(&md5ctx, ":/", 2); + MD5_Update(&md5ctx, apptmp.av_val, apptmp.av_len); + if (!AValChr(&apptmp, '/')) + MD5_Update(&md5ctx, "/_definst_", sizeof("/_definst_") - 1); + MD5_Final(md5sum_val, &md5ctx); + RTMP_Log(RTMP_LOGDEBUG, "%s, md5(%s:/%.*s) =>", __FUNCTION__, + method, apptmp.av_len, apptmp.av_val); + RTMP_LogHexString(RTMP_LOGDEBUG, md5sum_val, MD5_DIGEST_LENGTH); + hexenc(md5sum_val, MD5_DIGEST_LENGTH, hash2); + + /* hash3 = hexenc(md5(hash1 + ":" + nonce + ":" + nchex + ":" + cnonce + ":" + qop + ":" + hash2)) */ + MD5_Init(&md5ctx); + MD5_Update(&md5ctx, hash1, HEXHASH_LEN); + MD5_Update(&md5ctx, ":", 1); + MD5_Update(&md5ctx, nonce.av_val, nonce.av_len); + MD5_Update(&md5ctx, ":", 1); + MD5_Update(&md5ctx, nchex, sizeof(nchex)-1); + MD5_Update(&md5ctx, ":", 1); + MD5_Update(&md5ctx, cnonce, sizeof(cnonce)-1); + MD5_Update(&md5ctx, ":", 1); + MD5_Update(&md5ctx, qop, sizeof(qop)-1); + MD5_Update(&md5ctx, ":", 1); + MD5_Update(&md5ctx, hash2, HEXHASH_LEN); + MD5_Final(md5sum_val, &md5ctx); + RTMP_Log(RTMP_LOGDEBUG, "%s, md5(%s:%s:%s:%s:%s:%s) =>", __FUNCTION__, + hash1, nonce.av_val, nchex, cnonce, qop, hash2); + RTMP_LogHexString(RTMP_LOGDEBUG, md5sum_val, MD5_DIGEST_LENGTH); + hexenc(md5sum_val, MD5_DIGEST_LENGTH, hash3); + + /* pubToken = &authmod=&user=&nonce=&cnonce=&nc=&response= */ + /* Append nonces and response to query string which already contains + * user + authmod */ + pubToken.av_val = malloc(64 + sizeof(authmod)-1 + user.av_len + nonce.av_len + sizeof(cnonce)-1 + sizeof(nchex)-1 + HEXHASH_LEN); + sprintf(pubToken.av_val, + "&nonce=%s&cnonce=%s&nc=%s&response=%s", + nonce.av_val, cnonce, nchex, hash3); + pubToken.av_len = strlen(pubToken.av_val); + RTMP_Log(RTMP_LOGDEBUG, "%s, pubToken2: %s", __FUNCTION__, pubToken.av_val); + + free(orig_ptr); + } + else if(strstr(description->av_val, "?reason=authfail") != NULL) + { + RTMP_Log(RTMP_LOGERROR, "%s, Authentication failed", __FUNCTION__); + return 0; + } + else if(strstr(description->av_val, "?reason=nosuchuser") != NULL) + { + RTMP_Log(RTMP_LOGERROR, "%s, Authentication failed: no such user", __FUNCTION__); + return 0; + } + else + { + RTMP_Log(RTMP_LOGERROR, "%s, Authentication failed: unknown auth mode: %s", + __FUNCTION__, description->av_val); + return 0; + } + + ptr = malloc(r->Link.app.av_len + pubToken.av_len); + strncpy(ptr, r->Link.app.av_val, r->Link.app.av_len); + strncpy(ptr + r->Link.app.av_len, pubToken.av_val, pubToken.av_len); + r->Link.app.av_len += pubToken.av_len; + if(r->Link.lFlags & RTMP_LF_FAPU) + free(r->Link.app.av_val); + r->Link.app.av_val = ptr; + + ptr = malloc(r->Link.tcUrl.av_len + pubToken.av_len); + strncpy(ptr, r->Link.tcUrl.av_val, r->Link.tcUrl.av_len); + strncpy(ptr + r->Link.tcUrl.av_len, pubToken.av_val, pubToken.av_len); + r->Link.tcUrl.av_len += pubToken.av_len; + if(r->Link.lFlags & RTMP_LF_FTCU) + free(r->Link.tcUrl.av_val); + r->Link.tcUrl.av_val = ptr; + + free(pubToken.av_val); + r->Link.lFlags |= RTMP_LF_FTCU | RTMP_LF_FAPU; + + RTMP_Log(RTMP_LOGDEBUG, "%s, new app: %.*s tcUrl: %.*s playpath: %s", __FUNCTION__, + r->Link.app.av_len, r->Link.app.av_val, + r->Link.tcUrl.av_len, r->Link.tcUrl.av_val, + r->Link.playpath.av_val); + } + else + { + return 0; + } + return 1; +} +#endif + + +SAVC(onBWDone); +SAVC(onFCSubscribe); +SAVC(onFCUnsubscribe); +SAVC(_onbwcheck); +SAVC(_onbwdone); +SAVC(_error); +SAVC(close); +SAVC(code); +SAVC(level); +SAVC(description); +SAVC(onStatus); +SAVC(playlist_ready); +static const AVal av_NetStream_Failed = AVC("NetStream.Failed"); +static const AVal av_NetStream_Play_Failed = AVC("NetStream.Play.Failed"); +static const AVal av_NetStream_Play_StreamNotFound = +AVC("NetStream.Play.StreamNotFound"); +static const AVal av_NetConnection_Connect_InvalidApp = +AVC("NetConnection.Connect.InvalidApp"); +static const AVal av_NetStream_Play_Start = AVC("NetStream.Play.Start"); +static const AVal av_NetStream_Play_Complete = AVC("NetStream.Play.Complete"); +static const AVal av_NetStream_Play_Stop = AVC("NetStream.Play.Stop"); +static const AVal av_NetStream_Seek_Notify = AVC("NetStream.Seek.Notify"); +static const AVal av_NetStream_Pause_Notify = AVC("NetStream.Pause.Notify"); +static const AVal av_NetStream_Play_PublishNotify = +AVC("NetStream.Play.PublishNotify"); +static const AVal av_NetStream_Play_UnpublishNotify = +AVC("NetStream.Play.UnpublishNotify"); +static const AVal av_NetStream_Publish_Start = AVC("NetStream.Publish.Start"); +static const AVal av_NetConnection_Connect_Rejected = +AVC("NetConnection.Connect.Rejected"); + +/* Returns 0 for OK/Failed/error, 1 for 'Stop or Complete' */ +static int +HandleInvoke(RTMP *r, const char *body, unsigned int nBodySize) +{ + AMFObject obj; + AVal method; + double txn; + int ret = 0, nRes; + if (body[0] != 0x02) /* make sure it is a string method name we start with */ + { + RTMP_Log(RTMP_LOGWARNING, "%s, Sanity failed. no string method in invoke packet", + __FUNCTION__); + return 0; + } + + nRes = AMF_Decode(&obj, body, nBodySize, FALSE); + if (nRes < 0) + { + RTMP_Log(RTMP_LOGERROR, "%s, error decoding invoke packet", __FUNCTION__); + return 0; + } + + AMF_Dump(&obj); + AMFProp_GetString(AMF_GetProp(&obj, NULL, 0), &method); + txn = AMFProp_GetNumber(AMF_GetProp(&obj, NULL, 1)); + RTMP_Log(RTMP_LOGDEBUG, "%s, server invoking <%s>", __FUNCTION__, method.av_val); + + if (AVMATCH(&method, &av__result)) + { + AVal methodInvoked = {0}; + int i; + + for (i=0; im_numCalls; i++) { + if (r->m_methodCalls[i].num == (int)txn) { + methodInvoked = r->m_methodCalls[i].name; + AV_erase(r->m_methodCalls, &r->m_numCalls, i, FALSE); + break; + } + } + if (!methodInvoked.av_val) { + RTMP_Log(RTMP_LOGDEBUG, "%s, received result id %f without matching request", + __FUNCTION__, txn); + goto leave; + } + + RTMP_Log(RTMP_LOGDEBUG, "%s, received result for method call <%s>", __FUNCTION__, + methodInvoked.av_val); + + if (AVMATCH(&methodInvoked, &av_connect)) + { + if (r->Link.token.av_len) + { + AMFObjectProperty p; + if (RTMP_FindFirstMatchingProperty(&obj, &av_secureToken, &p)) + { + DecodeTEA(&r->Link.token, &p.p_vu.p_aval); + SendSecureTokenResponse(r, &p.p_vu.p_aval); + } + } + if (r->Link.protocol & RTMP_FEATURE_WRITE) + { + SendReleaseStream(r); + SendFCPublish(r); + } + else + { + RTMP_SendServerBW(r); + RTMP_SendCtrl(r, 3, 0, 300); + } + RTMP_SendCreateStream(r); + + if (!(r->Link.protocol & RTMP_FEATURE_WRITE)) + { + /* Authenticate on Justin.tv legacy servers before sending FCSubscribe */ + if (r->Link.usherToken.av_len) + SendUsherToken(r, &r->Link.usherToken); + /* Send the FCSubscribe if live stream or if subscribepath is set */ + if (r->Link.subscribepath.av_len) + SendFCSubscribe(r, &r->Link.subscribepath); + else if (r->Link.lFlags & RTMP_LF_LIVE) + SendFCSubscribe(r, &r->Link.playpath); + } + } + else if (AVMATCH(&methodInvoked, &av_createStream)) + { + r->m_stream_id = (int)AMFProp_GetNumber(AMF_GetProp(&obj, NULL, 3)); + + if (r->Link.protocol & RTMP_FEATURE_WRITE) + { + SendPublish(r); + } + else + { + if (r->Link.lFlags & RTMP_LF_PLST) + SendPlaylist(r); + SendPlay(r); + RTMP_SendCtrl(r, 3, r->m_stream_id, r->m_nBufferMS); + } + } + else if (AVMATCH(&methodInvoked, &av_play) || + AVMATCH(&methodInvoked, &av_publish)) + { + r->m_bPlaying = TRUE; + } + free(methodInvoked.av_val); + } + else if (AVMATCH(&method, &av_onBWDone)) + { + if (!r->m_nBWCheckCounter) + SendCheckBW(r); + } + else if (AVMATCH(&method, &av_onFCSubscribe)) + { + /* SendOnFCSubscribe(); */ + } + else if (AVMATCH(&method, &av_onFCUnsubscribe)) + { + RTMP_Close(r); + ret = 1; + } + else if (AVMATCH(&method, &av_ping)) + { + SendPong(r, txn); + } + else if (AVMATCH(&method, &av__onbwcheck)) + { + SendCheckBWResult(r, txn); + } + else if (AVMATCH(&method, &av__onbwdone)) + { + int i; + for (i = 0; i < r->m_numCalls; i++) + if (AVMATCH(&r->m_methodCalls[i].name, &av__checkbw)) + { + AV_erase(r->m_methodCalls, &r->m_numCalls, i, TRUE); + break; + } + } + else if (AVMATCH(&method, &av__error)) + { +#ifdef CRYPTO + AVal methodInvoked = {0}; + int i; + + if (r->Link.protocol & RTMP_FEATURE_WRITE) + { + for (i=0; im_numCalls; i++) + { + if (r->m_methodCalls[i].num == txn) + { + methodInvoked = r->m_methodCalls[i].name; + AV_erase(r->m_methodCalls, &r->m_numCalls, i, FALSE); + break; + } + } + if (!methodInvoked.av_val) + { + RTMP_Log(RTMP_LOGDEBUG, "%s, received result id %f without matching request", + __FUNCTION__, txn); + goto leave; + } + + RTMP_Log(RTMP_LOGDEBUG, "%s, received error for method call <%s>", __FUNCTION__, + methodInvoked.av_val); + + if (AVMATCH(&methodInvoked, &av_connect)) + { + AMFObject obj2; + AVal code, level, description; + AMFProp_GetObject(AMF_GetProp(&obj, NULL, 3), &obj2); + AMFProp_GetString(AMF_GetProp(&obj2, &av_code, -1), &code); + AMFProp_GetString(AMF_GetProp(&obj2, &av_level, -1), &level); + AMFProp_GetString(AMF_GetProp(&obj2, &av_description, -1), &description); + RTMP_Log(RTMP_LOGDEBUG, "%s, error description: %s", __FUNCTION__, description.av_val); + /* if PublisherAuth returns 1, then reconnect */ + if (PublisherAuth(r, &description) == 1) + { + CloseInternal(r, 1); + if (!RTMP_Connect(r, NULL) || !RTMP_ConnectStream(r, 0)) + goto leave; + } + } + } + else + { + RTMP_Log(RTMP_LOGERROR, "rtmp server sent error"); + } + free(methodInvoked.av_val); +#else + RTMP_Log(RTMP_LOGERROR, "rtmp server sent error"); +#endif + } + else if (AVMATCH(&method, &av_close)) + { + RTMP_Log(RTMP_LOGERROR, "rtmp server requested close"); + RTMP_Close(r); + } + else if (AVMATCH(&method, &av_onStatus)) + { + AMFObject obj2; + AVal code, level; + AMFProp_GetObject(AMF_GetProp(&obj, NULL, 3), &obj2); + AMFProp_GetString(AMF_GetProp(&obj2, &av_code, -1), &code); + AMFProp_GetString(AMF_GetProp(&obj2, &av_level, -1), &level); + + RTMP_Log(RTMP_LOGDEBUG, "%s, onStatus: %s", __FUNCTION__, code.av_val); + if (AVMATCH(&code, &av_NetStream_Failed) + || AVMATCH(&code, &av_NetStream_Play_Failed) + || AVMATCH(&code, &av_NetStream_Play_StreamNotFound) + || AVMATCH(&code, &av_NetConnection_Connect_InvalidApp)) + { + r->m_stream_id = -1; + RTMP_Close(r); + RTMP_Log(RTMP_LOGERROR, "Closing connection: %s", code.av_val); + } + + else if (AVMATCH(&code, &av_NetStream_Play_Start) + || AVMATCH(&code, &av_NetStream_Play_PublishNotify)) + { + int i; + r->m_bPlaying = TRUE; + for (i = 0; i < r->m_numCalls; i++) + { + if (AVMATCH(&r->m_methodCalls[i].name, &av_play)) + { + AV_erase(r->m_methodCalls, &r->m_numCalls, i, TRUE); + break; + } + } + } + + else if (AVMATCH(&code, &av_NetStream_Publish_Start)) + { + int i; + r->m_bPlaying = TRUE; + for (i = 0; i < r->m_numCalls; i++) + { + if (AVMATCH(&r->m_methodCalls[i].name, &av_publish)) + { + AV_erase(r->m_methodCalls, &r->m_numCalls, i, TRUE); + break; + } + } + } + + /* Return 1 if this is a Play.Complete or Play.Stop */ + else if (AVMATCH(&code, &av_NetStream_Play_Complete) + || AVMATCH(&code, &av_NetStream_Play_Stop) + || AVMATCH(&code, &av_NetStream_Play_UnpublishNotify)) + { + RTMP_Close(r); + ret = 1; + } + + else if (AVMATCH(&code, &av_NetStream_Seek_Notify)) + { + r->m_read.flags &= ~RTMP_READ_SEEKING; + } + + else if (AVMATCH(&code, &av_NetStream_Pause_Notify)) + { + if (r->m_pausing == 1 || r->m_pausing == 2) + { + RTMP_SendPause(r, FALSE, r->m_pauseStamp); + r->m_pausing = 3; + } + } + } + else if (AVMATCH(&method, &av_playlist_ready)) + { + int i; + for (i = 0; i < r->m_numCalls; i++) + { + if (AVMATCH(&r->m_methodCalls[i].name, &av_set_playlist)) + { + AV_erase(r->m_methodCalls, &r->m_numCalls, i, TRUE); + break; + } + } + } + else + { + + } +leave: + AMF_Reset(&obj); + return ret; +} + +int +RTMP_FindFirstMatchingProperty(AMFObject *obj, const AVal *name, + AMFObjectProperty * p) +{ + int n; + /* this is a small object search to locate the "duration" property */ + for (n = 0; n < obj->o_num; n++) + { + AMFObjectProperty *prop = AMF_GetProp(obj, NULL, n); + + if (AVMATCH(&prop->p_name, name)) + { + memcpy(p, prop, sizeof(*prop)); + return TRUE; + } + + if (prop->p_type == AMF_OBJECT || prop->p_type == AMF_ECMA_ARRAY) + { + if (RTMP_FindFirstMatchingProperty(&prop->p_vu.p_object, name, p)) + return TRUE; + } + } + return FALSE; +} + +/* Like above, but only check if name is a prefix of property */ +int +RTMP_FindPrefixProperty(AMFObject *obj, const AVal *name, + AMFObjectProperty * p) +{ + int n; + for (n = 0; n < obj->o_num; n++) + { + AMFObjectProperty *prop = AMF_GetProp(obj, NULL, n); + + if (prop->p_name.av_len > name->av_len && + !memcmp(prop->p_name.av_val, name->av_val, name->av_len)) + { + memcpy(p, prop, sizeof(*prop)); + return TRUE; + } + + if (prop->p_type == AMF_OBJECT) + { + if (RTMP_FindPrefixProperty(&prop->p_vu.p_object, name, p)) + return TRUE; + } + } + return FALSE; +} + +static int +DumpMetaData(AMFObject *obj) +{ + AMFObjectProperty *prop; + int n, len; + for (n = 0; n < obj->o_num; n++) + { + char str[256] = ""; + prop = AMF_GetProp(obj, NULL, n); + switch (prop->p_type) + { + case AMF_OBJECT: + case AMF_ECMA_ARRAY: + case AMF_STRICT_ARRAY: + if (prop->p_name.av_len) + RTMP_Log(RTMP_LOGINFO, "%.*s:", prop->p_name.av_len, prop->p_name.av_val); + DumpMetaData(&prop->p_vu.p_object); + break; + case AMF_NUMBER: + snprintf(str, 255, "%.2f", prop->p_vu.p_number); + break; + case AMF_BOOLEAN: + snprintf(str, 255, "%s", + prop->p_vu.p_number != 0. ? "TRUE" : "FALSE"); + break; + case AMF_STRING: + len = snprintf(str, 255, "%.*s", prop->p_vu.p_aval.av_len, + prop->p_vu.p_aval.av_val); + if (len >= 1 && str[len-1] == '\n') + str[len-1] = '\0'; + break; + case AMF_DATE: + snprintf(str, 255, "timestamp:%.2f", prop->p_vu.p_number); + break; + default: + snprintf(str, 255, "INVALID TYPE 0x%02x", + (unsigned char)prop->p_type); + } + if (str[0] && prop->p_name.av_len) + { + RTMP_Log(RTMP_LOGINFO, " %-22.*s%s", prop->p_name.av_len, + prop->p_name.av_val, str); + } + } + return FALSE; +} + +SAVC(onMetaData); +SAVC(duration); +SAVC(video); +SAVC(audio); + +static int +HandleMetadata(RTMP *r, char *body, unsigned int len) +{ + /* allright we get some info here, so parse it and print it */ + /* also keep duration or filesize to make a nice progress bar */ + + AMFObject obj; + AVal metastring; + int ret = FALSE; + + int nRes = AMF_Decode(&obj, body, len, FALSE); + if (nRes < 0) + { + RTMP_Log(RTMP_LOGERROR, "%s, error decoding meta data packet", __FUNCTION__); + return FALSE; + } + + AMF_Dump(&obj); + AMFProp_GetString(AMF_GetProp(&obj, NULL, 0), &metastring); + + if (AVMATCH(&metastring, &av_onMetaData)) + { + AMFObjectProperty prop; + /* Show metadata */ + RTMP_Log(RTMP_LOGINFO, "Metadata:"); + DumpMetaData(&obj); + if (RTMP_FindFirstMatchingProperty(&obj, &av_duration, &prop)) + { + r->m_fDuration = prop.p_vu.p_number; + /*RTMP_Log(RTMP_LOGDEBUG, "Set duration: %.2f", m_fDuration); */ + } + /* Search for audio or video tags */ + if (RTMP_FindPrefixProperty(&obj, &av_video, &prop)) + r->m_read.dataType |= 1; + if (RTMP_FindPrefixProperty(&obj, &av_audio, &prop)) + r->m_read.dataType |= 4; + ret = TRUE; + } + AMF_Reset(&obj); + return ret; +} + +static void +HandleChangeChunkSize(RTMP *r, const RTMPPacket *packet) +{ + if (packet->m_nBodySize >= 4) + { + r->m_inChunkSize = AMF_DecodeInt32(packet->m_body); + RTMP_Log(RTMP_LOGDEBUG, "%s, received: chunk size change to %d", __FUNCTION__, + r->m_inChunkSize); + } +} + +static void +HandleAudio(RTMP *r, const RTMPPacket *packet) +{ +} + +static void +HandleVideo(RTMP *r, const RTMPPacket *packet) +{ +} + +static void +HandleCtrl(RTMP *r, const RTMPPacket *packet) +{ + short nType = -1; + unsigned int tmp; + if (packet->m_body && packet->m_nBodySize >= 2) + nType = AMF_DecodeInt16(packet->m_body); + RTMP_Log(RTMP_LOGDEBUG, "%s, received ctrl. type: %d, len: %d", __FUNCTION__, nType, + packet->m_nBodySize); + /*RTMP_LogHex(packet.m_body, packet.m_nBodySize); */ + + if (packet->m_nBodySize >= 6) + { + switch (nType) + { + case 0: + tmp = AMF_DecodeInt32(packet->m_body + 2); + RTMP_Log(RTMP_LOGDEBUG, "%s, Stream Begin %d", __FUNCTION__, tmp); + break; + + case 1: + tmp = AMF_DecodeInt32(packet->m_body + 2); + RTMP_Log(RTMP_LOGDEBUG, "%s, Stream EOF %d", __FUNCTION__, tmp); + if (r->m_pausing == 1) + r->m_pausing = 2; + break; + + case 2: + tmp = AMF_DecodeInt32(packet->m_body + 2); + RTMP_Log(RTMP_LOGDEBUG, "%s, Stream Dry %d", __FUNCTION__, tmp); + break; + + case 4: + tmp = AMF_DecodeInt32(packet->m_body + 2); + RTMP_Log(RTMP_LOGDEBUG, "%s, Stream IsRecorded %d", __FUNCTION__, tmp); + break; + + case 6: /* server ping. reply with pong. */ + tmp = AMF_DecodeInt32(packet->m_body + 2); + RTMP_Log(RTMP_LOGDEBUG, "%s, Ping %d", __FUNCTION__, tmp); + RTMP_SendCtrl(r, 0x07, tmp, 0); + break; + + /* FMS 3.5 servers send the following two controls to let the client + * know when the server has sent a complete buffer. I.e., when the + * server has sent an amount of data equal to m_nBufferMS in duration. + * The server meters its output so that data arrives at the client + * in realtime and no faster. + * + * The rtmpdump program tries to set m_nBufferMS as large as + * possible, to force the server to send data as fast as possible. + * In practice, the server appears to cap this at about 1 hour's + * worth of data. After the server has sent a complete buffer, and + * sends this BufferEmpty message, it will wait until the play + * duration of that buffer has passed before sending a new buffer. + * The BufferReady message will be sent when the new buffer starts. + * (There is no BufferReady message for the very first buffer; + * presumably the Stream Begin message is sufficient for that + * purpose.) + * + * If the network speed is much faster than the data bitrate, then + * there may be long delays between the end of one buffer and the + * start of the next. + * + * Since usually the network allows data to be sent at + * faster than realtime, and rtmpdump wants to download the data + * as fast as possible, we use this RTMP_LF_BUFX hack: when we + * get the BufferEmpty message, we send a Pause followed by an + * Unpause. This causes the server to send the next buffer immediately + * instead of waiting for the full duration to elapse. (That's + * also the purpose of the ToggleStream function, which rtmpdump + * calls if we get a read timeout.) + * + * Media player apps don't need this hack since they are just + * going to play the data in realtime anyway. It also doesn't work + * for live streams since they obviously can only be sent in + * realtime. And it's all moot if the network speed is actually + * slower than the media bitrate. + */ + case 31: + tmp = AMF_DecodeInt32(packet->m_body + 2); + RTMP_Log(RTMP_LOGDEBUG, "%s, Stream BufferEmpty %d", __FUNCTION__, tmp); + if (!(r->Link.lFlags & RTMP_LF_BUFX)) + break; + if (!r->m_pausing) + { + r->m_pauseStamp = r->m_mediaChannel < r->m_channelsAllocatedIn ? + r->m_channelTimestamp[r->m_mediaChannel] : 0; + RTMP_SendPause(r, TRUE, r->m_pauseStamp); + r->m_pausing = 1; + } + else if (r->m_pausing == 2) + { + RTMP_SendPause(r, FALSE, r->m_pauseStamp); + r->m_pausing = 3; + } + break; + + case 32: + tmp = AMF_DecodeInt32(packet->m_body + 2); + RTMP_Log(RTMP_LOGDEBUG, "%s, Stream BufferReady %d", __FUNCTION__, tmp); + break; + + default: + tmp = AMF_DecodeInt32(packet->m_body + 2); + RTMP_Log(RTMP_LOGDEBUG, "%s, Stream xx %d", __FUNCTION__, tmp); + break; + } + + } + + if (nType == 0x1A) + { + RTMP_Log(RTMP_LOGDEBUG, "%s, SWFVerification ping received: ", __FUNCTION__); + if (packet->m_nBodySize > 2 && packet->m_body[2] > 0x01) + { + RTMP_Log(RTMP_LOGERROR, + "%s: SWFVerification Type %d request not supported! Patches welcome...", + __FUNCTION__, packet->m_body[2]); + } +#ifdef CRYPTO + /*RTMP_LogHex(packet.m_body, packet.m_nBodySize); */ + + /* respond with HMAC SHA256 of decompressed SWF, key is the 30byte player key, also the last 30 bytes of the server handshake are applied */ + else if (r->Link.SWFSize) + { + RTMP_SendCtrl(r, 0x1B, 0, 0); + } + else + { + RTMP_Log(RTMP_LOGERROR, + "%s: Ignoring SWFVerification request, use --swfVfy!", + __FUNCTION__); + } +#else + RTMP_Log(RTMP_LOGERROR, + "%s: Ignoring SWFVerification request, no CRYPTO support!", + __FUNCTION__); +#endif + } +} + +static void +HandleServerBW(RTMP *r, const RTMPPacket *packet) +{ + r->m_nServerBW = AMF_DecodeInt32(packet->m_body); + RTMP_Log(RTMP_LOGDEBUG, "%s: server BW = %d", __FUNCTION__, r->m_nServerBW); +} + +static void +HandleClientBW(RTMP *r, const RTMPPacket *packet) +{ + r->m_nClientBW = AMF_DecodeInt32(packet->m_body); + if (packet->m_nBodySize > 4) + r->m_nClientBW2 = packet->m_body[4]; + else + r->m_nClientBW2 = -1; + RTMP_Log(RTMP_LOGDEBUG, "%s: client BW = %d %d", __FUNCTION__, r->m_nClientBW, + r->m_nClientBW2); +} + +static int +DecodeInt32LE(const char *data) +{ + unsigned char *c = (unsigned char *)data; + unsigned int val; + + val = (c[3] << 24) | (c[2] << 16) | (c[1] << 8) | c[0]; + return val; +} + +static int +EncodeInt32LE(char *output, int nVal) +{ + output[0] = nVal; + nVal >>= 8; + output[1] = nVal; + nVal >>= 8; + output[2] = nVal; + nVal >>= 8; + output[3] = nVal; + return 4; +} + +int +RTMP_ReadPacket(RTMP *r, RTMPPacket *packet) +{ + uint8_t hbuf[RTMP_MAX_HEADER_SIZE] = { 0 }; + char *header = (char *)hbuf; + int nSize, hSize, nToRead, nChunk; + int didAlloc = FALSE; + int extendedTimestamp; + + RTMP_Log(RTMP_LOGDEBUG2, "%s: fd=%d", __FUNCTION__, r->m_sb.sb_socket); + + if (ReadN(r, (char *)hbuf, 1) == 0) + { + RTMP_Log(RTMP_LOGERROR, "%s, failed to read RTMP packet header", __FUNCTION__); + return FALSE; + } + + packet->m_headerType = (hbuf[0] & 0xc0) >> 6; + packet->m_nChannel = (hbuf[0] & 0x3f); + header++; + if (packet->m_nChannel == 0) + { + if (ReadN(r, (char *)&hbuf[1], 1) != 1) + { + RTMP_Log(RTMP_LOGERROR, "%s, failed to read RTMP packet header 2nd byte", + __FUNCTION__); + return FALSE; + } + packet->m_nChannel = hbuf[1]; + packet->m_nChannel += 64; + header++; + } + else if (packet->m_nChannel == 1) + { + int tmp; + if (ReadN(r, (char *)&hbuf[1], 2) != 2) + { + RTMP_Log(RTMP_LOGERROR, "%s, failed to read RTMP packet header 3nd byte", + __FUNCTION__); + return FALSE; + } + tmp = (hbuf[2] << 8) + hbuf[1]; + packet->m_nChannel = tmp + 64; + RTMP_Log(RTMP_LOGDEBUG, "%s, m_nChannel: %0x", __FUNCTION__, packet->m_nChannel); + header += 2; + } + + nSize = packetSize[packet->m_headerType]; + + if (packet->m_nChannel >= r->m_channelsAllocatedIn) + { + int n = packet->m_nChannel + 10; + int *timestamp = realloc(r->m_channelTimestamp, sizeof(int) * n); + RTMPPacket **packets = realloc(r->m_vecChannelsIn, sizeof(RTMPPacket*) * n); + if (!timestamp) + free(r->m_channelTimestamp); + if (!packets) + free(r->m_vecChannelsIn); + r->m_channelTimestamp = timestamp; + r->m_vecChannelsIn = packets; + if (!timestamp || !packets) { + r->m_channelsAllocatedIn = 0; + return FALSE; + } + memset(r->m_channelTimestamp + r->m_channelsAllocatedIn, 0, sizeof(int) * (n - r->m_channelsAllocatedIn)); + memset(r->m_vecChannelsIn + r->m_channelsAllocatedIn, 0, sizeof(RTMPPacket*) * (n - r->m_channelsAllocatedIn)); + r->m_channelsAllocatedIn = n; + } + + if (nSize == RTMP_LARGE_HEADER_SIZE) /* if we get a full header the timestamp is absolute */ + packet->m_hasAbsTimestamp = TRUE; + + else if (nSize < RTMP_LARGE_HEADER_SIZE) + { /* using values from the last message of this channel */ + if (r->m_vecChannelsIn[packet->m_nChannel]) + memcpy(packet, r->m_vecChannelsIn[packet->m_nChannel], + sizeof(RTMPPacket)); + } + + nSize--; + + if (nSize > 0 && ReadN(r, header, nSize) != nSize) + { + RTMP_Log(RTMP_LOGERROR, "%s, failed to read RTMP packet header. type: %x", + __FUNCTION__, (unsigned int)hbuf[0]); + return FALSE; + } + + hSize = nSize + (header - (char *)hbuf); + + if (nSize >= 3) + { + packet->m_nTimeStamp = AMF_DecodeInt24(header); + + /*RTMP_Log(RTMP_LOGDEBUG, "%s, reading RTMP packet chunk on channel %x, headersz %i, timestamp %i, abs timestamp %i", __FUNCTION__, packet.m_nChannel, nSize, packet.m_nTimeStamp, packet.m_hasAbsTimestamp); */ + + if (nSize >= 6) + { + packet->m_nBodySize = AMF_DecodeInt24(header + 3); + packet->m_nBytesRead = 0; + + if (nSize > 6) + { + packet->m_packetType = header[6]; + + if (nSize == 11) + packet->m_nInfoField2 = DecodeInt32LE(header + 7); + } + } + } + + extendedTimestamp = packet->m_nTimeStamp == 0xffffff; + if (extendedTimestamp) + { + if (ReadN(r, header + nSize, 4) != 4) + { + RTMP_Log(RTMP_LOGERROR, "%s, failed to read extended timestamp", + __FUNCTION__); + return FALSE; + } + packet->m_nTimeStamp = AMF_DecodeInt32(header + nSize); + hSize += 4; + } + + RTMP_LogHexString(RTMP_LOGDEBUG2, (uint8_t *)hbuf, hSize); + + if (packet->m_nBodySize > 0 && packet->m_body == NULL) + { + if (!RTMPPacket_Alloc(packet, packet->m_nBodySize)) + { + RTMP_Log(RTMP_LOGDEBUG, "%s, failed to allocate packet", __FUNCTION__); + return FALSE; + } + didAlloc = TRUE; + packet->m_headerType = (hbuf[0] & 0xc0) >> 6; + } + + nToRead = packet->m_nBodySize - packet->m_nBytesRead; + nChunk = r->m_inChunkSize; + if (nToRead < nChunk) + nChunk = nToRead; + + /* Does the caller want the raw chunk? */ + if (packet->m_chunk) + { + packet->m_chunk->c_headerSize = hSize; + memcpy(packet->m_chunk->c_header, hbuf, hSize); + packet->m_chunk->c_chunk = packet->m_body + packet->m_nBytesRead; + packet->m_chunk->c_chunkSize = nChunk; + } + + if (ReadN(r, packet->m_body + packet->m_nBytesRead, nChunk) != nChunk) + { + RTMP_Log(RTMP_LOGERROR, "%s, failed to read RTMP packet body. len: %u", + __FUNCTION__, packet->m_nBodySize); + return FALSE; + } + + RTMP_LogHexString(RTMP_LOGDEBUG2, (uint8_t *)packet->m_body + packet->m_nBytesRead, nChunk); + + packet->m_nBytesRead += nChunk; + + /* keep the packet as ref for other packets on this channel */ + if (!r->m_vecChannelsIn[packet->m_nChannel]) + r->m_vecChannelsIn[packet->m_nChannel] = malloc(sizeof(RTMPPacket)); + memcpy(r->m_vecChannelsIn[packet->m_nChannel], packet, sizeof(RTMPPacket)); + if (extendedTimestamp) + { + r->m_vecChannelsIn[packet->m_nChannel]->m_nTimeStamp = 0xffffff; + } + + if (RTMPPacket_IsReady(packet)) + { + /* make packet's timestamp absolute */ + if (!packet->m_hasAbsTimestamp) + packet->m_nTimeStamp += r->m_channelTimestamp[packet->m_nChannel]; /* timestamps seem to be always relative!! */ + + r->m_channelTimestamp[packet->m_nChannel] = packet->m_nTimeStamp; + + /* reset the data from the stored packet. we keep the header since we may use it later if a new packet for this channel */ + /* arrives and requests to re-use some info (small packet header) */ + r->m_vecChannelsIn[packet->m_nChannel]->m_body = NULL; + r->m_vecChannelsIn[packet->m_nChannel]->m_nBytesRead = 0; + r->m_vecChannelsIn[packet->m_nChannel]->m_hasAbsTimestamp = FALSE; /* can only be false if we reuse header */ + } + else + { + packet->m_body = NULL; /* so it won't be erased on free */ + } + + // @remark debug info by http://github.com/ossrs/srs + if (packet->m_packetType == 8 || packet->m_packetType == 9) { + _srs_state = 3; + } + + return TRUE; +} + +#ifndef CRYPTO +static int +HandShake(RTMP *r, int FP9HandShake) +{ + int i; + uint32_t uptime, suptime; + int bMatch; + char type; + char clientbuf[RTMP_SIG_SIZE + 1], *clientsig = clientbuf + 1; + char serversig[RTMP_SIG_SIZE]; + + clientbuf[0] = 0x03; /* not encrypted */ + + uptime = htonl(RTMP_GetTime()); + memcpy(clientsig, &uptime, 4); + + memset(&clientsig[4], 0, 4); + +#ifdef _DEBUG + for (i = 8; i < RTMP_SIG_SIZE; i++) + clientsig[i] = 0xff; +#else + for (i = 8; i < RTMP_SIG_SIZE; i++) + clientsig[i] = (char)(rand() % 256); +#endif + + if (!WriteN(r, clientbuf, RTMP_SIG_SIZE + 1)) + return FALSE; + + if (ReadN(r, &type, 1) != 1) /* 0x03 or 0x06 */ + return FALSE; + + RTMP_Log(RTMP_LOGDEBUG, "%s: Type Answer : %02X", __FUNCTION__, type); + + if (type != clientbuf[0]) + RTMP_Log(RTMP_LOGWARNING, "%s: Type mismatch: client sent %d, server answered %d", + __FUNCTION__, clientbuf[0], type); + + if (ReadN(r, serversig, RTMP_SIG_SIZE) != RTMP_SIG_SIZE) + return FALSE; + + /* decode server response */ + + memcpy(&suptime, serversig, 4); + suptime = ntohl(suptime); + + RTMP_Log(RTMP_LOGDEBUG, "%s: Server Uptime : %d", __FUNCTION__, suptime); + RTMP_Log(RTMP_LOGDEBUG, "%s: FMS Version : %d.%d.%d.%d", __FUNCTION__, + serversig[4], serversig[5], serversig[6], serversig[7]); + + /* 2nd part of handshake */ + if (!WriteN(r, serversig, RTMP_SIG_SIZE)) + return FALSE; + + if (ReadN(r, serversig, RTMP_SIG_SIZE) != RTMP_SIG_SIZE) + return FALSE; + + bMatch = (memcmp(serversig, clientsig, RTMP_SIG_SIZE) == 0); + if (!bMatch) + { + RTMP_Log(RTMP_LOGWARNING, "%s, client signature does not match!", __FUNCTION__); + } + return TRUE; +} + +static int +SHandShake(RTMP *r) +{ + int i; + char serverbuf[RTMP_SIG_SIZE + 1], *serversig = serverbuf + 1; + char clientsig[RTMP_SIG_SIZE]; + uint32_t uptime; + int bMatch; + + if (ReadN(r, serverbuf, 1) != 1) /* 0x03 or 0x06 */ + return FALSE; + + RTMP_Log(RTMP_LOGDEBUG, "%s: Type Request : %02X", __FUNCTION__, serverbuf[0]); + + if (serverbuf[0] != 3) + { + RTMP_Log(RTMP_LOGERROR, "%s: Type unknown: client sent %02X", + __FUNCTION__, serverbuf[0]); + return FALSE; + } + + uptime = htonl(RTMP_GetTime()); + memcpy(serversig, &uptime, 4); + + memset(&serversig[4], 0, 4); +#ifdef _DEBUG + for (i = 8; i < RTMP_SIG_SIZE; i++) + serversig[i] = 0xff; +#else + for (i = 8; i < RTMP_SIG_SIZE; i++) + serversig[i] = (char)(rand() % 256); +#endif + + if (!WriteN(r, serverbuf, RTMP_SIG_SIZE + 1)) + return FALSE; + + if (ReadN(r, clientsig, RTMP_SIG_SIZE) != RTMP_SIG_SIZE) + return FALSE; + + /* decode client response */ + + memcpy(&uptime, clientsig, 4); + uptime = ntohl(uptime); + + RTMP_Log(RTMP_LOGDEBUG, "%s: Client Uptime : %d", __FUNCTION__, uptime); + RTMP_Log(RTMP_LOGDEBUG, "%s: Player Version: %d.%d.%d.%d", __FUNCTION__, + clientsig[4], clientsig[5], clientsig[6], clientsig[7]); + + /* 2nd part of handshake */ + if (!WriteN(r, clientsig, RTMP_SIG_SIZE)) + return FALSE; + + if (ReadN(r, clientsig, RTMP_SIG_SIZE) != RTMP_SIG_SIZE) + return FALSE; + + bMatch = (memcmp(serversig, clientsig, RTMP_SIG_SIZE) == 0); + if (!bMatch) + { + RTMP_Log(RTMP_LOGWARNING, "%s, client signature does not match!", __FUNCTION__); + } + return TRUE; +} +#endif + +int +RTMP_SendChunk(RTMP *r, RTMPChunk *chunk) +{ + int wrote; + char hbuf[RTMP_MAX_HEADER_SIZE]; + + RTMP_Log(RTMP_LOGDEBUG2, "%s: fd=%d, size=%d", __FUNCTION__, r->m_sb.sb_socket, + chunk->c_chunkSize); + RTMP_LogHexString(RTMP_LOGDEBUG2, (uint8_t *)chunk->c_header, chunk->c_headerSize); + if (chunk->c_chunkSize) + { + char *ptr = chunk->c_chunk - chunk->c_headerSize; + RTMP_LogHexString(RTMP_LOGDEBUG2, (uint8_t *)chunk->c_chunk, chunk->c_chunkSize); + /* save header bytes we're about to overwrite */ + memcpy(hbuf, ptr, chunk->c_headerSize); + memcpy(ptr, chunk->c_header, chunk->c_headerSize); + wrote = WriteN(r, ptr, chunk->c_headerSize + chunk->c_chunkSize); + memcpy(ptr, hbuf, chunk->c_headerSize); + } + else + wrote = WriteN(r, chunk->c_header, chunk->c_headerSize); + return wrote; +} + +int +RTMP_SendPacket(RTMP *r, RTMPPacket *packet, int queue) +{ + const RTMPPacket *prevPacket; + uint32_t last = 0; + int nSize; + int hSize, cSize; + char *header, *hptr, *hend, hbuf[RTMP_MAX_HEADER_SIZE], c; + uint32_t t; + char *buffer, *tbuf = NULL, *toff = NULL; + int nChunkSize; + int tlen; + + // @remark debug info by http://github.com/ossrs/srs + if (packet->m_packetType == 8 || packet->m_packetType == 9) { + _srs_state = 3; + } + + + if (packet->m_nChannel >= r->m_channelsAllocatedOut) + { + int n = packet->m_nChannel + 10; + RTMPPacket **packets = realloc(r->m_vecChannelsOut, sizeof(RTMPPacket*) * n); + if (!packets) { + free(r->m_vecChannelsOut); + r->m_vecChannelsOut = NULL; + r->m_channelsAllocatedOut = 0; + return FALSE; + } + r->m_vecChannelsOut = packets; + memset(r->m_vecChannelsOut + r->m_channelsAllocatedOut, 0, sizeof(RTMPPacket*) * (n - r->m_channelsAllocatedOut)); + r->m_channelsAllocatedOut = n; + } + + prevPacket = r->m_vecChannelsOut[packet->m_nChannel]; + if (prevPacket && packet->m_headerType != RTMP_PACKET_SIZE_LARGE) + { + /* compress a bit by using the prev packet's attributes */ + if (prevPacket->m_nBodySize == packet->m_nBodySize + && prevPacket->m_packetType == packet->m_packetType + && packet->m_headerType == RTMP_PACKET_SIZE_MEDIUM) + packet->m_headerType = RTMP_PACKET_SIZE_SMALL; + + if (prevPacket->m_nTimeStamp == packet->m_nTimeStamp + && packet->m_headerType == RTMP_PACKET_SIZE_SMALL) + packet->m_headerType = RTMP_PACKET_SIZE_MINIMUM; + last = prevPacket->m_nTimeStamp; + } + + if (packet->m_headerType > 3) /* sanity */ + { + RTMP_Log(RTMP_LOGERROR, "sanity failed!! trying to send header of type: 0x%02x.", + (unsigned char)packet->m_headerType); + return FALSE; + } + + nSize = packetSize[packet->m_headerType]; + hSize = nSize; cSize = 0; + t = packet->m_nTimeStamp - last; + + if (packet->m_body) + { + header = packet->m_body - nSize; + hend = packet->m_body; + } + else + { + header = hbuf + 6; + hend = hbuf + sizeof(hbuf); + } + + if (packet->m_nChannel > 319) + cSize = 2; + else if (packet->m_nChannel > 63) + cSize = 1; + if (cSize) + { + header -= cSize; + hSize += cSize; + } + + if (t >= 0xffffff) + { + header -= 4; + hSize += 4; + RTMP_Log(RTMP_LOGWARNING, "Larger timestamp than 24-bit: 0x%x", t); + } + + hptr = header; + c = packet->m_headerType << 6; + switch (cSize) + { + case 0: + c |= packet->m_nChannel; + break; + case 1: + break; + case 2: + c |= 1; + break; + } + *hptr++ = c; + if (cSize) + { + int tmp = packet->m_nChannel - 64; + *hptr++ = tmp & 0xff; + if (cSize == 2) + *hptr++ = tmp >> 8; + } + + if (nSize > 1) + { + hptr = AMF_EncodeInt24(hptr, hend, t > 0xffffff ? 0xffffff : t); + } + + if (nSize > 4) + { + hptr = AMF_EncodeInt24(hptr, hend, packet->m_nBodySize); + *hptr++ = packet->m_packetType; + } + + if (nSize > 8) + hptr += EncodeInt32LE(hptr, packet->m_nInfoField2); + + if (t >= 0xffffff) + hptr = AMF_EncodeInt32(hptr, hend, t); + + nSize = packet->m_nBodySize; + buffer = packet->m_body; + nChunkSize = r->m_outChunkSize; + + RTMP_Log(RTMP_LOGDEBUG2, "%s: fd=%d, size=%d", __FUNCTION__, r->m_sb.sb_socket, + nSize); + /* send all chunks in one HTTP request */ + if (r->Link.protocol & RTMP_FEATURE_HTTP) + { + int chunks = (nSize+nChunkSize-1) / nChunkSize; + if (chunks > 1) + { + tlen = chunks * (cSize + 1) + nSize + hSize; + tbuf = malloc(tlen); + if (!tbuf) + return FALSE; + toff = tbuf; + } + } + while (nSize + hSize) + { + int wrote; + + if (nSize < nChunkSize) + nChunkSize = nSize; + + RTMP_LogHexString(RTMP_LOGDEBUG2, (uint8_t *)header, hSize); + RTMP_LogHexString(RTMP_LOGDEBUG2, (uint8_t *)buffer, nChunkSize); + if (tbuf) + { + memcpy(toff, header, nChunkSize + hSize); + toff += nChunkSize + hSize; + } + else + { + wrote = WriteN(r, header, nChunkSize + hSize); + if (!wrote) + return FALSE; + } + nSize -= nChunkSize; + buffer += nChunkSize; + hSize = 0; + + if (nSize > 0) + { + header = buffer - 1; + hSize = 1; + if (cSize) + { + header -= cSize; + hSize += cSize; + } + if (t >= 0xffffff) + { + header -= 4; + hSize += 4; + } + *header = (0xc0 | c); + if (cSize) + { + int tmp = packet->m_nChannel - 64; + header[1] = tmp & 0xff; + if (cSize == 2) + header[2] = tmp >> 8; + } + if (t >= 0xffffff) + { + char* extendedTimestamp = header + 1 + cSize; + AMF_EncodeInt32(extendedTimestamp, extendedTimestamp + 4, t); + } + } + } + if (tbuf) + { + int wrote = WriteN(r, tbuf, toff-tbuf); + free(tbuf); + tbuf = NULL; + if (!wrote) + return FALSE; + } + + /* we invoked a remote method */ + if (packet->m_packetType == RTMP_PACKET_TYPE_INVOKE) + { + AVal method; + char *ptr; + ptr = packet->m_body + 1; + AMF_DecodeString(ptr, &method); + RTMP_Log(RTMP_LOGDEBUG, "Invoking %s", method.av_val); + /* keep it in call queue till result arrives */ + if (queue) { + int txn; + ptr += 3 + method.av_len; + txn = (int)AMF_DecodeNumber(ptr); + AV_queue(&r->m_methodCalls, &r->m_numCalls, &method, txn); + } + } + + if (!r->m_vecChannelsOut[packet->m_nChannel]) + r->m_vecChannelsOut[packet->m_nChannel] = malloc(sizeof(RTMPPacket)); + memcpy(r->m_vecChannelsOut[packet->m_nChannel], packet, sizeof(RTMPPacket)); + return TRUE; +} + +int +RTMP_Serve(RTMP *r) +{ + return SHandShake(r); +} + +void +RTMP_Close(RTMP *r) +{ + CloseInternal(r, 0); + + // @remark debug info by http://github.com/ossrs/srs + _srs_state = 4; +} + +static void +CloseInternal(RTMP *r, int reconnect) +{ + int i; + + if (RTMP_IsConnected(r)) + { + if (r->m_stream_id > 0) + { + i = r->m_stream_id; + r->m_stream_id = 0; + if ((r->Link.protocol & RTMP_FEATURE_WRITE)) + SendFCUnpublish(r); + SendDeleteStream(r, i); + } + if (r->m_clientID.av_val) + { + HTTP_Post(r, RTMPT_CLOSE, "", 1); + free(r->m_clientID.av_val); + r->m_clientID.av_val = NULL; + r->m_clientID.av_len = 0; + } + RTMPSockBuf_Close(&r->m_sb); + } + + r->m_stream_id = -1; + r->m_sb.sb_socket = -1; + r->m_nBWCheckCounter = 0; + r->m_nBytesIn = 0; + r->m_nBytesInSent = 0; + + if (r->m_read.flags & RTMP_READ_HEADER) { + free(r->m_read.buf); + r->m_read.buf = NULL; + } + r->m_read.dataType = 0; + r->m_read.flags = 0; + r->m_read.status = 0; + r->m_read.nResumeTS = 0; + r->m_read.nIgnoredFrameCounter = 0; + r->m_read.nIgnoredFlvFrameCounter = 0; + + r->m_write.m_nBytesRead = 0; + RTMPPacket_Free(&r->m_write); + + for (i = 0; i < r->m_channelsAllocatedIn; i++) + { + if (r->m_vecChannelsIn[i]) + { + RTMPPacket_Free(r->m_vecChannelsIn[i]); + free(r->m_vecChannelsIn[i]); + r->m_vecChannelsIn[i] = NULL; + } + } + free(r->m_vecChannelsIn); + r->m_vecChannelsIn = NULL; + free(r->m_channelTimestamp); + r->m_channelTimestamp = NULL; + r->m_channelsAllocatedIn = 0; + for (i = 0; i < r->m_channelsAllocatedOut; i++) + { + if (r->m_vecChannelsOut[i]) + { + free(r->m_vecChannelsOut[i]); + r->m_vecChannelsOut[i] = NULL; + } + } + free(r->m_vecChannelsOut); + r->m_vecChannelsOut = NULL; + r->m_channelsAllocatedOut = 0; + AV_clear(r->m_methodCalls, r->m_numCalls); + r->m_methodCalls = NULL; + r->m_numCalls = 0; + r->m_numInvokes = 0; + + r->m_bPlaying = FALSE; + r->m_sb.sb_size = 0; + + r->m_msgCounter = 0; + r->m_resplen = 0; + r->m_unackd = 0; + + if (r->Link.lFlags & RTMP_LF_FTCU && !reconnect) + { + free(r->Link.tcUrl.av_val); + r->Link.tcUrl.av_val = NULL; + r->Link.lFlags ^= RTMP_LF_FTCU; + } + if (r->Link.lFlags & RTMP_LF_FAPU && !reconnect) + { + free(r->Link.app.av_val); + r->Link.app.av_val = NULL; + r->Link.lFlags ^= RTMP_LF_FAPU; + } + + if (!reconnect) + { + free(r->Link.playpath0.av_val); + r->Link.playpath0.av_val = NULL; + } +#ifdef CRYPTO + if (r->Link.dh) + { + MDH_free(r->Link.dh); + r->Link.dh = NULL; + } + if (r->Link.rc4keyIn) + { + RC4_free(r->Link.rc4keyIn); + r->Link.rc4keyIn = NULL; + } + if (r->Link.rc4keyOut) + { + RC4_free(r->Link.rc4keyOut); + r->Link.rc4keyOut = NULL; + } +#endif +} + +int +RTMPSockBuf_Fill(RTMPSockBuf *sb) +{ + int nBytes; + + if (!sb->sb_size) + sb->sb_start = sb->sb_buf; + + while (1) + { + nBytes = sizeof(sb->sb_buf) - 1 - sb->sb_size - (sb->sb_start - sb->sb_buf); +#if defined(CRYPTO) && !defined(NO_SSL) + if (sb->sb_ssl) + { + nBytes = TLS_read(sb->sb_ssl, sb->sb_start + sb->sb_size, nBytes); + } + else +#endif + { + nBytes = recv(sb->sb_socket, sb->sb_start + sb->sb_size, nBytes, 0); + } + if (nBytes != -1) + { + sb->sb_size += nBytes; + } + else + { + int sockerr = GetSockError(); + RTMP_Log(RTMP_LOGDEBUG, "%s, recv returned %d. GetSockError(): %d (%s)", + __FUNCTION__, nBytes, sockerr, strerror(sockerr)); + if (sockerr == EINTR && !RTMP_ctrlC) + continue; + + if (sockerr == EWOULDBLOCK || sockerr == EAGAIN) + { + sb->sb_timedout = TRUE; + nBytes = 0; + } + } + break; + } + + return nBytes; +} + +int +RTMPSockBuf_Send(RTMPSockBuf *sb, const char *buf, int len) +{ + int rc; + +#ifdef _DEBUG +// fwrite(buf, 1, len, netstackdump); +#endif + +#if defined(CRYPTO) && !defined(NO_SSL) + if (sb->sb_ssl) + { + rc = TLS_write(sb->sb_ssl, buf, len); + } + else +#endif + { + rc = send(sb->sb_socket, buf, len, 0); + } + return rc; +} + +int +RTMPSockBuf_Close(RTMPSockBuf *sb) +{ +#if defined(CRYPTO) && !defined(NO_SSL) + if (sb->sb_ssl) + { + TLS_shutdown(sb->sb_ssl); + TLS_close(sb->sb_ssl); + sb->sb_ssl = NULL; + } +#endif + if (sb->sb_socket != -1) + return closesocket(sb->sb_socket); + return 0; +} + +#define HEX2BIN(a) (((a)&0x40)?((a)&0xf)+9:((a)&0xf)) + +static void +DecodeTEA(AVal *key, AVal *text) +{ + uint32_t *v, k[4] = { 0 }, u; + uint32_t z, y, sum = 0, e, DELTA = 0x9e3779b9; + int32_t p, q; + int i, n; + unsigned char *ptr, *out; + + /* prep key: pack 1st 16 chars into 4 LittleEndian ints */ + ptr = (unsigned char *)key->av_val; + u = 0; + n = 0; + v = k; + p = key->av_len > 16 ? 16 : key->av_len; + for (i = 0; i < p; i++) + { + u |= ptr[i] << (n * 8); + if (n == 3) + { + *v++ = u; + u = 0; + n = 0; + } + else + { + n++; + } + } + /* any trailing chars */ + if (u) + *v = u; + + /* prep text: hex2bin, multiples of 4 */ + n = (text->av_len + 7) / 8; + out = malloc(n * 8); + ptr = (unsigned char *)text->av_val; + v = (uint32_t *) out; + for (i = 0; i < n; i++) + { + u = (HEX2BIN(ptr[0]) << 4) + HEX2BIN(ptr[1]); + u |= ((HEX2BIN(ptr[2]) << 4) + HEX2BIN(ptr[3])) << 8; + u |= ((HEX2BIN(ptr[4]) << 4) + HEX2BIN(ptr[5])) << 16; + u |= ((HEX2BIN(ptr[6]) << 4) + HEX2BIN(ptr[7])) << 24; + *v++ = u; + ptr += 8; + } + v = (uint32_t *) out; + + /* http://www.movable-type.co.uk/scripts/tea-block.html */ +#define MX (((z>>5)^(y<<2)) + ((y>>3)^(z<<4))) ^ ((sum^y) + (k[(p&3)^e]^z)); + z = v[n - 1]; + y = v[0]; + q = 6 + 52 / n; + sum = q * DELTA; + while (sum != 0) + { + e = sum >> 2 & 3; + for (p = n - 1; p > 0; p--) + z = v[p - 1], y = v[p] -= MX; + z = v[n - 1]; + y = v[0] -= MX; + sum -= DELTA; + } + + text->av_len /= 2; + memcpy(text->av_val, out, text->av_len); + free(out); +} + +static int +HTTP_Post(RTMP *r, RTMPTCmd cmd, const char *buf, int len) +{ + char hbuf[512]; + int hlen = snprintf(hbuf, sizeof(hbuf), "POST /%s%s/%d HTTP/1.1\r\n" + "Host: %.*s:%d\r\n" + "Accept: */*\r\n" + "User-Agent: Shockwave Flash\r\n" + "Connection: Keep-Alive\r\n" + "Cache-Control: no-cache\r\n" + "Content-type: application/x-fcs\r\n" + "Content-length: %d\r\n\r\n", RTMPT_cmds[cmd], + r->m_clientID.av_val ? r->m_clientID.av_val : "", + r->m_msgCounter, r->Link.hostname.av_len, r->Link.hostname.av_val, + r->Link.port, len); + RTMPSockBuf_Send(&r->m_sb, hbuf, hlen); + hlen = RTMPSockBuf_Send(&r->m_sb, buf, len); + r->m_msgCounter++; + r->m_unackd++; + return hlen; +} + +static int +HTTP_read(RTMP *r, int fill) +{ + char *ptr; + int hlen; + +restart: + if (fill) + RTMPSockBuf_Fill(&r->m_sb); + if (r->m_sb.sb_size < 13) { + if (fill) + goto restart; + return -2; + } + if (strncmp(r->m_sb.sb_start, "HTTP/1.1 200 ", 13)) + return -1; + r->m_sb.sb_start[r->m_sb.sb_size] = '\0'; + if (!strstr(r->m_sb.sb_start, "\r\n\r\n")) { + if (fill) + goto restart; + return -2; + } + + ptr = r->m_sb.sb_start + sizeof("HTTP/1.1 200"); + while ((ptr = strstr(ptr, "Content-"))) { + if (!strncasecmp(ptr+8, "length:", 7)) break; + ptr += 8; + } + if (!ptr) + return -1; + hlen = atoi(ptr+16); + ptr = strstr(ptr+16, "\r\n\r\n"); + if (!ptr) + return -1; + ptr += 4; + if (ptr + (r->m_clientID.av_val ? 1 : hlen) > r->m_sb.sb_start + r->m_sb.sb_size) + { + if (fill) + goto restart; + return -2; + } + r->m_sb.sb_size -= ptr - r->m_sb.sb_start; + r->m_sb.sb_start = ptr; + r->m_unackd--; + + if (!r->m_clientID.av_val) + { + r->m_clientID.av_len = hlen; + r->m_clientID.av_val = malloc(hlen+1); + if (!r->m_clientID.av_val) + return -1; + r->m_clientID.av_val[0] = '/'; + memcpy(r->m_clientID.av_val+1, ptr, hlen-1); + r->m_clientID.av_val[hlen] = 0; + r->m_sb.sb_size = 0; + } + else + { + r->m_polling = *ptr++; + r->m_resplen = hlen - 1; + r->m_sb.sb_start++; + r->m_sb.sb_size--; + } + return 0; +} + +#define MAX_IGNORED_FRAMES 50 + +/* Read from the stream until we get a media packet. + * Returns -3 if Play.Close/Stop, -2 if fatal error, -1 if no more media + * packets, 0 if ignorable error, >0 if there is a media packet + */ +static int +Read_1_Packet(RTMP *r, char *buf, unsigned int buflen) +{ + uint32_t prevTagSize = 0; + int rtnGetNextMediaPacket = 0, ret = RTMP_READ_EOF; + RTMPPacket packet = { 0 }; + int recopy = FALSE; + unsigned int size; + char *ptr, *pend; + uint32_t nTimeStamp = 0; + unsigned int len; + + rtnGetNextMediaPacket = RTMP_GetNextMediaPacket(r, &packet); + while (rtnGetNextMediaPacket) + { + char *packetBody = packet.m_body; + unsigned int nPacketLen = packet.m_nBodySize; + + /* Return RTMP_READ_COMPLETE if this was completed nicely with + * invoke message Play.Stop or Play.Complete + */ + if (rtnGetNextMediaPacket == 2) + { + RTMP_Log(RTMP_LOGDEBUG, + "Got Play.Complete or Play.Stop from server. " + "Assuming stream is complete"); + ret = RTMP_READ_COMPLETE; + break; + } + + r->m_read.dataType |= (((packet.m_packetType == RTMP_PACKET_TYPE_AUDIO) << 2) | + (packet.m_packetType == RTMP_PACKET_TYPE_VIDEO)); + + if (packet.m_packetType == RTMP_PACKET_TYPE_VIDEO && nPacketLen <= 5) + { + RTMP_Log(RTMP_LOGDEBUG, "ignoring too small video packet: size: %d", + nPacketLen); + ret = RTMP_READ_IGNORE; + break; + } + if (packet.m_packetType == RTMP_PACKET_TYPE_AUDIO && nPacketLen <= 1) + { + RTMP_Log(RTMP_LOGDEBUG, "ignoring too small audio packet: size: %d", + nPacketLen); + ret = RTMP_READ_IGNORE; + break; + } + + if (r->m_read.flags & RTMP_READ_SEEKING) + { + ret = RTMP_READ_IGNORE; + break; + } +#ifdef _DEBUG + RTMP_Log(RTMP_LOGDEBUG, "type: %02X, size: %d, TS: %d ms, abs TS: %d", + packet.m_packetType, nPacketLen, packet.m_nTimeStamp, + packet.m_hasAbsTimestamp); + if (packet.m_packetType == RTMP_PACKET_TYPE_VIDEO) + RTMP_Log(RTMP_LOGDEBUG, "frametype: %02X", (*packetBody & 0xf0)); +#endif + + if (r->m_read.flags & RTMP_READ_RESUME) + { + /* check the header if we get one */ + if (packet.m_nTimeStamp == 0) + { + if (r->m_read.nMetaHeaderSize > 0 + && packet.m_packetType == RTMP_PACKET_TYPE_INFO) + { + AMFObject metaObj; + int nRes = + AMF_Decode(&metaObj, packetBody, nPacketLen, FALSE); + if (nRes >= 0) + { + AVal metastring; + AMFProp_GetString(AMF_GetProp(&metaObj, NULL, 0), + &metastring); + + if (AVMATCH(&metastring, &av_onMetaData)) + { + /* compare */ + if ((r->m_read.nMetaHeaderSize != nPacketLen) || + (memcmp + (r->m_read.metaHeader, packetBody, + r->m_read.nMetaHeaderSize) != 0)) + { + ret = RTMP_READ_ERROR; + } + } + AMF_Reset(&metaObj); + if (ret == RTMP_READ_ERROR) + break; + } + } + + /* check first keyframe to make sure we got the right position + * in the stream! (the first non ignored frame) + */ + if (r->m_read.nInitialFrameSize > 0) + { + /* video or audio data */ + if (packet.m_packetType == r->m_read.initialFrameType + && r->m_read.nInitialFrameSize == nPacketLen) + { + /* we don't compare the sizes since the packet can + * contain several FLV packets, just make sure the + * first frame is our keyframe (which we are going + * to rewrite) + */ + if (memcmp + (r->m_read.initialFrame, packetBody, + r->m_read.nInitialFrameSize) == 0) + { + RTMP_Log(RTMP_LOGDEBUG, "Checked keyframe successfully!"); + r->m_read.flags |= RTMP_READ_GOTKF; + /* ignore it! (what about audio data after it? it is + * handled by ignoring all 0ms frames, see below) + */ + ret = RTMP_READ_IGNORE; + break; + } + } + + /* hande FLV streams, even though the server resends the + * keyframe as an extra video packet it is also included + * in the first FLV stream chunk and we have to compare + * it and filter it out !! + */ + if (packet.m_packetType == RTMP_PACKET_TYPE_FLASH_VIDEO) + { + /* basically we have to find the keyframe with the + * correct TS being nResumeTS + */ + unsigned int pos = 0; + uint32_t ts = 0; + + while (pos + 11 < nPacketLen) + { + /* size without header (11) and prevTagSize (4) */ + uint32_t dataSize = + AMF_DecodeInt24(packetBody + pos + 1); + ts = AMF_DecodeInt24(packetBody + pos + 4); + ts |= (packetBody[pos + 7] << 24); + +#ifdef _DEBUG + RTMP_Log(RTMP_LOGDEBUG, + "keyframe search: FLV Packet: type %02X, dataSize: %d, timeStamp: %d ms", + packetBody[pos], dataSize, ts); +#endif + /* ok, is it a keyframe?: + * well doesn't work for audio! + */ + if (packetBody[pos /*6928, test 0 */ ] == + r->m_read.initialFrameType + /* && (packetBody[11]&0xf0) == 0x10 */ ) + { + if (ts == r->m_read.nResumeTS) + { + RTMP_Log(RTMP_LOGDEBUG, + "Found keyframe with resume-keyframe timestamp!"); + if (r->m_read.nInitialFrameSize != dataSize + || memcmp(r->m_read.initialFrame, + packetBody + pos + 11, + r->m_read. + nInitialFrameSize) != 0) + { + RTMP_Log(RTMP_LOGERROR, + "FLV Stream: Keyframe doesn't match!"); + ret = RTMP_READ_ERROR; + break; + } + r->m_read.flags |= RTMP_READ_GOTFLVK; + + /* skip this packet? + * check whether skippable: + */ + if (pos + 11 + dataSize + 4 > nPacketLen) + { + RTMP_Log(RTMP_LOGWARNING, + "Non skipable packet since it doesn't end with chunk, stream corrupt!"); + ret = RTMP_READ_ERROR; + break; + } + packetBody += (pos + 11 + dataSize + 4); + nPacketLen -= (pos + 11 + dataSize + 4); + + goto stopKeyframeSearch; + + } + else if (r->m_read.nResumeTS < ts) + { + /* the timestamp ts will only increase with + * further packets, wait for seek + */ + goto stopKeyframeSearch; + } + } + pos += (11 + dataSize + 4); + } + if (ts < r->m_read.nResumeTS) + { + RTMP_Log(RTMP_LOGERROR, + "First packet does not contain keyframe, all " + "timestamps are smaller than the keyframe " + "timestamp; probably the resume seek failed?"); + } + stopKeyframeSearch: + ; + if (!(r->m_read.flags & RTMP_READ_GOTFLVK)) + { + RTMP_Log(RTMP_LOGERROR, + "Couldn't find the seeked keyframe in this chunk!"); + ret = RTMP_READ_IGNORE; + break; + } + } + } + } + + if (packet.m_nTimeStamp > 0 + && (r->m_read.flags & (RTMP_READ_GOTKF|RTMP_READ_GOTFLVK))) + { + /* another problem is that the server can actually change from + * 09/08 video/audio packets to an FLV stream or vice versa and + * our keyframe check will prevent us from going along with the + * new stream if we resumed. + * + * in this case set the 'found keyframe' variables to true. + * We assume that if we found one keyframe somewhere and were + * already beyond TS > 0 we have written data to the output + * which means we can accept all forthcoming data including the + * change between 08/09 <-> FLV packets + */ + r->m_read.flags |= (RTMP_READ_GOTKF|RTMP_READ_GOTFLVK); + } + + /* skip till we find our keyframe + * (seeking might put us somewhere before it) + */ + if (!(r->m_read.flags & RTMP_READ_GOTKF) && + packet.m_packetType != RTMP_PACKET_TYPE_FLASH_VIDEO) + { + RTMP_Log(RTMP_LOGWARNING, + "Stream does not start with requested frame, ignoring data... "); + r->m_read.nIgnoredFrameCounter++; + if (r->m_read.nIgnoredFrameCounter > MAX_IGNORED_FRAMES) + ret = RTMP_READ_ERROR; /* fatal error, couldn't continue stream */ + else + ret = RTMP_READ_IGNORE; + break; + } + /* ok, do the same for FLV streams */ + if (!(r->m_read.flags & RTMP_READ_GOTFLVK) && + packet.m_packetType == RTMP_PACKET_TYPE_FLASH_VIDEO) + { + RTMP_Log(RTMP_LOGWARNING, + "Stream does not start with requested FLV frame, ignoring data... "); + r->m_read.nIgnoredFlvFrameCounter++; + if (r->m_read.nIgnoredFlvFrameCounter > MAX_IGNORED_FRAMES) + ret = RTMP_READ_ERROR; + else + ret = RTMP_READ_IGNORE; + break; + } + + /* we have to ignore the 0ms frames since these are the first + * keyframes; we've got these so don't mess around with multiple + * copies sent by the server to us! (if the keyframe is found at a + * later position there is only one copy and it will be ignored by + * the preceding if clause) + */ + if (!(r->m_read.flags & RTMP_READ_NO_IGNORE) && + packet.m_packetType != RTMP_PACKET_TYPE_FLASH_VIDEO) + { + /* exclude type RTMP_PACKET_TYPE_FLASH_VIDEO since it can + * contain several FLV packets + */ + if (packet.m_nTimeStamp == 0) + { + ret = RTMP_READ_IGNORE; + break; + } + else + { + /* stop ignoring packets */ + r->m_read.flags |= RTMP_READ_NO_IGNORE; + } + } + } + + /* calculate packet size and allocate slop buffer if necessary */ + size = nPacketLen + + ((packet.m_packetType == RTMP_PACKET_TYPE_AUDIO + || packet.m_packetType == RTMP_PACKET_TYPE_VIDEO + || packet.m_packetType == RTMP_PACKET_TYPE_INFO) ? 11 : 0) + + (packet.m_packetType != RTMP_PACKET_TYPE_FLASH_VIDEO ? 4 : 0); + + if (size + 4 > buflen) + { + /* the extra 4 is for the case of an FLV stream without a last + * prevTagSize (we need extra 4 bytes to append it) */ + r->m_read.buf = malloc(size + 4); + if (r->m_read.buf == 0) + { + RTMP_Log(RTMP_LOGERROR, "Couldn't allocate memory!"); + ret = RTMP_READ_ERROR; /* fatal error */ + break; + } + recopy = TRUE; + ptr = r->m_read.buf; + } + else + { + ptr = buf; + } + pend = ptr + size + 4; + + /* use to return timestamp of last processed packet */ + + /* audio (0x08), video (0x09) or metadata (0x12) packets : + * construct 11 byte header then add rtmp packet's data */ + if (packet.m_packetType == RTMP_PACKET_TYPE_AUDIO + || packet.m_packetType == RTMP_PACKET_TYPE_VIDEO + || packet.m_packetType == RTMP_PACKET_TYPE_INFO) + { + nTimeStamp = r->m_read.nResumeTS + packet.m_nTimeStamp; + prevTagSize = 11 + nPacketLen; + + *ptr = packet.m_packetType; + ptr++; + ptr = AMF_EncodeInt24(ptr, pend, nPacketLen); + +#if 0 + if(packet.m_packetType == RTMP_PACKET_TYPE_VIDEO) { + + /* H264 fix: */ + if((packetBody[0] & 0x0f) == 7) { /* CodecId = H264 */ + uint8_t packetType = *(packetBody+1); + + uint32_t ts = AMF_DecodeInt24(packetBody+2); /* composition time */ + int32_t cts = (ts+0xff800000)^0xff800000; + RTMP_Log(RTMP_LOGDEBUG, "cts : %d\n", cts); + + nTimeStamp -= cts; + /* get rid of the composition time */ + CRTMP::EncodeInt24(packetBody+2, 0); + } + RTMP_Log(RTMP_LOGDEBUG, "VIDEO: nTimeStamp: 0x%08X (%d)\n", nTimeStamp, nTimeStamp); + } +#endif + + ptr = AMF_EncodeInt24(ptr, pend, nTimeStamp); + *ptr = (char)((nTimeStamp & 0xFF000000) >> 24); + ptr++; + + /* stream id */ + ptr = AMF_EncodeInt24(ptr, pend, 0); + } + + memcpy(ptr, packetBody, nPacketLen); + len = nPacketLen; + + /* correct tagSize and obtain timestamp if we have an FLV stream */ + if (packet.m_packetType == RTMP_PACKET_TYPE_FLASH_VIDEO) + { + unsigned int pos = 0; + int delta; + + /* grab first timestamp and see if it needs fixing */ + nTimeStamp = AMF_DecodeInt24(packetBody + 4); + nTimeStamp |= (packetBody[7] << 24); + delta = packet.m_nTimeStamp - nTimeStamp + r->m_read.nResumeTS; + + while (pos + 11 < nPacketLen) + { + /* size without header (11) and without prevTagSize (4) */ + uint32_t dataSize = AMF_DecodeInt24(packetBody + pos + 1); + nTimeStamp = AMF_DecodeInt24(packetBody + pos + 4); + nTimeStamp |= (packetBody[pos + 7] << 24); + + if (delta) + { + nTimeStamp += delta; + AMF_EncodeInt24(ptr+pos+4, pend, nTimeStamp); + ptr[pos+7] = nTimeStamp>>24; + } + + /* set data type */ + r->m_read.dataType |= (((*(packetBody + pos) == 0x08) << 2) | + (*(packetBody + pos) == 0x09)); + + if (pos + 11 + dataSize + 4 > nPacketLen) + { + if (pos + 11 + dataSize > nPacketLen) + { + RTMP_Log(RTMP_LOGERROR, + "Wrong data size (%u), stream corrupted, aborting!", + dataSize); + ret = RTMP_READ_ERROR; + break; + } + RTMP_Log(RTMP_LOGWARNING, "No tagSize found, appending!"); + + /* we have to append a last tagSize! */ + prevTagSize = dataSize + 11; + AMF_EncodeInt32(ptr + pos + 11 + dataSize, pend, + prevTagSize); + size += 4; + len += 4; + } + else + { + prevTagSize = + AMF_DecodeInt32(packetBody + pos + 11 + dataSize); + +#ifdef _DEBUG + RTMP_Log(RTMP_LOGDEBUG, + "FLV Packet: type %02X, dataSize: %lu, tagSize: %lu, timeStamp: %lu ms", + (unsigned char)packetBody[pos], dataSize, prevTagSize, + nTimeStamp); +#endif + + if (prevTagSize != (dataSize + 11)) + { +#ifdef _DEBUG + RTMP_Log(RTMP_LOGWARNING, + "Tag and data size are not consitent, writing tag size according to dataSize+11: %d", + dataSize + 11); +#endif + + prevTagSize = dataSize + 11; + AMF_EncodeInt32(ptr + pos + 11 + dataSize, pend, + prevTagSize); + } + } + + pos += prevTagSize + 4; /*(11+dataSize+4); */ + } + } + ptr += len; + + if (packet.m_packetType != RTMP_PACKET_TYPE_FLASH_VIDEO) + { + /* FLV tag packets contain their own prevTagSize */ + AMF_EncodeInt32(ptr, pend, prevTagSize); + } + + /* In non-live this nTimeStamp can contain an absolute TS. + * Update ext timestamp with this absolute offset in non-live mode + * otherwise report the relative one + */ + /* RTMP_Log(RTMP_LOGDEBUG, "type: %02X, size: %d, pktTS: %dms, TS: %dms, bLiveStream: %d", packet.m_packetType, nPacketLen, packet.m_nTimeStamp, nTimeStamp, r->Link.lFlags & RTMP_LF_LIVE); */ + r->m_read.timestamp = (r->Link.lFlags & RTMP_LF_LIVE) ? packet.m_nTimeStamp : nTimeStamp; + + ret = size; + break; + } + + if (rtnGetNextMediaPacket) + RTMPPacket_Free(&packet); + + if (recopy) + { + len = ret > buflen ? buflen : ret; + memcpy(buf, r->m_read.buf, len); + r->m_read.bufpos = r->m_read.buf + len; + r->m_read.buflen = ret - len; + } + return ret; +} + +static const char flvHeader[] = { 'F', 'L', 'V', 0x01, + 0x00, /* 0x04 == audio, 0x01 == video */ + 0x00, 0x00, 0x00, 0x09, + 0x00, 0x00, 0x00, 0x00 +}; + +#define HEADERBUF (128*1024) +int +RTMP_Read(RTMP *r, char *buf, int size) +{ + int nRead = 0, total = 0; + + /* can't continue */ +fail: + switch (r->m_read.status) { + case RTMP_READ_EOF: + case RTMP_READ_COMPLETE: + return 0; + case RTMP_READ_ERROR: /* corrupted stream, resume failed */ + SetSockError(EINVAL); + return -1; + default: + break; + } + + /* first time thru */ + if (!(r->m_read.flags & RTMP_READ_HEADER)) + { + if (!(r->m_read.flags & RTMP_READ_RESUME)) + { + char *mybuf = malloc(HEADERBUF), *end = mybuf + HEADERBUF; + int cnt = 0; + r->m_read.buf = mybuf; + r->m_read.buflen = HEADERBUF; + + memcpy(mybuf, flvHeader, sizeof(flvHeader)); + r->m_read.buf += sizeof(flvHeader); + r->m_read.buflen -= sizeof(flvHeader); + cnt += sizeof(flvHeader); + + while (r->m_read.timestamp == 0) + { + nRead = Read_1_Packet(r, r->m_read.buf, r->m_read.buflen); + if (nRead < 0) + { + free(mybuf); + r->m_read.buf = NULL; + r->m_read.buflen = 0; + r->m_read.status = nRead; + goto fail; + } + /* buffer overflow, fix buffer and give up */ + if (r->m_read.buf < mybuf || r->m_read.buf > end) { + mybuf = realloc(mybuf, cnt + nRead); + memcpy(mybuf+cnt, r->m_read.buf, nRead); + free(r->m_read.buf); + r->m_read.buf = mybuf+cnt+nRead; + break; + } + cnt += nRead; + r->m_read.buf += nRead; + r->m_read.buflen -= nRead; + if (r->m_read.dataType == 5) + break; + } + mybuf[4] = r->m_read.dataType; + r->m_read.buflen = r->m_read.buf - mybuf; + r->m_read.buf = mybuf; + r->m_read.bufpos = mybuf; + } + r->m_read.flags |= RTMP_READ_HEADER; + } + + if ((r->m_read.flags & RTMP_READ_SEEKING) && r->m_read.buf) + { + /* drop whatever's here */ + free(r->m_read.buf); + r->m_read.buf = NULL; + r->m_read.bufpos = NULL; + r->m_read.buflen = 0; + } + + /* If there's leftover data buffered, use it up */ + if (r->m_read.buf) + { + nRead = r->m_read.buflen; + if (nRead > size) + nRead = size; + memcpy(buf, r->m_read.bufpos, nRead); + r->m_read.buflen -= nRead; + if (!r->m_read.buflen) + { + free(r->m_read.buf); + r->m_read.buf = NULL; + r->m_read.bufpos = NULL; + } + else + { + r->m_read.bufpos += nRead; + } + buf += nRead; + total += nRead; + size -= nRead; + } + + while (size > 0 && (nRead = Read_1_Packet(r, buf, size)) >= 0) + { + if (!nRead) continue; + buf += nRead; + total += nRead; + size -= nRead; + break; + } + if (nRead < 0) + r->m_read.status = nRead; + + if (size < 0) + total += size; + return total; +} + +static const AVal av_setDataFrame = AVC("@setDataFrame"); + +int +RTMP_Write(RTMP *r, const char *buf, int size) +{ + RTMPPacket *pkt = &r->m_write; + char *pend, *enc; + int s2 = size, ret, num; + + pkt->m_nChannel = 0x04; /* source channel */ + pkt->m_nInfoField2 = r->m_stream_id; + + while (s2) + { + if (!pkt->m_nBytesRead) + { + if (size < 11) { + /* FLV pkt too small */ + return 0; + } + + if (buf[0] == 'F' && buf[1] == 'L' && buf[2] == 'V') + { + buf += 13; + s2 -= 13; + } + + pkt->m_packetType = *buf++; + pkt->m_nBodySize = AMF_DecodeInt24(buf); + buf += 3; + pkt->m_nTimeStamp = AMF_DecodeInt24(buf); + buf += 3; + pkt->m_nTimeStamp |= *buf++ << 24; + buf += 3; + s2 -= 11; + + if (((pkt->m_packetType == RTMP_PACKET_TYPE_AUDIO + || pkt->m_packetType == RTMP_PACKET_TYPE_VIDEO) && + !pkt->m_nTimeStamp) || pkt->m_packetType == RTMP_PACKET_TYPE_INFO) + { + pkt->m_headerType = RTMP_PACKET_SIZE_LARGE; + if (pkt->m_packetType == RTMP_PACKET_TYPE_INFO) + pkt->m_nBodySize += 16; + } + else + { + pkt->m_headerType = RTMP_PACKET_SIZE_MEDIUM; + } + + if (!RTMPPacket_Alloc(pkt, pkt->m_nBodySize)) + { + RTMP_Log(RTMP_LOGDEBUG, "%s, failed to allocate packet", __FUNCTION__); + return FALSE; + } + enc = pkt->m_body; + pend = enc + pkt->m_nBodySize; + if (pkt->m_packetType == RTMP_PACKET_TYPE_INFO) + { + enc = AMF_EncodeString(enc, pend, &av_setDataFrame); + pkt->m_nBytesRead = enc - pkt->m_body; + } + } + else + { + enc = pkt->m_body + pkt->m_nBytesRead; + } + num = pkt->m_nBodySize - pkt->m_nBytesRead; + if (num > s2) + num = s2; + memcpy(enc, buf, num); + pkt->m_nBytesRead += num; + s2 -= num; + buf += num; + if (pkt->m_nBytesRead == pkt->m_nBodySize) + { + ret = RTMP_SendPacket(r, pkt, FALSE); + RTMPPacket_Free(pkt); + pkt->m_nBytesRead = 0; + if (!ret) + return -1; + buf += 4; + s2 -= 4; + if (s2 < 0) + break; + } + } + return size+s2; +} diff --git a/librtmp/rtmp.h b/librtmp/rtmp.h new file mode 100644 index 0000000..bccfc77 --- /dev/null +++ b/librtmp/rtmp.h @@ -0,0 +1,426 @@ +#ifndef __RTMP_H__ +#define __RTMP_H__ +/* + * Copyright (C) 2005-2008 Team XBMC + * http://www.xbmc.org + * Copyright (C) 2008-2009 Andrej Stepanchuk + * Copyright (C) 2009-2010 Howard Chu + * + * This file is part of librtmp. + * + * librtmp is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1, + * or (at your option) any later version. + * + * librtmp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with librtmp see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/lgpl.html + */ + +#if !defined(NO_CRYPTO) && !defined(CRYPTO) +#define CRYPTO +#endif + +#include +#include +#include + +#include "amf.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +#define RTMP_LIB_VERSION 0x020300 /* 2.3 */ + +#define RTMP_FEATURE_HTTP 0x01 +#define RTMP_FEATURE_ENC 0x02 +#define RTMP_FEATURE_SSL 0x04 +#define RTMP_FEATURE_MFP 0x08 /* not yet supported */ +#define RTMP_FEATURE_WRITE 0x10 /* publish, not play */ +#define RTMP_FEATURE_HTTP2 0x20 /* server-side rtmpt */ + +#define RTMP_PROTOCOL_UNDEFINED -1 +#define RTMP_PROTOCOL_RTMP 0 +#define RTMP_PROTOCOL_RTMPE RTMP_FEATURE_ENC +#define RTMP_PROTOCOL_RTMPT RTMP_FEATURE_HTTP +#define RTMP_PROTOCOL_RTMPS RTMP_FEATURE_SSL +#define RTMP_PROTOCOL_RTMPTE (RTMP_FEATURE_HTTP|RTMP_FEATURE_ENC) +#define RTMP_PROTOCOL_RTMPTS (RTMP_FEATURE_HTTP|RTMP_FEATURE_SSL) +#define RTMP_PROTOCOL_RTMFP RTMP_FEATURE_MFP + +#define RTMP_DEFAULT_CHUNKSIZE 128 + +/* needs to fit largest number of bytes recv() may return */ +#define RTMP_BUFFER_CACHE_SIZE (16*1024) + +#define RTMP_CHANNELS 65600 + + extern const char RTMPProtocolStringsLower[][7]; + extern const AVal RTMP_DefaultFlashVer; + extern int RTMP_ctrlC; + + uint32_t RTMP_GetTime(void); + +/* RTMP_PACKET_TYPE_... 0x00 */ +#define RTMP_PACKET_TYPE_CHUNK_SIZE 0x01 +/* RTMP_PACKET_TYPE_... 0x02 */ +#define RTMP_PACKET_TYPE_BYTES_READ_REPORT 0x03 +#define RTMP_PACKET_TYPE_CONTROL 0x04 +#define RTMP_PACKET_TYPE_SERVER_BW 0x05 +#define RTMP_PACKET_TYPE_CLIENT_BW 0x06 +/* RTMP_PACKET_TYPE_... 0x07 */ +#define RTMP_PACKET_TYPE_AUDIO 0x08 +#define RTMP_PACKET_TYPE_VIDEO 0x09 +/* RTMP_PACKET_TYPE_... 0x0A */ +/* RTMP_PACKET_TYPE_... 0x0B */ +/* RTMP_PACKET_TYPE_... 0x0C */ +/* RTMP_PACKET_TYPE_... 0x0D */ +/* RTMP_PACKET_TYPE_... 0x0E */ +#define RTMP_PACKET_TYPE_FLEX_STREAM_SEND 0x0F +#define RTMP_PACKET_TYPE_FLEX_SHARED_OBJECT 0x10 +#define RTMP_PACKET_TYPE_FLEX_MESSAGE 0x11 +#define RTMP_PACKET_TYPE_INFO 0x12 +#define RTMP_PACKET_TYPE_SHARED_OBJECT 0x13 +#define RTMP_PACKET_TYPE_INVOKE 0x14 +/* RTMP_PACKET_TYPE_... 0x15 */ +#define RTMP_PACKET_TYPE_FLASH_VIDEO 0x16 + +#define RTMP_MAX_HEADER_SIZE 18 + +#define RTMP_PACKET_SIZE_LARGE 0 +#define RTMP_PACKET_SIZE_MEDIUM 1 +#define RTMP_PACKET_SIZE_SMALL 2 +#define RTMP_PACKET_SIZE_MINIMUM 3 + + typedef struct RTMPChunk + { + int c_headerSize; + int c_chunkSize; + char *c_chunk; + char c_header[RTMP_MAX_HEADER_SIZE]; + } RTMPChunk; + + typedef struct RTMPPacket + { + uint8_t m_headerType; + uint8_t m_packetType; + uint8_t m_hasAbsTimestamp; /* timestamp absolute or relative? */ + int m_nChannel; + uint32_t m_nTimeStamp; /* timestamp */ + int32_t m_nInfoField2; /* last 4 bytes in a long header */ + uint32_t m_nBodySize; + uint32_t m_nBytesRead; + RTMPChunk *m_chunk; + char *m_body; + } RTMPPacket; + + typedef struct RTMPSockBuf + { + int sb_socket; + int sb_size; /* number of unprocessed bytes in buffer */ + char *sb_start; /* pointer into sb_pBuffer of next byte to process */ + char sb_buf[RTMP_BUFFER_CACHE_SIZE]; /* data read from socket */ + int sb_timedout; + void *sb_ssl; + } RTMPSockBuf; + + void RTMPPacket_Reset(RTMPPacket *p); + void RTMPPacket_Dump(RTMPPacket *p); + int RTMPPacket_Alloc(RTMPPacket *p, uint32_t nSize); + void RTMPPacket_Free(RTMPPacket *p); + +#define RTMPPacket_IsReady(a) ((a)->m_nBytesRead == (a)->m_nBodySize) + + typedef struct RTMP_LNK + { + AVal hostname; + AVal sockshost; + + AVal playpath0; /* parsed from URL */ + AVal playpath; /* passed in explicitly */ + AVal tcUrl; + AVal swfUrl; + AVal pageUrl; + AVal app; + AVal auth; + AVal flashVer; + AVal subscribepath; + AVal usherToken; + AVal token; + AVal pubUser; + AVal pubPasswd; + AMFObject extras; + int edepth; + + int seekTime; + int stopTime; + +#define RTMP_LF_AUTH 0x0001 /* using auth param */ +#define RTMP_LF_LIVE 0x0002 /* stream is live */ +#define RTMP_LF_SWFV 0x0004 /* do SWF verification */ +#define RTMP_LF_PLST 0x0008 /* send playlist before play */ +#define RTMP_LF_BUFX 0x0010 /* toggle stream on BufferEmpty msg */ +#define RTMP_LF_FTCU 0x0020 /* free tcUrl on close */ +#define RTMP_LF_FAPU 0x0040 /* free app on close */ + int lFlags; + + int swfAge; + + int protocol; + int timeout; /* connection timeout in seconds */ + + int pFlags; /* unused, but kept to avoid breaking ABI */ + + unsigned short socksport; + unsigned short port; + +#ifdef CRYPTO +#define RTMP_SWF_HASHLEN 32 + void *dh; /* for encryption */ + void *rc4keyIn; + void *rc4keyOut; + + uint32_t SWFSize; + uint8_t SWFHash[RTMP_SWF_HASHLEN]; + char SWFVerificationResponse[RTMP_SWF_HASHLEN+10]; +#endif + } RTMP_LNK; + + /* state for read() wrapper */ + typedef struct RTMP_READ + { + char *buf; + char *bufpos; + unsigned int buflen; + uint32_t timestamp; + uint8_t dataType; + uint8_t flags; +#define RTMP_READ_HEADER 0x01 +#define RTMP_READ_RESUME 0x02 +#define RTMP_READ_NO_IGNORE 0x04 +#define RTMP_READ_GOTKF 0x08 +#define RTMP_READ_GOTFLVK 0x10 +#define RTMP_READ_SEEKING 0x20 + int8_t status; +#define RTMP_READ_COMPLETE -3 +#define RTMP_READ_ERROR -2 +#define RTMP_READ_EOF -1 +#define RTMP_READ_IGNORE 0 + + /* if bResume == TRUE */ + uint8_t initialFrameType; + uint32_t nResumeTS; + char *metaHeader; + char *initialFrame; + uint32_t nMetaHeaderSize; + uint32_t nInitialFrameSize; + uint32_t nIgnoredFrameCounter; + uint32_t nIgnoredFlvFrameCounter; + } RTMP_READ; + + typedef struct RTMP_METHOD + { + AVal name; + int num; + } RTMP_METHOD; + + typedef struct RTMP + { + int m_inChunkSize; + int m_outChunkSize; + int m_nBWCheckCounter; + int m_nBytesIn; + int m_nBytesInSent; + int m_nBufferMS; + int m_stream_id; /* returned in _result from createStream */ + int m_mediaChannel; + uint32_t m_mediaStamp; + uint32_t m_pauseStamp; + int m_pausing; + int m_nServerBW; + int m_nClientBW; + uint8_t m_nClientBW2; + uint8_t m_bPlaying; + uint8_t m_bSendEncoding; + uint8_t m_bSendCounter; + + int m_numInvokes; + int m_numCalls; + RTMP_METHOD *m_methodCalls; /* remote method calls queue */ + + int m_channelsAllocatedIn; + int m_channelsAllocatedOut; + RTMPPacket **m_vecChannelsIn; + RTMPPacket **m_vecChannelsOut; + int *m_channelTimestamp; /* abs timestamp of last packet */ + + double m_fAudioCodecs; /* audioCodecs for the connect packet */ + double m_fVideoCodecs; /* videoCodecs for the connect packet */ + double m_fEncoding; /* AMF0 or AMF3 */ + + double m_fDuration; /* duration of stream in seconds */ + + int m_msgCounter; /* RTMPT stuff */ + int m_polling; + int m_resplen; + int m_unackd; + AVal m_clientID; + + RTMP_READ m_read; + RTMPPacket m_write; + RTMPSockBuf m_sb; + RTMP_LNK Link; + } RTMP; + + int RTMP_ParseURL(const char *url, int *protocol, AVal *host, + unsigned int *port, AVal *playpath, AVal *app); + + void RTMP_ParsePlaypath(AVal *in, AVal *out); + void RTMP_SetBufferMS(RTMP *r, int size); + void RTMP_UpdateBufferMS(RTMP *r); + + int RTMP_SetOpt(RTMP *r, const AVal *opt, AVal *arg); + int RTMP_SetupURL(RTMP *r, char *url); + void RTMP_SetupStream(RTMP *r, int protocol, + AVal *hostname, + unsigned int port, + AVal *sockshost, + AVal *playpath, + AVal *tcUrl, + AVal *swfUrl, + AVal *pageUrl, + AVal *app, + AVal *auth, + AVal *swfSHA256Hash, + uint32_t swfSize, + AVal *flashVer, + AVal *subscribepath, + AVal *usherToken, + int dStart, + int dStop, int bLiveStream, long int timeout); + + int RTMP_Connect(RTMP *r, RTMPPacket *cp); + struct sockaddr; + int RTMP_Connect0(RTMP *r, struct sockaddr *svc); + int RTMP_Connect1(RTMP *r, RTMPPacket *cp); + int RTMP_Serve(RTMP *r); + int RTMP_TLS_Accept(RTMP *r, void *ctx); + + int RTMP_ReadPacket(RTMP *r, RTMPPacket *packet); + int RTMP_SendPacket(RTMP *r, RTMPPacket *packet, int queue); + int RTMP_SendChunk(RTMP *r, RTMPChunk *chunk); + int RTMP_IsConnected(RTMP *r); + int RTMP_Socket(RTMP *r); + int RTMP_IsTimedout(RTMP *r); + double RTMP_GetDuration(RTMP *r); + int RTMP_ToggleStream(RTMP *r); + + int RTMP_ConnectStream(RTMP *r, int seekTime); + int RTMP_ReconnectStream(RTMP *r, int seekTime); + void RTMP_DeleteStream(RTMP *r); + int RTMP_GetNextMediaPacket(RTMP *r, RTMPPacket *packet); + int RTMP_ClientPacket(RTMP *r, RTMPPacket *packet); + + void RTMP_Init(RTMP *r); + void RTMP_Close(RTMP *r); + RTMP *RTMP_Alloc(void); + void RTMP_Free(RTMP *r); + void RTMP_EnableWrite(RTMP *r); + + void *RTMP_TLS_AllocServerContext(const char* cert, const char* key); + void RTMP_TLS_FreeServerContext(void *ctx); + + int RTMP_LibVersion(void); + void RTMP_UserInterrupt(void); /* user typed Ctrl-C */ + + int RTMP_SendCtrl(RTMP *r, short nType, unsigned int nObject, + unsigned int nTime); + + /* caller probably doesn't know current timestamp, should + * just use RTMP_Pause instead + */ + int RTMP_SendPause(RTMP *r, int DoPause, int dTime); + int RTMP_Pause(RTMP *r, int DoPause); + + int RTMP_FindFirstMatchingProperty(AMFObject *obj, const AVal *name, + AMFObjectProperty * p); + + int RTMPSockBuf_Fill(RTMPSockBuf *sb); + int RTMPSockBuf_Send(RTMPSockBuf *sb, const char *buf, int len); + int RTMPSockBuf_Close(RTMPSockBuf *sb); + + int RTMP_SendCreateStream(RTMP *r); + int RTMP_SendSeek(RTMP *r, int dTime); + int RTMP_SendServerBW(RTMP *r); + int RTMP_SendClientBW(RTMP *r); + void RTMP_DropRequest(RTMP *r, int i, int freeit); + int RTMP_Read(RTMP *r, char *buf, int size); + int RTMP_Write(RTMP *r, const char *buf, int size); + +/* hashswf.c */ + int RTMP_HashSWF(const char *url, unsigned int *size, unsigned char *hash, + int age); + +/* + *********************************************************************** +<<<<<<< HEAD +<<<<<<< HEAD + * Introduced by SRS, export the ip/pid/cid of BMS + *********************************************************************** + */ +/* + * The exported ip of server, for example, we use DNS to connect to server, + * but the ip resolved by DNS system maybe virtual ip, that is, the "real ip" + * only known by server itself and return by the rtmp connect result or flv + * metadata. + */ +extern char* _srs_ip; +/* + * The pid of BMS, used to query the detail log of client. + * A BMS server may restart and the pid changed. + */ +extern int _srs_pid; +/* + * The cid of BMS, used to query the detail log of client. + * A connection of a process(identify by pid) is unique and its id(cid) is + * unique also. The cid generally is a thread or connection or logic unit, + * for example, cid of rtmp client is the rtmp connection, while cid of hls+ + * is a virtual connection which merge many http connections. + */ +extern int _srs_cid; +/* + *********************************************************************** + * Introduced by SRS, other useful data. + *********************************************************************** + */ +/* + * The received bytes from server. user can use to stat the kbps by: + * rkbps = rbytes * 8 / 1000 / (diff seconds) + */ +extern unsigned long _srs_rbytes; +/* + * The sent bytes from server. user can use to stat the kbps by: + * skbps = sbytes * 8 / 1000 / (diff seconds) + */ +extern unsigned long _srs_sbytes; +/* + * The current state of client. + * 0,init 1,idle 2,connected 3,working 4,closed + */ +extern int _srs_state; + +#ifdef __cplusplus +}; +#endif + +#endif diff --git a/librtmp/rtmp_sys.h b/librtmp/rtmp_sys.h new file mode 100644 index 0000000..d5437c7 --- /dev/null +++ b/librtmp/rtmp_sys.h @@ -0,0 +1,138 @@ +#ifndef __RTMP_SYS_H__ +#define __RTMP_SYS_H__ +/* + * Copyright (C) 2010 Howard Chu + * + * This file is part of librtmp. + * + * librtmp is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1, + * or (at your option) any later version. + * + * librtmp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with librtmp see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/lgpl.html + */ + +#ifdef _WIN32 + +#include +#include + +#if _MSC_VER < 1500 /* MSVC */ + #define snprintf _snprintf + #define strcasecmp stricmp + #define strncasecmp strnicmp + #define vsnprintf _vsnprintf +#endif + +#define GetSockError() WSAGetLastError() +#define SetSockError(e) WSASetLastError(e) +#define setsockopt(a,b,c,d,e) (setsockopt)(a,b,c,(const char *)d,(int)e) +#define EWOULDBLOCK WSAETIMEDOUT /* we don't use nonblocking, but we do use timeouts */ +#define msleep(n) Sleep(n) +#define SET_RCVTIMEO(tv,s) int tv = s*1000 +#else /* !_WIN32 */ +#include +#include +#include +#include +#include +#include +#include +#include +#define GetSockError() errno +#define SetSockError(e) errno = e +#undef closesocket +#define closesocket(s) close(s) +#define msleep(n) usleep(n*1000) +#define SET_RCVTIMEO(tv,s) struct timeval tv = {s,0} +#endif + +#include "rtmp.h" + +#ifdef USE_POLARSSL +#include +#include +#include +#include +#if POLARSSL_VERSION_NUMBER < 0x01010000 +#define havege_random havege_rand +#endif +#if POLARSSL_VERSION_NUMBER >= 0x01020000 +#define SSL_SET_SESSION(S,resume,timeout,ctx) ssl_set_session(S,ctx) +#else +#define SSL_SET_SESSION(S,resume,timeout,ctx) ssl_set_session(S,resume,timeout,ctx) +#endif +typedef struct tls_ctx { + havege_state hs; + ssl_session ssn; +} tls_ctx; +typedef struct tls_server_ctx { + havege_state *hs; + x509_cert cert; + rsa_context key; + ssl_session ssn; + const char *dhm_P, *dhm_G; +} tls_server_ctx; + +#define TLS_CTX tls_ctx * +#define TLS_client(ctx,s) s = malloc(sizeof(ssl_context)); ssl_init(s);\ + ssl_set_endpoint(s, SSL_IS_CLIENT); ssl_set_authmode(s, SSL_VERIFY_NONE);\ + ssl_set_rng(s, havege_random, &ctx->hs);\ + ssl_set_ciphersuites(s, ssl_default_ciphersuites);\ + SSL_SET_SESSION(s, 1, 600, &ctx->ssn) +#define TLS_server(ctx,s) s = malloc(sizeof(ssl_context)); ssl_init(s);\ + ssl_set_endpoint(s, SSL_IS_SERVER); ssl_set_authmode(s, SSL_VERIFY_NONE);\ + ssl_set_rng(s, havege_random, ((tls_server_ctx*)ctx)->hs);\ + ssl_set_ciphersuites(s, ssl_default_ciphersuites);\ + SSL_SET_SESSION(s, 1, 600, &((tls_server_ctx*)ctx)->ssn);\ + ssl_set_own_cert(s, &((tls_server_ctx*)ctx)->cert, &((tls_server_ctx*)ctx)->key);\ + ssl_set_dh_param(s, ((tls_server_ctx*)ctx)->dhm_P, ((tls_server_ctx*)ctx)->dhm_G) +#define TLS_setfd(s,fd) ssl_set_bio(s, net_recv, &fd, net_send, &fd) +#define TLS_connect(s) ssl_handshake(s) +#define TLS_accept(s) ssl_handshake(s) +#define TLS_read(s,b,l) ssl_read(s,(unsigned char *)b,l) +#define TLS_write(s,b,l) ssl_write(s,(unsigned char *)b,l) +#define TLS_shutdown(s) ssl_close_notify(s) +#define TLS_close(s) ssl_free(s); free(s) + +#elif defined(USE_GNUTLS) +#include +typedef struct tls_ctx { + gnutls_certificate_credentials_t cred; + gnutls_priority_t prios; +} tls_ctx; +#define TLS_CTX tls_ctx * +#define TLS_client(ctx,s) gnutls_init((gnutls_session_t *)(&s), GNUTLS_CLIENT); gnutls_priority_set(s, ctx->prios); gnutls_credentials_set(s, GNUTLS_CRD_CERTIFICATE, ctx->cred) +#define TLS_server(ctx,s) gnutls_init((gnutls_session_t *)(&s), GNUTLS_SERVER); gnutls_priority_set_direct(s, "NORMAL", NULL); gnutls_credentials_set(s, GNUTLS_CRD_CERTIFICATE, ctx) +#define TLS_setfd(s,fd) gnutls_transport_set_ptr(s, (gnutls_transport_ptr_t)(long)fd) +#define TLS_connect(s) gnutls_handshake(s) +#define TLS_accept(s) gnutls_handshake(s) +#define TLS_read(s,b,l) gnutls_record_recv(s,b,l) +#define TLS_write(s,b,l) gnutls_record_send(s,b,l) +#define TLS_shutdown(s) gnutls_bye(s, GNUTLS_SHUT_RDWR) +#define TLS_close(s) gnutls_deinit(s) + +#else /* USE_OPENSSL */ +#define TLS_CTX SSL_CTX * +#define TLS_client(ctx,s) s = SSL_new(ctx) +#define TLS_server(ctx,s) s = SSL_new(ctx) +#define TLS_setfd(s,fd) SSL_set_fd(s,fd) +#define TLS_connect(s) SSL_connect(s) +#define TLS_accept(s) SSL_accept(s) +#define TLS_read(s,b,l) SSL_read(s,b,l) +#define TLS_write(s,b,l) SSL_write(s,b,l) +#define TLS_shutdown(s) SSL_shutdown(s) +#define TLS_close(s) SSL_free(s) + +#endif +#endif diff --git a/librtmp/strncasecmp.h b/librtmp/strncasecmp.h new file mode 100644 index 0000000..0e6dfb1 --- /dev/null +++ b/librtmp/strncasecmp.h @@ -0,0 +1,17 @@ +#ifndef STRNCASECMP_H +#define STRNCASECMP_H + + +#ifndef WINSHIT_INCLUDED +#define WINSHIT_INCLUDED + +#if defined(WIN32) || defined(WIN64) +#define strcasecmp _stricmp +#define strncasecmp(x,y,z) _strnicmp(x,y,z) + +#endif /* Def WIN32 or Def WIN64 */ + +#endif /* Ndef WINSHIT_INCLUDED */ + + +#endif // STRNCASECMP_H diff --git a/librtmp/zlib.h b/librtmp/zlib.h new file mode 100644 index 0000000..32c2ce0 --- /dev/null +++ b/librtmp/zlib.h @@ -0,0 +1,1916 @@ +/* zlib.h -- interface of the 'zlib' general purpose compression library + version 1.2.11, January 15th, 2017 + + Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. + + Jean-loup Gailly Mark Adler + jloup@gzip.org madler@alumni.caltech.edu + + + The data format used by the zlib library is described by RFCs (Request for + Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950 + (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format). +*/ + +#ifndef ZLIB_H +#define ZLIB_H + +#include "zconf.h" + +#include +#undef ZEXTERN +#define ZEXTERN Q_CORE_EXPORT + +#ifdef __cplusplus +extern "C" { +#endif + +#define ZLIB_VERSION "1.2.11 (Qt)" +#define ZLIB_VERNUM 0x12b0f +#define ZLIB_VER_MAJOR 1 +#define ZLIB_VER_MINOR 2 +#define ZLIB_VER_REVISION 11 +#define ZLIB_VER_SUBREVISION 0 + +/* + The 'zlib' compression library provides in-memory compression and + decompression functions, including integrity checks of the uncompressed data. + This version of the library supports only one compression method (deflation) + but other algorithms will be added later and will have the same stream + interface. + + Compression can be done in a single step if the buffers are large enough, + or can be done by repeated calls of the compression function. In the latter + case, the application must provide more input and/or consume the output + (providing more output space) before each call. + + The compressed data format used by default by the in-memory functions is + the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped + around a deflate stream, which is itself documented in RFC 1951. + + The library also supports reading and writing files in gzip (.gz) format + with an interface similar to that of stdio using the functions that start + with "gz". The gzip format is different from the zlib format. gzip is a + gzip wrapper, documented in RFC 1952, wrapped around a deflate stream. + + This library can optionally read and write gzip and raw deflate streams in + memory as well. + + The zlib format was designed to be compact and fast for use in memory + and on communications channels. The gzip format was designed for single- + file compression on file systems, has a larger header than zlib to maintain + directory information, and uses a different, slower check method than zlib. + + The library does not install any signal handler. The decoder checks + the consistency of the compressed data, so the library should never crash + even in the case of corrupted input. +*/ + +typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size)); +typedef void (*free_func) OF((voidpf opaque, voidpf address)); + +struct internal_state; + +typedef struct z_stream_s { + z_const Bytef *next_in; /* next input byte */ + uInt avail_in; /* number of bytes available at next_in */ + uLong total_in; /* total number of input bytes read so far */ + + Bytef *next_out; /* next output byte will go here */ + uInt avail_out; /* remaining free space at next_out */ + uLong total_out; /* total number of bytes output so far */ + + z_const char *msg; /* last error message, NULL if no error */ + struct internal_state FAR *state; /* not visible by applications */ + + alloc_func zalloc; /* used to allocate the internal state */ + free_func zfree; /* used to free the internal state */ + voidpf opaque; /* private data object passed to zalloc and zfree */ + + int data_type; /* best guess about the data type: binary or text + for deflate, or the decoding state for inflate */ + uLong adler; /* Adler-32 or CRC-32 value of the uncompressed data */ + uLong reserved; /* reserved for future use */ +} z_stream; + +typedef z_stream FAR *z_streamp; + +/* + gzip header information passed to and from zlib routines. See RFC 1952 + for more details on the meanings of these fields. +*/ +typedef struct gz_header_s { + int text; /* true if compressed data believed to be text */ + uLong time; /* modification time */ + int xflags; /* extra flags (not used when writing a gzip file) */ + int os; /* operating system */ + Bytef *extra; /* pointer to extra field or Z_NULL if none */ + uInt extra_len; /* extra field length (valid if extra != Z_NULL) */ + uInt extra_max; /* space at extra (only when reading header) */ + Bytef *name; /* pointer to zero-terminated file name or Z_NULL */ + uInt name_max; /* space at name (only when reading header) */ + Bytef *comment; /* pointer to zero-terminated comment or Z_NULL */ + uInt comm_max; /* space at comment (only when reading header) */ + int hcrc; /* true if there was or will be a header crc */ + int done; /* true when done reading gzip header (not used + when writing a gzip file) */ +} gz_header; + +typedef gz_header FAR *gz_headerp; + +/* + The application must update next_in and avail_in when avail_in has dropped + to zero. It must update next_out and avail_out when avail_out has dropped + to zero. The application must initialize zalloc, zfree and opaque before + calling the init function. All other fields are set by the compression + library and must not be updated by the application. + + The opaque value provided by the application will be passed as the first + parameter for calls of zalloc and zfree. This can be useful for custom + memory management. The compression library attaches no meaning to the + opaque value. + + zalloc must return Z_NULL if there is not enough memory for the object. + If zlib is used in a multi-threaded application, zalloc and zfree must be + thread safe. In that case, zlib is thread-safe. When zalloc and zfree are + Z_NULL on entry to the initialization function, they are set to internal + routines that use the standard library functions malloc() and free(). + + On 16-bit systems, the functions zalloc and zfree must be able to allocate + exactly 65536 bytes, but will not be required to allocate more than this if + the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS, pointers + returned by zalloc for objects of exactly 65536 bytes *must* have their + offset normalized to zero. The default allocation function provided by this + library ensures this (see zutil.c). To reduce memory requirements and avoid + any allocation of 64K objects, at the expense of compression ratio, compile + the library with -DMAX_WBITS=14 (see zconf.h). + + The fields total_in and total_out can be used for statistics or progress + reports. After compression, total_in holds the total size of the + uncompressed data and may be saved for use by the decompressor (particularly + if the decompressor wants to decompress everything in a single step). +*/ + + /* constants */ + +#define Z_NO_FLUSH 0 +#define Z_PARTIAL_FLUSH 1 +#define Z_SYNC_FLUSH 2 +#define Z_FULL_FLUSH 3 +#define Z_FINISH 4 +#define Z_BLOCK 5 +#define Z_TREES 6 +/* Allowed flush values; see deflate() and inflate() below for details */ + +#define Z_OK 0 +#define Z_STREAM_END 1 +#define Z_NEED_DICT 2 +#define Z_ERRNO (-1) +#define Z_STREAM_ERROR (-2) +#define Z_DATA_ERROR (-3) +#define Z_MEM_ERROR (-4) +#define Z_BUF_ERROR (-5) +#define Z_VERSION_ERROR (-6) +/* Return codes for the compression/decompression functions. Negative values + * are errors, positive values are used for special but normal events. + */ + +#define Z_NO_COMPRESSION 0 +#define Z_BEST_SPEED 1 +#define Z_BEST_COMPRESSION 9 +#define Z_DEFAULT_COMPRESSION (-1) +/* compression levels */ + +#define Z_FILTERED 1 +#define Z_HUFFMAN_ONLY 2 +#define Z_RLE 3 +#define Z_FIXED 4 +#define Z_DEFAULT_STRATEGY 0 +/* compression strategy; see deflateInit2() below for details */ + +#define Z_BINARY 0 +#define Z_TEXT 1 +#define Z_ASCII Z_TEXT /* for compatibility with 1.2.2 and earlier */ +#define Z_UNKNOWN 2 +/* Possible values of the data_type field for deflate() */ + +#define Z_DEFLATED 8 +/* The deflate compression method (the only one supported in this version) */ + +#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */ + +#define zlib_version zlibVersion() +/* for compatibility with versions < 1.0.2 */ + + + /* basic functions */ + +ZEXTERN const char * ZEXPORT zlibVersion OF((void)); +/* The application can compare zlibVersion and ZLIB_VERSION for consistency. + If the first character differs, the library code actually used is not + compatible with the zlib.h header file used by the application. This check + is automatically made by deflateInit and inflateInit. + */ + +/* +ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level)); + + Initializes the internal stream state for compression. The fields + zalloc, zfree and opaque must be initialized before by the caller. If + zalloc and zfree are set to Z_NULL, deflateInit updates them to use default + allocation functions. + + The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9: + 1 gives best speed, 9 gives best compression, 0 gives no compression at all + (the input data is simply copied a block at a time). Z_DEFAULT_COMPRESSION + requests a default compromise between speed and compression (currently + equivalent to level 6). + + deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_STREAM_ERROR if level is not a valid compression level, or + Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible + with the version assumed by the caller (ZLIB_VERSION). msg is set to null + if there is no error message. deflateInit does not perform any compression: + this will be done by deflate(). +*/ + + +ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush)); +/* + deflate compresses as much data as possible, and stops when the input + buffer becomes empty or the output buffer becomes full. It may introduce + some output latency (reading input without producing any output) except when + forced to flush. + + The detailed semantics are as follows. deflate performs one or both of the + following actions: + + - Compress more input starting at next_in and update next_in and avail_in + accordingly. If not all input can be processed (because there is not + enough room in the output buffer), next_in and avail_in are updated and + processing will resume at this point for the next call of deflate(). + + - Generate more output starting at next_out and update next_out and avail_out + accordingly. This action is forced if the parameter flush is non zero. + Forcing flush frequently degrades the compression ratio, so this parameter + should be set only when necessary. Some output may be provided even if + flush is zero. + + Before the call of deflate(), the application should ensure that at least + one of the actions is possible, by providing more input and/or consuming more + output, and updating avail_in or avail_out accordingly; avail_out should + never be zero before the call. The application can consume the compressed + output when it wants, for example when the output buffer is full (avail_out + == 0), or after each call of deflate(). If deflate returns Z_OK and with + zero avail_out, it must be called again after making room in the output + buffer because there might be more output pending. See deflatePending(), + which can be used if desired to determine whether or not there is more ouput + in that case. + + Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to + decide how much data to accumulate before producing output, in order to + maximize compression. + + If the parameter flush is set to Z_SYNC_FLUSH, all pending output is + flushed to the output buffer and the output is aligned on a byte boundary, so + that the decompressor can get all input data available so far. (In + particular avail_in is zero after the call if enough output space has been + provided before the call.) Flushing may degrade compression for some + compression algorithms and so it should be used only when necessary. This + completes the current deflate block and follows it with an empty stored block + that is three bits plus filler bits to the next byte, followed by four bytes + (00 00 ff ff). + + If flush is set to Z_PARTIAL_FLUSH, all pending output is flushed to the + output buffer, but the output is not aligned to a byte boundary. All of the + input data so far will be available to the decompressor, as for Z_SYNC_FLUSH. + This completes the current deflate block and follows it with an empty fixed + codes block that is 10 bits long. This assures that enough bytes are output + in order for the decompressor to finish the block before the empty fixed + codes block. + + If flush is set to Z_BLOCK, a deflate block is completed and emitted, as + for Z_SYNC_FLUSH, but the output is not aligned on a byte boundary, and up to + seven bits of the current block are held to be written as the next byte after + the next deflate block is completed. In this case, the decompressor may not + be provided enough bits at this point in order to complete decompression of + the data provided so far to the compressor. It may need to wait for the next + block to be emitted. This is for advanced applications that need to control + the emission of deflate blocks. + + If flush is set to Z_FULL_FLUSH, all output is flushed as with + Z_SYNC_FLUSH, and the compression state is reset so that decompression can + restart from this point if previous compressed data has been damaged or if + random access is desired. Using Z_FULL_FLUSH too often can seriously degrade + compression. + + If deflate returns with avail_out == 0, this function must be called again + with the same value of the flush parameter and more output space (updated + avail_out), until the flush is complete (deflate returns with non-zero + avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that + avail_out is greater than six to avoid repeated flush markers due to + avail_out == 0 on return. + + If the parameter flush is set to Z_FINISH, pending input is processed, + pending output is flushed and deflate returns with Z_STREAM_END if there was + enough output space. If deflate returns with Z_OK or Z_BUF_ERROR, this + function must be called again with Z_FINISH and more output space (updated + avail_out) but no more input data, until it returns with Z_STREAM_END or an + error. After deflate has returned Z_STREAM_END, the only possible operations + on the stream are deflateReset or deflateEnd. + + Z_FINISH can be used in the first deflate call after deflateInit if all the + compression is to be done in a single step. In order to complete in one + call, avail_out must be at least the value returned by deflateBound (see + below). Then deflate is guaranteed to return Z_STREAM_END. If not enough + output space is provided, deflate will not return Z_STREAM_END, and it must + be called again as described above. + + deflate() sets strm->adler to the Adler-32 checksum of all input read + so far (that is, total_in bytes). If a gzip stream is being generated, then + strm->adler will be the CRC-32 checksum of the input read so far. (See + deflateInit2 below.) + + deflate() may update strm->data_type if it can make a good guess about + the input data type (Z_BINARY or Z_TEXT). If in doubt, the data is + considered binary. This field is only for information purposes and does not + affect the compression algorithm in any manner. + + deflate() returns Z_OK if some progress has been made (more input + processed or more output produced), Z_STREAM_END if all input has been + consumed and all output has been produced (only when flush is set to + Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example + if next_in or next_out was Z_NULL or the state was inadvertently written over + by the application), or Z_BUF_ERROR if no progress is possible (for example + avail_in or avail_out was zero). Note that Z_BUF_ERROR is not fatal, and + deflate() can be called again with more input and more output space to + continue compressing. +*/ + + +ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm)); +/* + All dynamically allocated data structures for this stream are freed. + This function discards any unprocessed input and does not flush any pending + output. + + deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the + stream state was inconsistent, Z_DATA_ERROR if the stream was freed + prematurely (some input or output was discarded). In the error case, msg + may be set but then points to a static string (which must not be + deallocated). +*/ + + +/* +ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm)); + + Initializes the internal stream state for decompression. The fields + next_in, avail_in, zalloc, zfree and opaque must be initialized before by + the caller. In the current version of inflate, the provided input is not + read or consumed. The allocation of a sliding window will be deferred to + the first call of inflate (if the decompression does not complete on the + first call). If zalloc and zfree are set to Z_NULL, inflateInit updates + them to use default allocation functions. + + inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_VERSION_ERROR if the zlib library version is incompatible with the + version assumed by the caller, or Z_STREAM_ERROR if the parameters are + invalid, such as a null pointer to the structure. msg is set to null if + there is no error message. inflateInit does not perform any decompression. + Actual decompression will be done by inflate(). So next_in, and avail_in, + next_out, and avail_out are unused and unchanged. The current + implementation of inflateInit() does not process any header information -- + that is deferred until inflate() is called. +*/ + + +ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush)); +/* + inflate decompresses as much data as possible, and stops when the input + buffer becomes empty or the output buffer becomes full. It may introduce + some output latency (reading input without producing any output) except when + forced to flush. + + The detailed semantics are as follows. inflate performs one or both of the + following actions: + + - Decompress more input starting at next_in and update next_in and avail_in + accordingly. If not all input can be processed (because there is not + enough room in the output buffer), then next_in and avail_in are updated + accordingly, and processing will resume at this point for the next call of + inflate(). + + - Generate more output starting at next_out and update next_out and avail_out + accordingly. inflate() provides as much output as possible, until there is + no more input data or no more space in the output buffer (see below about + the flush parameter). + + Before the call of inflate(), the application should ensure that at least + one of the actions is possible, by providing more input and/or consuming more + output, and updating the next_* and avail_* values accordingly. If the + caller of inflate() does not provide both available input and available + output space, it is possible that there will be no progress made. The + application can consume the uncompressed output when it wants, for example + when the output buffer is full (avail_out == 0), or after each call of + inflate(). If inflate returns Z_OK and with zero avail_out, it must be + called again after making room in the output buffer because there might be + more output pending. + + The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH, Z_FINISH, + Z_BLOCK, or Z_TREES. Z_SYNC_FLUSH requests that inflate() flush as much + output as possible to the output buffer. Z_BLOCK requests that inflate() + stop if and when it gets to the next deflate block boundary. When decoding + the zlib or gzip format, this will cause inflate() to return immediately + after the header and before the first block. When doing a raw inflate, + inflate() will go ahead and process the first block, and will return when it + gets to the end of that block, or when it runs out of data. + + The Z_BLOCK option assists in appending to or combining deflate streams. + To assist in this, on return inflate() always sets strm->data_type to the + number of unused bits in the last byte taken from strm->next_in, plus 64 if + inflate() is currently decoding the last block in the deflate stream, plus + 128 if inflate() returned immediately after decoding an end-of-block code or + decoding the complete header up to just before the first byte of the deflate + stream. The end-of-block will not be indicated until all of the uncompressed + data from that block has been written to strm->next_out. The number of + unused bits may in general be greater than seven, except when bit 7 of + data_type is set, in which case the number of unused bits will be less than + eight. data_type is set as noted here every time inflate() returns for all + flush options, and so can be used to determine the amount of currently + consumed input in bits. + + The Z_TREES option behaves as Z_BLOCK does, but it also returns when the + end of each deflate block header is reached, before any actual data in that + block is decoded. This allows the caller to determine the length of the + deflate block header for later use in random access within a deflate block. + 256 is added to the value of strm->data_type when inflate() returns + immediately after reaching the end of the deflate block header. + + inflate() should normally be called until it returns Z_STREAM_END or an + error. However if all decompression is to be performed in a single step (a + single call of inflate), the parameter flush should be set to Z_FINISH. In + this case all pending input is processed and all pending output is flushed; + avail_out must be large enough to hold all of the uncompressed data for the + operation to complete. (The size of the uncompressed data may have been + saved by the compressor for this purpose.) The use of Z_FINISH is not + required to perform an inflation in one step. However it may be used to + inform inflate that a faster approach can be used for the single inflate() + call. Z_FINISH also informs inflate to not maintain a sliding window if the + stream completes, which reduces inflate's memory footprint. If the stream + does not complete, either because not all of the stream is provided or not + enough output space is provided, then a sliding window will be allocated and + inflate() can be called again to continue the operation as if Z_NO_FLUSH had + been used. + + In this implementation, inflate() always flushes as much output as + possible to the output buffer, and always uses the faster approach on the + first call. So the effects of the flush parameter in this implementation are + on the return value of inflate() as noted below, when inflate() returns early + when Z_BLOCK or Z_TREES is used, and when inflate() avoids the allocation of + memory for a sliding window when Z_FINISH is used. + + If a preset dictionary is needed after this call (see inflateSetDictionary + below), inflate sets strm->adler to the Adler-32 checksum of the dictionary + chosen by the compressor and returns Z_NEED_DICT; otherwise it sets + strm->adler to the Adler-32 checksum of all output produced so far (that is, + total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described + below. At the end of the stream, inflate() checks that its computed Adler-32 + checksum is equal to that saved by the compressor and returns Z_STREAM_END + only if the checksum is correct. + + inflate() can decompress and check either zlib-wrapped or gzip-wrapped + deflate data. The header type is detected automatically, if requested when + initializing with inflateInit2(). Any information contained in the gzip + header is not retained unless inflateGetHeader() is used. When processing + gzip-wrapped deflate data, strm->adler32 is set to the CRC-32 of the output + produced so far. The CRC-32 is checked against the gzip trailer, as is the + uncompressed length, modulo 2^32. + + inflate() returns Z_OK if some progress has been made (more input processed + or more output produced), Z_STREAM_END if the end of the compressed data has + been reached and all uncompressed output has been produced, Z_NEED_DICT if a + preset dictionary is needed at this point, Z_DATA_ERROR if the input data was + corrupted (input stream not conforming to the zlib format or incorrect check + value, in which case strm->msg points to a string with a more specific + error), Z_STREAM_ERROR if the stream structure was inconsistent (for example + next_in or next_out was Z_NULL, or the state was inadvertently written over + by the application), Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR + if no progress was possible or if there was not enough room in the output + buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and + inflate() can be called again with more input and more output space to + continue decompressing. If Z_DATA_ERROR is returned, the application may + then call inflateSync() to look for a good compression block if a partial + recovery of the data is to be attempted. +*/ + + +ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm)); +/* + All dynamically allocated data structures for this stream are freed. + This function discards any unprocessed input and does not flush any pending + output. + + inflateEnd returns Z_OK if success, or Z_STREAM_ERROR if the stream state + was inconsistent. +*/ + + + /* Advanced functions */ + +/* + The following functions are needed only in some special applications. +*/ + +/* +ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm, + int level, + int method, + int windowBits, + int memLevel, + int strategy)); + + This is another version of deflateInit with more compression options. The + fields next_in, zalloc, zfree and opaque must be initialized before by the + caller. + + The method parameter is the compression method. It must be Z_DEFLATED in + this version of the library. + + The windowBits parameter is the base two logarithm of the window size + (the size of the history buffer). It should be in the range 8..15 for this + version of the library. Larger values of this parameter result in better + compression at the expense of memory usage. The default value is 15 if + deflateInit is used instead. + + For the current implementation of deflate(), a windowBits value of 8 (a + window size of 256 bytes) is not supported. As a result, a request for 8 + will result in 9 (a 512-byte window). In that case, providing 8 to + inflateInit2() will result in an error when the zlib header with 9 is + checked against the initialization of inflate(). The remedy is to not use 8 + with deflateInit2() with this initialization, or at least in that case use 9 + with inflateInit2(). + + windowBits can also be -8..-15 for raw deflate. In this case, -windowBits + determines the window size. deflate() will then generate raw deflate data + with no zlib header or trailer, and will not compute a check value. + + windowBits can also be greater than 15 for optional gzip encoding. Add + 16 to windowBits to write a simple gzip header and trailer around the + compressed data instead of a zlib wrapper. The gzip header will have no + file name, no extra data, no comment, no modification time (set to zero), no + header crc, and the operating system will be set to the appropriate value, + if the operating system was determined at compile time. If a gzip stream is + being written, strm->adler is a CRC-32 instead of an Adler-32. + + For raw deflate or gzip encoding, a request for a 256-byte window is + rejected as invalid, since only the zlib header provides a means of + transmitting the window size to the decompressor. + + The memLevel parameter specifies how much memory should be allocated + for the internal compression state. memLevel=1 uses minimum memory but is + slow and reduces compression ratio; memLevel=9 uses maximum memory for + optimal speed. The default value is 8. See zconf.h for total memory usage + as a function of windowBits and memLevel. + + The strategy parameter is used to tune the compression algorithm. Use the + value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a + filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no + string match), or Z_RLE to limit match distances to one (run-length + encoding). Filtered data consists mostly of small values with a somewhat + random distribution. In this case, the compression algorithm is tuned to + compress them better. The effect of Z_FILTERED is to force more Huffman + coding and less string matching; it is somewhat intermediate between + Z_DEFAULT_STRATEGY and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as + fast as Z_HUFFMAN_ONLY, but give better compression for PNG image data. The + strategy parameter only affects the compression ratio but not the + correctness of the compressed output even if it is not set appropriately. + Z_FIXED prevents the use of dynamic Huffman codes, allowing for a simpler + decoder for special applications. + + deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_STREAM_ERROR if any parameter is invalid (such as an invalid + method), or Z_VERSION_ERROR if the zlib library version (zlib_version) is + incompatible with the version assumed by the caller (ZLIB_VERSION). msg is + set to null if there is no error message. deflateInit2 does not perform any + compression: this will be done by deflate(). +*/ + +ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm, + const Bytef *dictionary, + uInt dictLength)); +/* + Initializes the compression dictionary from the given byte sequence + without producing any compressed output. When using the zlib format, this + function must be called immediately after deflateInit, deflateInit2 or + deflateReset, and before any call of deflate. When doing raw deflate, this + function must be called either before any call of deflate, or immediately + after the completion of a deflate block, i.e. after all input has been + consumed and all output has been delivered when using any of the flush + options Z_BLOCK, Z_PARTIAL_FLUSH, Z_SYNC_FLUSH, or Z_FULL_FLUSH. The + compressor and decompressor must use exactly the same dictionary (see + inflateSetDictionary). + + The dictionary should consist of strings (byte sequences) that are likely + to be encountered later in the data to be compressed, with the most commonly + used strings preferably put towards the end of the dictionary. Using a + dictionary is most useful when the data to be compressed is short and can be + predicted with good accuracy; the data can then be compressed better than + with the default empty dictionary. + + Depending on the size of the compression data structures selected by + deflateInit or deflateInit2, a part of the dictionary may in effect be + discarded, for example if the dictionary is larger than the window size + provided in deflateInit or deflateInit2. Thus the strings most likely to be + useful should be put at the end of the dictionary, not at the front. In + addition, the current implementation of deflate will use at most the window + size minus 262 bytes of the provided dictionary. + + Upon return of this function, strm->adler is set to the Adler-32 value + of the dictionary; the decompressor may later use this value to determine + which dictionary has been used by the compressor. (The Adler-32 value + applies to the whole dictionary even if only a subset of the dictionary is + actually used by the compressor.) If a raw deflate was requested, then the + Adler-32 value is not computed and strm->adler is not set. + + deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a + parameter is invalid (e.g. dictionary being Z_NULL) or the stream state is + inconsistent (for example if deflate has already been called for this stream + or if not at a block boundary for raw deflate). deflateSetDictionary does + not perform any compression: this will be done by deflate(). +*/ + +ZEXTERN int ZEXPORT deflateGetDictionary OF((z_streamp strm, + Bytef *dictionary, + uInt *dictLength)); +/* + Returns the sliding dictionary being maintained by deflate. dictLength is + set to the number of bytes in the dictionary, and that many bytes are copied + to dictionary. dictionary must have enough space, where 32768 bytes is + always enough. If deflateGetDictionary() is called with dictionary equal to + Z_NULL, then only the dictionary length is returned, and nothing is copied. + Similary, if dictLength is Z_NULL, then it is not set. + + deflateGetDictionary() may return a length less than the window size, even + when more than the window size in input has been provided. It may return up + to 258 bytes less in that case, due to how zlib's implementation of deflate + manages the sliding window and lookahead for matches, where matches can be + up to 258 bytes long. If the application needs the last window-size bytes of + input, then that would need to be saved by the application outside of zlib. + + deflateGetDictionary returns Z_OK on success, or Z_STREAM_ERROR if the + stream state is inconsistent. +*/ + +ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest, + z_streamp source)); +/* + Sets the destination stream as a complete copy of the source stream. + + This function can be useful when several compression strategies will be + tried, for example when there are several ways of pre-processing the input + data with a filter. The streams that will be discarded should then be freed + by calling deflateEnd. Note that deflateCopy duplicates the internal + compression state which can be quite large, so this strategy is slow and can + consume lots of memory. + + deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_STREAM_ERROR if the source stream state was inconsistent + (such as zalloc being Z_NULL). msg is left unchanged in both source and + destination. +*/ + +ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm)); +/* + This function is equivalent to deflateEnd followed by deflateInit, but + does not free and reallocate the internal compression state. The stream + will leave the compression level and any other attributes that may have been + set unchanged. + + deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent (such as zalloc or state being Z_NULL). +*/ + +ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm, + int level, + int strategy)); +/* + Dynamically update the compression level and compression strategy. The + interpretation of level and strategy is as in deflateInit2(). This can be + used to switch between compression and straight copy of the input data, or + to switch to a different kind of input data requiring a different strategy. + If the compression approach (which is a function of the level) or the + strategy is changed, and if any input has been consumed in a previous + deflate() call, then the input available so far is compressed with the old + level and strategy using deflate(strm, Z_BLOCK). There are three approaches + for the compression levels 0, 1..3, and 4..9 respectively. The new level + and strategy will take effect at the next call of deflate(). + + If a deflate(strm, Z_BLOCK) is performed by deflateParams(), and it does + not have enough output space to complete, then the parameter change will not + take effect. In this case, deflateParams() can be called again with the + same parameters and more output space to try again. + + In order to assure a change in the parameters on the first try, the + deflate stream should be flushed using deflate() with Z_BLOCK or other flush + request until strm.avail_out is not zero, before calling deflateParams(). + Then no more input data should be provided before the deflateParams() call. + If this is done, the old level and strategy will be applied to the data + compressed before deflateParams(), and the new level and strategy will be + applied to the the data compressed after deflateParams(). + + deflateParams returns Z_OK on success, Z_STREAM_ERROR if the source stream + state was inconsistent or if a parameter was invalid, or Z_BUF_ERROR if + there was not enough output space to complete the compression of the + available input data before a change in the strategy or approach. Note that + in the case of a Z_BUF_ERROR, the parameters are not changed. A return + value of Z_BUF_ERROR is not fatal, in which case deflateParams() can be + retried with more output space. +*/ + +ZEXTERN int ZEXPORT deflateTune OF((z_streamp strm, + int good_length, + int max_lazy, + int nice_length, + int max_chain)); +/* + Fine tune deflate's internal compression parameters. This should only be + used by someone who understands the algorithm used by zlib's deflate for + searching for the best matching string, and even then only by the most + fanatic optimizer trying to squeeze out the last compressed bit for their + specific input data. Read the deflate.c source code for the meaning of the + max_lazy, good_length, nice_length, and max_chain parameters. + + deflateTune() can be called after deflateInit() or deflateInit2(), and + returns Z_OK on success, or Z_STREAM_ERROR for an invalid deflate stream. + */ + +ZEXTERN uLong ZEXPORT deflateBound OF((z_streamp strm, + uLong sourceLen)); +/* + deflateBound() returns an upper bound on the compressed size after + deflation of sourceLen bytes. It must be called after deflateInit() or + deflateInit2(), and after deflateSetHeader(), if used. This would be used + to allocate an output buffer for deflation in a single pass, and so would be + called before deflate(). If that first deflate() call is provided the + sourceLen input bytes, an output buffer allocated to the size returned by + deflateBound(), and the flush value Z_FINISH, then deflate() is guaranteed + to return Z_STREAM_END. Note that it is possible for the compressed size to + be larger than the value returned by deflateBound() if flush options other + than Z_FINISH or Z_NO_FLUSH are used. +*/ + +ZEXTERN int ZEXPORT deflatePending OF((z_streamp strm, + unsigned *pending, + int *bits)); +/* + deflatePending() returns the number of bytes and bits of output that have + been generated, but not yet provided in the available output. The bytes not + provided would be due to the available output space having being consumed. + The number of bits of output not provided are between 0 and 7, where they + await more bits to join them in order to fill out a full byte. If pending + or bits are Z_NULL, then those values are not set. + + deflatePending returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. + */ + +ZEXTERN int ZEXPORT deflatePrime OF((z_streamp strm, + int bits, + int value)); +/* + deflatePrime() inserts bits in the deflate output stream. The intent + is that this function is used to start off the deflate output with the bits + leftover from a previous deflate stream when appending to it. As such, this + function can only be used for raw deflate, and must be used before the first + deflate() call after a deflateInit2() or deflateReset(). bits must be less + than or equal to 16, and that many of the least significant bits of value + will be inserted in the output. + + deflatePrime returns Z_OK if success, Z_BUF_ERROR if there was not enough + room in the internal buffer to insert the bits, or Z_STREAM_ERROR if the + source stream state was inconsistent. +*/ + +ZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm, + gz_headerp head)); +/* + deflateSetHeader() provides gzip header information for when a gzip + stream is requested by deflateInit2(). deflateSetHeader() may be called + after deflateInit2() or deflateReset() and before the first call of + deflate(). The text, time, os, extra field, name, and comment information + in the provided gz_header structure are written to the gzip header (xflag is + ignored -- the extra flags are set according to the compression level). The + caller must assure that, if not Z_NULL, name and comment are terminated with + a zero byte, and that if extra is not Z_NULL, that extra_len bytes are + available there. If hcrc is true, a gzip header crc is included. Note that + the current versions of the command-line version of gzip (up through version + 1.3.x) do not support header crc's, and will report that it is a "multi-part + gzip file" and give up. + + If deflateSetHeader is not used, the default gzip header has text false, + the time set to zero, and os set to 255, with no extra, name, or comment + fields. The gzip header is returned to the default state by deflateReset(). + + deflateSetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. +*/ + +/* +ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm, + int windowBits)); + + This is another version of inflateInit with an extra parameter. The + fields next_in, avail_in, zalloc, zfree and opaque must be initialized + before by the caller. + + The windowBits parameter is the base two logarithm of the maximum window + size (the size of the history buffer). It should be in the range 8..15 for + this version of the library. The default value is 15 if inflateInit is used + instead. windowBits must be greater than or equal to the windowBits value + provided to deflateInit2() while compressing, or it must be equal to 15 if + deflateInit2() was not used. If a compressed stream with a larger window + size is given as input, inflate() will return with the error code + Z_DATA_ERROR instead of trying to allocate a larger window. + + windowBits can also be zero to request that inflate use the window size in + the zlib header of the compressed stream. + + windowBits can also be -8..-15 for raw inflate. In this case, -windowBits + determines the window size. inflate() will then process raw deflate data, + not looking for a zlib or gzip header, not generating a check value, and not + looking for any check values for comparison at the end of the stream. This + is for use with other formats that use the deflate compressed data format + such as zip. Those formats provide their own check values. If a custom + format is developed using the raw deflate format for compressed data, it is + recommended that a check value such as an Adler-32 or a CRC-32 be applied to + the uncompressed data as is done in the zlib, gzip, and zip formats. For + most applications, the zlib format should be used as is. Note that comments + above on the use in deflateInit2() applies to the magnitude of windowBits. + + windowBits can also be greater than 15 for optional gzip decoding. Add + 32 to windowBits to enable zlib and gzip decoding with automatic header + detection, or add 16 to decode only the gzip format (the zlib format will + return a Z_DATA_ERROR). If a gzip stream is being decoded, strm->adler is a + CRC-32 instead of an Adler-32. Unlike the gunzip utility and gzread() (see + below), inflate() will not automatically decode concatenated gzip streams. + inflate() will return Z_STREAM_END at the end of the gzip stream. The state + would need to be reset to continue decoding a subsequent gzip stream. + + inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_VERSION_ERROR if the zlib library version is incompatible with the + version assumed by the caller, or Z_STREAM_ERROR if the parameters are + invalid, such as a null pointer to the structure. msg is set to null if + there is no error message. inflateInit2 does not perform any decompression + apart from possibly reading the zlib header if present: actual decompression + will be done by inflate(). (So next_in and avail_in may be modified, but + next_out and avail_out are unused and unchanged.) The current implementation + of inflateInit2() does not process any header information -- that is + deferred until inflate() is called. +*/ + +ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm, + const Bytef *dictionary, + uInt dictLength)); +/* + Initializes the decompression dictionary from the given uncompressed byte + sequence. This function must be called immediately after a call of inflate, + if that call returned Z_NEED_DICT. The dictionary chosen by the compressor + can be determined from the Adler-32 value returned by that call of inflate. + The compressor and decompressor must use exactly the same dictionary (see + deflateSetDictionary). For raw inflate, this function can be called at any + time to set the dictionary. If the provided dictionary is smaller than the + window and there is already data in the window, then the provided dictionary + will amend what's there. The application must insure that the dictionary + that was used for compression is provided. + + inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a + parameter is invalid (e.g. dictionary being Z_NULL) or the stream state is + inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the + expected one (incorrect Adler-32 value). inflateSetDictionary does not + perform any decompression: this will be done by subsequent calls of + inflate(). +*/ + +ZEXTERN int ZEXPORT inflateGetDictionary OF((z_streamp strm, + Bytef *dictionary, + uInt *dictLength)); +/* + Returns the sliding dictionary being maintained by inflate. dictLength is + set to the number of bytes in the dictionary, and that many bytes are copied + to dictionary. dictionary must have enough space, where 32768 bytes is + always enough. If inflateGetDictionary() is called with dictionary equal to + Z_NULL, then only the dictionary length is returned, and nothing is copied. + Similary, if dictLength is Z_NULL, then it is not set. + + inflateGetDictionary returns Z_OK on success, or Z_STREAM_ERROR if the + stream state is inconsistent. +*/ + +ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm)); +/* + Skips invalid compressed data until a possible full flush point (see above + for the description of deflate with Z_FULL_FLUSH) can be found, or until all + available input is skipped. No output is provided. + + inflateSync searches for a 00 00 FF FF pattern in the compressed data. + All full flush points have this pattern, but not all occurrences of this + pattern are full flush points. + + inflateSync returns Z_OK if a possible full flush point has been found, + Z_BUF_ERROR if no more input was provided, Z_DATA_ERROR if no flush point + has been found, or Z_STREAM_ERROR if the stream structure was inconsistent. + In the success case, the application may save the current current value of + total_in which indicates where valid compressed data was found. In the + error case, the application may repeatedly call inflateSync, providing more + input each time, until success or end of the input data. +*/ + +ZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest, + z_streamp source)); +/* + Sets the destination stream as a complete copy of the source stream. + + This function can be useful when randomly accessing a large stream. The + first pass through the stream can periodically record the inflate state, + allowing restarting inflate at those points when randomly accessing the + stream. + + inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_STREAM_ERROR if the source stream state was inconsistent + (such as zalloc being Z_NULL). msg is left unchanged in both source and + destination. +*/ + +ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm)); +/* + This function is equivalent to inflateEnd followed by inflateInit, + but does not free and reallocate the internal decompression state. The + stream will keep attributes that may have been set by inflateInit2. + + inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent (such as zalloc or state being Z_NULL). +*/ + +ZEXTERN int ZEXPORT inflateReset2 OF((z_streamp strm, + int windowBits)); +/* + This function is the same as inflateReset, but it also permits changing + the wrap and window size requests. The windowBits parameter is interpreted + the same as it is for inflateInit2. If the window size is changed, then the + memory allocated for the window is freed, and the window will be reallocated + by inflate() if needed. + + inflateReset2 returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent (such as zalloc or state being Z_NULL), or if + the windowBits parameter is invalid. +*/ + +ZEXTERN int ZEXPORT inflatePrime OF((z_streamp strm, + int bits, + int value)); +/* + This function inserts bits in the inflate input stream. The intent is + that this function is used to start inflating at a bit position in the + middle of a byte. The provided bits will be used before any bytes are used + from next_in. This function should only be used with raw inflate, and + should be used before the first inflate() call after inflateInit2() or + inflateReset(). bits must be less than or equal to 16, and that many of the + least significant bits of value will be inserted in the input. + + If bits is negative, then the input stream bit buffer is emptied. Then + inflatePrime() can be called again to put bits in the buffer. This is used + to clear out bits leftover after feeding inflate a block description prior + to feeding inflate codes. + + inflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. +*/ + +ZEXTERN long ZEXPORT inflateMark OF((z_streamp strm)); +/* + This function returns two values, one in the lower 16 bits of the return + value, and the other in the remaining upper bits, obtained by shifting the + return value down 16 bits. If the upper value is -1 and the lower value is + zero, then inflate() is currently decoding information outside of a block. + If the upper value is -1 and the lower value is non-zero, then inflate is in + the middle of a stored block, with the lower value equaling the number of + bytes from the input remaining to copy. If the upper value is not -1, then + it is the number of bits back from the current bit position in the input of + the code (literal or length/distance pair) currently being processed. In + that case the lower value is the number of bytes already emitted for that + code. + + A code is being processed if inflate is waiting for more input to complete + decoding of the code, or if it has completed decoding but is waiting for + more output space to write the literal or match data. + + inflateMark() is used to mark locations in the input data for random + access, which may be at bit positions, and to note those cases where the + output of a code may span boundaries of random access blocks. The current + location in the input stream can be determined from avail_in and data_type + as noted in the description for the Z_BLOCK flush parameter for inflate. + + inflateMark returns the value noted above, or -65536 if the provided + source stream state was inconsistent. +*/ + +ZEXTERN int ZEXPORT inflateGetHeader OF((z_streamp strm, + gz_headerp head)); +/* + inflateGetHeader() requests that gzip header information be stored in the + provided gz_header structure. inflateGetHeader() may be called after + inflateInit2() or inflateReset(), and before the first call of inflate(). + As inflate() processes the gzip stream, head->done is zero until the header + is completed, at which time head->done is set to one. If a zlib stream is + being decoded, then head->done is set to -1 to indicate that there will be + no gzip header information forthcoming. Note that Z_BLOCK or Z_TREES can be + used to force inflate() to return immediately after header processing is + complete and before any actual data is decompressed. + + The text, time, xflags, and os fields are filled in with the gzip header + contents. hcrc is set to true if there is a header CRC. (The header CRC + was valid if done is set to one.) If extra is not Z_NULL, then extra_max + contains the maximum number of bytes to write to extra. Once done is true, + extra_len contains the actual extra field length, and extra contains the + extra field, or that field truncated if extra_max is less than extra_len. + If name is not Z_NULL, then up to name_max characters are written there, + terminated with a zero unless the length is greater than name_max. If + comment is not Z_NULL, then up to comm_max characters are written there, + terminated with a zero unless the length is greater than comm_max. When any + of extra, name, or comment are not Z_NULL and the respective field is not + present in the header, then that field is set to Z_NULL to signal its + absence. This allows the use of deflateSetHeader() with the returned + structure to duplicate the header. However if those fields are set to + allocated memory, then the application will need to save those pointers + elsewhere so that they can be eventually freed. + + If inflateGetHeader is not used, then the header information is simply + discarded. The header is always checked for validity, including the header + CRC if present. inflateReset() will reset the process to discard the header + information. The application would need to call inflateGetHeader() again to + retrieve the header from the next gzip stream. + + inflateGetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. +*/ + +/* +ZEXTERN int ZEXPORT inflateBackInit OF((z_streamp strm, int windowBits, + unsigned char FAR *window)); + + Initialize the internal stream state for decompression using inflateBack() + calls. The fields zalloc, zfree and opaque in strm must be initialized + before the call. If zalloc and zfree are Z_NULL, then the default library- + derived memory allocation routines are used. windowBits is the base two + logarithm of the window size, in the range 8..15. window is a caller + supplied buffer of that size. Except for special applications where it is + assured that deflate was used with small window sizes, windowBits must be 15 + and a 32K byte window must be supplied to be able to decompress general + deflate streams. + + See inflateBack() for the usage of these routines. + + inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of + the parameters are invalid, Z_MEM_ERROR if the internal state could not be + allocated, or Z_VERSION_ERROR if the version of the library does not match + the version of the header file. +*/ + +typedef unsigned (*in_func) OF((void FAR *, + z_const unsigned char FAR * FAR *)); +typedef int (*out_func) OF((void FAR *, unsigned char FAR *, unsigned)); + +ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm, + in_func in, void FAR *in_desc, + out_func out, void FAR *out_desc)); +/* + inflateBack() does a raw inflate with a single call using a call-back + interface for input and output. This is potentially more efficient than + inflate() for file i/o applications, in that it avoids copying between the + output and the sliding window by simply making the window itself the output + buffer. inflate() can be faster on modern CPUs when used with large + buffers. inflateBack() trusts the application to not change the output + buffer passed by the output function, at least until inflateBack() returns. + + inflateBackInit() must be called first to allocate the internal state + and to initialize the state with the user-provided window buffer. + inflateBack() may then be used multiple times to inflate a complete, raw + deflate stream with each call. inflateBackEnd() is then called to free the + allocated state. + + A raw deflate stream is one with no zlib or gzip header or trailer. + This routine would normally be used in a utility that reads zip or gzip + files and writes out uncompressed files. The utility would decode the + header and process the trailer on its own, hence this routine expects only + the raw deflate stream to decompress. This is different from the default + behavior of inflate(), which expects a zlib header and trailer around the + deflate stream. + + inflateBack() uses two subroutines supplied by the caller that are then + called by inflateBack() for input and output. inflateBack() calls those + routines until it reads a complete deflate stream and writes out all of the + uncompressed data, or until it encounters an error. The function's + parameters and return types are defined above in the in_func and out_func + typedefs. inflateBack() will call in(in_desc, &buf) which should return the + number of bytes of provided input, and a pointer to that input in buf. If + there is no input available, in() must return zero -- buf is ignored in that + case -- and inflateBack() will return a buffer error. inflateBack() will + call out(out_desc, buf, len) to write the uncompressed data buf[0..len-1]. + out() should return zero on success, or non-zero on failure. If out() + returns non-zero, inflateBack() will return with an error. Neither in() nor + out() are permitted to change the contents of the window provided to + inflateBackInit(), which is also the buffer that out() uses to write from. + The length written by out() will be at most the window size. Any non-zero + amount of input may be provided by in(). + + For convenience, inflateBack() can be provided input on the first call by + setting strm->next_in and strm->avail_in. If that input is exhausted, then + in() will be called. Therefore strm->next_in must be initialized before + calling inflateBack(). If strm->next_in is Z_NULL, then in() will be called + immediately for input. If strm->next_in is not Z_NULL, then strm->avail_in + must also be initialized, and then if strm->avail_in is not zero, input will + initially be taken from strm->next_in[0 .. strm->avail_in - 1]. + + The in_desc and out_desc parameters of inflateBack() is passed as the + first parameter of in() and out() respectively when they are called. These + descriptors can be optionally used to pass any information that the caller- + supplied in() and out() functions need to do their job. + + On return, inflateBack() will set strm->next_in and strm->avail_in to + pass back any unused input that was provided by the last in() call. The + return values of inflateBack() can be Z_STREAM_END on success, Z_BUF_ERROR + if in() or out() returned an error, Z_DATA_ERROR if there was a format error + in the deflate stream (in which case strm->msg is set to indicate the nature + of the error), or Z_STREAM_ERROR if the stream was not properly initialized. + In the case of Z_BUF_ERROR, an input or output error can be distinguished + using strm->next_in which will be Z_NULL only if in() returned an error. If + strm->next_in is not Z_NULL, then the Z_BUF_ERROR was due to out() returning + non-zero. (in() will always be called before out(), so strm->next_in is + assured to be defined if out() returns non-zero.) Note that inflateBack() + cannot return Z_OK. +*/ + +ZEXTERN int ZEXPORT inflateBackEnd OF((z_streamp strm)); +/* + All memory allocated by inflateBackInit() is freed. + + inflateBackEnd() returns Z_OK on success, or Z_STREAM_ERROR if the stream + state was inconsistent. +*/ + +ZEXTERN uLong ZEXPORT zlibCompileFlags OF((void)); +/* Return flags indicating compile-time options. + + Type sizes, two bits each, 00 = 16 bits, 01 = 32, 10 = 64, 11 = other: + 1.0: size of uInt + 3.2: size of uLong + 5.4: size of voidpf (pointer) + 7.6: size of z_off_t + + Compiler, assembler, and debug options: + 8: ZLIB_DEBUG + 9: ASMV or ASMINF -- use ASM code + 10: ZLIB_WINAPI -- exported functions use the WINAPI calling convention + 11: 0 (reserved) + + One-time table building (smaller code, but not thread-safe if true): + 12: BUILDFIXED -- build static block decoding tables when needed + 13: DYNAMIC_CRC_TABLE -- build CRC calculation tables when needed + 14,15: 0 (reserved) + + Library content (indicates missing functionality): + 16: NO_GZCOMPRESS -- gz* functions cannot compress (to avoid linking + deflate code when not needed) + 17: NO_GZIP -- deflate can't write gzip streams, and inflate can't detect + and decode gzip streams (to avoid linking crc code) + 18-19: 0 (reserved) + + Operation variations (changes in library functionality): + 20: PKZIP_BUG_WORKAROUND -- slightly more permissive inflate + 21: FASTEST -- deflate algorithm with only one, lowest compression level + 22,23: 0 (reserved) + + The sprintf variant used by gzprintf (zero is best): + 24: 0 = vs*, 1 = s* -- 1 means limited to 20 arguments after the format + 25: 0 = *nprintf, 1 = *printf -- 1 means gzprintf() not secure! + 26: 0 = returns value, 1 = void -- 1 means inferred string length returned + + Remainder: + 27-31: 0 (reserved) + */ + +#ifndef Z_SOLO + + /* utility functions */ + +/* + The following utility functions are implemented on top of the basic + stream-oriented functions. To simplify the interface, some default options + are assumed (compression level and memory usage, standard memory allocation + functions). The source code of these utility functions can be modified if + you need special options. +*/ + +ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen, + const Bytef *source, uLong sourceLen)); +/* + Compresses the source buffer into the destination buffer. sourceLen is + the byte length of the source buffer. Upon entry, destLen is the total size + of the destination buffer, which must be at least the value returned by + compressBound(sourceLen). Upon exit, destLen is the actual size of the + compressed data. compress() is equivalent to compress2() with a level + parameter of Z_DEFAULT_COMPRESSION. + + compress returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_BUF_ERROR if there was not enough room in the output + buffer. +*/ + +ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen, + const Bytef *source, uLong sourceLen, + int level)); +/* + Compresses the source buffer into the destination buffer. The level + parameter has the same meaning as in deflateInit. sourceLen is the byte + length of the source buffer. Upon entry, destLen is the total size of the + destination buffer, which must be at least the value returned by + compressBound(sourceLen). Upon exit, destLen is the actual size of the + compressed data. + + compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_BUF_ERROR if there was not enough room in the output buffer, + Z_STREAM_ERROR if the level parameter is invalid. +*/ + +ZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen)); +/* + compressBound() returns an upper bound on the compressed size after + compress() or compress2() on sourceLen bytes. It would be used before a + compress() or compress2() call to allocate the destination buffer. +*/ + +ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen, + const Bytef *source, uLong sourceLen)); +/* + Decompresses the source buffer into the destination buffer. sourceLen is + the byte length of the source buffer. Upon entry, destLen is the total size + of the destination buffer, which must be large enough to hold the entire + uncompressed data. (The size of the uncompressed data must have been saved + previously by the compressor and transmitted to the decompressor by some + mechanism outside the scope of this compression library.) Upon exit, destLen + is the actual size of the uncompressed data. + + uncompress returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_BUF_ERROR if there was not enough room in the output + buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete. In + the case where there is not enough room, uncompress() will fill the output + buffer with the uncompressed data up to that point. +*/ + +ZEXTERN int ZEXPORT uncompress2 OF((Bytef *dest, uLongf *destLen, + const Bytef *source, uLong *sourceLen)); +/* + Same as uncompress, except that sourceLen is a pointer, where the + length of the source is *sourceLen. On return, *sourceLen is the number of + source bytes consumed. +*/ + + /* gzip file access functions */ + +/* + This library supports reading and writing files in gzip (.gz) format with + an interface similar to that of stdio, using the functions that start with + "gz". The gzip format is different from the zlib format. gzip is a gzip + wrapper, documented in RFC 1952, wrapped around a deflate stream. +*/ + +typedef struct gzFile_s *gzFile; /* semi-opaque gzip file descriptor */ + +/* +ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode)); + + Opens a gzip (.gz) file for reading or writing. The mode parameter is as + in fopen ("rb" or "wb") but can also include a compression level ("wb9") or + a strategy: 'f' for filtered data as in "wb6f", 'h' for Huffman-only + compression as in "wb1h", 'R' for run-length encoding as in "wb1R", or 'F' + for fixed code compression as in "wb9F". (See the description of + deflateInit2 for more information about the strategy parameter.) 'T' will + request transparent writing or appending with no compression and not using + the gzip format. + + "a" can be used instead of "w" to request that the gzip stream that will + be written be appended to the file. "+" will result in an error, since + reading and writing to the same gzip file is not supported. The addition of + "x" when writing will create the file exclusively, which fails if the file + already exists. On systems that support it, the addition of "e" when + reading or writing will set the flag to close the file on an execve() call. + + These functions, as well as gzip, will read and decode a sequence of gzip + streams in a file. The append function of gzopen() can be used to create + such a file. (Also see gzflush() for another way to do this.) When + appending, gzopen does not test whether the file begins with a gzip stream, + nor does it look for the end of the gzip streams to begin appending. gzopen + will simply append a gzip stream to the existing file. + + gzopen can be used to read a file which is not in gzip format; in this + case gzread will directly read from the file without decompression. When + reading, this will be detected automatically by looking for the magic two- + byte gzip header. + + gzopen returns NULL if the file could not be opened, if there was + insufficient memory to allocate the gzFile state, or if an invalid mode was + specified (an 'r', 'w', or 'a' was not provided, or '+' was provided). + errno can be checked to determine if the reason gzopen failed was that the + file could not be opened. +*/ + +ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode)); +/* + gzdopen associates a gzFile with the file descriptor fd. File descriptors + are obtained from calls like open, dup, creat, pipe or fileno (if the file + has been previously opened with fopen). The mode parameter is as in gzopen. + + The next call of gzclose on the returned gzFile will also close the file + descriptor fd, just like fclose(fdopen(fd, mode)) closes the file descriptor + fd. If you want to keep fd open, use fd = dup(fd_keep); gz = gzdopen(fd, + mode);. The duplicated descriptor should be saved to avoid a leak, since + gzdopen does not close fd if it fails. If you are using fileno() to get the + file descriptor from a FILE *, then you will have to use dup() to avoid + double-close()ing the file descriptor. Both gzclose() and fclose() will + close the associated file descriptor, so they need to have different file + descriptors. + + gzdopen returns NULL if there was insufficient memory to allocate the + gzFile state, if an invalid mode was specified (an 'r', 'w', or 'a' was not + provided, or '+' was provided), or if fd is -1. The file descriptor is not + used until the next gz* read, write, seek, or close operation, so gzdopen + will not detect if fd is invalid (unless fd is -1). +*/ + +ZEXTERN int ZEXPORT gzbuffer OF((gzFile file, unsigned size)); +/* + Set the internal buffer size used by this library's functions. The + default buffer size is 8192 bytes. This function must be called after + gzopen() or gzdopen(), and before any other calls that read or write the + file. The buffer memory allocation is always deferred to the first read or + write. Three times that size in buffer space is allocated. A larger buffer + size of, for example, 64K or 128K bytes will noticeably increase the speed + of decompression (reading). + + The new buffer size also affects the maximum length for gzprintf(). + + gzbuffer() returns 0 on success, or -1 on failure, such as being called + too late. +*/ + +ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy)); +/* + Dynamically update the compression level or strategy. See the description + of deflateInit2 for the meaning of these parameters. Previously provided + data is flushed before the parameter change. + + gzsetparams returns Z_OK if success, Z_STREAM_ERROR if the file was not + opened for writing, Z_ERRNO if there is an error writing the flushed data, + or Z_MEM_ERROR if there is a memory allocation error. +*/ + +ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len)); +/* + Reads the given number of uncompressed bytes from the compressed file. If + the input file is not in gzip format, gzread copies the given number of + bytes into the buffer directly from the file. + + After reaching the end of a gzip stream in the input, gzread will continue + to read, looking for another gzip stream. Any number of gzip streams may be + concatenated in the input file, and will all be decompressed by gzread(). + If something other than a gzip stream is encountered after a gzip stream, + that remaining trailing garbage is ignored (and no error is returned). + + gzread can be used to read a gzip file that is being concurrently written. + Upon reaching the end of the input, gzread will return with the available + data. If the error code returned by gzerror is Z_OK or Z_BUF_ERROR, then + gzclearerr can be used to clear the end of file indicator in order to permit + gzread to be tried again. Z_OK indicates that a gzip stream was completed + on the last gzread. Z_BUF_ERROR indicates that the input file ended in the + middle of a gzip stream. Note that gzread does not return -1 in the event + of an incomplete gzip stream. This error is deferred until gzclose(), which + will return Z_BUF_ERROR if the last gzread ended in the middle of a gzip + stream. Alternatively, gzerror can be used before gzclose to detect this + case. + + gzread returns the number of uncompressed bytes actually read, less than + len for end of file, or -1 for error. If len is too large to fit in an int, + then nothing is read, -1 is returned, and the error state is set to + Z_STREAM_ERROR. +*/ + +ZEXTERN z_size_t ZEXPORT gzfread OF((voidp buf, z_size_t size, z_size_t nitems, + gzFile file)); +/* + Read up to nitems items of size size from file to buf, otherwise operating + as gzread() does. This duplicates the interface of stdio's fread(), with + size_t request and return types. If the library defines size_t, then + z_size_t is identical to size_t. If not, then z_size_t is an unsigned + integer type that can contain a pointer. + + gzfread() returns the number of full items read of size size, or zero if + the end of the file was reached and a full item could not be read, or if + there was an error. gzerror() must be consulted if zero is returned in + order to determine if there was an error. If the multiplication of size and + nitems overflows, i.e. the product does not fit in a z_size_t, then nothing + is read, zero is returned, and the error state is set to Z_STREAM_ERROR. + + In the event that the end of file is reached and only a partial item is + available at the end, i.e. the remaining uncompressed data length is not a + multiple of size, then the final partial item is nevetheless read into buf + and the end-of-file flag is set. The length of the partial item read is not + provided, but could be inferred from the result of gztell(). This behavior + is the same as the behavior of fread() implementations in common libraries, + but it prevents the direct use of gzfread() to read a concurrently written + file, reseting and retrying on end-of-file, when size is not 1. +*/ + +ZEXTERN int ZEXPORT gzwrite OF((gzFile file, + voidpc buf, unsigned len)); +/* + Writes the given number of uncompressed bytes into the compressed file. + gzwrite returns the number of uncompressed bytes written or 0 in case of + error. +*/ + +ZEXTERN z_size_t ZEXPORT gzfwrite OF((voidpc buf, z_size_t size, + z_size_t nitems, gzFile file)); +/* + gzfwrite() writes nitems items of size size from buf to file, duplicating + the interface of stdio's fwrite(), with size_t request and return types. If + the library defines size_t, then z_size_t is identical to size_t. If not, + then z_size_t is an unsigned integer type that can contain a pointer. + + gzfwrite() returns the number of full items written of size size, or zero + if there was an error. If the multiplication of size and nitems overflows, + i.e. the product does not fit in a z_size_t, then nothing is written, zero + is returned, and the error state is set to Z_STREAM_ERROR. +*/ + +ZEXTERN int ZEXPORTVA gzprintf Z_ARG((gzFile file, const char *format, ...)); +/* + Converts, formats, and writes the arguments to the compressed file under + control of the format string, as in fprintf. gzprintf returns the number of + uncompressed bytes actually written, or a negative zlib error code in case + of error. The number of uncompressed bytes written is limited to 8191, or + one less than the buffer size given to gzbuffer(). The caller should assure + that this limit is not exceeded. If it is exceeded, then gzprintf() will + return an error (0) with nothing written. In this case, there may also be a + buffer overflow with unpredictable consequences, which is possible only if + zlib was compiled with the insecure functions sprintf() or vsprintf() + because the secure snprintf() or vsnprintf() functions were not available. + This can be determined using zlibCompileFlags(). +*/ + +ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s)); +/* + Writes the given null-terminated string to the compressed file, excluding + the terminating null character. + + gzputs returns the number of characters written, or -1 in case of error. +*/ + +ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len)); +/* + Reads bytes from the compressed file until len-1 characters are read, or a + newline character is read and transferred to buf, or an end-of-file + condition is encountered. If any characters are read or if len == 1, the + string is terminated with a null character. If no characters are read due + to an end-of-file or len < 1, then the buffer is left untouched. + + gzgets returns buf which is a null-terminated string, or it returns NULL + for end-of-file or in case of error. If there was an error, the contents at + buf are indeterminate. +*/ + +ZEXTERN int ZEXPORT gzputc OF((gzFile file, int c)); +/* + Writes c, converted to an unsigned char, into the compressed file. gzputc + returns the value that was written, or -1 in case of error. +*/ + +ZEXTERN int ZEXPORT gzgetc OF((gzFile file)); +/* + Reads one byte from the compressed file. gzgetc returns this byte or -1 + in case of end of file or error. This is implemented as a macro for speed. + As such, it does not do all of the checking the other functions do. I.e. + it does not check to see if file is NULL, nor whether the structure file + points to has been clobbered or not. +*/ + +ZEXTERN int ZEXPORT gzungetc OF((int c, gzFile file)); +/* + Push one character back onto the stream to be read as the first character + on the next read. At least one character of push-back is allowed. + gzungetc() returns the character pushed, or -1 on failure. gzungetc() will + fail if c is -1, and may fail if a character has been pushed but not read + yet. If gzungetc is used immediately after gzopen or gzdopen, at least the + output buffer size of pushed characters is allowed. (See gzbuffer above.) + The pushed character will be discarded if the stream is repositioned with + gzseek() or gzrewind(). +*/ + +ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush)); +/* + Flushes all pending output into the compressed file. The parameter flush + is as in the deflate() function. The return value is the zlib error number + (see function gzerror below). gzflush is only permitted when writing. + + If the flush parameter is Z_FINISH, the remaining data is written and the + gzip stream is completed in the output. If gzwrite() is called again, a new + gzip stream will be started in the output. gzread() is able to read such + concatenated gzip streams. + + gzflush should be called only when strictly necessary because it will + degrade compression if called too often. +*/ + +/* +ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file, + z_off_t offset, int whence)); + + Sets the starting position for the next gzread or gzwrite on the given + compressed file. The offset represents a number of bytes in the + uncompressed data stream. The whence parameter is defined as in lseek(2); + the value SEEK_END is not supported. + + If the file is opened for reading, this function is emulated but can be + extremely slow. If the file is opened for writing, only forward seeks are + supported; gzseek then compresses a sequence of zeroes up to the new + starting position. + + gzseek returns the resulting offset location as measured in bytes from + the beginning of the uncompressed stream, or -1 in case of error, in + particular if the file is opened for writing and the new starting position + would be before the current position. +*/ + +ZEXTERN int ZEXPORT gzrewind OF((gzFile file)); +/* + Rewinds the given file. This function is supported only for reading. + + gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET) +*/ + +/* +ZEXTERN z_off_t ZEXPORT gztell OF((gzFile file)); + + Returns the starting position for the next gzread or gzwrite on the given + compressed file. This position represents a number of bytes in the + uncompressed data stream, and is zero when starting, even if appending or + reading a gzip stream from the middle of a file using gzdopen(). + + gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR) +*/ + +/* +ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile file)); + + Returns the current offset in the file being read or written. This offset + includes the count of bytes that precede the gzip stream, for example when + appending or when using gzdopen() for reading. When reading, the offset + does not include as yet unused buffered input. This information can be used + for a progress indicator. On error, gzoffset() returns -1. +*/ + +ZEXTERN int ZEXPORT gzeof OF((gzFile file)); +/* + Returns true (1) if the end-of-file indicator has been set while reading, + false (0) otherwise. Note that the end-of-file indicator is set only if the + read tried to go past the end of the input, but came up short. Therefore, + just like feof(), gzeof() may return false even if there is no more data to + read, in the event that the last read request was for the exact number of + bytes remaining in the input file. This will happen if the input file size + is an exact multiple of the buffer size. + + If gzeof() returns true, then the read functions will return no more data, + unless the end-of-file indicator is reset by gzclearerr() and the input file + has grown since the previous end of file was detected. +*/ + +ZEXTERN int ZEXPORT gzdirect OF((gzFile file)); +/* + Returns true (1) if file is being copied directly while reading, or false + (0) if file is a gzip stream being decompressed. + + If the input file is empty, gzdirect() will return true, since the input + does not contain a gzip stream. + + If gzdirect() is used immediately after gzopen() or gzdopen() it will + cause buffers to be allocated to allow reading the file to determine if it + is a gzip file. Therefore if gzbuffer() is used, it should be called before + gzdirect(). + + When writing, gzdirect() returns true (1) if transparent writing was + requested ("wT" for the gzopen() mode), or false (0) otherwise. (Note: + gzdirect() is not needed when writing. Transparent writing must be + explicitly requested, so the application already knows the answer. When + linking statically, using gzdirect() will include all of the zlib code for + gzip file reading and decompression, which may not be desired.) +*/ + +ZEXTERN int ZEXPORT gzclose OF((gzFile file)); +/* + Flushes all pending output if necessary, closes the compressed file and + deallocates the (de)compression state. Note that once file is closed, you + cannot call gzerror with file, since its structures have been deallocated. + gzclose must not be called more than once on the same file, just as free + must not be called more than once on the same allocation. + + gzclose will return Z_STREAM_ERROR if file is not valid, Z_ERRNO on a + file operation error, Z_MEM_ERROR if out of memory, Z_BUF_ERROR if the + last read ended in the middle of a gzip stream, or Z_OK on success. +*/ + +ZEXTERN int ZEXPORT gzclose_r OF((gzFile file)); +ZEXTERN int ZEXPORT gzclose_w OF((gzFile file)); +/* + Same as gzclose(), but gzclose_r() is only for use when reading, and + gzclose_w() is only for use when writing or appending. The advantage to + using these instead of gzclose() is that they avoid linking in zlib + compression or decompression code that is not used when only reading or only + writing respectively. If gzclose() is used, then both compression and + decompression code will be included the application when linking to a static + zlib library. +*/ + +ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum)); +/* + Returns the error message for the last error which occurred on the given + compressed file. errnum is set to zlib error number. If an error occurred + in the file system and not in the compression library, errnum is set to + Z_ERRNO and the application may consult errno to get the exact error code. + + The application must not modify the returned string. Future calls to + this function may invalidate the previously returned string. If file is + closed, then the string previously returned by gzerror will no longer be + available. + + gzerror() should be used to distinguish errors from end-of-file for those + functions above that do not distinguish those cases in their return values. +*/ + +ZEXTERN void ZEXPORT gzclearerr OF((gzFile file)); +/* + Clears the error and end-of-file flags for file. This is analogous to the + clearerr() function in stdio. This is useful for continuing to read a gzip + file that is being written concurrently. +*/ + +#endif /* !Z_SOLO */ + + /* checksum functions */ + +/* + These functions are not related to compression but are exported + anyway because they might be useful in applications using the compression + library. +*/ + +ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len)); +/* + Update a running Adler-32 checksum with the bytes buf[0..len-1] and + return the updated checksum. If buf is Z_NULL, this function returns the + required initial value for the checksum. + + An Adler-32 checksum is almost as reliable as a CRC-32 but can be computed + much faster. + + Usage example: + + uLong adler = adler32(0L, Z_NULL, 0); + + while (read_buffer(buffer, length) != EOF) { + adler = adler32(adler, buffer, length); + } + if (adler != original_adler) error(); +*/ + +ZEXTERN uLong ZEXPORT adler32_z OF((uLong adler, const Bytef *buf, + z_size_t len)); +/* + Same as adler32(), but with a size_t length. +*/ + +/* +ZEXTERN uLong ZEXPORT adler32_combine OF((uLong adler1, uLong adler2, + z_off_t len2)); + + Combine two Adler-32 checksums into one. For two sequences of bytes, seq1 + and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for + each, adler1 and adler2. adler32_combine() returns the Adler-32 checksum of + seq1 and seq2 concatenated, requiring only adler1, adler2, and len2. Note + that the z_off_t type (like off_t) is a signed integer. If len2 is + negative, the result has no meaning or utility. +*/ + +ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len)); +/* + Update a running CRC-32 with the bytes buf[0..len-1] and return the + updated CRC-32. If buf is Z_NULL, this function returns the required + initial value for the crc. Pre- and post-conditioning (one's complement) is + performed within this function so it shouldn't be done by the application. + + Usage example: + + uLong crc = crc32(0L, Z_NULL, 0); + + while (read_buffer(buffer, length) != EOF) { + crc = crc32(crc, buffer, length); + } + if (crc != original_crc) error(); +*/ + +ZEXTERN uLong ZEXPORT crc32_z OF((uLong adler, const Bytef *buf, + z_size_t len)); +/* + Same as crc32(), but with a size_t length. +*/ + +/* +ZEXTERN uLong ZEXPORT crc32_combine OF((uLong crc1, uLong crc2, z_off_t len2)); + + Combine two CRC-32 check values into one. For two sequences of bytes, + seq1 and seq2 with lengths len1 and len2, CRC-32 check values were + calculated for each, crc1 and crc2. crc32_combine() returns the CRC-32 + check value of seq1 and seq2 concatenated, requiring only crc1, crc2, and + len2. +*/ + + + /* various hacks, don't look :) */ + +/* deflateInit and inflateInit are macros to allow checking the zlib version + * and the compiler's view of z_stream: + */ +ZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level, + const char *version, int stream_size)); +ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm, + const char *version, int stream_size)); +ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int level, int method, + int windowBits, int memLevel, + int strategy, const char *version, + int stream_size)); +ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int windowBits, + const char *version, int stream_size)); +ZEXTERN int ZEXPORT inflateBackInit_ OF((z_streamp strm, int windowBits, + unsigned char FAR *window, + const char *version, + int stream_size)); +#ifdef Z_PREFIX_SET +# define z_deflateInit(strm, level) \ + deflateInit_((strm), (level), ZLIB_VERSION, (int)sizeof(z_stream)) +# define z_inflateInit(strm) \ + inflateInit_((strm), ZLIB_VERSION, (int)sizeof(z_stream)) +# define z_deflateInit2(strm, level, method, windowBits, memLevel, strategy) \ + deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\ + (strategy), ZLIB_VERSION, (int)sizeof(z_stream)) +# define z_inflateInit2(strm, windowBits) \ + inflateInit2_((strm), (windowBits), ZLIB_VERSION, \ + (int)sizeof(z_stream)) +# define z_inflateBackInit(strm, windowBits, window) \ + inflateBackInit_((strm), (windowBits), (window), \ + ZLIB_VERSION, (int)sizeof(z_stream)) +#else +# define deflateInit(strm, level) \ + deflateInit_((strm), (level), ZLIB_VERSION, (int)sizeof(z_stream)) +# define inflateInit(strm) \ + inflateInit_((strm), ZLIB_VERSION, (int)sizeof(z_stream)) +# define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \ + deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\ + (strategy), ZLIB_VERSION, (int)sizeof(z_stream)) +# define inflateInit2(strm, windowBits) \ + inflateInit2_((strm), (windowBits), ZLIB_VERSION, \ + (int)sizeof(z_stream)) +# define inflateBackInit(strm, windowBits, window) \ + inflateBackInit_((strm), (windowBits), (window), \ + ZLIB_VERSION, (int)sizeof(z_stream)) +#endif + +#ifndef Z_SOLO + +/* gzgetc() macro and its supporting function and exposed data structure. Note + * that the real internal state is much larger than the exposed structure. + * This abbreviated structure exposes just enough for the gzgetc() macro. The + * user should not mess with these exposed elements, since their names or + * behavior could change in the future, perhaps even capriciously. They can + * only be used by the gzgetc() macro. You have been warned. + */ +struct gzFile_s { + unsigned have; + unsigned char *next; + z_off64_t pos; +}; +ZEXTERN int ZEXPORT gzgetc_ OF((gzFile file)); /* backward compatibility */ +#ifdef Z_PREFIX_SET +# undef z_gzgetc +# define z_gzgetc(g) \ + ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : (gzgetc)(g)) +#else +# define gzgetc(g) \ + ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : (gzgetc)(g)) +#endif + +/* provide 64-bit offset functions if _LARGEFILE64_SOURCE defined, and/or + * change the regular functions to 64 bits if _FILE_OFFSET_BITS is 64 (if + * both are true, the application gets the *64 functions, and the regular + * functions are changed to 64 bits) -- in case these are set on systems + * without large file support, _LFS64_LARGEFILE must also be true + */ +#ifdef Z_LARGE64 + ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *)); + ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int)); + ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile)); + ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile)); + ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off64_t)); + ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off64_t)); +#endif + +#if !defined(ZLIB_INTERNAL) && defined(Z_WANT64) +# ifdef Z_PREFIX_SET +# define z_gzopen z_gzopen64 +# define z_gzseek z_gzseek64 +# define z_gztell z_gztell64 +# define z_gzoffset z_gzoffset64 +# define z_adler32_combine z_adler32_combine64 +# define z_crc32_combine z_crc32_combine64 +# else +# define gzopen gzopen64 +# define gzseek gzseek64 +# define gztell gztell64 +# define gzoffset gzoffset64 +# define adler32_combine adler32_combine64 +# define crc32_combine crc32_combine64 +# endif +# ifndef Z_LARGE64 + ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *)); + ZEXTERN z_off_t ZEXPORT gzseek64 OF((gzFile, z_off_t, int)); + ZEXTERN z_off_t ZEXPORT gztell64 OF((gzFile)); + ZEXTERN z_off_t ZEXPORT gzoffset64 OF((gzFile)); + ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t)); + ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t)); +# endif +#else + ZEXTERN gzFile ZEXPORT gzopen OF((const char *, const char *)); + ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile, z_off_t, int)); + ZEXTERN z_off_t ZEXPORT gztell OF((gzFile)); + ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile)); + ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t)); + ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t)); +#endif + +#else /* Z_SOLO */ + + ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t)); + ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t)); + +#endif /* !Z_SOLO */ + +/* undocumented functions */ +ZEXTERN const char * ZEXPORT zError OF((int)); +ZEXTERN int ZEXPORT inflateSyncPoint OF((z_streamp)); +ZEXTERN const z_crc_t FAR * ZEXPORT get_crc_table OF((void)); +ZEXTERN int ZEXPORT inflateUndermine OF((z_streamp, int)); +ZEXTERN int ZEXPORT inflateValidate OF((z_streamp, int)); +ZEXTERN unsigned long ZEXPORT inflateCodesUsed OF ((z_streamp)); +ZEXTERN int ZEXPORT inflateResetKeep OF((z_streamp)); +ZEXTERN int ZEXPORT deflateResetKeep OF((z_streamp)); +#if (defined(_WIN32) || defined(__CYGWIN__)) && !defined(Z_SOLO) +ZEXTERN gzFile ZEXPORT gzopen_w OF((const wchar_t *path, + const char *mode)); +#endif +#if defined(STDC) || defined(Z_HAVE_STDARG_H) +# ifndef Z_SOLO +ZEXTERN int ZEXPORTVA gzvprintf Z_ARG((gzFile file, + const char *format, + va_list va)); +# endif +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* ZLIB_H */ diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..34ffc55 --- /dev/null +++ b/main.cpp @@ -0,0 +1,47 @@ +#include "mainwindow.h" +#include +#include "cplaywidget.h" +#include +#include "CameraCapture.h" +#include "mainwindow.h" +#include +#include +#include +#include "media/screen_capture.h" +#include "media/DXGICapture.h" +#include +#include +#include +#include +#include + +#if _MSC_VER >= 1600 +#pragma execution_character_set("utf-8") +#endif + +#ifdef __MINGW32__ +#include +#include "winuser.h" +#endif + +int RegiesterOwnType(){ + return 0; +} + +int main(int argc, char *argv[]) +{ + setbuf(stdout, NULL);//printf + ScreenCapture p; + p.EnumScreen(); + + Direct3D9TakeScreenshots(0,4); + QssEventFilter filter; + QApplication app(argc, argv); + + MainWindow main; + + main.setWindowTitle("ýԹ"); + main.setFixedSize(1920,1080); + main.show(); + return app.exec(); +} diff --git a/mainwindow.cpp b/mainwindow.cpp new file mode 100644 index 0000000..2ce41cf --- /dev/null +++ b/mainwindow.cpp @@ -0,0 +1,140 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" +#include +#include + +#if _MSC_VER >= 1600 +#pragma execution_character_set("utf-8") +#endif + +MainWindow::MainWindow(QWidget *parent) : + QssMainWindow(parent,0,1.5), + ui(new Ui::MainWindow), + m_bCameraOpen(false), + mCamera(nullptr), + m_bRtmpPushing(false), + mPlayerWidget(nullptr), + mVideoCoder(nullptr), + mPusher(nullptr), + mAudioCapture(nullptr), + mTimer(nullptr) +{ + ui->setupUi(this); + this->move(50,50); + int i(0); + QDesktopWidget* desktopWidget = QApplication::desktop(); + QRect clientRect = desktopWidget->availableGeometry(); + QRect applicationRect = desktopWidget->screenGeometry(); + + qDebug()<pos()<m_frame->geometry()<centralWidget()->geometry(); + + std::vector cameras = Camera::EnumAllCamera(); + for(std::wstring x : cameras){ + ui->comboBox->addItem(QString::fromWCharArray(x.c_str(),x.size()), + QString::fromWCharArray(x.c_str(),x.size())); + } + mAudioCapture = new CaptureAudioFfmpeg(44100, 2); + mMic = mAudioCapture->EnumSpeakers(); + qDebug()<<"capture "<::iterator itr = mMic.begin();itr != mMic.end();itr++){ + qDebug()<name)<index; + + } + + mTimer = new QTimer(this); + connect(mTimer, SIGNAL(timeout()), this, SLOT(DetectDpi())); + mTimer->start(100); + +} + +MainWindow::~MainWindow(){ + delete ui; +} + +void MainWindow::on_pushButton_clicked(){ + if(nullptr == mPlayerWidget){ + mPlayerWidget = new CPlayWidget(nullptr); + } + if(!m_bCameraOpen){ + mPlayerWidget->SetDataType(CPlayWidget::IMG_TYPE::TYPE_RGB32); + mPlayerWidget->SetImgSize(640,480); + + qDebug()<comboBox->currentText().size()<comboBox->currentText(); + wchar_t *opencamera = new wchar_t[ui->comboBox->currentText().size()]; + ui->comboBox->currentText().toWCharArray(opencamera); + wstring ss = wstring(opencamera,ui->comboBox->currentText().size()); + if(nullptr == mCamera){ + this->mCamera = new Camera(ss); + } + this->mCamera->SetObserver(mPlayerWidget); + qDebug()<comboBox->currentText(); + ui->pushButton->setText("关闭摄像头"); + m_bCameraOpen = true; + mPlayerWidget->show(); + ui->verticalLayout->addWidget(mPlayerWidget); + qDebug()<verticalLayout->layout(); + ui->verticalLayout->setStretch(0,1); + ui->verticalLayout->setStretch(1,0); + ui->verticalLayout->setStretch(2,9); + + } else{ + m_bCameraOpen = false; + ui->pushButton->setText("打开摄像头"); + } +} + +void MainWindow::on_pushButton_2_clicked() +{ + if(!m_bRtmpPushing){ + if(!m_bCameraOpen){ + ToastWidget::showTip("请打开摄像头",this); + return; + }else{ + // + if(nullptr == mVideoCoder){ + mVideoCoder = new VideoCoder(mCamera->GetWidth(), + mCamera->GetHeight(), + GUIDToAvFormat(mCamera->MediaType())); + } + mCamera->SetObserver(mVideoCoder); + + // todo 根据返回结果判断是否推流 + qDebug()<<"连接RTMP服务器"<lineEdit->text(); + if (!mPusher->IfConnect()) { + const char* address = ui->lineEdit->text().toLocal8Bit().data(); + qDebug()<RTMP264_Connect("rtmp://127.0.0.1:1939/live/1")) { + ToastWidget::showTip("已经连接上RTMP服务器",this->parentWidget()); + mVideoCoder->SetOberver(mPusher); + mPusher->StartPush(); + ui->pushButton_2->setText("关闭推流"); + /* + if (nullptr != this->mAudioCoder) { + this->mAudioCoder->SetObserver(mPusher); + //音频流先不推流 + }*/ + } + else { + ToastWidget::showTip("连接RTMP服务器失败,请检查服务器地址",this->parentWidget()); + } + }else{ + ToastWidget::showTip("正在推流,请先关闭",this->parentWidget()); + } + } + } +} + +void MainWindow::on_pushButton_3_clicked() +{ + qDebug()<comboBox_2->currentText(); +} + +void MainWindow::DetectDpi() +{ +// qDebug()<<"detect dpi"; + int horizontalDPI = logicalDpiX(); + int verticalDPI = logicalDpiY(); + +// qDebug()< +#include "media/CameraCapture.h" +#include "cplaywidget.h" +#include "media/VideoCoder.h" +#include "media/RtmpPusher.h" +#include "components/toast.h" +#include "utils.h" +#include "Qss.h" +#include "media/audiocaptureff.h" +#include +using namespace std; + +namespace Ui { +class MainWindow; +} + +class MainWindow : public QssMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + +private slots: + void on_pushButton_clicked(); + void on_pushButton_2_clicked(); + void on_pushButton_3_clicked(); + void DetectDpi(); +private: + Ui::MainWindow *ui; + Camera *mCamera; + QStringList mCameraList; + bool m_bCameraOpen; + CPlayWidget *mPlayerWidget; + VideoCoder *mVideoCoder; + bool m_bRtmpPushing; + H264RtmpPuser *mPusher; + CaptureAudioFfmpeg *mAudioCapture; + vector mMic; + QTimer *mTimer; + +}; + +#endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui new file mode 100644 index 0000000..45d9f32 --- /dev/null +++ b/mainwindow.ui @@ -0,0 +1,185 @@ + + + MainWindow + + + + 0 + 0 + 1383 + 1116 + + + + + 0 + 0 + + + + + 600 + 800 + + + + MainWindow + + + + + 0 + 0 + + + + + + + + + 2 + + + 2 + + + 2 + + + 1 + + + + + + 100 + 50 + + + + + 0 + 6 + + + + + 0 + 50 + + + + ??????? + + + + + + + + 200 + 35 + + + + + 0 + 50 + + + + + + + + + 100 + 50 + + + + ?????? + + + + + + + + 200 + 35 + + + + + + + + rtmp??????? + + + + + + + + 300 + 30 + + + + rtmp://127.0.0.1:1935/live/1 + + + + + + + + 60 + 50 + + + + ???? + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + + + diff --git a/media/AACAudioCoder.cpp b/media/AACAudioCoder.cpp new file mode 100644 index 0000000..3fed772 --- /dev/null +++ b/media/AACAudioCoder.cpp @@ -0,0 +1,145 @@ +#include "AACAudioCoder.h" +//#include "Debuger.h" + +using namespace AAC_CODER; +AACAudioCoder::~AACAudioCoder() { + +} + + +void AACAudioCoder::OnAudioData(const void *frameaddress, uint32_t framelen) +{ + this->Encode((unsigned char *)frameaddress, framelen * 4); +} + +AACAudioCoder::AACAudioCoder(unsigned int smprate, unsigned int channel) { + AVCodecID codec_id = AV_CODEC_ID_AAC; + + pCodec = (AVCodec *)avcodec_find_encoder_by_name("libfdk_aac"); + if (!pCodec) { + printf("Codec not found\n"); + this->mStatus = FAIL; + } + mCodecCtx = avcodec_alloc_context3(pCodec); + if (!mCodecCtx) { + printf("Could not allocate video codec context\n"); + this->mStatus = FAIL; + } + + mCodecCtx->codec_id = pCodec->id; + mCodecCtx->codec_type = AVMEDIA_TYPE_AUDIO; + mCodecCtx->sample_fmt = AV_SAMPLE_FMT_S16; ///< float + mCodecCtx->sample_rate = 44100; + mCodecCtx->channel_layout = AV_CH_LAYOUT_STEREO; + mCodecCtx->channels = 2; + mCodecCtx->bit_rate = 640000; + mCodecCtx->time_base.den = 1; + mCodecCtx->time_base.num = 23; + mCodecCtx->frame_size = 1024; + this->mObserver = nullptr; + + if (avcodec_open2(mCodecCtx, pCodec, NULL) < 0) { + this->mStatus = FAIL; + } + mFrame = av_frame_alloc(); + mFrame->nb_samples = mCodecCtx->frame_size; + mFrame->format = mCodecCtx->sample_fmt; + int size = av_samples_get_buffer_size(NULL, mCodecCtx->channels, mCodecCtx->frame_size, mCodecCtx->sample_fmt, 1); + mFrameBuf = (uint8_t *)av_malloc(size); + avcodec_fill_audio_frame(mFrame, mCodecCtx->channels, mCodecCtx->sample_fmt, (const uint8_t*)mFrameBuf, size, 1); + mPts = 0; +} + +int adts_sample_rates[]={96000,882000,64000,48000,441000,32000,24000,22050,16000,12000,11025,8000,7350,0,0,0}; + +int FindAdstSRIndex(int samplerate) +{ + int i; + for (i = 0; i < 16; i++) + { + if (samplerate == adts_sample_rates[i]) + return i; + } + return 16 - 1; +} +#define ADTS_HEAD_LEN 7 + +void MakeAdtsHeader(unsigned char *data, int samplerate, int channels, int iFrameLen) +{ + int profile = 2; //AAC LCMediaCodecInfo.CodecProfileLevel.AACObjectLC; + int freqIdx = 4; //32K, עavpriv_mpeg4audio_sample_rates32000Ӧ±꣬ffmpegԴ + int chanCfg = channels; //עchannel_configurationStero˫ + + /*int avpriv_mpeg4audio_sample_rates[] = { + 96000, 88200, 64000, 48000, 44100, 32000, + 24000, 22050, 16000, 12000, 11025, 8000, 7350 + }; + channel_configuration: ʾchanCfg + 0: Defined in AOT Specifc Config + 1: 1 channel: front-center + 2: 2 channels: front-left, front-right + 3: 3 channels: front-center, front-left, front-right + 4: 4 channels: front-center, front-left, front-right, back-center + 5: 5 channels: front-center, front-left, front-right, back-left, back-right + 6: 6 channels: front-center, front-left, front-right, back-left, back-right, LFE-channel + 7: 8 channels: front-center, front-left, front-right, side-left, side-right, back-left, back-right, LFE-channel + 8-15: Reserved + */ + + // fill in ADTS data + data[0] = (uint8_t)0xFF; + data[1] = (uint8_t)0xF9; + data[2] = (uint8_t)(((profile - 1) << 6) + (freqIdx << 2) + (chanCfg >> 2)); + data[3] = (uint8_t)(((chanCfg & 3) << 6) + (iFrameLen >> 11)); + data[4] = (uint8_t)((iFrameLen & 0x7FF) >> 3); + data[5] = (uint8_t)(((iFrameLen & 7) << 5) + 0x1F); + data[6] = (uint8_t)0xFC; +} + +FILE *ptest = nullptr; +int once = 1; +int AACAudioCoder::Encode( unsigned char *input, unsigned int num) { + + mFrame->nb_samples = mCodecCtx->frame_size; + mFrame->format = mCodecCtx->sample_fmt; + + avcodec_fill_audio_frame(mFrame, mCodecCtx->channels, + mCodecCtx->sample_fmt, input, + num, 1); + + int aac_out_len = 0; + unsigned char*aac_buf; + if (nullptr == input) { + return -1; + } + if (nullptr == ptest) { + ptest = fopen("dst.aac", "wb"); + } + av_init_packet(&pkt); + pkt.data = NULL; // packet data will be allocated by the encoder + pkt.size = 0; + + int got_output = 0; + mFrame->pts = mPts += 23; + int ret = avcodec_encode_audio2(mCodecCtx, &pkt, mFrame, &got_output); + if (ret < 0) { + printf("Error encoding frame\n"); + return -1; + } + if (got_output) { + if (nullptr != mObserver) { + mObserver->OnAudioEncode(pkt.data, pkt.size, mFrame->pts); + } + fwrite(pkt.data, 1, pkt.size, ptest); + av_free_packet(&pkt); + } + return 0; +} + +int AAC_CODER::AACAudioCoder::SetObserver(EncodeAudioObserver *p) +{ + if (nullptr == this->mObserver) { + this->mObserver = p; + } + return 0; +} diff --git a/media/AACAudioCoder.h b/media/AACAudioCoder.h new file mode 100644 index 0000000..a2f9283 --- /dev/null +++ b/media/AACAudioCoder.h @@ -0,0 +1,52 @@ +#pragma once +#include "AudioCapture.h" +#ifdef __cplusplus +extern "C" +{ +#include "libavcodec/avcodec.h" +#include "libavformat/avformat.h" +#include "libavutil/avutil.h" +#include "libswscale/swscale.h" +#include "libavutil/opt.h" +#include "libavutil/imgutils.h" +}; +#endif +namespace AAC_CODER { + class AACAudioCoder :public CaptureAudio::CaptureAudioObserver { + public: + class EncodeAudioObserver { + public: + virtual void OnAudioEncode(const void *frameaddress, uint32_t framelen,uint16_t pts) {}; + }; + enum CAP_STATUS { + RUNNING = 1, + STOP = 2, + PAUSE = 3, + READY = 4, + UNREADY = 5, + FAIL = 6, + }; + void OnAudioData(const void *frameaddress, uint32_t framelen); + AACAudioCoder(unsigned int smprate, unsigned int channel); + ~AACAudioCoder(); + int Encode(unsigned char *input, unsigned int num); + int SetObserver(EncodeAudioObserver *); + private: + unsigned int mpts; + CAP_STATUS mStatus; + unsigned long mSampleRate = 44100; + unsigned int mChannels = 2; + unsigned int mPCMBitSize = 16; + uint8_t* mAACBuffer; + unsigned long nMaxOutputBytes; + uintptr_t mFablaAacenc; + AVCodec *mCodec; + AVCodec *pCodec; + AVCodecContext *mCodecCtx = NULL; + AVFrame *mFrame; + AVPacket pkt; + uint8_t* mFrameBuf; + uint16_t mPts; + EncodeAudioObserver *mObserver; + }; +} diff --git a/media/AACDecoder.cpp b/media/AACDecoder.cpp new file mode 100644 index 0000000..8379c66 --- /dev/null +++ b/media/AACDecoder.cpp @@ -0,0 +1,138 @@ +#include "AACDecoder.h" +#include "Debuger.h" + + +void AACDecoder::OnRtmpFrame(void * dat, uint32_t size) +{ + this->Decode((uint8_t *)dat, size); +} +AACDecoder::AACDecoder() :mObserver(nullptr) +{ + mStatus = RUNNING; + this->mObserverType = Observer_Audio; + mCodec = avcodec_find_decoder(AV_CODEC_ID_AAC); + if (mCodec == NULL) { + Debuger::Debug(L"find codec fail\r\n"); + mStatus = FAIL; + } + mCodecCtx = avcodec_alloc_context3(mCodec); + if (nullptr == mCodecCtx) { + Debuger::Debug(L"find codec ctx fail\r\n"); + mStatus = FAIL; + } + mCodecCtx->codec = mCodec; + mCodecCtx->codec_type = AVMEDIA_TYPE_AUDIO; + mCodecCtx->sample_rate = 44100; + mCodecCtx->channels = 2; + mCodecCtx->channel_layout = AV_CH_LAYOUT_STEREO; + mCodecCtx->sample_fmt = AV_SAMPLE_FMT_FLTP; + mCodecCtx->frame_size = 2048; +#if LIBSWRESAMPLE_VERSION_MINOR >= 17 // ݰ汾ͬѡʵ + mSwrCtx = swr_alloc(); + + av_opt_set_int(mSwrCtx, "in_channel_layout", AV_CH_LAYOUT_STEREO, 0); + av_opt_set_int(mSwrCtx, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0); + av_opt_set_int(mSwrCtx, "in_sample_rate", 44100, 0); + av_opt_set_int(mSwrCtx, "out_sample_rate", 44100, 0); + av_opt_set_sample_fmt(mSwrCtx, "in_sample_fmt", AV_SAMPLE_FMT_FLTP, 0); + av_opt_set_sample_fmt(mSwrCtx, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0); + swr_init(mSwrCtx); + +#else + mSwrCtx = swr_alloc(); + + mSwrCtx = swr_alloc_set_opts(mSwrCtx, + AV_CH_LAYOUT_STEREO, //output + AV_SAMPLE_FMT_S16, + 44100, + AV_CH_LAYOUT_STEREO, // input + AV_SAMPLE_FMT_FLTP, + 44100, + 0, NULL); + swr_init(mSwrCtx); +#endif + + if (avcodec_open2(mCodecCtx, mCodec, NULL) < 0) { + Debuger::Debug(L"can't open codec\r\n"); + mStatus = FAIL; + } + mSampleRate = 44100; + mChannel = 2; + mChannelLayout = AV_CH_LAYOUT_STEREO; + mSampleFmt = AV_SAMPLE_FMT_FLTP; + mStatus = RUNNING; + mU16Data = (uint8_t *)av_malloc(192000); +} +#define MAX_AUDIO_FRAME_SIZE 192000 + +AACDecoder::AACDecoder(AVStream * p):mObserver(nullptr) +{ + mStatus = RUNNING; + this->mObserverType = Observer_Audio; + if (nullptr == p) { + Debuger::Debug(L"find codec fail\r\n"); + mStatus = FAIL; + } + mCodecCtx = p->codec; + mCodec = avcodec_find_decoder(mCodecCtx->codec_id); + if (mCodec == NULL) { + Debuger::Debug(L"find codec fail\r\n"); + mStatus = FAIL; + } + if (avcodec_open2(mCodecCtx, mCodec, NULL) < 0) { + Debuger::Debug(L"can't open codec\r\n"); + mStatus = FAIL; + } + mSampleRate = mCodecCtx->sample_rate; + mChannel = mCodecCtx->channels; + mChannelLayout = mCodecCtx->channel_layout; + mSampleFmt = mCodecCtx->sample_fmt; +} + +int AACDecoder::Decode(uint8_t * dat, uint16_t size) +{ + AVPacket pkt; + int got_pcm = 0; + int len = 0; + + if (mStatus == RUNNING) { + mPcmDat = av_frame_alloc(); + av_init_packet(&pkt); + + char* data = (char*)dat; + pkt.data = (uint8_t *)data; + pkt.size = size; + + len = avcodec_decode_audio4(this->mCodecCtx, mPcmDat, &got_pcm, &pkt); + if (len < 0) { + printf("Error while decoding a frame.\n"); + return -1; + } + if (got_pcm == 0) { + return 0; + } + int buffer_size = av_samples_get_buffer_size(NULL, AV_CH_LAYOUT_STEREO, + mPcmDat->nb_samples, + AV_SAMPLE_FMT_S16, 1); + + swr_convert(mSwrCtx, &mU16Data, buffer_size, (const uint8_t **)mPcmDat->data, + mPcmDat->nb_samples); + + //Debuger::Debug(L"get %d audio samples\r\n", mPcmDat->nb_samples); + if (nullptr != this->mObserver) { + int out_buffer_size = av_samples_get_buffer_size(NULL, 2, mPcmDat->nb_samples, + AV_SAMPLE_FMT_FLTP, 1); + this->mObserver->OnAudioDecode(mU16Data, buffer_size); + } + //(const uint8_t **)mPcmDat->data, mPcmDat->nb_samples; + av_frame_free(&mPcmDat); + return 0; + } +} + +int AACDecoder::SetObserver(AACDecoderObserver *p) +{ + if(nullptr != p) + this->mObserver = p; + return 0; +} diff --git a/media/AACDecoder.h b/media/AACDecoder.h new file mode 100644 index 0000000..c17b4f6 --- /dev/null +++ b/media/AACDecoder.h @@ -0,0 +1,48 @@ +#pragma once + +#include "RtmpPuller2.h" +extern "C" +{ +#include "libavcodec/avcodec.h" +#include "libavformat/avformat.h" +#include "libavutil/avutil.h" +#include "libswscale/swscale.h" +#include "libavutil/opt.h" +#include "libavutil/imgutils.h" +#include "libswresample\swresample.h" +}; + +class AACDecoder :public RtmpPuller2::RtmpPullObserver { +public: + enum DECODE_STATUS { + RUNNING = 1, + STOP = 2, + PAUSE = 3, + FAIL = 4, + NOSOURCE = 6, + }; + class AACDecoderObserver { + public: + virtual int OnAudioDecode(uint8_t *dat, uint16_t size) { return 0; }; + }; + void OnRtmpFrame(void * dat, uint32_t size); + AACDecoder(); + AACDecoder(AVStream *p); + int Decode(uint8_t *dat,uint16_t); + int SetObserver(AACDecoderObserver *); +private: + AVFormatContext *mFormatCtx = nullptr; + AVCodecContext *mCodecCtx = nullptr; + AVCodec *mCodec = nullptr; + AVPacket *mPacket = nullptr; + uint16_t mSampleCnt; + enum AVSampleFormat mSampleFmt; + uint16_t mSampleRate; + uint16_t mChannel; + uint64_t mChannelLayout; + AVFrame *mPcmDat; + uint8_t *mU16Data; + AACDecoderObserver *mObserver; + DECODE_STATUS mStatus; + SwrContext * mSwrCtx; +}; diff --git a/media/AudioCapture.cpp b/media/AudioCapture.cpp new file mode 100644 index 0000000..9abf18c --- /dev/null +++ b/media/AudioCapture.cpp @@ -0,0 +1,133 @@ +#include "AudioCapture.h" +#include "Debuger.h" +PaStream *gStreamOut = nullptr; + +CaptureAudio::CaptureAudio(uint16_t rate, uint8_t channel) { + this->mChanel = channel; + this->mSampleRate = rate; + this->mSize = 0; + this->mStatus = FAIL; + this->observer = nullptr; + +} +/** + * @brief ò񵽵Ļص֪ͨ + * + * @param CaptureAudioObserver ˵ + * @return ˵ + * @retval -1 Ϊָ + * @retval 0ɹ + */ +int CaptureAudio::SetObserver(CaptureAudioObserver* ob) { + if (nullptr == ob) return -1; + this->observer = ob; + return 0; +} + + +int paOutStreamBkss(const void* input, void* output, unsigned long frameCount, + const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags, void * userData) +{ + CaptureAudio *pCap; + Debuger::Debug(L"%d\r\n", frameCount); + if (userData != nullptr) { + pCap = (CaptureAudio *)userData; + pCap->OnCallBack(input,output,frameCount); + } + pCap->AddCnt(4 * frameCount); + return 0; +} + +int CaptureAudio::OnCallBack(const void* input, void* output, unsigned long frameCount) { + if(nullptr != this->observer) + this->observer->OnAudioData(input, frameCount); + return 0; +} + +CaptureAudio::~CaptureAudio() { + if(mInStream != nullptr) + Pa_CloseStream(mInStream); +} + +int CaptureAudio::StartCapture() +{ + PaError err = paNoError; + if (this->mStatus == RUNNING) { + err = Pa_StartStream(mInStream); + if (err != paNoError) { + this->mStatus = FAIL; + } + } + else + return -1; + + return 0; +} + +vector CaptureAudio::EnumSpeakers() +{ + vector ret; + PaError err = Pa_Initialize(); + if (err != paNoError) { + Debuger::Debug(L"init stream error\r\n"); + mStatus = FAIL; + } + //豸 + PaDeviceIndex iNumDevices = Pa_GetDeviceCount(); + if (iNumDevices <= 0) + { + return ret; + } + for (int i = 0; i < iNumDevices; i++) + { + MICInfo ins; + ins.index = i; + const PaDeviceInfo *deviceInfo = Pa_GetDeviceInfo(i); + if (nullptr != deviceInfo) + if (deviceInfo->maxInputChannels > 0) { + ins.name = deviceInfo->name; + ret.push_back(ins); + } + } + return ret; +} + +int CaptureAudio::InitCapture(int index,uint16_t rate, uint8_t channel) { + PaStreamParameters intputParameters; + PaError err = paNoError; + err = Pa_Initialize(); + if (err != paNoError) goto error; + if (index < 0) + { + index = Pa_GetDefaultInputDevice(); + } + if (paNoDevice == index) { + mStatus = FAIL; + return -1; + } + intputParameters.device = index; + intputParameters.channelCount = 2; + intputParameters.sampleFormat = paInt16; + intputParameters.suggestedLatency = Pa_GetDeviceInfo(intputParameters.device)->defaultLowInputLatency; + intputParameters.hostApiSpecificStreamInfo = NULL; + + err = Pa_OpenStream(&mInStream, &intputParameters, NULL, 44100, 1024, + paFramesPerBufferUnspecified, paOutStreamBkss, this); + if (err != paNoError) { + this->mStatus = FAIL; + return -1; + } + this->mStatus = RUNNING; + return 0; +error: + Pa_Terminate(); + return -1; +} + +void CaptureAudio::StopCapture() +{ + if (this->mStatus == RUNNING) { + Pa_StopStream(mInStream); + this->mStatus = STOP; + } +} diff --git a/media/AudioCapture.h b/media/AudioCapture.h new file mode 100644 index 0000000..61433de --- /dev/null +++ b/media/AudioCapture.h @@ -0,0 +1,73 @@ +#ifndef __CAPTUREAUDIO_H__ +#define __CAPTUREAUDIO_H__ +#include "stdint.h" +#include "../third/portaudio/portaudio.h" +#include +#include +//Windows +extern "C" +{ +#include "libavcodec/avcodec.h" +#include "libavformat/avformat.h" +#include "libavutil/avutil.h" +#include "libswscale/swscale.h" +#include "libavutil/opt.h" +#include "libavutil/imgutils.h" +#include "libavdevice/avdevice.h" +#include "libavfilter/avfilter.h" + + +}; +#include +#include +#include +#include "qedit.h" +#include +#include +#include +#include "guiddef.h" +using namespace std; + +typedef int (CbAudio)(const void* input, void* output, unsigned long frameCount, + const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags, void * userData); + +class CaptureAudio { +public: + class CaptureAudioObserver { + public: + virtual void OnAudioData(const void *frameaddress, uint32_t framelen) {}; + }; + typedef struct MICInfo + { + string name; + int index; + }MICInfo; + enum CAP_STATUS { + RUNNING = 1, + STOP = 2, + PAUSE = 3, + FAIL = 4, + }; + + vector EnumSpeakers(); + CaptureAudio(uint16_t rate, uint8_t channel); + ~CaptureAudio(); + int StartCapture(); + int InitCapture(int index,uint16_t rate,uint8_t channel); + void StopCapture(); + int SetObserver(CaptureAudioObserver*); + int OnCallBack(const void* input, void* output, unsigned long frameCount); + void AddCnt(unsigned int x) {this->mSize += x;}; +private: + uint16_t mSampleRate; // + uint16_t mChanel; //ͨ + PaStream *mInStream; + PaStream *mOutStream; + unsigned long mSize; + CAP_STATUS mStatus; + CaptureAudioObserver *observer; +}; + + + +#endif //__CAPTUREAUDIO_H__ diff --git a/media/AudioPlayer.cpp b/media/AudioPlayer.cpp new file mode 100644 index 0000000..6300a72 --- /dev/null +++ b/media/AudioPlayer.cpp @@ -0,0 +1,91 @@ +#include "AudioPlayer.h" +#include "Debuger.h" +#include "utils.h" +AudioPlayer::AudioPlayer(int index) +{ + mStatus = RUNNING; + + PaError err = Pa_Initialize(); + if (err != paNoError) { + Debuger::Debug(L"init stream error\r\n"); + mStatus = FAIL; + } + //豸 + PaDeviceIndex iNumDevices = Pa_GetDeviceCount(); + if (iNumDevices < 0) + { + } + for (int i = 0; i < iNumDevices; i++) + { + const PaDeviceInfo *deviceInfo = Pa_GetDeviceInfo(i); + Debuger::Debug(L"index %d %d %d \r\n",i, + deviceInfo->maxInputChannels, deviceInfo->maxOutputChannels); //ӡ豸 + } + mOutputParameters.device = index; + mOutputParameters.channelCount = 2; //˫ǰ + mOutputParameters.sampleFormat = paInt16; + mOutputParameters.suggestedLatency = Pa_GetDeviceInfo(mOutputParameters.device)->defaultLowOutputLatency; + mOutputParameters.hostApiSpecificStreamInfo = NULL; + + err = Pa_OpenStream(&mOutStream, NULL, &mOutputParameters, 44100, 1024, + paFramesPerBufferUnspecified, NULL, NULL); + if (err != paNoError) { + Debuger::Debug(L"open output stream error\r\n"); + mStatus = FAIL; + goto end; + } + err = Pa_StartStream(mOutStream); + if (err != paNoError) { + Debuger::Debug(L"start stream error\r\n"); + mStatus = FAIL; + } + end: + return; +} + +vector AudioPlayer::EnumSpeakers() +{ + vector ret; + PaError err = Pa_Initialize(); + if (err != paNoError) { + Debuger::Debug(L"init stream error\r\n"); + mStatus = FAIL; + } + //豸 + PaDeviceIndex iNumDevices = Pa_GetDeviceCount(); + if (iNumDevices <= 0) + { + return ret; + } + for (int i = 0; i < iNumDevices; i++) + { + SpeakerInfo ins; + ins.index = i; + const PaDeviceInfo *deviceInfo = Pa_GetDeviceInfo(i); + if(nullptr != deviceInfo) + if (deviceInfo->maxOutputChannels > 0) { + //ins.name = char2wchar(deviceInfo->name); + ret.push_back(ins); + } + } + return ret; +} +int AudioPlayer::Play(uint8_t * data, uint16_t num) +{ + PaError err; + if (mStatus == RUNNING) { + err = Pa_WriteStream(mOutStream, data, num); + if (paNoError != err) { + return -1; + } + } + else { + return -1; + } + return 0; +} +int AudioPlayer::OnAudioDecode(uint8_t * dat, uint16_t size) +{ + return this->Play(dat, 1024); + return 0; +} diff --git a/media/AudioPlayer.h b/media/AudioPlayer.h new file mode 100644 index 0000000..375dbfb --- /dev/null +++ b/media/AudioPlayer.h @@ -0,0 +1,31 @@ +#pragma once +#include "stdint.h" +#include "../third/portaudio/portaudio.h" +#include "AACDecoder.h" + +class AudioPlayer :public AACDecoder::AACDecoderObserver{ +public: + class AudioPlayerObserver{ + public: + virtual int OnAudioPlay(); + }; + typedef struct { + int index; + wstring name; + }SpeakerInfo; + enum PLAY_STATUS { + RUNNING = 1, + STOP = 2, + PAUSE = 3, + FAIL = 4, + }; + AudioPlayer(int index); + vector EnumSpeakers(); + int Play(uint8_t *data,uint16_t num); + int OnAudioDecode(uint8_t *dat, uint16_t size); +private: + PLAY_STATUS mStatus; + PaStreamParameters mOutputParameters; + PaStream *mOutStream; + +}; \ No newline at end of file diff --git a/media/CameraCapture.cpp b/media/CameraCapture.cpp new file mode 100644 index 0000000..5737f73 --- /dev/null +++ b/media/CameraCapture.cpp @@ -0,0 +1,431 @@ +#include "CameraCapture.h" +#include + +#ifdef __MINGW32__ +#pragma comment(lib, "strmiids") +#endif +//define release maco +#define ReleaseInterface(x) \ + if ( nullptr != x ) \ +{ \ + x->Release( ); \ + x = nullptr; \ +} +// Application-defined message to notify app of filter graph events +#define WM_GRAPHNOTIFY WM_APP+100 + +Camera::Camera(): + mInitOK(false), + mVideoHeight(0), + mVideoWidth(0), + mDevFilter(nullptr), + mCaptureGB(nullptr), + mGraphBuilder(nullptr), + mMediaControl(nullptr), + mMediaEvent(nullptr), + mSampGrabber(nullptr), + mIsVideoOpened(false), + mDebug(false) +{ + +} + +GUID Camera::MediaType() +{ + return mMediaType; +} + +Camera::Camera(wstring camera) +{ + mInitOK = false; + mVideoHeight = 0; + mVideoWidth = 0; + mDevFilter = nullptr; + mCaptureGB = nullptr; + mGraphBuilder = nullptr; + mMediaControl = nullptr; + mMediaEvent = nullptr; + mSampGrabber = nullptr; + mIsVideoOpened = false; + if(!this->Open(camera)){ + mStatus = FAIL; + } + mStatus = STOP; +} + +Camera::~Camera() +{ + Close(); + CoUninitialize(); +} + +HRESULT Camera::InitializeEnv() { + HRESULT hr; + + //Create the filter graph + hr = CoCreateInstance(CLSID_FilterGraph, nullptr, CLSCTX_INPROC_SERVER, + IID_IGraphBuilder, (LPVOID*)&mGraphBuilder); + if (FAILED(hr)) + return hr; + + //Create the capture graph builder + hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, nullptr, CLSCTX_INPROC_SERVER, + IID_ICaptureGraphBuilder2, (LPVOID*)&mCaptureGB); + if (FAILED(hr)) + return hr; + + //Obtain interfaces for media control and Video Window + hr = mGraphBuilder->QueryInterface(IID_IMediaControl, (LPVOID*)&mMediaControl); + if (FAILED(hr)) + return hr; + + + hr = mGraphBuilder->QueryInterface(IID_IMediaEventEx, (LPVOID*)&mMediaEvent); + if (FAILED(hr)) + return hr; + + mCaptureGB->SetFiltergraph(mGraphBuilder); + if (FAILED(hr)) + return hr; + return hr; +} + +std::vector Camera::EnumAllCamera(void) { + + std::vector names; + IEnumMoniker *pEnum = nullptr; + // Create the System Device Enumerator. + ICreateDevEnum *pDevEnum; + HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, nullptr, + CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDevEnum)); + + if (SUCCEEDED(hr)) + { + // Create an enumerator for the category. + hr = pDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEnum, 0); + if (hr == S_FALSE) + { + hr = VFW_E_NOT_FOUND; // The category is empty. Treat as an error. + } + pDevEnum->Release(); + } + + if (!SUCCEEDED(hr)) + return std::vector(); + + IMoniker *pMoniker = nullptr; + while (pEnum->Next(1, &pMoniker, nullptr) == S_OK) + { + IPropertyBag *pPropBag; + IBindCtx* bindCtx = nullptr; + LPOLESTR str = nullptr; + VARIANT var; + VariantInit(&var); + + HRESULT hr = pMoniker->BindToStorage(0, 0, IID_PPV_ARGS(&pPropBag)); + if (FAILED(hr)) + { + pMoniker->Release(); + continue; + } + + // Get description or friendly name. + hr = pPropBag->Read(L"Description", &var, 0); + if (FAILED(hr)) + { + hr = pPropBag->Read(L"FriendlyName", &var, 0); + } + if (SUCCEEDED(hr)) + { + names.push_back(var.bstrVal); + VariantClear(&var); + } + + pPropBag->Release(); + pMoniker->Release(); + } + + pEnum->Release(); + + return names; +} + + +HRESULT Camera::BindFilter(int deviceID, IBaseFilter **pBaseFilter) { + ICreateDevEnum *pDevEnum; + IEnumMoniker *pEnumMon; + IMoniker *pMoniker; + HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, nullptr, CLSCTX_INPROC_SERVER, + IID_ICreateDevEnum, (LPVOID*)&pDevEnum); + if (SUCCEEDED(hr)) + { + hr = pDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEnumMon, 0); + if (hr == S_FALSE) + { + hr = VFW_E_NOT_FOUND; + return hr; + } + pEnumMon->Reset(); + ULONG cFetched; + int index = 0; + hr = pEnumMon->Next(1, &pMoniker, &cFetched); + while (hr == S_OK && index <= deviceID) { + IPropertyBag *pProBag; + hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag, (LPVOID*)&pProBag); + if (SUCCEEDED(hr)) { + if (index == deviceID) { + pMoniker->BindToObject(0, 0, IID_IBaseFilter, (LPVOID*)pBaseFilter); + } + } + pMoniker->Release(); + index++; + hr = pEnumMon->Next(1, &pMoniker, &cFetched); + } + pEnumMon->Release(); + } + return hr; +} + +int Camera::SetObserver(CameraObserver *p) { + return this->mSampleGrabberCB.SetObserver(p); +} + +int Camera::RemoveObserver(CameraObserver * p) { + return this->mSampleGrabberCB.RemoveObserver(p); +} + +void Camera::SetDebug(bool isDebug) { + mDebug = isDebug; +} + +int Camera::SampleGrabberCallback::SetObserver(CameraObserver *p) { + if (nullptr == p) + return -1; + mMux.lock(); + for (auto itr = this->mObserver.begin(); itr != mObserver.end(); itr++) { + if (p == *itr) { + mMux.unlock(); + return 0; + } + } + this->mObserver.push_back(p); + mMux.unlock(); + return 0; +} + +int Camera::SampleGrabberCallback::RemoveObserver(CameraObserver * p) +{ + mMux.lock(); + bool founded = false; + auto itrDel = this->mObserver.begin(); + for (auto itr = this->mObserver.begin(); itr != mObserver.end(); itr++) { + if (p == *itr) { + itrDel = itr; + founded = true; + } + } + if (founded) + mObserver.erase(itrDel); + mMux.unlock(); + return 0; +} + + +bool Camera::Open(std::wstring &camera_name) +{ + if (mIsVideoOpened) + return true; + HRESULT hr; +#define CHECK_HR(x) do{ hr = (x); if (FAILED(hr)){ Close(); return false;}}while(0) + + CHECK_HR(InitializeEnv()); + + IBaseFilter *pSampleGrabberFilter , *dest_filter; + + std::vector names = EnumAllCamera(); + + if (names.empty()) + { + Close(); + return false; + } + bool founded = false; + int deviceID = 0; + + for(std::wstring i : names ){ + if(i == camera_name){ + founded = true; + } + } + if (!founded){ + return false; + } + // create grabber filter instance + CHECK_HR(CoCreateInstance(CLSID_SampleGrabber, nullptr, CLSCTX_INPROC_SERVER, + IID_IBaseFilter, (LPVOID*)&pSampleGrabberFilter)); + + // bind source device + CHECK_HR(BindFilter(deviceID, &mDevFilter)); + + // add src filter + CHECK_HR(mGraphBuilder->AddFilter(mDevFilter, L"Video Filter")); + + + // add grabber filter and query interface + CHECK_HR(mGraphBuilder->AddFilter(pSampleGrabberFilter, L"Sample Grabber")); + + CHECK_HR(pSampleGrabberFilter->QueryInterface(IID_ISampleGrabber, (LPVOID*)&mSampGrabber)); + + // find the current bit depth + HDC hdc = GetDC(nullptr); + mBitDepth = GetDeviceCaps(hdc, BITSPIXEL); + ReleaseDC(nullptr, hdc); + + // set the media type for grabber filter + AM_MEDIA_TYPE mediaType; + ZeroMemory(&mediaType, sizeof(AM_MEDIA_TYPE)); + mediaType.majortype = MEDIATYPE_Video; + switch (mBitDepth) + { + case 8: + mediaType.subtype = MEDIASUBTYPE_RGB8; + break; + case 16: + mediaType.subtype = MEDIASUBTYPE_RGB555; + break; + case 24: + mediaType.subtype = MEDIASUBTYPE_RGB24; + break; + case 32: + mediaType.subtype = MEDIASUBTYPE_RGB32; + break; + default: + Close(); + return false; + } + mediaType.formattype = FORMAT_VideoInfo; + hr = mSampGrabber->SetMediaType(&mediaType); + // 意味着最后的数据是丢掉的 + CHECK_HR(CoCreateInstance(CLSID_NullRenderer, nullptr, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void**)(&dest_filter))); + mGraphBuilder->AddFilter(dest_filter, L"nullptrRenderer"); + + // connect source filter to grabber filter + CHECK_HR(mCaptureGB->RenderStream(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video, + mDevFilter, pSampleGrabberFilter, dest_filter)); + + // get connected media type + CHECK_HR(mSampGrabber->GetConnectedMediaType(&mediaType)); + VIDEOINFOHEADER * vih = (VIDEOINFOHEADER*)mediaType.pbFormat; + mVideoWidth = vih->bmiHeader.biWidth; + mVideoHeight = vih->bmiHeader.biHeight; + mPixFmt = mediaType.subtype; + mMediaType = mediaType.subtype; + std::cout<<"guid media type is"<SetOneShot(0)); + + CHECK_HR(mSampGrabber->SetBufferSamples(0)); + + // Use the BufferCB callback method + CHECK_HR(mSampGrabber->SetCallback(&mSampleGrabberCB, 1)); + + mSampleGrabberCB.mNewDataCallBack = mFrameCallBack; + + mMediaControl->Run(); + dest_filter->Release(); + pSampleGrabberFilter->Release(); + + // release resource + if (mediaType.cbFormat != 0) + { + CoTaskMemFree((PVOID)mediaType.pbFormat); + mediaType.cbFormat = 0; + mediaType.pbFormat = nullptr; + } + if (mediaType.pUnk != nullptr) + { + mediaType.pUnk->Release(); + mediaType.pUnk = nullptr; + } + mIsVideoOpened = TRUE; + mStatus = RUNNING; + return true; +} + +bool Camera::Close() { + if (mMediaControl) + { + mMediaControl->Stop(); + } + if (mMediaEvent) + { + mMediaEvent->SetNotifyWindow(NULL, WM_GRAPHNOTIFY, 0); + } + mIsVideoOpened = false; + //release interface + ReleaseInterface(mDevFilter); + ReleaseInterface(mCaptureGB); + ReleaseInterface(mGraphBuilder); + ReleaseInterface(mMediaControl); + ReleaseInterface(mMediaEvent); + ReleaseInterface(mSampGrabber); + + return true; +} + +void Camera::SetCallBack(std::function f) { + mFrameCallBack = f; +} + +ULONG STDMETHODCALLTYPE Camera::SampleGrabberCallback::AddRef() { + return 1; +} + +ULONG STDMETHODCALLTYPE Camera::SampleGrabberCallback::Release() { + return 2; +} + +HRESULT STDMETHODCALLTYPE Camera::SampleGrabberCallback::QueryInterface(REFIID riid, void** ppvObject) { + if (nullptr == ppvObject) return E_POINTER; + if (riid == __uuidof(IUnknown)) + { + *ppvObject = static_cast(this); + return S_OK; + } + if (riid == IID_ISampleGrabberCB) + { + *ppvObject = static_cast(this); + return S_OK; + } + return E_NOTIMPL; +} + +HRESULT STDMETHODCALLTYPE Camera::SampleGrabberCallback::SampleCB(double Time, IMediaSample *pSample) { + return E_NOTIMPL; +} + +HRESULT STDMETHODCALLTYPE Camera::SampleGrabberCallback::BufferCB(double Time, BYTE * pBuffer, long BufferLen) +{ +#ifdef DEBUG_CAMERA + static FILE *p = fopen("camera_test.yuv","wb+"); + fwrite(pBuffer,BufferLen,1,p); + fflush(p); +#endif + + + if (mObserver.size() > 0) { + mMux.lock(); + for (auto itr = this->mObserver.begin(); itr != mObserver.end(); itr++) { + CameraObserver *p = (CameraObserver *)*itr; + p->OnCameraData(pBuffer, BufferLen); + } + mMux.unlock(); + } + return S_OK; +} + + diff --git a/media/CameraCapture.h b/media/CameraCapture.h new file mode 100644 index 0000000..02e8605 --- /dev/null +++ b/media/CameraCapture.h @@ -0,0 +1,95 @@ +#pragma once +#include +#include +#include +#include +#include "qedit.h" +#include +#include +#include "guiddef.h" + +using namespace std; + + +class Camera +{ +public: + enum CAP_STATUS { + RUNNING = 1, + STOP = 2, + PAUSE = 3, + FAIL = 4, + }; + class CameraObserver { + public: + virtual int OnCameraData(uint8_t *dat, uint32_t size) { return 0; }; + }; + + + class SampleGrabberCallback : public ISampleGrabberCB + { + public: + ULONG STDMETHODCALLTYPE AddRef(); + ULONG STDMETHODCALLTYPE Release(); + HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObject); + HRESULT STDMETHODCALLTYPE SampleCB(double Time, IMediaSample *pSample); + HRESULT STDMETHODCALLTYPE BufferCB(double Time, BYTE *pBuffer, long BufferLen); + std::function mNewDataCallBack; + mutex mMux; + + int SetObserver(CameraObserver *); + int RemoveObserver(CameraObserver *p); + private: + vector mObserver; + }; + + + Camera(wstring camera); + Camera(const Camera &) = delete; + Camera& operator =(const Camera&) = delete; + ~Camera(); + +private: + Camera(); + + bool mInitOK; + bool mIsVideoOpened; + + int mVideoWidth, mVideoHeight, mBitDepth; + std::function mFrameCallBack; + + IGraphBuilder *mGraphBuilder; + ICaptureGraphBuilder2 *mCaptureGB; + IMediaControl *mMediaControl; + IBaseFilter *mDevFilter; + ISampleGrabber *mSampGrabber; + IMediaEventEx *mMediaEvent; + + SampleGrabberCallback mSampleGrabberCB; + HRESULT InitializeEnv(); + HRESULT BindFilter(int deviceID, IBaseFilter **pBaseFilter); + GUID mMediaType; + bool mDebug; +public: + int SetObserver(CameraObserver *); + int RemoveObserver(CameraObserver *p); + CAP_STATUS mStatus; + + void SetDebug(bool); + static std::vector EnumAllCamera(void); + GUID mPixFmt; + bool Open(std::wstring &camera_name); + bool Close(void); + /*! + * @param time : Starting time of the sample, in seconds. + * @param buff : Pointer to a buffer that contains the sample data. + * @param len : Length of the buffer pointed to by pBuffer, in bytes. + */ + void SetCallBack(std::function); + int GetHeight() { return mVideoHeight; } + int GetWidth() { return mVideoWidth; } + int GetBitDepth() { return mBitDepth; } + GUID MediaType(); + + +}; diff --git a/media/DXGICapture.cpp b/media/DXGICapture.cpp new file mode 100644 index 0000000..09156a0 --- /dev/null +++ b/media/DXGICapture.cpp @@ -0,0 +1,684 @@ +/***************************************************************************** +* DXGICapture.cpp +* +* Copyright (C) 2020 Gokhan Erdogdu +* +* DXGICapture is free software; you can redistribute it and/or modify it under +* the terms of the GNU Lesser General Public License as published by the Free +* Software Foundation; either version 2.1 of the License, or (at your option) +* any later version. +* +* DXGICapture is distributed in the hope that it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more +* details. +* +******************************************************************************/ +#include "DXGICapture.h" +#include "DXGICaptureHelper.h" + +#include + +#pragma comment(lib, "D3D11.lib") +#pragma comment(lib, "d2d1.lib") +#pragma comment(lib, "windowscodecs.lib") + +#pragma comment(lib, "shcore.lib") // SetProcessDpiAwareness + +// Driver types supported +const D3D_DRIVER_TYPE g_DriverTypes[] = +{ + D3D_DRIVER_TYPE_HARDWARE, + D3D_DRIVER_TYPE_WARP, + D3D_DRIVER_TYPE_REFERENCE, +}; +const UINT g_NumDriverTypes = ARRAYSIZE(g_DriverTypes); + +// Feature levels supported +const D3D_FEATURE_LEVEL g_FeatureLevels[] = +{ + D3D_FEATURE_LEVEL_11_0, + D3D_FEATURE_LEVEL_10_1, + D3D_FEATURE_LEVEL_10_0, + D3D_FEATURE_LEVEL_9_1 +}; +const UINT g_NumFeatureLevels = ARRAYSIZE(g_FeatureLevels); + +#define AUTOLOCK() ATL::CComCritSecLock auto_lock((ATL::CComAutoCriticalSection&)(m_csLock)) + +// +// class CDXGICapture +// +CDXGICapture::CDXGICapture() + : m_csLock() + , m_bInitialized(FALSE) + , m_lD3DFeatureLevel(D3D_FEATURE_LEVEL_INVALID) +{ + RtlZeroMemory(&m_rendererInfo, sizeof(m_rendererInfo)); + RtlZeroMemory(&m_mouseInfo, sizeof(m_mouseInfo)); + RtlZeroMemory(&m_tempMouseBuffer, sizeof(m_tempMouseBuffer)); + RtlZeroMemory(&m_desktopOutputDesc, sizeof(m_desktopOutputDesc)); +} + +CDXGICapture::~CDXGICapture() +{ + this->Terminate(); +} + +HRESULT CDXGICapture::loadMonitorInfos(ID3D11Device *pDevice) +{ + CHECK_POINTER(pDevice); + + HRESULT hr = S_OK; + CComPtr ipDevice(pDevice); + + // Get DXGI device + CComPtr ipDxgiDevice; + hr = ipDevice->QueryInterface(IID_PPV_ARGS(&ipDxgiDevice)); + if (FAILED(hr)) { + return hr; + } + + // Get DXGI adapter + CComPtr ipDxgiAdapter; + hr = ipDxgiDevice->GetParent(IID_PPV_ARGS(&ipDxgiAdapter)); + if (FAILED(hr)) { + return hr; + } + ipDxgiDevice = nullptr; + + CComPtr ipDxgiOutput; + for (UINT i = 0; SUCCEEDED(hr); ++i) + { + ipDxgiOutput = nullptr; + hr = ipDxgiAdapter->EnumOutputs(i, &ipDxgiOutput); + if ((nullptr != ipDxgiOutput) && (hr != DXGI_ERROR_NOT_FOUND)) + { + DXGI_OUTPUT_DESC DesktopDesc; + hr = ipDxgiOutput->GetDesc(&DesktopDesc); + if (FAILED(hr)) { + continue; + } + + tagDublicatorMonitorInfo *pInfo; + pInfo = new (std::nothrow) tagDublicatorMonitorInfo; + if (nullptr == pInfo) { + return E_OUTOFMEMORY; + } + + hr = DXGICaptureHelper::ConvertDxgiOutputToMonitorInfo(&DesktopDesc, i, pInfo); + if (FAILED(hr)) { + delete pInfo; + continue; + } + + m_monitorInfos.push_back(pInfo); + } + } + + ipDxgiOutput = nullptr; + ipDxgiAdapter = nullptr; + + return S_OK; +} + +void CDXGICapture::freeMonitorInfos() +{ + size_t nCount = m_monitorInfos.size(); + if (nCount == 0) { + return; + } + DublicatorMonitorInfoVec::iterator it = m_monitorInfos.begin(); + DublicatorMonitorInfoVec::iterator end = m_monitorInfos.end(); + for (size_t i = 0; (i < nCount) && (it != end); i++, it++) { + tagDublicatorMonitorInfo *pInfo = *it; + if (nullptr != pInfo) { + delete pInfo; + } + } + m_monitorInfos.clear(); +} + +HRESULT CDXGICapture::createDeviceResource( + const tagScreenCaptureFilterConfig *pConfig, + const tagDublicatorMonitorInfo *pSelectedMonitorInfo + ) +{ + HRESULT hr = S_OK; + + CComPtr ipDxgiOutputDuplication; + CComPtr ipCopyTexture2D; + CComPtr ipD2D1Device; + CComPtr ipD2D1DeviceContext; + CComPtr ipD2D1Factory; + CComPtr ipWICImageFactory; + CComPtr ipWICOutputBitmap; + CComPtr ipD2D1RenderTarget; + DXGI_OUTPUT_DESC dgixOutputDesc; + tagRendererInfo rendererInfo; + + RtlZeroMemory(&dgixOutputDesc, sizeof(dgixOutputDesc)); + RtlZeroMemory(&rendererInfo, sizeof(rendererInfo)); + + // copy configuration to renderer info + rendererInfo.MonitorIdx = pConfig->MonitorIdx; + rendererInfo.ShowCursor = pConfig->ShowCursor; + rendererInfo.RotationMode = pConfig->RotationMode; + rendererInfo.SizeMode = pConfig->SizeMode; + rendererInfo.OutputSize = pConfig->OutputSize; + // default + rendererInfo.ScaleX = 1.0f; + rendererInfo.ScaleY = 1.0f; + + do + { + // Get DXGI factory + CComPtr ipDxgiDevice; + hr = m_ipD3D11Device->QueryInterface(IID_PPV_ARGS(&ipDxgiDevice)); + CHECK_HR_BREAK(hr); + + CComPtr ipDxgiAdapter; + hr = ipDxgiDevice->GetParent(IID_PPV_ARGS(&ipDxgiAdapter)); + CHECK_HR_BREAK(hr); + + // Get output + CComPtr ipDxgiOutput; + hr = ipDxgiAdapter->EnumOutputs(rendererInfo.MonitorIdx, &ipDxgiOutput); + CHECK_HR_BREAK(hr); + + // Get output description + hr = ipDxgiOutput->GetDesc(&dgixOutputDesc); + CHECK_HR_BREAK(hr); + + tagDublicatorMonitorInfo curMonInfo; + hr = DXGICaptureHelper::ConvertDxgiOutputToMonitorInfo(&dgixOutputDesc, rendererInfo.MonitorIdx, &curMonInfo); + CHECK_HR_BREAK(hr); + + if (!DXGICaptureHelper::IsEqualMonitorInfo(pSelectedMonitorInfo, &curMonInfo)) { + hr = E_INVALIDARG; // Monitor settings have changed ??? + break; + } + + // QI for Output 1 + CComPtr ipDxgiOutput1; + hr = ipDxgiOutput->QueryInterface(IID_PPV_ARGS(&ipDxgiOutput1)); + CHECK_HR_BREAK(hr); + + // Create desktop duplication + hr = ipDxgiOutput1->DuplicateOutput(m_ipD3D11Device, &ipDxgiOutputDuplication); + CHECK_HR_BREAK(hr); + + DXGI_OUTDUPL_DESC dxgiOutputDuplDesc; + ipDxgiOutputDuplication->GetDesc(&dxgiOutputDuplDesc); + + hr = DXGICaptureHelper::CalculateRendererInfo(&dxgiOutputDuplDesc, &rendererInfo); + CHECK_HR_BREAK(hr); + + // Create CPU access texture + D3D11_TEXTURE2D_DESC desc; + desc.Width = rendererInfo.SrcBounds.Width; + desc.Height = rendererInfo.SrcBounds.Height; + desc.Format = rendererInfo.SrcFormat; + desc.ArraySize = 1; + desc.BindFlags = 0; + desc.MiscFlags = 0; + desc.SampleDesc.Count = 1; + desc.SampleDesc.Quality = 0; + desc.MipLevels = 1; + desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE; + desc.Usage = D3D11_USAGE_STAGING; + + hr = m_ipD3D11Device->CreateTexture2D(&desc, NULL, &ipCopyTexture2D); + CHECK_HR_BREAK(hr); + + if (nullptr == ipCopyTexture2D) + { + hr = E_OUTOFMEMORY; + break; + } + +#pragma region + + // Create D2D1 device + UINT uiFlags = m_ipD3D11Device->GetCreationFlags(); + D2D1_CREATION_PROPERTIES d2d1Props = D2D1::CreationProperties + ( + (uiFlags & D3D11_CREATE_DEVICE_SINGLETHREADED) + ? D2D1_THREADING_MODE_SINGLE_THREADED + : D2D1_THREADING_MODE_MULTI_THREADED, + D2D1_DEBUG_LEVEL_NONE, + (uiFlags & D3D11_CREATE_DEVICE_SINGLETHREADED) + ? D2D1_DEVICE_CONTEXT_OPTIONS_NONE + : D2D1_DEVICE_CONTEXT_OPTIONS_ENABLE_MULTITHREADED_OPTIMIZATIONS + ); + hr = D2D1CreateDevice(ipDxgiDevice, d2d1Props, &ipD2D1Device); + CHECK_HR_BREAK(hr); + + // Get D2D1 factory + ipD2D1Device->GetFactory(&ipD2D1Factory); + + if (nullptr == ipD2D1Factory) + { + hr = D2DERR_INVALID_CALL; + break; + } + + //create WIC factory + hr = CoCreateInstance( + CLSID_WICImagingFactory, + NULL, + CLSCTX_INPROC_SERVER, + IID_IWICImagingFactory, + reinterpret_cast(&ipWICImageFactory) + ); + CHECK_HR_BREAK(hr); + + // create D2D1 target bitmap for render + hr = ipWICImageFactory->CreateBitmap( + (UINT)rendererInfo.OutputSize.Width, + (UINT)rendererInfo.OutputSize.Height, + GUID_WICPixelFormat32bppPBGRA, + WICBitmapCacheOnDemand, + &ipWICOutputBitmap); + CHECK_HR_BREAK(hr); + + if (nullptr == ipWICOutputBitmap) + { + hr = E_OUTOFMEMORY; + break; + } + + // create a D2D1 render target (for D2D1 drawing) + D2D1_RENDER_TARGET_PROPERTIES d2d1RenderTargetProp = D2D1::RenderTargetProperties + ( + D2D1_RENDER_TARGET_TYPE_DEFAULT, + D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED), + 0.0f, // default dpi + 0.0f, // default dpi + D2D1_RENDER_TARGET_USAGE_GDI_COMPATIBLE + ); + hr = ipD2D1Factory->CreateWicBitmapRenderTarget( + ipWICOutputBitmap, + d2d1RenderTargetProp, + &ipD2D1RenderTarget + ); + CHECK_HR_BREAK(hr); + +#pragma endregion + + } while (false); + + if (SUCCEEDED(hr)) + { + // copy output parameters + memcpy_s((void*)&m_rendererInfo, sizeof(m_rendererInfo), (const void*)&rendererInfo, sizeof(m_rendererInfo)); + + // set parameters + m_desktopOutputDesc = dgixOutputDesc; + + m_ipDxgiOutputDuplication = ipDxgiOutputDuplication; + m_ipCopyTexture2D = ipCopyTexture2D; + + m_ipD2D1Device = ipD2D1Device; + m_ipD2D1Factory = ipD2D1Factory; + m_ipWICImageFactory = ipWICImageFactory; + m_ipWICOutputBitmap = ipWICOutputBitmap; + m_ipD2D1RenderTarget = ipD2D1RenderTarget; + } + + return S_OK; +} + +void CDXGICapture::terminateDeviceResource() +{ + m_ipDxgiOutputDuplication = nullptr; + m_ipCopyTexture2D = nullptr; + + m_ipD2D1Device = nullptr; + m_ipD2D1Factory = nullptr; + m_ipWICImageFactory = nullptr; + m_ipWICOutputBitmap = nullptr; + m_ipD2D1RenderTarget = nullptr; + + // clear config parameters + RtlZeroMemory(&m_rendererInfo, sizeof(m_rendererInfo)); + + // clear mouse information parameters + if (m_mouseInfo.PtrShapeBuffer != nullptr) { + delete[] m_mouseInfo.PtrShapeBuffer; + m_mouseInfo.PtrShapeBuffer = nullptr; + } + RtlZeroMemory(&m_mouseInfo, sizeof(m_mouseInfo)); + + // clear temp temp buffer + if (m_tempMouseBuffer.Buffer != nullptr) { + delete[] m_tempMouseBuffer.Buffer; + m_tempMouseBuffer.Buffer = nullptr; + } + RtlZeroMemory(&m_tempMouseBuffer, sizeof(m_tempMouseBuffer)); + + // clear desktop output desc + RtlZeroMemory(&m_desktopOutputDesc, sizeof(m_desktopOutputDesc)); +} + +HRESULT CDXGICapture::Initialize() +{ + AUTOLOCK(); + if (m_bInitialized) { + return HRESULT_FROM_WIN32(ERROR_ALREADY_INITIALIZED); // already initialized + } + + HRESULT hr = S_OK; + D3D_FEATURE_LEVEL lFeatureLevel; + CComPtr ipDevice; + CComPtr ipDeviceContext; + + // required for monitor dpi problem (???) + SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); + + // Create device + for (UINT i = 0; i < g_NumDriverTypes; ++i) + { + hr = D3D11CreateDevice( + nullptr, + g_DriverTypes[i], + nullptr, + /* D3D11_CREATE_DEVICE_BGRA_SUPPORT + * This flag adds support for surfaces with a different + * color channel ordering than the API default. + * You need it for compatibility with Direct2D. */ + D3D11_CREATE_DEVICE_BGRA_SUPPORT, + g_FeatureLevels, + g_NumFeatureLevels, + D3D11_SDK_VERSION, + &ipDevice, + &lFeatureLevel, + &ipDeviceContext); + + if (SUCCEEDED(hr)) + { + // Device creation success, no need to loop anymore + break; + } + + ipDevice = nullptr; + ipDeviceContext = nullptr; + } + + if (FAILED(hr)) { + return hr; + } + + if (nullptr == ipDevice) { + return E_UNEXPECTED; + } + + // load all monitor informations + hr = loadMonitorInfos(ipDevice); + if (FAILED(hr)) { + return hr; + } + + // set common fields + m_lD3DFeatureLevel = lFeatureLevel; + m_ipD3D11Device = ipDevice; + m_ipD3D11DeviceContext = ipDeviceContext; + + m_bInitialized = TRUE; + + return S_OK; +} + +HRESULT CDXGICapture::Terminate() +{ + AUTOLOCK(); + if (!m_bInitialized) { + return S_FALSE; // already terminated + } + + this->terminateDeviceResource(); + + m_ipD3D11Device = nullptr; + m_ipD3D11DeviceContext = nullptr; + m_lD3DFeatureLevel = D3D_FEATURE_LEVEL_INVALID; + + freeMonitorInfos(); + + m_bInitialized = FALSE; + return S_OK; +} + +HRESULT CDXGICapture::SetConfig(const tagScreenCaptureFilterConfig *pConfig) +{ + AUTOLOCK(); + if (!m_bInitialized) { + return D2DERR_NOT_INITIALIZED; + } + + if (nullptr == pConfig) { + return E_INVALIDARG; + } + + // terminate old resources + this->terminateDeviceResource(); + + HRESULT hr = S_OK; + const tagDublicatorMonitorInfo *pSelectedMonitorInfo = nullptr; + + pSelectedMonitorInfo = this->FindDublicatorMonitorInfo(pConfig->MonitorIdx); + if (nullptr == pSelectedMonitorInfo) { + return E_INVALIDARG; + } + + hr = this->createDeviceResource(pConfig, pSelectedMonitorInfo); + if (FAILED(hr)) { + return hr; + } + + return hr; +} + +HRESULT CDXGICapture::SetConfig(const tagScreenCaptureFilterConfig &config) +{ + return this->SetConfig(&config); +} + +BOOL CDXGICapture::IsInitialized() const +{ + AUTOLOCK(); + return m_bInitialized; +} + +D3D_FEATURE_LEVEL CDXGICapture::GetD3DFeatureLevel() const +{ + AUTOLOCK(); + return m_lD3DFeatureLevel; +} + +int CDXGICapture::GetDublicatorMonitorInfoCount() const +{ + AUTOLOCK(); + return (int)m_monitorInfos.size(); +} + +const tagDublicatorMonitorInfo* CDXGICapture::GetDublicatorMonitorInfo(int index) const +{ + AUTOLOCK(); + + size_t nCount = m_monitorInfos.size(); + if ((index < 0) || (index >= (int)nCount)) { + return nullptr; + } + + return m_monitorInfos[index]; +} // GetDublicatorMonitorInfo + +const tagDublicatorMonitorInfo* CDXGICapture::FindDublicatorMonitorInfo(int monitorIdx) const +{ + AUTOLOCK(); + + size_t nCount = m_monitorInfos.size(); + if (nCount == 0) { + return nullptr; + } + DublicatorMonitorInfoVec::const_iterator it = m_monitorInfos.begin(); + DublicatorMonitorInfoVec::const_iterator end = m_monitorInfos.end(); + for (size_t i = 0; (i < nCount) && (it != end); i++, it++) { + tagDublicatorMonitorInfo *pInfo = *it; + if (monitorIdx == pInfo->Idx) { + return pInfo; + } + } + + return nullptr; +} // FindDublicatorMonitorInfo + +// +// CaptureToFile +// +HRESULT CDXGICapture::CaptureToFile(_In_ LPCWSTR lpcwOutputFileName, _Out_opt_ BOOL *pRetIsTimeout /*= NULL*/, _Out_opt_ UINT *pRetRenderDuration /*= NULL*/) +{ + AUTOLOCK(); + + if (nullptr != pRetIsTimeout) { + *pRetIsTimeout = FALSE; + } + + if (nullptr != pRetRenderDuration) { + *pRetRenderDuration = 0xFFFFFFFF; + } + + if (!m_bInitialized) { + return D2DERR_NOT_INITIALIZED; + } + + CHECK_POINTER_EX(m_ipDxgiOutputDuplication, E_INVALIDARG); + CHECK_POINTER_EX(lpcwOutputFileName, E_INVALIDARG); + + HRESULT hr = S_OK; + + hr = DXGICaptureHelper::IsRendererInfoValid(&m_rendererInfo); + if (FAILED(hr)) { + return hr; + } + + // is valid? + hr = DXGICaptureHelper::GetContainerFormatByFileName(lpcwOutputFileName); + if (FAILED(hr)) { + return hr; + } + + DXGI_OUTDUPL_FRAME_INFO FrameInfo; + CComPtr ipDesktopResource; + CComPtr ipAcquiredDesktopImage; + CComPtr ipD2D1SourceBitmap; + + std::chrono::system_clock::time_point startTick; + if (nullptr != pRetRenderDuration) { + startTick = std::chrono::system_clock::now(); + } + + // Get new frame + hr = m_ipDxgiOutputDuplication->AcquireNextFrame(1000, &FrameInfo, &ipDesktopResource); + if (hr == DXGI_ERROR_WAIT_TIMEOUT) + { + if (nullptr != pRetIsTimeout) { + *pRetIsTimeout = TRUE; + } + return S_FALSE; + } + else if (FAILED(hr)) + { + return hr; + } + + // QI for ID3D11Texture2D + hr = ipDesktopResource->QueryInterface(IID_PPV_ARGS(&ipAcquiredDesktopImage)); + ipDesktopResource = nullptr; + CHECK_HR_RETURN(hr); + + if (nullptr == ipAcquiredDesktopImage) + { + // release frame + m_ipDxgiOutputDuplication->ReleaseFrame(); + return E_OUTOFMEMORY; + } + + // Copy needed full part of desktop image + m_ipD3D11DeviceContext->CopyResource(m_ipCopyTexture2D, ipAcquiredDesktopImage); + + if (m_rendererInfo.ShowCursor) { + hr = DXGICaptureHelper::GetMouse(m_ipDxgiOutputDuplication, &m_mouseInfo, &FrameInfo, (UINT)m_rendererInfo.MonitorIdx, m_desktopOutputDesc.DesktopCoordinates.left, m_desktopOutputDesc.DesktopCoordinates.top); + if (SUCCEEDED(hr) && m_mouseInfo.Visible) { + hr = DXGICaptureHelper::DrawMouse(&m_mouseInfo, &m_desktopOutputDesc, &m_tempMouseBuffer, m_ipCopyTexture2D); + } + + if (FAILED(hr)) { + // release frame + m_ipDxgiOutputDuplication->ReleaseFrame(); + return hr; + } + } + + // release frame + hr = m_ipDxgiOutputDuplication->ReleaseFrame(); + CHECK_HR_RETURN(hr); + + // create D2D1 source bitmap + hr = DXGICaptureHelper::CreateBitmap(m_ipD2D1RenderTarget, m_ipCopyTexture2D, &ipD2D1SourceBitmap); + CHECK_HR_RETURN(hr); + + D2D1_RECT_F rcSource = D2D1::RectF( + (FLOAT)m_rendererInfo.SrcBounds.X, + (FLOAT)m_rendererInfo.SrcBounds.Y, + (FLOAT)(m_rendererInfo.SrcBounds.X + m_rendererInfo.SrcBounds.Width), + (FLOAT)(m_rendererInfo.SrcBounds.Y + m_rendererInfo.SrcBounds.Height)); + D2D1_RECT_F rcTarget = D2D1::RectF( + (FLOAT)m_rendererInfo.DstBounds.X, + (FLOAT)m_rendererInfo.DstBounds.Y, + (FLOAT)(m_rendererInfo.DstBounds.X + m_rendererInfo.DstBounds.Width), + (FLOAT)(m_rendererInfo.DstBounds.Y + m_rendererInfo.DstBounds.Height)); + D2D1_POINT_2F ptTransformCenter = D2D1::Point2F(m_rendererInfo.OutputSize.Width / 2.0f, m_rendererInfo.OutputSize.Height / 2.0f); + + // Apply the rotation transform to the render target. + D2D1::Matrix3x2F rotate = D2D1::Matrix3x2F::Rotation( + m_rendererInfo.RotationDegrees, + ptTransformCenter + ); + + D2D1::Matrix3x2F scale = D2D1::Matrix3x2F::Scale( + D2D1::SizeF(m_rendererInfo.ScaleX, m_rendererInfo.ScaleY), + ptTransformCenter + ); + + // Priority: first rotate, after scale... + m_ipD2D1RenderTarget->SetTransform(rotate * scale); + + m_ipD2D1RenderTarget->BeginDraw(); + // clear background color + m_ipD2D1RenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::Black, 1.0f)); + m_ipD2D1RenderTarget->DrawBitmap(ipD2D1SourceBitmap, rcTarget, 1.0f, + D2D1_BITMAP_INTERPOLATION_MODE_LINEAR, rcSource); + // Reset transform + //m_ipD2D1RenderTarget->SetTransform(D2D1::Matrix3x2F::Identity()); + // Logo draw sample + //m_ipD2D1RenderTarget->DrawBitmap(ipBmpLogo, D2D1::RectF(0, 0, 2 * 200, 2 * 46)); + hr = m_ipD2D1RenderTarget->EndDraw(); + if (FAILED(hr)) { + return hr; + } + + // calculate render time without save + if (nullptr != pRetRenderDuration) { + *pRetRenderDuration = (UINT)((std::chrono::system_clock::now() - startTick).count() / 10000); + } + hr = DXGICaptureHelper::SaveImageToFile(m_ipWICImageFactory, + m_ipWICOutputBitmap, lpcwOutputFileName); + if (FAILED(hr)) { + return hr; + } + + return S_OK; +} // CaptureToFile + +#undef AUTOLOCK diff --git a/media/DXGICapture.h b/media/DXGICapture.h new file mode 100644 index 0000000..fd7a5c7 --- /dev/null +++ b/media/DXGICapture.h @@ -0,0 +1,92 @@ +/***************************************************************************** +* DXGICapture.h +* +* Copyright (C) 2020 Gokhan Erdogdu +* +* DXGICapture is free software; you can redistribute it and/or modify it under +* the terms of the GNU Lesser General Public License as published by the Free +* Software Foundation; either version 2.1 of the License, or (at your option) +* any later version. +* +* DXGICapture is distributed in the hope that it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more +* details. +* +******************************************************************************/ +#pragma once +#ifndef __DXGICAPTURE_H__ +#define __DXGICAPTURE_H__ + +#include +#include + +#include +#include + +#include +#include // for ID2D1Effect +#include + +#include "DXGICaptureTypes.h" + +#define D3D_FEATURE_LEVEL_INVALID ((D3D_FEATURE_LEVEL)0x0) + +class CDXGICapture +{ +private: + ATL::CComAutoCriticalSection m_csLock; + + BOOL m_bInitialized; + DublicatorMonitorInfoVec m_monitorInfos; + tagRendererInfo m_rendererInfo; + + tagMouseInfo m_mouseInfo; + tagFrameBufferInfo m_tempMouseBuffer; + DXGI_OUTPUT_DESC m_desktopOutputDesc; + + D3D_FEATURE_LEVEL m_lD3DFeatureLevel; + CComPtr m_ipD3D11Device; + CComPtr m_ipD3D11DeviceContext; + + CComPtr m_ipDxgiOutputDuplication; + CComPtr m_ipCopyTexture2D; + + CComPtr m_ipD2D1Device; + CComPtr m_ipD2D1Factory; + CComPtr m_ipWICImageFactory; + CComPtr m_ipWICOutputBitmap; + CComPtr m_ipD2D1RenderTarget; + +public: + CDXGICapture(); + ~CDXGICapture(); + +private: + HRESULT loadMonitorInfos(ID3D11Device *pDevice); + void freeMonitorInfos(); + + HRESULT createDeviceResource( + const tagScreenCaptureFilterConfig *pConfig, + const tagDublicatorMonitorInfo *pSelectedMonitorInfo); + void terminateDeviceResource(); + +public: + HRESULT Initialize(); + HRESULT Terminate(); + HRESULT SetConfig(const tagScreenCaptureFilterConfig *pConfig); + HRESULT SetConfig(const tagScreenCaptureFilterConfig &config); + + BOOL IsInitialized() const; + D3D_FEATURE_LEVEL GetD3DFeatureLevel() const; + + int GetDublicatorMonitorInfoCount() const; + const tagDublicatorMonitorInfo* GetDublicatorMonitorInfo(int index) const; + const tagDublicatorMonitorInfo* FindDublicatorMonitorInfo(int monitorIdx) const; + + HRESULT CaptureToFile(_In_ LPCWSTR lpcwOutputFileName, _Out_opt_ BOOL *pRetIsTimeout = NULL, _Out_opt_ UINT *pRetRenderDuration = NULL); +}; + +#endif // __DXGICAPTURE_H__ + + diff --git a/media/DXGICaptureHelper.h b/media/DXGICaptureHelper.h new file mode 100644 index 0000000..8080582 --- /dev/null +++ b/media/DXGICaptureHelper.h @@ -0,0 +1,960 @@ +/***************************************************************************** +* DXGICaptureHelper.h +* +* Copyright (C) 2020 Gokhan Erdogdu +* +* DXGICapture is free software; you can redistribute it and/or modify it under +* the terms of the GNU Lesser General Public License as published by the Free +* Software Foundation; either version 2.1 of the License, or (at your option) +* any later version. +* +* DXGICapture is distributed in the hope that it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more +* details. +* +******************************************************************************/ +#pragma once +#ifndef __DXGICAPTUREHELPER_H__ +#define __DXGICAPTUREHELPER_H__ + +#include +#include + +#include +#include + +#include +#include + +#include "DXGICaptureTypes.h" + +#pragma comment (lib, "Shlwapi.lib") + +// +// class DXGICaptureHelper +// +class DXGICaptureHelper +{ +public: + static + COM_DECLSPEC_NOTHROW + inline + HRESULT + ConvertDxgiOutputToMonitorInfo( + _In_ const DXGI_OUTPUT_DESC *pDxgiOutput, + _In_ int monitorIdx, + _Out_ tagDublicatorMonitorInfo *pOutVal + ) + { + CHECK_POINTER(pOutVal); + // reset output parameter + RtlZeroMemory(pOutVal, sizeof(tagDublicatorMonitorInfo)); + CHECK_POINTER_EX(pDxgiOutput, E_INVALIDARG); + + switch (pDxgiOutput->Rotation) + { + case DXGI_MODE_ROTATION_UNSPECIFIED: + case DXGI_MODE_ROTATION_IDENTITY: + pOutVal->RotationDegrees = 0; + break; + + case DXGI_MODE_ROTATION_ROTATE90: + pOutVal->RotationDegrees = 90; + break; + + case DXGI_MODE_ROTATION_ROTATE180: + pOutVal->RotationDegrees = 180; + break; + + case DXGI_MODE_ROTATION_ROTATE270: + pOutVal->RotationDegrees = 270; + break; + } + + pOutVal->Idx = monitorIdx; + pOutVal->Bounds.X = pDxgiOutput->DesktopCoordinates.left; + pOutVal->Bounds.Y = pDxgiOutput->DesktopCoordinates.top; + pOutVal->Bounds.Width = pDxgiOutput->DesktopCoordinates.right - pDxgiOutput->DesktopCoordinates.left; + pOutVal->Bounds.Height = pDxgiOutput->DesktopCoordinates.bottom - pDxgiOutput->DesktopCoordinates.top; + + wsprintfW(pOutVal->DisplayName, L"Display %d: %ldx%ld @ %ld,%ld" + , monitorIdx + 1 + , pOutVal->Bounds.Width, pOutVal->Bounds.Height + , pOutVal->Bounds.X, pOutVal->Bounds.Y); + + return S_OK; + } // ConvertDxgiOutputToMonitorInfo + + static + COM_DECLSPEC_NOTHROW + inline + BOOL + IsEqualMonitorInfo( + _In_ const tagDublicatorMonitorInfo *p1, + _In_ const tagDublicatorMonitorInfo *p2 + ) + { + if (nullptr == p1) { + return (nullptr == p2); + } + if (nullptr == p2) { + return FALSE; + } + + return memcmp((const void*)p1, (const void*)p2, sizeof(tagDublicatorMonitorInfo)) == 0; + } // IsEqualMonitorInfo + + static + COM_DECLSPEC_NOTHROW + inline + HRESULT + IsRendererInfoValid( + _In_ const tagRendererInfo *pRendererInfo + ) + { + CHECK_POINTER_EX(pRendererInfo, E_INVALIDARG); + + if (pRendererInfo->SrcFormat != DXGI_FORMAT_B8G8R8A8_UNORM) { + return D2DERR_UNSUPPORTED_PIXEL_FORMAT; + } + + if (pRendererInfo->SizeMode != tagFrameSizeMode_Normal) { + if ((pRendererInfo->OutputSize.Width <= 0) || (pRendererInfo->OutputSize.Height <= 0)) { + return D2DERR_BITMAP_BOUND_AS_TARGET; + } + } + + if ((pRendererInfo->DstBounds.Width <= 0) || (pRendererInfo->DstBounds.Height <= 0) || + (pRendererInfo->SrcBounds.Width <= 0) || (pRendererInfo->SrcBounds.Height <= 0)) + { + return D2DERR_ORIGINAL_TARGET_NOT_BOUND; + } + + return S_OK; + } + + static + COM_DECLSPEC_NOTHROW + inline + HRESULT + CalculateRendererInfo( + _In_ const DXGI_OUTDUPL_DESC *pDxgiOutputDuplDesc, + _Inout_ tagRendererInfo *pRendererInfo + ) + { + CHECK_POINTER_EX(pDxgiOutputDuplDesc, E_INVALIDARG); + CHECK_POINTER_EX(pRendererInfo, E_INVALIDARG); + + pRendererInfo->SrcFormat = pDxgiOutputDuplDesc->ModeDesc.Format; + // get rotate state + switch (pDxgiOutputDuplDesc->Rotation) + { + case DXGI_MODE_ROTATION_ROTATE90: + pRendererInfo->RotationDegrees = 90.0f; + pRendererInfo->SrcBounds.X = 0; + pRendererInfo->SrcBounds.Y = 0; + pRendererInfo->SrcBounds.Width = pDxgiOutputDuplDesc->ModeDesc.Height; + pRendererInfo->SrcBounds.Height = pDxgiOutputDuplDesc->ModeDesc.Width; + break; + case DXGI_MODE_ROTATION_ROTATE180: + pRendererInfo->RotationDegrees = 180.0; + pRendererInfo->SrcBounds.X = 0; + pRendererInfo->SrcBounds.Y = 0; + pRendererInfo->SrcBounds.Width = pDxgiOutputDuplDesc->ModeDesc.Width; + pRendererInfo->SrcBounds.Height = pDxgiOutputDuplDesc->ModeDesc.Height; + break; + case DXGI_MODE_ROTATION_ROTATE270: + pRendererInfo->RotationDegrees = 270.0f; + pRendererInfo->SrcBounds.X = 0; + pRendererInfo->SrcBounds.Y = 0; + pRendererInfo->SrcBounds.Width = pDxgiOutputDuplDesc->ModeDesc.Height; + pRendererInfo->SrcBounds.Height = pDxgiOutputDuplDesc->ModeDesc.Width; + break; + default: // OR DXGI_MODE_ROTATION_IDENTITY: + pRendererInfo->RotationDegrees = 0.0f; + pRendererInfo->SrcBounds.X = 0; + pRendererInfo->SrcBounds.Y = 0; + pRendererInfo->SrcBounds.Width = pDxgiOutputDuplDesc->ModeDesc.Width; + pRendererInfo->SrcBounds.Height = pDxgiOutputDuplDesc->ModeDesc.Height; + break; + } + + // force rotate + switch (pRendererInfo->RotationMode) + { + case tagFrameRotationMode::tagFrameRotationMode_Identity: + pRendererInfo->RotationDegrees = 0.0f; + break; + case tagFrameRotationMode::tagFrameRotationMode_90: + pRendererInfo->RotationDegrees = 90.0f; + break; + case tagFrameRotationMode::tagFrameRotationMode_180: + pRendererInfo->RotationDegrees = 180.0f; + break; + case tagFrameRotationMode::tagFrameRotationMode_270: + pRendererInfo->RotationDegrees = 270.0f; + break; + default: // tagFrameRotationMode::tagFrameRotationMode_Auto + break; + } + + if (pRendererInfo->SizeMode == tagFrameSizeMode_Zoom) + { + FLOAT fSrcAspect, fOutAspect, fScaleFactor; + + // center for output + pRendererInfo->DstBounds.Width = pRendererInfo->SrcBounds.Width; + pRendererInfo->DstBounds.Height = pRendererInfo->SrcBounds.Height; + pRendererInfo->DstBounds.X = (pRendererInfo->OutputSize.Width - pRendererInfo->SrcBounds.Width) >> 1; + pRendererInfo->DstBounds.Y = (pRendererInfo->OutputSize.Height - pRendererInfo->SrcBounds.Height) >> 1; + + fOutAspect = (FLOAT)pRendererInfo->OutputSize.Width / pRendererInfo->OutputSize.Height; + + if ((pRendererInfo->RotationDegrees == 0.0f) || (pRendererInfo->RotationDegrees == 180.0f)) + { + fSrcAspect = (FLOAT)pRendererInfo->SrcBounds.Width / pRendererInfo->SrcBounds.Height; + + if (fSrcAspect > fOutAspect) + { + fScaleFactor = (FLOAT)pRendererInfo->OutputSize.Width / pRendererInfo->SrcBounds.Width; + } + else + { + fScaleFactor = (FLOAT)pRendererInfo->OutputSize.Height / pRendererInfo->SrcBounds.Height; + } + } + else // 90 or 270 degree + { + fSrcAspect = (FLOAT)pRendererInfo->SrcBounds.Height / pRendererInfo->SrcBounds.Width; + + if (fSrcAspect > fOutAspect) + { + fScaleFactor = (FLOAT)pRendererInfo->OutputSize.Width / pRendererInfo->SrcBounds.Height; + } + else + { + fScaleFactor = (FLOAT)pRendererInfo->OutputSize.Height / pRendererInfo->SrcBounds.Width; + } + } + + pRendererInfo->ScaleX = fScaleFactor; + pRendererInfo->ScaleY = fScaleFactor; + } + else if (pRendererInfo->SizeMode == tagFrameSizeMode_CenterImage) + { + // center for output + pRendererInfo->DstBounds.Width = pRendererInfo->SrcBounds.Width; + pRendererInfo->DstBounds.Height = pRendererInfo->SrcBounds.Height; + pRendererInfo->DstBounds.X = (pRendererInfo->OutputSize.Width - pRendererInfo->SrcBounds.Width) >> 1; + pRendererInfo->DstBounds.Y = (pRendererInfo->OutputSize.Height - pRendererInfo->SrcBounds.Height) >> 1; + } + else if (pRendererInfo->SizeMode == tagFrameSizeMode_AutoSize) + { + // set the destination bounds + pRendererInfo->DstBounds.Width = pRendererInfo->SrcBounds.Width; + pRendererInfo->DstBounds.Height = pRendererInfo->SrcBounds.Height; + + if ((pRendererInfo->RotationDegrees == 0.0f) || (pRendererInfo->RotationDegrees == 180.0f)) + { + // same as the source size + pRendererInfo->OutputSize.Width = pRendererInfo->SrcBounds.Width; + pRendererInfo->OutputSize.Height = pRendererInfo->SrcBounds.Height; + } + else // 90 or 270 degree + { + // same as the source size + pRendererInfo->OutputSize.Width = pRendererInfo->SrcBounds.Height; + pRendererInfo->OutputSize.Height = pRendererInfo->SrcBounds.Width; + + // center for output + pRendererInfo->DstBounds.X = (pRendererInfo->OutputSize.Width - pRendererInfo->SrcBounds.Width) >> 1; + pRendererInfo->DstBounds.Y = (pRendererInfo->OutputSize.Height - pRendererInfo->SrcBounds.Height) >> 1; + } + } + else if (pRendererInfo->SizeMode == tagFrameSizeMode_StretchImage) + { + // center for output + pRendererInfo->DstBounds.Width = pRendererInfo->SrcBounds.Width; + pRendererInfo->DstBounds.Height = pRendererInfo->SrcBounds.Height; + pRendererInfo->DstBounds.X = (pRendererInfo->OutputSize.Width - pRendererInfo->SrcBounds.Width) >> 1; + pRendererInfo->DstBounds.Y = (pRendererInfo->OutputSize.Height - pRendererInfo->SrcBounds.Height) >> 1; + + if ((pRendererInfo->RotationDegrees == 0.0f) || (pRendererInfo->RotationDegrees == 180.0f)) + { + pRendererInfo->ScaleX = (FLOAT)pRendererInfo->OutputSize.Width / pRendererInfo->DstBounds.Width; + pRendererInfo->ScaleY = (FLOAT)pRendererInfo->OutputSize.Height / pRendererInfo->DstBounds.Height; + } + else // 90 or 270 degree + { + pRendererInfo->ScaleX = (FLOAT)pRendererInfo->OutputSize.Width / pRendererInfo->DstBounds.Height; + pRendererInfo->ScaleY = (FLOAT)pRendererInfo->OutputSize.Height / pRendererInfo->DstBounds.Width; + } + } + else // tagFrameSizeMode_Normal + { + pRendererInfo->DstBounds.Width = pRendererInfo->SrcBounds.Width; + pRendererInfo->DstBounds.Height = pRendererInfo->SrcBounds.Height; + + if (pRendererInfo->RotationDegrees == 90) + { + // set destination origin (bottom-left) + pRendererInfo->DstBounds.X = (pRendererInfo->OutputSize.Width - pRendererInfo->OutputSize.Height) >> 1; + pRendererInfo->DstBounds.Y = ((pRendererInfo->OutputSize.Width + pRendererInfo->OutputSize.Height) >> 1) - pRendererInfo->DstBounds.Height; + } + else if (pRendererInfo->RotationDegrees == 180.0f) + { + // set destination origin (bottom-right) + pRendererInfo->DstBounds.X = pRendererInfo->OutputSize.Width - pRendererInfo->DstBounds.Width; + pRendererInfo->DstBounds.Y = pRendererInfo->OutputSize.Height - pRendererInfo->DstBounds.Height; + } + else if (pRendererInfo->RotationDegrees == 270) + { + // set destination origin (top-right) + pRendererInfo->DstBounds.Y = (pRendererInfo->OutputSize.Height - pRendererInfo->OutputSize.Width) >> 1; + pRendererInfo->DstBounds.X = pRendererInfo->OutputSize.Width - pRendererInfo->DstBounds.Width - ((pRendererInfo->OutputSize.Width - pRendererInfo->OutputSize.Height) >> 1); + } + } + + return S_OK; + } + + static + COM_DECLSPEC_NOTHROW + inline + HRESULT + ResizeFrameBuffer( + _Inout_ tagFrameBufferInfo *pBufferInfo, + _In_ UINT uiNewSize + ) + { + CHECK_POINTER(pBufferInfo); + + if (uiNewSize <= pBufferInfo->BufferSize) + { + return S_FALSE; // no change + } + + if (nullptr != pBufferInfo->Buffer) { + delete[] pBufferInfo->Buffer; + pBufferInfo->Buffer = nullptr; + } + + pBufferInfo->Buffer = new (std::nothrow) BYTE[uiNewSize]; + if (!(pBufferInfo->Buffer)) + { + pBufferInfo->BufferSize = 0; + return E_OUTOFMEMORY; + } + pBufferInfo->BufferSize = uiNewSize; + + return S_OK; + } // ResizeFrameBuffer + + static + COM_DECLSPEC_NOTHROW + inline + HRESULT + GetMouse( + _In_ IDXGIOutputDuplication *pOutputDuplication, + _Inout_ tagMouseInfo *PtrInfo, + _In_ DXGI_OUTDUPL_FRAME_INFO *FrameInfo, + UINT MonitorIdx, + INT OffsetX, + INT OffsetY + ) + { + CHECK_POINTER_EX(pOutputDuplication, E_INVALIDARG); + CHECK_POINTER_EX(PtrInfo, E_INVALIDARG); + CHECK_POINTER_EX(FrameInfo, E_INVALIDARG); + + // A non-zero mouse update timestamp indicates that there is a mouse position update and optionally a shape change + if (FrameInfo->LastMouseUpdateTime.QuadPart == 0) + { + return S_OK; + } + + bool UpdatePosition = true; + + // Make sure we don't update pointer position wrongly + // If pointer is invisible, make sure we did not get an update from another output that the last time that said pointer + // was visible, if so, don't set it to invisible or update. + if (!FrameInfo->PointerPosition.Visible && (PtrInfo->WhoUpdatedPositionLast != MonitorIdx)) + { + UpdatePosition = false; + } + + // If two outputs both say they have a visible, only update if new update has newer timestamp + if (FrameInfo->PointerPosition.Visible && PtrInfo->Visible && (PtrInfo->WhoUpdatedPositionLast != MonitorIdx) && (PtrInfo->LastTimeStamp.QuadPart > FrameInfo->LastMouseUpdateTime.QuadPart)) + { + UpdatePosition = false; + } + + // Update position + if (UpdatePosition) + { + PtrInfo->Position.x = FrameInfo->PointerPosition.Position.x - OffsetX; + PtrInfo->Position.y = FrameInfo->PointerPosition.Position.y - OffsetY; + PtrInfo->WhoUpdatedPositionLast = MonitorIdx; + PtrInfo->LastTimeStamp = FrameInfo->LastMouseUpdateTime; + PtrInfo->Visible = FrameInfo->PointerPosition.Visible != 0; + } + + // No new shape + if (FrameInfo->PointerShapeBufferSize == 0) + { + return S_OK; + } + + // Old buffer too small + if (FrameInfo->PointerShapeBufferSize > PtrInfo->ShapeBufferSize) + { + if (PtrInfo->PtrShapeBuffer != nullptr) + { + delete[] PtrInfo->PtrShapeBuffer; + PtrInfo->PtrShapeBuffer = nullptr; + } + PtrInfo->PtrShapeBuffer = new (std::nothrow) BYTE[FrameInfo->PointerShapeBufferSize]; + if (PtrInfo->PtrShapeBuffer == nullptr) + { + PtrInfo->ShapeBufferSize = 0; + return E_OUTOFMEMORY; + } + + // Update buffer size + PtrInfo->ShapeBufferSize = FrameInfo->PointerShapeBufferSize; + } + + // Get shape + UINT BufferSizeRequired; + HRESULT hr = pOutputDuplication->GetFramePointerShape( + FrameInfo->PointerShapeBufferSize, + reinterpret_cast(PtrInfo->PtrShapeBuffer), + &BufferSizeRequired, + &(PtrInfo->ShapeInfo) + ); + if (FAILED(hr)) + { + delete[] PtrInfo->PtrShapeBuffer; + PtrInfo->PtrShapeBuffer = nullptr; + PtrInfo->ShapeBufferSize = 0; + return hr; + } + + return S_OK; + } // GetMouse + + static + COM_DECLSPEC_NOTHROW + inline + HRESULT + ProcessMouseMask( + _In_ const tagMouseInfo *PtrInfo, + _In_ const DXGI_OUTPUT_DESC *DesktopDesc, + _Inout_ tagFrameBufferInfo *pBufferInfo + ) + { + CHECK_POINTER_EX(PtrInfo, E_INVALIDARG); + CHECK_POINTER_EX(DesktopDesc, E_INVALIDARG); + CHECK_POINTER_EX(pBufferInfo, E_INVALIDARG); + + if (!PtrInfo->Visible) { + return S_FALSE; + } + + HRESULT hr = S_OK; + INT DesktopWidth = (INT)(DesktopDesc->DesktopCoordinates.right - DesktopDesc->DesktopCoordinates.left); + INT DesktopHeight = (INT)(DesktopDesc->DesktopCoordinates.bottom - DesktopDesc->DesktopCoordinates.top); + + pBufferInfo->Bounds.X = PtrInfo->Position.x; + pBufferInfo->Bounds.Y = PtrInfo->Position.y; + pBufferInfo->Bounds.Width = PtrInfo->ShapeInfo.Width; + pBufferInfo->Bounds.Height = (PtrInfo->ShapeInfo.Type == DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MONOCHROME) + ? (INT)(PtrInfo->ShapeInfo.Height / 2) + : (INT)PtrInfo->ShapeInfo.Height; + pBufferInfo->Pitch = pBufferInfo->Bounds.Width * 4; + + switch (PtrInfo->ShapeInfo.Type) + { + case DXGI_OUTDUPL_POINTER_SHAPE_TYPE_COLOR: + { + // Resize mouseshape buffer (if necessary) + hr = DXGICaptureHelper::ResizeFrameBuffer(pBufferInfo, PtrInfo->ShapeBufferSize); + if (FAILED(hr)) { + return hr; + } + + // use current mouseshape buffer + // Copy mouseshape buffer + memcpy_s((void*)pBufferInfo->Buffer, pBufferInfo->BufferSize, (const void*)PtrInfo->PtrShapeBuffer, PtrInfo->ShapeBufferSize); + break; + } + + case DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MONOCHROME: + { + // Resize mouseshape buffer (if necessary) + hr = DXGICaptureHelper::ResizeFrameBuffer(pBufferInfo, pBufferInfo->Bounds.Height * pBufferInfo->Pitch); + if (FAILED(hr)) { + return hr; + } + + UINT* InitBuffer32 = reinterpret_cast(pBufferInfo->Buffer); + + for (INT Row = 0; Row < pBufferInfo->Bounds.Height; ++Row) + { + // Set mask + BYTE Mask = 0x80; + for (INT Col = 0; Col < pBufferInfo->Bounds.Width; ++Col) + { + BYTE XorMask = PtrInfo->PtrShapeBuffer[(Col / 8) + ((Row + (PtrInfo->ShapeInfo.Height / 2)) * (PtrInfo->ShapeInfo.Pitch))] & Mask; + + // Set new pixel + InitBuffer32[(Row * pBufferInfo->Bounds.Width) + Col] = (XorMask) ? 0xFFFFFFFF : 0x00000000; + + // Adjust mask + if (Mask == 0x01) + { + Mask = 0x80; + } + else + { + Mask = Mask >> 1; + } + } + } + + break; + } + + case DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MASKED_COLOR: + { + // Resize mouseshape buffer (if necessary) + hr = DXGICaptureHelper::ResizeFrameBuffer(pBufferInfo, pBufferInfo->Bounds.Height * pBufferInfo->Pitch); + if (FAILED(hr)) { + return hr; + } + + UINT* InitBuffer32 = reinterpret_cast(pBufferInfo->Buffer); + UINT* ShapeBuffer32 = reinterpret_cast(PtrInfo->PtrShapeBuffer); + + for (INT Row = 0; Row < pBufferInfo->Bounds.Height; ++Row) + { + for (INT Col = 0; Col < pBufferInfo->Bounds.Width; ++Col) + { + InitBuffer32[(Row * pBufferInfo->Bounds.Width) + Col] = ShapeBuffer32[Col + (Row * (PtrInfo->ShapeInfo.Pitch / sizeof(UINT)))] | 0xFF000000; + } + } + + break; + } + + default: + return E_INVALIDARG; + + } + + UINT* InitBuffer32 = reinterpret_cast(pBufferInfo->Buffer); + UINT width = (UINT)pBufferInfo->Bounds.Width; + UINT height = (UINT)pBufferInfo->Bounds.Height; + + switch (DesktopDesc->Rotation) + { + case DXGI_MODE_ROTATION_ROTATE90: + { + // Rotate -90 or +270 + for (UINT i = 0; i < width; i++) + { + for (UINT j = 0; j < height; j++) + { + UINT I = j; + UINT J = width - 1 - i; + while ((i*height + j) >(I*width + J)) + { + UINT p = I*width + J; + UINT tmp_i = p / height; + UINT tmp_j = p % height; + I = tmp_j; + J = width - 1 - tmp_i; + } + std::swap(*(InitBuffer32 + (i*height + j)), *(InitBuffer32 + (I*width + J))); + } + } + + // translate bounds + std::swap(pBufferInfo->Bounds.Width, pBufferInfo->Bounds.Height); + INT nX = pBufferInfo->Bounds.Y; + INT nY = DesktopWidth - (INT)(pBufferInfo->Bounds.X + pBufferInfo->Bounds.Height); + pBufferInfo->Bounds.X = nX; + pBufferInfo->Bounds.Y = nY; + pBufferInfo->Pitch = pBufferInfo->Bounds.Width * 4; + } break; + case DXGI_MODE_ROTATION_ROTATE180: + { + // Rotate -180 or +180 + if (height % 2 != 0) + { + //If N is odd reverse the middle row in the matrix + UINT j = height >> 1; + for (UINT i = 0; i < (width >> 1); i++) + { + std::swap(InitBuffer32[j * width + i], InitBuffer32[j * width + width - i - 1]); + } + } + + for (UINT j = 0; j < (height >> 1); j++) + { + for (UINT i = 0; i < width; i++) + { + std::swap(InitBuffer32[j * width + i], InitBuffer32[(height - j - 1) * width + width - i - 1]); + } + } + + // translate position + INT nX = DesktopWidth - (INT)(pBufferInfo->Bounds.X + pBufferInfo->Bounds.Width); + INT nY = DesktopHeight - (INT)(pBufferInfo->Bounds.Y + pBufferInfo->Bounds.Height); + pBufferInfo->Bounds.X = nX; + pBufferInfo->Bounds.Y = nY; + } break; + case DXGI_MODE_ROTATION_ROTATE270: + { + // Rotate -270 or +90 + for (UINT i = 0; i < width; i++) + { + for (UINT j = 0; j < height; j++) + { + UINT I = height - 1 - j; + UINT J = i; + while ((i*height + j) >(I*width + J)) + { + int p = I*width + J; + int tmp_i = p / height; + int tmp_j = p % height; + I = height - 1 - tmp_j; + J = tmp_i; + } + std::swap(*(InitBuffer32 + (i*height + j)), *(InitBuffer32 + (I*width + J))); + } + } + + // translate bounds + std::swap(pBufferInfo->Bounds.Width, pBufferInfo->Bounds.Height); + INT nX = DesktopHeight - (pBufferInfo->Bounds.Y + pBufferInfo->Bounds.Width); + INT nY = pBufferInfo->Bounds.X; + pBufferInfo->Bounds.X = nX; + pBufferInfo->Bounds.Y = nY; + pBufferInfo->Pitch = pBufferInfo->Bounds.Width * 4; + } break; + } + + return S_OK; + } // ProcessMouseMask + + // + // Draw mouse provided in buffer to backbuffer + // + static + COM_DECLSPEC_NOTHROW + inline + HRESULT + DrawMouse( + _In_ tagMouseInfo *PtrInfo, + _In_ const DXGI_OUTPUT_DESC *DesktopDesc, + _Inout_ tagFrameBufferInfo *pTempMouseBuffer, + _Inout_ ID3D11Texture2D *pSharedSurf + ) + { + CHECK_POINTER_EX(PtrInfo, E_INVALIDARG); + CHECK_POINTER_EX(DesktopDesc, E_INVALIDARG); + CHECK_POINTER_EX(pTempMouseBuffer, E_INVALIDARG); + CHECK_POINTER_EX(pSharedSurf, E_INVALIDARG); + + HRESULT hr = S_OK; + + D3D11_TEXTURE2D_DESC FullDesc; + pSharedSurf->GetDesc(&FullDesc); + + INT SurfWidth = FullDesc.Width; + INT SurfHeight = FullDesc.Height; + INT SurfPitch = FullDesc.Width * 4; + + hr = DXGICaptureHelper::ProcessMouseMask(PtrInfo, DesktopDesc, pTempMouseBuffer); + if (FAILED(hr)) { + return hr; + } + + // Buffer used if necessary (in case of monochrome or masked pointer) + BYTE* InitBuffer = pTempMouseBuffer->Buffer; + + // Clipping adjusted coordinates / dimensions + INT PtrWidth = (INT)pTempMouseBuffer->Bounds.Width; + INT PtrHeight = (INT)pTempMouseBuffer->Bounds.Height; + + INT PtrLeft = (INT)pTempMouseBuffer->Bounds.X; + INT PtrTop = (INT)pTempMouseBuffer->Bounds.Y; + INT PtrPitch = (INT)pTempMouseBuffer->Pitch; + + INT SrcLeft = 0; + INT SrcTop = 0; + INT SrcWidth = PtrWidth; + INT SrcHeight = PtrHeight; + + if (PtrLeft < 0) + { + // crop mouseshape left + SrcLeft = -PtrLeft; + // new mouse x position for drawing + PtrLeft = 0; + } + else if (PtrLeft + PtrWidth > SurfWidth) + { + // crop mouseshape width + SrcWidth = SurfWidth - PtrLeft; + } + + if (PtrTop < 0) + { + // crop mouseshape top + SrcTop = -PtrTop; + // new mouse y position for drawing + PtrTop = 0; + } + else if (PtrTop + PtrHeight > SurfHeight) + { + // crop mouseshape height + SrcHeight = SurfHeight - PtrTop; + } + + // QI for IDXGISurface + CComPtr ipCopySurface; + hr = pSharedSurf->QueryInterface(__uuidof(IDXGISurface), (void **)&ipCopySurface); + if (SUCCEEDED(hr)) { + // Map pixels + DXGI_MAPPED_RECT MappedSurface; + hr = ipCopySurface->Map(&MappedSurface, DXGI_MAP_READ | DXGI_MAP_WRITE); + if (SUCCEEDED(hr)) + { + // 0xAARRGGBB + UINT* SrcBuffer32 = reinterpret_cast(InitBuffer); + UINT* DstBuffer32 = reinterpret_cast(MappedSurface.pBits) + PtrTop * SurfWidth + PtrLeft; + + // Alpha blending masks + const UINT AMask = 0xFF000000; + const UINT RBMask = 0x00FF00FF; + const UINT GMask = 0x0000FF00; + const UINT AGMask = AMask | GMask; + const UINT OneAlpha = 0x01000000; + UINT uiPixel1; + UINT uiPixel2; + UINT uiAlpha; + UINT uiNAlpha; + UINT uiRedBlue; + UINT uiAlphaGreen; + + for (INT Row = SrcTop; Row < SrcHeight; ++Row) + { + for (INT Col = SrcLeft; Col < SrcWidth; ++Col) + { + // Alpha blending + uiPixel1 = DstBuffer32[((Row - SrcTop) * SurfWidth) + (Col - SrcLeft)]; + uiPixel2 = SrcBuffer32[(Row * PtrWidth) + Col]; + uiAlpha = (uiPixel2 & AMask) >> 24; + uiNAlpha = 255 - uiAlpha; + uiRedBlue = ((uiNAlpha * (uiPixel1 & RBMask)) + (uiAlpha * (uiPixel2 & RBMask))) >> 8; + uiAlphaGreen = (uiNAlpha * ((uiPixel1 & AGMask) >> 8)) + (uiAlpha * (OneAlpha | ((uiPixel2 & GMask) >> 8))); + + DstBuffer32[((Row - SrcTop) * SurfWidth) + (Col - SrcLeft)] = ((uiRedBlue & RBMask) | (uiAlphaGreen & AGMask)); + } + } + } + + // Done with resource + hr = ipCopySurface->Unmap(); + } + + return S_OK; + } // DrawMouse + + static + COM_DECLSPEC_NOTHROW + inline + HRESULT + CreateBitmap( + _In_ ID2D1RenderTarget *pRenderTarget, + _In_ ID3D11Texture2D *pSourceTexture, + _Outptr_ ID2D1Bitmap **ppOutBitmap + ) + { + CHECK_POINTER(ppOutBitmap); + *ppOutBitmap = nullptr; + CHECK_POINTER_EX(pRenderTarget, E_INVALIDARG); + CHECK_POINTER_EX(pSourceTexture, E_INVALIDARG); + + HRESULT hr = S_OK; + CComPtr ipSourceTexture(pSourceTexture); + CComPtr ipCopySurface; + CComPtr ipD2D1SourceBitmap; + + // QI for IDXGISurface + hr = ipSourceTexture->QueryInterface(__uuidof(IDXGISurface), (void **)&ipCopySurface); + CHECK_HR_RETURN(hr); + + // Map pixels + DXGI_MAPPED_RECT MappedSurface; + hr = ipCopySurface->Map(&MappedSurface, DXGI_MAP_READ); + CHECK_HR_RETURN(hr); + + D3D11_TEXTURE2D_DESC destImageDesc; + ipSourceTexture->GetDesc(&destImageDesc); + + hr = pRenderTarget->CreateBitmap( + D2D1::SizeU(destImageDesc.Width, destImageDesc.Height), + (const void*)MappedSurface.pBits, + MappedSurface.Pitch, + D2D1::BitmapProperties(D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED)), + &ipD2D1SourceBitmap); + if (FAILED(hr)) + { + // Done with resource + hr = ipCopySurface->Unmap(); + return hr; + } + + // Done with resource + hr = ipCopySurface->Unmap(); + CHECK_HR_RETURN(hr); + + // set return value + *ppOutBitmap = ipD2D1SourceBitmap.Detach(); + + return S_OK; + } // CreateBitmap + + static + inline + COM_DECLSPEC_NOTHROW + HRESULT + GetContainerFormatByFileName( + _In_ LPCWSTR lpcwFileName, + _Out_opt_ GUID *pRetVal = NULL + ) + { + RESET_POINTER_EX(pRetVal, GUID_NULL); + CHECK_POINTER_EX(lpcwFileName, E_INVALIDARG); + + if (lstrlenW(lpcwFileName) == 0) { + return E_INVALIDARG; + } + + LPCWSTR lpcwExtension = ::PathFindExtensionW(lpcwFileName); + if (lstrlenW(lpcwExtension) == 0) { + return MK_E_INVALIDEXTENSION; // ERROR_MRM_INVALID_FILE_TYPE + } + + if (lstrcmpiW(lpcwExtension, L".bmp") == 0) + { + RESET_POINTER_EX(pRetVal, GUID_ContainerFormatBmp); + } + else if ((lstrcmpiW(lpcwExtension, L".tif") == 0) || + (lstrcmpiW(lpcwExtension, L".tiff") == 0)) + { + RESET_POINTER_EX(pRetVal, GUID_ContainerFormatTiff); + } + else if (lstrcmpiW(lpcwExtension, L".png") == 0) + { + RESET_POINTER_EX(pRetVal, GUID_ContainerFormatPng); + } + else if ((lstrcmpiW(lpcwExtension, L".jpg") == 0) || + (lstrcmpiW(lpcwExtension, L".jpeg") == 0)) + { + RESET_POINTER_EX(pRetVal, GUID_ContainerFormatJpeg); + } + else + { + return ERROR_MRM_INVALID_FILE_TYPE; + } + + return S_OK; + } + + + static + COM_DECLSPEC_NOTHROW + inline + HRESULT + SaveImageToFile( + _In_ IWICImagingFactory *pWICImagingFactory, + _In_ IWICBitmapSource *pWICBitmapSource, + _In_ LPCWSTR lpcwFileName + ) + { + CHECK_POINTER_EX(pWICImagingFactory, E_INVALIDARG); + CHECK_POINTER_EX(pWICBitmapSource, E_INVALIDARG); + + HRESULT hr = S_OK; + GUID guidContainerFormat; + + hr = GetContainerFormatByFileName(lpcwFileName, &guidContainerFormat); + if (FAILED(hr)) { + return hr; + } + + WICPixelFormatGUID format = GUID_WICPixelFormatDontCare; + CComPtr ipWICImagingFactory(pWICImagingFactory); + CComPtr ipWICBitmapSource(pWICBitmapSource); + CComPtr ipStream; + CComPtr ipEncoder; + CComPtr ipFrameEncode; + unsigned int uiWidth = 0; + unsigned int uiHeight = 0; + + hr = ipWICImagingFactory->CreateStream(&ipStream); + if (SUCCEEDED(hr)) { + hr = ipStream->InitializeFromFilename(lpcwFileName, GENERIC_WRITE); + } + + if (SUCCEEDED(hr)) { + hr = ipWICImagingFactory->CreateEncoder(guidContainerFormat, NULL, &ipEncoder); + } + if (SUCCEEDED(hr)) + { + hr = ipEncoder->Initialize(ipStream, WICBitmapEncoderNoCache); + } + if (SUCCEEDED(hr)) + { + hr = ipEncoder->CreateNewFrame(&ipFrameEncode, NULL); + } + if (SUCCEEDED(hr)) + { + hr = ipFrameEncode->Initialize(NULL); + } + if (SUCCEEDED(hr)) + { + hr = ipWICBitmapSource->GetSize(&uiWidth, &uiHeight); + } + if (SUCCEEDED(hr)) + { + hr = ipFrameEncode->SetSize(uiWidth, uiHeight); + } + if (SUCCEEDED(hr)) + { + hr = ipFrameEncode->SetPixelFormat(&format); + } + if (SUCCEEDED(hr)) + { + hr = ipFrameEncode->WriteSource(ipWICBitmapSource, NULL); + } + if (SUCCEEDED(hr)) + { + hr = ipFrameEncode->Commit(); + } + if (SUCCEEDED(hr)) + { + hr = ipEncoder->Commit(); + } + + return hr; + } // SaveImageToFile + +}; // end class DXGICaptureHelper + +#endif // __DXGICAPTUREHELPER_H__ diff --git a/media/DXGICaptureTypes.h b/media/DXGICaptureTypes.h new file mode 100644 index 0000000..bda220f --- /dev/null +++ b/media/DXGICaptureTypes.h @@ -0,0 +1,150 @@ +/***************************************************************************** +* DXGICaptureTypes.h +* +* Copyright (C) 2020 Gokhan Erdogdu +* +* DXGICapture is free software; you can redistribute it and/or modify it under +* the terms of the GNU Lesser General Public License as published by the Free +* Software Foundation; either version 2.1 of the License, or (at your option) +* any later version. +* +* DXGICapture is distributed in the hope that it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more +* details. +* +******************************************************************************/ +#pragma once +#ifndef __DXGICAPTURETYPES_H__ +#define __DXGICAPTURETYPES_H__ + +#include +#include +#include +#include + +// +// enum tagFrameSizeMode_e +// +typedef enum tagFrameSizeMode_e : UINT +{ + tagFrameSizeMode_Normal = 0x0, + tagFrameSizeMode_StretchImage = 0x1, + tagFrameSizeMode_AutoSize = 0x2, + tagFrameSizeMode_CenterImage = 0x3, + tagFrameSizeMode_Zoom = 0x4, +} tagFrameSizeMode; + +// +// enum tagFrameRotationMode_e +// +typedef enum tagFrameRotationMode_e : UINT +{ + tagFrameRotationMode_Auto = 0x0, + tagFrameRotationMode_Identity = 0x1, + tagFrameRotationMode_90 = 0x2, + tagFrameRotationMode_180 = 0x3, + tagFrameRotationMode_270 = 0x4, +} tagFrameRotationMode; + +// +// Holds info about the pointer/cursor +// struct tagMouseInfo_s +// +typedef struct tagMouseInfo_s +{ + UINT ShapeBufferSize; + _Field_size_bytes_(ShapeBufferSize) BYTE* PtrShapeBuffer; + DXGI_OUTDUPL_POINTER_SHAPE_INFO ShapeInfo; + POINT Position; + bool Visible; + UINT WhoUpdatedPositionLast; + LARGE_INTEGER LastTimeStamp; +} tagMouseInfo; + +// +// struct tagFrameSize_s +// +typedef struct tagFrameSize_s +{ + LONG Width; + LONG Height; +} tagFrameSize; + +// +// struct tagBounds_s +// +typedef struct tagFrameBounds_s +{ + LONG X; + LONG Y; + LONG Width; + LONG Height; +} tagFrameBounds; + +// +// struct tagFrameBufferInfo_s +// +typedef struct tagFrameBufferInfo_s +{ + UINT BufferSize; + _Field_size_bytes_(BufferSize) BYTE* Buffer; + INT BytesPerPixel; + tagFrameBounds Bounds; + INT Pitch; +} tagFrameBufferInfo; + +// +// struct tagDublicatorMonitorInfo_s +// +typedef struct tagDublicatorMonitorInfo_s +{ + INT Idx; + WCHAR DisplayName[64]; + INT RotationDegrees; + tagFrameBounds Bounds; +} tagDublicatorMonitorInfo; + +typedef std::vector DublicatorMonitorInfoVec; + +// +// struct tagScreenCaptureFilterConfig_s +// +typedef struct tagScreenCaptureFilterConfig_s +{ +public: + INT MonitorIdx; + INT ShowCursor; + tagFrameRotationMode RotationMode; + tagFrameSizeMode SizeMode; + tagFrameSize OutputSize; /* Discard for tagFrameSizeMode_AutoSize */ +} tagScreenCaptureFilterConfig; + +// +// struct tagRendererInfo_s +// +typedef struct tagRendererInfo_s +{ + INT MonitorIdx; + INT ShowCursor; + tagFrameRotationMode RotationMode; + tagFrameSizeMode SizeMode; + tagFrameSize OutputSize; + + FLOAT RotationDegrees; + FLOAT ScaleX; + FLOAT ScaleY; + DXGI_FORMAT SrcFormat; + tagFrameBounds SrcBounds; + tagFrameBounds DstBounds; +} tagRendererInfo; + +// macros +#define RESET_POINTER_EX(p, v) if (nullptr != (p)) { *(p) = (v); } +#define RESET_POINTER(p) RESET_POINTER_EX(p, nullptr) +#define CHECK_POINTER_EX(p, hr) if (nullptr == (p)) { return (hr); } +#define CHECK_POINTER(p) CHECK_POINTER_EX(p, E_POINTER) +#define CHECK_HR_BREAK(hr) if (FAILED(hr)) { break; } +#define CHECK_HR_RETURN(hr) { HRESULT hr_379f4648 = hr; if (FAILED(hr_379f4648)) { return hr_379f4648; } } + +#endif // __DXGICAPTURETYPES_H__ diff --git a/media/Debuger.h b/media/Debuger.h new file mode 100644 index 0000000..1d65800 --- /dev/null +++ b/media/Debuger.h @@ -0,0 +1,15 @@ +#pragma once +#include +using namespace std; + +class Debuger +{ +public: + Debuger(); + ~Debuger(); + static int Debug(wstring log); + static int Debug(const wchar_t *format, ...); + static int Debug(string log); + +}; + diff --git a/media/H264Docoder.cpp b/media/H264Docoder.cpp new file mode 100644 index 0000000..c2d98d3 --- /dev/null +++ b/media/H264Docoder.cpp @@ -0,0 +1,100 @@ +#include "H264Docoder.h" +#include "Debuger.h" +extern "C" { +#include "libswscale/swscale.h" +#include "libavformat/avformat.h" +#include "libavcodec/avcodec.h" +#include "libswscale/swscale.h" +#include "libavutil/pixfmt.h" +} + +H264decoder::H264decoder() + :mObserver(nullptr){ + this->mObserverType = Observer_Video; + avcodec_register_all(); + mCodec = avcodec_find_decoder(AV_CODEC_ID_H264); + if (!mCodec) { + cout << "could not found 264 decoder" << endl; + exit(1); + } + mCtx = avcodec_alloc_context3(mCodec); + picture = av_frame_alloc(); + if ((mCodec->capabilities)&AV_CODEC_CAP_TRUNCATED) + (mCtx->flags) |= AV_CODEC_FLAG2_CHUNKS; + mCtx->height = 720; + mCtx->width = 1280; + if (avcodec_open2(mCtx, mCodec, NULL) < 0) { + cout << "could not open codec\n"; + exit(1); + } + +} +H264decoder::~H264decoder() +{ + +} +// +// Created by 29019 on 2019/5/7. +// +const int width = 640; +const int height = 480; +const int framesize = width * height * 3 / 2; //һͼظ + +VData *H264decoder::Decodes(void *dat,uint32_t size) { + //FILE *pOut = fopen("pic.yuv","wb+"); + AVPacket pkt; + int got_picture = 0; + int len = 0; + + picture = av_frame_alloc(); + av_init_packet(&pkt); + + char* data = (char*)dat; + pkt.data = (uint8_t *)data; + pkt.size = size; + + len = avcodec_decode_video2(this->mCtx, picture, &got_picture, &pkt); + if (len < 0) { + printf("Error while decoding a frame.\n"); + return nullptr; + } + if (got_picture == 0) { + return nullptr; + } + ++frame; + AVPixelFormat pix; + int pic_size = avpicture_get_size(AV_PIX_FMT_YUVJ420P, picture->width, picture->height); + /* + cout << "receive width " << picture->width << " height " + << picture->height<<"pic size " + << pic_size <<" channel layout " + << picture->linesize[0]<< endl;*/ + + uint32_t pitchY = picture->linesize[0]; + uint32_t pitchU = picture->linesize[1]; + uint32_t pitchV = picture->linesize[2]; + + uint8_t *avY = picture->data[0]; + uint8_t *avU = picture->data[1]; + uint8_t *avV = picture->data[2]; + if (nullptr != mObserver) { + this->mObserver->OnRecieveData(picture); + } + av_frame_free(&picture); + +} + +void H264decoder::OnRtmpFrame(void * dat, uint32_t size) +{ + //Debuger::Debug(L"get data\r\n"); + this->Decodes(dat, size); +} + +int H264decoder::SetObserver(H264DecodeObserver *p) +{ + if (nullptr != p) + this->mObserver = p; + else + return -1; + return 0; +} diff --git a/media/H264Docoder.h b/media/H264Docoder.h new file mode 100644 index 0000000..da711c6 --- /dev/null +++ b/media/H264Docoder.h @@ -0,0 +1,58 @@ +#pragma once +#include +#include +#include +#include +#include +#include "RtmpPuller.h" +#include "RtmpPuller2.h" + +using namespace std; +extern "C" { +#include "libavutil/pixfmt.h" +#include "libavcodec/avcodec.h" +#include "sdl/SDL.h" +} +#define INBUF_SIZE 4096 +typedef vector VData; +class Decoder { +private: + list mDecodeData; +public: + virtual int Decode(VData &dat) { return -1; }; +}; + +typedef class H264decoder :public Decoder, public RtmpPuller2::RtmpPullObserver { +public: + class H264DecodeObserver { + public: + virtual int OnRecieveData(AVFrame *frame) { return 0; }; + }; + enum CAP_STATUS { + RUNNING = 1, + STOP = 2, + PAUSE = 3, + FAIL = 4, + }; + H264decoder(); + ~H264decoder(); + VData *Decodes(void *dat, uint32_t len); + void OnRtmpFrame(void * dat, uint32_t size); + int SetObserver(H264DecodeObserver *); +private: + AVCodec *mCodec; + AVCodecContext *mCtx = NULL; + int frame, got_picture, len; + AVFrame *picture; + AVPacket avpkt; + H264DecodeObserver *mObserver; + + //SDL--------------------------- + int screen_w = 0, screen_h = 0; + SDL_Window *screen; + SDL_Renderer* sdlRenderer; + SDL_Texture* sdlTexture; + SDL_Rect sdlRect; + + +}CH264Decoder; diff --git a/media/ImageUtil.cpp b/media/ImageUtil.cpp new file mode 100644 index 0000000..d10ad15 --- /dev/null +++ b/media/ImageUtil.cpp @@ -0,0 +1,36 @@ +#pragma once +#include "ImageUtil.h" + +bool GuidCompare(GUID g1, GUID g2) { + if (g1.Data1 != g2.Data1) { + return false; + } + if (g1.Data2 != g2.Data2) { + return false; + } + if (g1.Data3 != g2.Data3) { + return false; + } + + return true; +} + +AVPixelFormat GetFormatFromGuid(GUID g) +{ + if (GuidCompare(g, MEDIASUBTYPE_YUY2)) { + return AV_PIX_FMT_YUYV422; + } + if (GuidCompare(g, MEDIASUBTYPE_RGB24)) { + return AV_PIX_FMT_RGB24; + } + if (GuidCompare(g, MEDIASUBTYPE_RGB32)) { + return AV_PIX_FMT_RGB32; + } + if (GuidCompare(g, MEDIASUBTYPE_MJPG)) { + return AV_PIX_FMT_YUVJ420P; + } + if (GuidCompare(g, MEDIASUBTYPE_IYUV)) { + return AV_PIX_FMT_YUYV422; + } + return AV_PIX_FMT_NONE; +} diff --git a/media/ImageUtil.h b/media/ImageUtil.h new file mode 100644 index 0000000..fb16758 --- /dev/null +++ b/media/ImageUtil.h @@ -0,0 +1,12 @@ +#pragma once +#include "guiddef.h" +#include "uuids.h" +extern "C" { +#include "libswscale/swscale.h" +#include "libavformat/avformat.h" +#include "libavcodec/avcodec.h" +#include "libswscale/swscale.h" +#include "libavutil/pixfmt.h" +} + +AVPixelFormat GetFormatFromGuid(GUID g); diff --git a/media/RtmpPuller.cpp b/media/RtmpPuller.cpp new file mode 100644 index 0000000..356712a --- /dev/null +++ b/media/RtmpPuller.cpp @@ -0,0 +1,132 @@ +#include "RtmpPuller.h" + + +RtmpPuller::RtmpPuller() + :mFrameIndex(0), mAudioIndex(-1),mVideoIndex(-1) + , mAudioStream(nullptr),mVideoStream(nullptr) +{ + av_register_all(); + //Network + avformat_network_init(); + //Input +} + +int RtmpPuller::ConnectServer(const char *p) +{ + int ret = 0; + if ((ret = avformat_open_input(&mIfmtCtx, p, 0, 0)) < 0) { + printf("Could not open input file."); + return -1; + } + if ((ret = avformat_find_stream_info(mIfmtCtx, 0)) < 0) { + printf("Failed to retrieve input stream information"); + return -1; + } + for (int i = 0; i < mIfmtCtx->nb_streams; i++) { + if (mIfmtCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) { + mVideoIndex = i; + } + if (mIfmtCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) { + mAudioIndex = i; + } + } + if(mAudioIndex > -1) + this->mAudioStream = mIfmtCtx->streams[mAudioIndex]; + if(mVideoIndex > -1) + this->mVideoStream = mIfmtCtx->streams[mVideoIndex]; + av_dump_format(mIfmtCtx, 0, p, 0); + mH264bsfc = av_bitstream_filter_init("h264_mp4toannexb"); + mStatus = RUNNING; + if((mAudioIndex == -1 ) &&(mVideoIndex == -1)) + mStatus = NOSOURCE; + return 0; +} + +int ThreadPull(RtmpPuller*p) { + while (p->Status() == RtmpPuller::CAP_STATUS::RUNNING) { + p->PullData(); + } + return 0; +} + +int RtmpPuller::StartPull() +{ + this->mThread = new std::thread(ThreadPull, this); + this->mThread->get_id(); + mStatus = RUNNING; + return 0; +} + +int RtmpPuller::PullData() +{ + static int drop = 0; + AVStream *in_stream; + //Get an AVPacket + int ret = av_read_frame(mIfmtCtx, &pkt); + if (ret < 0) + return -1; + in_stream = mIfmtCtx->streams[pkt.stream_index]; + /* copy packet */ + //Convert PTS/DTS + pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, in_stream->time_base, + (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)); + pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, in_stream->time_base, + (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)); + pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, in_stream->time_base); + pkt.pos = -1; + //Print to Screen + if (drop < 100) { + drop++; + goto end; + } + if (pkt.stream_index == mVideoIndex) { + printf("Receive %8d video frames from input URL\n", mFrameIndex); + mFrameIndex++; + av_bitstream_filter_filter(mH264bsfc, in_stream->codec, NULL, + &pkt.data, &pkt.size, pkt.data, pkt.size, 0); + if (mObserver.size() > 0) { + for (auto itr = this->mObserver.begin(); itr != mObserver.end(); itr++) { + RtmpPullObserver *p = (RtmpPullObserver *)*itr; + if (p->mObserverType == RtmpPullObserver::Observer_Video) { + p->OnRtmpFrame(pkt.data, pkt.size); + } + } + } + } + if (pkt.stream_index == mAudioIndex) { + if (mObserver.size() > 0) { + for (auto itr = this->mObserver.begin(); itr != mObserver.end(); itr++) { + RtmpPullObserver *p = (RtmpPullObserver *)*itr; + if (p->mObserverType == RtmpPullObserver::Observer_Audio) { + p->OnRtmpFrame(pkt.data, pkt.size); + } + } + } + } +end: + //printf("%02x %02x %02x %02x %02x\r\n", pkt.data[0], pkt.data[1], pkt.data[2], pkt.data[3], pkt.data[4]); + av_free_packet(&pkt); +} + +int RtmpPuller::SetObserver(RtmpPullObserver *p) +{ + if (nullptr == p) + return -1; + mMux.lock(); + for (auto itr = this->mObserver.begin(); itr != mObserver.end(); itr++) { + if (p == *itr) return 0; + } + this->mObserver.push_back(p); + mMux.unlock(); + return 0; +} + +RtmpPuller::CAP_STATUS RtmpPuller::Status() +{ + return this->mStatus; +} + +AVStream * RtmpPuller::AudioStream() +{ + return this->mAudioStream; +} diff --git a/media/RtmpPuller.h b/media/RtmpPuller.h new file mode 100644 index 0000000..76b0920 --- /dev/null +++ b/media/RtmpPuller.h @@ -0,0 +1,64 @@ +#pragma once +//Windows +#include +#include +#include +#include +using namespace std; + +extern "C" +{ +#include "libavformat/avformat.h" +#include "libavutil/mathematics.h" +#include "libavutil/time.h" +}; + +#pragma comment (lib, "ws2_32.lib") +#pragma comment (lib, "Secur32.lib") +#pragma comment (lib, "Bcrypt.lib") + +class RtmpPuller { +public: + class RtmpPullObserver { + public : + enum ObserverType { + Observer_Video = 0, + Observer_Audio = 1, + }; + virtual void OnRtmpFrame(void * dat, uint32_t size) {}; + ObserverType mObserverType; + }; + enum CAP_STATUS { + RUNNING = 1, + STOP = 2, + PAUSE = 3, + FAIL = 4, + NOSOURCE = 6, + }; + RtmpPuller(); + int ConnectServer(const char *); + int StartPull(); + int PullData(); + int SetObserver(RtmpPullObserver *); + CAP_STATUS Status(); + AVStream *AudioStream(); +private: + CAP_STATUS mStatus; + AVOutputFormat *mOutFormat = NULL; + //Input AVFormatContext and Output AVFormatContext + AVFormatContext *mIfmtCtx = NULL; + AVPacket pkt; + string mRtmpUrl; + int mVideoIndex; + int mAudioIndex; + + int mFrameIndex; + AVBitStreamFilterContext* mH264bsfc; + std::thread *mThread; + vector mObserver; + AVStream *mAudioStream; + AVStream *mVideoStream; + mutex mMux; +}; + +int ThreadPull(RtmpPuller*p); diff --git a/media/RtmpPuller2.cpp b/media/RtmpPuller2.cpp new file mode 100644 index 0000000..b97b573 --- /dev/null +++ b/media/RtmpPuller2.cpp @@ -0,0 +1,251 @@ +#include "RtmpPuller2.h" +#include "Debuger.h" +RtmpPuller2::RtmpPuller2() +{ + + mAccBuffer = new uint8_t[3000]; +} +RtmpPuller2::~RtmpPuller2() +{ +} + +int ThreadPull(RtmpPuller2*p) { + while (p->Status() == RtmpPuller2::CAP_STATUS::RUNNING) { + p->PullData(); + Sleep(10); + } + return 0; +} +// ر +int RtmpPuller2::StopPull() +{ + mStatus = STOP; + this->mThread->join(); + RTMP_Close(mRtmp); + RTMP_Free(mRtmp); + return 0; +} + +int RtmpPuller2::StartPull() +{ + if(this->mStatus == CONNECTED) { + mStatus = RUNNING; + this->mThread = new std::thread(ThreadPull, this); + this->mThread->get_id(); + } + else { + } + return 0; +} + +FILE *fp = nullptr; +int RtmpPuller2::PullData() +{ + RTMPPacket packet = { 0 }; + // Parse rtmp stream to h264 and aac + uint8_t nalu_header[4] = { 0x00, 0x00, 0x00, 0x01 }; + int ret = RTMP_ReadPacket(mRtmp, &packet); + if (ret < 0) + return ret; + + if (nullptr == fp) { + fp = fopen("src.aac", "wb"); + } + if (RTMPPacket_IsReady(&packet)) { + // Process packet, eg: set chunk size, set bw, ... + RTMP_ClientPacket(mRtmp, &packet); + if (packet.m_packetType == RTMP_PACKET_TYPE_VIDEO) { + bool keyframe = 0x17 == packet.m_body[0] ? true : false; + bool sequence = 0x00 == packet.m_body[1]; + printf("keyframe=%s, sequence=%s\n", keyframe ? "true" : "false", sequence ? "true" : "false"); + // SPS/PPS sequence + if (sequence) { + uint32_t offset = 10; + uint32_t sps_num = packet.m_body[offset++] & 0x1f; + for (int i = 0; i < sps_num; i++) { + uint8_t ch0 = packet.m_body[offset]; + uint8_t ch1 = packet.m_body[offset + 1]; + uint32_t sps_len = ((ch0 << 8) | ch1); + offset += 2; + packet.m_body[offset - 1] = 0x01; + packet.m_body[offset - 2] = 0x00; + packet.m_body[offset - 3] = 0x00; + packet.m_body[offset - 4] = 0x00; + + if (mObserver.size() > 0) { + for (auto itr = this->mObserver.begin(); itr != mObserver.end(); itr++) { + RtmpPullObserver *p = (RtmpPullObserver *)*itr; + if (p->mObserverType == RtmpPullObserver::Observer_Video) { + p->OnRtmpFrame(packet.m_body + offset - 4, sps_len + 4); + } + } + } + // Write sps data + //fwrite(nalu_header, sizeof(uint8_t), 4, _file_ptr); + //fwrite(packet.m_body + offset, sizeof(uint8_t), sps_len, _file_ptr); + offset += sps_len; + } + uint32_t pps_num = packet.m_body[offset++] & 0x1f; + for (int i = 0; i < pps_num; i++) { + uint8_t ch0 = packet.m_body[offset]; + uint8_t ch1 = packet.m_body[offset + 1]; + uint32_t pps_len = ((ch0 << 8) | ch1); + offset += 2; + packet.m_body[offset - 1] = 0x01; + packet.m_body[offset - 2] = 0x00; + packet.m_body[offset - 3] = 0x00; + packet.m_body[offset - 4] = 0x00; + if (mObserver.size() > 0) { + for (auto itr = this->mObserver.begin(); itr != mObserver.end(); itr++) { + RtmpPullObserver *p = (RtmpPullObserver *)*itr; + if (p->mObserverType == RtmpPullObserver::Observer_Video) { + p->OnRtmpFrame(packet.m_body + offset - 4, pps_len + 4); + } + } + } + // Write pps data + offset += pps_len; + } + } + // Nalu frames + else { + uint32_t offset = 5; + uint8_t ch0 = packet.m_body[offset]; + uint8_t ch1 = packet.m_body[offset + 1]; + uint8_t ch2 = packet.m_body[offset + 2]; + uint8_t ch3 = packet.m_body[offset + 3]; + uint32_t data_len = ((ch0 << 24) | (ch1 << 16) | (ch2 << 8) | ch3); + offset += 4; + packet.m_body[offset - 1] = 0x01; + packet.m_body[offset - 2] = 0x00; + packet.m_body[offset - 3] = 0x00; + packet.m_body[offset - 4] = 0x00; + + if (mObserver.size() > 0) { + for (auto itr = this->mObserver.begin(); itr != mObserver.end(); itr++) { + RtmpPullObserver *p = (RtmpPullObserver *)*itr; + if (p->mObserverType == RtmpPullObserver::Observer_Video) { + p->OnRtmpFrame(packet.m_body + offset - 4, data_len + 4); + } + } + } + // Write nalu data(already started with '0x00,0x00,0x00,0x01') + //fwrite(nalu_header, sizeof(uint8_t), 4, _file_ptr); + offset += data_len; + } + } + else if (packet.m_packetType == RTMP_PACKET_TYPE_AUDIO) { + bool sequence = 0x00 == packet.m_body[1]; + printf("sequence=%s\n", sequence ? "true" : "false"); + // AAC sequence + if (sequence) { + uint8_t format = (packet.m_body[0] & 0xf0) >> 4; + uint8_t samplerate = (packet.m_body[0] & 0x0c) >> 2; + uint8_t sampledepth = (packet.m_body[0] & 0x02) >> 1; + uint8_t type = packet.m_body[0] & 0x01; + // sequence = packet.m_body[1]; + // AAC(AudioSpecificConfig) + if (format == 10) { + ch0 = packet.m_body[2]; + ch1 = packet.m_body[3]; + config = ((ch0 << 8) | ch1); + object_type = (config & 0xF800) >> 11; + sample_frequency_index = (config & 0x0780) >> 7; + channels = (config & 0x78) >> 3; + frame_length_flag = (config & 0x04) >> 2; + depend_on_core_coder = (config & 0x02) >> 1; + extension_flag = config & 0x01; + } + // Speex(Fix data here, so no need to parse...) + else if (format == 11) { + // 16 KHz, mono, 16bit/sample + type = 0; + sampledepth = 1; + samplerate = 4; + } + } + // Audio frames + else { + // ADTS(7 bytes) + AAC data + uint32_t data_len = packet.m_nBodySize - 2 + 7; + uint8_t adts[7]; + adts[0] = 0xff; + adts[1] = 0xf1; + adts[2] = ((object_type - 1) << 6) | (sample_frequency_index << 2) + | (channels >> 2); + adts[3] = ((channels & 3) << 6) + (data_len >> 11); + adts[4] = (data_len & 0x7FF) >> 3; + adts[5] = ((data_len & 7) << 5) + 0x1F; + adts[6] = 0xfc; + // Write audio frames + fwrite(adts, sizeof(uint8_t), 7, fp); + fwrite(packet.m_body + 2, sizeof(uint8_t), packet.m_nBodySize - 2, fp); + fflush(fp); + memcpy(mAccBuffer, adts, 7); + memcpy(mAccBuffer + 7, packet.m_body + 2, packet.m_nBodySize - 2); + + if (mObserver.size() > 0) { + for (auto itr = this->mObserver.begin(); itr != mObserver.end(); itr++) { + RtmpPullObserver *p = (RtmpPullObserver *)*itr; + if (p->mObserverType == RtmpPullObserver::Observer_Audio) { + p->OnRtmpFrame(mAccBuffer, packet.m_nBodySize - 2 + 7); + } + } + } + } + } + RTMPPacket_Free(&packet); + } + return 0; +} + +int RtmpPuller2::SetObserver(RtmpPuller2::RtmpPullObserver *p) +{ + if (nullptr == p) + return -1; + mMux.lock(); + for (auto itr = this->mObserver.begin(); itr != mObserver.end(); itr++) { + if (p == *itr) return 0; + } + this->mObserver.push_back(p); + mMux.unlock(); + return 0; +} + +RtmpPuller2::CAP_STATUS RtmpPuller2::Status() +{ + return mStatus; +} + + +int RtmpPuller2::ConnectServer(string url) +{ + mRtmp = RTMP_Alloc(); + RTMP_Init(mRtmp); + if (RTMP_SetupURL(mRtmp, (char*)url.c_str()) == FALSE) + { + RTMP_Free(mRtmp); + mStatus = FAIL; + return -1; + } + /*ӷ*/ + if (RTMP_Connect(mRtmp, NULL) == FALSE) + { + RTMP_Free(mRtmp); + mStatus = FAIL; + return -1; + } + /**/ + if (RTMP_ConnectStream(mRtmp, 0) == FALSE) + { + RTMP_Close(mRtmp); + RTMP_Free(mRtmp); + mStatus = FAIL; + return -1; + } + mStatus = CONNECTED; + return 0; +} + + + diff --git a/media/RtmpPuller2.h b/media/RtmpPuller2.h new file mode 100644 index 0000000..81c6601 --- /dev/null +++ b/media/RtmpPuller2.h @@ -0,0 +1,78 @@ +#pragma once + +#ifdef __cplusplus +extern "C"{ +#endif + +extern "C" { +#include "librtmp\rtmp.h" +#include "librtmp\rtmp_sys.h" +#include "librtmp\amf.h" +} + +#ifdef __cplusplus +} +#endif +#include +#include +#include +#include +#include +using namespace std; +#ifdef WIN32 +#include +#pragma comment(lib,"WS2_32.lib") +#pragma comment(lib,"winmm.lib") +#endif + +class RtmpPuller2 +{ +public: + class RtmpPullObserver { + public: + enum ObserverType { + Observer_Video = 0, + Observer_Audio = 1, + }; + virtual void OnRtmpFrame(void * dat, uint32_t size) {}; + ObserverType mObserverType; + }; + enum CAP_STATUS { + CONNECTED = 0, + RUNNING = 1, + STOP = 2, + PAUSE = 3, + FAIL = 4, + NOSOURCE = 6, + }; + RtmpPuller2(); + ~RtmpPuller2(); + + int StopPull(); + int StartPull(); + int PullData(); + int SetObserver(RtmpPuller2::RtmpPullObserver *); + CAP_STATUS Status(); + + int ConnectServer(string url); +private: + std::thread *mThread; + RTMP *mRtmp; + string mUrl; + CAP_STATUS mStatus; + vector mObserver; + mutex mMux; + uint8_t *mAccBuffer; + // adts ͷϢΪaacֻװ + uint8_t ch0 = 0; + uint8_t ch1 = 0; + uint16_t config = 0; + uint16_t object_type = 0; + uint16_t sample_frequency_index = 0; + uint16_t channels = 0; + uint16_t frame_length_flag = 0; + uint16_t depend_on_core_coder = 0; + uint16_t extension_flag = 0; + +}; + diff --git a/media/RtmpPusher.cpp b/media/RtmpPusher.cpp new file mode 100644 index 0000000..8fb71da --- /dev/null +++ b/media/RtmpPusher.cpp @@ -0,0 +1,573 @@ +#include "RtmpPusher.h" + +/** + * ʼwinsock + * + * @ɹ򷵻1 , ʧ򷵻Ӧ + */ +int InitSockets() +{ +#ifdef WIN32 + WORD version; + WSADATA wsaData; + version = MAKEWORD(1, 1); + return (WSAStartup(version, &wsaData) == 0); +#else + return TRUE; +#endif +} + +bool RtmpPusher::IfConnect() +{ + return mIfConnected; +} + +int RtmpPusher::RTMP264_Connect(const char* url) +{ + InitSockets(); + m_pRtmp = RTMP_Alloc(); + RTMP_Init(m_pRtmp); + /*URL*/ + if (RTMP_SetupURL(m_pRtmp, (char*)url) == FALSE) + { + RTMP_Free(m_pRtmp); + return -1; + } + /*ÿд,,ǰʹ,Ч*/ + RTMP_EnableWrite(m_pRtmp); + /*ӷ*/ + if (RTMP_Connect(m_pRtmp, NULL) == FALSE) + { + RTMP_Free(m_pRtmp); + return -1; + } + + /**/ + if (RTMP_ConnectStream(m_pRtmp, 0) == FALSE) + { + RTMP_Close(m_pRtmp); + RTMP_Free(m_pRtmp); + return -1; + } + + this->mUrl = string(url); + this->mIfConnected = true; + return 0; +} +/** + * ͷwinsock + * + * @ɹ򷵻0 , ʧ򷵻Ӧ + */ +inline void CleanupSockets() +{ +#ifdef WIN32 + WSACleanup(); +#endif +} + +void RtmpPusher::RTMP264_Close() +{ + mMux.lock(); + if (m_pRtmp) + { + RTMP_Close(m_pRtmp); + RTMP_Free(m_pRtmp); + m_pRtmp = NULL; + } + mMux.unlock(); + CleanupSockets(); +} +RTMPPacket* gPacket = nullptr; + +int RtmpPusher::SendPacket(unsigned int nPacketType, unsigned char * data, + unsigned int size, unsigned int nTimestamp) +{ + static bool once = true; + /*ڴͳʼ,lenΪ峤*/ + if(nullptr == gPacket) + gPacket = (RTMPPacket *)malloc(640*720*3 + size); + memset(gPacket, 0, RTMP_HEAD_SIZE); + /*ڴ*/ + + gPacket->m_body = (char *)gPacket + RTMP_HEAD_SIZE; + gPacket->m_nBodySize = size; + memcpy(gPacket->m_body, data, size); + + gPacket->m_hasAbsTimestamp = 0; + gPacket->m_packetType = nPacketType; /*˴ΪһƵ,һƵ*/ + gPacket->m_nInfoField2 = m_pRtmp->m_stream_id; + gPacket->m_nChannel = 0x04; + + + gPacket->m_headerType = RTMP_PACKET_SIZE_LARGE; + if (RTMP_PACKET_TYPE_AUDIO == nPacketType && size != 4) + { + gPacket->m_headerType = RTMP_PACKET_SIZE_MEDIUM; + } + + gPacket->m_nTimeStamp = nTimestamp; + /**/ + int nRet = 0; + if (RTMP_IsConnected(m_pRtmp)) + { + nRet = RTMP_SendPacket(m_pRtmp, gPacket, FALSE); /*TRUEΪŽͶ,FALSEDzŽͶ,ֱӷ*/ + } + else { + if (once) { + once = false; + } + } + /*ͷڴ*/ + //free(gPacket); + return nRet; +} + +int RtmpPusher::SendVideoPacket(unsigned int nPacketType, + unsigned char * data, unsigned int size, unsigned int nTimestamp) +{ + + RTMPPacket* packet; + /*ڴͳʼ,lenΪ峤*/ + packet = (RTMPPacket *)malloc(RTMP_HEAD_SIZE + size); + memset(packet, 0, RTMP_HEAD_SIZE); + /*ڴ*/ + packet->m_body = (char *)packet + RTMP_HEAD_SIZE; + packet->m_nBodySize = size; + memcpy(packet->m_body, data, size); + packet->m_hasAbsTimestamp = 0; + packet->m_packetType = nPacketType; /*˴ΪһƵ,һƵ*/ + packet->m_nInfoField2 = m_pRtmp->m_stream_id; + packet->m_nChannel = 0x04; + packet->m_nTimeStamp += 33; + + packet->m_headerType = RTMP_PACKET_SIZE_LARGE; + if (RTMP_PACKET_TYPE_AUDIO == nPacketType && size != 4) + { + packet->m_headerType = RTMP_PACKET_SIZE_MEDIUM; + } + packet->m_nTimeStamp = nTimestamp; + /**/ + int nRet = 0; + if (RTMP_IsConnected(m_pRtmp)) + { + nRet = RTMP_SendPacket(m_pRtmp, packet, TRUE); /*TRUEΪŽͶ,FALSEDzŽͶ,ֱӷ*/ + } + /*ͷڴ*/ + free(packet); + return 0; +} + +RtmpPusher::RtmpPusher() + :mThread(nullptr), + mIfConnected(false) +{ + +} + + +RtmpPusher::~RtmpPusher() +{ + if (m_pRtmp) + { + RTMP_Close(m_pRtmp); + RTMP_Free(m_pRtmp); + m_pRtmp = NULL; + } + CleanupSockets(); +} + +void H264RtmpPuser::OnAudioEncode(const void * frameaddress, uint32_t framelen,uint16_t pts) +{ + uint8_t *pack = (uint8_t*)malloc(framelen); + memcpy(pack, frameaddress, framelen); + + mMux.lock(); + Buffer buf; + buf.buf = (uint8_t *)pack; + buf.len = framelen; + buf.type = PAYLOAD_TYPE_AUDIO; + this->mPack.push(buf); + mMux.unlock(); + this->mAudioPts = pts; +} + +H264RtmpPuser::H264RtmpPuser() +{ + this->metaData.Pps = nullptr; + this->metaData.Sps = nullptr; + this->metaData.nPpsLen = 0; + this->metaData.nSpsLen = 0; + this->mStartTime = 0; + mFirtACC = false; +} + +int H264RtmpPuser::sortAndSendNal(uint8_t * data, int len) +{ + int i = 0; + uint8_t * nalhead = nullptr; + uint8_t * naltail = nullptr; + uint32_t size = 0; + if(0 == mStartTime){ + mStartTime = RTMP_GetTime(); + } + if (nullptr == data) { + return -1; + } + while (i < len) + { + // sps pps p frame + if ((data[i] == 0x00) && (data[i + 1] == 0x00) + && ((data[i + 2] == 0x00) && (data[i + 3] == 0x01) || (data[i + 2] == 0x01))) { + if ((nalhead == nullptr) && (i == 0) ) { + if ((data[i + 3] == 0x01) && (data[i + 4] == 0x41)) { //p ֱ֡ӷ + + nalhead = data; + naltail = data + (len); + size = naltail - nalhead; + this->SendH264Packet(nalhead, size, 0, RTMP_GetTime() - mStartTime); + return 0; + } + //sps ֡н + if ((data[i + 3] == 0x01) && (data[i + 4] == 0x67)) { // sps or pps or sei + nalhead = data; + i += 1; + } + //sei + if ((data[i + 2] == 0x01) && (data[i + 3] == 0x06)) { + i += 1; + } + } + else { + // i frame + if ((data[i + 2] == 0x01) && (data[i + 3] == 0x65)) { + + naltail = data + i; + size = naltail - nalhead; + this->SendH264Packet(nalhead, size, 0, RTMP_GetTime() - mStartTime); + nalhead = data + i; + naltail = data + (len); + size = naltail - nalhead; + this->SendH264Packet(nalhead, size, 0, RTMP_GetTime() - mStartTime); + return 0; + } + //pps + if ((data[i + 3] == 0x01) && (data[i + 4] == 0x68)) { // sps or pps or sei + naltail = data + i; + size = naltail - nalhead; + this->SendH264Packet(nalhead, size, 0, RTMP_GetTime() - mStartTime); + nalhead = data + i; + i += 3; + }//sps + if ((data[i + 3] == 0x01) && (data[i + 4] == 0x67)) { // sps or pps or sei + nalhead = data + i; + i += 3; + } + //sei + if ((data[i + 3] == 0x01) && (data[i + 4] == 0x06)) { // sps or pps or sei + naltail = data + i; + size = naltail - nalhead; + this->SendH264Packet(nalhead, size, 0, RTMP_GetTime() - mStartTime); + nalhead = data + i; + i += 3; + } + // sps pps or sei + } + // 00 00 00 00 01 + } + i++; + } + return 0; +} + + +// Ƶͬϸṹhttps://blog.csdn.net/liwf616/article/details/51596373 +int H264RtmpPuser::SendVideoSpsPps(unsigned char * pps, + int pps_len, unsigned char * sps, + int sps_len,unsigned int nTimeStamp) +{ + RTMPPacket * packet = NULL;//rtmpṹ + unsigned char * body = NULL; + int i; + packet = (RTMPPacket *)malloc(RTMP_HEAD_SIZE + 1024); + //RTMPPacket_Reset(packet);//packet״̬ + memset(packet, 0, RTMP_HEAD_SIZE + 1024); + packet->m_body = (char *)packet + RTMP_HEAD_SIZE; + body = (unsigned char *)packet->m_body; + i = 0; + // FrameType == 1CodecID == 7 + body[i++] = 0x17; + //AVCPacketType + body[i++] = 0x00; + + //CompositionTime + body[i++] = 0x00; + body[i++] = 0x00; + body[i++] = 0x00; + + /*AVCDecoderConfigurationRecord*/ + body[i++] = 0x01; + body[i++] = sps[1]; + body[i++] = sps[2]; + body[i++] = sps[3]; + body[i++] = 0xff; + + /*sps*/ + body[i++] = 0xe1; + body[i++] = (sps_len >> 8) & 0xff; + body[i++] = sps_len & 0xff; + memcpy(&body[i], sps, sps_len); + i += sps_len; + + /*pps*/ + body[i++] = 0x01; + body[i++] = (pps_len >> 8) & 0xff; + body[i++] = (pps_len) & 0xff; + memcpy(&body[i], pps, pps_len); + i += pps_len; + + packet->m_packetType = RTMP_PACKET_TYPE_VIDEO; + packet->m_nBodySize = i; + packet->m_nChannel = 0x04; + packet->m_nTimeStamp = nTimeStamp; + packet->m_hasAbsTimestamp = 0; + packet->m_headerType = RTMP_PACKET_SIZE_MEDIUM; + packet->m_nInfoField2 = m_pRtmp->m_stream_id; + + /*÷ͽӿ*/ + int nRet = RTMP_SendPacket(m_pRtmp, packet, TRUE); + free(packet); //ͷڴ + return nRet; +} + +int H264RtmpPuser::SendAudioData(unsigned char * dat, + unsigned int size, unsigned int nTimeStamp) +{ + return 0; +} + +int H264RtmpPuser::SendH264Packet(unsigned char * data, + unsigned int size, int bIsKeyFrame, unsigned int nTimeStamp) +{ + if(data == NULL){ + return false; + } + unsigned int nal_type = 0; + // С֡ӦPPSSPS + if ((data[0] != 0x00) || (data[1] != 0x00) + || ((data[2] != 0x00)&&data[2]!= 0x01)) { + return false; + } + //Debuger::Debug(L"%02x %02x %02x %02x %02x %02d\r\n", + // data[0],data[1],data[2],data[3],data[4],size); + if (data[2] == 0x01) { + nal_type = data[3]; + } + if (data[3] == 0x01) { + nal_type = data[4]; + } + switch (nal_type) + { + case 0x67: //just update sps and pps + if (NULL == metaData.Sps) + metaData.Sps = (unsigned char *)malloc(size - 4); + h264_decode_sps(data + 4, size - 4, metaData.nWidth, metaData.nHeight, metaData.nFrameRate); + metaData.nSpsLen = size - 4; + memcpy(this->metaData.Sps, data + 4, size - 4); + break; + case 0x68: //just update sps and pps + this->metaData.nPpsLen = size - 4; + if (NULL == metaData.Pps) metaData.Pps = (unsigned char *)malloc(size - 4); + memcpy(this->metaData.Pps, data + 4, size - 4); + break; + case 0x41: //p frame + this->sendDataPackH264(data + 4, size - 4, 0, nTimeStamp); + break; + case 0x65: //i frame + this->sendDataPackH264(data + 3, size - 3, 1, nTimeStamp); + break; + case 0x06: + size = size; + //this->sendDataPack(data + 4, size - 4, 0, nTimeStamp); + break; + default: + break; + } + +} +unsigned char *gBody = nullptr; +int H264RtmpPuser::sendDataPackH264(unsigned char * data, + unsigned int size, int bIsKeyFrame, unsigned int nTimeStamp) +{ + if (gBody == nullptr) { + gBody = new unsigned char[640*720*3 + 9]; + } + if (size < 0) { + gBody = gBody; + } + memset(gBody, 0, size + 9); + int i = 0; + if (1 == bIsKeyFrame) { + gBody[i++] = 0x17;// 1:Iframe 7:AVC + gBody[i++] = 0x01;// AVC NALU + gBody[i++] = 0x00; + gBody[i++] = 0x00; + gBody[i++] = 0x00; + + // NALU size + gBody[i++] = size >> 24 & 0xff; + gBody[i++] = size >> 16 & 0xff; + gBody[i++] = size >> 8 & 0xff; + gBody[i++] = size & 0xff; + // NALU data + memcpy(&gBody[i], data, size); + if(metaData.Sps != nullptr) + SendVideoSpsPps(metaData.Pps, metaData.nPpsLen, metaData.Sps, + metaData.nSpsLen, 0); + } + else { + gBody[i++] = 0x27;// 2:Pframe 7:AVC + gBody[i++] = 0x01;// AVC NALU + gBody[i++] = 0x00; + gBody[i++] = 0x00; + gBody[i++] = 0x00; + // NALU size + gBody[i++] = size >> 24 & 0xff; + gBody[i++] = size >> 16 & 0xff; + gBody[i++] = size >> 8 & 0xff; + gBody[i++] = size & 0xff; + // NALU data + + memcpy(&gBody[i], data, size); + } + int bRet = SendPacket(RTMP_PACKET_TYPE_VIDEO, gBody, i + size, nTimeStamp); + return bRet; +} + + +int H264RtmpPuser::SendAudioSync(int audioType, + int sampleIndex, int channel, unsigned int nTimeStamp) +{ + RTMPPacket * packet = NULL;//rtmpṹ + unsigned char * body = NULL; + int i; + + packet = (RTMPPacket *)malloc(RTMP_HEAD_SIZE + 1024); + //RTMPPacket_Reset(packet);//packet״̬ + memset(packet, 0, RTMP_HEAD_SIZE + 1024); + packet->m_body = (char *)packet + RTMP_HEAD_SIZE; + body = (unsigned char *)packet->m_body; + + body[0] = 0xaf; + body[1] = 0x00; + + uint16_t audioSpecConf = 0; + audioSpecConf |= ((2 << 11) & 0xf800); //2: AACLC + audioSpecConf |= ((4 << 7) & 0x0780); //4: 44khz + audioSpecConf |= ((2 << 3) & 0x78); //4: 2:stero + audioSpecConf |= 0 & 0x07; //4: 0 padding + body[2] = (audioSpecConf >> 8) & 0xff; + body[3] = audioSpecConf & 0xff; + + + packet->m_packetType = RTMP_PACKET_TYPE_AUDIO; + packet->m_nBodySize = 4; + packet->m_nChannel = 0x04; + packet->m_nTimeStamp = nTimeStamp; + packet->m_hasAbsTimestamp = 0; + packet->m_headerType = RTMP_PACKET_SIZE_MEDIUM; + packet->m_nInfoField2 = m_pRtmp->m_stream_id; + + /*÷ͽӿ*/ + int nRet = RTMP_SendPacket(m_pRtmp, packet, TRUE); + free(packet); //ͷڴ + return nRet; +} + + +int H264RtmpPuser::sendDataPackAAC(unsigned char * data, + unsigned int size, unsigned int nTimeStamp) +{ + unsigned char *gBody = nullptr; + static int timestamp = 0; + timestamp += 20; + if (!mFirtACC) { + SendAudioSync(2,4,4, timestamp); + mFirtACC = 1; + } + gBody = (unsigned char*)malloc(size + 2); + gBody[0] = 0xAF; + gBody[1] = 0x01; //aac raw data + memcpy(gBody + 2, data + 7, size - 7); + int bRet = SendPacket(RTMP_PACKET_TYPE_AUDIO, gBody, + size - 7 + 2, timestamp); + free(gBody); + return 0; +} + +void H264RtmpPuser::OnGetCodeFrame(uint8_t * data, int len) +{ + static int timetamp = 0; + timetamp += this->mTick; + uint8_t *pack = (uint8_t*)malloc(len); + memcpy(pack, data, len); + mMux.lock(); + Buffer buf; + buf.buf = pack; + buf.len = len; + buf.type = PAYLOAD_TYPE_VIDEO; + this->mPack.push(buf); + mMux.unlock(); +} + +void H264RtmpPuser::ProcessSend() +{ + while (this->mIfStart) { + int len = mPack.size(); + if (!mPack.empty()) { + mMux.lock(); + Buffer buf = mPack.front(); + mPack.pop(); + mMux.unlock(); + //Ƶ֡ + if (buf.type == PAYLOAD_TYPE_VIDEO) { + this->sortAndSendNal(buf.buf, buf.len); + + }// Ƶ֡ + if (buf.type == PAYLOAD_TYPE_AUDIO) { + this->sendDataPackAAC(buf.buf, buf.len, this->mAudioPts); + } + free(buf.buf); + } + msleep(10); + } +} + +int ThreadEncode(H264RtmpPuser * p) +{ + Debuger::Debug(L"thread started\r\n"); + if (nullptr == p) + return -1; + p->ProcessSend(); + return 0; +} + +int H264RtmpPuser::StartPush() +{ + mIfStart = true; + this->mThread = new std::thread(ThreadEncode,this); + mThreadId = this->mThread->get_id(); + return 0; +} + +int H264RtmpPuser::StopPush() +{ + mIfConnected = false; + mIfStart = false; + if(mThread != nullptr) + this->mThread->join(); + this->RTMP264_Close(); + return 0; +} + + diff --git a/media/RtmpPusher.h b/media/RtmpPusher.h new file mode 100644 index 0000000..48859cd --- /dev/null +++ b/media/RtmpPusher.h @@ -0,0 +1,117 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + + +#include "librtmp_send264.h" +#include "librtmp\rtmp.h" +#include "librtmp\rtmp_sys.h" + + + +#ifdef __cplusplus +} +#endif + +#include "librtmp\amf.h" + +#include "AACAudioCoder.h" +#include "sps_decode.h" +#include "VideoCoder.h" +#include +#include +#include +#include +#include +using namespace std; + +#define RTMP_HEAD_SIZE (sizeof(RTMPPacket)+RTMP_MAX_HEADER_SIZE) + +class RtmpPusher +{ +protected: + RTMP *m_pRtmp; + string mUrl; + int mTick = 10; + std::mutex mMux; + std::thread *mThread; + bool mIfConnected = false; + std::thread::id mThreadId; +public: + bool IfConnect(); + int RTMP264_Connect(const char* url); + void RTMP264_Close(); + int SendPacket(unsigned int nPacketType, unsigned char *data, unsigned int size, unsigned int nTimestamp); + int SendVideoPacket(unsigned int nPacketType, unsigned char *data, unsigned int size, unsigned int nTimestamp); + int SetTick(int tick) { this->mTick = tick; }; + virtual int StartPush() { return 0; }; + RtmpPusher(); + virtual ~RtmpPusher(); +}; + +/** + * _RTMPMetadata + * ڲṹ塣ýṹҪڴ洢ʹԪϢ + */ +typedef struct _RTMPMetadata +{ + // video, must be h264 type + int nWidth; + int nHeight; + int nFrameRate; + unsigned int nSpsLen; + unsigned char *Sps; + unsigned int nPpsLen; + unsigned char *Pps; +} RTMPMetadata, *LPRTMPMetadata; + +enum Payload_Type { + PAYLOAD_TYPE_VIDEO = 0, + PAYLOAD_TYPE_AUDIO = 1 +}; + +typedef struct _T_Buffer { + uint8_t *buf; + int len; + Payload_Type type; +}Buffer; + +class H264RtmpPuser : public RtmpPusher , + public VideoCodeObserver, + public AAC_CODER::AACAudioCoder::EncodeAudioObserver { +private: + bool mFirtACC; + uint16_t mAudioPts; + bool mIfStart = false; + // Ƶͬ + int SendVideoSpsPps(unsigned char *pps, int pps_len, + unsigned char * sps, int sps_len, unsigned int nTimeStamp); + // Ƶͬ + int SendAudioSync(int audioType, int sampleIndex, int channel, unsigned int nTimeStamp); + int SendAudioData(unsigned char*dat, unsigned int size, unsigned int nTimeStamp); + int SendH264Packet(unsigned char *data, + unsigned int size, int bIsKeyFrame, unsigned int nTimeStamp); + int sendDataPackH264(unsigned char *data, + unsigned int size, int bIsKeyFrame, unsigned int nTimeStamp); + int sendDataPackAAC(unsigned char *data, unsigned int size, unsigned int nTimeStamp); + uint32_t mStartTime; +public: + + queue mPack; + RTMPMetadata metaData; + H264RtmpPuser(); + int sortAndSendNal(uint8_t *data, int len); + + int SetSpsPps(unsigned char *pps, int pps_len, + unsigned char * sps, int sps_len); + void OnAudioEncode(const void *frameaddress, uint32_t framelen, uint16_t pts); + void OnGetCodeFrame(uint8_t *data, int len); + + void ProcessSend(); + int StartPush(); + int StopPush(); +}; + +int ThreadEncode(H264RtmpPuser*p); diff --git a/media/SdlPlayer.h b/media/SdlPlayer.h new file mode 100644 index 0000000..43d5c70 --- /dev/null +++ b/media/SdlPlayer.h @@ -0,0 +1,34 @@ +#pragma once +#include +#include "H264Docoder.h" +#include "CameraCapture.h" +extern "C" +{ +#include "libavcodec/avcodec.h" +#include "libavformat/avformat.h" +#include "libavutil/avutil.h" +#include "libswscale/swscale.h" +#include "libavutil/opt.h" +#include "libavutil/imgutils.h" +#include "sdl/SDL.h" +}; + +class SDLPlayser : public H264decoder::H264DecodeObserver , public Camera::CameraObserver{ +public: + SDLPlayser(HWND,int ,int, AVPixelFormat); + ~SDLPlayser(); + int RenderYuv(void *pBuf,uint32_t size, AVPixelFormat pix); + int OnRecieveData(AVFrame *frame); + int OnBuffer(double dblSampleTime, BYTE * pBuffer, long lBufferSize) ; + int OnCameraData(uint8_t *dat, uint32_t size) ; +private: + HWND mWindowWnd; + //SDL--------------------------- + int screen_w = 0, screen_h = 0; + int mInWidth, mInHeight; + SDL_Texture* mTexture; + SDL_Rect sdlRect; + AVPixelFormat mFormat; + SDL_Window *mScreen; + SDL_Renderer *mRender; +}; diff --git a/media/VideoCoder.cpp b/media/VideoCoder.cpp new file mode 100644 index 0000000..243df21 --- /dev/null +++ b/media/VideoCoder.cpp @@ -0,0 +1,280 @@ +#include "VideoCoder.h" +#include "Debuger.h" +FILE *p = nullptr; +int VideoCoder::OnBuffer(double dblSampleTime, BYTE * pBuffer, long lBufferSize) +{ + this->Encode(pBuffer, lBufferSize, AV_PIX_FMT_YUV420P); + return 0; +} + +int VideoCoder::OnCameraData(uint8_t * dat, uint32_t size) +{ + //std::cout<<"captrue data and into coder"<Encode(dat, size, AV_PIX_FMT_YUV420P); + return 0; +} + +int VideoCoder::SetDestPix(uint8_t width, uint8_t height) { + this->mDestHeight = height; + this->mDestWidth = width; + return 0; +} + +VideoCoder::VideoCoder(int width, int height, AVPixelFormat formt): + mObserver(nullptr), + mFrame(nullptr), + mPitureBuffer(nullptr), + mFormatCtx(nullptr), + mOutputFmt(nullptr), + mVideoStream(nullptr), + mCodecCtx(nullptr), + mCodec(nullptr) { + AVCodecID codec_id = AV_CODEC_ID_H264; + mCodec = avcodec_find_encoder(codec_id); + + av_register_all(); + if (nullptr == p) { + p = fopen("shit.h264", "wb"); + } + this->mWidth = width; + this->mHeight = height; + this->mInformat = formt; + if (!mCodec) { + printf("Codec not found\n"); + } + this->mFormatCtx = avformat_alloc_context(); + + //原文链接:https ://blog.csdn.net/leixiaohua1020/article/details/25430425 引用来自雷神的文章,雷神保佑 + this->mOutputFmt = av_guess_format(NULL, "shit.h264", NULL); + this->mFormatCtx->oformat = mOutputFmt; + mCodecCtx = avcodec_alloc_context3(mCodec); + if (!mCodecCtx) { + printf("Could not allocate video codec context\n"); + } + mCodecCtx->bit_rate = 1000; + this->mDestHeight = 480; + this->mDestWidth = 640; + mCodecCtx->width = this->mDestWidth; + mCodecCtx->height = this->mDestHeight; + mCodecCtx->time_base.num = 1; + mCodecCtx->time_base.den = 10; + mCodecCtx->max_b_frames = 0; + mCodecCtx->qmin = 10; + mCodecCtx->qmax = 25; + //mCodecCtx->flags |= AV_CODEC_FLAG_LOW_DELAY; + mCodecCtx->gop_size = 10; + mCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P; + av_opt_set(mCodecCtx->priv_data, "preset", "superfast", 0); + av_opt_set(mCodecCtx->priv_data, "tune", "zerolatency", 0); + if (avcodec_open2(mCodecCtx, mCodec, NULL) < 0) { + printf("Could not open codec\n"); + } + mFrame = av_frame_alloc(); + if (!mFrame) { + printf("Could not allocate video frame\n"); + } + mFrame->format = mCodecCtx->pix_fmt; + mFrame->width = mCodecCtx->width/2; + mFrame->height = mCodecCtx->height/2; + mFrame->pts = 0; + int ret = av_image_alloc(mFrame->data, mFrame->linesize, mCodecCtx->width, mCodecCtx->height, + mCodecCtx->pix_fmt, 8); + if (ret < 0) { + printf("Could not allocate raw picture buffer\n"); + } + + // 让我们假设分辨率都是不可改变的,AvPack可以复用 + avformat_write_header(mFormatCtx, NULL); + int picture_size = avpicture_get_size(AV_PIX_FMT_YUV420P, mCodecCtx->width, mCodecCtx->height); +} + +VideoCoder::~VideoCoder() +{ + fclose(p); +} + +void VideoCoder::Encode(uint8_t * src, int size, enum AVPixelFormat format) { + uint8_t *pFrame[4]; + int lineSize[4]; + static int debugs = 1; + //如果不是yuv420p就转成yuv420p + int iFramesize; + + av_init_packet(&mAVPack); + mAVPack.data = NULL; // packet data will be allocated by the encoder + + int ret = av_image_alloc(pFrame, lineSize, mWidth, mHeight, AV_PIX_FMT_YUV420P, 1); + if (ret< 0) { + Debuger::Debug(L"Could not allocate destination image\n"); + } + + if (this->mInformat != AV_PIX_FMT_YUV420P || (this->mDestHeight != mHeight)) { + int size = avpicture_get_size(this->mInformat,mWidth,mHeight); + this->forceYUV420P(src, size, mInformat, (uint8_t ***)&pFrame,&iFramesize); + //仅仅支持yuv420p + mFrame->data[0] = pFrame[0]; //Y + mFrame->data[1] = pFrame[1]; //U + mFrame->data[2] = pFrame[2]; //V + } + else { + mFrame->data[0] = src; //Y + mFrame->data[1] = src + mWidth*mHeight; //U + mFrame->data[2] = src + mWidth*mHeight + mWidth*mHeight/4; //V + } + //PTS + mFrame->pts++; + int got_picture = 0; + //Encode + avcodec_encode_video2(mCodecCtx, &mAVPack, mFrame, &got_picture); + if (got_picture > 0) { + if(nullptr != this->mObserver) + this->mObserver->OnGetCodeFrame(mAVPack.data, mAVPack.size); + } + //Debuger::Debug(L"Succeed to encode frame: %5d\tsize:%5d\n", 1, mAVPack.size); + fwrite(mAVPack.data, 1, mAVPack.size, p); + fflush(p); + // 刷新coder,防止包挤压 + av_packet_unref(&mAVPack); + av_freep(&pFrame[0]); + free(pFrame[0]); + //av_freep(&mFrame->data[0]); + //av_freep(&mFrame->data[0]); +} + +void VideoCoder::SetOutPutPixel(unsigned int width, unsigned int height) +{ + this->mHeight = height; + this->mWidth = width; +} + +int VideoCoder::flushCoder(AVFormatContext *fmt_ctx, unsigned int stream_index) { + int ret; + int got_frame; + AVPacket enc_pkt; + if (!(this->mFormatCtx->streams[stream_index]->codec->codec->capabilities )) + return 0; + while (1) { + enc_pkt.data = NULL; + enc_pkt.size = 0; + av_init_packet(&enc_pkt); + ret = avcodec_encode_video2(fmt_ctx->streams[stream_index]->codec, &enc_pkt, + NULL, &got_frame); + av_frame_free(NULL); + if (ret < 0) + break; + if (!got_frame) { + ret = 0; + break; + } + Debuger::Debug(L"Flush Encoder: Succeed to encode 1 frame!\tsize:%5d\n", enc_pkt.size); + /* mux encoded frame */ + ret = av_write_frame(fmt_ctx, &enc_pkt); + if (ret < 0) + break; + } + return ret; +} +// 强制把其他个数的数据转换成libav可以认得到的数据 +int VideoCoder::forceYUV420P(uint8_t * src, int size, + AVPixelFormat format,uint8_t **dst[4],int *len) +{ + uint8_t *src_data[4]; + int src_linesize[4]; + uint8_t *dst_data[4]; + int dst_linesize[4]; + struct SwsContext *img_convert_ctx; + int ret = 0; + + if (nullptr == dst || nullptr == len) { + return -2; + } + + int src_bpp = av_get_bits_per_pixel(av_pix_fmt_desc_get(format)); + AVPixelFormat dst_pixfmt = AV_PIX_FMT_YUV420P; + int dst_bpp = av_get_bits_per_pixel(av_pix_fmt_desc_get(dst_pixfmt)); + + ret = av_image_alloc(src_data, src_linesize, mWidth, mHeight, format, 1); + if (ret< 0) { + Debuger::Debug(L"Could not allocate source image\n"); + return -1; + } + ret = av_image_alloc(dst_data, dst_linesize, mDestWidth, mDestHeight, AV_PIX_FMT_YUV420P, 1); + if (ret< 0) { + Debuger::Debug(L"Could not allocate destination image\n"); + return -1; + } + + img_convert_ctx = sws_alloc_context(); + //Show AVOption + //av_opt_show2(img_convert_ctx, stdout, AV_OPT_FLAG_VIDEO_PARAM, 0); + //Set Value + av_opt_set_int(img_convert_ctx, "sws_flags", SWS_BICUBIC | SWS_PRINT_INFO, 0); + av_opt_set_int(img_convert_ctx, "srcw", mWidth, 0); + av_opt_set_int(img_convert_ctx, "srch", mHeight, 0); + av_opt_set_int(img_convert_ctx, "src_format", format, 0); + av_opt_set_int(img_convert_ctx, "src_range", 1, 0); + + av_opt_set_int(img_convert_ctx, "dstw", mDestWidth, 0); + av_opt_set_int(img_convert_ctx, "dsth", mDestHeight, 0); + av_opt_set_int(img_convert_ctx, "dst_format", dst_pixfmt, 0); + av_opt_set_int(img_convert_ctx, "dst_range", 1, 0); + sws_init_context(img_convert_ctx, NULL, NULL); + + // 设置输入 + switch (format) { + case AV_PIX_FMT_GRAY8: { + memcpy(src_data[0], src, mWidth*mHeight); + break; + } + case AV_PIX_FMT_YUV420P: { + memcpy(src_data[0], src, mWidth*mHeight); //Y + memcpy(src_data[1], src + mWidth*mHeight, mWidth*mHeight / 4); //U + memcpy(src_data[2], src + mWidth*mHeight * 5 / 4, mWidth*mHeight / 4); //V + break; + } + case AV_PIX_FMT_YUV422P: { + memcpy(src_data[0], src, mWidth*mHeight); //Y + memcpy(src_data[1], src + mWidth*mHeight, mWidth*mHeight / 2); //U + memcpy(src_data[2], src + mWidth*mHeight * 3 / 2, mWidth*mHeight / 2); //V + break; + } + case AV_PIX_FMT_YUV444P: { + memcpy(src_data[0], src, mWidth*mHeight); //Y + memcpy(src_data[1], src + mWidth*mHeight, mWidth*mHeight); //U + memcpy(src_data[2], src + mWidth*mHeight * 2, mWidth*mHeight); //V + break; + } + case AV_PIX_FMT_YUYV422: { + memcpy(src_data[0], src, mWidth*mHeight * 2); //Packed + break; + } + case AV_PIX_FMT_RGB24: { + memcpy(src_data[0], src, mWidth*mHeight * 3); //Packed + break; + } + case AV_PIX_FMT_RGB32: { + memcpy(src_data[0], src, mWidth*mHeight *4); //Packed + break; + } + default: { + Debuger::Debug(L"Not Support Input Pixel Format.\n"); + break; + } + } + // 转换数据 + ret = sws_scale(img_convert_ctx, src_data, src_linesize, 0, mHeight, dst_data, dst_linesize); + if (ret < 0) { + return ret; + } + memcpy(dst[0], dst_data[0], mDestWidth*mDestHeight); + memcpy(dst[1], dst_data[1], mDestWidth*mDestHeight /4); + memcpy(dst[2], dst_data[2], mDestWidth*mDestHeight /4); + + *len = mDestWidth*mDestHeight + mDestWidth*mDestHeight / 2; + // source此时就不需要了,但是dst要在外面free + av_freep(&src_data[0]); + av_freep(&dst_data[0]); + + sws_freeContext(img_convert_ctx); + return 0; +} diff --git a/media/VideoCoder.h b/media/VideoCoder.h new file mode 100644 index 0000000..4c66b28 --- /dev/null +++ b/media/VideoCoder.h @@ -0,0 +1,70 @@ +#pragma once +#ifdef _WIN32 +#include "Debuger.h" +#include "CameraCapture.h" +//Windows +extern "C" +{ +#include "libavcodec/avcodec.h" +#include "libavformat/avformat.h" +#include "libavutil/avutil.h" +#include "libswscale/swscale.h" +#include "libavutil/opt.h" +#include "libavutil/imgutils.h" +}; +#include +#else +#ifdef __cplusplus +extern "C" +{ +#endif +#include "libavutil/opt.h" +#include "libavcodec/avcodec.h" +#include "libavformat/avformat.h" +#ifdef __cplusplus +}; +#endif +#endif + +class VideoCodeObserver { +public: + virtual void OnGetCodeFrame(uint8_t *data, int len) { + Debuger::Debug(L"get one code %d \r\n", len); + } +}; + +class VideoCoder : public Camera::CameraObserver{ +private: + int mWidth; + int mHeight; + unsigned int mDestWidth; + unsigned int mDestHeight; + int mBytePerPixel; + enum AVPixelFormat mInformat; + AVFormatContext *mFormatCtx; + AVOutputFormat *mOutputFmt; + AVStream *mVideoStream; + AVCodecContext *mCodecCtx; + AVCodec *mCodec; + AVPacket mAVPack; + uint8_t *mPitureBuffer; + AVFrame *mFrame; + VideoCodeObserver *mObserver; + +public: + int OnBuffer(double dblSampleTime, BYTE * pBuffer, long lBufferSize); + int OnCameraData(uint8_t *dat, uint32_t size) ; + int SetDestPix(uint8_t width,uint8_t height); + VideoCoder(int width,int height,AVPixelFormat formt); + ~VideoCoder(); + void Encode(uint8_t*src,int size, enum AVPixelFormat format); + void SetOberver(VideoCodeObserver *p) { + this->mObserver = p; + } + void SetOutPutPixel(unsigned int width,unsigned int height); +private: + int flushCoder(AVFormatContext *fmt_ctx, unsigned int stream_index); + int forceYUV420P(uint8_t *src, int size, enum AVPixelFormat format, uint8_t ***dst,int *s); + +}; + diff --git a/media/audiocaptureff.cpp b/media/audiocaptureff.cpp new file mode 100644 index 0000000..7f8f71a --- /dev/null +++ b/media/audiocaptureff.cpp @@ -0,0 +1,409 @@ +#include "audiocaptureff.h" + +#ifdef __MINGW32__ +std::string WString2String(const std::wstring& ws) +{ + std::string strLocale = setlocale(LC_ALL, ""); + const wchar_t* wchSrc = ws.c_str(); + size_t nDestSize = wcstombs(NULL, wchSrc, 0) + 1; + char *chDest = new char[nDestSize]; + memset(chDest, 0, nDestSize); + wcstombs(chDest, wchSrc, nDestSize); + std::string strResult = chDest; + delete[]chDest; + setlocale(LC_ALL, strLocale.c_str()); + return strResult; +} +#endif + +vector CaptureAudioFfmpeg::EnumSpeakers() +{ + vector ret; + std::vector names; + IEnumMoniker *pEnum = nullptr; + // Create the System Device Enumerator. + ICreateDevEnum *pDevEnum; + HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, nullptr, + CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDevEnum)); + + if (SUCCEEDED(hr)) + { + // Create an enumerator for the category. + hr = pDevEnum->CreateClassEnumerator(CLSID_AudioInputDeviceCategory, &pEnum, 0); + if (hr == S_FALSE) + { + hr = VFW_E_NOT_FOUND; // The category is empty. Treat as an error. + } + pDevEnum->Release(); + } + + if (!SUCCEEDED(hr)) + return ret; + + IMoniker *pMoniker = nullptr; + while (pEnum->Next(1, &pMoniker, nullptr) == S_OK) + { + IPropertyBag *pPropBag; + IBindCtx* bindCtx = nullptr; + LPOLESTR str = nullptr; + VARIANT var; + VariantInit(&var); + + HRESULT hr = pMoniker->BindToStorage(0, 0, IID_PPV_ARGS(&pPropBag)); + if (FAILED(hr)) + { + pMoniker->Release(); + continue; + } + + // Get description or friendly name. + hr = pPropBag->Read(L"Description", &var, 0); + if (FAILED(hr)) + { + hr = pPropBag->Read(L"FriendlyName", &var, 0); + } + if (SUCCEEDED(hr)) + { + names.push_back(var.bstrVal); + CaptureAudioFfmpeg::MICInfo ele; + ele.name = var.bstrVal; + ret.push_back(ele); + VariantClear(&var); + } + + pPropBag->Release(); + pMoniker->Release(); + } + + pEnum->Release(); + + return ret; +} + + +CaptureAudioFfmpeg::CaptureAudioFfmpeg(uint16_t rate, uint8_t channel) +{ + mSampleRate = rate; + mChanel = channel; + +} + + +static char *dup_wchar_to_utf8(wchar_t *w) +{ + char *s = NULL; + int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0); + s = (char *)av_malloc(l); + if (s) + WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0); + return s; +} + +int CaptureAudioFfmpeg::InitCapture(wstring url, uint16_t rate, uint8_t channel) +{ + string fileAudioInput = dup_wchar_to_utf8((wchar_t *)url.c_str()); + AVInputFormat* imft = av_find_input_format("dshow"); + AVDictionary *format_opts = nullptr; + av_dict_set_int(&format_opts, "audio_buffer_size", 20, 0); + if (0 > avformat_open_input(&mInfmt_ctx, fileAudioInput.c_str(), imft, &format_opts)) { + printf("failed input file\n"); + return -1; + } + if (0 > avformat_find_stream_info(mInfmt_ctx, NULL)) { + printf("failed find stream info\n"); + avformat_close_input(&mInfmt_ctx); + return -1; + } + int audio_index = -1; + audio_index = av_find_best_stream(mInfmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0); + if (-1 == audio_index) { + printf("failed find best stream\n"); + avformat_close_input(&mInfmt_ctx); + return -1; + } + //av_dump_format(infmt_ctx, 0, fileAudioInput.c_str(), 1); + //ENDļ + + //򿪽 + static AVCodec* decodec = avcodec_find_decoder(mInfmt_ctx->streams[0]->codec->codec_id); + if (!decodec) { + printf("failed find decoder\n"); + return -1; + } + if (0 > avcodec_open2(mInfmt_ctx->streams[0]->codec, decodec, NULL)) { + printf("failed open decoder\n"); + return -1; + } + //END + //زʼ + initAudioFilters(); + //ENDزʼ + // + static AVCodec* codec = NULL; + //codec = avcodec_find_encoder_by_name("libmp3lame"); + codec = avcodec_find_encoder(AV_CODEC_ID_AAC); + static AVCodecContext* codec_ctx = NULL; + codec_ctx = avcodec_alloc_context3(codec); + // codec_ctx->bit_rate = 64000; + // inputContext->streams[0]->codec + codec_ctx->codec = codec; + codec_ctx->sample_rate = 48000; + codec_ctx->channel_layout = 3; + codec_ctx->channels = 2; + //codec_ctx->frame_size = 1024; + codec_ctx->sample_fmt = AV_SAMPLE_FMT_FLTP; + codec_ctx->codec_tag = 0; + codec_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; + + if (0 > avcodec_open2(codec_ctx, codec, NULL)) { + printf("failed open coder\n"); + avformat_close_input(&mInfmt_ctx); + avcodec_free_context(&codec_ctx); + return -1; + } + //END + //ļ + AVFormatContext* outfmt_ctx = NULL; + if (0 > avformat_alloc_output_context2(&outfmt_ctx, NULL, NULL, "aac.aac")) { + printf("failed alloc outputcontext\n"); + avformat_close_input(&mInfmt_ctx); + avcodec_free_context(&codec_ctx); + return -1; + } + AVStream* out_stream = avformat_new_stream(outfmt_ctx, codec_ctx->codec); + if (!out_stream) { + printf("failed new stream\n"); + avformat_close_input(&mInfmt_ctx); + avcodec_free_context(&codec_ctx); + avformat_close_input(&outfmt_ctx); + return -1; + } + avcodec_copy_context(out_stream->codec, codec_ctx); + // if (0 > avio_open(&outfmt_ctx->pb, "rtmp://localhost/testlive", AVIO_FLAG_WRITE)) { + if (0 > avio_open(&outfmt_ctx->pb, "aac.aac", AVIO_FLAG_WRITE)) { + printf("failed to open outfile\n"); + avformat_close_input(&mInfmt_ctx); + avcodec_free_context(&codec_ctx); + avformat_close_input(&outfmt_ctx); + return -1; + } + avformat_write_header(outfmt_ctx, NULL); + //ENDļ + #if 0 + AVFrame* Frame = av_frame_alloc(); + Frame->nb_samples = codec_ctx->frame_size; + Frame->format = codec_ctx->sample_fmt; + Frame->channel_layout = codec_ctx->channel_layout; + int size = av_samples_get_buffer_size(NULL, codec_ctx->channels, codec_ctx->frame_size, + codec_ctx->sample_fmt, 1); + uint8_t* frame_buf = (uint8_t *)av_malloc(size); + avcodec_fill_audio_frame(Frame, codec_ctx->channels, codec_ctx->sample_fmt, (const uint8_t*)frame_buf, size, 1); + int64_t in_channel_layout = av_get_default_channel_layout(codec_ctx->channels); + AVPacket pkt; + av_new_packet(&pkt, size); + pkt.data = NULL; + int got_frame = -1; + int delayedFrame = 0; + static uint8_t audio_buf[(MAX_AUDIO_FRAME_SIZE * 3) / 2]; + int audioCount = 0; + const uint8_t *indata[AV_NUM_DATA_POINTERS] = { 0 }; + AVFrame* Frame1 = av_frame_alloc(); + #endif + int loop = 1; + int delayedFrame = 0; + AVPacket packet; + av_init_packet(&packet); + packet.data = NULL; + packet.size = 0; + AVPacket pkt; + av_init_packet(&pkt); + pkt.data = NULL; + pkt.size = 0; + + AVFrame* pSrcAudioFrame = av_frame_alloc(); + int got_frame = 0; + + while (1) { + av_read_frame(mInfmt_ctx, &packet); + loop++; + if (packet.stream_index == audio_index) { + auto filterFrame = DecodeAudio(&packet, pSrcAudioFrame); + if (filterFrame) { + avcodec_encode_audio2(codec_ctx, &pkt, filterFrame, &got_frame); + if (got_frame) { + #if 1 + auto streamTimeBase = outfmt_ctx->streams[pkt.stream_index]->time_base.den; + auto codecTimeBase = outfmt_ctx->streams[pkt.stream_index]->codec->time_base.den; + pkt.pts = pkt.dts = (1024 * streamTimeBase * mAudioCount) / codecTimeBase; + mAudioCount++; + auto inputStream = mInfmt_ctx->streams[pkt.stream_index]; + auto outputStream = outfmt_ctx->streams[pkt.stream_index]; + av_packet_rescale_ts(&pkt, inputStream->time_base, outputStream->time_base); + #endif + // pkt.stream_index = out_stream->index; + av_interleaved_write_frame(outfmt_ctx, &pkt); + av_packet_unref(&pkt); + printf("output frame %3d\n", loop - delayedFrame); + } + else { + delayedFrame++; + av_packet_unref(&pkt); + printf("no output frame\n"); + } + } + } + av_packet_unref(&packet); + } + flush_encoder(outfmt_ctx, 0); + av_write_trailer(outfmt_ctx); + //av_free(Frame); + av_free(pSrcAudioFrame); + avio_close(outfmt_ctx->pb); + avformat_close_input(&mInfmt_ctx); + //avformat_close_input(&outfmt_ctx); + return 0; +} + +int CaptureAudioFfmpeg::initAudioFilters() +{ + char args[512]; + int ret; + AVFilter *abuffersrc = (AVFilter *)avfilter_get_by_name("abuffer"); + AVFilter *abuffersink = (AVFilter *)avfilter_get_by_name("abuffersink"); + AVFilterInOut *outputs = avfilter_inout_alloc(); + AVFilterInOut *inputs = avfilter_inout_alloc(); + + auto audioDecoderContext = mInfmt_ctx->streams[0]->codec; + if (!audioDecoderContext->channel_layout) + audioDecoderContext->channel_layout = av_get_default_channel_layout(audioDecoderContext->channels); + + static const enum AVSampleFormat out_sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE }; + static const uint64_t out_channel_layouts[] = { audioDecoderContext->channel_layout}; + static const int out_sample_rates[] = { audioDecoderContext->sample_rate , -1 }; + + AVRational time_base = mInfmt_ctx->streams[0]->time_base; + mFilterGraph = avfilter_graph_alloc(); + mFilterGraph->nb_threads = 1; + + sprintf_s(args, sizeof(args), + "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%I64x", + time_base.num, time_base.den, audioDecoderContext->sample_rate, + av_get_sample_fmt_name(audioDecoderContext->sample_fmt), + audioDecoderContext->channel_layout); + + ret = avfilter_graph_create_filter(&mBuffersrcCtx, abuffersrc, "in", + args, NULL, mFilterGraph); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer source\n"); + return ret; + } + + /* buffer audio sink: to terminate the filter chain. */ + ret = avfilter_graph_create_filter(&mBuffersinkCtx, abuffersink, "out", + NULL, NULL, mFilterGraph); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer sink\n"); + return ret; + } + + ret = av_opt_set_int_list(mBuffersinkCtx, "sample_fmts", out_sample_fmts, -1, + AV_OPT_SEARCH_CHILDREN); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot set output sample format\n"); + return ret; + } + + ret = av_opt_set_int_list(mBuffersinkCtx, "channel_layouts", out_channel_layouts, -1, + AV_OPT_SEARCH_CHILDREN); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot set output channel layout\n"); + return ret; + } + + ret = av_opt_set_int_list(mBuffersinkCtx, "sample_rates", out_sample_rates, -1, + AV_OPT_SEARCH_CHILDREN); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot set output sample rate\n"); + return ret; + } + + /* Endpoints for the filter graph. */ + outputs->name = av_strdup("in"); + outputs->filter_ctx = mBuffersrcCtx;; + outputs->pad_idx = 0; + outputs->next = NULL; + + inputs->name = av_strdup("out"); + inputs->filter_ctx = mBuffersinkCtx; + inputs->pad_idx = 0; + inputs->next = NULL; + + if ((ret = avfilter_graph_parse_ptr(mFilterGraph, "anull", + &inputs, &outputs, nullptr)) < 0) + return ret; + + if ((ret = avfilter_graph_config(mFilterGraph, NULL)) < 0) + return ret; + + av_buffersink_set_frame_size(mBuffersinkCtx, 1024); + return 0; +} + +int CaptureAudioFfmpeg::flush_encoder(AVFormatContext *fmt_ctx, unsigned int stream_index) +{ + int ret; + int got_frame; + AVPacket enc_pkt; + if (!(fmt_ctx->streams[stream_index]->codec->codec->capabilities & + 0x0020)) + return 0; + while (1) { + enc_pkt.data = NULL; + enc_pkt.size = 0; + av_init_packet(&enc_pkt); + ret = avcodec_encode_audio2(fmt_ctx->streams[stream_index]->codec, &enc_pkt, + NULL, &got_frame); + av_frame_free(NULL); + if (ret < 0) + break; + if (!got_frame) { + ret = 0; + break; + } + printf("Flush Encoder: Succeed to encode 1 frame!\tsize:%5d\n", enc_pkt.size); + /* mux encoded frame */ + ret = av_write_frame(fmt_ctx, &enc_pkt); + if (ret < 0) + break; + } + return ret; + + +} + +AVFrame *CaptureAudioFfmpeg::DecodeAudio(AVPacket *packet, AVFrame *pSrcAudioFrame) +{ + AVStream * stream = mInfmt_ctx->streams[0]; + AVCodecContext* codecContext = stream->codec; + int gotFrame; + AVFrame *filtFrame = nullptr; + auto length = avcodec_decode_audio4(codecContext, pSrcAudioFrame, &gotFrame, packet); + if (length >= 0 && gotFrame != 0) + { + if (av_buffersrc_add_frame_flags(mBuffersrcCtx, pSrcAudioFrame, AV_BUFFERSRC_FLAG_PUSH) < 0) { + av_log(NULL, AV_LOG_ERROR, "buffe src add frame error!\n"); + return nullptr; + } + + filtFrame = av_frame_alloc(); + int ret = av_buffersink_get_frame_flags(mBuffersinkCtx, filtFrame, AV_BUFFERSINK_FLAG_NO_REQUEST); + if (ret < 0) + { + av_frame_free(&filtFrame); + goto error; + } + return filtFrame; + } + error: + return nullptr; +} diff --git a/media/audiocaptureff.h b/media/audiocaptureff.h new file mode 100644 index 0000000..15276ac --- /dev/null +++ b/media/audiocaptureff.h @@ -0,0 +1,85 @@ +#ifndef AUDIOCAPTUREFF_H +#define AUDIOCAPTUREFF_H + +#include "stdint.h" +#include "../third/portaudio/portaudio.h" +#include +#include +//Windows +extern "C" +{ +#include "libavcodec/avcodec.h" +#include "libavformat/avformat.h" +#include "libavutil/avutil.h" +#include "libswscale/swscale.h" +#include "libavutil/opt.h" +#include "libavutil/imgutils.h" +#include "libavdevice/avdevice.h" +#include "libavfilter/avfilter.h" +#include "libavfilter/buffersrc.h" +#include "libavfilter/buffersink.h" +}; +#include +#include +#include +#include "qedit.h" +#include +#include +#include +#include "guiddef.h" +using namespace std; + + +class CaptureAudioFfmpeg { +public: + class CaptureAudioObserver { + public: + virtual void OnAudioData(const void *frameaddress, uint32_t framelen) {}; + }; + typedef struct _T_MicInfo + { + wstring name; + int index; + }MICInfo; + enum CAP_STATUS { + RUNNING = 1, + STOP = 2, + PAUSE = 3, + FAIL = 4, + }; + + vector EnumSpeakers(); + CaptureAudioFfmpeg(uint16_t rate, uint8_t channel); + int InitCapture(wstring url,uint16_t rate,uint8_t channel); + + /* + ~CaptureAudio(); + int StartCapture(); + void StopCapture(); + int SetObserver(CaptureAudioObserver*); + int OnCallBack(const void* input, void* output, unsigned long frameCount); + void AddCnt(unsigned int x) {this->mSize += x;}; + */ +private: + std::thread mThread; + uint16_t mSampleRate; //采样率 + uint16_t mChanel; //通道号 + uint16_t mSamplefmt; + unsigned long mSize; + CAP_STATUS mStatus; + CaptureAudioObserver *observer; + int initAudioFilters(); + AVFormatContext *mInfmt_ctx = nullptr; + AVFormatContext * mOutfmt_ctx = nullptr; + int64_t mLastReadPacktTime; + AVFilterContext *mBuffersinkCtx = nullptr; + AVFilterContext *mBuffersrcCtx = nullptr; + AVFilterGraph *mFilterGraph = nullptr; + AVCodecContext* mOutPutAudioEncContext = nullptr; + int64_t mAudioCount = 0; + + int flush_encoder(AVFormatContext *fmt_ctx, unsigned int stream_index); + AVFrame *DecodeAudio(AVPacket* packet, AVFrame*pSrcAudioFrame); +}; + +#endif // AUDIOCAPTUREFF_H diff --git a/media/imgutil.cpp b/media/imgutil.cpp new file mode 100644 index 0000000..5585c76 --- /dev/null +++ b/media/imgutil.cpp @@ -0,0 +1,177 @@ +#include +#include +#include +#include +#include + +#define MAX_LEN (1*1024*1024) +#define POSITIVE_HEIGHT (1) + +/*12Bytes*/ +typedef struct /**** BMP file header structure ****/ +{ + unsigned int bfSize; /* Size of file */ + unsigned short bfReserved1; /* Reserved */ + unsigned short bfReserved2; /* ... */ + unsigned int bfOffBits; /* Offset to bitmap data */ +}BITMAPFILEHEADER; + +/*40Bytes*/ +typedef struct /**** BMP file info structure ****/ +{ + unsigned int biSize; /* Size of info header */ + int biWidth; /* Width of image */ + int biHeight; /* Height of image */ + unsigned short biPlanes; /* Number of color planes */ + unsigned short biBitCount; /* Number of bits per pixel */ + unsigned int biCompression; /* Type of compression to use */ + unsigned int biSizeImage; /* Size of image data */ + int biXPelsPerMeter; /* X pixels per meter */ + int biYPelsPerMeter; /* Y pixels per meter */ + unsigned int biClrUsed; /* Number of colors used */ + unsigned int biClrImportant; /* Number of important colors */ +}BITMAPINFOHEADER; + +int simplest_rgb24_to_bmp(const char* rgb24Path, int w, int h, const char* bmpPath) +{ + int s32Ret = 0; + int fd_ori = -1; + int fd_bmp = -1; + int headerSize = 0; + int i = 0;//for circle + int j = 0;//for circle + unsigned char temp = 0; + + unsigned char readBuff[MAX_LEN] = {'\0'}; + memset(readBuff, 0, sizeof(readBuff)); + +#ifdef POSITIVE_HEIGHT + unsigned char readBuff4Ph[MAX_LEN] = {'\0'}; + memset(readBuff4Ph, 0, sizeof(readBuff4Ph)); +#endif + + char bfType[2] = {'B', 'M'}; + + BITMAPFILEHEADER myHead; + BITMAPINFOHEADER myHeadInfo; + memset(&myHead, 0, sizeof(myHead)); + memset(&myHeadInfo, 0, sizeof(myHeadInfo)); + printf("sizeof(myHead) = %d\n", sizeof(myHead)); + printf("sizeof(myHeadInfo) = %d\n", sizeof(myHeadInfo)); + + /*myHead*/ + headerSize = sizeof(bfType) + sizeof(myHead) + sizeof(myHeadInfo); + myHead.bfSize = headerSize + w*h*3; + myHead.bfOffBits = headerSize; + + /*myHeadInfo*/ + myHeadInfo.biSize = sizeof(myHeadInfo); + myHeadInfo.biWidth = w; + +#ifndef POSITIVE_HEIGHT + myHeadInfo.biHeight = -1 * h; +#else + myHeadInfo.biHeight = h; +#endif + + myHeadInfo.biPlanes = 1; + myHeadInfo.biBitCount = 24; + myHeadInfo.biSizeImage = w*h*3; + + /*open files*/ + fd_ori = open(rgb24Path, O_RDONLY); + if(fd_ori < 0) + { + printf("open rgb24 failed!\n"); + return -1; + } + printf("open rgb24 success!\n"); + + fd_bmp = open(bmpPath, O_WRONLY|O_CREAT|O_TRUNC|O_APPEND, 777); + if(fd_bmp < 0) + { + printf("open bmp failed!\n"); + close(fd_ori); + return -1; + } + printf("open bmp success!\n"); + + /*read*/ + memset(readBuff, 0, sizeof(readBuff)); + s32Ret = read(fd_ori, readBuff, sizeof(readBuff)); + if((s32Ret < 0) || (s32Ret != w*h*3)) + { + printf("read RGB file failed!\n"); + close(fd_bmp); + close(fd_ori); + return -1; + } + printf("read RGB file success!\n"); + + /*change R-G-B to B-G-R*/ + for(i = 0; i < (w*h); i++) + { + temp = *(readBuff + i*3); + *(readBuff + i*3) = *(readBuff + i*3 + 2); + *(readBuff + i*3 + 2) = temp; + } + +/*positive height storage sequence:left-right down-up*/ +#ifdef POSITIVE_HEIGHT + for(i = (h - 1), j = 0; i >= 0; i--, j++) + { + memcpy(readBuff4Ph + j*w*3, readBuff + i*w*3, w*3); + } +#endif + + /*write-4 parts*/ + s32Ret = write(fd_bmp, bfType, sizeof(bfType)); + if(s32Ret < 0) + { + printf("write bfType failed!\n"); + close(fd_bmp); + close(fd_ori); + return -1; + } + s32Ret = write(fd_bmp, &myHead, sizeof(myHead)); + if(s32Ret < 0) + { + printf("write myHead failed!\n"); + close(fd_bmp); + close(fd_ori); + return -1; + } + s32Ret = write(fd_bmp, &myHeadInfo, sizeof(myHeadInfo)); + if(s32Ret < 0) + { + printf("write myHeadInfo failed!\n"); + close(fd_bmp); + close(fd_ori); + return -1; + } +#ifdef POSITIVE_HEIGHT + s32Ret = write(fd_bmp, readBuff4Ph, w*h*3); + if(s32Ret < 0) + { + printf("write readBuff4Ph failed!\n"); + close(fd_bmp); + close(fd_ori); + return -1; + } + printf("write readBuff4Ph success!\n"); +#else + s32Ret = write(fd_bmp, readBuff, w*h*3); + if(s32Ret < 0) + { + printf("write readBuff failed!\n"); + close(fd_bmp); + close(fd_ori); + return -1; + } + printf("write readBuff success!\n"); +#endif + + close(fd_bmp); + close(fd_ori); + return 0; +} diff --git a/media/librtmp_send264.h b/media/librtmp_send264.h new file mode 100644 index 0000000..daec69b --- /dev/null +++ b/media/librtmp_send264.h @@ -0,0 +1,41 @@ +/** + * Simplest Librtmp Send 264 + * + * 裬 + * leixiaohua1020@126.com + * zhanghuicuc@gmail.com + * йýѧ/ֵӼ + * Communication University of China / Digital TV Technology + * http://blog.csdn.net/leixiaohua1020 + * + * ڽڴеH.264RTMPý + * + */ + +/** + * ʼӵ + * + * @param url ϶Ӧwebappĵַ + * + * @ɹ򷵻1 , ʧ򷵻0 + */ +int RTMP264_Connect(const char* url); + +/** + * ڴеһH.264ƵRTMPЭ鷢͵ + * + * @param read_buffer صݲʱϵͳԶøúȡݡ + * 2ܣ + * uint8_t *bufⲿõַ + * int buf_sizeⲿݴС + * ֵɹȡڴС + * @ɹ򷵻1 , ʧ򷵻0 + */ +int RTMP264_Send(int (*read_buffer)(unsigned char *buf, int buf_size)); + +/** + * ϿӣͷصԴ + * + */ +void RTMP264_Close(); + diff --git a/media/screen_capture.cpp b/media/screen_capture.cpp new file mode 100644 index 0000000..3cbcaaa --- /dev/null +++ b/media/screen_capture.cpp @@ -0,0 +1,178 @@ +#include "screen_capture.h" + +#include +#include +#include +#include + +#if _MSC_VER >= 1600 +#pragma execution_character_set("utf-8") +#endif + + +#define WIDEN2(x) L ## x +#define WIDEN(x) WIDEN2(x) +#define __WFILE__ WIDEN(__FILE__) +#define HRCHECK(__expr) {hr=(__expr);if(FAILED(hr)){wprintf(L"FAILURE 0x%08X (%i)\n\tline: %u file: '%s'\n\texpr: '" WIDEN(#__expr) L"'\n",hr, hr, __LINE__,__WFILE__);goto cleanup;}} +#define RELEASE(__p) {if(__p!=nullptr){__p->Release();__p=nullptr;}} + +// ȱenum +// https://www.gamedev.net/forums/topic/132636-enum-displaymode--dx9--false/ + +HRESULT SavePixelsToFile32bppPBGRA(UINT width, UINT height, UINT stride, + LPBYTE pixels, LPWSTR filePath, const GUID &format) +{ + if (!filePath || !pixels) + return E_INVALIDARG; + + HRESULT hr = S_OK; + IWICImagingFactory *factory = nullptr; + IWICBitmapEncoder *encoder = nullptr; + IWICBitmapFrameEncode *frame = nullptr; + IWICStream *stream = nullptr; + GUID pf = GUID_WICPixelFormat32bppPBGRA; + BOOL coInit = CoInitialize(nullptr); + + HRCHECK(CoCreateInstance(CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&factory))); + HRCHECK(factory->CreateStream(&stream)); + HRCHECK(stream->InitializeFromFilename(filePath, GENERIC_WRITE)); + HRCHECK(factory->CreateEncoder(format, nullptr, &encoder)); + HRCHECK(encoder->Initialize(stream, WICBitmapEncoderNoCache)); + HRCHECK(encoder->CreateNewFrame(&frame, nullptr)); // we don't use options here + HRCHECK(frame->Initialize(nullptr)); // we dont' use any options here + HRCHECK(frame->SetSize(width, height)); + HRCHECK(frame->SetPixelFormat(&pf)); + HRCHECK(frame->WritePixels(height, stride, stride * height, pixels)); + HRCHECK(frame->Commit()); + HRCHECK(encoder->Commit()); + + cleanup: + RELEASE(stream); + RELEASE(frame); + RELEASE(encoder); + RELEASE(factory); + if (coInit) CoUninitialize(); + return hr; +} + +HRESULT Direct3D9TakeScreenshots(UINT adapter, UINT count) +{ + HRESULT hr = S_OK; + IDirect3D9 *d3d = nullptr; + IDirect3DDevice9 *device = nullptr; + IDirect3DSurface9 *surface = nullptr; + D3DPRESENT_PARAMETERS parameters = { 0 }; + D3DDISPLAYMODE mode; + D3DLOCKED_RECT rc; + UINT pitch; + SYSTEMTIME st; + LPBYTE *shots = nullptr; + + // init D3D and get screen size + d3d = Direct3DCreate9(D3D_SDK_VERSION); + HRCHECK(d3d->GetAdapterDisplayMode(adapter, &mode)); + + parameters.Windowed = TRUE; + parameters.BackBufferCount = 1; + parameters.BackBufferHeight = mode.Height; + parameters.BackBufferWidth = mode.Width; + parameters.SwapEffect = D3DSWAPEFFECT_DISCARD; + parameters.hDeviceWindow = NULL; + + // create device & capture surface + HRCHECK(d3d->CreateDevice(adapter, D3DDEVTYPE_HAL, NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, ¶meters, &device)); + HRCHECK(device->CreateOffscreenPlainSurface(mode.Width, mode.Height, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &surface, nullptr)); + + // compute the required buffer size + HRCHECK(surface->LockRect(&rc, NULL, 0)); + pitch = rc.Pitch; + HRCHECK(surface->UnlockRect()); + + // allocate screenshots buffers + shots = new LPBYTE[count]; + for (UINT i = 0; i < count; i++) + { + shots[i] = new BYTE[pitch * mode.Height]; + } + + GetSystemTime(&st); // measure the time we spend doing captures + wprintf(L"%i:%i:%i.%i\n", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); + for (UINT i = 0; i < count; i++) + { + // get the data + HRCHECK(device->GetFrontBufferData(0, surface)); + + // copy it into our buffers + HRCHECK(surface->LockRect(&rc, NULL, 0)); + CopyMemory(shots[i], rc.pBits, rc.Pitch * mode.Height); + HRCHECK(surface->UnlockRect()); + } + GetSystemTime(&st); + wprintf(L"%i:%i:%i.%i\n", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); + + // save all screenshots + for (UINT i = 0; i < count; i++) + { + WCHAR file[100]; + wsprintf(file, L"cap%i.png", i); + HRCHECK(SavePixelsToFile32bppPBGRA(mode.Width, mode.Height, pitch, shots[i], file, GUID_ContainerFormatPng)); + } + +cleanup: + if (shots != nullptr) + { + for (UINT i = 0; i < count; i++) + { + delete shots[i]; + } + delete[] shots; + } + RELEASE(surface); + RELEASE(device); + RELEASE(d3d); + return hr; +} + +ScreenCapture::ScreenCapture() +{ + m_d3d9_dev = ::Direct3DCreate9(D3D_SDK_VERSION); + +} + +BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor,HDC hdcMonitor, + LPRECT lprcMonitor,LPARAM dwData) +{ + MONITORINFOEX mi; + mi.cbSize=sizeof(MONITORINFOEX); + GetMonitorInfo(hMonitor,&mi); + qDebug()<GetAdapterCount(); + for(DWORD i = 0; i < dwDisplayCount; i++) + { + if( m_d3d9_dev->GetAdapterIdentifier( i/*D3DADAPTER_DEFAULT*/, 0,&adapterID ) != D3D_OK ) + { + return; + } + qDebug()< +#include // we use WIC for saving images +#include // DirectX 9 header +#include + + +HRESULT Direct3D9TakeScreenshots(UINT adapter, UINT count); + + +class ScreenCapture +{ +public: + ScreenCapture(); + void EnumScreen(); + void PrintDisplayModeInfo(IDirect3D9 *pD3D, D3DFORMAT fmt); +private: + IDirect3D9* m_d3d9_dev = nullptr; +}; + +#endif // SCREEN_CAPTURE_H + diff --git a/media/sps_decode.cpp b/media/sps_decode.cpp new file mode 100644 index 0000000..a776999 --- /dev/null +++ b/media/sps_decode.cpp @@ -0,0 +1,247 @@ +/** + * Simplest Librtmp Send 264 + * + * 裬 + * leixiaohua1020@126.com + * zhanghuicuc@gmail.com + * йýѧ/ֵӼ + * Communication University of China / Digital TV Technology + * http://blog.csdn.net/leixiaohua1020 + * + * ڽڴеH.264RTMPý + * + */ +#include "sps_decode.h" + + +typedef unsigned int UINT; +typedef unsigned char BYTE; +typedef unsigned long DWORD; + +UINT Ue(BYTE *pBuff, UINT nLen, UINT &nStartBit) +{ + //0bitĸ + UINT nZeroNum = 0; + while (nStartBit < nLen * 8) + { + if (pBuff[nStartBit / 8] & (0x80 >> (nStartBit % 8))) //&:λ룬%ȡ + { + break; + } + nZeroNum++; + nStartBit++; + } + nStartBit ++; + + + // + DWORD dwRet = 0; + for (UINT i=0; i> (nStartBit % 8))) + { + dwRet += 1; + } + nStartBit++; + } + return (1 << nZeroNum) - 1 + dwRet; +} + + +int Se(BYTE *pBuff, UINT nLen, UINT &nStartBit) +{ + int UeVal=Ue(pBuff,nLen,nStartBit); + double k=UeVal; + int nValue=ceil(k/2); + //ceilceilСڸʵСceil(2)=ceil(1.2)=cei(1.5)=2.00 + if (UeVal % 2==0) + nValue=-nValue; + return nValue; +} + + +DWORD u(UINT BitCount,BYTE * buf,UINT &nStartBit) +{ + DWORD dwRet = 0; + for (UINT i=0; i> (nStartBit % 8))) + { + dwRet += 1; + } + nStartBit++; + } + return dwRet; +} + +/** + * H264NALʼ + * + * @param buf SPS + * + * @޷ֵ + */ +void de_emulation_prevention(BYTE* buf,unsigned int* buf_size) +{ + int i=0,j=0; + BYTE* tmp_ptr = nullptr; + unsigned int tmp_buf_size=0; + int val=0; + + tmp_ptr=buf; + tmp_buf_size=*buf_size; + for(i=0;i<(tmp_buf_size-2);i++) + { + //check for 0x000003 + val=(tmp_ptr[i]^0x00) +(tmp_ptr[i+1]^0x00)+(tmp_ptr[i+2]^0x03); + if(val==0) + { + //kick out 0x03 + for(j=i+2;j>7; + int constraint_set1_flag=u(1,buf,StartBit);//(buf[1] & 0x40)>>6; + int constraint_set2_flag=u(1,buf,StartBit);//(buf[1] & 0x20)>>5; + int constraint_set3_flag=u(1,buf,StartBit);//(buf[1] & 0x10)>>4; + int reserved_zero_4bits=u(4,buf,StartBit); + int level_idc=u(8,buf,StartBit); + + int seq_parameter_set_id=Ue(buf,nLen,StartBit); + + if( profile_idc == 100 || profile_idc == 110 || + profile_idc == 122 || profile_idc == 144 ) + { + int chroma_format_idc=Ue(buf,nLen,StartBit); + if( chroma_format_idc == 3 ) + int residual_colour_transform_flag=u(1,buf,StartBit); + int bit_depth_luma_minus8=Ue(buf,nLen,StartBit); + int bit_depth_chroma_minus8=Ue(buf,nLen,StartBit); + int qpprime_y_zero_transform_bypass_flag=u(1,buf,StartBit); + int seq_scaling_matrix_present_flag=u(1,buf,StartBit); + + int seq_scaling_list_present_flag[8]; + if( seq_scaling_matrix_present_flag ) + { + for( int i = 0; i < 8; i++ ) { + seq_scaling_list_present_flag[i]=u(1,buf,StartBit); + } + } + } + int log2_max_frame_num_minus4=Ue(buf,nLen,StartBit); + int pic_order_cnt_type=Ue(buf,nLen,StartBit); + if( pic_order_cnt_type == 0 ) + int log2_max_pic_order_cnt_lsb_minus4=Ue(buf,nLen,StartBit); + else if( pic_order_cnt_type == 1 ) + { + int delta_pic_order_always_zero_flag=u(1,buf,StartBit); + int offset_for_non_ref_pic=Se(buf,nLen,StartBit); + int offset_for_top_to_bottom_field=Se(buf,nLen,StartBit); + int num_ref_frames_in_pic_order_cnt_cycle=Ue(buf,nLen,StartBit); + + int *offset_for_ref_frame=new int[num_ref_frames_in_pic_order_cnt_cycle]; + for( int i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++ ) + offset_for_ref_frame[i]=Se(buf,nLen,StartBit); + delete [] offset_for_ref_frame; + } + int num_ref_frames=Ue(buf,nLen,StartBit); + int gaps_in_frame_num_value_allowed_flag=u(1,buf,StartBit); + int pic_width_in_mbs_minus1=Ue(buf,nLen,StartBit); + int pic_height_in_map_units_minus1=Ue(buf,nLen,StartBit); + + width=(pic_width_in_mbs_minus1+1)*16; + height=(pic_height_in_map_units_minus1+1)*16; + + int frame_mbs_only_flag=u(1,buf,StartBit); + if(!frame_mbs_only_flag) + int mb_adaptive_frame_field_flag=u(1,buf,StartBit); + + int direct_8x8_inference_flag=u(1,buf,StartBit); + int frame_cropping_flag=u(1,buf,StartBit); + if(frame_cropping_flag) + { + int frame_crop_left_offset=Ue(buf,nLen,StartBit); + int frame_crop_right_offset=Ue(buf,nLen,StartBit); + int frame_crop_top_offset=Ue(buf,nLen,StartBit); + int frame_crop_bottom_offset=Ue(buf,nLen,StartBit); + } + int vui_parameter_present_flag=u(1,buf,StartBit); + if(vui_parameter_present_flag) + { + int aspect_ratio_info_present_flag=u(1,buf,StartBit); + if(aspect_ratio_info_present_flag) + { + int aspect_ratio_idc=u(8,buf,StartBit); + if(aspect_ratio_idc==255) + { + int sar_width=u(16,buf,StartBit); + int sar_height=u(16,buf,StartBit); + } + } + int overscan_info_present_flag=u(1,buf,StartBit); + if(overscan_info_present_flag) + int overscan_appropriate_flagu=u(1,buf,StartBit); + int video_signal_type_present_flag=u(1,buf,StartBit); + if(video_signal_type_present_flag) + { + int video_format=u(3,buf,StartBit); + int video_full_range_flag=u(1,buf,StartBit); + int colour_description_present_flag=u(1,buf,StartBit); + if(colour_description_present_flag) + { + int colour_primaries=u(8,buf,StartBit); + int transfer_characteristics=u(8,buf,StartBit); + int matrix_coefficients=u(8,buf,StartBit); + } + } + int chroma_loc_info_present_flag=u(1,buf,StartBit); + if(chroma_loc_info_present_flag) + { + int chroma_sample_loc_type_top_field=Ue(buf,nLen,StartBit); + int chroma_sample_loc_type_bottom_field=Ue(buf,nLen,StartBit); + } + int timing_info_present_flag=u(1,buf,StartBit); + if(timing_info_present_flag) + { + int num_units_in_tick=u(32,buf,StartBit); + int time_scale=u(32,buf,StartBit); + fps=time_scale/(2*num_units_in_tick); + } + } + return true; + } + else + return false; +} diff --git a/media/sps_decode.h b/media/sps_decode.h new file mode 100644 index 0000000..a986b83 --- /dev/null +++ b/media/sps_decode.h @@ -0,0 +1,34 @@ +#ifndef __SPS_DECODE__ +#define __SPS_DECODE__ + +#include +#include +#include +#include +#include + +UINT Ue(BYTE *pBuff, UINT nLen, UINT &nStartBit); + +int Se(BYTE *pBuff, UINT nLen, UINT &nStartBit); +DWORD u(UINT BitCount, BYTE * buf, UINT &nStartBit); +/** + * H264NALʼ + * + * @param buf SPS + * + * @޷ֵ + */ +void de_emulation_prevention(BYTE* buf, unsigned int* buf_size); +/** + * SPS,ȡƵͼϢ + * + * @param buf SPS + * @param nLen SPSݵij + * @param width ͼ + * @param height ͼ߶ + + * @ɹ򷵻1 , ʧ򷵻0 + */ +int h264_decode_sps(BYTE * buf, unsigned int nLen, int &width, int &height, int &fps); + +#endif diff --git a/qedit.h b/qedit.h new file mode 100644 index 0000000..a520288 --- /dev/null +++ b/qedit.h @@ -0,0 +1,66 @@ + +#include +#include + +#pragma comment(lib, "strmiids.lib") + +#ifndef __qedit_h__ +#define __qedit_h__ + +/////////////////////////////////////////////////////////////////////////////////// + +#pragma once + +/////////////////////////////////////////////////////////////////////////////////// + +struct __declspec(uuid("0579154a-2b53-4994-b0d0-e773148eff85")) + ISampleGrabberCB : IUnknown +{ + // + // Raw methods provided by interface + // + + virtual HRESULT __stdcall SampleCB( + double SampleTime, + struct IMediaSample * pSample) = 0; + virtual HRESULT __stdcall BufferCB( + double SampleTime, + unsigned char * pBuffer, + long BufferLen) = 0; +}; + + + + +struct __declspec(uuid("6b652fff-11fe-4fce-92ad-0266b5d7c78f")) + ISampleGrabber : IUnknown +{ + // + // Raw methods provided by interface + // + + virtual HRESULT __stdcall SetOneShot( + long OneShot) = 0; + virtual HRESULT __stdcall SetMediaType( + struct _AMMediaType * pType) = 0; + virtual HRESULT __stdcall GetConnectedMediaType( + struct _AMMediaType * pType) = 0; + virtual HRESULT __stdcall SetBufferSamples( + long BufferThem) = 0; + virtual HRESULT __stdcall GetCurrentBuffer( + /*[in,out]*/ long * pBufferSize, + /*[out]*/ long * pBuffer) = 0; + virtual HRESULT __stdcall GetCurrentSample( + /*[out,retval]*/ struct IMediaSample * * ppSample) = 0; + virtual HRESULT __stdcall SetCallback( + struct ISampleGrabberCB * pCallback, + long WhichMethodToCallback) = 0; +}; + + +static const IID IID_ISampleGrabber = { 0x6B652FFF, 0x11FE, 0x4fce,{ 0x92, 0xAD, 0x02, 0x66, 0xB5, 0xD7, 0xC7, 0x8F } }; +static const IID IID_ISampleGrabberCB = { 0x0579154A, 0x2B53, 0x4994,{ 0xB0, 0xD0, 0xE7, 0x73, 0x14, 0x8E, 0xFF, 0x85 } }; +static const CLSID CLSID_SampleGrabber = { 0xC1F400A0, 0x3F08, 0x11d3,{ 0x9F, 0x0B, 0x00, 0x60, 0x08, 0x03, 0x9E, 0x37 } }; +static const CLSID CLSID_NullRenderer = { 0xC1F400A4, 0x3F08, 0x11d3,{ 0x9F, 0x0B, 0x00, 0x60, 0x08, 0x03, 0x9E, 0x37 } }; + +#endif \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..1dfeace --- /dev/null +++ b/readme.md @@ -0,0 +1,21 @@ +### 基于qt本地PC摄像头采集并264 编码rtmp推流 + +#### 支持平台 +1. windows + +#### 依赖库: +1. 使用librtmp提供的接口实现rtmp推流。 +2. ffmpeg作为视频264编码。 +3. opengl用于本地摄像头的渲染。 +4. directshow的相关接口用于获取本地的摄像头数据。 +5. 界面风格基于!qssWrpter库 https://gitee.com/290198252/qsswraper + + +### 界面: +![image.png](https://www.testingcloud.club/sapi/api/image_download/80463174-3633-11eb-a135-525400dc6cec.png) + +#### 支持的摄像头 + + +#### 发行版 +- win32 \ No newline at end of file diff --git a/ui_mainwindow.h b/ui_mainwindow.h new file mode 100644 index 0000000..edfe545 --- /dev/null +++ b/ui_mainwindow.h @@ -0,0 +1,161 @@ +/******************************************************************************** +** Form generated from reading UI file 'mainwindow.ui' +** +** Created by: Qt User Interface Compiler version 5.14.0 +** +** WARNING! All changes made in this file will be lost when recompiling UI file! +********************************************************************************/ + +#ifndef UI_MAINWINDOW_H +#define UI_MAINWINDOW_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class Ui_MainWindow +{ +public: + QWidget *centralWidget; + QGridLayout *gridLayout; + QVBoxLayout *verticalLayout; + QHBoxLayout *horizontalLayout; + QPushButton *pushButton; + QComboBox *comboBox; + QPushButton *pushButton_3; + QComboBox *comboBox_2; + QLabel *label; + QLineEdit *lineEdit; + QPushButton *pushButton_2; + QSpacerItem *horizontalSpacer_2; + QSpacerItem *verticalSpacer; + + void setupUi(QMainWindow *MainWindow) + { + if (MainWindow->objectName().isEmpty()) + MainWindow->setObjectName(QString::fromUtf8("MainWindow")); + MainWindow->resize(1383, 1116); + QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); + sizePolicy.setHorizontalStretch(0); + sizePolicy.setVerticalStretch(0); + sizePolicy.setHeightForWidth(MainWindow->sizePolicy().hasHeightForWidth()); + MainWindow->setSizePolicy(sizePolicy); + MainWindow->setMinimumSize(QSize(600, 800)); + centralWidget = new QWidget(MainWindow); + centralWidget->setObjectName(QString::fromUtf8("centralWidget")); + QSizePolicy sizePolicy1(QSizePolicy::Expanding, QSizePolicy::Expanding); + sizePolicy1.setHorizontalStretch(0); + sizePolicy1.setVerticalStretch(0); + sizePolicy1.setHeightForWidth(centralWidget->sizePolicy().hasHeightForWidth()); + centralWidget->setSizePolicy(sizePolicy1); + gridLayout = new QGridLayout(centralWidget); + gridLayout->setSpacing(6); + gridLayout->setContentsMargins(11, 11, 11, 11); + gridLayout->setObjectName(QString::fromUtf8("gridLayout")); + verticalLayout = new QVBoxLayout(); + verticalLayout->setSpacing(6); + verticalLayout->setObjectName(QString::fromUtf8("verticalLayout")); + horizontalLayout = new QHBoxLayout(); + horizontalLayout->setSpacing(6); + horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout")); + horizontalLayout->setContentsMargins(2, 2, 2, 1); + pushButton = new QPushButton(centralWidget); + pushButton->setObjectName(QString::fromUtf8("pushButton")); + pushButton->setMinimumSize(QSize(100, 50)); + pushButton->setSizeIncrement(QSize(0, 6)); + pushButton->setBaseSize(QSize(0, 50)); + + horizontalLayout->addWidget(pushButton); + + comboBox = new QComboBox(centralWidget); + comboBox->setObjectName(QString::fromUtf8("comboBox")); + comboBox->setMinimumSize(QSize(200, 35)); + comboBox->setBaseSize(QSize(0, 50)); + + horizontalLayout->addWidget(comboBox); + + pushButton_3 = new QPushButton(centralWidget); + pushButton_3->setObjectName(QString::fromUtf8("pushButton_3")); + pushButton_3->setMinimumSize(QSize(100, 50)); + + horizontalLayout->addWidget(pushButton_3); + + comboBox_2 = new QComboBox(centralWidget); + comboBox_2->setObjectName(QString::fromUtf8("comboBox_2")); + comboBox_2->setMinimumSize(QSize(200, 35)); + + horizontalLayout->addWidget(comboBox_2); + + label = new QLabel(centralWidget); + label->setObjectName(QString::fromUtf8("label")); + + horizontalLayout->addWidget(label); + + lineEdit = new QLineEdit(centralWidget); + lineEdit->setObjectName(QString::fromUtf8("lineEdit")); + lineEdit->setMinimumSize(QSize(300, 30)); + + horizontalLayout->addWidget(lineEdit); + + pushButton_2 = new QPushButton(centralWidget); + pushButton_2->setObjectName(QString::fromUtf8("pushButton_2")); + pushButton_2->setMinimumSize(QSize(60, 50)); + + horizontalLayout->addWidget(pushButton_2); + + horizontalSpacer_2 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); + + horizontalLayout->addItem(horizontalSpacer_2); + + horizontalLayout->setStretch(0, 1); + horizontalLayout->setStretch(1, 2); + horizontalLayout->setStretch(7, 13); + + verticalLayout->addLayout(horizontalLayout); + + verticalSpacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding); + + verticalLayout->addItem(verticalSpacer); + + verticalLayout->setStretch(0, 1); + verticalLayout->setStretch(1, 9); + + gridLayout->addLayout(verticalLayout, 0, 0, 1, 1); + + MainWindow->setCentralWidget(centralWidget); + + retranslateUi(MainWindow); + + QMetaObject::connectSlotsByName(MainWindow); + } // setupUi + + void retranslateUi(QMainWindow *MainWindow) + { + MainWindow->setWindowTitle(QCoreApplication::translate("MainWindow", "MainWindow", nullptr)); + pushButton->setText(QCoreApplication::translate("MainWindow", "\346\211\223\345\274\200\346\221\204\345\203\217\345\244\264", nullptr)); + pushButton_3->setText(QCoreApplication::translate("MainWindow", "\346\211\223\345\274\200\351\272\246\345\205\213\351\243\216", nullptr)); + label->setText(QCoreApplication::translate("MainWindow", "rtmp\346\216\250\346\265\201\345\234\260\345\235\200", nullptr)); + lineEdit->setText(QCoreApplication::translate("MainWindow", "rtmp://127.0.0.1:1935/live/1", nullptr)); + pushButton_2->setText(QCoreApplication::translate("MainWindow", "\346\216\250\346\265\201", nullptr)); + } // retranslateUi + +}; + +namespace Ui { + class MainWindow: public Ui_MainWindow {}; +} // namespace Ui + +QT_END_NAMESPACE + +#endif // UI_MAINWINDOW_H diff --git a/ui_process.h b/ui_process.h new file mode 100644 index 0000000..06347c1 --- /dev/null +++ b/ui_process.h @@ -0,0 +1,63 @@ +/******************************************************************************** +** Form generated from reading UI file 'process.ui' +** +** Created by: Qt User Interface Compiler version 5.14.0 +** +** WARNING! All changes made in this file will be lost when recompiling UI file! +********************************************************************************/ + +#ifndef UI_PROCESS_H +#define UI_PROCESS_H + +#include +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class Ui_Process +{ +public: + QProgressBar *progressBar; + QLabel *label; + + void setupUi(QDialog *Process) + { + if (Process->objectName().isEmpty()) + Process->setObjectName(QString::fromUtf8("Process")); + Process->resize(324, 88); + QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); + sizePolicy.setHorizontalStretch(0); + sizePolicy.setVerticalStretch(0); + sizePolicy.setHeightForWidth(Process->sizePolicy().hasHeightForWidth()); + Process->setSizePolicy(sizePolicy); + progressBar = new QProgressBar(Process); + progressBar->setObjectName(QString::fromUtf8("progressBar")); + progressBar->setGeometry(QRect(30, 50, 281, 31)); + progressBar->setValue(24); + label = new QLabel(Process); + label->setObjectName(QString::fromUtf8("label")); + label->setGeometry(QRect(120, 30, 121, 16)); + + retranslateUi(Process); + + QMetaObject::connectSlotsByName(Process); + } // setupUi + + void retranslateUi(QDialog *Process) + { + Process->setWindowTitle(QCoreApplication::translate("Process", "Dialog", nullptr)); + label->setText(QCoreApplication::translate("Process", "\346\255\243\345\234\250\345\212\240\350\275\275\346\225\260\346\215\256", nullptr)); + } // retranslateUi + +}; + +namespace Ui { + class Process: public Ui_Process {}; +} // namespace Ui + +QT_END_NAMESPACE + +#endif // UI_PROCESS_H diff --git a/ui_qsstoast.h b/ui_qsstoast.h new file mode 100644 index 0000000..79a6455 --- /dev/null +++ b/ui_qsstoast.h @@ -0,0 +1,55 @@ +/******************************************************************************** +** Form generated from reading UI file 'qsstoast.ui' +** +** Created by: Qt User Interface Compiler version 5.14.0 +** +** WARNING! All changes made in this file will be lost when recompiling UI file! +********************************************************************************/ + +#ifndef UI_QSSTOAST_H +#define UI_QSSTOAST_H + +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class Ui_Toast +{ +public: + QLabel *label; + + void setupUi(QWidget *Toast) + { + if (Toast->objectName().isEmpty()) + Toast->setObjectName(QString::fromUtf8("Toast")); + Toast->resize(932, 59); + QFont font; + font.setFamily(QString::fromUtf8("Arial")); + Toast->setFont(font); + label = new QLabel(Toast); + label->setObjectName(QString::fromUtf8("label")); + label->setGeometry(QRect(170, 10, 231, 31)); + + retranslateUi(Toast); + + QMetaObject::connectSlotsByName(Toast); + } // setupUi + + void retranslateUi(QWidget *Toast) + { + Toast->setWindowTitle(QCoreApplication::translate("Toast", "Form", nullptr)); + label->setText(QCoreApplication::translate("Toast", "TextLabel", nullptr)); + } // retranslateUi + +}; + +namespace Ui { + class Toast: public Ui_Toast {}; +} // namespace Ui + +QT_END_NAMESPACE + +#endif // UI_QSSTOAST_H diff --git a/ui_toast.h b/ui_toast.h new file mode 100644 index 0000000..4b59238 --- /dev/null +++ b/ui_toast.h @@ -0,0 +1,52 @@ +/******************************************************************************** +** Form generated from reading UI file 'toast.ui' +** +** Created by: Qt User Interface Compiler version 5.14.0 +** +** WARNING! All changes made in this file will be lost when recompiling UI file! +********************************************************************************/ + +#ifndef UI_TOAST_H +#define UI_TOAST_H + +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class Ui_Form +{ +public: + QLabel *label; + + void setupUi(QWidget *Form) + { + if (Form->objectName().isEmpty()) + Form->setObjectName(QString::fromUtf8("Form")); + Form->resize(932, 59); + label = new QLabel(Form); + label->setObjectName(QString::fromUtf8("label")); + label->setGeometry(QRect(170, 10, 231, 31)); + + retranslateUi(Form); + + QMetaObject::connectSlotsByName(Form); + } // setupUi + + void retranslateUi(QWidget *Form) + { + Form->setWindowTitle(QCoreApplication::translate("Form", "Form", nullptr)); + label->setText(QCoreApplication::translate("Form", "TextLabel", nullptr)); + } // retranslateUi + +}; + +namespace Ui { + class Form: public Ui_Form {}; +} // namespace Ui + +QT_END_NAMESPACE + +#endif // UI_TOAST_H diff --git a/utils/Base64.cpp b/utils/Base64.cpp new file mode 100644 index 0000000..0dc54f0 --- /dev/null +++ b/utils/Base64.cpp @@ -0,0 +1,119 @@ +#include "Base64.h" +#include "string.h" +int DecodeBase64(char * pInput, char * pOutput) { + int i = 0; + int iCnt = 0; + int iSrcLen = (int)strlen(pInput); + + char * p = pOutput; + + for (i=0; i 127) continue; + if (pInput[i] == '=') return p-pOutput+1; + + char a = BVal(pInput[i]); + if (a == 255) continue; + + switch (iCnt) + { + case 0: + { + *p = a << 2; + iCnt++; + } + break; + + case 1: + { + *p++ |= a >> 4; + *p = a << 4; + iCnt++; + } + break; + + case 2: + { + *p++ |= a >> 2; + *p = a << 6; + iCnt++; + } + break; + + case 3: + { + *p++ |= a; + iCnt = 0; + } + break; + } + } + + *p = 0x00; + return p-pOutput; +} + +int EncodeBase64(char * pInput,int iInputLen,char * pOutput) +{ + int i = 0; + int loop = 0; + int remain = 0; + int iDstLen = 0; + int iSrcLen = iInputLen; + + loop = iSrcLen/3; + remain = iSrcLen%3; + + // also can encode native char one by one as decode method + // but because all of char in native string is to be encoded so encode 3-chars one time is easier. + + for (i=0; i> 2); + char a2 = ( ((pInput[i*3] & 0x03) << 4) | (pInput[i*3+1] >> 4) ); + char a3 = ( ((pInput[i*3+1] & 0x0F) << 2) | ((pInput[i*3+2] & 0xC0) >> 6) ); + char a4 = (pInput[i*3+2] & 0x3F); + + pOutput[i*4] = AVal(a1); + pOutput[i*4+1] = AVal(a2); + pOutput[i*4+2] = AVal(a3); + pOutput[i*4+3] = AVal(a4); + } + + iDstLen = i*4; + + if (remain == 1) + { + // should pad two equal sign + i = iSrcLen-1; + char a1 = (pInput[i] >> 2); + char a2 = ((pInput[i] & 0x03) << 4); + + pOutput[iDstLen++] = AVal(a1); + pOutput[iDstLen++] = AVal(a2); + pOutput[iDstLen++] = '='; + pOutput[iDstLen++] = '='; + pOutput[iDstLen] = 0x00; + } + else if (remain == 2) + { + // should pad one equal sign + i = iSrcLen-2; + char a1 = (pInput[i] >> 2); + char a2 = ( ((pInput[i] & 0x03) << 4) | (pInput[i+1] >> 4)); + char a3 = ( (pInput[i+1] & 0x0F) << 2); + + pOutput[iDstLen++] = AVal(a1); + pOutput[iDstLen++] = AVal(a2); + pOutput[iDstLen++] = AVal(a3); + pOutput[iDstLen++] = '='; + pOutput[iDstLen] = 0x00; + } + else + { + // just division by 3 + pOutput[iDstLen] = 0x00; + } + + return iDstLen; +} diff --git a/utils/Debuger.cpp b/utils/Debuger.cpp new file mode 100644 index 0000000..e70acf0 --- /dev/null +++ b/utils/Debuger.cpp @@ -0,0 +1,21 @@ +#include "Debuger.h" + + +Debuger::Debuger() { +} + +Debuger::~Debuger() { +} + +int Debuger::Debug(wstring log) { + return 0; +} + +int Debuger::Debug(string log) { + return 0; +} + +int Debuger::Debug(const wchar_t *format, ...) { + + return 0; +} diff --git a/utils/utils.cpp b/utils/utils.cpp new file mode 100644 index 0000000..b577c78 --- /dev/null +++ b/utils/utils.cpp @@ -0,0 +1,24 @@ +#include "utils.h" + + +wstring char2wchar(const char* cchar) +{ + /* + wchar_t m_wchar[2000]; + int len = MultiByteToWideChar(CP_ACP, 0, cchar, strlen(cchar), NULL, 0); + if (len > 0) + { + MultiByteToWideChar(CP_ACP, 0, cchar, strlen(cchar), m_wchar, len); + m_wchar[len] = L'\0'; + return std::wstring(m_wchar); + }*/ + return wstring(L""); +} + + +AVPixelFormat GUIDToAvFormat(GUID mediatype){ + if(IsEqualIID(MEDIASUBTYPE_RGB32,mediatype)){ + return AV_PIX_FMT_BGRA; + } + return AV_PIX_FMT_NONE; +} diff --git a/yuvgl.pro b/yuvgl.pro new file mode 100644 index 0000000..e506b92 --- /dev/null +++ b/yuvgl.pro @@ -0,0 +1,119 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2019-09-23T11:02:49 +# +#------------------------------------------------- +QT += core gui +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +QT += network +QT += multimedia +TARGET = yuvgl + + +INCLUDEPATH += $$[QT_INSTALL_HEADERS]/QtZlib + +include(G:\\project\\c++qt\\qsswraper\\qsswraper.pri) + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which has been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +CONFIG += C++11 + +DEFINES += WIN32_LEAN_AND_MEAN + +SOURCES += \ + components/toast.cpp \ + librtmp/amf.c \ + librtmp/hashswf.c \ + librtmp/log.c \ + librtmp/parseurl.c \ + librtmp/rtmp.c \ + main.cpp \ + mainwindow.cpp \ + cplaywidget.cpp \ + media/AACAudioCoder.cpp \ + media/AudioCapture.cpp \ + media/CameraCapture.cpp \ + media/DXGICapture.cpp \ + media/RtmpPusher.cpp \ + media/VideoCoder.cpp \ + media/audiocaptureff.cpp \ + media/screen_capture.cpp \ + media/sps_decode.cpp \ + utils/Base64.cpp \ + utils/Debuger.cpp \ + utils/utils.cpp +HEADERS += \ + components/toast.h \ + librtmp/strncasecmp.h \ + mainwindow.h \ + cplaywidget.h \ + media/screen_capture.h + + +FORMS += \ + components/toast.ui \ + mainwindow.ui + + +INCLUDEPATH += media/ \ + C:\\Program Files\\OpenSSL-Win64\\include + +contains(DEFINES, __MINGW32__){ + message("mingw") + INCLUDEPATH += media/ inc/ + contains(QT_ARCH, i386) { + message("32-bit") + LIBS += -L$$PWD/third/ffmpeg/mingw/32/lib + LIBS += -lm -lavformat -lavdevice -lavfilter -lavcodec -lavutil -lswresample -lswscale -lpthread -lm -lfdk-aac -lx264 -liconv -lucrtbase -lstrmiids + LIBS += -lole32 -loleAut32 -lquartz -ldxguid -ldxapi -lwinmm -lbcrypt -lssl -lcrypto -lGdi32 -lws2_32 -lbz2 -lz -lportaudio -lshlwapi -lvfw32 -lpostproc -luuid + } else { + message("64-bit") + } + }else{ + message("msvc") + + + DEFINES += _CRT_SECURE_NO_DEPRECATE \ + _CRT_NONSTDC_NO_DEPRECATE + + + contains(QT_ARCH, i386) { + INCLUDEPATH += inc $$PWD/third/msvc32/fdk-aac/include \ + $$PWD/third/msvc32/libx264/include \ + $$PWD/third/msvc32/ffmpeg/include \ + $$PWD/third/msvc32/openssl/include + + LIBS += -L$$PWD/third/msvc32/libx264/lib + LIBS += -L$$PWD/third/msvc32/fdk-aac/lib + LIBS += -L$$PWD/third/msvc32/ffmpeg/lib + LIBS += -L$$PWD/third/msvc32/openssl/lib + + LIBS += libavfilter.a libavdevice.a libavcodec.a libpostproc.a \ + libavformat.a libavutil.a \ + libswresample.a libswscale.a fdk-aac.lib ws2_32.lib libeay32.lib ssleay32.lib \ + shell32.lib gdi32.lib crypt32.lib User32.lib GDI32.lib Advapi32.lib zlibstaticd.lib Secur32.lib \ + Bcrypt.lib Kernel32.lib portaudio_x86.lib ole32.lib oleaut32.lib strmiids.lib libx264.lib d3d9.lib + + } + else{ + message("64-bit") + + QMAKE_CXXFLAGS_RELEASE += -Zi + QMAKE_LFLAGS_RELEASE += /DEBUG /OPT:REF + } +} + + +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target + diff --git a/yuvgl.pro.user b/yuvgl.pro.user new file mode 100644 index 0000000..a027c12 --- /dev/null +++ b/yuvgl.pro.user @@ -0,0 +1,263 @@ + + + + + + EnvironmentId + {2f8149c7-6065-4889-8af2-fdde6a16f5dc} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + GBK + false + 4 + false + 80 + true + true + 1 + false + true + false + 0 + true + true + 2 + 8 + true + false + 1 + true + true + true + *.md, *.MD, Makefile + false + true + true + + + + ProjectExplorer.Project.PluginSettings + + + true + false + true + true + true + true + + + 0 + true + + true + true + Builtin.DefaultTidyAndClazy + 6 + true + + + + true + + + + + ProjectExplorer.Project.Target.0 + + Desktop + Desktop Qt 5.15.2 MSVC2019 64bit + Desktop Qt 5.15.2 MSVC2019 64bit + qt.qt5.5152.win64_msvc2019_64_kit + 0 + 0 + 0 + + 0 + G:\project\multimedia\client\rtmp_demo\build-yuvgl-Desktop_Qt_5_15_2_MSVC2019_64bit-Debug + G:/project/multimedia/client/rtmp_demo/build-yuvgl-Desktop_Qt_5_15_2_MSVC2019_64bit-Debug + + + true + QtProjectManager.QMakeBuildStep + false + + + + true + Qt4ProjectManager.MakeStep + + 2 + 构建 + 构建 + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + clean + + 1 + 清除 + 清除 + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Debug + Qt4ProjectManager.Qt4BuildConfiguration + 2 + + + G:\project\multimedia\client\rtmp_demo\build-yuvgl-Desktop_Qt_5_15_2_MSVC2019_64bit-Release + G:/project/multimedia/client/rtmp_demo/build-yuvgl-Desktop_Qt_5_15_2_MSVC2019_64bit-Release + + + true + QtProjectManager.QMakeBuildStep + false + + + + true + Qt4ProjectManager.MakeStep + + 2 + 构建 + 构建 + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + clean + + 1 + 清除 + 清除 + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Release + Qt4ProjectManager.Qt4BuildConfiguration + 0 + 0 + + + 0 + G:\project\multimedia\client\rtmp_demo\build-yuvgl-Desktop_Qt_5_15_2_MSVC2019_64bit-Profile + G:/project/multimedia/client/rtmp_demo/build-yuvgl-Desktop_Qt_5_15_2_MSVC2019_64bit-Profile + + + true + QtProjectManager.QMakeBuildStep + false + + + + true + Qt4ProjectManager.MakeStep + + 2 + 构建 + 构建 + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + clean + + 1 + 清除 + 清除 + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Profile + Qt4ProjectManager.Qt4BuildConfiguration + 0 + 0 + 0 + + 3 + + + 0 + 部署 + 部署 + ProjectExplorer.BuildSteps.Deploy + + 1 + + false + ProjectExplorer.DefaultDeployConfiguration + + 1 + + true + true + true + + 2 + + false + Qt4ProjectManager.Qt4RunConfiguration:G:/project/multimedia/client/rtmp_demo/yuvgl/yuvgl.pro + G:/project/multimedia/client/rtmp_demo/yuvgl/yuvgl.pro + true + true + true + G:/project/multimedia/client/rtmp_demo/build-yuvgl-Desktop_Qt_5_15_2_MSVC2019_64bit-Debug + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 22 + + + Version + 22 + + diff --git a/yuvgl.pro.user.76c403c b/yuvgl.pro.user.76c403c new file mode 100644 index 0000000..c7df5ea --- /dev/null +++ b/yuvgl.pro.user.76c403c @@ -0,0 +1,1048 @@ + + + + + + EnvironmentId + {76c403c8-c45c-47e2-adcf-7c2390aa8798} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + GB2312 + false + 4 + false + 80 + true + true + 1 + true + false + 0 + true + true + 0 + 8 + false + 1 + true + true + true + false + + + + ProjectExplorer.Project.PluginSettings + + + -fno-delayed-template-parsing + + true + + + + ProjectExplorer.Project.Target.0 + + Desktop Qt 5.14.0 MSVC2017 32bit + Desktop Qt 5.14.0 MSVC2017 32bit + qt.qt5.5140.win32_msvc2017_kit + 0 + 0 + 0 + + D:/project/multimedia/client/qt_gl_/build-yuvgl-Desktop_Qt_5_14_0_MSVC2017_32bit-Debug + + + true + QtProjectManager.QMakeBuildStep + true + + false + false + false + + + true + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Build + Build + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Clean + Clean + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Debug + Qt4ProjectManager.Qt4BuildConfiguration + 2 + + + D:/project/multimedia/client/qt_gl_/build-yuvgl-Desktop_Qt_5_14_0_MSVC2017_32bit-Release + + + true + QtProjectManager.QMakeBuildStep + false + + false + false + true + + + true + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Build + Build + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Clean + Clean + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Release + Qt4ProjectManager.Qt4BuildConfiguration + 0 + + + D:/project/multimedia/client/qt_gl_/build-yuvgl-Desktop_Qt_5_14_0_MSVC2017_32bit-Profile + + + true + QtProjectManager.QMakeBuildStep + true + + false + true + true + + + true + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Build + Build + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Clean + Clean + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Profile + Qt4ProjectManager.Qt4BuildConfiguration + 0 + + 3 + + + 0 + Deploy + Deploy + ProjectExplorer.BuildSteps.Deploy + + 1 + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + dwarf + + cpu-cycles + + + 250 + + -e + cpu-cycles + --call-graph + dwarf,4096 + -F + 250 + + -F + true + 4096 + false + false + 1000 + + true + + false + false + false + false + true + 0.01 + 10 + true + kcachegrind + 1 + 25 + + 1 + true + false + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + 2 + + Qt4ProjectManager.Qt4RunConfiguration:D:/project/multimedia/client/qt_gl_/yuvgl/yuvgl.pro + D:/project/multimedia/client/qt_gl_/yuvgl/yuvgl.pro + + false + + false + true + true + false + false + true + + D:/project/multimedia/client/qt_gl_/build-yuvgl-Desktop_Qt_5_14_0_MSVC2017_32bit-Debug + + 1 + + + + ProjectExplorer.Project.Target.1 + + Desktop Qt 5.14.0 MSVC2017 64bit + Desktop Qt 5.14.0 MSVC2017 64bit + qt.qt5.5140.win64_msvc2017_64_kit + 0 + 0 + 0 + + D:/project/multimedia/client/qt_gl_/yuvgl + + + true + QtProjectManager.QMakeBuildStep + true + + false + false + false + + + true + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Build + Build + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Clean + Clean + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Debug + Qt4ProjectManager.Qt4BuildConfiguration + 2 + + + D:/project/multimedia/client/qt_gl_/build-yuvgl-Desktop_Qt_5_14_0_MSVC2017_64bit-Release + + + true + QtProjectManager.QMakeBuildStep + false + + false + false + true + + + true + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Build + Build + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Clean + Clean + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Release + Qt4ProjectManager.Qt4BuildConfiguration + 0 + + + D:/project/multimedia/client/qt_gl_/build-yuvgl-Desktop_Qt_5_14_0_MSVC2017_64bit-Profile + + + true + QtProjectManager.QMakeBuildStep + true + + false + true + true + + + true + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Build + Build + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Clean + Clean + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Profile + Qt4ProjectManager.Qt4BuildConfiguration + 0 + + 3 + + + 0 + Deploy + Deploy + ProjectExplorer.BuildSteps.Deploy + + 1 + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + dwarf + + cpu-cycles + + + 250 + + -e + cpu-cycles + --call-graph + dwarf,4096 + -F + 250 + + -F + true + 4096 + false + false + 1000 + + true + + false + false + false + false + true + 0.01 + 10 + true + kcachegrind + 1 + 25 + + 1 + true + false + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + 2 + + Qt4ProjectManager.Qt4RunConfiguration:D:/project/multimedia/client/qt_gl_/yuvgl/yuvgl.pro + D:/project/multimedia/client/qt_gl_/yuvgl/yuvgl.pro + + false + + false + true + true + false + false + true + + + + 1 + + + + ProjectExplorer.Project.Target.2 + + Desktop Qt 5.14.0 MinGW 32-bit + Desktop Qt 5.14.0 MinGW 32-bit + qt.qt5.5140.win32_mingw73_kit + 0 + 0 + 0 + + D:/project/multimedia/client/qt_gl_/build-yuvgl-Desktop_Qt_5_14_0_MinGW_32_bit-Debug + + + true + QtProjectManager.QMakeBuildStep + true + + false + false + false + + + true + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Build + Build + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Clean + Clean + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Debug + Qt4ProjectManager.Qt4BuildConfiguration + 2 + + + D:/project/multimedia/client/qt_gl_/build-yuvgl-Desktop_Qt_5_14_0_MinGW_32_bit-Release + + + true + QtProjectManager.QMakeBuildStep + false + + false + false + true + + + true + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Build + Build + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Clean + Clean + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Release + Qt4ProjectManager.Qt4BuildConfiguration + 0 + + + D:/project/multimedia/client/qt_gl_/build-yuvgl-Desktop_Qt_5_14_0_MinGW_32_bit-Profile + + + true + QtProjectManager.QMakeBuildStep + true + + false + true + true + + + true + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Build + Build + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Clean + Clean + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Profile + Qt4ProjectManager.Qt4BuildConfiguration + 0 + + 3 + + + 0 + Deploy + Deploy + ProjectExplorer.BuildSteps.Deploy + + 1 + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + dwarf + + cpu-cycles + + + 250 + + -e + cpu-cycles + --call-graph + dwarf,4096 + -F + 250 + + -F + true + 4096 + false + false + 1000 + + true + + false + false + false + false + true + 0.01 + 10 + true + kcachegrind + 1 + 25 + + 1 + true + false + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + 2 + + Qt4ProjectManager.Qt4RunConfiguration:D:/project/multimedia/client/qt_gl_/yuvgl/yuvgl.pro + D:/project/multimedia/client/qt_gl_/yuvgl/yuvgl.pro + + false + + false + true + true + false + false + true + + + + 1 + + + + ProjectExplorer.Project.Target.3 + + Desktop Qt 5.14.0 MinGW 64-bit + Desktop Qt 5.14.0 MinGW 64-bit + qt.qt5.5140.win64_mingw73_kit + 0 + 0 + 0 + + D:/project/multimedia/client/qt_gl_/build-yuvgl-Desktop_Qt_5_14_0_MinGW_64_bit-Debug + + + true + QtProjectManager.QMakeBuildStep + true + + false + false + false + + + true + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Build + Build + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Clean + Clean + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Debug + Qt4ProjectManager.Qt4BuildConfiguration + 2 + + + D:/project/multimedia/client/qt_gl_/build-yuvgl-Desktop_Qt_5_14_0_MinGW_64_bit-Release + + + true + QtProjectManager.QMakeBuildStep + false + + false + false + true + + + true + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Build + Build + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Clean + Clean + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Release + Qt4ProjectManager.Qt4BuildConfiguration + 0 + + + D:/project/multimedia/client/qt_gl_/build-yuvgl-Desktop_Qt_5_14_0_MinGW_64_bit-Profile + + + true + QtProjectManager.QMakeBuildStep + true + + false + true + true + + + true + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Build + Build + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Clean + Clean + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Profile + Qt4ProjectManager.Qt4BuildConfiguration + 0 + + 3 + + + 0 + Deploy + Deploy + ProjectExplorer.BuildSteps.Deploy + + 1 + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + dwarf + + cpu-cycles + + + 250 + + -e + cpu-cycles + --call-graph + dwarf,4096 + -F + 250 + + -F + true + 4096 + false + false + 1000 + + true + + false + false + false + false + true + 0.01 + 10 + true + kcachegrind + 1 + 25 + + 1 + true + false + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + 2 + + Qt4ProjectManager.Qt4RunConfiguration:D:/project/multimedia/client/qt_gl_/yuvgl/yuvgl.pro + D:/project/multimedia/client/qt_gl_/yuvgl/yuvgl.pro + + false + + false + true + true + false + false + true + + + + 1 + + + + ProjectExplorer.Project.TargetCount + 4 + + + ProjectExplorer.Project.Updater.FileVersion + 22 + + + Version + 22 + + diff --git a/zlib.h b/zlib.h new file mode 100644 index 0000000..32c2ce0 --- /dev/null +++ b/zlib.h @@ -0,0 +1,1916 @@ +/* zlib.h -- interface of the 'zlib' general purpose compression library + version 1.2.11, January 15th, 2017 + + Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. + + Jean-loup Gailly Mark Adler + jloup@gzip.org madler@alumni.caltech.edu + + + The data format used by the zlib library is described by RFCs (Request for + Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950 + (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format). +*/ + +#ifndef ZLIB_H +#define ZLIB_H + +#include "zconf.h" + +#include +#undef ZEXTERN +#define ZEXTERN Q_CORE_EXPORT + +#ifdef __cplusplus +extern "C" { +#endif + +#define ZLIB_VERSION "1.2.11 (Qt)" +#define ZLIB_VERNUM 0x12b0f +#define ZLIB_VER_MAJOR 1 +#define ZLIB_VER_MINOR 2 +#define ZLIB_VER_REVISION 11 +#define ZLIB_VER_SUBREVISION 0 + +/* + The 'zlib' compression library provides in-memory compression and + decompression functions, including integrity checks of the uncompressed data. + This version of the library supports only one compression method (deflation) + but other algorithms will be added later and will have the same stream + interface. + + Compression can be done in a single step if the buffers are large enough, + or can be done by repeated calls of the compression function. In the latter + case, the application must provide more input and/or consume the output + (providing more output space) before each call. + + The compressed data format used by default by the in-memory functions is + the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped + around a deflate stream, which is itself documented in RFC 1951. + + The library also supports reading and writing files in gzip (.gz) format + with an interface similar to that of stdio using the functions that start + with "gz". The gzip format is different from the zlib format. gzip is a + gzip wrapper, documented in RFC 1952, wrapped around a deflate stream. + + This library can optionally read and write gzip and raw deflate streams in + memory as well. + + The zlib format was designed to be compact and fast for use in memory + and on communications channels. The gzip format was designed for single- + file compression on file systems, has a larger header than zlib to maintain + directory information, and uses a different, slower check method than zlib. + + The library does not install any signal handler. The decoder checks + the consistency of the compressed data, so the library should never crash + even in the case of corrupted input. +*/ + +typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size)); +typedef void (*free_func) OF((voidpf opaque, voidpf address)); + +struct internal_state; + +typedef struct z_stream_s { + z_const Bytef *next_in; /* next input byte */ + uInt avail_in; /* number of bytes available at next_in */ + uLong total_in; /* total number of input bytes read so far */ + + Bytef *next_out; /* next output byte will go here */ + uInt avail_out; /* remaining free space at next_out */ + uLong total_out; /* total number of bytes output so far */ + + z_const char *msg; /* last error message, NULL if no error */ + struct internal_state FAR *state; /* not visible by applications */ + + alloc_func zalloc; /* used to allocate the internal state */ + free_func zfree; /* used to free the internal state */ + voidpf opaque; /* private data object passed to zalloc and zfree */ + + int data_type; /* best guess about the data type: binary or text + for deflate, or the decoding state for inflate */ + uLong adler; /* Adler-32 or CRC-32 value of the uncompressed data */ + uLong reserved; /* reserved for future use */ +} z_stream; + +typedef z_stream FAR *z_streamp; + +/* + gzip header information passed to and from zlib routines. See RFC 1952 + for more details on the meanings of these fields. +*/ +typedef struct gz_header_s { + int text; /* true if compressed data believed to be text */ + uLong time; /* modification time */ + int xflags; /* extra flags (not used when writing a gzip file) */ + int os; /* operating system */ + Bytef *extra; /* pointer to extra field or Z_NULL if none */ + uInt extra_len; /* extra field length (valid if extra != Z_NULL) */ + uInt extra_max; /* space at extra (only when reading header) */ + Bytef *name; /* pointer to zero-terminated file name or Z_NULL */ + uInt name_max; /* space at name (only when reading header) */ + Bytef *comment; /* pointer to zero-terminated comment or Z_NULL */ + uInt comm_max; /* space at comment (only when reading header) */ + int hcrc; /* true if there was or will be a header crc */ + int done; /* true when done reading gzip header (not used + when writing a gzip file) */ +} gz_header; + +typedef gz_header FAR *gz_headerp; + +/* + The application must update next_in and avail_in when avail_in has dropped + to zero. It must update next_out and avail_out when avail_out has dropped + to zero. The application must initialize zalloc, zfree and opaque before + calling the init function. All other fields are set by the compression + library and must not be updated by the application. + + The opaque value provided by the application will be passed as the first + parameter for calls of zalloc and zfree. This can be useful for custom + memory management. The compression library attaches no meaning to the + opaque value. + + zalloc must return Z_NULL if there is not enough memory for the object. + If zlib is used in a multi-threaded application, zalloc and zfree must be + thread safe. In that case, zlib is thread-safe. When zalloc and zfree are + Z_NULL on entry to the initialization function, they are set to internal + routines that use the standard library functions malloc() and free(). + + On 16-bit systems, the functions zalloc and zfree must be able to allocate + exactly 65536 bytes, but will not be required to allocate more than this if + the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS, pointers + returned by zalloc for objects of exactly 65536 bytes *must* have their + offset normalized to zero. The default allocation function provided by this + library ensures this (see zutil.c). To reduce memory requirements and avoid + any allocation of 64K objects, at the expense of compression ratio, compile + the library with -DMAX_WBITS=14 (see zconf.h). + + The fields total_in and total_out can be used for statistics or progress + reports. After compression, total_in holds the total size of the + uncompressed data and may be saved for use by the decompressor (particularly + if the decompressor wants to decompress everything in a single step). +*/ + + /* constants */ + +#define Z_NO_FLUSH 0 +#define Z_PARTIAL_FLUSH 1 +#define Z_SYNC_FLUSH 2 +#define Z_FULL_FLUSH 3 +#define Z_FINISH 4 +#define Z_BLOCK 5 +#define Z_TREES 6 +/* Allowed flush values; see deflate() and inflate() below for details */ + +#define Z_OK 0 +#define Z_STREAM_END 1 +#define Z_NEED_DICT 2 +#define Z_ERRNO (-1) +#define Z_STREAM_ERROR (-2) +#define Z_DATA_ERROR (-3) +#define Z_MEM_ERROR (-4) +#define Z_BUF_ERROR (-5) +#define Z_VERSION_ERROR (-6) +/* Return codes for the compression/decompression functions. Negative values + * are errors, positive values are used for special but normal events. + */ + +#define Z_NO_COMPRESSION 0 +#define Z_BEST_SPEED 1 +#define Z_BEST_COMPRESSION 9 +#define Z_DEFAULT_COMPRESSION (-1) +/* compression levels */ + +#define Z_FILTERED 1 +#define Z_HUFFMAN_ONLY 2 +#define Z_RLE 3 +#define Z_FIXED 4 +#define Z_DEFAULT_STRATEGY 0 +/* compression strategy; see deflateInit2() below for details */ + +#define Z_BINARY 0 +#define Z_TEXT 1 +#define Z_ASCII Z_TEXT /* for compatibility with 1.2.2 and earlier */ +#define Z_UNKNOWN 2 +/* Possible values of the data_type field for deflate() */ + +#define Z_DEFLATED 8 +/* The deflate compression method (the only one supported in this version) */ + +#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */ + +#define zlib_version zlibVersion() +/* for compatibility with versions < 1.0.2 */ + + + /* basic functions */ + +ZEXTERN const char * ZEXPORT zlibVersion OF((void)); +/* The application can compare zlibVersion and ZLIB_VERSION for consistency. + If the first character differs, the library code actually used is not + compatible with the zlib.h header file used by the application. This check + is automatically made by deflateInit and inflateInit. + */ + +/* +ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level)); + + Initializes the internal stream state for compression. The fields + zalloc, zfree and opaque must be initialized before by the caller. If + zalloc and zfree are set to Z_NULL, deflateInit updates them to use default + allocation functions. + + The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9: + 1 gives best speed, 9 gives best compression, 0 gives no compression at all + (the input data is simply copied a block at a time). Z_DEFAULT_COMPRESSION + requests a default compromise between speed and compression (currently + equivalent to level 6). + + deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_STREAM_ERROR if level is not a valid compression level, or + Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible + with the version assumed by the caller (ZLIB_VERSION). msg is set to null + if there is no error message. deflateInit does not perform any compression: + this will be done by deflate(). +*/ + + +ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush)); +/* + deflate compresses as much data as possible, and stops when the input + buffer becomes empty or the output buffer becomes full. It may introduce + some output latency (reading input without producing any output) except when + forced to flush. + + The detailed semantics are as follows. deflate performs one or both of the + following actions: + + - Compress more input starting at next_in and update next_in and avail_in + accordingly. If not all input can be processed (because there is not + enough room in the output buffer), next_in and avail_in are updated and + processing will resume at this point for the next call of deflate(). + + - Generate more output starting at next_out and update next_out and avail_out + accordingly. This action is forced if the parameter flush is non zero. + Forcing flush frequently degrades the compression ratio, so this parameter + should be set only when necessary. Some output may be provided even if + flush is zero. + + Before the call of deflate(), the application should ensure that at least + one of the actions is possible, by providing more input and/or consuming more + output, and updating avail_in or avail_out accordingly; avail_out should + never be zero before the call. The application can consume the compressed + output when it wants, for example when the output buffer is full (avail_out + == 0), or after each call of deflate(). If deflate returns Z_OK and with + zero avail_out, it must be called again after making room in the output + buffer because there might be more output pending. See deflatePending(), + which can be used if desired to determine whether or not there is more ouput + in that case. + + Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to + decide how much data to accumulate before producing output, in order to + maximize compression. + + If the parameter flush is set to Z_SYNC_FLUSH, all pending output is + flushed to the output buffer and the output is aligned on a byte boundary, so + that the decompressor can get all input data available so far. (In + particular avail_in is zero after the call if enough output space has been + provided before the call.) Flushing may degrade compression for some + compression algorithms and so it should be used only when necessary. This + completes the current deflate block and follows it with an empty stored block + that is three bits plus filler bits to the next byte, followed by four bytes + (00 00 ff ff). + + If flush is set to Z_PARTIAL_FLUSH, all pending output is flushed to the + output buffer, but the output is not aligned to a byte boundary. All of the + input data so far will be available to the decompressor, as for Z_SYNC_FLUSH. + This completes the current deflate block and follows it with an empty fixed + codes block that is 10 bits long. This assures that enough bytes are output + in order for the decompressor to finish the block before the empty fixed + codes block. + + If flush is set to Z_BLOCK, a deflate block is completed and emitted, as + for Z_SYNC_FLUSH, but the output is not aligned on a byte boundary, and up to + seven bits of the current block are held to be written as the next byte after + the next deflate block is completed. In this case, the decompressor may not + be provided enough bits at this point in order to complete decompression of + the data provided so far to the compressor. It may need to wait for the next + block to be emitted. This is for advanced applications that need to control + the emission of deflate blocks. + + If flush is set to Z_FULL_FLUSH, all output is flushed as with + Z_SYNC_FLUSH, and the compression state is reset so that decompression can + restart from this point if previous compressed data has been damaged or if + random access is desired. Using Z_FULL_FLUSH too often can seriously degrade + compression. + + If deflate returns with avail_out == 0, this function must be called again + with the same value of the flush parameter and more output space (updated + avail_out), until the flush is complete (deflate returns with non-zero + avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that + avail_out is greater than six to avoid repeated flush markers due to + avail_out == 0 on return. + + If the parameter flush is set to Z_FINISH, pending input is processed, + pending output is flushed and deflate returns with Z_STREAM_END if there was + enough output space. If deflate returns with Z_OK or Z_BUF_ERROR, this + function must be called again with Z_FINISH and more output space (updated + avail_out) but no more input data, until it returns with Z_STREAM_END or an + error. After deflate has returned Z_STREAM_END, the only possible operations + on the stream are deflateReset or deflateEnd. + + Z_FINISH can be used in the first deflate call after deflateInit if all the + compression is to be done in a single step. In order to complete in one + call, avail_out must be at least the value returned by deflateBound (see + below). Then deflate is guaranteed to return Z_STREAM_END. If not enough + output space is provided, deflate will not return Z_STREAM_END, and it must + be called again as described above. + + deflate() sets strm->adler to the Adler-32 checksum of all input read + so far (that is, total_in bytes). If a gzip stream is being generated, then + strm->adler will be the CRC-32 checksum of the input read so far. (See + deflateInit2 below.) + + deflate() may update strm->data_type if it can make a good guess about + the input data type (Z_BINARY or Z_TEXT). If in doubt, the data is + considered binary. This field is only for information purposes and does not + affect the compression algorithm in any manner. + + deflate() returns Z_OK if some progress has been made (more input + processed or more output produced), Z_STREAM_END if all input has been + consumed and all output has been produced (only when flush is set to + Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example + if next_in or next_out was Z_NULL or the state was inadvertently written over + by the application), or Z_BUF_ERROR if no progress is possible (for example + avail_in or avail_out was zero). Note that Z_BUF_ERROR is not fatal, and + deflate() can be called again with more input and more output space to + continue compressing. +*/ + + +ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm)); +/* + All dynamically allocated data structures for this stream are freed. + This function discards any unprocessed input and does not flush any pending + output. + + deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the + stream state was inconsistent, Z_DATA_ERROR if the stream was freed + prematurely (some input or output was discarded). In the error case, msg + may be set but then points to a static string (which must not be + deallocated). +*/ + + +/* +ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm)); + + Initializes the internal stream state for decompression. The fields + next_in, avail_in, zalloc, zfree and opaque must be initialized before by + the caller. In the current version of inflate, the provided input is not + read or consumed. The allocation of a sliding window will be deferred to + the first call of inflate (if the decompression does not complete on the + first call). If zalloc and zfree are set to Z_NULL, inflateInit updates + them to use default allocation functions. + + inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_VERSION_ERROR if the zlib library version is incompatible with the + version assumed by the caller, or Z_STREAM_ERROR if the parameters are + invalid, such as a null pointer to the structure. msg is set to null if + there is no error message. inflateInit does not perform any decompression. + Actual decompression will be done by inflate(). So next_in, and avail_in, + next_out, and avail_out are unused and unchanged. The current + implementation of inflateInit() does not process any header information -- + that is deferred until inflate() is called. +*/ + + +ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush)); +/* + inflate decompresses as much data as possible, and stops when the input + buffer becomes empty or the output buffer becomes full. It may introduce + some output latency (reading input without producing any output) except when + forced to flush. + + The detailed semantics are as follows. inflate performs one or both of the + following actions: + + - Decompress more input starting at next_in and update next_in and avail_in + accordingly. If not all input can be processed (because there is not + enough room in the output buffer), then next_in and avail_in are updated + accordingly, and processing will resume at this point for the next call of + inflate(). + + - Generate more output starting at next_out and update next_out and avail_out + accordingly. inflate() provides as much output as possible, until there is + no more input data or no more space in the output buffer (see below about + the flush parameter). + + Before the call of inflate(), the application should ensure that at least + one of the actions is possible, by providing more input and/or consuming more + output, and updating the next_* and avail_* values accordingly. If the + caller of inflate() does not provide both available input and available + output space, it is possible that there will be no progress made. The + application can consume the uncompressed output when it wants, for example + when the output buffer is full (avail_out == 0), or after each call of + inflate(). If inflate returns Z_OK and with zero avail_out, it must be + called again after making room in the output buffer because there might be + more output pending. + + The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH, Z_FINISH, + Z_BLOCK, or Z_TREES. Z_SYNC_FLUSH requests that inflate() flush as much + output as possible to the output buffer. Z_BLOCK requests that inflate() + stop if and when it gets to the next deflate block boundary. When decoding + the zlib or gzip format, this will cause inflate() to return immediately + after the header and before the first block. When doing a raw inflate, + inflate() will go ahead and process the first block, and will return when it + gets to the end of that block, or when it runs out of data. + + The Z_BLOCK option assists in appending to or combining deflate streams. + To assist in this, on return inflate() always sets strm->data_type to the + number of unused bits in the last byte taken from strm->next_in, plus 64 if + inflate() is currently decoding the last block in the deflate stream, plus + 128 if inflate() returned immediately after decoding an end-of-block code or + decoding the complete header up to just before the first byte of the deflate + stream. The end-of-block will not be indicated until all of the uncompressed + data from that block has been written to strm->next_out. The number of + unused bits may in general be greater than seven, except when bit 7 of + data_type is set, in which case the number of unused bits will be less than + eight. data_type is set as noted here every time inflate() returns for all + flush options, and so can be used to determine the amount of currently + consumed input in bits. + + The Z_TREES option behaves as Z_BLOCK does, but it also returns when the + end of each deflate block header is reached, before any actual data in that + block is decoded. This allows the caller to determine the length of the + deflate block header for later use in random access within a deflate block. + 256 is added to the value of strm->data_type when inflate() returns + immediately after reaching the end of the deflate block header. + + inflate() should normally be called until it returns Z_STREAM_END or an + error. However if all decompression is to be performed in a single step (a + single call of inflate), the parameter flush should be set to Z_FINISH. In + this case all pending input is processed and all pending output is flushed; + avail_out must be large enough to hold all of the uncompressed data for the + operation to complete. (The size of the uncompressed data may have been + saved by the compressor for this purpose.) The use of Z_FINISH is not + required to perform an inflation in one step. However it may be used to + inform inflate that a faster approach can be used for the single inflate() + call. Z_FINISH also informs inflate to not maintain a sliding window if the + stream completes, which reduces inflate's memory footprint. If the stream + does not complete, either because not all of the stream is provided or not + enough output space is provided, then a sliding window will be allocated and + inflate() can be called again to continue the operation as if Z_NO_FLUSH had + been used. + + In this implementation, inflate() always flushes as much output as + possible to the output buffer, and always uses the faster approach on the + first call. So the effects of the flush parameter in this implementation are + on the return value of inflate() as noted below, when inflate() returns early + when Z_BLOCK or Z_TREES is used, and when inflate() avoids the allocation of + memory for a sliding window when Z_FINISH is used. + + If a preset dictionary is needed after this call (see inflateSetDictionary + below), inflate sets strm->adler to the Adler-32 checksum of the dictionary + chosen by the compressor and returns Z_NEED_DICT; otherwise it sets + strm->adler to the Adler-32 checksum of all output produced so far (that is, + total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described + below. At the end of the stream, inflate() checks that its computed Adler-32 + checksum is equal to that saved by the compressor and returns Z_STREAM_END + only if the checksum is correct. + + inflate() can decompress and check either zlib-wrapped or gzip-wrapped + deflate data. The header type is detected automatically, if requested when + initializing with inflateInit2(). Any information contained in the gzip + header is not retained unless inflateGetHeader() is used. When processing + gzip-wrapped deflate data, strm->adler32 is set to the CRC-32 of the output + produced so far. The CRC-32 is checked against the gzip trailer, as is the + uncompressed length, modulo 2^32. + + inflate() returns Z_OK if some progress has been made (more input processed + or more output produced), Z_STREAM_END if the end of the compressed data has + been reached and all uncompressed output has been produced, Z_NEED_DICT if a + preset dictionary is needed at this point, Z_DATA_ERROR if the input data was + corrupted (input stream not conforming to the zlib format or incorrect check + value, in which case strm->msg points to a string with a more specific + error), Z_STREAM_ERROR if the stream structure was inconsistent (for example + next_in or next_out was Z_NULL, or the state was inadvertently written over + by the application), Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR + if no progress was possible or if there was not enough room in the output + buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and + inflate() can be called again with more input and more output space to + continue decompressing. If Z_DATA_ERROR is returned, the application may + then call inflateSync() to look for a good compression block if a partial + recovery of the data is to be attempted. +*/ + + +ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm)); +/* + All dynamically allocated data structures for this stream are freed. + This function discards any unprocessed input and does not flush any pending + output. + + inflateEnd returns Z_OK if success, or Z_STREAM_ERROR if the stream state + was inconsistent. +*/ + + + /* Advanced functions */ + +/* + The following functions are needed only in some special applications. +*/ + +/* +ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm, + int level, + int method, + int windowBits, + int memLevel, + int strategy)); + + This is another version of deflateInit with more compression options. The + fields next_in, zalloc, zfree and opaque must be initialized before by the + caller. + + The method parameter is the compression method. It must be Z_DEFLATED in + this version of the library. + + The windowBits parameter is the base two logarithm of the window size + (the size of the history buffer). It should be in the range 8..15 for this + version of the library. Larger values of this parameter result in better + compression at the expense of memory usage. The default value is 15 if + deflateInit is used instead. + + For the current implementation of deflate(), a windowBits value of 8 (a + window size of 256 bytes) is not supported. As a result, a request for 8 + will result in 9 (a 512-byte window). In that case, providing 8 to + inflateInit2() will result in an error when the zlib header with 9 is + checked against the initialization of inflate(). The remedy is to not use 8 + with deflateInit2() with this initialization, or at least in that case use 9 + with inflateInit2(). + + windowBits can also be -8..-15 for raw deflate. In this case, -windowBits + determines the window size. deflate() will then generate raw deflate data + with no zlib header or trailer, and will not compute a check value. + + windowBits can also be greater than 15 for optional gzip encoding. Add + 16 to windowBits to write a simple gzip header and trailer around the + compressed data instead of a zlib wrapper. The gzip header will have no + file name, no extra data, no comment, no modification time (set to zero), no + header crc, and the operating system will be set to the appropriate value, + if the operating system was determined at compile time. If a gzip stream is + being written, strm->adler is a CRC-32 instead of an Adler-32. + + For raw deflate or gzip encoding, a request for a 256-byte window is + rejected as invalid, since only the zlib header provides a means of + transmitting the window size to the decompressor. + + The memLevel parameter specifies how much memory should be allocated + for the internal compression state. memLevel=1 uses minimum memory but is + slow and reduces compression ratio; memLevel=9 uses maximum memory for + optimal speed. The default value is 8. See zconf.h for total memory usage + as a function of windowBits and memLevel. + + The strategy parameter is used to tune the compression algorithm. Use the + value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a + filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no + string match), or Z_RLE to limit match distances to one (run-length + encoding). Filtered data consists mostly of small values with a somewhat + random distribution. In this case, the compression algorithm is tuned to + compress them better. The effect of Z_FILTERED is to force more Huffman + coding and less string matching; it is somewhat intermediate between + Z_DEFAULT_STRATEGY and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as + fast as Z_HUFFMAN_ONLY, but give better compression for PNG image data. The + strategy parameter only affects the compression ratio but not the + correctness of the compressed output even if it is not set appropriately. + Z_FIXED prevents the use of dynamic Huffman codes, allowing for a simpler + decoder for special applications. + + deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_STREAM_ERROR if any parameter is invalid (such as an invalid + method), or Z_VERSION_ERROR if the zlib library version (zlib_version) is + incompatible with the version assumed by the caller (ZLIB_VERSION). msg is + set to null if there is no error message. deflateInit2 does not perform any + compression: this will be done by deflate(). +*/ + +ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm, + const Bytef *dictionary, + uInt dictLength)); +/* + Initializes the compression dictionary from the given byte sequence + without producing any compressed output. When using the zlib format, this + function must be called immediately after deflateInit, deflateInit2 or + deflateReset, and before any call of deflate. When doing raw deflate, this + function must be called either before any call of deflate, or immediately + after the completion of a deflate block, i.e. after all input has been + consumed and all output has been delivered when using any of the flush + options Z_BLOCK, Z_PARTIAL_FLUSH, Z_SYNC_FLUSH, or Z_FULL_FLUSH. The + compressor and decompressor must use exactly the same dictionary (see + inflateSetDictionary). + + The dictionary should consist of strings (byte sequences) that are likely + to be encountered later in the data to be compressed, with the most commonly + used strings preferably put towards the end of the dictionary. Using a + dictionary is most useful when the data to be compressed is short and can be + predicted with good accuracy; the data can then be compressed better than + with the default empty dictionary. + + Depending on the size of the compression data structures selected by + deflateInit or deflateInit2, a part of the dictionary may in effect be + discarded, for example if the dictionary is larger than the window size + provided in deflateInit or deflateInit2. Thus the strings most likely to be + useful should be put at the end of the dictionary, not at the front. In + addition, the current implementation of deflate will use at most the window + size minus 262 bytes of the provided dictionary. + + Upon return of this function, strm->adler is set to the Adler-32 value + of the dictionary; the decompressor may later use this value to determine + which dictionary has been used by the compressor. (The Adler-32 value + applies to the whole dictionary even if only a subset of the dictionary is + actually used by the compressor.) If a raw deflate was requested, then the + Adler-32 value is not computed and strm->adler is not set. + + deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a + parameter is invalid (e.g. dictionary being Z_NULL) or the stream state is + inconsistent (for example if deflate has already been called for this stream + or if not at a block boundary for raw deflate). deflateSetDictionary does + not perform any compression: this will be done by deflate(). +*/ + +ZEXTERN int ZEXPORT deflateGetDictionary OF((z_streamp strm, + Bytef *dictionary, + uInt *dictLength)); +/* + Returns the sliding dictionary being maintained by deflate. dictLength is + set to the number of bytes in the dictionary, and that many bytes are copied + to dictionary. dictionary must have enough space, where 32768 bytes is + always enough. If deflateGetDictionary() is called with dictionary equal to + Z_NULL, then only the dictionary length is returned, and nothing is copied. + Similary, if dictLength is Z_NULL, then it is not set. + + deflateGetDictionary() may return a length less than the window size, even + when more than the window size in input has been provided. It may return up + to 258 bytes less in that case, due to how zlib's implementation of deflate + manages the sliding window and lookahead for matches, where matches can be + up to 258 bytes long. If the application needs the last window-size bytes of + input, then that would need to be saved by the application outside of zlib. + + deflateGetDictionary returns Z_OK on success, or Z_STREAM_ERROR if the + stream state is inconsistent. +*/ + +ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest, + z_streamp source)); +/* + Sets the destination stream as a complete copy of the source stream. + + This function can be useful when several compression strategies will be + tried, for example when there are several ways of pre-processing the input + data with a filter. The streams that will be discarded should then be freed + by calling deflateEnd. Note that deflateCopy duplicates the internal + compression state which can be quite large, so this strategy is slow and can + consume lots of memory. + + deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_STREAM_ERROR if the source stream state was inconsistent + (such as zalloc being Z_NULL). msg is left unchanged in both source and + destination. +*/ + +ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm)); +/* + This function is equivalent to deflateEnd followed by deflateInit, but + does not free and reallocate the internal compression state. The stream + will leave the compression level and any other attributes that may have been + set unchanged. + + deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent (such as zalloc or state being Z_NULL). +*/ + +ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm, + int level, + int strategy)); +/* + Dynamically update the compression level and compression strategy. The + interpretation of level and strategy is as in deflateInit2(). This can be + used to switch between compression and straight copy of the input data, or + to switch to a different kind of input data requiring a different strategy. + If the compression approach (which is a function of the level) or the + strategy is changed, and if any input has been consumed in a previous + deflate() call, then the input available so far is compressed with the old + level and strategy using deflate(strm, Z_BLOCK). There are three approaches + for the compression levels 0, 1..3, and 4..9 respectively. The new level + and strategy will take effect at the next call of deflate(). + + If a deflate(strm, Z_BLOCK) is performed by deflateParams(), and it does + not have enough output space to complete, then the parameter change will not + take effect. In this case, deflateParams() can be called again with the + same parameters and more output space to try again. + + In order to assure a change in the parameters on the first try, the + deflate stream should be flushed using deflate() with Z_BLOCK or other flush + request until strm.avail_out is not zero, before calling deflateParams(). + Then no more input data should be provided before the deflateParams() call. + If this is done, the old level and strategy will be applied to the data + compressed before deflateParams(), and the new level and strategy will be + applied to the the data compressed after deflateParams(). + + deflateParams returns Z_OK on success, Z_STREAM_ERROR if the source stream + state was inconsistent or if a parameter was invalid, or Z_BUF_ERROR if + there was not enough output space to complete the compression of the + available input data before a change in the strategy or approach. Note that + in the case of a Z_BUF_ERROR, the parameters are not changed. A return + value of Z_BUF_ERROR is not fatal, in which case deflateParams() can be + retried with more output space. +*/ + +ZEXTERN int ZEXPORT deflateTune OF((z_streamp strm, + int good_length, + int max_lazy, + int nice_length, + int max_chain)); +/* + Fine tune deflate's internal compression parameters. This should only be + used by someone who understands the algorithm used by zlib's deflate for + searching for the best matching string, and even then only by the most + fanatic optimizer trying to squeeze out the last compressed bit for their + specific input data. Read the deflate.c source code for the meaning of the + max_lazy, good_length, nice_length, and max_chain parameters. + + deflateTune() can be called after deflateInit() or deflateInit2(), and + returns Z_OK on success, or Z_STREAM_ERROR for an invalid deflate stream. + */ + +ZEXTERN uLong ZEXPORT deflateBound OF((z_streamp strm, + uLong sourceLen)); +/* + deflateBound() returns an upper bound on the compressed size after + deflation of sourceLen bytes. It must be called after deflateInit() or + deflateInit2(), and after deflateSetHeader(), if used. This would be used + to allocate an output buffer for deflation in a single pass, and so would be + called before deflate(). If that first deflate() call is provided the + sourceLen input bytes, an output buffer allocated to the size returned by + deflateBound(), and the flush value Z_FINISH, then deflate() is guaranteed + to return Z_STREAM_END. Note that it is possible for the compressed size to + be larger than the value returned by deflateBound() if flush options other + than Z_FINISH or Z_NO_FLUSH are used. +*/ + +ZEXTERN int ZEXPORT deflatePending OF((z_streamp strm, + unsigned *pending, + int *bits)); +/* + deflatePending() returns the number of bytes and bits of output that have + been generated, but not yet provided in the available output. The bytes not + provided would be due to the available output space having being consumed. + The number of bits of output not provided are between 0 and 7, where they + await more bits to join them in order to fill out a full byte. If pending + or bits are Z_NULL, then those values are not set. + + deflatePending returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. + */ + +ZEXTERN int ZEXPORT deflatePrime OF((z_streamp strm, + int bits, + int value)); +/* + deflatePrime() inserts bits in the deflate output stream. The intent + is that this function is used to start off the deflate output with the bits + leftover from a previous deflate stream when appending to it. As such, this + function can only be used for raw deflate, and must be used before the first + deflate() call after a deflateInit2() or deflateReset(). bits must be less + than or equal to 16, and that many of the least significant bits of value + will be inserted in the output. + + deflatePrime returns Z_OK if success, Z_BUF_ERROR if there was not enough + room in the internal buffer to insert the bits, or Z_STREAM_ERROR if the + source stream state was inconsistent. +*/ + +ZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm, + gz_headerp head)); +/* + deflateSetHeader() provides gzip header information for when a gzip + stream is requested by deflateInit2(). deflateSetHeader() may be called + after deflateInit2() or deflateReset() and before the first call of + deflate(). The text, time, os, extra field, name, and comment information + in the provided gz_header structure are written to the gzip header (xflag is + ignored -- the extra flags are set according to the compression level). The + caller must assure that, if not Z_NULL, name and comment are terminated with + a zero byte, and that if extra is not Z_NULL, that extra_len bytes are + available there. If hcrc is true, a gzip header crc is included. Note that + the current versions of the command-line version of gzip (up through version + 1.3.x) do not support header crc's, and will report that it is a "multi-part + gzip file" and give up. + + If deflateSetHeader is not used, the default gzip header has text false, + the time set to zero, and os set to 255, with no extra, name, or comment + fields. The gzip header is returned to the default state by deflateReset(). + + deflateSetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. +*/ + +/* +ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm, + int windowBits)); + + This is another version of inflateInit with an extra parameter. The + fields next_in, avail_in, zalloc, zfree and opaque must be initialized + before by the caller. + + The windowBits parameter is the base two logarithm of the maximum window + size (the size of the history buffer). It should be in the range 8..15 for + this version of the library. The default value is 15 if inflateInit is used + instead. windowBits must be greater than or equal to the windowBits value + provided to deflateInit2() while compressing, or it must be equal to 15 if + deflateInit2() was not used. If a compressed stream with a larger window + size is given as input, inflate() will return with the error code + Z_DATA_ERROR instead of trying to allocate a larger window. + + windowBits can also be zero to request that inflate use the window size in + the zlib header of the compressed stream. + + windowBits can also be -8..-15 for raw inflate. In this case, -windowBits + determines the window size. inflate() will then process raw deflate data, + not looking for a zlib or gzip header, not generating a check value, and not + looking for any check values for comparison at the end of the stream. This + is for use with other formats that use the deflate compressed data format + such as zip. Those formats provide their own check values. If a custom + format is developed using the raw deflate format for compressed data, it is + recommended that a check value such as an Adler-32 or a CRC-32 be applied to + the uncompressed data as is done in the zlib, gzip, and zip formats. For + most applications, the zlib format should be used as is. Note that comments + above on the use in deflateInit2() applies to the magnitude of windowBits. + + windowBits can also be greater than 15 for optional gzip decoding. Add + 32 to windowBits to enable zlib and gzip decoding with automatic header + detection, or add 16 to decode only the gzip format (the zlib format will + return a Z_DATA_ERROR). If a gzip stream is being decoded, strm->adler is a + CRC-32 instead of an Adler-32. Unlike the gunzip utility and gzread() (see + below), inflate() will not automatically decode concatenated gzip streams. + inflate() will return Z_STREAM_END at the end of the gzip stream. The state + would need to be reset to continue decoding a subsequent gzip stream. + + inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_VERSION_ERROR if the zlib library version is incompatible with the + version assumed by the caller, or Z_STREAM_ERROR if the parameters are + invalid, such as a null pointer to the structure. msg is set to null if + there is no error message. inflateInit2 does not perform any decompression + apart from possibly reading the zlib header if present: actual decompression + will be done by inflate(). (So next_in and avail_in may be modified, but + next_out and avail_out are unused and unchanged.) The current implementation + of inflateInit2() does not process any header information -- that is + deferred until inflate() is called. +*/ + +ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm, + const Bytef *dictionary, + uInt dictLength)); +/* + Initializes the decompression dictionary from the given uncompressed byte + sequence. This function must be called immediately after a call of inflate, + if that call returned Z_NEED_DICT. The dictionary chosen by the compressor + can be determined from the Adler-32 value returned by that call of inflate. + The compressor and decompressor must use exactly the same dictionary (see + deflateSetDictionary). For raw inflate, this function can be called at any + time to set the dictionary. If the provided dictionary is smaller than the + window and there is already data in the window, then the provided dictionary + will amend what's there. The application must insure that the dictionary + that was used for compression is provided. + + inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a + parameter is invalid (e.g. dictionary being Z_NULL) or the stream state is + inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the + expected one (incorrect Adler-32 value). inflateSetDictionary does not + perform any decompression: this will be done by subsequent calls of + inflate(). +*/ + +ZEXTERN int ZEXPORT inflateGetDictionary OF((z_streamp strm, + Bytef *dictionary, + uInt *dictLength)); +/* + Returns the sliding dictionary being maintained by inflate. dictLength is + set to the number of bytes in the dictionary, and that many bytes are copied + to dictionary. dictionary must have enough space, where 32768 bytes is + always enough. If inflateGetDictionary() is called with dictionary equal to + Z_NULL, then only the dictionary length is returned, and nothing is copied. + Similary, if dictLength is Z_NULL, then it is not set. + + inflateGetDictionary returns Z_OK on success, or Z_STREAM_ERROR if the + stream state is inconsistent. +*/ + +ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm)); +/* + Skips invalid compressed data until a possible full flush point (see above + for the description of deflate with Z_FULL_FLUSH) can be found, or until all + available input is skipped. No output is provided. + + inflateSync searches for a 00 00 FF FF pattern in the compressed data. + All full flush points have this pattern, but not all occurrences of this + pattern are full flush points. + + inflateSync returns Z_OK if a possible full flush point has been found, + Z_BUF_ERROR if no more input was provided, Z_DATA_ERROR if no flush point + has been found, or Z_STREAM_ERROR if the stream structure was inconsistent. + In the success case, the application may save the current current value of + total_in which indicates where valid compressed data was found. In the + error case, the application may repeatedly call inflateSync, providing more + input each time, until success or end of the input data. +*/ + +ZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest, + z_streamp source)); +/* + Sets the destination stream as a complete copy of the source stream. + + This function can be useful when randomly accessing a large stream. The + first pass through the stream can periodically record the inflate state, + allowing restarting inflate at those points when randomly accessing the + stream. + + inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_STREAM_ERROR if the source stream state was inconsistent + (such as zalloc being Z_NULL). msg is left unchanged in both source and + destination. +*/ + +ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm)); +/* + This function is equivalent to inflateEnd followed by inflateInit, + but does not free and reallocate the internal decompression state. The + stream will keep attributes that may have been set by inflateInit2. + + inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent (such as zalloc or state being Z_NULL). +*/ + +ZEXTERN int ZEXPORT inflateReset2 OF((z_streamp strm, + int windowBits)); +/* + This function is the same as inflateReset, but it also permits changing + the wrap and window size requests. The windowBits parameter is interpreted + the same as it is for inflateInit2. If the window size is changed, then the + memory allocated for the window is freed, and the window will be reallocated + by inflate() if needed. + + inflateReset2 returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent (such as zalloc or state being Z_NULL), or if + the windowBits parameter is invalid. +*/ + +ZEXTERN int ZEXPORT inflatePrime OF((z_streamp strm, + int bits, + int value)); +/* + This function inserts bits in the inflate input stream. The intent is + that this function is used to start inflating at a bit position in the + middle of a byte. The provided bits will be used before any bytes are used + from next_in. This function should only be used with raw inflate, and + should be used before the first inflate() call after inflateInit2() or + inflateReset(). bits must be less than or equal to 16, and that many of the + least significant bits of value will be inserted in the input. + + If bits is negative, then the input stream bit buffer is emptied. Then + inflatePrime() can be called again to put bits in the buffer. This is used + to clear out bits leftover after feeding inflate a block description prior + to feeding inflate codes. + + inflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. +*/ + +ZEXTERN long ZEXPORT inflateMark OF((z_streamp strm)); +/* + This function returns two values, one in the lower 16 bits of the return + value, and the other in the remaining upper bits, obtained by shifting the + return value down 16 bits. If the upper value is -1 and the lower value is + zero, then inflate() is currently decoding information outside of a block. + If the upper value is -1 and the lower value is non-zero, then inflate is in + the middle of a stored block, with the lower value equaling the number of + bytes from the input remaining to copy. If the upper value is not -1, then + it is the number of bits back from the current bit position in the input of + the code (literal or length/distance pair) currently being processed. In + that case the lower value is the number of bytes already emitted for that + code. + + A code is being processed if inflate is waiting for more input to complete + decoding of the code, or if it has completed decoding but is waiting for + more output space to write the literal or match data. + + inflateMark() is used to mark locations in the input data for random + access, which may be at bit positions, and to note those cases where the + output of a code may span boundaries of random access blocks. The current + location in the input stream can be determined from avail_in and data_type + as noted in the description for the Z_BLOCK flush parameter for inflate. + + inflateMark returns the value noted above, or -65536 if the provided + source stream state was inconsistent. +*/ + +ZEXTERN int ZEXPORT inflateGetHeader OF((z_streamp strm, + gz_headerp head)); +/* + inflateGetHeader() requests that gzip header information be stored in the + provided gz_header structure. inflateGetHeader() may be called after + inflateInit2() or inflateReset(), and before the first call of inflate(). + As inflate() processes the gzip stream, head->done is zero until the header + is completed, at which time head->done is set to one. If a zlib stream is + being decoded, then head->done is set to -1 to indicate that there will be + no gzip header information forthcoming. Note that Z_BLOCK or Z_TREES can be + used to force inflate() to return immediately after header processing is + complete and before any actual data is decompressed. + + The text, time, xflags, and os fields are filled in with the gzip header + contents. hcrc is set to true if there is a header CRC. (The header CRC + was valid if done is set to one.) If extra is not Z_NULL, then extra_max + contains the maximum number of bytes to write to extra. Once done is true, + extra_len contains the actual extra field length, and extra contains the + extra field, or that field truncated if extra_max is less than extra_len. + If name is not Z_NULL, then up to name_max characters are written there, + terminated with a zero unless the length is greater than name_max. If + comment is not Z_NULL, then up to comm_max characters are written there, + terminated with a zero unless the length is greater than comm_max. When any + of extra, name, or comment are not Z_NULL and the respective field is not + present in the header, then that field is set to Z_NULL to signal its + absence. This allows the use of deflateSetHeader() with the returned + structure to duplicate the header. However if those fields are set to + allocated memory, then the application will need to save those pointers + elsewhere so that they can be eventually freed. + + If inflateGetHeader is not used, then the header information is simply + discarded. The header is always checked for validity, including the header + CRC if present. inflateReset() will reset the process to discard the header + information. The application would need to call inflateGetHeader() again to + retrieve the header from the next gzip stream. + + inflateGetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. +*/ + +/* +ZEXTERN int ZEXPORT inflateBackInit OF((z_streamp strm, int windowBits, + unsigned char FAR *window)); + + Initialize the internal stream state for decompression using inflateBack() + calls. The fields zalloc, zfree and opaque in strm must be initialized + before the call. If zalloc and zfree are Z_NULL, then the default library- + derived memory allocation routines are used. windowBits is the base two + logarithm of the window size, in the range 8..15. window is a caller + supplied buffer of that size. Except for special applications where it is + assured that deflate was used with small window sizes, windowBits must be 15 + and a 32K byte window must be supplied to be able to decompress general + deflate streams. + + See inflateBack() for the usage of these routines. + + inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of + the parameters are invalid, Z_MEM_ERROR if the internal state could not be + allocated, or Z_VERSION_ERROR if the version of the library does not match + the version of the header file. +*/ + +typedef unsigned (*in_func) OF((void FAR *, + z_const unsigned char FAR * FAR *)); +typedef int (*out_func) OF((void FAR *, unsigned char FAR *, unsigned)); + +ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm, + in_func in, void FAR *in_desc, + out_func out, void FAR *out_desc)); +/* + inflateBack() does a raw inflate with a single call using a call-back + interface for input and output. This is potentially more efficient than + inflate() for file i/o applications, in that it avoids copying between the + output and the sliding window by simply making the window itself the output + buffer. inflate() can be faster on modern CPUs when used with large + buffers. inflateBack() trusts the application to not change the output + buffer passed by the output function, at least until inflateBack() returns. + + inflateBackInit() must be called first to allocate the internal state + and to initialize the state with the user-provided window buffer. + inflateBack() may then be used multiple times to inflate a complete, raw + deflate stream with each call. inflateBackEnd() is then called to free the + allocated state. + + A raw deflate stream is one with no zlib or gzip header or trailer. + This routine would normally be used in a utility that reads zip or gzip + files and writes out uncompressed files. The utility would decode the + header and process the trailer on its own, hence this routine expects only + the raw deflate stream to decompress. This is different from the default + behavior of inflate(), which expects a zlib header and trailer around the + deflate stream. + + inflateBack() uses two subroutines supplied by the caller that are then + called by inflateBack() for input and output. inflateBack() calls those + routines until it reads a complete deflate stream and writes out all of the + uncompressed data, or until it encounters an error. The function's + parameters and return types are defined above in the in_func and out_func + typedefs. inflateBack() will call in(in_desc, &buf) which should return the + number of bytes of provided input, and a pointer to that input in buf. If + there is no input available, in() must return zero -- buf is ignored in that + case -- and inflateBack() will return a buffer error. inflateBack() will + call out(out_desc, buf, len) to write the uncompressed data buf[0..len-1]. + out() should return zero on success, or non-zero on failure. If out() + returns non-zero, inflateBack() will return with an error. Neither in() nor + out() are permitted to change the contents of the window provided to + inflateBackInit(), which is also the buffer that out() uses to write from. + The length written by out() will be at most the window size. Any non-zero + amount of input may be provided by in(). + + For convenience, inflateBack() can be provided input on the first call by + setting strm->next_in and strm->avail_in. If that input is exhausted, then + in() will be called. Therefore strm->next_in must be initialized before + calling inflateBack(). If strm->next_in is Z_NULL, then in() will be called + immediately for input. If strm->next_in is not Z_NULL, then strm->avail_in + must also be initialized, and then if strm->avail_in is not zero, input will + initially be taken from strm->next_in[0 .. strm->avail_in - 1]. + + The in_desc and out_desc parameters of inflateBack() is passed as the + first parameter of in() and out() respectively when they are called. These + descriptors can be optionally used to pass any information that the caller- + supplied in() and out() functions need to do their job. + + On return, inflateBack() will set strm->next_in and strm->avail_in to + pass back any unused input that was provided by the last in() call. The + return values of inflateBack() can be Z_STREAM_END on success, Z_BUF_ERROR + if in() or out() returned an error, Z_DATA_ERROR if there was a format error + in the deflate stream (in which case strm->msg is set to indicate the nature + of the error), or Z_STREAM_ERROR if the stream was not properly initialized. + In the case of Z_BUF_ERROR, an input or output error can be distinguished + using strm->next_in which will be Z_NULL only if in() returned an error. If + strm->next_in is not Z_NULL, then the Z_BUF_ERROR was due to out() returning + non-zero. (in() will always be called before out(), so strm->next_in is + assured to be defined if out() returns non-zero.) Note that inflateBack() + cannot return Z_OK. +*/ + +ZEXTERN int ZEXPORT inflateBackEnd OF((z_streamp strm)); +/* + All memory allocated by inflateBackInit() is freed. + + inflateBackEnd() returns Z_OK on success, or Z_STREAM_ERROR if the stream + state was inconsistent. +*/ + +ZEXTERN uLong ZEXPORT zlibCompileFlags OF((void)); +/* Return flags indicating compile-time options. + + Type sizes, two bits each, 00 = 16 bits, 01 = 32, 10 = 64, 11 = other: + 1.0: size of uInt + 3.2: size of uLong + 5.4: size of voidpf (pointer) + 7.6: size of z_off_t + + Compiler, assembler, and debug options: + 8: ZLIB_DEBUG + 9: ASMV or ASMINF -- use ASM code + 10: ZLIB_WINAPI -- exported functions use the WINAPI calling convention + 11: 0 (reserved) + + One-time table building (smaller code, but not thread-safe if true): + 12: BUILDFIXED -- build static block decoding tables when needed + 13: DYNAMIC_CRC_TABLE -- build CRC calculation tables when needed + 14,15: 0 (reserved) + + Library content (indicates missing functionality): + 16: NO_GZCOMPRESS -- gz* functions cannot compress (to avoid linking + deflate code when not needed) + 17: NO_GZIP -- deflate can't write gzip streams, and inflate can't detect + and decode gzip streams (to avoid linking crc code) + 18-19: 0 (reserved) + + Operation variations (changes in library functionality): + 20: PKZIP_BUG_WORKAROUND -- slightly more permissive inflate + 21: FASTEST -- deflate algorithm with only one, lowest compression level + 22,23: 0 (reserved) + + The sprintf variant used by gzprintf (zero is best): + 24: 0 = vs*, 1 = s* -- 1 means limited to 20 arguments after the format + 25: 0 = *nprintf, 1 = *printf -- 1 means gzprintf() not secure! + 26: 0 = returns value, 1 = void -- 1 means inferred string length returned + + Remainder: + 27-31: 0 (reserved) + */ + +#ifndef Z_SOLO + + /* utility functions */ + +/* + The following utility functions are implemented on top of the basic + stream-oriented functions. To simplify the interface, some default options + are assumed (compression level and memory usage, standard memory allocation + functions). The source code of these utility functions can be modified if + you need special options. +*/ + +ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen, + const Bytef *source, uLong sourceLen)); +/* + Compresses the source buffer into the destination buffer. sourceLen is + the byte length of the source buffer. Upon entry, destLen is the total size + of the destination buffer, which must be at least the value returned by + compressBound(sourceLen). Upon exit, destLen is the actual size of the + compressed data. compress() is equivalent to compress2() with a level + parameter of Z_DEFAULT_COMPRESSION. + + compress returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_BUF_ERROR if there was not enough room in the output + buffer. +*/ + +ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen, + const Bytef *source, uLong sourceLen, + int level)); +/* + Compresses the source buffer into the destination buffer. The level + parameter has the same meaning as in deflateInit. sourceLen is the byte + length of the source buffer. Upon entry, destLen is the total size of the + destination buffer, which must be at least the value returned by + compressBound(sourceLen). Upon exit, destLen is the actual size of the + compressed data. + + compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_BUF_ERROR if there was not enough room in the output buffer, + Z_STREAM_ERROR if the level parameter is invalid. +*/ + +ZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen)); +/* + compressBound() returns an upper bound on the compressed size after + compress() or compress2() on sourceLen bytes. It would be used before a + compress() or compress2() call to allocate the destination buffer. +*/ + +ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen, + const Bytef *source, uLong sourceLen)); +/* + Decompresses the source buffer into the destination buffer. sourceLen is + the byte length of the source buffer. Upon entry, destLen is the total size + of the destination buffer, which must be large enough to hold the entire + uncompressed data. (The size of the uncompressed data must have been saved + previously by the compressor and transmitted to the decompressor by some + mechanism outside the scope of this compression library.) Upon exit, destLen + is the actual size of the uncompressed data. + + uncompress returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_BUF_ERROR if there was not enough room in the output + buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete. In + the case where there is not enough room, uncompress() will fill the output + buffer with the uncompressed data up to that point. +*/ + +ZEXTERN int ZEXPORT uncompress2 OF((Bytef *dest, uLongf *destLen, + const Bytef *source, uLong *sourceLen)); +/* + Same as uncompress, except that sourceLen is a pointer, where the + length of the source is *sourceLen. On return, *sourceLen is the number of + source bytes consumed. +*/ + + /* gzip file access functions */ + +/* + This library supports reading and writing files in gzip (.gz) format with + an interface similar to that of stdio, using the functions that start with + "gz". The gzip format is different from the zlib format. gzip is a gzip + wrapper, documented in RFC 1952, wrapped around a deflate stream. +*/ + +typedef struct gzFile_s *gzFile; /* semi-opaque gzip file descriptor */ + +/* +ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode)); + + Opens a gzip (.gz) file for reading or writing. The mode parameter is as + in fopen ("rb" or "wb") but can also include a compression level ("wb9") or + a strategy: 'f' for filtered data as in "wb6f", 'h' for Huffman-only + compression as in "wb1h", 'R' for run-length encoding as in "wb1R", or 'F' + for fixed code compression as in "wb9F". (See the description of + deflateInit2 for more information about the strategy parameter.) 'T' will + request transparent writing or appending with no compression and not using + the gzip format. + + "a" can be used instead of "w" to request that the gzip stream that will + be written be appended to the file. "+" will result in an error, since + reading and writing to the same gzip file is not supported. The addition of + "x" when writing will create the file exclusively, which fails if the file + already exists. On systems that support it, the addition of "e" when + reading or writing will set the flag to close the file on an execve() call. + + These functions, as well as gzip, will read and decode a sequence of gzip + streams in a file. The append function of gzopen() can be used to create + such a file. (Also see gzflush() for another way to do this.) When + appending, gzopen does not test whether the file begins with a gzip stream, + nor does it look for the end of the gzip streams to begin appending. gzopen + will simply append a gzip stream to the existing file. + + gzopen can be used to read a file which is not in gzip format; in this + case gzread will directly read from the file without decompression. When + reading, this will be detected automatically by looking for the magic two- + byte gzip header. + + gzopen returns NULL if the file could not be opened, if there was + insufficient memory to allocate the gzFile state, or if an invalid mode was + specified (an 'r', 'w', or 'a' was not provided, or '+' was provided). + errno can be checked to determine if the reason gzopen failed was that the + file could not be opened. +*/ + +ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode)); +/* + gzdopen associates a gzFile with the file descriptor fd. File descriptors + are obtained from calls like open, dup, creat, pipe or fileno (if the file + has been previously opened with fopen). The mode parameter is as in gzopen. + + The next call of gzclose on the returned gzFile will also close the file + descriptor fd, just like fclose(fdopen(fd, mode)) closes the file descriptor + fd. If you want to keep fd open, use fd = dup(fd_keep); gz = gzdopen(fd, + mode);. The duplicated descriptor should be saved to avoid a leak, since + gzdopen does not close fd if it fails. If you are using fileno() to get the + file descriptor from a FILE *, then you will have to use dup() to avoid + double-close()ing the file descriptor. Both gzclose() and fclose() will + close the associated file descriptor, so they need to have different file + descriptors. + + gzdopen returns NULL if there was insufficient memory to allocate the + gzFile state, if an invalid mode was specified (an 'r', 'w', or 'a' was not + provided, or '+' was provided), or if fd is -1. The file descriptor is not + used until the next gz* read, write, seek, or close operation, so gzdopen + will not detect if fd is invalid (unless fd is -1). +*/ + +ZEXTERN int ZEXPORT gzbuffer OF((gzFile file, unsigned size)); +/* + Set the internal buffer size used by this library's functions. The + default buffer size is 8192 bytes. This function must be called after + gzopen() or gzdopen(), and before any other calls that read or write the + file. The buffer memory allocation is always deferred to the first read or + write. Three times that size in buffer space is allocated. A larger buffer + size of, for example, 64K or 128K bytes will noticeably increase the speed + of decompression (reading). + + The new buffer size also affects the maximum length for gzprintf(). + + gzbuffer() returns 0 on success, or -1 on failure, such as being called + too late. +*/ + +ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy)); +/* + Dynamically update the compression level or strategy. See the description + of deflateInit2 for the meaning of these parameters. Previously provided + data is flushed before the parameter change. + + gzsetparams returns Z_OK if success, Z_STREAM_ERROR if the file was not + opened for writing, Z_ERRNO if there is an error writing the flushed data, + or Z_MEM_ERROR if there is a memory allocation error. +*/ + +ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len)); +/* + Reads the given number of uncompressed bytes from the compressed file. If + the input file is not in gzip format, gzread copies the given number of + bytes into the buffer directly from the file. + + After reaching the end of a gzip stream in the input, gzread will continue + to read, looking for another gzip stream. Any number of gzip streams may be + concatenated in the input file, and will all be decompressed by gzread(). + If something other than a gzip stream is encountered after a gzip stream, + that remaining trailing garbage is ignored (and no error is returned). + + gzread can be used to read a gzip file that is being concurrently written. + Upon reaching the end of the input, gzread will return with the available + data. If the error code returned by gzerror is Z_OK or Z_BUF_ERROR, then + gzclearerr can be used to clear the end of file indicator in order to permit + gzread to be tried again. Z_OK indicates that a gzip stream was completed + on the last gzread. Z_BUF_ERROR indicates that the input file ended in the + middle of a gzip stream. Note that gzread does not return -1 in the event + of an incomplete gzip stream. This error is deferred until gzclose(), which + will return Z_BUF_ERROR if the last gzread ended in the middle of a gzip + stream. Alternatively, gzerror can be used before gzclose to detect this + case. + + gzread returns the number of uncompressed bytes actually read, less than + len for end of file, or -1 for error. If len is too large to fit in an int, + then nothing is read, -1 is returned, and the error state is set to + Z_STREAM_ERROR. +*/ + +ZEXTERN z_size_t ZEXPORT gzfread OF((voidp buf, z_size_t size, z_size_t nitems, + gzFile file)); +/* + Read up to nitems items of size size from file to buf, otherwise operating + as gzread() does. This duplicates the interface of stdio's fread(), with + size_t request and return types. If the library defines size_t, then + z_size_t is identical to size_t. If not, then z_size_t is an unsigned + integer type that can contain a pointer. + + gzfread() returns the number of full items read of size size, or zero if + the end of the file was reached and a full item could not be read, or if + there was an error. gzerror() must be consulted if zero is returned in + order to determine if there was an error. If the multiplication of size and + nitems overflows, i.e. the product does not fit in a z_size_t, then nothing + is read, zero is returned, and the error state is set to Z_STREAM_ERROR. + + In the event that the end of file is reached and only a partial item is + available at the end, i.e. the remaining uncompressed data length is not a + multiple of size, then the final partial item is nevetheless read into buf + and the end-of-file flag is set. The length of the partial item read is not + provided, but could be inferred from the result of gztell(). This behavior + is the same as the behavior of fread() implementations in common libraries, + but it prevents the direct use of gzfread() to read a concurrently written + file, reseting and retrying on end-of-file, when size is not 1. +*/ + +ZEXTERN int ZEXPORT gzwrite OF((gzFile file, + voidpc buf, unsigned len)); +/* + Writes the given number of uncompressed bytes into the compressed file. + gzwrite returns the number of uncompressed bytes written or 0 in case of + error. +*/ + +ZEXTERN z_size_t ZEXPORT gzfwrite OF((voidpc buf, z_size_t size, + z_size_t nitems, gzFile file)); +/* + gzfwrite() writes nitems items of size size from buf to file, duplicating + the interface of stdio's fwrite(), with size_t request and return types. If + the library defines size_t, then z_size_t is identical to size_t. If not, + then z_size_t is an unsigned integer type that can contain a pointer. + + gzfwrite() returns the number of full items written of size size, or zero + if there was an error. If the multiplication of size and nitems overflows, + i.e. the product does not fit in a z_size_t, then nothing is written, zero + is returned, and the error state is set to Z_STREAM_ERROR. +*/ + +ZEXTERN int ZEXPORTVA gzprintf Z_ARG((gzFile file, const char *format, ...)); +/* + Converts, formats, and writes the arguments to the compressed file under + control of the format string, as in fprintf. gzprintf returns the number of + uncompressed bytes actually written, or a negative zlib error code in case + of error. The number of uncompressed bytes written is limited to 8191, or + one less than the buffer size given to gzbuffer(). The caller should assure + that this limit is not exceeded. If it is exceeded, then gzprintf() will + return an error (0) with nothing written. In this case, there may also be a + buffer overflow with unpredictable consequences, which is possible only if + zlib was compiled with the insecure functions sprintf() or vsprintf() + because the secure snprintf() or vsnprintf() functions were not available. + This can be determined using zlibCompileFlags(). +*/ + +ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s)); +/* + Writes the given null-terminated string to the compressed file, excluding + the terminating null character. + + gzputs returns the number of characters written, or -1 in case of error. +*/ + +ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len)); +/* + Reads bytes from the compressed file until len-1 characters are read, or a + newline character is read and transferred to buf, or an end-of-file + condition is encountered. If any characters are read or if len == 1, the + string is terminated with a null character. If no characters are read due + to an end-of-file or len < 1, then the buffer is left untouched. + + gzgets returns buf which is a null-terminated string, or it returns NULL + for end-of-file or in case of error. If there was an error, the contents at + buf are indeterminate. +*/ + +ZEXTERN int ZEXPORT gzputc OF((gzFile file, int c)); +/* + Writes c, converted to an unsigned char, into the compressed file. gzputc + returns the value that was written, or -1 in case of error. +*/ + +ZEXTERN int ZEXPORT gzgetc OF((gzFile file)); +/* + Reads one byte from the compressed file. gzgetc returns this byte or -1 + in case of end of file or error. This is implemented as a macro for speed. + As such, it does not do all of the checking the other functions do. I.e. + it does not check to see if file is NULL, nor whether the structure file + points to has been clobbered or not. +*/ + +ZEXTERN int ZEXPORT gzungetc OF((int c, gzFile file)); +/* + Push one character back onto the stream to be read as the first character + on the next read. At least one character of push-back is allowed. + gzungetc() returns the character pushed, or -1 on failure. gzungetc() will + fail if c is -1, and may fail if a character has been pushed but not read + yet. If gzungetc is used immediately after gzopen or gzdopen, at least the + output buffer size of pushed characters is allowed. (See gzbuffer above.) + The pushed character will be discarded if the stream is repositioned with + gzseek() or gzrewind(). +*/ + +ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush)); +/* + Flushes all pending output into the compressed file. The parameter flush + is as in the deflate() function. The return value is the zlib error number + (see function gzerror below). gzflush is only permitted when writing. + + If the flush parameter is Z_FINISH, the remaining data is written and the + gzip stream is completed in the output. If gzwrite() is called again, a new + gzip stream will be started in the output. gzread() is able to read such + concatenated gzip streams. + + gzflush should be called only when strictly necessary because it will + degrade compression if called too often. +*/ + +/* +ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file, + z_off_t offset, int whence)); + + Sets the starting position for the next gzread or gzwrite on the given + compressed file. The offset represents a number of bytes in the + uncompressed data stream. The whence parameter is defined as in lseek(2); + the value SEEK_END is not supported. + + If the file is opened for reading, this function is emulated but can be + extremely slow. If the file is opened for writing, only forward seeks are + supported; gzseek then compresses a sequence of zeroes up to the new + starting position. + + gzseek returns the resulting offset location as measured in bytes from + the beginning of the uncompressed stream, or -1 in case of error, in + particular if the file is opened for writing and the new starting position + would be before the current position. +*/ + +ZEXTERN int ZEXPORT gzrewind OF((gzFile file)); +/* + Rewinds the given file. This function is supported only for reading. + + gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET) +*/ + +/* +ZEXTERN z_off_t ZEXPORT gztell OF((gzFile file)); + + Returns the starting position for the next gzread or gzwrite on the given + compressed file. This position represents a number of bytes in the + uncompressed data stream, and is zero when starting, even if appending or + reading a gzip stream from the middle of a file using gzdopen(). + + gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR) +*/ + +/* +ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile file)); + + Returns the current offset in the file being read or written. This offset + includes the count of bytes that precede the gzip stream, for example when + appending or when using gzdopen() for reading. When reading, the offset + does not include as yet unused buffered input. This information can be used + for a progress indicator. On error, gzoffset() returns -1. +*/ + +ZEXTERN int ZEXPORT gzeof OF((gzFile file)); +/* + Returns true (1) if the end-of-file indicator has been set while reading, + false (0) otherwise. Note that the end-of-file indicator is set only if the + read tried to go past the end of the input, but came up short. Therefore, + just like feof(), gzeof() may return false even if there is no more data to + read, in the event that the last read request was for the exact number of + bytes remaining in the input file. This will happen if the input file size + is an exact multiple of the buffer size. + + If gzeof() returns true, then the read functions will return no more data, + unless the end-of-file indicator is reset by gzclearerr() and the input file + has grown since the previous end of file was detected. +*/ + +ZEXTERN int ZEXPORT gzdirect OF((gzFile file)); +/* + Returns true (1) if file is being copied directly while reading, or false + (0) if file is a gzip stream being decompressed. + + If the input file is empty, gzdirect() will return true, since the input + does not contain a gzip stream. + + If gzdirect() is used immediately after gzopen() or gzdopen() it will + cause buffers to be allocated to allow reading the file to determine if it + is a gzip file. Therefore if gzbuffer() is used, it should be called before + gzdirect(). + + When writing, gzdirect() returns true (1) if transparent writing was + requested ("wT" for the gzopen() mode), or false (0) otherwise. (Note: + gzdirect() is not needed when writing. Transparent writing must be + explicitly requested, so the application already knows the answer. When + linking statically, using gzdirect() will include all of the zlib code for + gzip file reading and decompression, which may not be desired.) +*/ + +ZEXTERN int ZEXPORT gzclose OF((gzFile file)); +/* + Flushes all pending output if necessary, closes the compressed file and + deallocates the (de)compression state. Note that once file is closed, you + cannot call gzerror with file, since its structures have been deallocated. + gzclose must not be called more than once on the same file, just as free + must not be called more than once on the same allocation. + + gzclose will return Z_STREAM_ERROR if file is not valid, Z_ERRNO on a + file operation error, Z_MEM_ERROR if out of memory, Z_BUF_ERROR if the + last read ended in the middle of a gzip stream, or Z_OK on success. +*/ + +ZEXTERN int ZEXPORT gzclose_r OF((gzFile file)); +ZEXTERN int ZEXPORT gzclose_w OF((gzFile file)); +/* + Same as gzclose(), but gzclose_r() is only for use when reading, and + gzclose_w() is only for use when writing or appending. The advantage to + using these instead of gzclose() is that they avoid linking in zlib + compression or decompression code that is not used when only reading or only + writing respectively. If gzclose() is used, then both compression and + decompression code will be included the application when linking to a static + zlib library. +*/ + +ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum)); +/* + Returns the error message for the last error which occurred on the given + compressed file. errnum is set to zlib error number. If an error occurred + in the file system and not in the compression library, errnum is set to + Z_ERRNO and the application may consult errno to get the exact error code. + + The application must not modify the returned string. Future calls to + this function may invalidate the previously returned string. If file is + closed, then the string previously returned by gzerror will no longer be + available. + + gzerror() should be used to distinguish errors from end-of-file for those + functions above that do not distinguish those cases in their return values. +*/ + +ZEXTERN void ZEXPORT gzclearerr OF((gzFile file)); +/* + Clears the error and end-of-file flags for file. This is analogous to the + clearerr() function in stdio. This is useful for continuing to read a gzip + file that is being written concurrently. +*/ + +#endif /* !Z_SOLO */ + + /* checksum functions */ + +/* + These functions are not related to compression but are exported + anyway because they might be useful in applications using the compression + library. +*/ + +ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len)); +/* + Update a running Adler-32 checksum with the bytes buf[0..len-1] and + return the updated checksum. If buf is Z_NULL, this function returns the + required initial value for the checksum. + + An Adler-32 checksum is almost as reliable as a CRC-32 but can be computed + much faster. + + Usage example: + + uLong adler = adler32(0L, Z_NULL, 0); + + while (read_buffer(buffer, length) != EOF) { + adler = adler32(adler, buffer, length); + } + if (adler != original_adler) error(); +*/ + +ZEXTERN uLong ZEXPORT adler32_z OF((uLong adler, const Bytef *buf, + z_size_t len)); +/* + Same as adler32(), but with a size_t length. +*/ + +/* +ZEXTERN uLong ZEXPORT adler32_combine OF((uLong adler1, uLong adler2, + z_off_t len2)); + + Combine two Adler-32 checksums into one. For two sequences of bytes, seq1 + and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for + each, adler1 and adler2. adler32_combine() returns the Adler-32 checksum of + seq1 and seq2 concatenated, requiring only adler1, adler2, and len2. Note + that the z_off_t type (like off_t) is a signed integer. If len2 is + negative, the result has no meaning or utility. +*/ + +ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len)); +/* + Update a running CRC-32 with the bytes buf[0..len-1] and return the + updated CRC-32. If buf is Z_NULL, this function returns the required + initial value for the crc. Pre- and post-conditioning (one's complement) is + performed within this function so it shouldn't be done by the application. + + Usage example: + + uLong crc = crc32(0L, Z_NULL, 0); + + while (read_buffer(buffer, length) != EOF) { + crc = crc32(crc, buffer, length); + } + if (crc != original_crc) error(); +*/ + +ZEXTERN uLong ZEXPORT crc32_z OF((uLong adler, const Bytef *buf, + z_size_t len)); +/* + Same as crc32(), but with a size_t length. +*/ + +/* +ZEXTERN uLong ZEXPORT crc32_combine OF((uLong crc1, uLong crc2, z_off_t len2)); + + Combine two CRC-32 check values into one. For two sequences of bytes, + seq1 and seq2 with lengths len1 and len2, CRC-32 check values were + calculated for each, crc1 and crc2. crc32_combine() returns the CRC-32 + check value of seq1 and seq2 concatenated, requiring only crc1, crc2, and + len2. +*/ + + + /* various hacks, don't look :) */ + +/* deflateInit and inflateInit are macros to allow checking the zlib version + * and the compiler's view of z_stream: + */ +ZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level, + const char *version, int stream_size)); +ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm, + const char *version, int stream_size)); +ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int level, int method, + int windowBits, int memLevel, + int strategy, const char *version, + int stream_size)); +ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int windowBits, + const char *version, int stream_size)); +ZEXTERN int ZEXPORT inflateBackInit_ OF((z_streamp strm, int windowBits, + unsigned char FAR *window, + const char *version, + int stream_size)); +#ifdef Z_PREFIX_SET +# define z_deflateInit(strm, level) \ + deflateInit_((strm), (level), ZLIB_VERSION, (int)sizeof(z_stream)) +# define z_inflateInit(strm) \ + inflateInit_((strm), ZLIB_VERSION, (int)sizeof(z_stream)) +# define z_deflateInit2(strm, level, method, windowBits, memLevel, strategy) \ + deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\ + (strategy), ZLIB_VERSION, (int)sizeof(z_stream)) +# define z_inflateInit2(strm, windowBits) \ + inflateInit2_((strm), (windowBits), ZLIB_VERSION, \ + (int)sizeof(z_stream)) +# define z_inflateBackInit(strm, windowBits, window) \ + inflateBackInit_((strm), (windowBits), (window), \ + ZLIB_VERSION, (int)sizeof(z_stream)) +#else +# define deflateInit(strm, level) \ + deflateInit_((strm), (level), ZLIB_VERSION, (int)sizeof(z_stream)) +# define inflateInit(strm) \ + inflateInit_((strm), ZLIB_VERSION, (int)sizeof(z_stream)) +# define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \ + deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\ + (strategy), ZLIB_VERSION, (int)sizeof(z_stream)) +# define inflateInit2(strm, windowBits) \ + inflateInit2_((strm), (windowBits), ZLIB_VERSION, \ + (int)sizeof(z_stream)) +# define inflateBackInit(strm, windowBits, window) \ + inflateBackInit_((strm), (windowBits), (window), \ + ZLIB_VERSION, (int)sizeof(z_stream)) +#endif + +#ifndef Z_SOLO + +/* gzgetc() macro and its supporting function and exposed data structure. Note + * that the real internal state is much larger than the exposed structure. + * This abbreviated structure exposes just enough for the gzgetc() macro. The + * user should not mess with these exposed elements, since their names or + * behavior could change in the future, perhaps even capriciously. They can + * only be used by the gzgetc() macro. You have been warned. + */ +struct gzFile_s { + unsigned have; + unsigned char *next; + z_off64_t pos; +}; +ZEXTERN int ZEXPORT gzgetc_ OF((gzFile file)); /* backward compatibility */ +#ifdef Z_PREFIX_SET +# undef z_gzgetc +# define z_gzgetc(g) \ + ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : (gzgetc)(g)) +#else +# define gzgetc(g) \ + ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : (gzgetc)(g)) +#endif + +/* provide 64-bit offset functions if _LARGEFILE64_SOURCE defined, and/or + * change the regular functions to 64 bits if _FILE_OFFSET_BITS is 64 (if + * both are true, the application gets the *64 functions, and the regular + * functions are changed to 64 bits) -- in case these are set on systems + * without large file support, _LFS64_LARGEFILE must also be true + */ +#ifdef Z_LARGE64 + ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *)); + ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int)); + ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile)); + ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile)); + ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off64_t)); + ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off64_t)); +#endif + +#if !defined(ZLIB_INTERNAL) && defined(Z_WANT64) +# ifdef Z_PREFIX_SET +# define z_gzopen z_gzopen64 +# define z_gzseek z_gzseek64 +# define z_gztell z_gztell64 +# define z_gzoffset z_gzoffset64 +# define z_adler32_combine z_adler32_combine64 +# define z_crc32_combine z_crc32_combine64 +# else +# define gzopen gzopen64 +# define gzseek gzseek64 +# define gztell gztell64 +# define gzoffset gzoffset64 +# define adler32_combine adler32_combine64 +# define crc32_combine crc32_combine64 +# endif +# ifndef Z_LARGE64 + ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *)); + ZEXTERN z_off_t ZEXPORT gzseek64 OF((gzFile, z_off_t, int)); + ZEXTERN z_off_t ZEXPORT gztell64 OF((gzFile)); + ZEXTERN z_off_t ZEXPORT gzoffset64 OF((gzFile)); + ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t)); + ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t)); +# endif +#else + ZEXTERN gzFile ZEXPORT gzopen OF((const char *, const char *)); + ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile, z_off_t, int)); + ZEXTERN z_off_t ZEXPORT gztell OF((gzFile)); + ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile)); + ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t)); + ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t)); +#endif + +#else /* Z_SOLO */ + + ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t)); + ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t)); + +#endif /* !Z_SOLO */ + +/* undocumented functions */ +ZEXTERN const char * ZEXPORT zError OF((int)); +ZEXTERN int ZEXPORT inflateSyncPoint OF((z_streamp)); +ZEXTERN const z_crc_t FAR * ZEXPORT get_crc_table OF((void)); +ZEXTERN int ZEXPORT inflateUndermine OF((z_streamp, int)); +ZEXTERN int ZEXPORT inflateValidate OF((z_streamp, int)); +ZEXTERN unsigned long ZEXPORT inflateCodesUsed OF ((z_streamp)); +ZEXTERN int ZEXPORT inflateResetKeep OF((z_streamp)); +ZEXTERN int ZEXPORT deflateResetKeep OF((z_streamp)); +#if (defined(_WIN32) || defined(__CYGWIN__)) && !defined(Z_SOLO) +ZEXTERN gzFile ZEXPORT gzopen_w OF((const wchar_t *path, + const char *mode)); +#endif +#if defined(STDC) || defined(Z_HAVE_STDARG_H) +# ifndef Z_SOLO +ZEXTERN int ZEXPORTVA gzvprintf Z_ARG((gzFile file, + const char *format, + va_list va)); +# endif +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* ZLIB_H */