首页 新闻 搜索 专区 学院

基于内存缓冲区的流媒体数据缓存排序

0
悬赏园豆:100 [待解决问题]

1.针对一个流媒体节目的单线程下载进行处理。

2.节目数据包无丢失有乱序,假设程序范围不超过10个数据包。

3.将收到的数据包直接写入缓冲区,缓冲区长度无限定

4.排好序的数据如果超过一定长度,如30kb(可设定),则输出到文件。

5.如果结束,将缓冲区中的数据写入文件。

 

希望大神可以给完整程序,如果有时间请加详解(总共200园豆必须给了100)

-Sandy的主页 -Sandy | 初学一级 | 园豆:102
提问于:2014-08-14 14:49
< >
分享
所有回答(1)
0

x写一个得花蛮多时间的…

Firen | 园豆:5385 (大侠五级) | 2014-08-14 15:30

会很麻烦吗?

支持(0) 反对(0) -Sandy | 园豆:102 (初学一级) | 2014-08-14 15:31

@-Sandy: 挺麻烦的。

支持(0) 反对(0) Firen | 园豆:5385 (大侠五级) | 2014-08-14 15:33

@Firen: 刚找了一个程序。但是文件夹挺乱的,我想有可能是。

//
//  StreamBuffer.cpp
//  NetworkStreamEmulator
//
//  Created by Yifei Zhou on 7/11/14.
//  Copyright (c) 2014 Yifei Zhou. All rights reserved.
//

#include "StreamBuffer.h"
#include <map>
#include <string>
#include <fstream>

#define DEBUG_OUTPUT

StreamBuffer::StreamBuffer(ofstream &t_handle)
:handle(t_handle)
{
    minSeq = LONG_MAX;
    maxSeq = LONG_MIN;
    
    bufferSize = 0;
    storedSize = 0;
    buffer.clear();

}

StreamBuffer::~StreamBuffer()
{
    bufferSize = 0;
    storedSize = 0;
    // Write Remaining data
    for (map<long, Frame>::iterator iter = buffer.begin(); iter != buffer.end(); iter++) {
        Frame f = iter->second;
        handle.seekp(f.offset);
        handle.write(f.data, f.size);
#ifdef DEBUG_OUTPUT
        cout << "Stored Leftover: Offset: " << f.offset << ", Size: " << f.size << endl;
#endif
    }
    buffer.clear();
    //handle.close();
}

long StreamBuffer::ReceiveFrame(const Frame *f)
{
    if (bufferSize <= MAX_BUFFER_SIZE) {
        buffer[f->seq] = *f;
        bufferSize += f->size;
        if (f->seq <= minSeq) {
            minSeq = f->seq;
        }
        if (f->seq >= maxSeq) {
            maxSeq = f->seq;
        }
    } else {
#ifdef DEBUG_OUTPUT
        cout << "Dropped: Offset: " << f->offset << ", Size: " << f->size << endl;
#endif
    }
    return bufferSize;
}


tuple<long, long, long> StreamBuffer::SortedData(long &bufferSize)
{
    long sortedSize;
    if (bufferSize >= MAX_WINDOW_SIZE) {
        // Quick Search
        for (long seq = maxSeq-MAX_WINDOW_SIZE/MTU; seq <= maxSeq; seq++) {
            // Fixed Windows Size
            sortedSize = 0;
            long t_seq = seq;
            while (sortedSize <= MAX_WINDOW_SIZE) {
                map<long, Frame>::iterator iter = buffer.find(t_seq);
                if (iter != buffer.end()) {
                    sortedSize += iter->second.size;
                    t_seq++;
                } else {
                    break;
                }
            }
            if (sortedSize >= MAX_WINDOW_SIZE || t_seq > maxSeq) {
                return make_tuple(seq, t_seq, sortedSize);
            }
        }
    }
    if (bufferSize >= MAX_BUFFER_SIZE) {
        // Deep Search
        for (long seq = minSeq; seq <= maxSeq; seq++) {
            // Fixed Windows Size
            sortedSize = 0;
            long t_seq = seq;
            while (sortedSize <= MAX_WINDOW_SIZE) {
                map<long, Frame>::iterator iter = buffer.find(t_seq);
                if (iter != buffer.end()) {
                    sortedSize += iter->second.size;
                    t_seq++;
                } else {
                    break;
                }
            }
            if (sortedSize >= MAX_WINDOW_SIZE || t_seq > maxSeq) {
                return make_tuple(seq, t_seq, sortedSize);
            }
        }
    }
    return make_tuple(-1, -1, 0);
}

long StreamBuffer::StoreSortedData(long &bufferSize)
{
    tuple<long, long, long> t = SortedData(bufferSize);
    long sortedSize = get<2>(t);
    const long startSeq = get<0>(t);
    const long endSeq = get<1>(t);
    
    if (sortedSize >= MAX_WINDOW_SIZE) {
        for (long i = startSeq; i < endSeq; i++) {
            Frame f = buffer.find(i)->second;
            handle.seekp(f.offset);
            handle.write(f.data, f.size);
#ifdef DEBUG_OUTPUT
            cout << "Stored: SEQ: " << i << ", Size: " << f.size << endl;
#endif
            buffer.erase(i);
        }
    }
    
    if (startSeq == minSeq) {
        minSeq = endSeq+1;
    }
    if (endSeq == maxSeq) {
        maxSeq = startSeq-1;
    }
    bufferSize -= sortedSize;
    storedSize += sortedSize;
    return sortedSize;
}

long StreamBuffer::StoredSize()
{
    return storedSize;
}

求鉴定(不全),如果是,我就回家钻研哈。

支持(0) 反对(0) -Sandy | 园豆:102 (初学一级) | 2014-08-14 15:36
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册