Skip to content

Commit

Permalink
SDK: fix unsubcribe.
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwebrtc committed Mar 9, 2020
1 parent ada1644 commit f918725
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 51 deletions.
14 changes: 2 additions & 12 deletions pkg/node/biz/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,7 @@ func unsubscribe(peer *signal.Peer, msg map[string]interface{}) (map[string]inte
}
mid := util.Val(msg, "mid")

mediaInfo, perr := proto.ParseMediaInfo(mid)
if perr != nil {
return nil, util.NewNpError(500, perr.Error())
}

_, sfu, err := getRPCForSFU(mediaInfo.NID)
_, sfu, err := getRPCForSFU(mid)
if err != nil {
log.Warnf("Not found any sfu node, reject: %d => %s", err.Code, err.Reason)
return nil, util.NewNpError(err.Code, err.Reason)
Expand Down Expand Up @@ -353,12 +348,7 @@ func trickle(peer *signal.Peer, msg map[string]interface{}) (map[string]interfac
return nil, err
}

mediaInfo, perr := proto.ParseMediaInfo(mid)
if perr != nil {
return nil, util.NewNpError(500, perr.Error())
}

_, sfu, err := getRPCForSFU(mediaInfo.NID)
_, sfu, err := getRPCForSFU(mid)
if err != nil {
log.Warnf("Not found any sfu node, reject: %d => %s", err.Code, err.Reason)
return nil, util.NewNpError(err.Code, err.Reason)
Expand Down
16 changes: 8 additions & 8 deletions pkg/node/sfu/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func publish(msg map[string]interface{}) (map[string]interface{}, *nprotoo.Error
log.Infof("publish msg=%v", msg)
jsep := msg["jsep"].(map[string]interface{})
if jsep == nil {
return nil, util.NewNpError(415, "Unsupported Media Type")
return nil, util.NewNpError(415, "publish: jsep invaild.")
}
sdp := util.Val(jsep, "sdp")
uid := util.Val(msg, "uid")
Expand All @@ -88,15 +88,15 @@ func publish(msg map[string]interface{}) (map[string]interface{}, *nprotoo.Error
answer, err := pub.Answer(offer, rtcOptions)
if err != nil {
log.Errorf("err=%v answer=%v", err, answer)
return nil, util.NewNpError(415, "Unsupported Media Type")
return nil, util.NewNpError(415, "publish: pub.Answer failed.")
}

router.AddPub(uid, pub)

sdpObj, err := sdptransform.Parse(offer.SDP)
if err != nil {
log.Errorf("err=%v sdpObj=%v", err, sdpObj)
return nil, util.NewNpError(415, "Unsupported Media Type")
return nil, util.NewNpError(415, "publish: sdp parse failed.")
}

tracks := make(map[string][]proto.TrackInfo)
Expand Down Expand Up @@ -140,7 +140,7 @@ func unpublish(msg map[string]interface{}) (map[string]interface{}, *nprotoo.Err
rtc.DelRouter(mid)
return emptyMap, nil
}
return nil, util.NewNpError(404, "Router not found!")
return nil, util.NewNpError(404, "unpublish: Router not found!")
}

// subscribe .
Expand All @@ -150,12 +150,12 @@ func subscribe(msg map[string]interface{}) (map[string]interface{}, *nprotoo.Err
pubID := util.Val(msg, "mid")
router := rtc.GetOrNewRouter(pubID)
if router == nil {
return nil, util.NewNpError(404, "Router not found!")
return nil, util.NewNpError(404, "subscribe: Router not found!")
}

jsep := msg["jsep"].(map[string]interface{})
if jsep == nil {
return nil, util.NewNpError(415, "Unsupported Media Type")
return nil, util.NewNpError(415, "subscribe: Unsupported Media Type")
}

sdp := util.Val(jsep, "sdp")
Expand Down Expand Up @@ -241,7 +241,7 @@ func unsubscribe(msg map[string]interface{}) (map[string]interface{}, *nprotoo.E
if found {
return emptyMap, nil
}
return nil, util.NewNpError(404, "Router not found!")
return nil, util.NewNpError(404, fmt.Sprintf("unsubscribe: Sub [%s] not found!", mid))
}

func trickle(msg map[string]interface{}) (map[string]interface{}, *nprotoo.Error) {
Expand All @@ -255,5 +255,5 @@ func trickle(msg map[string]interface{}) (map[string]interface{}, *nprotoo.Error
//t.(*transport.WebRTCTransport).AddCandidate(cand)
}

return nil, util.NewNpError(404, "Router not found!")
return nil, util.NewNpError(404, "trickle: WebRTCTransport not found!")
}
2 changes: 1 addition & 1 deletion pkg/proto/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func ParseMediaInfo(key string) (*MediaInfo, error) {
var info MediaInfo
arr := strings.Split(key, "/")
if len(arr) != 6 {
return nil, fmt.Errorf("Can‘t parse userinfo; [%s]", key)
return nil, fmt.Errorf("Can‘t parse mediainfo; [%s]", key)
}
info.DC = arr[0]
info.RID = arr[1]
Expand Down
60 changes: 35 additions & 25 deletions sdk/js/demo/src/Conference.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from "react";
import { Spin } from "antd";
import { LocalVideoView, MainVideoView, SmallVideoView } from "./videoview";


class Conference extends React.Component {
constructor() {
super();
Expand All @@ -27,7 +26,7 @@ class Conference extends React.Component {
client.off("stream-remove", this._handleAddStream);
};

_publish = async (type,codec) => {
_publish = async (type, codec) => {
const { client, settings } = this.props;
let stream = await client.publish({
codec: settings.codec,
Expand All @@ -41,17 +40,22 @@ class Conference extends React.Component {
};

cleanUp = () => {
let { localStream, localScreen } = this.state;
let { localStream, localScreen, streams } = this.state;
streams.map(async item => {
await this._unsubscribe(item);
});

if (localStream) this._unpublish(localStream);
if (localScreen) this._unpublish(localScreen);
this.setState({ localStream: null, localScreen: null });

this.setState({ localStream: null, localScreen: null, streams: [] });
};

_notification = (message, description) => {
notification.info({
message: message,
description: description,
placement: 'bottomRight',
placement: "bottomRight"
});
};

Expand All @@ -63,6 +67,13 @@ class Conference extends React.Component {
}
};

_unsubscribe = async item => {
const { client } = this.props;
if (item) {
await client.unsubscribe(item.rid, item.mid);
}
};

muteMediaTrack = (type, enabled) => {
let { localStream } = this.state;
let tracks = localStream.stream.getTracks();
Expand All @@ -89,9 +100,9 @@ class Conference extends React.Component {
}
}
this.setState({ localStream });
} catch(e){
} catch (e) {
console.log("handleLocalStream error => " + e);
_notification("publish/unpublish failed!", e)
_notification("publish/unpublish failed!", e);
}
};

Expand Down Expand Up @@ -126,7 +137,7 @@ class Conference extends React.Component {
let streams = this.state.streams;
let stream = await client.subscribe(rid, mid);
stream.info = info;
streams.push({ mid, stream });
streams.push({ mid, stream, rid });
this.setState({ streams });
};

Expand Down Expand Up @@ -217,24 +228,23 @@ class Conference extends React.Component {
)}
<div className="small-video-list-div">
<div className="small-video-list">
{streams.map((item, index) => {
return index > 0 ? (
<SmallVideoView
key={item.mid}
id={item.mid}
stream={item.stream}
videoCount={streams.length}
collapsed={this.props.collapsed}
index={index}
onClick={this._onChangeVideoPosition}
/>
) : (
<div />
);
})}
</div>
{streams.map((item, index) => {
return index > 0 ? (
<SmallVideoView
key={item.mid}
id={item.mid}
stream={item.stream}
videoCount={streams.length}
collapsed={this.props.collapsed}
index={index}
onClick={this._onChangeVideoPosition}
/>
) : (
<div />
);
})}
</div>
</div>

</div>
);
};
Expand Down
Loading

0 comments on commit f918725

Please sign in to comment.