Search code examples
pythonopencvgstreamerrtmpazure-media-services

Gstreamer rtmpsink to Azure Media Services live pass through event


I am trying to use an on-prem gstreamer encoder pipeline to broadcast live video into Azure Media Services.

Testing the pipe using the videotestsrc seems to be working fine with the folowing string:

gst-launch-1.0 -e videotestsrc ! videoconvert ! video/x-raw,format=I420 ! x264enc tune=zerolatency byte-stream=true threads=4 ! flvmux streamable=true ! rtmpsink location='rtmp://xxxx.media.azure.net:1935/live/xxxx/mystream live=true flashver=FMLE/3.0(compatible;FMSc/1.0)'

And I'm able to watch the preview of the stream in the Azure AMS dashboard.

Now, if I try to use appsrc pipe from my python script using OpenCV (compiled with gstreamer support), nothing is showing in the preview window. However an asset is being created for the stream, and I am able to view this asset streaming through the AMS services.

The following python3 script is using a custom build of OpenCV version 4.0.0 with gstreamer and cuda compiled.

import sys
import time
import urllib
import cv2
import numpy as np
from datetime import datetime


TEST_CARD = 'https://upload.wikimedia.org/wikipedia/commons/c/c4/PM5544_with_non-PAL_signals.png'
HEADLINE = 'AZURE LIVE STREAM'

RTMP_SERVER = 'rtmp://xxxx.media.azure.net:1935/live/xxxx/mystream'
GST_PIPE = "appsrc ! videoconvert ! video/x-raw,format=I420 ! x264enc tune=zerolatency byte-stream=true threads=4 ! flvmux streamable=true ! rtmpsink location='{0} live=true flashver=FMLE/3.0(compatible;FMSc/1.0)' ".format(RTMP_SERVER)

if __name__ == '__main__':
    print ('Azure Mediastream tester')
    print(sys.version)
    print (cv2.getBuildInformation())

    imgRequest = urllib.request.urlopen(TEST_CARD)
    imgArray = np.asarray(bytearray(imgRequest.read()), dtype=np.uint8)
    imgO = cv2.imdecode(imgArray, -1)

    h,w,c = imgO.shape
    font = cv2.FONT_HERSHEY_PLAIN
    line = cv2.LINE_AA
    cv2.putText(imgO,HEADLINE,(302,85),font,1,(255,255,255),2,line)

    print(HEADLINE)
    print ('Showing: {0} at [h:{1},w:{2},c:{3}]'.format(TEST_CARD,h,w,c))
    print ('Opening GSTREAM {0}'.format(GST_PIPE))

    try:
        fcc = cv2.VideoWriter.fourcc ('X','2','6','4')
        stream = cv2.VideoWriter(GST_PIPE,fcc,25.0,(w,h))
        while True:
            currentTime = datetime.now()
            img = imgO.copy()
            cv2.putText(img,str(currentTime),(283,460),font,1,(255,255,255),1,line)

            stream.write(img)
            cv2.imshow(HEADLINE,img)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
    finally:
        stream.release()
        cv2.destroyAllWindows()
        print ('DONE')

What am I missing here?


Solution

  • So, after an extensive dialog with Azure Media Services team from Microsoft, it turns out that the Azure Media Player, requires a sound track to be present for it to be able to play back.

    Changing the GST_PIPE to:

    GST_PIPE = "appsrc ! videoconvert ! video/x-raw,format=I420 ! x264enc tune=zerolatency byte-stream=true threads=4 ! flvmux name=mux ! rtmpsink location='{0} live=true flashver=FMLE/3.0(compatibble;FMSc/1.0)' audiotestsrc is-live=true ! audioconvert ! audioresample ! audio/x-raw,rate=48000 ! voaacenc bitrate=96000 ! audio/mpeg ! aacparse ! audio/mpeg, mpegversion=4 ! mux.".format(RTMP_SERVER)
    

    In my Python code, made everything work as expected (except the channel preview panel in the Azure portal (still don't know what's going on there)).