C#推流RTMP,摄像头、麦克风、桌面、声卡(附源码)

技术杂谈57
*[En]*

**

这个demo的主要功能就是将采集到的摄像头或桌面的视频、以及麦克风或声卡的音频数据推到Nginx-RTMP服务器上,再由Web浏览器去拉流并播放。

接下来介绍Demo整个功能的实现原理和代码逻辑,大家可以从文末下载源码后,对照源码再来看下面的介绍就会更清晰些。

一.客户端实现

客户端的界面效果图如下所示:

C#推流RTMP,摄像头、麦克风、桌面、声卡(附源码)

客户端的具体功能:可以采集摄像头或者桌面图像,也可以采集麦克风与声卡的声音 并将它们推送到Nginx流服务器上。

*[En]*

**

*[En]*

**

#region 设置采集器
        if (this.radioButton_desktop.Checked)
        {
            //桌面采集器
            //如果需要录制鼠标的操作,第二个参数请设置为true
            this.desktopCapturer = CapturerFactory.CreateDesktopCapturer(frameRate, false,new Rectangle(0,0,1920,1080));
            this.desktopCapturer.ImageCaptured += this.Form1_ImageCaptured;

        }
        else if (this.radioButton_camera.Checked)
        {
            //摄像头采集器
            this.cameraCapturer = CapturerFactory.CreateCameraCapturer(0, this.defaultVideoSize, frameRate);
            this.cameraCapturer.ImageCaptured += new CbGeneric(this.Form1_ImageCaptured);
        }

        if (this.checkBox_micro.Checked)
        {
            //麦克风采集器
            this.microphoneCapturer = CapturerFactory.CreateMicrophoneCapturer(0);
            this.microphoneCapturer.CaptureError += new CbGeneric(this.CaptureError);
        }

        if (this.checkBox_soundCard.Checked)
        {
            //声卡采集器 【目前声卡采集仅支持vista以及以上系统】扬声器 属性 高级设置 16位 48000HZ(DVD音质)
            this.soundcardCapturer = CapturerFactory.CreateSoundcardCapturer();
            this.soundcardCapturer.CaptureError += this.CaptureError;
            if (this.soundcardCapturer.SampleRate != 48000)
            {
                throw new Exception("声卡采样率必须为48000HZ");
            }
            audioSampleRate = this.soundcardCapturer.SampleRate;
            this.channelCount = this.soundcardCapturer.ChannelCount;
        }

        if (this.checkBox_micro.Checked && this.checkBox_soundCard.Checked)
        {
            //混音器
            this.audioMixter = CapturerFactory.CreateAudioMixter(this.microphoneCapturer, this.soundcardCapturer,
                SoundcardMode4Mix.DoubleChannel, true);
            this.audioMixter.AudioMixed += audioMixter_AudioMixed;
            audioSampleRate = this.audioMixter.SampleRate;
            this.channelCount = this.audioMixter.ChannelCount;
        }

        else if (this.checkBox_micro.Checked)
        {
            this.microphoneCapturer.AudioCaptured += audioMixter_AudioMixed;
        }
        else if (this.checkBox_soundCard.Checked)
        {
            this.soundcardCapturer.AudioCaptured += audioMixter_AudioMixed;
        }
        #endregion

        #region //开始采集
        if (this.checkBox_micro.Checked)
        {
            this.microphoneCapturer.Start();
        }
        if (this.checkBox_soundCard.Checked)
        {
            this.soundcardCapturer.Start();
        }

        if (this.radioButton_camera.Checked)
        {
            this.cameraCapturer.Start();
        }
        else if (this.radioButton_desktop.Checked)
        {
            this.desktopCapturer.Start();
        }
        #endregion
*[En]*

**

//TODO 开始录制桌面,依据 声音复选框 来选择使用 声卡 麦克风 还是混合录制, 图像复选框来选择 图像的采集器
        try
        {
            int videoWidth = 0, videoHeight = 0;
            if (this.radioButton_desktop.Checked)
            {
                videoWidth = this.desktopCapturer.VideoSize.Width;
                videoHeight = this.desktopCapturer.VideoSize.Height;
            }
            else {
                videoWidth = this.defaultVideoSize.Width;
                videoHeight = this.defaultVideoSize.Height;
            }
            this.streamPusher.UpsideDown4RGB24 = true;
            this.streamPusher.Initialize("192.168.1.56", 9000, true, this.streamID, videoWidth, videoHeight, NPusher.InputAudioDataType.PCM, NPusher.InputVideoDataType.RGB24,this.channelCount);
            this.isPushing = true;
            this.button_start.Enabled = false;
            this.button_stop.Enabled = true;
            this.button3.Enabled = false;
            this.ShowStateMsg("推流中...");
        }
        catch (Exception ee)
        {
            MessageBox.Show(ee.Message);
        }

