Repository: ossrs/srs
File: trunk/src/protocol/srs_sip_stack.cpp (C++)

Code before:
                //json_key[1] = "info"
                json_key[xml_layer] = key; 
                if (json_key[xml_layer] == "Item") {
                    // "Item" won't be the first layer (xml_layer is 0)
                    in_item_tag = xml_layer;
                    one_item.clear();
                }
                xml_layer++;  
            }

            p +=1;
            value_start = p;
        } else {
          p++;
        }
    }

    // std::map<string, string>::iterator it2;
    // for (it2 = json_map.begin(); it2 != json_map.end(); ++it2) {
    //     srs_trace("========%s:%s", it2->first.c_str(), it2->second.c_str());
    // }

    return srs_success;
}

srs_error_t SrsSipStack::parse_sdp(std::string sdp_msg, std::map<std::string, std::string> &json_map){
    /* SDP描述由许多文本行组成，文本行的格式为<类型>=<值>，<类型>是一个字母，<值>是结构化的文本串，其格式依<类型>而定。
     * ＜type＞=<value>[CRLF]
     * json_map[type]=value
     * json_map[type]=value1,value2,value3  if type is the same
     */
    srs_error_t err = srs_success;
    std::vector<string> pairs = srs_string_split(sdp_msg, SRS_RTSP_CRLF);
    std::vector<string>::iterator it;
    for (it = pairs.begin(); it != pairs.end(); ++it) {
        std::vector<string> type_value = srs_string_split(*it, "=");
        std::string mkey = type_value.at(0);
        std::string mvalue = type_value.at(1);
        //set map value
        if (!mkey.empty()){
            if (json_map.find(mkey) == json_map.end()){
                json_map[mkey] = mvalue;         
            }else{
                json_map[mkey] = json_map[mkey] + ","+mvalue;
            }    
        }
    }
    return err;
}
srs_error_t SrsSipStack::do_parse_request(SrsSipRequest* req, const char* recv_msg)
{

Review comment (suggestion):
Last night, I thought it was pretty good about this, but I always felt something was off.

After thinking about it today, let me talk about this issue:

1. SDP should actually be "on-the-spot unified parsing", there is no need to use a map to store and parse different values such as y and a separately. The parsed values should be stored in variables for later use, instead of parsing them again when needed. Currently, for GB28181's SDP, only y needs to be parsed. When parsing a, do we need to put several a values into a map and then parse them? Therefore, the map is unnecessary, and it won't be needed for parsing other specific content in the future.

2. Here, the map connects the same types with commas, but commas are also part of the SDP content, for example...
a=fmtp:97 profile-level-id=42c01f;max-fs=3600;max-mbps=108000
a=fmtp:98 profile-level-id=42c00b;max-fs=240;max-mbps=3600
a=imageattr:97 send [x=1280,y=720] recv [x=1280,y=720]
a=imageattr:98 send [x=320,y=180] recv [x=320,y=180]
3. Actually, the name of this function is not parse_sdp, at most it should be store_sdp_to_map.
4. Additionally, when using a map, remember to always find the value before retrieving it, to maintain good programming habits.

`TRANS_BY_GPT3`

Code after:
                json_key[xml_layer] = key; 
                if (json_key[xml_layer] == "Item") {
                    // "Item" won't be the first layer (xml_layer is 0)
                    in_item_tag = xml_layer;
                    one_item.clear();
                }
                xml_layer++;  
            }

            p +=1;
            value_start = p;
        } else {
          p++;
        }
    }

    // std::map<string, string>::iterator it2;
    // for (it2 = json_map.begin(); it2 != json_map.end(); ++it2) {
    //     srs_trace("========%s:%s", it2->first.c_str(), it2->second.c_str());
    // }

    return srs_success;
}

srs_error_t SrsSipStack::do_parse_request(SrsSipRequest* req, const char* recv_msg)
{
    srs_error_t err = srs_success;

    std::vector<std::string> header_body = srs_string_split(recv_msg, SRS_RTSP_CRLFCRLF);
    if (header_body.empty()){
        return srs_error_new(ERROR_GB28181_SIP_PRASE_FAILED, "parse reques message"); 
    }

    std::string header = header_body.at(0);
    //Must be added SRS_RTSP_CRLFCRLF in order to handle the last line header
    header += SRS_RTSP_CRLFCRLF; 
    std::string body = "";

    if (header_body.size() > 1){
       // SRS_RTSP_CRLFCRLF may exist in content body (h3c)
       string recv_str(recv_msg);
       body = recv_str.substr(recv_str.find(SRS_RTSP_CRLFCRLF) + strlen(SRS_RTSP_CRLFCRLF));
       //body =  header_body.at(1);
    }

    srs_info("sip: header=%s\n", header.c_str());
    srs_info("sip: body=%s\n", body.c_str());

    // parse one by one.
    char* start = (char*)header.c_str();
    char* end = start + header.size();