Red5 Source Analysis-Client publish stream
Then the analysis of the previous chapter, referring to the analysis of "Red5 Source Analysis 7" conclusion, when the server returns STEAMID, the client will execute Basertmpclienthandler oncommand function, OnCommand function According to the returned method name "_ Result "starts executing the Handlependingcallresult function, Handlependingcallresult will get the previously registered callback function, according to the Red5 source code Analysis 7", The callback function is CreateStreamCallback, and after getting the callback function, it executes the resultreceived function of the callback function.
public void resultreceived (IpendingservicecallPager) {Number Streamid = (number)Pager. GetResult();if (conn = null && Streamid! = null) {NetStream stream = new NetStream (streameventdispatcher);Stream. SetConnection(conn);Stream. Setstreamid(Streamid);Conn. Addclientstream(stream);Netstreamprivatedata streamdata = new Netstreamprivatedata ();Streamdata. OutputStream= conn. Createoutputstream(Streamid);Streamdata. Connconsumer= new ConnectionConsumer (conn, streamdata. OutputStream. Getvideo(), Streamdata. OutputStream. Getaudio(), Streamdata. OutputStream. GetData());Streamdatamap. Put(Streamid, Streamdata);} Wrapped. resultreceived(Pager);}
This first takes out the server-assigned Streamid, constructs a netstream and sets the corresponding settings, NetStream to handle the later events about the flow, which is analyzed later. Next, set the NetStream that you just created by Addclientstream to rtmpminaconnection, defined in its parent class rtmpconnection,
publicvoidaddClientStream(IClientStream stream) { if (reservedStreams.add(stream.getStreamId().doubleValue())) { registerStream(stream); else { } }
Addclientstream first registers the Streamid with Reservedstreams and then registers with Registerstream,
privatebooleanregisterStream(IClientStream stream) { ifnull) { usedStreams.incrementAndGet(); returntrue; } returnfalse; }
This is where the stream is registered according to Streamid.
Go back to resultreceived, then construct Netstreamprivatedata, then create the output stream through the Createoutputstream function,
publiccreateOutputStream(Number streamId) { int channelId = getChannelIdForStreamId(streamId); final Channel data = getChannel(channelId++); final Channel video = getChannel(channelId++); final Channel audio = getChannel(channelId++); returnnew OutputStream(video, audio, data); }
This is the construction of three channel, respectively, to send data, audio and video, and then construct OutputStream.
Then go back to resultreceived, create the ConnectionConsumer, and set it into Streamdatamap through Streamid, and finally execute another callback function resultreceived,
Public voidResultreceived (Ipendingservicecall call) {Object result=Pager.GetResult ();if(Result instanceof Objectmap) {if("Connect".equals(Call.Getservicemethodname ())) {CreateStream (this); } }Else{if("CreateStream".equals(Call.Getservicemethodname ())) {if(Result instanceofInteger) {IntegerStreamidint=(Integer) result; int Streamid=Streamidint.Intvalue (); Publish (Streamid,"Testgio2","Live", this);//invoke ("Getroomsinfo", this); }Else{Disconnect (); } }Else if("Getroomsinfo".equals(Call.Getservicemethodname ())) {ArrayList<String> List =(ArrayList<String>) result; for (int i= 0; I< List.Size (); I++) {System.Out.printlnList.Get (i)); } } } }
Compared to the previous chapters, here the createstream corresponding to the Resultreceived method part of the corresponding rewrite, annotated "Getroomsinfo" part.
Publish defined in Basertmpclienthandler,
Public void Publish(number Streamid, string name, string mode, Inetstreameventhandler handler) {if(Handler! =NULL) {Netstreamprivatedata Streamdata = Streamdatamap.Get(Streamid);if(Streamdata! =NULL) {Streamdata.handler = handler; }Else{}} final object[]params=Newobject[2];params[0] = name;params[1] = mode; Pendingcall Pendingcall =NewPendingcall ("Publish",params); Conn.invoke (Pendingcall, Getchannelforstreamid (Streamid)); }
This first set up the event listener, and then constructs the Pendingcall to send the server, and sends the command "publish", the parameters are name and mode respectively.
The next chapter begins the analysis of how the server handles the Publish command.
Red5 Source Analysis---9