上述代码中红色标记部分,即是初始化推流器:由于我们采集到的视频是H264数据,声音是PCM数据,所以,在初始化时,选择InputAudioDataType.PCM和InputVideoDataType.RGB24。

在采集时我们预定了对应的采集事件,采集到数据后我们就加到推流器中,它会自动将数据推到我们的Nginx服务器上:

//采集到的视频或桌面图像
        void Form1_ImageCaptured(Bitmap img)
        {
            if (this.radioButton_camera.Checked)//显示摄像头的图像到窗体
            {
                Image copy = ESBasic.Helpers.ImageHelper.CopyImageDeeply(img);
                this.DisplayVideo(copy);
            }
            if (this.isPushing)
            {
                img.RotateFlip(RotateFlipType.Rotate180FlipY);
                byte[] data = ESBasic.Helpers.ImageHelper.GetRGB24CoreData(img);
                this.streamPusher.PushVideoFrame(data);
            }
        }

        //采集到的声卡、麦克风、声卡麦克风的混音数据
        void audioMixter_AudioMixed(byte[] audioData)
        {
            if (this.isPushing)
            {
                if (this.checkBox_soundCard.Checked && !this.checkBox_micro.Checked)
                {
                    audioData = AudioHelper.ConvertTo16kFrom48k(audioData ,this.channelCount);
                }
                this.streamPusher.PushAudioFrame(audioData);
            }
        }

代码中标记为红色的部分PushVideoFrame和PushAudioFrame方法,即是将采集到的视频帧和音频帧推流到流服务器。

二.Nginx服务端部署

这里可以在文末网盘下载服务端来部署到服务器上,其中有3个地方需要根据服务器的配置自行做修改

  1. conf目录下nginx.conf 文件中 rtmp 端口 9000、http 端口8000 。
  2. html目录下index.html 文件中 设置流服务器的IP
src: "rtmp://192.168.1.56:9000/hls/"+pqs._parameters.id[0],    //将192.168.1.56改成流服务器的IP
  1. html目录下mobile.html 文件中 也同样设置流服务器的IP
var hls_url = "http://192.168.1.56:8000/hls/" + pqs._parameters.id[0] + ".m3u8"; //将192.168.1.56改成流服务器的IP

三.浏览器访问

PC的浏览器访问 http://192.168.1.56:8000/?id=aa01,其中aa01为流的ID。效果如下图

C#推流RTMP,摄像头、麦克风、桌面、声卡(附源码)

手机浏览器访问 http://192.168.1.56:8000/mobile.html?id=aa01,其中aa01为流的ID。效果如下图

C#推流RTMP,摄像头、麦克风、桌面、声卡(附源码)

四.源码下载

(1) C#推流RTMP(摄像头、麦克风、桌面、声卡)-源码

(2) Nginx部署版下载 网盘下载 (提取码: 1234)

注:查看Nginx运行状态可访问: http://192.168.1.56:8000/stat 。

Original: https://www.cnblogs.com/zhuweisky/p/14024966.html
Author: zhuweisky
Title: C#推流RTMP,摄像头、麦克风、桌面、声卡(附源码)



相关阅读

Title: []总结渠道转化质量要看哪些指标

[原创]总结渠道转化质量要看哪些指标

渠道转化质量指标

1 渠道指标

  • 广告展示量
  • 广告点击率

2 落地页指标

  • 访问量
  • 转化率
  • 跳出率
  • 下载量
  • 下载率
  • 首次打开率

3 产品内转化指标

  • 浏览时长
  • 访问页面
  • 搜索行为
  • 注册率
  • 激活率
  • 购买率

4 长期转化指标

  • 留存率
  • 复购率
  • LTV

5 渠道用户质量指标

  • 用户规模
  • 激活用户数
  • 用户质量
  • 新用户使用时长
  • 新用户使用次数
  • 新用户次日留存
  • 用户目标转化事件
  • 完成x行为总人数
  • 完成x行为新用户占比数

6 部分常见提标:

UV成本: 投放价格/投放UV

注册成本: 投放价格/注册数

UV到注册转化率=注册数/投放落地页UV

单个订单成本=投放价格/订单数销售额=订单数*客单价

UV到订单的转化率=订单数/投放落地页UV

注册到订单的转化率=订单数/注册UV ROl=销售额/投放价格

付费投放评估模型:

ROI=收入/成本

展现量

点击量

访问量

咨询量

订单量

Original: https://www.cnblogs.com/mayingbao/p/15210577.html
Author: 卖烧烤的鱼
Title: []总结渠道转化质量要看哪些指标